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