[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / media / dvb / frontends / tda8083.c
blob168e013d23bd5478f06b048e3185c06e73b72c31
1 /*
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"
34 #include "tda8083.h"
37 struct tda8083_state {
38 struct i2c_adapter* i2c;
39 struct dvb_frontend_ops ops;
40 /* configuration settings */
41 const struct tda8083_config* config;
42 struct dvb_frontend frontend;
45 static int debug;
46 #define dprintk(args...) \
47 do { \
48 if (debug) printk(KERN_DEBUG "tda8083: " args); \
49 } while (0)
52 static u8 tda8083_init_tab [] = {
53 0x04, 0x00, 0x4a, 0x79, 0x04, 0x00, 0xff, 0xea,
54 0x48, 0x42, 0x79, 0x60, 0x70, 0x52, 0x9a, 0x10,
55 0x0e, 0x10, 0xf2, 0xa7, 0x93, 0x0b, 0x05, 0xc8,
56 0x9d, 0x00, 0x42, 0x80, 0x00, 0x60, 0x40, 0x00,
57 0x00, 0x75, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00
62 static int tda8083_writereg (struct tda8083_state* state, u8 reg, u8 data)
64 int ret;
65 u8 buf [] = { reg, data };
66 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
68 ret = i2c_transfer(state->i2c, &msg, 1);
70 if (ret != 1)
71 dprintk ("%s: writereg error (reg %02x, ret == %i)\n",
72 __FUNCTION__, reg, ret);
74 return (ret != 1) ? -1 : 0;
77 static int tda8083_readregs (struct tda8083_state* state, u8 reg1, u8 *b, u8 len)
79 int ret;
80 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = &reg1, .len = 1 },
81 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };
83 ret = i2c_transfer(state->i2c, msg, 2);
85 if (ret != 2)
86 dprintk ("%s: readreg error (reg %02x, ret == %i)\n",
87 __FUNCTION__, reg1, ret);
89 return ret == 2 ? 0 : -1;
92 static inline u8 tda8083_readreg (struct tda8083_state* state, u8 reg)
94 u8 val;
96 tda8083_readregs (state, reg, &val, 1);
98 return val;
101 static int tda8083_set_inversion (struct tda8083_state* state, fe_spectral_inversion_t inversion)
103 /* XXX FIXME: implement other modes than FEC_AUTO */
104 if (inversion == INVERSION_AUTO)
105 return 0;
107 return -EINVAL;
110 static int tda8083_set_fec (struct tda8083_state* state, fe_code_rate_t fec)
112 if (fec == FEC_AUTO)
113 return tda8083_writereg (state, 0x07, 0xff);
115 if (fec >= FEC_1_2 && fec <= FEC_8_9)
116 return tda8083_writereg (state, 0x07, 1 << (FEC_8_9 - fec));
118 return -EINVAL;
121 static fe_code_rate_t tda8083_get_fec (struct tda8083_state* state)
123 u8 index;
124 static fe_code_rate_t fec_tab [] = { FEC_8_9, FEC_1_2, FEC_2_3, FEC_3_4,
125 FEC_4_5, FEC_5_6, FEC_6_7, FEC_7_8 };
127 index = tda8083_readreg(state, 0x0e) & 0x07;
129 return fec_tab [index];
132 static int tda8083_set_symbolrate (struct tda8083_state* state, u32 srate)
134 u32 ratio;
135 u32 tmp;
136 u8 filter;
138 if (srate > 32000000)
139 srate = 32000000;
140 if (srate < 500000)
141 srate = 500000;
143 filter = 0;
144 if (srate < 24000000)
145 filter = 2;
146 if (srate < 16000000)
147 filter = 3;
149 tmp = 31250 << 16;
150 ratio = tmp / srate;
152 tmp = (tmp % srate) << 8;
153 ratio = (ratio << 8) + tmp / srate;
155 tmp = (tmp % srate) << 8;
156 ratio = (ratio << 8) + tmp / srate;
158 dprintk("tda8083: ratio == %08x\n", (unsigned int) ratio);
160 tda8083_writereg (state, 0x05, filter);
161 tda8083_writereg (state, 0x02, (ratio >> 16) & 0xff);
162 tda8083_writereg (state, 0x03, (ratio >> 8) & 0xff);
163 tda8083_writereg (state, 0x04, (ratio ) & 0xff);
165 tda8083_writereg (state, 0x00, 0x3c);
166 tda8083_writereg (state, 0x00, 0x04);
168 return 1;
171 static void tda8083_wait_diseqc_fifo (struct tda8083_state* state, int timeout)
173 unsigned long start = jiffies;
175 while (jiffies - start < timeout &&
176 !(tda8083_readreg(state, 0x02) & 0x80))
178 msleep(50);
182 static int tda8083_set_tone (struct tda8083_state* state, fe_sec_tone_mode_t tone)
184 tda8083_writereg (state, 0x26, 0xf1);
186 switch (tone) {
187 case SEC_TONE_OFF:
188 return tda8083_writereg (state, 0x29, 0x00);
189 case SEC_TONE_ON:
190 return tda8083_writereg (state, 0x29, 0x80);
191 default:
192 return -EINVAL;
196 static int tda8083_set_voltage (struct tda8083_state* state, fe_sec_voltage_t voltage)
198 switch (voltage) {
199 case SEC_VOLTAGE_13:
200 return tda8083_writereg (state, 0x20, 0x00);
201 case SEC_VOLTAGE_18:
202 return tda8083_writereg (state, 0x20, 0x11);
203 default:
204 return -EINVAL;
208 static int tda8083_send_diseqc_burst (struct tda8083_state* state, fe_sec_mini_cmd_t burst)
210 switch (burst) {
211 case SEC_MINI_A:
212 tda8083_writereg (state, 0x29, (5 << 2)); /* send burst A */
213 break;
214 case SEC_MINI_B:
215 tda8083_writereg (state, 0x29, (7 << 2)); /* send B */
216 break;
217 default:
218 return -EINVAL;
221 tda8083_wait_diseqc_fifo (state, 100);
223 return 0;
226 static int tda8083_send_diseqc_msg (struct dvb_frontend* fe,
227 struct dvb_diseqc_master_cmd *m)
229 struct tda8083_state* state = fe->demodulator_priv;
230 int i;
232 tda8083_writereg (state, 0x29, (m->msg_len - 3) | (1 << 2)); /* enable */
234 for (i=0; i<m->msg_len; i++)
235 tda8083_writereg (state, 0x23 + i, m->msg[i]);
237 tda8083_writereg (state, 0x29, (m->msg_len - 3) | (3 << 2)); /* send!! */
239 tda8083_wait_diseqc_fifo (state, 100);
241 return 0;
244 static int tda8083_read_status(struct dvb_frontend* fe, fe_status_t* status)
246 struct tda8083_state* state = fe->demodulator_priv;
248 u8 signal = ~tda8083_readreg (state, 0x01);
249 u8 sync = tda8083_readreg (state, 0x02);
251 *status = 0;
253 if (signal > 10)
254 *status |= FE_HAS_SIGNAL;
256 if (sync & 0x01)
257 *status |= FE_HAS_CARRIER;
259 if (sync & 0x02)
260 *status |= FE_HAS_VITERBI;
262 if (sync & 0x10)
263 *status |= FE_HAS_SYNC;
265 if ((sync & 0x1f) == 0x1f)
266 *status |= FE_HAS_LOCK;
268 return 0;
271 static int tda8083_read_signal_strength(struct dvb_frontend* fe, u16* strength)
273 struct tda8083_state* state = fe->demodulator_priv;
275 u8 signal = ~tda8083_readreg (state, 0x01);
276 *strength = (signal << 8) | signal;
278 return 0;
281 static int tda8083_read_snr(struct dvb_frontend* fe, u16* snr)
283 struct tda8083_state* state = fe->demodulator_priv;
285 u8 _snr = tda8083_readreg (state, 0x08);
286 *snr = (_snr << 8) | _snr;
288 return 0;
291 static int tda8083_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
293 struct tda8083_state* state = fe->demodulator_priv;
295 state->config->pll_set(fe, p);
296 tda8083_set_inversion (state, p->inversion);
297 tda8083_set_fec (state, p->u.qpsk.fec_inner);
298 tda8083_set_symbolrate (state, p->u.qpsk.symbol_rate);
300 tda8083_writereg (state, 0x00, 0x3c);
301 tda8083_writereg (state, 0x00, 0x04);
303 return 0;
306 static int tda8083_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
308 struct tda8083_state* state = fe->demodulator_priv;
310 /* FIXME: get symbolrate & frequency offset...*/
311 /*p->frequency = ???;*/
312 p->inversion = (tda8083_readreg (state, 0x0e) & 0x80) ?
313 INVERSION_ON : INVERSION_OFF;
314 p->u.qpsk.fec_inner = tda8083_get_fec (state);
315 /*p->u.qpsk.symbol_rate = tda8083_get_symbolrate (state);*/
317 return 0;
320 static int tda8083_sleep(struct dvb_frontend* fe)
322 struct tda8083_state* state = fe->demodulator_priv;
324 tda8083_writereg (state, 0x00, 0x02);
325 return 0;
328 static int tda8083_init(struct dvb_frontend* fe)
330 struct tda8083_state* state = fe->demodulator_priv;
331 int i;
333 for (i=0; i<44; i++)
334 tda8083_writereg (state, i, tda8083_init_tab[i]);
336 if (state->config->pll_init) state->config->pll_init(fe);
338 tda8083_writereg (state, 0x00, 0x3c);
339 tda8083_writereg (state, 0x00, 0x04);
341 return 0;
344 static int tda8083_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
346 struct tda8083_state* state = fe->demodulator_priv;
348 tda8083_send_diseqc_burst (state, burst);
349 tda8083_writereg (state, 0x00, 0x3c);
350 tda8083_writereg (state, 0x00, 0x04);
352 return 0;
355 static int tda8083_diseqc_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
357 struct tda8083_state* state = fe->demodulator_priv;
359 tda8083_set_tone (state, tone);
360 tda8083_writereg (state, 0x00, 0x3c);
361 tda8083_writereg (state, 0x00, 0x04);
363 return 0;
366 static int tda8083_diseqc_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
368 struct tda8083_state* state = fe->demodulator_priv;
370 tda8083_set_voltage (state, voltage);
371 tda8083_writereg (state, 0x00, 0x3c);
372 tda8083_writereg (state, 0x00, 0x04);
374 return 0;
377 static void tda8083_release(struct dvb_frontend* fe)
379 struct tda8083_state* state = fe->demodulator_priv;
380 kfree(state);
383 static struct dvb_frontend_ops tda8083_ops;
385 struct dvb_frontend* tda8083_attach(const struct tda8083_config* config,
386 struct i2c_adapter* i2c)
388 struct tda8083_state* state = NULL;
390 /* allocate memory for the internal state */
391 state = kmalloc(sizeof(struct tda8083_state), GFP_KERNEL);
392 if (state == NULL) goto error;
394 /* setup the state */
395 state->config = config;
396 state->i2c = i2c;
397 memcpy(&state->ops, &tda8083_ops, sizeof(struct dvb_frontend_ops));
399 /* check if the demod is there */
400 if ((tda8083_readreg(state, 0x00)) != 0x05) goto error;
402 /* create dvb_frontend */
403 state->frontend.ops = &state->ops;
404 state->frontend.demodulator_priv = state;
405 return &state->frontend;
407 error:
408 kfree(state);
409 return NULL;
412 static struct dvb_frontend_ops tda8083_ops = {
414 .info = {
415 .name = "Philips TDA8083 DVB-S",
416 .type = FE_QPSK,
417 .frequency_min = 950000, /* FIXME: guessed! */
418 .frequency_max = 1400000, /* FIXME: guessed! */
419 .frequency_stepsize = 125, /* kHz for QPSK frontends */
420 /* .frequency_tolerance = ???,*/
421 .symbol_rate_min = 1000000, /* FIXME: guessed! */
422 .symbol_rate_max = 45000000, /* FIXME: guessed! */
423 /* .symbol_rate_tolerance = ???,*/
424 .caps = FE_CAN_INVERSION_AUTO |
425 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
426 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
427 FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
428 FE_CAN_QPSK | FE_CAN_MUTE_TS
431 .release = tda8083_release,
433 .init = tda8083_init,
434 .sleep = tda8083_sleep,
436 .set_frontend = tda8083_set_frontend,
437 .get_frontend = tda8083_get_frontend,
439 .read_status = tda8083_read_status,
440 .read_signal_strength = tda8083_read_signal_strength,
441 .read_snr = tda8083_read_snr,
443 .diseqc_send_master_cmd = tda8083_send_diseqc_msg,
444 .diseqc_send_burst = tda8083_diseqc_send_burst,
445 .set_tone = tda8083_diseqc_set_tone,
446 .set_voltage = tda8083_diseqc_set_voltage,
449 module_param(debug, int, 0644);
450 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
452 MODULE_DESCRIPTION("Philips TDA8083 DVB-S Demodulator");
453 MODULE_AUTHOR("Ralph Metzler, Holger Waechtler");
454 MODULE_LICENSE("GPL");
456 EXPORT_SYMBOL(tda8083_attach);