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 
pinnacle_antenna_pwr(struct saa7134_dev * dev,int on)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 
mt352_pinnacle_init(struct dvb_frontend * fe)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 
mt352_aver777_init(struct dvb_frontend * fe)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 
mt352_avermedia_xc3028_init(struct dvb_frontend * fe)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 
mt352_pinnacle_tuner_set_params(struct dvb_frontend * fe)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 
kworld_sbtvd_gate_ctrl(struct dvb_frontend * fe,int enable)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 
philips_tda1004x_request_firmware(struct dvb_frontend * fe,const struct firmware ** fw,char * name)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 
philips_tda6651_pll_set(struct dvb_frontend * fe)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 
philips_tu1216_init(struct dvb_frontend * fe)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 
philips_td1316_tuner_init(struct dvb_frontend * fe)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 
philips_td1316_tuner_set_params(struct dvb_frontend * fe)417 static int philips_td1316_tuner_set_params(struct dvb_frontend *fe)
418 {
419 	return philips_tda6651_pll_set(fe);
420 }
421 
philips_td1316_tuner_sleep(struct dvb_frontend * fe)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 
philips_europa_tuner_init(struct dvb_frontend * fe)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 
philips_europa_tuner_sleep(struct dvb_frontend * fe)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 	if (i2c_transfer(&dev->i2c_adap, &analog_msg, 1) != 1)
470 		return -EIO;
471 
472 	return 0;
473 }
474 
philips_europa_demod_sleep(struct dvb_frontend * fe)475 static int philips_europa_demod_sleep(struct dvb_frontend *fe)
476 {
477 	struct saa7134_dev *dev = fe->dvb->priv;
478 
479 	if (dev->original_demod_sleep)
480 		dev->original_demod_sleep(fe);
481 	fe->ops.i2c_gate_ctrl(fe, 1);
482 	return 0;
483 }
484 
485 static struct tda1004x_config philips_europa_config = {
486 
487 	.demod_address = 0x8,
488 	.invert        = 0,
489 	.invert_oclk   = 0,
490 	.xtal_freq     = TDA10046_XTAL_4M,
491 	.agc_config    = TDA10046_AGC_IFO_AUTO_POS,
492 	.if_freq       = TDA10046_FREQ_052,
493 	.tuner_address = 0x61,
494 	.request_firmware = philips_tda1004x_request_firmware
495 };
496 
497 static struct tda1004x_config medion_cardbus = {
498 	.demod_address = 0x08,
499 	.invert        = 1,
500 	.invert_oclk   = 0,
501 	.xtal_freq     = TDA10046_XTAL_16M,
502 	.agc_config    = TDA10046_AGC_IFO_AUTO_NEG,
503 	.if_freq       = TDA10046_FREQ_3613,
504 	.tuner_address = 0x61,
505 	.request_firmware = philips_tda1004x_request_firmware
506 };
507 
508 static struct tda1004x_config technotrend_budget_t3000_config = {
509 	.demod_address = 0x8,
510 	.invert        = 1,
511 	.invert_oclk   = 0,
512 	.xtal_freq     = TDA10046_XTAL_4M,
513 	.agc_config    = TDA10046_AGC_DEFAULT,
514 	.if_freq       = TDA10046_FREQ_3617,
515 	.tuner_address = 0x63,
516 	.request_firmware = philips_tda1004x_request_firmware
517 };
518 
519 /* ------------------------------------------------------------------
520  * tda 1004x based cards with philips silicon tuner
521  */
522 
tda8290_i2c_gate_ctrl(struct dvb_frontend * fe,int enable)523 static int tda8290_i2c_gate_ctrl( struct dvb_frontend* fe, int enable)
524 {
525 	struct tda1004x_state *state = fe->demodulator_priv;
526 
527 	u8 addr = state->config->i2c_gate;
528 	static u8 tda8290_close[] = { 0x21, 0xc0};
529 	static u8 tda8290_open[]  = { 0x21, 0x80};
530 	struct i2c_msg tda8290_msg = {.addr = addr,.flags = 0, .len = 2};
531 	if (enable) {
532 		tda8290_msg.buf = tda8290_close;
533 	} else {
534 		tda8290_msg.buf = tda8290_open;
535 	}
536 	if (i2c_transfer(state->i2c, &tda8290_msg, 1) != 1) {
537 		pr_warn("could not access tda8290 I2C gate\n");
538 		return -EIO;
539 	}
540 	msleep(20);
541 	return 0;
542 }
543 
philips_tda827x_tuner_init(struct dvb_frontend * fe)544 static int philips_tda827x_tuner_init(struct dvb_frontend *fe)
545 {
546 	struct saa7134_dev *dev = fe->dvb->priv;
547 	struct tda1004x_state *state = fe->demodulator_priv;
548 
549 	switch (state->config->antenna_switch) {
550 	case 0:
551 		break;
552 	case 1:
553 		pr_debug("setting GPIO21 to 0 (TV antenna?)\n");
554 		saa7134_set_gpio(dev, 21, 0);
555 		break;
556 	case 2:
557 		pr_debug("setting GPIO21 to 1 (Radio antenna?)\n");
558 		saa7134_set_gpio(dev, 21, 1);
559 		break;
560 	}
561 	return 0;
562 }
563 
philips_tda827x_tuner_sleep(struct dvb_frontend * fe)564 static int philips_tda827x_tuner_sleep(struct dvb_frontend *fe)
565 {
566 	struct saa7134_dev *dev = fe->dvb->priv;
567 	struct tda1004x_state *state = fe->demodulator_priv;
568 
569 	switch (state->config->antenna_switch) {
570 	case 0:
571 		break;
572 	case 1:
573 		pr_debug("setting GPIO21 to 1 (Radio antenna?)\n");
574 		saa7134_set_gpio(dev, 21, 1);
575 		break;
576 	case 2:
577 		pr_debug("setting GPIO21 to 0 (TV antenna?)\n");
578 		saa7134_set_gpio(dev, 21, 0);
579 		break;
580 	}
581 	return 0;
582 }
583 
configure_tda827x_fe(struct saa7134_dev * dev,struct tda1004x_config * cdec_conf,struct tda827x_config * tuner_conf)584 static int configure_tda827x_fe(struct saa7134_dev *dev,
585 				struct tda1004x_config *cdec_conf,
586 				struct tda827x_config *tuner_conf)
587 {
588 	struct vb2_dvb_frontend *fe0;
589 
590 	/* Get the first frontend */
591 	fe0 = vb2_dvb_get_frontend(&dev->frontends, 1);
592 
593 	if (!fe0)
594 		return -EINVAL;
595 
596 	fe0->dvb.frontend = dvb_attach(tda10046_attach, cdec_conf, &dev->i2c_adap);
597 	if (fe0->dvb.frontend) {
598 		if (cdec_conf->i2c_gate)
599 			fe0->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
600 		if (dvb_attach(tda827x_attach, fe0->dvb.frontend,
601 			       cdec_conf->tuner_address,
602 			       &dev->i2c_adap, tuner_conf))
603 			return 0;
604 
605 		pr_warn("no tda827x tuner found at addr: %02x\n",
606 				cdec_conf->tuner_address);
607 	}
608 	return -EINVAL;
609 }
610 
611 /* ------------------------------------------------------------------ */
612 
613 static struct tda827x_config tda827x_cfg_0 = {
614 	.init = philips_tda827x_tuner_init,
615 	.sleep = philips_tda827x_tuner_sleep,
616 	.config = 0,
617 	.switch_addr = 0
618 };
619 
620 static struct tda827x_config tda827x_cfg_1 = {
621 	.init = philips_tda827x_tuner_init,
622 	.sleep = philips_tda827x_tuner_sleep,
623 	.config = 1,
624 	.switch_addr = 0x4b
625 };
626 
627 static struct tda827x_config tda827x_cfg_2 = {
628 	.init = philips_tda827x_tuner_init,
629 	.sleep = philips_tda827x_tuner_sleep,
630 	.config = 2,
631 	.switch_addr = 0x4b
632 };
633 
634 static struct tda827x_config tda827x_cfg_2_sw42 = {
635 	.init = philips_tda827x_tuner_init,
636 	.sleep = philips_tda827x_tuner_sleep,
637 	.config = 2,
638 	.switch_addr = 0x42
639 };
640 
641 /* ------------------------------------------------------------------ */
642 
643 static struct tda1004x_config tda827x_lifeview_config = {
644 	.demod_address = 0x08,
645 	.invert        = 1,
646 	.invert_oclk   = 0,
647 	.xtal_freq     = TDA10046_XTAL_16M,
648 	.agc_config    = TDA10046_AGC_TDA827X,
649 	.gpio_config   = TDA10046_GP11_I,
650 	.if_freq       = TDA10046_FREQ_045,
651 	.tuner_address = 0x60,
652 	.request_firmware = philips_tda1004x_request_firmware
653 };
654 
655 static struct tda1004x_config philips_tiger_config = {
656 	.demod_address = 0x08,
657 	.invert        = 1,
658 	.invert_oclk   = 0,
659 	.xtal_freq     = TDA10046_XTAL_16M,
660 	.agc_config    = TDA10046_AGC_TDA827X,
661 	.gpio_config   = TDA10046_GP11_I,
662 	.if_freq       = TDA10046_FREQ_045,
663 	.i2c_gate      = 0x4b,
664 	.tuner_address = 0x61,
665 	.antenna_switch= 1,
666 	.request_firmware = philips_tda1004x_request_firmware
667 };
668 
669 static struct tda1004x_config cinergy_ht_config = {
670 	.demod_address = 0x08,
671 	.invert        = 1,
672 	.invert_oclk   = 0,
673 	.xtal_freq     = TDA10046_XTAL_16M,
674 	.agc_config    = TDA10046_AGC_TDA827X,
675 	.gpio_config   = TDA10046_GP01_I,
676 	.if_freq       = TDA10046_FREQ_045,
677 	.i2c_gate      = 0x4b,
678 	.tuner_address = 0x61,
679 	.request_firmware = philips_tda1004x_request_firmware
680 };
681 
682 static struct tda1004x_config cinergy_ht_pci_config = {
683 	.demod_address = 0x08,
684 	.invert        = 1,
685 	.invert_oclk   = 0,
686 	.xtal_freq     = TDA10046_XTAL_16M,
687 	.agc_config    = TDA10046_AGC_TDA827X,
688 	.gpio_config   = TDA10046_GP01_I,
689 	.if_freq       = TDA10046_FREQ_045,
690 	.i2c_gate      = 0x4b,
691 	.tuner_address = 0x60,
692 	.request_firmware = philips_tda1004x_request_firmware
693 };
694 
695 static struct tda1004x_config philips_tiger_s_config = {
696 	.demod_address = 0x08,
697 	.invert        = 1,
698 	.invert_oclk   = 0,
699 	.xtal_freq     = TDA10046_XTAL_16M,
700 	.agc_config    = TDA10046_AGC_TDA827X,
701 	.gpio_config   = TDA10046_GP01_I,
702 	.if_freq       = TDA10046_FREQ_045,
703 	.i2c_gate      = 0x4b,
704 	.tuner_address = 0x61,
705 	.antenna_switch= 1,
706 	.request_firmware = philips_tda1004x_request_firmware
707 };
708 
709 static struct tda1004x_config pinnacle_pctv_310i_config = {
710 	.demod_address = 0x08,
711 	.invert        = 1,
712 	.invert_oclk   = 0,
713 	.xtal_freq     = TDA10046_XTAL_16M,
714 	.agc_config    = TDA10046_AGC_TDA827X,
715 	.gpio_config   = TDA10046_GP11_I,
716 	.if_freq       = TDA10046_FREQ_045,
717 	.i2c_gate      = 0x4b,
718 	.tuner_address = 0x61,
719 	.request_firmware = philips_tda1004x_request_firmware
720 };
721 
722 static struct tda1004x_config hauppauge_hvr_1110_config = {
723 	.demod_address = 0x08,
724 	.invert        = 1,
725 	.invert_oclk   = 0,
726 	.xtal_freq     = TDA10046_XTAL_16M,
727 	.agc_config    = TDA10046_AGC_TDA827X,
728 	.gpio_config   = TDA10046_GP11_I,
729 	.if_freq       = TDA10046_FREQ_045,
730 	.i2c_gate      = 0x4b,
731 	.tuner_address = 0x61,
732 	.request_firmware = philips_tda1004x_request_firmware
733 };
734 
735 static struct tda1004x_config asus_p7131_dual_config = {
736 	.demod_address = 0x08,
737 	.invert        = 1,
738 	.invert_oclk   = 0,
739 	.xtal_freq     = TDA10046_XTAL_16M,
740 	.agc_config    = TDA10046_AGC_TDA827X,
741 	.gpio_config   = TDA10046_GP11_I,
742 	.if_freq       = TDA10046_FREQ_045,
743 	.i2c_gate      = 0x4b,
744 	.tuner_address = 0x61,
745 	.antenna_switch= 2,
746 	.request_firmware = philips_tda1004x_request_firmware
747 };
748 
749 static struct tda1004x_config lifeview_trio_config = {
750 	.demod_address = 0x09,
751 	.invert        = 1,
752 	.invert_oclk   = 0,
753 	.xtal_freq     = TDA10046_XTAL_16M,
754 	.agc_config    = TDA10046_AGC_TDA827X,
755 	.gpio_config   = TDA10046_GP00_I,
756 	.if_freq       = TDA10046_FREQ_045,
757 	.tuner_address = 0x60,
758 	.request_firmware = philips_tda1004x_request_firmware
759 };
760 
761 static struct tda1004x_config tevion_dvbt220rf_config = {
762 	.demod_address = 0x08,
763 	.invert        = 1,
764 	.invert_oclk   = 0,
765 	.xtal_freq     = TDA10046_XTAL_16M,
766 	.agc_config    = TDA10046_AGC_TDA827X,
767 	.gpio_config   = TDA10046_GP11_I,
768 	.if_freq       = TDA10046_FREQ_045,
769 	.tuner_address = 0x60,
770 	.request_firmware = philips_tda1004x_request_firmware
771 };
772 
773 static struct tda1004x_config md8800_dvbt_config = {
774 	.demod_address = 0x08,
775 	.invert        = 1,
776 	.invert_oclk   = 0,
777 	.xtal_freq     = TDA10046_XTAL_16M,
778 	.agc_config    = TDA10046_AGC_TDA827X,
779 	.gpio_config   = TDA10046_GP01_I,
780 	.if_freq       = TDA10046_FREQ_045,
781 	.i2c_gate      = 0x4b,
782 	.tuner_address = 0x60,
783 	.request_firmware = philips_tda1004x_request_firmware
784 };
785 
786 static struct tda1004x_config asus_p7131_4871_config = {
787 	.demod_address = 0x08,
788 	.invert        = 1,
789 	.invert_oclk   = 0,
790 	.xtal_freq     = TDA10046_XTAL_16M,
791 	.agc_config    = TDA10046_AGC_TDA827X,
792 	.gpio_config   = TDA10046_GP01_I,
793 	.if_freq       = TDA10046_FREQ_045,
794 	.i2c_gate      = 0x4b,
795 	.tuner_address = 0x61,
796 	.antenna_switch= 2,
797 	.request_firmware = philips_tda1004x_request_firmware
798 };
799 
800 static struct tda1004x_config asus_p7131_hybrid_lna_config = {
801 	.demod_address = 0x08,
802 	.invert        = 1,
803 	.invert_oclk   = 0,
804 	.xtal_freq     = TDA10046_XTAL_16M,
805 	.agc_config    = TDA10046_AGC_TDA827X,
806 	.gpio_config   = TDA10046_GP11_I,
807 	.if_freq       = TDA10046_FREQ_045,
808 	.i2c_gate      = 0x4b,
809 	.tuner_address = 0x61,
810 	.antenna_switch= 2,
811 	.request_firmware = philips_tda1004x_request_firmware
812 };
813 
814 static struct tda1004x_config kworld_dvb_t_210_config = {
815 	.demod_address = 0x08,
816 	.invert        = 1,
817 	.invert_oclk   = 0,
818 	.xtal_freq     = TDA10046_XTAL_16M,
819 	.agc_config    = TDA10046_AGC_TDA827X,
820 	.gpio_config   = TDA10046_GP11_I,
821 	.if_freq       = TDA10046_FREQ_045,
822 	.i2c_gate      = 0x4b,
823 	.tuner_address = 0x61,
824 	.antenna_switch= 1,
825 	.request_firmware = philips_tda1004x_request_firmware
826 };
827 
828 static struct tda1004x_config avermedia_super_007_config = {
829 	.demod_address = 0x08,
830 	.invert        = 1,
831 	.invert_oclk   = 0,
832 	.xtal_freq     = TDA10046_XTAL_16M,
833 	.agc_config    = TDA10046_AGC_TDA827X,
834 	.gpio_config   = TDA10046_GP01_I,
835 	.if_freq       = TDA10046_FREQ_045,
836 	.i2c_gate      = 0x4b,
837 	.tuner_address = 0x60,
838 	.antenna_switch= 1,
839 	.request_firmware = philips_tda1004x_request_firmware
840 };
841 
842 static struct tda1004x_config twinhan_dtv_dvb_3056_config = {
843 	.demod_address = 0x08,
844 	.invert        = 1,
845 	.invert_oclk   = 0,
846 	.xtal_freq     = TDA10046_XTAL_16M,
847 	.agc_config    = TDA10046_AGC_TDA827X,
848 	.gpio_config   = TDA10046_GP01_I,
849 	.if_freq       = TDA10046_FREQ_045,
850 	.i2c_gate      = 0x42,
851 	.tuner_address = 0x61,
852 	.antenna_switch = 1,
853 	.request_firmware = philips_tda1004x_request_firmware
854 };
855 
856 static struct tda1004x_config asus_tiger_3in1_config = {
857 	.demod_address = 0x0b,
858 	.invert        = 1,
859 	.invert_oclk   = 0,
860 	.xtal_freq     = TDA10046_XTAL_16M,
861 	.agc_config    = TDA10046_AGC_TDA827X,
862 	.gpio_config   = TDA10046_GP11_I,
863 	.if_freq       = TDA10046_FREQ_045,
864 	.i2c_gate      = 0x4b,
865 	.tuner_address = 0x61,
866 	.antenna_switch = 1,
867 	.request_firmware = philips_tda1004x_request_firmware
868 };
869 
870 static struct tda1004x_config asus_ps3_100_config = {
871 	.demod_address = 0x0b,
872 	.invert        = 1,
873 	.invert_oclk   = 0,
874 	.xtal_freq     = TDA10046_XTAL_16M,
875 	.agc_config    = TDA10046_AGC_TDA827X,
876 	.gpio_config   = TDA10046_GP11_I,
877 	.if_freq       = TDA10046_FREQ_045,
878 	.i2c_gate      = 0x4b,
879 	.tuner_address = 0x61,
880 	.antenna_switch = 1,
881 	.request_firmware = philips_tda1004x_request_firmware
882 };
883 
884 /* ------------------------------------------------------------------
885  * special case: this card uses saa713x GPIO22 for the mode switch
886  */
887 
ads_duo_tuner_init(struct dvb_frontend * fe)888 static int ads_duo_tuner_init(struct dvb_frontend *fe)
889 {
890 	struct saa7134_dev *dev = fe->dvb->priv;
891 	philips_tda827x_tuner_init(fe);
892 	/* route TDA8275a AGC input to the channel decoder */
893 	saa7134_set_gpio(dev, 22, 1);
894 	return 0;
895 }
896 
ads_duo_tuner_sleep(struct dvb_frontend * fe)897 static int ads_duo_tuner_sleep(struct dvb_frontend *fe)
898 {
899 	struct saa7134_dev *dev = fe->dvb->priv;
900 	/* route TDA8275a AGC input to the analog IF chip*/
901 	saa7134_set_gpio(dev, 22, 0);
902 	philips_tda827x_tuner_sleep(fe);
903 	return 0;
904 }
905 
906 static struct tda827x_config ads_duo_cfg = {
907 	.init = ads_duo_tuner_init,
908 	.sleep = ads_duo_tuner_sleep,
909 	.config = 0
910 };
911 
912 static struct tda1004x_config ads_tech_duo_config = {
913 	.demod_address = 0x08,
914 	.invert        = 1,
915 	.invert_oclk   = 0,
916 	.xtal_freq     = TDA10046_XTAL_16M,
917 	.agc_config    = TDA10046_AGC_TDA827X,
918 	.gpio_config   = TDA10046_GP00_I,
919 	.if_freq       = TDA10046_FREQ_045,
920 	.tuner_address = 0x61,
921 	.request_firmware = philips_tda1004x_request_firmware
922 };
923 
924 static struct zl10353_config behold_h6_config = {
925 	.demod_address = 0x1e>>1,
926 	.no_tuner      = 1,
927 	.parallel_ts   = 1,
928 	.disable_i2c_gate_ctrl = 1,
929 };
930 
931 static struct xc5000_config behold_x7_tunerconfig = {
932 	.i2c_address      = 0xc2>>1,
933 	.if_khz           = 4560,
934 	.radio_input      = XC5000_RADIO_FM1,
935 };
936 
937 static struct zl10353_config behold_x7_config = {
938 	.demod_address = 0x1e>>1,
939 	.if2           = 45600,
940 	.no_tuner      = 1,
941 	.parallel_ts   = 1,
942 	.disable_i2c_gate_ctrl = 1,
943 };
944 
945 static struct zl10353_config videomate_t750_zl10353_config = {
946 	.demod_address         = 0x0f,
947 	.no_tuner              = 1,
948 	.parallel_ts           = 1,
949 	.disable_i2c_gate_ctrl = 1,
950 };
951 
952 static struct qt1010_config videomate_t750_qt1010_config = {
953 	.i2c_address = 0x62
954 };
955 
956 
957 /* ==================================================================
958  * tda10086 based DVB-S cards, helper functions
959  */
960 
961 static struct tda10086_config flydvbs = {
962 	.demod_address = 0x0e,
963 	.invert = 0,
964 	.diseqc_tone = 0,
965 	.xtal_freq = TDA10086_XTAL_16M,
966 };
967 
968 static struct tda10086_config sd1878_4m = {
969 	.demod_address = 0x0e,
970 	.invert = 0,
971 	.diseqc_tone = 0,
972 	.xtal_freq = TDA10086_XTAL_4M,
973 };
974 
975 /* ------------------------------------------------------------------
976  * special case: lnb supply is connected to the gated i2c
977  */
978 
md8800_set_voltage(struct dvb_frontend * fe,enum fe_sec_voltage voltage)979 static int md8800_set_voltage(struct dvb_frontend *fe,
980 			      enum fe_sec_voltage voltage)
981 {
982 	int res = -EIO;
983 	struct saa7134_dev *dev = fe->dvb->priv;
984 	if (fe->ops.i2c_gate_ctrl) {
985 		fe->ops.i2c_gate_ctrl(fe, 1);
986 		if (dev->original_set_voltage)
987 			res = dev->original_set_voltage(fe, voltage);
988 		fe->ops.i2c_gate_ctrl(fe, 0);
989 	}
990 	return res;
991 };
992 
md8800_set_high_voltage(struct dvb_frontend * fe,long arg)993 static int md8800_set_high_voltage(struct dvb_frontend *fe, long arg)
994 {
995 	int res = -EIO;
996 	struct saa7134_dev *dev = fe->dvb->priv;
997 	if (fe->ops.i2c_gate_ctrl) {
998 		fe->ops.i2c_gate_ctrl(fe, 1);
999 		if (dev->original_set_high_voltage)
1000 			res = dev->original_set_high_voltage(fe, arg);
1001 		fe->ops.i2c_gate_ctrl(fe, 0);
1002 	}
1003 	return res;
1004 };
1005 
md8800_set_voltage2(struct dvb_frontend * fe,enum fe_sec_voltage voltage)1006 static int md8800_set_voltage2(struct dvb_frontend *fe,
1007 			       enum fe_sec_voltage voltage)
1008 {
1009 	struct saa7134_dev *dev = fe->dvb->priv;
1010 	u8 wbuf[2] = { 0x1f, 00 };
1011 	u8 rbuf;
1012 	struct i2c_msg msg[] = { { .addr = 0x08, .flags = 0, .buf = wbuf, .len = 1 },
1013 				 { .addr = 0x08, .flags = I2C_M_RD, .buf = &rbuf, .len = 1 } };
1014 
1015 	if (i2c_transfer(&dev->i2c_adap, msg, 2) != 2)
1016 		return -EIO;
1017 	/* NOTE: this assumes that gpo1 is used, it might be bit 5 (gpo2) */
1018 	if (voltage == SEC_VOLTAGE_18)
1019 		wbuf[1] = rbuf | 0x10;
1020 	else
1021 		wbuf[1] = rbuf & 0xef;
1022 	msg[0].len = 2;
1023 	if (i2c_transfer(&dev->i2c_adap, msg, 1) != 1)
1024 		return -EIO;
1025 
1026 	return 0;
1027 }
1028 
md8800_set_high_voltage2(struct dvb_frontend * fe,long arg)1029 static int md8800_set_high_voltage2(struct dvb_frontend *fe, long arg)
1030 {
1031 	pr_warn("%s: sorry can't set high LNB supply voltage from here\n",
1032 		__func__);
1033 	return -EIO;
1034 }
1035 
1036 /* ==================================================================
1037  * nxt200x based ATSC cards, helper functions
1038  */
1039 
1040 static const struct nxt200x_config avertvhda180 = {
1041 	.demod_address    = 0x0a,
1042 };
1043 
1044 static const struct nxt200x_config kworldatsc110 = {
1045 	.demod_address    = 0x0a,
1046 };
1047 
1048 /* ------------------------------------------------------------------ */
1049 
1050 static struct mt312_config avertv_a700_mt312 = {
1051 	.demod_address = 0x0e,
1052 	.voltage_inverted = 1,
1053 };
1054 
1055 static struct zl10036_config avertv_a700_tuner = {
1056 	.tuner_address = 0x60,
1057 };
1058 
1059 static struct mt312_config zl10313_compro_s350_config = {
1060 	.demod_address = 0x0e,
1061 };
1062 
1063 static struct mt312_config zl10313_avermedia_a706_config = {
1064 	.demod_address = 0x0e,
1065 };
1066 
1067 static struct lgdt3305_config hcw_lgdt3305_config = {
1068 	.i2c_addr           = 0x0e,
1069 	.mpeg_mode          = LGDT3305_MPEG_SERIAL,
1070 	.tpclk_edge         = LGDT3305_TPCLK_RISING_EDGE,
1071 	.tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
1072 	.deny_i2c_rptr      = 1,
1073 	.spectral_inversion = 1,
1074 	.qam_if_khz         = 4000,
1075 	.vsb_if_khz         = 3250,
1076 };
1077 
1078 static struct tda10048_config hcw_tda10048_config = {
1079 	.demod_address    = 0x10 >> 1,
1080 	.output_mode      = TDA10048_SERIAL_OUTPUT,
1081 	.fwbulkwritelen   = TDA10048_BULKWRITE_200,
1082 	.inversion        = TDA10048_INVERSION_ON,
1083 	.dtv6_if_freq_khz = TDA10048_IF_3300,
1084 	.dtv7_if_freq_khz = TDA10048_IF_3500,
1085 	.dtv8_if_freq_khz = TDA10048_IF_4000,
1086 	.clk_freq_khz     = TDA10048_CLK_16000,
1087 	.disable_gate_access = 1,
1088 };
1089 
1090 static struct tda18271_std_map hauppauge_tda18271_std_map = {
1091 	.atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 4,
1092 		      .if_lvl = 1, .rfagc_top = 0x58, },
1093 	.qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 5,
1094 		      .if_lvl = 1, .rfagc_top = 0x58, },
1095 };
1096 
1097 static struct tda18271_config hcw_tda18271_config = {
1098 	.std_map = &hauppauge_tda18271_std_map,
1099 	.gate    = TDA18271_GATE_ANALOG,
1100 	.config  = 3,
1101 	.output_opt = TDA18271_OUTPUT_LT_OFF,
1102 };
1103 
1104 static struct tda829x_config tda829x_no_probe = {
1105 	.probe_tuner = TDA829X_DONT_PROBE,
1106 };
1107 
1108 static struct tda10048_config zolid_tda10048_config = {
1109 	.demod_address    = 0x10 >> 1,
1110 	.output_mode      = TDA10048_PARALLEL_OUTPUT,
1111 	.fwbulkwritelen   = TDA10048_BULKWRITE_200,
1112 	.inversion        = TDA10048_INVERSION_ON,
1113 	.dtv6_if_freq_khz = TDA10048_IF_3300,
1114 	.dtv7_if_freq_khz = TDA10048_IF_3500,
1115 	.dtv8_if_freq_khz = TDA10048_IF_4000,
1116 	.clk_freq_khz     = TDA10048_CLK_16000,
1117 	.disable_gate_access = 1,
1118 };
1119 
1120 static struct tda18271_config zolid_tda18271_config = {
1121 	.gate    = TDA18271_GATE_ANALOG,
1122 };
1123 
1124 static struct tda10048_config dtv1000s_tda10048_config = {
1125 	.demod_address    = 0x10 >> 1,
1126 	.output_mode      = TDA10048_PARALLEL_OUTPUT,
1127 	.fwbulkwritelen   = TDA10048_BULKWRITE_200,
1128 	.inversion        = TDA10048_INVERSION_ON,
1129 	.dtv6_if_freq_khz = TDA10048_IF_3300,
1130 	.dtv7_if_freq_khz = TDA10048_IF_3800,
1131 	.dtv8_if_freq_khz = TDA10048_IF_4300,
1132 	.clk_freq_khz     = TDA10048_CLK_16000,
1133 	.disable_gate_access = 1,
1134 };
1135 
1136 static struct tda18271_std_map dtv1000s_tda18271_std_map = {
1137 	.dvbt_6   = { .if_freq = 3300, .agc_mode = 3, .std = 4,
1138 		      .if_lvl = 1, .rfagc_top = 0x37, },
1139 	.dvbt_7   = { .if_freq = 3800, .agc_mode = 3, .std = 5,
1140 		      .if_lvl = 1, .rfagc_top = 0x37, },
1141 	.dvbt_8   = { .if_freq = 4300, .agc_mode = 3, .std = 6,
1142 		      .if_lvl = 1, .rfagc_top = 0x37, },
1143 };
1144 
1145 static struct tda18271_config dtv1000s_tda18271_config = {
1146 	.std_map = &dtv1000s_tda18271_std_map,
1147 	.gate    = TDA18271_GATE_ANALOG,
1148 };
1149 
1150 static struct lgs8gxx_config prohdtv_pro2_lgs8g75_config = {
1151 	.prod = LGS8GXX_PROD_LGS8G75,
1152 	.demod_address = 0x1d,
1153 	.serial_ts = 0,
1154 	.ts_clk_pol = 1,
1155 	.ts_clk_gated = 0,
1156 	.if_clk_freq = 30400, /* 30.4 MHz */
1157 	.if_freq = 4000, /* 4.00 MHz */
1158 	.if_neg_center = 0,
1159 	.ext_adc = 0,
1160 	.adc_signed = 1,
1161 	.adc_vpp = 3, /* 2.0 Vpp */
1162 	.if_neg_edge = 1,
1163 };
1164 
1165 static struct tda18271_config prohdtv_pro2_tda18271_config = {
1166 	.gate = TDA18271_GATE_ANALOG,
1167 	.output_opt = TDA18271_OUTPUT_LT_OFF,
1168 };
1169 
1170 static struct tda18271_std_map kworld_tda18271_std_map = {
1171 	.atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 3,
1172 		      .if_lvl = 6, .rfagc_top = 0x37 },
1173 	.qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 0,
1174 		      .if_lvl = 6, .rfagc_top = 0x37 },
1175 };
1176 
1177 static struct tda18271_config kworld_pc150u_tda18271_config = {
1178 	.std_map = &kworld_tda18271_std_map,
1179 	.gate    = TDA18271_GATE_ANALOG,
1180 	.output_opt = TDA18271_OUTPUT_LT_OFF,
1181 	.config  = 3,	/* Use tuner callback for AGC */
1182 	.rf_cal_on_startup = 1
1183 };
1184 
1185 static struct s5h1411_config kworld_s5h1411_config = {
1186 	.output_mode   = S5H1411_PARALLEL_OUTPUT,
1187 	.gpio          = S5H1411_GPIO_OFF,
1188 	.qam_if        = S5H1411_IF_4000,
1189 	.vsb_if        = S5H1411_IF_3250,
1190 	.inversion     = S5H1411_INVERSION_ON,
1191 	.status_mode   = S5H1411_DEMODLOCKING,
1192 	.mpeg_timing   =
1193 		S5H1411_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK,
1194 };
1195 
1196 static struct tda18271_config hdtv200h_tda18271_config = {
1197 	.gate    = TDA18271_GATE_ANALOG,
1198 	.config  = 3	/* Use tuner callback for AGC */
1199 };
1200 
1201 static struct s5h1411_config hdtv200h_s5h1411_config = {
1202 	.output_mode   = S5H1411_PARALLEL_OUTPUT,
1203 	.gpio          = S5H1411_GPIO_OFF,
1204 	.qam_if        = S5H1411_IF_4000,
1205 	.vsb_if        = S5H1411_IF_3250,
1206 	.inversion     = S5H1411_INVERSION_ON,
1207 	.status_mode   = S5H1411_DEMODLOCKING,
1208 	.mpeg_timing   =
1209 		S5H1411_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK,
1210 };
1211 
1212 
1213 /* ==================================================================
1214  * Core code
1215  */
1216 
dvb_init(struct saa7134_dev * dev)1217 static int dvb_init(struct saa7134_dev *dev)
1218 {
1219 	int ret;
1220 	int attach_xc3028 = 0;
1221 	struct vb2_dvb_frontend *fe0;
1222 	struct vb2_queue *q;
1223 
1224 	/* FIXME: add support for multi-frontend */
1225 	mutex_init(&dev->frontends.lock);
1226 	INIT_LIST_HEAD(&dev->frontends.felist);
1227 
1228 	pr_info("%s() allocating 1 frontend\n", __func__);
1229 	fe0 = vb2_dvb_alloc_frontend(&dev->frontends, 1);
1230 	if (!fe0) {
1231 		pr_err("%s() failed to alloc\n", __func__);
1232 		return -ENOMEM;
1233 	}
1234 
1235 	/* init struct vb2_dvb */
1236 	dev->ts.nr_bufs    = 32;
1237 	dev->ts.nr_packets = 32*4;
1238 	fe0->dvb.name = dev->name;
1239 	q = &fe0->dvb.dvbq;
1240 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1241 	q->io_modes = VB2_MMAP | VB2_READ;
1242 	q->drv_priv = &dev->ts_q;
1243 	q->ops = &saa7134_ts_qops;
1244 	q->mem_ops = &vb2_dma_sg_memops;
1245 	q->buf_struct_size = sizeof(struct saa7134_buf);
1246 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1247 	q->lock = &dev->lock;
1248 	q->dev = &dev->pci->dev;
1249 	ret = vb2_queue_init(q);
1250 	if (ret) {
1251 		vb2_dvb_dealloc_frontends(&dev->frontends);
1252 		return ret;
1253 	}
1254 
1255 	switch (dev->board) {
1256 	case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
1257 		pr_debug("pinnacle 300i dvb setup\n");
1258 		fe0->dvb.frontend = dvb_attach(mt352_attach, &pinnacle_300i,
1259 					       &dev->i2c_adap);
1260 		if (fe0->dvb.frontend) {
1261 			fe0->dvb.frontend->ops.tuner_ops.set_params = mt352_pinnacle_tuner_set_params;
1262 		}
1263 		break;
1264 	case SAA7134_BOARD_AVERMEDIA_777:
1265 	case SAA7134_BOARD_AVERMEDIA_A16AR:
1266 		pr_debug("avertv 777 dvb setup\n");
1267 		fe0->dvb.frontend = dvb_attach(mt352_attach, &avermedia_777,
1268 					       &dev->i2c_adap);
1269 		if (fe0->dvb.frontend) {
1270 			dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1271 				   &dev->i2c_adap, 0x61,
1272 				   TUNER_PHILIPS_TD1316);
1273 		}
1274 		break;
1275 	case SAA7134_BOARD_AVERMEDIA_A16D:
1276 		pr_debug("AverMedia A16D dvb setup\n");
1277 		fe0->dvb.frontend = dvb_attach(mt352_attach,
1278 						&avermedia_xc3028_mt352_dev,
1279 						&dev->i2c_adap);
1280 		attach_xc3028 = 1;
1281 		break;
1282 	case SAA7134_BOARD_MD7134:
1283 		fe0->dvb.frontend = dvb_attach(tda10046_attach,
1284 					       &medion_cardbus,
1285 					       &dev->i2c_adap);
1286 		if (fe0->dvb.frontend) {
1287 			/*
1288 			 * The TV tuner on this board is actually NOT
1289 			 * behind the demod i2c gate.
1290 			 * However, the demod EEPROM is indeed there and it
1291 			 * conflicts with the SAA7134 chip config EEPROM
1292 			 * if the i2c gate is open (since they have same
1293 			 * bus addresses) resulting in card PCI SVID / SSID
1294 			 * being garbage after a reboot from time to time.
1295 			 *
1296 			 * Let's just leave the gate permanently closed -
1297 			 * saa7134_i2c_eeprom_md7134_gate() will close it for
1298 			 * us at probe time if it was open for some reason.
1299 			 */
1300 			fe0->dvb.frontend->ops.i2c_gate_ctrl = NULL;
1301 			dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1302 				   &dev->i2c_adap, medion_cardbus.tuner_address,
1303 				   TUNER_PHILIPS_FMD1216ME_MK3);
1304 		}
1305 		break;
1306 	case SAA7134_BOARD_PHILIPS_TOUGH:
1307 		fe0->dvb.frontend = dvb_attach(tda10046_attach,
1308 					       &philips_tu1216_60_config,
1309 					       &dev->i2c_adap);
1310 		if (fe0->dvb.frontend) {
1311 			fe0->dvb.frontend->ops.tuner_ops.init = philips_tu1216_init;
1312 			fe0->dvb.frontend->ops.tuner_ops.set_params = philips_tda6651_pll_set;
1313 		}
1314 		break;
1315 	case SAA7134_BOARD_FLYDVBTDUO:
1316 	case SAA7134_BOARD_FLYDVBT_DUO_CARDBUS:
1317 		if (configure_tda827x_fe(dev, &tda827x_lifeview_config,
1318 					 &tda827x_cfg_0) < 0)
1319 			goto detach_frontend;
1320 		break;
1321 	case SAA7134_BOARD_PHILIPS_EUROPA:
1322 	case SAA7134_BOARD_VIDEOMATE_DVBT_300:
1323 	case SAA7134_BOARD_ASUS_EUROPA_HYBRID:
1324 		fe0->dvb.frontend = dvb_attach(tda10046_attach,
1325 					       &philips_europa_config,
1326 					       &dev->i2c_adap);
1327 		if (fe0->dvb.frontend) {
1328 			dev->original_demod_sleep = fe0->dvb.frontend->ops.sleep;
1329 			fe0->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1330 			fe0->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1331 			fe0->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1332 			fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1333 		}
1334 		break;
1335 	case SAA7134_BOARD_TECHNOTREND_BUDGET_T3000:
1336 		fe0->dvb.frontend = dvb_attach(tda10046_attach,
1337 					       &technotrend_budget_t3000_config,
1338 					       &dev->i2c_adap);
1339 		if (fe0->dvb.frontend) {
1340 			dev->original_demod_sleep = fe0->dvb.frontend->ops.sleep;
1341 			fe0->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1342 			fe0->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1343 			fe0->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1344 			fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1345 		}
1346 		break;
1347 	case SAA7134_BOARD_VIDEOMATE_DVBT_200:
1348 		fe0->dvb.frontend = dvb_attach(tda10046_attach,
1349 					       &philips_tu1216_61_config,
1350 					       &dev->i2c_adap);
1351 		if (fe0->dvb.frontend) {
1352 			fe0->dvb.frontend->ops.tuner_ops.init = philips_tu1216_init;
1353 			fe0->dvb.frontend->ops.tuner_ops.set_params = philips_tda6651_pll_set;
1354 		}
1355 		break;
1356 	case SAA7134_BOARD_KWORLD_DVBT_210:
1357 		if (configure_tda827x_fe(dev, &kworld_dvb_t_210_config,
1358 					 &tda827x_cfg_2) < 0)
1359 			goto detach_frontend;
1360 		break;
1361 	case SAA7134_BOARD_HAUPPAUGE_HVR1120:
1362 		fe0->dvb.frontend = dvb_attach(tda10048_attach,
1363 					       &hcw_tda10048_config,
1364 					       &dev->i2c_adap);
1365 		if (fe0->dvb.frontend != NULL) {
1366 			dvb_attach(tda829x_attach, fe0->dvb.frontend,
1367 				   &dev->i2c_adap, 0x4b,
1368 				   &tda829x_no_probe);
1369 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1370 				   0x60, &dev->i2c_adap,
1371 				   &hcw_tda18271_config);
1372 		}
1373 		break;
1374 	case SAA7134_BOARD_PHILIPS_TIGER:
1375 		if (configure_tda827x_fe(dev, &philips_tiger_config,
1376 					 &tda827x_cfg_0) < 0)
1377 			goto detach_frontend;
1378 		break;
1379 	case SAA7134_BOARD_PINNACLE_PCTV_310i:
1380 		if (configure_tda827x_fe(dev, &pinnacle_pctv_310i_config,
1381 					 &tda827x_cfg_1) < 0)
1382 			goto detach_frontend;
1383 		break;
1384 	case SAA7134_BOARD_HAUPPAUGE_HVR1110:
1385 		if (configure_tda827x_fe(dev, &hauppauge_hvr_1110_config,
1386 					 &tda827x_cfg_1) < 0)
1387 			goto detach_frontend;
1388 		break;
1389 	case SAA7134_BOARD_HAUPPAUGE_HVR1150:
1390 		fe0->dvb.frontend = dvb_attach(lgdt3305_attach,
1391 					       &hcw_lgdt3305_config,
1392 					       &dev->i2c_adap);
1393 		if (fe0->dvb.frontend) {
1394 			dvb_attach(tda829x_attach, fe0->dvb.frontend,
1395 				   &dev->i2c_adap, 0x4b,
1396 				   &tda829x_no_probe);
1397 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1398 				   0x60, &dev->i2c_adap,
1399 				   &hcw_tda18271_config);
1400 		}
1401 		break;
1402 	case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
1403 		if (configure_tda827x_fe(dev, &asus_p7131_dual_config,
1404 					 &tda827x_cfg_0) < 0)
1405 			goto detach_frontend;
1406 		break;
1407 	case SAA7134_BOARD_FLYDVBT_LR301:
1408 		if (configure_tda827x_fe(dev, &tda827x_lifeview_config,
1409 					 &tda827x_cfg_0) < 0)
1410 			goto detach_frontend;
1411 		break;
1412 	case SAA7134_BOARD_FLYDVB_TRIO:
1413 		if (!use_frontend) {	/* terrestrial */
1414 			if (configure_tda827x_fe(dev, &lifeview_trio_config,
1415 						 &tda827x_cfg_0) < 0)
1416 				goto detach_frontend;
1417 		} else {		/* satellite */
1418 			fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs, &dev->i2c_adap);
1419 			if (fe0->dvb.frontend) {
1420 				if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x63,
1421 									&dev->i2c_adap, 0) == NULL) {
1422 					pr_warn("%s: Lifeview Trio, No tda826x found!\n",
1423 						__func__);
1424 					goto detach_frontend;
1425 				}
1426 				if (dvb_attach(isl6421_attach, fe0->dvb.frontend,
1427 					       &dev->i2c_adap,
1428 					       0x08, 0, 0, false) == NULL) {
1429 					pr_warn("%s: Lifeview Trio, No ISL6421 found!\n",
1430 						__func__);
1431 					goto detach_frontend;
1432 				}
1433 			}
1434 		}
1435 		break;
1436 	case SAA7134_BOARD_ADS_DUO_CARDBUS_PTV331:
1437 	case SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS:
1438 		fe0->dvb.frontend = dvb_attach(tda10046_attach,
1439 					       &ads_tech_duo_config,
1440 					       &dev->i2c_adap);
1441 		if (fe0->dvb.frontend) {
1442 			if (dvb_attach(tda827x_attach,fe0->dvb.frontend,
1443 				   ads_tech_duo_config.tuner_address, &dev->i2c_adap,
1444 								&ads_duo_cfg) == NULL) {
1445 				pr_warn("no tda827x tuner found at addr: %02x\n",
1446 					ads_tech_duo_config.tuner_address);
1447 				goto detach_frontend;
1448 			}
1449 		} else
1450 			pr_warn("failed to attach tda10046\n");
1451 		break;
1452 	case SAA7134_BOARD_TEVION_DVBT_220RF:
1453 		if (configure_tda827x_fe(dev, &tevion_dvbt220rf_config,
1454 					 &tda827x_cfg_0) < 0)
1455 			goto detach_frontend;
1456 		break;
1457 	case SAA7134_BOARD_MEDION_MD8800_QUADRO:
1458 		if (!use_frontend) {     /* terrestrial */
1459 			if (configure_tda827x_fe(dev, &md8800_dvbt_config,
1460 						 &tda827x_cfg_0) < 0)
1461 				goto detach_frontend;
1462 		} else {        /* satellite */
1463 			fe0->dvb.frontend = dvb_attach(tda10086_attach,
1464 							&flydvbs, &dev->i2c_adap);
1465 			if (fe0->dvb.frontend) {
1466 				struct dvb_frontend *fe = fe0->dvb.frontend;
1467 				u8 dev_id = dev->eedata[2];
1468 				u8 data = 0xc4;
1469 				struct i2c_msg msg = {.addr = 0x08, .flags = 0, .len = 1};
1470 
1471 				if (dvb_attach(tda826x_attach, fe0->dvb.frontend,
1472 						0x60, &dev->i2c_adap, 0) == NULL) {
1473 					pr_warn("%s: Medion Quadro, no tda826x found !\n",
1474 						__func__);
1475 					goto detach_frontend;
1476 				}
1477 				if (dev_id != 0x08) {
1478 					/* we need to open the i2c gate (we know it exists) */
1479 					fe->ops.i2c_gate_ctrl(fe, 1);
1480 					if (dvb_attach(isl6405_attach, fe,
1481 							&dev->i2c_adap, 0x08, 0, 0) == NULL) {
1482 						pr_warn("%s: Medion Quadro, no ISL6405 found !\n",
1483 							__func__);
1484 						goto detach_frontend;
1485 					}
1486 					if (dev_id == 0x07) {
1487 						/* fire up the 2nd section of the LNB supply since
1488 						   we can't do this from the other section */
1489 						msg.buf = &data;
1490 						i2c_transfer(&dev->i2c_adap, &msg, 1);
1491 					}
1492 					fe->ops.i2c_gate_ctrl(fe, 0);
1493 					dev->original_set_voltage = fe->ops.set_voltage;
1494 					fe->ops.set_voltage = md8800_set_voltage;
1495 					dev->original_set_high_voltage = fe->ops.enable_high_lnb_voltage;
1496 					fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage;
1497 				} else {
1498 					fe->ops.set_voltage = md8800_set_voltage2;
1499 					fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage2;
1500 				}
1501 			}
1502 		}
1503 		break;
1504 	case SAA7134_BOARD_AVERMEDIA_AVERTVHD_A180:
1505 		fe0->dvb.frontend = dvb_attach(nxt200x_attach, &avertvhda180,
1506 					       &dev->i2c_adap);
1507 		if (fe0->dvb.frontend)
1508 			dvb_attach(dvb_pll_attach, fe0->dvb.frontend, 0x61,
1509 				   NULL, DVB_PLL_TDHU2);
1510 		break;
1511 	case SAA7134_BOARD_ADS_INSTANT_HDTV_PCI:
1512 	case SAA7134_BOARD_KWORLD_ATSC110:
1513 		fe0->dvb.frontend = dvb_attach(nxt200x_attach, &kworldatsc110,
1514 					       &dev->i2c_adap);
1515 		if (fe0->dvb.frontend)
1516 			dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1517 				   &dev->i2c_adap, 0x61,
1518 				   TUNER_PHILIPS_TUV1236D);
1519 		break;
1520 	case SAA7134_BOARD_KWORLD_PC150U:
1521 		saa7134_set_gpio(dev, 18, 1); /* Switch to digital mode */
1522 		saa7134_tuner_callback(dev, 0,
1523 				       TDA18271_CALLBACK_CMD_AGC_ENABLE, 1);
1524 		fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1525 					       &kworld_s5h1411_config,
1526 					       &dev->i2c_adap);
1527 		if (fe0->dvb.frontend != NULL) {
1528 			dvb_attach(tda829x_attach, fe0->dvb.frontend,
1529 				   &dev->i2c_adap, 0x4b,
1530 				   &tda829x_no_probe);
1531 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1532 				   0x60, &dev->i2c_adap,
1533 				   &kworld_pc150u_tda18271_config);
1534 		}
1535 		break;
1536 	case SAA7134_BOARD_FLYDVBS_LR300:
1537 		fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs,
1538 					       &dev->i2c_adap);
1539 		if (fe0->dvb.frontend) {
1540 			if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x60,
1541 				       &dev->i2c_adap, 0) == NULL) {
1542 				pr_warn("%s: No tda826x found!\n", __func__);
1543 				goto detach_frontend;
1544 			}
1545 			if (dvb_attach(isl6421_attach, fe0->dvb.frontend,
1546 				       &dev->i2c_adap,
1547 				       0x08, 0, 0, false) == NULL) {
1548 				pr_warn("%s: No ISL6421 found!\n", __func__);
1549 				goto detach_frontend;
1550 			}
1551 		}
1552 		break;
1553 	case SAA7134_BOARD_ASUS_EUROPA2_HYBRID:
1554 		fe0->dvb.frontend = dvb_attach(tda10046_attach,
1555 					       &medion_cardbus,
1556 					       &dev->i2c_adap);
1557 		if (fe0->dvb.frontend) {
1558 			dev->original_demod_sleep = fe0->dvb.frontend->ops.sleep;
1559 			fe0->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1560 
1561 			dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1562 				   &dev->i2c_adap, medion_cardbus.tuner_address,
1563 				   TUNER_PHILIPS_FMD1216ME_MK3);
1564 		}
1565 		break;
1566 	case SAA7134_BOARD_VIDEOMATE_DVBT_200A:
1567 		fe0->dvb.frontend = dvb_attach(tda10046_attach,
1568 				&philips_europa_config,
1569 				&dev->i2c_adap);
1570 		if (fe0->dvb.frontend) {
1571 			fe0->dvb.frontend->ops.tuner_ops.init = philips_td1316_tuner_init;
1572 			fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1573 		}
1574 		break;
1575 	case SAA7134_BOARD_CINERGY_HT_PCMCIA:
1576 		if (configure_tda827x_fe(dev, &cinergy_ht_config,
1577 					 &tda827x_cfg_0) < 0)
1578 			goto detach_frontend;
1579 		break;
1580 	case SAA7134_BOARD_CINERGY_HT_PCI:
1581 		if (configure_tda827x_fe(dev, &cinergy_ht_pci_config,
1582 					 &tda827x_cfg_0) < 0)
1583 			goto detach_frontend;
1584 		break;
1585 	case SAA7134_BOARD_PHILIPS_TIGER_S:
1586 		if (configure_tda827x_fe(dev, &philips_tiger_s_config,
1587 					 &tda827x_cfg_2) < 0)
1588 			goto detach_frontend;
1589 		break;
1590 	case SAA7134_BOARD_ASUS_P7131_4871:
1591 		if (configure_tda827x_fe(dev, &asus_p7131_4871_config,
1592 					 &tda827x_cfg_2) < 0)
1593 			goto detach_frontend;
1594 		break;
1595 	case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
1596 		if (configure_tda827x_fe(dev, &asus_p7131_hybrid_lna_config,
1597 					 &tda827x_cfg_2) < 0)
1598 			goto detach_frontend;
1599 		break;
1600 	case SAA7134_BOARD_AVERMEDIA_SUPER_007:
1601 		if (configure_tda827x_fe(dev, &avermedia_super_007_config,
1602 					 &tda827x_cfg_0) < 0)
1603 			goto detach_frontend;
1604 		break;
1605 	case SAA7134_BOARD_TWINHAN_DTV_DVB_3056:
1606 		if (configure_tda827x_fe(dev, &twinhan_dtv_dvb_3056_config,
1607 					 &tda827x_cfg_2_sw42) < 0)
1608 			goto detach_frontend;
1609 		break;
1610 	case SAA7134_BOARD_PHILIPS_SNAKE:
1611 		fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs,
1612 						&dev->i2c_adap);
1613 		if (fe0->dvb.frontend) {
1614 			if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x60,
1615 					&dev->i2c_adap, 0) == NULL) {
1616 				pr_warn("%s: No tda826x found!\n", __func__);
1617 				goto detach_frontend;
1618 			}
1619 			if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1620 					&dev->i2c_adap, 0, 0) == NULL) {
1621 				pr_warn("%s: No lnbp21 found!\n", __func__);
1622 				goto detach_frontend;
1623 			}
1624 		}
1625 		break;
1626 	case SAA7134_BOARD_CREATIX_CTX953:
1627 		if (configure_tda827x_fe(dev, &md8800_dvbt_config,
1628 					 &tda827x_cfg_0) < 0)
1629 			goto detach_frontend;
1630 		break;
1631 	case SAA7134_BOARD_MSI_TVANYWHERE_AD11:
1632 		if (configure_tda827x_fe(dev, &philips_tiger_s_config,
1633 					 &tda827x_cfg_2) < 0)
1634 			goto detach_frontend;
1635 		break;
1636 	case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
1637 		pr_debug("AverMedia E506R dvb setup\n");
1638 		saa7134_set_gpio(dev, 25, 0);
1639 		msleep(10);
1640 		saa7134_set_gpio(dev, 25, 1);
1641 		fe0->dvb.frontend = dvb_attach(mt352_attach,
1642 						&avermedia_xc3028_mt352_dev,
1643 						&dev->i2c_adap);
1644 		attach_xc3028 = 1;
1645 		break;
1646 	case SAA7134_BOARD_MD7134_BRIDGE_2:
1647 		fe0->dvb.frontend = dvb_attach(tda10086_attach,
1648 						&sd1878_4m, &dev->i2c_adap);
1649 		if (fe0->dvb.frontend) {
1650 			struct dvb_frontend *fe;
1651 			if (dvb_attach(dvb_pll_attach, fe0->dvb.frontend, 0x60,
1652 				  &dev->i2c_adap, DVB_PLL_PHILIPS_SD1878_TDA8261) == NULL) {
1653 				pr_warn("%s: MD7134 DVB-S, no SD1878 found !\n",
1654 					__func__);
1655 				goto detach_frontend;
1656 			}
1657 			/* we need to open the i2c gate (we know it exists) */
1658 			fe = fe0->dvb.frontend;
1659 			fe->ops.i2c_gate_ctrl(fe, 1);
1660 			if (dvb_attach(isl6405_attach, fe,
1661 					&dev->i2c_adap, 0x08, 0, 0) == NULL) {
1662 				pr_warn("%s: MD7134 DVB-S, no ISL6405 found !\n",
1663 					__func__);
1664 				goto detach_frontend;
1665 			}
1666 			fe->ops.i2c_gate_ctrl(fe, 0);
1667 			dev->original_set_voltage = fe->ops.set_voltage;
1668 			fe->ops.set_voltage = md8800_set_voltage;
1669 			dev->original_set_high_voltage = fe->ops.enable_high_lnb_voltage;
1670 			fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage;
1671 		}
1672 		break;
1673 	case SAA7134_BOARD_AVERMEDIA_M103:
1674 		saa7134_set_gpio(dev, 25, 0);
1675 		msleep(10);
1676 		saa7134_set_gpio(dev, 25, 1);
1677 		fe0->dvb.frontend = dvb_attach(mt352_attach,
1678 						&avermedia_xc3028_mt352_dev,
1679 						&dev->i2c_adap);
1680 		attach_xc3028 = 1;
1681 		break;
1682 	case SAA7134_BOARD_ASUSTeK_TIGER_3IN1:
1683 		if (!use_frontend) {     /* terrestrial */
1684 			if (configure_tda827x_fe(dev, &asus_tiger_3in1_config,
1685 							&tda827x_cfg_2) < 0)
1686 				goto detach_frontend;
1687 		} else {		/* satellite */
1688 			fe0->dvb.frontend = dvb_attach(tda10086_attach,
1689 						&flydvbs, &dev->i2c_adap);
1690 			if (fe0->dvb.frontend) {
1691 				if (dvb_attach(tda826x_attach,
1692 						fe0->dvb.frontend, 0x60,
1693 						&dev->i2c_adap, 0) == NULL) {
1694 					pr_warn("%s: Asus Tiger 3in1, no tda826x found!\n",
1695 						__func__);
1696 					goto detach_frontend;
1697 				}
1698 				if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1699 						&dev->i2c_adap, 0, 0) == NULL) {
1700 					pr_warn("%s: Asus Tiger 3in1, no lnbp21 found!\n",
1701 						__func__);
1702 					goto detach_frontend;
1703 			       }
1704 		       }
1705 	       }
1706 	       break;
1707 	case SAA7134_BOARD_ASUSTeK_PS3_100:
1708 		if (!use_frontend) {     /* terrestrial */
1709 			if (configure_tda827x_fe(dev, &asus_ps3_100_config,
1710 						 &tda827x_cfg_2) < 0)
1711 				goto detach_frontend;
1712 	       } else {                /* satellite */
1713 			fe0->dvb.frontend = dvb_attach(tda10086_attach,
1714 						       &flydvbs, &dev->i2c_adap);
1715 			if (fe0->dvb.frontend) {
1716 				if (dvb_attach(tda826x_attach,
1717 					       fe0->dvb.frontend, 0x60,
1718 					       &dev->i2c_adap, 0) == NULL) {
1719 					pr_warn("%s: Asus My Cinema PS3-100, no tda826x found!\n",
1720 						__func__);
1721 					goto detach_frontend;
1722 				}
1723 				if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1724 					       &dev->i2c_adap, 0, 0) == NULL) {
1725 					pr_warn("%s: Asus My Cinema PS3-100, no lnbp21 found!\n",
1726 						__func__);
1727 					goto detach_frontend;
1728 				}
1729 			}
1730 		}
1731 		break;
1732 	case SAA7134_BOARD_ASUSTeK_TIGER:
1733 		if (configure_tda827x_fe(dev, &philips_tiger_config,
1734 					 &tda827x_cfg_0) < 0)
1735 			goto detach_frontend;
1736 		break;
1737 	case SAA7134_BOARD_BEHOLD_H6:
1738 		fe0->dvb.frontend = dvb_attach(zl10353_attach,
1739 						&behold_h6_config,
1740 						&dev->i2c_adap);
1741 		if (fe0->dvb.frontend) {
1742 			dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1743 				   &dev->i2c_adap, 0x61,
1744 				   TUNER_PHILIPS_FMD1216MEX_MK3);
1745 		}
1746 		break;
1747 	case SAA7134_BOARD_BEHOLD_X7:
1748 		fe0->dvb.frontend = dvb_attach(zl10353_attach,
1749 						&behold_x7_config,
1750 						&dev->i2c_adap);
1751 		if (fe0->dvb.frontend) {
1752 			dvb_attach(xc5000_attach, fe0->dvb.frontend,
1753 				   &dev->i2c_adap, &behold_x7_tunerconfig);
1754 		}
1755 		break;
1756 	case SAA7134_BOARD_BEHOLD_H7:
1757 		fe0->dvb.frontend = dvb_attach(zl10353_attach,
1758 						&behold_x7_config,
1759 						&dev->i2c_adap);
1760 		if (fe0->dvb.frontend) {
1761 			dvb_attach(xc5000_attach, fe0->dvb.frontend,
1762 				   &dev->i2c_adap, &behold_x7_tunerconfig);
1763 		}
1764 		break;
1765 	case SAA7134_BOARD_AVERMEDIA_A700_PRO:
1766 	case SAA7134_BOARD_AVERMEDIA_A700_HYBRID:
1767 		/* Zarlink ZL10313 */
1768 		fe0->dvb.frontend = dvb_attach(mt312_attach,
1769 			&avertv_a700_mt312, &dev->i2c_adap);
1770 		if (fe0->dvb.frontend) {
1771 			if (dvb_attach(zl10036_attach, fe0->dvb.frontend,
1772 					&avertv_a700_tuner, &dev->i2c_adap) == NULL) {
1773 				pr_warn("%s: No zl10036 found!\n",
1774 					__func__);
1775 			}
1776 		}
1777 		break;
1778 	case SAA7134_BOARD_VIDEOMATE_S350:
1779 		fe0->dvb.frontend = dvb_attach(mt312_attach,
1780 				&zl10313_compro_s350_config, &dev->i2c_adap);
1781 		if (fe0->dvb.frontend)
1782 			if (dvb_attach(zl10039_attach, fe0->dvb.frontend,
1783 					0x60, &dev->i2c_adap) == NULL)
1784 				pr_warn("%s: No zl10039 found!\n",
1785 					__func__);
1786 
1787 		break;
1788 	case SAA7134_BOARD_VIDEOMATE_T750:
1789 		fe0->dvb.frontend = dvb_attach(zl10353_attach,
1790 						&videomate_t750_zl10353_config,
1791 						&dev->i2c_adap);
1792 		if (fe0->dvb.frontend != NULL) {
1793 			if (dvb_attach(qt1010_attach,
1794 					fe0->dvb.frontend,
1795 					&dev->i2c_adap,
1796 					&videomate_t750_qt1010_config) == NULL)
1797 				pr_warn("error attaching QT1010\n");
1798 		}
1799 		break;
1800 	case SAA7134_BOARD_ZOLID_HYBRID_PCI:
1801 		fe0->dvb.frontend = dvb_attach(tda10048_attach,
1802 					       &zolid_tda10048_config,
1803 					       &dev->i2c_adap);
1804 		if (fe0->dvb.frontend != NULL) {
1805 			dvb_attach(tda829x_attach, fe0->dvb.frontend,
1806 				   &dev->i2c_adap, 0x4b,
1807 				   &tda829x_no_probe);
1808 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1809 				   0x60, &dev->i2c_adap,
1810 				   &zolid_tda18271_config);
1811 		}
1812 		break;
1813 	case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
1814 		fe0->dvb.frontend = dvb_attach(tda10048_attach,
1815 					       &dtv1000s_tda10048_config,
1816 					       &dev->i2c_adap);
1817 		if (fe0->dvb.frontend != NULL) {
1818 			dvb_attach(tda829x_attach, fe0->dvb.frontend,
1819 				   &dev->i2c_adap, 0x4b,
1820 				   &tda829x_no_probe);
1821 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1822 				   0x60, &dev->i2c_adap,
1823 				   &dtv1000s_tda18271_config);
1824 		}
1825 		break;
1826 	case SAA7134_BOARD_KWORLD_PCI_SBTVD_FULLSEG:
1827 		/* Switch to digital mode */
1828 		saa7134_tuner_callback(dev, 0,
1829 				       TDA18271_CALLBACK_CMD_AGC_ENABLE, 1);
1830 		fe0->dvb.frontend = dvb_attach(mb86a20s_attach,
1831 					       &kworld_mb86a20s_config,
1832 					       &dev->i2c_adap);
1833 		if (fe0->dvb.frontend != NULL) {
1834 			dvb_attach(tda829x_attach, fe0->dvb.frontend,
1835 				   &dev->i2c_adap, 0x4b,
1836 				   &tda829x_no_probe);
1837 			fe0->dvb.frontend->ops.i2c_gate_ctrl = kworld_sbtvd_gate_ctrl;
1838 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1839 				   0x60, &dev->i2c_adap,
1840 				   &kworld_tda18271_config);
1841 		}
1842 
1843 		/* mb86a20s need to use the I2C gateway */
1844 		break;
1845 	case SAA7134_BOARD_MAGICPRO_PROHDTV_PRO2:
1846 		fe0->dvb.frontend = dvb_attach(lgs8gxx_attach,
1847 					       &prohdtv_pro2_lgs8g75_config,
1848 					       &dev->i2c_adap);
1849 		if (fe0->dvb.frontend != NULL) {
1850 			dvb_attach(tda829x_attach, fe0->dvb.frontend,
1851 				   &dev->i2c_adap, 0x4b,
1852 				   &tda829x_no_probe);
1853 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1854 				   0x60, &dev->i2c_adap,
1855 				   &prohdtv_pro2_tda18271_config);
1856 		}
1857 		break;
1858 	case SAA7134_BOARD_AVERMEDIA_A706:
1859 		/* Enable all DVB-S devices now */
1860 		/* CE5039 DVB-S tuner SLEEP pin low */
1861 		saa7134_set_gpio(dev, 23, 0);
1862 		/* CE6313 DVB-S demod SLEEP pin low */
1863 		saa7134_set_gpio(dev, 9, 0);
1864 		/* CE6313 DVB-S demod RESET# pin high */
1865 		saa7134_set_gpio(dev, 25, 1);
1866 		msleep(1);
1867 		fe0->dvb.frontend = dvb_attach(mt312_attach,
1868 				&zl10313_avermedia_a706_config, &dev->i2c_adap);
1869 		if (fe0->dvb.frontend) {
1870 			fe0->dvb.frontend->ops.i2c_gate_ctrl = NULL;
1871 			if (dvb_attach(zl10039_attach, fe0->dvb.frontend,
1872 					0x60, &dev->i2c_adap) == NULL)
1873 				pr_warn("%s: No zl10039 found!\n",
1874 					__func__);
1875 		}
1876 		break;
1877 	case SAA7134_BOARD_LEADTEK_WINFAST_HDTV200_H:
1878 		fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1879 					       &hdtv200h_s5h1411_config,
1880 					       &dev->i2c_adap);
1881 		if (fe0->dvb.frontend) {
1882 			dvb_attach(tda829x_attach, fe0->dvb.frontend,
1883 				   &dev->i2c_adap, 0x4b,
1884 				   &tda829x_no_probe);
1885 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1886 				   0x60, &dev->i2c_adap,
1887 				   &hdtv200h_tda18271_config);
1888 		}
1889 		break;
1890 	default:
1891 		pr_warn("Huh? unknown DVB card?\n");
1892 		break;
1893 	}
1894 
1895 	if (attach_xc3028) {
1896 		struct dvb_frontend *fe;
1897 		struct xc2028_config cfg = {
1898 			.i2c_adap  = &dev->i2c_adap,
1899 			.i2c_addr  = 0x61,
1900 		};
1901 
1902 		if (!fe0->dvb.frontend)
1903 			goto detach_frontend;
1904 
1905 		fe = dvb_attach(xc2028_attach, fe0->dvb.frontend, &cfg);
1906 		if (!fe) {
1907 			pr_err("%s/2: xc3028 attach failed\n",
1908 			       dev->name);
1909 			goto detach_frontend;
1910 		}
1911 	}
1912 
1913 	if (NULL == fe0->dvb.frontend) {
1914 		pr_err("%s/dvb: frontend initialization failed\n", dev->name);
1915 		goto detach_frontend;
1916 	}
1917 	/* define general-purpose callback pointer */
1918 	fe0->dvb.frontend->callback = saa7134_tuner_callback;
1919 
1920 	/* register everything else */
1921 #ifndef CONFIG_MEDIA_CONTROLLER_DVB
1922 	ret = vb2_dvb_register_bus(&dev->frontends, THIS_MODULE, dev,
1923 				   &dev->pci->dev, NULL,
1924 				   adapter_nr, 0);
1925 #else
1926 	ret = vb2_dvb_register_bus(&dev->frontends, THIS_MODULE, dev,
1927 				   &dev->pci->dev, dev->media_dev,
1928 				   adapter_nr, 0);
1929 #endif
1930 
1931 	/* this sequence is necessary to make the tda1004x load its firmware
1932 	 * and to enter analog mode of hybrid boards
1933 	 */
1934 	if (!ret) {
1935 		if (fe0->dvb.frontend->ops.init)
1936 			fe0->dvb.frontend->ops.init(fe0->dvb.frontend);
1937 		if (fe0->dvb.frontend->ops.sleep)
1938 			fe0->dvb.frontend->ops.sleep(fe0->dvb.frontend);
1939 		if (fe0->dvb.frontend->ops.tuner_ops.sleep)
1940 			fe0->dvb.frontend->ops.tuner_ops.sleep(fe0->dvb.frontend);
1941 	}
1942 	return ret;
1943 
1944 detach_frontend:
1945 	vb2_dvb_dealloc_frontends(&dev->frontends);
1946 	vb2_queue_release(&fe0->dvb.dvbq);
1947 	return -EINVAL;
1948 }
1949 
dvb_fini(struct saa7134_dev * dev)1950 static int dvb_fini(struct saa7134_dev *dev)
1951 {
1952 	struct vb2_dvb_frontend *fe0;
1953 
1954 	/* Get the first frontend */
1955 	fe0 = vb2_dvb_get_frontend(&dev->frontends, 1);
1956 	if (!fe0)
1957 		return -EINVAL;
1958 
1959 	/* FIXME: I suspect that this code is bogus, since the entry for
1960 	   Pinnacle 300I DVB-T PAL already defines the proper init to allow
1961 	   the detection of mt2032 (TDA9887_PORT2_INACTIVE)
1962 	 */
1963 	if (dev->board == SAA7134_BOARD_PINNACLE_300I_DVBT_PAL) {
1964 		struct v4l2_priv_tun_config tda9887_cfg;
1965 		static int on  = TDA9887_PRESENT | TDA9887_PORT2_INACTIVE;
1966 
1967 		tda9887_cfg.tuner = TUNER_TDA9887;
1968 		tda9887_cfg.priv  = &on;
1969 
1970 		/* otherwise we don't detect the tuner on next insmod */
1971 		saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1972 	} else if (dev->board == SAA7134_BOARD_MEDION_MD8800_QUADRO) {
1973 		if ((dev->eedata[2] == 0x07) && use_frontend) {
1974 			/* turn off the 2nd lnb supply */
1975 			u8 data = 0x80;
1976 			struct i2c_msg msg = {.addr = 0x08, .buf = &data, .flags = 0, .len = 1};
1977 			struct dvb_frontend *fe;
1978 			fe = fe0->dvb.frontend;
1979 			if (fe->ops.i2c_gate_ctrl) {
1980 				fe->ops.i2c_gate_ctrl(fe, 1);
1981 				i2c_transfer(&dev->i2c_adap, &msg, 1);
1982 				fe->ops.i2c_gate_ctrl(fe, 0);
1983 			}
1984 		}
1985 	}
1986 	vb2_dvb_unregister_bus(&dev->frontends);
1987 	vb2_queue_release(&fe0->dvb.dvbq);
1988 	return 0;
1989 }
1990 
1991 static struct saa7134_mpeg_ops dvb_ops = {
1992 	.type          = SAA7134_MPEG_DVB,
1993 	.init          = dvb_init,
1994 	.fini          = dvb_fini,
1995 };
1996 
dvb_register(void)1997 static int __init dvb_register(void)
1998 {
1999 	return saa7134_ts_register(&dvb_ops);
2000 }
2001 
dvb_unregister(void)2002 static void __exit dvb_unregister(void)
2003 {
2004 	saa7134_ts_unregister(&dvb_ops);
2005 }
2006 
2007 module_init(dvb_register);
2008 module_exit(dvb_unregister);
2009