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.
28 #define TDA10045_DEFAULT_FIRMWARE "dvb-fe-tda10045.fw"
29 #define TDA10046_DEFAULT_FIRMWARE "dvb-fe-tda10046.fw"
31 #include <linux/init.h>
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/device.h>
35 #include "dvb_frontend.h"
39 TDA1004X_DEMOD_TDA10045,
40 TDA1004X_DEMOD_TDA10046,
43 struct tda1004x_state {
44 struct i2c_adapter* i2c;
45 struct dvb_frontend_ops ops;
46 const struct tda1004x_config* config;
47 struct dvb_frontend frontend;
49 /* private demod data */
51 enum tda1004x_demod demod_type;
55 #define dprintk(args...) \
57 if (debug) printk(KERN_DEBUG "tda1004x: " args); \
60 #define TDA1004X_CHIPID 0x00
61 #define TDA1004X_AUTO 0x01
62 #define TDA1004X_IN_CONF1 0x02
63 #define TDA1004X_IN_CONF2 0x03
64 #define TDA1004X_OUT_CONF1 0x04
65 #define TDA1004X_OUT_CONF2 0x05
66 #define TDA1004X_STATUS_CD 0x06
67 #define TDA1004X_CONFC4 0x07
68 #define TDA1004X_DSSPARE2 0x0C
69 #define TDA10045H_CODE_IN 0x0D
70 #define TDA10045H_FWPAGE 0x0E
71 #define TDA1004X_SCAN_CPT 0x10
72 #define TDA1004X_DSP_CMD 0x11
73 #define TDA1004X_DSP_ARG 0x12
74 #define TDA1004X_DSP_DATA1 0x13
75 #define TDA1004X_DSP_DATA2 0x14
76 #define TDA1004X_CONFADC1 0x15
77 #define TDA1004X_CONFC1 0x16
78 #define TDA10045H_S_AGC 0x1a
79 #define TDA10046H_AGC_TUN_LEVEL 0x1a
80 #define TDA1004X_SNR 0x1c
81 #define TDA1004X_CONF_TS1 0x1e
82 #define TDA1004X_CONF_TS2 0x1f
83 #define TDA1004X_CBER_RESET 0x20
84 #define TDA1004X_CBER_MSB 0x21
85 #define TDA1004X_CBER_LSB 0x22
86 #define TDA1004X_CVBER_LUT 0x23
87 #define TDA1004X_VBER_MSB 0x24
88 #define TDA1004X_VBER_MID 0x25
89 #define TDA1004X_VBER_LSB 0x26
90 #define TDA1004X_UNCOR 0x27
92 #define TDA10045H_CONFPLL_P 0x2D
93 #define TDA10045H_CONFPLL_M_MSB 0x2E
94 #define TDA10045H_CONFPLL_M_LSB 0x2F
95 #define TDA10045H_CONFPLL_N 0x30
97 #define TDA10046H_CONFPLL1 0x2D
98 #define TDA10046H_CONFPLL2 0x2F
99 #define TDA10046H_CONFPLL3 0x30
100 #define TDA10046H_TIME_WREF1 0x31
101 #define TDA10046H_TIME_WREF2 0x32
102 #define TDA10046H_TIME_WREF3 0x33
103 #define TDA10046H_TIME_WREF4 0x34
104 #define TDA10046H_TIME_WREF5 0x35
106 #define TDA10045H_UNSURW_MSB 0x31
107 #define TDA10045H_UNSURW_LSB 0x32
108 #define TDA10045H_WREF_MSB 0x33
109 #define TDA10045H_WREF_MID 0x34
110 #define TDA10045H_WREF_LSB 0x35
111 #define TDA10045H_MUXOUT 0x36
112 #define TDA1004X_CONFADC2 0x37
114 #define TDA10045H_IOFFSET 0x38
116 #define TDA10046H_CONF_TRISTATE1 0x3B
117 #define TDA10046H_CONF_TRISTATE2 0x3C
118 #define TDA10046H_CONF_POLARITY 0x3D
119 #define TDA10046H_FREQ_OFFSET 0x3E
120 #define TDA10046H_GPIO_OUT_SEL 0x41
121 #define TDA10046H_GPIO_SELECT 0x42
122 #define TDA10046H_AGC_CONF 0x43
123 #define TDA10046H_AGC_THR 0x44
124 #define TDA10046H_AGC_RENORM 0x45
125 #define TDA10046H_AGC_GAINS 0x46
126 #define TDA10046H_AGC_TUN_MIN 0x47
127 #define TDA10046H_AGC_TUN_MAX 0x48
128 #define TDA10046H_AGC_IF_MIN 0x49
129 #define TDA10046H_AGC_IF_MAX 0x4A
131 #define TDA10046H_FREQ_PHY2_MSB 0x4D
132 #define TDA10046H_FREQ_PHY2_LSB 0x4E
134 #define TDA10046H_CVBER_CTRL 0x4F
135 #define TDA10046H_AGC_IF_LEVEL 0x52
136 #define TDA10046H_CODE_CPT 0x57
137 #define TDA10046H_CODE_IN 0x58
140 static int tda1004x_write_byteI(struct tda1004x_state *state, int reg, int data)
143 u8 buf[] = { reg, data };
144 struct i2c_msg msg = { .flags = 0, .buf = buf, .len = 2 };
146 dprintk("%s: reg=0x%x, data=0x%x\n", __FUNCTION__, reg, data);
148 msg.addr = state->config->demod_address;
149 ret = i2c_transfer(state->i2c, &msg, 1);
152 dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
153 __FUNCTION__, reg, data, ret);
155 dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __FUNCTION__,
157 return (ret != 1) ? -1 : 0;
160 static int tda1004x_read_byte(struct tda1004x_state *state, int reg)
165 struct i2c_msg msg[] = {{ .flags = 0, .buf = b0, .len = 1 },
166 { .flags = I2C_M_RD, .buf = b1, .len = 1 }};
168 dprintk("%s: reg=0x%x\n", __FUNCTION__, reg);
170 msg[0].addr = state->config->demod_address;
171 msg[1].addr = state->config->demod_address;
172 ret = i2c_transfer(state->i2c, msg, 2);
175 dprintk("%s: error reg=0x%x, ret=%i\n", __FUNCTION__, reg,
180 dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __FUNCTION__,
185 static int tda1004x_write_mask(struct tda1004x_state *state, int reg, int mask, int data)
188 dprintk("%s: reg=0x%x, mask=0x%x, data=0x%x\n", __FUNCTION__, reg,
191 // read a byte and check
192 val = tda1004x_read_byte(state, reg);
200 // write it out again
201 return tda1004x_write_byteI(state, reg, val);
204 static int tda1004x_write_buf(struct tda1004x_state *state, int reg, unsigned char *buf, int len)
209 dprintk("%s: reg=0x%x, len=0x%x\n", __FUNCTION__, reg, len);
212 for (i = 0; i < len; i++) {
213 result = tda1004x_write_byteI(state, reg + i, buf[i]);
221 static int tda1004x_enable_tuner_i2c(struct tda1004x_state *state)
224 dprintk("%s\n", __FUNCTION__);
226 result = tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 2);
231 static int tda1004x_disable_tuner_i2c(struct tda1004x_state *state)
233 dprintk("%s\n", __FUNCTION__);
235 return tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 0);
238 static int tda10045h_set_bandwidth(struct tda1004x_state *state,
239 fe_bandwidth_t bandwidth)
241 static u8 bandwidth_6mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x60, 0x1e, 0xa7, 0x45, 0x4f };
242 static u8 bandwidth_7mhz[] = { 0x02, 0x00, 0x37, 0x00, 0x4a, 0x2f, 0x6d, 0x76, 0xdb };
243 static u8 bandwidth_8mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x48, 0x17, 0x89, 0xc7, 0x14 };
246 case BANDWIDTH_6_MHZ:
247 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_6mhz, sizeof(bandwidth_6mhz));
250 case BANDWIDTH_7_MHZ:
251 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_7mhz, sizeof(bandwidth_7mhz));
254 case BANDWIDTH_8_MHZ:
255 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_8mhz, sizeof(bandwidth_8mhz));
262 tda1004x_write_byteI(state, TDA10045H_IOFFSET, 0);
267 static int tda10046h_set_bandwidth(struct tda1004x_state *state,
268 fe_bandwidth_t bandwidth)
270 static u8 bandwidth_6mhz[] = { 0x80, 0x15, 0xfe, 0xab, 0x8e };
271 static u8 bandwidth_7mhz[] = { 0x6e, 0x02, 0x53, 0xc8, 0x25 };
272 static u8 bandwidth_8mhz[] = { 0x60, 0x12, 0xa8, 0xe4, 0xbd };
275 case BANDWIDTH_6_MHZ:
276 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz, sizeof(bandwidth_6mhz));
277 if (state->config->if_freq == TDA10046_FREQ_045) {
278 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x09);
279 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x4f);
283 case BANDWIDTH_7_MHZ:
284 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz, sizeof(bandwidth_7mhz));
285 if (state->config->if_freq == TDA10046_FREQ_045) {
286 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0a);
287 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x79);
291 case BANDWIDTH_8_MHZ:
292 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz, sizeof(bandwidth_8mhz));
293 if (state->config->if_freq == TDA10046_FREQ_045) {
294 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0b);
295 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xa3);
306 static int tda1004x_do_upload(struct tda1004x_state *state,
307 unsigned char *mem, unsigned int len,
308 u8 dspCodeCounterReg, u8 dspCodeInReg)
311 struct i2c_msg fw_msg = { .flags = 0, .buf = buf, .len = 0 };
315 /* clear code counter */
316 tda1004x_write_byteI(state, dspCodeCounterReg, 0);
317 fw_msg.addr = state->config->demod_address;
319 buf[0] = dspCodeInReg;
321 // work out how much to send this time
327 memcpy(buf + 1, mem + pos, tx_size);
328 fw_msg.len = tx_size + 1;
329 if (i2c_transfer(state->i2c, &fw_msg, 1) != 1) {
330 printk(KERN_ERR "tda1004x: Error during firmware upload\n");
335 dprintk("%s: fw_pos=0x%x\n", __FUNCTION__, pos);
337 // give the DSP a chance to settle 03/10/05 Hac
343 static int tda1004x_check_upload_ok(struct tda1004x_state *state)
346 unsigned long timeout;
348 if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
349 timeout = jiffies + 2 * HZ;
350 while(!(tda1004x_read_byte(state, TDA1004X_STATUS_CD) & 0x20)) {
351 if (time_after(jiffies, timeout)) {
352 printk(KERN_ERR "tda1004x: timeout waiting for DSP ready\n");
360 // check upload was OK
361 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0); // we want to read from the DSP
362 tda1004x_write_byteI(state, TDA1004X_DSP_CMD, 0x67);
364 data1 = tda1004x_read_byte(state, TDA1004X_DSP_DATA1);
365 data2 = tda1004x_read_byte(state, TDA1004X_DSP_DATA2);
366 if (data1 != 0x67 || data2 < 0x20 || data2 > 0x2e) {
367 printk(KERN_INFO "tda1004x: found firmware revision %x -- invalid\n", data2);
370 printk(KERN_INFO "tda1004x: found firmware revision %x -- ok\n", data2);
374 static int tda10045_fwupload(struct dvb_frontend* fe)
376 struct tda1004x_state* state = fe->demodulator_priv;
378 const struct firmware *fw;
380 /* don't re-upload unless necessary */
381 if (tda1004x_check_upload_ok(state) == 0)
384 /* request the firmware, this will block until someone uploads it */
385 printk(KERN_INFO "tda1004x: waiting for firmware upload (%s)...\n", TDA10045_DEFAULT_FIRMWARE);
386 ret = state->config->request_firmware(fe, &fw, TDA10045_DEFAULT_FIRMWARE);
388 printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
393 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0);
394 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
395 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
399 tda10045h_set_bandwidth(state, BANDWIDTH_8_MHZ);
401 ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10045H_FWPAGE, TDA10045H_CODE_IN);
402 release_firmware(fw);
405 printk(KERN_INFO "tda1004x: firmware upload complete\n");
407 /* wait for DSP to initialise */
408 /* DSPREADY doesn't seem to work on the TDA10045H */
411 return tda1004x_check_upload_ok(state);
414 static void tda10046_init_plls(struct dvb_frontend* fe)
416 struct tda1004x_state* state = fe->demodulator_priv;
418 tda1004x_write_byteI(state, TDA10046H_CONFPLL1, 0xf0);
419 tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 10); // PLL M = 10
420 if (state->config->xtal_freq == TDA10046_XTAL_4M ) {
421 dprintk("%s: setting up PLLs for a 4 MHz Xtal\n", __FUNCTION__);
422 tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 0); // PLL P = N = 0
424 dprintk("%s: setting up PLLs for a 16 MHz Xtal\n", __FUNCTION__);
425 tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 3); // PLL P = 0, N = 3
427 tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 99);
428 switch (state->config->if_freq) {
429 case TDA10046_FREQ_3617:
430 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd4);
431 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x2c);
433 case TDA10046_FREQ_3613:
434 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd4);
435 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x13);
437 case TDA10046_FREQ_045:
438 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0b);
439 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xa3);
441 case TDA10046_FREQ_052:
442 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
443 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x06);
446 tda10046h_set_bandwidth(state, BANDWIDTH_8_MHZ); // default bandwidth 8 MHz
449 static int tda10046_fwupload(struct dvb_frontend* fe)
451 struct tda1004x_state* state = fe->demodulator_priv;
453 const struct firmware *fw;
455 /* reset + wake up chip */
456 tda1004x_write_byteI(state, TDA1004X_CONFC4, 0);
457 tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 1, 0);
458 /* let the clocks recover from sleep */
461 /* don't re-upload unless necessary */
462 if (tda1004x_check_upload_ok(state) == 0)
466 tda10046_init_plls(fe);
468 if (state->config->request_firmware != NULL) {
469 /* request the firmware, this will block until someone uploads it */
470 printk(KERN_INFO "tda1004x: waiting for firmware upload...\n");
471 ret = state->config->request_firmware(fe, &fw, TDA10046_DEFAULT_FIRMWARE);
473 printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
476 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8); // going to boot from HOST
477 ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10046H_CODE_CPT, TDA10046H_CODE_IN);
478 release_firmware(fw);
482 /* boot from firmware eeprom */
483 /* Hac Note: we might need to do some GPIO Magic here */
484 printk(KERN_INFO "tda1004x: booting from eeprom\n");
485 tda1004x_write_mask(state, TDA1004X_CONFC4, 4, 4);
488 return tda1004x_check_upload_ok(state);
491 static int tda1004x_encode_fec(int fec)
493 // convert known FEC values
511 static int tda1004x_decode_fec(int tdafec)
513 // convert known FEC values
531 int tda1004x_write_byte(struct dvb_frontend* fe, int reg, int data)
533 struct tda1004x_state* state = fe->demodulator_priv;
535 return tda1004x_write_byteI(state, reg, data);
538 static int tda10045_init(struct dvb_frontend* fe)
540 struct tda1004x_state* state = fe->demodulator_priv;
542 dprintk("%s\n", __FUNCTION__);
544 if (state->initialised)
547 if (tda10045_fwupload(fe)) {
548 printk("tda1004x: firmware upload failed\n");
552 tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0); // wake up the ADC
555 if (state->config->pll_init) {
556 tda1004x_enable_tuner_i2c(state);
557 state->config->pll_init(fe);
558 tda1004x_disable_tuner_i2c(state);
562 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
563 tda1004x_write_mask(state, TDA1004X_AUTO, 8, 0); // select HP stream
564 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x40, 0); // set polarity of VAGC signal
565 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x80, 0x80); // enable pulse killer
566 tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10); // enable auto offset
567 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0x0); // no frequency offset
568 tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 0); // setup MPEG2 TS interface
569 tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0); // setup MPEG2 TS interface
570 tda1004x_write_mask(state, TDA1004X_VBER_MSB, 0xe0, 0xa0); // 10^6 VBER measurement bits
571 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x10, 0); // VAGC polarity
572 tda1004x_write_byteI(state, TDA1004X_CONFADC1, 0x2e);
574 tda1004x_write_mask(state, 0x1f, 0x01, state->config->invert_oclk);
576 state->initialised = 1;
580 static int tda10046_init(struct dvb_frontend* fe)
582 struct tda1004x_state* state = fe->demodulator_priv;
583 dprintk("%s\n", __FUNCTION__);
585 if (state->initialised)
588 if (tda10046_fwupload(fe)) {
589 printk("tda1004x: firmware upload failed\n");
593 // Init the tuner PLL
594 if (state->config->pll_init) {
595 tda1004x_enable_tuner_i2c(state);
596 state->config->pll_init(fe);
597 tda1004x_disable_tuner_i2c(state);
601 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
602 tda1004x_write_byteI(state, TDA1004X_AUTO, 7); // select HP stream
603 tda1004x_write_byteI(state, TDA1004X_CONFC1, 8); // disable pulse killer
605 tda10046_init_plls(fe);
606 switch (state->config->agc_config) {
607 case TDA10046_AGC_DEFAULT:
608 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x00); // AGC setup
609 tda1004x_write_byteI(state, TDA10046H_CONF_POLARITY, 0x60); // set AGC polarities
611 case TDA10046_AGC_IFO_AUTO_NEG:
612 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x0a); // AGC setup
613 tda1004x_write_byteI(state, TDA10046H_CONF_POLARITY, 0x60); // set AGC polarities
615 case TDA10046_AGC_IFO_AUTO_POS:
616 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x0a); // AGC setup
617 tda1004x_write_byteI(state, TDA10046H_CONF_POLARITY, 0x00); // set AGC polarities
619 case TDA10046_AGC_TDA827X:
620 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x02); // AGC setup
621 tda1004x_write_byteI(state, TDA10046H_AGC_THR, 0x70); // AGC Threshold
622 tda1004x_write_byteI(state, TDA10046H_AGC_RENORM, 0x0E); // Gain Renormalize
623 tda1004x_write_byteI(state, TDA10046H_CONF_POLARITY, 0x60); // set AGC polarities
626 tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE1, 0x61); // Turn both AGC outputs on
627 tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MIN, 0); // }
628 tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MAX, 0xff); // } AGC min/max values
629 tda1004x_write_byteI(state, TDA10046H_AGC_IF_MIN, 0); // }
630 tda1004x_write_byteI(state, TDA10046H_AGC_IF_MAX, 0xff); // }
631 tda1004x_write_byteI(state, TDA10046H_AGC_GAINS, 1); // IF gain 2, TUN gain 1
632 tda1004x_write_byteI(state, TDA10046H_CVBER_CTRL, 0x1a); // 10^6 VBER measurement bits
633 tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 7); // MPEG2 interface config
634 tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0xc0); // MPEG2 interface config
635 tda1004x_write_mask(state, 0x3a, 0x80, state->config->invert_oclk << 7);
637 tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE2, 0xe1); // tristate setup
638 tda1004x_write_byteI(state, TDA10046H_GPIO_OUT_SEL, 0xcc); // GPIO output config
639 tda1004x_write_byteI(state, TDA10046H_GPIO_SELECT, 8); // GPIO select
641 state->initialised = 1;
645 static int tda1004x_set_fe(struct dvb_frontend* fe,
646 struct dvb_frontend_parameters *fe_params)
648 struct tda1004x_state* state = fe->demodulator_priv;
652 dprintk("%s\n", __FUNCTION__);
654 if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
656 tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10);
657 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x80, 0);
658 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0);
660 // disable agc_conf[2]
661 tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 0);
665 tda1004x_enable_tuner_i2c(state);
666 state->config->pll_set(fe, fe_params);
667 tda1004x_disable_tuner_i2c(state);
669 // Hardcoded to use auto as much as possible on the TDA10045 as it
670 // is very unreliable if AUTO mode is _not_ used.
671 if (state->demod_type == TDA1004X_DEMOD_TDA10045) {
672 fe_params->u.ofdm.code_rate_HP = FEC_AUTO;
673 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
674 fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
677 // Set standard params.. or put them to auto
678 if ((fe_params->u.ofdm.code_rate_HP == FEC_AUTO) ||
679 (fe_params->u.ofdm.code_rate_LP == FEC_AUTO) ||
680 (fe_params->u.ofdm.constellation == QAM_AUTO) ||
681 (fe_params->u.ofdm.hierarchy_information == HIERARCHY_AUTO)) {
682 tda1004x_write_mask(state, TDA1004X_AUTO, 1, 1); // enable auto
683 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x03, 0); // turn off constellation bits
684 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 0); // turn off hierarchy bits
685 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x3f, 0); // turn off FEC bits
687 tda1004x_write_mask(state, TDA1004X_AUTO, 1, 0); // disable auto
690 tmp = tda1004x_encode_fec(fe_params->u.ofdm.code_rate_HP);
693 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 7, tmp);
696 tmp = tda1004x_encode_fec(fe_params->u.ofdm.code_rate_LP);
699 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x38, tmp << 3);
702 switch (fe_params->u.ofdm.constellation) {
704 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 0);
708 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 1);
712 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 2);
720 switch (fe_params->u.ofdm.hierarchy_information) {
722 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 0 << 5);
726 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 1 << 5);
730 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 2 << 5);
734 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 3 << 5);
743 switch (state->demod_type) {
744 case TDA1004X_DEMOD_TDA10045:
745 tda10045h_set_bandwidth(state, fe_params->u.ofdm.bandwidth);
748 case TDA1004X_DEMOD_TDA10046:
749 tda10046h_set_bandwidth(state, fe_params->u.ofdm.bandwidth);
754 inversion = fe_params->inversion;
755 if (state->config->invert)
756 inversion = inversion ? INVERSION_OFF : INVERSION_ON;
759 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x20, 0);
763 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x20, 0x20);
770 // set guard interval
771 switch (fe_params->u.ofdm.guard_interval) {
772 case GUARD_INTERVAL_1_32:
773 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
774 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 0 << 2);
777 case GUARD_INTERVAL_1_16:
778 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
779 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 1 << 2);
782 case GUARD_INTERVAL_1_8:
783 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
784 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 2 << 2);
787 case GUARD_INTERVAL_1_4:
788 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
789 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 3 << 2);
792 case GUARD_INTERVAL_AUTO:
793 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 2);
794 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 0 << 2);
801 // set transmission mode
802 switch (fe_params->u.ofdm.transmission_mode) {
803 case TRANSMISSION_MODE_2K:
804 tda1004x_write_mask(state, TDA1004X_AUTO, 4, 0);
805 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 0 << 4);
808 case TRANSMISSION_MODE_8K:
809 tda1004x_write_mask(state, TDA1004X_AUTO, 4, 0);
810 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 1 << 4);
813 case TRANSMISSION_MODE_AUTO:
814 tda1004x_write_mask(state, TDA1004X_AUTO, 4, 4);
815 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 0);
823 switch (state->demod_type) {
824 case TDA1004X_DEMOD_TDA10045:
825 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
826 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
829 case TDA1004X_DEMOD_TDA10046:
830 tda1004x_write_mask(state, TDA1004X_AUTO, 0x40, 0x40);
839 static int tda1004x_get_fe(struct dvb_frontend* fe, struct dvb_frontend_parameters *fe_params)
841 struct tda1004x_state* state = fe->demodulator_priv;
842 dprintk("%s\n", __FUNCTION__);
845 fe_params->inversion = INVERSION_OFF;
846 if (tda1004x_read_byte(state, TDA1004X_CONFC1) & 0x20)
847 fe_params->inversion = INVERSION_ON;
848 if (state->config->invert)
849 fe_params->inversion = fe_params->inversion ? INVERSION_OFF : INVERSION_ON;
852 switch (state->demod_type) {
853 case TDA1004X_DEMOD_TDA10045:
854 switch (tda1004x_read_byte(state, TDA10045H_WREF_LSB)) {
856 fe_params->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
859 fe_params->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
862 fe_params->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
867 case TDA1004X_DEMOD_TDA10046:
868 switch (tda1004x_read_byte(state, TDA10046H_TIME_WREF1)) {
870 fe_params->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
873 fe_params->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
876 fe_params->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
883 fe_params->u.ofdm.code_rate_HP =
884 tda1004x_decode_fec(tda1004x_read_byte(state, TDA1004X_OUT_CONF2) & 7);
885 fe_params->u.ofdm.code_rate_LP =
886 tda1004x_decode_fec((tda1004x_read_byte(state, TDA1004X_OUT_CONF2) >> 3) & 7);
889 switch (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 3) {
891 fe_params->u.ofdm.constellation = QPSK;
894 fe_params->u.ofdm.constellation = QAM_16;
897 fe_params->u.ofdm.constellation = QAM_64;
902 fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K;
903 if (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x10)
904 fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
907 switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x0c) >> 2) {
909 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_32;
912 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_16;
915 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_8;
918 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_4;
923 switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x60) >> 5) {
925 fe_params->u.ofdm.hierarchy_information = HIERARCHY_NONE;
928 fe_params->u.ofdm.hierarchy_information = HIERARCHY_1;
931 fe_params->u.ofdm.hierarchy_information = HIERARCHY_2;
934 fe_params->u.ofdm.hierarchy_information = HIERARCHY_4;
941 static int tda1004x_read_status(struct dvb_frontend* fe, fe_status_t * fe_status)
943 struct tda1004x_state* state = fe->demodulator_priv;
948 dprintk("%s\n", __FUNCTION__);
951 status = tda1004x_read_byte(state, TDA1004X_STATUS_CD);
958 *fe_status |= FE_HAS_SIGNAL;
960 *fe_status |= FE_HAS_CARRIER;
962 *fe_status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
964 // if we don't already have VITERBI (i.e. not LOCKED), see if the viterbi
965 // is getting anything valid
966 if (!(*fe_status & FE_HAS_VITERBI)) {
968 cber = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
971 status = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
974 cber |= (status << 8);
975 tda1004x_read_byte(state, TDA1004X_CBER_RESET);
978 *fe_status |= FE_HAS_VITERBI;
981 // if we DO have some valid VITERBI output, but don't already have SYNC
982 // bytes (i.e. not LOCKED), see if the RS decoder is getting anything valid.
983 if ((*fe_status & FE_HAS_VITERBI) && (!(*fe_status & FE_HAS_SYNC))) {
985 vber = tda1004x_read_byte(state, TDA1004X_VBER_LSB);
988 status = tda1004x_read_byte(state, TDA1004X_VBER_MID);
991 vber |= (status << 8);
992 status = tda1004x_read_byte(state, TDA1004X_VBER_MSB);
995 vber |= ((status << 16) & 0x0f);
996 tda1004x_read_byte(state, TDA1004X_CVBER_LUT);
998 // if RS has passed some valid TS packets, then we must be
999 // getting some SYNC bytes
1001 *fe_status |= FE_HAS_SYNC;
1005 dprintk("%s: fe_status=0x%x\n", __FUNCTION__, *fe_status);
1009 static int tda1004x_read_signal_strength(struct dvb_frontend* fe, u16 * signal)
1011 struct tda1004x_state* state = fe->demodulator_priv;
1015 dprintk("%s\n", __FUNCTION__);
1017 // determine the register to use
1018 switch (state->demod_type) {
1019 case TDA1004X_DEMOD_TDA10045:
1020 reg = TDA10045H_S_AGC;
1023 case TDA1004X_DEMOD_TDA10046:
1024 reg = TDA10046H_AGC_IF_LEVEL;
1029 tmp = tda1004x_read_byte(state, reg);
1033 *signal = (tmp << 8) | tmp;
1034 dprintk("%s: signal=0x%x\n", __FUNCTION__, *signal);
1038 static int tda1004x_read_snr(struct dvb_frontend* fe, u16 * snr)
1040 struct tda1004x_state* state = fe->demodulator_priv;
1043 dprintk("%s\n", __FUNCTION__);
1046 tmp = tda1004x_read_byte(state, TDA1004X_SNR);
1052 *snr = ((tmp << 8) | tmp);
1053 dprintk("%s: snr=0x%x\n", __FUNCTION__, *snr);
1057 static int tda1004x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
1059 struct tda1004x_state* state = fe->demodulator_priv;
1064 dprintk("%s\n", __FUNCTION__);
1066 // read the UCBLOCKS and reset
1068 tmp = tda1004x_read_byte(state, TDA1004X_UNCOR);
1072 while (counter++ < 5) {
1073 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1074 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1075 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1077 tmp2 = tda1004x_read_byte(state, TDA1004X_UNCOR);
1081 if ((tmp2 < tmp) || (tmp2 == 0))
1088 *ucblocks = 0xffffffff;
1090 dprintk("%s: ucblocks=0x%x\n", __FUNCTION__, *ucblocks);
1094 static int tda1004x_read_ber(struct dvb_frontend* fe, u32* ber)
1096 struct tda1004x_state* state = fe->demodulator_priv;
1099 dprintk("%s\n", __FUNCTION__);
1102 tmp = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
1106 tmp = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
1110 tda1004x_read_byte(state, TDA1004X_CBER_RESET);
1112 dprintk("%s: ber=0x%x\n", __FUNCTION__, *ber);
1116 static int tda1004x_sleep(struct dvb_frontend* fe)
1118 struct tda1004x_state* state = fe->demodulator_priv;
1120 switch (state->demod_type) {
1121 case TDA1004X_DEMOD_TDA10045:
1122 tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0x10);
1125 case TDA1004X_DEMOD_TDA10046:
1126 if (state->config->pll_sleep != NULL) {
1127 tda1004x_enable_tuner_i2c(state);
1128 state->config->pll_sleep(fe);
1129 tda1004x_disable_tuner_i2c(state);
1131 tda1004x_write_mask(state, TDA1004X_CONFC4, 1, 1);
1134 state->initialised = 0;
1139 static int tda1004x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
1141 fesettings->min_delay_ms = 800;
1142 /* Drift compensation makes no sense for DVB-T */
1143 fesettings->step_size = 0;
1144 fesettings->max_drift = 0;
1148 static void tda1004x_release(struct dvb_frontend* fe)
1150 struct tda1004x_state *state = fe->demodulator_priv;
1154 static struct dvb_frontend_ops tda10045_ops = {
1156 .name = "Philips TDA10045H DVB-T",
1158 .frequency_min = 51000000,
1159 .frequency_max = 858000000,
1160 .frequency_stepsize = 166667,
1162 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1163 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1164 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
1165 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
1168 .release = tda1004x_release,
1170 .init = tda10045_init,
1171 .sleep = tda1004x_sleep,
1173 .set_frontend = tda1004x_set_fe,
1174 .get_frontend = tda1004x_get_fe,
1175 .get_tune_settings = tda1004x_get_tune_settings,
1177 .read_status = tda1004x_read_status,
1178 .read_ber = tda1004x_read_ber,
1179 .read_signal_strength = tda1004x_read_signal_strength,
1180 .read_snr = tda1004x_read_snr,
1181 .read_ucblocks = tda1004x_read_ucblocks,
1184 struct dvb_frontend* tda10045_attach(const struct tda1004x_config* config,
1185 struct i2c_adapter* i2c)
1187 struct tda1004x_state *state;
1189 /* allocate memory for the internal state */
1190 state = kmalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
1194 /* setup the state */
1195 state->config = config;
1197 memcpy(&state->ops, &tda10045_ops, sizeof(struct dvb_frontend_ops));
1198 state->initialised = 0;
1199 state->demod_type = TDA1004X_DEMOD_TDA10045;
1201 /* check if the demod is there */
1202 if (tda1004x_read_byte(state, TDA1004X_CHIPID) != 0x25) {
1207 /* create dvb_frontend */
1208 state->frontend.ops = &state->ops;
1209 state->frontend.demodulator_priv = state;
1210 return &state->frontend;
1213 static struct dvb_frontend_ops tda10046_ops = {
1215 .name = "Philips TDA10046H DVB-T",
1217 .frequency_min = 51000000,
1218 .frequency_max = 858000000,
1219 .frequency_stepsize = 166667,
1221 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1222 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1223 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
1224 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
1227 .release = tda1004x_release,
1229 .init = tda10046_init,
1230 .sleep = tda1004x_sleep,
1232 .set_frontend = tda1004x_set_fe,
1233 .get_frontend = tda1004x_get_fe,
1234 .get_tune_settings = tda1004x_get_tune_settings,
1236 .read_status = tda1004x_read_status,
1237 .read_ber = tda1004x_read_ber,
1238 .read_signal_strength = tda1004x_read_signal_strength,
1239 .read_snr = tda1004x_read_snr,
1240 .read_ucblocks = tda1004x_read_ucblocks,
1243 struct dvb_frontend* tda10046_attach(const struct tda1004x_config* config,
1244 struct i2c_adapter* i2c)
1246 struct tda1004x_state *state;
1248 /* allocate memory for the internal state */
1249 state = kmalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
1253 /* setup the state */
1254 state->config = config;
1256 memcpy(&state->ops, &tda10046_ops, sizeof(struct dvb_frontend_ops));
1257 state->initialised = 0;
1258 state->demod_type = TDA1004X_DEMOD_TDA10046;
1260 /* check if the demod is there */
1261 if (tda1004x_read_byte(state, TDA1004X_CHIPID) != 0x46) {
1266 /* create dvb_frontend */
1267 state->frontend.ops = &state->ops;
1268 state->frontend.demodulator_priv = state;
1269 return &state->frontend;
1272 module_param(debug, int, 0644);
1273 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
1275 MODULE_DESCRIPTION("Philips TDA10045H & TDA10046H DVB-T Demodulator");
1276 MODULE_AUTHOR("Andrew de Quincey & Robert Schlabbach");
1277 MODULE_LICENSE("GPL");
1279 EXPORT_SYMBOL(tda10045_attach);
1280 EXPORT_SYMBOL(tda10046_attach);
1281 EXPORT_SYMBOL(tda1004x_write_byte);