mm/slab.c: proper prototypes
[wrt350n-kernel.git] / drivers / media / dvb / frontends / tda827x.c
blob256fc4bf500b17f44fe368f0ffcfa0623fb5a340
1 /*
3 * (c) 2005 Hartmut Hackmann
4 * (c) 2007 Michael Krufky
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/module.h>
22 #include <linux/dvb/frontend.h>
23 #include <asm/types.h>
25 #include "tda827x.h"
27 static int debug = 0;
28 #define dprintk(args...) \
29 do { \
30 if (debug) printk(KERN_DEBUG "tda827x: " args); \
31 } while (0)
33 struct tda827x_priv {
34 int i2c_addr;
35 struct i2c_adapter *i2c_adap;
36 struct tda827x_config *cfg;
37 u32 frequency;
38 u32 bandwidth;
41 struct tda827x_data {
42 u32 lomax;
43 u8 spd;
44 u8 bs;
45 u8 bp;
46 u8 cp;
47 u8 gc3;
48 u8 div1p5;
51 static const struct tda827x_data tda827x_dvbt[] = {
52 { .lomax = 62000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
53 { .lomax = 66000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
54 { .lomax = 76000000, .spd = 3, .bs = 1, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
55 { .lomax = 84000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
56 { .lomax = 93000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0},
57 { .lomax = 98000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0},
58 { .lomax = 109000000, .spd = 3, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
59 { .lomax = 123000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1},
60 { .lomax = 133000000, .spd = 2, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1},
61 { .lomax = 151000000, .spd = 2, .bs = 1, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
62 { .lomax = 154000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
63 { .lomax = 181000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 0, .div1p5 = 0},
64 { .lomax = 185000000, .spd = 2, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
65 { .lomax = 217000000, .spd = 2, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
66 { .lomax = 244000000, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1},
67 { .lomax = 265000000, .spd = 1, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1},
68 { .lomax = 302000000, .spd = 1, .bs = 1, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
69 { .lomax = 324000000, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
70 { .lomax = 370000000, .spd = 1, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
71 { .lomax = 454000000, .spd = 1, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
72 { .lomax = 493000000, .spd = 0, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1},
73 { .lomax = 530000000, .spd = 0, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1},
74 { .lomax = 554000000, .spd = 0, .bs = 1, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
75 { .lomax = 604000000, .spd = 0, .bs = 1, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
76 { .lomax = 696000000, .spd = 0, .bs = 2, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
77 { .lomax = 740000000, .spd = 0, .bs = 2, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0},
78 { .lomax = 820000000, .spd = 0, .bs = 3, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
79 { .lomax = 865000000, .spd = 0, .bs = 3, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0},
80 { .lomax = 0, .spd = 0, .bs = 0, .bp = 0, .cp = 0, .gc3 = 0, .div1p5 = 0}
83 static int tda827xo_set_params(struct dvb_frontend *fe,
84 struct dvb_frontend_parameters *params)
86 struct tda827x_priv *priv = fe->tuner_priv;
87 u8 buf[14];
89 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
90 .buf = buf, .len = sizeof(buf) };
91 int i, tuner_freq, if_freq;
92 u32 N;
94 dprintk("%s:\n", __FUNCTION__);
95 switch (params->u.ofdm.bandwidth) {
96 case BANDWIDTH_6_MHZ:
97 if_freq = 4000000;
98 break;
99 case BANDWIDTH_7_MHZ:
100 if_freq = 4500000;
101 break;
102 default: /* 8 MHz or Auto */
103 if_freq = 5000000;
104 break;
106 tuner_freq = params->frequency + if_freq;
108 i = 0;
109 while (tda827x_dvbt[i].lomax < tuner_freq) {
110 if(tda827x_dvbt[i + 1].lomax == 0)
111 break;
112 i++;
115 N = ((tuner_freq + 125000) / 250000) << (tda827x_dvbt[i].spd + 2);
116 buf[0] = 0;
117 buf[1] = (N>>8) | 0x40;
118 buf[2] = N & 0xff;
119 buf[3] = 0;
120 buf[4] = 0x52;
121 buf[5] = (tda827x_dvbt[i].spd << 6) + (tda827x_dvbt[i].div1p5 << 5) +
122 (tda827x_dvbt[i].bs << 3) + tda827x_dvbt[i].bp;
123 buf[6] = (tda827x_dvbt[i].gc3 << 4) + 0x8f;
124 buf[7] = 0xbf;
125 buf[8] = 0x2a;
126 buf[9] = 0x05;
127 buf[10] = 0xff;
128 buf[11] = 0x00;
129 buf[12] = 0x00;
130 buf[13] = 0x40;
132 msg.len = 14;
133 if (fe->ops.i2c_gate_ctrl)
134 fe->ops.i2c_gate_ctrl(fe, 1);
135 if (i2c_transfer(priv->i2c_adap, &msg, 1) != 1) {
136 printk("%s: could not write to tuner at addr: 0x%02x\n",
137 __FUNCTION__, priv->i2c_addr << 1);
138 return -EIO;
140 msleep(500);
141 /* correct CP value */
142 buf[0] = 0x30;
143 buf[1] = 0x50 + tda827x_dvbt[i].cp;
144 msg.len = 2;
146 if (fe->ops.i2c_gate_ctrl)
147 fe->ops.i2c_gate_ctrl(fe, 1);
148 i2c_transfer(priv->i2c_adap, &msg, 1);
150 priv->frequency = tuner_freq - if_freq; // FIXME
151 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
153 return 0;
156 static int tda827xo_sleep(struct dvb_frontend *fe)
158 struct tda827x_priv *priv = fe->tuner_priv;
159 static u8 buf[] = { 0x30, 0xd0 };
160 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
161 .buf = buf, .len = sizeof(buf) };
163 dprintk("%s:\n", __FUNCTION__);
164 if (fe->ops.i2c_gate_ctrl)
165 fe->ops.i2c_gate_ctrl(fe, 1);
166 i2c_transfer(priv->i2c_adap, &msg, 1);
168 if (priv->cfg && priv->cfg->sleep)
169 priv->cfg->sleep(fe);
171 return 0;
174 /* ------------------------------------------------------------------ */
176 struct tda827xa_data {
177 u32 lomax;
178 u8 svco;
179 u8 spd;
180 u8 scr;
181 u8 sbs;
182 u8 gc3;
185 static const struct tda827xa_data tda827xa_dvbt[] = {
186 { .lomax = 56875000, .svco = 3, .spd = 4, .scr = 0, .sbs = 0, .gc3 = 1},
187 { .lomax = 67250000, .svco = 0, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
188 { .lomax = 81250000, .svco = 1, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
189 { .lomax = 97500000, .svco = 2, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
190 { .lomax = 113750000, .svco = 3, .spd = 3, .scr = 0, .sbs = 1, .gc3 = 1},
191 { .lomax = 134500000, .svco = 0, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
192 { .lomax = 154000000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
193 { .lomax = 162500000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
194 { .lomax = 183000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
195 { .lomax = 195000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
196 { .lomax = 227500000, .svco = 3, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
197 { .lomax = 269000000, .svco = 0, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
198 { .lomax = 290000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
199 { .lomax = 325000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
200 { .lomax = 390000000, .svco = 2, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
201 { .lomax = 455000000, .svco = 3, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
202 { .lomax = 520000000, .svco = 0, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
203 { .lomax = 538000000, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1},
204 { .lomax = 550000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
205 { .lomax = 620000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
206 { .lomax = 650000000, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
207 { .lomax = 700000000, .svco = 2, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
208 { .lomax = 780000000, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
209 { .lomax = 820000000, .svco = 3, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
210 { .lomax = 870000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
211 { .lomax = 911000000, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 0},
212 { .lomax = 0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0}
215 static int tda827xa_set_params(struct dvb_frontend *fe,
216 struct dvb_frontend_parameters *params)
218 struct tda827x_priv *priv = fe->tuner_priv;
219 u8 buf[11];
221 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
222 .buf = buf, .len = sizeof(buf) };
224 int i, tuner_freq, if_freq;
225 u32 N;
227 dprintk("%s:\n", __FUNCTION__);
228 if (priv->cfg && priv->cfg->lna_gain)
229 priv->cfg->lna_gain(fe, 1);
230 msleep(20);
232 switch (params->u.ofdm.bandwidth) {
233 case BANDWIDTH_6_MHZ:
234 if_freq = 4000000;
235 break;
236 case BANDWIDTH_7_MHZ:
237 if_freq = 4500000;
238 break;
239 default: /* 8 MHz or Auto */
240 if_freq = 5000000;
241 break;
243 tuner_freq = params->frequency + if_freq;
245 i = 0;
246 while (tda827xa_dvbt[i].lomax < tuner_freq) {
247 if(tda827xa_dvbt[i + 1].lomax == 0)
248 break;
249 i++;
252 N = ((tuner_freq + 31250) / 62500) << tda827xa_dvbt[i].spd;
253 buf[0] = 0; // subaddress
254 buf[1] = N >> 8;
255 buf[2] = N & 0xff;
256 buf[3] = 0;
257 buf[4] = 0x16;
258 buf[5] = (tda827xa_dvbt[i].spd << 5) + (tda827xa_dvbt[i].svco << 3) +
259 tda827xa_dvbt[i].sbs;
260 buf[6] = 0x4b + (tda827xa_dvbt[i].gc3 << 4);
261 buf[7] = 0x1c;
262 buf[8] = 0x06;
263 buf[9] = 0x24;
264 buf[10] = 0x00;
265 msg.len = 11;
266 if (fe->ops.i2c_gate_ctrl)
267 fe->ops.i2c_gate_ctrl(fe, 1);
268 if (i2c_transfer(priv->i2c_adap, &msg, 1) != 1) {
269 printk("%s: could not write to tuner at addr: 0x%02x\n",
270 __FUNCTION__, priv->i2c_addr << 1);
271 return -EIO;
273 buf[0] = 0x90;
274 buf[1] = 0xff;
275 buf[2] = 0x60;
276 buf[3] = 0x00;
277 buf[4] = 0x59; // lpsel, for 6MHz + 2
278 msg.len = 5;
279 if (fe->ops.i2c_gate_ctrl)
280 fe->ops.i2c_gate_ctrl(fe, 1);
281 i2c_transfer(priv->i2c_adap, &msg, 1);
283 buf[0] = 0xa0;
284 buf[1] = 0x40;
285 msg.len = 2;
286 if (fe->ops.i2c_gate_ctrl)
287 fe->ops.i2c_gate_ctrl(fe, 1);
288 i2c_transfer(priv->i2c_adap, &msg, 1);
290 msleep(11);
291 msg.flags = I2C_M_RD;
292 if (fe->ops.i2c_gate_ctrl)
293 fe->ops.i2c_gate_ctrl(fe, 1);
294 i2c_transfer(priv->i2c_adap, &msg, 1);
295 msg.flags = 0;
297 buf[1] >>= 4;
298 dprintk("tda8275a AGC2 gain is: %d\n", buf[1]);
299 if ((buf[1]) < 2) {
300 if (priv->cfg && priv->cfg->lna_gain)
301 priv->cfg->lna_gain(fe, 0);
302 buf[0] = 0x60;
303 buf[1] = 0x0c;
304 if (fe->ops.i2c_gate_ctrl)
305 fe->ops.i2c_gate_ctrl(fe, 1);
306 i2c_transfer(priv->i2c_adap, &msg, 1);
309 buf[0] = 0xc0;
310 buf[1] = 0x99; // lpsel, for 6MHz + 2
311 if (fe->ops.i2c_gate_ctrl)
312 fe->ops.i2c_gate_ctrl(fe, 1);
313 i2c_transfer(priv->i2c_adap, &msg, 1);
315 buf[0] = 0x60;
316 buf[1] = 0x3c;
317 if (fe->ops.i2c_gate_ctrl)
318 fe->ops.i2c_gate_ctrl(fe, 1);
319 i2c_transfer(priv->i2c_adap, &msg, 1);
321 /* correct CP value */
322 buf[0] = 0x30;
323 buf[1] = 0x10 + tda827xa_dvbt[i].scr;
324 if (fe->ops.i2c_gate_ctrl)
325 fe->ops.i2c_gate_ctrl(fe, 1);
326 i2c_transfer(priv->i2c_adap, &msg, 1);
328 msleep(163);
329 buf[0] = 0xc0;
330 buf[1] = 0x39; // lpsel, for 6MHz + 2
331 if (fe->ops.i2c_gate_ctrl)
332 fe->ops.i2c_gate_ctrl(fe, 1);
333 i2c_transfer(priv->i2c_adap, &msg, 1);
335 msleep(3);
336 /* freeze AGC1 */
337 buf[0] = 0x50;
338 buf[1] = 0x4f + (tda827xa_dvbt[i].gc3 << 4);
339 if (fe->ops.i2c_gate_ctrl)
340 fe->ops.i2c_gate_ctrl(fe, 1);
341 i2c_transfer(priv->i2c_adap, &msg, 1);
343 priv->frequency = tuner_freq - if_freq; // FIXME
344 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
346 return 0;
349 static int tda827xa_sleep(struct dvb_frontend *fe)
351 struct tda827x_priv *priv = fe->tuner_priv;
352 static u8 buf[] = { 0x30, 0x90 };
353 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
354 .buf = buf, .len = sizeof(buf) };
356 dprintk("%s:\n", __FUNCTION__);
357 if (fe->ops.i2c_gate_ctrl)
358 fe->ops.i2c_gate_ctrl(fe, 1);
360 i2c_transfer(priv->i2c_adap, &msg, 1);
362 if (fe->ops.i2c_gate_ctrl)
363 fe->ops.i2c_gate_ctrl(fe, 0);
365 if (priv->cfg && priv->cfg->sleep)
366 priv->cfg->sleep(fe);
368 return 0;
371 static int tda827x_release(struct dvb_frontend *fe)
373 kfree(fe->tuner_priv);
374 fe->tuner_priv = NULL;
375 return 0;
378 static int tda827x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
380 struct tda827x_priv *priv = fe->tuner_priv;
381 *frequency = priv->frequency;
382 return 0;
385 static int tda827x_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
387 struct tda827x_priv *priv = fe->tuner_priv;
388 *bandwidth = priv->bandwidth;
389 return 0;
392 static int tda827x_init(struct dvb_frontend *fe)
394 struct tda827x_priv *priv = fe->tuner_priv;
395 dprintk("%s:\n", __FUNCTION__);
396 if (priv->cfg && priv->cfg->init)
397 priv->cfg->init(fe);
399 return 0;
402 static int tda827x_probe_version(struct dvb_frontend *fe);
404 static int tda827x_initial_init(struct dvb_frontend *fe)
406 int ret;
407 ret = tda827x_probe_version(fe);
408 if (ret)
409 return ret;
410 return fe->ops.tuner_ops.init(fe);
413 static int tda827x_initial_sleep(struct dvb_frontend *fe)
415 int ret;
416 ret = tda827x_probe_version(fe);
417 if (ret)
418 return ret;
419 return fe->ops.tuner_ops.sleep(fe);
422 static struct dvb_tuner_ops tda827xo_tuner_ops = {
423 .info = {
424 .name = "Philips TDA827X",
425 .frequency_min = 55000000,
426 .frequency_max = 860000000,
427 .frequency_step = 250000
429 .release = tda827x_release,
430 .init = tda827x_initial_init,
431 .sleep = tda827x_initial_sleep,
432 .set_params = tda827xo_set_params,
433 .get_frequency = tda827x_get_frequency,
434 .get_bandwidth = tda827x_get_bandwidth,
437 static struct dvb_tuner_ops tda827xa_tuner_ops = {
438 .info = {
439 .name = "Philips TDA827XA",
440 .frequency_min = 44000000,
441 .frequency_max = 906000000,
442 .frequency_step = 62500
444 .release = tda827x_release,
445 .init = tda827x_init,
446 .sleep = tda827xa_sleep,
447 .set_params = tda827xa_set_params,
448 .get_frequency = tda827x_get_frequency,
449 .get_bandwidth = tda827x_get_bandwidth,
452 static int tda827x_probe_version(struct dvb_frontend *fe)
453 { u8 data;
454 struct tda827x_priv *priv = fe->tuner_priv;
455 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = I2C_M_RD,
456 .buf = &data, .len = 1 };
457 if (fe->ops.i2c_gate_ctrl)
458 fe->ops.i2c_gate_ctrl(fe, 1);
459 if (i2c_transfer(priv->i2c_adap, &msg, 1) != 1) {
460 printk("%s: could not read from tuner at addr: 0x%02x\n",
461 __FUNCTION__, msg.addr << 1);
462 return -EIO;
464 if ((data & 0x3c) == 0) {
465 dprintk("tda827x tuner found\n");
466 fe->ops.tuner_ops.init = tda827x_init;
467 fe->ops.tuner_ops.sleep = tda827xo_sleep;
468 } else {
469 dprintk("tda827xa tuner found\n");
470 memcpy(&fe->ops.tuner_ops, &tda827xa_tuner_ops, sizeof(struct dvb_tuner_ops));
472 return 0;
475 struct dvb_frontend *tda827x_attach(struct dvb_frontend *fe, int addr,
476 struct i2c_adapter *i2c,
477 struct tda827x_config *cfg)
479 struct tda827x_priv *priv = NULL;
481 dprintk("%s:\n", __FUNCTION__);
482 priv = kzalloc(sizeof(struct tda827x_priv), GFP_KERNEL);
483 if (priv == NULL)
484 return NULL;
486 priv->i2c_addr = addr;
487 priv->i2c_adap = i2c;
488 priv->cfg = cfg;
489 memcpy(&fe->ops.tuner_ops, &tda827xo_tuner_ops, sizeof(struct dvb_tuner_ops));
491 fe->tuner_priv = priv;
493 return fe;
496 EXPORT_SYMBOL(tda827x_attach);
498 module_param(debug, int, 0644);
499 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
501 MODULE_DESCRIPTION("DVB TDA827x driver");
502 MODULE_AUTHOR("Hartmut Hackmann <hartmut.hackmann@t-online.de>");
503 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
504 MODULE_LICENSE("GPL");
507 * Overrides for Emacs so that we follow Linus's tabbing style.
508 * ---------------------------------------------------------------------------
509 * Local variables:
510 * c-basic-offset: 8
511 * End: