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