Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / drivers / media / video / cx88 / cx88-dvb.c
1 /*
2  * $Id: cx88-dvb.c,v 1.31 2005/03/07 15:58:05 kraxel 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 /* those two frontends need merging via linuxtv cvs ... */
34 #define HAVE_CX22702 0
35 #define HAVE_OR51132 1
36
37 #include "cx88.h"
38 #include "dvb-pll.h"
39 #include "mt352.h"
40 #include "mt352_priv.h"
41 #if HAVE_CX22702
42 # include "cx22702.h"
43 #endif
44 #if HAVE_OR51132
45 # include "or51132.h"
46 #endif
47
48 MODULE_DESCRIPTION("driver for cx2388x based DVB cards");
49 MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
50 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
51 MODULE_LICENSE("GPL");
52
53 static unsigned int debug = 0;
54 module_param(debug, int, 0644);
55 MODULE_PARM_DESC(debug,"enable debug messages [dvb]");
56
57 #define dprintk(level,fmt, arg...)      if (debug >= level) \
58         printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->core->name , ## arg)
59
60 /* ------------------------------------------------------------------ */
61
62 static int dvb_buf_setup(struct videobuf_queue *q,
63                          unsigned int *count, unsigned int *size)
64 {
65         struct cx8802_dev *dev = q->priv_data;
66
67         dev->ts_packet_size  = 188 * 4;
68         dev->ts_packet_count = 32;
69
70         *size  = dev->ts_packet_size * dev->ts_packet_count;
71         *count = 32;
72         return 0;
73 }
74
75 static int dvb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
76                            enum v4l2_field field)
77 {
78         struct cx8802_dev *dev = q->priv_data;
79         return cx8802_buf_prepare(dev, (struct cx88_buffer*)vb);
80 }
81
82 static void dvb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
83 {
84         struct cx8802_dev *dev = q->priv_data;
85         cx8802_buf_queue(dev, (struct cx88_buffer*)vb);
86 }
87
88 static void dvb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
89 {
90         struct cx8802_dev *dev = q->priv_data;
91         cx88_free_buffer(dev->pci, (struct cx88_buffer*)vb);
92 }
93
94 struct videobuf_queue_ops dvb_qops = {
95         .buf_setup    = dvb_buf_setup,
96         .buf_prepare  = dvb_buf_prepare,
97         .buf_queue    = dvb_buf_queue,
98         .buf_release  = dvb_buf_release,
99 };
100
101 /* ------------------------------------------------------------------ */
102
103 static int dvico_fusionhdtv_demod_init(struct dvb_frontend* fe)
104 {
105         static u8 clock_config []  = { CLOCK_CTL,  0x38, 0x39 };
106         static u8 reset []         = { RESET,      0x80 };
107         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
108         static u8 agc_cfg []       = { AGC_TARGET, 0x24, 0x20 };
109         static u8 gpp_ctl_cfg []   = { GPP_CTL,    0x33 };
110         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
111
112         mt352_write(fe, clock_config,   sizeof(clock_config));
113         udelay(200);
114         mt352_write(fe, reset,          sizeof(reset));
115         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
116
117         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
118         mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
119         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
120         return 0;
121 }
122
123 static int dntv_live_dvbt_demod_init(struct dvb_frontend* fe)
124 {
125         static u8 clock_config []  = { 0x89, 0x38, 0x39 };
126         static u8 reset []         = { 0x50, 0x80 };
127         static u8 adc_ctl_1_cfg [] = { 0x8E, 0x40 };
128         static u8 agc_cfg []       = { 0x67, 0x10, 0x23, 0x00, 0xFF, 0xFF,
129                                        0x00, 0xFF, 0x00, 0x40, 0x40 };
130         static u8 dntv_extra[]     = { 0xB5, 0x7A };
131         static u8 capt_range_cfg[] = { 0x75, 0x32 };
132
133         mt352_write(fe, clock_config,   sizeof(clock_config));
134         udelay(2000);
135         mt352_write(fe, reset,          sizeof(reset));
136         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
137
138         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
139         udelay(2000);
140         mt352_write(fe, dntv_extra,     sizeof(dntv_extra));
141         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
142
143         return 0;
144 }
145
146 static int mt352_pll_set(struct dvb_frontend* fe,
147                          struct dvb_frontend_parameters* params,
148                          u8* pllbuf)
149 {
150         struct cx8802_dev *dev= fe->dvb->priv;
151
152         pllbuf[0] = dev->core->pll_addr << 1;
153         dvb_pll_configure(dev->core->pll_desc, pllbuf+1,
154                           params->frequency,
155                           params->u.ofdm.bandwidth);
156         return 0;
157 }
158
159 static struct mt352_config dvico_fusionhdtv = {
160         .demod_address = 0x0F,
161         .demod_init    = dvico_fusionhdtv_demod_init,
162         .pll_set       = mt352_pll_set,
163 };
164
165 static struct mt352_config dntv_live_dvbt_config = {
166         .demod_address = 0x0f,
167         .demod_init    = dntv_live_dvbt_demod_init,
168         .pll_set       = mt352_pll_set,
169 };
170
171 #if HAVE_CX22702
172 static struct cx22702_config connexant_refboard_config = {
173         .demod_address = 0x43,
174         .pll_address   = 0x60,
175         .pll_desc      = &dvb_pll_thomson_dtt7579,
176 };
177
178 static struct cx22702_config hauppauge_novat_config = {
179         .demod_address = 0x43,
180         .pll_address   = 0x61,
181         .pll_desc      = &dvb_pll_thomson_dtt759x,
182 };
183 #endif
184
185 #if HAVE_OR51132
186 static int or51132_set_ts_param(struct dvb_frontend* fe,
187                                 int is_punctured)
188 {
189         struct cx8802_dev *dev= fe->dvb->priv;
190         dev->ts_gen_cntrl = is_punctured ? 0x04 : 0x00;
191         return 0;
192 }
193
194 struct or51132_config pchdtv_hd3000 = {
195         .demod_address    = 0x15,
196         .pll_address      = 0x61,
197         .pll_desc         = &dvb_pll_thomson_dtt7610,
198         .set_ts_params    = or51132_set_ts_param,
199 };
200 #endif
201
202 static int dvb_register(struct cx8802_dev *dev)
203 {
204         /* init struct videobuf_dvb */
205         dev->dvb.name = dev->core->name;
206         dev->ts_gen_cntrl = 0x0c;
207
208         /* init frontend */
209         switch (dev->core->board) {
210 #if HAVE_CX22702
211         case CX88_BOARD_HAUPPAUGE_DVB_T1:
212                 dev->dvb.frontend = cx22702_attach(&hauppauge_novat_config,
213                                                    &dev->core->i2c_adap);
214                 break;
215         case CX88_BOARD_CONEXANT_DVB_T1:
216                 dev->dvb.frontend = cx22702_attach(&connexant_refboard_config,
217                                                    &dev->core->i2c_adap);
218                 break;
219 #endif
220         case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T1:
221                 dev->core->pll_addr = 0x61;
222                 dev->core->pll_desc = &dvb_pll_lg_z201;
223                 dev->dvb.frontend = mt352_attach(&dvico_fusionhdtv,
224                                                  &dev->core->i2c_adap);
225                 break;
226         case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PLUS:
227                 dev->core->pll_addr = 0x60;
228                 dev->core->pll_desc = &dvb_pll_thomson_dtt7579;
229                 dev->dvb.frontend = mt352_attach(&dvico_fusionhdtv,
230                                                  &dev->core->i2c_adap);
231                 break;
232         case CX88_BOARD_KWORLD_DVB_T:
233         case CX88_BOARD_DNTV_LIVE_DVB_T:
234                 dev->core->pll_addr = 0x61;
235                 dev->core->pll_desc = &dvb_pll_unknown_1;
236                 dev->dvb.frontend = mt352_attach(&dntv_live_dvbt_config,
237                                                  &dev->core->i2c_adap);
238                 break;
239 #if HAVE_OR51132
240         case CX88_BOARD_PCHDTV_HD3000:
241                 dev->dvb.frontend = or51132_attach(&pchdtv_hd3000,
242                                                  &dev->core->i2c_adap);
243                 break;
244 #endif
245         default:
246                 printk("%s: The frontend of your DVB/ATSC card isn't supported yet\n"
247                        "%s: you might want to look out for patches here:\n"
248                        "%s:     http://dl.bytesex.org/patches/\n",
249                        dev->core->name, dev->core->name, dev->core->name);
250                 break;
251         }
252         if (NULL == dev->dvb.frontend) {
253                 printk("%s: frontend initialization failed\n",dev->core->name);
254                 return -1;
255         }
256
257         if (dev->core->pll_desc) {
258                 dev->dvb.frontend->ops->info.frequency_min = dev->core->pll_desc->min;
259                 dev->dvb.frontend->ops->info.frequency_max = dev->core->pll_desc->max;
260         }
261
262         /* Copy the board name into the DVB structure */
263         strlcpy(dev->dvb.frontend->ops->info.name,
264                 cx88_boards[dev->core->board].name,
265                 sizeof(dev->dvb.frontend->ops->info.name));
266
267         /* register everything */
268         return videobuf_dvb_register(&dev->dvb, THIS_MODULE, dev);
269 }
270
271 /* ----------------------------------------------------------- */
272
273 static int __devinit dvb_probe(struct pci_dev *pci_dev,
274                                const struct pci_device_id *pci_id)
275 {
276         struct cx8802_dev *dev;
277         struct cx88_core  *core;
278         int err;
279
280         /* general setup */
281         core = cx88_core_get(pci_dev);
282         if (NULL == core)
283                 return -EINVAL;
284
285         err = -ENODEV;
286         if (!cx88_boards[core->board].dvb)
287                 goto fail_core;
288
289         err = -ENOMEM;
290         dev = kmalloc(sizeof(*dev),GFP_KERNEL);
291         if (NULL == dev)
292                 goto fail_core;
293         memset(dev,0,sizeof(*dev));
294         dev->pci = pci_dev;
295         dev->core = core;
296
297         err = cx8802_init_common(dev);
298         if (0 != err)
299                 goto fail_free;
300
301         /* dvb stuff */
302         printk("%s/2: cx2388x based dvb card\n", core->name);
303         videobuf_queue_init(&dev->dvb.dvbq, &dvb_qops,
304                             dev->pci, &dev->slock,
305                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
306                             V4L2_FIELD_TOP,
307                             sizeof(struct cx88_buffer),
308                             dev);
309         err = dvb_register(dev);
310         if (0 != err)
311                 goto fail_free;
312         return 0;
313
314  fail_free:
315         kfree(dev);
316  fail_core:
317         cx88_core_put(core,pci_dev);
318         return err;
319 }
320
321 static void __devexit dvb_remove(struct pci_dev *pci_dev)
322 {
323         struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
324
325         /* dvb */
326         videobuf_dvb_unregister(&dev->dvb);
327
328         /* common */
329         cx8802_fini_common(dev);
330         cx88_core_put(dev->core,dev->pci);
331         kfree(dev);
332 }
333
334 static struct pci_device_id cx8802_pci_tbl[] = {
335         {
336                 .vendor       = 0x14f1,
337                 .device       = 0x8802,
338                 .subvendor    = PCI_ANY_ID,
339                 .subdevice    = PCI_ANY_ID,
340         },{
341                 /* --- end of list --- */
342         }
343 };
344 MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
345
346 static struct pci_driver dvb_pci_driver = {
347         .name     = "cx88-dvb",
348         .id_table = cx8802_pci_tbl,
349         .probe    = dvb_probe,
350         .remove   = __devexit_p(dvb_remove),
351         .suspend  = cx8802_suspend_common,
352         .resume   = cx8802_resume_common,
353 };
354
355 static int dvb_init(void)
356 {
357         printk(KERN_INFO "cx2388x dvb driver version %d.%d.%d loaded\n",
358                (CX88_VERSION_CODE >> 16) & 0xff,
359                (CX88_VERSION_CODE >>  8) & 0xff,
360                CX88_VERSION_CODE & 0xff);
361 #ifdef SNAPSHOT
362         printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
363                SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
364 #endif
365         return pci_register_driver(&dvb_pci_driver);
366 }
367
368 static void dvb_fini(void)
369 {
370         pci_unregister_driver(&dvb_pci_driver);
371 }
372
373 module_init(dvb_init);
374 module_exit(dvb_fini);
375
376 /*
377  * Local variables:
378  * c-basic-offset: 8
379  * compile-command: "make DVB=1"
380  * End:
381  */