blob: 0f041e873781d6f8f09cd276905a6ffd6cc01e39 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080033#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "dvb_frontend.h"
35#include "tda8083.h"
36
37
38struct tda8083_state {
39 struct i2c_adapter* i2c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 /* configuration settings */
41 const struct tda8083_config* config;
42 struct dvb_frontend frontend;
43};
44
45static int debug;
46#define dprintk(args...) \
47 do { \
48 if (debug) printk(KERN_DEBUG "tda8083: " args); \
49 } while (0)
50
51
52static 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
59};
60
61
62static int tda8083_writereg (struct tda8083_state* state, u8 reg, u8 data)
63{
64 int ret;
65 u8 buf [] = { reg, data };
66 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
67
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080068 ret = i2c_transfer(state->i2c, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080070 if (ret != 1)
71 dprintk ("%s: writereg error (reg %02x, ret == %i)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 __FUNCTION__, reg, ret);
73
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080074 return (ret != 1) ? -1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77static int tda8083_readregs (struct tda8083_state* state, u8 reg1, u8 *b, u8 len)
78{
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 } };
82
83 ret = i2c_transfer(state->i2c, msg, 2);
84
85 if (ret != 2)
86 dprintk ("%s: readreg error (reg %02x, ret == %i)\n",
87 __FUNCTION__, reg1, ret);
88
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080089 return ret == 2 ? 0 : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92static inline u8 tda8083_readreg (struct tda8083_state* state, u8 reg)
93{
94 u8 val;
95
96 tda8083_readregs (state, reg, &val, 1);
97
98 return val;
99}
100
101static int tda8083_set_inversion (struct tda8083_state* state, fe_spectral_inversion_t inversion)
102{
103 /* XXX FIXME: implement other modes than FEC_AUTO */
104 if (inversion == INVERSION_AUTO)
105 return 0;
106
107 return -EINVAL;
108}
109
110static int tda8083_set_fec (struct tda8083_state* state, fe_code_rate_t fec)
111{
112 if (fec == FEC_AUTO)
113 return tda8083_writereg (state, 0x07, 0xff);
114
115 if (fec >= FEC_1_2 && fec <= FEC_8_9)
116 return tda8083_writereg (state, 0x07, 1 << (FEC_8_9 - fec));
117
118 return -EINVAL;
119}
120
121static fe_code_rate_t tda8083_get_fec (struct tda8083_state* state)
122{
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 };
126
127 index = tda8083_readreg(state, 0x0e) & 0x07;
128
129 return fec_tab [index];
130}
131
132static int tda8083_set_symbolrate (struct tda8083_state* state, u32 srate)
133{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800134 u32 ratio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 u32 tmp;
136 u8 filter;
137
138 if (srate > 32000000)
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800139 srate = 32000000;
140 if (srate < 500000)
141 srate = 500000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 filter = 0;
144 if (srate < 24000000)
145 filter = 2;
146 if (srate < 16000000)
147 filter = 3;
148
149 tmp = 31250 << 16;
150 ratio = tmp / srate;
151
152 tmp = (tmp % srate) << 8;
153 ratio = (ratio << 8) + tmp / srate;
154
155 tmp = (tmp % srate) << 8;
156 ratio = (ratio << 8) + tmp / srate;
157
158 dprintk("tda8083: ratio == %08x\n", (unsigned int) ratio);
159
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);
164
165 tda8083_writereg (state, 0x00, 0x3c);
166 tda8083_writereg (state, 0x00, 0x04);
167
168 return 1;
169}
170
171static void tda8083_wait_diseqc_fifo (struct tda8083_state* state, int timeout)
172{
173 unsigned long start = jiffies;
174
175 while (jiffies - start < timeout &&
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800176 !(tda8083_readreg(state, 0x02) & 0x80))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 {
178 msleep(50);
179 };
180}
181
182static int tda8083_set_tone (struct tda8083_state* state, fe_sec_tone_mode_t tone)
183{
184 tda8083_writereg (state, 0x26, 0xf1);
185
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;
193 };
194}
195
196static int tda8083_set_voltage (struct tda8083_state* state, fe_sec_voltage_t voltage)
197{
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;
205 };
206}
207
208static int tda8083_send_diseqc_burst (struct tda8083_state* state, fe_sec_mini_cmd_t burst)
209{
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;
219 };
220
221 tda8083_wait_diseqc_fifo (state, 100);
222
223 return 0;
224}
225
226static int tda8083_send_diseqc_msg (struct dvb_frontend* fe,
227 struct dvb_diseqc_master_cmd *m)
228{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700229 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 int i;
231
232 tda8083_writereg (state, 0x29, (m->msg_len - 3) | (1 << 2)); /* enable */
233
234 for (i=0; i<m->msg_len; i++)
235 tda8083_writereg (state, 0x23 + i, m->msg[i]);
236
237 tda8083_writereg (state, 0x29, (m->msg_len - 3) | (3 << 2)); /* send!! */
238
239 tda8083_wait_diseqc_fifo (state, 100);
240
241 return 0;
242}
243
244static int tda8083_read_status(struct dvb_frontend* fe, fe_status_t* status)
245{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700246 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 u8 signal = ~tda8083_readreg (state, 0x01);
249 u8 sync = tda8083_readreg (state, 0x02);
250
251 *status = 0;
252
253 if (signal > 10)
254 *status |= FE_HAS_SIGNAL;
255
256 if (sync & 0x01)
257 *status |= FE_HAS_CARRIER;
258
259 if (sync & 0x02)
260 *status |= FE_HAS_VITERBI;
261
262 if (sync & 0x10)
263 *status |= FE_HAS_SYNC;
264
Christoph Haubrichdbb2e632006-10-31 00:29:30 -0300265 if (sync & 0x20) /* frontend can not lock */
266 *status |= FE_TIMEDOUT;
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if ((sync & 0x1f) == 0x1f)
269 *status |= FE_HAS_LOCK;
270
271 return 0;
272}
273
Christoph Haubrichdbb2e632006-10-31 00:29:30 -0300274static int tda8083_read_ber(struct dvb_frontend* fe, u32* ber)
275{
276 struct tda8083_state* state = fe->demodulator_priv;
277 int ret;
278 u8 buf[3];
279
280 if ((ret = tda8083_readregs(state, 0x0b, buf, sizeof(buf))))
281 return ret;
282
283 *ber = ((buf[0] & 0x1f) << 16) | (buf[1] << 8) | buf[2];
284
285 return 0;
286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288static int tda8083_read_signal_strength(struct dvb_frontend* fe, u16* strength)
289{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700290 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 u8 signal = ~tda8083_readreg (state, 0x01);
293 *strength = (signal << 8) | signal;
294
295 return 0;
296}
297
298static int tda8083_read_snr(struct dvb_frontend* fe, u16* snr)
299{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700300 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 u8 _snr = tda8083_readreg (state, 0x08);
303 *snr = (_snr << 8) | _snr;
304
305 return 0;
306}
307
Christoph Haubrichdbb2e632006-10-31 00:29:30 -0300308static int tda8083_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
309{
310 struct tda8083_state* state = fe->demodulator_priv;
311
312 *ucblocks = tda8083_readreg(state, 0x0f);
313 if (*ucblocks == 0xff)
314 *ucblocks = 0xffffffff;
315
316 return 0;
317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319static int tda8083_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
320{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700321 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Patrick Boettcherdea74862006-05-14 05:01:31 -0300323 if (fe->ops.tuner_ops.set_params) {
324 fe->ops.tuner_ops.set_params(fe, p);
325 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quinceyd21eac02006-04-18 17:47:09 -0300326 }
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 tda8083_set_inversion (state, p->inversion);
329 tda8083_set_fec (state, p->u.qpsk.fec_inner);
330 tda8083_set_symbolrate (state, p->u.qpsk.symbol_rate);
331
332 tda8083_writereg (state, 0x00, 0x3c);
333 tda8083_writereg (state, 0x00, 0x04);
334
335 return 0;
336}
337
338static int tda8083_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
339{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700340 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* FIXME: get symbolrate & frequency offset...*/
343 /*p->frequency = ???;*/
344 p->inversion = (tda8083_readreg (state, 0x0e) & 0x80) ?
345 INVERSION_ON : INVERSION_OFF;
346 p->u.qpsk.fec_inner = tda8083_get_fec (state);
347 /*p->u.qpsk.symbol_rate = tda8083_get_symbolrate (state);*/
348
349 return 0;
350}
351
352static int tda8083_sleep(struct dvb_frontend* fe)
353{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700354 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 tda8083_writereg (state, 0x00, 0x02);
357 return 0;
358}
359
360static int tda8083_init(struct dvb_frontend* fe)
361{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700362 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 int i;
364
365 for (i=0; i<44; i++)
366 tda8083_writereg (state, i, tda8083_init_tab[i]);
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 tda8083_writereg (state, 0x00, 0x3c);
369 tda8083_writereg (state, 0x00, 0x04);
370
371 return 0;
372}
373
374static int tda8083_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
375{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700376 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 tda8083_send_diseqc_burst (state, burst);
379 tda8083_writereg (state, 0x00, 0x3c);
380 tda8083_writereg (state, 0x00, 0x04);
381
382 return 0;
383}
384
385static int tda8083_diseqc_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
386{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700387 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 tda8083_set_tone (state, tone);
390 tda8083_writereg (state, 0x00, 0x3c);
391 tda8083_writereg (state, 0x00, 0x04);
392
393 return 0;
394}
395
396static int tda8083_diseqc_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
397{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700398 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 tda8083_set_voltage (state, voltage);
401 tda8083_writereg (state, 0x00, 0x3c);
402 tda8083_writereg (state, 0x00, 0x04);
403
404 return 0;
405}
406
407static void tda8083_release(struct dvb_frontend* fe)
408{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700409 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 kfree(state);
411}
412
413static struct dvb_frontend_ops tda8083_ops;
414
415struct dvb_frontend* tda8083_attach(const struct tda8083_config* config,
416 struct i2c_adapter* i2c)
417{
418 struct tda8083_state* state = NULL;
419
420 /* allocate memory for the internal state */
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700421 state = kmalloc(sizeof(struct tda8083_state), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (state == NULL) goto error;
423
424 /* setup the state */
425 state->config = config;
426 state->i2c = i2c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /* check if the demod is there */
429 if ((tda8083_readreg(state, 0x00)) != 0x05) goto error;
430
431 /* create dvb_frontend */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300432 memcpy(&state->frontend.ops, &tda8083_ops, sizeof(struct dvb_frontend_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 state->frontend.demodulator_priv = state;
434 return &state->frontend;
435
436error:
437 kfree(state);
438 return NULL;
439}
440
441static struct dvb_frontend_ops tda8083_ops = {
442
443 .info = {
444 .name = "Philips TDA8083 DVB-S",
445 .type = FE_QPSK,
Oliver Endriss4f76b672007-08-06 13:59:19 -0300446 .frequency_min = 920000, /* TDA8060 */
447 .frequency_max = 2200000, /* TDA8060 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 .frequency_stepsize = 125, /* kHz for QPSK frontends */
449 /* .frequency_tolerance = ???,*/
Oliver Endriss4f76b672007-08-06 13:59:19 -0300450 .symbol_rate_min = 12000000,
451 .symbol_rate_max = 30000000,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /* .symbol_rate_tolerance = ???,*/
453 .caps = FE_CAN_INVERSION_AUTO |
454 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
455 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
456 FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
457 FE_CAN_QPSK | FE_CAN_MUTE_TS
458 },
459
460 .release = tda8083_release,
461
462 .init = tda8083_init,
463 .sleep = tda8083_sleep,
464
465 .set_frontend = tda8083_set_frontend,
466 .get_frontend = tda8083_get_frontend,
467
468 .read_status = tda8083_read_status,
469 .read_signal_strength = tda8083_read_signal_strength,
470 .read_snr = tda8083_read_snr,
Christoph Haubrichdbb2e632006-10-31 00:29:30 -0300471 .read_ber = tda8083_read_ber,
472 .read_ucblocks = tda8083_read_ucblocks,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 .diseqc_send_master_cmd = tda8083_send_diseqc_msg,
475 .diseqc_send_burst = tda8083_diseqc_send_burst,
476 .set_tone = tda8083_diseqc_set_tone,
477 .set_voltage = tda8083_diseqc_set_voltage,
478};
479
480module_param(debug, int, 0644);
481MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
482
483MODULE_DESCRIPTION("Philips TDA8083 DVB-S Demodulator");
484MODULE_AUTHOR("Ralph Metzler, Holger Waechtler");
485MODULE_LICENSE("GPL");
486
487EXPORT_SYMBOL(tda8083_attach);