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