Merge branch 'omap-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tmlind...
[pandora-kernel.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-simple.h"
49 #include "tda9887.h"
50 #include "s5h1411.h"
51 #include "stv0299.h"
52 #include "z0194a.h"
53 #include "stv0288.h"
54 #include "stb6000.h"
55 #include "cx24116.h"
56 #include "stv0900.h"
57 #include "stb6100.h"
58 #include "stb6100_proc.h"
59
60 MODULE_DESCRIPTION("driver for cx2388x based DVB cards");
61 MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
62 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
63 MODULE_LICENSE("GPL");
64
65 static unsigned int debug;
66 module_param(debug, int, 0644);
67 MODULE_PARM_DESC(debug,"enable debug messages [dvb]");
68
69 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
70
71 #define dprintk(level,fmt, arg...)      if (debug >= level) \
72         printk(KERN_DEBUG "%s/2-dvb: " fmt, core->name, ## arg)
73
74 /* ------------------------------------------------------------------ */
75
76 static int dvb_buf_setup(struct videobuf_queue *q,
77                          unsigned int *count, unsigned int *size)
78 {
79         struct cx8802_dev *dev = q->priv_data;
80
81         dev->ts_packet_size  = 188 * 4;
82         dev->ts_packet_count = 32;
83
84         *size  = dev->ts_packet_size * dev->ts_packet_count;
85         *count = 32;
86         return 0;
87 }
88
89 static int dvb_buf_prepare(struct videobuf_queue *q,
90                            struct videobuf_buffer *vb, enum v4l2_field field)
91 {
92         struct cx8802_dev *dev = q->priv_data;
93         return cx8802_buf_prepare(q, dev, (struct cx88_buffer*)vb,field);
94 }
95
96 static void dvb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
97 {
98         struct cx8802_dev *dev = q->priv_data;
99         cx8802_buf_queue(dev, (struct cx88_buffer*)vb);
100 }
101
102 static void dvb_buf_release(struct videobuf_queue *q,
103                             struct videobuf_buffer *vb)
104 {
105         cx88_free_buffer(q, (struct cx88_buffer*)vb);
106 }
107
108 static struct videobuf_queue_ops dvb_qops = {
109         .buf_setup    = dvb_buf_setup,
110         .buf_prepare  = dvb_buf_prepare,
111         .buf_queue    = dvb_buf_queue,
112         .buf_release  = dvb_buf_release,
113 };
114
115 /* ------------------------------------------------------------------ */
116
117 static int cx88_dvb_bus_ctrl(struct dvb_frontend* fe, int acquire)
118 {
119         struct cx8802_dev *dev= fe->dvb->priv;
120         struct cx8802_driver *drv = NULL;
121         int ret = 0;
122         int fe_id;
123
124         fe_id = videobuf_dvb_find_frontend(&dev->frontends, fe);
125         if (!fe_id) {
126                 printk(KERN_ERR "%s() No frontend found\n", __func__);
127                 return -EINVAL;
128         }
129
130         drv = cx8802_get_driver(dev, CX88_MPEG_DVB);
131         if (drv) {
132                 if (acquire){
133                         dev->frontends.active_fe_id = fe_id;
134                         ret = drv->request_acquire(drv);
135                 } else {
136                         ret = drv->request_release(drv);
137                         dev->frontends.active_fe_id = 0;
138                 }
139         }
140
141         return ret;
142 }
143
144 static void cx88_dvb_gate_ctrl(struct cx88_core  *core, int open)
145 {
146         struct videobuf_dvb_frontends *f;
147         struct videobuf_dvb_frontend *fe;
148
149         if (!core->dvbdev)
150                 return;
151
152         f = &core->dvbdev->frontends;
153
154         if (!f)
155                 return;
156
157         if (f->gate <= 1) /* undefined or fe0 */
158                 fe = videobuf_dvb_get_frontend(f, 1);
159         else
160                 fe = videobuf_dvb_get_frontend(f, f->gate);
161
162         if (fe && fe->dvb.frontend && fe->dvb.frontend->ops.i2c_gate_ctrl)
163                 fe->dvb.frontend->ops.i2c_gate_ctrl(fe->dvb.frontend, open);
164 }
165
166 /* ------------------------------------------------------------------ */
167
168 static int dvico_fusionhdtv_demod_init(struct dvb_frontend* fe)
169 {
170         static u8 clock_config []  = { CLOCK_CTL,  0x38, 0x39 };
171         static u8 reset []         = { RESET,      0x80 };
172         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
173         static u8 agc_cfg []       = { AGC_TARGET, 0x24, 0x20 };
174         static u8 gpp_ctl_cfg []   = { GPP_CTL,    0x33 };
175         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
176
177         mt352_write(fe, clock_config,   sizeof(clock_config));
178         udelay(200);
179         mt352_write(fe, reset,          sizeof(reset));
180         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
181
182         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
183         mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
184         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
185         return 0;
186 }
187
188 static int dvico_dual_demod_init(struct dvb_frontend *fe)
189 {
190         static u8 clock_config []  = { CLOCK_CTL,  0x38, 0x38 };
191         static u8 reset []         = { RESET,      0x80 };
192         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
193         static u8 agc_cfg []       = { AGC_TARGET, 0x28, 0x20 };
194         static u8 gpp_ctl_cfg []   = { GPP_CTL,    0x33 };
195         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
196
197         mt352_write(fe, clock_config,   sizeof(clock_config));
198         udelay(200);
199         mt352_write(fe, reset,          sizeof(reset));
200         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
201
202         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
203         mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
204         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
205
206         return 0;
207 }
208
209 static int dntv_live_dvbt_demod_init(struct dvb_frontend* fe)
210 {
211         static u8 clock_config []  = { 0x89, 0x38, 0x39 };
212         static u8 reset []         = { 0x50, 0x80 };
213         static u8 adc_ctl_1_cfg [] = { 0x8E, 0x40 };
214         static u8 agc_cfg []       = { 0x67, 0x10, 0x23, 0x00, 0xFF, 0xFF,
215                                        0x00, 0xFF, 0x00, 0x40, 0x40 };
216         static u8 dntv_extra[]     = { 0xB5, 0x7A };
217         static u8 capt_range_cfg[] = { 0x75, 0x32 };
218
219         mt352_write(fe, clock_config,   sizeof(clock_config));
220         udelay(2000);
221         mt352_write(fe, reset,          sizeof(reset));
222         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
223
224         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
225         udelay(2000);
226         mt352_write(fe, dntv_extra,     sizeof(dntv_extra));
227         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
228
229         return 0;
230 }
231
232 static struct mt352_config dvico_fusionhdtv = {
233         .demod_address = 0x0f,
234         .demod_init    = dvico_fusionhdtv_demod_init,
235 };
236
237 static struct mt352_config dntv_live_dvbt_config = {
238         .demod_address = 0x0f,
239         .demod_init    = dntv_live_dvbt_demod_init,
240 };
241
242 static struct mt352_config dvico_fusionhdtv_dual = {
243         .demod_address = 0x0f,
244         .demod_init    = dvico_dual_demod_init,
245 };
246
247 static struct zl10353_config cx88_terratec_cinergy_ht_pci_mkii_config = {
248         .demod_address = (0x1e >> 1),
249         .no_tuner      = 1,
250         .if2           = 45600,
251 };
252
253 #if defined(CONFIG_VIDEO_CX88_VP3054) || (defined(CONFIG_VIDEO_CX88_VP3054_MODULE) && defined(MODULE))
254 static int dntv_live_dvbt_pro_demod_init(struct dvb_frontend* fe)
255 {
256         static u8 clock_config []  = { 0x89, 0x38, 0x38 };
257         static u8 reset []         = { 0x50, 0x80 };
258         static u8 adc_ctl_1_cfg [] = { 0x8E, 0x40 };
259         static u8 agc_cfg []       = { 0x67, 0x10, 0x20, 0x00, 0xFF, 0xFF,
260                                        0x00, 0xFF, 0x00, 0x40, 0x40 };
261         static u8 dntv_extra[]     = { 0xB5, 0x7A };
262         static u8 capt_range_cfg[] = { 0x75, 0x32 };
263
264         mt352_write(fe, clock_config,   sizeof(clock_config));
265         udelay(2000);
266         mt352_write(fe, reset,          sizeof(reset));
267         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
268
269         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
270         udelay(2000);
271         mt352_write(fe, dntv_extra,     sizeof(dntv_extra));
272         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
273
274         return 0;
275 }
276
277 static struct mt352_config dntv_live_dvbt_pro_config = {
278         .demod_address = 0x0f,
279         .no_tuner      = 1,
280         .demod_init    = dntv_live_dvbt_pro_demod_init,
281 };
282 #endif
283
284 static struct zl10353_config dvico_fusionhdtv_hybrid = {
285         .demod_address = 0x0f,
286         .no_tuner      = 1,
287 };
288
289 static struct zl10353_config dvico_fusionhdtv_xc3028 = {
290         .demod_address = 0x0f,
291         .if2           = 45600,
292         .no_tuner      = 1,
293 };
294
295 static struct mt352_config dvico_fusionhdtv_mt352_xc3028 = {
296         .demod_address = 0x0f,
297         .if2 = 4560,
298         .no_tuner = 1,
299         .demod_init = dvico_fusionhdtv_demod_init,
300 };
301
302 static struct zl10353_config dvico_fusionhdtv_plus_v1_1 = {
303         .demod_address = 0x0f,
304 };
305
306 static struct cx22702_config connexant_refboard_config = {
307         .demod_address = 0x43,
308         .output_mode   = CX22702_SERIAL_OUTPUT,
309 };
310
311 static struct cx22702_config hauppauge_hvr_config = {
312         .demod_address = 0x63,
313         .output_mode   = CX22702_SERIAL_OUTPUT,
314 };
315
316 static int or51132_set_ts_param(struct dvb_frontend* fe, int is_punctured)
317 {
318         struct cx8802_dev *dev= fe->dvb->priv;
319         dev->ts_gen_cntrl = is_punctured ? 0x04 : 0x00;
320         return 0;
321 }
322
323 static struct or51132_config pchdtv_hd3000 = {
324         .demod_address = 0x15,
325         .set_ts_params = or51132_set_ts_param,
326 };
327
328 static int lgdt330x_pll_rf_set(struct dvb_frontend* fe, int index)
329 {
330         struct cx8802_dev *dev= fe->dvb->priv;
331         struct cx88_core *core = dev->core;
332
333         dprintk(1, "%s: index = %d\n", __func__, index);
334         if (index == 0)
335                 cx_clear(MO_GP0_IO, 8);
336         else
337                 cx_set(MO_GP0_IO, 8);
338         return 0;
339 }
340
341 static int lgdt330x_set_ts_param(struct dvb_frontend* fe, int is_punctured)
342 {
343         struct cx8802_dev *dev= fe->dvb->priv;
344         if (is_punctured)
345                 dev->ts_gen_cntrl |= 0x04;
346         else
347                 dev->ts_gen_cntrl &= ~0x04;
348         return 0;
349 }
350
351 static struct lgdt330x_config fusionhdtv_3_gold = {
352         .demod_address = 0x0e,
353         .demod_chip    = LGDT3302,
354         .serial_mpeg   = 0x04, /* TPSERIAL for 3302 in TOP_CONTROL */
355         .set_ts_params = lgdt330x_set_ts_param,
356 };
357
358 static struct lgdt330x_config fusionhdtv_5_gold = {
359         .demod_address = 0x0e,
360         .demod_chip    = LGDT3303,
361         .serial_mpeg   = 0x40, /* TPSERIAL for 3303 in TOP_CONTROL */
362         .set_ts_params = lgdt330x_set_ts_param,
363 };
364
365 static struct lgdt330x_config pchdtv_hd5500 = {
366         .demod_address = 0x59,
367         .demod_chip    = LGDT3303,
368         .serial_mpeg   = 0x40, /* TPSERIAL for 3303 in TOP_CONTROL */
369         .set_ts_params = lgdt330x_set_ts_param,
370 };
371
372 static int nxt200x_set_ts_param(struct dvb_frontend* fe, int is_punctured)
373 {
374         struct cx8802_dev *dev= fe->dvb->priv;
375         dev->ts_gen_cntrl = is_punctured ? 0x04 : 0x00;
376         return 0;
377 }
378
379 static struct nxt200x_config ati_hdtvwonder = {
380         .demod_address = 0x0a,
381         .set_ts_params = nxt200x_set_ts_param,
382 };
383
384 static int cx24123_set_ts_param(struct dvb_frontend* fe,
385         int is_punctured)
386 {
387         struct cx8802_dev *dev= fe->dvb->priv;
388         dev->ts_gen_cntrl = 0x02;
389         return 0;
390 }
391
392 static int kworld_dvbs_100_set_voltage(struct dvb_frontend* fe,
393                                        fe_sec_voltage_t voltage)
394 {
395         struct cx8802_dev *dev= fe->dvb->priv;
396         struct cx88_core *core = dev->core;
397
398         if (voltage == SEC_VOLTAGE_OFF)
399                 cx_write(MO_GP0_IO, 0x000006fb);
400         else
401                 cx_write(MO_GP0_IO, 0x000006f9);
402
403         if (core->prev_set_voltage)
404                 return core->prev_set_voltage(fe, voltage);
405         return 0;
406 }
407
408 static int geniatech_dvbs_set_voltage(struct dvb_frontend *fe,
409                                       fe_sec_voltage_t voltage)
410 {
411         struct cx8802_dev *dev= fe->dvb->priv;
412         struct cx88_core *core = dev->core;
413
414         if (voltage == SEC_VOLTAGE_OFF) {
415                 dprintk(1,"LNB Voltage OFF\n");
416                 cx_write(MO_GP0_IO, 0x0000efff);
417         }
418
419         if (core->prev_set_voltage)
420                 return core->prev_set_voltage(fe, voltage);
421         return 0;
422 }
423
424 static int tevii_dvbs_set_voltage(struct dvb_frontend *fe,
425                                       fe_sec_voltage_t voltage)
426 {
427         struct cx8802_dev *dev= fe->dvb->priv;
428         struct cx88_core *core = dev->core;
429
430         cx_set(MO_GP0_IO, 0x6040);
431         switch (voltage) {
432                 case SEC_VOLTAGE_13:
433                         cx_clear(MO_GP0_IO, 0x20);
434                         break;
435                 case SEC_VOLTAGE_18:
436                         cx_set(MO_GP0_IO, 0x20);
437                         break;
438                 case SEC_VOLTAGE_OFF:
439                         cx_clear(MO_GP0_IO, 0x20);
440                         break;
441         }
442
443         if (core->prev_set_voltage)
444                 return core->prev_set_voltage(fe, voltage);
445         return 0;
446 }
447
448 static struct cx24123_config geniatech_dvbs_config = {
449         .demod_address = 0x55,
450         .set_ts_params = cx24123_set_ts_param,
451 };
452
453 static struct cx24123_config hauppauge_novas_config = {
454         .demod_address = 0x55,
455         .set_ts_params = cx24123_set_ts_param,
456 };
457
458 static struct cx24123_config kworld_dvbs_100_config = {
459         .demod_address = 0x15,
460         .set_ts_params = cx24123_set_ts_param,
461         .lnb_polarity  = 1,
462 };
463
464 static struct s5h1409_config pinnacle_pctv_hd_800i_config = {
465         .demod_address = 0x32 >> 1,
466         .output_mode   = S5H1409_PARALLEL_OUTPUT,
467         .gpio          = S5H1409_GPIO_ON,
468         .qam_if        = 44000,
469         .inversion     = S5H1409_INVERSION_OFF,
470         .status_mode   = S5H1409_DEMODLOCKING,
471         .mpeg_timing   = S5H1409_MPEGTIMING_NONCONTINOUS_NONINVERTING_CLOCK,
472 };
473
474 static struct s5h1409_config dvico_hdtv5_pci_nano_config = {
475         .demod_address = 0x32 >> 1,
476         .output_mode   = S5H1409_SERIAL_OUTPUT,
477         .gpio          = S5H1409_GPIO_OFF,
478         .inversion     = S5H1409_INVERSION_OFF,
479         .status_mode   = S5H1409_DEMODLOCKING,
480         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
481 };
482
483 static struct s5h1409_config kworld_atsc_120_config = {
484         .demod_address = 0x32 >> 1,
485         .output_mode   = S5H1409_SERIAL_OUTPUT,
486         .gpio          = S5H1409_GPIO_OFF,
487         .inversion     = S5H1409_INVERSION_OFF,
488         .status_mode   = S5H1409_DEMODLOCKING,
489         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
490 };
491
492 static struct xc5000_config pinnacle_pctv_hd_800i_tuner_config = {
493         .i2c_address    = 0x64,
494         .if_khz         = 5380,
495 };
496
497 static struct zl10353_config cx88_pinnacle_hybrid_pctv = {
498         .demod_address = (0x1e >> 1),
499         .no_tuner      = 1,
500         .if2           = 45600,
501 };
502
503 static struct zl10353_config cx88_geniatech_x8000_mt = {
504         .demod_address = (0x1e >> 1),
505         .no_tuner = 1,
506         .disable_i2c_gate_ctrl = 1,
507 };
508
509 static struct s5h1411_config dvico_fusionhdtv7_config = {
510         .output_mode   = S5H1411_SERIAL_OUTPUT,
511         .gpio          = S5H1411_GPIO_ON,
512         .mpeg_timing   = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
513         .qam_if        = S5H1411_IF_44000,
514         .vsb_if        = S5H1411_IF_44000,
515         .inversion     = S5H1411_INVERSION_OFF,
516         .status_mode   = S5H1411_DEMODLOCKING
517 };
518
519 static struct xc5000_config dvico_fusionhdtv7_tuner_config = {
520         .i2c_address    = 0xc2 >> 1,
521         .if_khz         = 5380,
522 };
523
524 static int attach_xc3028(u8 addr, struct cx8802_dev *dev)
525 {
526         struct dvb_frontend *fe;
527         struct videobuf_dvb_frontend *fe0 = NULL;
528         struct xc2028_ctrl ctl;
529         struct xc2028_config cfg = {
530                 .i2c_adap  = &dev->core->i2c_adap,
531                 .i2c_addr  = addr,
532                 .ctrl      = &ctl,
533         };
534
535         /* Get the first frontend */
536         fe0 = videobuf_dvb_get_frontend(&dev->frontends, 1);
537         if (!fe0)
538                 return -EINVAL;
539
540         if (!fe0->dvb.frontend) {
541                 printk(KERN_ERR "%s/2: dvb frontend not attached. "
542                                 "Can't attach xc3028\n",
543                        dev->core->name);
544                 return -EINVAL;
545         }
546
547         /*
548          * Some xc3028 devices may be hidden by an I2C gate. This is known
549          * to happen with some s5h1409-based devices.
550          * Now that I2C gate is open, sets up xc3028 configuration
551          */
552         cx88_setup_xc3028(dev->core, &ctl);
553
554         fe = dvb_attach(xc2028_attach, fe0->dvb.frontend, &cfg);
555         if (!fe) {
556                 printk(KERN_ERR "%s/2: xc3028 attach failed\n",
557                        dev->core->name);
558                 dvb_frontend_detach(fe0->dvb.frontend);
559                 dvb_unregister_frontend(fe0->dvb.frontend);
560                 fe0->dvb.frontend = NULL;
561                 return -EINVAL;
562         }
563
564         printk(KERN_INFO "%s/2: xc3028 attached\n",
565                dev->core->name);
566
567         return 0;
568 }
569
570 static int cx24116_set_ts_param(struct dvb_frontend *fe,
571         int is_punctured)
572 {
573         struct cx8802_dev *dev = fe->dvb->priv;
574         dev->ts_gen_cntrl = 0x2;
575
576         return 0;
577 }
578
579 static int stv0900_set_ts_param(struct dvb_frontend *fe,
580         int is_punctured)
581 {
582         struct cx8802_dev *dev = fe->dvb->priv;
583         dev->ts_gen_cntrl = 0;
584
585         return 0;
586 }
587
588 static int cx24116_reset_device(struct dvb_frontend *fe)
589 {
590         struct cx8802_dev *dev = fe->dvb->priv;
591         struct cx88_core *core = dev->core;
592
593         /* Reset the part */
594         /* Put the cx24116 into reset */
595         cx_write(MO_SRST_IO, 0);
596         msleep(10);
597         /* Take the cx24116 out of reset */
598         cx_write(MO_SRST_IO, 1);
599         msleep(10);
600
601         return 0;
602 }
603
604 static struct cx24116_config hauppauge_hvr4000_config = {
605         .demod_address          = 0x05,
606         .set_ts_params          = cx24116_set_ts_param,
607         .reset_device           = cx24116_reset_device,
608 };
609
610 static struct cx24116_config tevii_s460_config = {
611         .demod_address = 0x55,
612         .set_ts_params = cx24116_set_ts_param,
613         .reset_device  = cx24116_reset_device,
614 };
615
616 static struct stv0900_config prof_7301_stv0900_config = {
617         .demod_address = 0x6a,
618 /*      demod_mode = 0,*/
619         .xtal = 27000000,
620         .clkmode = 3,/* 0-CLKI, 2-XTALI, else AUTO */
621         .diseqc_mode = 2,/* 2/3 PWM */
622         .tun1_maddress = 0,/* 0x60 */
623         .tun1_adc = 0,/* 2 Vpp */
624         .path1_mode = 3,
625         .set_ts_params = stv0900_set_ts_param,
626 };
627
628 static struct stb6100_config prof_7301_stb6100_config = {
629         .tuner_address = 0x60,
630         .refclock = 27000000,
631 };
632
633 static struct stv0299_config tevii_tuner_sharp_config = {
634         .demod_address = 0x68,
635         .inittab = sharp_z0194a_inittab,
636         .mclk = 88000000UL,
637         .invert = 1,
638         .skip_reinit = 0,
639         .lock_output = 1,
640         .volt13_op0_op1 = STV0299_VOLT13_OP1,
641         .min_delay_ms = 100,
642         .set_symbol_rate = sharp_z0194a_set_symbol_rate,
643         .set_ts_params = cx24116_set_ts_param,
644 };
645
646 static struct stv0288_config tevii_tuner_earda_config = {
647         .demod_address = 0x68,
648         .min_delay_ms = 100,
649         .set_ts_params = cx24116_set_ts_param,
650 };
651
652 static int cx8802_alloc_frontends(struct cx8802_dev *dev)
653 {
654         struct cx88_core *core = dev->core;
655         struct videobuf_dvb_frontend *fe = NULL;
656         int i;
657
658         mutex_init(&dev->frontends.lock);
659         INIT_LIST_HEAD(&dev->frontends.felist);
660
661         if (!core->board.num_frontends)
662                 return -ENODEV;
663
664         printk(KERN_INFO "%s() allocating %d frontend(s)\n", __func__,
665                          core->board.num_frontends);
666         for (i = 1; i <= core->board.num_frontends; i++) {
667                 fe = videobuf_dvb_alloc_frontend(&dev->frontends, i);
668                 if (!fe) {
669                         printk(KERN_ERR "%s() failed to alloc\n", __func__);
670                         videobuf_dvb_dealloc_frontends(&dev->frontends);
671                         return -ENOMEM;
672                 }
673         }
674         return 0;
675 }
676
677
678
679 static u8 samsung_smt_7020_inittab[] = {
680              0x01, 0x15,
681              0x02, 0x00,
682              0x03, 0x00,
683              0x04, 0x7D,
684              0x05, 0x0F,
685              0x06, 0x02,
686              0x07, 0x00,
687              0x08, 0x60,
688
689              0x0A, 0xC2,
690              0x0B, 0x00,
691              0x0C, 0x01,
692              0x0D, 0x81,
693              0x0E, 0x44,
694              0x0F, 0x09,
695              0x10, 0x3C,
696              0x11, 0x84,
697              0x12, 0xDA,
698              0x13, 0x99,
699              0x14, 0x8D,
700              0x15, 0xCE,
701              0x16, 0xE8,
702              0x17, 0x43,
703              0x18, 0x1C,
704              0x19, 0x1B,
705              0x1A, 0x1D,
706
707              0x1C, 0x12,
708              0x1D, 0x00,
709              0x1E, 0x00,
710              0x1F, 0x00,
711              0x20, 0x00,
712              0x21, 0x00,
713              0x22, 0x00,
714              0x23, 0x00,
715
716              0x28, 0x02,
717              0x29, 0x28,
718              0x2A, 0x14,
719              0x2B, 0x0F,
720              0x2C, 0x09,
721              0x2D, 0x05,
722
723              0x31, 0x1F,
724              0x32, 0x19,
725              0x33, 0xFC,
726              0x34, 0x13,
727              0xff, 0xff,
728 };
729
730
731 static int samsung_smt_7020_tuner_set_params(struct dvb_frontend *fe,
732         struct dvb_frontend_parameters *params)
733 {
734         struct cx8802_dev *dev = fe->dvb->priv;
735         u8 buf[4];
736         u32 div;
737         struct i2c_msg msg = {
738                 .addr = 0x61,
739                 .flags = 0,
740                 .buf = buf,
741                 .len = sizeof(buf) };
742
743         div = params->frequency / 125;
744
745         buf[0] = (div >> 8) & 0x7f;
746         buf[1] = div & 0xff;
747         buf[2] = 0x84;  /* 0xC4 */
748         buf[3] = 0x00;
749
750         if (params->frequency < 1500000)
751                 buf[3] |= 0x10;
752
753         if (fe->ops.i2c_gate_ctrl)
754                 fe->ops.i2c_gate_ctrl(fe, 1);
755
756         if (i2c_transfer(&dev->core->i2c_adap, &msg, 1) != 1)
757                 return -EIO;
758
759         return 0;
760 }
761
762 static int samsung_smt_7020_set_tone(struct dvb_frontend *fe,
763         fe_sec_tone_mode_t tone)
764 {
765         struct cx8802_dev *dev = fe->dvb->priv;
766         struct cx88_core *core = dev->core;
767
768         cx_set(MO_GP0_IO, 0x0800);
769
770         switch (tone) {
771         case SEC_TONE_ON:
772                 cx_set(MO_GP0_IO, 0x08);
773                 break;
774         case SEC_TONE_OFF:
775                 cx_clear(MO_GP0_IO, 0x08);
776                 break;
777         default:
778                 return -EINVAL;
779         }
780
781         return 0;
782 }
783
784 static int samsung_smt_7020_set_voltage(struct dvb_frontend *fe,
785         fe_sec_voltage_t voltage)
786 {
787         struct cx8802_dev *dev = fe->dvb->priv;
788         struct cx88_core *core = dev->core;
789
790         u8 data;
791         struct i2c_msg msg = {
792                 .addr = 8,
793                 .flags = 0,
794                 .buf = &data,
795                 .len = sizeof(data) };
796
797         cx_set(MO_GP0_IO, 0x8000);
798
799         switch (voltage) {
800         case SEC_VOLTAGE_OFF:
801                 break;
802         case SEC_VOLTAGE_13:
803                 data = ISL6421_EN1 | ISL6421_LLC1;
804                 cx_clear(MO_GP0_IO, 0x80);
805                 break;
806         case SEC_VOLTAGE_18:
807                 data = ISL6421_EN1 | ISL6421_LLC1 | ISL6421_VSEL1;
808                 cx_clear(MO_GP0_IO, 0x80);
809                 break;
810         default:
811                 return -EINVAL;
812         };
813
814         return (i2c_transfer(&dev->core->i2c_adap, &msg, 1) == 1) ? 0 : -EIO;
815 }
816
817 static int samsung_smt_7020_stv0299_set_symbol_rate(struct dvb_frontend *fe,
818         u32 srate, u32 ratio)
819 {
820         u8 aclk = 0;
821         u8 bclk = 0;
822
823         if (srate < 1500000) {
824                 aclk = 0xb7;
825                 bclk = 0x47;
826         } else if (srate < 3000000) {
827                 aclk = 0xb7;
828                 bclk = 0x4b;
829         } else if (srate < 7000000) {
830                 aclk = 0xb7;
831                 bclk = 0x4f;
832         } else if (srate < 14000000) {
833                 aclk = 0xb7;
834                 bclk = 0x53;
835         } else if (srate < 30000000) {
836                 aclk = 0xb6;
837                 bclk = 0x53;
838         } else if (srate < 45000000) {
839                 aclk = 0xb4;
840                 bclk = 0x51;
841         }
842
843         stv0299_writereg(fe, 0x13, aclk);
844         stv0299_writereg(fe, 0x14, bclk);
845         stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
846         stv0299_writereg(fe, 0x20, (ratio >>  8) & 0xff);
847         stv0299_writereg(fe, 0x21, ratio & 0xf0);
848
849         return 0;
850 }
851
852
853 static struct stv0299_config samsung_stv0299_config = {
854         .demod_address = 0x68,
855         .inittab = samsung_smt_7020_inittab,
856         .mclk = 88000000UL,
857         .invert = 0,
858         .skip_reinit = 0,
859         .lock_output = STV0299_LOCKOUTPUT_LK,
860         .volt13_op0_op1 = STV0299_VOLT13_OP1,
861         .min_delay_ms = 100,
862         .set_symbol_rate = samsung_smt_7020_stv0299_set_symbol_rate,
863 };
864
865 static int dvb_register(struct cx8802_dev *dev)
866 {
867         struct cx88_core *core = dev->core;
868         struct videobuf_dvb_frontend *fe0, *fe1 = NULL;
869         int mfe_shared = 0; /* bus not shared by default */
870
871         if (0 != core->i2c_rc) {
872                 printk(KERN_ERR "%s/2: no i2c-bus available, cannot attach dvb drivers\n", core->name);
873                 goto frontend_detach;
874         }
875
876         /* Get the first frontend */
877         fe0 = videobuf_dvb_get_frontend(&dev->frontends, 1);
878         if (!fe0)
879                 goto frontend_detach;
880
881         /* multi-frontend gate control is undefined or defaults to fe0 */
882         dev->frontends.gate = 0;
883
884         /* Sets the gate control callback to be used by i2c command calls */
885         core->gate_ctrl = cx88_dvb_gate_ctrl;
886
887         /* init frontend(s) */
888         switch (core->boardnr) {
889         case CX88_BOARD_HAUPPAUGE_DVB_T1:
890                 fe0->dvb.frontend = dvb_attach(cx22702_attach,
891                                                &connexant_refboard_config,
892                                                &core->i2c_adap);
893                 if (fe0->dvb.frontend != NULL) {
894                         if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend,
895                                         0x61, &core->i2c_adap,
896                                         DVB_PLL_THOMSON_DTT759X))
897                                 goto frontend_detach;
898                 }
899                 break;
900         case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
901         case CX88_BOARD_CONEXANT_DVB_T1:
902         case CX88_BOARD_KWORLD_DVB_T_CX22702:
903         case CX88_BOARD_WINFAST_DTV1000:
904                 fe0->dvb.frontend = dvb_attach(cx22702_attach,
905                                                &connexant_refboard_config,
906                                                &core->i2c_adap);
907                 if (fe0->dvb.frontend != NULL) {
908                         if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend,
909                                         0x60, &core->i2c_adap,
910                                         DVB_PLL_THOMSON_DTT7579))
911                                 goto frontend_detach;
912                 }
913                 break;
914         case CX88_BOARD_WINFAST_DTV2000H:
915         case CX88_BOARD_WINFAST_DTV2000H_J:
916         case CX88_BOARD_HAUPPAUGE_HVR1100:
917         case CX88_BOARD_HAUPPAUGE_HVR1100LP:
918         case CX88_BOARD_HAUPPAUGE_HVR1300:
919                 fe0->dvb.frontend = dvb_attach(cx22702_attach,
920                                                &hauppauge_hvr_config,
921                                                &core->i2c_adap);
922                 if (fe0->dvb.frontend != NULL) {
923                         if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
924                                    &core->i2c_adap, 0x61,
925                                    TUNER_PHILIPS_FMD1216ME_MK3))
926                                 goto frontend_detach;
927                 }
928                 break;
929         case CX88_BOARD_HAUPPAUGE_HVR3000:
930                 /* MFE frontend 1 */
931                 mfe_shared = 1;
932                 dev->frontends.gate = 2;
933                 /* DVB-S init */
934                 fe0->dvb.frontend = dvb_attach(cx24123_attach,
935                                         &hauppauge_novas_config,
936                                         &dev->core->i2c_adap);
937                 if (fe0->dvb.frontend) {
938                         if (!dvb_attach(isl6421_attach,
939                                         fe0->dvb.frontend,
940                                         &dev->core->i2c_adap,
941                                         0x08, ISL6421_DCL, 0x00))
942                                 goto frontend_detach;
943                 }
944                 /* MFE frontend 2 */
945                 fe1 = videobuf_dvb_get_frontend(&dev->frontends, 2);
946                 if (!fe1)
947                         goto frontend_detach;
948                 /* DVB-T init */
949                 fe1->dvb.frontend = dvb_attach(cx22702_attach,
950                                         &hauppauge_hvr_config,
951                                         &dev->core->i2c_adap);
952                 if (fe1->dvb.frontend) {
953                         fe1->dvb.frontend->id = 1;
954                         if (!dvb_attach(simple_tuner_attach,
955                                         fe1->dvb.frontend,
956                                         &dev->core->i2c_adap,
957                                         0x61, TUNER_PHILIPS_FMD1216ME_MK3))
958                                 goto frontend_detach;
959                 }
960                 break;
961         case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PLUS:
962                 fe0->dvb.frontend = dvb_attach(mt352_attach,
963                                                &dvico_fusionhdtv,
964                                                &core->i2c_adap);
965                 if (fe0->dvb.frontend != NULL) {
966                         if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend,
967                                         0x60, NULL, DVB_PLL_THOMSON_DTT7579))
968                                 goto frontend_detach;
969                         break;
970                 }
971                 /* ZL10353 replaces MT352 on later cards */
972                 fe0->dvb.frontend = dvb_attach(zl10353_attach,
973                                                &dvico_fusionhdtv_plus_v1_1,
974                                                &core->i2c_adap);
975                 if (fe0->dvb.frontend != NULL) {
976                         if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend,
977                                         0x60, NULL, DVB_PLL_THOMSON_DTT7579))
978                                 goto frontend_detach;
979                 }
980                 break;
981         case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL:
982                 /* The tin box says DEE1601, but it seems to be DTT7579
983                  * compatible, with a slightly different MT352 AGC gain. */
984                 fe0->dvb.frontend = dvb_attach(mt352_attach,
985                                                &dvico_fusionhdtv_dual,
986                                                &core->i2c_adap);
987                 if (fe0->dvb.frontend != NULL) {
988                         if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend,
989                                         0x61, NULL, DVB_PLL_THOMSON_DTT7579))
990                                 goto frontend_detach;
991                         break;
992                 }
993                 /* ZL10353 replaces MT352 on later cards */
994                 fe0->dvb.frontend = dvb_attach(zl10353_attach,
995                                                &dvico_fusionhdtv_plus_v1_1,
996                                                &core->i2c_adap);
997                 if (fe0->dvb.frontend != NULL) {
998                         if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend,
999                                         0x61, NULL, DVB_PLL_THOMSON_DTT7579))
1000                                 goto frontend_detach;
1001                 }
1002                 break;
1003         case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T1:
1004                 fe0->dvb.frontend = dvb_attach(mt352_attach,
1005                                                &dvico_fusionhdtv,
1006                                                &core->i2c_adap);
1007                 if (fe0->dvb.frontend != NULL) {
1008                         if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend,
1009                                         0x61, NULL, DVB_PLL_LG_Z201))
1010                                 goto frontend_detach;
1011                 }
1012                 break;
1013         case CX88_BOARD_KWORLD_DVB_T:
1014         case CX88_BOARD_DNTV_LIVE_DVB_T:
1015         case CX88_BOARD_ADSTECH_DVB_T_PCI:
1016                 fe0->dvb.frontend = dvb_attach(mt352_attach,
1017                                                &dntv_live_dvbt_config,
1018                                                &core->i2c_adap);
1019                 if (fe0->dvb.frontend != NULL) {
1020                         if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend,
1021                                         0x61, NULL, DVB_PLL_UNKNOWN_1))
1022                                 goto frontend_detach;
1023                 }
1024                 break;
1025         case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
1026 #if defined(CONFIG_VIDEO_CX88_VP3054) || (defined(CONFIG_VIDEO_CX88_VP3054_MODULE) && defined(MODULE))
1027                 /* MT352 is on a secondary I2C bus made from some GPIO lines */
1028                 fe0->dvb.frontend = dvb_attach(mt352_attach, &dntv_live_dvbt_pro_config,
1029                                                &dev->vp3054->adap);
1030                 if (fe0->dvb.frontend != NULL) {
1031                         if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1032                                         &core->i2c_adap, 0x61,
1033                                         TUNER_PHILIPS_FMD1216ME_MK3))
1034                                 goto frontend_detach;
1035                 }
1036 #else
1037                 printk(KERN_ERR "%s/2: built without vp3054 support\n",
1038                                 core->name);
1039 #endif
1040                 break;
1041         case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_HYBRID:
1042                 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1043                                                &dvico_fusionhdtv_hybrid,
1044                                                &core->i2c_adap);
1045                 if (fe0->dvb.frontend != NULL) {
1046                         if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1047                                    &core->i2c_adap, 0x61,
1048                                    TUNER_THOMSON_FE6600))
1049                                 goto frontend_detach;
1050                 }
1051                 break;
1052         case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PRO:
1053                 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1054                                                &dvico_fusionhdtv_xc3028,
1055                                                &core->i2c_adap);
1056                 if (fe0->dvb.frontend == NULL)
1057                         fe0->dvb.frontend = dvb_attach(mt352_attach,
1058                                                 &dvico_fusionhdtv_mt352_xc3028,
1059                                                 &core->i2c_adap);
1060                 /*
1061                  * On this board, the demod provides the I2C bus pullup.
1062                  * We must not permit gate_ctrl to be performed, or
1063                  * the xc3028 cannot communicate on the bus.
1064                  */
1065                 if (fe0->dvb.frontend)
1066                         fe0->dvb.frontend->ops.i2c_gate_ctrl = NULL;
1067                 if (attach_xc3028(0x61, dev) < 0)
1068                         goto frontend_detach;
1069                 break;
1070         case CX88_BOARD_PCHDTV_HD3000:
1071                 fe0->dvb.frontend = dvb_attach(or51132_attach, &pchdtv_hd3000,
1072                                                &core->i2c_adap);
1073                 if (fe0->dvb.frontend != NULL) {
1074                         if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1075                                         &core->i2c_adap, 0x61,
1076                                         TUNER_THOMSON_DTT761X))
1077                                 goto frontend_detach;
1078                 }
1079                 break;
1080         case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
1081                 dev->ts_gen_cntrl = 0x08;
1082
1083                 /* Do a hardware reset of chip before using it. */
1084                 cx_clear(MO_GP0_IO, 1);
1085                 mdelay(100);
1086                 cx_set(MO_GP0_IO, 1);
1087                 mdelay(200);
1088
1089                 /* Select RF connector callback */
1090                 fusionhdtv_3_gold.pll_rf_set = lgdt330x_pll_rf_set;
1091                 fe0->dvb.frontend = dvb_attach(lgdt330x_attach,
1092                                                &fusionhdtv_3_gold,
1093                                                &core->i2c_adap);
1094                 if (fe0->dvb.frontend != NULL) {
1095                         if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1096                                         &core->i2c_adap, 0x61,
1097                                         TUNER_MICROTUNE_4042FI5))
1098                                 goto frontend_detach;
1099                 }
1100                 break;
1101         case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
1102                 dev->ts_gen_cntrl = 0x08;
1103
1104                 /* Do a hardware reset of chip before using it. */
1105                 cx_clear(MO_GP0_IO, 1);
1106                 mdelay(100);
1107                 cx_set(MO_GP0_IO, 9);
1108                 mdelay(200);
1109                 fe0->dvb.frontend = dvb_attach(lgdt330x_attach,
1110                                                &fusionhdtv_3_gold,
1111                                                &core->i2c_adap);
1112                 if (fe0->dvb.frontend != NULL) {
1113                         if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1114                                         &core->i2c_adap, 0x61,
1115                                         TUNER_THOMSON_DTT761X))
1116                                 goto frontend_detach;
1117                 }
1118                 break;
1119         case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
1120                 dev->ts_gen_cntrl = 0x08;
1121
1122                 /* Do a hardware reset of chip before using it. */
1123                 cx_clear(MO_GP0_IO, 1);
1124                 mdelay(100);
1125                 cx_set(MO_GP0_IO, 1);
1126                 mdelay(200);
1127                 fe0->dvb.frontend = dvb_attach(lgdt330x_attach,
1128                                                &fusionhdtv_5_gold,
1129                                                &core->i2c_adap);
1130                 if (fe0->dvb.frontend != NULL) {
1131                         if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1132                                         &core->i2c_adap, 0x61,
1133                                         TUNER_LG_TDVS_H06XF))
1134                                 goto frontend_detach;
1135                         if (!dvb_attach(tda9887_attach, fe0->dvb.frontend,
1136                                    &core->i2c_adap, 0x43))
1137                                 goto frontend_detach;
1138                 }
1139                 break;
1140         case CX88_BOARD_PCHDTV_HD5500:
1141                 dev->ts_gen_cntrl = 0x08;
1142
1143                 /* Do a hardware reset of chip before using it. */
1144                 cx_clear(MO_GP0_IO, 1);
1145                 mdelay(100);
1146                 cx_set(MO_GP0_IO, 1);
1147                 mdelay(200);
1148                 fe0->dvb.frontend = dvb_attach(lgdt330x_attach,
1149                                                &pchdtv_hd5500,
1150                                                &core->i2c_adap);
1151                 if (fe0->dvb.frontend != NULL) {
1152                         if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1153                                         &core->i2c_adap, 0x61,
1154                                         TUNER_LG_TDVS_H06XF))
1155                                 goto frontend_detach;
1156                         if (!dvb_attach(tda9887_attach, fe0->dvb.frontend,
1157                                    &core->i2c_adap, 0x43))
1158                                 goto frontend_detach;
1159                 }
1160                 break;
1161         case CX88_BOARD_ATI_HDTVWONDER:
1162                 fe0->dvb.frontend = dvb_attach(nxt200x_attach,
1163                                                &ati_hdtvwonder,
1164                                                &core->i2c_adap);
1165                 if (fe0->dvb.frontend != NULL) {
1166                         if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1167                                         &core->i2c_adap, 0x61,
1168                                         TUNER_PHILIPS_TUV1236D))
1169                                 goto frontend_detach;
1170                 }
1171                 break;
1172         case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
1173         case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
1174                 fe0->dvb.frontend = dvb_attach(cx24123_attach,
1175                                                &hauppauge_novas_config,
1176                                                &core->i2c_adap);
1177                 if (fe0->dvb.frontend) {
1178                         if (!dvb_attach(isl6421_attach, fe0->dvb.frontend,
1179                                         &core->i2c_adap, 0x08, ISL6421_DCL, 0x00))
1180                                 goto frontend_detach;
1181                 }
1182                 break;
1183         case CX88_BOARD_KWORLD_DVBS_100:
1184                 fe0->dvb.frontend = dvb_attach(cx24123_attach,
1185                                                &kworld_dvbs_100_config,
1186                                                &core->i2c_adap);
1187                 if (fe0->dvb.frontend) {
1188                         core->prev_set_voltage = fe0->dvb.frontend->ops.set_voltage;
1189                         fe0->dvb.frontend->ops.set_voltage = kworld_dvbs_100_set_voltage;
1190                 }
1191                 break;
1192         case CX88_BOARD_GENIATECH_DVBS:
1193                 fe0->dvb.frontend = dvb_attach(cx24123_attach,
1194                                                &geniatech_dvbs_config,
1195                                                &core->i2c_adap);
1196                 if (fe0->dvb.frontend) {
1197                         core->prev_set_voltage = fe0->dvb.frontend->ops.set_voltage;
1198                         fe0->dvb.frontend->ops.set_voltage = geniatech_dvbs_set_voltage;
1199                 }
1200                 break;
1201         case CX88_BOARD_PINNACLE_PCTV_HD_800i:
1202                 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
1203                                                &pinnacle_pctv_hd_800i_config,
1204                                                &core->i2c_adap);
1205                 if (fe0->dvb.frontend != NULL) {
1206                         if (!dvb_attach(xc5000_attach, fe0->dvb.frontend,
1207                                         &core->i2c_adap,
1208                                         &pinnacle_pctv_hd_800i_tuner_config))
1209                                 goto frontend_detach;
1210                 }
1211                 break;
1212         case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1213                 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
1214                                                 &dvico_hdtv5_pci_nano_config,
1215                                                 &core->i2c_adap);
1216                 if (fe0->dvb.frontend != NULL) {
1217                         struct dvb_frontend *fe;
1218                         struct xc2028_config cfg = {
1219                                 .i2c_adap  = &core->i2c_adap,
1220                                 .i2c_addr  = 0x61,
1221                         };
1222                         static struct xc2028_ctrl ctl = {
1223                                 .fname       = XC2028_DEFAULT_FIRMWARE,
1224                                 .max_len     = 64,
1225                                 .scode_table = XC3028_FE_OREN538,
1226                         };
1227
1228                         fe = dvb_attach(xc2028_attach,
1229                                         fe0->dvb.frontend, &cfg);
1230                         if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
1231                                 fe->ops.tuner_ops.set_config(fe, &ctl);
1232                 }
1233                 break;
1234         case CX88_BOARD_PINNACLE_HYBRID_PCTV:
1235         case CX88_BOARD_WINFAST_DTV1800H:
1236                 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1237                                                &cx88_pinnacle_hybrid_pctv,
1238                                                &core->i2c_adap);
1239                 if (fe0->dvb.frontend) {
1240                         fe0->dvb.frontend->ops.i2c_gate_ctrl = NULL;
1241                         if (attach_xc3028(0x61, dev) < 0)
1242                                 goto frontend_detach;
1243                 }
1244                 break;
1245          case CX88_BOARD_GENIATECH_X8000_MT:
1246                 dev->ts_gen_cntrl = 0x00;
1247
1248                 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1249                                                &cx88_geniatech_x8000_mt,
1250                                                &core->i2c_adap);
1251                 if (attach_xc3028(0x61, dev) < 0)
1252                         goto frontend_detach;
1253                 break;
1254          case CX88_BOARD_KWORLD_ATSC_120:
1255                 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
1256                                                &kworld_atsc_120_config,
1257                                                &core->i2c_adap);
1258                 if (attach_xc3028(0x61, dev) < 0)
1259                         goto frontend_detach;
1260                 break;
1261         case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD:
1262                 fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1263                                                &dvico_fusionhdtv7_config,
1264                                                &core->i2c_adap);
1265                 if (fe0->dvb.frontend != NULL) {
1266                         if (!dvb_attach(xc5000_attach, fe0->dvb.frontend,
1267                                         &core->i2c_adap,
1268                                         &dvico_fusionhdtv7_tuner_config))
1269                                 goto frontend_detach;
1270                 }
1271                 break;
1272         case CX88_BOARD_HAUPPAUGE_HVR4000:
1273                 /* MFE frontend 1 */
1274                 mfe_shared = 1;
1275                 dev->frontends.gate = 2;
1276                 /* DVB-S/S2 Init */
1277                 fe0->dvb.frontend = dvb_attach(cx24116_attach,
1278                                         &hauppauge_hvr4000_config,
1279                                         &dev->core->i2c_adap);
1280                 if (fe0->dvb.frontend) {
1281                         if (!dvb_attach(isl6421_attach,
1282                                         fe0->dvb.frontend,
1283                                         &dev->core->i2c_adap,
1284                                         0x08, ISL6421_DCL, 0x00))
1285                                 goto frontend_detach;
1286                 }
1287                 /* MFE frontend 2 */
1288                 fe1 = videobuf_dvb_get_frontend(&dev->frontends, 2);
1289                 if (!fe1)
1290                         goto frontend_detach;
1291                 /* DVB-T Init */
1292                 fe1->dvb.frontend = dvb_attach(cx22702_attach,
1293                                         &hauppauge_hvr_config,
1294                                         &dev->core->i2c_adap);
1295                 if (fe1->dvb.frontend) {
1296                         fe1->dvb.frontend->id = 1;
1297                         if (!dvb_attach(simple_tuner_attach,
1298                                         fe1->dvb.frontend,
1299                                         &dev->core->i2c_adap,
1300                                         0x61, TUNER_PHILIPS_FMD1216ME_MK3))
1301                                 goto frontend_detach;
1302                 }
1303                 break;
1304         case CX88_BOARD_HAUPPAUGE_HVR4000LITE:
1305                 fe0->dvb.frontend = dvb_attach(cx24116_attach,
1306                                         &hauppauge_hvr4000_config,
1307                                         &dev->core->i2c_adap);
1308                 if (fe0->dvb.frontend) {
1309                         if (!dvb_attach(isl6421_attach,
1310                                         fe0->dvb.frontend,
1311                                         &dev->core->i2c_adap,
1312                                         0x08, ISL6421_DCL, 0x00))
1313                                 goto frontend_detach;
1314                 }
1315                 break;
1316         case CX88_BOARD_PROF_6200:
1317         case CX88_BOARD_TBS_8910:
1318         case CX88_BOARD_TEVII_S420:
1319                 fe0->dvb.frontend = dvb_attach(stv0299_attach,
1320                                                 &tevii_tuner_sharp_config,
1321                                                 &core->i2c_adap);
1322                 if (fe0->dvb.frontend != NULL) {
1323                         if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend, 0x60,
1324                                         &core->i2c_adap, DVB_PLL_OPERA1))
1325                                 goto frontend_detach;
1326                         core->prev_set_voltage = fe0->dvb.frontend->ops.set_voltage;
1327                         fe0->dvb.frontend->ops.set_voltage = tevii_dvbs_set_voltage;
1328
1329                 } else {
1330                         fe0->dvb.frontend = dvb_attach(stv0288_attach,
1331                                                             &tevii_tuner_earda_config,
1332                                                             &core->i2c_adap);
1333                                 if (fe0->dvb.frontend != NULL) {
1334                                         if (!dvb_attach(stb6000_attach, fe0->dvb.frontend, 0x61,
1335                                                 &core->i2c_adap))
1336                                         goto frontend_detach;
1337                                 core->prev_set_voltage = fe0->dvb.frontend->ops.set_voltage;
1338                                 fe0->dvb.frontend->ops.set_voltage = tevii_dvbs_set_voltage;
1339                         }
1340                 }
1341                 break;
1342         case CX88_BOARD_TEVII_S460:
1343                 fe0->dvb.frontend = dvb_attach(cx24116_attach,
1344                                                &tevii_s460_config,
1345                                                &core->i2c_adap);
1346                 if (fe0->dvb.frontend != NULL)
1347                         fe0->dvb.frontend->ops.set_voltage = tevii_dvbs_set_voltage;
1348                 break;
1349         case CX88_BOARD_OMICOM_SS4_PCI:
1350         case CX88_BOARD_TBS_8920:
1351         case CX88_BOARD_PROF_7300:
1352         case CX88_BOARD_SATTRADE_ST4200:
1353                 fe0->dvb.frontend = dvb_attach(cx24116_attach,
1354                                                &hauppauge_hvr4000_config,
1355                                                &core->i2c_adap);
1356                 if (fe0->dvb.frontend != NULL)
1357                         fe0->dvb.frontend->ops.set_voltage = tevii_dvbs_set_voltage;
1358                 break;
1359         case CX88_BOARD_TERRATEC_CINERGY_HT_PCI_MKII:
1360                 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1361                                                &cx88_terratec_cinergy_ht_pci_mkii_config,
1362                                                &core->i2c_adap);
1363                 if (fe0->dvb.frontend) {
1364                         fe0->dvb.frontend->ops.i2c_gate_ctrl = NULL;
1365                         if (attach_xc3028(0x61, dev) < 0)
1366                                 goto frontend_detach;
1367                 }
1368                 break;
1369         case CX88_BOARD_PROF_7301:{
1370                 struct dvb_tuner_ops *tuner_ops = NULL;
1371
1372                 fe0->dvb.frontend = dvb_attach(stv0900_attach,
1373                                                 &prof_7301_stv0900_config,
1374                                                 &core->i2c_adap, 0);
1375                 if (fe0->dvb.frontend != NULL) {
1376                         if (!dvb_attach(stb6100_attach, fe0->dvb.frontend,
1377                                         &prof_7301_stb6100_config,
1378                                         &core->i2c_adap))
1379                                 goto frontend_detach;
1380
1381                         tuner_ops = &fe0->dvb.frontend->ops.tuner_ops;
1382                         tuner_ops->set_frequency = stb6100_set_freq;
1383                         tuner_ops->get_frequency = stb6100_get_freq;
1384                         tuner_ops->set_bandwidth = stb6100_set_bandw;
1385                         tuner_ops->get_bandwidth = stb6100_get_bandw;
1386
1387                         core->prev_set_voltage =
1388                                         fe0->dvb.frontend->ops.set_voltage;
1389                         fe0->dvb.frontend->ops.set_voltage =
1390                                         tevii_dvbs_set_voltage;
1391                 }
1392                 break;
1393                 }
1394         case CX88_BOARD_SAMSUNG_SMT_7020:
1395                 dev->ts_gen_cntrl = 0x08;
1396
1397                 cx_set(MO_GP0_IO, 0x0101);
1398
1399                 cx_clear(MO_GP0_IO, 0x01);
1400                 mdelay(100);
1401                 cx_set(MO_GP0_IO, 0x01);
1402                 mdelay(200);
1403
1404                 fe0->dvb.frontend = dvb_attach(stv0299_attach,
1405                                         &samsung_stv0299_config,
1406                                         &dev->core->i2c_adap);
1407                 if (fe0->dvb.frontend) {
1408                         fe0->dvb.frontend->ops.tuner_ops.set_params =
1409                                 samsung_smt_7020_tuner_set_params;
1410                         fe0->dvb.frontend->tuner_priv =
1411                                 &dev->core->i2c_adap;
1412                         fe0->dvb.frontend->ops.set_voltage =
1413                                 samsung_smt_7020_set_voltage;
1414                         fe0->dvb.frontend->ops.set_tone =
1415                                 samsung_smt_7020_set_tone;
1416                 }
1417
1418                 break;
1419
1420         default:
1421                 printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card isn't supported yet\n",
1422                        core->name);
1423                 break;
1424         }
1425
1426         if ( (NULL == fe0->dvb.frontend) || (fe1 && NULL == fe1->dvb.frontend) ) {
1427                 printk(KERN_ERR
1428                        "%s/2: frontend initialization failed\n",
1429                        core->name);
1430                 goto frontend_detach;
1431         }
1432         /* define general-purpose callback pointer */
1433         fe0->dvb.frontend->callback = cx88_tuner_callback;
1434
1435         /* Ensure all frontends negotiate bus access */
1436         fe0->dvb.frontend->ops.ts_bus_ctrl = cx88_dvb_bus_ctrl;
1437         if (fe1)
1438                 fe1->dvb.frontend->ops.ts_bus_ctrl = cx88_dvb_bus_ctrl;
1439
1440         /* Put the analog decoder in standby to keep it quiet */
1441         call_all(core, core, s_power, 0);
1442
1443         /* register everything */
1444         return videobuf_dvb_register_bus(&dev->frontends, THIS_MODULE, dev,
1445                                          &dev->pci->dev, adapter_nr, mfe_shared, NULL);
1446
1447 frontend_detach:
1448         core->gate_ctrl = NULL;
1449         videobuf_dvb_dealloc_frontends(&dev->frontends);
1450         return -EINVAL;
1451 }
1452
1453 /* ----------------------------------------------------------- */
1454
1455 /* CX8802 MPEG -> mini driver - We have been given the hardware */
1456 static int cx8802_dvb_advise_acquire(struct cx8802_driver *drv)
1457 {
1458         struct cx88_core *core = drv->core;
1459         int err = 0;
1460         dprintk( 1, "%s\n", __func__);
1461
1462         switch (core->boardnr) {
1463         case CX88_BOARD_HAUPPAUGE_HVR1300:
1464                 /* We arrive here with either the cx23416 or the cx22702
1465                  * on the bus. Take the bus from the cx23416 and enable the
1466                  * cx22702 demod
1467                  */
1468                 /* Toggle reset on cx22702 leaving i2c active */
1469                 cx_set(MO_GP0_IO, 0x00000080);
1470                 udelay(1000);
1471                 cx_clear(MO_GP0_IO, 0x00000080);
1472                 udelay(50);
1473                 cx_set(MO_GP0_IO, 0x00000080);
1474                 udelay(1000);
1475                 /* enable the cx22702 pins */
1476                 cx_clear(MO_GP0_IO, 0x00000004);
1477                 udelay(1000);
1478                 break;
1479
1480         case CX88_BOARD_HAUPPAUGE_HVR3000:
1481         case CX88_BOARD_HAUPPAUGE_HVR4000:
1482                 /* Toggle reset on cx22702 leaving i2c active */
1483                 cx_set(MO_GP0_IO, 0x00000080);
1484                 udelay(1000);
1485                 cx_clear(MO_GP0_IO, 0x00000080);
1486                 udelay(50);
1487                 cx_set(MO_GP0_IO, 0x00000080);
1488                 udelay(1000);
1489                 switch (core->dvbdev->frontends.active_fe_id) {
1490                 case 1: /* DVB-S/S2 Enabled */
1491                         /* tri-state the cx22702 pins */
1492                         cx_set(MO_GP0_IO, 0x00000004);
1493                         /* Take the cx24116/cx24123 out of reset */
1494                         cx_write(MO_SRST_IO, 1);
1495                         core->dvbdev->ts_gen_cntrl = 0x02; /* Parallel IO */
1496                         break;
1497                 case 2: /* DVB-T Enabled */
1498                         /* Put the cx24116/cx24123 into reset */
1499                         cx_write(MO_SRST_IO, 0);
1500                         /* enable the cx22702 pins */
1501                         cx_clear(MO_GP0_IO, 0x00000004);
1502                         core->dvbdev->ts_gen_cntrl = 0x0c; /* Serial IO */
1503                         break;
1504                 }
1505                 udelay(1000);
1506                 break;
1507
1508         default:
1509                 err = -ENODEV;
1510         }
1511         return err;
1512 }
1513
1514 /* CX8802 MPEG -> mini driver - We no longer have the hardware */
1515 static int cx8802_dvb_advise_release(struct cx8802_driver *drv)
1516 {
1517         struct cx88_core *core = drv->core;
1518         int err = 0;
1519         dprintk( 1, "%s\n", __func__);
1520
1521         switch (core->boardnr) {
1522         case CX88_BOARD_HAUPPAUGE_HVR1300:
1523                 /* Do Nothing, leave the cx22702 on the bus. */
1524                 break;
1525         case CX88_BOARD_HAUPPAUGE_HVR3000:
1526         case CX88_BOARD_HAUPPAUGE_HVR4000:
1527                 break;
1528         default:
1529                 err = -ENODEV;
1530         }
1531         return err;
1532 }
1533
1534 static int cx8802_dvb_probe(struct cx8802_driver *drv)
1535 {
1536         struct cx88_core *core = drv->core;
1537         struct cx8802_dev *dev = drv->core->dvbdev;
1538         int err;
1539         struct videobuf_dvb_frontend *fe;
1540         int i;
1541
1542         dprintk( 1, "%s\n", __func__);
1543         dprintk( 1, " ->being probed by Card=%d Name=%s, PCI %02x:%02x\n",
1544                 core->boardnr,
1545                 core->name,
1546                 core->pci_bus,
1547                 core->pci_slot);
1548
1549         err = -ENODEV;
1550         if (!(core->board.mpeg & CX88_MPEG_DVB))
1551                 goto fail_core;
1552
1553         /* If vp3054 isn't enabled, a stub will just return 0 */
1554         err = vp3054_i2c_probe(dev);
1555         if (0 != err)
1556                 goto fail_core;
1557
1558         /* dvb stuff */
1559         printk(KERN_INFO "%s/2: cx2388x based DVB/ATSC card\n", core->name);
1560         dev->ts_gen_cntrl = 0x0c;
1561
1562         err = cx8802_alloc_frontends(dev);
1563         if (err)
1564                 goto fail_core;
1565
1566         err = -ENODEV;
1567         for (i = 1; i <= core->board.num_frontends; i++) {
1568                 fe = videobuf_dvb_get_frontend(&core->dvbdev->frontends, i);
1569                 if (fe == NULL) {
1570                         printk(KERN_ERR "%s() failed to get frontend(%d)\n",
1571                                         __func__, i);
1572                         goto fail_probe;
1573                 }
1574                 videobuf_queue_sg_init(&fe->dvb.dvbq, &dvb_qops,
1575                                     &dev->pci->dev, &dev->slock,
1576                                     V4L2_BUF_TYPE_VIDEO_CAPTURE,
1577                                     V4L2_FIELD_TOP,
1578                                     sizeof(struct cx88_buffer),
1579                                     dev);
1580                 /* init struct videobuf_dvb */
1581                 fe->dvb.name = dev->core->name;
1582         }
1583
1584         err = dvb_register(dev);
1585         if (err)
1586                 /* frontends/adapter de-allocated in dvb_register */
1587                 printk(KERN_ERR "%s/2: dvb_register failed (err = %d)\n",
1588                        core->name, err);
1589         return err;
1590 fail_probe:
1591         videobuf_dvb_dealloc_frontends(&core->dvbdev->frontends);
1592 fail_core:
1593         return err;
1594 }
1595
1596 static int cx8802_dvb_remove(struct cx8802_driver *drv)
1597 {
1598         struct cx88_core *core = drv->core;
1599         struct cx8802_dev *dev = drv->core->dvbdev;
1600
1601         dprintk( 1, "%s\n", __func__);
1602
1603         videobuf_dvb_unregister_bus(&dev->frontends);
1604
1605         vp3054_i2c_remove(dev);
1606
1607         core->gate_ctrl = NULL;
1608
1609         return 0;
1610 }
1611
1612 static struct cx8802_driver cx8802_dvb_driver = {
1613         .type_id        = CX88_MPEG_DVB,
1614         .hw_access      = CX8802_DRVCTL_SHARED,
1615         .probe          = cx8802_dvb_probe,
1616         .remove         = cx8802_dvb_remove,
1617         .advise_acquire = cx8802_dvb_advise_acquire,
1618         .advise_release = cx8802_dvb_advise_release,
1619 };
1620
1621 static int __init dvb_init(void)
1622 {
1623         printk(KERN_INFO "cx88/2: cx2388x dvb driver version %d.%d.%d loaded\n",
1624                (CX88_VERSION_CODE >> 16) & 0xff,
1625                (CX88_VERSION_CODE >>  8) & 0xff,
1626                CX88_VERSION_CODE & 0xff);
1627 #ifdef SNAPSHOT
1628         printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
1629                SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
1630 #endif
1631         return cx8802_register_driver(&cx8802_dvb_driver);
1632 }
1633
1634 static void __exit dvb_fini(void)
1635 {
1636         cx8802_unregister_driver(&cx8802_dvb_driver);
1637 }
1638
1639 module_init(dvb_init);
1640 module_exit(dvb_fini);
1641
1642 /*
1643  * Local variables:
1644  * c-basic-offset: 8
1645  * compile-command: "make DVB=1"
1646  * End:
1647  */