2 * Fujitu mb86a20s ISDB-T/ISDB-Tsb Module driver
4 * Copyright (C) 2010-2013 Mauro Carvalho Chehab <mchehab@redhat.com>
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"
24 module_param(debug
, int, 0644);
25 MODULE_PARM_DESC(debug
, "Activates frontend debugging (default:0)");
27 struct mb86a20s_state
{
28 struct i2c_adapter
*i2c
;
29 const struct mb86a20s_config
*config
;
32 struct dvb_frontend frontend
;
36 u32 estimated_rate
[3];
46 #define BER_SAMPLING_RATE 1 /* Seconds */
49 * Initialization sequence: Use whatevere default values that PV SBTVD
50 * does on its initialisation, obtained via USB snoop
52 static struct regdata mb86a20s_init1
[] = {
57 { 0x50, 0xd1 }, { 0x51, 0x22 },
60 { 0x28, 0x2a }, { 0x29, 0x00 }, { 0x2a, 0xff }, { 0x2b, 0x80 },
63 static struct regdata mb86a20s_init2
[] = {
64 { 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
68 { 0x04, 0x08 }, { 0x05, 0x05 },
69 { 0x04, 0x0e }, { 0x05, 0x00 },
70 { 0x04, 0x0f }, { 0x05, 0x14 },
71 { 0x04, 0x0b }, { 0x05, 0x8c },
72 { 0x04, 0x00 }, { 0x05, 0x00 },
73 { 0x04, 0x01 }, { 0x05, 0x07 },
74 { 0x04, 0x02 }, { 0x05, 0x0f },
75 { 0x04, 0x03 }, { 0x05, 0xa0 },
76 { 0x04, 0x09 }, { 0x05, 0x00 },
77 { 0x04, 0x0a }, { 0x05, 0xff },
78 { 0x04, 0x27 }, { 0x05, 0x64 },
79 { 0x04, 0x28 }, { 0x05, 0x00 },
80 { 0x04, 0x1e }, { 0x05, 0xff },
81 { 0x04, 0x29 }, { 0x05, 0x0a },
82 { 0x04, 0x32 }, { 0x05, 0x0a },
83 { 0x04, 0x14 }, { 0x05, 0x02 },
84 { 0x04, 0x04 }, { 0x05, 0x00 },
85 { 0x04, 0x05 }, { 0x05, 0x22 },
86 { 0x04, 0x06 }, { 0x05, 0x0e },
87 { 0x04, 0x07 }, { 0x05, 0xd8 },
88 { 0x04, 0x12 }, { 0x05, 0x00 },
89 { 0x04, 0x13 }, { 0x05, 0xff },
90 { 0x04, 0x15 }, { 0x05, 0x4e },
91 { 0x04, 0x16 }, { 0x05, 0x20 },
94 * On this demod, when the bit count reaches the count below,
95 * it collects the bit error count. The bit counters are initialized
96 * to 65535 here. This warrants that all of them will be quickly
97 * calculated when device gets locked. As TMCC is parsed, the values
98 * will be adjusted later in the driver's code.
100 { 0x52, 0x01 }, /* Turn on BER before Viterbi */
101 { 0x50, 0xa7 }, { 0x51, 0x00 },
102 { 0x50, 0xa8 }, { 0x51, 0xff },
103 { 0x50, 0xa9 }, { 0x51, 0xff },
104 { 0x50, 0xaa }, { 0x51, 0x00 },
105 { 0x50, 0xab }, { 0x51, 0xff },
106 { 0x50, 0xac }, { 0x51, 0xff },
107 { 0x50, 0xad }, { 0x51, 0x00 },
108 { 0x50, 0xae }, { 0x51, 0xff },
109 { 0x50, 0xaf }, { 0x51, 0xff },
112 * On this demod, post BER counts blocks. When the count reaches the
113 * value below, it collects the block error count. The block counters
114 * are initialized to 127 here. This warrants that all of them will be
115 * quickly calculated when device gets locked. As TMCC is parsed, the
116 * values will be adjusted later in the driver's code.
118 { 0x5e, 0x07 }, /* Turn on BER after Viterbi */
119 { 0x50, 0xdc }, { 0x51, 0x00 },
120 { 0x50, 0xdd }, { 0x51, 0x7f },
121 { 0x50, 0xde }, { 0x51, 0x00 },
122 { 0x50, 0xdf }, { 0x51, 0x7f },
123 { 0x50, 0xe0 }, { 0x51, 0x00 },
124 { 0x50, 0xe1 }, { 0x51, 0x7f },
127 * On this demod, when the block count reaches the count below,
128 * it collects the block error count. The block counters are initialized
129 * to 127 here. This warrants that all of them will be quickly
130 * calculated when device gets locked. As TMCC is parsed, the values
131 * will be adjusted later in the driver's code.
133 { 0x50, 0xb0 }, { 0x51, 0x07 }, /* Enable PER */
134 { 0x50, 0xb2 }, { 0x51, 0x00 },
135 { 0x50, 0xb3 }, { 0x51, 0x7f },
136 { 0x50, 0xb4 }, { 0x51, 0x00 },
137 { 0x50, 0xb5 }, { 0x51, 0x7f },
138 { 0x50, 0xb6 }, { 0x51, 0x00 },
139 { 0x50, 0xb7 }, { 0x51, 0x7f },
141 { 0x50, 0x50 }, { 0x51, 0x02 }, /* MER manual mode */
142 { 0x50, 0x51 }, { 0x51, 0x04 }, /* MER symbol 4 */
143 { 0x45, 0x04 }, /* CN symbol 4 */
144 { 0x48, 0x04 }, /* CN manual mode */
146 { 0x50, 0xd5 }, { 0x51, 0x01 }, /* Serial */
147 { 0x50, 0xd6 }, { 0x51, 0x1f },
148 { 0x50, 0xd2 }, { 0x51, 0x03 },
149 { 0x50, 0xd7 }, { 0x51, 0x3f },
150 { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x28, 0x74 }, { 0x29, 0x40 },
151 { 0x28, 0x46 }, { 0x29, 0x2c }, { 0x28, 0x46 }, { 0x29, 0x0c },
153 { 0x04, 0x40 }, { 0x05, 0x00 },
154 { 0x28, 0x00 }, { 0x29, 0x10 },
155 { 0x28, 0x05 }, { 0x29, 0x02 },
157 { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x03 },
158 { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0d },
159 { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
160 { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x01 },
161 { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x21 },
162 { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x29 },
163 { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
164 { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x31 },
165 { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0e },
166 { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x4e },
167 { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x46 },
168 { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
169 { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x56 },
170 { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x35 },
171 { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbe },
172 { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0x84 },
173 { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x03 }, { 0x2b, 0xee },
174 { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x98 },
175 { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x9f },
176 { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xb2 },
177 { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0xc2 },
178 { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0x4a },
179 { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbc },
180 { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x04 }, { 0x2b, 0xba },
181 { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0x14 },
182 { 0x50, 0x1e }, { 0x51, 0x5d },
183 { 0x50, 0x22 }, { 0x51, 0x00 },
184 { 0x50, 0x23 }, { 0x51, 0xc8 },
185 { 0x50, 0x24 }, { 0x51, 0x00 },
186 { 0x50, 0x25 }, { 0x51, 0xf0 },
187 { 0x50, 0x26 }, { 0x51, 0x00 },
188 { 0x50, 0x27 }, { 0x51, 0xc3 },
189 { 0x50, 0x39 }, { 0x51, 0x02 },
190 { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
194 static struct regdata mb86a20s_reset_reception
[] = {
201 static struct regdata mb86a20s_per_ber_reset
[] = {
202 { 0x53, 0x00 }, /* pre BER Counter reset */
205 { 0x5f, 0x00 }, /* post BER Counter reset */
208 { 0x50, 0xb1 }, /* PER Counter reset */
214 * I2C read/write functions and macros
217 static int mb86a20s_i2c_writereg(struct mb86a20s_state
*state
,
218 u8 i2c_addr
, u8 reg
, u8 data
)
220 u8 buf
[] = { reg
, data
};
221 struct i2c_msg msg
= {
222 .addr
= i2c_addr
, .flags
= 0, .buf
= buf
, .len
= 2
226 rc
= i2c_transfer(state
->i2c
, &msg
, 1);
228 dev_err(&state
->i2c
->dev
,
229 "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
230 __func__
, rc
, reg
, data
);
237 static int mb86a20s_i2c_writeregdata(struct mb86a20s_state
*state
,
238 u8 i2c_addr
, struct regdata
*rd
, int size
)
242 for (i
= 0; i
< size
; i
++) {
243 rc
= mb86a20s_i2c_writereg(state
, i2c_addr
, rd
[i
].reg
,
251 static int mb86a20s_i2c_readreg(struct mb86a20s_state
*state
,
256 struct i2c_msg msg
[] = {
257 { .addr
= i2c_addr
, .flags
= 0, .buf
= ®
, .len
= 1 },
258 { .addr
= i2c_addr
, .flags
= I2C_M_RD
, .buf
= &val
, .len
= 1 }
261 rc
= i2c_transfer(state
->i2c
, msg
, 2);
264 dev_err(&state
->i2c
->dev
, "%s: reg=0x%x (error=%d)\n",
266 return (rc
< 0) ? rc
: -EIO
;
272 #define mb86a20s_readreg(state, reg) \
273 mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
274 #define mb86a20s_writereg(state, reg, val) \
275 mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
276 #define mb86a20s_writeregdata(state, regdata) \
277 mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
278 regdata, ARRAY_SIZE(regdata))
281 * Ancillary internal routines (likely compiled inlined)
283 * The functions below assume that gateway lock has already obtained
286 static int mb86a20s_read_status(struct dvb_frontend
*fe
, fe_status_t
*status
)
288 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
293 val
= mb86a20s_readreg(state
, 0x0a) & 0xf;
298 *status
|= FE_HAS_SIGNAL
;
301 *status
|= FE_HAS_CARRIER
;
304 *status
|= FE_HAS_VITERBI
;
307 *status
|= FE_HAS_SYNC
;
309 if (val
>= 8) /* Maybe 9? */
310 *status
|= FE_HAS_LOCK
;
312 dev_dbg(&state
->i2c
->dev
, "%s: Status = 0x%02x (state = %d)\n",
313 __func__
, *status
, val
);
318 static int mb86a20s_read_signal_strength(struct dvb_frontend
*fe
)
320 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
322 unsigned rf_max
, rf_min
, rf
;
324 /* Does a binary search to get RF strength */
328 rf
= (rf_max
+ rf_min
) / 2;
329 rc
= mb86a20s_writereg(state
, 0x04, 0x1f);
332 rc
= mb86a20s_writereg(state
, 0x05, rf
>> 8);
335 rc
= mb86a20s_writereg(state
, 0x04, 0x20);
338 rc
= mb86a20s_writereg(state
, 0x04, rf
);
342 rc
= mb86a20s_readreg(state
, 0x02);
346 rf_min
= (rf_max
+ rf_min
) / 2;
348 rf_max
= (rf_max
+ rf_min
) / 2;
349 if (rf_max
- rf_min
< 4) {
350 rf
= (rf_max
+ rf_min
) / 2;
352 /* Rescale it from 2^12 (4096) to 2^16 */
354 dev_dbg(&state
->i2c
->dev
,
355 "%s: signal strength = %d (%d < RF=%d < %d)\n",
356 __func__
, rf
, rf_min
, rf
>> 4, rf_max
);
364 static int mb86a20s_get_modulation(struct mb86a20s_state
*state
,
368 static unsigned char reg
[] = {
369 [0] = 0x86, /* Layer A */
370 [1] = 0x8a, /* Layer B */
371 [2] = 0x8e, /* Layer C */
374 if (layer
>= ARRAY_SIZE(reg
))
376 rc
= mb86a20s_writereg(state
, 0x6d, reg
[layer
]);
379 rc
= mb86a20s_readreg(state
, 0x6e);
382 switch ((rc
>> 4) & 0x07) {
396 static int mb86a20s_get_fec(struct mb86a20s_state
*state
,
401 static unsigned char reg
[] = {
402 [0] = 0x87, /* Layer A */
403 [1] = 0x8b, /* Layer B */
404 [2] = 0x8f, /* Layer C */
407 if (layer
>= ARRAY_SIZE(reg
))
409 rc
= mb86a20s_writereg(state
, 0x6d, reg
[layer
]);
412 rc
= mb86a20s_readreg(state
, 0x6e);
415 switch ((rc
>> 4) & 0x07) {
431 static int mb86a20s_get_interleaving(struct mb86a20s_state
*state
,
436 static unsigned char reg
[] = {
437 [0] = 0x88, /* Layer A */
438 [1] = 0x8c, /* Layer B */
439 [2] = 0x90, /* Layer C */
442 if (layer
>= ARRAY_SIZE(reg
))
444 rc
= mb86a20s_writereg(state
, 0x6d, reg
[layer
]);
447 rc
= mb86a20s_readreg(state
, 0x6e);
451 switch ((rc
>> 4) & 0x07) {
453 return GUARD_INTERVAL_1_4
;
455 return GUARD_INTERVAL_1_8
;
457 return GUARD_INTERVAL_1_16
;
459 return GUARD_INTERVAL_1_32
;
463 return GUARD_INTERVAL_AUTO
;
467 static int mb86a20s_get_segment_count(struct mb86a20s_state
*state
,
471 static unsigned char reg
[] = {
472 [0] = 0x89, /* Layer A */
473 [1] = 0x8d, /* Layer B */
474 [2] = 0x91, /* Layer C */
477 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
479 if (layer
>= ARRAY_SIZE(reg
))
482 rc
= mb86a20s_writereg(state
, 0x6d, reg
[layer
]);
485 rc
= mb86a20s_readreg(state
, 0x6e);
488 count
= (rc
>> 4) & 0x0f;
490 dev_dbg(&state
->i2c
->dev
, "%s: segments: %d.\n", __func__
, count
);
495 static void mb86a20s_reset_frontend_cache(struct dvb_frontend
*fe
)
497 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
498 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
500 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
502 /* Fixed parameters */
503 c
->delivery_system
= SYS_ISDBT
;
504 c
->bandwidth_hz
= 6000000;
506 /* Initialize values that will be later autodetected */
507 c
->isdbt_layer_enabled
= 0;
508 c
->transmission_mode
= TRANSMISSION_MODE_AUTO
;
509 c
->guard_interval
= GUARD_INTERVAL_AUTO
;
510 c
->isdbt_sb_mode
= 0;
511 c
->isdbt_sb_segment_count
= 0;
515 * Estimates the bit rate using the per-segment bit rate given by
516 * ABNT/NBR 15601 spec (table 4).
518 static u32 isdbt_rate
[3][5][4] = {
520 { 280850, 312060, 330420, 340430 }, /* 1/2 */
521 { 374470, 416080, 440560, 453910 }, /* 2/3 */
522 { 421280, 468090, 495630, 510650 }, /* 3/4 */
523 { 468090, 520100, 550700, 567390 }, /* 5/6 */
524 { 491500, 546110, 578230, 595760 }, /* 7/8 */
526 { 561710, 624130, 660840, 680870 }, /* 1/2 */
527 { 748950, 832170, 881120, 907820 }, /* 2/3 */
528 { 842570, 936190, 991260, 1021300 }, /* 3/4 */
529 { 936190, 1040210, 1101400, 1134780 }, /* 5/6 */
530 { 983000, 1092220, 1156470, 1191520 }, /* 7/8 */
532 { 842570, 936190, 991260, 1021300 }, /* 1/2 */
533 { 1123430, 1248260, 1321680, 1361740 }, /* 2/3 */
534 { 1263860, 1404290, 1486900, 1531950 }, /* 3/4 */
535 { 1404290, 1560320, 1652110, 1702170 }, /* 5/6 */
536 { 1474500, 1638340, 1734710, 1787280 }, /* 7/8 */
540 static void mb86a20s_layer_bitrate(struct dvb_frontend
*fe
, u32 layer
,
541 u32 modulation
, u32 fec
, u32 interleaving
,
544 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
549 * If modulation/fec/interleaving is not detected, the default is
550 * to consider the lowest bit rate, to avoid taking too long time
553 switch (modulation
) {
587 switch (interleaving
) {
589 case GUARD_INTERVAL_1_4
:
592 case GUARD_INTERVAL_1_8
:
595 case GUARD_INTERVAL_1_16
:
598 case GUARD_INTERVAL_1_32
:
603 /* Samples BER at BER_SAMPLING_RATE seconds */
604 rate
= isdbt_rate
[m
][f
][i
] * segment
* BER_SAMPLING_RATE
;
606 /* Avoids sampling too quickly or to overflow the register */
609 else if (rate
> (1 << 24) - 1)
610 rate
= (1 << 24) - 1;
612 dev_dbg(&state
->i2c
->dev
,
613 "%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n",
614 __func__
, 'A' + layer
, segment
* isdbt_rate
[m
][f
][i
]/1000,
617 state
->estimated_rate
[i
] = rate
;
621 static int mb86a20s_get_frontend(struct dvb_frontend
*fe
)
623 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
624 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
627 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
629 /* Reset frontend cache to default values */
630 mb86a20s_reset_frontend_cache(fe
);
632 /* Check for partial reception */
633 rc
= mb86a20s_writereg(state
, 0x6d, 0x85);
636 rc
= mb86a20s_readreg(state
, 0x6e);
639 c
->isdbt_partial_reception
= (rc
& 0x10) ? 1 : 0;
641 /* Get per-layer data */
643 for (i
= 0; i
< 3; i
++) {
644 dev_dbg(&state
->i2c
->dev
, "%s: getting data for layer %c.\n",
647 rc
= mb86a20s_get_segment_count(state
, i
);
649 goto noperlayer_error
;
650 if (rc
>= 0 && rc
< 14) {
651 c
->layer
[i
].segment_count
= rc
;
653 c
->layer
[i
].segment_count
= 0;
654 state
->estimated_rate
[i
] = 0;
657 c
->isdbt_layer_enabled
|= 1 << i
;
658 rc
= mb86a20s_get_modulation(state
, i
);
660 goto noperlayer_error
;
661 dev_dbg(&state
->i2c
->dev
, "%s: modulation %d.\n",
663 c
->layer
[i
].modulation
= rc
;
664 rc
= mb86a20s_get_fec(state
, i
);
666 goto noperlayer_error
;
667 dev_dbg(&state
->i2c
->dev
, "%s: FEC %d.\n",
669 c
->layer
[i
].fec
= rc
;
670 rc
= mb86a20s_get_interleaving(state
, i
);
672 goto noperlayer_error
;
673 dev_dbg(&state
->i2c
->dev
, "%s: interleaving %d.\n",
675 c
->layer
[i
].interleaving
= rc
;
676 mb86a20s_layer_bitrate(fe
, i
, c
->layer
[i
].modulation
,
678 c
->layer
[i
].interleaving
,
679 c
->layer
[i
].segment_count
);
682 rc
= mb86a20s_writereg(state
, 0x6d, 0x84);
685 if ((rc
& 0x60) == 0x20) {
686 c
->isdbt_sb_mode
= 1;
687 /* At least, one segment should exist */
688 if (!c
->isdbt_sb_segment_count
)
689 c
->isdbt_sb_segment_count
= 1;
692 /* Get transmission mode and guard interval */
693 rc
= mb86a20s_readreg(state
, 0x07);
696 if ((rc
& 0x60) == 0x20) {
697 switch (rc
& 0x0c >> 2) {
699 c
->transmission_mode
= TRANSMISSION_MODE_2K
;
702 c
->transmission_mode
= TRANSMISSION_MODE_4K
;
705 c
->transmission_mode
= TRANSMISSION_MODE_8K
;
712 c
->guard_interval
= GUARD_INTERVAL_1_4
;
715 c
->guard_interval
= GUARD_INTERVAL_1_8
;
718 c
->guard_interval
= GUARD_INTERVAL_1_16
;
726 /* per-layer info is incomplete; discard all per-layer */
727 c
->isdbt_layer_enabled
= 0;
732 static int mb86a20s_reset_counters(struct dvb_frontend
*fe
)
734 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
735 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
738 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
740 /* Reset the counters, if the channel changed */
741 if (state
->last_frequency
!= c
->frequency
) {
742 memset(&c
->strength
, 0, sizeof(c
->strength
));
743 memset(&c
->cnr
, 0, sizeof(c
->cnr
));
744 memset(&c
->pre_bit_error
, 0, sizeof(c
->pre_bit_error
));
745 memset(&c
->pre_bit_count
, 0, sizeof(c
->pre_bit_count
));
746 memset(&c
->post_bit_error
, 0, sizeof(c
->post_bit_error
));
747 memset(&c
->post_bit_count
, 0, sizeof(c
->post_bit_count
));
748 memset(&c
->block_error
, 0, sizeof(c
->block_error
));
749 memset(&c
->block_count
, 0, sizeof(c
->block_count
));
751 state
->last_frequency
= c
->frequency
;
754 /* Clear status for most stats */
756 /* BER/PER counter reset */
757 rc
= mb86a20s_writeregdata(state
, mb86a20s_per_ber_reset
);
761 /* CNR counter reset */
762 rc
= mb86a20s_readreg(state
, 0x45);
766 rc
= mb86a20s_writereg(state
, 0x45, val
| 0x10);
769 rc
= mb86a20s_writereg(state
, 0x45, val
& 0x6f);
773 /* MER counter reset */
774 rc
= mb86a20s_writereg(state
, 0x50, 0x50);
777 rc
= mb86a20s_readreg(state
, 0x51);
781 rc
= mb86a20s_writereg(state
, 0x51, val
| 0x01);
784 rc
= mb86a20s_writereg(state
, 0x51, val
& 0x06);
790 dev_err(&state
->i2c
->dev
,
791 "%s: Can't reset FE statistics (error %d).\n",
797 static int mb86a20s_get_pre_ber(struct dvb_frontend
*fe
,
799 u32
*error
, u32
*count
)
801 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
804 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
809 /* Check if the BER measures are already available */
810 rc
= mb86a20s_readreg(state
, 0x54);
814 /* Check if data is available for that layer */
815 if (!(rc
& (1 << layer
))) {
816 dev_dbg(&state
->i2c
->dev
,
817 "%s: preBER for layer %c is not available yet.\n",
818 __func__
, 'A' + layer
);
822 /* Read Bit Error Count */
823 rc
= mb86a20s_readreg(state
, 0x55 + layer
* 3);
827 rc
= mb86a20s_readreg(state
, 0x56 + layer
* 3);
831 rc
= mb86a20s_readreg(state
, 0x57 + layer
* 3);
836 dev_dbg(&state
->i2c
->dev
,
837 "%s: bit error before Viterbi for layer %c: %d.\n",
838 __func__
, 'A' + layer
, *error
);
841 rc
= mb86a20s_writereg(state
, 0x50, 0xa7 + layer
* 3);
844 rc
= mb86a20s_readreg(state
, 0x51);
848 rc
= mb86a20s_writereg(state
, 0x50, 0xa8 + layer
* 3);
851 rc
= mb86a20s_readreg(state
, 0x51);
855 rc
= mb86a20s_writereg(state
, 0x50, 0xa9 + layer
* 3);
858 rc
= mb86a20s_readreg(state
, 0x51);
863 dev_dbg(&state
->i2c
->dev
,
864 "%s: bit count before Viterbi for layer %c: %d.\n",
865 __func__
, 'A' + layer
, *count
);
869 * As we get TMCC data from the frontend, we can better estimate the
870 * BER bit counters, in order to do the BER measure during a longer
871 * time. Use those data, if available, to update the bit count
875 if (state
->estimated_rate
[layer
]
876 && state
->estimated_rate
[layer
] != *count
) {
877 dev_dbg(&state
->i2c
->dev
,
878 "%s: updating layer %c preBER counter to %d.\n",
879 __func__
, 'A' + layer
, state
->estimated_rate
[layer
]);
881 /* Turn off BER before Viterbi */
882 rc
= mb86a20s_writereg(state
, 0x52, 0x00);
884 /* Update counter for this layer */
885 rc
= mb86a20s_writereg(state
, 0x50, 0xa7 + layer
* 3);
888 rc
= mb86a20s_writereg(state
, 0x51,
889 state
->estimated_rate
[layer
] >> 16);
892 rc
= mb86a20s_writereg(state
, 0x50, 0xa8 + layer
* 3);
895 rc
= mb86a20s_writereg(state
, 0x51,
896 state
->estimated_rate
[layer
] >> 8);
899 rc
= mb86a20s_writereg(state
, 0x50, 0xa9 + layer
* 3);
902 rc
= mb86a20s_writereg(state
, 0x51,
903 state
->estimated_rate
[layer
]);
907 /* Turn on BER before Viterbi */
908 rc
= mb86a20s_writereg(state
, 0x52, 0x01);
910 /* Reset all preBER counters */
911 rc
= mb86a20s_writereg(state
, 0x53, 0x00);
914 rc
= mb86a20s_writereg(state
, 0x53, 0x07);
916 /* Reset counter to collect new data */
917 rc
= mb86a20s_readreg(state
, 0x53);
921 rc
= mb86a20s_writereg(state
, 0x53, val
& ~(1 << layer
));
924 rc
= mb86a20s_writereg(state
, 0x53, val
| (1 << layer
));
930 static int mb86a20s_get_post_ber(struct dvb_frontend
*fe
,
932 u32
*error
, u32
*count
)
934 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
935 u32 counter
, collect_rate
;
938 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
943 /* Check if the BER measures are already available */
944 rc
= mb86a20s_readreg(state
, 0x60);
948 /* Check if data is available for that layer */
949 if (!(rc
& (1 << layer
))) {
950 dev_dbg(&state
->i2c
->dev
,
951 "%s: post BER for layer %c is not available yet.\n",
952 __func__
, 'A' + layer
);
956 /* Read Bit Error Count */
957 rc
= mb86a20s_readreg(state
, 0x64 + layer
* 3);
961 rc
= mb86a20s_readreg(state
, 0x65 + layer
* 3);
965 rc
= mb86a20s_readreg(state
, 0x66 + layer
* 3);
970 dev_dbg(&state
->i2c
->dev
,
971 "%s: post bit error for layer %c: %d.\n",
972 __func__
, 'A' + layer
, *error
);
975 rc
= mb86a20s_writereg(state
, 0x50, 0xdc + layer
* 2);
978 rc
= mb86a20s_readreg(state
, 0x51);
982 rc
= mb86a20s_writereg(state
, 0x50, 0xdd + layer
* 2);
985 rc
= mb86a20s_readreg(state
, 0x51);
989 *count
= counter
* 204 * 8;
991 dev_dbg(&state
->i2c
->dev
,
992 "%s: post bit count for layer %c: %d.\n",
993 __func__
, 'A' + layer
, *count
);
996 * As we get TMCC data from the frontend, we can better estimate the
997 * BER bit counters, in order to do the BER measure during a longer
998 * time. Use those data, if available, to update the bit count
1002 if (!state
->estimated_rate
[layer
])
1003 goto reset_measurement
;
1005 collect_rate
= state
->estimated_rate
[layer
] / 204 / 8;
1006 if (collect_rate
< 32)
1008 if (collect_rate
> 65535)
1009 collect_rate
= 65535;
1010 if (collect_rate
!= counter
) {
1011 dev_dbg(&state
->i2c
->dev
,
1012 "%s: updating postBER counter on layer %c to %d.\n",
1013 __func__
, 'A' + layer
, collect_rate
);
1015 /* Turn off BER after Viterbi */
1016 rc
= mb86a20s_writereg(state
, 0x5e, 0x00);
1018 /* Update counter for this layer */
1019 rc
= mb86a20s_writereg(state
, 0x50, 0xdc + layer
* 2);
1022 rc
= mb86a20s_writereg(state
, 0x51, collect_rate
>> 8);
1025 rc
= mb86a20s_writereg(state
, 0x50, 0xdd + layer
* 2);
1028 rc
= mb86a20s_writereg(state
, 0x51, collect_rate
& 0xff);
1032 /* Turn on BER after Viterbi */
1033 rc
= mb86a20s_writereg(state
, 0x5e, 0x07);
1035 /* Reset all preBER counters */
1036 rc
= mb86a20s_writereg(state
, 0x5f, 0x00);
1039 rc
= mb86a20s_writereg(state
, 0x5f, 0x07);
1045 /* Reset counter to collect new data */
1046 rc
= mb86a20s_readreg(state
, 0x5f);
1050 rc
= mb86a20s_writereg(state
, 0x5f, val
& ~(1 << layer
));
1053 rc
= mb86a20s_writereg(state
, 0x5f, val
| (1 << layer
));
1058 static int mb86a20s_get_blk_error(struct dvb_frontend
*fe
,
1060 u32
*error
, u32
*count
)
1062 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1065 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1070 /* Check if the PER measures are already available */
1071 rc
= mb86a20s_writereg(state
, 0x50, 0xb8);
1074 rc
= mb86a20s_readreg(state
, 0x51);
1078 /* Check if data is available for that layer */
1080 if (!(rc
& (1 << layer
))) {
1081 dev_dbg(&state
->i2c
->dev
,
1082 "%s: block counts for layer %c aren't available yet.\n",
1083 __func__
, 'A' + layer
);
1087 /* Read Packet error Count */
1088 rc
= mb86a20s_writereg(state
, 0x50, 0xb9 + layer
* 2);
1091 rc
= mb86a20s_readreg(state
, 0x51);
1095 rc
= mb86a20s_writereg(state
, 0x50, 0xba + layer
* 2);
1098 rc
= mb86a20s_readreg(state
, 0x51);
1102 dev_dbg(&state
->i2c
->dev
, "%s: block error for layer %c: %d.\n",
1103 __func__
, 'A' + layer
, *error
);
1105 /* Read Bit Count */
1106 rc
= mb86a20s_writereg(state
, 0x50, 0xb2 + layer
* 2);
1109 rc
= mb86a20s_readreg(state
, 0x51);
1113 rc
= mb86a20s_writereg(state
, 0x50, 0xb3 + layer
* 2);
1116 rc
= mb86a20s_readreg(state
, 0x51);
1121 dev_dbg(&state
->i2c
->dev
,
1122 "%s: block count for layer %c: %d.\n",
1123 __func__
, 'A' + layer
, *count
);
1126 * As we get TMCC data from the frontend, we can better estimate the
1127 * BER bit counters, in order to do the BER measure during a longer
1128 * time. Use those data, if available, to update the bit count
1132 if (!state
->estimated_rate
[layer
])
1133 goto reset_measurement
;
1135 collect_rate
= state
->estimated_rate
[layer
] / 204 / 8;
1136 if (collect_rate
< 32)
1138 if (collect_rate
> 65535)
1139 collect_rate
= 65535;
1141 if (collect_rate
!= *count
) {
1142 dev_dbg(&state
->i2c
->dev
,
1143 "%s: updating PER counter on layer %c to %d.\n",
1144 __func__
, 'A' + layer
, collect_rate
);
1146 /* Stop PER measurement */
1147 rc
= mb86a20s_writereg(state
, 0x50, 0xb0);
1150 rc
= mb86a20s_writereg(state
, 0x51, 0x00);
1154 /* Update this layer's counter */
1155 rc
= mb86a20s_writereg(state
, 0x50, 0xb2 + layer
* 2);
1158 rc
= mb86a20s_writereg(state
, 0x51, collect_rate
>> 8);
1161 rc
= mb86a20s_writereg(state
, 0x50, 0xb3 + layer
* 2);
1164 rc
= mb86a20s_writereg(state
, 0x51, collect_rate
& 0xff);
1168 /* start PER measurement */
1169 rc
= mb86a20s_writereg(state
, 0x50, 0xb0);
1172 rc
= mb86a20s_writereg(state
, 0x51, 0x07);
1176 /* Reset all counters to collect new data */
1177 rc
= mb86a20s_writereg(state
, 0x50, 0xb1);
1180 rc
= mb86a20s_writereg(state
, 0x51, 0x07);
1183 rc
= mb86a20s_writereg(state
, 0x51, 0x00);
1189 /* Reset counter to collect new data */
1190 rc
= mb86a20s_writereg(state
, 0x50, 0xb1);
1193 rc
= mb86a20s_readreg(state
, 0x51);
1197 rc
= mb86a20s_writereg(state
, 0x51, val
| (1 << layer
));
1200 rc
= mb86a20s_writereg(state
, 0x51, val
& ~(1 << layer
));
1205 struct linear_segments
{
1210 * All tables below return a dB/1000 measurement
1213 static struct linear_segments cnr_to_db_table
[] = {
1247 static struct linear_segments cnr_64qam_table
[] = {
1281 static struct linear_segments cnr_16qam_table
[] = {
1315 struct linear_segments cnr_qpsk_table
[] = {
1349 static u32
interpolate_value(u32 value
, struct linear_segments
*segments
,
1356 if (value
>= segments
[0].x
)
1357 return segments
[0].y
;
1358 if (value
< segments
[len
-1].x
)
1359 return segments
[len
-1].y
;
1361 for (i
= 1; i
< len
- 1; i
++) {
1362 /* If value is identical, no need to interpolate */
1363 if (value
== segments
[i
].x
)
1364 return segments
[i
].y
;
1365 if (value
> segments
[i
].x
)
1369 /* Linear interpolation between the two (x,y) points */
1370 dy
= segments
[i
].y
- segments
[i
- 1].y
;
1371 dx
= segments
[i
- 1].x
- segments
[i
].x
;
1372 tmp64
= value
- segments
[i
].x
;
1375 ret
= segments
[i
].y
- tmp64
;
1380 static int mb86a20s_get_main_CNR(struct dvb_frontend
*fe
)
1382 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1383 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1384 u32 cnr_linear
, cnr
;
1387 /* Check if CNR is available */
1388 rc
= mb86a20s_readreg(state
, 0x45);
1393 dev_dbg(&state
->i2c
->dev
, "%s: CNR is not available yet.\n",
1399 rc
= mb86a20s_readreg(state
, 0x46);
1402 cnr_linear
= rc
<< 8;
1404 rc
= mb86a20s_readreg(state
, 0x46);
1409 cnr
= interpolate_value(cnr_linear
,
1410 cnr_to_db_table
, ARRAY_SIZE(cnr_to_db_table
));
1412 c
->cnr
.stat
[0].scale
= FE_SCALE_DECIBEL
;
1413 c
->cnr
.stat
[0].svalue
= cnr
;
1415 dev_dbg(&state
->i2c
->dev
, "%s: CNR is %d.%03d dB (%d)\n",
1416 __func__
, cnr
/ 1000, cnr
% 1000, cnr_linear
);
1418 /* CNR counter reset */
1419 rc
= mb86a20s_writereg(state
, 0x45, val
| 0x10);
1422 rc
= mb86a20s_writereg(state
, 0x45, val
& 0x6f);
1427 static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend
*fe
)
1429 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1430 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1433 struct linear_segments
*segs
;
1436 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1438 /* Check if the measures are already available */
1439 rc
= mb86a20s_writereg(state
, 0x50, 0x5b);
1442 rc
= mb86a20s_readreg(state
, 0x51);
1446 /* Check if data is available */
1448 dev_dbg(&state
->i2c
->dev
,
1449 "%s: MER measures aren't available yet.\n", __func__
);
1453 /* Read all layers */
1454 for (i
= 0; i
< 3; i
++) {
1455 if (!(c
->isdbt_layer_enabled
& (1 << i
))) {
1456 c
->cnr
.stat
[1 + i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1460 rc
= mb86a20s_writereg(state
, 0x50, 0x52 + i
* 3);
1463 rc
= mb86a20s_readreg(state
, 0x51);
1467 rc
= mb86a20s_writereg(state
, 0x50, 0x53 + i
* 3);
1470 rc
= mb86a20s_readreg(state
, 0x51);
1474 rc
= mb86a20s_writereg(state
, 0x50, 0x54 + i
* 3);
1477 rc
= mb86a20s_readreg(state
, 0x51);
1482 switch (c
->layer
[i
].modulation
) {
1485 segs
= cnr_qpsk_table
;
1486 segs_len
= ARRAY_SIZE(cnr_qpsk_table
);
1489 segs
= cnr_16qam_table
;
1490 segs_len
= ARRAY_SIZE(cnr_16qam_table
);
1494 segs
= cnr_64qam_table
;
1495 segs_len
= ARRAY_SIZE(cnr_64qam_table
);
1498 cnr
= interpolate_value(mer
, segs
, segs_len
);
1500 c
->cnr
.stat
[1 + i
].scale
= FE_SCALE_DECIBEL
;
1501 c
->cnr
.stat
[1 + i
].svalue
= cnr
;
1503 dev_dbg(&state
->i2c
->dev
,
1504 "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
1505 __func__
, 'A' + i
, cnr
/ 1000, cnr
% 1000, mer
);
1509 /* Start a new MER measurement */
1510 /* MER counter reset */
1511 rc
= mb86a20s_writereg(state
, 0x50, 0x50);
1514 rc
= mb86a20s_readreg(state
, 0x51);
1519 rc
= mb86a20s_writereg(state
, 0x51, val
| 0x01);
1522 rc
= mb86a20s_writereg(state
, 0x51, val
& 0x06);
1529 static void mb86a20s_stats_not_ready(struct dvb_frontend
*fe
)
1531 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1532 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1535 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1537 /* Fill the length of each status counter */
1539 /* Only global stats */
1540 c
->strength
.len
= 1;
1542 /* Per-layer stats - 3 layers + global */
1544 c
->pre_bit_error
.len
= 4;
1545 c
->pre_bit_count
.len
= 4;
1546 c
->post_bit_error
.len
= 4;
1547 c
->post_bit_count
.len
= 4;
1548 c
->block_error
.len
= 4;
1549 c
->block_count
.len
= 4;
1551 /* Signal is always available */
1552 c
->strength
.stat
[0].scale
= FE_SCALE_RELATIVE
;
1553 c
->strength
.stat
[0].uvalue
= 0;
1555 /* Put all of them at FE_SCALE_NOT_AVAILABLE */
1556 for (i
= 0; i
< 4; i
++) {
1557 c
->cnr
.stat
[i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1558 c
->pre_bit_error
.stat
[i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1559 c
->pre_bit_count
.stat
[i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1560 c
->post_bit_error
.stat
[i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1561 c
->post_bit_count
.stat
[i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1562 c
->block_error
.stat
[i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1563 c
->block_count
.stat
[i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1567 static int mb86a20s_get_stats(struct dvb_frontend
*fe
)
1569 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1570 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1572 u32 bit_error
= 0, bit_count
= 0;
1573 u32 t_pre_bit_error
= 0, t_pre_bit_count
= 0;
1574 u32 t_post_bit_error
= 0, t_post_bit_count
= 0;
1575 u32 block_error
= 0, block_count
= 0;
1576 u32 t_block_error
= 0, t_block_count
= 0;
1577 int active_layers
= 0, pre_ber_layers
= 0, post_ber_layers
= 0;
1580 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1582 mb86a20s_get_main_CNR(fe
);
1584 /* Get per-layer stats */
1585 mb86a20s_get_blk_error_layer_CNR(fe
);
1587 for (i
= 0; i
< 3; i
++) {
1588 if (c
->isdbt_layer_enabled
& (1 << i
)) {
1589 /* Layer is active and has rc segments */
1592 /* Handle BER before vterbi */
1593 rc
= mb86a20s_get_pre_ber(fe
, i
,
1594 &bit_error
, &bit_count
);
1596 c
->pre_bit_error
.stat
[1 + i
].scale
= FE_SCALE_COUNTER
;
1597 c
->pre_bit_error
.stat
[1 + i
].uvalue
+= bit_error
;
1598 c
->pre_bit_count
.stat
[1 + i
].scale
= FE_SCALE_COUNTER
;
1599 c
->pre_bit_count
.stat
[1 + i
].uvalue
+= bit_count
;
1600 } else if (rc
!= -EBUSY
) {
1602 * If an I/O error happened,
1603 * measures are now unavailable
1605 c
->pre_bit_error
.stat
[1 + i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1606 c
->pre_bit_count
.stat
[1 + i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1607 dev_err(&state
->i2c
->dev
,
1608 "%s: Can't get BER for layer %c (error %d).\n",
1609 __func__
, 'A' + i
, rc
);
1611 if (c
->block_error
.stat
[1 + i
].scale
!= FE_SCALE_NOT_AVAILABLE
)
1614 /* Handle BER post vterbi */
1615 rc
= mb86a20s_get_post_ber(fe
, i
,
1616 &bit_error
, &bit_count
);
1618 c
->post_bit_error
.stat
[1 + i
].scale
= FE_SCALE_COUNTER
;
1619 c
->post_bit_error
.stat
[1 + i
].uvalue
+= bit_error
;
1620 c
->post_bit_count
.stat
[1 + i
].scale
= FE_SCALE_COUNTER
;
1621 c
->post_bit_count
.stat
[1 + i
].uvalue
+= bit_count
;
1622 } else if (rc
!= -EBUSY
) {
1624 * If an I/O error happened,
1625 * measures are now unavailable
1627 c
->post_bit_error
.stat
[1 + i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1628 c
->post_bit_count
.stat
[1 + i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1629 dev_err(&state
->i2c
->dev
,
1630 "%s: Can't get BER for layer %c (error %d).\n",
1631 __func__
, 'A' + i
, rc
);
1633 if (c
->block_error
.stat
[1 + i
].scale
!= FE_SCALE_NOT_AVAILABLE
)
1636 /* Handle Block errors for PER/UCB reports */
1637 rc
= mb86a20s_get_blk_error(fe
, i
,
1641 c
->block_error
.stat
[1 + i
].scale
= FE_SCALE_COUNTER
;
1642 c
->block_error
.stat
[1 + i
].uvalue
+= block_error
;
1643 c
->block_count
.stat
[1 + i
].scale
= FE_SCALE_COUNTER
;
1644 c
->block_count
.stat
[1 + i
].uvalue
+= block_count
;
1645 } else if (rc
!= -EBUSY
) {
1647 * If an I/O error happened,
1648 * measures are now unavailable
1650 c
->block_error
.stat
[1 + i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1651 c
->block_count
.stat
[1 + i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1652 dev_err(&state
->i2c
->dev
,
1653 "%s: Can't get PER for layer %c (error %d).\n",
1654 __func__
, 'A' + i
, rc
);
1657 if (c
->block_error
.stat
[1 + i
].scale
!= FE_SCALE_NOT_AVAILABLE
)
1660 /* Update total preBER */
1661 t_pre_bit_error
+= c
->pre_bit_error
.stat
[1 + i
].uvalue
;
1662 t_pre_bit_count
+= c
->pre_bit_count
.stat
[1 + i
].uvalue
;
1664 /* Update total postBER */
1665 t_post_bit_error
+= c
->post_bit_error
.stat
[1 + i
].uvalue
;
1666 t_post_bit_count
+= c
->post_bit_count
.stat
[1 + i
].uvalue
;
1668 /* Update total PER */
1669 t_block_error
+= c
->block_error
.stat
[1 + i
].uvalue
;
1670 t_block_count
+= c
->block_count
.stat
[1 + i
].uvalue
;
1675 * Start showing global count if at least one error count is
1678 if (pre_ber_layers
) {
1680 * At least one per-layer BER measure was read. We can now
1681 * calculate the total BER
1683 * Total Bit Error/Count is calculated as the sum of the
1684 * bit errors on all active layers.
1686 c
->pre_bit_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
1687 c
->pre_bit_error
.stat
[0].uvalue
= t_pre_bit_error
;
1688 c
->pre_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
1689 c
->pre_bit_count
.stat
[0].uvalue
= t_pre_bit_count
;
1691 c
->pre_bit_error
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
1692 c
->pre_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
1696 * Start showing global count if at least one error count is
1699 if (post_ber_layers
) {
1701 * At least one per-layer BER measure was read. We can now
1702 * calculate the total BER
1704 * Total Bit Error/Count is calculated as the sum of the
1705 * bit errors on all active layers.
1707 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
1708 c
->post_bit_error
.stat
[0].uvalue
= t_post_bit_error
;
1709 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
1710 c
->post_bit_count
.stat
[0].uvalue
= t_post_bit_count
;
1712 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
1713 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
1718 * At least one per-layer UCB measure was read. We can now
1719 * calculate the total UCB
1721 * Total block Error/Count is calculated as the sum of the
1722 * block errors on all active layers.
1724 c
->block_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
1725 c
->block_error
.stat
[0].uvalue
= t_block_error
;
1726 c
->block_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
1727 c
->block_count
.stat
[0].uvalue
= t_block_count
;
1729 c
->block_error
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
1730 c
->block_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
1737 * The functions below are called via DVB callbacks, so they need to
1738 * properly use the I2C gate control
1741 static int mb86a20s_initfe(struct dvb_frontend
*fe
)
1743 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1748 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1750 if (fe
->ops
.i2c_gate_ctrl
)
1751 fe
->ops
.i2c_gate_ctrl(fe
, 0);
1753 /* Initialize the frontend */
1754 rc
= mb86a20s_writeregdata(state
, mb86a20s_init1
);
1758 /* Adjust IF frequency to match tuner */
1759 if (fe
->ops
.tuner_ops
.get_if_frequency
)
1760 fe
->ops
.tuner_ops
.get_if_frequency(fe
, &state
->if_freq
);
1762 if (!state
->if_freq
)
1763 state
->if_freq
= 3300000;
1765 /* pll = freq[Hz] * 2^24/10^6 / 16.285714286 */
1766 pll
= state
->if_freq
* 1677721600L;
1767 do_div(pll
, 1628571429L);
1768 rc
= mb86a20s_writereg(state
, 0x28, 0x20);
1771 rc
= mb86a20s_writereg(state
, 0x29, (pll
>> 16) & 0xff);
1774 rc
= mb86a20s_writereg(state
, 0x2a, (pll
>> 8) & 0xff);
1777 rc
= mb86a20s_writereg(state
, 0x2b, pll
& 0xff);
1780 dev_dbg(&state
->i2c
->dev
, "%s: IF=%d, PLL=0x%06llx\n",
1781 __func__
, state
->if_freq
, (long long)pll
);
1783 if (!state
->config
->is_serial
) {
1786 rc
= mb86a20s_writereg(state
, 0x50, 0xd5);
1789 rc
= mb86a20s_writereg(state
, 0x51, regD5
);
1794 rc
= mb86a20s_writeregdata(state
, mb86a20s_init2
);
1800 if (fe
->ops
.i2c_gate_ctrl
)
1801 fe
->ops
.i2c_gate_ctrl(fe
, 1);
1804 state
->need_init
= true;
1805 dev_info(&state
->i2c
->dev
,
1806 "mb86a20s: Init failed. Will try again later\n");
1808 state
->need_init
= false;
1809 dev_dbg(&state
->i2c
->dev
, "Initialization succeeded.\n");
1814 static int mb86a20s_set_frontend(struct dvb_frontend
*fe
)
1816 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1820 * FIXME: Properly implement the set frontend properties
1822 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1824 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1827 * Gate should already be opened, but it doesn't hurt to
1830 if (fe
->ops
.i2c_gate_ctrl
)
1831 fe
->ops
.i2c_gate_ctrl(fe
, 1);
1832 fe
->ops
.tuner_ops
.set_params(fe
);
1834 if (fe
->ops
.tuner_ops
.get_if_frequency
) {
1835 fe
->ops
.tuner_ops
.get_if_frequency(fe
, &if_freq
);
1838 * If the IF frequency changed, re-initialize the
1839 * frontend. This is needed by some drivers like tda18271,
1840 * that only sets the IF after receiving a set_params() call
1842 if (if_freq
!= state
->if_freq
)
1843 state
->need_init
= true;
1847 * Make it more reliable: if, for some reason, the initial
1848 * device initialization doesn't happen, initialize it when
1849 * a SBTVD parameters are adjusted.
1851 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
1852 * the agc callback logic is not called during DVB attach time,
1853 * causing mb86a20s to not be initialized with Kworld SBTVD.
1854 * So, this hack is needed, in order to make Kworld SBTVD to work.
1856 * It is also needed to change the IF after the initial init.
1858 if (state
->need_init
)
1859 mb86a20s_initfe(fe
);
1861 if (fe
->ops
.i2c_gate_ctrl
)
1862 fe
->ops
.i2c_gate_ctrl(fe
, 0);
1864 rc
= mb86a20s_writeregdata(state
, mb86a20s_reset_reception
);
1865 mb86a20s_reset_counters(fe
);
1867 if (fe
->ops
.i2c_gate_ctrl
)
1868 fe
->ops
.i2c_gate_ctrl(fe
, 1);
1873 static int mb86a20s_read_status_and_stats(struct dvb_frontend
*fe
,
1874 fe_status_t
*status
)
1876 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1877 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1880 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1882 if (fe
->ops
.i2c_gate_ctrl
)
1883 fe
->ops
.i2c_gate_ctrl(fe
, 0);
1886 rc
= mb86a20s_read_status(fe
, status
);
1887 if (!(*status
& FE_HAS_LOCK
)) {
1888 mb86a20s_stats_not_ready(fe
);
1889 mb86a20s_reset_frontend_cache(fe
);
1892 dev_err(&state
->i2c
->dev
,
1893 "%s: Can't read frontend lock status\n", __func__
);
1897 /* Get signal strength */
1898 rc
= mb86a20s_read_signal_strength(fe
);
1900 dev_err(&state
->i2c
->dev
,
1901 "%s: Can't reset VBER registers.\n", __func__
);
1902 mb86a20s_stats_not_ready(fe
);
1903 mb86a20s_reset_frontend_cache(fe
);
1905 rc
= 0; /* Status is OK */
1908 /* Fill signal strength */
1909 c
->strength
.stat
[0].uvalue
= rc
;
1911 if (*status
& FE_HAS_LOCK
) {
1913 rc
= mb86a20s_get_frontend(fe
);
1915 dev_err(&state
->i2c
->dev
,
1916 "%s: Can't get FE TMCC data.\n", __func__
);
1917 rc
= 0; /* Status is OK */
1921 /* Get statistics */
1922 rc
= mb86a20s_get_stats(fe
);
1923 if (rc
< 0 && rc
!= -EBUSY
) {
1924 dev_err(&state
->i2c
->dev
,
1925 "%s: Can't get FE statistics.\n", __func__
);
1929 rc
= 0; /* Don't return EBUSY to userspace */
1934 mb86a20s_stats_not_ready(fe
);
1937 if (fe
->ops
.i2c_gate_ctrl
)
1938 fe
->ops
.i2c_gate_ctrl(fe
, 1);
1943 static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend
*fe
,
1946 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1949 *strength
= c
->strength
.stat
[0].uvalue
;
1954 static int mb86a20s_get_frontend_dummy(struct dvb_frontend
*fe
)
1957 * get_frontend is now handled together with other stats
1958 * retrival, when read_status() is called, as some statistics
1959 * will depend on the layers detection.
1964 static int mb86a20s_tune(struct dvb_frontend
*fe
,
1966 unsigned int mode_flags
,
1967 unsigned int *delay
,
1968 fe_status_t
*status
)
1970 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1973 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1976 rc
= mb86a20s_set_frontend(fe
);
1978 if (!(mode_flags
& FE_TUNE_MODE_ONESHOT
))
1979 mb86a20s_read_status_and_stats(fe
, status
);
1984 static void mb86a20s_release(struct dvb_frontend
*fe
)
1986 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1988 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1993 static struct dvb_frontend_ops mb86a20s_ops
;
1995 struct dvb_frontend
*mb86a20s_attach(const struct mb86a20s_config
*config
,
1996 struct i2c_adapter
*i2c
)
1998 struct mb86a20s_state
*state
;
2001 dev_dbg(&i2c
->dev
, "%s called.\n", __func__
);
2003 /* allocate memory for the internal state */
2004 state
= kzalloc(sizeof(struct mb86a20s_state
), GFP_KERNEL
);
2005 if (state
== NULL
) {
2007 "%s: unable to allocate memory for state\n", __func__
);
2011 /* setup the state */
2012 state
->config
= config
;
2015 /* create dvb_frontend */
2016 memcpy(&state
->frontend
.ops
, &mb86a20s_ops
,
2017 sizeof(struct dvb_frontend_ops
));
2018 state
->frontend
.demodulator_priv
= state
;
2020 /* Check if it is a mb86a20s frontend */
2021 rev
= mb86a20s_readreg(state
, 0);
2025 "Detected a Fujitsu mb86a20s frontend\n");
2028 "Frontend revision %d is unknown - aborting.\n",
2033 return &state
->frontend
;
2039 EXPORT_SYMBOL(mb86a20s_attach
);
2041 static struct dvb_frontend_ops mb86a20s_ops
= {
2042 .delsys
= { SYS_ISDBT
},
2043 /* Use dib8000 values per default */
2045 .name
= "Fujitsu mb86A20s",
2046 .caps
= FE_CAN_INVERSION_AUTO
| FE_CAN_RECOVER
|
2047 FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
2048 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
2049 FE_CAN_QPSK
| FE_CAN_QAM_16
| FE_CAN_QAM_64
|
2050 FE_CAN_TRANSMISSION_MODE_AUTO
| FE_CAN_QAM_AUTO
|
2051 FE_CAN_GUARD_INTERVAL_AUTO
| FE_CAN_HIERARCHY_AUTO
,
2052 /* Actually, those values depend on the used tuner */
2053 .frequency_min
= 45000000,
2054 .frequency_max
= 864000000,
2055 .frequency_stepsize
= 62500,
2058 .release
= mb86a20s_release
,
2060 .init
= mb86a20s_initfe
,
2061 .set_frontend
= mb86a20s_set_frontend
,
2062 .get_frontend
= mb86a20s_get_frontend_dummy
,
2063 .read_status
= mb86a20s_read_status_and_stats
,
2064 .read_signal_strength
= mb86a20s_read_signal_strength_from_cache
,
2065 .tune
= mb86a20s_tune
,
2068 MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
2069 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
2070 MODULE_LICENSE("GPL");