V4L/DVB (10823): saa7134: add DVB support for Avermedia A700 cards
[safe/jmp/linux-2.6] / drivers / media / video / saa7134 / saa7134-dvb.c
1 /*
2  *
3  * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
4  *
5  *  Extended 3 / 2005 by Hartmut Hackmann to support various
6  *  cards with the tda10046 DVB-T channel decoder
7  *
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.
12  *
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.
17  *
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.
21  */
22
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>
31
32 #include "saa7134-reg.h"
33 #include "saa7134.h"
34 #include <media/v4l2-common.h>
35 #include "dvb-pll.h"
36 #include <dvb_frontend.h>
37
38 #include "mt352.h"
39 #include "mt352_priv.h" /* FIXME */
40 #include "tda1004x.h"
41 #include "nxt200x.h"
42 #include "tuner-xc2028.h"
43
44 #include "tda10086.h"
45 #include "tda826x.h"
46 #include "tda827x.h"
47 #include "isl6421.h"
48 #include "isl6405.h"
49 #include "lnbp21.h"
50 #include "tuner-simple.h"
51
52 #include "zl10353.h"
53
54 #include "zl10036.h"
55 #include "mt312.h"
56
57 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
58 MODULE_LICENSE("GPL");
59
60 static unsigned int antenna_pwr;
61
62 module_param(antenna_pwr, int, 0444);
63 MODULE_PARM_DESC(antenna_pwr,"enable antenna power (Pinnacle 300i)");
64
65 static int use_frontend;
66 module_param(use_frontend, int, 0644);
67 MODULE_PARM_DESC(use_frontend,"for cards with multiple frontends (0: terrestrial, 1: satellite)");
68
69 static int debug;
70 module_param(debug, int, 0644);
71 MODULE_PARM_DESC(debug, "Turn on/off module debugging (default:off).");
72
73 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
74
75 #define dprintk(fmt, arg...)    do { if (debug) \
76         printk(KERN_DEBUG "%s/dvb: " fmt, dev->name , ## arg); } while(0)
77
78 /* Print a warning */
79 #define wprintk(fmt, arg...) \
80         printk(KERN_WARNING "%s/dvb: " fmt, dev->name, ## arg)
81
82 /* ------------------------------------------------------------------
83  * mt352 based DVB-T cards
84  */
85
86 static int pinnacle_antenna_pwr(struct saa7134_dev *dev, int on)
87 {
88         u32 ok;
89
90         if (!on) {
91                 saa_setl(SAA7134_GPIO_GPMODE0 >> 2,     (1 << 26));
92                 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 26));
93                 return 0;
94         }
95
96         saa_setl(SAA7134_GPIO_GPMODE0 >> 2,     (1 << 26));
97         saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2,   (1 << 26));
98         udelay(10);
99
100         saa_setl(SAA7134_GPIO_GPMODE0 >> 2,     (1 << 28));
101         saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 28));
102         udelay(10);
103         saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2,   (1 << 28));
104         udelay(10);
105         ok = saa_readl(SAA7134_GPIO_GPSTATUS0) & (1 << 27);
106         dprintk("%s %s\n", __func__, ok ? "on" : "off");
107
108         if (!ok)
109                 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2,   (1 << 26));
110         return ok;
111 }
112
113 static int mt352_pinnacle_init(struct dvb_frontend* fe)
114 {
115         static u8 clock_config []  = { CLOCK_CTL,  0x3d, 0x28 };
116         static u8 reset []         = { RESET,      0x80 };
117         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
118         static u8 agc_cfg []       = { AGC_TARGET, 0x28, 0xa0 };
119         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x31 };
120         static u8 fsm_ctl_cfg[]    = { 0x7b,       0x04 };
121         static u8 gpp_ctl_cfg []   = { GPP_CTL,    0x0f };
122         static u8 scan_ctl_cfg []  = { SCAN_CTL,   0x0d };
123         static u8 irq_cfg []       = { INTERRUPT_EN_0, 0x00, 0x00, 0x00, 0x00 };
124         struct saa7134_dev *dev= fe->dvb->priv;
125
126         dprintk("%s called\n", __func__);
127
128         mt352_write(fe, clock_config,   sizeof(clock_config));
129         udelay(200);
130         mt352_write(fe, reset,          sizeof(reset));
131         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
132         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
133         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
134         mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
135
136         mt352_write(fe, fsm_ctl_cfg,    sizeof(fsm_ctl_cfg));
137         mt352_write(fe, scan_ctl_cfg,   sizeof(scan_ctl_cfg));
138         mt352_write(fe, irq_cfg,        sizeof(irq_cfg));
139
140         return 0;
141 }
142
143 static int mt352_aver777_init(struct dvb_frontend* fe)
144 {
145         static u8 clock_config []  = { CLOCK_CTL,  0x38, 0x2d };
146         static u8 reset []         = { RESET,      0x80 };
147         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
148         static u8 agc_cfg []       = { AGC_TARGET, 0x28, 0xa0 };
149         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x33 };
150
151         mt352_write(fe, clock_config,   sizeof(clock_config));
152         udelay(200);
153         mt352_write(fe, reset,          sizeof(reset));
154         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
155         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
156         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
157
158         return 0;
159 }
160
161 static int mt352_avermedia_xc3028_init(struct dvb_frontend *fe)
162 {
163         static u8 clock_config []  = { CLOCK_CTL, 0x38, 0x2d };
164         static u8 reset []         = { RESET, 0x80 };
165         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1, 0x40 };
166         static u8 agc_cfg []       = { AGC_TARGET, 0xe };
167         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x33 };
168
169         mt352_write(fe, clock_config,   sizeof(clock_config));
170         udelay(200);
171         mt352_write(fe, reset,          sizeof(reset));
172         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
173         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
174         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
175         return 0;
176 }
177
178 static int mt352_pinnacle_tuner_set_params(struct dvb_frontend* fe,
179                                            struct dvb_frontend_parameters* params)
180 {
181         u8 off[] = { 0x00, 0xf1};
182         u8 on[]  = { 0x00, 0x71};
183         struct i2c_msg msg = {.addr=0x43, .flags=0, .buf=off, .len = sizeof(off)};
184
185         struct saa7134_dev *dev = fe->dvb->priv;
186         struct v4l2_frequency f;
187
188         /* set frequency (mt2050) */
189         f.tuner     = 0;
190         f.type      = V4L2_TUNER_DIGITAL_TV;
191         f.frequency = params->frequency / 1000 * 16 / 1000;
192         if (fe->ops.i2c_gate_ctrl)
193                 fe->ops.i2c_gate_ctrl(fe, 1);
194         i2c_transfer(&dev->i2c_adap, &msg, 1);
195         saa_call_all(dev, tuner, s_frequency, &f);
196         msg.buf = on;
197         if (fe->ops.i2c_gate_ctrl)
198                 fe->ops.i2c_gate_ctrl(fe, 1);
199         i2c_transfer(&dev->i2c_adap, &msg, 1);
200
201         pinnacle_antenna_pwr(dev, antenna_pwr);
202
203         /* mt352 setup */
204         return mt352_pinnacle_init(fe);
205 }
206
207 static struct mt352_config pinnacle_300i = {
208         .demod_address = 0x3c >> 1,
209         .adc_clock     = 20333,
210         .if2           = 36150,
211         .no_tuner      = 1,
212         .demod_init    = mt352_pinnacle_init,
213 };
214
215 static struct mt352_config avermedia_777 = {
216         .demod_address = 0xf,
217         .demod_init    = mt352_aver777_init,
218 };
219
220 static struct mt352_config avermedia_xc3028_mt352_dev = {
221         .demod_address   = (0x1e >> 1),
222         .no_tuner        = 1,
223         .demod_init      = mt352_avermedia_xc3028_init,
224 };
225
226 /* ==================================================================
227  * tda1004x based DVB-T cards, helper functions
228  */
229
230 static int philips_tda1004x_request_firmware(struct dvb_frontend *fe,
231                                            const struct firmware **fw, char *name)
232 {
233         struct saa7134_dev *dev = fe->dvb->priv;
234         return request_firmware(fw, name, &dev->pci->dev);
235 }
236
237 /* ------------------------------------------------------------------
238  * these tuners are tu1216, td1316(a)
239  */
240
241 static int philips_tda6651_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
242 {
243         struct saa7134_dev *dev = fe->dvb->priv;
244         struct tda1004x_state *state = fe->demodulator_priv;
245         u8 addr = state->config->tuner_address;
246         u8 tuner_buf[4];
247         struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tuner_buf,.len =
248                         sizeof(tuner_buf) };
249         int tuner_frequency = 0;
250         u8 band, cp, filter;
251
252         /* determine charge pump */
253         tuner_frequency = params->frequency + 36166000;
254         if (tuner_frequency < 87000000)
255                 return -EINVAL;
256         else if (tuner_frequency < 130000000)
257                 cp = 3;
258         else if (tuner_frequency < 160000000)
259                 cp = 5;
260         else if (tuner_frequency < 200000000)
261                 cp = 6;
262         else if (tuner_frequency < 290000000)
263                 cp = 3;
264         else if (tuner_frequency < 420000000)
265                 cp = 5;
266         else if (tuner_frequency < 480000000)
267                 cp = 6;
268         else if (tuner_frequency < 620000000)
269                 cp = 3;
270         else if (tuner_frequency < 830000000)
271                 cp = 5;
272         else if (tuner_frequency < 895000000)
273                 cp = 7;
274         else
275                 return -EINVAL;
276
277         /* determine band */
278         if (params->frequency < 49000000)
279                 return -EINVAL;
280         else if (params->frequency < 161000000)
281                 band = 1;
282         else if (params->frequency < 444000000)
283                 band = 2;
284         else if (params->frequency < 861000000)
285                 band = 4;
286         else
287                 return -EINVAL;
288
289         /* setup PLL filter */
290         switch (params->u.ofdm.bandwidth) {
291         case BANDWIDTH_6_MHZ:
292                 filter = 0;
293                 break;
294
295         case BANDWIDTH_7_MHZ:
296                 filter = 0;
297                 break;
298
299         case BANDWIDTH_8_MHZ:
300                 filter = 1;
301                 break;
302
303         default:
304                 return -EINVAL;
305         }
306
307         /* calculate divisor
308          * ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
309          */
310         tuner_frequency = (((params->frequency / 1000) * 6) + 217496) / 1000;
311
312         /* setup tuner buffer */
313         tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
314         tuner_buf[1] = tuner_frequency & 0xff;
315         tuner_buf[2] = 0xca;
316         tuner_buf[3] = (cp << 5) | (filter << 3) | band;
317
318         if (fe->ops.i2c_gate_ctrl)
319                 fe->ops.i2c_gate_ctrl(fe, 1);
320         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1) {
321                 wprintk("could not write to tuner at addr: 0x%02x\n",
322                         addr << 1);
323                 return -EIO;
324         }
325         msleep(1);
326         return 0;
327 }
328
329 static int philips_tu1216_init(struct dvb_frontend *fe)
330 {
331         struct saa7134_dev *dev = fe->dvb->priv;
332         struct tda1004x_state *state = fe->demodulator_priv;
333         u8 addr = state->config->tuner_address;
334         static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
335         struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
336
337         /* setup PLL configuration */
338         if (fe->ops.i2c_gate_ctrl)
339                 fe->ops.i2c_gate_ctrl(fe, 1);
340         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
341                 return -EIO;
342         msleep(1);
343
344         return 0;
345 }
346
347 /* ------------------------------------------------------------------ */
348
349 static struct tda1004x_config philips_tu1216_60_config = {
350         .demod_address = 0x8,
351         .invert        = 1,
352         .invert_oclk   = 0,
353         .xtal_freq     = TDA10046_XTAL_4M,
354         .agc_config    = TDA10046_AGC_DEFAULT,
355         .if_freq       = TDA10046_FREQ_3617,
356         .tuner_address = 0x60,
357         .request_firmware = philips_tda1004x_request_firmware
358 };
359
360 static struct tda1004x_config philips_tu1216_61_config = {
361
362         .demod_address = 0x8,
363         .invert        = 1,
364         .invert_oclk   = 0,
365         .xtal_freq     = TDA10046_XTAL_4M,
366         .agc_config    = TDA10046_AGC_DEFAULT,
367         .if_freq       = TDA10046_FREQ_3617,
368         .tuner_address = 0x61,
369         .request_firmware = philips_tda1004x_request_firmware
370 };
371
372 /* ------------------------------------------------------------------ */
373
374 static int philips_td1316_tuner_init(struct dvb_frontend *fe)
375 {
376         struct saa7134_dev *dev = fe->dvb->priv;
377         struct tda1004x_state *state = fe->demodulator_priv;
378         u8 addr = state->config->tuner_address;
379         static u8 msg[] = { 0x0b, 0xf5, 0x86, 0xab };
380         struct i2c_msg init_msg = {.addr = addr,.flags = 0,.buf = msg,.len = sizeof(msg) };
381
382         /* setup PLL configuration */
383         if (fe->ops.i2c_gate_ctrl)
384                 fe->ops.i2c_gate_ctrl(fe, 1);
385         if (i2c_transfer(&dev->i2c_adap, &init_msg, 1) != 1)
386                 return -EIO;
387         return 0;
388 }
389
390 static int philips_td1316_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
391 {
392         return philips_tda6651_pll_set(fe, params);
393 }
394
395 static int philips_td1316_tuner_sleep(struct dvb_frontend *fe)
396 {
397         struct saa7134_dev *dev = fe->dvb->priv;
398         struct tda1004x_state *state = fe->demodulator_priv;
399         u8 addr = state->config->tuner_address;
400         static u8 msg[] = { 0x0b, 0xdc, 0x86, 0xa4 };
401         struct i2c_msg analog_msg = {.addr = addr,.flags = 0,.buf = msg,.len = sizeof(msg) };
402
403         /* switch the tuner to analog mode */
404         if (fe->ops.i2c_gate_ctrl)
405                 fe->ops.i2c_gate_ctrl(fe, 1);
406         if (i2c_transfer(&dev->i2c_adap, &analog_msg, 1) != 1)
407                 return -EIO;
408         return 0;
409 }
410
411 /* ------------------------------------------------------------------ */
412
413 static int philips_europa_tuner_init(struct dvb_frontend *fe)
414 {
415         struct saa7134_dev *dev = fe->dvb->priv;
416         static u8 msg[] = { 0x00, 0x40};
417         struct i2c_msg init_msg = {.addr = 0x43,.flags = 0,.buf = msg,.len = sizeof(msg) };
418
419
420         if (philips_td1316_tuner_init(fe))
421                 return -EIO;
422         msleep(1);
423         if (i2c_transfer(&dev->i2c_adap, &init_msg, 1) != 1)
424                 return -EIO;
425
426         return 0;
427 }
428
429 static int philips_europa_tuner_sleep(struct dvb_frontend *fe)
430 {
431         struct saa7134_dev *dev = fe->dvb->priv;
432
433         static u8 msg[] = { 0x00, 0x14 };
434         struct i2c_msg analog_msg = {.addr = 0x43,.flags = 0,.buf = msg,.len = sizeof(msg) };
435
436         if (philips_td1316_tuner_sleep(fe))
437                 return -EIO;
438
439         /* switch the board to analog mode */
440         if (fe->ops.i2c_gate_ctrl)
441                 fe->ops.i2c_gate_ctrl(fe, 1);
442         i2c_transfer(&dev->i2c_adap, &analog_msg, 1);
443         return 0;
444 }
445
446 static int philips_europa_demod_sleep(struct dvb_frontend *fe)
447 {
448         struct saa7134_dev *dev = fe->dvb->priv;
449
450         if (dev->original_demod_sleep)
451                 dev->original_demod_sleep(fe);
452         fe->ops.i2c_gate_ctrl(fe, 1);
453         return 0;
454 }
455
456 static struct tda1004x_config philips_europa_config = {
457
458         .demod_address = 0x8,
459         .invert        = 0,
460         .invert_oclk   = 0,
461         .xtal_freq     = TDA10046_XTAL_4M,
462         .agc_config    = TDA10046_AGC_IFO_AUTO_POS,
463         .if_freq       = TDA10046_FREQ_052,
464         .tuner_address = 0x61,
465         .request_firmware = philips_tda1004x_request_firmware
466 };
467
468 static struct tda1004x_config medion_cardbus = {
469         .demod_address = 0x08,
470         .invert        = 1,
471         .invert_oclk   = 0,
472         .xtal_freq     = TDA10046_XTAL_16M,
473         .agc_config    = TDA10046_AGC_IFO_AUTO_NEG,
474         .if_freq       = TDA10046_FREQ_3613,
475         .tuner_address = 0x61,
476         .request_firmware = philips_tda1004x_request_firmware
477 };
478
479 /* ------------------------------------------------------------------
480  * tda 1004x based cards with philips silicon tuner
481  */
482
483 static int tda8290_i2c_gate_ctrl( struct dvb_frontend* fe, int enable)
484 {
485         struct tda1004x_state *state = fe->demodulator_priv;
486
487         u8 addr = state->config->i2c_gate;
488         static u8 tda8290_close[] = { 0x21, 0xc0};
489         static u8 tda8290_open[]  = { 0x21, 0x80};
490         struct i2c_msg tda8290_msg = {.addr = addr,.flags = 0, .len = 2};
491         if (enable) {
492                 tda8290_msg.buf = tda8290_close;
493         } else {
494                 tda8290_msg.buf = tda8290_open;
495         }
496         if (i2c_transfer(state->i2c, &tda8290_msg, 1) != 1) {
497                 struct saa7134_dev *dev = fe->dvb->priv;
498                 wprintk("could not access tda8290 I2C gate\n");
499                 return -EIO;
500         }
501         msleep(20);
502         return 0;
503 }
504
505 static int philips_tda827x_tuner_init(struct dvb_frontend *fe)
506 {
507         struct saa7134_dev *dev = fe->dvb->priv;
508         struct tda1004x_state *state = fe->demodulator_priv;
509
510         switch (state->config->antenna_switch) {
511         case 0: break;
512         case 1: dprintk("setting GPIO21 to 0 (TV antenna?)\n");
513                 saa7134_set_gpio(dev, 21, 0);
514                 break;
515         case 2: dprintk("setting GPIO21 to 1 (Radio antenna?)\n");
516                 saa7134_set_gpio(dev, 21, 1);
517                 break;
518         }
519         return 0;
520 }
521
522 static int philips_tda827x_tuner_sleep(struct dvb_frontend *fe)
523 {
524         struct saa7134_dev *dev = fe->dvb->priv;
525         struct tda1004x_state *state = fe->demodulator_priv;
526
527         switch (state->config->antenna_switch) {
528         case 0: break;
529         case 1: dprintk("setting GPIO21 to 1 (Radio antenna?)\n");
530                 saa7134_set_gpio(dev, 21, 1);
531                 break;
532         case 2: dprintk("setting GPIO21 to 0 (TV antenna?)\n");
533                 saa7134_set_gpio(dev, 21, 0);
534                 break;
535         }
536         return 0;
537 }
538
539 static int configure_tda827x_fe(struct saa7134_dev *dev,
540                                 struct tda1004x_config *cdec_conf,
541                                 struct tda827x_config *tuner_conf)
542 {
543         struct videobuf_dvb_frontend *fe0;
544
545         /* Get the first frontend */
546         fe0 = videobuf_dvb_get_frontend(&dev->frontends, 1);
547
548         fe0->dvb.frontend = dvb_attach(tda10046_attach, cdec_conf, &dev->i2c_adap);
549         if (fe0->dvb.frontend) {
550                 if (cdec_conf->i2c_gate)
551                         fe0->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
552                 if (dvb_attach(tda827x_attach, fe0->dvb.frontend,
553                                cdec_conf->tuner_address,
554                                &dev->i2c_adap, tuner_conf))
555                         return 0;
556
557                 wprintk("no tda827x tuner found at addr: %02x\n",
558                                 cdec_conf->tuner_address);
559         }
560         return -EINVAL;
561 }
562
563 /* ------------------------------------------------------------------ */
564
565 static struct tda827x_config tda827x_cfg_0 = {
566         .init = philips_tda827x_tuner_init,
567         .sleep = philips_tda827x_tuner_sleep,
568         .config = 0,
569         .switch_addr = 0
570 };
571
572 static struct tda827x_config tda827x_cfg_1 = {
573         .init = philips_tda827x_tuner_init,
574         .sleep = philips_tda827x_tuner_sleep,
575         .config = 1,
576         .switch_addr = 0x4b
577 };
578
579 static struct tda827x_config tda827x_cfg_2 = {
580         .init = philips_tda827x_tuner_init,
581         .sleep = philips_tda827x_tuner_sleep,
582         .config = 2,
583         .switch_addr = 0x4b
584 };
585
586 static struct tda827x_config tda827x_cfg_2_sw42 = {
587         .init = philips_tda827x_tuner_init,
588         .sleep = philips_tda827x_tuner_sleep,
589         .config = 2,
590         .switch_addr = 0x42
591 };
592
593 /* ------------------------------------------------------------------ */
594
595 static struct tda1004x_config tda827x_lifeview_config = {
596         .demod_address = 0x08,
597         .invert        = 1,
598         .invert_oclk   = 0,
599         .xtal_freq     = TDA10046_XTAL_16M,
600         .agc_config    = TDA10046_AGC_TDA827X,
601         .gpio_config   = TDA10046_GP11_I,
602         .if_freq       = TDA10046_FREQ_045,
603         .tuner_address = 0x60,
604         .request_firmware = philips_tda1004x_request_firmware
605 };
606
607 static struct tda1004x_config philips_tiger_config = {
608         .demod_address = 0x08,
609         .invert        = 1,
610         .invert_oclk   = 0,
611         .xtal_freq     = TDA10046_XTAL_16M,
612         .agc_config    = TDA10046_AGC_TDA827X,
613         .gpio_config   = TDA10046_GP11_I,
614         .if_freq       = TDA10046_FREQ_045,
615         .i2c_gate      = 0x4b,
616         .tuner_address = 0x61,
617         .antenna_switch= 1,
618         .request_firmware = philips_tda1004x_request_firmware
619 };
620
621 static struct tda1004x_config cinergy_ht_config = {
622         .demod_address = 0x08,
623         .invert        = 1,
624         .invert_oclk   = 0,
625         .xtal_freq     = TDA10046_XTAL_16M,
626         .agc_config    = TDA10046_AGC_TDA827X,
627         .gpio_config   = TDA10046_GP01_I,
628         .if_freq       = TDA10046_FREQ_045,
629         .i2c_gate      = 0x4b,
630         .tuner_address = 0x61,
631         .request_firmware = philips_tda1004x_request_firmware
632 };
633
634 static struct tda1004x_config cinergy_ht_pci_config = {
635         .demod_address = 0x08,
636         .invert        = 1,
637         .invert_oclk   = 0,
638         .xtal_freq     = TDA10046_XTAL_16M,
639         .agc_config    = TDA10046_AGC_TDA827X,
640         .gpio_config   = TDA10046_GP01_I,
641         .if_freq       = TDA10046_FREQ_045,
642         .i2c_gate      = 0x4b,
643         .tuner_address = 0x60,
644         .request_firmware = philips_tda1004x_request_firmware
645 };
646
647 static struct tda1004x_config philips_tiger_s_config = {
648         .demod_address = 0x08,
649         .invert        = 1,
650         .invert_oclk   = 0,
651         .xtal_freq     = TDA10046_XTAL_16M,
652         .agc_config    = TDA10046_AGC_TDA827X,
653         .gpio_config   = TDA10046_GP01_I,
654         .if_freq       = TDA10046_FREQ_045,
655         .i2c_gate      = 0x4b,
656         .tuner_address = 0x61,
657         .antenna_switch= 1,
658         .request_firmware = philips_tda1004x_request_firmware
659 };
660
661 static struct tda1004x_config pinnacle_pctv_310i_config = {
662         .demod_address = 0x08,
663         .invert        = 1,
664         .invert_oclk   = 0,
665         .xtal_freq     = TDA10046_XTAL_16M,
666         .agc_config    = TDA10046_AGC_TDA827X,
667         .gpio_config   = TDA10046_GP11_I,
668         .if_freq       = TDA10046_FREQ_045,
669         .i2c_gate      = 0x4b,
670         .tuner_address = 0x61,
671         .request_firmware = philips_tda1004x_request_firmware
672 };
673
674 static struct tda1004x_config hauppauge_hvr_1110_config = {
675         .demod_address = 0x08,
676         .invert        = 1,
677         .invert_oclk   = 0,
678         .xtal_freq     = TDA10046_XTAL_16M,
679         .agc_config    = TDA10046_AGC_TDA827X,
680         .gpio_config   = TDA10046_GP11_I,
681         .if_freq       = TDA10046_FREQ_045,
682         .i2c_gate      = 0x4b,
683         .tuner_address = 0x61,
684         .request_firmware = philips_tda1004x_request_firmware
685 };
686
687 static struct tda1004x_config asus_p7131_dual_config = {
688         .demod_address = 0x08,
689         .invert        = 1,
690         .invert_oclk   = 0,
691         .xtal_freq     = TDA10046_XTAL_16M,
692         .agc_config    = TDA10046_AGC_TDA827X,
693         .gpio_config   = TDA10046_GP11_I,
694         .if_freq       = TDA10046_FREQ_045,
695         .i2c_gate      = 0x4b,
696         .tuner_address = 0x61,
697         .antenna_switch= 2,
698         .request_firmware = philips_tda1004x_request_firmware
699 };
700
701 static struct tda1004x_config lifeview_trio_config = {
702         .demod_address = 0x09,
703         .invert        = 1,
704         .invert_oclk   = 0,
705         .xtal_freq     = TDA10046_XTAL_16M,
706         .agc_config    = TDA10046_AGC_TDA827X,
707         .gpio_config   = TDA10046_GP00_I,
708         .if_freq       = TDA10046_FREQ_045,
709         .tuner_address = 0x60,
710         .request_firmware = philips_tda1004x_request_firmware
711 };
712
713 static struct tda1004x_config tevion_dvbt220rf_config = {
714         .demod_address = 0x08,
715         .invert        = 1,
716         .invert_oclk   = 0,
717         .xtal_freq     = TDA10046_XTAL_16M,
718         .agc_config    = TDA10046_AGC_TDA827X,
719         .gpio_config   = TDA10046_GP11_I,
720         .if_freq       = TDA10046_FREQ_045,
721         .tuner_address = 0x60,
722         .request_firmware = philips_tda1004x_request_firmware
723 };
724
725 static struct tda1004x_config md8800_dvbt_config = {
726         .demod_address = 0x08,
727         .invert        = 1,
728         .invert_oclk   = 0,
729         .xtal_freq     = TDA10046_XTAL_16M,
730         .agc_config    = TDA10046_AGC_TDA827X,
731         .gpio_config   = TDA10046_GP01_I,
732         .if_freq       = TDA10046_FREQ_045,
733         .i2c_gate      = 0x4b,
734         .tuner_address = 0x60,
735         .request_firmware = philips_tda1004x_request_firmware
736 };
737
738 static struct tda1004x_config asus_p7131_4871_config = {
739         .demod_address = 0x08,
740         .invert        = 1,
741         .invert_oclk   = 0,
742         .xtal_freq     = TDA10046_XTAL_16M,
743         .agc_config    = TDA10046_AGC_TDA827X,
744         .gpio_config   = TDA10046_GP01_I,
745         .if_freq       = TDA10046_FREQ_045,
746         .i2c_gate      = 0x4b,
747         .tuner_address = 0x61,
748         .antenna_switch= 2,
749         .request_firmware = philips_tda1004x_request_firmware
750 };
751
752 static struct tda1004x_config asus_p7131_hybrid_lna_config = {
753         .demod_address = 0x08,
754         .invert        = 1,
755         .invert_oclk   = 0,
756         .xtal_freq     = TDA10046_XTAL_16M,
757         .agc_config    = TDA10046_AGC_TDA827X,
758         .gpio_config   = TDA10046_GP11_I,
759         .if_freq       = TDA10046_FREQ_045,
760         .i2c_gate      = 0x4b,
761         .tuner_address = 0x61,
762         .antenna_switch= 2,
763         .request_firmware = philips_tda1004x_request_firmware
764 };
765
766 static struct tda1004x_config kworld_dvb_t_210_config = {
767         .demod_address = 0x08,
768         .invert        = 1,
769         .invert_oclk   = 0,
770         .xtal_freq     = TDA10046_XTAL_16M,
771         .agc_config    = TDA10046_AGC_TDA827X,
772         .gpio_config   = TDA10046_GP11_I,
773         .if_freq       = TDA10046_FREQ_045,
774         .i2c_gate      = 0x4b,
775         .tuner_address = 0x61,
776         .antenna_switch= 1,
777         .request_firmware = philips_tda1004x_request_firmware
778 };
779
780 static struct tda1004x_config avermedia_super_007_config = {
781         .demod_address = 0x08,
782         .invert        = 1,
783         .invert_oclk   = 0,
784         .xtal_freq     = TDA10046_XTAL_16M,
785         .agc_config    = TDA10046_AGC_TDA827X,
786         .gpio_config   = TDA10046_GP01_I,
787         .if_freq       = TDA10046_FREQ_045,
788         .i2c_gate      = 0x4b,
789         .tuner_address = 0x60,
790         .antenna_switch= 1,
791         .request_firmware = philips_tda1004x_request_firmware
792 };
793
794 static struct tda1004x_config twinhan_dtv_dvb_3056_config = {
795         .demod_address = 0x08,
796         .invert        = 1,
797         .invert_oclk   = 0,
798         .xtal_freq     = TDA10046_XTAL_16M,
799         .agc_config    = TDA10046_AGC_TDA827X,
800         .gpio_config   = TDA10046_GP01_I,
801         .if_freq       = TDA10046_FREQ_045,
802         .i2c_gate      = 0x42,
803         .tuner_address = 0x61,
804         .antenna_switch = 1,
805         .request_firmware = philips_tda1004x_request_firmware
806 };
807
808 static struct tda1004x_config asus_tiger_3in1_config = {
809         .demod_address = 0x0b,
810         .invert        = 1,
811         .invert_oclk   = 0,
812         .xtal_freq     = TDA10046_XTAL_16M,
813         .agc_config    = TDA10046_AGC_TDA827X,
814         .gpio_config   = TDA10046_GP11_I,
815         .if_freq       = TDA10046_FREQ_045,
816         .i2c_gate      = 0x4b,
817         .tuner_address = 0x61,
818         .antenna_switch = 1,
819         .request_firmware = philips_tda1004x_request_firmware
820 };
821
822 /* ------------------------------------------------------------------
823  * special case: this card uses saa713x GPIO22 for the mode switch
824  */
825
826 static int ads_duo_tuner_init(struct dvb_frontend *fe)
827 {
828         struct saa7134_dev *dev = fe->dvb->priv;
829         philips_tda827x_tuner_init(fe);
830         /* route TDA8275a AGC input to the channel decoder */
831         saa7134_set_gpio(dev, 22, 1);
832         return 0;
833 }
834
835 static int ads_duo_tuner_sleep(struct dvb_frontend *fe)
836 {
837         struct saa7134_dev *dev = fe->dvb->priv;
838         /* route TDA8275a AGC input to the analog IF chip*/
839         saa7134_set_gpio(dev, 22, 0);
840         philips_tda827x_tuner_sleep(fe);
841         return 0;
842 }
843
844 static struct tda827x_config ads_duo_cfg = {
845         .init = ads_duo_tuner_init,
846         .sleep = ads_duo_tuner_sleep,
847         .config = 0
848 };
849
850 static struct tda1004x_config ads_tech_duo_config = {
851         .demod_address = 0x08,
852         .invert        = 1,
853         .invert_oclk   = 0,
854         .xtal_freq     = TDA10046_XTAL_16M,
855         .agc_config    = TDA10046_AGC_TDA827X,
856         .gpio_config   = TDA10046_GP00_I,
857         .if_freq       = TDA10046_FREQ_045,
858         .tuner_address = 0x61,
859         .request_firmware = philips_tda1004x_request_firmware
860 };
861
862 static struct zl10353_config behold_h6_config = {
863         .demod_address = 0x1e>>1,
864         .no_tuner      = 1,
865         .parallel_ts   = 1,
866         .disable_i2c_gate_ctrl = 1,
867 };
868
869 /* ==================================================================
870  * tda10086 based DVB-S cards, helper functions
871  */
872
873 static struct tda10086_config flydvbs = {
874         .demod_address = 0x0e,
875         .invert = 0,
876         .diseqc_tone = 0,
877         .xtal_freq = TDA10086_XTAL_16M,
878 };
879
880 static struct tda10086_config sd1878_4m = {
881         .demod_address = 0x0e,
882         .invert = 0,
883         .diseqc_tone = 0,
884         .xtal_freq = TDA10086_XTAL_4M,
885 };
886
887 /* ------------------------------------------------------------------
888  * special case: lnb supply is connected to the gated i2c
889  */
890
891 static int md8800_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
892 {
893         int res = -EIO;
894         struct saa7134_dev *dev = fe->dvb->priv;
895         if (fe->ops.i2c_gate_ctrl) {
896                 fe->ops.i2c_gate_ctrl(fe, 1);
897                 if (dev->original_set_voltage)
898                         res = dev->original_set_voltage(fe, voltage);
899                 fe->ops.i2c_gate_ctrl(fe, 0);
900         }
901         return res;
902 };
903
904 static int md8800_set_high_voltage(struct dvb_frontend *fe, long arg)
905 {
906         int res = -EIO;
907         struct saa7134_dev *dev = fe->dvb->priv;
908         if (fe->ops.i2c_gate_ctrl) {
909                 fe->ops.i2c_gate_ctrl(fe, 1);
910                 if (dev->original_set_high_voltage)
911                         res = dev->original_set_high_voltage(fe, arg);
912                 fe->ops.i2c_gate_ctrl(fe, 0);
913         }
914         return res;
915 };
916
917 static int md8800_set_voltage2(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
918 {
919         struct saa7134_dev *dev = fe->dvb->priv;
920         u8 wbuf[2] = { 0x1f, 00 };
921         u8 rbuf;
922         struct i2c_msg msg[] = { { .addr = 0x08, .flags = 0, .buf = wbuf, .len = 1 },
923                                  { .addr = 0x08, .flags = I2C_M_RD, .buf = &rbuf, .len = 1 } };
924
925         if (i2c_transfer(&dev->i2c_adap, msg, 2) != 2)
926                 return -EIO;
927         /* NOTE: this assumes that gpo1 is used, it might be bit 5 (gpo2) */
928         if (voltage == SEC_VOLTAGE_18)
929                 wbuf[1] = rbuf | 0x10;
930         else
931                 wbuf[1] = rbuf & 0xef;
932         msg[0].len = 2;
933         i2c_transfer(&dev->i2c_adap, msg, 1);
934         return 0;
935 }
936
937 static int md8800_set_high_voltage2(struct dvb_frontend *fe, long arg)
938 {
939         struct saa7134_dev *dev = fe->dvb->priv;
940         wprintk("%s: sorry can't set high LNB supply voltage from here\n", __func__);
941         return -EIO;
942 }
943
944 /* ==================================================================
945  * nxt200x based ATSC cards, helper functions
946  */
947
948 static struct nxt200x_config avertvhda180 = {
949         .demod_address    = 0x0a,
950 };
951
952 static struct nxt200x_config kworldatsc110 = {
953         .demod_address    = 0x0a,
954 };
955
956 /* ------------------------------------------------------------------ */
957
958 static struct mt312_config avertv_a700_mt312 = {
959         .demod_address = 0x0e,
960         .voltage_inverted = 1,
961 };
962
963 static struct zl10036_config avertv_a700_tuner = {
964         .tuner_address = 0x60,
965 };
966
967 /* ==================================================================
968  * Core code
969  */
970
971 static int dvb_init(struct saa7134_dev *dev)
972 {
973         int ret;
974         int attach_xc3028 = 0;
975         struct videobuf_dvb_frontend *fe0;
976
977         /* FIXME: add support for multi-frontend */
978         mutex_init(&dev->frontends.lock);
979         INIT_LIST_HEAD(&dev->frontends.felist);
980
981         printk(KERN_INFO "%s() allocating 1 frontend\n", __func__);
982         fe0 = videobuf_dvb_alloc_frontend(&dev->frontends, 1);
983         if (!fe0) {
984                 printk(KERN_ERR "%s() failed to alloc\n", __func__);
985                 return -ENOMEM;
986         }
987
988         /* init struct videobuf_dvb */
989         dev->ts.nr_bufs    = 32;
990         dev->ts.nr_packets = 32*4;
991         fe0->dvb.name = dev->name;
992         videobuf_queue_sg_init(&fe0->dvb.dvbq, &saa7134_ts_qops,
993                             &dev->pci->dev, &dev->slock,
994                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
995                             V4L2_FIELD_ALTERNATE,
996                             sizeof(struct saa7134_buf),
997                             dev);
998
999         switch (dev->board) {
1000         case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
1001                 dprintk("pinnacle 300i dvb setup\n");
1002                 fe0->dvb.frontend = dvb_attach(mt352_attach, &pinnacle_300i,
1003                                                &dev->i2c_adap);
1004                 if (fe0->dvb.frontend) {
1005                         fe0->dvb.frontend->ops.tuner_ops.set_params = mt352_pinnacle_tuner_set_params;
1006                 }
1007                 break;
1008         case SAA7134_BOARD_AVERMEDIA_777:
1009         case SAA7134_BOARD_AVERMEDIA_A16AR:
1010                 dprintk("avertv 777 dvb setup\n");
1011                 fe0->dvb.frontend = dvb_attach(mt352_attach, &avermedia_777,
1012                                                &dev->i2c_adap);
1013                 if (fe0->dvb.frontend) {
1014                         dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1015                                    &dev->i2c_adap, 0x61,
1016                                    TUNER_PHILIPS_TD1316);
1017                 }
1018                 break;
1019         case SAA7134_BOARD_AVERMEDIA_A16D:
1020                 dprintk("AverMedia A16D dvb setup\n");
1021                 fe0->dvb.frontend = dvb_attach(mt352_attach,
1022                                                 &avermedia_xc3028_mt352_dev,
1023                                                 &dev->i2c_adap);
1024                 attach_xc3028 = 1;
1025                 break;
1026         case SAA7134_BOARD_MD7134:
1027                 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1028                                                &medion_cardbus,
1029                                                &dev->i2c_adap);
1030                 if (fe0->dvb.frontend) {
1031                         dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1032                                    &dev->i2c_adap, medion_cardbus.tuner_address,
1033                                    TUNER_PHILIPS_FMD1216ME_MK3);
1034                 }
1035                 break;
1036         case SAA7134_BOARD_PHILIPS_TOUGH:
1037                 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1038                                                &philips_tu1216_60_config,
1039                                                &dev->i2c_adap);
1040                 if (fe0->dvb.frontend) {
1041                         fe0->dvb.frontend->ops.tuner_ops.init = philips_tu1216_init;
1042                         fe0->dvb.frontend->ops.tuner_ops.set_params = philips_tda6651_pll_set;
1043                 }
1044                 break;
1045         case SAA7134_BOARD_FLYDVBTDUO:
1046         case SAA7134_BOARD_FLYDVBT_DUO_CARDBUS:
1047                 if (configure_tda827x_fe(dev, &tda827x_lifeview_config,
1048                                          &tda827x_cfg_0) < 0)
1049                         goto dettach_frontend;
1050                 break;
1051         case SAA7134_BOARD_PHILIPS_EUROPA:
1052         case SAA7134_BOARD_VIDEOMATE_DVBT_300:
1053                 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1054                                                &philips_europa_config,
1055                                                &dev->i2c_adap);
1056                 if (fe0->dvb.frontend) {
1057                         dev->original_demod_sleep = fe0->dvb.frontend->ops.sleep;
1058                         fe0->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1059                         fe0->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1060                         fe0->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1061                         fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1062                 }
1063                 break;
1064         case SAA7134_BOARD_VIDEOMATE_DVBT_200:
1065                 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1066                                                &philips_tu1216_61_config,
1067                                                &dev->i2c_adap);
1068                 if (fe0->dvb.frontend) {
1069                         fe0->dvb.frontend->ops.tuner_ops.init = philips_tu1216_init;
1070                         fe0->dvb.frontend->ops.tuner_ops.set_params = philips_tda6651_pll_set;
1071                 }
1072                 break;
1073         case SAA7134_BOARD_KWORLD_DVBT_210:
1074                 if (configure_tda827x_fe(dev, &kworld_dvb_t_210_config,
1075                                          &tda827x_cfg_2) < 0)
1076                         goto dettach_frontend;
1077                 break;
1078         case SAA7134_BOARD_PHILIPS_TIGER:
1079                 if (configure_tda827x_fe(dev, &philips_tiger_config,
1080                                          &tda827x_cfg_0) < 0)
1081                         goto dettach_frontend;
1082                 break;
1083         case SAA7134_BOARD_PINNACLE_PCTV_310i:
1084                 if (configure_tda827x_fe(dev, &pinnacle_pctv_310i_config,
1085                                          &tda827x_cfg_1) < 0)
1086                         goto dettach_frontend;
1087                 break;
1088         case SAA7134_BOARD_HAUPPAUGE_HVR1110:
1089                 if (configure_tda827x_fe(dev, &hauppauge_hvr_1110_config,
1090                                          &tda827x_cfg_1) < 0)
1091                         goto dettach_frontend;
1092                 break;
1093         case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
1094                 if (configure_tda827x_fe(dev, &asus_p7131_dual_config,
1095                                          &tda827x_cfg_0) < 0)
1096                         goto dettach_frontend;
1097                 break;
1098         case SAA7134_BOARD_FLYDVBT_LR301:
1099                 if (configure_tda827x_fe(dev, &tda827x_lifeview_config,
1100                                          &tda827x_cfg_0) < 0)
1101                         goto dettach_frontend;
1102                 break;
1103         case SAA7134_BOARD_FLYDVB_TRIO:
1104                 if (!use_frontend) {    /* terrestrial */
1105                         if (configure_tda827x_fe(dev, &lifeview_trio_config,
1106                                                  &tda827x_cfg_0) < 0)
1107                                 goto dettach_frontend;
1108                 } else {                /* satellite */
1109                         fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs, &dev->i2c_adap);
1110                         if (fe0->dvb.frontend) {
1111                                 if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x63,
1112                                                                         &dev->i2c_adap, 0) == NULL) {
1113                                         wprintk("%s: Lifeview Trio, No tda826x found!\n", __func__);
1114                                         goto dettach_frontend;
1115                                 }
1116                                 if (dvb_attach(isl6421_attach, fe0->dvb.frontend, &dev->i2c_adap,
1117                                                                                 0x08, 0, 0) == NULL) {
1118                                         wprintk("%s: Lifeview Trio, No ISL6421 found!\n", __func__);
1119                                         goto dettach_frontend;
1120                                 }
1121                         }
1122                 }
1123                 break;
1124         case SAA7134_BOARD_ADS_DUO_CARDBUS_PTV331:
1125         case SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS:
1126                 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1127                                                &ads_tech_duo_config,
1128                                                &dev->i2c_adap);
1129                 if (fe0->dvb.frontend) {
1130                         if (dvb_attach(tda827x_attach,fe0->dvb.frontend,
1131                                    ads_tech_duo_config.tuner_address, &dev->i2c_adap,
1132                                                                 &ads_duo_cfg) == NULL) {
1133                                 wprintk("no tda827x tuner found at addr: %02x\n",
1134                                         ads_tech_duo_config.tuner_address);
1135                                 goto dettach_frontend;
1136                         }
1137                 } else
1138                         wprintk("failed to attach tda10046\n");
1139                 break;
1140         case SAA7134_BOARD_TEVION_DVBT_220RF:
1141                 if (configure_tda827x_fe(dev, &tevion_dvbt220rf_config,
1142                                          &tda827x_cfg_0) < 0)
1143                         goto dettach_frontend;
1144                 break;
1145         case SAA7134_BOARD_MEDION_MD8800_QUADRO:
1146                 if (!use_frontend) {     /* terrestrial */
1147                         if (configure_tda827x_fe(dev, &md8800_dvbt_config,
1148                                                  &tda827x_cfg_0) < 0)
1149                                 goto dettach_frontend;
1150                 } else {        /* satellite */
1151                         fe0->dvb.frontend = dvb_attach(tda10086_attach,
1152                                                         &flydvbs, &dev->i2c_adap);
1153                         if (fe0->dvb.frontend) {
1154                                 struct dvb_frontend *fe = fe0->dvb.frontend;
1155                                 u8 dev_id = dev->eedata[2];
1156                                 u8 data = 0xc4;
1157                                 struct i2c_msg msg = {.addr = 0x08, .flags = 0, .len = 1};
1158
1159                                 if (dvb_attach(tda826x_attach, fe0->dvb.frontend,
1160                                                 0x60, &dev->i2c_adap, 0) == NULL) {
1161                                         wprintk("%s: Medion Quadro, no tda826x "
1162                                                 "found !\n", __func__);
1163                                         goto dettach_frontend;
1164                                 }
1165                                 if (dev_id != 0x08) {
1166                                         /* we need to open the i2c gate (we know it exists) */
1167                                         fe->ops.i2c_gate_ctrl(fe, 1);
1168                                         if (dvb_attach(isl6405_attach, fe,
1169                                                         &dev->i2c_adap, 0x08, 0, 0) == NULL) {
1170                                                 wprintk("%s: Medion Quadro, no ISL6405 "
1171                                                         "found !\n", __func__);
1172                                                 goto dettach_frontend;
1173                                         }
1174                                         if (dev_id == 0x07) {
1175                                                 /* fire up the 2nd section of the LNB supply since
1176                                                    we can't do this from the other section */
1177                                                 msg.buf = &data;
1178                                                 i2c_transfer(&dev->i2c_adap, &msg, 1);
1179                                         }
1180                                         fe->ops.i2c_gate_ctrl(fe, 0);
1181                                         dev->original_set_voltage = fe->ops.set_voltage;
1182                                         fe->ops.set_voltage = md8800_set_voltage;
1183                                         dev->original_set_high_voltage = fe->ops.enable_high_lnb_voltage;
1184                                         fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage;
1185                                 } else {
1186                                         fe->ops.set_voltage = md8800_set_voltage2;
1187                                         fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage2;
1188                                 }
1189                         }
1190                 }
1191                 break;
1192         case SAA7134_BOARD_AVERMEDIA_AVERTVHD_A180:
1193                 fe0->dvb.frontend = dvb_attach(nxt200x_attach, &avertvhda180,
1194                                                &dev->i2c_adap);
1195                 if (fe0->dvb.frontend)
1196                         dvb_attach(dvb_pll_attach, fe0->dvb.frontend, 0x61,
1197                                    NULL, DVB_PLL_TDHU2);
1198                 break;
1199         case SAA7134_BOARD_ADS_INSTANT_HDTV_PCI:
1200         case SAA7134_BOARD_KWORLD_ATSC110:
1201                 fe0->dvb.frontend = dvb_attach(nxt200x_attach, &kworldatsc110,
1202                                                &dev->i2c_adap);
1203                 if (fe0->dvb.frontend)
1204                         dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1205                                    &dev->i2c_adap, 0x61,
1206                                    TUNER_PHILIPS_TUV1236D);
1207                 break;
1208         case SAA7134_BOARD_FLYDVBS_LR300:
1209                 fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs,
1210                                                &dev->i2c_adap);
1211                 if (fe0->dvb.frontend) {
1212                         if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x60,
1213                                        &dev->i2c_adap, 0) == NULL) {
1214                                 wprintk("%s: No tda826x found!\n", __func__);
1215                                 goto dettach_frontend;
1216                         }
1217                         if (dvb_attach(isl6421_attach, fe0->dvb.frontend,
1218                                        &dev->i2c_adap, 0x08, 0, 0) == NULL) {
1219                                 wprintk("%s: No ISL6421 found!\n", __func__);
1220                                 goto dettach_frontend;
1221                         }
1222                 }
1223                 break;
1224         case SAA7134_BOARD_ASUS_EUROPA2_HYBRID:
1225                 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1226                                                &medion_cardbus,
1227                                                &dev->i2c_adap);
1228                 if (fe0->dvb.frontend) {
1229                         dev->original_demod_sleep = fe0->dvb.frontend->ops.sleep;
1230                         fe0->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1231
1232                         dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1233                                    &dev->i2c_adap, medion_cardbus.tuner_address,
1234                                    TUNER_PHILIPS_FMD1216ME_MK3);
1235                 }
1236                 break;
1237         case SAA7134_BOARD_VIDEOMATE_DVBT_200A:
1238                 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1239                                 &philips_europa_config,
1240                                 &dev->i2c_adap);
1241                 if (fe0->dvb.frontend) {
1242                         fe0->dvb.frontend->ops.tuner_ops.init = philips_td1316_tuner_init;
1243                         fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1244                 }
1245                 break;
1246         case SAA7134_BOARD_CINERGY_HT_PCMCIA:
1247                 if (configure_tda827x_fe(dev, &cinergy_ht_config,
1248                                          &tda827x_cfg_0) < 0)
1249                         goto dettach_frontend;
1250                 break;
1251         case SAA7134_BOARD_CINERGY_HT_PCI:
1252                 if (configure_tda827x_fe(dev, &cinergy_ht_pci_config,
1253                                          &tda827x_cfg_0) < 0)
1254                         goto dettach_frontend;
1255                 break;
1256         case SAA7134_BOARD_PHILIPS_TIGER_S:
1257                 if (configure_tda827x_fe(dev, &philips_tiger_s_config,
1258                                          &tda827x_cfg_2) < 0)
1259                         goto dettach_frontend;
1260                 break;
1261         case SAA7134_BOARD_ASUS_P7131_4871:
1262                 if (configure_tda827x_fe(dev, &asus_p7131_4871_config,
1263                                          &tda827x_cfg_2) < 0)
1264                         goto dettach_frontend;
1265                 break;
1266         case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
1267                 if (configure_tda827x_fe(dev, &asus_p7131_hybrid_lna_config,
1268                                          &tda827x_cfg_2) < 0)
1269                         goto dettach_frontend;
1270                 break;
1271         case SAA7134_BOARD_AVERMEDIA_SUPER_007:
1272                 if (configure_tda827x_fe(dev, &avermedia_super_007_config,
1273                                          &tda827x_cfg_0) < 0)
1274                         goto dettach_frontend;
1275                 break;
1276         case SAA7134_BOARD_TWINHAN_DTV_DVB_3056:
1277                 if (configure_tda827x_fe(dev, &twinhan_dtv_dvb_3056_config,
1278                                          &tda827x_cfg_2_sw42) < 0)
1279                         goto dettach_frontend;
1280                 break;
1281         case SAA7134_BOARD_PHILIPS_SNAKE:
1282                 fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs,
1283                                                 &dev->i2c_adap);
1284                 if (fe0->dvb.frontend) {
1285                         if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x60,
1286                                         &dev->i2c_adap, 0) == NULL) {
1287                                 wprintk("%s: No tda826x found!\n", __func__);
1288                                 goto dettach_frontend;
1289                         }
1290                         if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1291                                         &dev->i2c_adap, 0, 0) == NULL) {
1292                                 wprintk("%s: No lnbp21 found!\n", __func__);
1293                                 goto dettach_frontend;
1294                         }
1295                 }
1296                 break;
1297         case SAA7134_BOARD_CREATIX_CTX953:
1298                 if (configure_tda827x_fe(dev, &md8800_dvbt_config,
1299                                          &tda827x_cfg_0) < 0)
1300                         goto dettach_frontend;
1301                 break;
1302         case SAA7134_BOARD_MSI_TVANYWHERE_AD11:
1303                 if (configure_tda827x_fe(dev, &philips_tiger_s_config,
1304                                          &tda827x_cfg_2) < 0)
1305                         goto dettach_frontend;
1306                 break;
1307         case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
1308                 dprintk("AverMedia E506R dvb setup\n");
1309                 saa7134_set_gpio(dev, 25, 0);
1310                 msleep(10);
1311                 saa7134_set_gpio(dev, 25, 1);
1312                 fe0->dvb.frontend = dvb_attach(mt352_attach,
1313                                                 &avermedia_xc3028_mt352_dev,
1314                                                 &dev->i2c_adap);
1315                 attach_xc3028 = 1;
1316                 break;
1317         case SAA7134_BOARD_MD7134_BRIDGE_2:
1318                 fe0->dvb.frontend = dvb_attach(tda10086_attach,
1319                                                 &sd1878_4m, &dev->i2c_adap);
1320                 if (fe0->dvb.frontend) {
1321                         struct dvb_frontend *fe;
1322                         if (dvb_attach(dvb_pll_attach, fe0->dvb.frontend, 0x60,
1323                                   &dev->i2c_adap, DVB_PLL_PHILIPS_SD1878_TDA8261) == NULL) {
1324                                 wprintk("%s: MD7134 DVB-S, no SD1878 "
1325                                         "found !\n", __func__);
1326                                 goto dettach_frontend;
1327                         }
1328                         /* we need to open the i2c gate (we know it exists) */
1329                         fe = fe0->dvb.frontend;
1330                         fe->ops.i2c_gate_ctrl(fe, 1);
1331                         if (dvb_attach(isl6405_attach, fe,
1332                                         &dev->i2c_adap, 0x08, 0, 0) == NULL) {
1333                                 wprintk("%s: MD7134 DVB-S, no ISL6405 "
1334                                         "found !\n", __func__);
1335                                 goto dettach_frontend;
1336                         }
1337                         fe->ops.i2c_gate_ctrl(fe, 0);
1338                         dev->original_set_voltage = fe->ops.set_voltage;
1339                         fe->ops.set_voltage = md8800_set_voltage;
1340                         dev->original_set_high_voltage = fe->ops.enable_high_lnb_voltage;
1341                         fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage;
1342                 }
1343                 break;
1344         case SAA7134_BOARD_AVERMEDIA_M103:
1345                 saa7134_set_gpio(dev, 25, 0);
1346                 msleep(10);
1347                 saa7134_set_gpio(dev, 25, 1);
1348                 fe0->dvb.frontend = dvb_attach(mt352_attach,
1349                                                 &avermedia_xc3028_mt352_dev,
1350                                                 &dev->i2c_adap);
1351                 attach_xc3028 = 1;
1352                 break;
1353         case SAA7134_BOARD_ASUSTeK_TIGER_3IN1:
1354                 if (!use_frontend) {     /* terrestrial */
1355                         if (configure_tda827x_fe(dev, &asus_tiger_3in1_config,
1356                                                         &tda827x_cfg_2) < 0)
1357                                 goto dettach_frontend;
1358                 } else {                /* satellite */
1359                         fe0->dvb.frontend = dvb_attach(tda10086_attach,
1360                                                 &flydvbs, &dev->i2c_adap);
1361                         if (fe0->dvb.frontend) {
1362                                 if (dvb_attach(tda826x_attach,
1363                                                 fe0->dvb.frontend, 0x60,
1364                                                 &dev->i2c_adap, 0) == NULL) {
1365                                         wprintk("%s: Asus Tiger 3in1, no "
1366                                                 "tda826x found!\n", __func__);
1367                                         goto dettach_frontend;
1368                                 }
1369                                 if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1370                                                 &dev->i2c_adap, 0, 0) == NULL) {
1371                                         wprintk("%s: Asus Tiger 3in1, no lnbp21"
1372                                                 " found!\n", __func__);
1373                                         goto dettach_frontend;
1374                                 }
1375                         }
1376                 }
1377                 break;
1378         case SAA7134_BOARD_ASUSTeK_TIGER:
1379                 if (configure_tda827x_fe(dev, &philips_tiger_config,
1380                                          &tda827x_cfg_0) < 0)
1381                         goto dettach_frontend;
1382                 break;
1383         case SAA7134_BOARD_BEHOLD_H6:
1384                 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1385                                                 &behold_h6_config,
1386                                                 &dev->i2c_adap);
1387                 if (fe0->dvb.frontend) {
1388                         dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1389                                    &dev->i2c_adap, 0x61,
1390                                    TUNER_PHILIPS_FMD1216ME_MK3);
1391                 }
1392                 break;
1393         case SAA7134_BOARD_AVERMEDIA_A700_PRO:
1394         case SAA7134_BOARD_AVERMEDIA_A700_HYBRID:
1395                 /* Zarlink ZL10313 */
1396                 fe0->dvb.frontend = dvb_attach(mt312_attach,
1397                         &avertv_a700_mt312, &dev->i2c_adap);
1398                 if (fe0->dvb.frontend) {
1399                         if (dvb_attach(zl10036_attach, fe0->dvb.frontend,
1400                                         &avertv_a700_tuner, &dev->i2c_adap) == NULL) {
1401                                 wprintk("%s: No zl10036 found!\n",
1402                                         __func__);
1403                         }
1404                 }
1405                 break;
1406         default:
1407                 wprintk("Huh? unknown DVB card?\n");
1408                 break;
1409         }
1410
1411         if (attach_xc3028) {
1412                 struct dvb_frontend *fe;
1413                 struct xc2028_config cfg = {
1414                         .i2c_adap  = &dev->i2c_adap,
1415                         .i2c_addr  = 0x61,
1416                 };
1417
1418                 if (!fe0->dvb.frontend)
1419                         goto dettach_frontend;
1420
1421                 fe = dvb_attach(xc2028_attach, fe0->dvb.frontend, &cfg);
1422                 if (!fe) {
1423                         printk(KERN_ERR "%s/2: xc3028 attach failed\n",
1424                                dev->name);
1425                         goto dettach_frontend;
1426                 }
1427         }
1428
1429         if (NULL == fe0->dvb.frontend) {
1430                 printk(KERN_ERR "%s/dvb: frontend initialization failed\n", dev->name);
1431                 goto dettach_frontend;
1432         }
1433         /* define general-purpose callback pointer */
1434         fe0->dvb.frontend->callback = saa7134_tuner_callback;
1435
1436         /* register everything else */
1437         ret = videobuf_dvb_register_bus(&dev->frontends, THIS_MODULE, dev,
1438                 &dev->pci->dev, adapter_nr, 0);
1439
1440         /* this sequence is necessary to make the tda1004x load its firmware
1441          * and to enter analog mode of hybrid boards
1442          */
1443         if (!ret) {
1444                 if (fe0->dvb.frontend->ops.init)
1445                         fe0->dvb.frontend->ops.init(fe0->dvb.frontend);
1446                 if (fe0->dvb.frontend->ops.sleep)
1447                         fe0->dvb.frontend->ops.sleep(fe0->dvb.frontend);
1448                 if (fe0->dvb.frontend->ops.tuner_ops.sleep)
1449                         fe0->dvb.frontend->ops.tuner_ops.sleep(fe0->dvb.frontend);
1450         }
1451         return ret;
1452
1453 dettach_frontend:
1454         videobuf_dvb_dealloc_frontends(&dev->frontends);
1455         return -EINVAL;
1456 }
1457
1458 static int dvb_fini(struct saa7134_dev *dev)
1459 {
1460         struct videobuf_dvb_frontend *fe0;
1461
1462         /* Get the first frontend */
1463         fe0 = videobuf_dvb_get_frontend(&dev->frontends, 1);
1464         if (!fe0)
1465                 return -EINVAL;
1466
1467         /* FIXME: I suspect that this code is bogus, since the entry for
1468            Pinnacle 300I DVB-T PAL already defines the proper init to allow
1469            the detection of mt2032 (TDA9887_PORT2_INACTIVE)
1470          */
1471         if (dev->board == SAA7134_BOARD_PINNACLE_300I_DVBT_PAL) {
1472                 struct v4l2_priv_tun_config tda9887_cfg;
1473                 static int on  = TDA9887_PRESENT | TDA9887_PORT2_INACTIVE;
1474
1475                 tda9887_cfg.tuner = TUNER_TDA9887;
1476                 tda9887_cfg.priv  = &on;
1477
1478                 /* otherwise we don't detect the tuner on next insmod */
1479                 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1480         } else if (dev->board == SAA7134_BOARD_MEDION_MD8800_QUADRO) {
1481                 if ((dev->eedata[2] == 0x07) && use_frontend) {
1482                         /* turn off the 2nd lnb supply */
1483                         u8 data = 0x80;
1484                         struct i2c_msg msg = {.addr = 0x08, .buf = &data, .flags = 0, .len = 1};
1485                         struct dvb_frontend *fe;
1486                         fe = fe0->dvb.frontend;
1487                         if (fe->ops.i2c_gate_ctrl) {
1488                                 fe->ops.i2c_gate_ctrl(fe, 1);
1489                                 i2c_transfer(&dev->i2c_adap, &msg, 1);
1490                                 fe->ops.i2c_gate_ctrl(fe, 0);
1491                         }
1492                 }
1493         }
1494         videobuf_dvb_unregister_bus(&dev->frontends);
1495         return 0;
1496 }
1497
1498 static struct saa7134_mpeg_ops dvb_ops = {
1499         .type          = SAA7134_MPEG_DVB,
1500         .init          = dvb_init,
1501         .fini          = dvb_fini,
1502 };
1503
1504 static int __init dvb_register(void)
1505 {
1506         return saa7134_ts_register(&dvb_ops);
1507 }
1508
1509 static void __exit dvb_unregister(void)
1510 {
1511         saa7134_ts_unregister(&dvb_ops);
1512 }
1513
1514 module_init(dvb_register);
1515 module_exit(dvb_unregister);
1516
1517 /* ------------------------------------------------------------------ */
1518 /*
1519  * Local variables:
1520  * c-basic-offset: 8
1521  * End:
1522  */