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