blob: 95847b5a487b2fba59d6c7a6ff657c411bca525d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Michael Krufky29780bb2005-07-27 11:45:59 -07002 * $Id: cx88-dvb.c,v 1.52 2005/07/24 22:12:47 mkrufky Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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>
Michael Krufky84de2ef2005-07-27 11:45:58 -070032#include <linux/config.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Michael Krufky55ee3b82005-07-12 13:59:08 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "cx88.h"
36#include "dvb-pll.h"
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -070037
Michael Krufky29780bb2005-07-27 11:45:59 -070038#ifdef HAVE_MT352
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -070039# include "mt352.h"
40# include "mt352_priv.h"
41#endif
Michael Krufky29780bb2005-07-27 11:45:59 -070042#ifdef HAVE_CX22702
Linus Torvalds1da177e2005-04-16 15:20:36 -070043# include "cx22702.h"
44#endif
Michael Krufky29780bb2005-07-27 11:45:59 -070045#ifdef HAVE_OR51132
Linus Torvalds1da177e2005-04-16 15:20:36 -070046# include "or51132.h"
47#endif
Michael Krufky29780bb2005-07-27 11:45:59 -070048#ifdef HAVE_LGDT3302
Michael Krufkyf1798492005-07-07 17:58:39 -070049# include "lgdt3302.h"
50#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52MODULE_DESCRIPTION("driver for cx2388x based DVB cards");
53MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
54MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
55MODULE_LICENSE("GPL");
56
57static unsigned int debug = 0;
58module_param(debug, int, 0644);
59MODULE_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
66static 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
79static 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);
84}
85
86static 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
92static 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
Adrian Bunk408b6642005-05-01 08:59:29 -070098static struct videobuf_queue_ops dvb_qops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .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
Michael Krufky29780bb2005-07-27 11:45:59 -0700107#ifdef HAVE_MT352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static 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
128static 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
151static 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
164static struct mt352_config dvico_fusionhdtv = {
165 .demod_address = 0x0F,
166 .demod_init = dvico_fusionhdtv_demod_init,
167 .pll_set = mt352_pll_set,
168};
169
170static 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};
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -0700175#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Michael Krufky29780bb2005-07-27 11:45:59 -0700177#ifdef HAVE_CX22702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static struct cx22702_config connexant_refboard_config = {
179 .demod_address = 0x43,
Patrick Boettcher38d84c32005-07-15 12:20:26 -0700180 .output_mode = CX22702_SERIAL_OUTPUT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 .pll_address = 0x60,
182 .pll_desc = &dvb_pll_thomson_dtt7579,
183};
184
185static struct cx22702_config hauppauge_novat_config = {
186 .demod_address = 0x43,
Patrick Boettcher38d84c32005-07-15 12:20:26 -0700187 .output_mode = CX22702_SERIAL_OUTPUT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 .pll_address = 0x61,
189 .pll_desc = &dvb_pll_thomson_dtt759x,
190};
191#endif
192
Michael Krufky29780bb2005-07-27 11:45:59 -0700193#ifdef HAVE_OR51132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static 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
Adrian Bunk408b6642005-05-01 08:59:29 -0700202static struct or51132_config pchdtv_hd3000 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 .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
Michael Krufky29780bb2005-07-27 11:45:59 -0700210#ifdef HAVE_LGDT3302
Michael Krufkyb6aef072005-07-27 11:45:54 -0700211static int lgdt3302_pll_set(struct dvb_frontend* fe,
212 struct dvb_frontend_parameters* params,
213 u8* pllbuf)
214{
215 struct cx8802_dev *dev= fe->dvb->priv;
216
217 pllbuf[0] = dev->core->pll_addr;
218 dvb_pll_configure(dev->core->pll_desc, &pllbuf[1],
219 params->frequency, 0);
220 return 0;
221}
222
Michael Krufky0ccef6d2005-07-27 11:45:55 -0700223static int lgdt3302_pll_rf_set(struct dvb_frontend* fe, int index)
224{
225 struct cx8802_dev *dev= fe->dvb->priv;
226 struct cx88_core *core = dev->core;
227
228 dprintk(1, "%s: index = %d\n", __FUNCTION__, index);
229 if (index == 0)
230 cx_clear(MO_GP0_IO, 8);
231 else
232 cx_set(MO_GP0_IO, 8);
233 return 0;
234}
235
Michael Krufkyf1798492005-07-07 17:58:39 -0700236static int lgdt3302_set_ts_param(struct dvb_frontend* fe, int is_punctured)
237{
238 struct cx8802_dev *dev= fe->dvb->priv;
239 if (is_punctured)
240 dev->ts_gen_cntrl |= 0x04;
241 else
242 dev->ts_gen_cntrl &= ~0x04;
243 return 0;
244}
245
Michael Krufkyb6aef072005-07-27 11:45:54 -0700246static struct lgdt3302_config fusionhdtv_3_gold = {
Michael Krufkyf1798492005-07-07 17:58:39 -0700247 .demod_address = 0x0e,
Michael Krufkyb6aef072005-07-27 11:45:54 -0700248 .pll_set = lgdt3302_pll_set,
Michael Krufky0d723c02005-07-07 17:58:42 -0700249 .set_ts_params = lgdt3302_set_ts_param,
250};
Michael Krufkyf1798492005-07-07 17:58:39 -0700251#endif
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static int dvb_register(struct cx8802_dev *dev)
254{
255 /* init struct videobuf_dvb */
256 dev->dvb.name = dev->core->name;
257 dev->ts_gen_cntrl = 0x0c;
258
259 /* init frontend */
260 switch (dev->core->board) {
Michael Krufky29780bb2005-07-27 11:45:59 -0700261#ifdef HAVE_CX22702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 case CX88_BOARD_HAUPPAUGE_DVB_T1:
263 dev->dvb.frontend = cx22702_attach(&hauppauge_novat_config,
264 &dev->core->i2c_adap);
265 break;
Michael Krufkye057ee12005-07-07 17:58:40 -0700266 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 case CX88_BOARD_CONEXANT_DVB_T1:
268 dev->dvb.frontend = cx22702_attach(&connexant_refboard_config,
269 &dev->core->i2c_adap);
270 break;
271#endif
Michael Krufky29780bb2005-07-27 11:45:59 -0700272#ifdef HAVE_MT352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T1:
274 dev->core->pll_addr = 0x61;
275 dev->core->pll_desc = &dvb_pll_lg_z201;
276 dev->dvb.frontend = mt352_attach(&dvico_fusionhdtv,
277 &dev->core->i2c_adap);
278 break;
279 case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PLUS:
280 dev->core->pll_addr = 0x60;
281 dev->core->pll_desc = &dvb_pll_thomson_dtt7579;
282 dev->dvb.frontend = mt352_attach(&dvico_fusionhdtv,
283 &dev->core->i2c_adap);
284 break;
285 case CX88_BOARD_KWORLD_DVB_T:
286 case CX88_BOARD_DNTV_LIVE_DVB_T:
Mauro Carvalho Chehaba82decf2005-07-07 17:58:36 -0700287 case CX88_BOARD_ADSTECH_DVB_T_PCI:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 dev->core->pll_addr = 0x61;
289 dev->core->pll_desc = &dvb_pll_unknown_1;
290 dev->dvb.frontend = mt352_attach(&dntv_live_dvbt_config,
291 &dev->core->i2c_adap);
292 break;
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -0700293#endif
Michael Krufky29780bb2005-07-27 11:45:59 -0700294#ifdef HAVE_OR51132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 case CX88_BOARD_PCHDTV_HD3000:
296 dev->dvb.frontend = or51132_attach(&pchdtv_hd3000,
297 &dev->core->i2c_adap);
298 break;
299#endif
Michael Krufky29780bb2005-07-27 11:45:59 -0700300#ifdef HAVE_LGDT3302
Michael Krufkyf1798492005-07-07 17:58:39 -0700301 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
302 dev->ts_gen_cntrl = 0x08;
303 {
304 /* Do a hardware reset of chip before using it. */
305 struct cx88_core *core = dev->core;
306
307 cx_clear(MO_GP0_IO, 1);
308 mdelay(100);
Michael Krufky0ccef6d2005-07-27 11:45:55 -0700309 cx_set(MO_GP0_IO, 1);
Michael Krufkyf1798492005-07-07 17:58:39 -0700310 mdelay(200);
Michael Krufky0ccef6d2005-07-27 11:45:55 -0700311
312 /* Select RF connector callback */
313 fusionhdtv_3_gold.pll_rf_set = lgdt3302_pll_rf_set;
Michael Krufkyb6aef072005-07-27 11:45:54 -0700314 dev->core->pll_addr = 0x61;
315 dev->core->pll_desc = &dvb_pll_microtune_4042;
316 dev->dvb.frontend = lgdt3302_attach(&fusionhdtv_3_gold,
Michael Krufkyf1798492005-07-07 17:58:39 -0700317 &dev->core->i2c_adap);
318 }
319 break;
Michael Krufky0d723c02005-07-07 17:58:42 -0700320 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
321 dev->ts_gen_cntrl = 0x08;
322 {
323 /* Do a hardware reset of chip before using it. */
324 struct cx88_core *core = dev->core;
325
326 cx_clear(MO_GP0_IO, 1);
327 mdelay(100);
Michael Krufkyd9758722005-07-27 11:45:56 -0700328 cx_set(MO_GP0_IO, 9);
Michael Krufky0d723c02005-07-07 17:58:42 -0700329 mdelay(200);
Michael Krufkyb6aef072005-07-27 11:45:54 -0700330 dev->core->pll_addr = 0x61;
331 dev->core->pll_desc = &dvb_pll_thomson_dtt7611;
332 dev->dvb.frontend = lgdt3302_attach(&fusionhdtv_3_gold,
Michael Krufky0d723c02005-07-07 17:58:42 -0700333 &dev->core->i2c_adap);
334 }
335 break;
Michael Krufkyf1798492005-07-07 17:58:39 -0700336#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 default:
Gerd Knorr1622c3f2005-05-01 08:59:19 -0700338 printk("%s: The frontend of your DVB/ATSC card isn't supported yet\n",
339 dev->core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 break;
341 }
342 if (NULL == dev->dvb.frontend) {
343 printk("%s: frontend initialization failed\n",dev->core->name);
344 return -1;
345 }
346
347 if (dev->core->pll_desc) {
348 dev->dvb.frontend->ops->info.frequency_min = dev->core->pll_desc->min;
349 dev->dvb.frontend->ops->info.frequency_max = dev->core->pll_desc->max;
350 }
351
352 /* Copy the board name into the DVB structure */
353 strlcpy(dev->dvb.frontend->ops->info.name,
354 cx88_boards[dev->core->board].name,
355 sizeof(dev->dvb.frontend->ops->info.name));
356
357 /* register everything */
358 return videobuf_dvb_register(&dev->dvb, THIS_MODULE, dev);
359}
360
361/* ----------------------------------------------------------- */
362
363static int __devinit dvb_probe(struct pci_dev *pci_dev,
364 const struct pci_device_id *pci_id)
365{
366 struct cx8802_dev *dev;
367 struct cx88_core *core;
368 int err;
369
370 /* general setup */
371 core = cx88_core_get(pci_dev);
372 if (NULL == core)
373 return -EINVAL;
374
375 err = -ENODEV;
376 if (!cx88_boards[core->board].dvb)
377 goto fail_core;
378
379 err = -ENOMEM;
380 dev = kmalloc(sizeof(*dev),GFP_KERNEL);
381 if (NULL == dev)
382 goto fail_core;
383 memset(dev,0,sizeof(*dev));
384 dev->pci = pci_dev;
385 dev->core = core;
386
387 err = cx8802_init_common(dev);
388 if (0 != err)
389 goto fail_free;
390
391 /* dvb stuff */
392 printk("%s/2: cx2388x based dvb card\n", core->name);
393 videobuf_queue_init(&dev->dvb.dvbq, &dvb_qops,
394 dev->pci, &dev->slock,
395 V4L2_BUF_TYPE_VIDEO_CAPTURE,
396 V4L2_FIELD_TOP,
397 sizeof(struct cx88_buffer),
398 dev);
399 err = dvb_register(dev);
400 if (0 != err)
Gerd Knorr1622c3f2005-05-01 08:59:19 -0700401 goto fail_fini;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return 0;
403
Gerd Knorr1622c3f2005-05-01 08:59:19 -0700404 fail_fini:
405 cx8802_fini_common(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 fail_free:
407 kfree(dev);
408 fail_core:
409 cx88_core_put(core,pci_dev);
410 return err;
411}
412
413static void __devexit dvb_remove(struct pci_dev *pci_dev)
414{
415 struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
416
417 /* dvb */
418 videobuf_dvb_unregister(&dev->dvb);
419
420 /* common */
421 cx8802_fini_common(dev);
422 cx88_core_put(dev->core,dev->pci);
423 kfree(dev);
424}
425
426static struct pci_device_id cx8802_pci_tbl[] = {
427 {
428 .vendor = 0x14f1,
429 .device = 0x8802,
430 .subvendor = PCI_ANY_ID,
431 .subdevice = PCI_ANY_ID,
432 },{
433 /* --- end of list --- */
434 }
435};
436MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
437
438static struct pci_driver dvb_pci_driver = {
439 .name = "cx88-dvb",
440 .id_table = cx8802_pci_tbl,
441 .probe = dvb_probe,
442 .remove = __devexit_p(dvb_remove),
443 .suspend = cx8802_suspend_common,
444 .resume = cx8802_resume_common,
445};
446
447static int dvb_init(void)
448{
449 printk(KERN_INFO "cx2388x dvb driver version %d.%d.%d loaded\n",
450 (CX88_VERSION_CODE >> 16) & 0xff,
451 (CX88_VERSION_CODE >> 8) & 0xff,
452 CX88_VERSION_CODE & 0xff);
453#ifdef SNAPSHOT
454 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
455 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
456#endif
457 return pci_register_driver(&dvb_pci_driver);
458}
459
460static void dvb_fini(void)
461{
462 pci_unregister_driver(&dvb_pci_driver);
463}
464
465module_init(dvb_init);
466module_exit(dvb_fini);
467
468/*
469 * Local variables:
470 * c-basic-offset: 8
471 * compile-command: "make DVB=1"
472 * End:
473 */