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