2 Driver for Philips tda10086 DVBS Demodulator
4 (c) 2006 Andrew de Quincey
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 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.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"
34 #define SACLK 96000000
36 struct tda10086_state {
37 struct i2c_adapter* i2c;
38 const struct tda10086_config* config;
39 struct dvb_frontend frontend;
41 /* private demod data */
47 #define dprintk(args...) \
49 if (debug) printk(KERN_DEBUG "tda10086: " args); \
52 static int tda10086_write_byte(struct tda10086_state *state, int reg, int data)
55 u8 b0[] = { reg, data };
56 struct i2c_msg msg = { .flags = 0, .buf = b0, .len = 2 };
58 msg.addr = state->config->demod_address;
59 ret = i2c_transfer(state->i2c, &msg, 1);
62 dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
63 __FUNCTION__, reg, data, ret);
65 return (ret != 1) ? ret : 0;
68 static int tda10086_read_byte(struct tda10086_state *state, int reg)
73 struct i2c_msg msg[] = {{ .flags = 0, .buf = b0, .len = 1 },
74 { .flags = I2C_M_RD, .buf = b1, .len = 1 }};
76 msg[0].addr = state->config->demod_address;
77 msg[1].addr = state->config->demod_address;
78 ret = i2c_transfer(state->i2c, msg, 2);
81 dprintk("%s: error reg=0x%x, ret=%i\n", __FUNCTION__, reg,
89 static int tda10086_write_mask(struct tda10086_state *state, int reg, int mask, int data)
93 // read a byte and check
94 val = tda10086_read_byte(state, reg);
102 // write it out again
103 return tda10086_write_byte(state, reg, val);
106 static int tda10086_init(struct dvb_frontend* fe)
108 struct tda10086_state* state = fe->demodulator_priv;
110 dprintk ("%s\n", __FUNCTION__);
113 tda10086_write_byte(state, 0x00, 0x00);
117 tda10086_write_byte(state, 0x01, 0x94);
118 tda10086_write_byte(state, 0x02, 0x35); // NOTE: TT drivers appear to disable CSWP
119 tda10086_write_byte(state, 0x03, 0x64);
120 tda10086_write_byte(state, 0x04, 0x43);
121 tda10086_write_byte(state, 0x0c, 0x0c);
122 tda10086_write_byte(state, 0x1b, 0xb0); // noise threshold
123 tda10086_write_byte(state, 0x20, 0x89); // misc
124 tda10086_write_byte(state, 0x30, 0x04); // acquisition period length
125 tda10086_write_byte(state, 0x32, 0x00); // irq off
126 tda10086_write_byte(state, 0x31, 0x56); // setup AFC
128 // setup PLL (assumes 16Mhz XIN)
129 tda10086_write_byte(state, 0x55, 0x2c); // misc PLL setup
130 tda10086_write_byte(state, 0x3a, 0x0b); // M=12
131 tda10086_write_byte(state, 0x3b, 0x01); // P=2
132 tda10086_write_mask(state, 0x55, 0x20, 0x00); // powerup PLL
134 // setup TS interface
135 tda10086_write_byte(state, 0x11, 0x81);
136 tda10086_write_byte(state, 0x12, 0x81);
137 tda10086_write_byte(state, 0x19, 0x40); // parallel mode A + MSBFIRST
138 tda10086_write_byte(state, 0x56, 0x80); // powerdown WPLL - unused in the mode we use
139 tda10086_write_byte(state, 0x57, 0x08); // bypass WPLL - unused in the mode we use
140 tda10086_write_byte(state, 0x10, 0x2a);
143 tda10086_write_byte(state, 0x58, 0x61); // ADC setup
144 tda10086_write_mask(state, 0x58, 0x01, 0x00); // powerup ADC
147 tda10086_write_byte(state, 0x05, 0x0B);
148 tda10086_write_byte(state, 0x37, 0x63);
149 tda10086_write_byte(state, 0x3f, 0x03); // NOTE: flydvb uses 0x0a and varies it
150 tda10086_write_byte(state, 0x40, 0x64);
151 tda10086_write_byte(state, 0x41, 0x4f);
152 tda10086_write_byte(state, 0x42, 0x43);
155 tda10086_write_byte(state, 0x1a, 0x11); // VBER 10^6, DVB, QPSK
157 // setup carrier recovery
158 tda10086_write_byte(state, 0x3d, 0x80);
161 tda10086_write_byte(state, 0x36, 0x00); // all SEC off
162 tda10086_write_byte(state, 0x34, (((1<<19) * (22000/1000)) / (SACLK/1000))); // } tone frequency
163 tda10086_write_byte(state, 0x35, (((1<<19) * (22000/1000)) / (SACLK/1000)) >> 8); // }
168 static void tda10086_diseqc_wait(struct tda10086_state *state)
170 unsigned long timeout = jiffies + msecs_to_jiffies(200);
171 while (!(tda10086_read_byte(state, 0x50) & 0x01)) {
172 if(time_after(jiffies, timeout)) {
173 printk("%s: diseqc queue not ready, command may be lost.\n", __FUNCTION__);
180 static int tda10086_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
182 struct tda10086_state* state = fe->demodulator_priv;
184 dprintk ("%s\n", __FUNCTION__);
188 tda10086_write_byte(state, 0x36, 0x00);
192 tda10086_write_byte(state, 0x36, 0x01);
199 static int tda10086_send_master_cmd (struct dvb_frontend* fe,
200 struct dvb_diseqc_master_cmd* cmd)
202 struct tda10086_state* state = fe->demodulator_priv;
206 dprintk ("%s\n", __FUNCTION__);
208 if (cmd->msg_len > 6)
210 oldval = tda10086_read_byte(state, 0x36);
212 for(i=0; i< cmd->msg_len; i++) {
213 tda10086_write_byte(state, 0x48+i, cmd->msg[i]);
215 tda10086_write_byte(state, 0x36, 0x08 | ((cmd->msg_len + 1) << 4));
217 tda10086_diseqc_wait(state);
219 tda10086_write_byte(state, 0x36, oldval);
224 static int tda10086_send_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t minicmd)
226 struct tda10086_state* state = fe->demodulator_priv;
227 u8 oldval = tda10086_read_byte(state, 0x36);
229 dprintk ("%s\n", __FUNCTION__);
233 tda10086_write_byte(state, 0x36, 0x04);
237 tda10086_write_byte(state, 0x36, 0x06);
241 tda10086_diseqc_wait(state);
243 tda10086_write_byte(state, 0x36, oldval);
248 static int tda10086_set_inversion(struct tda10086_state *state,
249 struct dvb_frontend_parameters *fe_params)
253 dprintk ("%s %i %i\n", __FUNCTION__, fe_params->inversion, state->config->invert);
255 switch(fe_params->inversion) {
257 if (state->config->invert)
261 if (!state->config->invert)
268 tda10086_write_mask(state, 0x0c, 0xc0, invval);
273 static int tda10086_set_symbol_rate(struct tda10086_state *state,
274 struct dvb_frontend_parameters *fe_params)
285 u32 symbol_rate = fe_params->u.qpsk.symbol_rate;
287 dprintk ("%s %i\n", __FUNCTION__, symbol_rate);
289 // setup the decimation and anti-aliasing filters..
290 if (symbol_rate < (u32) (SACLK * 0.0137)) {
293 } else if (symbol_rate < (u32) (SACLK * 0.0208)) {
296 } else if (symbol_rate < (u32) (SACLK * 0.0270)) {
299 } else if (symbol_rate < (u32) (SACLK * 0.0416)) {
302 } else if (symbol_rate < (u32) (SACLK * 0.0550)) {
305 } else if (symbol_rate < (u32) (SACLK * 0.0833)) {
308 } else if (symbol_rate < (u32) (SACLK * 0.1100)) {
311 } else if (symbol_rate < (u32) (SACLK * 0.1666)) {
314 } else if (symbol_rate < (u32) (SACLK * 0.2200)) {
317 } else if (symbol_rate < (u32) (SACLK * 0.3333)) {
327 big = (1ULL<<21) * ((u64) symbol_rate/1000ULL) * (1ULL<<dfn);
328 big += ((SACLK/1000ULL)-1ULL);
329 do_div(big, (SACLK/1000ULL));
333 tmp = (1<<dfn)*(symbol_rate/1000);
334 bdri = ((32 * (SACLK/1000)) + (tmp-1)) / tmp;
336 tda10086_write_byte(state, 0x21, (afs << 7) | dfn);
337 tda10086_write_mask(state, 0x20, 0x08, byp << 3);
338 tda10086_write_byte(state, 0x06, bdr);
339 tda10086_write_byte(state, 0x07, bdr >> 8);
340 tda10086_write_byte(state, 0x08, bdr >> 16);
341 tda10086_write_byte(state, 0x09, bdri);
342 tda10086_write_byte(state, 0x37, reg37);
343 tda10086_write_byte(state, 0x42, reg42);
348 static int tda10086_set_fec(struct tda10086_state *state,
349 struct dvb_frontend_parameters *fe_params)
353 dprintk ("%s %i\n", __FUNCTION__, fe_params->u.qpsk.fec_inner);
355 switch(fe_params->u.qpsk.fec_inner) {
386 tda10086_write_byte(state, 0x0d, fecval);
391 static int tda10086_set_frontend(struct dvb_frontend* fe,
392 struct dvb_frontend_parameters *fe_params)
394 struct tda10086_state *state = fe->demodulator_priv;
399 dprintk ("%s\n", __FUNCTION__);
402 if (fe->ops.tuner_ops.set_params) {
403 fe->ops.tuner_ops.set_params(fe, fe_params);
404 if (fe->ops.i2c_gate_ctrl)
405 fe->ops.i2c_gate_ctrl(fe, 0);
407 if (fe->ops.tuner_ops.get_frequency)
408 fe->ops.tuner_ops.get_frequency(fe, &freq);
409 if (fe->ops.i2c_gate_ctrl)
410 fe->ops.i2c_gate_ctrl(fe, 0);
413 // calcluate the frequency offset (in *Hz* not kHz)
414 freqoff = fe_params->frequency - freq;
415 freqoff = ((1<<16) * freqoff) / (SACLK/1000);
416 tda10086_write_byte(state, 0x3d, 0x80 | ((freqoff >> 8) & 0x7f));
417 tda10086_write_byte(state, 0x3e, freqoff);
419 if ((ret = tda10086_set_inversion(state, fe_params)) < 0)
421 if ((ret = tda10086_set_symbol_rate(state, fe_params)) < 0)
423 if ((ret = tda10086_set_fec(state, fe_params)) < 0)
426 // soft reset + disable TS output until lock
427 tda10086_write_mask(state, 0x10, 0x40, 0x40);
428 tda10086_write_mask(state, 0x00, 0x01, 0x00);
430 state->symbol_rate = fe_params->u.qpsk.symbol_rate;
431 state->frequency = fe_params->frequency;
435 static int tda10086_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *fe_params)
437 struct tda10086_state* state = fe->demodulator_priv;
442 dprintk ("%s\n", __FUNCTION__);
444 // calculate the updated frequency (note: we convert from Hz->kHz)
445 tmp64 = tda10086_read_byte(state, 0x52);
446 tmp64 |= (tda10086_read_byte(state, 0x51) << 8);
448 tmp64 |= 0xffffffffffff0000ULL;
449 tmp64 = (tmp64 * (SACLK/1000ULL));
450 do_div(tmp64, (1ULL<<15) * (1ULL<<1));
451 fe_params->frequency = (int) state->frequency + (int) tmp64;
454 val = tda10086_read_byte(state, 0x0c);
458 fe_params->inversion = INVERSION_OFF;
459 if (state->config->invert)
460 fe_params->inversion = INVERSION_ON;
463 fe_params->inversion = INVERSION_ON;
464 if (state->config->invert)
465 fe_params->inversion = INVERSION_OFF;
469 tda10086_read_byte(state, 0x0f);
472 fe_params->inversion = INVERSION_OFF;
473 if (state->config->invert)
474 fe_params->inversion = INVERSION_ON;
477 fe_params->inversion = INVERSION_ON;
478 if (state->config->invert)
479 fe_params->inversion = INVERSION_OFF;
484 // calculate the updated symbol rate
485 tmp = tda10086_read_byte(state, 0x1d);
488 tmp = (tmp * 480 * (1<<1)) / 128;
489 tmp = ((state->symbol_rate/1000) * tmp) / (1000000/1000);
490 fe_params->u.qpsk.symbol_rate = state->symbol_rate + tmp;
493 val = (tda10086_read_byte(state, 0x0d) & 0x70) >> 4;
496 fe_params->u.qpsk.fec_inner = FEC_1_2;
499 fe_params->u.qpsk.fec_inner = FEC_2_3;
502 fe_params->u.qpsk.fec_inner = FEC_3_4;
505 fe_params->u.qpsk.fec_inner = FEC_4_5;
508 fe_params->u.qpsk.fec_inner = FEC_5_6;
511 fe_params->u.qpsk.fec_inner = FEC_6_7;
514 fe_params->u.qpsk.fec_inner = FEC_7_8;
517 fe_params->u.qpsk.fec_inner = FEC_8_9;
524 static int tda10086_read_status(struct dvb_frontend* fe, fe_status_t *fe_status)
526 struct tda10086_state* state = fe->demodulator_priv;
529 dprintk ("%s\n", __FUNCTION__);
531 val = tda10086_read_byte(state, 0x0e);
534 *fe_status |= FE_HAS_SIGNAL;
536 *fe_status |= FE_HAS_CARRIER;
538 *fe_status |= FE_HAS_VITERBI;
540 *fe_status |= FE_HAS_SYNC;
542 *fe_status |= FE_HAS_LOCK;
547 static int tda10086_read_signal_strength(struct dvb_frontend* fe, u16 * signal)
549 struct tda10086_state* state = fe->demodulator_priv;
552 dprintk ("%s\n", __FUNCTION__);
554 _str = tda10086_read_byte(state, 0x43);
555 *signal = (_str << 8) | _str;
560 static int tda10086_read_snr(struct dvb_frontend* fe, u16 * snr)
562 struct tda10086_state* state = fe->demodulator_priv;
565 dprintk ("%s\n", __FUNCTION__);
567 _snr = tda10086_read_byte(state, 0x1c);
568 *snr = (_snr << 8) | _snr;
573 static int tda10086_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
575 struct tda10086_state* state = fe->demodulator_priv;
577 dprintk ("%s\n", __FUNCTION__);
580 *ucblocks = tda10086_read_byte(state, 0x18) & 0x7f;
583 tda10086_write_byte(state, 0x18, 0x00);
584 tda10086_write_byte(state, 0x18, 0x80);
589 static int tda10086_read_ber(struct dvb_frontend* fe, u32* ber)
591 struct tda10086_state* state = fe->demodulator_priv;
593 dprintk ("%s\n", __FUNCTION__);
597 *ber |= tda10086_read_byte(state, 0x15);
598 *ber |= tda10086_read_byte(state, 0x16) << 8;
599 *ber |= (tda10086_read_byte(state, 0x17) & 0xf) << 16;
604 static int tda10086_sleep(struct dvb_frontend* fe)
606 struct tda10086_state* state = fe->demodulator_priv;
608 dprintk ("%s\n", __FUNCTION__);
610 tda10086_write_mask(state, 0x00, 0x08, 0x08);
615 static int tda10086_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
617 struct tda10086_state* state = fe->demodulator_priv;
619 dprintk ("%s\n", __FUNCTION__);
622 tda10086_write_mask(state, 0x00, 0x10, 0x10);
624 tda10086_write_mask(state, 0x00, 0x10, 0x00);
630 static int tda10086_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
632 if (fesettings->parameters.u.qpsk.symbol_rate > 20000000) {
633 fesettings->min_delay_ms = 50;
634 fesettings->step_size = 2000;
635 fesettings->max_drift = 8000;
636 } else if (fesettings->parameters.u.qpsk.symbol_rate > 12000000) {
637 fesettings->min_delay_ms = 100;
638 fesettings->step_size = 1500;
639 fesettings->max_drift = 9000;
640 } else if (fesettings->parameters.u.qpsk.symbol_rate > 8000000) {
641 fesettings->min_delay_ms = 100;
642 fesettings->step_size = 1000;
643 fesettings->max_drift = 8000;
644 } else if (fesettings->parameters.u.qpsk.symbol_rate > 4000000) {
645 fesettings->min_delay_ms = 100;
646 fesettings->step_size = 500;
647 fesettings->max_drift = 7000;
648 } else if (fesettings->parameters.u.qpsk.symbol_rate > 2000000) {
649 fesettings->min_delay_ms = 200;
650 fesettings->step_size = (fesettings->parameters.u.qpsk.symbol_rate / 8000);
651 fesettings->max_drift = 14 * fesettings->step_size;
653 fesettings->min_delay_ms = 200;
654 fesettings->step_size = (fesettings->parameters.u.qpsk.symbol_rate / 8000);
655 fesettings->max_drift = 18 * fesettings->step_size;
661 static void tda10086_release(struct dvb_frontend* fe)
663 struct tda10086_state *state = fe->demodulator_priv;
668 static struct dvb_frontend_ops tda10086_ops = {
671 .name = "Philips TDA10086 DVB-S",
673 .frequency_min = 950000,
674 .frequency_max = 2150000,
675 .frequency_stepsize = 125, /* kHz for QPSK frontends */
676 .symbol_rate_min = 1000000,
677 .symbol_rate_max = 45000000,
678 .caps = FE_CAN_INVERSION_AUTO |
679 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
680 FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
684 .release = tda10086_release,
686 .init = tda10086_init,
687 .sleep = tda10086_sleep,
688 .i2c_gate_ctrl = tda10086_i2c_gate_ctrl,
690 .set_frontend = tda10086_set_frontend,
691 .get_frontend = tda10086_get_frontend,
692 .get_tune_settings = tda10086_get_tune_settings,
694 .read_status = tda10086_read_status,
695 .read_ber = tda10086_read_ber,
696 .read_signal_strength = tda10086_read_signal_strength,
697 .read_snr = tda10086_read_snr,
698 .read_ucblocks = tda10086_read_ucblocks,
700 .diseqc_send_master_cmd = tda10086_send_master_cmd,
701 .diseqc_send_burst = tda10086_send_burst,
702 .set_tone = tda10086_set_tone,
705 struct dvb_frontend* tda10086_attach(const struct tda10086_config* config,
706 struct i2c_adapter* i2c)
708 struct tda10086_state *state;
710 dprintk ("%s\n", __FUNCTION__);
712 /* allocate memory for the internal state */
713 state = kmalloc(sizeof(struct tda10086_state), GFP_KERNEL);
717 /* setup the state */
718 state->config = config;
721 /* check if the demod is there */
722 if (tda10086_read_byte(state, 0x1e) != 0xe1) {
727 /* create dvb_frontend */
728 memcpy(&state->frontend.ops, &tda10086_ops, sizeof(struct dvb_frontend_ops));
729 state->frontend.demodulator_priv = state;
730 return &state->frontend;
733 module_param(debug, int, 0644);
734 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
736 MODULE_DESCRIPTION("Philips TDA10086 DVB-S Demodulator");
737 MODULE_AUTHOR("Andrew de Quincey");
738 MODULE_LICENSE("GPL");
740 EXPORT_SYMBOL(tda10086_attach);