2 * Fujitu mb86a20s ISDB-T/ISDB-Tsb Module driver
4 * Copyright (C) 2010-2013 Mauro Carvalho Chehab
5 * Copyright (C) 2009-2010 Douglas Landgraf <dougsland@redhat.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation version 2.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
17 #include <linux/kernel.h>
18 #include <asm/div64.h>
20 #include "dvb_frontend.h"
25 enum mb86a20s_bandwidth {
27 MB86A20S_13SEG_PARTIAL = 1,
32 static u8 mb86a20s_subchannel[] = {
33 0xb0, 0xc0, 0xd0, 0xe0,
34 0xf0, 0x00, 0x10, 0x20,
37 struct mb86a20s_state {
38 struct i2c_adapter *i2c;
39 const struct mb86a20s_config *config;
42 struct dvb_frontend frontend;
45 enum mb86a20s_bandwidth bw;
49 u32 estimated_rate[NUM_LAYERS];
50 unsigned long get_strength_time;
60 #define BER_SAMPLING_RATE 1 /* Seconds */
63 * Initialization sequence: Use whatevere default values that PV SBTVD
64 * does on its initialisation, obtained via USB snoop
66 static struct regdata mb86a20s_init1[] = {
70 { 0x50, 0xd1 }, { 0x51, 0x20 },
73 static struct regdata mb86a20s_init2[] = {
74 { 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
78 { 0x04, 0x08 }, { 0x05, 0x03 },
79 { 0x04, 0x0e }, { 0x05, 0x00 },
80 { 0x04, 0x0f }, { 0x05, 0x37 },
81 { 0x04, 0x0b }, { 0x05, 0x78 },
82 { 0x04, 0x00 }, { 0x05, 0x00 },
83 { 0x04, 0x01 }, { 0x05, 0x1e },
84 { 0x04, 0x02 }, { 0x05, 0x07 },
85 { 0x04, 0x03 }, { 0x05, 0xd0 },
86 { 0x04, 0x09 }, { 0x05, 0x00 },
87 { 0x04, 0x0a }, { 0x05, 0xff },
88 { 0x04, 0x27 }, { 0x05, 0x00 },
89 { 0x04, 0x28 }, { 0x05, 0x00 },
90 { 0x04, 0x1e }, { 0x05, 0x00 },
91 { 0x04, 0x29 }, { 0x05, 0x64 },
92 { 0x04, 0x32 }, { 0x05, 0x02 },
93 { 0x04, 0x14 }, { 0x05, 0x02 },
94 { 0x04, 0x04 }, { 0x05, 0x00 },
95 { 0x04, 0x05 }, { 0x05, 0x22 },
96 { 0x04, 0x06 }, { 0x05, 0x0e },
97 { 0x04, 0x07 }, { 0x05, 0xd8 },
98 { 0x04, 0x12 }, { 0x05, 0x00 },
99 { 0x04, 0x13 }, { 0x05, 0xff },
100 { 0x04, 0x15 }, { 0x05, 0x4e },
101 { 0x04, 0x16 }, { 0x05, 0x20 },
104 * On this demod, when the bit count reaches the count below,
105 * it collects the bit error count. The bit counters are initialized
106 * to 65535 here. This warrants that all of them will be quickly
107 * calculated when device gets locked. As TMCC is parsed, the values
108 * will be adjusted later in the driver's code.
110 { 0x52, 0x01 }, /* Turn on BER before Viterbi */
111 { 0x50, 0xa7 }, { 0x51, 0x00 },
112 { 0x50, 0xa8 }, { 0x51, 0xff },
113 { 0x50, 0xa9 }, { 0x51, 0xff },
114 { 0x50, 0xaa }, { 0x51, 0x00 },
115 { 0x50, 0xab }, { 0x51, 0xff },
116 { 0x50, 0xac }, { 0x51, 0xff },
117 { 0x50, 0xad }, { 0x51, 0x00 },
118 { 0x50, 0xae }, { 0x51, 0xff },
119 { 0x50, 0xaf }, { 0x51, 0xff },
122 * On this demod, post BER counts blocks. When the count reaches the
123 * value below, it collects the block error count. The block counters
124 * are initialized to 127 here. This warrants that all of them will be
125 * quickly calculated when device gets locked. As TMCC is parsed, the
126 * values will be adjusted later in the driver's code.
128 { 0x5e, 0x07 }, /* Turn on BER after Viterbi */
129 { 0x50, 0xdc }, { 0x51, 0x00 },
130 { 0x50, 0xdd }, { 0x51, 0x7f },
131 { 0x50, 0xde }, { 0x51, 0x00 },
132 { 0x50, 0xdf }, { 0x51, 0x7f },
133 { 0x50, 0xe0 }, { 0x51, 0x00 },
134 { 0x50, 0xe1 }, { 0x51, 0x7f },
137 * On this demod, when the block count reaches the count below,
138 * it collects the block error count. The block counters are initialized
139 * to 127 here. This warrants that all of them will be quickly
140 * calculated when device gets locked. As TMCC is parsed, the values
141 * will be adjusted later in the driver's code.
143 { 0x50, 0xb0 }, { 0x51, 0x07 }, /* Enable PER */
144 { 0x50, 0xb2 }, { 0x51, 0x00 },
145 { 0x50, 0xb3 }, { 0x51, 0x7f },
146 { 0x50, 0xb4 }, { 0x51, 0x00 },
147 { 0x50, 0xb5 }, { 0x51, 0x7f },
148 { 0x50, 0xb6 }, { 0x51, 0x00 },
149 { 0x50, 0xb7 }, { 0x51, 0x7f },
151 { 0x50, 0x50 }, { 0x51, 0x02 }, /* MER manual mode */
152 { 0x50, 0x51 }, { 0x51, 0x04 }, /* MER symbol 4 */
153 { 0x45, 0x04 }, /* CN symbol 4 */
154 { 0x48, 0x04 }, /* CN manual mode */
156 { 0x50, 0xd6 }, { 0x51, 0x1f },
157 { 0x50, 0xd2 }, { 0x51, 0x03 },
158 { 0x50, 0xd7 }, { 0x51, 0xbf },
159 { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0xff },
160 { 0x28, 0x46 }, { 0x29, 0x00 }, { 0x2a, 0x1a }, { 0x2b, 0x0c },
162 { 0x04, 0x40 }, { 0x05, 0x00 },
163 { 0x28, 0x00 }, { 0x2b, 0x08 },
164 { 0x28, 0x05 }, { 0x2b, 0x00 },
166 { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x1f },
167 { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x18 },
168 { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x12 },
169 { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x30 },
170 { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x37 },
171 { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
172 { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x09 },
173 { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x06 },
174 { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x7b },
175 { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x76 },
176 { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x7d },
177 { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x08 },
178 { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0b },
179 { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
180 { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xf2 },
181 { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xf3 },
182 { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x05 },
183 { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
184 { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
185 { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xef },
186 { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xd8 },
187 { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xf1 },
188 { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x3d },
189 { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x94 },
190 { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0xba },
191 { 0x50, 0x1e }, { 0x51, 0x5d },
192 { 0x50, 0x22 }, { 0x51, 0x00 },
193 { 0x50, 0x23 }, { 0x51, 0xc8 },
194 { 0x50, 0x24 }, { 0x51, 0x00 },
195 { 0x50, 0x25 }, { 0x51, 0xf0 },
196 { 0x50, 0x26 }, { 0x51, 0x00 },
197 { 0x50, 0x27 }, { 0x51, 0xc3 },
198 { 0x50, 0x39 }, { 0x51, 0x02 },
201 { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
205 static struct regdata mb86a20s_reset_reception[] = {
212 static struct regdata mb86a20s_per_ber_reset[] = {
213 { 0x53, 0x00 }, /* pre BER Counter reset */
216 { 0x5f, 0x00 }, /* post BER Counter reset */
219 { 0x50, 0xb1 }, /* PER Counter reset */
225 * I2C read/write functions and macros
228 static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
229 u8 i2c_addr, u8 reg, u8 data)
231 u8 buf[] = { reg, data };
232 struct i2c_msg msg = {
233 .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
237 rc = i2c_transfer(state->i2c, &msg, 1);
239 dev_err(&state->i2c->dev,
240 "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
241 __func__, rc, reg, data);
248 static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
249 u8 i2c_addr, struct regdata *rd, int size)
253 for (i = 0; i < size; i++) {
254 rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
262 static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
267 struct i2c_msg msg[] = {
268 { .addr = i2c_addr, .flags = 0, .buf = ®, .len = 1 },
269 { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
272 rc = i2c_transfer(state->i2c, msg, 2);
275 dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
277 return (rc < 0) ? rc : -EIO;
283 #define mb86a20s_readreg(state, reg) \
284 mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
285 #define mb86a20s_writereg(state, reg, val) \
286 mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
287 #define mb86a20s_writeregdata(state, regdata) \
288 mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
289 regdata, ARRAY_SIZE(regdata))
292 * Ancillary internal routines (likely compiled inlined)
294 * The functions below assume that gateway lock has already obtained
297 static int mb86a20s_read_status(struct dvb_frontend *fe, enum fe_status *status)
299 struct mb86a20s_state *state = fe->demodulator_priv;
304 val = mb86a20s_readreg(state, 0x0a);
310 *status |= FE_HAS_SIGNAL;
313 *status |= FE_HAS_CARRIER;
316 *status |= FE_HAS_VITERBI;
319 *status |= FE_HAS_SYNC;
321 if (val >= 8) /* Maybe 9? */
322 *status |= FE_HAS_LOCK;
324 dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
325 __func__, *status, val);
330 static int mb86a20s_read_signal_strength(struct dvb_frontend *fe)
332 struct mb86a20s_state *state = fe->demodulator_priv;
333 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
335 unsigned rf_max, rf_min, rf;
337 if (state->get_strength_time &&
338 (!time_after(jiffies, state->get_strength_time)))
339 return c->strength.stat[0].uvalue;
341 /* Reset its value if an error happen */
342 c->strength.stat[0].uvalue = 0;
344 /* Does a binary search to get RF strength */
348 rf = (rf_max + rf_min) / 2;
349 rc = mb86a20s_writereg(state, 0x04, 0x1f);
352 rc = mb86a20s_writereg(state, 0x05, rf >> 8);
355 rc = mb86a20s_writereg(state, 0x04, 0x20);
358 rc = mb86a20s_writereg(state, 0x05, rf);
362 rc = mb86a20s_readreg(state, 0x02);
366 rf_min = (rf_max + rf_min) / 2;
368 rf_max = (rf_max + rf_min) / 2;
369 if (rf_max - rf_min < 4) {
370 rf = (rf_max + rf_min) / 2;
372 /* Rescale it from 2^12 (4096) to 2^16 */
373 rf = rf << (16 - 12);
377 dev_dbg(&state->i2c->dev,
378 "%s: signal strength = %d (%d < RF=%d < %d)\n",
379 __func__, rf, rf_min, rf >> 4, rf_max);
380 c->strength.stat[0].uvalue = rf;
381 state->get_strength_time = jiffies +
382 msecs_to_jiffies(1000);
388 static int mb86a20s_get_modulation(struct mb86a20s_state *state,
392 static unsigned char reg[] = {
393 [0] = 0x86, /* Layer A */
394 [1] = 0x8a, /* Layer B */
395 [2] = 0x8e, /* Layer C */
398 if (layer >= ARRAY_SIZE(reg))
400 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
403 rc = mb86a20s_readreg(state, 0x6e);
406 switch ((rc >> 4) & 0x07) {
420 static int mb86a20s_get_fec(struct mb86a20s_state *state,
425 static unsigned char reg[] = {
426 [0] = 0x87, /* Layer A */
427 [1] = 0x8b, /* Layer B */
428 [2] = 0x8f, /* Layer C */
431 if (layer >= ARRAY_SIZE(reg))
433 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
436 rc = mb86a20s_readreg(state, 0x6e);
439 switch ((rc >> 4) & 0x07) {
455 static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
459 int interleaving[] = {
463 static unsigned char reg[] = {
464 [0] = 0x88, /* Layer A */
465 [1] = 0x8c, /* Layer B */
466 [2] = 0x90, /* Layer C */
469 if (layer >= ARRAY_SIZE(reg))
471 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
474 rc = mb86a20s_readreg(state, 0x6e);
478 return interleaving[(rc >> 4) & 0x07];
481 static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
485 static unsigned char reg[] = {
486 [0] = 0x89, /* Layer A */
487 [1] = 0x8d, /* Layer B */
488 [2] = 0x91, /* Layer C */
491 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
493 if (layer >= ARRAY_SIZE(reg))
496 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
499 rc = mb86a20s_readreg(state, 0x6e);
502 count = (rc >> 4) & 0x0f;
504 dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
509 static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
511 struct mb86a20s_state *state = fe->demodulator_priv;
512 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
514 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
516 /* Fixed parameters */
517 c->delivery_system = SYS_ISDBT;
518 c->bandwidth_hz = 6000000;
520 /* Initialize values that will be later autodetected */
521 c->isdbt_layer_enabled = 0;
522 c->transmission_mode = TRANSMISSION_MODE_AUTO;
523 c->guard_interval = GUARD_INTERVAL_AUTO;
524 c->isdbt_sb_mode = 0;
525 c->isdbt_sb_segment_count = 0;
529 * Estimates the bit rate using the per-segment bit rate given by
530 * ABNT/NBR 15601 spec (table 4).
532 static u32 isdbt_rate[3][5][4] = {
534 { 280850, 312060, 330420, 340430 }, /* 1/2 */
535 { 374470, 416080, 440560, 453910 }, /* 2/3 */
536 { 421280, 468090, 495630, 510650 }, /* 3/4 */
537 { 468090, 520100, 550700, 567390 }, /* 5/6 */
538 { 491500, 546110, 578230, 595760 }, /* 7/8 */
540 { 561710, 624130, 660840, 680870 }, /* 1/2 */
541 { 748950, 832170, 881120, 907820 }, /* 2/3 */
542 { 842570, 936190, 991260, 1021300 }, /* 3/4 */
543 { 936190, 1040210, 1101400, 1134780 }, /* 5/6 */
544 { 983000, 1092220, 1156470, 1191520 }, /* 7/8 */
546 { 842570, 936190, 991260, 1021300 }, /* 1/2 */
547 { 1123430, 1248260, 1321680, 1361740 }, /* 2/3 */
548 { 1263860, 1404290, 1486900, 1531950 }, /* 3/4 */
549 { 1404290, 1560320, 1652110, 1702170 }, /* 5/6 */
550 { 1474500, 1638340, 1734710, 1787280 }, /* 7/8 */
554 static void mb86a20s_layer_bitrate(struct dvb_frontend *fe, u32 layer,
555 u32 modulation, u32 forward_error_correction,
559 struct mb86a20s_state *state = fe->demodulator_priv;
564 * If modulation/fec/guard is not detected, the default is
565 * to consider the lowest bit rate, to avoid taking too long time
568 switch (modulation) {
582 switch (forward_error_correction) {
602 switch (guard_interval) {
604 case GUARD_INTERVAL_1_4:
607 case GUARD_INTERVAL_1_8:
610 case GUARD_INTERVAL_1_16:
613 case GUARD_INTERVAL_1_32:
618 /* Samples BER at BER_SAMPLING_RATE seconds */
619 rate = isdbt_rate[mod][fec][guard] * segment * BER_SAMPLING_RATE;
621 /* Avoids sampling too quickly or to overflow the register */
624 else if (rate > (1 << 24) - 1)
625 rate = (1 << 24) - 1;
627 dev_dbg(&state->i2c->dev,
628 "%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n",
629 __func__, 'A' + layer,
630 segment * isdbt_rate[mod][fec][guard]/1000,
633 state->estimated_rate[layer] = rate;
636 static int mb86a20s_get_frontend(struct dvb_frontend *fe)
638 struct mb86a20s_state *state = fe->demodulator_priv;
639 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
642 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
644 /* Reset frontend cache to default values */
645 mb86a20s_reset_frontend_cache(fe);
647 /* Check for partial reception */
648 rc = mb86a20s_writereg(state, 0x6d, 0x85);
651 rc = mb86a20s_readreg(state, 0x6e);
654 c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
656 /* Get per-layer data */
658 for (layer = 0; layer < NUM_LAYERS; layer++) {
659 dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
660 __func__, 'A' + layer);
662 rc = mb86a20s_get_segment_count(state, layer);
664 goto noperlayer_error;
665 if (rc >= 0 && rc < 14) {
666 c->layer[layer].segment_count = rc;
668 c->layer[layer].segment_count = 0;
669 state->estimated_rate[layer] = 0;
672 c->isdbt_layer_enabled |= 1 << layer;
673 rc = mb86a20s_get_modulation(state, layer);
675 goto noperlayer_error;
676 dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
678 c->layer[layer].modulation = rc;
679 rc = mb86a20s_get_fec(state, layer);
681 goto noperlayer_error;
682 dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
684 c->layer[layer].fec = rc;
685 rc = mb86a20s_get_interleaving(state, layer);
687 goto noperlayer_error;
688 dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
690 c->layer[layer].interleaving = rc;
691 mb86a20s_layer_bitrate(fe, layer, c->layer[layer].modulation,
694 c->layer[layer].segment_count);
697 rc = mb86a20s_writereg(state, 0x6d, 0x84);
700 if ((rc & 0x60) == 0x20) {
701 c->isdbt_sb_mode = 1;
702 /* At least, one segment should exist */
703 if (!c->isdbt_sb_segment_count)
704 c->isdbt_sb_segment_count = 1;
707 /* Get transmission mode and guard interval */
708 rc = mb86a20s_readreg(state, 0x07);
711 c->transmission_mode = TRANSMISSION_MODE_AUTO;
712 if ((rc & 0x60) == 0x20) {
713 /* Only modes 2 and 3 are supported */
714 switch ((rc >> 2) & 0x03) {
716 c->transmission_mode = TRANSMISSION_MODE_4K;
719 c->transmission_mode = TRANSMISSION_MODE_8K;
723 c->guard_interval = GUARD_INTERVAL_AUTO;
725 /* Guard interval 1/32 is not supported */
728 c->guard_interval = GUARD_INTERVAL_1_4;
731 c->guard_interval = GUARD_INTERVAL_1_8;
734 c->guard_interval = GUARD_INTERVAL_1_16;
742 /* per-layer info is incomplete; discard all per-layer */
743 c->isdbt_layer_enabled = 0;
748 static int mb86a20s_reset_counters(struct dvb_frontend *fe)
750 struct mb86a20s_state *state = fe->demodulator_priv;
751 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
754 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
756 /* Reset the counters, if the channel changed */
757 if (state->last_frequency != c->frequency) {
758 memset(&c->cnr, 0, sizeof(c->cnr));
759 memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
760 memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
761 memset(&c->post_bit_error, 0, sizeof(c->post_bit_error));
762 memset(&c->post_bit_count, 0, sizeof(c->post_bit_count));
763 memset(&c->block_error, 0, sizeof(c->block_error));
764 memset(&c->block_count, 0, sizeof(c->block_count));
766 state->last_frequency = c->frequency;
769 /* Clear status for most stats */
771 /* BER/PER counter reset */
772 rc = mb86a20s_writeregdata(state, mb86a20s_per_ber_reset);
776 /* CNR counter reset */
777 rc = mb86a20s_readreg(state, 0x45);
781 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
784 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
788 /* MER counter reset */
789 rc = mb86a20s_writereg(state, 0x50, 0x50);
792 rc = mb86a20s_readreg(state, 0x51);
796 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
799 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
805 dev_err(&state->i2c->dev,
806 "%s: Can't reset FE statistics (error %d).\n",
812 static int mb86a20s_get_pre_ber(struct dvb_frontend *fe,
814 u32 *error, u32 *count)
816 struct mb86a20s_state *state = fe->demodulator_priv;
819 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
821 if (layer >= NUM_LAYERS)
824 /* Check if the BER measures are already available */
825 rc = mb86a20s_readreg(state, 0x54);
829 /* Check if data is available for that layer */
830 if (!(rc & (1 << layer))) {
831 dev_dbg(&state->i2c->dev,
832 "%s: preBER for layer %c is not available yet.\n",
833 __func__, 'A' + layer);
837 /* Read Bit Error Count */
838 rc = mb86a20s_readreg(state, 0x55 + layer * 3);
842 rc = mb86a20s_readreg(state, 0x56 + layer * 3);
846 rc = mb86a20s_readreg(state, 0x57 + layer * 3);
851 dev_dbg(&state->i2c->dev,
852 "%s: bit error before Viterbi for layer %c: %d.\n",
853 __func__, 'A' + layer, *error);
856 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
859 rc = mb86a20s_readreg(state, 0x51);
863 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
866 rc = mb86a20s_readreg(state, 0x51);
870 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
873 rc = mb86a20s_readreg(state, 0x51);
878 dev_dbg(&state->i2c->dev,
879 "%s: bit count before Viterbi for layer %c: %d.\n",
880 __func__, 'A' + layer, *count);
884 * As we get TMCC data from the frontend, we can better estimate the
885 * BER bit counters, in order to do the BER measure during a longer
886 * time. Use those data, if available, to update the bit count
890 if (state->estimated_rate[layer]
891 && state->estimated_rate[layer] != *count) {
892 dev_dbg(&state->i2c->dev,
893 "%s: updating layer %c preBER counter to %d.\n",
894 __func__, 'A' + layer, state->estimated_rate[layer]);
896 /* Turn off BER before Viterbi */
897 rc = mb86a20s_writereg(state, 0x52, 0x00);
899 /* Update counter for this layer */
900 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
903 rc = mb86a20s_writereg(state, 0x51,
904 state->estimated_rate[layer] >> 16);
907 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
910 rc = mb86a20s_writereg(state, 0x51,
911 state->estimated_rate[layer] >> 8);
914 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
917 rc = mb86a20s_writereg(state, 0x51,
918 state->estimated_rate[layer]);
922 /* Turn on BER before Viterbi */
923 rc = mb86a20s_writereg(state, 0x52, 0x01);
925 /* Reset all preBER counters */
926 rc = mb86a20s_writereg(state, 0x53, 0x00);
929 rc = mb86a20s_writereg(state, 0x53, 0x07);
931 /* Reset counter to collect new data */
932 rc = mb86a20s_readreg(state, 0x53);
936 rc = mb86a20s_writereg(state, 0x53, val & ~(1 << layer));
939 rc = mb86a20s_writereg(state, 0x53, val | (1 << layer));
945 static int mb86a20s_get_post_ber(struct dvb_frontend *fe,
947 u32 *error, u32 *count)
949 struct mb86a20s_state *state = fe->demodulator_priv;
950 u32 counter, collect_rate;
953 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
955 if (layer >= NUM_LAYERS)
958 /* Check if the BER measures are already available */
959 rc = mb86a20s_readreg(state, 0x60);
963 /* Check if data is available for that layer */
964 if (!(rc & (1 << layer))) {
965 dev_dbg(&state->i2c->dev,
966 "%s: post BER for layer %c is not available yet.\n",
967 __func__, 'A' + layer);
971 /* Read Bit Error Count */
972 rc = mb86a20s_readreg(state, 0x64 + layer * 3);
976 rc = mb86a20s_readreg(state, 0x65 + layer * 3);
980 rc = mb86a20s_readreg(state, 0x66 + layer * 3);
985 dev_dbg(&state->i2c->dev,
986 "%s: post bit error for layer %c: %d.\n",
987 __func__, 'A' + layer, *error);
990 rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
993 rc = mb86a20s_readreg(state, 0x51);
997 rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
1000 rc = mb86a20s_readreg(state, 0x51);
1004 *count = counter * 204 * 8;
1006 dev_dbg(&state->i2c->dev,
1007 "%s: post bit count for layer %c: %d.\n",
1008 __func__, 'A' + layer, *count);
1011 * As we get TMCC data from the frontend, we can better estimate the
1012 * BER bit counters, in order to do the BER measure during a longer
1013 * time. Use those data, if available, to update the bit count
1017 if (!state->estimated_rate[layer])
1018 goto reset_measurement;
1020 collect_rate = state->estimated_rate[layer] / 204 / 8;
1021 if (collect_rate < 32)
1023 if (collect_rate > 65535)
1024 collect_rate = 65535;
1025 if (collect_rate != counter) {
1026 dev_dbg(&state->i2c->dev,
1027 "%s: updating postBER counter on layer %c to %d.\n",
1028 __func__, 'A' + layer, collect_rate);
1030 /* Turn off BER after Viterbi */
1031 rc = mb86a20s_writereg(state, 0x5e, 0x00);
1033 /* Update counter for this layer */
1034 rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
1037 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1040 rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
1043 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1047 /* Turn on BER after Viterbi */
1048 rc = mb86a20s_writereg(state, 0x5e, 0x07);
1050 /* Reset all preBER counters */
1051 rc = mb86a20s_writereg(state, 0x5f, 0x00);
1054 rc = mb86a20s_writereg(state, 0x5f, 0x07);
1060 /* Reset counter to collect new data */
1061 rc = mb86a20s_readreg(state, 0x5f);
1065 rc = mb86a20s_writereg(state, 0x5f, val & ~(1 << layer));
1068 rc = mb86a20s_writereg(state, 0x5f, val | (1 << layer));
1073 static int mb86a20s_get_blk_error(struct dvb_frontend *fe,
1075 u32 *error, u32 *count)
1077 struct mb86a20s_state *state = fe->demodulator_priv;
1080 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1082 if (layer >= NUM_LAYERS)
1085 /* Check if the PER measures are already available */
1086 rc = mb86a20s_writereg(state, 0x50, 0xb8);
1089 rc = mb86a20s_readreg(state, 0x51);
1093 /* Check if data is available for that layer */
1095 if (!(rc & (1 << layer))) {
1096 dev_dbg(&state->i2c->dev,
1097 "%s: block counts for layer %c aren't available yet.\n",
1098 __func__, 'A' + layer);
1102 /* Read Packet error Count */
1103 rc = mb86a20s_writereg(state, 0x50, 0xb9 + layer * 2);
1106 rc = mb86a20s_readreg(state, 0x51);
1110 rc = mb86a20s_writereg(state, 0x50, 0xba + layer * 2);
1113 rc = mb86a20s_readreg(state, 0x51);
1117 dev_dbg(&state->i2c->dev, "%s: block error for layer %c: %d.\n",
1118 __func__, 'A' + layer, *error);
1120 /* Read Bit Count */
1121 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1124 rc = mb86a20s_readreg(state, 0x51);
1128 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1131 rc = mb86a20s_readreg(state, 0x51);
1136 dev_dbg(&state->i2c->dev,
1137 "%s: block count for layer %c: %d.\n",
1138 __func__, 'A' + layer, *count);
1141 * As we get TMCC data from the frontend, we can better estimate the
1142 * BER bit counters, in order to do the BER measure during a longer
1143 * time. Use those data, if available, to update the bit count
1147 if (!state->estimated_rate[layer])
1148 goto reset_measurement;
1150 collect_rate = state->estimated_rate[layer] / 204 / 8;
1151 if (collect_rate < 32)
1153 if (collect_rate > 65535)
1154 collect_rate = 65535;
1156 if (collect_rate != *count) {
1157 dev_dbg(&state->i2c->dev,
1158 "%s: updating PER counter on layer %c to %d.\n",
1159 __func__, 'A' + layer, collect_rate);
1161 /* Stop PER measurement */
1162 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1165 rc = mb86a20s_writereg(state, 0x51, 0x00);
1169 /* Update this layer's counter */
1170 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1173 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1176 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1179 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1183 /* start PER measurement */
1184 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1187 rc = mb86a20s_writereg(state, 0x51, 0x07);
1191 /* Reset all counters to collect new data */
1192 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1195 rc = mb86a20s_writereg(state, 0x51, 0x07);
1198 rc = mb86a20s_writereg(state, 0x51, 0x00);
1204 /* Reset counter to collect new data */
1205 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1208 rc = mb86a20s_readreg(state, 0x51);
1212 rc = mb86a20s_writereg(state, 0x51, val | (1 << layer));
1215 rc = mb86a20s_writereg(state, 0x51, val & ~(1 << layer));
1220 struct linear_segments {
1225 * All tables below return a dB/1000 measurement
1228 static const struct linear_segments cnr_to_db_table[] = {
1262 static const struct linear_segments cnr_64qam_table[] = {
1296 static const struct linear_segments cnr_16qam_table[] = {
1330 static const struct linear_segments cnr_qpsk_table[] = {
1364 static u32 interpolate_value(u32 value, const struct linear_segments *segments,
1371 if (value >= segments[0].x)
1372 return segments[0].y;
1373 if (value < segments[len-1].x)
1374 return segments[len-1].y;
1376 for (i = 1; i < len - 1; i++) {
1377 /* If value is identical, no need to interpolate */
1378 if (value == segments[i].x)
1379 return segments[i].y;
1380 if (value > segments[i].x)
1384 /* Linear interpolation between the two (x,y) points */
1385 dy = segments[i].y - segments[i - 1].y;
1386 dx = segments[i - 1].x - segments[i].x;
1387 tmp64 = value - segments[i].x;
1390 ret = segments[i].y - tmp64;
1395 static int mb86a20s_get_main_CNR(struct dvb_frontend *fe)
1397 struct mb86a20s_state *state = fe->demodulator_priv;
1398 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1399 u32 cnr_linear, cnr;
1402 /* Check if CNR is available */
1403 rc = mb86a20s_readreg(state, 0x45);
1408 dev_dbg(&state->i2c->dev, "%s: CNR is not available yet.\n",
1414 rc = mb86a20s_readreg(state, 0x46);
1417 cnr_linear = rc << 8;
1419 rc = mb86a20s_readreg(state, 0x46);
1424 cnr = interpolate_value(cnr_linear,
1425 cnr_to_db_table, ARRAY_SIZE(cnr_to_db_table));
1427 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
1428 c->cnr.stat[0].svalue = cnr;
1430 dev_dbg(&state->i2c->dev, "%s: CNR is %d.%03d dB (%d)\n",
1431 __func__, cnr / 1000, cnr % 1000, cnr_linear);
1433 /* CNR counter reset */
1434 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
1437 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
1442 static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend *fe)
1444 struct mb86a20s_state *state = fe->demodulator_priv;
1445 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1448 const struct linear_segments *segs;
1451 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1453 /* Check if the measures are already available */
1454 rc = mb86a20s_writereg(state, 0x50, 0x5b);
1457 rc = mb86a20s_readreg(state, 0x51);
1461 /* Check if data is available */
1463 dev_dbg(&state->i2c->dev,
1464 "%s: MER measures aren't available yet.\n", __func__);
1468 /* Read all layers */
1469 for (layer = 0; layer < NUM_LAYERS; layer++) {
1470 if (!(c->isdbt_layer_enabled & (1 << layer))) {
1471 c->cnr.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1475 rc = mb86a20s_writereg(state, 0x50, 0x52 + layer * 3);
1478 rc = mb86a20s_readreg(state, 0x51);
1482 rc = mb86a20s_writereg(state, 0x50, 0x53 + layer * 3);
1485 rc = mb86a20s_readreg(state, 0x51);
1489 rc = mb86a20s_writereg(state, 0x50, 0x54 + layer * 3);
1492 rc = mb86a20s_readreg(state, 0x51);
1497 switch (c->layer[layer].modulation) {
1500 segs = cnr_qpsk_table;
1501 segs_len = ARRAY_SIZE(cnr_qpsk_table);
1504 segs = cnr_16qam_table;
1505 segs_len = ARRAY_SIZE(cnr_16qam_table);
1509 segs = cnr_64qam_table;
1510 segs_len = ARRAY_SIZE(cnr_64qam_table);
1513 cnr = interpolate_value(mer, segs, segs_len);
1515 c->cnr.stat[1 + layer].scale = FE_SCALE_DECIBEL;
1516 c->cnr.stat[1 + layer].svalue = cnr;
1518 dev_dbg(&state->i2c->dev,
1519 "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
1520 __func__, 'A' + layer, cnr / 1000, cnr % 1000, mer);
1524 /* Start a new MER measurement */
1525 /* MER counter reset */
1526 rc = mb86a20s_writereg(state, 0x50, 0x50);
1529 rc = mb86a20s_readreg(state, 0x51);
1534 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
1537 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
1544 static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
1546 struct mb86a20s_state *state = fe->demodulator_priv;
1547 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1550 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1552 /* Fill the length of each status counter */
1554 /* Only global stats */
1555 c->strength.len = 1;
1557 /* Per-layer stats - 3 layers + global */
1558 c->cnr.len = NUM_LAYERS + 1;
1559 c->pre_bit_error.len = NUM_LAYERS + 1;
1560 c->pre_bit_count.len = NUM_LAYERS + 1;
1561 c->post_bit_error.len = NUM_LAYERS + 1;
1562 c->post_bit_count.len = NUM_LAYERS + 1;
1563 c->block_error.len = NUM_LAYERS + 1;
1564 c->block_count.len = NUM_LAYERS + 1;
1566 /* Signal is always available */
1567 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
1568 c->strength.stat[0].uvalue = 0;
1570 /* Put all of them at FE_SCALE_NOT_AVAILABLE */
1571 for (layer = 0; layer < NUM_LAYERS + 1; layer++) {
1572 c->cnr.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1573 c->pre_bit_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1574 c->pre_bit_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1575 c->post_bit_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1576 c->post_bit_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1577 c->block_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1578 c->block_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1582 static int mb86a20s_get_stats(struct dvb_frontend *fe, int status_nr)
1584 struct mb86a20s_state *state = fe->demodulator_priv;
1585 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1587 u32 bit_error = 0, bit_count = 0;
1588 u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
1589 u32 t_post_bit_error = 0, t_post_bit_count = 0;
1590 u32 block_error = 0, block_count = 0;
1591 u32 t_block_error = 0, t_block_count = 0;
1592 int active_layers = 0, pre_ber_layers = 0, post_ber_layers = 0;
1595 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1597 mb86a20s_get_main_CNR(fe);
1599 /* Get per-layer stats */
1600 mb86a20s_get_blk_error_layer_CNR(fe);
1603 * At state 7, only CNR is available
1604 * For BER measures, state=9 is required
1605 * FIXME: we may get MER measures with state=8
1610 for (layer = 0; layer < NUM_LAYERS; layer++) {
1611 if (c->isdbt_layer_enabled & (1 << layer)) {
1612 /* Layer is active and has rc segments */
1615 /* Handle BER before vterbi */
1616 rc = mb86a20s_get_pre_ber(fe, layer,
1617 &bit_error, &bit_count);
1619 c->pre_bit_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
1620 c->pre_bit_error.stat[1 + layer].uvalue += bit_error;
1621 c->pre_bit_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
1622 c->pre_bit_count.stat[1 + layer].uvalue += bit_count;
1623 } else if (rc != -EBUSY) {
1625 * If an I/O error happened,
1626 * measures are now unavailable
1628 c->pre_bit_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1629 c->pre_bit_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1630 dev_err(&state->i2c->dev,
1631 "%s: Can't get BER for layer %c (error %d).\n",
1632 __func__, 'A' + layer, rc);
1634 if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
1637 /* Handle BER post vterbi */
1638 rc = mb86a20s_get_post_ber(fe, layer,
1639 &bit_error, &bit_count);
1641 c->post_bit_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
1642 c->post_bit_error.stat[1 + layer].uvalue += bit_error;
1643 c->post_bit_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
1644 c->post_bit_count.stat[1 + layer].uvalue += bit_count;
1645 } else if (rc != -EBUSY) {
1647 * If an I/O error happened,
1648 * measures are now unavailable
1650 c->post_bit_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1651 c->post_bit_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1652 dev_err(&state->i2c->dev,
1653 "%s: Can't get BER for layer %c (error %d).\n",
1654 __func__, 'A' + layer, rc);
1656 if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
1659 /* Handle Block errors for PER/UCB reports */
1660 rc = mb86a20s_get_blk_error(fe, layer,
1664 c->block_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
1665 c->block_error.stat[1 + layer].uvalue += block_error;
1666 c->block_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
1667 c->block_count.stat[1 + layer].uvalue += block_count;
1668 } else if (rc != -EBUSY) {
1670 * If an I/O error happened,
1671 * measures are now unavailable
1673 c->block_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1674 c->block_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1675 dev_err(&state->i2c->dev,
1676 "%s: Can't get PER for layer %c (error %d).\n",
1677 __func__, 'A' + layer, rc);
1680 if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
1683 /* Update total preBER */
1684 t_pre_bit_error += c->pre_bit_error.stat[1 + layer].uvalue;
1685 t_pre_bit_count += c->pre_bit_count.stat[1 + layer].uvalue;
1687 /* Update total postBER */
1688 t_post_bit_error += c->post_bit_error.stat[1 + layer].uvalue;
1689 t_post_bit_count += c->post_bit_count.stat[1 + layer].uvalue;
1691 /* Update total PER */
1692 t_block_error += c->block_error.stat[1 + layer].uvalue;
1693 t_block_count += c->block_count.stat[1 + layer].uvalue;
1698 * Start showing global count if at least one error count is
1701 if (pre_ber_layers) {
1703 * At least one per-layer BER measure was read. We can now
1704 * calculate the total BER
1706 * Total Bit Error/Count is calculated as the sum of the
1707 * bit errors on all active layers.
1709 c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1710 c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
1711 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1712 c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
1714 c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1715 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1719 * Start showing global count if at least one error count is
1722 if (post_ber_layers) {
1724 * At least one per-layer BER measure was read. We can now
1725 * calculate the total BER
1727 * Total Bit Error/Count is calculated as the sum of the
1728 * bit errors on all active layers.
1730 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1731 c->post_bit_error.stat[0].uvalue = t_post_bit_error;
1732 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1733 c->post_bit_count.stat[0].uvalue = t_post_bit_count;
1735 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1736 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1741 * At least one per-layer UCB measure was read. We can now
1742 * calculate the total UCB
1744 * Total block Error/Count is calculated as the sum of the
1745 * block errors on all active layers.
1747 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1748 c->block_error.stat[0].uvalue = t_block_error;
1749 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1750 c->block_count.stat[0].uvalue = t_block_count;
1752 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1753 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1760 * The functions below are called via DVB callbacks, so they need to
1761 * properly use the I2C gate control
1764 static int mb86a20s_initfe(struct dvb_frontend *fe)
1766 struct mb86a20s_state *state = fe->demodulator_priv;
1770 u8 regD5 = 1, reg71, reg09 = 0x3a;
1772 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1774 if (fe->ops.i2c_gate_ctrl)
1775 fe->ops.i2c_gate_ctrl(fe, 0);
1777 /* Initialize the frontend */
1778 rc = mb86a20s_writeregdata(state, mb86a20s_init1);
1782 if (!state->inversion)
1784 rc = mb86a20s_writereg(state, 0x09, reg09);
1791 rc = mb86a20s_writereg(state, 0x39, reg71);
1794 rc = mb86a20s_writereg(state, 0x71, state->bw);
1797 if (state->subchannel) {
1798 rc = mb86a20s_writereg(state, 0x44, state->subchannel);
1803 fclk = state->config->fclk;
1807 /* Adjust IF frequency to match tuner */
1808 if (fe->ops.tuner_ops.get_if_frequency)
1809 fe->ops.tuner_ops.get_if_frequency(fe, &state->if_freq);
1811 if (!state->if_freq)
1812 state->if_freq = 3300000;
1814 pll = (((u64)1) << 34) * state->if_freq;
1815 do_div(pll, 63 * fclk);
1816 pll = (1 << 25) - pll;
1817 rc = mb86a20s_writereg(state, 0x28, 0x2a);
1820 rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
1823 rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
1826 rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
1829 dev_dbg(&state->i2c->dev, "%s: fclk=%d, IF=%d, clock reg=0x%06llx\n",
1830 __func__, fclk, state->if_freq, (long long)pll);
1832 /* pll = freq[Hz] * 2^24/10^6 / 16.285714286 */
1833 pll = state->if_freq * 1677721600L;
1834 do_div(pll, 1628571429L);
1835 rc = mb86a20s_writereg(state, 0x28, 0x20);
1838 rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
1841 rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
1844 rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
1847 dev_dbg(&state->i2c->dev, "%s: IF=%d, IF reg=0x%06llx\n",
1848 __func__, state->if_freq, (long long)pll);
1850 if (!state->config->is_serial)
1853 rc = mb86a20s_writereg(state, 0x50, 0xd5);
1856 rc = mb86a20s_writereg(state, 0x51, regD5);
1860 rc = mb86a20s_writeregdata(state, mb86a20s_init2);
1866 if (fe->ops.i2c_gate_ctrl)
1867 fe->ops.i2c_gate_ctrl(fe, 1);
1870 state->need_init = true;
1871 dev_info(&state->i2c->dev,
1872 "mb86a20s: Init failed. Will try again later\n");
1874 state->need_init = false;
1875 dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
1880 static int mb86a20s_set_frontend(struct dvb_frontend *fe)
1882 struct mb86a20s_state *state = fe->demodulator_priv;
1883 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1885 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1887 if (!c->isdbt_layer_enabled)
1888 c->isdbt_layer_enabled = 7;
1890 if (c->isdbt_layer_enabled == 1)
1891 state->bw = MB86A20S_1SEG;
1892 else if (c->isdbt_partial_reception)
1893 state->bw = MB86A20S_13SEG_PARTIAL;
1895 state->bw = MB86A20S_13SEG;
1897 if (c->inversion == INVERSION_ON)
1898 state->inversion = true;
1900 state->inversion = false;
1902 if (!c->isdbt_sb_mode) {
1903 state->subchannel = 0;
1905 if (c->isdbt_sb_subchannel >= ARRAY_SIZE(mb86a20s_subchannel))
1906 c->isdbt_sb_subchannel = 0;
1908 state->subchannel = mb86a20s_subchannel[c->isdbt_sb_subchannel];
1912 * Gate should already be opened, but it doesn't hurt to
1915 if (fe->ops.i2c_gate_ctrl)
1916 fe->ops.i2c_gate_ctrl(fe, 1);
1917 fe->ops.tuner_ops.set_params(fe);
1919 if (fe->ops.tuner_ops.get_if_frequency)
1920 fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
1923 * Make it more reliable: if, for some reason, the initial
1924 * device initialization doesn't happen, initialize it when
1925 * a SBTVD parameters are adjusted.
1927 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
1928 * the agc callback logic is not called during DVB attach time,
1929 * causing mb86a20s to not be initialized with Kworld SBTVD.
1930 * So, this hack is needed, in order to make Kworld SBTVD to work.
1932 * It is also needed to change the IF after the initial init.
1934 * HACK: Always init the frontend when set_frontend is called:
1935 * it was noticed that, on some devices, it fails to lock on a
1936 * different channel. So, it is better to reset everything, even
1937 * wasting some time, than to loose channel lock.
1939 mb86a20s_initfe(fe);
1941 if (fe->ops.i2c_gate_ctrl)
1942 fe->ops.i2c_gate_ctrl(fe, 0);
1944 rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
1945 mb86a20s_reset_counters(fe);
1946 mb86a20s_stats_not_ready(fe);
1948 if (fe->ops.i2c_gate_ctrl)
1949 fe->ops.i2c_gate_ctrl(fe, 1);
1954 static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
1955 enum fe_status *status)
1957 struct mb86a20s_state *state = fe->demodulator_priv;
1960 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1962 if (fe->ops.i2c_gate_ctrl)
1963 fe->ops.i2c_gate_ctrl(fe, 0);
1966 status_nr = mb86a20s_read_status(fe, status);
1967 if (status_nr < 7) {
1968 mb86a20s_stats_not_ready(fe);
1969 mb86a20s_reset_frontend_cache(fe);
1971 if (status_nr < 0) {
1972 dev_err(&state->i2c->dev,
1973 "%s: Can't read frontend lock status\n", __func__);
1977 /* Get signal strength */
1978 rc = mb86a20s_read_signal_strength(fe);
1980 dev_err(&state->i2c->dev,
1981 "%s: Can't reset VBER registers.\n", __func__);
1982 mb86a20s_stats_not_ready(fe);
1983 mb86a20s_reset_frontend_cache(fe);
1985 rc = 0; /* Status is OK */
1989 if (status_nr >= 7) {
1991 rc = mb86a20s_get_frontend(fe);
1993 dev_err(&state->i2c->dev,
1994 "%s: Can't get FE TMCC data.\n", __func__);
1995 rc = 0; /* Status is OK */
1999 /* Get statistics */
2000 rc = mb86a20s_get_stats(fe, status_nr);
2001 if (rc < 0 && rc != -EBUSY) {
2002 dev_err(&state->i2c->dev,
2003 "%s: Can't get FE statistics.\n", __func__);
2007 rc = 0; /* Don't return EBUSY to userspace */
2012 mb86a20s_stats_not_ready(fe);
2015 if (fe->ops.i2c_gate_ctrl)
2016 fe->ops.i2c_gate_ctrl(fe, 1);
2021 static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
2024 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2027 *strength = c->strength.stat[0].uvalue;
2032 static int mb86a20s_tune(struct dvb_frontend *fe,
2034 unsigned int mode_flags,
2035 unsigned int *delay,
2036 enum fe_status *status)
2038 struct mb86a20s_state *state = fe->demodulator_priv;
2041 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
2044 rc = mb86a20s_set_frontend(fe);
2046 if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
2047 mb86a20s_read_status_and_stats(fe, status);
2052 static void mb86a20s_release(struct dvb_frontend *fe)
2054 struct mb86a20s_state *state = fe->demodulator_priv;
2056 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
2061 static struct dvb_frontend_ops mb86a20s_ops;
2063 struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
2064 struct i2c_adapter *i2c)
2066 struct mb86a20s_state *state;
2069 dev_dbg(&i2c->dev, "%s called.\n", __func__);
2071 /* allocate memory for the internal state */
2072 state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
2073 if (state == NULL) {
2075 "%s: unable to allocate memory for state\n", __func__);
2079 /* setup the state */
2080 state->config = config;
2083 /* create dvb_frontend */
2084 memcpy(&state->frontend.ops, &mb86a20s_ops,
2085 sizeof(struct dvb_frontend_ops));
2086 state->frontend.demodulator_priv = state;
2088 /* Check if it is a mb86a20s frontend */
2089 rev = mb86a20s_readreg(state, 0);
2093 "Detected a Fujitsu mb86a20s frontend\n");
2096 "Frontend revision %d is unknown - aborting.\n",
2101 return &state->frontend;
2107 EXPORT_SYMBOL(mb86a20s_attach);
2109 static struct dvb_frontend_ops mb86a20s_ops = {
2110 .delsys = { SYS_ISDBT },
2111 /* Use dib8000 values per default */
2113 .name = "Fujitsu mb86A20s",
2114 .caps = FE_CAN_RECOVER |
2115 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
2116 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
2117 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
2118 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
2119 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
2120 /* Actually, those values depend on the used tuner */
2121 .frequency_min = 45000000,
2122 .frequency_max = 864000000,
2123 .frequency_stepsize = 62500,
2126 .release = mb86a20s_release,
2128 .init = mb86a20s_initfe,
2129 .set_frontend = mb86a20s_set_frontend,
2130 .read_status = mb86a20s_read_status_and_stats,
2131 .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
2132 .tune = mb86a20s_tune,
2135 MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
2136 MODULE_AUTHOR("Mauro Carvalho Chehab");
2137 MODULE_LICENSE("GPL");