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