blob: c05cf1861051b3271effd7136625e5cc6b3cd543 [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;
40 struct dvb_frontend_ops ops;
41 /* configuration settings */
42 const struct tda8083_config* config;
43 struct dvb_frontend frontend;
44};
45
46static int debug;
47#define dprintk(args...) \
48 do { \
49 if (debug) printk(KERN_DEBUG "tda8083: " args); \
50 } while (0)
51
52
53static u8 tda8083_init_tab [] = {
54 0x04, 0x00, 0x4a, 0x79, 0x04, 0x00, 0xff, 0xea,
55 0x48, 0x42, 0x79, 0x60, 0x70, 0x52, 0x9a, 0x10,
56 0x0e, 0x10, 0xf2, 0xa7, 0x93, 0x0b, 0x05, 0xc8,
57 0x9d, 0x00, 0x42, 0x80, 0x00, 0x60, 0x40, 0x00,
58 0x00, 0x75, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00,
59 0x00, 0x00, 0x00, 0x00
60};
61
62
63static int tda8083_writereg (struct tda8083_state* state, u8 reg, u8 data)
64{
65 int ret;
66 u8 buf [] = { reg, data };
67 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
68
69 ret = i2c_transfer(state->i2c, &msg, 1);
70
71 if (ret != 1)
72 dprintk ("%s: writereg error (reg %02x, ret == %i)\n",
73 __FUNCTION__, reg, ret);
74
75 return (ret != 1) ? -1 : 0;
76}
77
78static int tda8083_readregs (struct tda8083_state* state, u8 reg1, u8 *b, u8 len)
79{
80 int ret;
81 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = &reg1, .len = 1 },
82 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };
83
84 ret = i2c_transfer(state->i2c, msg, 2);
85
86 if (ret != 2)
87 dprintk ("%s: readreg error (reg %02x, ret == %i)\n",
88 __FUNCTION__, reg1, ret);
89
90 return ret == 2 ? 0 : -1;
91}
92
93static inline u8 tda8083_readreg (struct tda8083_state* state, u8 reg)
94{
95 u8 val;
96
97 tda8083_readregs (state, reg, &val, 1);
98
99 return val;
100}
101
102static int tda8083_set_inversion (struct tda8083_state* state, fe_spectral_inversion_t inversion)
103{
104 /* XXX FIXME: implement other modes than FEC_AUTO */
105 if (inversion == INVERSION_AUTO)
106 return 0;
107
108 return -EINVAL;
109}
110
111static int tda8083_set_fec (struct tda8083_state* state, fe_code_rate_t fec)
112{
113 if (fec == FEC_AUTO)
114 return tda8083_writereg (state, 0x07, 0xff);
115
116 if (fec >= FEC_1_2 && fec <= FEC_8_9)
117 return tda8083_writereg (state, 0x07, 1 << (FEC_8_9 - fec));
118
119 return -EINVAL;
120}
121
122static fe_code_rate_t tda8083_get_fec (struct tda8083_state* state)
123{
124 u8 index;
125 static fe_code_rate_t fec_tab [] = { FEC_8_9, FEC_1_2, FEC_2_3, FEC_3_4,
126 FEC_4_5, FEC_5_6, FEC_6_7, FEC_7_8 };
127
128 index = tda8083_readreg(state, 0x0e) & 0x07;
129
130 return fec_tab [index];
131}
132
133static int tda8083_set_symbolrate (struct tda8083_state* state, u32 srate)
134{
135 u32 ratio;
136 u32 tmp;
137 u8 filter;
138
139 if (srate > 32000000)
140 srate = 32000000;
141 if (srate < 500000)
142 srate = 500000;
143
144 filter = 0;
145 if (srate < 24000000)
146 filter = 2;
147 if (srate < 16000000)
148 filter = 3;
149
150 tmp = 31250 << 16;
151 ratio = tmp / srate;
152
153 tmp = (tmp % srate) << 8;
154 ratio = (ratio << 8) + tmp / srate;
155
156 tmp = (tmp % srate) << 8;
157 ratio = (ratio << 8) + tmp / srate;
158
159 dprintk("tda8083: ratio == %08x\n", (unsigned int) ratio);
160
161 tda8083_writereg (state, 0x05, filter);
162 tda8083_writereg (state, 0x02, (ratio >> 16) & 0xff);
163 tda8083_writereg (state, 0x03, (ratio >> 8) & 0xff);
164 tda8083_writereg (state, 0x04, (ratio ) & 0xff);
165
166 tda8083_writereg (state, 0x00, 0x3c);
167 tda8083_writereg (state, 0x00, 0x04);
168
169 return 1;
170}
171
172static void tda8083_wait_diseqc_fifo (struct tda8083_state* state, int timeout)
173{
174 unsigned long start = jiffies;
175
176 while (jiffies - start < timeout &&
177 !(tda8083_readreg(state, 0x02) & 0x80))
178 {
179 msleep(50);
180 };
181}
182
183static int tda8083_set_tone (struct tda8083_state* state, fe_sec_tone_mode_t tone)
184{
185 tda8083_writereg (state, 0x26, 0xf1);
186
187 switch (tone) {
188 case SEC_TONE_OFF:
189 return tda8083_writereg (state, 0x29, 0x00);
190 case SEC_TONE_ON:
191 return tda8083_writereg (state, 0x29, 0x80);
192 default:
193 return -EINVAL;
194 };
195}
196
197static int tda8083_set_voltage (struct tda8083_state* state, fe_sec_voltage_t voltage)
198{
199 switch (voltage) {
200 case SEC_VOLTAGE_13:
201 return tda8083_writereg (state, 0x20, 0x00);
202 case SEC_VOLTAGE_18:
203 return tda8083_writereg (state, 0x20, 0x11);
204 default:
205 return -EINVAL;
206 };
207}
208
209static int tda8083_send_diseqc_burst (struct tda8083_state* state, fe_sec_mini_cmd_t burst)
210{
211 switch (burst) {
212 case SEC_MINI_A:
213 tda8083_writereg (state, 0x29, (5 << 2)); /* send burst A */
214 break;
215 case SEC_MINI_B:
216 tda8083_writereg (state, 0x29, (7 << 2)); /* send B */
217 break;
218 default:
219 return -EINVAL;
220 };
221
222 tda8083_wait_diseqc_fifo (state, 100);
223
224 return 0;
225}
226
227static int tda8083_send_diseqc_msg (struct dvb_frontend* fe,
228 struct dvb_diseqc_master_cmd *m)
229{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700230 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int i;
232
233 tda8083_writereg (state, 0x29, (m->msg_len - 3) | (1 << 2)); /* enable */
234
235 for (i=0; i<m->msg_len; i++)
236 tda8083_writereg (state, 0x23 + i, m->msg[i]);
237
238 tda8083_writereg (state, 0x29, (m->msg_len - 3) | (3 << 2)); /* send!! */
239
240 tda8083_wait_diseqc_fifo (state, 100);
241
242 return 0;
243}
244
245static int tda8083_read_status(struct dvb_frontend* fe, fe_status_t* status)
246{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700247 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 u8 signal = ~tda8083_readreg (state, 0x01);
250 u8 sync = tda8083_readreg (state, 0x02);
251
252 *status = 0;
253
254 if (signal > 10)
255 *status |= FE_HAS_SIGNAL;
256
257 if (sync & 0x01)
258 *status |= FE_HAS_CARRIER;
259
260 if (sync & 0x02)
261 *status |= FE_HAS_VITERBI;
262
263 if (sync & 0x10)
264 *status |= FE_HAS_SYNC;
265
266 if ((sync & 0x1f) == 0x1f)
267 *status |= FE_HAS_LOCK;
268
269 return 0;
270}
271
272static int tda8083_read_signal_strength(struct dvb_frontend* fe, u16* strength)
273{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700274 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 u8 signal = ~tda8083_readreg (state, 0x01);
277 *strength = (signal << 8) | signal;
278
279 return 0;
280}
281
282static int tda8083_read_snr(struct dvb_frontend* fe, u16* snr)
283{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700284 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 u8 _snr = tda8083_readreg (state, 0x08);
287 *snr = (_snr << 8) | _snr;
288
289 return 0;
290}
291
292static int tda8083_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
293{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700294 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 state->config->pll_set(fe, p);
297 tda8083_set_inversion (state, p->inversion);
298 tda8083_set_fec (state, p->u.qpsk.fec_inner);
299 tda8083_set_symbolrate (state, p->u.qpsk.symbol_rate);
300
301 tda8083_writereg (state, 0x00, 0x3c);
302 tda8083_writereg (state, 0x00, 0x04);
303
304 return 0;
305}
306
307static int tda8083_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
308{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700309 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* FIXME: get symbolrate & frequency offset...*/
312 /*p->frequency = ???;*/
313 p->inversion = (tda8083_readreg (state, 0x0e) & 0x80) ?
314 INVERSION_ON : INVERSION_OFF;
315 p->u.qpsk.fec_inner = tda8083_get_fec (state);
316 /*p->u.qpsk.symbol_rate = tda8083_get_symbolrate (state);*/
317
318 return 0;
319}
320
321static int tda8083_sleep(struct dvb_frontend* fe)
322{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700323 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 tda8083_writereg (state, 0x00, 0x02);
326 return 0;
327}
328
329static int tda8083_init(struct dvb_frontend* fe)
330{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700331 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 int i;
333
334 for (i=0; i<44; i++)
335 tda8083_writereg (state, i, tda8083_init_tab[i]);
336
337 if (state->config->pll_init) state->config->pll_init(fe);
338
339 tda8083_writereg (state, 0x00, 0x3c);
340 tda8083_writereg (state, 0x00, 0x04);
341
342 return 0;
343}
344
345static int tda8083_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
346{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700347 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 tda8083_send_diseqc_burst (state, burst);
350 tda8083_writereg (state, 0x00, 0x3c);
351 tda8083_writereg (state, 0x00, 0x04);
352
353 return 0;
354}
355
356static int tda8083_diseqc_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
357{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700358 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 tda8083_set_tone (state, tone);
361 tda8083_writereg (state, 0x00, 0x3c);
362 tda8083_writereg (state, 0x00, 0x04);
363
364 return 0;
365}
366
367static int tda8083_diseqc_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
368{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700369 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 tda8083_set_voltage (state, voltage);
372 tda8083_writereg (state, 0x00, 0x3c);
373 tda8083_writereg (state, 0x00, 0x04);
374
375 return 0;
376}
377
378static void tda8083_release(struct dvb_frontend* fe)
379{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700380 struct tda8083_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 kfree(state);
382}
383
384static struct dvb_frontend_ops tda8083_ops;
385
386struct dvb_frontend* tda8083_attach(const struct tda8083_config* config,
387 struct i2c_adapter* i2c)
388{
389 struct tda8083_state* state = NULL;
390
391 /* allocate memory for the internal state */
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700392 state = kmalloc(sizeof(struct tda8083_state), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (state == NULL) goto error;
394
395 /* setup the state */
396 state->config = config;
397 state->i2c = i2c;
398 memcpy(&state->ops, &tda8083_ops, sizeof(struct dvb_frontend_ops));
399
400 /* check if the demod is there */
401 if ((tda8083_readreg(state, 0x00)) != 0x05) goto error;
402
403 /* create dvb_frontend */
404 state->frontend.ops = &state->ops;
405 state->frontend.demodulator_priv = state;
406 return &state->frontend;
407
408error:
409 kfree(state);
410 return NULL;
411}
412
413static struct dvb_frontend_ops tda8083_ops = {
414
415 .info = {
416 .name = "Philips TDA8083 DVB-S",
417 .type = FE_QPSK,
418 .frequency_min = 950000, /* FIXME: guessed! */
419 .frequency_max = 1400000, /* FIXME: guessed! */
420 .frequency_stepsize = 125, /* kHz for QPSK frontends */
421 /* .frequency_tolerance = ???,*/
422 .symbol_rate_min = 1000000, /* FIXME: guessed! */
423 .symbol_rate_max = 45000000, /* FIXME: guessed! */
424 /* .symbol_rate_tolerance = ???,*/
425 .caps = FE_CAN_INVERSION_AUTO |
426 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
427 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
428 FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
429 FE_CAN_QPSK | FE_CAN_MUTE_TS
430 },
431
432 .release = tda8083_release,
433
434 .init = tda8083_init,
435 .sleep = tda8083_sleep,
436
437 .set_frontend = tda8083_set_frontend,
438 .get_frontend = tda8083_get_frontend,
439
440 .read_status = tda8083_read_status,
441 .read_signal_strength = tda8083_read_signal_strength,
442 .read_snr = tda8083_read_snr,
443
444 .diseqc_send_master_cmd = tda8083_send_diseqc_msg,
445 .diseqc_send_burst = tda8083_diseqc_send_burst,
446 .set_tone = tda8083_diseqc_set_tone,
447 .set_voltage = tda8083_diseqc_set_voltage,
448};
449
450module_param(debug, int, 0644);
451MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
452
453MODULE_DESCRIPTION("Philips TDA8083 DVB-S Demodulator");
454MODULE_AUTHOR("Ralph Metzler, Holger Waechtler");
455MODULE_LICENSE("GPL");
456
457EXPORT_SYMBOL(tda8083_attach);