GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / media / dvb / frontends / tda10021.c
blob1d212b15d2d0a5f9d95f5f831720fed57c34cfd6
1 /*
2 TDA10021 - Single Chip Cable Channel Receiver driver module
3 used on the Siemens DVB-C cards
5 Copyright (C) 1999 Convergence Integrated Media GmbH <ralph@convergence.de>
6 Copyright (C) 2004 Markus Schulz <msc@antzsystem.de>
7 Support for TDA10021
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/delay.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/string.h>
30 #include <linux/slab.h>
32 #include "dvb_frontend.h"
33 #include "tda1002x.h"
36 struct tda10021_state {
37 struct i2c_adapter* i2c;
38 /* configuration settings */
39 const struct tda1002x_config* config;
40 struct dvb_frontend frontend;
42 u8 pwm;
43 u8 reg0;
47 #define dprintk(x...)
49 static int verbose;
51 #define XIN 57840000UL
53 #define FIN (XIN >> 4)
55 static int tda10021_inittab_size = 0x40;
56 static u8 tda10021_inittab[0x40]=
58 0x73, 0x6a, 0x23, 0x0a, 0x02, 0x37, 0x77, 0x1a,
59 0x37, 0x6a, 0x17, 0x8a, 0x1e, 0x86, 0x43, 0x40,
60 0xb8, 0x3f, 0xa1, 0x00, 0xcd, 0x01, 0x00, 0xff,
61 0x11, 0x00, 0x7c, 0x31, 0x30, 0x20, 0x00, 0x00,
62 0x02, 0x00, 0x00, 0x7d, 0x00, 0x00, 0x00, 0x00,
63 0x07, 0x00, 0x33, 0x11, 0x0d, 0x95, 0x08, 0x58,
64 0x00, 0x00, 0x80, 0x00, 0x80, 0xff, 0x00, 0x00,
65 0x04, 0x2d, 0x2f, 0xff, 0x00, 0x00, 0x00, 0x00,
68 static int _tda10021_writereg (struct tda10021_state* state, u8 reg, u8 data)
70 u8 buf[] = { reg, data };
71 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
72 int ret;
74 ret = i2c_transfer (state->i2c, &msg, 1);
75 if (ret != 1)
76 printk("DVB: TDA10021(%d): %s, writereg error "
77 "(reg == 0x%02x, val == 0x%02x, ret == %i)\n",
78 state->frontend.dvb->num, __func__, reg, data, ret);
80 msleep(10);
81 return (ret != 1) ? -EREMOTEIO : 0;
84 static u8 tda10021_readreg (struct tda10021_state* state, u8 reg)
86 u8 b0 [] = { reg };
87 u8 b1 [] = { 0 };
88 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
89 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
90 int ret;
92 ret = i2c_transfer (state->i2c, msg, 2);
93 // Don't print an error message if the id is read.
94 if (ret != 2 && reg != 0x1a)
95 printk("DVB: TDA10021: %s: readreg error (ret == %i)\n",
96 __func__, ret);
97 return b1[0];
100 //get access to tuner
101 static int lock_tuner(struct tda10021_state* state)
103 u8 buf[2] = { 0x0f, tda10021_inittab[0x0f] | 0x80 };
104 struct i2c_msg msg = {.addr=state->config->demod_address, .flags=0, .buf=buf, .len=2};
106 if(i2c_transfer(state->i2c, &msg, 1) != 1)
108 printk("tda10021: lock tuner fails\n");
109 return -EREMOTEIO;
111 return 0;
114 //release access from tuner
115 static int unlock_tuner(struct tda10021_state* state)
117 u8 buf[2] = { 0x0f, tda10021_inittab[0x0f] & 0x7f };
118 struct i2c_msg msg_post={.addr=state->config->demod_address, .flags=0, .buf=buf, .len=2};
120 if(i2c_transfer(state->i2c, &msg_post, 1) != 1)
122 printk("tda10021: unlock tuner fails\n");
123 return -EREMOTEIO;
125 return 0;
128 static int tda10021_setup_reg0 (struct tda10021_state* state, u8 reg0,
129 fe_spectral_inversion_t inversion)
131 reg0 |= state->reg0 & 0x63;
133 if ((INVERSION_ON == inversion) ^ (state->config->invert == 0))
134 reg0 &= ~0x20;
135 else
136 reg0 |= 0x20;
138 _tda10021_writereg (state, 0x00, reg0 & 0xfe);
139 _tda10021_writereg (state, 0x00, reg0 | 0x01);
141 state->reg0 = reg0;
142 return 0;
145 static int tda10021_set_symbolrate (struct tda10021_state* state, u32 symbolrate)
147 s32 BDR;
148 s32 BDRI;
149 s16 SFIL=0;
150 u16 NDEC = 0;
151 u32 tmp, ratio;
153 if (symbolrate > XIN/2)
154 symbolrate = XIN/2;
155 if (symbolrate < 500000)
156 symbolrate = 500000;
158 if (symbolrate < XIN/16) NDEC = 1;
159 if (symbolrate < XIN/32) NDEC = 2;
160 if (symbolrate < XIN/64) NDEC = 3;
162 if (symbolrate < (u32)(XIN/12.3)) SFIL = 1;
163 if (symbolrate < (u32)(XIN/16)) SFIL = 0;
164 if (symbolrate < (u32)(XIN/24.6)) SFIL = 1;
165 if (symbolrate < (u32)(XIN/32)) SFIL = 0;
166 if (symbolrate < (u32)(XIN/49.2)) SFIL = 1;
167 if (symbolrate < (u32)(XIN/64)) SFIL = 0;
168 if (symbolrate < (u32)(XIN/98.4)) SFIL = 1;
170 symbolrate <<= NDEC;
171 ratio = (symbolrate << 4) / FIN;
172 tmp = ((symbolrate << 4) % FIN) << 8;
173 ratio = (ratio << 8) + tmp / FIN;
174 tmp = (tmp % FIN) << 8;
175 ratio = (ratio << 8) + DIV_ROUND_CLOSEST(tmp, FIN);
177 BDR = ratio;
178 BDRI = (((XIN << 5) / symbolrate) + 1) / 2;
180 if (BDRI > 0xFF)
181 BDRI = 0xFF;
183 SFIL = (SFIL << 4) | tda10021_inittab[0x0E];
185 NDEC = (NDEC << 6) | tda10021_inittab[0x03];
187 _tda10021_writereg (state, 0x03, NDEC);
188 _tda10021_writereg (state, 0x0a, BDR&0xff);
189 _tda10021_writereg (state, 0x0b, (BDR>> 8)&0xff);
190 _tda10021_writereg (state, 0x0c, (BDR>>16)&0x3f);
192 _tda10021_writereg (state, 0x0d, BDRI);
193 _tda10021_writereg (state, 0x0e, SFIL);
195 return 0;
198 static int tda10021_init (struct dvb_frontend *fe)
200 struct tda10021_state* state = fe->demodulator_priv;
201 int i;
203 dprintk("DVB: TDA10021(%d): init chip\n", fe->adapter->num);
205 //_tda10021_writereg (fe, 0, 0);
207 for (i=0; i<tda10021_inittab_size; i++)
208 _tda10021_writereg (state, i, tda10021_inittab[i]);
210 _tda10021_writereg (state, 0x34, state->pwm);
212 //Comment by markus
213 //0x2A[3-0] == PDIV -> P multiplaying factor (P=PDIV+1)(default 0)
214 //0x2A[4] == BYPPLL -> Power down mode (default 1)
215 //0x2A[5] == LCK -> PLL Lock Flag
216 //0x2A[6] == POLAXIN -> Polarity of the input reference clock (default 0)
218 //Activate PLL
219 _tda10021_writereg(state, 0x2a, tda10021_inittab[0x2a] & 0xef);
220 return 0;
223 static int tda10021_set_parameters (struct dvb_frontend *fe,
224 struct dvb_frontend_parameters *p)
226 struct tda10021_state* state = fe->demodulator_priv;
228 //table for QAM4-QAM256 ready QAM4 QAM16 QAM32 QAM64 QAM128 QAM256
229 //CONF
230 static const u8 reg0x00 [] = { 0x14, 0x00, 0x04, 0x08, 0x0c, 0x10 };
231 //AGCREF value
232 static const u8 reg0x01 [] = { 0x78, 0x8c, 0x8c, 0x6a, 0x78, 0x5c };
233 //LTHR value
234 static const u8 reg0x05 [] = { 0x78, 0x87, 0x64, 0x46, 0x36, 0x26 };
235 //MSETH
236 static const u8 reg0x08 [] = { 0x8c, 0xa2, 0x74, 0x43, 0x34, 0x23 };
237 //AREF
238 static const u8 reg0x09 [] = { 0x96, 0x91, 0x96, 0x6a, 0x7e, 0x6b };
240 int qam = p->u.qam.modulation;
242 if (qam < 0 || qam > 5)
243 return -EINVAL;
245 if (p->inversion != INVERSION_ON && p->inversion != INVERSION_OFF)
246 return -EINVAL;
248 //printk("tda10021: set frequency to %d qam=%d symrate=%d\n", p->frequency,qam,p->u.qam.symbol_rate);
250 if (fe->ops.tuner_ops.set_params) {
251 fe->ops.tuner_ops.set_params(fe, p);
252 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
255 tda10021_set_symbolrate (state, p->u.qam.symbol_rate);
256 _tda10021_writereg (state, 0x34, state->pwm);
258 _tda10021_writereg (state, 0x01, reg0x01[qam]);
259 _tda10021_writereg (state, 0x05, reg0x05[qam]);
260 _tda10021_writereg (state, 0x08, reg0x08[qam]);
261 _tda10021_writereg (state, 0x09, reg0x09[qam]);
263 tda10021_setup_reg0 (state, reg0x00[qam], p->inversion);
265 return 0;
268 static int tda10021_read_status(struct dvb_frontend* fe, fe_status_t* status)
270 struct tda10021_state* state = fe->demodulator_priv;
271 int sync;
273 *status = 0;
274 //0x11[0] == EQALGO -> Equalizer algorithms state
275 //0x11[1] == CARLOCK -> Carrier locked
276 //0x11[2] == FSYNC -> Frame synchronisation
277 //0x11[3] == FEL -> Front End locked
278 //0x11[6] == NODVB -> DVB Mode Information
279 sync = tda10021_readreg (state, 0x11);
281 if (sync & 2)
282 *status |= FE_HAS_SIGNAL|FE_HAS_CARRIER;
284 if (sync & 4)
285 *status |= FE_HAS_SYNC|FE_HAS_VITERBI;
287 if (sync & 8)
288 *status |= FE_HAS_LOCK;
290 return 0;
293 static int tda10021_read_ber(struct dvb_frontend* fe, u32* ber)
295 struct tda10021_state* state = fe->demodulator_priv;
297 u32 _ber = tda10021_readreg(state, 0x14) |
298 (tda10021_readreg(state, 0x15) << 8) |
299 ((tda10021_readreg(state, 0x16) & 0x0f) << 16);
300 _tda10021_writereg(state, 0x10, (tda10021_readreg(state, 0x10) & ~0xc0)
301 | (tda10021_inittab[0x10] & 0xc0));
302 *ber = 10 * _ber;
304 return 0;
307 static int tda10021_read_signal_strength(struct dvb_frontend* fe, u16* strength)
309 struct tda10021_state* state = fe->demodulator_priv;
311 u8 config = tda10021_readreg(state, 0x02);
312 u8 gain = tda10021_readreg(state, 0x17);
313 if (config & 0x02)
314 /* the agc value is inverted */
315 gain = ~gain;
316 *strength = (gain << 8) | gain;
318 return 0;
321 static int tda10021_read_snr(struct dvb_frontend* fe, u16* snr)
323 struct tda10021_state* state = fe->demodulator_priv;
325 u8 quality = ~tda10021_readreg(state, 0x18);
326 *snr = (quality << 8) | quality;
328 return 0;
331 static int tda10021_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
333 struct tda10021_state* state = fe->demodulator_priv;
335 *ucblocks = tda10021_readreg (state, 0x13) & 0x7f;
336 if (*ucblocks == 0x7f)
337 *ucblocks = 0xffffffff;
339 /* reset uncorrected block counter */
340 _tda10021_writereg (state, 0x10, tda10021_inittab[0x10] & 0xdf);
341 _tda10021_writereg (state, 0x10, tda10021_inittab[0x10]);
343 return 0;
346 static int tda10021_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
348 struct tda10021_state* state = fe->demodulator_priv;
349 int sync;
350 s8 afc = 0;
352 sync = tda10021_readreg(state, 0x11);
353 afc = tda10021_readreg(state, 0x19);
354 if (verbose) {
355 /* AFC only valid when carrier has been recovered */
356 printk(sync & 2 ? "DVB: TDA10021(%d): AFC (%d) %dHz\n" :
357 "DVB: TDA10021(%d): [AFC (%d) %dHz]\n",
358 state->frontend.dvb->num, afc,
359 -((s32)p->u.qam.symbol_rate * afc) >> 10);
362 p->inversion = ((state->reg0 & 0x20) == 0x20) ^ (state->config->invert != 0) ? INVERSION_ON : INVERSION_OFF;
363 p->u.qam.modulation = ((state->reg0 >> 2) & 7) + QAM_16;
365 p->u.qam.fec_inner = FEC_NONE;
366 p->frequency = ((p->frequency + 31250) / 62500) * 62500;
368 if (sync & 2)
369 p->frequency -= ((s32)p->u.qam.symbol_rate * afc) >> 10;
371 return 0;
374 static int tda10021_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
376 struct tda10021_state* state = fe->demodulator_priv;
378 if (enable) {
379 lock_tuner(state);
380 } else {
381 unlock_tuner(state);
383 return 0;
386 static int tda10021_sleep(struct dvb_frontend* fe)
388 struct tda10021_state* state = fe->demodulator_priv;
390 _tda10021_writereg (state, 0x1b, 0x02); /* pdown ADC */
391 _tda10021_writereg (state, 0x00, 0x80); /* standby */
393 return 0;
396 static void tda10021_release(struct dvb_frontend* fe)
398 struct tda10021_state* state = fe->demodulator_priv;
399 kfree(state);
402 static struct dvb_frontend_ops tda10021_ops;
404 struct dvb_frontend* tda10021_attach(const struct tda1002x_config* config,
405 struct i2c_adapter* i2c,
406 u8 pwm)
408 struct tda10021_state* state = NULL;
409 u8 id;
411 /* allocate memory for the internal state */
412 state = kzalloc(sizeof(struct tda10021_state), GFP_KERNEL);
413 if (state == NULL) goto error;
415 /* setup the state */
416 state->config = config;
417 state->i2c = i2c;
418 state->pwm = pwm;
419 state->reg0 = tda10021_inittab[0];
421 /* check if the demod is there */
422 id = tda10021_readreg(state, 0x1a);
423 if ((id & 0xf0) != 0x70) goto error;
425 /* Don't claim TDA10023 */
426 if (id == 0x7d)
427 goto error;
429 printk("TDA10021: i2c-addr = 0x%02x, id = 0x%02x\n",
430 state->config->demod_address, id);
432 /* create dvb_frontend */
433 memcpy(&state->frontend.ops, &tda10021_ops, sizeof(struct dvb_frontend_ops));
434 state->frontend.demodulator_priv = state;
435 return &state->frontend;
437 error:
438 kfree(state);
439 return NULL;
442 static struct dvb_frontend_ops tda10021_ops = {
444 .info = {
445 .name = "Philips TDA10021 DVB-C",
446 .type = FE_QAM,
447 .frequency_stepsize = 62500,
448 .frequency_min = 47000000,
449 .frequency_max = 862000000,
450 .symbol_rate_min = (XIN/2)/64, /* SACLK/64 == (XIN/2)/64 */
451 .symbol_rate_max = (XIN/2)/4, /* SACLK/4 */
452 .caps = 0x400 | //FE_CAN_QAM_4
453 FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 |
454 FE_CAN_QAM_128 | FE_CAN_QAM_256 |
455 FE_CAN_FEC_AUTO
458 .release = tda10021_release,
460 .init = tda10021_init,
461 .sleep = tda10021_sleep,
462 .i2c_gate_ctrl = tda10021_i2c_gate_ctrl,
464 .set_frontend = tda10021_set_parameters,
465 .get_frontend = tda10021_get_frontend,
467 .read_status = tda10021_read_status,
468 .read_ber = tda10021_read_ber,
469 .read_signal_strength = tda10021_read_signal_strength,
470 .read_snr = tda10021_read_snr,
471 .read_ucblocks = tda10021_read_ucblocks,
474 module_param(verbose, int, 0644);
475 MODULE_PARM_DESC(verbose, "print AFC offset after tuning for debugging the PWM setting");
477 MODULE_DESCRIPTION("Philips TDA10021 DVB-C demodulator driver");
478 MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Markus Schulz");
479 MODULE_LICENSE("GPL");
481 EXPORT_SYMBOL(tda10021_attach);