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.
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/device.h>
27 #include <linux/jiffies.h>
28 #include <linux/string.h>
29 #include <linux/slab.h>
31 #include "dvb_frontend.h"
35 #define dprintk(args...) \
37 if (debug) printk(KERN_DEBUG "tda1004x: " args); \
40 #define TDA1004X_CHIPID 0x00
41 #define TDA1004X_AUTO 0x01
42 #define TDA1004X_IN_CONF1 0x02
43 #define TDA1004X_IN_CONF2 0x03
44 #define TDA1004X_OUT_CONF1 0x04
45 #define TDA1004X_OUT_CONF2 0x05
46 #define TDA1004X_STATUS_CD 0x06
47 #define TDA1004X_CONFC4 0x07
48 #define TDA1004X_DSSPARE2 0x0C
49 #define TDA10045H_CODE_IN 0x0D
50 #define TDA10045H_FWPAGE 0x0E
51 #define TDA1004X_SCAN_CPT 0x10
52 #define TDA1004X_DSP_CMD 0x11
53 #define TDA1004X_DSP_ARG 0x12
54 #define TDA1004X_DSP_DATA1 0x13
55 #define TDA1004X_DSP_DATA2 0x14
56 #define TDA1004X_CONFADC1 0x15
57 #define TDA1004X_CONFC1 0x16
58 #define TDA10045H_S_AGC 0x1a
59 #define TDA10046H_AGC_TUN_LEVEL 0x1a
60 #define TDA1004X_SNR 0x1c
61 #define TDA1004X_CONF_TS1 0x1e
62 #define TDA1004X_CONF_TS2 0x1f
63 #define TDA1004X_CBER_RESET 0x20
64 #define TDA1004X_CBER_MSB 0x21
65 #define TDA1004X_CBER_LSB 0x22
66 #define TDA1004X_CVBER_LUT 0x23
67 #define TDA1004X_VBER_MSB 0x24
68 #define TDA1004X_VBER_MID 0x25
69 #define TDA1004X_VBER_LSB 0x26
70 #define TDA1004X_UNCOR 0x27
72 #define TDA10045H_CONFPLL_P 0x2D
73 #define TDA10045H_CONFPLL_M_MSB 0x2E
74 #define TDA10045H_CONFPLL_M_LSB 0x2F
75 #define TDA10045H_CONFPLL_N 0x30
77 #define TDA10046H_CONFPLL1 0x2D
78 #define TDA10046H_CONFPLL2 0x2F
79 #define TDA10046H_CONFPLL3 0x30
80 #define TDA10046H_TIME_WREF1 0x31
81 #define TDA10046H_TIME_WREF2 0x32
82 #define TDA10046H_TIME_WREF3 0x33
83 #define TDA10046H_TIME_WREF4 0x34
84 #define TDA10046H_TIME_WREF5 0x35
86 #define TDA10045H_UNSURW_MSB 0x31
87 #define TDA10045H_UNSURW_LSB 0x32
88 #define TDA10045H_WREF_MSB 0x33
89 #define TDA10045H_WREF_MID 0x34
90 #define TDA10045H_WREF_LSB 0x35
91 #define TDA10045H_MUXOUT 0x36
92 #define TDA1004X_CONFADC2 0x37
94 #define TDA10045H_IOFFSET 0x38
96 #define TDA10046H_CONF_TRISTATE1 0x3B
97 #define TDA10046H_CONF_TRISTATE2 0x3C
98 #define TDA10046H_CONF_POLARITY 0x3D
99 #define TDA10046H_FREQ_OFFSET 0x3E
100 #define TDA10046H_GPIO_OUT_SEL 0x41
101 #define TDA10046H_GPIO_SELECT 0x42
102 #define TDA10046H_AGC_CONF 0x43
103 #define TDA10046H_AGC_THR 0x44
104 #define TDA10046H_AGC_RENORM 0x45
105 #define TDA10046H_AGC_GAINS 0x46
106 #define TDA10046H_AGC_TUN_MIN 0x47
107 #define TDA10046H_AGC_TUN_MAX 0x48
108 #define TDA10046H_AGC_IF_MIN 0x49
109 #define TDA10046H_AGC_IF_MAX 0x4A
111 #define TDA10046H_FREQ_PHY2_MSB 0x4D
112 #define TDA10046H_FREQ_PHY2_LSB 0x4E
114 #define TDA10046H_CVBER_CTRL 0x4F
115 #define TDA10046H_AGC_IF_LEVEL 0x52
116 #define TDA10046H_CODE_CPT 0x57
117 #define TDA10046H_CODE_IN 0x58
120 static int tda1004x_write_byteI(struct tda1004x_state *state, int reg, int data)
123 u8 buf[] = { reg, data };
124 struct i2c_msg msg = { .flags = 0, .buf = buf, .len = 2 };
126 dprintk("%s: reg=0x%x, data=0x%x\n", __func__, reg, data);
128 msg.addr = state->config->demod_address;
129 ret = i2c_transfer(state->i2c, &msg, 1);
132 dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
133 __func__, reg, data, ret);
135 dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __func__,
137 return (ret != 1) ? -1 : 0;
140 static int tda1004x_read_byte(struct tda1004x_state *state, int reg)
145 struct i2c_msg msg[] = {{ .flags = 0, .buf = b0, .len = 1 },
146 { .flags = I2C_M_RD, .buf = b1, .len = 1 }};
148 dprintk("%s: reg=0x%x\n", __func__, reg);
150 msg[0].addr = state->config->demod_address;
151 msg[1].addr = state->config->demod_address;
152 ret = i2c_transfer(state->i2c, msg, 2);
155 dprintk("%s: error reg=0x%x, ret=%i\n", __func__, reg,
160 dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __func__,
165 static int tda1004x_write_mask(struct tda1004x_state *state, int reg, int mask, int data)
168 dprintk("%s: reg=0x%x, mask=0x%x, data=0x%x\n", __func__, reg,
171 // read a byte and check
172 val = tda1004x_read_byte(state, reg);
180 // write it out again
181 return tda1004x_write_byteI(state, reg, val);
184 static int tda1004x_write_buf(struct tda1004x_state *state, int reg, unsigned char *buf, int len)
189 dprintk("%s: reg=0x%x, len=0x%x\n", __func__, reg, len);
192 for (i = 0; i < len; i++) {
193 result = tda1004x_write_byteI(state, reg + i, buf[i]);
201 static int tda1004x_enable_tuner_i2c(struct tda1004x_state *state)
204 dprintk("%s\n", __func__);
206 result = tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 2);
211 static int tda1004x_disable_tuner_i2c(struct tda1004x_state *state)
213 dprintk("%s\n", __func__);
215 return tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 0);
218 static int tda10045h_set_bandwidth(struct tda1004x_state *state,
221 static u8 bandwidth_6mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x60, 0x1e, 0xa7, 0x45, 0x4f };
222 static u8 bandwidth_7mhz[] = { 0x02, 0x00, 0x37, 0x00, 0x4a, 0x2f, 0x6d, 0x76, 0xdb };
223 static u8 bandwidth_8mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x48, 0x17, 0x89, 0xc7, 0x14 };
227 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_6mhz, sizeof(bandwidth_6mhz));
231 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_7mhz, sizeof(bandwidth_7mhz));
235 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_8mhz, sizeof(bandwidth_8mhz));
242 tda1004x_write_byteI(state, TDA10045H_IOFFSET, 0);
247 static int tda10046h_set_bandwidth(struct tda1004x_state *state,
250 static u8 bandwidth_6mhz_53M[] = { 0x7b, 0x2e, 0x11, 0xf0, 0xd2 };
251 static u8 bandwidth_7mhz_53M[] = { 0x6a, 0x02, 0x6a, 0x43, 0x9f };
252 static u8 bandwidth_8mhz_53M[] = { 0x5c, 0x32, 0xc2, 0x96, 0x6d };
254 static u8 bandwidth_6mhz_48M[] = { 0x70, 0x02, 0x49, 0x24, 0x92 };
255 static u8 bandwidth_7mhz_48M[] = { 0x60, 0x02, 0xaa, 0xaa, 0xab };
256 static u8 bandwidth_8mhz_48M[] = { 0x54, 0x03, 0x0c, 0x30, 0xc3 };
259 if ((state->config->if_freq == TDA10046_FREQ_045) ||
260 (state->config->if_freq == TDA10046_FREQ_052))
267 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz_53M,
268 sizeof(bandwidth_6mhz_53M));
270 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz_48M,
271 sizeof(bandwidth_6mhz_48M));
272 if (state->config->if_freq == TDA10046_FREQ_045) {
273 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0a);
274 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xab);
280 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz_53M,
281 sizeof(bandwidth_7mhz_53M));
283 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz_48M,
284 sizeof(bandwidth_7mhz_48M));
285 if (state->config->if_freq == TDA10046_FREQ_045) {
286 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
287 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x00);
293 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz_53M,
294 sizeof(bandwidth_8mhz_53M));
296 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz_48M,
297 sizeof(bandwidth_8mhz_48M));
298 if (state->config->if_freq == TDA10046_FREQ_045) {
299 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0d);
300 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x55);
311 static int tda1004x_do_upload(struct tda1004x_state *state,
312 const unsigned char *mem, unsigned int len,
313 u8 dspCodeCounterReg, u8 dspCodeInReg)
316 struct i2c_msg fw_msg = { .flags = 0, .buf = buf, .len = 0 };
320 /* clear code counter */
321 tda1004x_write_byteI(state, dspCodeCounterReg, 0);
322 fw_msg.addr = state->config->demod_address;
324 i2c_lock_adapter(state->i2c);
325 buf[0] = dspCodeInReg;
327 // work out how much to send this time
333 memcpy(buf + 1, mem + pos, tx_size);
334 fw_msg.len = tx_size + 1;
335 if (__i2c_transfer(state->i2c, &fw_msg, 1) != 1) {
336 printk(KERN_ERR "tda1004x: Error during firmware upload\n");
337 i2c_unlock_adapter(state->i2c);
342 dprintk("%s: fw_pos=0x%x\n", __func__, pos);
344 i2c_unlock_adapter(state->i2c);
346 /* give the DSP a chance to settle 03/10/05 Hac */
352 static int tda1004x_check_upload_ok(struct tda1004x_state *state)
355 unsigned long timeout;
357 if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
358 timeout = jiffies + 2 * HZ;
359 while(!(tda1004x_read_byte(state, TDA1004X_STATUS_CD) & 0x20)) {
360 if (time_after(jiffies, timeout)) {
361 printk(KERN_ERR "tda1004x: timeout waiting for DSP ready\n");
369 // check upload was OK
370 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0); // we want to read from the DSP
371 tda1004x_write_byteI(state, TDA1004X_DSP_CMD, 0x67);
373 data1 = tda1004x_read_byte(state, TDA1004X_DSP_DATA1);
374 data2 = tda1004x_read_byte(state, TDA1004X_DSP_DATA2);
375 if (data1 != 0x67 || data2 < 0x20 || data2 > 0x2e) {
376 printk(KERN_INFO "tda1004x: found firmware revision %x -- invalid\n", data2);
379 printk(KERN_INFO "tda1004x: found firmware revision %x -- ok\n", data2);
383 static int tda10045_fwupload(struct dvb_frontend* fe)
385 struct tda1004x_state* state = fe->demodulator_priv;
387 const struct firmware *fw;
389 /* don't re-upload unless necessary */
390 if (tda1004x_check_upload_ok(state) == 0)
393 /* request the firmware, this will block until someone uploads it */
394 printk(KERN_INFO "tda1004x: waiting for firmware upload (%s)...\n", "/*(DEBLOBBED)*/");
395 ret = state->config->reject_firmware(fe, &fw, "/*(DEBLOBBED)*/");
397 printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
402 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0);
403 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
404 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
408 tda10045h_set_bandwidth(state, 8000000);
410 ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10045H_FWPAGE, TDA10045H_CODE_IN);
411 release_firmware(fw);
414 printk(KERN_INFO "tda1004x: firmware upload complete\n");
416 /* wait for DSP to initialise */
417 /* DSPREADY doesn't seem to work on the TDA10045H */
420 return tda1004x_check_upload_ok(state);
423 static void tda10046_init_plls(struct dvb_frontend* fe)
425 struct tda1004x_state* state = fe->demodulator_priv;
428 if ((state->config->if_freq == TDA10046_FREQ_045) ||
429 (state->config->if_freq == TDA10046_FREQ_052))
434 tda1004x_write_byteI(state, TDA10046H_CONFPLL1, 0xf0);
435 if(tda10046_clk53m) {
436 printk(KERN_INFO "tda1004x: setting up plls for 53MHz sampling clock\n");
437 tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 0x08); // PLL M = 8
439 printk(KERN_INFO "tda1004x: setting up plls for 48MHz sampling clock\n");
440 tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 0x03); // PLL M = 3
442 if (state->config->xtal_freq == TDA10046_XTAL_4M ) {
443 dprintk("%s: setting up PLLs for a 4 MHz Xtal\n", __func__);
444 tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 0); // PLL P = N = 0
446 dprintk("%s: setting up PLLs for a 16 MHz Xtal\n", __func__);
447 tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 3); // PLL P = 0, N = 3
450 tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 0x67);
452 tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 0x72);
453 /* Note clock frequency is handled implicitly */
454 switch (state->config->if_freq) {
455 case TDA10046_FREQ_045:
456 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
457 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x00);
459 case TDA10046_FREQ_052:
460 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0d);
461 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xc7);
463 case TDA10046_FREQ_3617:
464 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd7);
465 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x59);
467 case TDA10046_FREQ_3613:
468 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd7);
469 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x3f);
472 tda10046h_set_bandwidth(state, 8000000); /* default bandwidth 8 MHz */
473 /* let the PLLs settle */
477 static int tda10046_fwupload(struct dvb_frontend* fe)
479 struct tda1004x_state* state = fe->demodulator_priv;
481 const struct firmware *fw;
483 /* reset + wake up chip */
484 if (state->config->xtal_freq == TDA10046_XTAL_4M) {
487 dprintk("%s: 16MHz Xtal, reducing I2C speed\n", __func__);
490 tda1004x_write_byteI(state, TDA1004X_CONFC4, confc4);
492 tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 1, 0);
493 /* set GPIO 1 and 3 */
494 if (state->config->gpio_config != TDA10046_GPTRI) {
495 tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE2, 0x33);
496 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x0f, state->config->gpio_config &0x0f);
498 /* let the clocks recover from sleep */
501 /* The PLLs need to be reprogrammed after sleep */
502 tda10046_init_plls(fe);
503 tda1004x_write_mask(state, TDA1004X_CONFADC2, 0xc0, 0);
505 /* don't re-upload unless necessary */
506 if (tda1004x_check_upload_ok(state) == 0)
510 For i2c normal work, we need to slow down the bus speed.
511 However, the slow down breaks the eeprom firmware load.
512 So, use normal speed for eeprom booting and then restore the
513 i2c speed after that. Tested with MSI TV @nyware A/D board,
514 that comes with firmware version 29 inside their eeprom.
516 It should also be noticed that no other I2C transfer should
517 be in course while booting from eeprom, otherwise, tda10046
518 goes into an instable state. So, proper locking are needed
519 at the i2c bus master.
521 printk(KERN_INFO "tda1004x: trying to boot from eeprom\n");
522 tda1004x_write_byteI(state, TDA1004X_CONFC4, 4);
524 tda1004x_write_byteI(state, TDA1004X_CONFC4, confc4);
526 /* Checks if eeprom firmware went without troubles */
527 if (tda1004x_check_upload_ok(state) == 0)
530 /* eeprom firmware didn't work. Load one manually. */
532 if (state->config->reject_firmware != NULL) {
533 /* request the firmware, this will block until someone uploads it */
534 printk(KERN_INFO "tda1004x: waiting for firmware upload...\n");
535 ret = state->config->reject_firmware(fe, &fw, "/*(DEBLOBBED)*/");
537 /* remain compatible to old bug: try to load with tda10045 image name */
538 ret = state->config->reject_firmware(fe, &fw, "/*(DEBLOBBED)*/");
540 printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
543 printk(KERN_INFO "tda1004x: please rename the firmware file to %s\n",
548 printk(KERN_ERR "tda1004x: no request function defined, can't upload from file\n");
551 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8); // going to boot from HOST
552 ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10046H_CODE_CPT, TDA10046H_CODE_IN);
553 release_firmware(fw);
554 return tda1004x_check_upload_ok(state);
557 static int tda1004x_encode_fec(int fec)
559 // convert known FEC values
577 static int tda1004x_decode_fec(int tdafec)
579 // convert known FEC values
597 static int tda1004x_write(struct dvb_frontend* fe, const u8 buf[], int len)
599 struct tda1004x_state* state = fe->demodulator_priv;
604 return tda1004x_write_byteI(state, buf[0], buf[1]);
607 static int tda10045_init(struct dvb_frontend* fe)
609 struct tda1004x_state* state = fe->demodulator_priv;
611 dprintk("%s\n", __func__);
613 if (tda10045_fwupload(fe)) {
614 printk("tda1004x: firmware upload failed\n");
618 tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0); // wake up the ADC
621 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
622 tda1004x_write_mask(state, TDA1004X_AUTO, 8, 0); // select HP stream
623 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x40, 0); // set polarity of VAGC signal
624 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x80, 0x80); // enable pulse killer
625 tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10); // enable auto offset
626 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0x0); // no frequency offset
627 tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 0); // setup MPEG2 TS interface
628 tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0); // setup MPEG2 TS interface
629 tda1004x_write_mask(state, TDA1004X_VBER_MSB, 0xe0, 0xa0); // 10^6 VBER measurement bits
630 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x10, 0); // VAGC polarity
631 tda1004x_write_byteI(state, TDA1004X_CONFADC1, 0x2e);
633 tda1004x_write_mask(state, 0x1f, 0x01, state->config->invert_oclk);
638 static int tda10046_init(struct dvb_frontend* fe)
640 struct tda1004x_state* state = fe->demodulator_priv;
641 dprintk("%s\n", __func__);
643 if (tda10046_fwupload(fe)) {
644 printk("tda1004x: firmware upload failed\n");
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_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 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_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 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_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 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_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60); // set AGC polarities
673 if (state->config->ts_mode == 0) {
674 tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 0xc0, 0x40);
675 tda1004x_write_mask(state, 0x3a, 0x80, state->config->invert_oclk << 7);
677 tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 0xc0, 0x80);
678 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x10,
679 state->config->invert_oclk << 4);
681 tda1004x_write_byteI(state, TDA1004X_CONFADC2, 0x38);
682 tda1004x_write_mask (state, TDA10046H_CONF_TRISTATE1, 0x3e, 0x38); // Turn IF AGC output on
683 tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MIN, 0); // }
684 tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MAX, 0xff); // } AGC min/max values
685 tda1004x_write_byteI(state, TDA10046H_AGC_IF_MIN, 0); // }
686 tda1004x_write_byteI(state, TDA10046H_AGC_IF_MAX, 0xff); // }
687 tda1004x_write_byteI(state, TDA10046H_AGC_GAINS, 0x12); // IF gain 2, TUN gain 1
688 tda1004x_write_byteI(state, TDA10046H_CVBER_CTRL, 0x1a); // 10^6 VBER measurement bits
689 tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 7); // MPEG2 interface config
690 tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0xc0); // MPEG2 interface config
691 // tda1004x_write_mask(state, 0x50, 0x80, 0x80); // handle out of guard echoes
696 static int tda1004x_set_fe(struct dvb_frontend *fe)
698 struct dtv_frontend_properties *fe_params = &fe->dtv_property_cache;
699 struct tda1004x_state* state = fe->demodulator_priv;
703 dprintk("%s\n", __func__);
705 if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
707 tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10);
708 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x80, 0);
709 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0);
711 // disable agc_conf[2]
712 tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 0);
716 if (fe->ops.tuner_ops.set_params) {
717 fe->ops.tuner_ops.set_params(fe);
718 if (fe->ops.i2c_gate_ctrl)
719 fe->ops.i2c_gate_ctrl(fe, 0);
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->code_rate_HP = FEC_AUTO;
726 fe_params->guard_interval = GUARD_INTERVAL_AUTO;
727 fe_params->transmission_mode = TRANSMISSION_MODE_AUTO;
730 // Set standard params.. or put them to auto
731 if ((fe_params->code_rate_HP == FEC_AUTO) ||
732 (fe_params->code_rate_LP == FEC_AUTO) ||
733 (fe_params->modulation == QAM_AUTO) ||
734 (fe_params->hierarchy == 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 modulation 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->code_rate_HP);
746 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 7, tmp);
749 tmp = tda1004x_encode_fec(fe_params->code_rate_LP);
752 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x38, tmp << 3);
755 switch (fe_params->modulation) {
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->hierarchy) {
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->bandwidth_hz);
801 case TDA1004X_DEMOD_TDA10046:
802 tda10046h_set_bandwidth(state, fe_params->bandwidth_hz);
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->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->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)
896 struct dtv_frontend_properties *fe_params = &fe->dtv_property_cache;
897 struct tda1004x_state* state = fe->demodulator_priv;
899 dprintk("%s\n", __func__);
902 fe_params->inversion = INVERSION_OFF;
903 if (tda1004x_read_byte(state, TDA1004X_CONFC1) & 0x20)
904 fe_params->inversion = INVERSION_ON;
905 if (state->config->invert)
906 fe_params->inversion = fe_params->inversion ? INVERSION_OFF : INVERSION_ON;
909 switch (state->demod_type) {
910 case TDA1004X_DEMOD_TDA10045:
911 switch (tda1004x_read_byte(state, TDA10045H_WREF_LSB)) {
913 fe_params->bandwidth_hz = 8000000;
916 fe_params->bandwidth_hz = 7000000;
919 fe_params->bandwidth_hz = 6000000;
923 case TDA1004X_DEMOD_TDA10046:
924 switch (tda1004x_read_byte(state, TDA10046H_TIME_WREF1)) {
927 fe_params->bandwidth_hz = 8000000;
931 fe_params->bandwidth_hz = 7000000;
935 fe_params->bandwidth_hz = 6000000;
942 fe_params->code_rate_HP =
943 tda1004x_decode_fec(tda1004x_read_byte(state, TDA1004X_OUT_CONF2) & 7);
944 fe_params->code_rate_LP =
945 tda1004x_decode_fec((tda1004x_read_byte(state, TDA1004X_OUT_CONF2) >> 3) & 7);
948 switch (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 3) {
950 fe_params->modulation = QPSK;
953 fe_params->modulation = QAM_16;
956 fe_params->modulation = QAM_64;
961 fe_params->transmission_mode = TRANSMISSION_MODE_2K;
962 if (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x10)
963 fe_params->transmission_mode = TRANSMISSION_MODE_8K;
966 switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x0c) >> 2) {
968 fe_params->guard_interval = GUARD_INTERVAL_1_32;
971 fe_params->guard_interval = GUARD_INTERVAL_1_16;
974 fe_params->guard_interval = GUARD_INTERVAL_1_8;
977 fe_params->guard_interval = GUARD_INTERVAL_1_4;
982 switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x60) >> 5) {
984 fe_params->hierarchy = HIERARCHY_NONE;
987 fe_params->hierarchy = HIERARCHY_1;
990 fe_params->hierarchy = HIERARCHY_2;
993 fe_params->hierarchy = HIERARCHY_4;
1000 static int tda1004x_read_status(struct dvb_frontend* fe, fe_status_t * fe_status)
1002 struct tda1004x_state* state = fe->demodulator_priv;
1007 dprintk("%s\n", __func__);
1010 status = tda1004x_read_byte(state, TDA1004X_STATUS_CD);
1017 *fe_status |= FE_HAS_SIGNAL;
1019 *fe_status |= FE_HAS_CARRIER;
1021 *fe_status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
1023 // if we don't already have VITERBI (i.e. not LOCKED), see if the viterbi
1024 // is getting anything valid
1025 if (!(*fe_status & FE_HAS_VITERBI)) {
1027 cber = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
1030 status = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
1033 cber |= (status << 8);
1034 // The address 0x20 should be read to cope with a TDA10046 bug
1035 tda1004x_read_byte(state, TDA1004X_CBER_RESET);
1038 *fe_status |= FE_HAS_VITERBI;
1041 // if we DO have some valid VITERBI output, but don't already have SYNC
1042 // bytes (i.e. not LOCKED), see if the RS decoder is getting anything valid.
1043 if ((*fe_status & FE_HAS_VITERBI) && (!(*fe_status & FE_HAS_SYNC))) {
1045 vber = tda1004x_read_byte(state, TDA1004X_VBER_LSB);
1048 status = tda1004x_read_byte(state, TDA1004X_VBER_MID);
1051 vber |= (status << 8);
1052 status = tda1004x_read_byte(state, TDA1004X_VBER_MSB);
1055 vber |= (status & 0x0f) << 16;
1056 // The CVBER_LUT should be read to cope with TDA10046 hardware bug
1057 tda1004x_read_byte(state, TDA1004X_CVBER_LUT);
1059 // if RS has passed some valid TS packets, then we must be
1060 // getting some SYNC bytes
1062 *fe_status |= FE_HAS_SYNC;
1066 dprintk("%s: fe_status=0x%x\n", __func__, *fe_status);
1070 static int tda1004x_read_signal_strength(struct dvb_frontend* fe, u16 * signal)
1072 struct tda1004x_state* state = fe->demodulator_priv;
1076 dprintk("%s\n", __func__);
1078 // determine the register to use
1079 switch (state->demod_type) {
1080 case TDA1004X_DEMOD_TDA10045:
1081 reg = TDA10045H_S_AGC;
1084 case TDA1004X_DEMOD_TDA10046:
1085 reg = TDA10046H_AGC_IF_LEVEL;
1090 tmp = tda1004x_read_byte(state, reg);
1094 *signal = (tmp << 8) | tmp;
1095 dprintk("%s: signal=0x%x\n", __func__, *signal);
1099 static int tda1004x_read_snr(struct dvb_frontend* fe, u16 * snr)
1101 struct tda1004x_state* state = fe->demodulator_priv;
1104 dprintk("%s\n", __func__);
1107 tmp = tda1004x_read_byte(state, TDA1004X_SNR);
1112 *snr = ((tmp << 8) | tmp);
1113 dprintk("%s: snr=0x%x\n", __func__, *snr);
1117 static int tda1004x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
1119 struct tda1004x_state* state = fe->demodulator_priv;
1124 dprintk("%s\n", __func__);
1126 // read the UCBLOCKS and reset
1128 tmp = tda1004x_read_byte(state, TDA1004X_UNCOR);
1132 while (counter++ < 5) {
1133 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1134 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1135 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1137 tmp2 = tda1004x_read_byte(state, TDA1004X_UNCOR);
1141 if ((tmp2 < tmp) || (tmp2 == 0))
1148 *ucblocks = 0xffffffff;
1150 dprintk("%s: ucblocks=0x%x\n", __func__, *ucblocks);
1154 static int tda1004x_read_ber(struct dvb_frontend* fe, u32* ber)
1156 struct tda1004x_state* state = fe->demodulator_priv;
1159 dprintk("%s\n", __func__);
1162 tmp = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
1166 tmp = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
1170 // The address 0x20 should be read to cope with a TDA10046 bug
1171 tda1004x_read_byte(state, TDA1004X_CBER_RESET);
1173 dprintk("%s: ber=0x%x\n", __func__, *ber);
1177 static int tda1004x_sleep(struct dvb_frontend* fe)
1179 struct tda1004x_state* state = fe->demodulator_priv;
1182 switch (state->demod_type) {
1183 case TDA1004X_DEMOD_TDA10045:
1184 tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0x10);
1187 case TDA1004X_DEMOD_TDA10046:
1188 /* set outputs to tristate */
1189 tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE1, 0xff);
1190 /* invert GPIO 1 and 3 if desired*/
1191 gpio_conf = state->config->gpio_config;
1192 if (gpio_conf >= TDA10046_GP00_I)
1193 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x0f,
1194 (gpio_conf & 0x0f) ^ 0x0a);
1196 tda1004x_write_mask(state, TDA1004X_CONFADC2, 0xc0, 0xc0);
1197 tda1004x_write_mask(state, TDA1004X_CONFC4, 1, 1);
1204 static int tda1004x_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
1206 struct tda1004x_state* state = fe->demodulator_priv;
1209 return tda1004x_enable_tuner_i2c(state);
1211 return tda1004x_disable_tuner_i2c(state);
1215 static int tda1004x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
1217 fesettings->min_delay_ms = 800;
1218 /* Drift compensation makes no sense for DVB-T */
1219 fesettings->step_size = 0;
1220 fesettings->max_drift = 0;
1224 static void tda1004x_release(struct dvb_frontend* fe)
1226 struct tda1004x_state *state = fe->demodulator_priv;
1230 static struct dvb_frontend_ops tda10045_ops = {
1231 .delsys = { SYS_DVBT },
1233 .name = "Philips TDA10045H DVB-T",
1234 .frequency_min = 51000000,
1235 .frequency_max = 858000000,
1236 .frequency_stepsize = 166667,
1238 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1239 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1240 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
1241 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
1244 .release = tda1004x_release,
1246 .init = tda10045_init,
1247 .sleep = tda1004x_sleep,
1248 .write = tda1004x_write,
1249 .i2c_gate_ctrl = tda1004x_i2c_gate_ctrl,
1251 .set_frontend = tda1004x_set_fe,
1252 .get_frontend = tda1004x_get_fe,
1253 .get_tune_settings = tda1004x_get_tune_settings,
1255 .read_status = tda1004x_read_status,
1256 .read_ber = tda1004x_read_ber,
1257 .read_signal_strength = tda1004x_read_signal_strength,
1258 .read_snr = tda1004x_read_snr,
1259 .read_ucblocks = tda1004x_read_ucblocks,
1262 struct dvb_frontend* tda10045_attach(const struct tda1004x_config* config,
1263 struct i2c_adapter* i2c)
1265 struct tda1004x_state *state;
1268 /* allocate memory for the internal state */
1269 state = kzalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
1271 printk(KERN_ERR "Can't allocate memory for tda10045 state\n");
1275 /* setup the state */
1276 state->config = config;
1278 state->demod_type = TDA1004X_DEMOD_TDA10045;
1280 /* check if the demod is there */
1281 id = tda1004x_read_byte(state, TDA1004X_CHIPID);
1283 printk(KERN_ERR "tda10045: chip is not answering. Giving up.\n");
1289 printk(KERN_ERR "Invalid tda1004x ID = 0x%02x. Can't proceed\n", id);
1294 /* create dvb_frontend */
1295 memcpy(&state->frontend.ops, &tda10045_ops, sizeof(struct dvb_frontend_ops));
1296 state->frontend.demodulator_priv = state;
1297 return &state->frontend;
1300 static struct dvb_frontend_ops tda10046_ops = {
1301 .delsys = { SYS_DVBT },
1303 .name = "Philips TDA10046H DVB-T",
1304 .frequency_min = 51000000,
1305 .frequency_max = 858000000,
1306 .frequency_stepsize = 166667,
1308 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1309 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1310 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
1311 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
1314 .release = tda1004x_release,
1316 .init = tda10046_init,
1317 .sleep = tda1004x_sleep,
1318 .write = tda1004x_write,
1319 .i2c_gate_ctrl = tda1004x_i2c_gate_ctrl,
1321 .set_frontend = tda1004x_set_fe,
1322 .get_frontend = tda1004x_get_fe,
1323 .get_tune_settings = tda1004x_get_tune_settings,
1325 .read_status = tda1004x_read_status,
1326 .read_ber = tda1004x_read_ber,
1327 .read_signal_strength = tda1004x_read_signal_strength,
1328 .read_snr = tda1004x_read_snr,
1329 .read_ucblocks = tda1004x_read_ucblocks,
1332 struct dvb_frontend* tda10046_attach(const struct tda1004x_config* config,
1333 struct i2c_adapter* i2c)
1335 struct tda1004x_state *state;
1338 /* allocate memory for the internal state */
1339 state = kzalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
1341 printk(KERN_ERR "Can't allocate memory for tda10046 state\n");
1345 /* setup the state */
1346 state->config = config;
1348 state->demod_type = TDA1004X_DEMOD_TDA10046;
1350 /* check if the demod is there */
1351 id = tda1004x_read_byte(state, TDA1004X_CHIPID);
1353 printk(KERN_ERR "tda10046: chip is not answering. Giving up.\n");
1358 printk(KERN_ERR "Invalid tda1004x ID = 0x%02x. Can't proceed\n", id);
1363 /* create dvb_frontend */
1364 memcpy(&state->frontend.ops, &tda10046_ops, sizeof(struct dvb_frontend_ops));
1365 state->frontend.demodulator_priv = state;
1366 return &state->frontend;
1369 module_param(debug, int, 0644);
1370 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
1372 MODULE_DESCRIPTION("Philips TDA10045H & TDA10046H DVB-T Demodulator");
1373 MODULE_AUTHOR("Andrew de Quincey & Robert Schlabbach");
1374 MODULE_LICENSE("GPL");
1376 EXPORT_SYMBOL(tda10045_attach);
1377 EXPORT_SYMBOL(tda10046_attach);