2 Driver for Philips TDA8083 based QPSK Demodulator
4 Copyright (C) 2001 Convergence Integrated Media GmbH
6 written by Ralph Metzler <ralph@convergence.de>
8 adoption to the new DVB frontend API and diagnostic ioctl's
9 by Holger Waechtler <holger@convergence.de>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/init.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/string.h>
32 #include <linux/slab.h>
33 #include "dvb_frontend.h"
37 struct tda8083_state {
39 struct i2c_adapter* i2c;
41 struct dvb_frontend_ops ops;
43 /* configuration settings */
44 const struct tda8083_config* config;
46 struct dvb_frontend frontend;
50 #define dprintk(args...) \
52 if (debug) printk(KERN_DEBUG "tda8083: " args); \
56 static u8 tda8083_init_tab [] = {
57 0x04, 0x00, 0x4a, 0x79, 0x04, 0x00, 0xff, 0xea,
58 0x48, 0x42, 0x79, 0x60, 0x70, 0x52, 0x9a, 0x10,
59 0x0e, 0x10, 0xf2, 0xa7, 0x93, 0x0b, 0x05, 0xc8,
60 0x9d, 0x00, 0x42, 0x80, 0x00, 0x60, 0x40, 0x00,
61 0x00, 0x75, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00,
62 0x00, 0x00, 0x00, 0x00
66 static int tda8083_writereg (struct tda8083_state* state, u8 reg, u8 data)
69 u8 buf [] = { reg, data };
70 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
72 ret = i2c_transfer(state->i2c, &msg, 1);
75 dprintk ("%s: writereg error (reg %02x, ret == %i)\n",
76 __FUNCTION__, reg, ret);
78 return (ret != 1) ? -1 : 0;
82 static int tda8083_readregs (struct tda8083_state* state, u8 reg1, u8 *b, u8 len)
85 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = ®1, .len = 1 },
86 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };
88 ret = i2c_transfer(state->i2c, msg, 2);
91 dprintk ("%s: readreg error (reg %02x, ret == %i)\n",
92 __FUNCTION__, reg1, ret);
94 return ret == 2 ? 0 : -1;
98 static inline u8 tda8083_readreg (struct tda8083_state* state, u8 reg)
102 tda8083_readregs (state, reg, &val, 1);
109 static int tda8083_set_inversion (struct tda8083_state* state, fe_spectral_inversion_t inversion)
111 /* XXX FIXME: implement other modes than FEC_AUTO */
112 if (inversion == INVERSION_AUTO)
119 static int tda8083_set_fec (struct tda8083_state* state, fe_code_rate_t fec)
122 return tda8083_writereg (state, 0x07, 0xff);
124 if (fec >= FEC_1_2 && fec <= FEC_8_9)
125 return tda8083_writereg (state, 0x07, 1 << (FEC_8_9 - fec));
131 static fe_code_rate_t tda8083_get_fec (struct tda8083_state* state)
134 static fe_code_rate_t fec_tab [] = { FEC_8_9, FEC_1_2, FEC_2_3, FEC_3_4,
135 FEC_4_5, FEC_5_6, FEC_6_7, FEC_7_8 };
137 index = tda8083_readreg(state, 0x0e) & 0x07;
139 return fec_tab [index];
143 static int tda8083_set_symbolrate (struct tda8083_state* state, u32 srate)
149 if (srate > 32000000)
155 if (srate < 24000000)
157 if (srate < 16000000)
163 tmp = (tmp % srate) << 8;
164 ratio = (ratio << 8) + tmp / srate;
166 tmp = (tmp % srate) << 8;
167 ratio = (ratio << 8) + tmp / srate;
169 dprintk("tda8083: ratio == %08x\n", (unsigned int) ratio);
171 tda8083_writereg (state, 0x05, filter);
172 tda8083_writereg (state, 0x02, (ratio >> 16) & 0xff);
173 tda8083_writereg (state, 0x03, (ratio >> 8) & 0xff);
174 tda8083_writereg (state, 0x04, (ratio ) & 0xff);
176 tda8083_writereg (state, 0x00, 0x3c);
177 tda8083_writereg (state, 0x00, 0x04);
183 static void tda8083_wait_diseqc_fifo (struct tda8083_state* state, int timeout)
185 unsigned long start = jiffies;
187 while (jiffies - start < timeout &&
188 !(tda8083_readreg(state, 0x02) & 0x80))
194 static int tda8083_set_tone (struct tda8083_state* state, fe_sec_tone_mode_t tone)
196 tda8083_writereg (state, 0x26, 0xf1);
200 return tda8083_writereg (state, 0x29, 0x00);
202 return tda8083_writereg (state, 0x29, 0x80);
209 static int tda8083_set_voltage (struct tda8083_state* state, fe_sec_voltage_t voltage)
213 return tda8083_writereg (state, 0x20, 0x00);
215 return tda8083_writereg (state, 0x20, 0x11);
221 static int tda8083_send_diseqc_burst (struct tda8083_state* state, fe_sec_mini_cmd_t burst)
225 tda8083_writereg (state, 0x29, (5 << 2)); /* send burst A */
228 tda8083_writereg (state, 0x29, (7 << 2)); /* send B */
234 tda8083_wait_diseqc_fifo (state, 100);
260 static int tda8083_send_diseqc_msg (struct dvb_frontend* fe,
261 struct dvb_diseqc_master_cmd *m)
263 struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
266 tda8083_writereg (state, 0x29, (m->msg_len - 3) | (1 << 2)); /* enable */
268 for (i=0; i<m->msg_len; i++)
269 tda8083_writereg (state, 0x23 + i, m->msg[i]);
271 tda8083_writereg (state, 0x29, (m->msg_len - 3) | (3 << 2)); /* send!! */
273 tda8083_wait_diseqc_fifo (state, 100);
278 static int tda8083_read_status(struct dvb_frontend* fe, fe_status_t* status)
280 struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
282 u8 signal = ~tda8083_readreg (state, 0x01);
283 u8 sync = tda8083_readreg (state, 0x02);
288 *status |= FE_HAS_SIGNAL;
291 *status |= FE_HAS_CARRIER;
294 *status |= FE_HAS_VITERBI;
297 *status |= FE_HAS_SYNC;
299 if ((sync & 0x1f) == 0x1f)
300 *status |= FE_HAS_LOCK;
305 static int tda8083_read_signal_strength(struct dvb_frontend* fe, u16* strength)
307 struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
309 u8 signal = ~tda8083_readreg (state, 0x01);
310 *strength = (signal << 8) | signal;
315 static int tda8083_read_snr(struct dvb_frontend* fe, u16* snr)
317 struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
319 u8 _snr = tda8083_readreg (state, 0x08);
320 *snr = (_snr << 8) | _snr;
325 static int tda8083_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
327 struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
329 state->config->pll_set(fe, p);
330 tda8083_set_inversion (state, p->inversion);
331 tda8083_set_fec (state, p->u.qpsk.fec_inner);
332 tda8083_set_symbolrate (state, p->u.qpsk.symbol_rate);
334 tda8083_writereg (state, 0x00, 0x3c);
335 tda8083_writereg (state, 0x00, 0x04);
340 static int tda8083_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
342 struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
344 /* FIXME: get symbolrate & frequency offset...*/
345 /*p->frequency = ???;*/
346 p->inversion = (tda8083_readreg (state, 0x0e) & 0x80) ?
347 INVERSION_ON : INVERSION_OFF;
348 p->u.qpsk.fec_inner = tda8083_get_fec (state);
349 /*p->u.qpsk.symbol_rate = tda8083_get_symbolrate (state);*/
354 static int tda8083_sleep(struct dvb_frontend* fe)
356 struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
358 tda8083_writereg (state, 0x00, 0x02);
362 static int tda8083_init(struct dvb_frontend* fe)
364 struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
368 tda8083_writereg (state, i, tda8083_init_tab[i]);
370 if (state->config->pll_init) state->config->pll_init(fe);
372 tda8083_writereg (state, 0x00, 0x3c);
373 tda8083_writereg (state, 0x00, 0x04);
378 static int tda8083_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
380 struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
382 tda8083_send_diseqc_burst (state, burst);
383 tda8083_writereg (state, 0x00, 0x3c);
384 tda8083_writereg (state, 0x00, 0x04);
389 static int tda8083_diseqc_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
391 struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
393 tda8083_set_tone (state, tone);
394 tda8083_writereg (state, 0x00, 0x3c);
395 tda8083_writereg (state, 0x00, 0x04);
400 static int tda8083_diseqc_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
402 struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
404 tda8083_set_voltage (state, voltage);
405 tda8083_writereg (state, 0x00, 0x3c);
406 tda8083_writereg (state, 0x00, 0x04);
411 static void tda8083_release(struct dvb_frontend* fe)
413 struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
417 static struct dvb_frontend_ops tda8083_ops;
419 struct dvb_frontend* tda8083_attach(const struct tda8083_config* config,
420 struct i2c_adapter* i2c)
422 struct tda8083_state* state = NULL;
424 /* allocate memory for the internal state */
425 state = (struct tda8083_state*) kmalloc(sizeof(struct tda8083_state), GFP_KERNEL);
426 if (state == NULL) goto error;
428 /* setup the state */
429 state->config = config;
431 memcpy(&state->ops, &tda8083_ops, sizeof(struct dvb_frontend_ops));
433 /* check if the demod is there */
434 if ((tda8083_readreg(state, 0x00)) != 0x05) goto error;
436 /* create dvb_frontend */
437 state->frontend.ops = &state->ops;
438 state->frontend.demodulator_priv = state;
439 return &state->frontend;
442 if (state) kfree(state);
446 static struct dvb_frontend_ops tda8083_ops = {
449 .name = "Philips TDA8083 DVB-S",
451 .frequency_min = 950000, /* FIXME: guessed! */
452 .frequency_max = 1400000, /* FIXME: guessed! */
453 .frequency_stepsize = 125, /* kHz for QPSK frontends */
454 /* .frequency_tolerance = ???,*/
455 .symbol_rate_min = 1000000, /* FIXME: guessed! */
456 .symbol_rate_max = 45000000, /* FIXME: guessed! */
457 /* .symbol_rate_tolerance = ???,*/
458 .caps = FE_CAN_INVERSION_AUTO |
459 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
460 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
461 FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
462 FE_CAN_QPSK | FE_CAN_MUTE_TS
465 .release = tda8083_release,
467 .init = tda8083_init,
468 .sleep = tda8083_sleep,
470 .set_frontend = tda8083_set_frontend,
471 .get_frontend = tda8083_get_frontend,
473 .read_status = tda8083_read_status,
474 .read_signal_strength = tda8083_read_signal_strength,
475 .read_snr = tda8083_read_snr,
477 .diseqc_send_master_cmd = tda8083_send_diseqc_msg,
478 .diseqc_send_burst = tda8083_diseqc_send_burst,
479 .set_tone = tda8083_diseqc_set_tone,
480 .set_voltage = tda8083_diseqc_set_voltage,
483 module_param(debug, int, 0644);
484 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
486 MODULE_DESCRIPTION("Philips TDA8083 DVB-S Demodulator");
487 MODULE_AUTHOR("Ralph Metzler, Holger Waechtler");
488 MODULE_LICENSE("GPL");
490 EXPORT_SYMBOL(tda8083_attach);