V4L (1007): Add support for KWorld DVB-S 100
[safe/jmp/linux-2.6] / 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 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
35 #ifdef HAVE_MT352
36 # include "mt352.h"
37 # include "mt352_priv.h"
38 #endif
39 #ifdef HAVE_CX22702
40 # include "cx22702.h"
41 #endif
42 #ifdef HAVE_OR51132
43 # include "or51132.h"
44 #endif
45 #ifdef HAVE_LGDT330X
46 # include "lgdt330x.h"
47 #endif
48 #ifdef HAVE_NXT200X
49 # include "nxt200x.h"
50 #endif
51 #ifdef HAVE_CX24123
52 # include "cx24123.h"
53 #endif
54
55 MODULE_DESCRIPTION("driver for cx2388x based DVB cards");
56 MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
57 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
58 MODULE_LICENSE("GPL");
59
60 static unsigned int debug = 0;
61 module_param(debug, int, 0644);
62 MODULE_PARM_DESC(debug,"enable debug messages [dvb]");
63
64 #define dprintk(level,fmt, arg...)      if (debug >= level) \
65         printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->core->name , ## arg)
66
67 /* ------------------------------------------------------------------ */
68
69 static int dvb_buf_setup(struct videobuf_queue *q,
70                          unsigned int *count, unsigned int *size)
71 {
72         struct cx8802_dev *dev = q->priv_data;
73
74         dev->ts_packet_size  = 188 * 4;
75         dev->ts_packet_count = 32;
76
77         *size  = dev->ts_packet_size * dev->ts_packet_count;
78         *count = 32;
79         return 0;
80 }
81
82 static int dvb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
83                            enum v4l2_field field)
84 {
85         struct cx8802_dev *dev = q->priv_data;
86         return cx8802_buf_prepare(dev, (struct cx88_buffer*)vb,field);
87 }
88
89 static void dvb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
90 {
91         struct cx8802_dev *dev = q->priv_data;
92         cx8802_buf_queue(dev, (struct cx88_buffer*)vb);
93 }
94
95 static void dvb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
96 {
97         struct cx8802_dev *dev = q->priv_data;
98         cx88_free_buffer(dev->pci, (struct cx88_buffer*)vb);
99 }
100
101 static struct videobuf_queue_ops dvb_qops = {
102         .buf_setup    = dvb_buf_setup,
103         .buf_prepare  = dvb_buf_prepare,
104         .buf_queue    = dvb_buf_queue,
105         .buf_release  = dvb_buf_release,
106 };
107
108 /* ------------------------------------------------------------------ */
109
110 #ifdef HAVE_MT352
111 static int dvico_fusionhdtv_demod_init(struct dvb_frontend* fe)
112 {
113         static u8 clock_config []  = { CLOCK_CTL,  0x38, 0x39 };
114         static u8 reset []         = { RESET,      0x80 };
115         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
116         static u8 agc_cfg []       = { AGC_TARGET, 0x24, 0x20 };
117         static u8 gpp_ctl_cfg []   = { GPP_CTL,    0x33 };
118         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
119
120         mt352_write(fe, clock_config,   sizeof(clock_config));
121         udelay(200);
122         mt352_write(fe, reset,          sizeof(reset));
123         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
124
125         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
126         mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
127         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
128         return 0;
129 }
130
131 static int dntv_live_dvbt_demod_init(struct dvb_frontend* fe)
132 {
133         static u8 clock_config []  = { 0x89, 0x38, 0x39 };
134         static u8 reset []         = { 0x50, 0x80 };
135         static u8 adc_ctl_1_cfg [] = { 0x8E, 0x40 };
136         static u8 agc_cfg []       = { 0x67, 0x10, 0x23, 0x00, 0xFF, 0xFF,
137                                        0x00, 0xFF, 0x00, 0x40, 0x40 };
138         static u8 dntv_extra[]     = { 0xB5, 0x7A };
139         static u8 capt_range_cfg[] = { 0x75, 0x32 };
140
141         mt352_write(fe, clock_config,   sizeof(clock_config));
142         udelay(2000);
143         mt352_write(fe, reset,          sizeof(reset));
144         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
145
146         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
147         udelay(2000);
148         mt352_write(fe, dntv_extra,     sizeof(dntv_extra));
149         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
150
151         return 0;
152 }
153
154 static int mt352_pll_set(struct dvb_frontend* fe,
155                          struct dvb_frontend_parameters* params,
156                          u8* pllbuf)
157 {
158         struct cx8802_dev *dev= fe->dvb->priv;
159
160         pllbuf[0] = dev->core->pll_addr << 1;
161         dvb_pll_configure(dev->core->pll_desc, pllbuf+1,
162                           params->frequency,
163                           params->u.ofdm.bandwidth);
164         return 0;
165 }
166
167 static struct mt352_config dvico_fusionhdtv = {
168         .demod_address = 0x0F,
169         .demod_init    = dvico_fusionhdtv_demod_init,
170         .pll_set       = mt352_pll_set,
171 };
172
173 static struct mt352_config dntv_live_dvbt_config = {
174         .demod_address = 0x0f,
175         .demod_init    = dntv_live_dvbt_demod_init,
176         .pll_set       = mt352_pll_set,
177 };
178 #endif
179
180 #ifdef HAVE_CX22702
181 static struct cx22702_config connexant_refboard_config = {
182         .demod_address = 0x43,
183         .output_mode   = CX22702_SERIAL_OUTPUT,
184         .pll_address   = 0x60,
185         .pll_desc      = &dvb_pll_thomson_dtt7579,
186 };
187
188 static struct cx22702_config hauppauge_novat_config = {
189         .demod_address = 0x43,
190         .output_mode   = CX22702_SERIAL_OUTPUT,
191         .pll_address   = 0x61,
192         .pll_desc      = &dvb_pll_thomson_dtt759x,
193 };
194 #endif
195
196 #ifdef HAVE_OR51132
197 static int or51132_set_ts_param(struct dvb_frontend* fe,
198                                 int is_punctured)
199 {
200         struct cx8802_dev *dev= fe->dvb->priv;
201         dev->ts_gen_cntrl = is_punctured ? 0x04 : 0x00;
202         return 0;
203 }
204
205 static struct or51132_config pchdtv_hd3000 = {
206         .demod_address    = 0x15,
207         .pll_address      = 0x61,
208         .pll_desc         = &dvb_pll_thomson_dtt7610,
209         .set_ts_params    = or51132_set_ts_param,
210 };
211 #endif
212
213 #ifdef HAVE_LGDT330X
214 static int lgdt330x_pll_set(struct dvb_frontend* fe,
215                             struct dvb_frontend_parameters* params)
216 {
217         /* FIXME make this routine use the tuner-simple code.
218          * It could probably be shared with a number of ATSC
219          * frontends. Many share the same tuner with analog TV. */
220
221         struct cx8802_dev *dev= fe->dvb->priv;
222         struct cx88_core *core = dev->core;
223         u8 buf[4];
224         struct i2c_msg msg =
225                 { .addr = dev->core->pll_addr, .flags = 0, .buf = buf, .len = 4 };
226         int err;
227
228         /* Put the analog decoder in standby to keep it quiet */
229         cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
230
231         dvb_pll_configure(core->pll_desc, buf, params->frequency, 0);
232         dprintk(1, "%s: tuner at 0x%02x bytes: 0x%02x 0x%02x 0x%02x 0x%02x\n",
233                         __FUNCTION__, msg.addr, buf[0],buf[1],buf[2],buf[3]);
234         if ((err = i2c_transfer(&core->i2c_adap, &msg, 1)) != 1) {
235                 printk(KERN_WARNING "cx88-dvb: %s error "
236                            "(addr %02x <- %02x, err = %i)\n",
237                            __FUNCTION__, buf[0], buf[1], err);
238                 if (err < 0)
239                         return err;
240                 else
241                         return -EREMOTEIO;
242         }
243         if (core->tuner_type == TUNER_LG_TDVS_H062F) {
244                 /* Set the Auxiliary Byte. */
245                 buf[2] &= ~0x20;
246                 buf[2] |= 0x18;
247                 buf[3] = 0x50;
248                 i2c_transfer(&core->i2c_adap, &msg, 1);
249         }
250         return 0;
251 }
252
253 static int lgdt330x_pll_rf_set(struct dvb_frontend* fe, int index)
254 {
255         struct cx8802_dev *dev= fe->dvb->priv;
256         struct cx88_core *core = dev->core;
257
258         dprintk(1, "%s: index = %d\n", __FUNCTION__, index);
259         if (index == 0)
260                 cx_clear(MO_GP0_IO, 8);
261         else
262                 cx_set(MO_GP0_IO, 8);
263         return 0;
264 }
265
266 static int lgdt330x_set_ts_param(struct dvb_frontend* fe, int is_punctured)
267 {
268         struct cx8802_dev *dev= fe->dvb->priv;
269         if (is_punctured)
270                 dev->ts_gen_cntrl |= 0x04;
271         else
272                 dev->ts_gen_cntrl &= ~0x04;
273         return 0;
274 }
275
276 static struct lgdt330x_config fusionhdtv_3_gold = {
277         .demod_address    = 0x0e,
278         .demod_chip       = LGDT3302,
279         .serial_mpeg      = 0x04, /* TPSERIAL for 3302 in TOP_CONTROL */
280         .pll_set          = lgdt330x_pll_set,
281         .set_ts_params    = lgdt330x_set_ts_param,
282 };
283
284 static struct lgdt330x_config fusionhdtv_5_gold = {
285         .demod_address    = 0x0e,
286         .demod_chip       = LGDT3303,
287         .serial_mpeg      = 0x40, /* TPSERIAL for 3303 in TOP_CONTROL */
288         .pll_set          = lgdt330x_pll_set,
289         .set_ts_params    = lgdt330x_set_ts_param,
290 };
291 #endif
292
293 #ifdef HAVE_NXT200X
294 static int nxt200x_set_ts_param(struct dvb_frontend* fe,
295                                 int is_punctured)
296 {
297         struct cx8802_dev *dev= fe->dvb->priv;
298         dev->ts_gen_cntrl = is_punctured ? 0x04 : 0x00;
299         return 0;
300 }
301
302 static int nxt200x_set_pll_input(u8* buf, int input)
303 {
304         if (input)
305                 buf[3] |= 0x08;
306         else
307                 buf[3] &= ~0x08;
308         return 0;
309 }
310
311 static struct nxt200x_config ati_hdtvwonder = {
312         .demod_address    = 0x0a,
313         .pll_address      = 0x61,
314         .pll_desc         = &dvb_pll_tuv1236d,
315         .set_pll_input    = nxt200x_set_pll_input,
316         .set_ts_params    = nxt200x_set_ts_param,
317 };
318 #endif
319
320 #ifdef HAVE_CX24123
321 static int cx24123_set_ts_param(struct dvb_frontend* fe,
322         int is_punctured)
323 {
324         struct cx8802_dev *dev= fe->dvb->priv;
325         dev->ts_gen_cntrl = 0x2;
326         return 0;
327 }
328
329 static void cx24123_enable_lnb_voltage(struct dvb_frontend* fe, int on)
330 {
331         struct cx8802_dev *dev= fe->dvb->priv;
332         struct cx88_core *core = dev->core;
333
334         if (on)
335                 cx_write(MO_GP0_IO, 0x000006f9);
336         else
337                 cx_write(MO_GP0_IO, 0x000006fB);
338 }
339
340 static struct cx24123_config hauppauge_novas_config = {
341         .demod_address          = 0x55,
342         .use_isl6421            = 1,
343         .set_ts_params          = cx24123_set_ts_param,
344 };
345
346 static struct cx24123_config kworld_dvbs_100_config = {
347         .demod_address          = 0x15,
348         .use_isl6421            = 0,
349         .set_ts_params          = cx24123_set_ts_param,
350         .enable_lnb_voltage     = cx24123_enable_lnb_voltage,
351 };
352 #endif
353
354 static int dvb_register(struct cx8802_dev *dev)
355 {
356         /* init struct videobuf_dvb */
357         dev->dvb.name = dev->core->name;
358         dev->ts_gen_cntrl = 0x0c;
359
360         /* init frontend */
361         switch (dev->core->board) {
362 #ifdef HAVE_CX22702
363         case CX88_BOARD_HAUPPAUGE_DVB_T1:
364                 dev->dvb.frontend = cx22702_attach(&hauppauge_novat_config,
365                                                    &dev->core->i2c_adap);
366                 break;
367         case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
368         case CX88_BOARD_CONEXANT_DVB_T1:
369         case CX88_BOARD_WINFAST_DTV1000:
370                 dev->dvb.frontend = cx22702_attach(&connexant_refboard_config,
371                                                    &dev->core->i2c_adap);
372                 break;
373 #endif
374 #ifdef HAVE_MT352
375         case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T1:
376                 dev->core->pll_addr = 0x61;
377                 dev->core->pll_desc = &dvb_pll_lg_z201;
378                 dev->dvb.frontend = mt352_attach(&dvico_fusionhdtv,
379                                                  &dev->core->i2c_adap);
380                 break;
381         case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PLUS:
382                 dev->core->pll_addr = 0x60;
383                 dev->core->pll_desc = &dvb_pll_thomson_dtt7579;
384                 dev->dvb.frontend = mt352_attach(&dvico_fusionhdtv,
385                                                  &dev->core->i2c_adap);
386                 break;
387         case CX88_BOARD_KWORLD_DVB_T:
388         case CX88_BOARD_DNTV_LIVE_DVB_T:
389         case CX88_BOARD_ADSTECH_DVB_T_PCI:
390                 dev->core->pll_addr = 0x61;
391                 dev->core->pll_desc = &dvb_pll_unknown_1;
392                 dev->dvb.frontend = mt352_attach(&dntv_live_dvbt_config,
393                                                  &dev->core->i2c_adap);
394                 break;
395 #endif
396 #ifdef HAVE_OR51132
397         case CX88_BOARD_PCHDTV_HD3000:
398                 dev->dvb.frontend = or51132_attach(&pchdtv_hd3000,
399                                                  &dev->core->i2c_adap);
400                 break;
401 #endif
402 #ifdef HAVE_LGDT330X
403         case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
404                 dev->ts_gen_cntrl = 0x08;
405                 {
406                 /* Do a hardware reset of chip before using it. */
407                 struct cx88_core *core = dev->core;
408
409                 cx_clear(MO_GP0_IO, 1);
410                 mdelay(100);
411                 cx_set(MO_GP0_IO, 1);
412                 mdelay(200);
413
414                 /* Select RF connector callback */
415                 fusionhdtv_3_gold.pll_rf_set = lgdt330x_pll_rf_set;
416                 dev->core->pll_addr = 0x61;
417                 dev->core->pll_desc = &dvb_pll_microtune_4042;
418                 dev->dvb.frontend = lgdt330x_attach(&fusionhdtv_3_gold,
419                                                     &dev->core->i2c_adap);
420                 }
421                 break;
422         case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
423                 dev->ts_gen_cntrl = 0x08;
424                 {
425                 /* Do a hardware reset of chip before using it. */
426                 struct cx88_core *core = dev->core;
427
428                 cx_clear(MO_GP0_IO, 1);
429                 mdelay(100);
430                 cx_set(MO_GP0_IO, 9);
431                 mdelay(200);
432                 dev->core->pll_addr = 0x61;
433                 dev->core->pll_desc = &dvb_pll_thomson_dtt7611;
434                 dev->dvb.frontend = lgdt330x_attach(&fusionhdtv_3_gold,
435                                                     &dev->core->i2c_adap);
436                 }
437                 break;
438         case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
439                 dev->ts_gen_cntrl = 0x08;
440                 {
441                 /* Do a hardware reset of chip before using it. */
442                 struct cx88_core *core = dev->core;
443
444                 cx_clear(MO_GP0_IO, 1);
445                 mdelay(100);
446                 cx_set(MO_GP0_IO, 1);
447                 mdelay(200);
448                 dev->core->pll_addr = 0x61;
449                 dev->core->pll_desc = &dvb_pll_tdvs_tua6034;
450                 dev->dvb.frontend = lgdt330x_attach(&fusionhdtv_5_gold,
451                                                     &dev->core->i2c_adap);
452                 }
453                 break;
454 #endif
455 #ifdef HAVE_NXT200X
456         case CX88_BOARD_ATI_HDTVWONDER:
457                 dev->dvb.frontend = nxt200x_attach(&ati_hdtvwonder,
458                                                  &dev->core->i2c_adap);
459                 break;
460 #endif
461 #ifdef HAVE_CX24123
462         case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
463         case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
464                 dev->dvb.frontend = cx24123_attach(&hauppauge_novas_config,
465                         &dev->core->i2c_adap);
466                 break;
467         case CX88_BOARD_KWORLD_DVBS_100:
468                 dev->dvb.frontend = cx24123_attach(&kworld_dvbs_100_config,
469                         &dev->core->i2c_adap);
470                 break;
471 #endif
472         default:
473                 printk("%s: The frontend of your DVB/ATSC card isn't supported yet\n",
474                        dev->core->name);
475                 break;
476         }
477         if (NULL == dev->dvb.frontend) {
478                 printk("%s: frontend initialization failed\n",dev->core->name);
479                 return -1;
480         }
481
482         if (dev->core->pll_desc) {
483                 dev->dvb.frontend->ops->info.frequency_min = dev->core->pll_desc->min;
484                 dev->dvb.frontend->ops->info.frequency_max = dev->core->pll_desc->max;
485         }
486
487         /* Put the analog decoder in standby to keep it quiet */
488         cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
489
490         /* register everything */
491         return videobuf_dvb_register(&dev->dvb, THIS_MODULE, dev);
492 }
493
494 /* ----------------------------------------------------------- */
495
496 static int __devinit dvb_probe(struct pci_dev *pci_dev,
497                                const struct pci_device_id *pci_id)
498 {
499         struct cx8802_dev *dev;
500         struct cx88_core  *core;
501         int err;
502
503         /* general setup */
504         core = cx88_core_get(pci_dev);
505         if (NULL == core)
506                 return -EINVAL;
507
508         err = -ENODEV;
509         if (!cx88_boards[core->board].dvb)
510                 goto fail_core;
511
512         err = -ENOMEM;
513         dev = kmalloc(sizeof(*dev),GFP_KERNEL);
514         if (NULL == dev)
515                 goto fail_core;
516         memset(dev,0,sizeof(*dev));
517         dev->pci = pci_dev;
518         dev->core = core;
519
520         err = cx8802_init_common(dev);
521         if (0 != err)
522                 goto fail_free;
523
524         /* dvb stuff */
525         printk("%s/2: cx2388x based dvb card\n", core->name);
526         videobuf_queue_init(&dev->dvb.dvbq, &dvb_qops,
527                             dev->pci, &dev->slock,
528                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
529                             V4L2_FIELD_TOP,
530                             sizeof(struct cx88_buffer),
531                             dev);
532         err = dvb_register(dev);
533         if (0 != err)
534                 goto fail_fini;
535         return 0;
536
537  fail_fini:
538         cx8802_fini_common(dev);
539  fail_free:
540         kfree(dev);
541  fail_core:
542         cx88_core_put(core,pci_dev);
543         return err;
544 }
545
546 static void __devexit dvb_remove(struct pci_dev *pci_dev)
547 {
548         struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
549
550         /* dvb */
551         videobuf_dvb_unregister(&dev->dvb);
552
553         /* common */
554         cx8802_fini_common(dev);
555         cx88_core_put(dev->core,dev->pci);
556         kfree(dev);
557 }
558
559 static struct pci_device_id cx8802_pci_tbl[] = {
560         {
561                 .vendor       = 0x14f1,
562                 .device       = 0x8802,
563                 .subvendor    = PCI_ANY_ID,
564                 .subdevice    = PCI_ANY_ID,
565         },{
566                 /* --- end of list --- */
567         }
568 };
569 MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
570
571 static struct pci_driver dvb_pci_driver = {
572         .name     = "cx88-dvb",
573         .id_table = cx8802_pci_tbl,
574         .probe    = dvb_probe,
575         .remove   = __devexit_p(dvb_remove),
576         .suspend  = cx8802_suspend_common,
577         .resume   = cx8802_resume_common,
578 };
579
580 static int dvb_init(void)
581 {
582         printk(KERN_INFO "cx2388x dvb driver version %d.%d.%d loaded\n",
583                (CX88_VERSION_CODE >> 16) & 0xff,
584                (CX88_VERSION_CODE >>  8) & 0xff,
585                CX88_VERSION_CODE & 0xff);
586 #ifdef SNAPSHOT
587         printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
588                SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
589 #endif
590         return pci_register_driver(&dvb_pci_driver);
591 }
592
593 static void dvb_fini(void)
594 {
595         pci_unregister_driver(&dvb_pci_driver);
596 }
597
598 module_init(dvb_init);
599 module_exit(dvb_fini);
600
601 /*
602  * Local variables:
603  * c-basic-offset: 8
604  * compile-command: "make DVB=1"
605  * End:
606  */