4 * Philips TDA8044 / TDA8083 QPSK demodulator driver
6 * Copyright (C) 2001 Felix Domke <tmbinc@elitedvb.net>
7 * Copyright (C) 2002-2004 Andreas Oberritter <obi@linuxtv.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/config.h>
25 #include <linux/delay.h>
26 #include <linux/init.h>
27 #include <linux/spinlock.h>
28 #include <linux/threads.h>
29 #include <linux/interrupt.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <asm/div64.h>
35 #include "dvb_frontend.h"
44 struct tda80xx_state {
46 struct i2c_adapter* i2c;
48 struct dvb_frontend_ops ops;
50 /* configuration settings */
51 const struct tda80xx_config* config;
53 struct dvb_frontend frontend;
57 struct work_struct worklet;
58 fe_code_rate_t code_rate;
59 fe_spectral_inversion_t spectral_inversion;
65 #define dprintk if (debug) printk
67 static u8 tda8044_inittab_pre[] = {
68 0x02, 0x00, 0x6f, 0xb5, 0x86, 0x22, 0x00, 0xea,
69 0x30, 0x42, 0x98, 0x68, 0x70, 0x42, 0x99, 0x58,
70 0x95, 0x10, 0xf5, 0xe7, 0x93, 0x0b, 0x15, 0x68,
71 0x9a, 0x90, 0x61, 0x80, 0x00, 0xe0, 0x40, 0x00,
72 0x0f, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 static u8 tda8044_inittab_post[] = {
77 0x04, 0x00, 0x6f, 0xb5, 0x86, 0x22, 0x00, 0xea,
78 0x30, 0x42, 0x98, 0x68, 0x70, 0x42, 0x99, 0x50,
79 0x95, 0x10, 0xf5, 0xe7, 0x93, 0x0b, 0x15, 0x68,
80 0x9a, 0x90, 0x61, 0x80, 0x00, 0xe0, 0x40, 0x6c,
81 0x0f, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85 static u8 tda8083_inittab[] = {
86 0x04, 0x00, 0x4a, 0x79, 0x04, 0x00, 0xff, 0xea,
87 0x48, 0x42, 0x79, 0x60, 0x70, 0x52, 0x9a, 0x10,
88 0x0e, 0x10, 0xf2, 0xa7, 0x93, 0x0b, 0x05, 0xc8,
89 0x9d, 0x00, 0x42, 0x80, 0x00, 0x60, 0x40, 0x00,
90 0x00, 0x75, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00
94 static __inline__ u32 tda80xx_div(u32 a, u32 b)
96 return (a + (b / 2)) / b;
99 static __inline__ u32 tda80xx_gcd(u32 a, u32 b)
103 while ((r = a % b)) {
111 static int tda80xx_read(struct tda80xx_state* state, u8 reg, u8 *buf, u8 len)
114 struct i2c_msg msg[] = { { .addr = state->config->demod_address, .flags = 0, .buf = ®, .len = 1 },
115 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = buf, .len = len } };
117 ret = i2c_transfer(state->i2c, msg, 2);
120 dprintk("%s: readreg error (reg %02x, ret == %i)\n",
121 __FUNCTION__, reg, ret);
125 return (ret == 2) ? 0 : -EREMOTEIO;
128 static int tda80xx_write(struct tda80xx_state* state, u8 reg, const u8 *buf, u8 len)
132 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = wbuf, .len = len + 1 };
135 memcpy(&wbuf[1], buf, len);
137 ret = i2c_transfer(state->i2c, &msg, 1);
140 dprintk("%s: i2c xfer error (ret == %i)\n", __FUNCTION__, ret);
144 return (ret == 1) ? 0 : -EREMOTEIO;
147 static __inline__ u8 tda80xx_readreg(struct tda80xx_state* state, u8 reg)
151 tda80xx_read(state, reg, &val, 1);
156 static __inline__ int tda80xx_writereg(struct tda80xx_state* state, u8 reg, u8 data)
158 return tda80xx_write(state, reg, &data, 1);
161 static int tda80xx_set_parameters(struct tda80xx_state* state,
162 fe_spectral_inversion_t inversion,
164 fe_code_rate_t fec_inner)
170 u32 sr = symbol_rate;
174 if (symbol_rate > (state->clk * 3) / 16)
176 else if (symbol_rate > (state->clk * 3) / 32)
178 else if (symbol_rate > (state->clk * 3) / 64)
183 clk = scd ? (state->clk / (scd * 2)) : state->clk;
187 * Differential decoding off
188 * Spectral inversion unknown
191 if (inversion == INVERSION_ON)
193 else if (inversion == INVERSION_OFF)
200 * system clock frequency is up to 64 or 96 MHz
203 * r = k * clk / symbol_rate
205 * k: 2^21 for caa 0..3,
209 if (symbol_rate <= (clk * 3) / 32)
211 else if (symbol_rate <= (clk * 3) / 16)
216 gcd = tda80xx_gcd(clk, sr);
220 gcd = tda80xx_gcd(k, sr);
224 ratio = (u64)k * (u64)clk;
227 buf[1] = ratio >> 16;
231 /* nyquist filter roll-off factor 35% */
234 clk = scd ? (state->clk / (scd * 2)) : state->clk;
236 /* Anti Alias Filter */
237 if (symbol_rate < (clk * 3) / 64)
238 printk("tda80xx: unsupported symbol rate: %u\n", symbol_rate);
239 else if (symbol_rate <= clk / 16)
241 else if (symbol_rate <= (clk * 3) / 32)
243 else if (symbol_rate <= clk / 8)
245 else if (symbol_rate <= (clk * 3) / 16)
247 else if (symbol_rate <= clk / 4)
249 else if (symbol_rate <= (clk * 3) / 8)
251 else if (symbol_rate <= clk / 2)
256 /* Sigma Delta converter */
259 /* FEC: Possible puncturing rates */
260 if (fec_inner == FEC_NONE)
262 else if ((fec_inner >= FEC_1_2) && (fec_inner <= FEC_8_9))
263 buf[6] = (1 << (8 - fec_inner));
264 else if (fec_inner == FEC_AUTO)
269 /* carrier lock detector threshold value */
271 /* AFC1: proportional part settings */
273 /* AFC1: integral part settings */
275 /* PD: Leaky integrator SCPC mode */
277 /* AFC2, AFC1 controls */
279 /* PD: proportional part settings */
281 /* PD: integral part settings */
284 buf[14] = 0x50 | scd;
286 printk("symbol_rate=%u clk=%u\n", symbol_rate, clk);
288 return tda80xx_write(state, 0x01, buf, sizeof(buf));
291 static int tda80xx_set_clk(struct tda80xx_state* state)
295 /* CLK proportional part */
296 buf[0] = (0x06 << 5) | 0x08; /* CMP[2:0], CSP[4:0] */
297 /* CLK integral part */
298 buf[1] = (0x04 << 5) | 0x1a; /* CMI[2:0], CSI[4:0] */
300 return tda80xx_write(state, 0x17, buf, sizeof(buf));
304 static int tda80xx_set_scpc_freq_offset(struct tda80xx_state* state)
306 /* a constant value is nonsense here imho */
307 return tda80xx_writereg(state, 0x22, 0xf9);
311 static int tda80xx_close_loop(struct tda80xx_state* state)
315 /* PD: Loop closed, LD: lock detect enable, SCPC: Sweep mode - AFC1 loop closed */
317 /* AFC1: Loop closed, CAR Feedback: 8192 */
320 return tda80xx_write(state, 0x0b, buf, sizeof(buf));
323 static irqreturn_t tda80xx_irq(int irq, void *priv, struct pt_regs *pt)
330 static void tda80xx_read_status_int(struct tda80xx_state* state)
334 static const fe_spectral_inversion_t inv_tab[] = {
335 INVERSION_OFF, INVERSION_ON
338 static const fe_code_rate_t fec_tab[] = {
339 FEC_8_9, FEC_1_2, FEC_2_3, FEC_3_4,
340 FEC_4_5, FEC_5_6, FEC_6_7, FEC_7_8,
343 val = tda80xx_readreg(state, 0x02);
347 if (val & 0x01) /* demodulator lock */
348 state->status |= FE_HAS_SIGNAL;
349 if (val & 0x02) /* clock recovery lock */
350 state->status |= FE_HAS_CARRIER;
351 if (val & 0x04) /* viterbi lock */
352 state->status |= FE_HAS_VITERBI;
353 if (val & 0x08) /* deinterleaver lock (packet sync) */
354 state->status |= FE_HAS_SYNC;
355 if (val & 0x10) /* derandomizer lock (frame sync) */
356 state->status |= FE_HAS_LOCK;
357 if (val & 0x20) /* frontend can not lock */
358 state->status |= FE_TIMEDOUT;
360 if ((state->status & (FE_HAS_CARRIER)) && (state->afc_loop)) {
361 printk("tda80xx: closing loop\n");
362 tda80xx_close_loop(state);
366 if (state->status & (FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK)) {
367 val = tda80xx_readreg(state, 0x0e);
368 state->code_rate = fec_tab[val & 0x07];
369 if (state->status & (FE_HAS_SYNC | FE_HAS_LOCK))
370 state->spectral_inversion = inv_tab[(val >> 7) & 0x01];
372 state->spectral_inversion = INVERSION_AUTO;
375 state->code_rate = FEC_AUTO;
379 static void tda80xx_worklet(void *priv)
381 struct tda80xx_state *state = priv;
383 tda80xx_writereg(state, 0x00, 0x04);
384 enable_irq(state->config->irq);
386 tda80xx_read_status_int(state);
389 static void tda80xx_wait_diseqc_fifo(struct tda80xx_state* state)
393 for (i = 0; i < 100; i++) {
394 if (tda80xx_readreg(state, 0x02) & 0x80)
400 static int tda8044_init(struct dvb_frontend* fe)
402 struct tda80xx_state* state = fe->demodulator_priv;
406 * this function is a mess...
409 if ((ret = tda80xx_write(state, 0x00, tda8044_inittab_pre, sizeof(tda8044_inittab_pre))))
412 tda80xx_writereg(state, 0x0f, 0x50);
414 tda80xx_writereg(state, 0x20, 0x8F); /* FIXME */
415 tda80xx_writereg(state, 0x20, state->config->volt18setting); /* FIXME */
416 //tda80xx_writereg(state, 0x00, 0x04);
417 tda80xx_writereg(state, 0x00, 0x0C);
419 //tda80xx_writereg(state, 0x00, 0x08); /* Reset AFC1 loop filter */
421 tda80xx_write(state, 0x00, tda8044_inittab_post, sizeof(tda8044_inittab_post));
423 if (state->config->pll_init) {
424 tda80xx_writereg(state, 0x1c, 0x80);
425 state->config->pll_init(fe);
426 tda80xx_writereg(state, 0x1c, 0x00);
432 static int tda8083_init(struct dvb_frontend* fe)
434 struct tda80xx_state* state = fe->demodulator_priv;
436 tda80xx_write(state, 0x00, tda8083_inittab, sizeof(tda8083_inittab));
438 if (state->config->pll_init) {
439 tda80xx_writereg(state, 0x1c, 0x80);
440 state->config->pll_init(fe);
441 tda80xx_writereg(state, 0x1c, 0x00);
447 static int tda80xx_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
449 struct tda80xx_state* state = fe->demodulator_priv;
453 return tda80xx_writereg(state, 0x20, state->config->volt13setting);
455 return tda80xx_writereg(state, 0x20, state->config->volt18setting);
456 case SEC_VOLTAGE_OFF:
457 return tda80xx_writereg(state, 0x20, 0);
463 static int tda80xx_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
465 struct tda80xx_state* state = fe->demodulator_priv;
469 return tda80xx_writereg(state, 0x29, 0x00);
471 return tda80xx_writereg(state, 0x29, 0x80);
477 static int tda80xx_send_diseqc_msg(struct dvb_frontend* fe, struct dvb_diseqc_master_cmd *cmd)
479 struct tda80xx_state* state = fe->demodulator_priv;
481 if (cmd->msg_len > 6)
484 tda80xx_writereg(state, 0x29, 0x08 | (cmd->msg_len - 3));
485 tda80xx_write(state, 0x23, cmd->msg, cmd->msg_len);
486 tda80xx_writereg(state, 0x29, 0x0c | (cmd->msg_len - 3));
487 tda80xx_wait_diseqc_fifo(state);
492 static int tda80xx_send_diseqc_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t cmd)
494 struct tda80xx_state* state = fe->demodulator_priv;
498 tda80xx_writereg(state, 0x29, 0x14);
501 tda80xx_writereg(state, 0x29, 0x1c);
507 tda80xx_wait_diseqc_fifo(state);
512 static int tda80xx_sleep(struct dvb_frontend* fe)
514 struct tda80xx_state* state = fe->demodulator_priv;
516 tda80xx_writereg(state, 0x00, 0x02); /* enter standby */
521 static int tda80xx_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
523 struct tda80xx_state* state = fe->demodulator_priv;
525 tda80xx_writereg(state, 0x1c, 0x80);
526 state->config->pll_set(fe, p);
527 tda80xx_writereg(state, 0x1c, 0x00);
529 tda80xx_set_parameters(state, p->inversion, p->u.qpsk.symbol_rate, p->u.qpsk.fec_inner);
530 tda80xx_set_clk(state);
531 //tda80xx_set_scpc_freq_offset(state);
537 static int tda80xx_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
539 struct tda80xx_state* state = fe->demodulator_priv;
541 if (!state->config->irq)
542 tda80xx_read_status_int(state);
544 p->inversion = state->spectral_inversion;
545 p->u.qpsk.fec_inner = state->code_rate;
550 static int tda80xx_read_status(struct dvb_frontend* fe, fe_status_t* status)
552 struct tda80xx_state* state = fe->demodulator_priv;
554 if (!state->config->irq)
555 tda80xx_read_status_int(state);
556 *status = state->status;
561 static int tda80xx_read_ber(struct dvb_frontend* fe, u32* ber)
563 struct tda80xx_state* state = fe->demodulator_priv;
567 if ((ret = tda80xx_read(state, 0x0b, buf, sizeof(buf))))
570 *ber = ((buf[0] & 0x1f) << 16) | (buf[1] << 8) | buf[2];
575 static int tda80xx_read_signal_strength(struct dvb_frontend* fe, u16* strength)
577 struct tda80xx_state* state = fe->demodulator_priv;
579 u8 gain = ~tda80xx_readreg(state, 0x01);
580 *strength = (gain << 8) | gain;
585 static int tda80xx_read_snr(struct dvb_frontend* fe, u16* snr)
587 struct tda80xx_state* state = fe->demodulator_priv;
589 u8 quality = tda80xx_readreg(state, 0x08);
590 *snr = (quality << 8) | quality;
595 static int tda80xx_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
597 struct tda80xx_state* state = fe->demodulator_priv;
599 *ucblocks = tda80xx_readreg(state, 0x0f);
600 if (*ucblocks == 0xff)
601 *ucblocks = 0xffffffff;
606 static int tda80xx_init(struct dvb_frontend* fe)
608 struct tda80xx_state* state = fe->demodulator_priv;
612 return tda8044_init(fe);
615 return tda8083_init(fe);
620 static void tda80xx_release(struct dvb_frontend* fe)
622 struct tda80xx_state* state = fe->demodulator_priv;
624 if (state->config->irq)
625 free_irq(state->config->irq, &state->worklet);
630 static struct dvb_frontend_ops tda80xx_ops;
632 struct dvb_frontend* tda80xx_attach(const struct tda80xx_config* config,
633 struct i2c_adapter* i2c)
635 struct tda80xx_state* state = NULL;
638 /* allocate memory for the internal state */
639 state = kmalloc(sizeof(struct tda80xx_state), GFP_KERNEL);
640 if (state == NULL) goto error;
642 /* setup the state */
643 state->config = config;
645 memcpy(&state->ops, &tda80xx_ops, sizeof(struct dvb_frontend_ops));
646 state->spectral_inversion = INVERSION_AUTO;
647 state->code_rate = FEC_AUTO;
651 /* check if the demod is there */
652 if (tda80xx_writereg(state, 0x89, 0x00) < 0) goto error;
653 state->id = tda80xx_readreg(state, 0x00);
657 state->clk = 96000000;
658 printk("tda80xx: Detected tda8044\n");
662 state->clk = 64000000;
663 printk("tda80xx: Detected tda8083\n");
671 if (state->config->irq) {
672 INIT_WORK(&state->worklet, tda80xx_worklet, state);
673 if ((ret = request_irq(state->config->irq, tda80xx_irq, SA_ONESHOT, "tda80xx", &state->worklet)) < 0) {
674 printk(KERN_ERR "tda80xx: request_irq failed (%d)\n", ret);
679 /* create dvb_frontend */
680 state->frontend.ops = &state->ops;
681 state->frontend.demodulator_priv = state;
682 return &state->frontend;
689 static struct dvb_frontend_ops tda80xx_ops = {
692 .name = "Philips TDA80xx DVB-S",
694 .frequency_min = 500000,
695 .frequency_max = 2700000,
696 .frequency_stepsize = 125,
697 .symbol_rate_min = 4500000,
698 .symbol_rate_max = 45000000,
699 .caps = FE_CAN_INVERSION_AUTO |
700 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
701 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
702 FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
707 .release = tda80xx_release,
709 .init = tda80xx_init,
710 .sleep = tda80xx_sleep,
712 .set_frontend = tda80xx_set_frontend,
713 .get_frontend = tda80xx_get_frontend,
715 .read_status = tda80xx_read_status,
716 .read_ber = tda80xx_read_ber,
717 .read_signal_strength = tda80xx_read_signal_strength,
718 .read_snr = tda80xx_read_snr,
719 .read_ucblocks = tda80xx_read_ucblocks,
721 .diseqc_send_master_cmd = tda80xx_send_diseqc_msg,
722 .diseqc_send_burst = tda80xx_send_diseqc_burst,
723 .set_tone = tda80xx_set_tone,
724 .set_voltage = tda80xx_set_voltage,
727 module_param(debug, int, 0644);
729 MODULE_DESCRIPTION("Philips TDA8044 / TDA8083 DVB-S Demodulator driver");
730 MODULE_AUTHOR("Felix Domke, Andreas Oberritter");
731 MODULE_LICENSE("GPL");
733 EXPORT_SYMBOL(tda80xx_attach);