d47fb22e12f276a9837b3f8498e1f6cf38aded49
[linux-2.6-block.git] / drivers / media / pci / 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 "saa7134.h"
24 #include "saa7134-reg.h"
25
26 #include <linux/init.h>
27 #include <linux/list.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/delay.h>
31 #include <linux/kthread.h>
32 #include <linux/suspend.h>
33
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 #include "xc5000.h"
44
45 #include "tda10086.h"
46 #include "tda826x.h"
47 #include "tda827x.h"
48 #include "isl6421.h"
49 #include "isl6405.h"
50 #include "lnbp21.h"
51 #include "tuner-simple.h"
52 #include "tda10048.h"
53 #include "tda18271.h"
54 #include "lgdt3305.h"
55 #include "tda8290.h"
56 #include "mb86a20s.h"
57 #include "lgs8gxx.h"
58
59 #include "zl10353.h"
60 #include "qt1010.h"
61
62 #include "zl10036.h"
63 #include "zl10039.h"
64 #include "mt312.h"
65 #include "s5h1411.h"
66
67 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
68 MODULE_LICENSE("GPL");
69
70 static unsigned int antenna_pwr;
71
72 module_param(antenna_pwr, int, 0444);
73 MODULE_PARM_DESC(antenna_pwr,"enable antenna power (Pinnacle 300i)");
74
75 static int use_frontend;
76 module_param(use_frontend, int, 0644);
77 MODULE_PARM_DESC(use_frontend,"for cards with multiple frontends (0: terrestrial, 1: satellite)");
78
79 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
80
81 /* ------------------------------------------------------------------
82  * mt352 based DVB-T cards
83  */
84
85 static int pinnacle_antenna_pwr(struct saa7134_dev *dev, int on)
86 {
87         u32 ok;
88
89         if (!on) {
90                 saa_setl(SAA7134_GPIO_GPMODE0 >> 2,     (1 << 26));
91                 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 26));
92                 return 0;
93         }
94
95         saa_setl(SAA7134_GPIO_GPMODE0 >> 2,     (1 << 26));
96         saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2,   (1 << 26));
97         udelay(10);
98
99         saa_setl(SAA7134_GPIO_GPMODE0 >> 2,     (1 << 28));
100         saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 28));
101         udelay(10);
102         saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2,   (1 << 28));
103         udelay(10);
104         ok = saa_readl(SAA7134_GPIO_GPSTATUS0) & (1 << 27);
105         pr_debug("%s %s\n", __func__, ok ? "on" : "off");
106
107         if (!ok)
108                 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2,   (1 << 26));
109         return ok;
110 }
111
112 static int mt352_pinnacle_init(struct dvb_frontend* fe)
113 {
114         static u8 clock_config []  = { CLOCK_CTL,  0x3d, 0x28 };
115         static u8 reset []         = { RESET,      0x80 };
116         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
117         static u8 agc_cfg []       = { AGC_TARGET, 0x28, 0xa0 };
118         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x31 };
119         static u8 fsm_ctl_cfg[]    = { 0x7b,       0x04 };
120         static u8 gpp_ctl_cfg []   = { GPP_CTL,    0x0f };
121         static u8 scan_ctl_cfg []  = { SCAN_CTL,   0x0d };
122         static u8 irq_cfg []       = { INTERRUPT_EN_0, 0x00, 0x00, 0x00, 0x00 };
123
124         pr_debug("%s called\n", __func__);
125
126         mt352_write(fe, clock_config,   sizeof(clock_config));
127         udelay(200);
128         mt352_write(fe, reset,          sizeof(reset));
129         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
130         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
131         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
132         mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
133
134         mt352_write(fe, fsm_ctl_cfg,    sizeof(fsm_ctl_cfg));
135         mt352_write(fe, scan_ctl_cfg,   sizeof(scan_ctl_cfg));
136         mt352_write(fe, irq_cfg,        sizeof(irq_cfg));
137
138         return 0;
139 }
140
141 static int mt352_aver777_init(struct dvb_frontend* fe)
142 {
143         static u8 clock_config []  = { CLOCK_CTL,  0x38, 0x2d };
144         static u8 reset []         = { RESET,      0x80 };
145         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
146         static u8 agc_cfg []       = { AGC_TARGET, 0x28, 0xa0 };
147         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x33 };
148
149         mt352_write(fe, clock_config,   sizeof(clock_config));
150         udelay(200);
151         mt352_write(fe, reset,          sizeof(reset));
152         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
153         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
154         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
155
156         return 0;
157 }
158
159 static int mt352_avermedia_xc3028_init(struct dvb_frontend *fe)
160 {
161         static u8 clock_config []  = { CLOCK_CTL, 0x38, 0x2d };
162         static u8 reset []         = { RESET, 0x80 };
163         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1, 0x40 };
164         static u8 agc_cfg []       = { AGC_TARGET, 0xe };
165         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x33 };
166
167         mt352_write(fe, clock_config,   sizeof(clock_config));
168         udelay(200);
169         mt352_write(fe, reset,          sizeof(reset));
170         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
171         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
172         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
173         return 0;
174 }
175
176 static int mt352_pinnacle_tuner_set_params(struct dvb_frontend *fe)
177 {
178         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
179         u8 off[] = { 0x00, 0xf1};
180         u8 on[]  = { 0x00, 0x71};
181         struct i2c_msg msg = {.addr=0x43, .flags=0, .buf=off, .len = sizeof(off)};
182
183         struct saa7134_dev *dev = fe->dvb->priv;
184         struct v4l2_frequency f;
185
186         /* set frequency (mt2050) */
187         f.tuner     = 0;
188         f.type      = V4L2_TUNER_DIGITAL_TV;
189         f.frequency = c->frequency / 1000 * 16 / 1000;
190         if (fe->ops.i2c_gate_ctrl)
191                 fe->ops.i2c_gate_ctrl(fe, 1);
192         i2c_transfer(&dev->i2c_adap, &msg, 1);
193         saa_call_all(dev, tuner, s_frequency, &f);
194         msg.buf = on;
195         if (fe->ops.i2c_gate_ctrl)
196                 fe->ops.i2c_gate_ctrl(fe, 1);
197         i2c_transfer(&dev->i2c_adap, &msg, 1);
198
199         pinnacle_antenna_pwr(dev, antenna_pwr);
200
201         /* mt352 setup */
202         return mt352_pinnacle_init(fe);
203 }
204
205 static struct mt352_config pinnacle_300i = {
206         .demod_address = 0x3c >> 1,
207         .adc_clock     = 20333,
208         .if2           = 36150,
209         .no_tuner      = 1,
210         .demod_init    = mt352_pinnacle_init,
211 };
212
213 static struct mt352_config avermedia_777 = {
214         .demod_address = 0xf,
215         .demod_init    = mt352_aver777_init,
216 };
217
218 static struct mt352_config avermedia_xc3028_mt352_dev = {
219         .demod_address   = (0x1e >> 1),
220         .no_tuner        = 1,
221         .demod_init      = mt352_avermedia_xc3028_init,
222 };
223
224 static struct tda18271_std_map mb86a20s_tda18271_std_map = {
225         .dvbt_6   = { .if_freq = 3300, .agc_mode = 3, .std = 4,
226                       .if_lvl = 7, .rfagc_top = 0x37, },
227 };
228
229 static struct tda18271_config kworld_tda18271_config = {
230         .std_map = &mb86a20s_tda18271_std_map,
231         .gate    = TDA18271_GATE_DIGITAL,
232         .config  = 3,   /* Use tuner callback for AGC */
233
234 };
235
236 static const struct mb86a20s_config kworld_mb86a20s_config = {
237         .demod_address = 0x10,
238 };
239
240 static int kworld_sbtvd_gate_ctrl(struct dvb_frontend* fe, int enable)
241 {
242         struct saa7134_dev *dev = fe->dvb->priv;
243
244         unsigned char initmsg[] = {0x45, 0x97};
245         unsigned char msg_enable[] = {0x45, 0xc1};
246         unsigned char msg_disable[] = {0x45, 0x81};
247         struct i2c_msg msg = {.addr = 0x4b, .flags = 0, .buf = initmsg, .len = 2};
248
249         if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1) {
250                 pr_warn("could not access the I2C gate\n");
251                 return -EIO;
252         }
253         if (enable)
254                 msg.buf = msg_enable;
255         else
256                 msg.buf = msg_disable;
257         if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1) {
258                 pr_warn("could not access the I2C gate\n");
259                 return -EIO;
260         }
261         msleep(20);
262         return 0;
263 }
264
265 /* ==================================================================
266  * tda1004x based DVB-T cards, helper functions
267  */
268
269 static int philips_tda1004x_request_firmware(struct dvb_frontend *fe,
270                                            const struct firmware **fw, char *name)
271 {
272         struct saa7134_dev *dev = fe->dvb->priv;
273         return request_firmware(fw, name, &dev->pci->dev);
274 }
275
276 /* ------------------------------------------------------------------
277  * these tuners are tu1216, td1316(a)
278  */
279
280 static int philips_tda6651_pll_set(struct dvb_frontend *fe)
281 {
282         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
283         struct saa7134_dev *dev = fe->dvb->priv;
284         struct tda1004x_state *state = fe->demodulator_priv;
285         u8 addr = state->config->tuner_address;
286         u8 tuner_buf[4];
287         struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tuner_buf,.len =
288                         sizeof(tuner_buf) };
289         int tuner_frequency = 0;
290         u8 band, cp, filter;
291
292         /* determine charge pump */
293         tuner_frequency = c->frequency + 36166000;
294         if (tuner_frequency < 87000000)
295                 return -EINVAL;
296         else if (tuner_frequency < 130000000)
297                 cp = 3;
298         else if (tuner_frequency < 160000000)
299                 cp = 5;
300         else if (tuner_frequency < 200000000)
301                 cp = 6;
302         else if (tuner_frequency < 290000000)
303                 cp = 3;
304         else if (tuner_frequency < 420000000)
305                 cp = 5;
306         else if (tuner_frequency < 480000000)
307                 cp = 6;
308         else if (tuner_frequency < 620000000)
309                 cp = 3;
310         else if (tuner_frequency < 830000000)
311                 cp = 5;
312         else if (tuner_frequency < 895000000)
313                 cp = 7;
314         else
315                 return -EINVAL;
316
317         /* determine band */
318         if (c->frequency < 49000000)
319                 return -EINVAL;
320         else if (c->frequency < 161000000)
321                 band = 1;
322         else if (c->frequency < 444000000)
323                 band = 2;
324         else if (c->frequency < 861000000)
325                 band = 4;
326         else
327                 return -EINVAL;
328
329         /* setup PLL filter */
330         switch (c->bandwidth_hz) {
331         case 6000000:
332                 filter = 0;
333                 break;
334
335         case 7000000:
336                 filter = 0;
337                 break;
338
339         case 8000000:
340                 filter = 1;
341                 break;
342
343         default:
344                 return -EINVAL;
345         }
346
347         /* calculate divisor
348          * ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
349          */
350         tuner_frequency = (((c->frequency / 1000) * 6) + 217496) / 1000;
351
352         /* setup tuner buffer */
353         tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
354         tuner_buf[1] = tuner_frequency & 0xff;
355         tuner_buf[2] = 0xca;
356         tuner_buf[3] = (cp << 5) | (filter << 3) | band;
357
358         if (fe->ops.i2c_gate_ctrl)
359                 fe->ops.i2c_gate_ctrl(fe, 1);
360         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1) {
361                 pr_warn("could not write to tuner at addr: 0x%02x\n",
362                         addr << 1);
363                 return -EIO;
364         }
365         msleep(1);
366         return 0;
367 }
368
369 static int philips_tu1216_init(struct dvb_frontend *fe)
370 {
371         struct saa7134_dev *dev = fe->dvb->priv;
372         struct tda1004x_state *state = fe->demodulator_priv;
373         u8 addr = state->config->tuner_address;
374         static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
375         struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
376
377         /* setup PLL configuration */
378         if (fe->ops.i2c_gate_ctrl)
379                 fe->ops.i2c_gate_ctrl(fe, 1);
380         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
381                 return -EIO;
382         msleep(1);
383
384         return 0;
385 }
386
387 /* ------------------------------------------------------------------ */
388
389 static struct tda1004x_config philips_tu1216_60_config = {
390         .demod_address = 0x8,
391         .invert        = 1,
392         .invert_oclk   = 0,
393         .xtal_freq     = TDA10046_XTAL_4M,
394         .agc_config    = TDA10046_AGC_DEFAULT,
395         .if_freq       = TDA10046_FREQ_3617,
396         .tuner_address = 0x60,
397         .request_firmware = philips_tda1004x_request_firmware
398 };
399
400 static struct tda1004x_config philips_tu1216_61_config = {
401
402         .demod_address = 0x8,
403         .invert        = 1,
404         .invert_oclk   = 0,
405         .xtal_freq     = TDA10046_XTAL_4M,
406         .agc_config    = TDA10046_AGC_DEFAULT,
407         .if_freq       = TDA10046_FREQ_3617,
408         .tuner_address = 0x61,
409         .request_firmware = philips_tda1004x_request_firmware
410 };
411
412 /* ------------------------------------------------------------------ */
413
414 static int philips_td1316_tuner_init(struct dvb_frontend *fe)
415 {
416         struct saa7134_dev *dev = fe->dvb->priv;
417         struct tda1004x_state *state = fe->demodulator_priv;
418         u8 addr = state->config->tuner_address;
419         static u8 msg[] = { 0x0b, 0xf5, 0x86, 0xab };
420         struct i2c_msg init_msg = {.addr = addr,.flags = 0,.buf = msg,.len = sizeof(msg) };
421
422         /* setup PLL configuration */
423         if (fe->ops.i2c_gate_ctrl)
424                 fe->ops.i2c_gate_ctrl(fe, 1);
425         if (i2c_transfer(&dev->i2c_adap, &init_msg, 1) != 1)
426                 return -EIO;
427         return 0;
428 }
429
430 static int philips_td1316_tuner_set_params(struct dvb_frontend *fe)
431 {
432         return philips_tda6651_pll_set(fe);
433 }
434
435 static int philips_td1316_tuner_sleep(struct dvb_frontend *fe)
436 {
437         struct saa7134_dev *dev = fe->dvb->priv;
438         struct tda1004x_state *state = fe->demodulator_priv;
439         u8 addr = state->config->tuner_address;
440         static u8 msg[] = { 0x0b, 0xdc, 0x86, 0xa4 };
441         struct i2c_msg analog_msg = {.addr = addr,.flags = 0,.buf = msg,.len = sizeof(msg) };
442
443         /* switch the tuner to analog mode */
444         if (fe->ops.i2c_gate_ctrl)
445                 fe->ops.i2c_gate_ctrl(fe, 1);
446         if (i2c_transfer(&dev->i2c_adap, &analog_msg, 1) != 1)
447                 return -EIO;
448         return 0;
449 }
450
451 /* ------------------------------------------------------------------ */
452
453 static int philips_europa_tuner_init(struct dvb_frontend *fe)
454 {
455         struct saa7134_dev *dev = fe->dvb->priv;
456         static u8 msg[] = { 0x00, 0x40};
457         struct i2c_msg init_msg = {.addr = 0x43,.flags = 0,.buf = msg,.len = sizeof(msg) };
458
459
460         if (philips_td1316_tuner_init(fe))
461                 return -EIO;
462         msleep(1);
463         if (i2c_transfer(&dev->i2c_adap, &init_msg, 1) != 1)
464                 return -EIO;
465
466         return 0;
467 }
468
469 static int philips_europa_tuner_sleep(struct dvb_frontend *fe)
470 {
471         struct saa7134_dev *dev = fe->dvb->priv;
472
473         static u8 msg[] = { 0x00, 0x14 };
474         struct i2c_msg analog_msg = {.addr = 0x43,.flags = 0,.buf = msg,.len = sizeof(msg) };
475
476         if (philips_td1316_tuner_sleep(fe))
477                 return -EIO;
478
479         /* switch the board to analog mode */
480         if (fe->ops.i2c_gate_ctrl)
481                 fe->ops.i2c_gate_ctrl(fe, 1);
482         i2c_transfer(&dev->i2c_adap, &analog_msg, 1);
483         return 0;
484 }
485
486 static int philips_europa_demod_sleep(struct dvb_frontend *fe)
487 {
488         struct saa7134_dev *dev = fe->dvb->priv;
489
490         if (dev->original_demod_sleep)
491                 dev->original_demod_sleep(fe);
492         fe->ops.i2c_gate_ctrl(fe, 1);
493         return 0;
494 }
495
496 static struct tda1004x_config philips_europa_config = {
497
498         .demod_address = 0x8,
499         .invert        = 0,
500         .invert_oclk   = 0,
501         .xtal_freq     = TDA10046_XTAL_4M,
502         .agc_config    = TDA10046_AGC_IFO_AUTO_POS,
503         .if_freq       = TDA10046_FREQ_052,
504         .tuner_address = 0x61,
505         .request_firmware = philips_tda1004x_request_firmware
506 };
507
508 static struct tda1004x_config medion_cardbus = {
509         .demod_address = 0x08,
510         .invert        = 1,
511         .invert_oclk   = 0,
512         .xtal_freq     = TDA10046_XTAL_16M,
513         .agc_config    = TDA10046_AGC_IFO_AUTO_NEG,
514         .if_freq       = TDA10046_FREQ_3613,
515         .tuner_address = 0x61,
516         .request_firmware = philips_tda1004x_request_firmware
517 };
518
519 static struct tda1004x_config technotrend_budget_t3000_config = {
520         .demod_address = 0x8,
521         .invert        = 1,
522         .invert_oclk   = 0,
523         .xtal_freq     = TDA10046_XTAL_4M,
524         .agc_config    = TDA10046_AGC_DEFAULT,
525         .if_freq       = TDA10046_FREQ_3617,
526         .tuner_address = 0x63,
527         .request_firmware = philips_tda1004x_request_firmware
528 };
529
530 /* ------------------------------------------------------------------
531  * tda 1004x based cards with philips silicon tuner
532  */
533
534 static int tda8290_i2c_gate_ctrl( struct dvb_frontend* fe, int enable)
535 {
536         struct tda1004x_state *state = fe->demodulator_priv;
537
538         u8 addr = state->config->i2c_gate;
539         static u8 tda8290_close[] = { 0x21, 0xc0};
540         static u8 tda8290_open[]  = { 0x21, 0x80};
541         struct i2c_msg tda8290_msg = {.addr = addr,.flags = 0, .len = 2};
542         if (enable) {
543                 tda8290_msg.buf = tda8290_close;
544         } else {
545                 tda8290_msg.buf = tda8290_open;
546         }
547         if (i2c_transfer(state->i2c, &tda8290_msg, 1) != 1) {
548                 pr_warn("could not access tda8290 I2C gate\n");
549                 return -EIO;
550         }
551         msleep(20);
552         return 0;
553 }
554
555 static int philips_tda827x_tuner_init(struct dvb_frontend *fe)
556 {
557         struct saa7134_dev *dev = fe->dvb->priv;
558         struct tda1004x_state *state = fe->demodulator_priv;
559
560         switch (state->config->antenna_switch) {
561         case 0:
562                 break;
563         case 1:
564                 pr_debug("setting GPIO21 to 0 (TV antenna?)\n");
565                 saa7134_set_gpio(dev, 21, 0);
566                 break;
567         case 2:
568                 pr_debug("setting GPIO21 to 1 (Radio antenna?)\n");
569                 saa7134_set_gpio(dev, 21, 1);
570                 break;
571         }
572         return 0;
573 }
574
575 static int philips_tda827x_tuner_sleep(struct dvb_frontend *fe)
576 {
577         struct saa7134_dev *dev = fe->dvb->priv;
578         struct tda1004x_state *state = fe->demodulator_priv;
579
580         switch (state->config->antenna_switch) {
581         case 0:
582                 break;
583         case 1:
584                 pr_debug("setting GPIO21 to 1 (Radio antenna?)\n");
585                 saa7134_set_gpio(dev, 21, 1);
586                 break;
587         case 2:
588                 pr_debug("setting GPIO21 to 0 (TV antenna?)\n");
589                 saa7134_set_gpio(dev, 21, 0);
590                 break;
591         }
592         return 0;
593 }
594
595 static int configure_tda827x_fe(struct saa7134_dev *dev,
596                                 struct tda1004x_config *cdec_conf,
597                                 struct tda827x_config *tuner_conf)
598 {
599         struct vb2_dvb_frontend *fe0;
600
601         /* Get the first frontend */
602         fe0 = vb2_dvb_get_frontend(&dev->frontends, 1);
603
604         if (!fe0)
605                 return -EINVAL;
606
607         fe0->dvb.frontend = dvb_attach(tda10046_attach, cdec_conf, &dev->i2c_adap);
608         if (fe0->dvb.frontend) {
609                 if (cdec_conf->i2c_gate)
610                         fe0->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
611                 if (dvb_attach(tda827x_attach, fe0->dvb.frontend,
612                                cdec_conf->tuner_address,
613                                &dev->i2c_adap, tuner_conf))
614                         return 0;
615
616                 pr_warn("no tda827x tuner found at addr: %02x\n",
617                                 cdec_conf->tuner_address);
618         }
619         return -EINVAL;
620 }
621
622 /* ------------------------------------------------------------------ */
623
624 static struct tda827x_config tda827x_cfg_0 = {
625         .init = philips_tda827x_tuner_init,
626         .sleep = philips_tda827x_tuner_sleep,
627         .config = 0,
628         .switch_addr = 0
629 };
630
631 static struct tda827x_config tda827x_cfg_1 = {
632         .init = philips_tda827x_tuner_init,
633         .sleep = philips_tda827x_tuner_sleep,
634         .config = 1,
635         .switch_addr = 0x4b
636 };
637
638 static struct tda827x_config tda827x_cfg_2 = {
639         .init = philips_tda827x_tuner_init,
640         .sleep = philips_tda827x_tuner_sleep,
641         .config = 2,
642         .switch_addr = 0x4b
643 };
644
645 static struct tda827x_config tda827x_cfg_2_sw42 = {
646         .init = philips_tda827x_tuner_init,
647         .sleep = philips_tda827x_tuner_sleep,
648         .config = 2,
649         .switch_addr = 0x42
650 };
651
652 /* ------------------------------------------------------------------ */
653
654 static struct tda1004x_config tda827x_lifeview_config = {
655         .demod_address = 0x08,
656         .invert        = 1,
657         .invert_oclk   = 0,
658         .xtal_freq     = TDA10046_XTAL_16M,
659         .agc_config    = TDA10046_AGC_TDA827X,
660         .gpio_config   = TDA10046_GP11_I,
661         .if_freq       = TDA10046_FREQ_045,
662         .tuner_address = 0x60,
663         .request_firmware = philips_tda1004x_request_firmware
664 };
665
666 static struct tda1004x_config philips_tiger_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         .antenna_switch= 1,
677         .request_firmware = philips_tda1004x_request_firmware
678 };
679
680 static struct tda1004x_config cinergy_ht_config = {
681         .demod_address = 0x08,
682         .invert        = 1,
683         .invert_oclk   = 0,
684         .xtal_freq     = TDA10046_XTAL_16M,
685         .agc_config    = TDA10046_AGC_TDA827X,
686         .gpio_config   = TDA10046_GP01_I,
687         .if_freq       = TDA10046_FREQ_045,
688         .i2c_gate      = 0x4b,
689         .tuner_address = 0x61,
690         .request_firmware = philips_tda1004x_request_firmware
691 };
692
693 static struct tda1004x_config cinergy_ht_pci_config = {
694         .demod_address = 0x08,
695         .invert        = 1,
696         .invert_oclk   = 0,
697         .xtal_freq     = TDA10046_XTAL_16M,
698         .agc_config    = TDA10046_AGC_TDA827X,
699         .gpio_config   = TDA10046_GP01_I,
700         .if_freq       = TDA10046_FREQ_045,
701         .i2c_gate      = 0x4b,
702         .tuner_address = 0x60,
703         .request_firmware = philips_tda1004x_request_firmware
704 };
705
706 static struct tda1004x_config philips_tiger_s_config = {
707         .demod_address = 0x08,
708         .invert        = 1,
709         .invert_oclk   = 0,
710         .xtal_freq     = TDA10046_XTAL_16M,
711         .agc_config    = TDA10046_AGC_TDA827X,
712         .gpio_config   = TDA10046_GP01_I,
713         .if_freq       = TDA10046_FREQ_045,
714         .i2c_gate      = 0x4b,
715         .tuner_address = 0x61,
716         .antenna_switch= 1,
717         .request_firmware = philips_tda1004x_request_firmware
718 };
719
720 static struct tda1004x_config pinnacle_pctv_310i_config = {
721         .demod_address = 0x08,
722         .invert        = 1,
723         .invert_oclk   = 0,
724         .xtal_freq     = TDA10046_XTAL_16M,
725         .agc_config    = TDA10046_AGC_TDA827X,
726         .gpio_config   = TDA10046_GP11_I,
727         .if_freq       = TDA10046_FREQ_045,
728         .i2c_gate      = 0x4b,
729         .tuner_address = 0x61,
730         .request_firmware = philips_tda1004x_request_firmware
731 };
732
733 static struct tda1004x_config hauppauge_hvr_1110_config = {
734         .demod_address = 0x08,
735         .invert        = 1,
736         .invert_oclk   = 0,
737         .xtal_freq     = TDA10046_XTAL_16M,
738         .agc_config    = TDA10046_AGC_TDA827X,
739         .gpio_config   = TDA10046_GP11_I,
740         .if_freq       = TDA10046_FREQ_045,
741         .i2c_gate      = 0x4b,
742         .tuner_address = 0x61,
743         .request_firmware = philips_tda1004x_request_firmware
744 };
745
746 static struct tda1004x_config asus_p7131_dual_config = {
747         .demod_address = 0x08,
748         .invert        = 1,
749         .invert_oclk   = 0,
750         .xtal_freq     = TDA10046_XTAL_16M,
751         .agc_config    = TDA10046_AGC_TDA827X,
752         .gpio_config   = TDA10046_GP11_I,
753         .if_freq       = TDA10046_FREQ_045,
754         .i2c_gate      = 0x4b,
755         .tuner_address = 0x61,
756         .antenna_switch= 2,
757         .request_firmware = philips_tda1004x_request_firmware
758 };
759
760 static struct tda1004x_config lifeview_trio_config = {
761         .demod_address = 0x09,
762         .invert        = 1,
763         .invert_oclk   = 0,
764         .xtal_freq     = TDA10046_XTAL_16M,
765         .agc_config    = TDA10046_AGC_TDA827X,
766         .gpio_config   = TDA10046_GP00_I,
767         .if_freq       = TDA10046_FREQ_045,
768         .tuner_address = 0x60,
769         .request_firmware = philips_tda1004x_request_firmware
770 };
771
772 static struct tda1004x_config tevion_dvbt220rf_config = {
773         .demod_address = 0x08,
774         .invert        = 1,
775         .invert_oclk   = 0,
776         .xtal_freq     = TDA10046_XTAL_16M,
777         .agc_config    = TDA10046_AGC_TDA827X,
778         .gpio_config   = TDA10046_GP11_I,
779         .if_freq       = TDA10046_FREQ_045,
780         .tuner_address = 0x60,
781         .request_firmware = philips_tda1004x_request_firmware
782 };
783
784 static struct tda1004x_config md8800_dvbt_config = {
785         .demod_address = 0x08,
786         .invert        = 1,
787         .invert_oclk   = 0,
788         .xtal_freq     = TDA10046_XTAL_16M,
789         .agc_config    = TDA10046_AGC_TDA827X,
790         .gpio_config   = TDA10046_GP01_I,
791         .if_freq       = TDA10046_FREQ_045,
792         .i2c_gate      = 0x4b,
793         .tuner_address = 0x60,
794         .request_firmware = philips_tda1004x_request_firmware
795 };
796
797 static struct tda1004x_config asus_p7131_4871_config = {
798         .demod_address = 0x08,
799         .invert        = 1,
800         .invert_oclk   = 0,
801         .xtal_freq     = TDA10046_XTAL_16M,
802         .agc_config    = TDA10046_AGC_TDA827X,
803         .gpio_config   = TDA10046_GP01_I,
804         .if_freq       = TDA10046_FREQ_045,
805         .i2c_gate      = 0x4b,
806         .tuner_address = 0x61,
807         .antenna_switch= 2,
808         .request_firmware = philips_tda1004x_request_firmware
809 };
810
811 static struct tda1004x_config asus_p7131_hybrid_lna_config = {
812         .demod_address = 0x08,
813         .invert        = 1,
814         .invert_oclk   = 0,
815         .xtal_freq     = TDA10046_XTAL_16M,
816         .agc_config    = TDA10046_AGC_TDA827X,
817         .gpio_config   = TDA10046_GP11_I,
818         .if_freq       = TDA10046_FREQ_045,
819         .i2c_gate      = 0x4b,
820         .tuner_address = 0x61,
821         .antenna_switch= 2,
822         .request_firmware = philips_tda1004x_request_firmware
823 };
824
825 static struct tda1004x_config kworld_dvb_t_210_config = {
826         .demod_address = 0x08,
827         .invert        = 1,
828         .invert_oclk   = 0,
829         .xtal_freq     = TDA10046_XTAL_16M,
830         .agc_config    = TDA10046_AGC_TDA827X,
831         .gpio_config   = TDA10046_GP11_I,
832         .if_freq       = TDA10046_FREQ_045,
833         .i2c_gate      = 0x4b,
834         .tuner_address = 0x61,
835         .antenna_switch= 1,
836         .request_firmware = philips_tda1004x_request_firmware
837 };
838
839 static struct tda1004x_config avermedia_super_007_config = {
840         .demod_address = 0x08,
841         .invert        = 1,
842         .invert_oclk   = 0,
843         .xtal_freq     = TDA10046_XTAL_16M,
844         .agc_config    = TDA10046_AGC_TDA827X,
845         .gpio_config   = TDA10046_GP01_I,
846         .if_freq       = TDA10046_FREQ_045,
847         .i2c_gate      = 0x4b,
848         .tuner_address = 0x60,
849         .antenna_switch= 1,
850         .request_firmware = philips_tda1004x_request_firmware
851 };
852
853 static struct tda1004x_config twinhan_dtv_dvb_3056_config = {
854         .demod_address = 0x08,
855         .invert        = 1,
856         .invert_oclk   = 0,
857         .xtal_freq     = TDA10046_XTAL_16M,
858         .agc_config    = TDA10046_AGC_TDA827X,
859         .gpio_config   = TDA10046_GP01_I,
860         .if_freq       = TDA10046_FREQ_045,
861         .i2c_gate      = 0x42,
862         .tuner_address = 0x61,
863         .antenna_switch = 1,
864         .request_firmware = philips_tda1004x_request_firmware
865 };
866
867 static struct tda1004x_config asus_tiger_3in1_config = {
868         .demod_address = 0x0b,
869         .invert        = 1,
870         .invert_oclk   = 0,
871         .xtal_freq     = TDA10046_XTAL_16M,
872         .agc_config    = TDA10046_AGC_TDA827X,
873         .gpio_config   = TDA10046_GP11_I,
874         .if_freq       = TDA10046_FREQ_045,
875         .i2c_gate      = 0x4b,
876         .tuner_address = 0x61,
877         .antenna_switch = 1,
878         .request_firmware = philips_tda1004x_request_firmware
879 };
880
881 static struct tda1004x_config asus_ps3_100_config = {
882         .demod_address = 0x0b,
883         .invert        = 1,
884         .invert_oclk   = 0,
885         .xtal_freq     = TDA10046_XTAL_16M,
886         .agc_config    = TDA10046_AGC_TDA827X,
887         .gpio_config   = TDA10046_GP11_I,
888         .if_freq       = TDA10046_FREQ_045,
889         .i2c_gate      = 0x4b,
890         .tuner_address = 0x61,
891         .antenna_switch = 1,
892         .request_firmware = philips_tda1004x_request_firmware
893 };
894
895 /* ------------------------------------------------------------------
896  * special case: this card uses saa713x GPIO22 for the mode switch
897  */
898
899 static int ads_duo_tuner_init(struct dvb_frontend *fe)
900 {
901         struct saa7134_dev *dev = fe->dvb->priv;
902         philips_tda827x_tuner_init(fe);
903         /* route TDA8275a AGC input to the channel decoder */
904         saa7134_set_gpio(dev, 22, 1);
905         return 0;
906 }
907
908 static int ads_duo_tuner_sleep(struct dvb_frontend *fe)
909 {
910         struct saa7134_dev *dev = fe->dvb->priv;
911         /* route TDA8275a AGC input to the analog IF chip*/
912         saa7134_set_gpio(dev, 22, 0);
913         philips_tda827x_tuner_sleep(fe);
914         return 0;
915 }
916
917 static struct tda827x_config ads_duo_cfg = {
918         .init = ads_duo_tuner_init,
919         .sleep = ads_duo_tuner_sleep,
920         .config = 0
921 };
922
923 static struct tda1004x_config ads_tech_duo_config = {
924         .demod_address = 0x08,
925         .invert        = 1,
926         .invert_oclk   = 0,
927         .xtal_freq     = TDA10046_XTAL_16M,
928         .agc_config    = TDA10046_AGC_TDA827X,
929         .gpio_config   = TDA10046_GP00_I,
930         .if_freq       = TDA10046_FREQ_045,
931         .tuner_address = 0x61,
932         .request_firmware = philips_tda1004x_request_firmware
933 };
934
935 static struct zl10353_config behold_h6_config = {
936         .demod_address = 0x1e>>1,
937         .no_tuner      = 1,
938         .parallel_ts   = 1,
939         .disable_i2c_gate_ctrl = 1,
940 };
941
942 static struct xc5000_config behold_x7_tunerconfig = {
943         .i2c_address      = 0xc2>>1,
944         .if_khz           = 4560,
945         .radio_input      = XC5000_RADIO_FM1,
946 };
947
948 static struct zl10353_config behold_x7_config = {
949         .demod_address = 0x1e>>1,
950         .if2           = 45600,
951         .no_tuner      = 1,
952         .parallel_ts   = 1,
953         .disable_i2c_gate_ctrl = 1,
954 };
955
956 static struct zl10353_config videomate_t750_zl10353_config = {
957         .demod_address         = 0x0f,
958         .no_tuner              = 1,
959         .parallel_ts           = 1,
960         .disable_i2c_gate_ctrl = 1,
961 };
962
963 static struct qt1010_config videomate_t750_qt1010_config = {
964         .i2c_address = 0x62
965 };
966
967
968 /* ==================================================================
969  * tda10086 based DVB-S cards, helper functions
970  */
971
972 static struct tda10086_config flydvbs = {
973         .demod_address = 0x0e,
974         .invert = 0,
975         .diseqc_tone = 0,
976         .xtal_freq = TDA10086_XTAL_16M,
977 };
978
979 static struct tda10086_config sd1878_4m = {
980         .demod_address = 0x0e,
981         .invert = 0,
982         .diseqc_tone = 0,
983         .xtal_freq = TDA10086_XTAL_4M,
984 };
985
986 /* ------------------------------------------------------------------
987  * special case: lnb supply is connected to the gated i2c
988  */
989
990 static int md8800_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
991 {
992         int res = -EIO;
993         struct saa7134_dev *dev = fe->dvb->priv;
994         if (fe->ops.i2c_gate_ctrl) {
995                 fe->ops.i2c_gate_ctrl(fe, 1);
996                 if (dev->original_set_voltage)
997                         res = dev->original_set_voltage(fe, voltage);
998                 fe->ops.i2c_gate_ctrl(fe, 0);
999         }
1000         return res;
1001 };
1002
1003 static int md8800_set_high_voltage(struct dvb_frontend *fe, long arg)
1004 {
1005         int res = -EIO;
1006         struct saa7134_dev *dev = fe->dvb->priv;
1007         if (fe->ops.i2c_gate_ctrl) {
1008                 fe->ops.i2c_gate_ctrl(fe, 1);
1009                 if (dev->original_set_high_voltage)
1010                         res = dev->original_set_high_voltage(fe, arg);
1011                 fe->ops.i2c_gate_ctrl(fe, 0);
1012         }
1013         return res;
1014 };
1015
1016 static int md8800_set_voltage2(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
1017 {
1018         struct saa7134_dev *dev = fe->dvb->priv;
1019         u8 wbuf[2] = { 0x1f, 00 };
1020         u8 rbuf;
1021         struct i2c_msg msg[] = { { .addr = 0x08, .flags = 0, .buf = wbuf, .len = 1 },
1022                                  { .addr = 0x08, .flags = I2C_M_RD, .buf = &rbuf, .len = 1 } };
1023
1024         if (i2c_transfer(&dev->i2c_adap, msg, 2) != 2)
1025                 return -EIO;
1026         /* NOTE: this assumes that gpo1 is used, it might be bit 5 (gpo2) */
1027         if (voltage == SEC_VOLTAGE_18)
1028                 wbuf[1] = rbuf | 0x10;
1029         else
1030                 wbuf[1] = rbuf & 0xef;
1031         msg[0].len = 2;
1032         i2c_transfer(&dev->i2c_adap, msg, 1);
1033         return 0;
1034 }
1035
1036 static int md8800_set_high_voltage2(struct dvb_frontend *fe, long arg)
1037 {
1038         pr_warn("%s: sorry can't set high LNB supply voltage from here\n",
1039                 __func__);
1040         return -EIO;
1041 }
1042
1043 /* ==================================================================
1044  * nxt200x based ATSC cards, helper functions
1045  */
1046
1047 static struct nxt200x_config avertvhda180 = {
1048         .demod_address    = 0x0a,
1049 };
1050
1051 static struct nxt200x_config kworldatsc110 = {
1052         .demod_address    = 0x0a,
1053 };
1054
1055 /* ------------------------------------------------------------------ */
1056
1057 static struct mt312_config avertv_a700_mt312 = {
1058         .demod_address = 0x0e,
1059         .voltage_inverted = 1,
1060 };
1061
1062 static struct zl10036_config avertv_a700_tuner = {
1063         .tuner_address = 0x60,
1064 };
1065
1066 static struct mt312_config zl10313_compro_s350_config = {
1067         .demod_address = 0x0e,
1068 };
1069
1070 static struct mt312_config zl10313_avermedia_a706_config = {
1071         .demod_address = 0x0e,
1072 };
1073
1074 static struct lgdt3305_config hcw_lgdt3305_config = {
1075         .i2c_addr           = 0x0e,
1076         .mpeg_mode          = LGDT3305_MPEG_SERIAL,
1077         .tpclk_edge         = LGDT3305_TPCLK_RISING_EDGE,
1078         .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
1079         .deny_i2c_rptr      = 1,
1080         .spectral_inversion = 1,
1081         .qam_if_khz         = 4000,
1082         .vsb_if_khz         = 3250,
1083 };
1084
1085 static struct tda10048_config hcw_tda10048_config = {
1086         .demod_address    = 0x10 >> 1,
1087         .output_mode      = TDA10048_SERIAL_OUTPUT,
1088         .fwbulkwritelen   = TDA10048_BULKWRITE_200,
1089         .inversion        = TDA10048_INVERSION_ON,
1090         .dtv6_if_freq_khz = TDA10048_IF_3300,
1091         .dtv7_if_freq_khz = TDA10048_IF_3500,
1092         .dtv8_if_freq_khz = TDA10048_IF_4000,
1093         .clk_freq_khz     = TDA10048_CLK_16000,
1094         .disable_gate_access = 1,
1095 };
1096
1097 static struct tda18271_std_map hauppauge_tda18271_std_map = {
1098         .atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 4,
1099                       .if_lvl = 1, .rfagc_top = 0x58, },
1100         .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 5,
1101                       .if_lvl = 1, .rfagc_top = 0x58, },
1102 };
1103
1104 static struct tda18271_config hcw_tda18271_config = {
1105         .std_map = &hauppauge_tda18271_std_map,
1106         .gate    = TDA18271_GATE_ANALOG,
1107         .config  = 3,
1108         .output_opt = TDA18271_OUTPUT_LT_OFF,
1109 };
1110
1111 static struct tda829x_config tda829x_no_probe = {
1112         .probe_tuner = TDA829X_DONT_PROBE,
1113 };
1114
1115 static struct tda10048_config zolid_tda10048_config = {
1116         .demod_address    = 0x10 >> 1,
1117         .output_mode      = TDA10048_PARALLEL_OUTPUT,
1118         .fwbulkwritelen   = TDA10048_BULKWRITE_200,
1119         .inversion        = TDA10048_INVERSION_ON,
1120         .dtv6_if_freq_khz = TDA10048_IF_3300,
1121         .dtv7_if_freq_khz = TDA10048_IF_3500,
1122         .dtv8_if_freq_khz = TDA10048_IF_4000,
1123         .clk_freq_khz     = TDA10048_CLK_16000,
1124         .disable_gate_access = 1,
1125 };
1126
1127 static struct tda18271_config zolid_tda18271_config = {
1128         .gate    = TDA18271_GATE_ANALOG,
1129 };
1130
1131 static struct tda10048_config dtv1000s_tda10048_config = {
1132         .demod_address    = 0x10 >> 1,
1133         .output_mode      = TDA10048_PARALLEL_OUTPUT,
1134         .fwbulkwritelen   = TDA10048_BULKWRITE_200,
1135         .inversion        = TDA10048_INVERSION_ON,
1136         .dtv6_if_freq_khz = TDA10048_IF_3300,
1137         .dtv7_if_freq_khz = TDA10048_IF_3800,
1138         .dtv8_if_freq_khz = TDA10048_IF_4300,
1139         .clk_freq_khz     = TDA10048_CLK_16000,
1140         .disable_gate_access = 1,
1141 };
1142
1143 static struct tda18271_std_map dtv1000s_tda18271_std_map = {
1144         .dvbt_6   = { .if_freq = 3300, .agc_mode = 3, .std = 4,
1145                       .if_lvl = 1, .rfagc_top = 0x37, },
1146         .dvbt_7   = { .if_freq = 3800, .agc_mode = 3, .std = 5,
1147                       .if_lvl = 1, .rfagc_top = 0x37, },
1148         .dvbt_8   = { .if_freq = 4300, .agc_mode = 3, .std = 6,
1149                       .if_lvl = 1, .rfagc_top = 0x37, },
1150 };
1151
1152 static struct tda18271_config dtv1000s_tda18271_config = {
1153         .std_map = &dtv1000s_tda18271_std_map,
1154         .gate    = TDA18271_GATE_ANALOG,
1155 };
1156
1157 static struct lgs8gxx_config prohdtv_pro2_lgs8g75_config = {
1158         .prod = LGS8GXX_PROD_LGS8G75,
1159         .demod_address = 0x1d,
1160         .serial_ts = 0,
1161         .ts_clk_pol = 1,
1162         .ts_clk_gated = 0,
1163         .if_clk_freq = 30400, /* 30.4 MHz */
1164         .if_freq = 4000, /* 4.00 MHz */
1165         .if_neg_center = 0,
1166         .ext_adc = 0,
1167         .adc_signed = 1,
1168         .adc_vpp = 3, /* 2.0 Vpp */
1169         .if_neg_edge = 1,
1170 };
1171
1172 static struct tda18271_config prohdtv_pro2_tda18271_config = {
1173         .gate = TDA18271_GATE_ANALOG,
1174         .output_opt = TDA18271_OUTPUT_LT_OFF,
1175 };
1176
1177 static struct tda18271_std_map kworld_tda18271_std_map = {
1178         .atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 3,
1179                       .if_lvl = 6, .rfagc_top = 0x37 },
1180         .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 0,
1181                       .if_lvl = 6, .rfagc_top = 0x37 },
1182 };
1183
1184 static struct tda18271_config kworld_pc150u_tda18271_config = {
1185         .std_map = &kworld_tda18271_std_map,
1186         .gate    = TDA18271_GATE_ANALOG,
1187         .output_opt = TDA18271_OUTPUT_LT_OFF,
1188         .config  = 3,   /* Use tuner callback for AGC */
1189         .rf_cal_on_startup = 1
1190 };
1191
1192 static struct s5h1411_config kworld_s5h1411_config = {
1193         .output_mode   = S5H1411_PARALLEL_OUTPUT,
1194         .gpio          = S5H1411_GPIO_OFF,
1195         .qam_if        = S5H1411_IF_4000,
1196         .vsb_if        = S5H1411_IF_3250,
1197         .inversion     = S5H1411_INVERSION_ON,
1198         .status_mode   = S5H1411_DEMODLOCKING,
1199         .mpeg_timing   =
1200                 S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
1201 };
1202
1203
1204 /* ==================================================================
1205  * Core code
1206  */
1207
1208 static int dvb_init(struct saa7134_dev *dev)
1209 {
1210         int ret;
1211         int attach_xc3028 = 0;
1212         struct vb2_dvb_frontend *fe0;
1213         struct vb2_queue *q;
1214
1215         /* FIXME: add support for multi-frontend */
1216         mutex_init(&dev->frontends.lock);
1217         INIT_LIST_HEAD(&dev->frontends.felist);
1218
1219         pr_info("%s() allocating 1 frontend\n", __func__);
1220         fe0 = vb2_dvb_alloc_frontend(&dev->frontends, 1);
1221         if (!fe0) {
1222                 pr_err("%s() failed to alloc\n", __func__);
1223                 return -ENOMEM;
1224         }
1225
1226         /* init struct vb2_dvb */
1227         dev->ts.nr_bufs    = 32;
1228         dev->ts.nr_packets = 32*4;
1229         fe0->dvb.name = dev->name;
1230         q = &fe0->dvb.dvbq;
1231         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1232         q->io_modes = VB2_MMAP | VB2_READ;
1233         q->drv_priv = &dev->ts_q;
1234         q->ops = &saa7134_ts_qops;
1235         q->mem_ops = &vb2_dma_sg_memops;
1236         q->buf_struct_size = sizeof(struct saa7134_buf);
1237         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1238         q->lock = &dev->lock;
1239         ret = vb2_queue_init(q);
1240         if (ret) {
1241                 vb2_dvb_dealloc_frontends(&dev->frontends);
1242                 return ret;
1243         }
1244
1245         switch (dev->board) {
1246         case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
1247                 pr_debug("pinnacle 300i dvb setup\n");
1248                 fe0->dvb.frontend = dvb_attach(mt352_attach, &pinnacle_300i,
1249                                                &dev->i2c_adap);
1250                 if (fe0->dvb.frontend) {
1251                         fe0->dvb.frontend->ops.tuner_ops.set_params = mt352_pinnacle_tuner_set_params;
1252                 }
1253                 break;
1254         case SAA7134_BOARD_AVERMEDIA_777:
1255         case SAA7134_BOARD_AVERMEDIA_A16AR:
1256                 pr_debug("avertv 777 dvb setup\n");
1257                 fe0->dvb.frontend = dvb_attach(mt352_attach, &avermedia_777,
1258                                                &dev->i2c_adap);
1259                 if (fe0->dvb.frontend) {
1260                         dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1261                                    &dev->i2c_adap, 0x61,
1262                                    TUNER_PHILIPS_TD1316);
1263                 }
1264                 break;
1265         case SAA7134_BOARD_AVERMEDIA_A16D:
1266                 pr_debug("AverMedia A16D dvb setup\n");
1267                 fe0->dvb.frontend = dvb_attach(mt352_attach,
1268                                                 &avermedia_xc3028_mt352_dev,
1269                                                 &dev->i2c_adap);
1270                 attach_xc3028 = 1;
1271                 break;
1272         case SAA7134_BOARD_MD7134:
1273                 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1274                                                &medion_cardbus,
1275                                                &dev->i2c_adap);
1276                 if (fe0->dvb.frontend) {
1277                         dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1278                                    &dev->i2c_adap, medion_cardbus.tuner_address,
1279                                    TUNER_PHILIPS_FMD1216ME_MK3);
1280                 }
1281                 break;
1282         case SAA7134_BOARD_PHILIPS_TOUGH:
1283                 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1284                                                &philips_tu1216_60_config,
1285                                                &dev->i2c_adap);
1286                 if (fe0->dvb.frontend) {
1287                         fe0->dvb.frontend->ops.tuner_ops.init = philips_tu1216_init;
1288                         fe0->dvb.frontend->ops.tuner_ops.set_params = philips_tda6651_pll_set;
1289                 }
1290                 break;
1291         case SAA7134_BOARD_FLYDVBTDUO:
1292         case SAA7134_BOARD_FLYDVBT_DUO_CARDBUS:
1293                 if (configure_tda827x_fe(dev, &tda827x_lifeview_config,
1294                                          &tda827x_cfg_0) < 0)
1295                         goto detach_frontend;
1296                 break;
1297         case SAA7134_BOARD_PHILIPS_EUROPA:
1298         case SAA7134_BOARD_VIDEOMATE_DVBT_300:
1299         case SAA7134_BOARD_ASUS_EUROPA_HYBRID:
1300                 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1301                                                &philips_europa_config,
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                         fe0->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1307                         fe0->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1308                         fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1309                 }
1310                 break;
1311         case SAA7134_BOARD_TECHNOTREND_BUDGET_T3000:
1312                 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1313                                                &technotrend_budget_t3000_config,
1314                                                &dev->i2c_adap);
1315                 if (fe0->dvb.frontend) {
1316                         dev->original_demod_sleep = fe0->dvb.frontend->ops.sleep;
1317                         fe0->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1318                         fe0->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1319                         fe0->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1320                         fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1321                 }
1322                 break;
1323         case SAA7134_BOARD_VIDEOMATE_DVBT_200:
1324                 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1325                                                &philips_tu1216_61_config,
1326                                                &dev->i2c_adap);
1327                 if (fe0->dvb.frontend) {
1328                         fe0->dvb.frontend->ops.tuner_ops.init = philips_tu1216_init;
1329                         fe0->dvb.frontend->ops.tuner_ops.set_params = philips_tda6651_pll_set;
1330                 }
1331                 break;
1332         case SAA7134_BOARD_KWORLD_DVBT_210:
1333                 if (configure_tda827x_fe(dev, &kworld_dvb_t_210_config,
1334                                          &tda827x_cfg_2) < 0)
1335                         goto detach_frontend;
1336                 break;
1337         case SAA7134_BOARD_HAUPPAUGE_HVR1120:
1338                 fe0->dvb.frontend = dvb_attach(tda10048_attach,
1339                                                &hcw_tda10048_config,
1340                                                &dev->i2c_adap);
1341                 if (fe0->dvb.frontend != NULL) {
1342                         dvb_attach(tda829x_attach, fe0->dvb.frontend,
1343                                    &dev->i2c_adap, 0x4b,
1344                                    &tda829x_no_probe);
1345                         dvb_attach(tda18271_attach, fe0->dvb.frontend,
1346                                    0x60, &dev->i2c_adap,
1347                                    &hcw_tda18271_config);
1348                 }
1349                 break;
1350         case SAA7134_BOARD_PHILIPS_TIGER:
1351                 if (configure_tda827x_fe(dev, &philips_tiger_config,
1352                                          &tda827x_cfg_0) < 0)
1353                         goto detach_frontend;
1354                 break;
1355         case SAA7134_BOARD_PINNACLE_PCTV_310i:
1356                 if (configure_tda827x_fe(dev, &pinnacle_pctv_310i_config,
1357                                          &tda827x_cfg_1) < 0)
1358                         goto detach_frontend;
1359                 break;
1360         case SAA7134_BOARD_HAUPPAUGE_HVR1110:
1361                 if (configure_tda827x_fe(dev, &hauppauge_hvr_1110_config,
1362                                          &tda827x_cfg_1) < 0)
1363                         goto detach_frontend;
1364                 break;
1365         case SAA7134_BOARD_HAUPPAUGE_HVR1150:
1366                 fe0->dvb.frontend = dvb_attach(lgdt3305_attach,
1367                                                &hcw_lgdt3305_config,
1368                                                &dev->i2c_adap);
1369                 if (fe0->dvb.frontend) {
1370                         dvb_attach(tda829x_attach, fe0->dvb.frontend,
1371                                    &dev->i2c_adap, 0x4b,
1372                                    &tda829x_no_probe);
1373                         dvb_attach(tda18271_attach, fe0->dvb.frontend,
1374                                    0x60, &dev->i2c_adap,
1375                                    &hcw_tda18271_config);
1376                 }
1377                 break;
1378         case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
1379                 if (configure_tda827x_fe(dev, &asus_p7131_dual_config,
1380                                          &tda827x_cfg_0) < 0)
1381                         goto detach_frontend;
1382                 break;
1383         case SAA7134_BOARD_FLYDVBT_LR301:
1384                 if (configure_tda827x_fe(dev, &tda827x_lifeview_config,
1385                                          &tda827x_cfg_0) < 0)
1386                         goto detach_frontend;
1387                 break;
1388         case SAA7134_BOARD_FLYDVB_TRIO:
1389                 if (!use_frontend) {    /* terrestrial */
1390                         if (configure_tda827x_fe(dev, &lifeview_trio_config,
1391                                                  &tda827x_cfg_0) < 0)
1392                                 goto detach_frontend;
1393                 } else {                /* satellite */
1394                         fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs, &dev->i2c_adap);
1395                         if (fe0->dvb.frontend) {
1396                                 if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x63,
1397                                                                         &dev->i2c_adap, 0) == NULL) {
1398                                         pr_warn("%s: Lifeview Trio, No tda826x found!\n",
1399                                                 __func__);
1400                                         goto detach_frontend;
1401                                 }
1402                                 if (dvb_attach(isl6421_attach, fe0->dvb.frontend,
1403                                                &dev->i2c_adap,
1404                                                0x08, 0, 0, false) == NULL) {
1405                                         pr_warn("%s: Lifeview Trio, No ISL6421 found!\n",
1406                                                 __func__);
1407                                         goto detach_frontend;
1408                                 }
1409                         }
1410                 }
1411                 break;
1412         case SAA7134_BOARD_ADS_DUO_CARDBUS_PTV331:
1413         case SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS:
1414                 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1415                                                &ads_tech_duo_config,
1416                                                &dev->i2c_adap);
1417                 if (fe0->dvb.frontend) {
1418                         if (dvb_attach(tda827x_attach,fe0->dvb.frontend,
1419                                    ads_tech_duo_config.tuner_address, &dev->i2c_adap,
1420                                                                 &ads_duo_cfg) == NULL) {
1421                                 pr_warn("no tda827x tuner found at addr: %02x\n",
1422                                         ads_tech_duo_config.tuner_address);
1423                                 goto detach_frontend;
1424                         }
1425                 } else
1426                         pr_warn("failed to attach tda10046\n");
1427                 break;
1428         case SAA7134_BOARD_TEVION_DVBT_220RF:
1429                 if (configure_tda827x_fe(dev, &tevion_dvbt220rf_config,
1430                                          &tda827x_cfg_0) < 0)
1431                         goto detach_frontend;
1432                 break;
1433         case SAA7134_BOARD_MEDION_MD8800_QUADRO:
1434                 if (!use_frontend) {     /* terrestrial */
1435                         if (configure_tda827x_fe(dev, &md8800_dvbt_config,
1436                                                  &tda827x_cfg_0) < 0)
1437                                 goto detach_frontend;
1438                 } else {        /* satellite */
1439                         fe0->dvb.frontend = dvb_attach(tda10086_attach,
1440                                                         &flydvbs, &dev->i2c_adap);
1441                         if (fe0->dvb.frontend) {
1442                                 struct dvb_frontend *fe = fe0->dvb.frontend;
1443                                 u8 dev_id = dev->eedata[2];
1444                                 u8 data = 0xc4;
1445                                 struct i2c_msg msg = {.addr = 0x08, .flags = 0, .len = 1};
1446
1447                                 if (dvb_attach(tda826x_attach, fe0->dvb.frontend,
1448                                                 0x60, &dev->i2c_adap, 0) == NULL) {
1449                                         pr_warn("%s: Medion Quadro, no tda826x "
1450                                                 "found !\n", __func__);
1451                                         goto detach_frontend;
1452                                 }
1453                                 if (dev_id != 0x08) {
1454                                         /* we need to open the i2c gate (we know it exists) */
1455                                         fe->ops.i2c_gate_ctrl(fe, 1);
1456                                         if (dvb_attach(isl6405_attach, fe,
1457                                                         &dev->i2c_adap, 0x08, 0, 0) == NULL) {
1458                                                 pr_warn("%s: Medion Quadro, no ISL6405 "
1459                                                         "found !\n", __func__);
1460                                                 goto detach_frontend;
1461                                         }
1462                                         if (dev_id == 0x07) {
1463                                                 /* fire up the 2nd section of the LNB supply since
1464                                                    we can't do this from the other section */
1465                                                 msg.buf = &data;
1466                                                 i2c_transfer(&dev->i2c_adap, &msg, 1);
1467                                         }
1468                                         fe->ops.i2c_gate_ctrl(fe, 0);
1469                                         dev->original_set_voltage = fe->ops.set_voltage;
1470                                         fe->ops.set_voltage = md8800_set_voltage;
1471                                         dev->original_set_high_voltage = fe->ops.enable_high_lnb_voltage;
1472                                         fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage;
1473                                 } else {
1474                                         fe->ops.set_voltage = md8800_set_voltage2;
1475                                         fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage2;
1476                                 }
1477                         }
1478                 }
1479                 break;
1480         case SAA7134_BOARD_AVERMEDIA_AVERTVHD_A180:
1481                 fe0->dvb.frontend = dvb_attach(nxt200x_attach, &avertvhda180,
1482                                                &dev->i2c_adap);
1483                 if (fe0->dvb.frontend)
1484                         dvb_attach(dvb_pll_attach, fe0->dvb.frontend, 0x61,
1485                                    NULL, DVB_PLL_TDHU2);
1486                 break;
1487         case SAA7134_BOARD_ADS_INSTANT_HDTV_PCI:
1488         case SAA7134_BOARD_KWORLD_ATSC110:
1489                 fe0->dvb.frontend = dvb_attach(nxt200x_attach, &kworldatsc110,
1490                                                &dev->i2c_adap);
1491                 if (fe0->dvb.frontend)
1492                         dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1493                                    &dev->i2c_adap, 0x61,
1494                                    TUNER_PHILIPS_TUV1236D);
1495                 break;
1496         case SAA7134_BOARD_KWORLD_PC150U:
1497                 saa7134_set_gpio(dev, 18, 1); /* Switch to digital mode */
1498                 saa7134_tuner_callback(dev, 0,
1499                                        TDA18271_CALLBACK_CMD_AGC_ENABLE, 1);
1500                 fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1501                                                &kworld_s5h1411_config,
1502                                                &dev->i2c_adap);
1503                 if (fe0->dvb.frontend != NULL) {
1504                         dvb_attach(tda829x_attach, fe0->dvb.frontend,
1505                                    &dev->i2c_adap, 0x4b,
1506                                    &tda829x_no_probe);
1507                         dvb_attach(tda18271_attach, fe0->dvb.frontend,
1508                                    0x60, &dev->i2c_adap,
1509                                    &kworld_pc150u_tda18271_config);
1510                 }
1511                 break;
1512         case SAA7134_BOARD_FLYDVBS_LR300:
1513                 fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs,
1514                                                &dev->i2c_adap);
1515                 if (fe0->dvb.frontend) {
1516                         if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x60,
1517                                        &dev->i2c_adap, 0) == NULL) {
1518                                 pr_warn("%s: No tda826x found!\n", __func__);
1519                                 goto detach_frontend;
1520                         }
1521                         if (dvb_attach(isl6421_attach, fe0->dvb.frontend,
1522                                        &dev->i2c_adap,
1523                                        0x08, 0, 0, false) == NULL) {
1524                                 pr_warn("%s: No ISL6421 found!\n", __func__);
1525                                 goto detach_frontend;
1526                         }
1527                 }
1528                 break;
1529         case SAA7134_BOARD_ASUS_EUROPA2_HYBRID:
1530                 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1531                                                &medion_cardbus,
1532                                                &dev->i2c_adap);
1533                 if (fe0->dvb.frontend) {
1534                         dev->original_demod_sleep = fe0->dvb.frontend->ops.sleep;
1535                         fe0->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1536
1537                         dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1538                                    &dev->i2c_adap, medion_cardbus.tuner_address,
1539                                    TUNER_PHILIPS_FMD1216ME_MK3);
1540                 }
1541                 break;
1542         case SAA7134_BOARD_VIDEOMATE_DVBT_200A:
1543                 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1544                                 &philips_europa_config,
1545                                 &dev->i2c_adap);
1546                 if (fe0->dvb.frontend) {
1547                         fe0->dvb.frontend->ops.tuner_ops.init = philips_td1316_tuner_init;
1548                         fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1549                 }
1550                 break;
1551         case SAA7134_BOARD_CINERGY_HT_PCMCIA:
1552                 if (configure_tda827x_fe(dev, &cinergy_ht_config,
1553                                          &tda827x_cfg_0) < 0)
1554                         goto detach_frontend;
1555                 break;
1556         case SAA7134_BOARD_CINERGY_HT_PCI:
1557                 if (configure_tda827x_fe(dev, &cinergy_ht_pci_config,
1558                                          &tda827x_cfg_0) < 0)
1559                         goto detach_frontend;
1560                 break;
1561         case SAA7134_BOARD_PHILIPS_TIGER_S:
1562                 if (configure_tda827x_fe(dev, &philips_tiger_s_config,
1563                                          &tda827x_cfg_2) < 0)
1564                         goto detach_frontend;
1565                 break;
1566         case SAA7134_BOARD_ASUS_P7131_4871:
1567                 if (configure_tda827x_fe(dev, &asus_p7131_4871_config,
1568                                          &tda827x_cfg_2) < 0)
1569                         goto detach_frontend;
1570                 break;
1571         case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
1572                 if (configure_tda827x_fe(dev, &asus_p7131_hybrid_lna_config,
1573                                          &tda827x_cfg_2) < 0)
1574                         goto detach_frontend;
1575                 break;
1576         case SAA7134_BOARD_AVERMEDIA_SUPER_007:
1577                 if (configure_tda827x_fe(dev, &avermedia_super_007_config,
1578                                          &tda827x_cfg_0) < 0)
1579                         goto detach_frontend;
1580                 break;
1581         case SAA7134_BOARD_TWINHAN_DTV_DVB_3056:
1582                 if (configure_tda827x_fe(dev, &twinhan_dtv_dvb_3056_config,
1583                                          &tda827x_cfg_2_sw42) < 0)
1584                         goto detach_frontend;
1585                 break;
1586         case SAA7134_BOARD_PHILIPS_SNAKE:
1587                 fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs,
1588                                                 &dev->i2c_adap);
1589                 if (fe0->dvb.frontend) {
1590                         if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x60,
1591                                         &dev->i2c_adap, 0) == NULL) {
1592                                 pr_warn("%s: No tda826x found!\n", __func__);
1593                                 goto detach_frontend;
1594                         }
1595                         if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1596                                         &dev->i2c_adap, 0, 0) == NULL) {
1597                                 pr_warn("%s: No lnbp21 found!\n", __func__);
1598                                 goto detach_frontend;
1599                         }
1600                 }
1601                 break;
1602         case SAA7134_BOARD_CREATIX_CTX953:
1603                 if (configure_tda827x_fe(dev, &md8800_dvbt_config,
1604                                          &tda827x_cfg_0) < 0)
1605                         goto detach_frontend;
1606                 break;
1607         case SAA7134_BOARD_MSI_TVANYWHERE_AD11:
1608                 if (configure_tda827x_fe(dev, &philips_tiger_s_config,
1609                                          &tda827x_cfg_2) < 0)
1610                         goto detach_frontend;
1611                 break;
1612         case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
1613                 pr_debug("AverMedia E506R dvb setup\n");
1614                 saa7134_set_gpio(dev, 25, 0);
1615                 msleep(10);
1616                 saa7134_set_gpio(dev, 25, 1);
1617                 fe0->dvb.frontend = dvb_attach(mt352_attach,
1618                                                 &avermedia_xc3028_mt352_dev,
1619                                                 &dev->i2c_adap);
1620                 attach_xc3028 = 1;
1621                 break;
1622         case SAA7134_BOARD_MD7134_BRIDGE_2:
1623                 fe0->dvb.frontend = dvb_attach(tda10086_attach,
1624                                                 &sd1878_4m, &dev->i2c_adap);
1625                 if (fe0->dvb.frontend) {
1626                         struct dvb_frontend *fe;
1627                         if (dvb_attach(dvb_pll_attach, fe0->dvb.frontend, 0x60,
1628                                   &dev->i2c_adap, DVB_PLL_PHILIPS_SD1878_TDA8261) == NULL) {
1629                                 pr_warn("%s: MD7134 DVB-S, no SD1878 "
1630                                         "found !\n", __func__);
1631                                 goto detach_frontend;
1632                         }
1633                         /* we need to open the i2c gate (we know it exists) */
1634                         fe = fe0->dvb.frontend;
1635                         fe->ops.i2c_gate_ctrl(fe, 1);
1636                         if (dvb_attach(isl6405_attach, fe,
1637                                         &dev->i2c_adap, 0x08, 0, 0) == NULL) {
1638                                 pr_warn("%s: MD7134 DVB-S, no ISL6405 "
1639                                         "found !\n", __func__);
1640                                 goto detach_frontend;
1641                         }
1642                         fe->ops.i2c_gate_ctrl(fe, 0);
1643                         dev->original_set_voltage = fe->ops.set_voltage;
1644                         fe->ops.set_voltage = md8800_set_voltage;
1645                         dev->original_set_high_voltage = fe->ops.enable_high_lnb_voltage;
1646                         fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage;
1647                 }
1648                 break;
1649         case SAA7134_BOARD_AVERMEDIA_M103:
1650                 saa7134_set_gpio(dev, 25, 0);
1651                 msleep(10);
1652                 saa7134_set_gpio(dev, 25, 1);
1653                 fe0->dvb.frontend = dvb_attach(mt352_attach,
1654                                                 &avermedia_xc3028_mt352_dev,
1655                                                 &dev->i2c_adap);
1656                 attach_xc3028 = 1;
1657                 break;
1658         case SAA7134_BOARD_ASUSTeK_TIGER_3IN1:
1659                 if (!use_frontend) {     /* terrestrial */
1660                         if (configure_tda827x_fe(dev, &asus_tiger_3in1_config,
1661                                                         &tda827x_cfg_2) < 0)
1662                                 goto detach_frontend;
1663                 } else {                /* satellite */
1664                         fe0->dvb.frontend = dvb_attach(tda10086_attach,
1665                                                 &flydvbs, &dev->i2c_adap);
1666                         if (fe0->dvb.frontend) {
1667                                 if (dvb_attach(tda826x_attach,
1668                                                 fe0->dvb.frontend, 0x60,
1669                                                 &dev->i2c_adap, 0) == NULL) {
1670                                         pr_warn("%s: Asus Tiger 3in1, no "
1671                                                 "tda826x found!\n", __func__);
1672                                         goto detach_frontend;
1673                                 }
1674                                 if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1675                                                 &dev->i2c_adap, 0, 0) == NULL) {
1676                                         pr_warn("%s: Asus Tiger 3in1, no lnbp21"
1677                                                 " found!\n", __func__);
1678                                         goto detach_frontend;
1679                                }
1680                        }
1681                }
1682                break;
1683         case SAA7134_BOARD_ASUSTeK_PS3_100:
1684                 if (!use_frontend) {     /* terrestrial */
1685                         if (configure_tda827x_fe(dev, &asus_ps3_100_config,
1686                                                  &tda827x_cfg_2) < 0)
1687                                 goto detach_frontend;
1688                } else {                /* satellite */
1689                         fe0->dvb.frontend = dvb_attach(tda10086_attach,
1690                                                        &flydvbs, &dev->i2c_adap);
1691                         if (fe0->dvb.frontend) {
1692                                 if (dvb_attach(tda826x_attach,
1693                                                fe0->dvb.frontend, 0x60,
1694                                                &dev->i2c_adap, 0) == NULL) {
1695                                         pr_warn("%s: Asus My Cinema PS3-100, no "
1696                                                 "tda826x found!\n", __func__);
1697                                         goto detach_frontend;
1698                                 }
1699                                 if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1700                                                &dev->i2c_adap, 0, 0) == NULL) {
1701                                         pr_warn("%s: Asus My Cinema PS3-100, no lnbp21"
1702                                                 " found!\n", __func__);
1703                                         goto detach_frontend;
1704                                 }
1705                         }
1706                 }
1707                 break;
1708         case SAA7134_BOARD_ASUSTeK_TIGER:
1709                 if (configure_tda827x_fe(dev, &philips_tiger_config,
1710                                          &tda827x_cfg_0) < 0)
1711                         goto detach_frontend;
1712                 break;
1713         case SAA7134_BOARD_BEHOLD_H6:
1714                 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1715                                                 &behold_h6_config,
1716                                                 &dev->i2c_adap);
1717                 if (fe0->dvb.frontend) {
1718                         dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1719                                    &dev->i2c_adap, 0x61,
1720                                    TUNER_PHILIPS_FMD1216MEX_MK3);
1721                 }
1722                 break;
1723         case SAA7134_BOARD_BEHOLD_X7:
1724                 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1725                                                 &behold_x7_config,
1726                                                 &dev->i2c_adap);
1727                 if (fe0->dvb.frontend) {
1728                         dvb_attach(xc5000_attach, fe0->dvb.frontend,
1729                                    &dev->i2c_adap, &behold_x7_tunerconfig);
1730                 }
1731                 break;
1732         case SAA7134_BOARD_BEHOLD_H7:
1733                 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1734                                                 &behold_x7_config,
1735                                                 &dev->i2c_adap);
1736                 if (fe0->dvb.frontend) {
1737                         dvb_attach(xc5000_attach, fe0->dvb.frontend,
1738                                    &dev->i2c_adap, &behold_x7_tunerconfig);
1739                 }
1740                 break;
1741         case SAA7134_BOARD_AVERMEDIA_A700_PRO:
1742         case SAA7134_BOARD_AVERMEDIA_A700_HYBRID:
1743                 /* Zarlink ZL10313 */
1744                 fe0->dvb.frontend = dvb_attach(mt312_attach,
1745                         &avertv_a700_mt312, &dev->i2c_adap);
1746                 if (fe0->dvb.frontend) {
1747                         if (dvb_attach(zl10036_attach, fe0->dvb.frontend,
1748                                         &avertv_a700_tuner, &dev->i2c_adap) == NULL) {
1749                                 pr_warn("%s: No zl10036 found!\n",
1750                                         __func__);
1751                         }
1752                 }
1753                 break;
1754         case SAA7134_BOARD_VIDEOMATE_S350:
1755                 fe0->dvb.frontend = dvb_attach(mt312_attach,
1756                                 &zl10313_compro_s350_config, &dev->i2c_adap);
1757                 if (fe0->dvb.frontend)
1758                         if (dvb_attach(zl10039_attach, fe0->dvb.frontend,
1759                                         0x60, &dev->i2c_adap) == NULL)
1760                                 pr_warn("%s: No zl10039 found!\n",
1761                                         __func__);
1762
1763                 break;
1764         case SAA7134_BOARD_VIDEOMATE_T750:
1765                 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1766                                                 &videomate_t750_zl10353_config,
1767                                                 &dev->i2c_adap);
1768                 if (fe0->dvb.frontend != NULL) {
1769                         if (dvb_attach(qt1010_attach,
1770                                         fe0->dvb.frontend,
1771                                         &dev->i2c_adap,
1772                                         &videomate_t750_qt1010_config) == NULL)
1773                                 pr_warn("error attaching QT1010\n");
1774                 }
1775                 break;
1776         case SAA7134_BOARD_ZOLID_HYBRID_PCI:
1777                 fe0->dvb.frontend = dvb_attach(tda10048_attach,
1778                                                &zolid_tda10048_config,
1779                                                &dev->i2c_adap);
1780                 if (fe0->dvb.frontend != NULL) {
1781                         dvb_attach(tda829x_attach, fe0->dvb.frontend,
1782                                    &dev->i2c_adap, 0x4b,
1783                                    &tda829x_no_probe);
1784                         dvb_attach(tda18271_attach, fe0->dvb.frontend,
1785                                    0x60, &dev->i2c_adap,
1786                                    &zolid_tda18271_config);
1787                 }
1788                 break;
1789         case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
1790                 fe0->dvb.frontend = dvb_attach(tda10048_attach,
1791                                                &dtv1000s_tda10048_config,
1792                                                &dev->i2c_adap);
1793                 if (fe0->dvb.frontend != NULL) {
1794                         dvb_attach(tda829x_attach, fe0->dvb.frontend,
1795                                    &dev->i2c_adap, 0x4b,
1796                                    &tda829x_no_probe);
1797                         dvb_attach(tda18271_attach, fe0->dvb.frontend,
1798                                    0x60, &dev->i2c_adap,
1799                                    &dtv1000s_tda18271_config);
1800                 }
1801                 break;
1802         case SAA7134_BOARD_KWORLD_PCI_SBTVD_FULLSEG:
1803                 /* Switch to digital mode */
1804                 saa7134_tuner_callback(dev, 0,
1805                                        TDA18271_CALLBACK_CMD_AGC_ENABLE, 1);
1806                 fe0->dvb.frontend = dvb_attach(mb86a20s_attach,
1807                                                &kworld_mb86a20s_config,
1808                                                &dev->i2c_adap);
1809                 if (fe0->dvb.frontend != NULL) {
1810                         dvb_attach(tda829x_attach, fe0->dvb.frontend,
1811                                    &dev->i2c_adap, 0x4b,
1812                                    &tda829x_no_probe);
1813                         fe0->dvb.frontend->ops.i2c_gate_ctrl = kworld_sbtvd_gate_ctrl;
1814                         dvb_attach(tda18271_attach, fe0->dvb.frontend,
1815                                    0x60, &dev->i2c_adap,
1816                                    &kworld_tda18271_config);
1817                 }
1818
1819                 /* mb86a20s need to use the I2C gateway */
1820                 break;
1821         case SAA7134_BOARD_MAGICPRO_PROHDTV_PRO2:
1822                 fe0->dvb.frontend = dvb_attach(lgs8gxx_attach,
1823                                                &prohdtv_pro2_lgs8g75_config,
1824                                                &dev->i2c_adap);
1825                 if (fe0->dvb.frontend != NULL) {
1826                         dvb_attach(tda829x_attach, fe0->dvb.frontend,
1827                                    &dev->i2c_adap, 0x4b,
1828                                    &tda829x_no_probe);
1829                         dvb_attach(tda18271_attach, fe0->dvb.frontend,
1830                                    0x60, &dev->i2c_adap,
1831                                    &prohdtv_pro2_tda18271_config);
1832                 }
1833                 break;
1834         case SAA7134_BOARD_AVERMEDIA_A706:
1835                 /* Enable all DVB-S devices now */
1836                 /* CE5039 DVB-S tuner SLEEP pin low */
1837                 saa7134_set_gpio(dev, 23, 0);
1838                 /* CE6313 DVB-S demod SLEEP pin low */
1839                 saa7134_set_gpio(dev, 9, 0);
1840                 /* CE6313 DVB-S demod RESET# pin high */
1841                 saa7134_set_gpio(dev, 25, 1);
1842                 msleep(1);
1843                 fe0->dvb.frontend = dvb_attach(mt312_attach,
1844                                 &zl10313_avermedia_a706_config, &dev->i2c_adap);
1845                 if (fe0->dvb.frontend) {
1846                         fe0->dvb.frontend->ops.i2c_gate_ctrl = NULL;
1847                         if (dvb_attach(zl10039_attach, fe0->dvb.frontend,
1848                                         0x60, &dev->i2c_adap) == NULL)
1849                                 pr_warn("%s: No zl10039 found!\n",
1850                                         __func__);
1851                 }
1852                 break;
1853         default:
1854                 pr_warn("Huh? unknown DVB card?\n");
1855                 break;
1856         }
1857
1858         if (attach_xc3028) {
1859                 struct dvb_frontend *fe;
1860                 struct xc2028_config cfg = {
1861                         .i2c_adap  = &dev->i2c_adap,
1862                         .i2c_addr  = 0x61,
1863                 };
1864
1865                 if (!fe0->dvb.frontend)
1866                         goto detach_frontend;
1867
1868                 fe = dvb_attach(xc2028_attach, fe0->dvb.frontend, &cfg);
1869                 if (!fe) {
1870                         pr_err("%s/2: xc3028 attach failed\n",
1871                                dev->name);
1872                         goto detach_frontend;
1873                 }
1874         }
1875
1876         if (NULL == fe0->dvb.frontend) {
1877                 pr_err("%s/dvb: frontend initialization failed\n", dev->name);
1878                 goto detach_frontend;
1879         }
1880         /* define general-purpose callback pointer */
1881         fe0->dvb.frontend->callback = saa7134_tuner_callback;
1882
1883         /* register everything else */
1884         ret = vb2_dvb_register_bus(&dev->frontends, THIS_MODULE, dev,
1885                                         &dev->pci->dev, adapter_nr, 0);
1886
1887         /* this sequence is necessary to make the tda1004x load its firmware
1888          * and to enter analog mode of hybrid boards
1889          */
1890         if (!ret) {
1891                 if (fe0->dvb.frontend->ops.init)
1892                         fe0->dvb.frontend->ops.init(fe0->dvb.frontend);
1893                 if (fe0->dvb.frontend->ops.sleep)
1894                         fe0->dvb.frontend->ops.sleep(fe0->dvb.frontend);
1895                 if (fe0->dvb.frontend->ops.tuner_ops.sleep)
1896                         fe0->dvb.frontend->ops.tuner_ops.sleep(fe0->dvb.frontend);
1897         }
1898         return ret;
1899
1900 detach_frontend:
1901         vb2_dvb_dealloc_frontends(&dev->frontends);
1902         vb2_queue_release(&fe0->dvb.dvbq);
1903         return -EINVAL;
1904 }
1905
1906 static int dvb_fini(struct saa7134_dev *dev)
1907 {
1908         struct vb2_dvb_frontend *fe0;
1909
1910         /* Get the first frontend */
1911         fe0 = vb2_dvb_get_frontend(&dev->frontends, 1);
1912         if (!fe0)
1913                 return -EINVAL;
1914
1915         /* FIXME: I suspect that this code is bogus, since the entry for
1916            Pinnacle 300I DVB-T PAL already defines the proper init to allow
1917            the detection of mt2032 (TDA9887_PORT2_INACTIVE)
1918          */
1919         if (dev->board == SAA7134_BOARD_PINNACLE_300I_DVBT_PAL) {
1920                 struct v4l2_priv_tun_config tda9887_cfg;
1921                 static int on  = TDA9887_PRESENT | TDA9887_PORT2_INACTIVE;
1922
1923                 tda9887_cfg.tuner = TUNER_TDA9887;
1924                 tda9887_cfg.priv  = &on;
1925
1926                 /* otherwise we don't detect the tuner on next insmod */
1927                 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1928         } else if (dev->board == SAA7134_BOARD_MEDION_MD8800_QUADRO) {
1929                 if ((dev->eedata[2] == 0x07) && use_frontend) {
1930                         /* turn off the 2nd lnb supply */
1931                         u8 data = 0x80;
1932                         struct i2c_msg msg = {.addr = 0x08, .buf = &data, .flags = 0, .len = 1};
1933                         struct dvb_frontend *fe;
1934                         fe = fe0->dvb.frontend;
1935                         if (fe->ops.i2c_gate_ctrl) {
1936                                 fe->ops.i2c_gate_ctrl(fe, 1);
1937                                 i2c_transfer(&dev->i2c_adap, &msg, 1);
1938                                 fe->ops.i2c_gate_ctrl(fe, 0);
1939                         }
1940                 }
1941         }
1942         vb2_dvb_unregister_bus(&dev->frontends);
1943         vb2_queue_release(&fe0->dvb.dvbq);
1944         return 0;
1945 }
1946
1947 static struct saa7134_mpeg_ops dvb_ops = {
1948         .type          = SAA7134_MPEG_DVB,
1949         .init          = dvb_init,
1950         .fini          = dvb_fini,
1951 };
1952
1953 static int __init dvb_register(void)
1954 {
1955         return saa7134_ts_register(&dvb_ops);
1956 }
1957
1958 static void __exit dvb_unregister(void)
1959 {
1960         saa7134_ts_unregister(&dvb_ops);
1961 }
1962
1963 module_init(dvb_register);
1964 module_exit(dvb_unregister);