2 Driver for Philips tda1004xh OFDM Demodulator
4 (c) 2003, 2004 Andrew de Quincey & Robert Schlabbach
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
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
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * This driver needs external firmware. Please use the commands
24 * "<kerneldir>/Documentation/dvb/get_dvb_firmware tda10045",
25 * "<kerneldir>/Documentation/dvb/get_dvb_firmware tda10046" to
26 * download/extract them, and then copy them to /usr/lib/hotplug/firmware
27 * or /lib/firmware (depending on configuration of firmware hotplug).
29 #define TDA10045_DEFAULT_FIRMWARE "dvb-fe-tda10045.fw"
30 #define TDA10046_DEFAULT_FIRMWARE "dvb-fe-tda10046.fw"
32 #include <linux/init.h>
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/device.h>
36 #include <linux/jiffies.h>
37 #include <linux/string.h>
38 #include <linux/slab.h>
40 #include "dvb_frontend.h"
44 TDA1004X_DEMOD_TDA10045,
45 TDA1004X_DEMOD_TDA10046,
48 struct tda1004x_state {
49 struct i2c_adapter* i2c;
50 struct dvb_frontend_ops ops;
51 const struct tda1004x_config* config;
52 struct dvb_frontend frontend;
54 /* private demod data */
55 enum tda1004x_demod demod_type;
59 #define dprintk(args...) \
61 if (debug) printk(KERN_DEBUG "tda1004x: " args); \
64 #define TDA1004X_CHIPID 0x00
65 #define TDA1004X_AUTO 0x01
66 #define TDA1004X_IN_CONF1 0x02
67 #define TDA1004X_IN_CONF2 0x03
68 #define TDA1004X_OUT_CONF1 0x04
69 #define TDA1004X_OUT_CONF2 0x05
70 #define TDA1004X_STATUS_CD 0x06
71 #define TDA1004X_CONFC4 0x07
72 #define TDA1004X_DSSPARE2 0x0C
73 #define TDA10045H_CODE_IN 0x0D
74 #define TDA10045H_FWPAGE 0x0E
75 #define TDA1004X_SCAN_CPT 0x10
76 #define TDA1004X_DSP_CMD 0x11
77 #define TDA1004X_DSP_ARG 0x12
78 #define TDA1004X_DSP_DATA1 0x13
79 #define TDA1004X_DSP_DATA2 0x14
80 #define TDA1004X_CONFADC1 0x15
81 #define TDA1004X_CONFC1 0x16
82 #define TDA10045H_S_AGC 0x1a
83 #define TDA10046H_AGC_TUN_LEVEL 0x1a
84 #define TDA1004X_SNR 0x1c
85 #define TDA1004X_CONF_TS1 0x1e
86 #define TDA1004X_CONF_TS2 0x1f
87 #define TDA1004X_CBER_RESET 0x20
88 #define TDA1004X_CBER_MSB 0x21
89 #define TDA1004X_CBER_LSB 0x22
90 #define TDA1004X_CVBER_LUT 0x23
91 #define TDA1004X_VBER_MSB 0x24
92 #define TDA1004X_VBER_MID 0x25
93 #define TDA1004X_VBER_LSB 0x26
94 #define TDA1004X_UNCOR 0x27
96 #define TDA10045H_CONFPLL_P 0x2D
97 #define TDA10045H_CONFPLL_M_MSB 0x2E
98 #define TDA10045H_CONFPLL_M_LSB 0x2F
99 #define TDA10045H_CONFPLL_N 0x30
101 #define TDA10046H_CONFPLL1 0x2D
102 #define TDA10046H_CONFPLL2 0x2F
103 #define TDA10046H_CONFPLL3 0x30
104 #define TDA10046H_TIME_WREF1 0x31
105 #define TDA10046H_TIME_WREF2 0x32
106 #define TDA10046H_TIME_WREF3 0x33
107 #define TDA10046H_TIME_WREF4 0x34
108 #define TDA10046H_TIME_WREF5 0x35
110 #define TDA10045H_UNSURW_MSB 0x31
111 #define TDA10045H_UNSURW_LSB 0x32
112 #define TDA10045H_WREF_MSB 0x33
113 #define TDA10045H_WREF_MID 0x34
114 #define TDA10045H_WREF_LSB 0x35
115 #define TDA10045H_MUXOUT 0x36
116 #define TDA1004X_CONFADC2 0x37
118 #define TDA10045H_IOFFSET 0x38
120 #define TDA10046H_CONF_TRISTATE1 0x3B
121 #define TDA10046H_CONF_TRISTATE2 0x3C
122 #define TDA10046H_CONF_POLARITY 0x3D
123 #define TDA10046H_FREQ_OFFSET 0x3E
124 #define TDA10046H_GPIO_OUT_SEL 0x41
125 #define TDA10046H_GPIO_SELECT 0x42
126 #define TDA10046H_AGC_CONF 0x43
127 #define TDA10046H_AGC_THR 0x44
128 #define TDA10046H_AGC_RENORM 0x45
129 #define TDA10046H_AGC_GAINS 0x46
130 #define TDA10046H_AGC_TUN_MIN 0x47
131 #define TDA10046H_AGC_TUN_MAX 0x48
132 #define TDA10046H_AGC_IF_MIN 0x49
133 #define TDA10046H_AGC_IF_MAX 0x4A
135 #define TDA10046H_FREQ_PHY2_MSB 0x4D
136 #define TDA10046H_FREQ_PHY2_LSB 0x4E
138 #define TDA10046H_CVBER_CTRL 0x4F
139 #define TDA10046H_AGC_IF_LEVEL 0x52
140 #define TDA10046H_CODE_CPT 0x57
141 #define TDA10046H_CODE_IN 0x58
144 static int tda1004x_write_byteI(struct tda1004x_state *state, int reg, int data)
147 u8 buf[] = { reg, data };
148 struct i2c_msg msg = { .flags = 0, .buf = buf, .len = 2 };
150 dprintk("%s: reg=0x%x, data=0x%x\n", __FUNCTION__, reg, data);
152 msg.addr = state->config->demod_address;
153 ret = i2c_transfer(state->i2c, &msg, 1);
156 dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
157 __FUNCTION__, reg, data, ret);
159 dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __FUNCTION__,
161 return (ret != 1) ? -1 : 0;
164 static int tda1004x_read_byte(struct tda1004x_state *state, int reg)
169 struct i2c_msg msg[] = {{ .flags = 0, .buf = b0, .len = 1 },
170 { .flags = I2C_M_RD, .buf = b1, .len = 1 }};
172 dprintk("%s: reg=0x%x\n", __FUNCTION__, reg);
174 msg[0].addr = state->config->demod_address;
175 msg[1].addr = state->config->demod_address;
176 ret = i2c_transfer(state->i2c, msg, 2);
179 dprintk("%s: error reg=0x%x, ret=%i\n", __FUNCTION__, reg,
184 dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __FUNCTION__,
189 static int tda1004x_write_mask(struct tda1004x_state *state, int reg, int mask, int data)
192 dprintk("%s: reg=0x%x, mask=0x%x, data=0x%x\n", __FUNCTION__, reg,
195 // read a byte and check
196 val = tda1004x_read_byte(state, reg);
204 // write it out again
205 return tda1004x_write_byteI(state, reg, val);
208 static int tda1004x_write_buf(struct tda1004x_state *state, int reg, unsigned char *buf, int len)
213 dprintk("%s: reg=0x%x, len=0x%x\n", __FUNCTION__, reg, len);
216 for (i = 0; i < len; i++) {
217 result = tda1004x_write_byteI(state, reg + i, buf[i]);
225 static int tda1004x_enable_tuner_i2c(struct tda1004x_state *state)
228 dprintk("%s\n", __FUNCTION__);
230 result = tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 2);
235 static int tda1004x_disable_tuner_i2c(struct tda1004x_state *state)
237 dprintk("%s\n", __FUNCTION__);
239 return tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 0);
242 static int tda10045h_set_bandwidth(struct tda1004x_state *state,
243 fe_bandwidth_t bandwidth)
245 static u8 bandwidth_6mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x60, 0x1e, 0xa7, 0x45, 0x4f };
246 static u8 bandwidth_7mhz[] = { 0x02, 0x00, 0x37, 0x00, 0x4a, 0x2f, 0x6d, 0x76, 0xdb };
247 static u8 bandwidth_8mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x48, 0x17, 0x89, 0xc7, 0x14 };
250 case BANDWIDTH_6_MHZ:
251 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_6mhz, sizeof(bandwidth_6mhz));
254 case BANDWIDTH_7_MHZ:
255 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_7mhz, sizeof(bandwidth_7mhz));
258 case BANDWIDTH_8_MHZ:
259 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_8mhz, sizeof(bandwidth_8mhz));
266 tda1004x_write_byteI(state, TDA10045H_IOFFSET, 0);
271 static int tda10046h_set_bandwidth(struct tda1004x_state *state,
272 fe_bandwidth_t bandwidth)
274 static u8 bandwidth_6mhz_53M[] = { 0x7b, 0x2e, 0x11, 0xf0, 0xd2 };
275 static u8 bandwidth_7mhz_53M[] = { 0x6a, 0x02, 0x6a, 0x43, 0x9f };
276 static u8 bandwidth_8mhz_53M[] = { 0x5c, 0x32, 0xc2, 0x96, 0x6d };
278 static u8 bandwidth_6mhz_48M[] = { 0x70, 0x02, 0x49, 0x24, 0x92 };
279 static u8 bandwidth_7mhz_48M[] = { 0x60, 0x02, 0xaa, 0xaa, 0xab };
280 static u8 bandwidth_8mhz_48M[] = { 0x54, 0x03, 0x0c, 0x30, 0xc3 };
283 if ((state->config->if_freq == TDA10046_FREQ_045) ||
284 (state->config->if_freq == TDA10046_FREQ_052))
289 case BANDWIDTH_6_MHZ:
291 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz_53M,
292 sizeof(bandwidth_6mhz_53M));
294 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz_48M,
295 sizeof(bandwidth_6mhz_48M));
296 if (state->config->if_freq == TDA10046_FREQ_045) {
297 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0a);
298 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xab);
302 case BANDWIDTH_7_MHZ:
304 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz_53M,
305 sizeof(bandwidth_7mhz_53M));
307 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz_48M,
308 sizeof(bandwidth_7mhz_48M));
309 if (state->config->if_freq == TDA10046_FREQ_045) {
310 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
311 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x00);
315 case BANDWIDTH_8_MHZ:
317 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz_53M,
318 sizeof(bandwidth_8mhz_53M));
320 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz_48M,
321 sizeof(bandwidth_8mhz_48M));
322 if (state->config->if_freq == TDA10046_FREQ_045) {
323 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0d);
324 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x55);
335 static int tda1004x_do_upload(struct tda1004x_state *state,
336 unsigned char *mem, unsigned int len,
337 u8 dspCodeCounterReg, u8 dspCodeInReg)
340 struct i2c_msg fw_msg = { .flags = 0, .buf = buf, .len = 0 };
344 /* clear code counter */
345 tda1004x_write_byteI(state, dspCodeCounterReg, 0);
346 fw_msg.addr = state->config->demod_address;
348 buf[0] = dspCodeInReg;
350 // work out how much to send this time
356 memcpy(buf + 1, mem + pos, tx_size);
357 fw_msg.len = tx_size + 1;
358 if (i2c_transfer(state->i2c, &fw_msg, 1) != 1) {
359 printk(KERN_ERR "tda1004x: Error during firmware upload\n");
364 dprintk("%s: fw_pos=0x%x\n", __FUNCTION__, pos);
366 // give the DSP a chance to settle 03/10/05 Hac
372 static int tda1004x_check_upload_ok(struct tda1004x_state *state)
375 unsigned long timeout;
377 if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
378 timeout = jiffies + 2 * HZ;
379 while(!(tda1004x_read_byte(state, TDA1004X_STATUS_CD) & 0x20)) {
380 if (time_after(jiffies, timeout)) {
381 printk(KERN_ERR "tda1004x: timeout waiting for DSP ready\n");
389 // check upload was OK
390 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0); // we want to read from the DSP
391 tda1004x_write_byteI(state, TDA1004X_DSP_CMD, 0x67);
393 data1 = tda1004x_read_byte(state, TDA1004X_DSP_DATA1);
394 data2 = tda1004x_read_byte(state, TDA1004X_DSP_DATA2);
395 if (data1 != 0x67 || data2 < 0x20 || data2 > 0x2e) {
396 printk(KERN_INFO "tda1004x: found firmware revision %x -- invalid\n", data2);
399 printk(KERN_INFO "tda1004x: found firmware revision %x -- ok\n", data2);
403 static int tda10045_fwupload(struct dvb_frontend* fe)
405 struct tda1004x_state* state = fe->demodulator_priv;
407 const struct firmware *fw;
409 /* don't re-upload unless necessary */
410 if (tda1004x_check_upload_ok(state) == 0)
413 /* request the firmware, this will block until someone uploads it */
414 printk(KERN_INFO "tda1004x: waiting for firmware upload (%s)...\n", TDA10045_DEFAULT_FIRMWARE);
415 ret = state->config->request_firmware(fe, &fw, TDA10045_DEFAULT_FIRMWARE);
417 printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
422 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0);
423 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
424 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
428 tda10045h_set_bandwidth(state, BANDWIDTH_8_MHZ);
430 ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10045H_FWPAGE, TDA10045H_CODE_IN);
431 release_firmware(fw);
434 printk(KERN_INFO "tda1004x: firmware upload complete\n");
436 /* wait for DSP to initialise */
437 /* DSPREADY doesn't seem to work on the TDA10045H */
440 return tda1004x_check_upload_ok(state);
443 static void tda10046_init_plls(struct dvb_frontend* fe)
445 struct tda1004x_state* state = fe->demodulator_priv;
448 if ((state->config->if_freq == TDA10046_FREQ_045) ||
449 (state->config->if_freq == TDA10046_FREQ_052))
454 tda1004x_write_byteI(state, TDA10046H_CONFPLL1, 0xf0);
455 if(tda10046_clk53m) {
456 printk(KERN_INFO "tda1004x: setting up plls for 53MHz sampling clock\n");
457 tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 0x08); // PLL M = 8
459 printk(KERN_INFO "tda1004x: setting up plls for 48MHz sampling clock\n");
460 tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 0x03); // PLL M = 3
462 if (state->config->xtal_freq == TDA10046_XTAL_4M ) {
463 dprintk("%s: setting up PLLs for a 4 MHz Xtal\n", __FUNCTION__);
464 tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 0); // PLL P = N = 0
466 dprintk("%s: setting up PLLs for a 16 MHz Xtal\n", __FUNCTION__);
467 tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 3); // PLL P = 0, N = 3
470 tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 0x67);
472 tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 0x72);
473 /* Note clock frequency is handled implicitly */
474 switch (state->config->if_freq) {
475 case TDA10046_FREQ_045:
476 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
477 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x00);
479 case TDA10046_FREQ_052:
480 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0d);
481 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xc7);
483 case TDA10046_FREQ_3617:
484 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd7);
485 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x59);
487 case TDA10046_FREQ_3613:
488 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd7);
489 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x3f);
492 tda10046h_set_bandwidth(state, BANDWIDTH_8_MHZ); // default bandwidth 8 MHz
493 /* let the PLLs settle */
497 static int tda10046_fwupload(struct dvb_frontend* fe)
499 struct tda1004x_state* state = fe->demodulator_priv;
501 const struct firmware *fw;
503 /* reset + wake up chip */
504 if (state->config->xtal_freq == TDA10046_XTAL_4M) {
505 tda1004x_write_byteI(state, TDA1004X_CONFC4, 0);
507 dprintk("%s: 16MHz Xtal, reducing I2C speed\n", __FUNCTION__);
508 tda1004x_write_byteI(state, TDA1004X_CONFC4, 0x80);
510 tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 1, 0);
511 /* let the clocks recover from sleep */
514 /* The PLLs need to be reprogrammed after sleep */
515 tda10046_init_plls(fe);
517 /* don't re-upload unless necessary */
518 if (tda1004x_check_upload_ok(state) == 0)
521 if (state->config->request_firmware != NULL) {
522 /* request the firmware, this will block until someone uploads it */
523 printk(KERN_INFO "tda1004x: waiting for firmware upload...\n");
524 ret = state->config->request_firmware(fe, &fw, TDA10046_DEFAULT_FIRMWARE);
526 printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
529 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8); // going to boot from HOST
530 ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10046H_CODE_CPT, TDA10046H_CODE_IN);
531 release_firmware(fw);
535 /* boot from firmware eeprom */
536 printk(KERN_INFO "tda1004x: booting from eeprom\n");
537 tda1004x_write_mask(state, TDA1004X_CONFC4, 4, 4);
540 return tda1004x_check_upload_ok(state);
543 static int tda1004x_encode_fec(int fec)
545 // convert known FEC values
563 static int tda1004x_decode_fec(int tdafec)
565 // convert known FEC values
583 int tda1004x_write_byte(struct dvb_frontend* fe, int reg, int data)
585 struct tda1004x_state* state = fe->demodulator_priv;
587 return tda1004x_write_byteI(state, reg, data);
590 static int tda10045_init(struct dvb_frontend* fe)
592 struct tda1004x_state* state = fe->demodulator_priv;
594 dprintk("%s\n", __FUNCTION__);
596 if (tda10045_fwupload(fe)) {
597 printk("tda1004x: firmware upload failed\n");
601 tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0); // wake up the ADC
604 if (state->config->pll_init) {
605 tda1004x_enable_tuner_i2c(state);
606 state->config->pll_init(fe);
607 tda1004x_disable_tuner_i2c(state);
611 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
612 tda1004x_write_mask(state, TDA1004X_AUTO, 8, 0); // select HP stream
613 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x40, 0); // set polarity of VAGC signal
614 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x80, 0x80); // enable pulse killer
615 tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10); // enable auto offset
616 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0x0); // no frequency offset
617 tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 0); // setup MPEG2 TS interface
618 tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0); // setup MPEG2 TS interface
619 tda1004x_write_mask(state, TDA1004X_VBER_MSB, 0xe0, 0xa0); // 10^6 VBER measurement bits
620 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x10, 0); // VAGC polarity
621 tda1004x_write_byteI(state, TDA1004X_CONFADC1, 0x2e);
623 tda1004x_write_mask(state, 0x1f, 0x01, state->config->invert_oclk);
628 static int tda10046_init(struct dvb_frontend* fe)
630 struct tda1004x_state* state = fe->demodulator_priv;
631 dprintk("%s\n", __FUNCTION__);
633 if (tda10046_fwupload(fe)) {
634 printk("tda1004x: firmware upload failed\n");
638 // Init the tuner PLL
639 if (state->config->pll_init) {
640 tda1004x_enable_tuner_i2c(state);
641 if (state->config->pll_init(fe)) {
642 printk(KERN_ERR "tda1004x: pll init failed\n");
645 tda1004x_disable_tuner_i2c(state);
649 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
650 tda1004x_write_byteI(state, TDA1004X_AUTO, 0x87); // 100 ppm crystal, select HP stream
651 tda1004x_write_byteI(state, TDA1004X_CONFC1, 0x88); // enable pulse killer
653 switch (state->config->agc_config) {
654 case TDA10046_AGC_DEFAULT:
655 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x00); // AGC setup
656 tda1004x_write_byteI(state, TDA10046H_CONF_POLARITY, 0x60); // set AGC polarities
658 case TDA10046_AGC_IFO_AUTO_NEG:
659 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x0a); // AGC setup
660 tda1004x_write_byteI(state, TDA10046H_CONF_POLARITY, 0x60); // set AGC polarities
662 case TDA10046_AGC_IFO_AUTO_POS:
663 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x0a); // AGC setup
664 tda1004x_write_byteI(state, TDA10046H_CONF_POLARITY, 0x00); // set AGC polarities
666 case TDA10046_AGC_TDA827X:
667 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x02); // AGC setup
668 tda1004x_write_byteI(state, TDA10046H_AGC_THR, 0x70); // AGC Threshold
669 tda1004x_write_byteI(state, TDA10046H_AGC_RENORM, 0x08); // Gain Renormalize
670 tda1004x_write_byteI(state, TDA10046H_CONF_POLARITY, 0x6a); // set AGC polarities
672 case TDA10046_AGC_TDA827X_GPL:
673 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x02); // AGC setup
674 tda1004x_write_byteI(state, TDA10046H_AGC_THR, 0x70); // AGC Threshold
675 tda1004x_write_byteI(state, TDA10046H_AGC_RENORM, 0x08); // Gain Renormalize
676 tda1004x_write_byteI(state, TDA10046H_CONF_POLARITY, 0x60); // set AGC polarities
679 tda1004x_write_byteI(state, TDA1004X_CONFADC2, 0x38);
680 tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE1, 0x61); // Turn both AGC outputs on
681 tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MIN, 0); // }
682 tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MAX, 0xff); // } AGC min/max values
683 tda1004x_write_byteI(state, TDA10046H_AGC_IF_MIN, 0); // }
684 tda1004x_write_byteI(state, TDA10046H_AGC_IF_MAX, 0xff); // }
685 tda1004x_write_byteI(state, TDA10046H_AGC_GAINS, 0x12); // IF gain 2, TUN gain 1
686 tda1004x_write_byteI(state, TDA10046H_CVBER_CTRL, 0x1a); // 10^6 VBER measurement bits
687 tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 7); // MPEG2 interface config
688 tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0xc0); // MPEG2 interface config
689 // tda1004x_write_mask(state, 0x50, 0x80, 0x80); // handle out of guard echoes
690 tda1004x_write_mask(state, 0x3a, 0x80, state->config->invert_oclk << 7);
695 static int tda1004x_set_fe(struct dvb_frontend* fe,
696 struct dvb_frontend_parameters *fe_params)
698 struct tda1004x_state* state = fe->demodulator_priv;
702 dprintk("%s\n", __FUNCTION__);
704 if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
706 tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10);
707 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x80, 0);
708 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0);
710 // disable agc_conf[2]
711 tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 0);
715 tda1004x_enable_tuner_i2c(state);
716 if (state->config->pll_set(fe, fe_params)) {
717 printk(KERN_ERR "tda1004x: pll set failed\n");
720 tda1004x_disable_tuner_i2c(state);
722 // Hardcoded to use auto as much as possible on the TDA10045 as it
723 // is very unreliable if AUTO mode is _not_ used.
724 if (state->demod_type == TDA1004X_DEMOD_TDA10045) {
725 fe_params->u.ofdm.code_rate_HP = FEC_AUTO;
726 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
727 fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
730 // Set standard params.. or put them to auto
731 if ((fe_params->u.ofdm.code_rate_HP == FEC_AUTO) ||
732 (fe_params->u.ofdm.code_rate_LP == FEC_AUTO) ||
733 (fe_params->u.ofdm.constellation == QAM_AUTO) ||
734 (fe_params->u.ofdm.hierarchy_information == HIERARCHY_AUTO)) {
735 tda1004x_write_mask(state, TDA1004X_AUTO, 1, 1); // enable auto
736 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x03, 0); // turn off constellation bits
737 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 0); // turn off hierarchy bits
738 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x3f, 0); // turn off FEC bits
740 tda1004x_write_mask(state, TDA1004X_AUTO, 1, 0); // disable auto
743 tmp = tda1004x_encode_fec(fe_params->u.ofdm.code_rate_HP);
746 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 7, tmp);
749 tmp = tda1004x_encode_fec(fe_params->u.ofdm.code_rate_LP);
752 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x38, tmp << 3);
755 switch (fe_params->u.ofdm.constellation) {
757 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 0);
761 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 1);
765 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 2);
773 switch (fe_params->u.ofdm.hierarchy_information) {
775 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 0 << 5);
779 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 1 << 5);
783 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 2 << 5);
787 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 3 << 5);
796 switch (state->demod_type) {
797 case TDA1004X_DEMOD_TDA10045:
798 tda10045h_set_bandwidth(state, fe_params->u.ofdm.bandwidth);
801 case TDA1004X_DEMOD_TDA10046:
802 tda10046h_set_bandwidth(state, fe_params->u.ofdm.bandwidth);
807 inversion = fe_params->inversion;
808 if (state->config->invert)
809 inversion = inversion ? INVERSION_OFF : INVERSION_ON;
812 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x20, 0);
816 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x20, 0x20);
823 // set guard interval
824 switch (fe_params->u.ofdm.guard_interval) {
825 case GUARD_INTERVAL_1_32:
826 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
827 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 0 << 2);
830 case GUARD_INTERVAL_1_16:
831 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
832 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 1 << 2);
835 case GUARD_INTERVAL_1_8:
836 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
837 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 2 << 2);
840 case GUARD_INTERVAL_1_4:
841 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
842 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 3 << 2);
845 case GUARD_INTERVAL_AUTO:
846 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 2);
847 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 0 << 2);
854 // set transmission mode
855 switch (fe_params->u.ofdm.transmission_mode) {
856 case TRANSMISSION_MODE_2K:
857 tda1004x_write_mask(state, TDA1004X_AUTO, 4, 0);
858 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 0 << 4);
861 case TRANSMISSION_MODE_8K:
862 tda1004x_write_mask(state, TDA1004X_AUTO, 4, 0);
863 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 1 << 4);
866 case TRANSMISSION_MODE_AUTO:
867 tda1004x_write_mask(state, TDA1004X_AUTO, 4, 4);
868 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 0);
876 switch (state->demod_type) {
877 case TDA1004X_DEMOD_TDA10045:
878 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
879 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
882 case TDA1004X_DEMOD_TDA10046:
883 tda1004x_write_mask(state, TDA1004X_AUTO, 0x40, 0x40);
885 tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 1);
894 static int tda1004x_get_fe(struct dvb_frontend* fe, struct dvb_frontend_parameters *fe_params)
896 struct tda1004x_state* state = fe->demodulator_priv;
898 dprintk("%s\n", __FUNCTION__);
901 fe_params->inversion = INVERSION_OFF;
902 if (tda1004x_read_byte(state, TDA1004X_CONFC1) & 0x20)
903 fe_params->inversion = INVERSION_ON;
904 if (state->config->invert)
905 fe_params->inversion = fe_params->inversion ? INVERSION_OFF : INVERSION_ON;
908 switch (state->demod_type) {
909 case TDA1004X_DEMOD_TDA10045:
910 switch (tda1004x_read_byte(state, TDA10045H_WREF_LSB)) {
912 fe_params->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
915 fe_params->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
918 fe_params->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
922 case TDA1004X_DEMOD_TDA10046:
923 switch (tda1004x_read_byte(state, TDA10046H_TIME_WREF1)) {
926 fe_params->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
930 fe_params->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
934 fe_params->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
941 fe_params->u.ofdm.code_rate_HP =
942 tda1004x_decode_fec(tda1004x_read_byte(state, TDA1004X_OUT_CONF2) & 7);
943 fe_params->u.ofdm.code_rate_LP =
944 tda1004x_decode_fec((tda1004x_read_byte(state, TDA1004X_OUT_CONF2) >> 3) & 7);
947 switch (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 3) {
949 fe_params->u.ofdm.constellation = QPSK;
952 fe_params->u.ofdm.constellation = QAM_16;
955 fe_params->u.ofdm.constellation = QAM_64;
960 fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K;
961 if (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x10)
962 fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
965 switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x0c) >> 2) {
967 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_32;
970 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_16;
973 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_8;
976 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_4;
981 switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x60) >> 5) {
983 fe_params->u.ofdm.hierarchy_information = HIERARCHY_NONE;
986 fe_params->u.ofdm.hierarchy_information = HIERARCHY_1;
989 fe_params->u.ofdm.hierarchy_information = HIERARCHY_2;
992 fe_params->u.ofdm.hierarchy_information = HIERARCHY_4;
999 static int tda1004x_read_status(struct dvb_frontend* fe, fe_status_t * fe_status)
1001 struct tda1004x_state* state = fe->demodulator_priv;
1006 dprintk("%s\n", __FUNCTION__);
1009 status = tda1004x_read_byte(state, TDA1004X_STATUS_CD);
1016 *fe_status |= FE_HAS_SIGNAL;
1018 *fe_status |= FE_HAS_CARRIER;
1020 *fe_status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
1022 // if we don't already have VITERBI (i.e. not LOCKED), see if the viterbi
1023 // is getting anything valid
1024 if (!(*fe_status & FE_HAS_VITERBI)) {
1026 cber = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
1029 status = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
1032 cber |= (status << 8);
1033 // The address 0x20 should be read to cope with a TDA10046 bug
1034 tda1004x_read_byte(state, TDA1004X_CBER_RESET);
1037 *fe_status |= FE_HAS_VITERBI;
1040 // if we DO have some valid VITERBI output, but don't already have SYNC
1041 // bytes (i.e. not LOCKED), see if the RS decoder is getting anything valid.
1042 if ((*fe_status & FE_HAS_VITERBI) && (!(*fe_status & FE_HAS_SYNC))) {
1044 vber = tda1004x_read_byte(state, TDA1004X_VBER_LSB);
1047 status = tda1004x_read_byte(state, TDA1004X_VBER_MID);
1050 vber |= (status << 8);
1051 status = tda1004x_read_byte(state, TDA1004X_VBER_MSB);
1054 vber |= (status & 0x0f) << 16;
1055 // The CVBER_LUT should be read to cope with TDA10046 hardware bug
1056 tda1004x_read_byte(state, TDA1004X_CVBER_LUT);
1058 // if RS has passed some valid TS packets, then we must be
1059 // getting some SYNC bytes
1061 *fe_status |= FE_HAS_SYNC;
1065 dprintk("%s: fe_status=0x%x\n", __FUNCTION__, *fe_status);
1069 static int tda1004x_read_signal_strength(struct dvb_frontend* fe, u16 * signal)
1071 struct tda1004x_state* state = fe->demodulator_priv;
1075 dprintk("%s\n", __FUNCTION__);
1077 // determine the register to use
1078 switch (state->demod_type) {
1079 case TDA1004X_DEMOD_TDA10045:
1080 reg = TDA10045H_S_AGC;
1083 case TDA1004X_DEMOD_TDA10046:
1084 reg = TDA10046H_AGC_IF_LEVEL;
1089 tmp = tda1004x_read_byte(state, reg);
1093 *signal = (tmp << 8) | tmp;
1094 dprintk("%s: signal=0x%x\n", __FUNCTION__, *signal);
1098 static int tda1004x_read_snr(struct dvb_frontend* fe, u16 * snr)
1100 struct tda1004x_state* state = fe->demodulator_priv;
1103 dprintk("%s\n", __FUNCTION__);
1106 tmp = tda1004x_read_byte(state, TDA1004X_SNR);
1111 *snr = ((tmp << 8) | tmp);
1112 dprintk("%s: snr=0x%x\n", __FUNCTION__, *snr);
1116 static int tda1004x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
1118 struct tda1004x_state* state = fe->demodulator_priv;
1123 dprintk("%s\n", __FUNCTION__);
1125 // read the UCBLOCKS and reset
1127 tmp = tda1004x_read_byte(state, TDA1004X_UNCOR);
1131 while (counter++ < 5) {
1132 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1133 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1134 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1136 tmp2 = tda1004x_read_byte(state, TDA1004X_UNCOR);
1140 if ((tmp2 < tmp) || (tmp2 == 0))
1147 *ucblocks = 0xffffffff;
1149 dprintk("%s: ucblocks=0x%x\n", __FUNCTION__, *ucblocks);
1153 static int tda1004x_read_ber(struct dvb_frontend* fe, u32* ber)
1155 struct tda1004x_state* state = fe->demodulator_priv;
1158 dprintk("%s\n", __FUNCTION__);
1161 tmp = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
1165 tmp = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
1169 // The address 0x20 should be read to cope with a TDA10046 bug
1170 tda1004x_read_byte(state, TDA1004X_CBER_RESET);
1172 dprintk("%s: ber=0x%x\n", __FUNCTION__, *ber);
1176 static int tda1004x_sleep(struct dvb_frontend* fe)
1178 struct tda1004x_state* state = fe->demodulator_priv;
1180 switch (state->demod_type) {
1181 case TDA1004X_DEMOD_TDA10045:
1182 tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0x10);
1185 case TDA1004X_DEMOD_TDA10046:
1186 if (state->config->pll_sleep != NULL) {
1187 tda1004x_enable_tuner_i2c(state);
1188 state->config->pll_sleep(fe);
1189 if (state->config->if_freq != TDA10046_FREQ_052) {
1190 /* special hack for Philips EUROPA Based boards:
1191 * keep the I2c bridge open for tuner access in analog mode
1193 tda1004x_disable_tuner_i2c(state);
1196 /* set outputs to tristate */
1197 tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE1, 0xff);
1198 tda1004x_write_mask(state, TDA1004X_CONFC4, 1, 1);
1205 static int tda1004x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
1207 fesettings->min_delay_ms = 800;
1208 /* Drift compensation makes no sense for DVB-T */
1209 fesettings->step_size = 0;
1210 fesettings->max_drift = 0;
1214 static void tda1004x_release(struct dvb_frontend* fe)
1216 struct tda1004x_state *state = fe->demodulator_priv;
1220 static struct dvb_frontend_ops tda10045_ops = {
1222 .name = "Philips TDA10045H DVB-T",
1224 .frequency_min = 51000000,
1225 .frequency_max = 858000000,
1226 .frequency_stepsize = 166667,
1228 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1229 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1230 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
1231 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
1234 .release = tda1004x_release,
1236 .init = tda10045_init,
1237 .sleep = tda1004x_sleep,
1239 .set_frontend = tda1004x_set_fe,
1240 .get_frontend = tda1004x_get_fe,
1241 .get_tune_settings = tda1004x_get_tune_settings,
1243 .read_status = tda1004x_read_status,
1244 .read_ber = tda1004x_read_ber,
1245 .read_signal_strength = tda1004x_read_signal_strength,
1246 .read_snr = tda1004x_read_snr,
1247 .read_ucblocks = tda1004x_read_ucblocks,
1250 struct dvb_frontend* tda10045_attach(const struct tda1004x_config* config,
1251 struct i2c_adapter* i2c)
1253 struct tda1004x_state *state;
1255 /* allocate memory for the internal state */
1256 state = kmalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
1260 /* setup the state */
1261 state->config = config;
1263 memcpy(&state->ops, &tda10045_ops, sizeof(struct dvb_frontend_ops));
1264 state->demod_type = TDA1004X_DEMOD_TDA10045;
1266 /* check if the demod is there */
1267 if (tda1004x_read_byte(state, TDA1004X_CHIPID) != 0x25) {
1272 /* create dvb_frontend */
1273 state->frontend.ops = &state->ops;
1274 state->frontend.demodulator_priv = state;
1275 return &state->frontend;
1278 static struct dvb_frontend_ops tda10046_ops = {
1280 .name = "Philips TDA10046H DVB-T",
1282 .frequency_min = 51000000,
1283 .frequency_max = 858000000,
1284 .frequency_stepsize = 166667,
1286 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1287 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1288 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
1289 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
1292 .release = tda1004x_release,
1294 .init = tda10046_init,
1295 .sleep = tda1004x_sleep,
1297 .set_frontend = tda1004x_set_fe,
1298 .get_frontend = tda1004x_get_fe,
1299 .get_tune_settings = tda1004x_get_tune_settings,
1301 .read_status = tda1004x_read_status,
1302 .read_ber = tda1004x_read_ber,
1303 .read_signal_strength = tda1004x_read_signal_strength,
1304 .read_snr = tda1004x_read_snr,
1305 .read_ucblocks = tda1004x_read_ucblocks,
1308 struct dvb_frontend* tda10046_attach(const struct tda1004x_config* config,
1309 struct i2c_adapter* i2c)
1311 struct tda1004x_state *state;
1313 /* allocate memory for the internal state */
1314 state = kmalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
1318 /* setup the state */
1319 state->config = config;
1321 memcpy(&state->ops, &tda10046_ops, sizeof(struct dvb_frontend_ops));
1322 state->demod_type = TDA1004X_DEMOD_TDA10046;
1324 /* check if the demod is there */
1325 if (tda1004x_read_byte(state, TDA1004X_CHIPID) != 0x46) {
1330 /* create dvb_frontend */
1331 state->frontend.ops = &state->ops;
1332 state->frontend.demodulator_priv = state;
1333 return &state->frontend;
1336 module_param(debug, int, 0644);
1337 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
1339 MODULE_DESCRIPTION("Philips TDA10045H & TDA10046H DVB-T Demodulator");
1340 MODULE_AUTHOR("Andrew de Quincey & Robert Schlabbach");
1341 MODULE_LICENSE("GPL");
1343 EXPORT_SYMBOL(tda10045_attach);
1344 EXPORT_SYMBOL(tda10046_attach);
1345 EXPORT_SYMBOL(tda1004x_write_byte);