1 // SPDX-License-Identifier: GPL-2.0-or-later
3 TDA10023 - DVB-C decoder
4 (as used in Philips CU1216-3 NIM and the Reelbox DVB-C tuner card)
6 Copyright (C) 2005 Georg Acher, BayCom GmbH (acher at baycom dot de)
7 Copyright (c) 2006 Hartmut Birr (e9hack at gmail dot com)
9 Remotely based on tda10021.c
10 Copyright (C) 1999 Convergence Integrated Media GmbH <ralph@convergence.de>
11 Copyright (C) 2004 Markus Schulz <msc@antzsystem.de>
16 #include <linux/delay.h>
17 #include <linux/errno.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/string.h>
22 #include <linux/slab.h>
24 #include <asm/div64.h>
26 #include <media/dvb_frontend.h>
29 #define REG0_INIT_VAL 0x23
31 struct tda10023_state
{
32 struct i2c_adapter
* i2c
;
33 /* configuration settings */
34 const struct tda10023_config
*config
;
35 struct dvb_frontend frontend
;
52 static u8
tda10023_readreg (struct tda10023_state
* state
, u8 reg
)
56 struct i2c_msg msg
[] = { { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= b0
, .len
= 1 },
57 { .addr
= state
->config
->demod_address
, .flags
= I2C_M_RD
, .buf
= b1
, .len
= 1 } };
60 ret
= i2c_transfer (state
->i2c
, msg
, 2);
62 int num
= state
->frontend
.dvb
? state
->frontend
.dvb
->num
: -1;
63 printk(KERN_ERR
"DVB: TDA10023(%d): %s: readreg error (reg == 0x%02x, ret == %i)\n",
64 num
, __func__
, reg
, ret
);
69 static int tda10023_writereg (struct tda10023_state
* state
, u8 reg
, u8 data
)
71 u8 buf
[] = { reg
, data
};
72 struct i2c_msg msg
= { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= buf
, .len
= 2 };
75 ret
= i2c_transfer (state
->i2c
, &msg
, 1);
77 int num
= state
->frontend
.dvb
? state
->frontend
.dvb
->num
: -1;
78 printk(KERN_ERR
"DVB: TDA10023(%d): %s, writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
79 num
, __func__
, reg
, data
, ret
);
81 return (ret
!= 1) ? -EREMOTEIO
: 0;
85 static int tda10023_writebit (struct tda10023_state
* state
, u8 reg
, u8 mask
,u8 data
)
88 return tda10023_writereg(state
, reg
, data
);
91 val
=tda10023_readreg(state
,reg
);
94 return tda10023_writereg(state
, reg
, val
);
98 static void tda10023_writetab(struct tda10023_state
* state
, u8
* tab
)
112 tda10023_writebit(state
,r
,m
,v
);
116 //get access to tuner
117 static int lock_tuner(struct tda10023_state
* state
)
119 u8 buf
[2] = { 0x0f, 0xc0 };
120 struct i2c_msg msg
= {.addr
=state
->config
->demod_address
, .flags
=0, .buf
=buf
, .len
=2};
122 if(i2c_transfer(state
->i2c
, &msg
, 1) != 1)
124 printk("tda10023: lock tuner fails\n");
130 //release access from tuner
131 static int unlock_tuner(struct tda10023_state
* state
)
133 u8 buf
[2] = { 0x0f, 0x40 };
134 struct i2c_msg msg_post
={.addr
=state
->config
->demod_address
, .flags
=0, .buf
=buf
, .len
=2};
136 if(i2c_transfer(state
->i2c
, &msg_post
, 1) != 1)
138 printk("tda10023: unlock tuner fails\n");
144 static int tda10023_setup_reg0 (struct tda10023_state
* state
, u8 reg0
)
146 reg0
|= state
->reg0
& 0x63;
148 tda10023_writereg (state
, 0x00, reg0
& 0xfe);
149 tda10023_writereg (state
, 0x00, reg0
| 0x01);
155 static int tda10023_set_symbolrate (struct tda10023_state
* state
, u32 sr
)
162 /* avoid floating point operations multiplying syscloc and divider
164 u32 sysclk_x_10
= state
->sysclk
* 10;
166 if (sr
< (u32
)(sysclk_x_10
/984)) {
169 } else if (sr
< (u32
)(sysclk_x_10
/640)) {
172 } else if (sr
< (u32
)(sysclk_x_10
/492)) {
175 } else if (sr
< (u32
)(sysclk_x_10
/320)) {
178 } else if (sr
< (u32
)(sysclk_x_10
/246)) {
181 } else if (sr
< (u32
)(sysclk_x_10
/160)) {
184 } else if (sr
< (u32
)(sysclk_x_10
/123)) {
189 BDRI
= (state
->sysclk
)*16;
202 do_div(BDRX
, state
->sysclk
); /* BDRX/=SYSCLK; */
206 dprintk("Symbolrate %i, BDR %i BDRI %i, NDEC %i\n",
207 sr
, BDR
, BDRI
, NDEC
);
208 tda10023_writebit (state
, 0x03, 0xc0, NDEC
<<6);
209 tda10023_writereg (state
, 0x0a, BDR
&255);
210 tda10023_writereg (state
, 0x0b, (BDR
>>8)&255);
211 tda10023_writereg (state
, 0x0c, (BDR
>>16)&31);
212 tda10023_writereg (state
, 0x0d, BDRI
);
213 tda10023_writereg (state
, 0x3d, (SFIL
<<7));
217 static int tda10023_init (struct dvb_frontend
*fe
)
219 struct tda10023_state
* state
= fe
->demodulator_priv
;
220 u8 tda10023_inittab
[] = {
222 /* 000 */ 0x2a, 0xff, 0x02, /* PLL3, Bypass, Power Down */
223 /* 003 */ 0xff, 0x64, 0x00, /* Sleep 100ms */
224 /* 006 */ 0x2a, 0xff, 0x03, /* PLL3, Bypass, Power Down */
225 /* 009 */ 0xff, 0x64, 0x00, /* Sleep 100ms */
227 /* 012 */ 0x28, 0xff, (state
->pll_m
-1),
229 /* 015 */ 0x29, 0xff, ((state
->pll_p
-1)<<6)|(state
->pll_n
-1),
230 /* GPR FSAMPLING=1 */
231 /* 018 */ 0x00, 0xff, REG0_INIT_VAL
,
232 /* 021 */ 0x2a, 0xff, 0x08, /* PLL3 PSACLK=1 */
233 /* 024 */ 0xff, 0x64, 0x00, /* Sleep 100ms */
234 /* 027 */ 0x1f, 0xff, 0x00, /* RESET */
235 /* 030 */ 0xff, 0x64, 0x00, /* Sleep 100ms */
236 /* 033 */ 0xe6, 0x0c, 0x04, /* RSCFG_IND */
237 /* 036 */ 0x10, 0xc0, 0x80, /* DECDVBCFG1 PBER=1 */
239 /* 039 */ 0x0e, 0xff, 0x82, /* GAIN1 */
240 /* 042 */ 0x03, 0x08, 0x08, /* CLKCONF DYN=1 */
241 /* 045 */ 0x2e, 0xbf, 0x30, /* AGCCONF2 TRIAGC=0,POSAGC=ENAGCIF=1
242 PPWMTUN=0 PPWMIF=0 */
243 /* 048 */ 0x01, 0xff, 0x30, /* AGCREF */
244 /* 051 */ 0x1e, 0x84, 0x84, /* CONTROL SACLK_ON=1 */
245 /* 054 */ 0x1b, 0xff, 0xc8, /* ADC TWOS=1 */
246 /* 057 */ 0x3b, 0xff, 0xff, /* IFMAX */
247 /* 060 */ 0x3c, 0xff, 0x00, /* IFMIN */
248 /* 063 */ 0x34, 0xff, 0x00, /* PWMREF */
249 /* 066 */ 0x35, 0xff, 0xff, /* TUNMAX */
250 /* 069 */ 0x36, 0xff, 0x00, /* TUNMIN */
251 /* 072 */ 0x06, 0xff, 0x7f, /* EQCONF1 POSI=7 ENADAPT=ENEQUAL=DFE=1 */
252 /* 075 */ 0x1c, 0x30, 0x30, /* EQCONF2 STEPALGO=SGNALGO=1 */
253 /* 078 */ 0x37, 0xff, 0xf6, /* DELTAF_LSB */
254 /* 081 */ 0x38, 0xff, 0xff, /* DELTAF_MSB */
255 /* 084 */ 0x02, 0xff, 0x93, /* AGCCONF1 IFS=1 KAGCIF=2 KAGCTUN=3 */
256 /* 087 */ 0x2d, 0xff, 0xf6, /* SWEEP SWPOS=1 SWDYN=7 SWSTEP=1 SWLEN=2 */
257 /* 090 */ 0x04, 0x10, 0x00, /* SWRAMP=1 */
258 /* 093 */ 0x12, 0xff, TDA10023_OUTPUT_MODE_PARALLEL_B
, /*
259 INTP1 POCLKP=1 FEL=1 MFS=0 */
260 /* 096 */ 0x2b, 0x01, 0xa1, /* INTS1 */
261 /* 099 */ 0x20, 0xff, 0x04, /* INTP2 SWAPP=? MSBFIRSTP=? INTPSEL=? */
262 /* 102 */ 0x2c, 0xff, 0x0d, /* INTP/S TRIP=0 TRIS=0 */
263 /* 105 */ 0xc4, 0xff, 0x00,
264 /* 108 */ 0xc3, 0x30, 0x00,
265 /* 111 */ 0xb5, 0xff, 0x19, /* ERAGC_THD */
266 /* 114 */ 0x00, 0x03, 0x01, /* GPR, CLBS soft reset */
267 /* 117 */ 0x00, 0x03, 0x03, /* GPR, CLBS soft reset */
268 /* 120 */ 0xff, 0x64, 0x00, /* Sleep 100ms */
269 /* 123 */ 0xff, 0xff, 0xff
271 dprintk("DVB: TDA10023(%d): init chip\n", fe
->dvb
->num
);
273 /* override default values if set in config */
274 if (state
->config
->deltaf
) {
275 tda10023_inittab
[80] = (state
->config
->deltaf
& 0xff);
276 tda10023_inittab
[83] = (state
->config
->deltaf
>> 8);
279 if (state
->config
->output_mode
)
280 tda10023_inittab
[95] = state
->config
->output_mode
;
282 tda10023_writetab(state
, tda10023_inittab
);
288 u8 qam
, lockthr
, mseth
, aref
, agcrefnyq
, eragnyq_thd
;
291 static int tda10023_set_parameters(struct dvb_frontend
*fe
)
293 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
294 u32 delsys
= c
->delivery_system
;
295 unsigned qam
= c
->modulation
;
297 struct tda10023_state
* state
= fe
->demodulator_priv
;
298 static const struct qam_params qam_params
[] = {
299 /* Modulation QAM LOCKTHR MSETH AREF AGCREFNYQ ERAGCNYQ_THD */
300 [QPSK
] = { (5<<2), 0x78, 0x8c, 0x96, 0x78, 0x4c },
301 [QAM_16
] = { (0<<2), 0x87, 0xa2, 0x91, 0x8c, 0x57 },
302 [QAM_32
] = { (1<<2), 0x64, 0x74, 0x96, 0x8c, 0x57 },
303 [QAM_64
] = { (2<<2), 0x46, 0x43, 0x6a, 0x6a, 0x44 },
304 [QAM_128
] = { (3<<2), 0x36, 0x34, 0x7e, 0x78, 0x4c },
305 [QAM_256
] = { (4<<2), 0x26, 0x23, 0x6c, 0x5c, 0x3c },
309 case SYS_DVBC_ANNEX_A
:
312 case SYS_DVBC_ANNEX_C
:
320 * gcc optimizes the code below the same way as it would code:
321 * "if (qam > 5) return -EINVAL;"
322 * Yet, the code is clearer, as it shows what QAM standards are
323 * supported by the driver, and avoids the usage of magic numbers on
338 if (fe
->ops
.tuner_ops
.set_params
) {
339 fe
->ops
.tuner_ops
.set_params(fe
);
340 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
343 tda10023_set_symbolrate(state
, c
->symbol_rate
);
344 tda10023_writereg(state
, 0x05, qam_params
[qam
].lockthr
);
345 tda10023_writereg(state
, 0x08, qam_params
[qam
].mseth
);
346 tda10023_writereg(state
, 0x09, qam_params
[qam
].aref
);
347 tda10023_writereg(state
, 0xb4, qam_params
[qam
].agcrefnyq
);
348 tda10023_writereg(state
, 0xb6, qam_params
[qam
].eragnyq_thd
);
350 tda10023_writereg(state
, 0x04, (c
->inversion
? 0x12 : 0x32));
351 tda10023_writebit(state
, 0x04, 0x60, (c
->inversion
? 0 : 0x20));
353 tda10023_writebit(state
, 0x04, 0x40, 0x40);
356 tda10023_writebit(state
, 0x3d, 0xfc, 0x03);
358 tda10023_writebit(state
, 0x3d, 0xfc, 0x02);
360 tda10023_setup_reg0(state
, qam_params
[qam
].qam
);
365 static int tda10023_read_status(struct dvb_frontend
*fe
,
366 enum fe_status
*status
)
368 struct tda10023_state
* state
= fe
->demodulator_priv
;
373 //0x11[1] == CARLOCK -> Carrier locked
374 //0x11[2] == FSYNC -> Frame synchronisation
375 //0x11[3] == FEL -> Front End locked
376 //0x11[6] == NODVB -> DVB Mode Information
377 sync
= tda10023_readreg (state
, 0x11);
380 *status
|= FE_HAS_SIGNAL
|FE_HAS_CARRIER
;
383 *status
|= FE_HAS_SYNC
|FE_HAS_VITERBI
;
386 *status
|= FE_HAS_LOCK
;
391 static int tda10023_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
393 struct tda10023_state
* state
= fe
->demodulator_priv
;
395 a
=tda10023_readreg(state
, 0x14);
396 b
=tda10023_readreg(state
, 0x15);
397 c
=tda10023_readreg(state
, 0x16)&0xf;
398 tda10023_writebit (state
, 0x10, 0xc0, 0x00);
400 *ber
= a
| (b
<<8)| (c
<<16);
404 static int tda10023_read_signal_strength(struct dvb_frontend
* fe
, u16
* strength
)
406 struct tda10023_state
* state
= fe
->demodulator_priv
;
407 u8 ifgain
=tda10023_readreg(state
, 0x2f);
409 u16 gain
= ((255-tda10023_readreg(state
, 0x17))) + (255-ifgain
)/16;
410 // Max raw value is about 0xb0 -> Normalize to >0xf0 after 0x90
412 gain
=gain
+2*(gain
-0x90);
416 *strength
= (gain
<<8)|gain
;
420 static int tda10023_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
422 struct tda10023_state
* state
= fe
->demodulator_priv
;
424 u8 quality
= ~tda10023_readreg(state
, 0x18);
425 *snr
= (quality
<< 8) | quality
;
429 static int tda10023_read_ucblocks(struct dvb_frontend
* fe
, u32
* ucblocks
)
431 struct tda10023_state
* state
= fe
->demodulator_priv
;
433 a
= tda10023_readreg (state
, 0x74);
434 b
= tda10023_readreg (state
, 0x75);
435 c
= tda10023_readreg (state
, 0x76);
436 d
= tda10023_readreg (state
, 0x77);
437 *ucblocks
= a
| (b
<<8)|(c
<<16)|(d
<<24);
439 tda10023_writebit (state
, 0x10, 0x20,0x00);
440 tda10023_writebit (state
, 0x10, 0x20,0x20);
441 tda10023_writebit (state
, 0x13, 0x01, 0x00);
446 static int tda10023_get_frontend(struct dvb_frontend
*fe
,
447 struct dtv_frontend_properties
*p
)
449 struct tda10023_state
* state
= fe
->demodulator_priv
;
453 sync
= tda10023_readreg(state
, 0x11);
454 afc
= tda10023_readreg(state
, 0x19);
455 inv
= tda10023_readreg(state
, 0x04);
458 /* AFC only valid when carrier has been recovered */
459 printk(sync
& 2 ? "DVB: TDA10023(%d): AFC (%d) %dHz\n" :
460 "DVB: TDA10023(%d): [AFC (%d) %dHz]\n",
461 state
->frontend
.dvb
->num
, afc
,
462 -((s32
)p
->symbol_rate
* afc
) >> 10);
465 p
->inversion
= (inv
&0x20?0:1);
466 p
->modulation
= ((state
->reg0
>> 2) & 7) + QAM_16
;
468 p
->fec_inner
= FEC_NONE
;
469 p
->frequency
= ((p
->frequency
+ 31250) / 62500) * 62500;
472 p
->frequency
-= ((s32
)p
->symbol_rate
* afc
) >> 10;
477 static int tda10023_sleep(struct dvb_frontend
* fe
)
479 struct tda10023_state
* state
= fe
->demodulator_priv
;
481 tda10023_writereg (state
, 0x1b, 0x02); /* pdown ADC */
482 tda10023_writereg (state
, 0x00, 0x80); /* standby */
487 static int tda10023_i2c_gate_ctrl(struct dvb_frontend
* fe
, int enable
)
489 struct tda10023_state
* state
= fe
->demodulator_priv
;
499 static void tda10023_release(struct dvb_frontend
* fe
)
501 struct tda10023_state
* state
= fe
->demodulator_priv
;
505 static const struct dvb_frontend_ops tda10023_ops
;
507 struct dvb_frontend
*tda10023_attach(const struct tda10023_config
*config
,
508 struct i2c_adapter
*i2c
,
511 struct tda10023_state
* state
= NULL
;
513 /* allocate memory for the internal state */
514 state
= kzalloc(sizeof(struct tda10023_state
), GFP_KERNEL
);
515 if (state
== NULL
) goto error
;
517 /* setup the state */
518 state
->config
= config
;
521 /* wakeup if in standby */
522 tda10023_writereg (state
, 0x00, 0x33);
523 /* check if the demod is there */
524 if ((tda10023_readreg(state
, 0x1a) & 0xf0) != 0x70) goto error
;
526 /* create dvb_frontend */
527 memcpy(&state
->frontend
.ops
, &tda10023_ops
, sizeof(struct dvb_frontend_ops
));
529 state
->reg0
= REG0_INIT_VAL
;
530 if (state
->config
->xtal
) {
531 state
->xtal
= state
->config
->xtal
;
532 state
->pll_m
= state
->config
->pll_m
;
533 state
->pll_p
= state
->config
->pll_p
;
534 state
->pll_n
= state
->config
->pll_n
;
536 /* set default values if not defined in config */
537 state
->xtal
= 28920000;
544 state
->sysclk
= (state
->xtal
* state
->pll_m
/ \
545 (state
->pll_n
* state
->pll_p
));
547 state
->frontend
.ops
.info
.symbol_rate_min
= (state
->sysclk
/2)/64;
548 state
->frontend
.ops
.info
.symbol_rate_max
= (state
->sysclk
/2)/4;
550 dprintk("DVB: TDA10023 %s: xtal:%d pll_m:%d pll_p:%d pll_n:%d\n",
551 __func__
, state
->xtal
, state
->pll_m
, state
->pll_p
,
554 state
->frontend
.demodulator_priv
= state
;
555 return &state
->frontend
;
562 static const struct dvb_frontend_ops tda10023_ops
= {
563 .delsys
= { SYS_DVBC_ANNEX_A
, SYS_DVBC_ANNEX_C
},
565 .name
= "Philips TDA10023 DVB-C",
566 .frequency_min_hz
= 47 * MHz
,
567 .frequency_max_hz
= 862 * MHz
,
568 .frequency_stepsize_hz
= 62500,
569 .symbol_rate_min
= 0, /* set in tda10023_attach */
570 .symbol_rate_max
= 0, /* set in tda10023_attach */
571 .caps
= 0x400 | //FE_CAN_QAM_4
572 FE_CAN_QAM_16
| FE_CAN_QAM_32
| FE_CAN_QAM_64
|
573 FE_CAN_QAM_128
| FE_CAN_QAM_256
|
577 .release
= tda10023_release
,
579 .init
= tda10023_init
,
580 .sleep
= tda10023_sleep
,
581 .i2c_gate_ctrl
= tda10023_i2c_gate_ctrl
,
583 .set_frontend
= tda10023_set_parameters
,
584 .get_frontend
= tda10023_get_frontend
,
585 .read_status
= tda10023_read_status
,
586 .read_ber
= tda10023_read_ber
,
587 .read_signal_strength
= tda10023_read_signal_strength
,
588 .read_snr
= tda10023_read_snr
,
589 .read_ucblocks
= tda10023_read_ucblocks
,
593 MODULE_DESCRIPTION("Philips TDA10023 DVB-C demodulator driver");
594 MODULE_AUTHOR("Georg Acher, Hartmut Birr");
595 MODULE_LICENSE("GPL");
597 EXPORT_SYMBOL(tda10023_attach
);