1 // SPDX-License-Identifier: GPL-2.0-or-later
3 NXP TDA10048HN DVB OFDM demodulator driver
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/string.h>
14 #include <linux/slab.h>
15 #include <linux/delay.h>
16 #include <linux/math64.h>
17 #include <asm/div64.h>
18 #include <media/dvb_frontend.h>
19 #include <media/dvb_math.h>
22 #define TDA10048_DEFAULT_FIRMWARE "dvb-fe-tda10048-1.0.fw"
23 #define TDA10048_DEFAULT_FIRMWARE_SIZE 24878
25 /* Register name definitions */
26 #define TDA10048_IDENTITY 0x00
27 #define TDA10048_VERSION 0x01
28 #define TDA10048_DSP_CODE_CPT 0x0C
29 #define TDA10048_DSP_CODE_IN 0x0E
30 #define TDA10048_IN_CONF1 0x10
31 #define TDA10048_IN_CONF2 0x11
32 #define TDA10048_IN_CONF3 0x12
33 #define TDA10048_OUT_CONF1 0x14
34 #define TDA10048_OUT_CONF2 0x15
35 #define TDA10048_OUT_CONF3 0x16
36 #define TDA10048_AUTO 0x18
37 #define TDA10048_SYNC_STATUS 0x1A
38 #define TDA10048_CONF_C4_1 0x1E
39 #define TDA10048_CONF_C4_2 0x1F
40 #define TDA10048_CODE_IN_RAM 0x20
41 #define TDA10048_CHANNEL_INFO1_R 0x22
42 #define TDA10048_CHANNEL_INFO2_R 0x23
43 #define TDA10048_CHANNEL_INFO1 0x24
44 #define TDA10048_CHANNEL_INFO2 0x25
45 #define TDA10048_TIME_ERROR_R 0x26
46 #define TDA10048_TIME_ERROR 0x27
47 #define TDA10048_FREQ_ERROR_LSB_R 0x28
48 #define TDA10048_FREQ_ERROR_MSB_R 0x29
49 #define TDA10048_FREQ_ERROR_LSB 0x2A
50 #define TDA10048_FREQ_ERROR_MSB 0x2B
51 #define TDA10048_IT_SEL 0x30
52 #define TDA10048_IT_STAT 0x32
53 #define TDA10048_DSP_AD_LSB 0x3C
54 #define TDA10048_DSP_AD_MSB 0x3D
55 #define TDA10048_DSP_REG_LSB 0x3E
56 #define TDA10048_DSP_REG_MSB 0x3F
57 #define TDA10048_CONF_TRISTATE1 0x44
58 #define TDA10048_CONF_TRISTATE2 0x45
59 #define TDA10048_CONF_POLARITY 0x46
60 #define TDA10048_GPIO_SP_DS0 0x48
61 #define TDA10048_GPIO_SP_DS1 0x49
62 #define TDA10048_GPIO_SP_DS2 0x4A
63 #define TDA10048_GPIO_SP_DS3 0x4B
64 #define TDA10048_GPIO_OUT_SEL 0x4C
65 #define TDA10048_GPIO_SELECT 0x4D
66 #define TDA10048_IC_MODE 0x4E
67 #define TDA10048_CONF_XO 0x50
68 #define TDA10048_CONF_PLL1 0x51
69 #define TDA10048_CONF_PLL2 0x52
70 #define TDA10048_CONF_PLL3 0x53
71 #define TDA10048_CONF_ADC 0x54
72 #define TDA10048_CONF_ADC_2 0x55
73 #define TDA10048_CONF_C1_1 0x60
74 #define TDA10048_CONF_C1_3 0x62
75 #define TDA10048_AGC_CONF 0x70
76 #define TDA10048_AGC_THRESHOLD_LSB 0x72
77 #define TDA10048_AGC_THRESHOLD_MSB 0x73
78 #define TDA10048_AGC_RENORM 0x74
79 #define TDA10048_AGC_GAINS 0x76
80 #define TDA10048_AGC_TUN_MIN 0x78
81 #define TDA10048_AGC_TUN_MAX 0x79
82 #define TDA10048_AGC_IF_MIN 0x7A
83 #define TDA10048_AGC_IF_MAX 0x7B
84 #define TDA10048_AGC_TUN_LEVEL 0x7E
85 #define TDA10048_AGC_IF_LEVEL 0x7F
86 #define TDA10048_DIG_AGC_LEVEL 0x81
87 #define TDA10048_FREQ_PHY2_LSB 0x86
88 #define TDA10048_FREQ_PHY2_MSB 0x87
89 #define TDA10048_TIME_INVWREF_LSB 0x88
90 #define TDA10048_TIME_INVWREF_MSB 0x89
91 #define TDA10048_TIME_WREF_LSB 0x8A
92 #define TDA10048_TIME_WREF_MID1 0x8B
93 #define TDA10048_TIME_WREF_MID2 0x8C
94 #define TDA10048_TIME_WREF_MSB 0x8D
95 #define TDA10048_NP_OUT 0xA2
96 #define TDA10048_CELL_ID_LSB 0xA4
97 #define TDA10048_CELL_ID_MSB 0xA5
98 #define TDA10048_EXTTPS_ODD 0xAA
99 #define TDA10048_EXTTPS_EVEN 0xAB
100 #define TDA10048_TPS_LENGTH 0xAC
101 #define TDA10048_FREE_REG_1 0xB2
102 #define TDA10048_FREE_REG_2 0xB3
103 #define TDA10048_CONF_C3_1 0xC0
104 #define TDA10048_CVBER_CTRL 0xC2
105 #define TDA10048_CBER_NMAX_LSB 0xC4
106 #define TDA10048_CBER_NMAX_MSB 0xC5
107 #define TDA10048_CBER_LSB 0xC6
108 #define TDA10048_CBER_MSB 0xC7
109 #define TDA10048_VBER_LSB 0xC8
110 #define TDA10048_VBER_MID 0xC9
111 #define TDA10048_VBER_MSB 0xCA
112 #define TDA10048_CVBER_LUT 0xCC
113 #define TDA10048_UNCOR_CTRL 0xCD
114 #define TDA10048_UNCOR_CPT_LSB 0xCE
115 #define TDA10048_UNCOR_CPT_MSB 0xCF
116 #define TDA10048_SOFT_IT_C3 0xD6
117 #define TDA10048_CONF_TS2 0xE0
118 #define TDA10048_CONF_TS1 0xE1
120 static unsigned int debug;
122 #define dprintk(level, fmt, arg...)\
123 do { if (debug >= level)\
124 printk(KERN_DEBUG "tda10048: " fmt, ## arg);\
127 struct tda10048_state {
129 struct i2c_adapter *i2c;
131 /* We'll cache and update the attach config settings */
132 struct tda10048_config config;
133 struct dvb_frontend frontend;
147 static struct init_tab {
151 { TDA10048_CONF_PLL1, 0x08 },
152 { TDA10048_CONF_ADC_2, 0x00 },
153 { TDA10048_CONF_C4_1, 0x00 },
154 { TDA10048_CONF_PLL1, 0x0f },
155 { TDA10048_CONF_PLL2, 0x0a },
156 { TDA10048_CONF_PLL3, 0x43 },
157 { TDA10048_FREQ_PHY2_LSB, 0x02 },
158 { TDA10048_FREQ_PHY2_MSB, 0x0a },
159 { TDA10048_TIME_WREF_LSB, 0xbd },
160 { TDA10048_TIME_WREF_MID1, 0xe4 },
161 { TDA10048_TIME_WREF_MID2, 0xa8 },
162 { TDA10048_TIME_WREF_MSB, 0x02 },
163 { TDA10048_TIME_INVWREF_LSB, 0x04 },
164 { TDA10048_TIME_INVWREF_MSB, 0x06 },
165 { TDA10048_CONF_C4_1, 0x00 },
166 { TDA10048_CONF_C1_1, 0xa8 },
167 { TDA10048_AGC_CONF, 0x16 },
168 { TDA10048_CONF_C1_3, 0x0b },
169 { TDA10048_AGC_TUN_MIN, 0x00 },
170 { TDA10048_AGC_TUN_MAX, 0xff },
171 { TDA10048_AGC_IF_MIN, 0x00 },
172 { TDA10048_AGC_IF_MAX, 0xff },
173 { TDA10048_AGC_THRESHOLD_MSB, 0x00 },
174 { TDA10048_AGC_THRESHOLD_LSB, 0x70 },
175 { TDA10048_CVBER_CTRL, 0x38 },
176 { TDA10048_AGC_GAINS, 0x12 },
177 { TDA10048_CONF_XO, 0x00 },
178 { TDA10048_CONF_TS1, 0x07 },
179 { TDA10048_IC_MODE, 0x00 },
180 { TDA10048_CONF_TS2, 0xc0 },
181 { TDA10048_CONF_TRISTATE1, 0x21 },
182 { TDA10048_CONF_TRISTATE2, 0x00 },
183 { TDA10048_CONF_POLARITY, 0x00 },
184 { TDA10048_CONF_C4_2, 0x04 },
185 { TDA10048_CONF_ADC, 0x60 },
186 { TDA10048_CONF_ADC_2, 0x10 },
187 { TDA10048_CONF_ADC, 0x60 },
188 { TDA10048_CONF_ADC_2, 0x00 },
189 { TDA10048_CONF_C1_1, 0xa8 },
190 { TDA10048_UNCOR_CTRL, 0x00 },
191 { TDA10048_CONF_C4_2, 0x04 },
194 static struct pll_tab {
198 { TDA10048_CLK_4000, TDA10048_IF_36130 },
199 { TDA10048_CLK_16000, TDA10048_IF_3300 },
200 { TDA10048_CLK_16000, TDA10048_IF_3500 },
201 { TDA10048_CLK_16000, TDA10048_IF_3800 },
202 { TDA10048_CLK_16000, TDA10048_IF_4000 },
203 { TDA10048_CLK_16000, TDA10048_IF_4300 },
204 { TDA10048_CLK_16000, TDA10048_IF_4500 },
205 { TDA10048_CLK_16000, TDA10048_IF_5000 },
206 { TDA10048_CLK_16000, TDA10048_IF_36130 },
209 static int tda10048_writereg(struct tda10048_state *state, u8 reg, u8 data)
211 struct tda10048_config *config = &state->config;
213 u8 buf[] = { reg, data };
214 struct i2c_msg msg = {
215 .addr = config->demod_address,
216 .flags = 0, .buf = buf, .len = 2 };
218 dprintk(2, "%s(reg = 0x%02x, data = 0x%02x)\n", __func__, reg, data);
220 ret = i2c_transfer(state->i2c, &msg, 1);
223 printk("%s: writereg error (ret == %i)\n", __func__, ret);
225 return (ret != 1) ? -1 : 0;
228 static u8 tda10048_readreg(struct tda10048_state *state, u8 reg)
230 struct tda10048_config *config = &state->config;
234 struct i2c_msg msg[] = {
235 { .addr = config->demod_address,
236 .flags = 0, .buf = b0, .len = 1 },
237 { .addr = config->demod_address,
238 .flags = I2C_M_RD, .buf = b1, .len = 1 } };
240 dprintk(2, "%s(reg = 0x%02x)\n", __func__, reg);
242 ret = i2c_transfer(state->i2c, msg, 2);
245 printk(KERN_ERR "%s: readreg error (ret == %i)\n",
251 static int tda10048_writeregbulk(struct tda10048_state *state, u8 reg,
252 const u8 *data, u16 len)
254 struct tda10048_config *config = &state->config;
255 int ret = -EREMOTEIO;
259 dprintk(2, "%s(%d, ?, len = %d)\n", __func__, reg, len);
261 buf = kmalloc(len + 1, GFP_KERNEL);
268 memcpy(buf + 1, data, len);
270 msg.addr = config->demod_address;
275 dprintk(2, "%s(): write len = %d\n",
278 ret = i2c_transfer(state->i2c, &msg, 1);
280 printk(KERN_ERR "%s(): writereg error err %i\n",
291 static int tda10048_set_phy2(struct dvb_frontend *fe, u32 sample_freq_hz,
294 struct tda10048_state *state = fe->demodulator_priv;
297 dprintk(1, "%s()\n", __func__);
299 if (sample_freq_hz == 0)
302 if (if_hz < (sample_freq_hz / 2)) {
303 /* PHY2 = (if2/fs) * 2^15 */
307 do_div(t, sample_freq_hz);
311 /* PHY2 = ((IF1-fs)/fs) * 2^15 */
312 t = sample_freq_hz - if_hz;
315 do_div(t, sample_freq_hz);
321 tda10048_writereg(state, TDA10048_FREQ_PHY2_LSB, (u8)t);
322 tda10048_writereg(state, TDA10048_FREQ_PHY2_MSB, (u8)(t >> 8));
327 static int tda10048_set_wref(struct dvb_frontend *fe, u32 sample_freq_hz,
330 struct tda10048_state *state = fe->demodulator_priv;
333 dprintk(1, "%s()\n", __func__);
335 if (sample_freq_hz == 0)
338 /* WREF = (B / (7 * fs)) * 2^31 */
340 /* avoid warning: this decimal constant is unsigned only in ISO C90 */
341 /* t *= 2147483648 on 32bit platforms */
344 z = 7 * sample_freq_hz;
349 tda10048_writereg(state, TDA10048_TIME_WREF_LSB, (u8)t);
350 tda10048_writereg(state, TDA10048_TIME_WREF_MID1, (u8)(t >> 8));
351 tda10048_writereg(state, TDA10048_TIME_WREF_MID2, (u8)(t >> 16));
352 tda10048_writereg(state, TDA10048_TIME_WREF_MSB, (u8)(t >> 24));
357 static int tda10048_set_invwref(struct dvb_frontend *fe, u32 sample_freq_hz,
360 struct tda10048_state *state = fe->demodulator_priv;
363 dprintk(1, "%s()\n", __func__);
365 if (sample_freq_hz == 0)
368 /* INVWREF = ((7 * fs) / B) * 2^5 */
377 tda10048_writereg(state, TDA10048_TIME_INVWREF_LSB, (u8)t);
378 tda10048_writereg(state, TDA10048_TIME_INVWREF_MSB, (u8)(t >> 8));
383 static int tda10048_set_bandwidth(struct dvb_frontend *fe,
386 struct tda10048_state *state = fe->demodulator_priv;
387 dprintk(1, "%s(bw=%d)\n", __func__, bw);
389 /* Bandwidth setting may need to be adjusted */
394 tda10048_set_wref(fe, state->sample_freq, bw);
395 tda10048_set_invwref(fe, state->sample_freq, bw);
398 printk(KERN_ERR "%s() invalid bandwidth\n", __func__);
402 state->bandwidth = bw;
407 static int tda10048_set_if(struct dvb_frontend *fe, u32 bw)
409 struct tda10048_state *state = fe->demodulator_priv;
410 struct tda10048_config *config = &state->config;
414 dprintk(1, "%s(bw = %d)\n", __func__, bw);
416 /* based on target bandwidth and clk we calculate pll factors */
419 if_freq_khz = config->dtv6_if_freq_khz;
422 if_freq_khz = config->dtv7_if_freq_khz;
425 if_freq_khz = config->dtv8_if_freq_khz;
428 printk(KERN_ERR "%s() no default\n", __func__);
432 for (i = 0; i < ARRAY_SIZE(pll_tab); i++) {
433 if ((pll_tab[i].clk_freq_khz == config->clk_freq_khz) &&
434 (pll_tab[i].if_freq_khz == if_freq_khz)) {
436 state->freq_if_hz = pll_tab[i].if_freq_khz * 1000;
437 state->xtal_hz = pll_tab[i].clk_freq_khz * 1000;
441 if (i == ARRAY_SIZE(pll_tab)) {
442 printk(KERN_ERR "%s() Incorrect attach settings\n",
447 dprintk(1, "- freq_if_hz = %d\n", state->freq_if_hz);
448 dprintk(1, "- xtal_hz = %d\n", state->xtal_hz);
449 dprintk(1, "- pll_mfactor = %d\n", state->pll_mfactor);
450 dprintk(1, "- pll_nfactor = %d\n", state->pll_nfactor);
451 dprintk(1, "- pll_pfactor = %d\n", state->pll_pfactor);
453 /* Calculate the sample frequency */
454 state->sample_freq = state->xtal_hz * (state->pll_mfactor + 45);
455 state->sample_freq /= (state->pll_nfactor + 1);
456 state->sample_freq /= (state->pll_pfactor + 4);
457 dprintk(1, "- sample_freq = %d\n", state->sample_freq);
460 tda10048_set_phy2(fe, state->sample_freq, state->freq_if_hz);
465 static int tda10048_firmware_upload(struct dvb_frontend *fe)
467 struct tda10048_state *state = fe->demodulator_priv;
468 struct tda10048_config *config = &state->config;
469 const struct firmware *fw;
473 u8 wlen = config->fwbulkwritelen;
475 if ((wlen != TDA10048_BULKWRITE_200) && (wlen != TDA10048_BULKWRITE_50))
476 wlen = TDA10048_BULKWRITE_200;
478 /* request the firmware, this will block and timeout */
479 printk(KERN_INFO "%s: waiting for firmware upload (%s)...\n",
481 TDA10048_DEFAULT_FIRMWARE);
483 ret = request_firmware(&fw, TDA10048_DEFAULT_FIRMWARE,
484 state->i2c->dev.parent);
486 printk(KERN_ERR "%s: Upload failed. (file not found?)\n",
490 printk(KERN_INFO "%s: firmware read %zu bytes.\n",
496 if (fw->size != TDA10048_DEFAULT_FIRMWARE_SIZE) {
497 printk(KERN_ERR "%s: firmware incorrect size\n", __func__);
500 printk(KERN_INFO "%s: firmware uploading\n", __func__);
503 tda10048_writereg(state, TDA10048_CONF_TRISTATE1,
504 tda10048_readreg(state, TDA10048_CONF_TRISTATE1)
506 tda10048_writereg(state, TDA10048_CONF_TRISTATE1,
507 tda10048_readreg(state, TDA10048_CONF_TRISTATE1)
510 /* Put the demod into host download mode */
511 tda10048_writereg(state, TDA10048_CONF_C4_1,
512 tda10048_readreg(state, TDA10048_CONF_C4_1) & 0xf9);
515 tda10048_writereg(state, TDA10048_CONF_C4_1,
516 tda10048_readreg(state, TDA10048_CONF_C4_1) | 0x08);
518 /* Prepare for download */
519 tda10048_writereg(state, TDA10048_DSP_CODE_CPT, 0);
521 /* Download the firmware payload */
522 while (pos < fw->size) {
524 if ((fw->size - pos) > wlen)
527 cnt = fw->size - pos;
529 tda10048_writeregbulk(state, TDA10048_DSP_CODE_IN,
530 &fw->data[pos], cnt);
536 /* Wait up to 250ms for the DSP to boot */
537 for (cnt = 0; cnt < 250 ; cnt += 10) {
541 if (tda10048_readreg(state, TDA10048_SYNC_STATUS)
549 release_firmware(fw);
552 printk(KERN_INFO "%s: firmware uploaded\n", __func__);
555 printk(KERN_ERR "%s: firmware upload failed\n", __func__);
560 static int tda10048_set_inversion(struct dvb_frontend *fe, int inversion)
562 struct tda10048_state *state = fe->demodulator_priv;
564 dprintk(1, "%s(%d)\n", __func__, inversion);
566 if (inversion == TDA10048_INVERSION_ON)
567 tda10048_writereg(state, TDA10048_CONF_C1_1,
568 tda10048_readreg(state, TDA10048_CONF_C1_1) | 0x20);
570 tda10048_writereg(state, TDA10048_CONF_C1_1,
571 tda10048_readreg(state, TDA10048_CONF_C1_1) & 0xdf);
576 /* Retrieve the demod settings */
577 static int tda10048_get_tps(struct tda10048_state *state,
578 struct dtv_frontend_properties *p)
582 /* Make sure the TPS regs are valid */
583 if (!(tda10048_readreg(state, TDA10048_AUTO) & 0x01))
586 val = tda10048_readreg(state, TDA10048_OUT_CONF2);
587 switch ((val & 0x60) >> 5) {
589 p->modulation = QPSK;
592 p->modulation = QAM_16;
595 p->modulation = QAM_64;
598 switch ((val & 0x18) >> 3) {
600 p->hierarchy = HIERARCHY_NONE;
603 p->hierarchy = HIERARCHY_1;
606 p->hierarchy = HIERARCHY_2;
609 p->hierarchy = HIERARCHY_4;
612 switch (val & 0x07) {
614 p->code_rate_HP = FEC_1_2;
617 p->code_rate_HP = FEC_2_3;
620 p->code_rate_HP = FEC_3_4;
623 p->code_rate_HP = FEC_5_6;
626 p->code_rate_HP = FEC_7_8;
630 val = tda10048_readreg(state, TDA10048_OUT_CONF3);
631 switch (val & 0x07) {
633 p->code_rate_LP = FEC_1_2;
636 p->code_rate_LP = FEC_2_3;
639 p->code_rate_LP = FEC_3_4;
642 p->code_rate_LP = FEC_5_6;
645 p->code_rate_LP = FEC_7_8;
649 val = tda10048_readreg(state, TDA10048_OUT_CONF1);
650 switch ((val & 0x0c) >> 2) {
652 p->guard_interval = GUARD_INTERVAL_1_32;
655 p->guard_interval = GUARD_INTERVAL_1_16;
658 p->guard_interval = GUARD_INTERVAL_1_8;
661 p->guard_interval = GUARD_INTERVAL_1_4;
664 switch (val & 0x03) {
666 p->transmission_mode = TRANSMISSION_MODE_2K;
669 p->transmission_mode = TRANSMISSION_MODE_8K;
676 static int tda10048_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
678 struct tda10048_state *state = fe->demodulator_priv;
679 struct tda10048_config *config = &state->config;
680 dprintk(1, "%s(%d)\n", __func__, enable);
682 if (config->disable_gate_access)
686 return tda10048_writereg(state, TDA10048_CONF_C4_1,
687 tda10048_readreg(state, TDA10048_CONF_C4_1) | 0x02);
689 return tda10048_writereg(state, TDA10048_CONF_C4_1,
690 tda10048_readreg(state, TDA10048_CONF_C4_1) & 0xfd);
693 static int tda10048_output_mode(struct dvb_frontend *fe, int serial)
695 struct tda10048_state *state = fe->demodulator_priv;
696 dprintk(1, "%s(%d)\n", __func__, serial);
698 /* Ensure pins are out of tri-state */
699 tda10048_writereg(state, TDA10048_CONF_TRISTATE1, 0x21);
700 tda10048_writereg(state, TDA10048_CONF_TRISTATE2, 0x00);
703 tda10048_writereg(state, TDA10048_IC_MODE, 0x80 | 0x20);
704 tda10048_writereg(state, TDA10048_CONF_TS2, 0xc0);
706 tda10048_writereg(state, TDA10048_IC_MODE, 0x00);
707 tda10048_writereg(state, TDA10048_CONF_TS2, 0x01);
713 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
714 /* TODO: Support manual tuning with specific params */
715 static int tda10048_set_frontend(struct dvb_frontend *fe)
717 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
718 struct tda10048_state *state = fe->demodulator_priv;
720 dprintk(1, "%s(frequency=%d)\n", __func__, p->frequency);
722 /* Update the I/F pll's if the bandwidth changes */
723 if (p->bandwidth_hz != state->bandwidth) {
724 tda10048_set_if(fe, p->bandwidth_hz);
725 tda10048_set_bandwidth(fe, p->bandwidth_hz);
728 if (fe->ops.tuner_ops.set_params) {
730 if (fe->ops.i2c_gate_ctrl)
731 fe->ops.i2c_gate_ctrl(fe, 1);
733 fe->ops.tuner_ops.set_params(fe);
735 if (fe->ops.i2c_gate_ctrl)
736 fe->ops.i2c_gate_ctrl(fe, 0);
739 /* Enable demod TPS auto detection and begin acquisition */
740 tda10048_writereg(state, TDA10048_AUTO, 0x57);
741 /* trigger cber and vber acquisition */
742 tda10048_writereg(state, TDA10048_CVBER_CTRL, 0x3B);
747 /* Establish sane defaults and load firmware. */
748 static int tda10048_init(struct dvb_frontend *fe)
750 struct tda10048_state *state = fe->demodulator_priv;
751 struct tda10048_config *config = &state->config;
754 dprintk(1, "%s()\n", __func__);
757 init_tab[4].data = (u8)(state->pll_mfactor);
758 init_tab[5].data = (u8)(state->pll_nfactor) | 0x40;
760 /* Apply register defaults */
761 for (i = 0; i < ARRAY_SIZE(init_tab); i++)
762 tda10048_writereg(state, init_tab[i].reg, init_tab[i].data);
764 if (state->fwloaded == 0)
765 ret = tda10048_firmware_upload(fe);
767 /* Set either serial or parallel */
768 tda10048_output_mode(fe, config->output_mode);
771 tda10048_set_inversion(fe, config->inversion);
773 /* Establish default RF values */
774 tda10048_set_if(fe, 8000000);
775 tda10048_set_bandwidth(fe, 8000000);
777 /* Ensure we leave the gate closed */
778 tda10048_i2c_gate_ctrl(fe, 0);
783 static int tda10048_read_status(struct dvb_frontend *fe, enum fe_status *status)
785 struct tda10048_state *state = fe->demodulator_priv;
790 reg = tda10048_readreg(state, TDA10048_SYNC_STATUS);
792 dprintk(1, "%s() status =0x%02x\n", __func__, reg);
795 *status |= FE_HAS_CARRIER;
798 *status |= FE_HAS_SIGNAL;
801 *status |= FE_HAS_LOCK;
802 *status |= FE_HAS_VITERBI;
803 *status |= FE_HAS_SYNC;
809 static int tda10048_read_ber(struct dvb_frontend *fe, u32 *ber)
811 struct tda10048_state *state = fe->demodulator_priv;
812 static u32 cber_current;
816 dprintk(1, "%s()\n", __func__);
818 /* update cber on interrupt */
819 if (tda10048_readreg(state, TDA10048_SOFT_IT_C3) & 0x01) {
820 cber_tmp = tda10048_readreg(state, TDA10048_CBER_MSB) << 8 |
821 tda10048_readreg(state, TDA10048_CBER_LSB);
822 cber_nmax = tda10048_readreg(state, TDA10048_CBER_NMAX_MSB) << 8 |
823 tda10048_readreg(state, TDA10048_CBER_NMAX_LSB);
824 cber_tmp *= 100000000;
826 cber_tmp = div_u64(cber_tmp, (cber_nmax * 32) + 1);
827 cber_current = (u32)cber_tmp;
828 /* retrigger cber acquisition */
829 tda10048_writereg(state, TDA10048_CVBER_CTRL, 0x39);
831 /* actual cber is (*ber)/1e8 */
837 static int tda10048_read_signal_strength(struct dvb_frontend *fe,
838 u16 *signal_strength)
840 struct tda10048_state *state = fe->demodulator_priv;
843 dprintk(1, "%s()\n", __func__);
845 *signal_strength = 65535;
847 v = tda10048_readreg(state, TDA10048_NP_OUT);
849 *signal_strength -= (v << 8) | v;
854 /* SNR lookup table */
855 static struct snr_tab {
984 static int tda10048_read_snr(struct dvb_frontend *fe, u16 *snr)
986 struct tda10048_state *state = fe->demodulator_priv;
988 int i, ret = -EINVAL;
990 dprintk(1, "%s()\n", __func__);
992 v = tda10048_readreg(state, TDA10048_NP_OUT);
993 for (i = 0; i < ARRAY_SIZE(snr_tab); i++) {
994 if (v <= snr_tab[i].val) {
995 *snr = snr_tab[i].data;
1004 static int tda10048_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1006 struct tda10048_state *state = fe->demodulator_priv;
1008 dprintk(1, "%s()\n", __func__);
1010 *ucblocks = tda10048_readreg(state, TDA10048_UNCOR_CPT_MSB) << 8 |
1011 tda10048_readreg(state, TDA10048_UNCOR_CPT_LSB);
1012 /* clear the uncorrected TS packets counter when saturated */
1013 if (*ucblocks == 0xFFFF)
1014 tda10048_writereg(state, TDA10048_UNCOR_CTRL, 0x80);
1019 static int tda10048_get_frontend(struct dvb_frontend *fe,
1020 struct dtv_frontend_properties *p)
1022 struct tda10048_state *state = fe->demodulator_priv;
1024 dprintk(1, "%s()\n", __func__);
1026 p->inversion = tda10048_readreg(state, TDA10048_CONF_C1_1)
1027 & 0x20 ? INVERSION_ON : INVERSION_OFF;
1029 return tda10048_get_tps(state, p);
1032 static int tda10048_get_tune_settings(struct dvb_frontend *fe,
1033 struct dvb_frontend_tune_settings *tune)
1035 tune->min_delay_ms = 1000;
1039 static void tda10048_release(struct dvb_frontend *fe)
1041 struct tda10048_state *state = fe->demodulator_priv;
1042 dprintk(1, "%s()\n", __func__);
1046 static void tda10048_establish_defaults(struct dvb_frontend *fe)
1048 struct tda10048_state *state = fe->demodulator_priv;
1049 struct tda10048_config *config = &state->config;
1051 /* Validate/default the config */
1052 if (config->dtv6_if_freq_khz == 0) {
1053 config->dtv6_if_freq_khz = TDA10048_IF_4300;
1054 printk(KERN_WARNING "%s() tda10048_config.dtv6_if_freq_khz is not set (defaulting to %d)\n",
1056 config->dtv6_if_freq_khz);
1059 if (config->dtv7_if_freq_khz == 0) {
1060 config->dtv7_if_freq_khz = TDA10048_IF_4300;
1061 printk(KERN_WARNING "%s() tda10048_config.dtv7_if_freq_khz is not set (defaulting to %d)\n",
1063 config->dtv7_if_freq_khz);
1066 if (config->dtv8_if_freq_khz == 0) {
1067 config->dtv8_if_freq_khz = TDA10048_IF_4300;
1068 printk(KERN_WARNING "%s() tda10048_config.dtv8_if_freq_khz is not set (defaulting to %d)\n",
1070 config->dtv8_if_freq_khz);
1073 if (config->clk_freq_khz == 0) {
1074 config->clk_freq_khz = TDA10048_CLK_16000;
1075 printk(KERN_WARNING "%s() tda10048_config.clk_freq_khz is not set (defaulting to %d)\n",
1077 config->clk_freq_khz);
1081 static const struct dvb_frontend_ops tda10048_ops;
1083 struct dvb_frontend *tda10048_attach(const struct tda10048_config *config,
1084 struct i2c_adapter *i2c)
1086 struct tda10048_state *state = NULL;
1088 dprintk(1, "%s()\n", __func__);
1090 /* allocate memory for the internal state */
1091 state = kzalloc(sizeof(struct tda10048_state), GFP_KERNEL);
1095 /* setup the state and clone the config */
1096 memcpy(&state->config, config, sizeof(*config));
1098 state->fwloaded = config->no_firmware;
1099 state->bandwidth = 8000000;
1101 /* check if the demod is present */
1102 if (tda10048_readreg(state, TDA10048_IDENTITY) != 0x048)
1105 /* create dvb_frontend */
1106 memcpy(&state->frontend.ops, &tda10048_ops,
1107 sizeof(struct dvb_frontend_ops));
1108 state->frontend.demodulator_priv = state;
1111 if (config->set_pll) {
1112 state->pll_mfactor = config->pll_m;
1113 state->pll_nfactor = config->pll_n;
1114 state->pll_pfactor = config->pll_p;
1116 state->pll_mfactor = 10;
1117 state->pll_nfactor = 3;
1118 state->pll_pfactor = 0;
1121 /* Establish any defaults the user didn't pass */
1122 tda10048_establish_defaults(&state->frontend);
1124 /* Set the xtal and freq defaults */
1125 if (tda10048_set_if(&state->frontend, 8000000) != 0)
1128 /* Default bandwidth */
1129 if (tda10048_set_bandwidth(&state->frontend, 8000000) != 0)
1132 /* Leave the gate closed */
1133 tda10048_i2c_gate_ctrl(&state->frontend, 0);
1135 return &state->frontend;
1141 EXPORT_SYMBOL(tda10048_attach);
1143 static const struct dvb_frontend_ops tda10048_ops = {
1144 .delsys = { SYS_DVBT },
1146 .name = "NXP TDA10048HN DVB-T",
1147 .frequency_min_hz = 177 * MHz,
1148 .frequency_max_hz = 858 * MHz,
1149 .frequency_stepsize_hz = 166666,
1150 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1151 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1152 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
1153 FE_CAN_HIERARCHY_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
1154 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER
1157 .release = tda10048_release,
1158 .init = tda10048_init,
1159 .i2c_gate_ctrl = tda10048_i2c_gate_ctrl,
1160 .set_frontend = tda10048_set_frontend,
1161 .get_frontend = tda10048_get_frontend,
1162 .get_tune_settings = tda10048_get_tune_settings,
1163 .read_status = tda10048_read_status,
1164 .read_ber = tda10048_read_ber,
1165 .read_signal_strength = tda10048_read_signal_strength,
1166 .read_snr = tda10048_read_snr,
1167 .read_ucblocks = tda10048_read_ucblocks,
1170 module_param(debug, int, 0644);
1171 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
1173 MODULE_DESCRIPTION("NXP TDA10048HN DVB-T Demodulator driver");
1174 MODULE_AUTHOR("Steven Toth");
1175 MODULE_LICENSE("GPL");