[PATCH] v4l: bug fixes for tuner, cx88 and tea5767
[safe/jmp/linux-2.6] / drivers / media / video / cx88 / cx88-dvb.c
1 /*
2  * $Id: cx88-dvb.c,v 1.42 2005/07/12 15:44:55 mkrufky Exp $
3  *
4  * device driver for Conexant 2388x based TV cards
5  * MPEG Transport Stream (DVB) routines
6  *
7  * (c) 2004 Chris Pascoe <c.pascoe@itee.uq.edu.au>
8  * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/device.h>
28 #include <linux/fs.h>
29 #include <linux/kthread.h>
30 #include <linux/file.h>
31 #include <linux/suspend.h>
32
33 #define CONFIG_DVB_MT352 1
34 #define CONFIG_DVB_CX22702 1
35 #define CONFIG_DVB_OR51132 1
36 #define CONFIG_DVB_LGDT3302 1
37
38 #include "cx88.h"
39 #include "dvb-pll.h"
40
41 #if CONFIG_DVB_MT352
42 # include "mt352.h"
43 # include "mt352_priv.h"
44 #endif
45 #if CONFIG_DVB_CX22702
46 # include "cx22702.h"
47 #endif
48 #if CONFIG_DVB_OR51132
49 # include "or51132.h"
50 #endif
51 #if CONFIG_DVB_LGDT3302
52 # include "lgdt3302.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);
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 #if CONFIG_DVB_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 #if CONFIG_DVB_CX22702
181 static struct cx22702_config connexant_refboard_config = {
182         .demod_address = 0x43,
183         .pll_address   = 0x60,
184         .pll_desc      = &dvb_pll_thomson_dtt7579,
185 };
186
187 static struct cx22702_config hauppauge_novat_config = {
188         .demod_address = 0x43,
189         .pll_address   = 0x61,
190         .pll_desc      = &dvb_pll_thomson_dtt759x,
191 };
192 #endif
193
194 #if CONFIG_DVB_OR51132
195 static int or51132_set_ts_param(struct dvb_frontend* fe,
196                                 int is_punctured)
197 {
198         struct cx8802_dev *dev= fe->dvb->priv;
199         dev->ts_gen_cntrl = is_punctured ? 0x04 : 0x00;
200         return 0;
201 }
202
203 static struct or51132_config pchdtv_hd3000 = {
204         .demod_address    = 0x15,
205         .pll_address      = 0x61,
206         .pll_desc         = &dvb_pll_thomson_dtt7610,
207         .set_ts_params    = or51132_set_ts_param,
208 };
209 #endif
210
211 #if CONFIG_DVB_LGDT3302
212 static int lgdt3302_set_ts_param(struct dvb_frontend* fe, int is_punctured)
213 {
214         struct cx8802_dev *dev= fe->dvb->priv;
215         if (is_punctured)
216                 dev->ts_gen_cntrl |= 0x04;
217         else
218                 dev->ts_gen_cntrl &= ~0x04;
219         return 0;
220 }
221
222 static struct lgdt3302_config fusionhdtv_3_gold_q = {
223         .demod_address    = 0x0e,
224         .pll_address      = 0x61,
225         .pll_desc         = &dvb_pll_microtune_4042,
226         .set_ts_params    = lgdt3302_set_ts_param,
227 };
228
229 static struct lgdt3302_config fusionhdtv_3_gold_t = {
230         .demod_address    = 0x0e,
231         .pll_address      = 0x61,
232         .pll_desc         = &dvb_pll_thomson_dtt7611,
233         .set_ts_params    = lgdt3302_set_ts_param,
234 };
235 #endif
236
237 static int dvb_register(struct cx8802_dev *dev)
238 {
239         /* init struct videobuf_dvb */
240         dev->dvb.name = dev->core->name;
241         dev->ts_gen_cntrl = 0x0c;
242
243         /* init frontend */
244         switch (dev->core->board) {
245 #if CONFIG_DVB_CX22702
246         case CX88_BOARD_HAUPPAUGE_DVB_T1:
247                 dev->dvb.frontend = cx22702_attach(&hauppauge_novat_config,
248                                                    &dev->core->i2c_adap);
249                 break;
250         case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
251         case CX88_BOARD_CONEXANT_DVB_T1:
252                 dev->dvb.frontend = cx22702_attach(&connexant_refboard_config,
253                                                    &dev->core->i2c_adap);
254                 break;
255 #endif
256 #if CONFIG_DVB_MT352
257         case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T1:
258                 dev->core->pll_addr = 0x61;
259                 dev->core->pll_desc = &dvb_pll_lg_z201;
260                 dev->dvb.frontend = mt352_attach(&dvico_fusionhdtv,
261                                                  &dev->core->i2c_adap);
262                 break;
263         case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PLUS:
264                 dev->core->pll_addr = 0x60;
265                 dev->core->pll_desc = &dvb_pll_thomson_dtt7579;
266                 dev->dvb.frontend = mt352_attach(&dvico_fusionhdtv,
267                                                  &dev->core->i2c_adap);
268                 break;
269         case CX88_BOARD_KWORLD_DVB_T:
270         case CX88_BOARD_DNTV_LIVE_DVB_T:
271         case CX88_BOARD_ADSTECH_DVB_T_PCI:
272                 dev->core->pll_addr = 0x61;
273                 dev->core->pll_desc = &dvb_pll_unknown_1;
274                 dev->dvb.frontend = mt352_attach(&dntv_live_dvbt_config,
275                                                  &dev->core->i2c_adap);
276                 break;
277 #endif
278 #if CONFIG_DVB_OR51132
279         case CX88_BOARD_PCHDTV_HD3000:
280                 dev->dvb.frontend = or51132_attach(&pchdtv_hd3000,
281                                                  &dev->core->i2c_adap);
282                 break;
283 #endif
284 #if CONFIG_DVB_LGDT3302
285         case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
286                 dev->ts_gen_cntrl = 0x08;
287                 {
288                 /* Do a hardware reset of chip before using it. */
289                 struct cx88_core *core = dev->core;
290
291                 cx_clear(MO_GP0_IO, 1);
292                 mdelay(100);
293                 cx_set(MO_GP0_IO, 9); // ANT connector too FIXME
294                 mdelay(200);
295                 dev->dvb.frontend = lgdt3302_attach(&fusionhdtv_3_gold_q,
296                                                     &dev->core->i2c_adap);
297                 }
298                 break;
299         case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
300                 dev->ts_gen_cntrl = 0x08;
301                 {
302                 /* Do a hardware reset of chip before using it. */
303                 struct cx88_core *core = dev->core;
304
305                 cx_clear(MO_GP0_IO, 1);
306                 mdelay(100);
307                 cx_set(MO_GP0_IO, 9); /* ANT connector too FIXME */
308                 mdelay(200);
309                 dev->dvb.frontend = lgdt3302_attach(&fusionhdtv_3_gold_t,
310                                                     &dev->core->i2c_adap);
311                 }
312                 break;
313 #endif
314         default:
315                 printk("%s: The frontend of your DVB/ATSC card isn't supported yet\n",
316                        dev->core->name);
317                 break;
318         }
319         if (NULL == dev->dvb.frontend) {
320                 printk("%s: frontend initialization failed\n",dev->core->name);
321                 return -1;
322         }
323
324         if (dev->core->pll_desc) {
325                 dev->dvb.frontend->ops->info.frequency_min = dev->core->pll_desc->min;
326                 dev->dvb.frontend->ops->info.frequency_max = dev->core->pll_desc->max;
327         }
328
329         /* Copy the board name into the DVB structure */
330         strlcpy(dev->dvb.frontend->ops->info.name,
331                 cx88_boards[dev->core->board].name,
332                 sizeof(dev->dvb.frontend->ops->info.name));
333
334         /* register everything */
335         return videobuf_dvb_register(&dev->dvb, THIS_MODULE, dev);
336 }
337
338 /* ----------------------------------------------------------- */
339
340 static int __devinit dvb_probe(struct pci_dev *pci_dev,
341                                const struct pci_device_id *pci_id)
342 {
343         struct cx8802_dev *dev;
344         struct cx88_core  *core;
345         int err;
346
347         /* general setup */
348         core = cx88_core_get(pci_dev);
349         if (NULL == core)
350                 return -EINVAL;
351
352         err = -ENODEV;
353         if (!cx88_boards[core->board].dvb)
354                 goto fail_core;
355
356         err = -ENOMEM;
357         dev = kmalloc(sizeof(*dev),GFP_KERNEL);
358         if (NULL == dev)
359                 goto fail_core;
360         memset(dev,0,sizeof(*dev));
361         dev->pci = pci_dev;
362         dev->core = core;
363
364         err = cx8802_init_common(dev);
365         if (0 != err)
366                 goto fail_free;
367
368         /* dvb stuff */
369         printk("%s/2: cx2388x based dvb card\n", core->name);
370         videobuf_queue_init(&dev->dvb.dvbq, &dvb_qops,
371                             dev->pci, &dev->slock,
372                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
373                             V4L2_FIELD_TOP,
374                             sizeof(struct cx88_buffer),
375                             dev);
376         err = dvb_register(dev);
377         if (0 != err)
378                 goto fail_fini;
379         return 0;
380
381  fail_fini:
382         cx8802_fini_common(dev);
383  fail_free:
384         kfree(dev);
385  fail_core:
386         cx88_core_put(core,pci_dev);
387         return err;
388 }
389
390 static void __devexit dvb_remove(struct pci_dev *pci_dev)
391 {
392         struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
393
394         /* dvb */
395         videobuf_dvb_unregister(&dev->dvb);
396
397         /* common */
398         cx8802_fini_common(dev);
399         cx88_core_put(dev->core,dev->pci);
400         kfree(dev);
401 }
402
403 static struct pci_device_id cx8802_pci_tbl[] = {
404         {
405                 .vendor       = 0x14f1,
406                 .device       = 0x8802,
407                 .subvendor    = PCI_ANY_ID,
408                 .subdevice    = PCI_ANY_ID,
409         },{
410                 /* --- end of list --- */
411         }
412 };
413 MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
414
415 static struct pci_driver dvb_pci_driver = {
416         .name     = "cx88-dvb",
417         .id_table = cx8802_pci_tbl,
418         .probe    = dvb_probe,
419         .remove   = __devexit_p(dvb_remove),
420         .suspend  = cx8802_suspend_common,
421         .resume   = cx8802_resume_common,
422 };
423
424 static int dvb_init(void)
425 {
426         printk(KERN_INFO "cx2388x dvb driver version %d.%d.%d loaded\n",
427                (CX88_VERSION_CODE >> 16) & 0xff,
428                (CX88_VERSION_CODE >>  8) & 0xff,
429                CX88_VERSION_CODE & 0xff);
430 #ifdef SNAPSHOT
431         printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
432                SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
433 #endif
434         return pci_register_driver(&dvb_pci_driver);
435 }
436
437 static void dvb_fini(void)
438 {
439         pci_unregister_driver(&dvb_pci_driver);
440 }
441
442 module_init(dvb_init);
443 module_exit(dvb_fini);
444
445 /*
446  * Local variables:
447  * c-basic-offset: 8
448  * compile-command: "make DVB=1"
449  * End:
450  */