This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / media / dvb / frontends / tda8083.c
1 /*
2     Driver for Philips TDA8083 based QPSK Demodulator
3
4     Copyright (C) 2001 Convergence Integrated Media GmbH
5
6     written by Ralph Metzler <ralph@convergence.de>
7
8     adoption to the new DVB frontend API and diagnostic ioctl's
9     by Holger Waechtler <holger@convergence.de>
10
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.
15
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.
20
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.
24
25 */
26
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"
35
36
37 struct tda8083_state {
38
39         struct i2c_adapter* i2c;
40
41         struct dvb_frontend_ops ops;
42
43         /* configuration settings */
44         const struct tda8083_config* config;
45
46         struct dvb_frontend frontend;
47 };
48
49 static int debug;
50 #define dprintk(args...) \
51         do { \
52                 if (debug) printk(KERN_DEBUG "tda8083: " args); \
53         } while (0)
54
55
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
63 };
64
65
66 static int tda8083_writereg (struct tda8083_state* state, u8 reg, u8 data)
67 {
68         int ret;
69         u8 buf [] = { reg, data };
70         struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
71
72         ret = i2c_transfer(state->i2c, &msg, 1);
73
74         if (ret != 1)
75                 dprintk ("%s: writereg error (reg %02x, ret == %i)\n",
76                         __FUNCTION__, reg, ret);
77
78         return (ret != 1) ? -1 : 0;
79 }
80
81
82 static int tda8083_readregs (struct tda8083_state* state, u8 reg1, u8 *b, u8 len)
83 {
84         int ret;
85         struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = &reg1, .len = 1 },
86                            { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };
87
88         ret = i2c_transfer(state->i2c, msg, 2);
89
90         if (ret != 2)
91                 dprintk ("%s: readreg error (reg %02x, ret == %i)\n",
92                         __FUNCTION__, reg1, ret);
93
94         return ret == 2 ? 0 : -1;
95 }
96
97
98 static inline u8 tda8083_readreg (struct tda8083_state* state, u8 reg)
99 {
100         u8 val;
101
102         tda8083_readregs (state, reg, &val, 1);
103
104         return val;
105 }
106
107
108
109 static int tda8083_set_inversion (struct tda8083_state* state, fe_spectral_inversion_t inversion)
110 {
111         /*  XXX FIXME: implement other modes than FEC_AUTO */
112         if (inversion == INVERSION_AUTO)
113                 return 0;
114
115         return -EINVAL;
116 }
117
118
119 static int tda8083_set_fec (struct tda8083_state* state, fe_code_rate_t fec)
120 {
121         if (fec == FEC_AUTO)
122                 return tda8083_writereg (state, 0x07, 0xff);
123
124         if (fec >= FEC_1_2 && fec <= FEC_8_9)
125                 return tda8083_writereg (state, 0x07, 1 << (FEC_8_9 - fec));
126
127         return -EINVAL;
128 }
129
130
131 static fe_code_rate_t tda8083_get_fec (struct tda8083_state* state)
132 {
133         u8 index;
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 };
136
137         index = tda8083_readreg(state, 0x0e) & 0x07;
138
139         return fec_tab [index];
140 }
141
142
143 static int tda8083_set_symbolrate (struct tda8083_state* state, u32 srate)
144 {
145         u32 ratio;
146         u32 tmp;
147         u8 filter;
148
149         if (srate > 32000000)
150                 srate = 32000000;
151         if (srate < 500000)
152                 srate = 500000;
153
154         filter = 0;
155         if (srate < 24000000)
156                 filter = 2;
157         if (srate < 16000000)
158                 filter = 3;
159
160         tmp = 31250 << 16;
161         ratio = tmp / srate;
162
163         tmp = (tmp % srate) << 8;
164         ratio = (ratio << 8) + tmp / srate;
165
166         tmp = (tmp % srate) << 8;
167         ratio = (ratio << 8) + tmp / srate;
168
169         dprintk("tda8083: ratio == %08x\n", (unsigned int) ratio);
170
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);
175
176         tda8083_writereg (state, 0x00, 0x3c);
177         tda8083_writereg (state, 0x00, 0x04);
178
179         return 1;
180 }
181
182
183 static void tda8083_wait_diseqc_fifo (struct tda8083_state* state, int timeout)
184 {
185         unsigned long start = jiffies;
186
187         while (jiffies - start < timeout &&
188                !(tda8083_readreg(state, 0x02) & 0x80))
189         {
190                 msleep(50);
191         };
192 }
193
194 static int tda8083_set_tone (struct tda8083_state* state, fe_sec_tone_mode_t tone)
195 {
196         tda8083_writereg (state, 0x26, 0xf1);
197
198         switch (tone) {
199         case SEC_TONE_OFF:
200                 return tda8083_writereg (state, 0x29, 0x00);
201         case SEC_TONE_ON:
202                 return tda8083_writereg (state, 0x29, 0x80);
203         default:
204                 return -EINVAL;
205         };
206 }
207
208
209 static int tda8083_set_voltage (struct tda8083_state* state, fe_sec_voltage_t voltage)
210 {
211         switch (voltage) {
212         case SEC_VOLTAGE_13:
213                 return tda8083_writereg (state, 0x20, 0x00);
214         case SEC_VOLTAGE_18:
215                 return tda8083_writereg (state, 0x20, 0x11);
216         default:
217                 return -EINVAL;
218         };
219 }
220
221 static int tda8083_send_diseqc_burst (struct tda8083_state* state, fe_sec_mini_cmd_t burst)
222 {
223         switch (burst) {
224         case SEC_MINI_A:
225                 tda8083_writereg (state, 0x29, (5 << 2));  /* send burst A */
226                 break;
227         case SEC_MINI_B:
228                 tda8083_writereg (state, 0x29, (7 << 2));  /* send B */
229                 break;
230         default:
231                 return -EINVAL;
232         };
233
234         tda8083_wait_diseqc_fifo (state, 100);
235
236         return 0;
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260 static int tda8083_send_diseqc_msg (struct dvb_frontend* fe,
261                                     struct dvb_diseqc_master_cmd *m)
262 {
263         struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
264         int i;
265
266         tda8083_writereg (state, 0x29, (m->msg_len - 3) | (1 << 2)); /* enable */
267
268         for (i=0; i<m->msg_len; i++)
269                 tda8083_writereg (state, 0x23 + i, m->msg[i]);
270
271         tda8083_writereg (state, 0x29, (m->msg_len - 3) | (3 << 2)); /* send!! */
272
273         tda8083_wait_diseqc_fifo (state, 100);
274
275         return 0;
276 }
277
278 static int tda8083_read_status(struct dvb_frontend* fe, fe_status_t* status)
279 {
280         struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
281
282         u8 signal = ~tda8083_readreg (state, 0x01);
283         u8 sync = tda8083_readreg (state, 0x02);
284
285         *status = 0;
286
287         if (signal > 10)
288                 *status |= FE_HAS_SIGNAL;
289
290         if (sync & 0x01)
291                 *status |= FE_HAS_CARRIER;
292
293         if (sync & 0x02)
294                 *status |= FE_HAS_VITERBI;
295
296         if (sync & 0x10)
297                 *status |= FE_HAS_SYNC;
298
299         if ((sync & 0x1f) == 0x1f)
300                 *status |= FE_HAS_LOCK;
301
302         return 0;
303 }
304
305 static int tda8083_read_signal_strength(struct dvb_frontend* fe, u16* strength)
306 {
307         struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
308
309         u8 signal = ~tda8083_readreg (state, 0x01);
310         *strength = (signal << 8) | signal;
311
312         return 0;
313 }
314
315 static int tda8083_read_snr(struct dvb_frontend* fe, u16* snr)
316 {
317         struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
318
319         u8 _snr = tda8083_readreg (state, 0x08);
320         *snr = (_snr << 8) | _snr;
321
322         return 0;
323 }
324
325 static int tda8083_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
326 {
327         struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
328
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);
333
334         tda8083_writereg (state, 0x00, 0x3c);
335         tda8083_writereg (state, 0x00, 0x04);
336
337         return 0;
338 }
339
340 static int tda8083_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
341 {
342         struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
343
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);*/
350
351         return 0;
352 }
353
354 static int tda8083_sleep(struct dvb_frontend* fe)
355 {
356         struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
357
358         tda8083_writereg (state, 0x00, 0x02);
359         return 0;
360 }
361
362 static int tda8083_init(struct dvb_frontend* fe)
363 {
364         struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
365         int i;
366
367         for (i=0; i<44; i++)
368                 tda8083_writereg (state, i, tda8083_init_tab[i]);
369
370         if (state->config->pll_init) state->config->pll_init(fe);
371
372         tda8083_writereg (state, 0x00, 0x3c);
373         tda8083_writereg (state, 0x00, 0x04);
374
375         return 0;
376 }
377
378 static int tda8083_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
379 {
380         struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
381
382         tda8083_send_diseqc_burst (state, burst);
383         tda8083_writereg (state, 0x00, 0x3c);
384         tda8083_writereg (state, 0x00, 0x04);
385
386         return 0;
387 }
388
389 static int tda8083_diseqc_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
390 {
391         struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
392
393         tda8083_set_tone (state, tone);
394         tda8083_writereg (state, 0x00, 0x3c);
395         tda8083_writereg (state, 0x00, 0x04);
396
397         return 0;
398 }
399
400 static int tda8083_diseqc_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
401 {
402         struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
403
404         tda8083_set_voltage (state, voltage);
405         tda8083_writereg (state, 0x00, 0x3c);
406         tda8083_writereg (state, 0x00, 0x04);
407
408         return 0;
409 }
410
411 static void tda8083_release(struct dvb_frontend* fe)
412 {
413         struct tda8083_state* state = (struct tda8083_state*) fe->demodulator_priv;
414         kfree(state);
415 }
416
417 static struct dvb_frontend_ops tda8083_ops;
418
419 struct dvb_frontend* tda8083_attach(const struct tda8083_config* config,
420                                     struct i2c_adapter* i2c)
421 {
422         struct tda8083_state* state = NULL;
423
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;
427
428         /* setup the state */
429         state->config = config;
430         state->i2c = i2c;
431         memcpy(&state->ops, &tda8083_ops, sizeof(struct dvb_frontend_ops));
432
433         /* check if the demod is there */
434         if ((tda8083_readreg(state, 0x00)) != 0x05) goto error;
435
436         /* create dvb_frontend */
437         state->frontend.ops = &state->ops;
438         state->frontend.demodulator_priv = state;
439         return &state->frontend;
440
441 error:
442         if (state) kfree(state);
443         return NULL;
444 }
445
446 static struct dvb_frontend_ops tda8083_ops = {
447
448         .info = {
449                 .name                   = "Philips TDA8083 DVB-S",
450                 .type                   = FE_QPSK,
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
463         },
464
465         .release = tda8083_release,
466
467         .init = tda8083_init,
468         .sleep = tda8083_sleep,
469
470         .set_frontend = tda8083_set_frontend,
471         .get_frontend = tda8083_get_frontend,
472
473         .read_status = tda8083_read_status,
474         .read_signal_strength = tda8083_read_signal_strength,
475         .read_snr = tda8083_read_snr,
476
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,
481 };
482
483 module_param(debug, int, 0644);
484 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
485
486 MODULE_DESCRIPTION("Philips TDA8083 DVB-S Demodulator");
487 MODULE_AUTHOR("Ralph Metzler, Holger Waechtler");
488 MODULE_LICENSE("GPL");
489
490 EXPORT_SYMBOL(tda8083_attach);