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 			/*
1268 			 * The TV tuner on this board is actually NOT
1269 			 * behind the demod i2c gate.
1270 			 * However, the demod EEPROM is indeed there and it
1271 			 * conflicts with the SAA7134 chip config EEPROM
1272 			 * if the i2c gate is open (since they have same
1273 			 * bus addresses) resulting in card PCI SVID / SSID
1274 			 * being garbage after a reboot from time to time.
1275 			 *
1276 			 * Let's just leave the gate permanently closed -
1277 			 * saa7134_i2c_eeprom_md7134_gate() will close it for
1278 			 * us at probe time if it was open for some reason.
1279 			 */
1280 			fe0->dvb.frontend->ops.i2c_gate_ctrl = NULL;
1281 			dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1282 				   &dev->i2c_adap, medion_cardbus.tuner_address,
1283 				   TUNER_PHILIPS_FMD1216ME_MK3);
1284 		}
1285 		break;
1286 	case SAA7134_BOARD_PHILIPS_TOUGH:
1287 		fe0->dvb.frontend = dvb_attach(tda10046_attach,
1288 					       &philips_tu1216_60_config,
1289 					       &dev->i2c_adap);
1290 		if (fe0->dvb.frontend) {
1291 			fe0->dvb.frontend->ops.tuner_ops.init = philips_tu1216_init;
1292 			fe0->dvb.frontend->ops.tuner_ops.set_params = philips_tda6651_pll_set;
1293 		}
1294 		break;
1295 	case SAA7134_BOARD_FLYDVBTDUO:
1296 	case SAA7134_BOARD_FLYDVBT_DUO_CARDBUS:
1297 		if (configure_tda827x_fe(dev, &tda827x_lifeview_config,
1298 					 &tda827x_cfg_0) < 0)
1299 			goto detach_frontend;
1300 		break;
1301 	case SAA7134_BOARD_PHILIPS_EUROPA:
1302 	case SAA7134_BOARD_VIDEOMATE_DVBT_300:
1303 	case SAA7134_BOARD_ASUS_EUROPA_HYBRID:
1304 		fe0->dvb.frontend = dvb_attach(tda10046_attach,
1305 					       &philips_europa_config,
1306 					       &dev->i2c_adap);
1307 		if (fe0->dvb.frontend) {
1308 			dev->original_demod_sleep = fe0->dvb.frontend->ops.sleep;
1309 			fe0->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1310 			fe0->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1311 			fe0->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1312 			fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1313 		}
1314 		break;
1315 	case SAA7134_BOARD_TECHNOTREND_BUDGET_T3000:
1316 		fe0->dvb.frontend = dvb_attach(tda10046_attach,
1317 					       &technotrend_budget_t3000_config,
1318 					       &dev->i2c_adap);
1319 		if (fe0->dvb.frontend) {
1320 			dev->original_demod_sleep = fe0->dvb.frontend->ops.sleep;
1321 			fe0->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1322 			fe0->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1323 			fe0->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1324 			fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1325 		}
1326 		break;
1327 	case SAA7134_BOARD_VIDEOMATE_DVBT_200:
1328 		fe0->dvb.frontend = dvb_attach(tda10046_attach,
1329 					       &philips_tu1216_61_config,
1330 					       &dev->i2c_adap);
1331 		if (fe0->dvb.frontend) {
1332 			fe0->dvb.frontend->ops.tuner_ops.init = philips_tu1216_init;
1333 			fe0->dvb.frontend->ops.tuner_ops.set_params = philips_tda6651_pll_set;
1334 		}
1335 		break;
1336 	case SAA7134_BOARD_KWORLD_DVBT_210:
1337 		if (configure_tda827x_fe(dev, &kworld_dvb_t_210_config,
1338 					 &tda827x_cfg_2) < 0)
1339 			goto detach_frontend;
1340 		break;
1341 	case SAA7134_BOARD_HAUPPAUGE_HVR1120:
1342 		fe0->dvb.frontend = dvb_attach(tda10048_attach,
1343 					       &hcw_tda10048_config,
1344 					       &dev->i2c_adap);
1345 		if (fe0->dvb.frontend != NULL) {
1346 			dvb_attach(tda829x_attach, fe0->dvb.frontend,
1347 				   &dev->i2c_adap, 0x4b,
1348 				   &tda829x_no_probe);
1349 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1350 				   0x60, &dev->i2c_adap,
1351 				   &hcw_tda18271_config);
1352 		}
1353 		break;
1354 	case SAA7134_BOARD_PHILIPS_TIGER:
1355 		if (configure_tda827x_fe(dev, &philips_tiger_config,
1356 					 &tda827x_cfg_0) < 0)
1357 			goto detach_frontend;
1358 		break;
1359 	case SAA7134_BOARD_PINNACLE_PCTV_310i:
1360 		if (configure_tda827x_fe(dev, &pinnacle_pctv_310i_config,
1361 					 &tda827x_cfg_1) < 0)
1362 			goto detach_frontend;
1363 		break;
1364 	case SAA7134_BOARD_HAUPPAUGE_HVR1110:
1365 		if (configure_tda827x_fe(dev, &hauppauge_hvr_1110_config,
1366 					 &tda827x_cfg_1) < 0)
1367 			goto detach_frontend;
1368 		break;
1369 	case SAA7134_BOARD_HAUPPAUGE_HVR1150:
1370 		fe0->dvb.frontend = dvb_attach(lgdt3305_attach,
1371 					       &hcw_lgdt3305_config,
1372 					       &dev->i2c_adap);
1373 		if (fe0->dvb.frontend) {
1374 			dvb_attach(tda829x_attach, fe0->dvb.frontend,
1375 				   &dev->i2c_adap, 0x4b,
1376 				   &tda829x_no_probe);
1377 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1378 				   0x60, &dev->i2c_adap,
1379 				   &hcw_tda18271_config);
1380 		}
1381 		break;
1382 	case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
1383 		if (configure_tda827x_fe(dev, &asus_p7131_dual_config,
1384 					 &tda827x_cfg_0) < 0)
1385 			goto detach_frontend;
1386 		break;
1387 	case SAA7134_BOARD_FLYDVBT_LR301:
1388 		if (configure_tda827x_fe(dev, &tda827x_lifeview_config,
1389 					 &tda827x_cfg_0) < 0)
1390 			goto detach_frontend;
1391 		break;
1392 	case SAA7134_BOARD_FLYDVB_TRIO:
1393 		if (!use_frontend) {	/* terrestrial */
1394 			if (configure_tda827x_fe(dev, &lifeview_trio_config,
1395 						 &tda827x_cfg_0) < 0)
1396 				goto detach_frontend;
1397 		} else {		/* satellite */
1398 			fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs, &dev->i2c_adap);
1399 			if (fe0->dvb.frontend) {
1400 				if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x63,
1401 									&dev->i2c_adap, 0) == NULL) {
1402 					pr_warn("%s: Lifeview Trio, No tda826x found!\n",
1403 						__func__);
1404 					goto detach_frontend;
1405 				}
1406 				if (dvb_attach(isl6421_attach, fe0->dvb.frontend,
1407 					       &dev->i2c_adap,
1408 					       0x08, 0, 0, false) == NULL) {
1409 					pr_warn("%s: Lifeview Trio, No ISL6421 found!\n",
1410 						__func__);
1411 					goto detach_frontend;
1412 				}
1413 			}
1414 		}
1415 		break;
1416 	case SAA7134_BOARD_ADS_DUO_CARDBUS_PTV331:
1417 	case SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS:
1418 		fe0->dvb.frontend = dvb_attach(tda10046_attach,
1419 					       &ads_tech_duo_config,
1420 					       &dev->i2c_adap);
1421 		if (fe0->dvb.frontend) {
1422 			if (dvb_attach(tda827x_attach,fe0->dvb.frontend,
1423 				   ads_tech_duo_config.tuner_address, &dev->i2c_adap,
1424 								&ads_duo_cfg) == NULL) {
1425 				pr_warn("no tda827x tuner found at addr: %02x\n",
1426 					ads_tech_duo_config.tuner_address);
1427 				goto detach_frontend;
1428 			}
1429 		} else
1430 			pr_warn("failed to attach tda10046\n");
1431 		break;
1432 	case SAA7134_BOARD_TEVION_DVBT_220RF:
1433 		if (configure_tda827x_fe(dev, &tevion_dvbt220rf_config,
1434 					 &tda827x_cfg_0) < 0)
1435 			goto detach_frontend;
1436 		break;
1437 	case SAA7134_BOARD_MEDION_MD8800_QUADRO:
1438 		if (!use_frontend) {     /* terrestrial */
1439 			if (configure_tda827x_fe(dev, &md8800_dvbt_config,
1440 						 &tda827x_cfg_0) < 0)
1441 				goto detach_frontend;
1442 		} else {        /* satellite */
1443 			fe0->dvb.frontend = dvb_attach(tda10086_attach,
1444 							&flydvbs, &dev->i2c_adap);
1445 			if (fe0->dvb.frontend) {
1446 				struct dvb_frontend *fe = fe0->dvb.frontend;
1447 				u8 dev_id = dev->eedata[2];
1448 				u8 data = 0xc4;
1449 				struct i2c_msg msg = {.addr = 0x08, .flags = 0, .len = 1};
1450 
1451 				if (dvb_attach(tda826x_attach, fe0->dvb.frontend,
1452 						0x60, &dev->i2c_adap, 0) == NULL) {
1453 					pr_warn("%s: Medion Quadro, no tda826x found !\n",
1454 						__func__);
1455 					goto detach_frontend;
1456 				}
1457 				if (dev_id != 0x08) {
1458 					/* we need to open the i2c gate (we know it exists) */
1459 					fe->ops.i2c_gate_ctrl(fe, 1);
1460 					if (dvb_attach(isl6405_attach, fe,
1461 							&dev->i2c_adap, 0x08, 0, 0) == NULL) {
1462 						pr_warn("%s: Medion Quadro, no ISL6405 found !\n",
1463 							__func__);
1464 						goto detach_frontend;
1465 					}
1466 					if (dev_id == 0x07) {
1467 						/* fire up the 2nd section of the LNB supply since
1468 						   we can't do this from the other section */
1469 						msg.buf = &data;
1470 						i2c_transfer(&dev->i2c_adap, &msg, 1);
1471 					}
1472 					fe->ops.i2c_gate_ctrl(fe, 0);
1473 					dev->original_set_voltage = fe->ops.set_voltage;
1474 					fe->ops.set_voltage = md8800_set_voltage;
1475 					dev->original_set_high_voltage = fe->ops.enable_high_lnb_voltage;
1476 					fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage;
1477 				} else {
1478 					fe->ops.set_voltage = md8800_set_voltage2;
1479 					fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage2;
1480 				}
1481 			}
1482 		}
1483 		break;
1484 	case SAA7134_BOARD_AVERMEDIA_AVERTVHD_A180:
1485 		fe0->dvb.frontend = dvb_attach(nxt200x_attach, &avertvhda180,
1486 					       &dev->i2c_adap);
1487 		if (fe0->dvb.frontend)
1488 			dvb_attach(dvb_pll_attach, fe0->dvb.frontend, 0x61,
1489 				   NULL, DVB_PLL_TDHU2);
1490 		break;
1491 	case SAA7134_BOARD_ADS_INSTANT_HDTV_PCI:
1492 	case SAA7134_BOARD_KWORLD_ATSC110:
1493 		fe0->dvb.frontend = dvb_attach(nxt200x_attach, &kworldatsc110,
1494 					       &dev->i2c_adap);
1495 		if (fe0->dvb.frontend)
1496 			dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1497 				   &dev->i2c_adap, 0x61,
1498 				   TUNER_PHILIPS_TUV1236D);
1499 		break;
1500 	case SAA7134_BOARD_KWORLD_PC150U:
1501 		saa7134_set_gpio(dev, 18, 1); /* Switch to digital mode */
1502 		saa7134_tuner_callback(dev, 0,
1503 				       TDA18271_CALLBACK_CMD_AGC_ENABLE, 1);
1504 		fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1505 					       &kworld_s5h1411_config,
1506 					       &dev->i2c_adap);
1507 		if (fe0->dvb.frontend != NULL) {
1508 			dvb_attach(tda829x_attach, fe0->dvb.frontend,
1509 				   &dev->i2c_adap, 0x4b,
1510 				   &tda829x_no_probe);
1511 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1512 				   0x60, &dev->i2c_adap,
1513 				   &kworld_pc150u_tda18271_config);
1514 		}
1515 		break;
1516 	case SAA7134_BOARD_FLYDVBS_LR300:
1517 		fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs,
1518 					       &dev->i2c_adap);
1519 		if (fe0->dvb.frontend) {
1520 			if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x60,
1521 				       &dev->i2c_adap, 0) == NULL) {
1522 				pr_warn("%s: No tda826x found!\n", __func__);
1523 				goto detach_frontend;
1524 			}
1525 			if (dvb_attach(isl6421_attach, fe0->dvb.frontend,
1526 				       &dev->i2c_adap,
1527 				       0x08, 0, 0, false) == NULL) {
1528 				pr_warn("%s: No ISL6421 found!\n", __func__);
1529 				goto detach_frontend;
1530 			}
1531 		}
1532 		break;
1533 	case SAA7134_BOARD_ASUS_EUROPA2_HYBRID:
1534 		fe0->dvb.frontend = dvb_attach(tda10046_attach,
1535 					       &medion_cardbus,
1536 					       &dev->i2c_adap);
1537 		if (fe0->dvb.frontend) {
1538 			dev->original_demod_sleep = fe0->dvb.frontend->ops.sleep;
1539 			fe0->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1540 
1541 			dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1542 				   &dev->i2c_adap, medion_cardbus.tuner_address,
1543 				   TUNER_PHILIPS_FMD1216ME_MK3);
1544 		}
1545 		break;
1546 	case SAA7134_BOARD_VIDEOMATE_DVBT_200A:
1547 		fe0->dvb.frontend = dvb_attach(tda10046_attach,
1548 				&philips_europa_config,
1549 				&dev->i2c_adap);
1550 		if (fe0->dvb.frontend) {
1551 			fe0->dvb.frontend->ops.tuner_ops.init = philips_td1316_tuner_init;
1552 			fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1553 		}
1554 		break;
1555 	case SAA7134_BOARD_CINERGY_HT_PCMCIA:
1556 		if (configure_tda827x_fe(dev, &cinergy_ht_config,
1557 					 &tda827x_cfg_0) < 0)
1558 			goto detach_frontend;
1559 		break;
1560 	case SAA7134_BOARD_CINERGY_HT_PCI:
1561 		if (configure_tda827x_fe(dev, &cinergy_ht_pci_config,
1562 					 &tda827x_cfg_0) < 0)
1563 			goto detach_frontend;
1564 		break;
1565 	case SAA7134_BOARD_PHILIPS_TIGER_S:
1566 		if (configure_tda827x_fe(dev, &philips_tiger_s_config,
1567 					 &tda827x_cfg_2) < 0)
1568 			goto detach_frontend;
1569 		break;
1570 	case SAA7134_BOARD_ASUS_P7131_4871:
1571 		if (configure_tda827x_fe(dev, &asus_p7131_4871_config,
1572 					 &tda827x_cfg_2) < 0)
1573 			goto detach_frontend;
1574 		break;
1575 	case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
1576 		if (configure_tda827x_fe(dev, &asus_p7131_hybrid_lna_config,
1577 					 &tda827x_cfg_2) < 0)
1578 			goto detach_frontend;
1579 		break;
1580 	case SAA7134_BOARD_AVERMEDIA_SUPER_007:
1581 		if (configure_tda827x_fe(dev, &avermedia_super_007_config,
1582 					 &tda827x_cfg_0) < 0)
1583 			goto detach_frontend;
1584 		break;
1585 	case SAA7134_BOARD_TWINHAN_DTV_DVB_3056:
1586 		if (configure_tda827x_fe(dev, &twinhan_dtv_dvb_3056_config,
1587 					 &tda827x_cfg_2_sw42) < 0)
1588 			goto detach_frontend;
1589 		break;
1590 	case SAA7134_BOARD_PHILIPS_SNAKE:
1591 		fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs,
1592 						&dev->i2c_adap);
1593 		if (fe0->dvb.frontend) {
1594 			if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x60,
1595 					&dev->i2c_adap, 0) == NULL) {
1596 				pr_warn("%s: No tda826x found!\n", __func__);
1597 				goto detach_frontend;
1598 			}
1599 			if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1600 					&dev->i2c_adap, 0, 0) == NULL) {
1601 				pr_warn("%s: No lnbp21 found!\n", __func__);
1602 				goto detach_frontend;
1603 			}
1604 		}
1605 		break;
1606 	case SAA7134_BOARD_CREATIX_CTX953:
1607 		if (configure_tda827x_fe(dev, &md8800_dvbt_config,
1608 					 &tda827x_cfg_0) < 0)
1609 			goto detach_frontend;
1610 		break;
1611 	case SAA7134_BOARD_MSI_TVANYWHERE_AD11:
1612 		if (configure_tda827x_fe(dev, &philips_tiger_s_config,
1613 					 &tda827x_cfg_2) < 0)
1614 			goto detach_frontend;
1615 		break;
1616 	case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
1617 		pr_debug("AverMedia E506R dvb setup\n");
1618 		saa7134_set_gpio(dev, 25, 0);
1619 		msleep(10);
1620 		saa7134_set_gpio(dev, 25, 1);
1621 		fe0->dvb.frontend = dvb_attach(mt352_attach,
1622 						&avermedia_xc3028_mt352_dev,
1623 						&dev->i2c_adap);
1624 		attach_xc3028 = 1;
1625 		break;
1626 	case SAA7134_BOARD_MD7134_BRIDGE_2:
1627 		fe0->dvb.frontend = dvb_attach(tda10086_attach,
1628 						&sd1878_4m, &dev->i2c_adap);
1629 		if (fe0->dvb.frontend) {
1630 			struct dvb_frontend *fe;
1631 			if (dvb_attach(dvb_pll_attach, fe0->dvb.frontend, 0x60,
1632 				  &dev->i2c_adap, DVB_PLL_PHILIPS_SD1878_TDA8261) == NULL) {
1633 				pr_warn("%s: MD7134 DVB-S, no SD1878 found !\n",
1634 					__func__);
1635 				goto detach_frontend;
1636 			}
1637 			/* we need to open the i2c gate (we know it exists) */
1638 			fe = fe0->dvb.frontend;
1639 			fe->ops.i2c_gate_ctrl(fe, 1);
1640 			if (dvb_attach(isl6405_attach, fe,
1641 					&dev->i2c_adap, 0x08, 0, 0) == NULL) {
1642 				pr_warn("%s: MD7134 DVB-S, no ISL6405 found !\n",
1643 					__func__);
1644 				goto detach_frontend;
1645 			}
1646 			fe->ops.i2c_gate_ctrl(fe, 0);
1647 			dev->original_set_voltage = fe->ops.set_voltage;
1648 			fe->ops.set_voltage = md8800_set_voltage;
1649 			dev->original_set_high_voltage = fe->ops.enable_high_lnb_voltage;
1650 			fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage;
1651 		}
1652 		break;
1653 	case SAA7134_BOARD_AVERMEDIA_M103:
1654 		saa7134_set_gpio(dev, 25, 0);
1655 		msleep(10);
1656 		saa7134_set_gpio(dev, 25, 1);
1657 		fe0->dvb.frontend = dvb_attach(mt352_attach,
1658 						&avermedia_xc3028_mt352_dev,
1659 						&dev->i2c_adap);
1660 		attach_xc3028 = 1;
1661 		break;
1662 	case SAA7134_BOARD_ASUSTeK_TIGER_3IN1:
1663 		if (!use_frontend) {     /* terrestrial */
1664 			if (configure_tda827x_fe(dev, &asus_tiger_3in1_config,
1665 							&tda827x_cfg_2) < 0)
1666 				goto detach_frontend;
1667 		} else {		/* satellite */
1668 			fe0->dvb.frontend = dvb_attach(tda10086_attach,
1669 						&flydvbs, &dev->i2c_adap);
1670 			if (fe0->dvb.frontend) {
1671 				if (dvb_attach(tda826x_attach,
1672 						fe0->dvb.frontend, 0x60,
1673 						&dev->i2c_adap, 0) == NULL) {
1674 					pr_warn("%s: Asus Tiger 3in1, no tda826x found!\n",
1675 						__func__);
1676 					goto detach_frontend;
1677 				}
1678 				if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1679 						&dev->i2c_adap, 0, 0) == NULL) {
1680 					pr_warn("%s: Asus Tiger 3in1, no lnbp21 found!\n",
1681 						__func__);
1682 					goto detach_frontend;
1683 			       }
1684 		       }
1685 	       }
1686 	       break;
1687 	case SAA7134_BOARD_ASUSTeK_PS3_100:
1688 		if (!use_frontend) {     /* terrestrial */
1689 			if (configure_tda827x_fe(dev, &asus_ps3_100_config,
1690 						 &tda827x_cfg_2) < 0)
1691 				goto detach_frontend;
1692 	       } else {                /* satellite */
1693 			fe0->dvb.frontend = dvb_attach(tda10086_attach,
1694 						       &flydvbs, &dev->i2c_adap);
1695 			if (fe0->dvb.frontend) {
1696 				if (dvb_attach(tda826x_attach,
1697 					       fe0->dvb.frontend, 0x60,
1698 					       &dev->i2c_adap, 0) == NULL) {
1699 					pr_warn("%s: Asus My Cinema PS3-100, no tda826x found!\n",
1700 						__func__);
1701 					goto detach_frontend;
1702 				}
1703 				if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1704 					       &dev->i2c_adap, 0, 0) == NULL) {
1705 					pr_warn("%s: Asus My Cinema PS3-100, no lnbp21 found!\n",
1706 						__func__);
1707 					goto detach_frontend;
1708 				}
1709 			}
1710 		}
1711 		break;
1712 	case SAA7134_BOARD_ASUSTeK_TIGER:
1713 		if (configure_tda827x_fe(dev, &philips_tiger_config,
1714 					 &tda827x_cfg_0) < 0)
1715 			goto detach_frontend;
1716 		break;
1717 	case SAA7134_BOARD_BEHOLD_H6:
1718 		fe0->dvb.frontend = dvb_attach(zl10353_attach,
1719 						&behold_h6_config,
1720 						&dev->i2c_adap);
1721 		if (fe0->dvb.frontend) {
1722 			dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1723 				   &dev->i2c_adap, 0x61,
1724 				   TUNER_PHILIPS_FMD1216MEX_MK3);
1725 		}
1726 		break;
1727 	case SAA7134_BOARD_BEHOLD_X7:
1728 		fe0->dvb.frontend = dvb_attach(zl10353_attach,
1729 						&behold_x7_config,
1730 						&dev->i2c_adap);
1731 		if (fe0->dvb.frontend) {
1732 			dvb_attach(xc5000_attach, fe0->dvb.frontend,
1733 				   &dev->i2c_adap, &behold_x7_tunerconfig);
1734 		}
1735 		break;
1736 	case SAA7134_BOARD_BEHOLD_H7:
1737 		fe0->dvb.frontend = dvb_attach(zl10353_attach,
1738 						&behold_x7_config,
1739 						&dev->i2c_adap);
1740 		if (fe0->dvb.frontend) {
1741 			dvb_attach(xc5000_attach, fe0->dvb.frontend,
1742 				   &dev->i2c_adap, &behold_x7_tunerconfig);
1743 		}
1744 		break;
1745 	case SAA7134_BOARD_AVERMEDIA_A700_PRO:
1746 	case SAA7134_BOARD_AVERMEDIA_A700_HYBRID:
1747 		/* Zarlink ZL10313 */
1748 		fe0->dvb.frontend = dvb_attach(mt312_attach,
1749 			&avertv_a700_mt312, &dev->i2c_adap);
1750 		if (fe0->dvb.frontend) {
1751 			if (dvb_attach(zl10036_attach, fe0->dvb.frontend,
1752 					&avertv_a700_tuner, &dev->i2c_adap) == NULL) {
1753 				pr_warn("%s: No zl10036 found!\n",
1754 					__func__);
1755 			}
1756 		}
1757 		break;
1758 	case SAA7134_BOARD_VIDEOMATE_S350:
1759 		fe0->dvb.frontend = dvb_attach(mt312_attach,
1760 				&zl10313_compro_s350_config, &dev->i2c_adap);
1761 		if (fe0->dvb.frontend)
1762 			if (dvb_attach(zl10039_attach, fe0->dvb.frontend,
1763 					0x60, &dev->i2c_adap) == NULL)
1764 				pr_warn("%s: No zl10039 found!\n",
1765 					__func__);
1766 
1767 		break;
1768 	case SAA7134_BOARD_VIDEOMATE_T750:
1769 		fe0->dvb.frontend = dvb_attach(zl10353_attach,
1770 						&videomate_t750_zl10353_config,
1771 						&dev->i2c_adap);
1772 		if (fe0->dvb.frontend != NULL) {
1773 			if (dvb_attach(qt1010_attach,
1774 					fe0->dvb.frontend,
1775 					&dev->i2c_adap,
1776 					&videomate_t750_qt1010_config) == NULL)
1777 				pr_warn("error attaching QT1010\n");
1778 		}
1779 		break;
1780 	case SAA7134_BOARD_ZOLID_HYBRID_PCI:
1781 		fe0->dvb.frontend = dvb_attach(tda10048_attach,
1782 					       &zolid_tda10048_config,
1783 					       &dev->i2c_adap);
1784 		if (fe0->dvb.frontend != NULL) {
1785 			dvb_attach(tda829x_attach, fe0->dvb.frontend,
1786 				   &dev->i2c_adap, 0x4b,
1787 				   &tda829x_no_probe);
1788 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1789 				   0x60, &dev->i2c_adap,
1790 				   &zolid_tda18271_config);
1791 		}
1792 		break;
1793 	case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
1794 		fe0->dvb.frontend = dvb_attach(tda10048_attach,
1795 					       &dtv1000s_tda10048_config,
1796 					       &dev->i2c_adap);
1797 		if (fe0->dvb.frontend != NULL) {
1798 			dvb_attach(tda829x_attach, fe0->dvb.frontend,
1799 				   &dev->i2c_adap, 0x4b,
1800 				   &tda829x_no_probe);
1801 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1802 				   0x60, &dev->i2c_adap,
1803 				   &dtv1000s_tda18271_config);
1804 		}
1805 		break;
1806 	case SAA7134_BOARD_KWORLD_PCI_SBTVD_FULLSEG:
1807 		/* Switch to digital mode */
1808 		saa7134_tuner_callback(dev, 0,
1809 				       TDA18271_CALLBACK_CMD_AGC_ENABLE, 1);
1810 		fe0->dvb.frontend = dvb_attach(mb86a20s_attach,
1811 					       &kworld_mb86a20s_config,
1812 					       &dev->i2c_adap);
1813 		if (fe0->dvb.frontend != NULL) {
1814 			dvb_attach(tda829x_attach, fe0->dvb.frontend,
1815 				   &dev->i2c_adap, 0x4b,
1816 				   &tda829x_no_probe);
1817 			fe0->dvb.frontend->ops.i2c_gate_ctrl = kworld_sbtvd_gate_ctrl;
1818 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1819 				   0x60, &dev->i2c_adap,
1820 				   &kworld_tda18271_config);
1821 		}
1822 
1823 		/* mb86a20s need to use the I2C gateway */
1824 		break;
1825 	case SAA7134_BOARD_MAGICPRO_PROHDTV_PRO2:
1826 		fe0->dvb.frontend = dvb_attach(lgs8gxx_attach,
1827 					       &prohdtv_pro2_lgs8g75_config,
1828 					       &dev->i2c_adap);
1829 		if (fe0->dvb.frontend != NULL) {
1830 			dvb_attach(tda829x_attach, fe0->dvb.frontend,
1831 				   &dev->i2c_adap, 0x4b,
1832 				   &tda829x_no_probe);
1833 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1834 				   0x60, &dev->i2c_adap,
1835 				   &prohdtv_pro2_tda18271_config);
1836 		}
1837 		break;
1838 	case SAA7134_BOARD_AVERMEDIA_A706:
1839 		/* Enable all DVB-S devices now */
1840 		/* CE5039 DVB-S tuner SLEEP pin low */
1841 		saa7134_set_gpio(dev, 23, 0);
1842 		/* CE6313 DVB-S demod SLEEP pin low */
1843 		saa7134_set_gpio(dev, 9, 0);
1844 		/* CE6313 DVB-S demod RESET# pin high */
1845 		saa7134_set_gpio(dev, 25, 1);
1846 		msleep(1);
1847 		fe0->dvb.frontend = dvb_attach(mt312_attach,
1848 				&zl10313_avermedia_a706_config, &dev->i2c_adap);
1849 		if (fe0->dvb.frontend) {
1850 			fe0->dvb.frontend->ops.i2c_gate_ctrl = NULL;
1851 			if (dvb_attach(zl10039_attach, fe0->dvb.frontend,
1852 					0x60, &dev->i2c_adap) == NULL)
1853 				pr_warn("%s: No zl10039 found!\n",
1854 					__func__);
1855 		}
1856 		break;
1857 	default:
1858 		pr_warn("Huh? unknown DVB card?\n");
1859 		break;
1860 	}
1861 
1862 	if (attach_xc3028) {
1863 		struct dvb_frontend *fe;
1864 		struct xc2028_config cfg = {
1865 			.i2c_adap  = &dev->i2c_adap,
1866 			.i2c_addr  = 0x61,
1867 		};
1868 
1869 		if (!fe0->dvb.frontend)
1870 			goto detach_frontend;
1871 
1872 		fe = dvb_attach(xc2028_attach, fe0->dvb.frontend, &cfg);
1873 		if (!fe) {
1874 			pr_err("%s/2: xc3028 attach failed\n",
1875 			       dev->name);
1876 			goto detach_frontend;
1877 		}
1878 	}
1879 
1880 	if (NULL == fe0->dvb.frontend) {
1881 		pr_err("%s/dvb: frontend initialization failed\n", dev->name);
1882 		goto detach_frontend;
1883 	}
1884 	/* define general-purpose callback pointer */
1885 	fe0->dvb.frontend->callback = saa7134_tuner_callback;
1886 
1887 	/* register everything else */
1888 #ifndef CONFIG_MEDIA_CONTROLLER_DVB
1889 	ret = vb2_dvb_register_bus(&dev->frontends, THIS_MODULE, dev,
1890 				   &dev->pci->dev, NULL,
1891 				   adapter_nr, 0);
1892 #else
1893 	ret = vb2_dvb_register_bus(&dev->frontends, THIS_MODULE, dev,
1894 				   &dev->pci->dev, dev->media_dev,
1895 				   adapter_nr, 0);
1896 #endif
1897 
1898 	/* this sequence is necessary to make the tda1004x load its firmware
1899 	 * and to enter analog mode of hybrid boards
1900 	 */
1901 	if (!ret) {
1902 		if (fe0->dvb.frontend->ops.init)
1903 			fe0->dvb.frontend->ops.init(fe0->dvb.frontend);
1904 		if (fe0->dvb.frontend->ops.sleep)
1905 			fe0->dvb.frontend->ops.sleep(fe0->dvb.frontend);
1906 		if (fe0->dvb.frontend->ops.tuner_ops.sleep)
1907 			fe0->dvb.frontend->ops.tuner_ops.sleep(fe0->dvb.frontend);
1908 	}
1909 	return ret;
1910 
1911 detach_frontend:
1912 	vb2_dvb_dealloc_frontends(&dev->frontends);
1913 	vb2_queue_release(&fe0->dvb.dvbq);
1914 	return -EINVAL;
1915 }
1916 
1917 static int dvb_fini(struct saa7134_dev *dev)
1918 {
1919 	struct vb2_dvb_frontend *fe0;
1920 
1921 	/* Get the first frontend */
1922 	fe0 = vb2_dvb_get_frontend(&dev->frontends, 1);
1923 	if (!fe0)
1924 		return -EINVAL;
1925 
1926 	/* FIXME: I suspect that this code is bogus, since the entry for
1927 	   Pinnacle 300I DVB-T PAL already defines the proper init to allow
1928 	   the detection of mt2032 (TDA9887_PORT2_INACTIVE)
1929 	 */
1930 	if (dev->board == SAA7134_BOARD_PINNACLE_300I_DVBT_PAL) {
1931 		struct v4l2_priv_tun_config tda9887_cfg;
1932 		static int on  = TDA9887_PRESENT | TDA9887_PORT2_INACTIVE;
1933 
1934 		tda9887_cfg.tuner = TUNER_TDA9887;
1935 		tda9887_cfg.priv  = &on;
1936 
1937 		/* otherwise we don't detect the tuner on next insmod */
1938 		saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1939 	} else if (dev->board == SAA7134_BOARD_MEDION_MD8800_QUADRO) {
1940 		if ((dev->eedata[2] == 0x07) && use_frontend) {
1941 			/* turn off the 2nd lnb supply */
1942 			u8 data = 0x80;
1943 			struct i2c_msg msg = {.addr = 0x08, .buf = &data, .flags = 0, .len = 1};
1944 			struct dvb_frontend *fe;
1945 			fe = fe0->dvb.frontend;
1946 			if (fe->ops.i2c_gate_ctrl) {
1947 				fe->ops.i2c_gate_ctrl(fe, 1);
1948 				i2c_transfer(&dev->i2c_adap, &msg, 1);
1949 				fe->ops.i2c_gate_ctrl(fe, 0);
1950 			}
1951 		}
1952 	}
1953 	vb2_dvb_unregister_bus(&dev->frontends);
1954 	vb2_queue_release(&fe0->dvb.dvbq);
1955 	return 0;
1956 }
1957 
1958 static struct saa7134_mpeg_ops dvb_ops = {
1959 	.type          = SAA7134_MPEG_DVB,
1960 	.init          = dvb_init,
1961 	.fini          = dvb_fini,
1962 };
1963 
1964 static int __init dvb_register(void)
1965 {
1966 	return saa7134_ts_register(&dvb_ops);
1967 }
1968 
1969 static void __exit dvb_unregister(void)
1970 {
1971 	saa7134_ts_unregister(&dvb_ops);
1972 }
1973 
1974 module_init(dvb_register);
1975 module_exit(dvb_unregister);
1976