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