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