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
;
34 u32 estimated_rate
[3];
44 #define BER_SAMPLING_RATE 1 /* Seconds */
47 * Initialization sequence: Use whatevere default values that PV SBTVD
48 * does on its initialisation, obtained via USB snoop
50 static struct regdata mb86a20s_init
[] = {
55 { 0x50, 0xd1 }, { 0x51, 0x22 },
58 { 0x28, 0x2a }, { 0x29, 0x00 }, { 0x2a, 0xff }, { 0x2b, 0x80 },
59 { 0x28, 0x20 }, { 0x29, 0x33 }, { 0x2a, 0xdf }, { 0x2b, 0xa9 },
60 { 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
64 { 0x04, 0x08 }, { 0x05, 0x05 },
65 { 0x04, 0x0e }, { 0x05, 0x00 },
66 { 0x04, 0x0f }, { 0x05, 0x14 },
67 { 0x04, 0x0b }, { 0x05, 0x8c },
68 { 0x04, 0x00 }, { 0x05, 0x00 },
69 { 0x04, 0x01 }, { 0x05, 0x07 },
70 { 0x04, 0x02 }, { 0x05, 0x0f },
71 { 0x04, 0x03 }, { 0x05, 0xa0 },
72 { 0x04, 0x09 }, { 0x05, 0x00 },
73 { 0x04, 0x0a }, { 0x05, 0xff },
74 { 0x04, 0x27 }, { 0x05, 0x64 },
75 { 0x04, 0x28 }, { 0x05, 0x00 },
76 { 0x04, 0x1e }, { 0x05, 0xff },
77 { 0x04, 0x29 }, { 0x05, 0x0a },
78 { 0x04, 0x32 }, { 0x05, 0x0a },
79 { 0x04, 0x14 }, { 0x05, 0x02 },
80 { 0x04, 0x04 }, { 0x05, 0x00 },
81 { 0x04, 0x05 }, { 0x05, 0x22 },
82 { 0x04, 0x06 }, { 0x05, 0x0e },
83 { 0x04, 0x07 }, { 0x05, 0xd8 },
84 { 0x04, 0x12 }, { 0x05, 0x00 },
85 { 0x04, 0x13 }, { 0x05, 0xff },
86 { 0x04, 0x15 }, { 0x05, 0x4e },
87 { 0x04, 0x16 }, { 0x05, 0x20 },
90 * On this demod, when the bit count reaches the count below,
91 * it collects the bit error count. The bit counters are initialized
92 * to 65535 here. This warrants that all of them will be quickly
93 * calculated when device gets locked. As TMCC is parsed, the values
94 * will be adjusted later in the driver's code.
96 { 0x52, 0x01 }, /* Turn on BER before Viterbi */
97 { 0x50, 0xa7 }, { 0x51, 0x00 },
98 { 0x50, 0xa8 }, { 0x51, 0xff },
99 { 0x50, 0xa9 }, { 0x51, 0xff },
100 { 0x50, 0xaa }, { 0x51, 0x00 },
101 { 0x50, 0xab }, { 0x51, 0xff },
102 { 0x50, 0xac }, { 0x51, 0xff },
103 { 0x50, 0xad }, { 0x51, 0x00 },
104 { 0x50, 0xae }, { 0x51, 0xff },
105 { 0x50, 0xaf }, { 0x51, 0xff },
107 { 0x5e, 0x00 }, /* Turn off BER after Viterbi */
108 { 0x50, 0xdc }, { 0x51, 0x01 },
109 { 0x50, 0xdd }, { 0x51, 0xf4 },
110 { 0x50, 0xde }, { 0x51, 0x01 },
111 { 0x50, 0xdf }, { 0x51, 0xf4 },
112 { 0x50, 0xe0 }, { 0x51, 0x01 },
113 { 0x50, 0xe1 }, { 0x51, 0xf4 },
114 { 0x50, 0xb0 }, { 0x51, 0x07 },
115 { 0x50, 0xb2 }, { 0x51, 0xff },
116 { 0x50, 0xb3 }, { 0x51, 0xff },
117 { 0x50, 0xb4 }, { 0x51, 0xff },
118 { 0x50, 0xb5 }, { 0x51, 0xff },
119 { 0x50, 0xb6 }, { 0x51, 0xff },
120 { 0x50, 0xb7 }, { 0x51, 0xff },
122 { 0x50, 0x50 }, { 0x51, 0x02 }, /* MER manual mode */
123 { 0x50, 0x51 }, { 0x51, 0x04 }, /* MER symbol 4 */
124 { 0x45, 0x04 }, /* CN symbol 4 */
125 { 0x48, 0x04 }, /* CN manual mode */
127 { 0x50, 0xd5 }, { 0x51, 0x01 }, /* Serial */
128 { 0x50, 0xd6 }, { 0x51, 0x1f },
129 { 0x50, 0xd2 }, { 0x51, 0x03 },
130 { 0x50, 0xd7 }, { 0x51, 0x3f },
131 { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x28, 0x74 }, { 0x29, 0x40 },
132 { 0x28, 0x46 }, { 0x29, 0x2c }, { 0x28, 0x46 }, { 0x29, 0x0c },
134 { 0x04, 0x40 }, { 0x05, 0x00 },
135 { 0x28, 0x00 }, { 0x29, 0x10 },
136 { 0x28, 0x05 }, { 0x29, 0x02 },
138 { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x03 },
139 { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0d },
140 { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
141 { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x01 },
142 { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x21 },
143 { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x29 },
144 { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
145 { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x31 },
146 { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0e },
147 { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x4e },
148 { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x46 },
149 { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
150 { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x56 },
151 { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x35 },
152 { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbe },
153 { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0x84 },
154 { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x03 }, { 0x2b, 0xee },
155 { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x98 },
156 { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x9f },
157 { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xb2 },
158 { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0xc2 },
159 { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0x4a },
160 { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbc },
161 { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x04 }, { 0x2b, 0xba },
162 { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0x14 },
163 { 0x50, 0x1e }, { 0x51, 0x5d },
164 { 0x50, 0x22 }, { 0x51, 0x00 },
165 { 0x50, 0x23 }, { 0x51, 0xc8 },
166 { 0x50, 0x24 }, { 0x51, 0x00 },
167 { 0x50, 0x25 }, { 0x51, 0xf0 },
168 { 0x50, 0x26 }, { 0x51, 0x00 },
169 { 0x50, 0x27 }, { 0x51, 0xc3 },
170 { 0x50, 0x39 }, { 0x51, 0x02 },
171 { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
175 static struct regdata mb86a20s_reset_reception
[] = {
182 static struct regdata mb86a20s_vber_reset
[] = {
183 { 0x53, 0x00 }, /* VBER Counter reset */
187 static struct regdata mb86a20s_per_reset
[] = {
188 { 0x50, 0xb1 }, /* PER Counter reset */
194 * I2C read/write functions and macros
197 static int mb86a20s_i2c_writereg(struct mb86a20s_state
*state
,
198 u8 i2c_addr
, u8 reg
, u8 data
)
200 u8 buf
[] = { reg
, data
};
201 struct i2c_msg msg
= {
202 .addr
= i2c_addr
, .flags
= 0, .buf
= buf
, .len
= 2
206 rc
= i2c_transfer(state
->i2c
, &msg
, 1);
208 dev_err(&state
->i2c
->dev
,
209 "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
210 __func__
, rc
, reg
, data
);
217 static int mb86a20s_i2c_writeregdata(struct mb86a20s_state
*state
,
218 u8 i2c_addr
, struct regdata
*rd
, int size
)
222 for (i
= 0; i
< size
; i
++) {
223 rc
= mb86a20s_i2c_writereg(state
, i2c_addr
, rd
[i
].reg
,
231 static int mb86a20s_i2c_readreg(struct mb86a20s_state
*state
,
236 struct i2c_msg msg
[] = {
237 { .addr
= i2c_addr
, .flags
= 0, .buf
= ®
, .len
= 1 },
238 { .addr
= i2c_addr
, .flags
= I2C_M_RD
, .buf
= &val
, .len
= 1 }
241 rc
= i2c_transfer(state
->i2c
, msg
, 2);
244 dev_err(&state
->i2c
->dev
, "%s: reg=0x%x (error=%d)\n",
246 return (rc
< 0) ? rc
: -EIO
;
252 #define mb86a20s_readreg(state, reg) \
253 mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
254 #define mb86a20s_writereg(state, reg, val) \
255 mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
256 #define mb86a20s_writeregdata(state, regdata) \
257 mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
258 regdata, ARRAY_SIZE(regdata))
261 * Ancillary internal routines (likely compiled inlined)
263 * The functions below assume that gateway lock has already obtained
266 static int mb86a20s_read_status(struct dvb_frontend
*fe
, fe_status_t
*status
)
268 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
273 val
= mb86a20s_readreg(state
, 0x0a) & 0xf;
278 *status
|= FE_HAS_SIGNAL
;
281 *status
|= FE_HAS_CARRIER
;
284 *status
|= FE_HAS_VITERBI
;
287 *status
|= FE_HAS_SYNC
;
289 if (val
>= 8) /* Maybe 9? */
290 *status
|= FE_HAS_LOCK
;
292 dev_dbg(&state
->i2c
->dev
, "%s: Status = 0x%02x (state = %d)\n",
293 __func__
, *status
, val
);
298 static int mb86a20s_read_signal_strength(struct dvb_frontend
*fe
)
300 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
302 unsigned rf_max
, rf_min
, rf
;
304 /* Does a binary search to get RF strength */
308 rf
= (rf_max
+ rf_min
) / 2;
309 rc
= mb86a20s_writereg(state
, 0x04, 0x1f);
312 rc
= mb86a20s_writereg(state
, 0x05, rf
>> 8);
315 rc
= mb86a20s_writereg(state
, 0x04, 0x20);
318 rc
= mb86a20s_writereg(state
, 0x04, rf
);
322 rc
= mb86a20s_readreg(state
, 0x02);
326 rf_min
= (rf_max
+ rf_min
) / 2;
328 rf_max
= (rf_max
+ rf_min
) / 2;
329 if (rf_max
- rf_min
< 4) {
330 rf
= (rf_max
+ rf_min
) / 2;
332 /* Rescale it from 2^12 (4096) to 2^16 */
334 dev_dbg(&state
->i2c
->dev
,
335 "%s: signal strength = %d (%d < RF=%d < %d)\n",
336 __func__
, rf
, rf_min
, rf
>> 4, rf_max
);
344 static int mb86a20s_get_modulation(struct mb86a20s_state
*state
,
348 static unsigned char reg
[] = {
349 [0] = 0x86, /* Layer A */
350 [1] = 0x8a, /* Layer B */
351 [2] = 0x8e, /* Layer C */
354 if (layer
>= ARRAY_SIZE(reg
))
356 rc
= mb86a20s_writereg(state
, 0x6d, reg
[layer
]);
359 rc
= mb86a20s_readreg(state
, 0x6e);
362 switch ((rc
>> 4) & 0x07) {
376 static int mb86a20s_get_fec(struct mb86a20s_state
*state
,
381 static unsigned char reg
[] = {
382 [0] = 0x87, /* Layer A */
383 [1] = 0x8b, /* Layer B */
384 [2] = 0x8f, /* Layer C */
387 if (layer
>= ARRAY_SIZE(reg
))
389 rc
= mb86a20s_writereg(state
, 0x6d, reg
[layer
]);
392 rc
= mb86a20s_readreg(state
, 0x6e);
395 switch ((rc
>> 4) & 0x07) {
411 static int mb86a20s_get_interleaving(struct mb86a20s_state
*state
,
416 static unsigned char reg
[] = {
417 [0] = 0x88, /* Layer A */
418 [1] = 0x8c, /* Layer B */
419 [2] = 0x90, /* Layer C */
422 if (layer
>= ARRAY_SIZE(reg
))
424 rc
= mb86a20s_writereg(state
, 0x6d, reg
[layer
]);
427 rc
= mb86a20s_readreg(state
, 0x6e);
431 switch ((rc
>> 4) & 0x07) {
433 return GUARD_INTERVAL_1_4
;
435 return GUARD_INTERVAL_1_8
;
437 return GUARD_INTERVAL_1_16
;
439 return GUARD_INTERVAL_1_32
;
443 return GUARD_INTERVAL_AUTO
;
447 static int mb86a20s_get_segment_count(struct mb86a20s_state
*state
,
451 static unsigned char reg
[] = {
452 [0] = 0x89, /* Layer A */
453 [1] = 0x8d, /* Layer B */
454 [2] = 0x91, /* Layer C */
457 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
459 if (layer
>= ARRAY_SIZE(reg
))
462 rc
= mb86a20s_writereg(state
, 0x6d, reg
[layer
]);
465 rc
= mb86a20s_readreg(state
, 0x6e);
468 count
= (rc
>> 4) & 0x0f;
470 dev_dbg(&state
->i2c
->dev
, "%s: segments: %d.\n", __func__
, count
);
475 static void mb86a20s_reset_frontend_cache(struct dvb_frontend
*fe
)
477 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
478 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
480 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
482 /* Fixed parameters */
483 c
->delivery_system
= SYS_ISDBT
;
484 c
->bandwidth_hz
= 6000000;
486 /* Initialize values that will be later autodetected */
487 c
->isdbt_layer_enabled
= 0;
488 c
->transmission_mode
= TRANSMISSION_MODE_AUTO
;
489 c
->guard_interval
= GUARD_INTERVAL_AUTO
;
490 c
->isdbt_sb_mode
= 0;
491 c
->isdbt_sb_segment_count
= 0;
495 * Estimates the bit rate using the per-segment bit rate given by
496 * ABNT/NBR 15601 spec (table 4).
498 static u32 isdbt_rate
[3][5][4] = {
500 { 280850, 312060, 330420, 340430 }, /* 1/2 */
501 { 374470, 416080, 440560, 453910 }, /* 2/3 */
502 { 421280, 468090, 495630, 510650 }, /* 3/4 */
503 { 468090, 520100, 550700, 567390 }, /* 5/6 */
504 { 491500, 546110, 578230, 595760 }, /* 7/8 */
506 { 561710, 624130, 660840, 680870 }, /* 1/2 */
507 { 748950, 832170, 881120, 907820 }, /* 2/3 */
508 { 842570, 936190, 991260, 1021300 }, /* 3/4 */
509 { 936190, 1040210, 1101400, 1134780 }, /* 5/6 */
510 { 983000, 1092220, 1156470, 1191520 }, /* 7/8 */
512 { 842570, 936190, 991260, 1021300 }, /* 1/2 */
513 { 1123430, 1248260, 1321680, 1361740 }, /* 2/3 */
514 { 1263860, 1404290, 1486900, 1531950 }, /* 3/4 */
515 { 1404290, 1560320, 1652110, 1702170 }, /* 5/6 */
516 { 1474500, 1638340, 1734710, 1787280 }, /* 7/8 */
520 static void mb86a20s_layer_bitrate(struct dvb_frontend
*fe
, u32 layer
,
521 u32 modulation
, u32 fec
, u32 interleaving
,
524 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
529 * If modulation/fec/interleaving is not detected, the default is
530 * to consider the lowest bit rate, to avoid taking too long time
533 switch (modulation
) {
567 switch (interleaving
) {
569 case GUARD_INTERVAL_1_4
:
572 case GUARD_INTERVAL_1_8
:
575 case GUARD_INTERVAL_1_16
:
578 case GUARD_INTERVAL_1_32
:
583 /* Samples BER at BER_SAMPLING_RATE seconds */
584 rate
= isdbt_rate
[m
][f
][i
] * segment
* BER_SAMPLING_RATE
;
586 /* Avoids sampling too quickly or to overflow the register */
589 else if (rate
> (1 << 24) - 1)
590 rate
= (1 << 24) - 1;
592 dev_dbg(&state
->i2c
->dev
,
593 "%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n",
594 __func__
, 'A' + layer
, segment
* isdbt_rate
[m
][f
][i
]/1000,
597 state
->estimated_rate
[i
] = rate
;
601 static int mb86a20s_get_frontend(struct dvb_frontend
*fe
)
603 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
604 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
607 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
609 /* Reset frontend cache to default values */
610 mb86a20s_reset_frontend_cache(fe
);
612 /* Check for partial reception */
613 rc
= mb86a20s_writereg(state
, 0x6d, 0x85);
616 rc
= mb86a20s_readreg(state
, 0x6e);
619 c
->isdbt_partial_reception
= (rc
& 0x10) ? 1 : 0;
621 /* Get per-layer data */
623 for (i
= 0; i
< 3; i
++) {
624 dev_dbg(&state
->i2c
->dev
, "%s: getting data for layer %c.\n",
627 rc
= mb86a20s_get_segment_count(state
, i
);
629 goto noperlayer_error
;
630 if (rc
>= 0 && rc
< 14) {
631 c
->layer
[i
].segment_count
= rc
;
633 c
->layer
[i
].segment_count
= 0;
634 state
->estimated_rate
[i
] = 0;
637 c
->isdbt_layer_enabled
|= 1 << i
;
638 rc
= mb86a20s_get_modulation(state
, i
);
640 goto noperlayer_error
;
641 dev_dbg(&state
->i2c
->dev
, "%s: modulation %d.\n",
643 c
->layer
[i
].modulation
= rc
;
644 rc
= mb86a20s_get_fec(state
, i
);
646 goto noperlayer_error
;
647 dev_dbg(&state
->i2c
->dev
, "%s: FEC %d.\n",
649 c
->layer
[i
].fec
= rc
;
650 rc
= mb86a20s_get_interleaving(state
, i
);
652 goto noperlayer_error
;
653 dev_dbg(&state
->i2c
->dev
, "%s: interleaving %d.\n",
655 c
->layer
[i
].interleaving
= rc
;
656 mb86a20s_layer_bitrate(fe
, i
, c
->layer
[i
].modulation
,
658 c
->layer
[i
].interleaving
,
659 c
->layer
[i
].segment_count
);
662 rc
= mb86a20s_writereg(state
, 0x6d, 0x84);
665 if ((rc
& 0x60) == 0x20) {
666 c
->isdbt_sb_mode
= 1;
667 /* At least, one segment should exist */
668 if (!c
->isdbt_sb_segment_count
)
669 c
->isdbt_sb_segment_count
= 1;
672 /* Get transmission mode and guard interval */
673 rc
= mb86a20s_readreg(state
, 0x07);
676 if ((rc
& 0x60) == 0x20) {
677 switch (rc
& 0x0c >> 2) {
679 c
->transmission_mode
= TRANSMISSION_MODE_2K
;
682 c
->transmission_mode
= TRANSMISSION_MODE_4K
;
685 c
->transmission_mode
= TRANSMISSION_MODE_8K
;
692 c
->guard_interval
= GUARD_INTERVAL_1_4
;
695 c
->guard_interval
= GUARD_INTERVAL_1_8
;
698 c
->guard_interval
= GUARD_INTERVAL_1_16
;
706 /* per-layer info is incomplete; discard all per-layer */
707 c
->isdbt_layer_enabled
= 0;
712 static int mb86a20s_reset_counters(struct dvb_frontend
*fe
)
714 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
715 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
718 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
720 /* Reset the counters, if the channel changed */
721 if (state
->last_frequency
!= c
->frequency
) {
722 memset(&c
->strength
, 0, sizeof(c
->strength
));
723 memset(&c
->cnr
, 0, sizeof(c
->cnr
));
724 memset(&c
->pre_bit_error
, 0, sizeof(c
->pre_bit_error
));
725 memset(&c
->pre_bit_count
, 0, sizeof(c
->pre_bit_count
));
726 memset(&c
->block_error
, 0, sizeof(c
->block_error
));
727 memset(&c
->block_count
, 0, sizeof(c
->block_count
));
729 state
->last_frequency
= c
->frequency
;
732 /* Clear status for most stats */
734 /* BER counter reset */
735 rc
= mb86a20s_writeregdata(state
, mb86a20s_vber_reset
);
739 /* MER, PER counter reset */
740 rc
= mb86a20s_writeregdata(state
, mb86a20s_per_reset
);
744 /* CNR counter reset */
745 rc
= mb86a20s_readreg(state
, 0x45);
749 rc
= mb86a20s_writereg(state
, 0x45, val
| 0x10);
752 rc
= mb86a20s_writereg(state
, 0x45, val
& 0x6f);
756 /* MER counter reset */
757 rc
= mb86a20s_writereg(state
, 0x50, 0x50);
760 rc
= mb86a20s_readreg(state
, 0x51);
764 rc
= mb86a20s_writereg(state
, 0x51, val
| 0x01);
767 rc
= mb86a20s_writereg(state
, 0x51, val
& 0x06);
773 dev_err(&state
->i2c
->dev
,
774 "%s: Can't reset FE statistics (error %d).\n",
780 static int mb86a20s_get_ber_before_vterbi(struct dvb_frontend
*fe
,
782 u32
*error
, u32
*count
)
784 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
787 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
792 /* Check if the BER measures are already available */
793 rc
= mb86a20s_readreg(state
, 0x54);
797 /* Check if data is available for that layer */
798 if (!(rc
& (1 << layer
))) {
799 dev_dbg(&state
->i2c
->dev
,
800 "%s: BER for layer %c is not available yet.\n",
801 __func__
, 'A' + layer
);
805 /* Read Bit Error Count */
806 rc
= mb86a20s_readreg(state
, 0x55 + layer
* 3);
810 rc
= mb86a20s_readreg(state
, 0x56 + layer
* 3);
814 rc
= mb86a20s_readreg(state
, 0x57 + layer
* 3);
819 dev_dbg(&state
->i2c
->dev
,
820 "%s: bit error before Viterbi for layer %c: %d.\n",
821 __func__
, 'A' + layer
, *error
);
824 rc
= mb86a20s_writereg(state
, 0x50, 0xa7 + layer
* 3);
827 rc
= mb86a20s_readreg(state
, 0x51);
831 rc
= mb86a20s_writereg(state
, 0x50, 0xa8 + layer
* 3);
834 rc
= mb86a20s_readreg(state
, 0x51);
838 rc
= mb86a20s_writereg(state
, 0x50, 0xa9 + layer
* 3);
841 rc
= mb86a20s_readreg(state
, 0x51);
846 dev_dbg(&state
->i2c
->dev
,
847 "%s: bit count before Viterbi for layer %c: %d.\n",
848 __func__
, 'A' + layer
, *count
);
852 * As we get TMCC data from the frontend, we can better estimate the
853 * BER bit counters, in order to do the BER measure during a longer
854 * time. Use those data, if available, to update the bit count
858 if (state
->estimated_rate
[layer
]
859 && state
->estimated_rate
[layer
] != *count
) {
860 dev_dbg(&state
->i2c
->dev
,
861 "%s: updating layer %c counter to %d.\n",
862 __func__
, 'A' + layer
, state
->estimated_rate
[layer
]);
863 rc
= mb86a20s_writereg(state
, 0x50, 0xa7 + layer
* 3);
866 rc
= mb86a20s_writereg(state
, 0x51,
867 state
->estimated_rate
[layer
] >> 16);
870 rc
= mb86a20s_writereg(state
, 0x50, 0xa8 + layer
* 3);
873 rc
= mb86a20s_writereg(state
, 0x51,
874 state
->estimated_rate
[layer
] >> 8);
877 rc
= mb86a20s_writereg(state
, 0x50, 0xa9 + layer
* 3);
880 rc
= mb86a20s_writereg(state
, 0x51,
881 state
->estimated_rate
[layer
]);
887 /* Reset counter to collect new data */
888 rc
= mb86a20s_writereg(state
, 0x53, 0x07 & ~(1 << layer
));
891 rc
= mb86a20s_writereg(state
, 0x53, 0x07);
896 struct linear_segments
{
901 * All tables below return a dB/1000 measurement
904 static struct linear_segments cnr_to_db_table
[] = {
938 static struct linear_segments cnr_64qam_table
[] = {
972 static struct linear_segments cnr_16qam_table
[] = {
1006 struct linear_segments cnr_qpsk_table
[] = {
1040 static u32
interpolate_value(u32 value
, struct linear_segments
*segments
,
1047 if (value
>= segments
[0].x
)
1048 return segments
[0].y
;
1049 if (value
< segments
[len
-1].x
)
1050 return segments
[len
-1].y
;
1052 for (i
= 1; i
< len
- 1; i
++) {
1053 /* If value is identical, no need to interpolate */
1054 if (value
== segments
[i
].x
)
1055 return segments
[i
].y
;
1056 if (value
> segments
[i
].x
)
1060 /* Linear interpolation between the two (x,y) points */
1061 dy
= segments
[i
].y
- segments
[i
- 1].y
;
1062 dx
= segments
[i
- 1].x
- segments
[i
].x
;
1063 tmp64
= value
- segments
[i
].x
;
1066 ret
= segments
[i
].y
- tmp64
;
1071 static int mb86a20s_get_main_CNR(struct dvb_frontend
*fe
)
1073 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1074 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1075 u32 cnr_linear
, cnr
;
1078 /* Check if CNR is available */
1079 rc
= mb86a20s_readreg(state
, 0x45);
1084 dev_info(&state
->i2c
->dev
, "%s: CNR is not available yet.\n",
1090 rc
= mb86a20s_readreg(state
, 0x46);
1093 cnr_linear
= rc
<< 8;
1095 rc
= mb86a20s_readreg(state
, 0x46);
1100 cnr
= interpolate_value(cnr_linear
,
1101 cnr_to_db_table
, ARRAY_SIZE(cnr_to_db_table
));
1103 c
->cnr
.stat
[0].scale
= FE_SCALE_DECIBEL
;
1104 c
->cnr
.stat
[0].svalue
= cnr
;
1106 dev_dbg(&state
->i2c
->dev
, "%s: CNR is %d.%03d dB (%d)\n",
1107 __func__
, cnr
/ 1000, cnr
% 1000, cnr_linear
);
1109 /* CNR counter reset */
1110 rc
= mb86a20s_writereg(state
, 0x45, val
| 0x10);
1113 rc
= mb86a20s_writereg(state
, 0x45, val
& 0x6f);
1118 static int mb86a20s_get_per_layer_CNR(struct dvb_frontend
*fe
)
1120 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1121 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1124 struct linear_segments
*segs
;
1127 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1129 /* Check if the measures are already available */
1130 rc
= mb86a20s_writereg(state
, 0x50, 0x5b);
1133 rc
= mb86a20s_readreg(state
, 0x51);
1137 /* Check if data is available */
1139 dev_info(&state
->i2c
->dev
,
1140 "%s: MER measures aren't available yet.\n", __func__
);
1144 /* Read all layers */
1145 for (i
= 0; i
< 3; i
++) {
1146 if (!(c
->isdbt_layer_enabled
& (1 << i
))) {
1147 c
->cnr
.stat
[1 + i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1151 rc
= mb86a20s_writereg(state
, 0x50, 0x52 + i
* 3);
1154 rc
= mb86a20s_readreg(state
, 0x51);
1158 rc
= mb86a20s_writereg(state
, 0x50, 0x53 + i
* 3);
1161 rc
= mb86a20s_readreg(state
, 0x51);
1165 rc
= mb86a20s_writereg(state
, 0x50, 0x54 + i
* 3);
1168 rc
= mb86a20s_readreg(state
, 0x51);
1173 switch (c
->layer
[i
].modulation
) {
1176 segs
= cnr_qpsk_table
;
1177 segs_len
= ARRAY_SIZE(cnr_qpsk_table
);
1180 segs
= cnr_16qam_table
;
1181 segs_len
= ARRAY_SIZE(cnr_16qam_table
);
1185 segs
= cnr_64qam_table
;
1186 segs_len
= ARRAY_SIZE(cnr_64qam_table
);
1189 cnr
= interpolate_value(mer
, segs
, segs_len
);
1191 c
->cnr
.stat
[1 + i
].scale
= FE_SCALE_DECIBEL
;
1192 c
->cnr
.stat
[1 + i
].svalue
= cnr
;
1194 dev_dbg(&state
->i2c
->dev
,
1195 "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
1196 __func__
, 'A' + i
, cnr
/ 1000, cnr
% 1000, mer
);
1200 /* Start a new MER measurement */
1201 /* MER counter reset */
1202 rc
= mb86a20s_writereg(state
, 0x50, 0x50);
1205 rc
= mb86a20s_readreg(state
, 0x51);
1210 rc
= mb86a20s_writereg(state
, 0x51, val
| 0x01);
1213 rc
= mb86a20s_writereg(state
, 0x51, val
& 0x06);
1220 static void mb86a20s_stats_not_ready(struct dvb_frontend
*fe
)
1222 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1223 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1226 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1228 /* Fill the length of each status counter */
1230 /* Only global stats */
1231 c
->strength
.len
= 1;
1233 /* Per-layer stats - 3 layers + global */
1235 c
->pre_bit_error
.len
= 4;
1236 c
->pre_bit_count
.len
= 4;
1237 c
->block_error
.len
= 4;
1238 c
->block_count
.len
= 4;
1240 /* Signal is always available */
1241 c
->strength
.stat
[0].scale
= FE_SCALE_RELATIVE
;
1242 c
->strength
.stat
[0].uvalue
= 0;
1244 /* Put all of them at FE_SCALE_NOT_AVAILABLE */
1245 for (i
= 0; i
< 4; i
++) {
1246 c
->cnr
.stat
[i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1247 c
->pre_bit_error
.stat
[i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1248 c
->pre_bit_count
.stat
[i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1249 c
->block_error
.stat
[i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1250 c
->block_count
.stat
[i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1254 static int mb86a20s_get_stats(struct dvb_frontend
*fe
)
1256 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1257 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1259 u32 bit_error
= 0, bit_count
= 0;
1260 u32 t_pre_bit_error
= 0, t_pre_bit_count
= 0;
1261 int active_layers
= 0, ber_layers
= 0;
1263 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1265 mb86a20s_get_main_CNR(fe
);
1267 /* Get per-layer stats */
1268 mb86a20s_get_per_layer_CNR(fe
);
1270 for (i
= 0; i
< 3; i
++) {
1271 if (c
->isdbt_layer_enabled
& (1 << i
)) {
1272 /* Layer is active and has rc segments */
1275 /* Read per-layer BER */
1276 /* Handle BER before vterbi */
1277 rc
= mb86a20s_get_ber_before_vterbi(fe
, i
,
1281 c
->pre_bit_error
.stat
[1 + i
].scale
= FE_SCALE_COUNTER
;
1282 c
->pre_bit_error
.stat
[1 + i
].uvalue
+= bit_error
;
1283 c
->pre_bit_count
.stat
[1 + i
].scale
= FE_SCALE_COUNTER
;
1284 c
->pre_bit_count
.stat
[1 + i
].uvalue
+= bit_count
;
1285 } else if (rc
!= -EBUSY
) {
1287 * If an I/O error happened,
1288 * measures are now unavailable
1290 c
->pre_bit_error
.stat
[1 + i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1291 c
->pre_bit_count
.stat
[1 + i
].scale
= FE_SCALE_NOT_AVAILABLE
;
1292 dev_err(&state
->i2c
->dev
,
1293 "%s: Can't get BER for layer %c (error %d).\n",
1294 __func__
, 'A' + i
, rc
);
1297 if (c
->block_error
.stat
[1 + i
].scale
!= FE_SCALE_NOT_AVAILABLE
)
1300 /* Update total BER */
1301 t_pre_bit_error
+= c
->pre_bit_error
.stat
[1 + i
].uvalue
;
1302 t_pre_bit_count
+= c
->pre_bit_count
.stat
[1 + i
].uvalue
;
1307 * Start showing global count if at least one error count is
1312 * At least one per-layer BER measure was read. We can now
1313 * calculate the total BER
1315 * Total Bit Error/Count is calculated as the sum of the
1316 * bit errors on all active layers.
1318 c
->pre_bit_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
1319 c
->pre_bit_error
.stat
[0].uvalue
= t_pre_bit_error
;
1320 c
->pre_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
1321 c
->pre_bit_count
.stat
[0].uvalue
= t_pre_bit_count
;
1328 * The functions below are called via DVB callbacks, so they need to
1329 * properly use the I2C gate control
1332 static int mb86a20s_initfe(struct dvb_frontend
*fe
)
1334 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1338 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1340 if (fe
->ops
.i2c_gate_ctrl
)
1341 fe
->ops
.i2c_gate_ctrl(fe
, 0);
1343 /* Initialize the frontend */
1344 rc
= mb86a20s_writeregdata(state
, mb86a20s_init
);
1348 if (!state
->config
->is_serial
) {
1351 rc
= mb86a20s_writereg(state
, 0x50, 0xd5);
1354 rc
= mb86a20s_writereg(state
, 0x51, regD5
);
1360 if (fe
->ops
.i2c_gate_ctrl
)
1361 fe
->ops
.i2c_gate_ctrl(fe
, 1);
1364 state
->need_init
= true;
1365 dev_info(&state
->i2c
->dev
,
1366 "mb86a20s: Init failed. Will try again later\n");
1368 state
->need_init
= false;
1369 dev_dbg(&state
->i2c
->dev
, "Initialization succeeded.\n");
1374 static int mb86a20s_set_frontend(struct dvb_frontend
*fe
)
1376 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1380 * FIXME: Properly implement the set frontend properties
1382 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1384 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1387 * Gate should already be opened, but it doesn't hurt to
1390 if (fe
->ops
.i2c_gate_ctrl
)
1391 fe
->ops
.i2c_gate_ctrl(fe
, 1);
1392 fe
->ops
.tuner_ops
.set_params(fe
);
1395 * Make it more reliable: if, for some reason, the initial
1396 * device initialization doesn't happen, initialize it when
1397 * a SBTVD parameters are adjusted.
1399 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
1400 * the agc callback logic is not called during DVB attach time,
1401 * causing mb86a20s to not be initialized with Kworld SBTVD.
1402 * So, this hack is needed, in order to make Kworld SBTVD to work.
1404 if (state
->need_init
)
1405 mb86a20s_initfe(fe
);
1407 if (fe
->ops
.i2c_gate_ctrl
)
1408 fe
->ops
.i2c_gate_ctrl(fe
, 0);
1410 rc
= mb86a20s_writeregdata(state
, mb86a20s_reset_reception
);
1411 mb86a20s_reset_counters(fe
);
1413 if (fe
->ops
.i2c_gate_ctrl
)
1414 fe
->ops
.i2c_gate_ctrl(fe
, 1);
1419 static int mb86a20s_read_status_and_stats(struct dvb_frontend
*fe
,
1420 fe_status_t
*status
)
1422 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1423 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1426 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1428 if (fe
->ops
.i2c_gate_ctrl
)
1429 fe
->ops
.i2c_gate_ctrl(fe
, 0);
1432 rc
= mb86a20s_read_status(fe
, status
);
1433 if (!(*status
& FE_HAS_LOCK
)) {
1434 mb86a20s_stats_not_ready(fe
);
1435 mb86a20s_reset_frontend_cache(fe
);
1438 dev_err(&state
->i2c
->dev
,
1439 "%s: Can't read frontend lock status\n", __func__
);
1443 /* Get signal strength */
1444 rc
= mb86a20s_read_signal_strength(fe
);
1446 dev_err(&state
->i2c
->dev
,
1447 "%s: Can't reset VBER registers.\n", __func__
);
1448 mb86a20s_stats_not_ready(fe
);
1449 mb86a20s_reset_frontend_cache(fe
);
1451 rc
= 0; /* Status is OK */
1454 /* Fill signal strength */
1455 c
->strength
.stat
[0].uvalue
= rc
;
1457 if (*status
& FE_HAS_LOCK
) {
1459 rc
= mb86a20s_get_frontend(fe
);
1461 dev_err(&state
->i2c
->dev
,
1462 "%s: Can't get FE TMCC data.\n", __func__
);
1463 rc
= 0; /* Status is OK */
1467 /* Get statistics */
1468 rc
= mb86a20s_get_stats(fe
);
1469 if (rc
< 0 && rc
!= -EBUSY
) {
1470 dev_err(&state
->i2c
->dev
,
1471 "%s: Can't get FE statistics.\n", __func__
);
1475 rc
= 0; /* Don't return EBUSY to userspace */
1480 mb86a20s_stats_not_ready(fe
);
1483 if (fe
->ops
.i2c_gate_ctrl
)
1484 fe
->ops
.i2c_gate_ctrl(fe
, 1);
1489 static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend
*fe
,
1492 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1495 *strength
= c
->strength
.stat
[0].uvalue
;
1500 static int mb86a20s_get_frontend_dummy(struct dvb_frontend
*fe
)
1503 * get_frontend is now handled together with other stats
1504 * retrival, when read_status() is called, as some statistics
1505 * will depend on the layers detection.
1510 static int mb86a20s_tune(struct dvb_frontend
*fe
,
1512 unsigned int mode_flags
,
1513 unsigned int *delay
,
1514 fe_status_t
*status
)
1516 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1519 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1522 rc
= mb86a20s_set_frontend(fe
);
1524 if (!(mode_flags
& FE_TUNE_MODE_ONESHOT
))
1525 mb86a20s_read_status_and_stats(fe
, status
);
1530 static void mb86a20s_release(struct dvb_frontend
*fe
)
1532 struct mb86a20s_state
*state
= fe
->demodulator_priv
;
1534 dev_dbg(&state
->i2c
->dev
, "%s called.\n", __func__
);
1539 static struct dvb_frontend_ops mb86a20s_ops
;
1541 struct dvb_frontend
*mb86a20s_attach(const struct mb86a20s_config
*config
,
1542 struct i2c_adapter
*i2c
)
1544 struct mb86a20s_state
*state
;
1547 dev_dbg(&i2c
->dev
, "%s called.\n", __func__
);
1549 /* allocate memory for the internal state */
1550 state
= kzalloc(sizeof(struct mb86a20s_state
), GFP_KERNEL
);
1551 if (state
== NULL
) {
1553 "%s: unable to allocate memory for state\n", __func__
);
1557 /* setup the state */
1558 state
->config
= config
;
1561 /* create dvb_frontend */
1562 memcpy(&state
->frontend
.ops
, &mb86a20s_ops
,
1563 sizeof(struct dvb_frontend_ops
));
1564 state
->frontend
.demodulator_priv
= state
;
1566 /* Check if it is a mb86a20s frontend */
1567 rev
= mb86a20s_readreg(state
, 0);
1571 "Detected a Fujitsu mb86a20s frontend\n");
1574 "Frontend revision %d is unknown - aborting.\n",
1579 return &state
->frontend
;
1585 EXPORT_SYMBOL(mb86a20s_attach
);
1587 static struct dvb_frontend_ops mb86a20s_ops
= {
1588 .delsys
= { SYS_ISDBT
},
1589 /* Use dib8000 values per default */
1591 .name
= "Fujitsu mb86A20s",
1592 .caps
= FE_CAN_INVERSION_AUTO
| FE_CAN_RECOVER
|
1593 FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
1594 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
1595 FE_CAN_QPSK
| FE_CAN_QAM_16
| FE_CAN_QAM_64
|
1596 FE_CAN_TRANSMISSION_MODE_AUTO
| FE_CAN_QAM_AUTO
|
1597 FE_CAN_GUARD_INTERVAL_AUTO
| FE_CAN_HIERARCHY_AUTO
,
1598 /* Actually, those values depend on the used tuner */
1599 .frequency_min
= 45000000,
1600 .frequency_max
= 864000000,
1601 .frequency_stepsize
= 62500,
1604 .release
= mb86a20s_release
,
1606 .init
= mb86a20s_initfe
,
1607 .set_frontend
= mb86a20s_set_frontend
,
1608 .get_frontend
= mb86a20s_get_frontend_dummy
,
1609 .read_status
= mb86a20s_read_status_and_stats
,
1610 .read_signal_strength
= mb86a20s_read_signal_strength_from_cache
,
1611 .tune
= mb86a20s_tune
,
1614 MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
1615 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
1616 MODULE_LICENSE("GPL");