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"
26 module_param(debug, int, 0644);
27 MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
29 enum mb86a20s_bandwidth {
31 MB86A20S_13SEG_PARTIAL = 1,
36 u8 mb86a20s_subchannel[] = {
37 0xb0, 0xc0, 0xd0, 0xe0,
38 0xf0, 0x00, 0x10, 0x20,
41 struct mb86a20s_state {
42 struct i2c_adapter *i2c;
43 const struct mb86a20s_config *config;
46 struct dvb_frontend frontend;
49 enum mb86a20s_bandwidth bw;
53 u32 estimated_rate[NUM_LAYERS];
54 unsigned long get_strength_time;
64 #define BER_SAMPLING_RATE 1 /* Seconds */
67 * Initialization sequence: Use whatevere default values that PV SBTVD
68 * does on its initialisation, obtained via USB snoop
70 static struct regdata mb86a20s_init1[] = {
74 { 0x50, 0xd1 }, { 0x51, 0x20 },
77 static struct regdata mb86a20s_init2[] = {
78 { 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
82 { 0x04, 0x08 }, { 0x05, 0x03 },
83 { 0x04, 0x0e }, { 0x05, 0x00 },
84 { 0x04, 0x0f }, { 0x05, 0x37 },
85 { 0x04, 0x0b }, { 0x05, 0x78 },
86 { 0x04, 0x00 }, { 0x05, 0x00 },
87 { 0x04, 0x01 }, { 0x05, 0x1e },
88 { 0x04, 0x02 }, { 0x05, 0x07 },
89 { 0x04, 0x03 }, { 0x05, 0xd0 },
90 { 0x04, 0x09 }, { 0x05, 0x00 },
91 { 0x04, 0x0a }, { 0x05, 0xff },
92 { 0x04, 0x27 }, { 0x05, 0x00 },
93 { 0x04, 0x28 }, { 0x05, 0x00 },
94 { 0x04, 0x1e }, { 0x05, 0x00 },
95 { 0x04, 0x29 }, { 0x05, 0x64 },
96 { 0x04, 0x32 }, { 0x05, 0x02 },
97 { 0x04, 0x14 }, { 0x05, 0x02 },
98 { 0x04, 0x04 }, { 0x05, 0x00 },
99 { 0x04, 0x05 }, { 0x05, 0x22 },
100 { 0x04, 0x06 }, { 0x05, 0x0e },
101 { 0x04, 0x07 }, { 0x05, 0xd8 },
102 { 0x04, 0x12 }, { 0x05, 0x00 },
103 { 0x04, 0x13 }, { 0x05, 0xff },
104 { 0x04, 0x15 }, { 0x05, 0x4e },
105 { 0x04, 0x16 }, { 0x05, 0x20 },
108 * On this demod, when the bit count reaches the count below,
109 * it collects the bit error count. The bit counters are initialized
110 * to 65535 here. This warrants that all of them will be quickly
111 * calculated when device gets locked. As TMCC is parsed, the values
112 * will be adjusted later in the driver's code.
114 { 0x52, 0x01 }, /* Turn on BER before Viterbi */
115 { 0x50, 0xa7 }, { 0x51, 0x00 },
116 { 0x50, 0xa8 }, { 0x51, 0xff },
117 { 0x50, 0xa9 }, { 0x51, 0xff },
118 { 0x50, 0xaa }, { 0x51, 0x00 },
119 { 0x50, 0xab }, { 0x51, 0xff },
120 { 0x50, 0xac }, { 0x51, 0xff },
121 { 0x50, 0xad }, { 0x51, 0x00 },
122 { 0x50, 0xae }, { 0x51, 0xff },
123 { 0x50, 0xaf }, { 0x51, 0xff },
126 * On this demod, post BER counts blocks. When the count reaches the
127 * value below, it collects the block error count. The block counters
128 * are initialized to 127 here. This warrants that all of them will be
129 * quickly calculated when device gets locked. As TMCC is parsed, the
130 * values will be adjusted later in the driver's code.
132 { 0x5e, 0x07 }, /* Turn on BER after Viterbi */
133 { 0x50, 0xdc }, { 0x51, 0x00 },
134 { 0x50, 0xdd }, { 0x51, 0x7f },
135 { 0x50, 0xde }, { 0x51, 0x00 },
136 { 0x50, 0xdf }, { 0x51, 0x7f },
137 { 0x50, 0xe0 }, { 0x51, 0x00 },
138 { 0x50, 0xe1 }, { 0x51, 0x7f },
141 * On this demod, when the block count reaches the count below,
142 * it collects the block error count. The block counters are initialized
143 * to 127 here. This warrants that all of them will be quickly
144 * calculated when device gets locked. As TMCC is parsed, the values
145 * will be adjusted later in the driver's code.
147 { 0x50, 0xb0 }, { 0x51, 0x07 }, /* Enable PER */
148 { 0x50, 0xb2 }, { 0x51, 0x00 },
149 { 0x50, 0xb3 }, { 0x51, 0x7f },
150 { 0x50, 0xb4 }, { 0x51, 0x00 },
151 { 0x50, 0xb5 }, { 0x51, 0x7f },
152 { 0x50, 0xb6 }, { 0x51, 0x00 },
153 { 0x50, 0xb7 }, { 0x51, 0x7f },
155 { 0x50, 0x50 }, { 0x51, 0x02 }, /* MER manual mode */
156 { 0x50, 0x51 }, { 0x51, 0x04 }, /* MER symbol 4 */
157 { 0x45, 0x04 }, /* CN symbol 4 */
158 { 0x48, 0x04 }, /* CN manual mode */
160 { 0x50, 0xd6 }, { 0x51, 0x1f },
161 { 0x50, 0xd2 }, { 0x51, 0x03 },
162 { 0x50, 0xd7 }, { 0x51, 0xbf },
163 { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0xff },
164 { 0x28, 0x46 }, { 0x29, 0x00 }, { 0x2a, 0x1a }, { 0x2b, 0x0c },
166 { 0x04, 0x40 }, { 0x05, 0x00 },
167 { 0x28, 0x00 }, { 0x2b, 0x08 },
168 { 0x28, 0x05 }, { 0x2b, 0x00 },
170 { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x1f },
171 { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x18 },
172 { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x12 },
173 { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x30 },
174 { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x37 },
175 { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
176 { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x09 },
177 { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x06 },
178 { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x7b },
179 { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x76 },
180 { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x7d },
181 { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x08 },
182 { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0b },
183 { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
184 { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xf2 },
185 { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xf3 },
186 { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x05 },
187 { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
188 { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
189 { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xef },
190 { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xd8 },
191 { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xf1 },
192 { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x3d },
193 { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x94 },
194 { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0xba },
195 { 0x50, 0x1e }, { 0x51, 0x5d },
196 { 0x50, 0x22 }, { 0x51, 0x00 },
197 { 0x50, 0x23 }, { 0x51, 0xc8 },
198 { 0x50, 0x24 }, { 0x51, 0x00 },
199 { 0x50, 0x25 }, { 0x51, 0xf0 },
200 { 0x50, 0x26 }, { 0x51, 0x00 },
201 { 0x50, 0x27 }, { 0x51, 0xc3 },
202 { 0x50, 0x39 }, { 0x51, 0x02 },
205 { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
209 static struct regdata mb86a20s_reset_reception[] = {
216 static struct regdata mb86a20s_per_ber_reset[] = {
217 { 0x53, 0x00 }, /* pre BER Counter reset */
220 { 0x5f, 0x00 }, /* post BER Counter reset */
223 { 0x50, 0xb1 }, /* PER Counter reset */
229 * I2C read/write functions and macros
232 static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
233 u8 i2c_addr, u8 reg, u8 data)
235 u8 buf[] = { reg, data };
236 struct i2c_msg msg = {
237 .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
241 rc = i2c_transfer(state->i2c, &msg, 1);
243 dev_err(&state->i2c->dev,
244 "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
245 __func__, rc, reg, data);
252 static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
253 u8 i2c_addr, struct regdata *rd, int size)
257 for (i = 0; i < size; i++) {
258 rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
266 static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
271 struct i2c_msg msg[] = {
272 { .addr = i2c_addr, .flags = 0, .buf = ®, .len = 1 },
273 { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
276 rc = i2c_transfer(state->i2c, msg, 2);
279 dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
281 return (rc < 0) ? rc : -EIO;
287 #define mb86a20s_readreg(state, reg) \
288 mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
289 #define mb86a20s_writereg(state, reg, val) \
290 mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
291 #define mb86a20s_writeregdata(state, regdata) \
292 mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
293 regdata, ARRAY_SIZE(regdata))
296 * Ancillary internal routines (likely compiled inlined)
298 * The functions below assume that gateway lock has already obtained
301 static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
303 struct mb86a20s_state *state = fe->demodulator_priv;
308 val = mb86a20s_readreg(state, 0x0a) & 0xf;
313 *status |= FE_HAS_SIGNAL;
316 *status |= FE_HAS_CARRIER;
319 *status |= FE_HAS_VITERBI;
322 *status |= FE_HAS_SYNC;
324 if (val >= 8) /* Maybe 9? */
325 *status |= FE_HAS_LOCK;
327 dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
328 __func__, *status, val);
333 static int mb86a20s_read_signal_strength(struct dvb_frontend *fe)
335 struct mb86a20s_state *state = fe->demodulator_priv;
336 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
338 unsigned rf_max, rf_min, rf;
340 if (state->get_strength_time &&
341 (!time_after(jiffies, state->get_strength_time)))
342 return c->strength.stat[0].uvalue;
344 /* Reset its value if an error happen */
345 c->strength.stat[0].uvalue = 0;
347 /* Does a binary search to get RF strength */
351 rf = (rf_max + rf_min) / 2;
352 rc = mb86a20s_writereg(state, 0x04, 0x1f);
355 rc = mb86a20s_writereg(state, 0x05, rf >> 8);
358 rc = mb86a20s_writereg(state, 0x04, 0x20);
361 rc = mb86a20s_writereg(state, 0x05, rf);
365 rc = mb86a20s_readreg(state, 0x02);
369 rf_min = (rf_max + rf_min) / 2;
371 rf_max = (rf_max + rf_min) / 2;
372 if (rf_max - rf_min < 4) {
373 rf = (rf_max + rf_min) / 2;
375 /* Rescale it from 2^12 (4096) to 2^16 */
376 rf = rf << (16 - 12);
380 dev_dbg(&state->i2c->dev,
381 "%s: signal strength = %d (%d < RF=%d < %d)\n",
382 __func__, rf, rf_min, rf >> 4, rf_max);
383 c->strength.stat[0].uvalue = rf;
384 state->get_strength_time = jiffies +
385 msecs_to_jiffies(1000);
391 static int mb86a20s_get_modulation(struct mb86a20s_state *state,
395 static unsigned char reg[] = {
396 [0] = 0x86, /* Layer A */
397 [1] = 0x8a, /* Layer B */
398 [2] = 0x8e, /* Layer C */
401 if (layer >= ARRAY_SIZE(reg))
403 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
406 rc = mb86a20s_readreg(state, 0x6e);
409 switch ((rc >> 4) & 0x07) {
423 static int mb86a20s_get_fec(struct mb86a20s_state *state,
428 static unsigned char reg[] = {
429 [0] = 0x87, /* Layer A */
430 [1] = 0x8b, /* Layer B */
431 [2] = 0x8f, /* Layer C */
434 if (layer >= ARRAY_SIZE(reg))
436 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
439 rc = mb86a20s_readreg(state, 0x6e);
442 switch ((rc >> 4) & 0x07) {
458 static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
462 int interleaving[] = {
466 static unsigned char reg[] = {
467 [0] = 0x88, /* Layer A */
468 [1] = 0x8c, /* Layer B */
469 [2] = 0x90, /* Layer C */
472 if (layer >= ARRAY_SIZE(reg))
474 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
477 rc = mb86a20s_readreg(state, 0x6e);
481 return interleaving[(rc >> 4) & 0x07];
484 static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
488 static unsigned char reg[] = {
489 [0] = 0x89, /* Layer A */
490 [1] = 0x8d, /* Layer B */
491 [2] = 0x91, /* Layer C */
494 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
496 if (layer >= ARRAY_SIZE(reg))
499 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
502 rc = mb86a20s_readreg(state, 0x6e);
505 count = (rc >> 4) & 0x0f;
507 dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
512 static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
514 struct mb86a20s_state *state = fe->demodulator_priv;
515 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
517 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
519 /* Fixed parameters */
520 c->delivery_system = SYS_ISDBT;
521 c->bandwidth_hz = 6000000;
523 /* Initialize values that will be later autodetected */
524 c->isdbt_layer_enabled = 0;
525 c->transmission_mode = TRANSMISSION_MODE_AUTO;
526 c->guard_interval = GUARD_INTERVAL_AUTO;
527 c->isdbt_sb_mode = 0;
528 c->isdbt_sb_segment_count = 0;
532 * Estimates the bit rate using the per-segment bit rate given by
533 * ABNT/NBR 15601 spec (table 4).
535 static u32 isdbt_rate[3][5][4] = {
537 { 280850, 312060, 330420, 340430 }, /* 1/2 */
538 { 374470, 416080, 440560, 453910 }, /* 2/3 */
539 { 421280, 468090, 495630, 510650 }, /* 3/4 */
540 { 468090, 520100, 550700, 567390 }, /* 5/6 */
541 { 491500, 546110, 578230, 595760 }, /* 7/8 */
543 { 561710, 624130, 660840, 680870 }, /* 1/2 */
544 { 748950, 832170, 881120, 907820 }, /* 2/3 */
545 { 842570, 936190, 991260, 1021300 }, /* 3/4 */
546 { 936190, 1040210, 1101400, 1134780 }, /* 5/6 */
547 { 983000, 1092220, 1156470, 1191520 }, /* 7/8 */
549 { 842570, 936190, 991260, 1021300 }, /* 1/2 */
550 { 1123430, 1248260, 1321680, 1361740 }, /* 2/3 */
551 { 1263860, 1404290, 1486900, 1531950 }, /* 3/4 */
552 { 1404290, 1560320, 1652110, 1702170 }, /* 5/6 */
553 { 1474500, 1638340, 1734710, 1787280 }, /* 7/8 */
557 static void mb86a20s_layer_bitrate(struct dvb_frontend *fe, u32 layer,
558 u32 modulation, u32 forward_error_correction,
562 struct mb86a20s_state *state = fe->demodulator_priv;
567 * If modulation/fec/guard is not detected, the default is
568 * to consider the lowest bit rate, to avoid taking too long time
571 switch (modulation) {
585 switch (forward_error_correction) {
605 switch (guard_interval) {
607 case GUARD_INTERVAL_1_4:
610 case GUARD_INTERVAL_1_8:
613 case GUARD_INTERVAL_1_16:
616 case GUARD_INTERVAL_1_32:
621 /* Samples BER at BER_SAMPLING_RATE seconds */
622 rate = isdbt_rate[mod][fec][guard] * segment * BER_SAMPLING_RATE;
624 /* Avoids sampling too quickly or to overflow the register */
627 else if (rate > (1 << 24) - 1)
628 rate = (1 << 24) - 1;
630 dev_dbg(&state->i2c->dev,
631 "%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n",
632 __func__, 'A' + layer,
633 segment * isdbt_rate[mod][fec][guard]/1000,
636 state->estimated_rate[layer] = rate;
639 static int mb86a20s_get_frontend(struct dvb_frontend *fe)
641 struct mb86a20s_state *state = fe->demodulator_priv;
642 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
645 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
647 /* Reset frontend cache to default values */
648 mb86a20s_reset_frontend_cache(fe);
650 /* Check for partial reception */
651 rc = mb86a20s_writereg(state, 0x6d, 0x85);
654 rc = mb86a20s_readreg(state, 0x6e);
657 c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
659 /* Get per-layer data */
661 for (layer = 0; layer < NUM_LAYERS; layer++) {
662 dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
663 __func__, 'A' + layer);
665 rc = mb86a20s_get_segment_count(state, layer);
667 goto noperlayer_error;
668 if (rc >= 0 && rc < 14) {
669 c->layer[layer].segment_count = rc;
671 c->layer[layer].segment_count = 0;
672 state->estimated_rate[layer] = 0;
675 c->isdbt_layer_enabled |= 1 << layer;
676 rc = mb86a20s_get_modulation(state, layer);
678 goto noperlayer_error;
679 dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
681 c->layer[layer].modulation = rc;
682 rc = mb86a20s_get_fec(state, layer);
684 goto noperlayer_error;
685 dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
687 c->layer[layer].fec = rc;
688 rc = mb86a20s_get_interleaving(state, layer);
690 goto noperlayer_error;
691 dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
693 c->layer[layer].interleaving = rc;
694 mb86a20s_layer_bitrate(fe, layer, c->layer[layer].modulation,
697 c->layer[layer].segment_count);
700 rc = mb86a20s_writereg(state, 0x6d, 0x84);
703 if ((rc & 0x60) == 0x20) {
704 c->isdbt_sb_mode = 1;
705 /* At least, one segment should exist */
706 if (!c->isdbt_sb_segment_count)
707 c->isdbt_sb_segment_count = 1;
710 /* Get transmission mode and guard interval */
711 rc = mb86a20s_readreg(state, 0x07);
714 c->transmission_mode = TRANSMISSION_MODE_AUTO;
715 if ((rc & 0x60) == 0x20) {
716 /* Only modes 2 and 3 are supported */
717 switch ((rc >> 2) & 0x03) {
719 c->transmission_mode = TRANSMISSION_MODE_4K;
722 c->transmission_mode = TRANSMISSION_MODE_8K;
726 c->guard_interval = GUARD_INTERVAL_AUTO;
728 /* Guard interval 1/32 is not supported */
731 c->guard_interval = GUARD_INTERVAL_1_4;
734 c->guard_interval = GUARD_INTERVAL_1_8;
737 c->guard_interval = GUARD_INTERVAL_1_16;
745 /* per-layer info is incomplete; discard all per-layer */
746 c->isdbt_layer_enabled = 0;
751 static int mb86a20s_reset_counters(struct dvb_frontend *fe)
753 struct mb86a20s_state *state = fe->demodulator_priv;
754 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
757 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
759 /* Reset the counters, if the channel changed */
760 if (state->last_frequency != c->frequency) {
761 memset(&c->cnr, 0, sizeof(c->cnr));
762 memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
763 memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
764 memset(&c->post_bit_error, 0, sizeof(c->post_bit_error));
765 memset(&c->post_bit_count, 0, sizeof(c->post_bit_count));
766 memset(&c->block_error, 0, sizeof(c->block_error));
767 memset(&c->block_count, 0, sizeof(c->block_count));
769 state->last_frequency = c->frequency;
772 /* Clear status for most stats */
774 /* BER/PER counter reset */
775 rc = mb86a20s_writeregdata(state, mb86a20s_per_ber_reset);
779 /* CNR counter reset */
780 rc = mb86a20s_readreg(state, 0x45);
784 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
787 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
791 /* MER counter reset */
792 rc = mb86a20s_writereg(state, 0x50, 0x50);
795 rc = mb86a20s_readreg(state, 0x51);
799 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
802 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
808 dev_err(&state->i2c->dev,
809 "%s: Can't reset FE statistics (error %d).\n",
815 static int mb86a20s_get_pre_ber(struct dvb_frontend *fe,
817 u32 *error, u32 *count)
819 struct mb86a20s_state *state = fe->demodulator_priv;
822 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
824 if (layer >= NUM_LAYERS)
827 /* Check if the BER measures are already available */
828 rc = mb86a20s_readreg(state, 0x54);
832 /* Check if data is available for that layer */
833 if (!(rc & (1 << layer))) {
834 dev_dbg(&state->i2c->dev,
835 "%s: preBER for layer %c is not available yet.\n",
836 __func__, 'A' + layer);
840 /* Read Bit Error Count */
841 rc = mb86a20s_readreg(state, 0x55 + layer * 3);
845 rc = mb86a20s_readreg(state, 0x56 + layer * 3);
849 rc = mb86a20s_readreg(state, 0x57 + layer * 3);
854 dev_dbg(&state->i2c->dev,
855 "%s: bit error before Viterbi for layer %c: %d.\n",
856 __func__, 'A' + layer, *error);
859 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
862 rc = mb86a20s_readreg(state, 0x51);
866 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
869 rc = mb86a20s_readreg(state, 0x51);
873 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
876 rc = mb86a20s_readreg(state, 0x51);
881 dev_dbg(&state->i2c->dev,
882 "%s: bit count before Viterbi for layer %c: %d.\n",
883 __func__, 'A' + layer, *count);
887 * As we get TMCC data from the frontend, we can better estimate the
888 * BER bit counters, in order to do the BER measure during a longer
889 * time. Use those data, if available, to update the bit count
893 if (state->estimated_rate[layer]
894 && state->estimated_rate[layer] != *count) {
895 dev_dbg(&state->i2c->dev,
896 "%s: updating layer %c preBER counter to %d.\n",
897 __func__, 'A' + layer, state->estimated_rate[layer]);
899 /* Turn off BER before Viterbi */
900 rc = mb86a20s_writereg(state, 0x52, 0x00);
902 /* Update counter for this layer */
903 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
906 rc = mb86a20s_writereg(state, 0x51,
907 state->estimated_rate[layer] >> 16);
910 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
913 rc = mb86a20s_writereg(state, 0x51,
914 state->estimated_rate[layer] >> 8);
917 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
920 rc = mb86a20s_writereg(state, 0x51,
921 state->estimated_rate[layer]);
925 /* Turn on BER before Viterbi */
926 rc = mb86a20s_writereg(state, 0x52, 0x01);
928 /* Reset all preBER counters */
929 rc = mb86a20s_writereg(state, 0x53, 0x00);
932 rc = mb86a20s_writereg(state, 0x53, 0x07);
934 /* Reset counter to collect new data */
935 rc = mb86a20s_readreg(state, 0x53);
939 rc = mb86a20s_writereg(state, 0x53, val & ~(1 << layer));
942 rc = mb86a20s_writereg(state, 0x53, val | (1 << layer));
948 static int mb86a20s_get_post_ber(struct dvb_frontend *fe,
950 u32 *error, u32 *count)
952 struct mb86a20s_state *state = fe->demodulator_priv;
953 u32 counter, collect_rate;
956 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
958 if (layer >= NUM_LAYERS)
961 /* Check if the BER measures are already available */
962 rc = mb86a20s_readreg(state, 0x60);
966 /* Check if data is available for that layer */
967 if (!(rc & (1 << layer))) {
968 dev_dbg(&state->i2c->dev,
969 "%s: post BER for layer %c is not available yet.\n",
970 __func__, 'A' + layer);
974 /* Read Bit Error Count */
975 rc = mb86a20s_readreg(state, 0x64 + layer * 3);
979 rc = mb86a20s_readreg(state, 0x65 + layer * 3);
983 rc = mb86a20s_readreg(state, 0x66 + layer * 3);
988 dev_dbg(&state->i2c->dev,
989 "%s: post bit error for layer %c: %d.\n",
990 __func__, 'A' + layer, *error);
993 rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
996 rc = mb86a20s_readreg(state, 0x51);
1000 rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
1003 rc = mb86a20s_readreg(state, 0x51);
1007 *count = counter * 204 * 8;
1009 dev_dbg(&state->i2c->dev,
1010 "%s: post bit count for layer %c: %d.\n",
1011 __func__, 'A' + layer, *count);
1014 * As we get TMCC data from the frontend, we can better estimate the
1015 * BER bit counters, in order to do the BER measure during a longer
1016 * time. Use those data, if available, to update the bit count
1020 if (!state->estimated_rate[layer])
1021 goto reset_measurement;
1023 collect_rate = state->estimated_rate[layer] / 204 / 8;
1024 if (collect_rate < 32)
1026 if (collect_rate > 65535)
1027 collect_rate = 65535;
1028 if (collect_rate != counter) {
1029 dev_dbg(&state->i2c->dev,
1030 "%s: updating postBER counter on layer %c to %d.\n",
1031 __func__, 'A' + layer, collect_rate);
1033 /* Turn off BER after Viterbi */
1034 rc = mb86a20s_writereg(state, 0x5e, 0x00);
1036 /* Update counter for this layer */
1037 rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
1040 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1043 rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
1046 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1050 /* Turn on BER after Viterbi */
1051 rc = mb86a20s_writereg(state, 0x5e, 0x07);
1053 /* Reset all preBER counters */
1054 rc = mb86a20s_writereg(state, 0x5f, 0x00);
1057 rc = mb86a20s_writereg(state, 0x5f, 0x07);
1063 /* Reset counter to collect new data */
1064 rc = mb86a20s_readreg(state, 0x5f);
1068 rc = mb86a20s_writereg(state, 0x5f, val & ~(1 << layer));
1071 rc = mb86a20s_writereg(state, 0x5f, val | (1 << layer));
1076 static int mb86a20s_get_blk_error(struct dvb_frontend *fe,
1078 u32 *error, u32 *count)
1080 struct mb86a20s_state *state = fe->demodulator_priv;
1083 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1085 if (layer >= NUM_LAYERS)
1088 /* Check if the PER measures are already available */
1089 rc = mb86a20s_writereg(state, 0x50, 0xb8);
1092 rc = mb86a20s_readreg(state, 0x51);
1096 /* Check if data is available for that layer */
1098 if (!(rc & (1 << layer))) {
1099 dev_dbg(&state->i2c->dev,
1100 "%s: block counts for layer %c aren't available yet.\n",
1101 __func__, 'A' + layer);
1105 /* Read Packet error Count */
1106 rc = mb86a20s_writereg(state, 0x50, 0xb9 + layer * 2);
1109 rc = mb86a20s_readreg(state, 0x51);
1113 rc = mb86a20s_writereg(state, 0x50, 0xba + layer * 2);
1116 rc = mb86a20s_readreg(state, 0x51);
1120 dev_dbg(&state->i2c->dev, "%s: block error for layer %c: %d.\n",
1121 __func__, 'A' + layer, *error);
1123 /* Read Bit Count */
1124 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1127 rc = mb86a20s_readreg(state, 0x51);
1131 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1134 rc = mb86a20s_readreg(state, 0x51);
1139 dev_dbg(&state->i2c->dev,
1140 "%s: block count for layer %c: %d.\n",
1141 __func__, 'A' + layer, *count);
1144 * As we get TMCC data from the frontend, we can better estimate the
1145 * BER bit counters, in order to do the BER measure during a longer
1146 * time. Use those data, if available, to update the bit count
1150 if (!state->estimated_rate[layer])
1151 goto reset_measurement;
1153 collect_rate = state->estimated_rate[layer] / 204 / 8;
1154 if (collect_rate < 32)
1156 if (collect_rate > 65535)
1157 collect_rate = 65535;
1159 if (collect_rate != *count) {
1160 dev_dbg(&state->i2c->dev,
1161 "%s: updating PER counter on layer %c to %d.\n",
1162 __func__, 'A' + layer, collect_rate);
1164 /* Stop PER measurement */
1165 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1168 rc = mb86a20s_writereg(state, 0x51, 0x00);
1172 /* Update this layer's counter */
1173 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1176 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1179 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1182 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1186 /* start PER measurement */
1187 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1190 rc = mb86a20s_writereg(state, 0x51, 0x07);
1194 /* Reset all counters to collect new data */
1195 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1198 rc = mb86a20s_writereg(state, 0x51, 0x07);
1201 rc = mb86a20s_writereg(state, 0x51, 0x00);
1207 /* Reset counter to collect new data */
1208 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1211 rc = mb86a20s_readreg(state, 0x51);
1215 rc = mb86a20s_writereg(state, 0x51, val | (1 << layer));
1218 rc = mb86a20s_writereg(state, 0x51, val & ~(1 << layer));
1223 struct linear_segments {
1228 * All tables below return a dB/1000 measurement
1231 static struct linear_segments cnr_to_db_table[] = {
1265 static struct linear_segments cnr_64qam_table[] = {
1299 static struct linear_segments cnr_16qam_table[] = {
1333 struct linear_segments cnr_qpsk_table[] = {
1367 static u32 interpolate_value(u32 value, struct linear_segments *segments,
1374 if (value >= segments[0].x)
1375 return segments[0].y;
1376 if (value < segments[len-1].x)
1377 return segments[len-1].y;
1379 for (i = 1; i < len - 1; i++) {
1380 /* If value is identical, no need to interpolate */
1381 if (value == segments[i].x)
1382 return segments[i].y;
1383 if (value > segments[i].x)
1387 /* Linear interpolation between the two (x,y) points */
1388 dy = segments[i].y - segments[i - 1].y;
1389 dx = segments[i - 1].x - segments[i].x;
1390 tmp64 = value - segments[i].x;
1393 ret = segments[i].y - tmp64;
1398 static int mb86a20s_get_main_CNR(struct dvb_frontend *fe)
1400 struct mb86a20s_state *state = fe->demodulator_priv;
1401 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1402 u32 cnr_linear, cnr;
1405 /* Check if CNR is available */
1406 rc = mb86a20s_readreg(state, 0x45);
1411 dev_dbg(&state->i2c->dev, "%s: CNR is not available yet.\n",
1417 rc = mb86a20s_readreg(state, 0x46);
1420 cnr_linear = rc << 8;
1422 rc = mb86a20s_readreg(state, 0x46);
1427 cnr = interpolate_value(cnr_linear,
1428 cnr_to_db_table, ARRAY_SIZE(cnr_to_db_table));
1430 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
1431 c->cnr.stat[0].svalue = cnr;
1433 dev_dbg(&state->i2c->dev, "%s: CNR is %d.%03d dB (%d)\n",
1434 __func__, cnr / 1000, cnr % 1000, cnr_linear);
1436 /* CNR counter reset */
1437 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
1440 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
1445 static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend *fe)
1447 struct mb86a20s_state *state = fe->demodulator_priv;
1448 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1451 struct linear_segments *segs;
1454 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1456 /* Check if the measures are already available */
1457 rc = mb86a20s_writereg(state, 0x50, 0x5b);
1460 rc = mb86a20s_readreg(state, 0x51);
1464 /* Check if data is available */
1466 dev_dbg(&state->i2c->dev,
1467 "%s: MER measures aren't available yet.\n", __func__);
1471 /* Read all layers */
1472 for (layer = 0; layer < NUM_LAYERS; layer++) {
1473 if (!(c->isdbt_layer_enabled & (1 << layer))) {
1474 c->cnr.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1478 rc = mb86a20s_writereg(state, 0x50, 0x52 + layer * 3);
1481 rc = mb86a20s_readreg(state, 0x51);
1485 rc = mb86a20s_writereg(state, 0x50, 0x53 + layer * 3);
1488 rc = mb86a20s_readreg(state, 0x51);
1492 rc = mb86a20s_writereg(state, 0x50, 0x54 + layer * 3);
1495 rc = mb86a20s_readreg(state, 0x51);
1500 switch (c->layer[layer].modulation) {
1503 segs = cnr_qpsk_table;
1504 segs_len = ARRAY_SIZE(cnr_qpsk_table);
1507 segs = cnr_16qam_table;
1508 segs_len = ARRAY_SIZE(cnr_16qam_table);
1512 segs = cnr_64qam_table;
1513 segs_len = ARRAY_SIZE(cnr_64qam_table);
1516 cnr = interpolate_value(mer, segs, segs_len);
1518 c->cnr.stat[1 + layer].scale = FE_SCALE_DECIBEL;
1519 c->cnr.stat[1 + layer].svalue = cnr;
1521 dev_dbg(&state->i2c->dev,
1522 "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
1523 __func__, 'A' + layer, cnr / 1000, cnr % 1000, mer);
1527 /* Start a new MER measurement */
1528 /* MER counter reset */
1529 rc = mb86a20s_writereg(state, 0x50, 0x50);
1532 rc = mb86a20s_readreg(state, 0x51);
1537 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
1540 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
1547 static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
1549 struct mb86a20s_state *state = fe->demodulator_priv;
1550 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1553 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1555 /* Fill the length of each status counter */
1557 /* Only global stats */
1558 c->strength.len = 1;
1560 /* Per-layer stats - 3 layers + global */
1561 c->cnr.len = NUM_LAYERS + 1;
1562 c->pre_bit_error.len = NUM_LAYERS + 1;
1563 c->pre_bit_count.len = NUM_LAYERS + 1;
1564 c->post_bit_error.len = NUM_LAYERS + 1;
1565 c->post_bit_count.len = NUM_LAYERS + 1;
1566 c->block_error.len = NUM_LAYERS + 1;
1567 c->block_count.len = NUM_LAYERS + 1;
1569 /* Signal is always available */
1570 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
1571 c->strength.stat[0].uvalue = 0;
1573 /* Put all of them at FE_SCALE_NOT_AVAILABLE */
1574 for (layer = 0; layer < NUM_LAYERS + 1; layer++) {
1575 c->cnr.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1576 c->pre_bit_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1577 c->pre_bit_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1578 c->post_bit_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1579 c->post_bit_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1580 c->block_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1581 c->block_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1585 static int mb86a20s_get_stats(struct dvb_frontend *fe, int status_nr)
1587 struct mb86a20s_state *state = fe->demodulator_priv;
1588 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1590 u32 bit_error = 0, bit_count = 0;
1591 u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
1592 u32 t_post_bit_error = 0, t_post_bit_count = 0;
1593 u32 block_error = 0, block_count = 0;
1594 u32 t_block_error = 0, t_block_count = 0;
1595 int active_layers = 0, pre_ber_layers = 0, post_ber_layers = 0;
1598 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1600 mb86a20s_get_main_CNR(fe);
1602 /* Get per-layer stats */
1603 mb86a20s_get_blk_error_layer_CNR(fe);
1606 * At state 7, only CNR is available
1607 * For BER measures, state=9 is required
1608 * FIXME: we may get MER measures with state=8
1613 for (layer = 0; layer < NUM_LAYERS; layer++) {
1614 if (c->isdbt_layer_enabled & (1 << layer)) {
1615 /* Layer is active and has rc segments */
1618 /* Handle BER before vterbi */
1619 rc = mb86a20s_get_pre_ber(fe, layer,
1620 &bit_error, &bit_count);
1622 c->pre_bit_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
1623 c->pre_bit_error.stat[1 + layer].uvalue += bit_error;
1624 c->pre_bit_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
1625 c->pre_bit_count.stat[1 + layer].uvalue += bit_count;
1626 } else if (rc != -EBUSY) {
1628 * If an I/O error happened,
1629 * measures are now unavailable
1631 c->pre_bit_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1632 c->pre_bit_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1633 dev_err(&state->i2c->dev,
1634 "%s: Can't get BER for layer %c (error %d).\n",
1635 __func__, 'A' + layer, rc);
1637 if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
1640 /* Handle BER post vterbi */
1641 rc = mb86a20s_get_post_ber(fe, layer,
1642 &bit_error, &bit_count);
1644 c->post_bit_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
1645 c->post_bit_error.stat[1 + layer].uvalue += bit_error;
1646 c->post_bit_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
1647 c->post_bit_count.stat[1 + layer].uvalue += bit_count;
1648 } else if (rc != -EBUSY) {
1650 * If an I/O error happened,
1651 * measures are now unavailable
1653 c->post_bit_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1654 c->post_bit_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1655 dev_err(&state->i2c->dev,
1656 "%s: Can't get BER for layer %c (error %d).\n",
1657 __func__, 'A' + layer, rc);
1659 if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
1662 /* Handle Block errors for PER/UCB reports */
1663 rc = mb86a20s_get_blk_error(fe, layer,
1667 c->block_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
1668 c->block_error.stat[1 + layer].uvalue += block_error;
1669 c->block_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
1670 c->block_count.stat[1 + layer].uvalue += block_count;
1671 } else if (rc != -EBUSY) {
1673 * If an I/O error happened,
1674 * measures are now unavailable
1676 c->block_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1677 c->block_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1678 dev_err(&state->i2c->dev,
1679 "%s: Can't get PER for layer %c (error %d).\n",
1680 __func__, 'A' + layer, rc);
1683 if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
1686 /* Update total preBER */
1687 t_pre_bit_error += c->pre_bit_error.stat[1 + layer].uvalue;
1688 t_pre_bit_count += c->pre_bit_count.stat[1 + layer].uvalue;
1690 /* Update total postBER */
1691 t_post_bit_error += c->post_bit_error.stat[1 + layer].uvalue;
1692 t_post_bit_count += c->post_bit_count.stat[1 + layer].uvalue;
1694 /* Update total PER */
1695 t_block_error += c->block_error.stat[1 + layer].uvalue;
1696 t_block_count += c->block_count.stat[1 + layer].uvalue;
1701 * Start showing global count if at least one error count is
1704 if (pre_ber_layers) {
1706 * At least one per-layer BER measure was read. We can now
1707 * calculate the total BER
1709 * Total Bit Error/Count is calculated as the sum of the
1710 * bit errors on all active layers.
1712 c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1713 c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
1714 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1715 c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
1717 c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1718 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1722 * Start showing global count if at least one error count is
1725 if (post_ber_layers) {
1727 * At least one per-layer BER measure was read. We can now
1728 * calculate the total BER
1730 * Total Bit Error/Count is calculated as the sum of the
1731 * bit errors on all active layers.
1733 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1734 c->post_bit_error.stat[0].uvalue = t_post_bit_error;
1735 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1736 c->post_bit_count.stat[0].uvalue = t_post_bit_count;
1738 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1739 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1744 * At least one per-layer UCB measure was read. We can now
1745 * calculate the total UCB
1747 * Total block Error/Count is calculated as the sum of the
1748 * block errors on all active layers.
1750 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1751 c->block_error.stat[0].uvalue = t_block_error;
1752 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1753 c->block_count.stat[0].uvalue = t_block_count;
1755 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1756 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1763 * The functions below are called via DVB callbacks, so they need to
1764 * properly use the I2C gate control
1767 static int mb86a20s_initfe(struct dvb_frontend *fe)
1769 struct mb86a20s_state *state = fe->demodulator_priv;
1773 u8 regD5 = 1, reg71, reg09 = 0x3a;
1775 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1777 if (fe->ops.i2c_gate_ctrl)
1778 fe->ops.i2c_gate_ctrl(fe, 0);
1780 /* Initialize the frontend */
1781 rc = mb86a20s_writeregdata(state, mb86a20s_init1);
1785 if (!state->inversion)
1787 rc = mb86a20s_writereg(state, 0x09, reg09);
1794 rc = mb86a20s_writereg(state, 0x39, reg71);
1797 rc = mb86a20s_writereg(state, 0x71, state->bw);
1800 if (state->subchannel) {
1801 rc = mb86a20s_writereg(state, 0x44, state->subchannel);
1806 fclk = state->config->fclk;
1810 /* Adjust IF frequency to match tuner */
1811 if (fe->ops.tuner_ops.get_if_frequency)
1812 fe->ops.tuner_ops.get_if_frequency(fe, &state->if_freq);
1814 if (!state->if_freq)
1815 state->if_freq = 3300000;
1817 pll = (((u64)1) << 34) * state->if_freq;
1818 do_div(pll, 63 * fclk);
1819 pll = (1 << 25) - pll;
1820 rc = mb86a20s_writereg(state, 0x28, 0x2a);
1823 rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
1826 rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
1829 rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
1832 dev_dbg(&state->i2c->dev, "%s: fclk=%d, IF=%d, clock reg=0x%06llx\n",
1833 __func__, fclk, state->if_freq, (long long)pll);
1835 /* pll = freq[Hz] * 2^24/10^6 / 16.285714286 */
1836 pll = state->if_freq * 1677721600L;
1837 do_div(pll, 1628571429L);
1838 rc = mb86a20s_writereg(state, 0x28, 0x20);
1841 rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
1844 rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
1847 rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
1850 dev_dbg(&state->i2c->dev, "%s: IF=%d, IF reg=0x%06llx\n",
1851 __func__, state->if_freq, (long long)pll);
1853 if (!state->config->is_serial)
1856 rc = mb86a20s_writereg(state, 0x50, 0xd5);
1859 rc = mb86a20s_writereg(state, 0x51, regD5);
1863 rc = mb86a20s_writeregdata(state, mb86a20s_init2);
1869 if (fe->ops.i2c_gate_ctrl)
1870 fe->ops.i2c_gate_ctrl(fe, 1);
1873 state->need_init = true;
1874 dev_info(&state->i2c->dev,
1875 "mb86a20s: Init failed. Will try again later\n");
1877 state->need_init = false;
1878 dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
1883 static int mb86a20s_set_frontend(struct dvb_frontend *fe)
1885 struct mb86a20s_state *state = fe->demodulator_priv;
1886 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1888 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1890 if (!c->isdbt_layer_enabled)
1891 c->isdbt_layer_enabled = 7;
1893 if (c->isdbt_layer_enabled == 1)
1894 state->bw = MB86A20S_1SEG;
1895 else if (c->isdbt_partial_reception)
1896 state->bw = MB86A20S_13SEG_PARTIAL;
1898 state->bw = MB86A20S_13SEG;
1900 if (c->inversion == INVERSION_ON)
1901 state->inversion = true;
1903 state->inversion = false;
1905 if (!c->isdbt_sb_mode) {
1906 state->subchannel = 0;
1908 if (c->isdbt_sb_subchannel >= ARRAY_SIZE(mb86a20s_subchannel))
1909 c->isdbt_sb_subchannel = 0;
1911 state->subchannel = mb86a20s_subchannel[c->isdbt_sb_subchannel];
1915 * Gate should already be opened, but it doesn't hurt to
1918 if (fe->ops.i2c_gate_ctrl)
1919 fe->ops.i2c_gate_ctrl(fe, 1);
1920 fe->ops.tuner_ops.set_params(fe);
1922 if (fe->ops.tuner_ops.get_if_frequency)
1923 fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
1926 * Make it more reliable: if, for some reason, the initial
1927 * device initialization doesn't happen, initialize it when
1928 * a SBTVD parameters are adjusted.
1930 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
1931 * the agc callback logic is not called during DVB attach time,
1932 * causing mb86a20s to not be initialized with Kworld SBTVD.
1933 * So, this hack is needed, in order to make Kworld SBTVD to work.
1935 * It is also needed to change the IF after the initial init.
1937 * HACK: Always init the frontend when set_frontend is called:
1938 * it was noticed that, on some devices, it fails to lock on a
1939 * different channel. So, it is better to reset everything, even
1940 * wasting some time, than to loose channel lock.
1942 mb86a20s_initfe(fe);
1944 if (fe->ops.i2c_gate_ctrl)
1945 fe->ops.i2c_gate_ctrl(fe, 0);
1947 rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
1948 mb86a20s_reset_counters(fe);
1949 mb86a20s_stats_not_ready(fe);
1951 if (fe->ops.i2c_gate_ctrl)
1952 fe->ops.i2c_gate_ctrl(fe, 1);
1957 static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
1958 fe_status_t *status)
1960 struct mb86a20s_state *state = fe->demodulator_priv;
1963 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1965 if (fe->ops.i2c_gate_ctrl)
1966 fe->ops.i2c_gate_ctrl(fe, 0);
1969 status_nr = mb86a20s_read_status(fe, status);
1970 if (status_nr < 7) {
1971 mb86a20s_stats_not_ready(fe);
1972 mb86a20s_reset_frontend_cache(fe);
1974 if (status_nr < 0) {
1975 dev_err(&state->i2c->dev,
1976 "%s: Can't read frontend lock status\n", __func__);
1980 /* Get signal strength */
1981 rc = mb86a20s_read_signal_strength(fe);
1983 dev_err(&state->i2c->dev,
1984 "%s: Can't reset VBER registers.\n", __func__);
1985 mb86a20s_stats_not_ready(fe);
1986 mb86a20s_reset_frontend_cache(fe);
1988 rc = 0; /* Status is OK */
1992 if (status_nr >= 7) {
1994 rc = mb86a20s_get_frontend(fe);
1996 dev_err(&state->i2c->dev,
1997 "%s: Can't get FE TMCC data.\n", __func__);
1998 rc = 0; /* Status is OK */
2002 /* Get statistics */
2003 rc = mb86a20s_get_stats(fe, status_nr);
2004 if (rc < 0 && rc != -EBUSY) {
2005 dev_err(&state->i2c->dev,
2006 "%s: Can't get FE statistics.\n", __func__);
2010 rc = 0; /* Don't return EBUSY to userspace */
2015 mb86a20s_stats_not_ready(fe);
2018 if (fe->ops.i2c_gate_ctrl)
2019 fe->ops.i2c_gate_ctrl(fe, 1);
2024 static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
2027 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2030 *strength = c->strength.stat[0].uvalue;
2035 static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe)
2038 * get_frontend is now handled together with other stats
2039 * retrival, when read_status() is called, as some statistics
2040 * will depend on the layers detection.
2045 static int mb86a20s_tune(struct dvb_frontend *fe,
2047 unsigned int mode_flags,
2048 unsigned int *delay,
2049 fe_status_t *status)
2051 struct mb86a20s_state *state = fe->demodulator_priv;
2054 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
2057 rc = mb86a20s_set_frontend(fe);
2059 if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
2060 mb86a20s_read_status_and_stats(fe, status);
2065 static void mb86a20s_release(struct dvb_frontend *fe)
2067 struct mb86a20s_state *state = fe->demodulator_priv;
2069 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
2074 static struct dvb_frontend_ops mb86a20s_ops;
2076 struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
2077 struct i2c_adapter *i2c)
2079 struct mb86a20s_state *state;
2082 dev_dbg(&i2c->dev, "%s called.\n", __func__);
2084 /* allocate memory for the internal state */
2085 state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
2086 if (state == NULL) {
2088 "%s: unable to allocate memory for state\n", __func__);
2092 /* setup the state */
2093 state->config = config;
2096 /* create dvb_frontend */
2097 memcpy(&state->frontend.ops, &mb86a20s_ops,
2098 sizeof(struct dvb_frontend_ops));
2099 state->frontend.demodulator_priv = state;
2101 /* Check if it is a mb86a20s frontend */
2102 rev = mb86a20s_readreg(state, 0);
2106 "Detected a Fujitsu mb86a20s frontend\n");
2109 "Frontend revision %d is unknown - aborting.\n",
2114 return &state->frontend;
2120 EXPORT_SYMBOL(mb86a20s_attach);
2122 static struct dvb_frontend_ops mb86a20s_ops = {
2123 .delsys = { SYS_ISDBT },
2124 /* Use dib8000 values per default */
2126 .name = "Fujitsu mb86A20s",
2127 .caps = FE_CAN_RECOVER |
2128 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
2129 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
2130 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
2131 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
2132 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
2133 /* Actually, those values depend on the used tuner */
2134 .frequency_min = 45000000,
2135 .frequency_max = 864000000,
2136 .frequency_stepsize = 62500,
2139 .release = mb86a20s_release,
2141 .init = mb86a20s_initfe,
2142 .set_frontend = mb86a20s_set_frontend,
2143 .get_frontend = mb86a20s_get_frontend_dummy,
2144 .read_status = mb86a20s_read_status_and_stats,
2145 .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
2146 .tune = mb86a20s_tune,
2149 MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
2150 MODULE_AUTHOR("Mauro Carvalho Chehab");
2151 MODULE_LICENSE("GPL");