V4L/DVB (7538): Adds selectable adapter numbers as per module option
[linux-2.6.git] / drivers / media / video / cx88 / cx88-dvb.c
1 /*
2  *
3  * device driver for Conexant 2388x based TV cards
4  * MPEG Transport Stream (DVB) routines
5  *
6  * (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
7  * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/device.h>
27 #include <linux/fs.h>
28 #include <linux/kthread.h>
29 #include <linux/file.h>
30 #include <linux/suspend.h>
31
32 #include "cx88.h"
33 #include "dvb-pll.h"
34 #include <media/v4l2-common.h>
35
36 #include "mt352.h"
37 #include "mt352_priv.h"
38 #include "cx88-vp3054-i2c.h"
39 #include "zl10353.h"
40 #include "cx22702.h"
41 #include "or51132.h"
42 #include "lgdt330x.h"
43 #include "s5h1409.h"
44 #include "xc5000.h"
45 #include "nxt200x.h"
46 #include "cx24123.h"
47 #include "isl6421.h"
48 #include "tuner-xc2028-types.h"
49 #include "tuner-simple.h"
50 #include "tda9887.h"
51
52 MODULE_DESCRIPTION("driver for cx2388x based DVB cards");
53 MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
54 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
55 MODULE_LICENSE("GPL");
56
57 static unsigned int debug;
58 module_param(debug, int, 0644);
59 MODULE_PARM_DESC(debug,"enable debug messages [dvb]");
60
61 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
62
63 #define dprintk(level,fmt, arg...)      if (debug >= level) \
64         printk(KERN_DEBUG "%s/2-dvb: " fmt, core->name, ## arg)
65
66 /* ------------------------------------------------------------------ */
67
68 static int dvb_buf_setup(struct videobuf_queue *q,
69                          unsigned int *count, unsigned int *size)
70 {
71         struct cx8802_dev *dev = q->priv_data;
72
73         dev->ts_packet_size  = 188 * 4;
74         dev->ts_packet_count = 32;
75
76         *size  = dev->ts_packet_size * dev->ts_packet_count;
77         *count = 32;
78         return 0;
79 }
80
81 static int dvb_buf_prepare(struct videobuf_queue *q,
82                            struct videobuf_buffer *vb, enum v4l2_field field)
83 {
84         struct cx8802_dev *dev = q->priv_data;
85         return cx8802_buf_prepare(q, dev, (struct cx88_buffer*)vb,field);
86 }
87
88 static void dvb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
89 {
90         struct cx8802_dev *dev = q->priv_data;
91         cx8802_buf_queue(dev, (struct cx88_buffer*)vb);
92 }
93
94 static void dvb_buf_release(struct videobuf_queue *q,
95                             struct videobuf_buffer *vb)
96 {
97         cx88_free_buffer(q, (struct cx88_buffer*)vb);
98 }
99
100 static struct videobuf_queue_ops dvb_qops = {
101         .buf_setup    = dvb_buf_setup,
102         .buf_prepare  = dvb_buf_prepare,
103         .buf_queue    = dvb_buf_queue,
104         .buf_release  = dvb_buf_release,
105 };
106
107 /* ------------------------------------------------------------------ */
108
109 static int cx88_dvb_bus_ctrl(struct dvb_frontend* fe, int acquire)
110 {
111         struct cx8802_dev *dev= fe->dvb->priv;
112         struct cx8802_driver *drv = NULL;
113         int ret = 0;
114
115         drv = cx8802_get_driver(dev, CX88_MPEG_DVB);
116         if (drv) {
117                 if (acquire)
118                         ret = drv->request_acquire(drv);
119                 else
120                         ret = drv->request_release(drv);
121         }
122
123         return ret;
124 }
125
126 /* ------------------------------------------------------------------ */
127
128 static int dvico_fusionhdtv_demod_init(struct dvb_frontend* fe)
129 {
130         static u8 clock_config []  = { CLOCK_CTL,  0x38, 0x39 };
131         static u8 reset []         = { RESET,      0x80 };
132         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
133         static u8 agc_cfg []       = { AGC_TARGET, 0x24, 0x20 };
134         static u8 gpp_ctl_cfg []   = { GPP_CTL,    0x33 };
135         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
136
137         mt352_write(fe, clock_config,   sizeof(clock_config));
138         udelay(200);
139         mt352_write(fe, reset,          sizeof(reset));
140         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
141
142         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
143         mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
144         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
145         return 0;
146 }
147
148 static int dvico_dual_demod_init(struct dvb_frontend *fe)
149 {
150         static u8 clock_config []  = { CLOCK_CTL,  0x38, 0x38 };
151         static u8 reset []         = { RESET,      0x80 };
152         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
153         static u8 agc_cfg []       = { AGC_TARGET, 0x28, 0x20 };
154         static u8 gpp_ctl_cfg []   = { GPP_CTL,    0x33 };
155         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
156
157         mt352_write(fe, clock_config,   sizeof(clock_config));
158         udelay(200);
159         mt352_write(fe, reset,          sizeof(reset));
160         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
161
162         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
163         mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
164         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
165
166         return 0;
167 }
168
169 static int dntv_live_dvbt_demod_init(struct dvb_frontend* fe)
170 {
171         static u8 clock_config []  = { 0x89, 0x38, 0x39 };
172         static u8 reset []         = { 0x50, 0x80 };
173         static u8 adc_ctl_1_cfg [] = { 0x8E, 0x40 };
174         static u8 agc_cfg []       = { 0x67, 0x10, 0x23, 0x00, 0xFF, 0xFF,
175                                        0x00, 0xFF, 0x00, 0x40, 0x40 };
176         static u8 dntv_extra[]     = { 0xB5, 0x7A };
177         static u8 capt_range_cfg[] = { 0x75, 0x32 };
178
179         mt352_write(fe, clock_config,   sizeof(clock_config));
180         udelay(2000);
181         mt352_write(fe, reset,          sizeof(reset));
182         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
183
184         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
185         udelay(2000);
186         mt352_write(fe, dntv_extra,     sizeof(dntv_extra));
187         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
188
189         return 0;
190 }
191
192 static struct mt352_config dvico_fusionhdtv = {
193         .demod_address = 0x0f,
194         .demod_init    = dvico_fusionhdtv_demod_init,
195 };
196
197 static struct mt352_config dntv_live_dvbt_config = {
198         .demod_address = 0x0f,
199         .demod_init    = dntv_live_dvbt_demod_init,
200 };
201
202 static struct mt352_config dvico_fusionhdtv_dual = {
203         .demod_address = 0x0f,
204         .demod_init    = dvico_dual_demod_init,
205 };
206
207 #if defined(CONFIG_VIDEO_CX88_VP3054) || (defined(CONFIG_VIDEO_CX88_VP3054_MODULE) && defined(MODULE))
208 static int dntv_live_dvbt_pro_demod_init(struct dvb_frontend* fe)
209 {
210         static u8 clock_config []  = { 0x89, 0x38, 0x38 };
211         static u8 reset []         = { 0x50, 0x80 };
212         static u8 adc_ctl_1_cfg [] = { 0x8E, 0x40 };
213         static u8 agc_cfg []       = { 0x67, 0x10, 0x20, 0x00, 0xFF, 0xFF,
214                                        0x00, 0xFF, 0x00, 0x40, 0x40 };
215         static u8 dntv_extra[]     = { 0xB5, 0x7A };
216         static u8 capt_range_cfg[] = { 0x75, 0x32 };
217
218         mt352_write(fe, clock_config,   sizeof(clock_config));
219         udelay(2000);
220         mt352_write(fe, reset,          sizeof(reset));
221         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
222
223         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
224         udelay(2000);
225         mt352_write(fe, dntv_extra,     sizeof(dntv_extra));
226         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
227
228         return 0;
229 }
230
231 static struct mt352_config dntv_live_dvbt_pro_config = {
232         .demod_address = 0x0f,
233         .no_tuner      = 1,
234         .demod_init    = dntv_live_dvbt_pro_demod_init,
235 };
236 #endif
237
238 static struct zl10353_config dvico_fusionhdtv_hybrid = {
239         .demod_address = 0x0f,
240         .no_tuner      = 1,
241 };
242
243 static struct zl10353_config dvico_fusionhdtv_xc3028 = {
244         .demod_address = 0x0f,
245         .if2           = 45600,
246         .no_tuner      = 1,
247 };
248
249 static struct mt352_config dvico_fusionhdtv_mt352_xc3028 = {
250         .demod_address = 0x0f,
251         .if2 = 4560,
252         .no_tuner = 1,
253         .demod_init = dvico_fusionhdtv_demod_init,
254 };
255
256 static struct zl10353_config dvico_fusionhdtv_plus_v1_1 = {
257         .demod_address = 0x0f,
258 };
259
260 static struct cx22702_config connexant_refboard_config = {
261         .demod_address = 0x43,
262         .output_mode   = CX22702_SERIAL_OUTPUT,
263 };
264
265 static struct cx22702_config hauppauge_hvr_config = {
266         .demod_address = 0x63,
267         .output_mode   = CX22702_SERIAL_OUTPUT,
268 };
269
270 static int or51132_set_ts_param(struct dvb_frontend* fe, int is_punctured)
271 {
272         struct cx8802_dev *dev= fe->dvb->priv;
273         dev->ts_gen_cntrl = is_punctured ? 0x04 : 0x00;
274         return 0;
275 }
276
277 static struct or51132_config pchdtv_hd3000 = {
278         .demod_address = 0x15,
279         .set_ts_params = or51132_set_ts_param,
280 };
281
282 static int lgdt330x_pll_rf_set(struct dvb_frontend* fe, int index)
283 {
284         struct cx8802_dev *dev= fe->dvb->priv;
285         struct cx88_core *core = dev->core;
286
287         dprintk(1, "%s: index = %d\n", __func__, index);
288         if (index == 0)
289                 cx_clear(MO_GP0_IO, 8);
290         else
291                 cx_set(MO_GP0_IO, 8);
292         return 0;
293 }
294
295 static int lgdt330x_set_ts_param(struct dvb_frontend* fe, int is_punctured)
296 {
297         struct cx8802_dev *dev= fe->dvb->priv;
298         if (is_punctured)
299                 dev->ts_gen_cntrl |= 0x04;
300         else
301                 dev->ts_gen_cntrl &= ~0x04;
302         return 0;
303 }
304
305 static struct lgdt330x_config fusionhdtv_3_gold = {
306         .demod_address = 0x0e,
307         .demod_chip    = LGDT3302,
308         .serial_mpeg   = 0x04, /* TPSERIAL for 3302 in TOP_CONTROL */
309         .set_ts_params = lgdt330x_set_ts_param,
310 };
311
312 static struct lgdt330x_config fusionhdtv_5_gold = {
313         .demod_address = 0x0e,
314         .demod_chip    = LGDT3303,
315         .serial_mpeg   = 0x40, /* TPSERIAL for 3303 in TOP_CONTROL */
316         .set_ts_params = lgdt330x_set_ts_param,
317 };
318
319 static struct lgdt330x_config pchdtv_hd5500 = {
320         .demod_address = 0x59,
321         .demod_chip    = LGDT3303,
322         .serial_mpeg   = 0x40, /* TPSERIAL for 3303 in TOP_CONTROL */
323         .set_ts_params = lgdt330x_set_ts_param,
324 };
325
326 static int nxt200x_set_ts_param(struct dvb_frontend* fe, int is_punctured)
327 {
328         struct cx8802_dev *dev= fe->dvb->priv;
329         dev->ts_gen_cntrl = is_punctured ? 0x04 : 0x00;
330         return 0;
331 }
332
333 static struct nxt200x_config ati_hdtvwonder = {
334         .demod_address = 0x0a,
335         .set_ts_params = nxt200x_set_ts_param,
336 };
337
338 static int cx24123_set_ts_param(struct dvb_frontend* fe,
339         int is_punctured)
340 {
341         struct cx8802_dev *dev= fe->dvb->priv;
342         dev->ts_gen_cntrl = 0x02;
343         return 0;
344 }
345
346 static int kworld_dvbs_100_set_voltage(struct dvb_frontend* fe,
347                                        fe_sec_voltage_t voltage)
348 {
349         struct cx8802_dev *dev= fe->dvb->priv;
350         struct cx88_core *core = dev->core;
351
352         if (voltage == SEC_VOLTAGE_OFF)
353                 cx_write(MO_GP0_IO, 0x000006fb);
354         else
355                 cx_write(MO_GP0_IO, 0x000006f9);
356
357         if (core->prev_set_voltage)
358                 return core->prev_set_voltage(fe, voltage);
359         return 0;
360 }
361
362 static int geniatech_dvbs_set_voltage(struct dvb_frontend *fe,
363                                       fe_sec_voltage_t voltage)
364 {
365         struct cx8802_dev *dev= fe->dvb->priv;
366         struct cx88_core *core = dev->core;
367
368         if (voltage == SEC_VOLTAGE_OFF) {
369                 dprintk(1,"LNB Voltage OFF\n");
370                 cx_write(MO_GP0_IO, 0x0000efff);
371         }
372
373         if (core->prev_set_voltage)
374                 return core->prev_set_voltage(fe, voltage);
375         return 0;
376 }
377
378 static int cx88_pci_nano_callback(void *ptr, int command, int arg)
379 {
380         struct cx88_core *core = ptr;
381
382         switch (command) {
383         case XC2028_TUNER_RESET:
384                 /* Send the tuner in then out of reset */
385                 dprintk(1, "%s: XC2028_TUNER_RESET %d\n", __func__, arg);
386
387                 switch (core->boardnr) {
388                 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
389                         /* GPIO-4 xc3028 tuner */
390
391                         cx_set(MO_GP0_IO, 0x00001000);
392                         cx_clear(MO_GP0_IO, 0x00000010);
393                         msleep(100);
394                         cx_set(MO_GP0_IO, 0x00000010);
395                         msleep(100);
396                         break;
397                 }
398
399                 break;
400         case XC2028_RESET_CLK:
401                 dprintk(1, "%s: XC2028_RESET_CLK %d\n", __func__, arg);
402                 break;
403         default:
404                 dprintk(1, "%s: unknown command %d, arg %d\n", __func__,
405                         command, arg);
406                 return -EINVAL;
407         }
408
409         return 0;
410 }
411
412 static struct cx24123_config geniatech_dvbs_config = {
413         .demod_address = 0x55,
414         .set_ts_params = cx24123_set_ts_param,
415 };
416
417 static struct cx24123_config hauppauge_novas_config = {
418         .demod_address = 0x55,
419         .set_ts_params = cx24123_set_ts_param,
420 };
421
422 static struct cx24123_config kworld_dvbs_100_config = {
423         .demod_address = 0x15,
424         .set_ts_params = cx24123_set_ts_param,
425         .lnb_polarity  = 1,
426 };
427
428 static struct s5h1409_config pinnacle_pctv_hd_800i_config = {
429         .demod_address = 0x32 >> 1,
430         .output_mode   = S5H1409_PARALLEL_OUTPUT,
431         .gpio          = S5H1409_GPIO_ON,
432         .qam_if        = 44000,
433         .inversion     = S5H1409_INVERSION_OFF,
434         .status_mode   = S5H1409_DEMODLOCKING,
435         .mpeg_timing   = S5H1409_MPEGTIMING_NONCONTINOUS_NONINVERTING_CLOCK,
436 };
437
438 static struct s5h1409_config dvico_hdtv5_pci_nano_config = {
439         .demod_address = 0x32 >> 1,
440         .output_mode   = S5H1409_SERIAL_OUTPUT,
441         .gpio          = S5H1409_GPIO_OFF,
442         .inversion     = S5H1409_INVERSION_OFF,
443         .status_mode   = S5H1409_DEMODLOCKING,
444         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
445 };
446
447 static struct s5h1409_config kworld_atsc_120_config = {
448         .demod_address = 0x32 >> 1,
449         .output_mode   = S5H1409_SERIAL_OUTPUT,
450         .gpio          = S5H1409_GPIO_OFF,
451         .inversion     = S5H1409_INVERSION_OFF,
452         .status_mode   = S5H1409_DEMODLOCKING,
453         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
454 };
455
456 static struct xc5000_config pinnacle_pctv_hd_800i_tuner_config = {
457         .i2c_address    = 0x64,
458         .if_khz         = 5380,
459         .tuner_callback = cx88_tuner_callback,
460 };
461
462 static struct zl10353_config cx88_geniatech_x8000_mt = {
463        .demod_address = (0x1e >> 1),
464        .no_tuner = 1,
465 };
466
467 static int attach_xc3028(u8 addr, struct cx8802_dev *dev)
468 {
469         struct dvb_frontend *fe;
470         struct xc2028_ctrl ctl;
471         struct xc2028_config cfg = {
472                 .i2c_adap  = &dev->core->i2c_adap,
473                 .i2c_addr  = addr,
474                 .ctrl      = &ctl,
475                 .callback  = cx88_tuner_callback,
476         };
477
478         if (!dev->dvb.frontend) {
479                 printk(KERN_ERR "%s/2: dvb frontend not attached. "
480                                 "Can't attach xc3028\n",
481                        dev->core->name);
482                 return -EINVAL;
483         }
484
485         /*
486          * Some xc3028 devices may be hidden by an I2C gate. This is known
487          * to happen with some s5h1409-based devices.
488          * Now that I2C gate is open, sets up xc3028 configuration
489          */
490         cx88_setup_xc3028(dev->core, &ctl);
491
492         fe = dvb_attach(xc2028_attach, dev->dvb.frontend, &cfg);
493         if (!fe) {
494                 printk(KERN_ERR "%s/2: xc3028 attach failed\n",
495                        dev->core->name);
496                 dvb_frontend_detach(dev->dvb.frontend);
497                 dvb_unregister_frontend(dev->dvb.frontend);
498                 dev->dvb.frontend = NULL;
499                 return -EINVAL;
500         }
501
502         printk(KERN_INFO "%s/2: xc3028 attached\n",
503                dev->core->name);
504
505         return 0;
506 }
507
508 static int dvb_register(struct cx8802_dev *dev)
509 {
510         /* init struct videobuf_dvb */
511         dev->dvb.name = dev->core->name;
512         dev->ts_gen_cntrl = 0x0c;
513
514         /* init frontend */
515         switch (dev->core->boardnr) {
516         case CX88_BOARD_HAUPPAUGE_DVB_T1:
517                 dev->dvb.frontend = dvb_attach(cx22702_attach,
518                                                &connexant_refboard_config,
519                                                &dev->core->i2c_adap);
520                 if (dev->dvb.frontend != NULL) {
521                         dvb_attach(dvb_pll_attach, dev->dvb.frontend, 0x61,
522                                    &dev->core->i2c_adap,
523                                    DVB_PLL_THOMSON_DTT759X);
524                 }
525                 break;
526         case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
527         case CX88_BOARD_CONEXANT_DVB_T1:
528         case CX88_BOARD_KWORLD_DVB_T_CX22702:
529         case CX88_BOARD_WINFAST_DTV1000:
530                 dev->dvb.frontend = dvb_attach(cx22702_attach,
531                                                &connexant_refboard_config,
532                                                &dev->core->i2c_adap);
533                 if (dev->dvb.frontend != NULL) {
534                         dvb_attach(dvb_pll_attach, dev->dvb.frontend, 0x60,
535                                    &dev->core->i2c_adap,
536                                    DVB_PLL_THOMSON_DTT7579);
537                 }
538                 break;
539         case CX88_BOARD_WINFAST_DTV2000H:
540         case CX88_BOARD_HAUPPAUGE_HVR1100:
541         case CX88_BOARD_HAUPPAUGE_HVR1100LP:
542         case CX88_BOARD_HAUPPAUGE_HVR1300:
543         case CX88_BOARD_HAUPPAUGE_HVR3000:
544                 dev->dvb.frontend = dvb_attach(cx22702_attach,
545                                                &hauppauge_hvr_config,
546                                                &dev->core->i2c_adap);
547                 if (dev->dvb.frontend != NULL) {
548                         dvb_attach(simple_tuner_attach, dev->dvb.frontend,
549                                    &dev->core->i2c_adap, 0x61,
550                                    TUNER_PHILIPS_FMD1216ME_MK3);
551                 }
552                 break;
553         case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PLUS:
554                 dev->dvb.frontend = dvb_attach(mt352_attach,
555                                                &dvico_fusionhdtv,
556                                                &dev->core->i2c_adap);
557                 if (dev->dvb.frontend != NULL) {
558                         dvb_attach(dvb_pll_attach, dev->dvb.frontend, 0x60,
559                                    NULL, DVB_PLL_THOMSON_DTT7579);
560                         break;
561                 }
562                 /* ZL10353 replaces MT352 on later cards */
563                 dev->dvb.frontend = dvb_attach(zl10353_attach,
564                                                &dvico_fusionhdtv_plus_v1_1,
565                                                &dev->core->i2c_adap);
566                 if (dev->dvb.frontend != NULL) {
567                         dvb_attach(dvb_pll_attach, dev->dvb.frontend, 0x60,
568                                    NULL, DVB_PLL_THOMSON_DTT7579);
569                 }
570                 break;
571         case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL:
572                 /* The tin box says DEE1601, but it seems to be DTT7579
573                  * compatible, with a slightly different MT352 AGC gain. */
574                 dev->dvb.frontend = dvb_attach(mt352_attach,
575                                                &dvico_fusionhdtv_dual,
576                                                &dev->core->i2c_adap);
577                 if (dev->dvb.frontend != NULL) {
578                         dvb_attach(dvb_pll_attach, dev->dvb.frontend, 0x61,
579                                    NULL, DVB_PLL_THOMSON_DTT7579);
580                         break;
581                 }
582                 /* ZL10353 replaces MT352 on later cards */
583                 dev->dvb.frontend = dvb_attach(zl10353_attach,
584                                                &dvico_fusionhdtv_plus_v1_1,
585                                                &dev->core->i2c_adap);
586                 if (dev->dvb.frontend != NULL) {
587                         dvb_attach(dvb_pll_attach, dev->dvb.frontend, 0x61,
588                                    NULL, DVB_PLL_THOMSON_DTT7579);
589                 }
590                 break;
591         case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T1:
592                 dev->dvb.frontend = dvb_attach(mt352_attach,
593                                                &dvico_fusionhdtv,
594                                                &dev->core->i2c_adap);
595                 if (dev->dvb.frontend != NULL) {
596                         dvb_attach(dvb_pll_attach, dev->dvb.frontend, 0x61,
597                                    NULL, DVB_PLL_LG_Z201);
598                 }
599                 break;
600         case CX88_BOARD_KWORLD_DVB_T:
601         case CX88_BOARD_DNTV_LIVE_DVB_T:
602         case CX88_BOARD_ADSTECH_DVB_T_PCI:
603                 dev->dvb.frontend = dvb_attach(mt352_attach,
604                                                &dntv_live_dvbt_config,
605                                                &dev->core->i2c_adap);
606                 if (dev->dvb.frontend != NULL) {
607                         dvb_attach(dvb_pll_attach, dev->dvb.frontend, 0x61,
608                                    NULL, DVB_PLL_UNKNOWN_1);
609                 }
610                 break;
611         case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
612 #if defined(CONFIG_VIDEO_CX88_VP3054) || (defined(CONFIG_VIDEO_CX88_VP3054_MODULE) && defined(MODULE))
613                 /* MT352 is on a secondary I2C bus made from some GPIO lines */
614                 dev->dvb.frontend = dvb_attach(mt352_attach, &dntv_live_dvbt_pro_config,
615                                                &dev->vp3054->adap);
616                 if (dev->dvb.frontend != NULL) {
617                         dvb_attach(simple_tuner_attach, dev->dvb.frontend,
618                                    &dev->core->i2c_adap, 0x61,
619                                    TUNER_PHILIPS_FMD1216ME_MK3);
620                 }
621 #else
622                 printk(KERN_ERR "%s/2: built without vp3054 support\n", dev->core->name);
623 #endif
624                 break;
625         case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_HYBRID:
626                 dev->dvb.frontend = dvb_attach(zl10353_attach,
627                                                &dvico_fusionhdtv_hybrid,
628                                                &dev->core->i2c_adap);
629                 if (dev->dvb.frontend != NULL) {
630                         dvb_attach(simple_tuner_attach, dev->dvb.frontend,
631                                    &dev->core->i2c_adap, 0x61,
632                                    TUNER_THOMSON_FE6600);
633                 }
634                 break;
635         case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PRO:
636                 dev->dvb.frontend = dvb_attach(zl10353_attach,
637                                                &dvico_fusionhdtv_xc3028,
638                                                &dev->core->i2c_adap);
639                 if (dev->dvb.frontend == NULL)
640                         dev->dvb.frontend = dvb_attach(mt352_attach,
641                                                 &dvico_fusionhdtv_mt352_xc3028,
642                                                 &dev->core->i2c_adap);
643                 /*
644                  * On this board, the demod provides the I2C bus pullup.
645                  * We must not permit gate_ctrl to be performed, or
646                  * the xc3028 cannot communicate on the bus.
647                  */
648                 if (dev->dvb.frontend)
649                         dev->dvb.frontend->ops.i2c_gate_ctrl = NULL;
650                 if (attach_xc3028(0x61, dev) < 0)
651                         return -EINVAL;
652                 break;
653         case CX88_BOARD_PCHDTV_HD3000:
654                 dev->dvb.frontend = dvb_attach(or51132_attach, &pchdtv_hd3000,
655                                                &dev->core->i2c_adap);
656                 if (dev->dvb.frontend != NULL) {
657                         dvb_attach(simple_tuner_attach, dev->dvb.frontend,
658                                    &dev->core->i2c_adap, 0x61,
659                                    TUNER_THOMSON_DTT761X);
660                 }
661                 break;
662         case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
663                 dev->ts_gen_cntrl = 0x08;
664                 {
665                 /* Do a hardware reset of chip before using it. */
666                 struct cx88_core *core = dev->core;
667
668                 cx_clear(MO_GP0_IO, 1);
669                 mdelay(100);
670                 cx_set(MO_GP0_IO, 1);
671                 mdelay(200);
672
673                 /* Select RF connector callback */
674                 fusionhdtv_3_gold.pll_rf_set = lgdt330x_pll_rf_set;
675                 dev->dvb.frontend = dvb_attach(lgdt330x_attach,
676                                                &fusionhdtv_3_gold,
677                                                &dev->core->i2c_adap);
678                 if (dev->dvb.frontend != NULL) {
679                         dvb_attach(simple_tuner_attach, dev->dvb.frontend,
680                                    &dev->core->i2c_adap, 0x61,
681                                    TUNER_MICROTUNE_4042FI5);
682                 }
683                 }
684                 break;
685         case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
686                 dev->ts_gen_cntrl = 0x08;
687                 {
688                 /* Do a hardware reset of chip before using it. */
689                 struct cx88_core *core = dev->core;
690
691                 cx_clear(MO_GP0_IO, 1);
692                 mdelay(100);
693                 cx_set(MO_GP0_IO, 9);
694                 mdelay(200);
695                 dev->dvb.frontend = dvb_attach(lgdt330x_attach,
696                                                &fusionhdtv_3_gold,
697                                                &dev->core->i2c_adap);
698                 if (dev->dvb.frontend != NULL) {
699                         dvb_attach(simple_tuner_attach, dev->dvb.frontend,
700                                    &dev->core->i2c_adap, 0x61,
701                                    TUNER_THOMSON_DTT761X);
702                 }
703                 }
704                 break;
705         case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
706                 dev->ts_gen_cntrl = 0x08;
707                 {
708                 /* Do a hardware reset of chip before using it. */
709                 struct cx88_core *core = dev->core;
710
711                 cx_clear(MO_GP0_IO, 1);
712                 mdelay(100);
713                 cx_set(MO_GP0_IO, 1);
714                 mdelay(200);
715                 dev->dvb.frontend = dvb_attach(lgdt330x_attach,
716                                                &fusionhdtv_5_gold,
717                                                &dev->core->i2c_adap);
718                 if (dev->dvb.frontend != NULL) {
719                         dvb_attach(simple_tuner_attach, dev->dvb.frontend,
720                                    &dev->core->i2c_adap, 0x61,
721                                    TUNER_LG_TDVS_H06XF);
722                         dvb_attach(tda9887_attach, dev->dvb.frontend,
723                                    &dev->core->i2c_adap, 0x43);
724                 }
725                 }
726                 break;
727         case CX88_BOARD_PCHDTV_HD5500:
728                 dev->ts_gen_cntrl = 0x08;
729                 {
730                 /* Do a hardware reset of chip before using it. */
731                 struct cx88_core *core = dev->core;
732
733                 cx_clear(MO_GP0_IO, 1);
734                 mdelay(100);
735                 cx_set(MO_GP0_IO, 1);
736                 mdelay(200);
737                 dev->dvb.frontend = dvb_attach(lgdt330x_attach,
738                                                &pchdtv_hd5500,
739                                                &dev->core->i2c_adap);
740                 if (dev->dvb.frontend != NULL) {
741                         dvb_attach(simple_tuner_attach, dev->dvb.frontend,
742                                    &dev->core->i2c_adap, 0x61,
743                                    TUNER_LG_TDVS_H06XF);
744                         dvb_attach(tda9887_attach, dev->dvb.frontend,
745                                    &dev->core->i2c_adap, 0x43);
746                 }
747                 }
748                 break;
749         case CX88_BOARD_ATI_HDTVWONDER:
750                 dev->dvb.frontend = dvb_attach(nxt200x_attach,
751                                                &ati_hdtvwonder,
752                                                &dev->core->i2c_adap);
753                 if (dev->dvb.frontend != NULL) {
754                         dvb_attach(simple_tuner_attach, dev->dvb.frontend,
755                                    &dev->core->i2c_adap, 0x61,
756                                    TUNER_PHILIPS_TUV1236D);
757                 }
758                 break;
759         case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
760         case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
761                 dev->dvb.frontend = dvb_attach(cx24123_attach,
762                                                &hauppauge_novas_config,
763                                                &dev->core->i2c_adap);
764                 if (dev->dvb.frontend) {
765                         dvb_attach(isl6421_attach, dev->dvb.frontend,
766                                    &dev->core->i2c_adap, 0x08, 0x00, 0x00);
767                 }
768                 break;
769         case CX88_BOARD_KWORLD_DVBS_100:
770                 dev->dvb.frontend = dvb_attach(cx24123_attach,
771                                                &kworld_dvbs_100_config,
772                                                &dev->core->i2c_adap);
773                 if (dev->dvb.frontend) {
774                         dev->core->prev_set_voltage = dev->dvb.frontend->ops.set_voltage;
775                         dev->dvb.frontend->ops.set_voltage = kworld_dvbs_100_set_voltage;
776                 }
777                 break;
778         case CX88_BOARD_GENIATECH_DVBS:
779                 dev->dvb.frontend = dvb_attach(cx24123_attach,
780                                                &geniatech_dvbs_config,
781                                                &dev->core->i2c_adap);
782                 if (dev->dvb.frontend) {
783                         dev->core->prev_set_voltage = dev->dvb.frontend->ops.set_voltage;
784                         dev->dvb.frontend->ops.set_voltage = geniatech_dvbs_set_voltage;
785                 }
786                 break;
787         case CX88_BOARD_PINNACLE_PCTV_HD_800i:
788                 dev->dvb.frontend = dvb_attach(s5h1409_attach,
789                                                &pinnacle_pctv_hd_800i_config,
790                                                &dev->core->i2c_adap);
791                 if (dev->dvb.frontend != NULL) {
792                         /* tuner_config.video_dev must point to
793                          * i2c_adap.algo_data
794                          */
795                         pinnacle_pctv_hd_800i_tuner_config.priv =
796                                                 dev->core->i2c_adap.algo_data;
797                         dvb_attach(xc5000_attach, dev->dvb.frontend,
798                                    &dev->core->i2c_adap,
799                                    &pinnacle_pctv_hd_800i_tuner_config);
800                 }
801                 break;
802         case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
803                 dev->dvb.frontend = dvb_attach(s5h1409_attach,
804                                                 &dvico_hdtv5_pci_nano_config,
805                                                 &dev->core->i2c_adap);
806                 if (dev->dvb.frontend != NULL) {
807                         struct dvb_frontend *fe;
808                         struct xc2028_config cfg = {
809                                 .i2c_adap  = &dev->core->i2c_adap,
810                                 .i2c_addr  = 0x61,
811                                 .callback  = cx88_pci_nano_callback,
812                         };
813                         static struct xc2028_ctrl ctl = {
814                                 .fname       = "xc3028-v27.fw",
815                                 .max_len     = 64,
816                                 .scode_table = OREN538,
817                         };
818
819                         fe = dvb_attach(xc2028_attach,
820                                         dev->dvb.frontend, &cfg);
821                         if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
822                                 fe->ops.tuner_ops.set_config(fe, &ctl);
823                 }
824                 break;
825          case CX88_BOARD_PINNACLE_HYBRID_PCTV:
826                 dev->dvb.frontend = dvb_attach(zl10353_attach,
827                                                &cx88_geniatech_x8000_mt,
828                                                &dev->core->i2c_adap);
829                 if (attach_xc3028(0x61, dev) < 0)
830                         return -EINVAL;
831                 break;
832          case CX88_BOARD_GENIATECH_X8000_MT:
833                 dev->ts_gen_cntrl = 0x00;
834
835                 dev->dvb.frontend = dvb_attach(zl10353_attach,
836                                                &cx88_geniatech_x8000_mt,
837                                                &dev->core->i2c_adap);
838                 if (attach_xc3028(0x61, dev) < 0)
839                         return -EINVAL;
840                 break;
841          case CX88_BOARD_KWORLD_ATSC_120:
842                 dev->dvb.frontend = dvb_attach(s5h1409_attach,
843                                                &kworld_atsc_120_config,
844                                                &dev->core->i2c_adap);
845                 if (attach_xc3028(0x61, dev) < 0)
846                         return -EINVAL;
847                 break;
848         default:
849                 printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card isn't supported yet\n",
850                        dev->core->name);
851                 break;
852         }
853         if (NULL == dev->dvb.frontend) {
854                 printk(KERN_ERR
855                        "%s/2: frontend initialization failed\n",
856                        dev->core->name);
857                 return -EINVAL;
858         }
859
860         /* Ensure all frontends negotiate bus access */
861         dev->dvb.frontend->ops.ts_bus_ctrl = cx88_dvb_bus_ctrl;
862
863         /* Put the analog decoder in standby to keep it quiet */
864         cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
865
866         /* register everything */
867         return videobuf_dvb_register(&dev->dvb, THIS_MODULE, dev,
868                                      &dev->pci->dev, adapter_nr);
869 }
870
871 /* ----------------------------------------------------------- */
872
873 /* CX8802 MPEG -> mini driver - We have been given the hardware */
874 static int cx8802_dvb_advise_acquire(struct cx8802_driver *drv)
875 {
876         struct cx88_core *core = drv->core;
877         int err = 0;
878         dprintk( 1, "%s\n", __func__);
879
880         switch (core->boardnr) {
881         case CX88_BOARD_HAUPPAUGE_HVR1300:
882                 /* We arrive here with either the cx23416 or the cx22702
883                  * on the bus. Take the bus from the cx23416 and enable the
884                  * cx22702 demod
885                  */
886                 cx_set(MO_GP0_IO,   0x00000080); /* cx22702 out of reset and enable */
887                 cx_clear(MO_GP0_IO, 0x00000004);
888                 udelay(1000);
889                 break;
890         default:
891                 err = -ENODEV;
892         }
893         return err;
894 }
895
896 /* CX8802 MPEG -> mini driver - We no longer have the hardware */
897 static int cx8802_dvb_advise_release(struct cx8802_driver *drv)
898 {
899         struct cx88_core *core = drv->core;
900         int err = 0;
901         dprintk( 1, "%s\n", __func__);
902
903         switch (core->boardnr) {
904         case CX88_BOARD_HAUPPAUGE_HVR1300:
905                 /* Do Nothing, leave the cx22702 on the bus. */
906                 break;
907         default:
908                 err = -ENODEV;
909         }
910         return err;
911 }
912
913 static int cx8802_dvb_probe(struct cx8802_driver *drv)
914 {
915         struct cx88_core *core = drv->core;
916         struct cx8802_dev *dev = drv->core->dvbdev;
917         int err;
918
919         dprintk( 1, "%s\n", __func__);
920         dprintk( 1, " ->being probed by Card=%d Name=%s, PCI %02x:%02x\n",
921                 core->boardnr,
922                 core->name,
923                 core->pci_bus,
924                 core->pci_slot);
925
926         err = -ENODEV;
927         if (!(core->board.mpeg & CX88_MPEG_DVB))
928                 goto fail_core;
929
930         /* If vp3054 isn't enabled, a stub will just return 0 */
931         err = vp3054_i2c_probe(dev);
932         if (0 != err)
933                 goto fail_core;
934
935         /* dvb stuff */
936         printk(KERN_INFO "%s/2: cx2388x based DVB/ATSC card\n", core->name);
937         videobuf_queue_sg_init(&dev->dvb.dvbq, &dvb_qops,
938                             &dev->pci->dev, &dev->slock,
939                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
940                             V4L2_FIELD_TOP,
941                             sizeof(struct cx88_buffer),
942                             dev);
943         err = dvb_register(dev);
944         if (err != 0)
945                 printk(KERN_ERR "%s/2: dvb_register failed (err = %d)\n",
946                        core->name, err);
947
948  fail_core:
949         return err;
950 }
951
952 static int cx8802_dvb_remove(struct cx8802_driver *drv)
953 {
954         struct cx8802_dev *dev = drv->core->dvbdev;
955
956         /* dvb */
957         if (dev->dvb.frontend)
958                 videobuf_dvb_unregister(&dev->dvb);
959
960         vp3054_i2c_remove(dev);
961
962         return 0;
963 }
964
965 static struct cx8802_driver cx8802_dvb_driver = {
966         .type_id        = CX88_MPEG_DVB,
967         .hw_access      = CX8802_DRVCTL_SHARED,
968         .probe          = cx8802_dvb_probe,
969         .remove         = cx8802_dvb_remove,
970         .advise_acquire = cx8802_dvb_advise_acquire,
971         .advise_release = cx8802_dvb_advise_release,
972 };
973
974 static int dvb_init(void)
975 {
976         printk(KERN_INFO "cx88/2: cx2388x dvb driver version %d.%d.%d loaded\n",
977                (CX88_VERSION_CODE >> 16) & 0xff,
978                (CX88_VERSION_CODE >>  8) & 0xff,
979                CX88_VERSION_CODE & 0xff);
980 #ifdef SNAPSHOT
981         printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
982                SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
983 #endif
984         return cx8802_register_driver(&cx8802_dvb_driver);
985 }
986
987 static void dvb_fini(void)
988 {
989         cx8802_unregister_driver(&cx8802_dvb_driver);
990 }
991
992 module_init(dvb_init);
993 module_exit(dvb_fini);
994
995 /*
996  * Local variables:
997  * c-basic-offset: 8
998  * compile-command: "make DVB=1"
999  * End:
1000  */