1 /*
2  *  Driver for the Conexant CX23885 PCIe bridge
3  *
4  *  Copyright (c) 2006 Steven Toth <stoth@linuxtv.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *
15  *  GNU General Public License for more details.
16  */
17 
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/device.h>
21 #include <linux/fs.h>
22 #include <linux/kthread.h>
23 #include <linux/file.h>
24 #include <linux/suspend.h>
25 
26 #include "cx23885.h"
27 #include <media/v4l2-common.h>
28 
29 #include "dvb_ca_en50221.h"
30 #include "s5h1409.h"
31 #include "s5h1411.h"
32 #include "mt2131.h"
33 #include "tda8290.h"
34 #include "tda18271.h"
35 #include "lgdt330x.h"
36 #include "xc4000.h"
37 #include "xc5000.h"
38 #include "max2165.h"
39 #include "tda10048.h"
40 #include "tuner-xc2028.h"
41 #include "tuner-simple.h"
42 #include "dib7000p.h"
43 #include "dib0070.h"
44 #include "dibx000_common.h"
45 #include "zl10353.h"
46 #include "stv0900.h"
47 #include "stv0900_reg.h"
48 #include "stv6110.h"
49 #include "lnbh24.h"
50 #include "cx24116.h"
51 #include "cx24117.h"
52 #include "cimax2.h"
53 #include "lgs8gxx.h"
54 #include "netup-eeprom.h"
55 #include "netup-init.h"
56 #include "lgdt3305.h"
57 #include "atbm8830.h"
58 #include "ts2020.h"
59 #include "ds3000.h"
60 #include "cx23885-f300.h"
61 #include "altera-ci.h"
62 #include "stv0367.h"
63 #include "drxk.h"
64 #include "mt2063.h"
65 #include "stv090x.h"
66 #include "stb6100.h"
67 #include "stb6100_cfg.h"
68 #include "tda10071.h"
69 #include "a8293.h"
70 #include "mb86a20s.h"
71 #include "si2165.h"
72 #include "si2168.h"
73 #include "si2157.h"
74 #include "sp2.h"
75 #include "m88ds3103.h"
76 #include "m88ts2022.h"
77 #include "m88rs6000t.h"
78 
79 static unsigned int debug;
80 
81 #define dprintk(level, fmt, arg...)\
82 	do { if (debug >= level)\
83 		printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
84 	} while (0)
85 
86 /* ------------------------------------------------------------------ */
87 
88 static unsigned int alt_tuner;
89 module_param(alt_tuner, int, 0644);
90 MODULE_PARM_DESC(alt_tuner, "Enable alternate tuner configuration");
91 
92 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
93 
94 /* ------------------------------------------------------------------ */
95 
96 static int queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
97 			   unsigned int *num_buffers, unsigned int *num_planes,
98 			   unsigned int sizes[], void *alloc_ctxs[])
99 {
100 	struct cx23885_tsport *port = q->drv_priv;
101 
102 	port->ts_packet_size  = 188 * 4;
103 	port->ts_packet_count = 32;
104 	*num_planes = 1;
105 	sizes[0] = port->ts_packet_size * port->ts_packet_count;
106 	alloc_ctxs[0] = port->dev->alloc_ctx;
107 	*num_buffers = 32;
108 	return 0;
109 }
110 
111 
112 static int buffer_prepare(struct vb2_buffer *vb)
113 {
114 	struct cx23885_tsport *port = vb->vb2_queue->drv_priv;
115 	struct cx23885_buffer *buf =
116 		container_of(vb, struct cx23885_buffer, vb);
117 
118 	return cx23885_buf_prepare(buf, port);
119 }
120 
121 static void buffer_finish(struct vb2_buffer *vb)
122 {
123 	struct cx23885_tsport *port = vb->vb2_queue->drv_priv;
124 	struct cx23885_dev *dev = port->dev;
125 	struct cx23885_buffer *buf = container_of(vb,
126 		struct cx23885_buffer, vb);
127 
128 	cx23885_free_buffer(dev, buf);
129 }
130 
131 static void buffer_queue(struct vb2_buffer *vb)
132 {
133 	struct cx23885_tsport *port = vb->vb2_queue->drv_priv;
134 	struct cx23885_buffer   *buf = container_of(vb,
135 		struct cx23885_buffer, vb);
136 
137 	cx23885_buf_queue(port, buf);
138 }
139 
140 static void cx23885_dvb_gate_ctrl(struct cx23885_tsport  *port, int open)
141 {
142 	struct vb2_dvb_frontends *f;
143 	struct vb2_dvb_frontend *fe;
144 
145 	f = &port->frontends;
146 
147 	if (f->gate <= 1) /* undefined or fe0 */
148 		fe = vb2_dvb_get_frontend(f, 1);
149 	else
150 		fe = vb2_dvb_get_frontend(f, f->gate);
151 
152 	if (fe && fe->dvb.frontend && fe->dvb.frontend->ops.i2c_gate_ctrl)
153 		fe->dvb.frontend->ops.i2c_gate_ctrl(fe->dvb.frontend, open);
154 }
155 
156 static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
157 {
158 	struct cx23885_tsport *port = q->drv_priv;
159 	struct cx23885_dmaqueue *dmaq = &port->mpegq;
160 	struct cx23885_buffer *buf = list_entry(dmaq->active.next,
161 			struct cx23885_buffer, queue);
162 
163 	cx23885_start_dma(port, dmaq, buf);
164 	return 0;
165 }
166 
167 static void cx23885_stop_streaming(struct vb2_queue *q)
168 {
169 	struct cx23885_tsport *port = q->drv_priv;
170 
171 	cx23885_cancel_buffers(port);
172 }
173 
174 static struct vb2_ops dvb_qops = {
175 	.queue_setup    = queue_setup,
176 	.buf_prepare  = buffer_prepare,
177 	.buf_finish = buffer_finish,
178 	.buf_queue    = buffer_queue,
179 	.wait_prepare = vb2_ops_wait_prepare,
180 	.wait_finish = vb2_ops_wait_finish,
181 	.start_streaming = cx23885_start_streaming,
182 	.stop_streaming = cx23885_stop_streaming,
183 };
184 
185 static struct s5h1409_config hauppauge_generic_config = {
186 	.demod_address = 0x32 >> 1,
187 	.output_mode   = S5H1409_SERIAL_OUTPUT,
188 	.gpio          = S5H1409_GPIO_ON,
189 	.qam_if        = 44000,
190 	.inversion     = S5H1409_INVERSION_OFF,
191 	.status_mode   = S5H1409_DEMODLOCKING,
192 	.mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
193 };
194 
195 static struct tda10048_config hauppauge_hvr1200_config = {
196 	.demod_address    = 0x10 >> 1,
197 	.output_mode      = TDA10048_SERIAL_OUTPUT,
198 	.fwbulkwritelen   = TDA10048_BULKWRITE_200,
199 	.inversion        = TDA10048_INVERSION_ON,
200 	.dtv6_if_freq_khz = TDA10048_IF_3300,
201 	.dtv7_if_freq_khz = TDA10048_IF_3800,
202 	.dtv8_if_freq_khz = TDA10048_IF_4300,
203 	.clk_freq_khz     = TDA10048_CLK_16000,
204 };
205 
206 static struct tda10048_config hauppauge_hvr1210_config = {
207 	.demod_address    = 0x10 >> 1,
208 	.output_mode      = TDA10048_SERIAL_OUTPUT,
209 	.fwbulkwritelen   = TDA10048_BULKWRITE_200,
210 	.inversion        = TDA10048_INVERSION_ON,
211 	.dtv6_if_freq_khz = TDA10048_IF_3300,
212 	.dtv7_if_freq_khz = TDA10048_IF_3500,
213 	.dtv8_if_freq_khz = TDA10048_IF_4000,
214 	.clk_freq_khz     = TDA10048_CLK_16000,
215 };
216 
217 static struct s5h1409_config hauppauge_ezqam_config = {
218 	.demod_address = 0x32 >> 1,
219 	.output_mode   = S5H1409_SERIAL_OUTPUT,
220 	.gpio          = S5H1409_GPIO_OFF,
221 	.qam_if        = 4000,
222 	.inversion     = S5H1409_INVERSION_ON,
223 	.status_mode   = S5H1409_DEMODLOCKING,
224 	.mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
225 };
226 
227 static struct s5h1409_config hauppauge_hvr1800lp_config = {
228 	.demod_address = 0x32 >> 1,
229 	.output_mode   = S5H1409_SERIAL_OUTPUT,
230 	.gpio          = S5H1409_GPIO_OFF,
231 	.qam_if        = 44000,
232 	.inversion     = S5H1409_INVERSION_OFF,
233 	.status_mode   = S5H1409_DEMODLOCKING,
234 	.mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
235 };
236 
237 static struct s5h1409_config hauppauge_hvr1500_config = {
238 	.demod_address = 0x32 >> 1,
239 	.output_mode   = S5H1409_SERIAL_OUTPUT,
240 	.gpio          = S5H1409_GPIO_OFF,
241 	.inversion     = S5H1409_INVERSION_OFF,
242 	.status_mode   = S5H1409_DEMODLOCKING,
243 	.mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
244 };
245 
246 static struct mt2131_config hauppauge_generic_tunerconfig = {
247 	0x61
248 };
249 
250 static struct lgdt330x_config fusionhdtv_5_express = {
251 	.demod_address = 0x0e,
252 	.demod_chip = LGDT3303,
253 	.serial_mpeg = 0x40,
254 };
255 
256 static struct s5h1409_config hauppauge_hvr1500q_config = {
257 	.demod_address = 0x32 >> 1,
258 	.output_mode   = S5H1409_SERIAL_OUTPUT,
259 	.gpio          = S5H1409_GPIO_ON,
260 	.qam_if        = 44000,
261 	.inversion     = S5H1409_INVERSION_OFF,
262 	.status_mode   = S5H1409_DEMODLOCKING,
263 	.mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
264 };
265 
266 static struct s5h1409_config dvico_s5h1409_config = {
267 	.demod_address = 0x32 >> 1,
268 	.output_mode   = S5H1409_SERIAL_OUTPUT,
269 	.gpio          = S5H1409_GPIO_ON,
270 	.qam_if        = 44000,
271 	.inversion     = S5H1409_INVERSION_OFF,
272 	.status_mode   = S5H1409_DEMODLOCKING,
273 	.mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
274 };
275 
276 static struct s5h1411_config dvico_s5h1411_config = {
277 	.output_mode   = S5H1411_SERIAL_OUTPUT,
278 	.gpio          = S5H1411_GPIO_ON,
279 	.qam_if        = S5H1411_IF_44000,
280 	.vsb_if        = S5H1411_IF_44000,
281 	.inversion     = S5H1411_INVERSION_OFF,
282 	.status_mode   = S5H1411_DEMODLOCKING,
283 	.mpeg_timing   = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
284 };
285 
286 static struct s5h1411_config hcw_s5h1411_config = {
287 	.output_mode   = S5H1411_SERIAL_OUTPUT,
288 	.gpio          = S5H1411_GPIO_OFF,
289 	.vsb_if        = S5H1411_IF_44000,
290 	.qam_if        = S5H1411_IF_4000,
291 	.inversion     = S5H1411_INVERSION_ON,
292 	.status_mode   = S5H1411_DEMODLOCKING,
293 	.mpeg_timing   = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
294 };
295 
296 static struct xc5000_config hauppauge_hvr1500q_tunerconfig = {
297 	.i2c_address      = 0x61,
298 	.if_khz           = 5380,
299 };
300 
301 static struct xc5000_config dvico_xc5000_tunerconfig = {
302 	.i2c_address      = 0x64,
303 	.if_khz           = 5380,
304 };
305 
306 static struct tda829x_config tda829x_no_probe = {
307 	.probe_tuner = TDA829X_DONT_PROBE,
308 };
309 
310 static struct tda18271_std_map hauppauge_tda18271_std_map = {
311 	.atsc_6   = { .if_freq = 5380, .agc_mode = 3, .std = 3,
312 		      .if_lvl = 6, .rfagc_top = 0x37 },
313 	.qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 0,
314 		      .if_lvl = 6, .rfagc_top = 0x37 },
315 };
316 
317 static struct tda18271_std_map hauppauge_hvr1200_tda18271_std_map = {
318 	.dvbt_6   = { .if_freq = 3300, .agc_mode = 3, .std = 4,
319 		      .if_lvl = 1, .rfagc_top = 0x37, },
320 	.dvbt_7   = { .if_freq = 3800, .agc_mode = 3, .std = 5,
321 		      .if_lvl = 1, .rfagc_top = 0x37, },
322 	.dvbt_8   = { .if_freq = 4300, .agc_mode = 3, .std = 6,
323 		      .if_lvl = 1, .rfagc_top = 0x37, },
324 };
325 
326 static struct tda18271_config hauppauge_tda18271_config = {
327 	.std_map = &hauppauge_tda18271_std_map,
328 	.gate    = TDA18271_GATE_ANALOG,
329 	.output_opt = TDA18271_OUTPUT_LT_OFF,
330 };
331 
332 static struct tda18271_config hauppauge_hvr1200_tuner_config = {
333 	.std_map = &hauppauge_hvr1200_tda18271_std_map,
334 	.gate    = TDA18271_GATE_ANALOG,
335 	.output_opt = TDA18271_OUTPUT_LT_OFF,
336 };
337 
338 static struct tda18271_config hauppauge_hvr1210_tuner_config = {
339 	.gate    = TDA18271_GATE_DIGITAL,
340 	.output_opt = TDA18271_OUTPUT_LT_OFF,
341 };
342 
343 static struct tda18271_config hauppauge_hvr4400_tuner_config = {
344 	.gate    = TDA18271_GATE_DIGITAL,
345 	.output_opt = TDA18271_OUTPUT_LT_OFF,
346 };
347 
348 static struct tda18271_std_map hauppauge_hvr127x_std_map = {
349 	.atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 4,
350 		      .if_lvl = 1, .rfagc_top = 0x58 },
351 	.qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 5,
352 		      .if_lvl = 1, .rfagc_top = 0x58 },
353 };
354 
355 static struct tda18271_config hauppauge_hvr127x_config = {
356 	.std_map = &hauppauge_hvr127x_std_map,
357 	.output_opt = TDA18271_OUTPUT_LT_OFF,
358 };
359 
360 static struct lgdt3305_config hauppauge_lgdt3305_config = {
361 	.i2c_addr           = 0x0e,
362 	.mpeg_mode          = LGDT3305_MPEG_SERIAL,
363 	.tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
364 	.tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
365 	.deny_i2c_rptr      = 1,
366 	.spectral_inversion = 1,
367 	.qam_if_khz         = 4000,
368 	.vsb_if_khz         = 3250,
369 };
370 
371 static struct dibx000_agc_config xc3028_agc_config = {
372 	BAND_VHF | BAND_UHF,	/* band_caps */
373 
374 	/* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=0,
375 	 * P_agc_inv_pwm1=0, P_agc_inv_pwm2=0,
376 	 * P_agc_inh_dc_rv_est=0, P_agc_time_est=3, P_agc_freeze=0,
377 	 * P_agc_nb_est=2, P_agc_write=0
378 	 */
379 	(0 << 15) | (0 << 14) | (0 << 11) | (0 << 10) | (0 << 9) | (0 << 8) |
380 		(3 << 5) | (0 << 4) | (2 << 1) | (0 << 0), /* setup */
381 
382 	712,	/* inv_gain */
383 	21,	/* time_stabiliz */
384 
385 	0,	/* alpha_level */
386 	118,	/* thlock */
387 
388 	0,	/* wbd_inv */
389 	2867,	/* wbd_ref */
390 	0,	/* wbd_sel */
391 	2,	/* wbd_alpha */
392 
393 	0,	/* agc1_max */
394 	0,	/* agc1_min */
395 	39718,	/* agc2_max */
396 	9930,	/* agc2_min */
397 	0,	/* agc1_pt1 */
398 	0,	/* agc1_pt2 */
399 	0,	/* agc1_pt3 */
400 	0,	/* agc1_slope1 */
401 	0,	/* agc1_slope2 */
402 	0,	/* agc2_pt1 */
403 	128,	/* agc2_pt2 */
404 	29,	/* agc2_slope1 */
405 	29,	/* agc2_slope2 */
406 
407 	17,	/* alpha_mant */
408 	27,	/* alpha_exp */
409 	23,	/* beta_mant */
410 	51,	/* beta_exp */
411 
412 	1,	/* perform_agc_softsplit */
413 };
414 
415 /* PLL Configuration for COFDM BW_MHz = 8.000000
416  * With external clock = 30.000000 */
417 static struct dibx000_bandwidth_config xc3028_bw_config = {
418 	60000,	/* internal */
419 	30000,	/* sampling */
420 	1,	/* pll_cfg: prediv */
421 	8,	/* pll_cfg: ratio */
422 	3,	/* pll_cfg: range */
423 	1,	/* pll_cfg: reset */
424 	0,	/* pll_cfg: bypass */
425 	0,	/* misc: refdiv */
426 	0,	/* misc: bypclk_div */
427 	1,	/* misc: IO_CLK_en_core */
428 	1,	/* misc: ADClkSrc */
429 	0,	/* misc: modulo */
430 	(3 << 14) | (1 << 12) | (524 << 0), /* sad_cfg: refsel, sel, freq_15k */
431 	(1 << 25) | 5816102, /* ifreq = 5.200000 MHz */
432 	20452225, /* timf */
433 	30000000  /* xtal_hz */
434 };
435 
436 static struct dib7000p_config hauppauge_hvr1400_dib7000_config = {
437 	.output_mpeg2_in_188_bytes = 1,
438 	.hostbus_diversity = 1,
439 	.tuner_is_baseband = 0,
440 	.update_lna  = NULL,
441 
442 	.agc_config_count = 1,
443 	.agc = &xc3028_agc_config,
444 	.bw  = &xc3028_bw_config,
445 
446 	.gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
447 	.gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
448 	.gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
449 
450 	.pwm_freq_div = 0,
451 	.agc_control  = NULL,
452 	.spur_protect = 0,
453 
454 	.output_mode = OUTMODE_MPEG2_SERIAL,
455 };
456 
457 static struct zl10353_config dvico_fusionhdtv_xc3028 = {
458 	.demod_address = 0x0f,
459 	.if2           = 45600,
460 	.no_tuner      = 1,
461 	.disable_i2c_gate_ctrl = 1,
462 };
463 
464 static struct stv0900_reg stv0900_ts_regs[] = {
465 	{ R0900_TSGENERAL, 0x00 },
466 	{ R0900_P1_TSSPEED, 0x40 },
467 	{ R0900_P2_TSSPEED, 0x40 },
468 	{ R0900_P1_TSCFGM, 0xc0 },
469 	{ R0900_P2_TSCFGM, 0xc0 },
470 	{ R0900_P1_TSCFGH, 0xe0 },
471 	{ R0900_P2_TSCFGH, 0xe0 },
472 	{ R0900_P1_TSCFGL, 0x20 },
473 	{ R0900_P2_TSCFGL, 0x20 },
474 	{ 0xffff, 0xff }, /* terminate */
475 };
476 
477 static struct stv0900_config netup_stv0900_config = {
478 	.demod_address = 0x68,
479 	.demod_mode = 1, /* dual */
480 	.xtal = 8000000,
481 	.clkmode = 3,/* 0-CLKI, 2-XTALI, else AUTO */
482 	.diseqc_mode = 2,/* 2/3 PWM */
483 	.ts_config_regs = stv0900_ts_regs,
484 	.tun1_maddress = 0,/* 0x60 */
485 	.tun2_maddress = 3,/* 0x63 */
486 	.tun1_adc = 1,/* 1 Vpp */
487 	.tun2_adc = 1,/* 1 Vpp */
488 };
489 
490 static struct stv6110_config netup_stv6110_tunerconfig_a = {
491 	.i2c_address = 0x60,
492 	.mclk = 16000000,
493 	.clk_div = 1,
494 	.gain = 8, /* +16 dB  - maximum gain */
495 };
496 
497 static struct stv6110_config netup_stv6110_tunerconfig_b = {
498 	.i2c_address = 0x63,
499 	.mclk = 16000000,
500 	.clk_div = 1,
501 	.gain = 8, /* +16 dB  - maximum gain */
502 };
503 
504 static struct cx24116_config tbs_cx24116_config = {
505 	.demod_address = 0x55,
506 };
507 
508 static struct cx24117_config tbs_cx24117_config = {
509 	.demod_address = 0x55,
510 };
511 
512 static struct ds3000_config tevii_ds3000_config = {
513 	.demod_address = 0x68,
514 };
515 
516 static struct ts2020_config tevii_ts2020_config  = {
517 	.tuner_address = 0x60,
518 	.clk_out_div = 1,
519 	.frequency_div = 1146000,
520 };
521 
522 static struct cx24116_config dvbworld_cx24116_config = {
523 	.demod_address = 0x05,
524 };
525 
526 static struct lgs8gxx_config mygica_x8506_lgs8gl5_config = {
527 	.prod = LGS8GXX_PROD_LGS8GL5,
528 	.demod_address = 0x19,
529 	.serial_ts = 0,
530 	.ts_clk_pol = 1,
531 	.ts_clk_gated = 1,
532 	.if_clk_freq = 30400, /* 30.4 MHz */
533 	.if_freq = 5380, /* 5.38 MHz */
534 	.if_neg_center = 1,
535 	.ext_adc = 0,
536 	.adc_signed = 0,
537 	.if_neg_edge = 0,
538 };
539 
540 static struct xc5000_config mygica_x8506_xc5000_config = {
541 	.i2c_address = 0x61,
542 	.if_khz = 5380,
543 };
544 
545 static struct mb86a20s_config mygica_x8507_mb86a20s_config = {
546 	.demod_address = 0x10,
547 };
548 
549 static struct xc5000_config mygica_x8507_xc5000_config = {
550 	.i2c_address = 0x61,
551 	.if_khz = 4000,
552 };
553 
554 static struct stv090x_config prof_8000_stv090x_config = {
555 	.device                 = STV0903,
556 	.demod_mode             = STV090x_SINGLE,
557 	.clk_mode               = STV090x_CLK_EXT,
558 	.xtal                   = 27000000,
559 	.address                = 0x6A,
560 	.ts1_mode               = STV090x_TSMODE_PARALLEL_PUNCTURED,
561 	.repeater_level         = STV090x_RPTLEVEL_64,
562 	.adc1_range             = STV090x_ADC_2Vpp,
563 	.diseqc_envelope_mode   = false,
564 
565 	.tuner_get_frequency    = stb6100_get_frequency,
566 	.tuner_set_frequency    = stb6100_set_frequency,
567 	.tuner_set_bandwidth    = stb6100_set_bandwidth,
568 	.tuner_get_bandwidth    = stb6100_get_bandwidth,
569 };
570 
571 static struct stb6100_config prof_8000_stb6100_config = {
572 	.tuner_address = 0x60,
573 	.refclock = 27000000,
574 };
575 
576 static int p8000_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
577 {
578 	struct cx23885_tsport *port = fe->dvb->priv;
579 	struct cx23885_dev *dev = port->dev;
580 
581 	if (voltage == SEC_VOLTAGE_18)
582 		cx_write(MC417_RWD, 0x00001e00);
583 	else if (voltage == SEC_VOLTAGE_13)
584 		cx_write(MC417_RWD, 0x00001a00);
585 	else
586 		cx_write(MC417_RWD, 0x00001800);
587 	return 0;
588 }
589 
590 static int dvbsky_t9580_set_voltage(struct dvb_frontend *fe,
591 					fe_sec_voltage_t voltage)
592 {
593 	struct cx23885_tsport *port = fe->dvb->priv;
594 	struct cx23885_dev *dev = port->dev;
595 
596 	cx23885_gpio_enable(dev, GPIO_0 | GPIO_1, 1);
597 
598 	switch (voltage) {
599 	case SEC_VOLTAGE_13:
600 		cx23885_gpio_set(dev, GPIO_1);
601 		cx23885_gpio_clear(dev, GPIO_0);
602 		break;
603 	case SEC_VOLTAGE_18:
604 		cx23885_gpio_set(dev, GPIO_1);
605 		cx23885_gpio_set(dev, GPIO_0);
606 		break;
607 	case SEC_VOLTAGE_OFF:
608 		cx23885_gpio_clear(dev, GPIO_1);
609 		cx23885_gpio_clear(dev, GPIO_0);
610 		break;
611 	}
612 
613 	/* call the frontend set_voltage function */
614 	port->fe_set_voltage(fe, voltage);
615 
616 	return 0;
617 }
618 
619 static int dvbsky_s952_portc_set_voltage(struct dvb_frontend *fe,
620 					fe_sec_voltage_t voltage)
621 {
622 	struct cx23885_tsport *port = fe->dvb->priv;
623 	struct cx23885_dev *dev = port->dev;
624 
625 	cx23885_gpio_enable(dev, GPIO_12 | GPIO_13, 1);
626 
627 	switch (voltage) {
628 	case SEC_VOLTAGE_13:
629 		cx23885_gpio_set(dev, GPIO_13);
630 		cx23885_gpio_clear(dev, GPIO_12);
631 		break;
632 	case SEC_VOLTAGE_18:
633 		cx23885_gpio_set(dev, GPIO_13);
634 		cx23885_gpio_set(dev, GPIO_12);
635 		break;
636 	case SEC_VOLTAGE_OFF:
637 		cx23885_gpio_clear(dev, GPIO_13);
638 		cx23885_gpio_clear(dev, GPIO_12);
639 		break;
640 	}
641 	/* call the frontend set_voltage function */
642 	return port->fe_set_voltage(fe, voltage);
643 }
644 
645 static int cx23885_sp2_ci_ctrl(void *priv, u8 read, int addr,
646 				u8 data, int *mem)
647 {
648 	/* MC417 */
649 	#define SP2_DATA              0x000000ff
650 	#define SP2_WR                0x00008000
651 	#define SP2_RD                0x00004000
652 	#define SP2_ACK               0x00001000
653 	#define SP2_ADHI              0x00000800
654 	#define SP2_ADLO              0x00000400
655 	#define SP2_CS1               0x00000200
656 	#define SP2_CS0               0x00000100
657 	#define SP2_EN_ALL            0x00001000
658 	#define SP2_CTRL_OFF          (SP2_CS1 | SP2_CS0 | SP2_WR | SP2_RD)
659 
660 	struct cx23885_tsport *port = priv;
661 	struct cx23885_dev *dev = port->dev;
662 	int ret;
663 	int tmp = 0;
664 	unsigned long timeout;
665 
666 	mutex_lock(&dev->gpio_lock);
667 
668 	/* write addr */
669 	cx_write(MC417_OEN, SP2_EN_ALL);
670 	cx_write(MC417_RWD, SP2_CTRL_OFF |
671 				SP2_ADLO | (0xff & addr));
672 	cx_clear(MC417_RWD, SP2_ADLO);
673 	cx_write(MC417_RWD, SP2_CTRL_OFF |
674 				SP2_ADHI | (0xff & (addr >> 8)));
675 	cx_clear(MC417_RWD, SP2_ADHI);
676 
677 	if (read)
678 		/* data in */
679 		cx_write(MC417_OEN, SP2_EN_ALL | SP2_DATA);
680 	else
681 		/* data out */
682 		cx_write(MC417_RWD, SP2_CTRL_OFF | data);
683 
684 	/* chip select 0 */
685 	cx_clear(MC417_RWD, SP2_CS0);
686 
687 	/* read/write */
688 	cx_clear(MC417_RWD, (read) ? SP2_RD : SP2_WR);
689 
690 	/* wait for a maximum of 1 msec */
691 	timeout = jiffies + msecs_to_jiffies(1);
692 	while (!time_after(jiffies, timeout)) {
693 		tmp = cx_read(MC417_RWD);
694 		if ((tmp & SP2_ACK) == 0)
695 			break;
696 		usleep_range(50, 100);
697 	}
698 
699 	cx_set(MC417_RWD, SP2_CTRL_OFF);
700 	*mem = tmp & 0xff;
701 
702 	mutex_unlock(&dev->gpio_lock);
703 
704 	if (!read) {
705 		if (*mem < 0) {
706 			ret = -EREMOTEIO;
707 			goto err;
708 		}
709 	}
710 
711 	return 0;
712 err:
713 	return ret;
714 }
715 
716 static int cx23885_dvb_set_frontend(struct dvb_frontend *fe)
717 {
718 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
719 	struct cx23885_tsport *port = fe->dvb->priv;
720 	struct cx23885_dev *dev = port->dev;
721 
722 	switch (dev->board) {
723 	case CX23885_BOARD_HAUPPAUGE_HVR1275:
724 		switch (p->modulation) {
725 		case VSB_8:
726 			cx23885_gpio_clear(dev, GPIO_5);
727 			break;
728 		case QAM_64:
729 		case QAM_256:
730 		default:
731 			cx23885_gpio_set(dev, GPIO_5);
732 			break;
733 		}
734 		break;
735 	case CX23885_BOARD_MYGICA_X8506:
736 	case CX23885_BOARD_MYGICA_X8507:
737 	case CX23885_BOARD_MAGICPRO_PROHDTVE2:
738 		/* Select Digital TV */
739 		cx23885_gpio_set(dev, GPIO_0);
740 		break;
741 	}
742 
743 	/* Call the real set_frontend */
744 	if (port->set_frontend)
745 		return port->set_frontend(fe);
746 
747 	return 0;
748 }
749 
750 static void cx23885_set_frontend_hook(struct cx23885_tsport *port,
751 				     struct dvb_frontend *fe)
752 {
753 	port->set_frontend = fe->ops.set_frontend;
754 	fe->ops.set_frontend = cx23885_dvb_set_frontend;
755 }
756 
757 static struct lgs8gxx_config magicpro_prohdtve2_lgs8g75_config = {
758 	.prod = LGS8GXX_PROD_LGS8G75,
759 	.demod_address = 0x19,
760 	.serial_ts = 0,
761 	.ts_clk_pol = 1,
762 	.ts_clk_gated = 1,
763 	.if_clk_freq = 30400, /* 30.4 MHz */
764 	.if_freq = 6500, /* 6.50 MHz */
765 	.if_neg_center = 1,
766 	.ext_adc = 0,
767 	.adc_signed = 1,
768 	.adc_vpp = 2, /* 1.6 Vpp */
769 	.if_neg_edge = 1,
770 };
771 
772 static struct xc5000_config magicpro_prohdtve2_xc5000_config = {
773 	.i2c_address = 0x61,
774 	.if_khz = 6500,
775 };
776 
777 static struct atbm8830_config mygica_x8558pro_atbm8830_cfg1 = {
778 	.prod = ATBM8830_PROD_8830,
779 	.demod_address = 0x44,
780 	.serial_ts = 0,
781 	.ts_sampling_edge = 1,
782 	.ts_clk_gated = 0,
783 	.osc_clk_freq = 30400, /* in kHz */
784 	.if_freq = 0, /* zero IF */
785 	.zif_swap_iq = 1,
786 	.agc_min = 0x2E,
787 	.agc_max = 0xFF,
788 	.agc_hold_loop = 0,
789 };
790 
791 static struct max2165_config mygic_x8558pro_max2165_cfg1 = {
792 	.i2c_address = 0x60,
793 	.osc_clk = 20
794 };
795 
796 static struct atbm8830_config mygica_x8558pro_atbm8830_cfg2 = {
797 	.prod = ATBM8830_PROD_8830,
798 	.demod_address = 0x44,
799 	.serial_ts = 1,
800 	.ts_sampling_edge = 1,
801 	.ts_clk_gated = 0,
802 	.osc_clk_freq = 30400, /* in kHz */
803 	.if_freq = 0, /* zero IF */
804 	.zif_swap_iq = 1,
805 	.agc_min = 0x2E,
806 	.agc_max = 0xFF,
807 	.agc_hold_loop = 0,
808 };
809 
810 static struct max2165_config mygic_x8558pro_max2165_cfg2 = {
811 	.i2c_address = 0x60,
812 	.osc_clk = 20
813 };
814 static struct stv0367_config netup_stv0367_config[] = {
815 	{
816 		.demod_address = 0x1c,
817 		.xtal = 27000000,
818 		.if_khz = 4500,
819 		.if_iq_mode = 0,
820 		.ts_mode = 1,
821 		.clk_pol = 0,
822 	}, {
823 		.demod_address = 0x1d,
824 		.xtal = 27000000,
825 		.if_khz = 4500,
826 		.if_iq_mode = 0,
827 		.ts_mode = 1,
828 		.clk_pol = 0,
829 	},
830 };
831 
832 static struct xc5000_config netup_xc5000_config[] = {
833 	{
834 		.i2c_address = 0x61,
835 		.if_khz = 4500,
836 	}, {
837 		.i2c_address = 0x64,
838 		.if_khz = 4500,
839 	},
840 };
841 
842 static struct drxk_config terratec_drxk_config[] = {
843 	{
844 		.adr = 0x29,
845 		.no_i2c_bridge = 1,
846 	}, {
847 		.adr = 0x2a,
848 		.no_i2c_bridge = 1,
849 	},
850 };
851 
852 static struct mt2063_config terratec_mt2063_config[] = {
853 	{
854 		.tuner_address = 0x60,
855 	}, {
856 		.tuner_address = 0x67,
857 	},
858 };
859 
860 static const struct tda10071_config hauppauge_tda10071_config = {
861 	.demod_i2c_addr = 0x05,
862 	.tuner_i2c_addr = 0x54,
863 	.i2c_wr_max = 64,
864 	.ts_mode = TDA10071_TS_SERIAL,
865 	.spec_inv = 0,
866 	.xtal = 40444000, /* 40.444 MHz */
867 	.pll_multiplier = 20,
868 };
869 
870 static const struct a8293_config hauppauge_a8293_config = {
871 	.i2c_addr = 0x0b,
872 };
873 
874 static const struct si2165_config hauppauge_hvr4400_si2165_config = {
875 	.i2c_addr	= 0x64,
876 	.chip_mode	= SI2165_MODE_PLL_XTAL,
877 	.ref_freq_Hz	= 16000000,
878 };
879 
880 static const struct m88ds3103_config dvbsky_t9580_m88ds3103_config = {
881 	.i2c_addr = 0x68,
882 	.clock = 27000000,
883 	.i2c_wr_max = 33,
884 	.clock_out = 0,
885 	.ts_mode = M88DS3103_TS_PARALLEL,
886 	.ts_clk = 16000,
887 	.ts_clk_pol = 1,
888 	.lnb_en_pol = 1,
889 	.lnb_hv_pol = 0,
890 	.agc = 0x99,
891 };
892 
893 static const struct m88ds3103_config dvbsky_s950c_m88ds3103_config = {
894 	.i2c_addr = 0x68,
895 	.clock = 27000000,
896 	.i2c_wr_max = 33,
897 	.clock_out = 0,
898 	.ts_mode = M88DS3103_TS_CI,
899 	.ts_clk = 10000,
900 	.ts_clk_pol = 1,
901 	.lnb_en_pol = 1,
902 	.lnb_hv_pol = 0,
903 	.agc = 0x99,
904 };
905 
906 static const struct m88ds3103_config dvbsky_s952_portc_m88ds3103_config = {
907 	.i2c_addr = 0x68,
908 	.clock = 27000000,
909 	.i2c_wr_max = 33,
910 	.clock_out = 0,
911 	.ts_mode = M88DS3103_TS_SERIAL,
912 	.ts_clk = 96000,
913 	.ts_clk_pol = 0,
914 	.lnb_en_pol = 1,
915 	.lnb_hv_pol = 0,
916 	.agc = 0x99,
917 };
918 
919 static const struct m88ds3103_config hauppauge_hvr5525_m88ds3103_config = {
920 	.i2c_addr = 0x69,
921 	.clock = 27000000,
922 	.i2c_wr_max = 33,
923 	.ts_mode = M88DS3103_TS_PARALLEL,
924 	.ts_clk = 16000,
925 	.ts_clk_pol = 1,
926 	.agc = 0x99,
927 };
928 
929 static int netup_altera_fpga_rw(void *device, int flag, int data, int read)
930 {
931 	struct cx23885_dev *dev = (struct cx23885_dev *)device;
932 	unsigned long timeout = jiffies + msecs_to_jiffies(1);
933 	uint32_t mem = 0;
934 
935 	mem = cx_read(MC417_RWD);
936 	if (read)
937 		cx_set(MC417_OEN, ALT_DATA);
938 	else {
939 		cx_clear(MC417_OEN, ALT_DATA);/* D0-D7 out */
940 		mem &= ~ALT_DATA;
941 		mem |= (data & ALT_DATA);
942 	}
943 
944 	if (flag)
945 		mem |= ALT_AD_RG;
946 	else
947 		mem &= ~ALT_AD_RG;
948 
949 	mem &= ~ALT_CS;
950 	if (read)
951 		mem = (mem & ~ALT_RD) | ALT_WR;
952 	else
953 		mem = (mem & ~ALT_WR) | ALT_RD;
954 
955 	cx_write(MC417_RWD, mem);  /* start RW cycle */
956 
957 	for (;;) {
958 		mem = cx_read(MC417_RWD);
959 		if ((mem & ALT_RDY) == 0)
960 			break;
961 		if (time_after(jiffies, timeout))
962 			break;
963 		udelay(1);
964 	}
965 
966 	cx_set(MC417_RWD, ALT_RD | ALT_WR | ALT_CS);
967 	if (read)
968 		return mem & ALT_DATA;
969 
970 	return 0;
971 };
972 
973 static int dib7070_tuner_reset(struct dvb_frontend *fe, int onoff)
974 {
975 	struct dib7000p_ops *dib7000p_ops = fe->sec_priv;
976 
977 	return dib7000p_ops->set_gpio(fe, 8, 0, !onoff);
978 }
979 
980 static int dib7070_tuner_sleep(struct dvb_frontend *fe, int onoff)
981 {
982 	return 0;
983 }
984 
985 static struct dib0070_config dib7070p_dib0070_config = {
986 	.i2c_address = DEFAULT_DIB0070_I2C_ADDRESS,
987 	.reset = dib7070_tuner_reset,
988 	.sleep = dib7070_tuner_sleep,
989 	.clock_khz = 12000,
990 	.freq_offset_khz_vhf = 550,
991 	/* .flip_chip = 1, */
992 };
993 
994 /* DIB7070 generic */
995 static struct dibx000_agc_config dib7070_agc_config = {
996 	.band_caps = BAND_UHF | BAND_VHF | BAND_LBAND | BAND_SBAND,
997 
998 	/*
999 	 * P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=5,
1000 	 * P_agc_inv_pwm1=0, P_agc_inv_pwm2=0, P_agc_inh_dc_rv_est=0,
1001 	 * P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=5, P_agc_write=0
1002 	 */
1003 	.setup = (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) |
1004 		 (0 << 8) | (3 << 5) | (0 << 4) | (5 << 1) | (0 << 0),
1005 	.inv_gain = 600,
1006 	.time_stabiliz = 10,
1007 	.alpha_level = 0,
1008 	.thlock = 118,
1009 	.wbd_inv = 0,
1010 	.wbd_ref = 3530,
1011 	.wbd_sel = 1,
1012 	.wbd_alpha = 5,
1013 	.agc1_max = 65535,
1014 	.agc1_min = 0,
1015 	.agc2_max = 65535,
1016 	.agc2_min = 0,
1017 	.agc1_pt1 = 0,
1018 	.agc1_pt2 = 40,
1019 	.agc1_pt3 = 183,
1020 	.agc1_slope1 = 206,
1021 	.agc1_slope2 = 255,
1022 	.agc2_pt1 = 72,
1023 	.agc2_pt2 = 152,
1024 	.agc2_slope1 = 88,
1025 	.agc2_slope2 = 90,
1026 	.alpha_mant = 17,
1027 	.alpha_exp = 27,
1028 	.beta_mant = 23,
1029 	.beta_exp = 51,
1030 	.perform_agc_softsplit = 0,
1031 };
1032 
1033 static struct dibx000_bandwidth_config dib7070_bw_config_12_mhz = {
1034 	.internal = 60000,
1035 	.sampling = 15000,
1036 	.pll_prediv = 1,
1037 	.pll_ratio = 20,
1038 	.pll_range = 3,
1039 	.pll_reset = 1,
1040 	.pll_bypass = 0,
1041 	.enable_refdiv = 0,
1042 	.bypclk_div = 0,
1043 	.IO_CLK_en_core = 1,
1044 	.ADClkSrc = 1,
1045 	.modulo = 2,
1046 	/* refsel, sel, freq_15k */
1047 	.sad_cfg = (3 << 14) | (1 << 12) | (524 << 0),
1048 	.ifreq = (0 << 25) | 0,
1049 	.timf = 20452225,
1050 	.xtal_hz = 12000000,
1051 };
1052 
1053 static struct dib7000p_config dib7070p_dib7000p_config = {
1054 	/* .output_mode = OUTMODE_MPEG2_FIFO, */
1055 	.output_mode = OUTMODE_MPEG2_SERIAL,
1056 	/* .output_mode = OUTMODE_MPEG2_PAR_GATED_CLK, */
1057 	.output_mpeg2_in_188_bytes = 1,
1058 
1059 	.agc_config_count = 1,
1060 	.agc = &dib7070_agc_config,
1061 	.bw  = &dib7070_bw_config_12_mhz,
1062 	.tuner_is_baseband = 1,
1063 	.spur_protect = 1,
1064 
1065 	.gpio_dir = 0xfcef, /* DIB7000P_GPIO_DEFAULT_DIRECTIONS, */
1066 	.gpio_val = 0x0110, /* DIB7000P_GPIO_DEFAULT_VALUES, */
1067 	.gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
1068 
1069 	.hostbus_diversity = 1,
1070 };
1071 
1072 static int dvb_register_ci_mac(struct cx23885_tsport *port)
1073 {
1074 	struct cx23885_dev *dev = port->dev;
1075 	struct i2c_client *client_ci = NULL;
1076 	struct vb2_dvb_frontend *fe0;
1077 
1078 	fe0 = vb2_dvb_get_frontend(&port->frontends, 1);
1079 	if (!fe0)
1080 		return -EINVAL;
1081 
1082 	switch (dev->board) {
1083 	case CX23885_BOARD_NETUP_DUAL_DVBS2_CI: {
1084 		static struct netup_card_info cinfo;
1085 
1086 		netup_get_card_info(&dev->i2c_bus[0].i2c_adap, &cinfo);
1087 		memcpy(port->frontends.adapter.proposed_mac,
1088 				cinfo.port[port->nr - 1].mac, 6);
1089 		printk(KERN_INFO "NetUP Dual DVB-S2 CI card port%d MAC=%pM\n",
1090 			port->nr, port->frontends.adapter.proposed_mac);
1091 
1092 		netup_ci_init(port);
1093 		return 0;
1094 		}
1095 	case CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF: {
1096 		struct altera_ci_config netup_ci_cfg = {
1097 			.dev = dev,/* magic number to identify*/
1098 			.adapter = &port->frontends.adapter,/* for CI */
1099 			.demux = &fe0->dvb.demux,/* for hw pid filter */
1100 			.fpga_rw = netup_altera_fpga_rw,
1101 		};
1102 
1103 		altera_ci_init(&netup_ci_cfg, port->nr);
1104 		return 0;
1105 		}
1106 	case CX23885_BOARD_TEVII_S470: {
1107 		u8 eeprom[256]; /* 24C02 i2c eeprom */
1108 
1109 		if (port->nr != 1)
1110 			return 0;
1111 
1112 		/* Read entire EEPROM */
1113 		dev->i2c_bus[0].i2c_client.addr = 0xa0 >> 1;
1114 		tveeprom_read(&dev->i2c_bus[0].i2c_client, eeprom, sizeof(eeprom));
1115 		printk(KERN_INFO "TeVii S470 MAC= %pM\n", eeprom + 0xa0);
1116 		memcpy(port->frontends.adapter.proposed_mac, eeprom + 0xa0, 6);
1117 		return 0;
1118 		}
1119 	case CX23885_BOARD_DVBSKY_T9580:
1120 	case CX23885_BOARD_DVBSKY_S950:
1121 	case CX23885_BOARD_DVBSKY_S952:
1122 	case CX23885_BOARD_DVBSKY_T982: {
1123 		u8 eeprom[256]; /* 24C02 i2c eeprom */
1124 
1125 		if (port->nr > 2)
1126 			return 0;
1127 
1128 		/* Read entire EEPROM */
1129 		dev->i2c_bus[0].i2c_client.addr = 0xa0 >> 1;
1130 		tveeprom_read(&dev->i2c_bus[0].i2c_client, eeprom,
1131 				sizeof(eeprom));
1132 		printk(KERN_INFO "%s port %d MAC address: %pM\n",
1133 			cx23885_boards[dev->board].name, port->nr,
1134 			eeprom + 0xc0 + (port->nr-1) * 8);
1135 		memcpy(port->frontends.adapter.proposed_mac, eeprom + 0xc0 +
1136 			(port->nr-1) * 8, 6);
1137 		return 0;
1138 		}
1139 	case CX23885_BOARD_DVBSKY_S950C:
1140 	case CX23885_BOARD_DVBSKY_T980C:
1141 	case CX23885_BOARD_TT_CT2_4500_CI: {
1142 		u8 eeprom[256]; /* 24C02 i2c eeprom */
1143 		struct sp2_config sp2_config;
1144 		struct i2c_board_info info;
1145 		struct cx23885_i2c *i2c_bus2 = &dev->i2c_bus[1];
1146 
1147 		/* attach CI */
1148 		memset(&sp2_config, 0, sizeof(sp2_config));
1149 		sp2_config.dvb_adap = &port->frontends.adapter;
1150 		sp2_config.priv = port;
1151 		sp2_config.ci_control = cx23885_sp2_ci_ctrl;
1152 		memset(&info, 0, sizeof(struct i2c_board_info));
1153 		strlcpy(info.type, "sp2", I2C_NAME_SIZE);
1154 		info.addr = 0x40;
1155 		info.platform_data = &sp2_config;
1156 		request_module(info.type);
1157 		client_ci = i2c_new_device(&i2c_bus2->i2c_adap, &info);
1158 		if (client_ci == NULL || client_ci->dev.driver == NULL)
1159 			return -ENODEV;
1160 		if (!try_module_get(client_ci->dev.driver->owner)) {
1161 			i2c_unregister_device(client_ci);
1162 			return -ENODEV;
1163 		}
1164 		port->i2c_client_ci = client_ci;
1165 
1166 		if (port->nr != 1)
1167 			return 0;
1168 
1169 		/* Read entire EEPROM */
1170 		dev->i2c_bus[0].i2c_client.addr = 0xa0 >> 1;
1171 		tveeprom_read(&dev->i2c_bus[0].i2c_client, eeprom,
1172 				sizeof(eeprom));
1173 		printk(KERN_INFO "%s MAC address: %pM\n",
1174 			cx23885_boards[dev->board].name, eeprom + 0xc0);
1175 		memcpy(port->frontends.adapter.proposed_mac, eeprom + 0xc0, 6);
1176 		return 0;
1177 		}
1178 	}
1179 	return 0;
1180 }
1181 
1182 static int dvb_register(struct cx23885_tsport *port)
1183 {
1184 	struct dib7000p_ops dib7000p_ops;
1185 	struct cx23885_dev *dev = port->dev;
1186 	struct cx23885_i2c *i2c_bus = NULL, *i2c_bus2 = NULL;
1187 	struct vb2_dvb_frontend *fe0, *fe1 = NULL;
1188 	struct si2168_config si2168_config;
1189 	struct si2157_config si2157_config;
1190 	struct m88ts2022_config m88ts2022_config;
1191 	struct i2c_board_info info;
1192 	struct i2c_adapter *adapter;
1193 	struct i2c_client *client_demod = NULL, *client_tuner = NULL;
1194 	const struct m88ds3103_config *p_m88ds3103_config = NULL;
1195 	int (*p_set_voltage)(struct dvb_frontend *fe, fe_sec_voltage_t voltage) = NULL;
1196 	int mfe_shared = 0; /* bus not shared by default */
1197 	int ret;
1198 
1199 	/* Get the first frontend */
1200 	fe0 = vb2_dvb_get_frontend(&port->frontends, 1);
1201 	if (!fe0)
1202 		return -EINVAL;
1203 
1204 	/* init struct vb2_dvb */
1205 	fe0->dvb.name = dev->name;
1206 
1207 	/* multi-frontend gate control is undefined or defaults to fe0 */
1208 	port->frontends.gate = 0;
1209 
1210 	/* Sets the gate control callback to be used by i2c command calls */
1211 	port->gate_ctrl = cx23885_dvb_gate_ctrl;
1212 
1213 	/* init frontend */
1214 	switch (dev->board) {
1215 	case CX23885_BOARD_HAUPPAUGE_HVR1250:
1216 		i2c_bus = &dev->i2c_bus[0];
1217 		fe0->dvb.frontend = dvb_attach(s5h1409_attach,
1218 						&hauppauge_generic_config,
1219 						&i2c_bus->i2c_adap);
1220 		if (fe0->dvb.frontend == NULL)
1221 			break;
1222 		dvb_attach(mt2131_attach, fe0->dvb.frontend,
1223 			   &i2c_bus->i2c_adap,
1224 			   &hauppauge_generic_tunerconfig, 0);
1225 		break;
1226 	case CX23885_BOARD_HAUPPAUGE_HVR1270:
1227 	case CX23885_BOARD_HAUPPAUGE_HVR1275:
1228 		i2c_bus = &dev->i2c_bus[0];
1229 		fe0->dvb.frontend = dvb_attach(lgdt3305_attach,
1230 					       &hauppauge_lgdt3305_config,
1231 					       &i2c_bus->i2c_adap);
1232 		if (fe0->dvb.frontend == NULL)
1233 			break;
1234 		dvb_attach(tda18271_attach, fe0->dvb.frontend,
1235 			   0x60, &dev->i2c_bus[1].i2c_adap,
1236 			   &hauppauge_hvr127x_config);
1237 		if (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1275)
1238 			cx23885_set_frontend_hook(port, fe0->dvb.frontend);
1239 		break;
1240 	case CX23885_BOARD_HAUPPAUGE_HVR1255:
1241 	case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1242 		i2c_bus = &dev->i2c_bus[0];
1243 		fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1244 					       &hcw_s5h1411_config,
1245 					       &i2c_bus->i2c_adap);
1246 		if (fe0->dvb.frontend == NULL)
1247 			break;
1248 
1249 		dvb_attach(tda18271_attach, fe0->dvb.frontend,
1250 			   0x60, &dev->i2c_bus[1].i2c_adap,
1251 			   &hauppauge_tda18271_config);
1252 
1253 		tda18271_attach(&dev->ts1.analog_fe,
1254 			0x60, &dev->i2c_bus[1].i2c_adap,
1255 			&hauppauge_tda18271_config);
1256 
1257 		break;
1258 	case CX23885_BOARD_HAUPPAUGE_HVR1800:
1259 		i2c_bus = &dev->i2c_bus[0];
1260 		switch (alt_tuner) {
1261 		case 1:
1262 			fe0->dvb.frontend =
1263 				dvb_attach(s5h1409_attach,
1264 					   &hauppauge_ezqam_config,
1265 					   &i2c_bus->i2c_adap);
1266 			if (fe0->dvb.frontend == NULL)
1267 				break;
1268 
1269 			dvb_attach(tda829x_attach, fe0->dvb.frontend,
1270 				   &dev->i2c_bus[1].i2c_adap, 0x42,
1271 				   &tda829x_no_probe);
1272 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1273 				   0x60, &dev->i2c_bus[1].i2c_adap,
1274 				   &hauppauge_tda18271_config);
1275 			break;
1276 		case 0:
1277 		default:
1278 			fe0->dvb.frontend =
1279 				dvb_attach(s5h1409_attach,
1280 					   &hauppauge_generic_config,
1281 					   &i2c_bus->i2c_adap);
1282 			if (fe0->dvb.frontend == NULL)
1283 				break;
1284 			dvb_attach(mt2131_attach, fe0->dvb.frontend,
1285 				   &i2c_bus->i2c_adap,
1286 				   &hauppauge_generic_tunerconfig, 0);
1287 		}
1288 		break;
1289 	case CX23885_BOARD_HAUPPAUGE_HVR1800lp:
1290 		i2c_bus = &dev->i2c_bus[0];
1291 		fe0->dvb.frontend = dvb_attach(s5h1409_attach,
1292 						&hauppauge_hvr1800lp_config,
1293 						&i2c_bus->i2c_adap);
1294 		if (fe0->dvb.frontend == NULL)
1295 			break;
1296 		dvb_attach(mt2131_attach, fe0->dvb.frontend,
1297 			   &i2c_bus->i2c_adap,
1298 			   &hauppauge_generic_tunerconfig, 0);
1299 		break;
1300 	case CX23885_BOARD_DVICO_FUSIONHDTV_5_EXP:
1301 		i2c_bus = &dev->i2c_bus[0];
1302 		fe0->dvb.frontend = dvb_attach(lgdt330x_attach,
1303 						&fusionhdtv_5_express,
1304 						&i2c_bus->i2c_adap);
1305 		if (fe0->dvb.frontend == NULL)
1306 			break;
1307 		dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1308 			   &i2c_bus->i2c_adap, 0x61,
1309 			   TUNER_LG_TDVS_H06XF);
1310 		break;
1311 	case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
1312 		i2c_bus = &dev->i2c_bus[1];
1313 		fe0->dvb.frontend = dvb_attach(s5h1409_attach,
1314 						&hauppauge_hvr1500q_config,
1315 						&dev->i2c_bus[0].i2c_adap);
1316 		if (fe0->dvb.frontend == NULL)
1317 			break;
1318 		dvb_attach(xc5000_attach, fe0->dvb.frontend,
1319 			   &i2c_bus->i2c_adap,
1320 			   &hauppauge_hvr1500q_tunerconfig);
1321 		break;
1322 	case CX23885_BOARD_HAUPPAUGE_HVR1500:
1323 		i2c_bus = &dev->i2c_bus[1];
1324 		fe0->dvb.frontend = dvb_attach(s5h1409_attach,
1325 						&hauppauge_hvr1500_config,
1326 						&dev->i2c_bus[0].i2c_adap);
1327 		if (fe0->dvb.frontend != NULL) {
1328 			struct dvb_frontend *fe;
1329 			struct xc2028_config cfg = {
1330 				.i2c_adap  = &i2c_bus->i2c_adap,
1331 				.i2c_addr  = 0x61,
1332 			};
1333 			static struct xc2028_ctrl ctl = {
1334 				.fname       = XC2028_DEFAULT_FIRMWARE,
1335 				.max_len     = 64,
1336 				.demod       = XC3028_FE_OREN538,
1337 			};
1338 
1339 			fe = dvb_attach(xc2028_attach,
1340 					fe0->dvb.frontend, &cfg);
1341 			if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
1342 				fe->ops.tuner_ops.set_config(fe, &ctl);
1343 		}
1344 		break;
1345 	case CX23885_BOARD_HAUPPAUGE_HVR1200:
1346 	case CX23885_BOARD_HAUPPAUGE_HVR1700:
1347 		i2c_bus = &dev->i2c_bus[0];
1348 		fe0->dvb.frontend = dvb_attach(tda10048_attach,
1349 			&hauppauge_hvr1200_config,
1350 			&i2c_bus->i2c_adap);
1351 		if (fe0->dvb.frontend == NULL)
1352 			break;
1353 		dvb_attach(tda829x_attach, fe0->dvb.frontend,
1354 			   &dev->i2c_bus[1].i2c_adap, 0x42,
1355 			   &tda829x_no_probe);
1356 		dvb_attach(tda18271_attach, fe0->dvb.frontend,
1357 			   0x60, &dev->i2c_bus[1].i2c_adap,
1358 			   &hauppauge_hvr1200_tuner_config);
1359 		break;
1360 	case CX23885_BOARD_HAUPPAUGE_HVR1210:
1361 		i2c_bus = &dev->i2c_bus[0];
1362 		fe0->dvb.frontend = dvb_attach(tda10048_attach,
1363 			&hauppauge_hvr1210_config,
1364 			&i2c_bus->i2c_adap);
1365 		if (fe0->dvb.frontend != NULL) {
1366 			dvb_attach(tda18271_attach, fe0->dvb.frontend,
1367 				0x60, &dev->i2c_bus[1].i2c_adap,
1368 				&hauppauge_hvr1210_tuner_config);
1369 		}
1370 		break;
1371 	case CX23885_BOARD_HAUPPAUGE_HVR1400:
1372 		i2c_bus = &dev->i2c_bus[0];
1373 
1374 		if (!dvb_attach(dib7000p_attach, &dib7000p_ops))
1375 			return -ENODEV;
1376 
1377 		fe0->dvb.frontend = dib7000p_ops.init(&i2c_bus->i2c_adap,
1378 			0x12, &hauppauge_hvr1400_dib7000_config);
1379 		if (fe0->dvb.frontend != NULL) {
1380 			struct dvb_frontend *fe;
1381 			struct xc2028_config cfg = {
1382 				.i2c_adap  = &dev->i2c_bus[1].i2c_adap,
1383 				.i2c_addr  = 0x64,
1384 			};
1385 			static struct xc2028_ctrl ctl = {
1386 				.fname   = XC3028L_DEFAULT_FIRMWARE,
1387 				.max_len = 64,
1388 				.demod   = XC3028_FE_DIBCOM52,
1389 				/* This is true for all demods with
1390 					v36 firmware? */
1391 				.type    = XC2028_D2633,
1392 			};
1393 
1394 			fe = dvb_attach(xc2028_attach,
1395 					fe0->dvb.frontend, &cfg);
1396 			if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
1397 				fe->ops.tuner_ops.set_config(fe, &ctl);
1398 		}
1399 		break;
1400 	case CX23885_BOARD_DVICO_FUSIONHDTV_7_DUAL_EXP:
1401 		i2c_bus = &dev->i2c_bus[port->nr - 1];
1402 
1403 		fe0->dvb.frontend = dvb_attach(s5h1409_attach,
1404 						&dvico_s5h1409_config,
1405 						&i2c_bus->i2c_adap);
1406 		if (fe0->dvb.frontend == NULL)
1407 			fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1408 							&dvico_s5h1411_config,
1409 							&i2c_bus->i2c_adap);
1410 		if (fe0->dvb.frontend != NULL)
1411 			dvb_attach(xc5000_attach, fe0->dvb.frontend,
1412 				   &i2c_bus->i2c_adap,
1413 				   &dvico_xc5000_tunerconfig);
1414 		break;
1415 	case CX23885_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL_EXP: {
1416 		i2c_bus = &dev->i2c_bus[port->nr - 1];
1417 
1418 		fe0->dvb.frontend = dvb_attach(zl10353_attach,
1419 					       &dvico_fusionhdtv_xc3028,
1420 					       &i2c_bus->i2c_adap);
1421 		if (fe0->dvb.frontend != NULL) {
1422 			struct dvb_frontend      *fe;
1423 			struct xc2028_config	  cfg = {
1424 				.i2c_adap  = &i2c_bus->i2c_adap,
1425 				.i2c_addr  = 0x61,
1426 			};
1427 			static struct xc2028_ctrl ctl = {
1428 				.fname       = XC2028_DEFAULT_FIRMWARE,
1429 				.max_len     = 64,
1430 				.demod       = XC3028_FE_ZARLINK456,
1431 			};
1432 
1433 			fe = dvb_attach(xc2028_attach, fe0->dvb.frontend,
1434 					&cfg);
1435 			if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
1436 				fe->ops.tuner_ops.set_config(fe, &ctl);
1437 		}
1438 		break;
1439 	}
1440 	case CX23885_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL_EXP2: {
1441 		i2c_bus = &dev->i2c_bus[port->nr - 1];
1442 		/* cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0); */
1443 		/* cxusb_bluebird_gpio_pulse(adap->dev, 0x02, 1); */
1444 
1445 		if (!dvb_attach(dib7000p_attach, &dib7000p_ops))
1446 			return -ENODEV;
1447 
1448 		if (dib7000p_ops.i2c_enumeration(&i2c_bus->i2c_adap, 1, 0x12, &dib7070p_dib7000p_config) < 0) {
1449 			printk(KERN_WARNING "Unable to enumerate dib7000p\n");
1450 			return -ENODEV;
1451 		}
1452 		fe0->dvb.frontend = dib7000p_ops.init(&i2c_bus->i2c_adap, 0x80, &dib7070p_dib7000p_config);
1453 		if (fe0->dvb.frontend != NULL) {
1454 			struct i2c_adapter *tun_i2c;
1455 
1456 			fe0->dvb.frontend->sec_priv = kmalloc(sizeof(dib7000p_ops), GFP_KERNEL);
1457 			memcpy(fe0->dvb.frontend->sec_priv, &dib7000p_ops, sizeof(dib7000p_ops));
1458 			tun_i2c = dib7000p_ops.get_i2c_master(fe0->dvb.frontend, DIBX000_I2C_INTERFACE_TUNER, 1);
1459 			if (!dvb_attach(dib0070_attach, fe0->dvb.frontend, tun_i2c, &dib7070p_dib0070_config))
1460 				return -ENODEV;
1461 		}
1462 		break;
1463 	}
1464 	case CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H:
1465 	case CX23885_BOARD_COMPRO_VIDEOMATE_E650F:
1466 	case CX23885_BOARD_COMPRO_VIDEOMATE_E800:
1467 		i2c_bus = &dev->i2c_bus[0];
1468 
1469 		fe0->dvb.frontend = dvb_attach(zl10353_attach,
1470 			&dvico_fusionhdtv_xc3028,
1471 			&i2c_bus->i2c_adap);
1472 		if (fe0->dvb.frontend != NULL) {
1473 			struct dvb_frontend      *fe;
1474 			struct xc2028_config	  cfg = {
1475 				.i2c_adap  = &dev->i2c_bus[1].i2c_adap,
1476 				.i2c_addr  = 0x61,
1477 			};
1478 			static struct xc2028_ctrl ctl = {
1479 				.fname       = XC2028_DEFAULT_FIRMWARE,
1480 				.max_len     = 64,
1481 				.demod       = XC3028_FE_ZARLINK456,
1482 			};
1483 
1484 			fe = dvb_attach(xc2028_attach, fe0->dvb.frontend,
1485 				&cfg);
1486 			if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
1487 				fe->ops.tuner_ops.set_config(fe, &ctl);
1488 		}
1489 		break;
1490 	case CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H_XC4000:
1491 		i2c_bus = &dev->i2c_bus[0];
1492 
1493 		fe0->dvb.frontend = dvb_attach(zl10353_attach,
1494 					       &dvico_fusionhdtv_xc3028,
1495 					       &i2c_bus->i2c_adap);
1496 		if (fe0->dvb.frontend != NULL) {
1497 			struct dvb_frontend	*fe;
1498 			struct xc4000_config	cfg = {
1499 				.i2c_address	  = 0x61,
1500 				.default_pm	  = 0,
1501 				.dvb_amplitude	  = 134,
1502 				.set_smoothedcvbs = 1,
1503 				.if_khz		  = 4560
1504 			};
1505 
1506 			fe = dvb_attach(xc4000_attach, fe0->dvb.frontend,
1507 					&dev->i2c_bus[1].i2c_adap, &cfg);
1508 			if (!fe) {
1509 				printk(KERN_ERR "%s/2: xc4000 attach failed\n",
1510 				       dev->name);
1511 				goto frontend_detach;
1512 			}
1513 		}
1514 		break;
1515 	case CX23885_BOARD_TBS_6920:
1516 		i2c_bus = &dev->i2c_bus[1];
1517 
1518 		fe0->dvb.frontend = dvb_attach(cx24116_attach,
1519 					&tbs_cx24116_config,
1520 					&i2c_bus->i2c_adap);
1521 		if (fe0->dvb.frontend != NULL)
1522 			fe0->dvb.frontend->ops.set_voltage = f300_set_voltage;
1523 
1524 		break;
1525 	case CX23885_BOARD_TBS_6980:
1526 	case CX23885_BOARD_TBS_6981:
1527 		i2c_bus = &dev->i2c_bus[1];
1528 
1529 		switch (port->nr) {
1530 		/* PORT B */
1531 		case 1:
1532 			fe0->dvb.frontend = dvb_attach(cx24117_attach,
1533 					&tbs_cx24117_config,
1534 					&i2c_bus->i2c_adap);
1535 			break;
1536 		/* PORT C */
1537 		case 2:
1538 			fe0->dvb.frontend = dvb_attach(cx24117_attach,
1539 					&tbs_cx24117_config,
1540 					&i2c_bus->i2c_adap);
1541 			break;
1542 		}
1543 		break;
1544 	case CX23885_BOARD_TEVII_S470:
1545 		i2c_bus = &dev->i2c_bus[1];
1546 
1547 		fe0->dvb.frontend = dvb_attach(ds3000_attach,
1548 					&tevii_ds3000_config,
1549 					&i2c_bus->i2c_adap);
1550 		if (fe0->dvb.frontend != NULL) {
1551 			dvb_attach(ts2020_attach, fe0->dvb.frontend,
1552 				&tevii_ts2020_config, &i2c_bus->i2c_adap);
1553 			fe0->dvb.frontend->ops.set_voltage = f300_set_voltage;
1554 		}
1555 
1556 		break;
1557 	case CX23885_BOARD_DVBWORLD_2005:
1558 		i2c_bus = &dev->i2c_bus[1];
1559 
1560 		fe0->dvb.frontend = dvb_attach(cx24116_attach,
1561 			&dvbworld_cx24116_config,
1562 			&i2c_bus->i2c_adap);
1563 		break;
1564 	case CX23885_BOARD_NETUP_DUAL_DVBS2_CI:
1565 		i2c_bus = &dev->i2c_bus[0];
1566 		switch (port->nr) {
1567 		/* port B */
1568 		case 1:
1569 			fe0->dvb.frontend = dvb_attach(stv0900_attach,
1570 							&netup_stv0900_config,
1571 							&i2c_bus->i2c_adap, 0);
1572 			if (fe0->dvb.frontend != NULL) {
1573 				if (dvb_attach(stv6110_attach,
1574 						fe0->dvb.frontend,
1575 						&netup_stv6110_tunerconfig_a,
1576 						&i2c_bus->i2c_adap)) {
1577 					if (!dvb_attach(lnbh24_attach,
1578 							fe0->dvb.frontend,
1579 							&i2c_bus->i2c_adap,
1580 							LNBH24_PCL | LNBH24_TTX,
1581 							LNBH24_TEN, 0x09))
1582 						printk(KERN_ERR
1583 							"No LNBH24 found!\n");
1584 
1585 				}
1586 			}
1587 			break;
1588 		/* port C */
1589 		case 2:
1590 			fe0->dvb.frontend = dvb_attach(stv0900_attach,
1591 							&netup_stv0900_config,
1592 							&i2c_bus->i2c_adap, 1);
1593 			if (fe0->dvb.frontend != NULL) {
1594 				if (dvb_attach(stv6110_attach,
1595 						fe0->dvb.frontend,
1596 						&netup_stv6110_tunerconfig_b,
1597 						&i2c_bus->i2c_adap)) {
1598 					if (!dvb_attach(lnbh24_attach,
1599 							fe0->dvb.frontend,
1600 							&i2c_bus->i2c_adap,
1601 							LNBH24_PCL | LNBH24_TTX,
1602 							LNBH24_TEN, 0x0a))
1603 						printk(KERN_ERR
1604 							"No LNBH24 found!\n");
1605 
1606 				}
1607 			}
1608 			break;
1609 		}
1610 		break;
1611 	case CX23885_BOARD_MYGICA_X8506:
1612 		i2c_bus = &dev->i2c_bus[0];
1613 		i2c_bus2 = &dev->i2c_bus[1];
1614 		fe0->dvb.frontend = dvb_attach(lgs8gxx_attach,
1615 			&mygica_x8506_lgs8gl5_config,
1616 			&i2c_bus->i2c_adap);
1617 		if (fe0->dvb.frontend == NULL)
1618 			break;
1619 		dvb_attach(xc5000_attach, fe0->dvb.frontend,
1620 			   &i2c_bus2->i2c_adap, &mygica_x8506_xc5000_config);
1621 		cx23885_set_frontend_hook(port, fe0->dvb.frontend);
1622 		break;
1623 	case CX23885_BOARD_MYGICA_X8507:
1624 		i2c_bus = &dev->i2c_bus[0];
1625 		i2c_bus2 = &dev->i2c_bus[1];
1626 		fe0->dvb.frontend = dvb_attach(mb86a20s_attach,
1627 			&mygica_x8507_mb86a20s_config,
1628 			&i2c_bus->i2c_adap);
1629 		if (fe0->dvb.frontend == NULL)
1630 			break;
1631 
1632 		dvb_attach(xc5000_attach, fe0->dvb.frontend,
1633 			   &i2c_bus2->i2c_adap,
1634 			   &mygica_x8507_xc5000_config);
1635 		cx23885_set_frontend_hook(port, fe0->dvb.frontend);
1636 		break;
1637 	case CX23885_BOARD_MAGICPRO_PROHDTVE2:
1638 		i2c_bus = &dev->i2c_bus[0];
1639 		i2c_bus2 = &dev->i2c_bus[1];
1640 		fe0->dvb.frontend = dvb_attach(lgs8gxx_attach,
1641 			&magicpro_prohdtve2_lgs8g75_config,
1642 			&i2c_bus->i2c_adap);
1643 		if (fe0->dvb.frontend == NULL)
1644 			break;
1645 		dvb_attach(xc5000_attach, fe0->dvb.frontend,
1646 			   &i2c_bus2->i2c_adap,
1647 			   &magicpro_prohdtve2_xc5000_config);
1648 		cx23885_set_frontend_hook(port, fe0->dvb.frontend);
1649 		break;
1650 	case CX23885_BOARD_HAUPPAUGE_HVR1850:
1651 		i2c_bus = &dev->i2c_bus[0];
1652 		fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1653 			&hcw_s5h1411_config,
1654 			&i2c_bus->i2c_adap);
1655 		if (fe0->dvb.frontend == NULL)
1656 			break;
1657 		dvb_attach(tda18271_attach, fe0->dvb.frontend,
1658 			   0x60, &dev->i2c_bus[0].i2c_adap,
1659 			   &hauppauge_tda18271_config);
1660 
1661 		tda18271_attach(&dev->ts1.analog_fe,
1662 			0x60, &dev->i2c_bus[1].i2c_adap,
1663 			&hauppauge_tda18271_config);
1664 
1665 		break;
1666 	case CX23885_BOARD_HAUPPAUGE_HVR1290:
1667 		i2c_bus = &dev->i2c_bus[0];
1668 		fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1669 			&hcw_s5h1411_config,
1670 			&i2c_bus->i2c_adap);
1671 		if (fe0->dvb.frontend == NULL)
1672 			break;
1673 		dvb_attach(tda18271_attach, fe0->dvb.frontend,
1674 			   0x60, &dev->i2c_bus[0].i2c_adap,
1675 			   &hauppauge_tda18271_config);
1676 		break;
1677 	case CX23885_BOARD_MYGICA_X8558PRO:
1678 		switch (port->nr) {
1679 		/* port B */
1680 		case 1:
1681 			i2c_bus = &dev->i2c_bus[0];
1682 			fe0->dvb.frontend = dvb_attach(atbm8830_attach,
1683 				&mygica_x8558pro_atbm8830_cfg1,
1684 				&i2c_bus->i2c_adap);
1685 			if (fe0->dvb.frontend == NULL)
1686 				break;
1687 			dvb_attach(max2165_attach, fe0->dvb.frontend,
1688 				   &i2c_bus->i2c_adap,
1689 				   &mygic_x8558pro_max2165_cfg1);
1690 			break;
1691 		/* port C */
1692 		case 2:
1693 			i2c_bus = &dev->i2c_bus[1];
1694 			fe0->dvb.frontend = dvb_attach(atbm8830_attach,
1695 				&mygica_x8558pro_atbm8830_cfg2,
1696 				&i2c_bus->i2c_adap);
1697 			if (fe0->dvb.frontend == NULL)
1698 				break;
1699 			dvb_attach(max2165_attach, fe0->dvb.frontend,
1700 				   &i2c_bus->i2c_adap,
1701 				   &mygic_x8558pro_max2165_cfg2);
1702 		}
1703 		break;
1704 	case CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF:
1705 		i2c_bus = &dev->i2c_bus[0];
1706 		mfe_shared = 1;/* MFE */
1707 		port->frontends.gate = 0;/* not clear for me yet */
1708 		/* ports B, C */
1709 		/* MFE frontend 1 DVB-T */
1710 		fe0->dvb.frontend = dvb_attach(stv0367ter_attach,
1711 					&netup_stv0367_config[port->nr - 1],
1712 					&i2c_bus->i2c_adap);
1713 		if (fe0->dvb.frontend == NULL)
1714 			break;
1715 		if (NULL == dvb_attach(xc5000_attach, fe0->dvb.frontend,
1716 					&i2c_bus->i2c_adap,
1717 					&netup_xc5000_config[port->nr - 1]))
1718 			goto frontend_detach;
1719 		/* load xc5000 firmware */
1720 		fe0->dvb.frontend->ops.tuner_ops.init(fe0->dvb.frontend);
1721 
1722 		/* MFE frontend 2 */
1723 		fe1 = vb2_dvb_get_frontend(&port->frontends, 2);
1724 		if (fe1 == NULL)
1725 			goto frontend_detach;
1726 		/* DVB-C init */
1727 		fe1->dvb.frontend = dvb_attach(stv0367cab_attach,
1728 					&netup_stv0367_config[port->nr - 1],
1729 					&i2c_bus->i2c_adap);
1730 		if (fe1->dvb.frontend == NULL)
1731 			break;
1732 
1733 		fe1->dvb.frontend->id = 1;
1734 		if (NULL == dvb_attach(xc5000_attach,
1735 				       fe1->dvb.frontend,
1736 				       &i2c_bus->i2c_adap,
1737 				       &netup_xc5000_config[port->nr - 1]))
1738 			goto frontend_detach;
1739 		break;
1740 	case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
1741 		i2c_bus = &dev->i2c_bus[0];
1742 		i2c_bus2 = &dev->i2c_bus[1];
1743 
1744 		switch (port->nr) {
1745 		/* port b */
1746 		case 1:
1747 			fe0->dvb.frontend = dvb_attach(drxk_attach,
1748 					&terratec_drxk_config[0],
1749 					&i2c_bus->i2c_adap);
1750 			if (fe0->dvb.frontend == NULL)
1751 				break;
1752 			if (!dvb_attach(mt2063_attach,
1753 					fe0->dvb.frontend,
1754 					&terratec_mt2063_config[0],
1755 					&i2c_bus2->i2c_adap))
1756 				goto frontend_detach;
1757 			break;
1758 		/* port c */
1759 		case 2:
1760 			fe0->dvb.frontend = dvb_attach(drxk_attach,
1761 					&terratec_drxk_config[1],
1762 					&i2c_bus->i2c_adap);
1763 			if (fe0->dvb.frontend == NULL)
1764 				break;
1765 			if (!dvb_attach(mt2063_attach,
1766 					fe0->dvb.frontend,
1767 					&terratec_mt2063_config[1],
1768 					&i2c_bus2->i2c_adap))
1769 				goto frontend_detach;
1770 			break;
1771 		}
1772 		break;
1773 	case CX23885_BOARD_TEVII_S471:
1774 		i2c_bus = &dev->i2c_bus[1];
1775 
1776 		fe0->dvb.frontend = dvb_attach(ds3000_attach,
1777 					&tevii_ds3000_config,
1778 					&i2c_bus->i2c_adap);
1779 		if (fe0->dvb.frontend == NULL)
1780 			break;
1781 		dvb_attach(ts2020_attach, fe0->dvb.frontend,
1782 			   &tevii_ts2020_config, &i2c_bus->i2c_adap);
1783 		break;
1784 	case CX23885_BOARD_PROF_8000:
1785 		i2c_bus = &dev->i2c_bus[0];
1786 
1787 		fe0->dvb.frontend = dvb_attach(stv090x_attach,
1788 						&prof_8000_stv090x_config,
1789 						&i2c_bus->i2c_adap,
1790 						STV090x_DEMODULATOR_0);
1791 		if (fe0->dvb.frontend == NULL)
1792 			break;
1793 		if (!dvb_attach(stb6100_attach,
1794 				fe0->dvb.frontend,
1795 				&prof_8000_stb6100_config,
1796 				&i2c_bus->i2c_adap))
1797 			goto frontend_detach;
1798 
1799 		fe0->dvb.frontend->ops.set_voltage = p8000_set_voltage;
1800 		break;
1801 	case CX23885_BOARD_HAUPPAUGE_HVR4400:
1802 		i2c_bus = &dev->i2c_bus[0];
1803 		i2c_bus2 = &dev->i2c_bus[1];
1804 		switch (port->nr) {
1805 		/* port b */
1806 		case 1:
1807 			fe0->dvb.frontend = dvb_attach(tda10071_attach,
1808 						&hauppauge_tda10071_config,
1809 						&i2c_bus->i2c_adap);
1810 			if (fe0->dvb.frontend == NULL)
1811 				break;
1812 			if (!dvb_attach(a8293_attach, fe0->dvb.frontend,
1813 					&i2c_bus->i2c_adap,
1814 					&hauppauge_a8293_config))
1815 				goto frontend_detach;
1816 			break;
1817 		/* port c */
1818 		case 2:
1819 			fe0->dvb.frontend = dvb_attach(si2165_attach,
1820 					&hauppauge_hvr4400_si2165_config,
1821 					&i2c_bus->i2c_adap);
1822 			if (fe0->dvb.frontend == NULL)
1823 				break;
1824 			fe0->dvb.frontend->ops.i2c_gate_ctrl = NULL;
1825 			if (!dvb_attach(tda18271_attach,
1826 					fe0->dvb.frontend,
1827 					0x60, &i2c_bus2->i2c_adap,
1828 				  &hauppauge_hvr4400_tuner_config))
1829 				goto frontend_detach;
1830 			break;
1831 		}
1832 		break;
1833 	case CX23885_BOARD_HAUPPAUGE_STARBURST:
1834 		i2c_bus = &dev->i2c_bus[0];
1835 		fe0->dvb.frontend = dvb_attach(tda10071_attach,
1836 						&hauppauge_tda10071_config,
1837 						&i2c_bus->i2c_adap);
1838 		if (fe0->dvb.frontend != NULL) {
1839 			dvb_attach(a8293_attach, fe0->dvb.frontend,
1840 				   &i2c_bus->i2c_adap,
1841 				   &hauppauge_a8293_config);
1842 		}
1843 		break;
1844 	case CX23885_BOARD_DVBSKY_T9580:
1845 	case CX23885_BOARD_DVBSKY_S950:
1846 		i2c_bus = &dev->i2c_bus[0];
1847 		i2c_bus2 = &dev->i2c_bus[1];
1848 		switch (port->nr) {
1849 		/* port b - satellite */
1850 		case 1:
1851 			/* attach frontend */
1852 			fe0->dvb.frontend = dvb_attach(m88ds3103_attach,
1853 					&dvbsky_t9580_m88ds3103_config,
1854 					&i2c_bus2->i2c_adap, &adapter);
1855 			if (fe0->dvb.frontend == NULL)
1856 				break;
1857 
1858 			/* attach tuner */
1859 			memset(&m88ts2022_config, 0, sizeof(m88ts2022_config));
1860 			m88ts2022_config.fe = fe0->dvb.frontend;
1861 			m88ts2022_config.clock = 27000000;
1862 			memset(&info, 0, sizeof(struct i2c_board_info));
1863 			strlcpy(info.type, "m88ts2022", I2C_NAME_SIZE);
1864 			info.addr = 0x60;
1865 			info.platform_data = &m88ts2022_config;
1866 			request_module(info.type);
1867 			client_tuner = i2c_new_device(adapter, &info);
1868 			if (client_tuner == NULL ||
1869 					client_tuner->dev.driver == NULL)
1870 				goto frontend_detach;
1871 			if (!try_module_get(client_tuner->dev.driver->owner)) {
1872 				i2c_unregister_device(client_tuner);
1873 				goto frontend_detach;
1874 			}
1875 
1876 			/* delegate signal strength measurement to tuner */
1877 			fe0->dvb.frontend->ops.read_signal_strength =
1878 				fe0->dvb.frontend->ops.tuner_ops.get_rf_strength;
1879 
1880 			/*
1881 			 * for setting the voltage we need to set GPIOs on
1882 			 * the card.
1883 			 */
1884 			port->fe_set_voltage =
1885 				fe0->dvb.frontend->ops.set_voltage;
1886 			fe0->dvb.frontend->ops.set_voltage =
1887 				dvbsky_t9580_set_voltage;
1888 
1889 			port->i2c_client_tuner = client_tuner;
1890 
1891 			break;
1892 		/* port c - terrestrial/cable */
1893 		case 2:
1894 			/* attach frontend */
1895 			memset(&si2168_config, 0, sizeof(si2168_config));
1896 			si2168_config.i2c_adapter = &adapter;
1897 			si2168_config.fe = &fe0->dvb.frontend;
1898 			si2168_config.ts_mode = SI2168_TS_SERIAL;
1899 			memset(&info, 0, sizeof(struct i2c_board_info));
1900 			strlcpy(info.type, "si2168", I2C_NAME_SIZE);
1901 			info.addr = 0x64;
1902 			info.platform_data = &si2168_config;
1903 			request_module(info.type);
1904 			client_demod = i2c_new_device(&i2c_bus->i2c_adap, &info);
1905 			if (client_demod == NULL ||
1906 					client_demod->dev.driver == NULL)
1907 				goto frontend_detach;
1908 			if (!try_module_get(client_demod->dev.driver->owner)) {
1909 				i2c_unregister_device(client_demod);
1910 				goto frontend_detach;
1911 			}
1912 			port->i2c_client_demod = client_demod;
1913 
1914 			/* attach tuner */
1915 			memset(&si2157_config, 0, sizeof(si2157_config));
1916 			si2157_config.fe = fe0->dvb.frontend;
1917 			memset(&info, 0, sizeof(struct i2c_board_info));
1918 			strlcpy(info.type, "si2157", I2C_NAME_SIZE);
1919 			info.addr = 0x60;
1920 			info.platform_data = &si2157_config;
1921 			request_module(info.type);
1922 			client_tuner = i2c_new_device(adapter, &info);
1923 			if (client_tuner == NULL ||
1924 					client_tuner->dev.driver == NULL)
1925 				goto frontend_detach;
1926 
1927 			if (!try_module_get(client_tuner->dev.driver->owner)) {
1928 				i2c_unregister_device(client_tuner);
1929 				goto frontend_detach;
1930 			}
1931 			port->i2c_client_tuner = client_tuner;
1932 			break;
1933 		}
1934 		break;
1935 	case CX23885_BOARD_DVBSKY_T980C:
1936 	case CX23885_BOARD_TT_CT2_4500_CI:
1937 		i2c_bus = &dev->i2c_bus[1];
1938 		i2c_bus2 = &dev->i2c_bus[0];
1939 
1940 		/* attach frontend */
1941 		memset(&si2168_config, 0, sizeof(si2168_config));
1942 		si2168_config.i2c_adapter = &adapter;
1943 		si2168_config.fe = &fe0->dvb.frontend;
1944 		si2168_config.ts_mode = SI2168_TS_PARALLEL;
1945 		memset(&info, 0, sizeof(struct i2c_board_info));
1946 		strlcpy(info.type, "si2168", I2C_NAME_SIZE);
1947 		info.addr = 0x64;
1948 		info.platform_data = &si2168_config;
1949 		request_module(info.type);
1950 		client_demod = i2c_new_device(&i2c_bus->i2c_adap, &info);
1951 		if (client_demod == NULL || client_demod->dev.driver == NULL)
1952 			goto frontend_detach;
1953 		if (!try_module_get(client_demod->dev.driver->owner)) {
1954 			i2c_unregister_device(client_demod);
1955 			goto frontend_detach;
1956 		}
1957 		port->i2c_client_demod = client_demod;
1958 
1959 		/* attach tuner */
1960 		memset(&si2157_config, 0, sizeof(si2157_config));
1961 		si2157_config.fe = fe0->dvb.frontend;
1962 		memset(&info, 0, sizeof(struct i2c_board_info));
1963 		strlcpy(info.type, "si2157", I2C_NAME_SIZE);
1964 		info.addr = 0x60;
1965 		info.platform_data = &si2157_config;
1966 		request_module(info.type);
1967 		client_tuner = i2c_new_device(adapter, &info);
1968 		if (client_tuner == NULL ||
1969 				client_tuner->dev.driver == NULL)
1970 			goto frontend_detach;
1971 		if (!try_module_get(client_tuner->dev.driver->owner)) {
1972 			i2c_unregister_device(client_tuner);
1973 			goto frontend_detach;
1974 		}
1975 		port->i2c_client_tuner = client_tuner;
1976 		break;
1977 	case CX23885_BOARD_DVBSKY_S950C:
1978 		i2c_bus = &dev->i2c_bus[1];
1979 		i2c_bus2 = &dev->i2c_bus[0];
1980 
1981 		/* attach frontend */
1982 		fe0->dvb.frontend = dvb_attach(m88ds3103_attach,
1983 				&dvbsky_s950c_m88ds3103_config,
1984 				&i2c_bus->i2c_adap, &adapter);
1985 		if (fe0->dvb.frontend == NULL)
1986 			break;
1987 
1988 		/* attach tuner */
1989 		memset(&m88ts2022_config, 0, sizeof(m88ts2022_config));
1990 		m88ts2022_config.fe = fe0->dvb.frontend;
1991 		m88ts2022_config.clock = 27000000;
1992 		memset(&info, 0, sizeof(struct i2c_board_info));
1993 		strlcpy(info.type, "m88ts2022", I2C_NAME_SIZE);
1994 		info.addr = 0x60;
1995 		info.platform_data = &m88ts2022_config;
1996 		request_module(info.type);
1997 		client_tuner = i2c_new_device(adapter, &info);
1998 		if (client_tuner == NULL || client_tuner->dev.driver == NULL)
1999 			goto frontend_detach;
2000 		if (!try_module_get(client_tuner->dev.driver->owner)) {
2001 			i2c_unregister_device(client_tuner);
2002 			goto frontend_detach;
2003 		}
2004 
2005 		/* delegate signal strength measurement to tuner */
2006 		fe0->dvb.frontend->ops.read_signal_strength =
2007 			fe0->dvb.frontend->ops.tuner_ops.get_rf_strength;
2008 
2009 		port->i2c_client_tuner = client_tuner;
2010 		break;
2011 	case CX23885_BOARD_DVBSKY_S952:
2012 		switch (port->nr) {
2013 		/* port b */
2014 		case 1:
2015 			i2c_bus = &dev->i2c_bus[1];
2016 			p_m88ds3103_config = &dvbsky_t9580_m88ds3103_config;
2017 			p_set_voltage = dvbsky_t9580_set_voltage;
2018 			break;
2019 		/* port c */
2020 		case 2:
2021 			i2c_bus = &dev->i2c_bus[0];
2022 			p_m88ds3103_config = &dvbsky_s952_portc_m88ds3103_config;
2023 			p_set_voltage = dvbsky_s952_portc_set_voltage;
2024 			break;
2025 		}
2026 
2027 		/* attach frontend */
2028 		fe0->dvb.frontend = dvb_attach(m88ds3103_attach,
2029 				p_m88ds3103_config,
2030 				&i2c_bus->i2c_adap, &adapter);
2031 		if (fe0->dvb.frontend == NULL)
2032 			break;
2033 
2034 		/* attach tuner */
2035 		memset(&m88ts2022_config, 0, sizeof(m88ts2022_config));
2036 		m88ts2022_config.fe = fe0->dvb.frontend;
2037 		m88ts2022_config.clock = 27000000;
2038 		memset(&info, 0, sizeof(struct i2c_board_info));
2039 		strlcpy(info.type, "m88ts2022", I2C_NAME_SIZE);
2040 		info.addr = 0x60;
2041 		info.platform_data = &m88ts2022_config;
2042 		request_module(info.type);
2043 		client_tuner = i2c_new_device(adapter, &info);
2044 		if (client_tuner == NULL || client_tuner->dev.driver == NULL)
2045 			goto frontend_detach;
2046 		if (!try_module_get(client_tuner->dev.driver->owner)) {
2047 			i2c_unregister_device(client_tuner);
2048 			goto frontend_detach;
2049 		}
2050 
2051 		/* delegate signal strength measurement to tuner */
2052 		fe0->dvb.frontend->ops.read_signal_strength =
2053 			fe0->dvb.frontend->ops.tuner_ops.get_rf_strength;
2054 
2055 		/*
2056 		 * for setting the voltage we need to set GPIOs on
2057 		 * the card.
2058 		 */
2059 		port->fe_set_voltage =
2060 			fe0->dvb.frontend->ops.set_voltage;
2061 		fe0->dvb.frontend->ops.set_voltage = p_set_voltage;
2062 
2063 		port->i2c_client_tuner = client_tuner;
2064 		break;
2065 	case CX23885_BOARD_DVBSKY_T982:
2066 		memset(&si2168_config, 0, sizeof(si2168_config));
2067 		switch (port->nr) {
2068 		/* port b */
2069 		case 1:
2070 			i2c_bus = &dev->i2c_bus[1];
2071 			si2168_config.ts_mode = SI2168_TS_PARALLEL;
2072 			break;
2073 		/* port c */
2074 		case 2:
2075 			i2c_bus = &dev->i2c_bus[0];
2076 			si2168_config.ts_mode = SI2168_TS_SERIAL;
2077 			break;
2078 		}
2079 
2080 		/* attach frontend */
2081 		si2168_config.i2c_adapter = &adapter;
2082 		si2168_config.fe = &fe0->dvb.frontend;
2083 		memset(&info, 0, sizeof(struct i2c_board_info));
2084 		strlcpy(info.type, "si2168", I2C_NAME_SIZE);
2085 		info.addr = 0x64;
2086 		info.platform_data = &si2168_config;
2087 		request_module(info.type);
2088 		client_demod = i2c_new_device(&i2c_bus->i2c_adap, &info);
2089 		if (client_demod == NULL || client_demod->dev.driver == NULL)
2090 			goto frontend_detach;
2091 		if (!try_module_get(client_demod->dev.driver->owner)) {
2092 			i2c_unregister_device(client_demod);
2093 			goto frontend_detach;
2094 		}
2095 		port->i2c_client_demod = client_demod;
2096 
2097 		/* attach tuner */
2098 		memset(&si2157_config, 0, sizeof(si2157_config));
2099 		si2157_config.fe = fe0->dvb.frontend;
2100 		memset(&info, 0, sizeof(struct i2c_board_info));
2101 		strlcpy(info.type, "si2157", I2C_NAME_SIZE);
2102 		info.addr = 0x60;
2103 		info.platform_data = &si2157_config;
2104 		request_module(info.type);
2105 		client_tuner = i2c_new_device(adapter, &info);
2106 		if (client_tuner == NULL ||
2107 				client_tuner->dev.driver == NULL)
2108 			goto frontend_detach;
2109 		if (!try_module_get(client_tuner->dev.driver->owner)) {
2110 			i2c_unregister_device(client_tuner);
2111 			goto frontend_detach;
2112 		}
2113 		port->i2c_client_tuner = client_tuner;
2114 		break;
2115 	case CX23885_BOARD_HAUPPAUGE_HVR5525:
2116 		switch (port->nr) {
2117 		struct m88rs6000t_config m88rs6000t_config;
2118 
2119 		/* port b - satellite */
2120 		case 1:
2121 			/* attach frontend */
2122 			fe0->dvb.frontend = dvb_attach(m88ds3103_attach,
2123 					&hauppauge_hvr5525_m88ds3103_config,
2124 					&dev->i2c_bus[0].i2c_adap, &adapter);
2125 			if (fe0->dvb.frontend == NULL)
2126 				break;
2127 
2128 			/* attach SEC */
2129 			if (!dvb_attach(a8293_attach, fe0->dvb.frontend,
2130 					&dev->i2c_bus[0].i2c_adap,
2131 					&hauppauge_a8293_config))
2132 				goto frontend_detach;
2133 
2134 			/* attach tuner */
2135 			memset(&m88rs6000t_config, 0, sizeof(m88rs6000t_config));
2136 			m88rs6000t_config.fe = fe0->dvb.frontend;
2137 			memset(&info, 0, sizeof(struct i2c_board_info));
2138 			strlcpy(info.type, "m88rs6000t", I2C_NAME_SIZE);
2139 			info.addr = 0x21;
2140 			info.platform_data = &m88rs6000t_config;
2141 			request_module("%s", info.type);
2142 			client_tuner = i2c_new_device(adapter, &info);
2143 			if (!client_tuner || !client_tuner->dev.driver)
2144 				goto frontend_detach;
2145 			if (!try_module_get(client_tuner->dev.driver->owner)) {
2146 				i2c_unregister_device(client_tuner);
2147 				goto frontend_detach;
2148 			}
2149 			port->i2c_client_tuner = client_tuner;
2150 
2151 			/* delegate signal strength measurement to tuner */
2152 			fe0->dvb.frontend->ops.read_signal_strength =
2153 				fe0->dvb.frontend->ops.tuner_ops.get_rf_strength;
2154 			break;
2155 		/* port c - terrestrial/cable */
2156 		case 2:
2157 			/* attach frontend */
2158 			memset(&si2168_config, 0, sizeof(si2168_config));
2159 			si2168_config.i2c_adapter = &adapter;
2160 			si2168_config.fe = &fe0->dvb.frontend;
2161 			si2168_config.ts_mode = SI2168_TS_SERIAL;
2162 			memset(&info, 0, sizeof(struct i2c_board_info));
2163 			strlcpy(info.type, "si2168", I2C_NAME_SIZE);
2164 			info.addr = 0x64;
2165 			info.platform_data = &si2168_config;
2166 			request_module("%s", info.type);
2167 			client_demod = i2c_new_device(&dev->i2c_bus[0].i2c_adap, &info);
2168 			if (!client_demod || !client_demod->dev.driver)
2169 				goto frontend_detach;
2170 			if (!try_module_get(client_demod->dev.driver->owner)) {
2171 				i2c_unregister_device(client_demod);
2172 				goto frontend_detach;
2173 			}
2174 			port->i2c_client_demod = client_demod;
2175 
2176 			/* attach tuner */
2177 			memset(&si2157_config, 0, sizeof(si2157_config));
2178 			si2157_config.fe = fe0->dvb.frontend;
2179 			memset(&info, 0, sizeof(struct i2c_board_info));
2180 			strlcpy(info.type, "si2157", I2C_NAME_SIZE);
2181 			info.addr = 0x60;
2182 			info.platform_data = &si2157_config;
2183 			request_module("%s", info.type);
2184 			client_tuner = i2c_new_device(&dev->i2c_bus[1].i2c_adap, &info);
2185 			if (!client_tuner || !client_tuner->dev.driver) {
2186 				module_put(client_demod->dev.driver->owner);
2187 				i2c_unregister_device(client_demod);
2188 				port->i2c_client_demod = NULL;
2189 				goto frontend_detach;
2190 			}
2191 			if (!try_module_get(client_tuner->dev.driver->owner)) {
2192 				i2c_unregister_device(client_tuner);
2193 				module_put(client_demod->dev.driver->owner);
2194 				i2c_unregister_device(client_demod);
2195 				port->i2c_client_demod = NULL;
2196 				goto frontend_detach;
2197 			}
2198 			port->i2c_client_tuner = client_tuner;
2199 			break;
2200 		}
2201 		break;
2202 	default:
2203 		printk(KERN_INFO "%s: The frontend of your DVB/ATSC card "
2204 			" isn't supported yet\n",
2205 		       dev->name);
2206 		break;
2207 	}
2208 
2209 	if ((NULL == fe0->dvb.frontend) || (fe1 && NULL == fe1->dvb.frontend)) {
2210 		printk(KERN_ERR "%s: frontend initialization failed\n",
2211 		       dev->name);
2212 		goto frontend_detach;
2213 	}
2214 
2215 	/* define general-purpose callback pointer */
2216 	fe0->dvb.frontend->callback = cx23885_tuner_callback;
2217 	if (fe1)
2218 		fe1->dvb.frontend->callback = cx23885_tuner_callback;
2219 #if 0
2220 	/* Ensure all frontends negotiate bus access */
2221 	fe0->dvb.frontend->ops.ts_bus_ctrl = cx23885_dvb_bus_ctrl;
2222 	if (fe1)
2223 		fe1->dvb.frontend->ops.ts_bus_ctrl = cx23885_dvb_bus_ctrl;
2224 #endif
2225 
2226 	/* Put the analog decoder in standby to keep it quiet */
2227 	call_all(dev, core, s_power, 0);
2228 
2229 	if (fe0->dvb.frontend->ops.analog_ops.standby)
2230 		fe0->dvb.frontend->ops.analog_ops.standby(fe0->dvb.frontend);
2231 
2232 	/* register everything */
2233 	ret = vb2_dvb_register_bus(&port->frontends, THIS_MODULE, port,
2234 					&dev->pci->dev, adapter_nr, mfe_shared);
2235 	if (ret)
2236 		goto frontend_detach;
2237 
2238 	ret = dvb_register_ci_mac(port);
2239 	if (ret)
2240 		goto frontend_detach;
2241 
2242 	return 0;
2243 
2244 frontend_detach:
2245 	/* remove I2C client for tuner */
2246 	client_tuner = port->i2c_client_tuner;
2247 	if (client_tuner) {
2248 		module_put(client_tuner->dev.driver->owner);
2249 		i2c_unregister_device(client_tuner);
2250 		port->i2c_client_tuner = NULL;
2251 	}
2252 
2253 	/* remove I2C client for demodulator */
2254 	client_demod = port->i2c_client_demod;
2255 	if (client_demod) {
2256 		module_put(client_demod->dev.driver->owner);
2257 		i2c_unregister_device(client_demod);
2258 		port->i2c_client_demod = NULL;
2259 	}
2260 
2261 	port->gate_ctrl = NULL;
2262 	vb2_dvb_dealloc_frontends(&port->frontends);
2263 	return -EINVAL;
2264 }
2265 
2266 int cx23885_dvb_register(struct cx23885_tsport *port)
2267 {
2268 
2269 	struct vb2_dvb_frontend *fe0;
2270 	struct cx23885_dev *dev = port->dev;
2271 	int err, i;
2272 
2273 	/* Here we need to allocate the correct number of frontends,
2274 	 * as reflected in the cards struct. The reality is that currently
2275 	 * no cx23885 boards support this - yet. But, if we don't modify this
2276 	 * code then the second frontend would never be allocated (later)
2277 	 * and fail with error before the attach in dvb_register().
2278 	 * Without these changes we risk an OOPS later. The changes here
2279 	 * are for safety, and should provide a good foundation for the
2280 	 * future addition of any multi-frontend cx23885 based boards.
2281 	 */
2282 	printk(KERN_INFO "%s() allocating %d frontend(s)\n", __func__,
2283 		port->num_frontends);
2284 
2285 	for (i = 1; i <= port->num_frontends; i++) {
2286 		struct vb2_queue *q;
2287 
2288 		if (vb2_dvb_alloc_frontend(
2289 			&port->frontends, i) == NULL) {
2290 			printk(KERN_ERR "%s() failed to alloc\n", __func__);
2291 			return -ENOMEM;
2292 		}
2293 
2294 		fe0 = vb2_dvb_get_frontend(&port->frontends, i);
2295 		if (!fe0)
2296 			return -EINVAL;
2297 
2298 		dprintk(1, "%s\n", __func__);
2299 		dprintk(1, " ->probed by Card=%d Name=%s, PCI %02x:%02x\n",
2300 			dev->board,
2301 			dev->name,
2302 			dev->pci_bus,
2303 			dev->pci_slot);
2304 
2305 		err = -ENODEV;
2306 
2307 		/* dvb stuff */
2308 		/* We have to init the queue for each frontend on a port. */
2309 		printk(KERN_INFO "%s: cx23885 based dvb card\n", dev->name);
2310 		q = &fe0->dvb.dvbq;
2311 		q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2312 		q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
2313 		q->gfp_flags = GFP_DMA32;
2314 		q->min_buffers_needed = 2;
2315 		q->drv_priv = port;
2316 		q->buf_struct_size = sizeof(struct cx23885_buffer);
2317 		q->ops = &dvb_qops;
2318 		q->mem_ops = &vb2_dma_sg_memops;
2319 		q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2320 		q->lock = &dev->lock;
2321 
2322 		err = vb2_queue_init(q);
2323 		if (err < 0)
2324 			return err;
2325 	}
2326 	err = dvb_register(port);
2327 	if (err != 0)
2328 		printk(KERN_ERR "%s() dvb_register failed err = %d\n",
2329 			__func__, err);
2330 
2331 	return err;
2332 }
2333 
2334 int cx23885_dvb_unregister(struct cx23885_tsport *port)
2335 {
2336 	struct vb2_dvb_frontend *fe0;
2337 	struct i2c_client *client;
2338 
2339 	/* remove I2C client for CI */
2340 	client = port->i2c_client_ci;
2341 	if (client) {
2342 		module_put(client->dev.driver->owner);
2343 		i2c_unregister_device(client);
2344 	}
2345 
2346 	/* remove I2C client for tuner */
2347 	client = port->i2c_client_tuner;
2348 	if (client) {
2349 		module_put(client->dev.driver->owner);
2350 		i2c_unregister_device(client);
2351 	}
2352 
2353 	/* remove I2C client for demodulator */
2354 	client = port->i2c_client_demod;
2355 	if (client) {
2356 		module_put(client->dev.driver->owner);
2357 		i2c_unregister_device(client);
2358 	}
2359 
2360 	fe0 = vb2_dvb_get_frontend(&port->frontends, 1);
2361 
2362 	if (fe0 && fe0->dvb.frontend)
2363 		vb2_dvb_unregister_bus(&port->frontends);
2364 
2365 	switch (port->dev->board) {
2366 	case CX23885_BOARD_NETUP_DUAL_DVBS2_CI:
2367 		netup_ci_exit(port);
2368 		break;
2369 	case CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF:
2370 		altera_ci_release(port->dev, port->nr);
2371 		break;
2372 	}
2373 
2374 	port->gate_ctrl = NULL;
2375 
2376 	return 0;
2377 }
2378