45784a384300f74ea1832f80fbc599917e040568
[safe/jmp/linux-2.6] / drivers / media / video / 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  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/device.h>
25 #include <linux/fs.h>
26 #include <linux/kthread.h>
27 #include <linux/file.h>
28 #include <linux/suspend.h>
29
30 #include "cx23885.h"
31 #include <media/v4l2-common.h>
32
33 #include "dvb_ca_en50221.h"
34 #include "s5h1409.h"
35 #include "s5h1411.h"
36 #include "mt2131.h"
37 #include "tda8290.h"
38 #include "tda18271.h"
39 #include "lgdt330x.h"
40 #include "xc5000.h"
41 #include "tda10048.h"
42 #include "tuner-xc2028.h"
43 #include "tuner-simple.h"
44 #include "dib7000p.h"
45 #include "dibx000_common.h"
46 #include "zl10353.h"
47 #include "stv0900.h"
48 #include "stv6110.h"
49 #include "lnbh24.h"
50 #include "cx24116.h"
51 #include "cimax2.h"
52 #include "netup-eeprom.h"
53 #include "netup-init.h"
54 #include "lgdt3305.h"
55
56 static unsigned int debug;
57
58 #define dprintk(level, fmt, arg...)\
59         do { if (debug >= level)\
60                 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
61         } while (0)
62
63 /* ------------------------------------------------------------------ */
64
65 static unsigned int alt_tuner;
66 module_param(alt_tuner, int, 0644);
67 MODULE_PARM_DESC(alt_tuner, "Enable alternate tuner configuration");
68
69 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
70
71 /* ------------------------------------------------------------------ */
72
73 static int dvb_buf_setup(struct videobuf_queue *q,
74                          unsigned int *count, unsigned int *size)
75 {
76         struct cx23885_tsport *port = q->priv_data;
77
78         port->ts_packet_size  = 188 * 4;
79         port->ts_packet_count = 32;
80
81         *size  = port->ts_packet_size * port->ts_packet_count;
82         *count = 32;
83         return 0;
84 }
85
86 static int dvb_buf_prepare(struct videobuf_queue *q,
87                            struct videobuf_buffer *vb, enum v4l2_field field)
88 {
89         struct cx23885_tsport *port = q->priv_data;
90         return cx23885_buf_prepare(q, port, (struct cx23885_buffer *)vb, field);
91 }
92
93 static void dvb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
94 {
95         struct cx23885_tsport *port = q->priv_data;
96         cx23885_buf_queue(port, (struct cx23885_buffer *)vb);
97 }
98
99 static void dvb_buf_release(struct videobuf_queue *q,
100                             struct videobuf_buffer *vb)
101 {
102         cx23885_free_buffer(q, (struct cx23885_buffer *)vb);
103 }
104
105 static struct videobuf_queue_ops dvb_qops = {
106         .buf_setup    = dvb_buf_setup,
107         .buf_prepare  = dvb_buf_prepare,
108         .buf_queue    = dvb_buf_queue,
109         .buf_release  = dvb_buf_release,
110 };
111
112 static struct s5h1409_config hauppauge_generic_config = {
113         .demod_address = 0x32 >> 1,
114         .output_mode   = S5H1409_SERIAL_OUTPUT,
115         .gpio          = S5H1409_GPIO_ON,
116         .qam_if        = 44000,
117         .inversion     = S5H1409_INVERSION_OFF,
118         .status_mode   = S5H1409_DEMODLOCKING,
119         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
120 };
121
122 static struct tda10048_config hauppauge_hvr1200_config = {
123         .demod_address    = 0x10 >> 1,
124         .output_mode      = TDA10048_SERIAL_OUTPUT,
125         .fwbulkwritelen   = TDA10048_BULKWRITE_200,
126         .inversion        = TDA10048_INVERSION_ON,
127         .dtv6_if_freq_khz = TDA10048_IF_3300,
128         .dtv7_if_freq_khz = TDA10048_IF_3800,
129         .dtv8_if_freq_khz = TDA10048_IF_4300,
130         .clk_freq_khz     = TDA10048_CLK_16000,
131 };
132
133 static struct tda10048_config hauppauge_hvr1210_config = {
134         .demod_address    = 0x10 >> 1,
135         .output_mode      = TDA10048_SERIAL_OUTPUT,
136         .fwbulkwritelen   = TDA10048_BULKWRITE_200,
137         .inversion        = TDA10048_INVERSION_ON,
138         .dtv6_if_freq_khz = TDA10048_IF_3300,
139         .dtv7_if_freq_khz = TDA10048_IF_3500,
140         .dtv8_if_freq_khz = TDA10048_IF_4000,
141         .clk_freq_khz     = TDA10048_CLK_16000,
142 };
143
144 static struct s5h1409_config hauppauge_ezqam_config = {
145         .demod_address = 0x32 >> 1,
146         .output_mode   = S5H1409_SERIAL_OUTPUT,
147         .gpio          = S5H1409_GPIO_OFF,
148         .qam_if        = 4000,
149         .inversion     = S5H1409_INVERSION_ON,
150         .status_mode   = S5H1409_DEMODLOCKING,
151         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
152 };
153
154 static struct s5h1409_config hauppauge_hvr1800lp_config = {
155         .demod_address = 0x32 >> 1,
156         .output_mode   = S5H1409_SERIAL_OUTPUT,
157         .gpio          = S5H1409_GPIO_OFF,
158         .qam_if        = 44000,
159         .inversion     = S5H1409_INVERSION_OFF,
160         .status_mode   = S5H1409_DEMODLOCKING,
161         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
162 };
163
164 static struct s5h1409_config hauppauge_hvr1500_config = {
165         .demod_address = 0x32 >> 1,
166         .output_mode   = S5H1409_SERIAL_OUTPUT,
167         .gpio          = S5H1409_GPIO_OFF,
168         .inversion     = S5H1409_INVERSION_OFF,
169         .status_mode   = S5H1409_DEMODLOCKING,
170         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
171 };
172
173 static struct mt2131_config hauppauge_generic_tunerconfig = {
174         0x61
175 };
176
177 static struct lgdt330x_config fusionhdtv_5_express = {
178         .demod_address = 0x0e,
179         .demod_chip = LGDT3303,
180         .serial_mpeg = 0x40,
181 };
182
183 static struct s5h1409_config hauppauge_hvr1500q_config = {
184         .demod_address = 0x32 >> 1,
185         .output_mode   = S5H1409_SERIAL_OUTPUT,
186         .gpio          = S5H1409_GPIO_ON,
187         .qam_if        = 44000,
188         .inversion     = S5H1409_INVERSION_OFF,
189         .status_mode   = S5H1409_DEMODLOCKING,
190         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
191 };
192
193 static struct s5h1409_config dvico_s5h1409_config = {
194         .demod_address = 0x32 >> 1,
195         .output_mode   = S5H1409_SERIAL_OUTPUT,
196         .gpio          = S5H1409_GPIO_ON,
197         .qam_if        = 44000,
198         .inversion     = S5H1409_INVERSION_OFF,
199         .status_mode   = S5H1409_DEMODLOCKING,
200         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
201 };
202
203 static struct s5h1411_config dvico_s5h1411_config = {
204         .output_mode   = S5H1411_SERIAL_OUTPUT,
205         .gpio          = S5H1411_GPIO_ON,
206         .qam_if        = S5H1411_IF_44000,
207         .vsb_if        = S5H1411_IF_44000,
208         .inversion     = S5H1411_INVERSION_OFF,
209         .status_mode   = S5H1411_DEMODLOCKING,
210         .mpeg_timing   = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
211 };
212
213 static struct s5h1411_config hcw_s5h1411_config = {
214         .output_mode   = S5H1411_SERIAL_OUTPUT,
215         .gpio          = S5H1411_GPIO_OFF,
216         .vsb_if        = S5H1411_IF_44000,
217         .qam_if        = S5H1411_IF_4000,
218         .inversion     = S5H1411_INVERSION_ON,
219         .status_mode   = S5H1411_DEMODLOCKING,
220         .mpeg_timing   = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
221 };
222
223 static struct xc5000_config hauppauge_hvr1500q_tunerconfig = {
224         .i2c_address      = 0x61,
225         .if_khz           = 5380,
226 };
227
228 static struct xc5000_config dvico_xc5000_tunerconfig = {
229         .i2c_address      = 0x64,
230         .if_khz           = 5380,
231 };
232
233 static struct tda829x_config tda829x_no_probe = {
234         .probe_tuner = TDA829X_DONT_PROBE,
235 };
236
237 static struct tda18271_std_map hauppauge_tda18271_std_map = {
238         .atsc_6   = { .if_freq = 5380, .agc_mode = 3, .std = 3,
239                       .if_lvl = 6, .rfagc_top = 0x37 },
240         .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 0,
241                       .if_lvl = 6, .rfagc_top = 0x37 },
242 };
243
244 static struct tda18271_config hauppauge_tda18271_config = {
245         .std_map = &hauppauge_tda18271_std_map,
246         .gate    = TDA18271_GATE_ANALOG,
247 };
248
249 static struct tda18271_config hauppauge_hvr1200_tuner_config = {
250         .gate    = TDA18271_GATE_ANALOG,
251 };
252
253 static struct tda18271_config hauppauge_hvr1210_tuner_config = {
254         .gate    = TDA18271_GATE_DIGITAL,
255 };
256
257 static struct tda18271_std_map hauppauge_hvr127x_std_map = {
258         .atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 4,
259                       .if_lvl = 1, .rfagc_top = 0x58 },
260         .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 5,
261                       .if_lvl = 1, .rfagc_top = 0x58 },
262 };
263
264 static struct tda18271_config hauppauge_hvr127x_config = {
265         .std_map = &hauppauge_hvr127x_std_map,
266 };
267
268 static struct lgdt3305_config hauppauge_lgdt3305_config = {
269         .i2c_addr           = 0x0e,
270         .mpeg_mode          = LGDT3305_MPEG_SERIAL,
271         .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
272         .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
273         .deny_i2c_rptr      = 1,
274         .spectral_inversion = 1,
275         .qam_if_khz         = 4000,
276         .vsb_if_khz         = 3250,
277 };
278
279 static struct dibx000_agc_config xc3028_agc_config = {
280         BAND_VHF | BAND_UHF,    /* band_caps */
281
282         /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=0,
283          * P_agc_inv_pwm1=0, P_agc_inv_pwm2=0,
284          * P_agc_inh_dc_rv_est=0, P_agc_time_est=3, P_agc_freeze=0,
285          * P_agc_nb_est=2, P_agc_write=0
286          */
287         (0 << 15) | (0 << 14) | (0 << 11) | (0 << 10) | (0 << 9) | (0 << 8) |
288                 (3 << 5) | (0 << 4) | (2 << 1) | (0 << 0), /* setup */
289
290         712,    /* inv_gain */
291         21,     /* time_stabiliz */
292
293         0,      /* alpha_level */
294         118,    /* thlock */
295
296         0,      /* wbd_inv */
297         2867,   /* wbd_ref */
298         0,      /* wbd_sel */
299         2,      /* wbd_alpha */
300
301         0,      /* agc1_max */
302         0,      /* agc1_min */
303         39718,  /* agc2_max */
304         9930,   /* agc2_min */
305         0,      /* agc1_pt1 */
306         0,      /* agc1_pt2 */
307         0,      /* agc1_pt3 */
308         0,      /* agc1_slope1 */
309         0,      /* agc1_slope2 */
310         0,      /* agc2_pt1 */
311         128,    /* agc2_pt2 */
312         29,     /* agc2_slope1 */
313         29,     /* agc2_slope2 */
314
315         17,     /* alpha_mant */
316         27,     /* alpha_exp */
317         23,     /* beta_mant */
318         51,     /* beta_exp */
319
320         1,      /* perform_agc_softsplit */
321 };
322
323 /* PLL Configuration for COFDM BW_MHz = 8.000000
324  * With external clock = 30.000000 */
325 static struct dibx000_bandwidth_config xc3028_bw_config = {
326         60000,  /* internal */
327         30000,  /* sampling */
328         1,      /* pll_cfg: prediv */
329         8,      /* pll_cfg: ratio */
330         3,      /* pll_cfg: range */
331         1,      /* pll_cfg: reset */
332         0,      /* pll_cfg: bypass */
333         0,      /* misc: refdiv */
334         0,      /* misc: bypclk_div */
335         1,      /* misc: IO_CLK_en_core */
336         1,      /* misc: ADClkSrc */
337         0,      /* misc: modulo */
338         (3 << 14) | (1 << 12) | (524 << 0), /* sad_cfg: refsel, sel, freq_15k */
339         (1 << 25) | 5816102, /* ifreq = 5.200000 MHz */
340         20452225, /* timf */
341         30000000  /* xtal_hz */
342 };
343
344 static struct dib7000p_config hauppauge_hvr1400_dib7000_config = {
345         .output_mpeg2_in_188_bytes = 1,
346         .hostbus_diversity = 1,
347         .tuner_is_baseband = 0,
348         .update_lna  = NULL,
349
350         .agc_config_count = 1,
351         .agc = &xc3028_agc_config,
352         .bw  = &xc3028_bw_config,
353
354         .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
355         .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
356         .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
357
358         .pwm_freq_div = 0,
359         .agc_control  = NULL,
360         .spur_protect = 0,
361
362         .output_mode = OUTMODE_MPEG2_SERIAL,
363 };
364
365 static struct zl10353_config dvico_fusionhdtv_xc3028 = {
366         .demod_address = 0x0f,
367         .if2           = 45600,
368         .no_tuner      = 1,
369         .disable_i2c_gate_ctrl = 1,
370 };
371
372 static struct stv0900_config netup_stv0900_config = {
373         .demod_address = 0x68,
374         .xtal = 27000000,
375         .clkmode = 3,/* 0-CLKI, 2-XTALI, else AUTO */
376         .diseqc_mode = 2,/* 2/3 PWM */
377         .path1_mode = 2,/*Serial continues clock */
378         .path2_mode = 2,/*Serial continues clock */
379         .tun1_maddress = 0,/* 0x60 */
380         .tun2_maddress = 3,/* 0x63 */
381         .tun1_adc = 1,/* 1 Vpp */
382         .tun2_adc = 1,/* 1 Vpp */
383 };
384
385 static struct stv6110_config netup_stv6110_tunerconfig_a = {
386         .i2c_address = 0x60,
387         .mclk = 27000000,
388         .iq_wiring = 0,
389 };
390
391 static struct stv6110_config netup_stv6110_tunerconfig_b = {
392         .i2c_address = 0x63,
393         .mclk = 27000000,
394         .iq_wiring = 1,
395 };
396
397 static int tbs_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
398 {
399         struct cx23885_tsport *port = fe->dvb->priv;
400         struct cx23885_dev *dev = port->dev;
401
402         if (voltage == SEC_VOLTAGE_18)
403                 cx_write(MC417_RWD, 0x00001e00);/* GPIO-13 high */
404         else if (voltage == SEC_VOLTAGE_13)
405                 cx_write(MC417_RWD, 0x00001a00);/* GPIO-13 low */
406         else
407                 cx_write(MC417_RWD, 0x00001800);/* GPIO-12 low */
408         return 0;
409 }
410
411 static struct cx24116_config tbs_cx24116_config = {
412         .demod_address = 0x05,
413 };
414
415 static struct cx24116_config tevii_cx24116_config = {
416         .demod_address = 0x55,
417 };
418
419 static struct cx24116_config dvbworld_cx24116_config = {
420         .demod_address = 0x05,
421 };
422
423 static int dvb_register(struct cx23885_tsport *port)
424 {
425         struct cx23885_dev *dev = port->dev;
426         struct cx23885_i2c *i2c_bus = NULL;
427         struct videobuf_dvb_frontend *fe0;
428         int ret;
429
430         /* Get the first frontend */
431         fe0 = videobuf_dvb_get_frontend(&port->frontends, 1);
432         if (!fe0)
433                 return -EINVAL;
434
435         /* init struct videobuf_dvb */
436         fe0->dvb.name = dev->name;
437
438         /* init frontend */
439         switch (dev->board) {
440         case CX23885_BOARD_HAUPPAUGE_HVR1250:
441                 i2c_bus = &dev->i2c_bus[0];
442                 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
443                                                 &hauppauge_generic_config,
444                                                 &i2c_bus->i2c_adap);
445                 if (fe0->dvb.frontend != NULL) {
446                         dvb_attach(mt2131_attach, fe0->dvb.frontend,
447                                    &i2c_bus->i2c_adap,
448                                    &hauppauge_generic_tunerconfig, 0);
449                 }
450                 break;
451         case CX23885_BOARD_HAUPPAUGE_HVR1270:
452         case CX23885_BOARD_HAUPPAUGE_HVR1275:
453                 i2c_bus = &dev->i2c_bus[0];
454                 fe0->dvb.frontend = dvb_attach(lgdt3305_attach,
455                                                &hauppauge_lgdt3305_config,
456                                                &i2c_bus->i2c_adap);
457                 if (fe0->dvb.frontend != NULL) {
458                         dvb_attach(tda18271_attach, fe0->dvb.frontend,
459                                    0x60, &dev->i2c_bus[1].i2c_adap,
460                                    &hauppauge_hvr127x_config);
461                 }
462                 break;
463         case CX23885_BOARD_HAUPPAUGE_HVR1255:
464                 i2c_bus = &dev->i2c_bus[0];
465                 fe0->dvb.frontend = dvb_attach(s5h1411_attach,
466                                                &hcw_s5h1411_config,
467                                                &i2c_bus->i2c_adap);
468                 if (fe0->dvb.frontend != NULL) {
469                         dvb_attach(tda18271_attach, fe0->dvb.frontend,
470                                    0x60, &dev->i2c_bus[1].i2c_adap,
471                                    &hauppauge_tda18271_config);
472                 }
473                 break;
474         case CX23885_BOARD_HAUPPAUGE_HVR1800:
475                 i2c_bus = &dev->i2c_bus[0];
476                 switch (alt_tuner) {
477                 case 1:
478                         fe0->dvb.frontend =
479                                 dvb_attach(s5h1409_attach,
480                                            &hauppauge_ezqam_config,
481                                            &i2c_bus->i2c_adap);
482                         if (fe0->dvb.frontend != NULL) {
483                                 dvb_attach(tda829x_attach, fe0->dvb.frontend,
484                                            &dev->i2c_bus[1].i2c_adap, 0x42,
485                                            &tda829x_no_probe);
486                                 dvb_attach(tda18271_attach, fe0->dvb.frontend,
487                                            0x60, &dev->i2c_bus[1].i2c_adap,
488                                            &hauppauge_tda18271_config);
489                         }
490                         break;
491                 case 0:
492                 default:
493                         fe0->dvb.frontend =
494                                 dvb_attach(s5h1409_attach,
495                                            &hauppauge_generic_config,
496                                            &i2c_bus->i2c_adap);
497                         if (fe0->dvb.frontend != NULL)
498                                 dvb_attach(mt2131_attach, fe0->dvb.frontend,
499                                            &i2c_bus->i2c_adap,
500                                            &hauppauge_generic_tunerconfig, 0);
501                         break;
502                 }
503                 break;
504         case CX23885_BOARD_HAUPPAUGE_HVR1800lp:
505                 i2c_bus = &dev->i2c_bus[0];
506                 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
507                                                 &hauppauge_hvr1800lp_config,
508                                                 &i2c_bus->i2c_adap);
509                 if (fe0->dvb.frontend != NULL) {
510                         dvb_attach(mt2131_attach, fe0->dvb.frontend,
511                                    &i2c_bus->i2c_adap,
512                                    &hauppauge_generic_tunerconfig, 0);
513                 }
514                 break;
515         case CX23885_BOARD_DVICO_FUSIONHDTV_5_EXP:
516                 i2c_bus = &dev->i2c_bus[0];
517                 fe0->dvb.frontend = dvb_attach(lgdt330x_attach,
518                                                 &fusionhdtv_5_express,
519                                                 &i2c_bus->i2c_adap);
520                 if (fe0->dvb.frontend != NULL) {
521                         dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
522                                    &i2c_bus->i2c_adap, 0x61,
523                                    TUNER_LG_TDVS_H06XF);
524                 }
525                 break;
526         case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
527                 i2c_bus = &dev->i2c_bus[1];
528                 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
529                                                 &hauppauge_hvr1500q_config,
530                                                 &dev->i2c_bus[0].i2c_adap);
531                 if (fe0->dvb.frontend != NULL)
532                         dvb_attach(xc5000_attach, fe0->dvb.frontend,
533                                    &i2c_bus->i2c_adap,
534                                    &hauppauge_hvr1500q_tunerconfig);
535                 break;
536         case CX23885_BOARD_HAUPPAUGE_HVR1500:
537                 i2c_bus = &dev->i2c_bus[1];
538                 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
539                                                 &hauppauge_hvr1500_config,
540                                                 &dev->i2c_bus[0].i2c_adap);
541                 if (fe0->dvb.frontend != NULL) {
542                         struct dvb_frontend *fe;
543                         struct xc2028_config cfg = {
544                                 .i2c_adap  = &i2c_bus->i2c_adap,
545                                 .i2c_addr  = 0x61,
546                         };
547                         static struct xc2028_ctrl ctl = {
548                                 .fname       = XC2028_DEFAULT_FIRMWARE,
549                                 .max_len     = 64,
550                                 .demod       = XC3028_FE_OREN538,
551                         };
552
553                         fe = dvb_attach(xc2028_attach,
554                                         fe0->dvb.frontend, &cfg);
555                         if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
556                                 fe->ops.tuner_ops.set_config(fe, &ctl);
557                 }
558                 break;
559         case CX23885_BOARD_HAUPPAUGE_HVR1200:
560         case CX23885_BOARD_HAUPPAUGE_HVR1700:
561                 i2c_bus = &dev->i2c_bus[0];
562                 fe0->dvb.frontend = dvb_attach(tda10048_attach,
563                         &hauppauge_hvr1200_config,
564                         &i2c_bus->i2c_adap);
565                 if (fe0->dvb.frontend != NULL) {
566                         dvb_attach(tda829x_attach, fe0->dvb.frontend,
567                                 &dev->i2c_bus[1].i2c_adap, 0x42,
568                                 &tda829x_no_probe);
569                         dvb_attach(tda18271_attach, fe0->dvb.frontend,
570                                 0x60, &dev->i2c_bus[1].i2c_adap,
571                                 &hauppauge_hvr1200_tuner_config);
572                 }
573                 break;
574         case CX23885_BOARD_HAUPPAUGE_HVR1210:
575                 i2c_bus = &dev->i2c_bus[0];
576                 fe0->dvb.frontend = dvb_attach(tda10048_attach,
577                         &hauppauge_hvr1210_config,
578                         &i2c_bus->i2c_adap);
579                 if (fe0->dvb.frontend != NULL) {
580                         dvb_attach(tda18271_attach, fe0->dvb.frontend,
581                                 0x60, &dev->i2c_bus[1].i2c_adap,
582                                 &hauppauge_hvr1210_tuner_config);
583                 }
584                 break;
585         case CX23885_BOARD_HAUPPAUGE_HVR1400:
586                 i2c_bus = &dev->i2c_bus[0];
587                 fe0->dvb.frontend = dvb_attach(dib7000p_attach,
588                         &i2c_bus->i2c_adap,
589                         0x12, &hauppauge_hvr1400_dib7000_config);
590                 if (fe0->dvb.frontend != NULL) {
591                         struct dvb_frontend *fe;
592                         struct xc2028_config cfg = {
593                                 .i2c_adap  = &dev->i2c_bus[1].i2c_adap,
594                                 .i2c_addr  = 0x64,
595                         };
596                         static struct xc2028_ctrl ctl = {
597                                 .fname   = XC3028L_DEFAULT_FIRMWARE,
598                                 .max_len = 64,
599                                 .demod   = 5000,
600                                 /* This is true for all demods with
601                                         v36 firmware? */
602                                 .type    = XC2028_D2633,
603                         };
604
605                         fe = dvb_attach(xc2028_attach,
606                                         fe0->dvb.frontend, &cfg);
607                         if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
608                                 fe->ops.tuner_ops.set_config(fe, &ctl);
609                 }
610                 break;
611         case CX23885_BOARD_DVICO_FUSIONHDTV_7_DUAL_EXP:
612                 i2c_bus = &dev->i2c_bus[port->nr - 1];
613
614                 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
615                                                 &dvico_s5h1409_config,
616                                                 &i2c_bus->i2c_adap);
617                 if (fe0->dvb.frontend == NULL)
618                         fe0->dvb.frontend = dvb_attach(s5h1411_attach,
619                                                         &dvico_s5h1411_config,
620                                                         &i2c_bus->i2c_adap);
621                 if (fe0->dvb.frontend != NULL)
622                         dvb_attach(xc5000_attach, fe0->dvb.frontend,
623                                    &i2c_bus->i2c_adap,
624                                    &dvico_xc5000_tunerconfig);
625                 break;
626         case CX23885_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL_EXP: {
627                 i2c_bus = &dev->i2c_bus[port->nr - 1];
628
629                 fe0->dvb.frontend = dvb_attach(zl10353_attach,
630                                                &dvico_fusionhdtv_xc3028,
631                                                &i2c_bus->i2c_adap);
632                 if (fe0->dvb.frontend != NULL) {
633                         struct dvb_frontend      *fe;
634                         struct xc2028_config      cfg = {
635                                 .i2c_adap  = &i2c_bus->i2c_adap,
636                                 .i2c_addr  = 0x61,
637                         };
638                         static struct xc2028_ctrl ctl = {
639                                 .fname       = XC2028_DEFAULT_FIRMWARE,
640                                 .max_len     = 64,
641                                 .demod       = XC3028_FE_ZARLINK456,
642                         };
643
644                         fe = dvb_attach(xc2028_attach, fe0->dvb.frontend,
645                                         &cfg);
646                         if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
647                                 fe->ops.tuner_ops.set_config(fe, &ctl);
648                 }
649                 break;
650         }
651         case CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H:
652         case CX23885_BOARD_COMPRO_VIDEOMATE_E650F:
653                 i2c_bus = &dev->i2c_bus[0];
654
655                 fe0->dvb.frontend = dvb_attach(zl10353_attach,
656                         &dvico_fusionhdtv_xc3028,
657                         &i2c_bus->i2c_adap);
658                 if (fe0->dvb.frontend != NULL) {
659                         struct dvb_frontend      *fe;
660                         struct xc2028_config      cfg = {
661                                 .i2c_adap  = &dev->i2c_bus[1].i2c_adap,
662                                 .i2c_addr  = 0x61,
663                         };
664                         static struct xc2028_ctrl ctl = {
665                                 .fname       = XC2028_DEFAULT_FIRMWARE,
666                                 .max_len     = 64,
667                                 .demod       = XC3028_FE_ZARLINK456,
668                         };
669
670                         fe = dvb_attach(xc2028_attach, fe0->dvb.frontend,
671                                 &cfg);
672                         if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
673                                 fe->ops.tuner_ops.set_config(fe, &ctl);
674                 }
675                 break;
676         case CX23885_BOARD_TBS_6920:
677                 i2c_bus = &dev->i2c_bus[0];
678
679                 fe0->dvb.frontend = dvb_attach(cx24116_attach,
680                         &tbs_cx24116_config,
681                         &i2c_bus->i2c_adap);
682                 if (fe0->dvb.frontend != NULL)
683                         fe0->dvb.frontend->ops.set_voltage = tbs_set_voltage;
684
685                 break;
686         case CX23885_BOARD_TEVII_S470:
687                 i2c_bus = &dev->i2c_bus[1];
688
689                 fe0->dvb.frontend = dvb_attach(cx24116_attach,
690                         &tevii_cx24116_config,
691                         &i2c_bus->i2c_adap);
692                 if (fe0->dvb.frontend != NULL)
693                         fe0->dvb.frontend->ops.set_voltage = tbs_set_voltage;
694
695                 break;
696         case CX23885_BOARD_DVBWORLD_2005:
697                 i2c_bus = &dev->i2c_bus[1];
698
699                 fe0->dvb.frontend = dvb_attach(cx24116_attach,
700                         &dvbworld_cx24116_config,
701                         &i2c_bus->i2c_adap);
702                 break;
703         case CX23885_BOARD_NETUP_DUAL_DVBS2_CI:
704                 i2c_bus = &dev->i2c_bus[0];
705                 switch (port->nr) {
706                 /* port B */
707                 case 1:
708                         fe0->dvb.frontend = dvb_attach(stv0900_attach,
709                                                         &netup_stv0900_config,
710                                                         &i2c_bus->i2c_adap, 0);
711                         if (fe0->dvb.frontend != NULL) {
712                                 if (dvb_attach(stv6110_attach,
713                                                 fe0->dvb.frontend,
714                                                 &netup_stv6110_tunerconfig_a,
715                                                 &i2c_bus->i2c_adap)) {
716                                         if (!dvb_attach(lnbh24_attach,
717                                                         fe0->dvb.frontend,
718                                                         &i2c_bus->i2c_adap,
719                                                         LNBH24_PCL, 0, 0x09))
720                                                 printk(KERN_ERR
721                                                         "No LNBH24 found!\n");
722
723                                 }
724                         }
725                         break;
726                 /* port C */
727                 case 2:
728                         fe0->dvb.frontend = dvb_attach(stv0900_attach,
729                                                         &netup_stv0900_config,
730                                                         &i2c_bus->i2c_adap, 1);
731                         if (fe0->dvb.frontend != NULL) {
732                                 if (dvb_attach(stv6110_attach,
733                                                 fe0->dvb.frontend,
734                                                 &netup_stv6110_tunerconfig_b,
735                                                 &i2c_bus->i2c_adap)) {
736                                         if (!dvb_attach(lnbh24_attach,
737                                                         fe0->dvb.frontend,
738                                                         &i2c_bus->i2c_adap,
739                                                         LNBH24_PCL, 0, 0x0a))
740                                                 printk(KERN_ERR
741                                                         "No LNBH24 found!\n");
742
743                                 }
744                         }
745                         break;
746                 }
747                 break;
748         default:
749                 printk(KERN_INFO "%s: The frontend of your DVB/ATSC card "
750                         " isn't supported yet\n",
751                        dev->name);
752                 break;
753         }
754         if (NULL == fe0->dvb.frontend) {
755                 printk(KERN_ERR "%s: frontend initialization failed\n",
756                         dev->name);
757                 return -1;
758         }
759         /* define general-purpose callback pointer */
760         fe0->dvb.frontend->callback = cx23885_tuner_callback;
761
762         /* Put the analog decoder in standby to keep it quiet */
763         call_all(dev, tuner, s_standby);
764
765         if (fe0->dvb.frontend->ops.analog_ops.standby)
766                 fe0->dvb.frontend->ops.analog_ops.standby(fe0->dvb.frontend);
767
768         /* register everything */
769         ret = videobuf_dvb_register_bus(&port->frontends, THIS_MODULE, port,
770                 &dev->pci->dev, adapter_nr, 0);
771
772         /* init CI & MAC */
773         switch (dev->board) {
774         case CX23885_BOARD_NETUP_DUAL_DVBS2_CI: {
775                 static struct netup_card_info cinfo;
776
777                 netup_get_card_info(&dev->i2c_bus[0].i2c_adap, &cinfo);
778                 memcpy(port->frontends.adapter.proposed_mac,
779                                 cinfo.port[port->nr - 1].mac, 6);
780                 printk(KERN_INFO "NetUP Dual DVB-S2 CI card port%d MAC="
781                         "%02X:%02X:%02X:%02X:%02X:%02X\n",
782                         port->nr,
783                         port->frontends.adapter.proposed_mac[0],
784                         port->frontends.adapter.proposed_mac[1],
785                         port->frontends.adapter.proposed_mac[2],
786                         port->frontends.adapter.proposed_mac[3],
787                         port->frontends.adapter.proposed_mac[4],
788                         port->frontends.adapter.proposed_mac[5]);
789
790                 netup_ci_init(port);
791                 break;
792                 }
793         }
794
795         return ret;
796 }
797
798 int cx23885_dvb_register(struct cx23885_tsport *port)
799 {
800
801         struct videobuf_dvb_frontend *fe0;
802         struct cx23885_dev *dev = port->dev;
803         int err, i;
804
805         /* Here we need to allocate the correct number of frontends,
806          * as reflected in the cards struct. The reality is that currrently
807          * no cx23885 boards support this - yet. But, if we don't modify this
808          * code then the second frontend would never be allocated (later)
809          * and fail with error before the attach in dvb_register().
810          * Without these changes we risk an OOPS later. The changes here
811          * are for safety, and should provide a good foundation for the
812          * future addition of any multi-frontend cx23885 based boards.
813          */
814         printk(KERN_INFO "%s() allocating %d frontend(s)\n", __func__,
815                 port->num_frontends);
816
817         for (i = 1; i <= port->num_frontends; i++) {
818                 if (videobuf_dvb_alloc_frontend(
819                         &port->frontends, i) == NULL) {
820                         printk(KERN_ERR "%s() failed to alloc\n", __func__);
821                         return -ENOMEM;
822                 }
823
824                 fe0 = videobuf_dvb_get_frontend(&port->frontends, i);
825                 if (!fe0)
826                         err = -EINVAL;
827
828                 dprintk(1, "%s\n", __func__);
829                 dprintk(1, " ->probed by Card=%d Name=%s, PCI %02x:%02x\n",
830                         dev->board,
831                         dev->name,
832                         dev->pci_bus,
833                         dev->pci_slot);
834
835                 err = -ENODEV;
836
837                 /* dvb stuff */
838                 /* We have to init the queue for each frontend on a port. */
839                 printk(KERN_INFO "%s: cx23885 based dvb card\n", dev->name);
840                 videobuf_queue_sg_init(&fe0->dvb.dvbq, &dvb_qops,
841                             &dev->pci->dev, &port->slock,
842                             V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_TOP,
843                             sizeof(struct cx23885_buffer), port);
844         }
845         err = dvb_register(port);
846         if (err != 0)
847                 printk(KERN_ERR "%s() dvb_register failed err = %d\n",
848                         __func__, err);
849
850         return err;
851 }
852
853 int cx23885_dvb_unregister(struct cx23885_tsport *port)
854 {
855         struct videobuf_dvb_frontend *fe0;
856
857         /* FIXME: in an error condition where the we have
858          * an expected number of frontends (attach problem)
859          * then this might not clean up correctly, if 1
860          * is invalid.
861          * This comment only applies to future boards IF they
862          * implement MFE support.
863          */
864         fe0 = videobuf_dvb_get_frontend(&port->frontends, 1);
865         if (fe0->dvb.frontend)
866                 videobuf_dvb_unregister_bus(&port->frontends);
867
868         switch (port->dev->board) {
869         case CX23885_BOARD_NETUP_DUAL_DVBS2_CI:
870                 netup_ci_exit(port);
871                 break;
872         }
873
874         return 0;
875 }
876