V4L/DVB (5369): Fixed 1 byte too short buffer in tda827x.c
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / saa7134 / saa7134-dvb.c
blob3887f04cc60a6a4deae786fb4878a84be7fbda5b
1 /*
3 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
5 * Extended 3 / 2005 by Hartmut Hackmann to support various
6 * cards with the tda10046 DVB-T channel decoder
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
29 #include <linux/kthread.h>
30 #include <linux/suspend.h>
32 #include "saa7134-reg.h"
33 #include "saa7134.h"
34 #include <media/v4l2-common.h>
35 #include "dvb-pll.h"
37 #include "mt352.h"
38 #include "mt352_priv.h" /* FIXME */
39 #include "tda1004x.h"
40 #include "nxt200x.h"
42 #include "tda10086.h"
43 #include "tda826x.h"
44 #include "tda827x.h"
45 #include "isl6421.h"
47 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
48 MODULE_LICENSE("GPL");
50 static unsigned int antenna_pwr = 0;
52 module_param(antenna_pwr, int, 0444);
53 MODULE_PARM_DESC(antenna_pwr,"enable antenna power (Pinnacle 300i)");
55 static int use_frontend = 0;
56 module_param(use_frontend, int, 0644);
57 MODULE_PARM_DESC(use_frontend,"for cards with multiple frontends (0: terrestrial, 1: satellite)");
59 static int debug = 0;
60 module_param(debug, int, 0644);
61 MODULE_PARM_DESC(debug, "Turn on/off module debugging (default:off).");
63 #define dprintk(fmt, arg...) if (debug) \
64 printk(KERN_DEBUG "%s/dvb: " fmt, dev->name , ## arg)
66 /* ------------------------------------------------------------------
67 * mt352 based DVB-T cards
70 static int pinnacle_antenna_pwr(struct saa7134_dev *dev, int on)
72 u32 ok;
74 if (!on) {
75 saa_setl(SAA7134_GPIO_GPMODE0 >> 2, (1 << 26));
76 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 26));
77 return 0;
80 saa_setl(SAA7134_GPIO_GPMODE0 >> 2, (1 << 26));
81 saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 26));
82 udelay(10);
84 saa_setl(SAA7134_GPIO_GPMODE0 >> 2, (1 << 28));
85 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 28));
86 udelay(10);
87 saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 28));
88 udelay(10);
89 ok = saa_readl(SAA7134_GPIO_GPSTATUS0) & (1 << 27);
90 printk("%s: %s %s\n", dev->name, __FUNCTION__,
91 ok ? "on" : "off");
93 if (!ok)
94 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 26));
95 return ok;
98 static int mt352_pinnacle_init(struct dvb_frontend* fe)
100 static u8 clock_config [] = { CLOCK_CTL, 0x3d, 0x28 };
101 static u8 reset [] = { RESET, 0x80 };
102 static u8 adc_ctl_1_cfg [] = { ADC_CTL_1, 0x40 };
103 static u8 agc_cfg [] = { AGC_TARGET, 0x28, 0xa0 };
104 static u8 capt_range_cfg[] = { CAPT_RANGE, 0x31 };
105 static u8 fsm_ctl_cfg[] = { 0x7b, 0x04 };
106 static u8 gpp_ctl_cfg [] = { GPP_CTL, 0x0f };
107 static u8 scan_ctl_cfg [] = { SCAN_CTL, 0x0d };
108 static u8 irq_cfg [] = { INTERRUPT_EN_0, 0x00, 0x00, 0x00, 0x00 };
109 struct saa7134_dev *dev= fe->dvb->priv;
111 printk("%s: %s called\n",dev->name,__FUNCTION__);
113 mt352_write(fe, clock_config, sizeof(clock_config));
114 udelay(200);
115 mt352_write(fe, reset, sizeof(reset));
116 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
117 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
118 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
119 mt352_write(fe, gpp_ctl_cfg, sizeof(gpp_ctl_cfg));
121 mt352_write(fe, fsm_ctl_cfg, sizeof(fsm_ctl_cfg));
122 mt352_write(fe, scan_ctl_cfg, sizeof(scan_ctl_cfg));
123 mt352_write(fe, irq_cfg, sizeof(irq_cfg));
125 return 0;
128 static int mt352_aver777_init(struct dvb_frontend* fe)
130 static u8 clock_config [] = { CLOCK_CTL, 0x38, 0x2d };
131 static u8 reset [] = { RESET, 0x80 };
132 static u8 adc_ctl_1_cfg [] = { ADC_CTL_1, 0x40 };
133 static u8 agc_cfg [] = { AGC_TARGET, 0x28, 0xa0 };
134 static u8 capt_range_cfg[] = { CAPT_RANGE, 0x33 };
136 mt352_write(fe, clock_config, sizeof(clock_config));
137 udelay(200);
138 mt352_write(fe, reset, sizeof(reset));
139 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
140 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
141 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
143 return 0;
146 static int mt352_pinnacle_tuner_set_params(struct dvb_frontend* fe,
147 struct dvb_frontend_parameters* params)
149 u8 off[] = { 0x00, 0xf1};
150 u8 on[] = { 0x00, 0x71};
151 struct i2c_msg msg = {.addr=0x43, .flags=0, .buf=off, .len = sizeof(off)};
153 struct saa7134_dev *dev = fe->dvb->priv;
154 struct v4l2_frequency f;
156 /* set frequency (mt2050) */
157 f.tuner = 0;
158 f.type = V4L2_TUNER_DIGITAL_TV;
159 f.frequency = params->frequency / 1000 * 16 / 1000;
160 if (fe->ops.i2c_gate_ctrl)
161 fe->ops.i2c_gate_ctrl(fe, 1);
162 i2c_transfer(&dev->i2c_adap, &msg, 1);
163 saa7134_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,&f);
164 msg.buf = on;
165 if (fe->ops.i2c_gate_ctrl)
166 fe->ops.i2c_gate_ctrl(fe, 1);
167 i2c_transfer(&dev->i2c_adap, &msg, 1);
169 pinnacle_antenna_pwr(dev, antenna_pwr);
171 /* mt352 setup */
172 return mt352_pinnacle_init(fe);
175 static int mt352_aver777_tuner_calc_regs(struct dvb_frontend *fe, struct dvb_frontend_parameters *params, u8* pllbuf, int buf_len)
177 if (buf_len < 5)
178 return -EINVAL;
180 pllbuf[0] = 0x61;
181 dvb_pll_configure(&dvb_pll_philips_td1316, pllbuf+1,
182 params->frequency,
183 params->u.ofdm.bandwidth);
184 return 5;
187 static struct mt352_config pinnacle_300i = {
188 .demod_address = 0x3c >> 1,
189 .adc_clock = 20333,
190 .if2 = 36150,
191 .no_tuner = 1,
192 .demod_init = mt352_pinnacle_init,
195 static struct mt352_config avermedia_777 = {
196 .demod_address = 0xf,
197 .demod_init = mt352_aver777_init,
200 /* ==================================================================
201 * tda1004x based DVB-T cards, helper functions
204 static int philips_tda1004x_request_firmware(struct dvb_frontend *fe,
205 const struct firmware **fw, char *name)
207 struct saa7134_dev *dev = fe->dvb->priv;
208 return request_firmware(fw, name, &dev->pci->dev);
211 /* ------------------------------------------------------------------
212 * these tuners are tu1216, td1316(a)
215 static int philips_tda6651_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
217 struct saa7134_dev *dev = fe->dvb->priv;
218 struct tda1004x_state *state = fe->demodulator_priv;
219 u8 addr = state->config->tuner_address;
220 u8 tuner_buf[4];
221 struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tuner_buf,.len =
222 sizeof(tuner_buf) };
223 int tuner_frequency = 0;
224 u8 band, cp, filter;
226 /* determine charge pump */
227 tuner_frequency = params->frequency + 36166000;
228 if (tuner_frequency < 87000000)
229 return -EINVAL;
230 else if (tuner_frequency < 130000000)
231 cp = 3;
232 else if (tuner_frequency < 160000000)
233 cp = 5;
234 else if (tuner_frequency < 200000000)
235 cp = 6;
236 else if (tuner_frequency < 290000000)
237 cp = 3;
238 else if (tuner_frequency < 420000000)
239 cp = 5;
240 else if (tuner_frequency < 480000000)
241 cp = 6;
242 else if (tuner_frequency < 620000000)
243 cp = 3;
244 else if (tuner_frequency < 830000000)
245 cp = 5;
246 else if (tuner_frequency < 895000000)
247 cp = 7;
248 else
249 return -EINVAL;
251 /* determine band */
252 if (params->frequency < 49000000)
253 return -EINVAL;
254 else if (params->frequency < 161000000)
255 band = 1;
256 else if (params->frequency < 444000000)
257 band = 2;
258 else if (params->frequency < 861000000)
259 band = 4;
260 else
261 return -EINVAL;
263 /* setup PLL filter */
264 switch (params->u.ofdm.bandwidth) {
265 case BANDWIDTH_6_MHZ:
266 filter = 0;
267 break;
269 case BANDWIDTH_7_MHZ:
270 filter = 0;
271 break;
273 case BANDWIDTH_8_MHZ:
274 filter = 1;
275 break;
277 default:
278 return -EINVAL;
281 /* calculate divisor
282 * ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
284 tuner_frequency = (((params->frequency / 1000) * 6) + 217496) / 1000;
286 /* setup tuner buffer */
287 tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
288 tuner_buf[1] = tuner_frequency & 0xff;
289 tuner_buf[2] = 0xca;
290 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
292 if (fe->ops.i2c_gate_ctrl)
293 fe->ops.i2c_gate_ctrl(fe, 1);
294 if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1) {
295 printk("%s/dvb: could not write to tuner at addr: 0x%02x\n",dev->name, addr << 1);
296 return -EIO;
298 msleep(1);
299 return 0;
302 static int philips_tu1216_init(struct dvb_frontend *fe)
304 struct saa7134_dev *dev = fe->dvb->priv;
305 struct tda1004x_state *state = fe->demodulator_priv;
306 u8 addr = state->config->tuner_address;
307 static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
308 struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
310 /* setup PLL configuration */
311 if (fe->ops.i2c_gate_ctrl)
312 fe->ops.i2c_gate_ctrl(fe, 1);
313 if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
314 return -EIO;
315 msleep(1);
317 return 0;
320 /* ------------------------------------------------------------------ */
322 static struct tda1004x_config philips_tu1216_60_config = {
323 .demod_address = 0x8,
324 .invert = 1,
325 .invert_oclk = 0,
326 .xtal_freq = TDA10046_XTAL_4M,
327 .agc_config = TDA10046_AGC_DEFAULT,
328 .if_freq = TDA10046_FREQ_3617,
329 .tuner_address = 0x60,
330 .request_firmware = philips_tda1004x_request_firmware
333 static struct tda1004x_config philips_tu1216_61_config = {
335 .demod_address = 0x8,
336 .invert = 1,
337 .invert_oclk = 0,
338 .xtal_freq = TDA10046_XTAL_4M,
339 .agc_config = TDA10046_AGC_DEFAULT,
340 .if_freq = TDA10046_FREQ_3617,
341 .tuner_address = 0x61,
342 .request_firmware = philips_tda1004x_request_firmware
345 /* ------------------------------------------------------------------ */
347 static int philips_td1316_tuner_init(struct dvb_frontend *fe)
349 struct saa7134_dev *dev = fe->dvb->priv;
350 struct tda1004x_state *state = fe->demodulator_priv;
351 u8 addr = state->config->tuner_address;
352 static u8 msg[] = { 0x0b, 0xf5, 0x86, 0xab };
353 struct i2c_msg init_msg = {.addr = addr,.flags = 0,.buf = msg,.len = sizeof(msg) };
355 /* setup PLL configuration */
356 if (fe->ops.i2c_gate_ctrl)
357 fe->ops.i2c_gate_ctrl(fe, 1);
358 if (i2c_transfer(&dev->i2c_adap, &init_msg, 1) != 1)
359 return -EIO;
360 return 0;
363 static int philips_td1316_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
365 return philips_tda6651_pll_set(fe, params);
368 static int philips_td1316_tuner_sleep(struct dvb_frontend *fe)
370 struct saa7134_dev *dev = fe->dvb->priv;
371 struct tda1004x_state *state = fe->demodulator_priv;
372 u8 addr = state->config->tuner_address;
373 static u8 msg[] = { 0x0b, 0xdc, 0x86, 0xa4 };
374 struct i2c_msg analog_msg = {.addr = addr,.flags = 0,.buf = msg,.len = sizeof(msg) };
376 /* switch the tuner to analog mode */
377 if (fe->ops.i2c_gate_ctrl)
378 fe->ops.i2c_gate_ctrl(fe, 1);
379 if (i2c_transfer(&dev->i2c_adap, &analog_msg, 1) != 1)
380 return -EIO;
381 return 0;
384 /* ------------------------------------------------------------------ */
386 static int philips_europa_tuner_init(struct dvb_frontend *fe)
388 struct saa7134_dev *dev = fe->dvb->priv;
389 static u8 msg[] = { 0x00, 0x40};
390 struct i2c_msg init_msg = {.addr = 0x43,.flags = 0,.buf = msg,.len = sizeof(msg) };
393 if (philips_td1316_tuner_init(fe))
394 return -EIO;
395 msleep(1);
396 if (i2c_transfer(&dev->i2c_adap, &init_msg, 1) != 1)
397 return -EIO;
399 return 0;
402 static int philips_europa_tuner_sleep(struct dvb_frontend *fe)
404 struct saa7134_dev *dev = fe->dvb->priv;
406 static u8 msg[] = { 0x00, 0x14 };
407 struct i2c_msg analog_msg = {.addr = 0x43,.flags = 0,.buf = msg,.len = sizeof(msg) };
409 if (philips_td1316_tuner_sleep(fe))
410 return -EIO;
412 /* switch the board to analog mode */
413 if (fe->ops.i2c_gate_ctrl)
414 fe->ops.i2c_gate_ctrl(fe, 1);
415 i2c_transfer(&dev->i2c_adap, &analog_msg, 1);
416 return 0;
419 static int philips_europa_demod_sleep(struct dvb_frontend *fe)
421 struct saa7134_dev *dev = fe->dvb->priv;
423 if (dev->original_demod_sleep)
424 dev->original_demod_sleep(fe);
425 fe->ops.i2c_gate_ctrl(fe, 1);
426 return 0;
429 static struct tda1004x_config philips_europa_config = {
431 .demod_address = 0x8,
432 .invert = 0,
433 .invert_oclk = 0,
434 .xtal_freq = TDA10046_XTAL_4M,
435 .agc_config = TDA10046_AGC_IFO_AUTO_POS,
436 .if_freq = TDA10046_FREQ_052,
437 .tuner_address = 0x61,
438 .request_firmware = philips_tda1004x_request_firmware
441 /* ------------------------------------------------------------------ */
443 static int philips_fmd1216_tuner_init(struct dvb_frontend *fe)
445 struct saa7134_dev *dev = fe->dvb->priv;
446 struct tda1004x_state *state = fe->demodulator_priv;
447 u8 addr = state->config->tuner_address;
448 /* this message is to set up ATC and ALC */
449 static u8 fmd1216_init[] = { 0x0b, 0xdc, 0x9c, 0xa0 };
450 struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = fmd1216_init,.len = sizeof(fmd1216_init) };
452 if (fe->ops.i2c_gate_ctrl)
453 fe->ops.i2c_gate_ctrl(fe, 1);
454 if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
455 return -EIO;
456 msleep(1);
458 return 0;
461 static int philips_fmd1216_tuner_sleep(struct dvb_frontend *fe)
463 struct saa7134_dev *dev = fe->dvb->priv;
464 struct tda1004x_state *state = fe->demodulator_priv;
465 u8 addr = state->config->tuner_address;
466 /* this message actually turns the tuner back to analog mode */
467 static u8 fmd1216_init[] = { 0x0b, 0xdc, 0x9c, 0x60 };
468 struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = fmd1216_init,.len = sizeof(fmd1216_init) };
470 if (fe->ops.i2c_gate_ctrl)
471 fe->ops.i2c_gate_ctrl(fe, 1);
472 i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
473 msleep(1);
474 fmd1216_init[2] = 0x86;
475 fmd1216_init[3] = 0x54;
476 if (fe->ops.i2c_gate_ctrl)
477 fe->ops.i2c_gate_ctrl(fe, 1);
478 i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
479 msleep(1);
480 return 0;
483 static int philips_fmd1216_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
485 struct saa7134_dev *dev = fe->dvb->priv;
486 struct tda1004x_state *state = fe->demodulator_priv;
487 u8 addr = state->config->tuner_address;
488 u8 tuner_buf[4];
489 struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tuner_buf,.len =
490 sizeof(tuner_buf) };
491 int tuner_frequency = 0;
492 int divider = 0;
493 u8 band, mode, cp;
495 /* determine charge pump */
496 tuner_frequency = params->frequency + 36130000;
497 if (tuner_frequency < 87000000)
498 return -EINVAL;
499 /* low band */
500 else if (tuner_frequency < 180000000) {
501 band = 1;
502 mode = 7;
503 cp = 0;
504 } else if (tuner_frequency < 195000000) {
505 band = 1;
506 mode = 6;
507 cp = 1;
508 /* mid band */
509 } else if (tuner_frequency < 366000000) {
510 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
511 band = 10;
512 } else {
513 band = 2;
515 mode = 7;
516 cp = 0;
517 } else if (tuner_frequency < 478000000) {
518 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
519 band = 10;
520 } else {
521 band = 2;
523 mode = 6;
524 cp = 1;
525 /* high band */
526 } else if (tuner_frequency < 662000000) {
527 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
528 band = 12;
529 } else {
530 band = 4;
532 mode = 7;
533 cp = 0;
534 } else if (tuner_frequency < 840000000) {
535 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
536 band = 12;
537 } else {
538 band = 4;
540 mode = 6;
541 cp = 1;
542 } else {
543 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
544 band = 12;
545 } else {
546 band = 4;
548 mode = 7;
549 cp = 1;
552 /* calculate divisor */
553 /* ((36166000 + Finput) / 166666) rounded! */
554 divider = (tuner_frequency + 83333) / 166667;
556 /* setup tuner buffer */
557 tuner_buf[0] = (divider >> 8) & 0x7f;
558 tuner_buf[1] = divider & 0xff;
559 tuner_buf[2] = 0x80 | (cp << 6) | (mode << 3) | 4;
560 tuner_buf[3] = 0x40 | band;
562 if (fe->ops.i2c_gate_ctrl)
563 fe->ops.i2c_gate_ctrl(fe, 1);
564 if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1) {
565 printk("%s/dvb: could not write to tuner at addr: 0x%02x\n",dev->name, addr << 1);
566 return -EIO;
568 return 0;
571 static struct tda1004x_config medion_cardbus = {
572 .demod_address = 0x08,
573 .invert = 1,
574 .invert_oclk = 0,
575 .xtal_freq = TDA10046_XTAL_16M,
576 .agc_config = TDA10046_AGC_IFO_AUTO_NEG,
577 .if_freq = TDA10046_FREQ_3613,
578 .tuner_address = 0x61,
579 .request_firmware = philips_tda1004x_request_firmware
582 /* ------------------------------------------------------------------
583 * tda 1004x based cards with philips silicon tuner
586 static void philips_tda827x_lna_gain(struct dvb_frontend *fe, int high)
588 struct saa7134_dev *dev = fe->dvb->priv;
589 struct tda1004x_state *state = fe->demodulator_priv;
590 u8 addr = state->config->i2c_gate;
591 u8 config = state->config->tuner_config;
592 u8 GP00_CF[] = {0x20, 0x01};
593 u8 GP00_LEV[] = {0x22, 0x00};
595 struct i2c_msg msg = {.addr = addr,.flags = 0,.buf = GP00_CF, .len = 2};
596 if (config) {
597 if (high) {
598 dprintk("setting LNA to high gain\n");
599 } else {
600 dprintk("setting LNA to low gain\n");
603 switch (config) {
604 case 0: /* no LNA */
605 break;
606 case 1: /* switch is GPIO 0 of tda8290 */
607 case 2:
608 /* turn Vsync off */
609 saa7134_set_gpio(dev, 22, 0);
610 GP00_LEV[1] = high ? 0 : 1;
611 if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1) {
612 printk("%s/dvb: could not access tda8290 at addr: 0x%02x\n",dev->name, addr << 1);
613 return;
615 msg.buf = GP00_LEV;
616 if (config == 2)
617 GP00_LEV[1] = high ? 1 : 0;
618 i2c_transfer(&dev->i2c_adap, &msg, 1);
619 break;
620 case 3: /* switch with GPIO of saa713x */
621 saa7134_set_gpio(dev, 22, high);
622 break;
626 static int tda8290_i2c_gate_ctrl( struct dvb_frontend* fe, int enable)
628 struct tda1004x_state *state = fe->demodulator_priv;
630 u8 addr = state->config->i2c_gate;
631 static u8 tda8290_close[] = { 0x21, 0xc0};
632 static u8 tda8290_open[] = { 0x21, 0x80};
633 struct i2c_msg tda8290_msg = {.addr = addr,.flags = 0, .len = 2};
634 if (enable) {
635 tda8290_msg.buf = tda8290_close;
636 } else {
637 tda8290_msg.buf = tda8290_open;
639 if (i2c_transfer(state->i2c, &tda8290_msg, 1) != 1) {
640 printk("saa7134/dvb: could not access tda8290 I2C gate\n");
641 return -EIO;
643 msleep(20);
644 return 0;
647 /* ------------------------------------------------------------------ */
649 static int philips_tda827x_tuner_init(struct dvb_frontend *fe)
651 struct saa7134_dev *dev = fe->dvb->priv;
652 struct tda1004x_state *state = fe->demodulator_priv;
654 switch (state->config->antenna_switch) {
655 case 0: break;
656 case 1: dprintk("setting GPIO21 to 0 (TV antenna?)\n");
657 saa7134_set_gpio(dev, 21, 0);
658 break;
659 case 2: dprintk("setting GPIO21 to 1 (Radio antenna?)\n");
660 saa7134_set_gpio(dev, 21, 1);
661 break;
663 return 0;
666 static int philips_tda827x_tuner_sleep(struct dvb_frontend *fe)
668 struct saa7134_dev *dev = fe->dvb->priv;
669 struct tda1004x_state *state = fe->demodulator_priv;
671 switch (state->config->antenna_switch) {
672 case 0: break;
673 case 1: dprintk("setting GPIO21 to 1 (Radio antenna?)\n");
674 saa7134_set_gpio(dev, 21, 1);
675 break;
676 case 2: dprintk("setting GPIO21 to 0 (TV antenna?)\n");
677 saa7134_set_gpio(dev, 21, 0);
678 break;
680 return 0;
683 static struct tda827x_config tda827x_cfg = {
684 .lna_gain = philips_tda827x_lna_gain,
685 .init = philips_tda827x_tuner_init,
686 .sleep = philips_tda827x_tuner_sleep
689 static void configure_tda827x_fe(struct saa7134_dev *dev, struct tda1004x_config *tda_conf)
691 dev->dvb.frontend = dvb_attach(tda10046_attach, tda_conf, &dev->i2c_adap);
692 if (dev->dvb.frontend) {
693 if (tda_conf->i2c_gate)
694 dev->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
695 if (dvb_attach(tda827x_attach, dev->dvb.frontend, tda_conf->tuner_address,
696 &dev->i2c_adap,&tda827x_cfg) == NULL) {
697 printk ("saa7134/dvb: no tda827x tuner found at addr: %02x\n",
698 tda_conf->tuner_address);
703 /* ------------------------------------------------------------------ */
704 static struct tda1004x_config tda827x_lifeview_config = {
705 .demod_address = 0x08,
706 .invert = 1,
707 .invert_oclk = 0,
708 .xtal_freq = TDA10046_XTAL_16M,
709 .agc_config = TDA10046_AGC_TDA827X,
710 .gpio_config = TDA10046_GP11_I,
711 .if_freq = TDA10046_FREQ_045,
712 .tuner_address = 0x60,
713 .request_firmware = philips_tda1004x_request_firmware
716 static struct tda1004x_config philips_tiger_config = {
717 .demod_address = 0x08,
718 .invert = 1,
719 .invert_oclk = 0,
720 .xtal_freq = TDA10046_XTAL_16M,
721 .agc_config = TDA10046_AGC_TDA827X,
722 .gpio_config = TDA10046_GP11_I,
723 .if_freq = TDA10046_FREQ_045,
724 .i2c_gate = 0x4b,
725 .tuner_address = 0x61,
726 .tuner_config = 0,
727 .antenna_switch= 1,
728 .request_firmware = philips_tda1004x_request_firmware
731 static struct tda1004x_config cinergy_ht_config = {
732 .demod_address = 0x08,
733 .invert = 1,
734 .invert_oclk = 0,
735 .xtal_freq = TDA10046_XTAL_16M,
736 .agc_config = TDA10046_AGC_TDA827X,
737 .gpio_config = TDA10046_GP01_I,
738 .if_freq = TDA10046_FREQ_045,
739 .i2c_gate = 0x4b,
740 .tuner_address = 0x61,
741 .tuner_config = 0,
742 .request_firmware = philips_tda1004x_request_firmware
745 static struct tda1004x_config cinergy_ht_pci_config = {
746 .demod_address = 0x08,
747 .invert = 1,
748 .invert_oclk = 0,
749 .xtal_freq = TDA10046_XTAL_16M,
750 .agc_config = TDA10046_AGC_TDA827X,
751 .gpio_config = TDA10046_GP01_I,
752 .if_freq = TDA10046_FREQ_045,
753 .i2c_gate = 0x4b,
754 .tuner_address = 0x60,
755 .tuner_config = 0,
756 .request_firmware = philips_tda1004x_request_firmware
759 static struct tda1004x_config philips_tiger_s_config = {
760 .demod_address = 0x08,
761 .invert = 1,
762 .invert_oclk = 0,
763 .xtal_freq = TDA10046_XTAL_16M,
764 .agc_config = TDA10046_AGC_TDA827X,
765 .gpio_config = TDA10046_GP01_I,
766 .if_freq = TDA10046_FREQ_045,
767 .i2c_gate = 0x4b,
768 .tuner_address = 0x61,
769 .tuner_config = 2,
770 .antenna_switch= 1,
771 .request_firmware = philips_tda1004x_request_firmware
774 static struct tda1004x_config pinnacle_pctv_310i_config = {
775 .demod_address = 0x08,
776 .invert = 1,
777 .invert_oclk = 0,
778 .xtal_freq = TDA10046_XTAL_16M,
779 .agc_config = TDA10046_AGC_TDA827X,
780 .gpio_config = TDA10046_GP11_I,
781 .if_freq = TDA10046_FREQ_045,
782 .i2c_gate = 0x4b,
783 .tuner_address = 0x61,
784 .tuner_config = 1,
785 .request_firmware = philips_tda1004x_request_firmware
788 static struct tda1004x_config hauppauge_hvr_1110_config = {
789 .demod_address = 0x08,
790 .invert = 1,
791 .invert_oclk = 0,
792 .xtal_freq = TDA10046_XTAL_16M,
793 .agc_config = TDA10046_AGC_TDA827X,
794 .gpio_config = TDA10046_GP11_I,
795 .if_freq = TDA10046_FREQ_045,
796 .i2c_gate = 0x4b,
797 .tuner_address = 0x61,
798 .request_firmware = philips_tda1004x_request_firmware
801 static struct tda1004x_config asus_p7131_dual_config = {
802 .demod_address = 0x08,
803 .invert = 1,
804 .invert_oclk = 0,
805 .xtal_freq = TDA10046_XTAL_16M,
806 .agc_config = TDA10046_AGC_TDA827X,
807 .gpio_config = TDA10046_GP11_I,
808 .if_freq = TDA10046_FREQ_045,
809 .i2c_gate = 0x4b,
810 .tuner_address = 0x61,
811 .tuner_config = 0,
812 .antenna_switch= 2,
813 .request_firmware = philips_tda1004x_request_firmware
816 static struct tda1004x_config lifeview_trio_config = {
817 .demod_address = 0x09,
818 .invert = 1,
819 .invert_oclk = 0,
820 .xtal_freq = TDA10046_XTAL_16M,
821 .agc_config = TDA10046_AGC_TDA827X,
822 .gpio_config = TDA10046_GP00_I,
823 .if_freq = TDA10046_FREQ_045,
824 .tuner_address = 0x60,
825 .request_firmware = philips_tda1004x_request_firmware
828 static struct tda1004x_config tevion_dvbt220rf_config = {
829 .demod_address = 0x08,
830 .invert = 1,
831 .invert_oclk = 0,
832 .xtal_freq = TDA10046_XTAL_16M,
833 .agc_config = TDA10046_AGC_TDA827X,
834 .gpio_config = TDA10046_GP11_I,
835 .if_freq = TDA10046_FREQ_045,
836 .tuner_address = 0x60,
837 .request_firmware = philips_tda1004x_request_firmware
840 static struct tda1004x_config md8800_dvbt_config = {
841 .demod_address = 0x08,
842 .invert = 1,
843 .invert_oclk = 0,
844 .xtal_freq = TDA10046_XTAL_16M,
845 .agc_config = TDA10046_AGC_TDA827X,
846 .gpio_config = TDA10046_GP01_I,
847 .if_freq = TDA10046_FREQ_045,
848 .i2c_gate = 0x4b,
849 .tuner_address = 0x60,
850 .tuner_config = 0,
851 .request_firmware = philips_tda1004x_request_firmware
854 /* ------------------------------------------------------------------
855 * special case: this card uses saa713x GPIO22 for the mode switch
858 static int ads_duo_tuner_init(struct dvb_frontend *fe)
860 struct saa7134_dev *dev = fe->dvb->priv;
861 philips_tda827x_tuner_init(fe);
862 /* route TDA8275a AGC input to the channel decoder */
863 saa7134_set_gpio(dev, 22, 1);
864 return 0;
867 static int ads_duo_tuner_sleep(struct dvb_frontend *fe)
869 struct saa7134_dev *dev = fe->dvb->priv;
870 /* route TDA8275a AGC input to the analog IF chip*/
871 saa7134_set_gpio(dev, 22, 0);
872 philips_tda827x_tuner_sleep(fe);
873 return 0;
876 static struct tda827x_config ads_duo_cfg = {
877 .lna_gain = philips_tda827x_lna_gain,
878 .init = ads_duo_tuner_init,
879 .sleep = ads_duo_tuner_sleep
882 static struct tda1004x_config ads_tech_duo_config = {
883 .demod_address = 0x08,
884 .invert = 1,
885 .invert_oclk = 0,
886 .xtal_freq = TDA10046_XTAL_16M,
887 .agc_config = TDA10046_AGC_TDA827X,
888 .gpio_config = TDA10046_GP00_I,
889 .if_freq = TDA10046_FREQ_045,
890 .tuner_address = 0x61,
891 .request_firmware = philips_tda1004x_request_firmware
894 /* ==================================================================
895 * tda10086 based DVB-S cards, helper functions
898 static struct tda10086_config flydvbs = {
899 .demod_address = 0x0e,
900 .invert = 0,
903 /* ==================================================================
904 * nxt200x based ATSC cards, helper functions
907 static struct nxt200x_config avertvhda180 = {
908 .demod_address = 0x0a,
911 static int nxt200x_set_pll_input(u8 *buf, int input)
913 if (input)
914 buf[3] |= 0x08;
915 else
916 buf[3] &= ~0x08;
917 return 0;
920 static struct nxt200x_config kworldatsc110 = {
921 .demod_address = 0x0a,
922 .set_pll_input = nxt200x_set_pll_input,
925 /* ==================================================================
926 * Core code
929 static int dvb_init(struct saa7134_dev *dev)
931 int ret;
932 /* init struct videobuf_dvb */
933 dev->ts.nr_bufs = 32;
934 dev->ts.nr_packets = 32*4;
935 dev->dvb.name = dev->name;
936 videobuf_queue_init(&dev->dvb.dvbq, &saa7134_ts_qops,
937 dev->pci, &dev->slock,
938 V4L2_BUF_TYPE_VIDEO_CAPTURE,
939 V4L2_FIELD_ALTERNATE,
940 sizeof(struct saa7134_buf),
941 dev);
943 switch (dev->board) {
944 case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
945 printk("%s: pinnacle 300i dvb setup\n",dev->name);
946 dev->dvb.frontend = dvb_attach(mt352_attach, &pinnacle_300i,
947 &dev->i2c_adap);
948 if (dev->dvb.frontend) {
949 dev->dvb.frontend->ops.tuner_ops.set_params = mt352_pinnacle_tuner_set_params;
951 break;
952 case SAA7134_BOARD_AVERMEDIA_777:
953 case SAA7134_BOARD_AVERMEDIA_A16AR:
954 printk("%s: avertv 777 dvb setup\n",dev->name);
955 dev->dvb.frontend = dvb_attach(mt352_attach, &avermedia_777,
956 &dev->i2c_adap);
957 if (dev->dvb.frontend) {
958 dev->dvb.frontend->ops.tuner_ops.calc_regs = mt352_aver777_tuner_calc_regs;
960 break;
961 case SAA7134_BOARD_MD7134:
962 dev->dvb.frontend = dvb_attach(tda10046_attach,
963 &medion_cardbus,
964 &dev->i2c_adap);
965 if (dev->dvb.frontend) {
966 dev->dvb.frontend->ops.tuner_ops.init = philips_fmd1216_tuner_init;
967 dev->dvb.frontend->ops.tuner_ops.sleep = philips_fmd1216_tuner_sleep;
968 dev->dvb.frontend->ops.tuner_ops.set_params = philips_fmd1216_tuner_set_params;
970 break;
971 case SAA7134_BOARD_PHILIPS_TOUGH:
972 dev->dvb.frontend = dvb_attach(tda10046_attach,
973 &philips_tu1216_60_config,
974 &dev->i2c_adap);
975 if (dev->dvb.frontend) {
976 dev->dvb.frontend->ops.tuner_ops.init = philips_tu1216_init;
977 dev->dvb.frontend->ops.tuner_ops.set_params = philips_tda6651_pll_set;
979 break;
980 case SAA7134_BOARD_FLYDVBTDUO:
981 case SAA7134_BOARD_FLYDVBT_DUO_CARDBUS:
982 configure_tda827x_fe(dev, &tda827x_lifeview_config);
983 break;
984 case SAA7134_BOARD_PHILIPS_EUROPA:
985 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
986 dev->dvb.frontend = dvb_attach(tda10046_attach,
987 &philips_europa_config,
988 &dev->i2c_adap);
989 if (dev->dvb.frontend) {
990 dev->original_demod_sleep = dev->dvb.frontend->ops.sleep;
991 dev->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
992 dev->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
993 dev->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
994 dev->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
996 break;
997 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
998 dev->dvb.frontend = dvb_attach(tda10046_attach,
999 &philips_tu1216_61_config,
1000 &dev->i2c_adap);
1001 if (dev->dvb.frontend) {
1002 dev->dvb.frontend->ops.tuner_ops.init = philips_tu1216_init;
1003 dev->dvb.frontend->ops.tuner_ops.set_params = philips_tda6651_pll_set;
1005 break;
1006 case SAA7134_BOARD_PHILIPS_TIGER:
1007 configure_tda827x_fe(dev, &philips_tiger_config);
1008 break;
1009 case SAA7134_BOARD_PINNACLE_PCTV_310i:
1010 configure_tda827x_fe(dev, &pinnacle_pctv_310i_config);
1011 break;
1012 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
1013 configure_tda827x_fe(dev, &hauppauge_hvr_1110_config);
1014 break;
1015 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
1016 configure_tda827x_fe(dev, &asus_p7131_dual_config);
1017 break;
1018 case SAA7134_BOARD_FLYDVBT_LR301:
1019 configure_tda827x_fe(dev, &tda827x_lifeview_config);
1020 break;
1021 case SAA7134_BOARD_FLYDVB_TRIO:
1022 if(! use_frontend) { //terrestrial
1023 configure_tda827x_fe(dev, &lifeview_trio_config);
1024 } else { //satellite
1025 dev->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs, &dev->i2c_adap);
1026 if (dev->dvb.frontend) {
1027 if (dvb_attach(tda826x_attach, dev->dvb.frontend, 0x63,
1028 &dev->i2c_adap, 0) == NULL) {
1029 printk("%s: Lifeview Trio, No tda826x found!\n", __FUNCTION__);
1031 if (dvb_attach(isl6421_attach, dev->dvb.frontend, &dev->i2c_adap,
1032 0x08, 0, 0) == NULL) {
1033 printk("%s: Lifeview Trio, No ISL6421 found!\n", __FUNCTION__);
1037 break;
1038 case SAA7134_BOARD_ADS_DUO_CARDBUS_PTV331:
1039 case SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS:
1040 dev->dvb.frontend = dvb_attach(tda10046_attach,
1041 &ads_tech_duo_config,
1042 &dev->i2c_adap);
1043 if (dev->dvb.frontend) {
1044 if (dvb_attach(tda827x_attach,dev->dvb.frontend,
1045 ads_tech_duo_config.tuner_address,
1046 &dev->i2c_adap,&ads_duo_cfg) == NULL) {
1047 printk ("saa7134/dvb: no tda827x tuner found at addr: %02x\n",
1048 ads_tech_duo_config.tuner_address);
1051 break;
1052 case SAA7134_BOARD_TEVION_DVBT_220RF:
1053 configure_tda827x_fe(dev, &tevion_dvbt220rf_config);
1054 break;
1055 case SAA7134_BOARD_MEDION_MD8800_QUADRO:
1056 configure_tda827x_fe(dev, &md8800_dvbt_config);
1057 break;
1058 case SAA7134_BOARD_AVERMEDIA_AVERTVHD_A180:
1059 dev->dvb.frontend = dvb_attach(nxt200x_attach, &avertvhda180,
1060 &dev->i2c_adap);
1061 if (dev->dvb.frontend) {
1062 dvb_attach(dvb_pll_attach, dev->dvb.frontend, 0x61,
1063 NULL, &dvb_pll_tdhu2);
1065 break;
1066 case SAA7134_BOARD_KWORLD_ATSC110:
1067 dev->dvb.frontend = dvb_attach(nxt200x_attach, &kworldatsc110,
1068 &dev->i2c_adap);
1069 if (dev->dvb.frontend) {
1070 dvb_attach(dvb_pll_attach, dev->dvb.frontend, 0x61,
1071 NULL, &dvb_pll_tuv1236d);
1073 break;
1074 case SAA7134_BOARD_FLYDVBS_LR300:
1075 dev->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs,
1076 &dev->i2c_adap);
1077 if (dev->dvb.frontend) {
1078 if (dvb_attach(tda826x_attach, dev->dvb.frontend, 0x60,
1079 &dev->i2c_adap, 0) == NULL) {
1080 printk("%s: No tda826x found!\n", __FUNCTION__);
1082 if (dvb_attach(isl6421_attach, dev->dvb.frontend,
1083 &dev->i2c_adap, 0x08, 0, 0) == NULL) {
1084 printk("%s: No ISL6421 found!\n", __FUNCTION__);
1087 break;
1088 case SAA7134_BOARD_ASUS_EUROPA2_HYBRID:
1089 dev->dvb.frontend = tda10046_attach(&medion_cardbus,
1090 &dev->i2c_adap);
1091 if (dev->dvb.frontend) {
1092 dev->original_demod_sleep = dev->dvb.frontend->ops.sleep;
1093 dev->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1094 dev->dvb.frontend->ops.tuner_ops.init = philips_fmd1216_tuner_init;
1095 dev->dvb.frontend->ops.tuner_ops.sleep = philips_fmd1216_tuner_sleep;
1096 dev->dvb.frontend->ops.tuner_ops.set_params = philips_fmd1216_tuner_set_params;
1098 break;
1099 case SAA7134_BOARD_VIDEOMATE_DVBT_200A:
1100 dev->dvb.frontend = dvb_attach(tda10046_attach,
1101 &philips_europa_config,
1102 &dev->i2c_adap);
1103 if (dev->dvb.frontend) {
1104 dev->dvb.frontend->ops.tuner_ops.init = philips_td1316_tuner_init;
1105 dev->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1107 break;
1108 case SAA7134_BOARD_CINERGY_HT_PCMCIA:
1109 configure_tda827x_fe(dev, &cinergy_ht_config);
1110 break;
1111 case SAA7134_BOARD_CINERGY_HT_PCI:
1112 configure_tda827x_fe(dev, &cinergy_ht_pci_config);
1113 break;
1114 case SAA7134_BOARD_PHILIPS_TIGER_S:
1115 configure_tda827x_fe(dev, &philips_tiger_s_config);
1116 break;
1117 default:
1118 printk("%s: Huh? unknown DVB card?\n",dev->name);
1119 break;
1122 if (NULL == dev->dvb.frontend) {
1123 printk("%s: frontend initialization failed\n",dev->name);
1124 return -1;
1127 /* register everything else */
1128 ret = videobuf_dvb_register(&dev->dvb, THIS_MODULE, dev, &dev->pci->dev);
1130 /* this sequence is necessary to make the tda1004x load its firmware
1131 * and to enter analog mode of hybrid boards
1133 if (!ret) {
1134 if (dev->dvb.frontend->ops.init)
1135 dev->dvb.frontend->ops.init(dev->dvb.frontend);
1136 if (dev->dvb.frontend->ops.sleep)
1137 dev->dvb.frontend->ops.sleep(dev->dvb.frontend);
1139 return ret;
1142 static int dvb_fini(struct saa7134_dev *dev)
1144 static int on = TDA9887_PRESENT | TDA9887_PORT2_INACTIVE;
1146 switch (dev->board) {
1147 case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
1148 /* otherwise we don't detect the tuner on next insmod */
1149 saa7134_i2c_call_clients(dev,TDA9887_SET_CONFIG,&on);
1150 break;
1152 videobuf_dvb_unregister(&dev->dvb);
1153 return 0;
1156 static struct saa7134_mpeg_ops dvb_ops = {
1157 .type = SAA7134_MPEG_DVB,
1158 .init = dvb_init,
1159 .fini = dvb_fini,
1162 static int __init dvb_register(void)
1164 return saa7134_ts_register(&dvb_ops);
1167 static void __exit dvb_unregister(void)
1169 saa7134_ts_unregister(&dvb_ops);
1172 module_init(dvb_register);
1173 module_exit(dvb_unregister);
1175 /* ------------------------------------------------------------------ */
1177 * Local variables:
1178 * c-basic-offset: 8
1179 * End: