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