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