blob: 78d223257a6888650238e1a660bdf8d27be321d6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Michael Krufky1963c902005-08-08 09:22:43 -07002 * $Id: cx88-dvb.c,v 1.58 2005/08/07 09:24:08 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "cx88.h"
35#include "dvb-pll.h"
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -070036
Michael Krufky29780bb2005-07-27 11:45:59 -070037#ifdef HAVE_MT352
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -070038# include "mt352.h"
39# include "mt352_priv.h"
40#endif
Michael Krufky29780bb2005-07-27 11:45:59 -070041#ifdef HAVE_CX22702
Linus Torvalds1da177e2005-04-16 15:20:36 -070042# include "cx22702.h"
43#endif
Michael Krufky29780bb2005-07-27 11:45:59 -070044#ifdef HAVE_OR51132
Linus Torvalds1da177e2005-04-16 15:20:36 -070045# include "or51132.h"
46#endif
Michael Krufky6ddcc912005-07-27 11:46:00 -070047#ifdef HAVE_LGDT330X
48# include "lgdt330x.h"
Michael Krufkyf1798492005-07-07 17:58:39 -070049#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51MODULE_DESCRIPTION("driver for cx2388x based DVB cards");
52MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
53MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
54MODULE_LICENSE("GPL");
55
56static unsigned int debug = 0;
57module_param(debug, int, 0644);
58MODULE_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
65static 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
78static 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
85static 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
91static 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
Adrian Bunk408b6642005-05-01 08:59:29 -070097static struct videobuf_queue_ops dvb_qops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 .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
Michael Krufky29780bb2005-07-27 11:45:59 -0700106#ifdef HAVE_MT352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static 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
127static 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
150static 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
163static struct mt352_config dvico_fusionhdtv = {
164 .demod_address = 0x0F,
165 .demod_init = dvico_fusionhdtv_demod_init,
166 .pll_set = mt352_pll_set,
167};
168
169static 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};
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -0700174#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Michael Krufky29780bb2005-07-27 11:45:59 -0700176#ifdef HAVE_CX22702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static struct cx22702_config connexant_refboard_config = {
178 .demod_address = 0x43,
Patrick Boettcher38d84c32005-07-15 12:20:26 -0700179 .output_mode = CX22702_SERIAL_OUTPUT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .pll_address = 0x60,
181 .pll_desc = &dvb_pll_thomson_dtt7579,
182};
183
184static struct cx22702_config hauppauge_novat_config = {
185 .demod_address = 0x43,
Patrick Boettcher38d84c32005-07-15 12:20:26 -0700186 .output_mode = CX22702_SERIAL_OUTPUT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 .pll_address = 0x61,
188 .pll_desc = &dvb_pll_thomson_dtt759x,
189};
190#endif
191
Michael Krufky29780bb2005-07-27 11:45:59 -0700192#ifdef HAVE_OR51132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static 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
Adrian Bunk408b6642005-05-01 08:59:29 -0700201static struct or51132_config pchdtv_hd3000 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 .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
Michael Krufky6ddcc912005-07-27 11:46:00 -0700209#ifdef HAVE_LGDT330X
210static int lgdt330x_pll_set(struct dvb_frontend* fe,
Michael Krufky1963c902005-08-08 09:22:43 -0700211 struct dvb_frontend_parameters* params)
Michael Krufkyb6aef072005-07-27 11:45:54 -0700212{
213 struct cx8802_dev *dev= fe->dvb->priv;
Michael Krufky1963c902005-08-08 09:22:43 -0700214 u8 buf[4];
215 struct i2c_msg msg =
216 { .addr = dev->core->pll_addr, .flags = 0, .buf = buf, .len = 4 };
217 int err;
Michael Krufkyb6aef072005-07-27 11:45:54 -0700218
Michael Krufky1963c902005-08-08 09:22:43 -0700219 dvb_pll_configure(dev->core->pll_desc, buf, params->frequency, 0);
220 dprintk(1, "%s: tuner at 0x%02x bytes: 0x%02x 0x%02x 0x%02x 0x%02x\n",
221 __FUNCTION__, msg.addr, buf[0],buf[1],buf[2],buf[3]);
222 if ((err = i2c_transfer(&dev->core->i2c_adap, &msg, 1)) != 1) {
223 printk(KERN_WARNING "cx88-dvb: %s error "
224 "(addr %02x <- %02x, err = %i)\n",
225 __FUNCTION__, buf[0], buf[1], err);
226 if (err < 0)
227 return err;
228 else
229 return -EREMOTEIO;
230 }
Michael Krufkyb6aef072005-07-27 11:45:54 -0700231 return 0;
232}
233
Michael Krufky6ddcc912005-07-27 11:46:00 -0700234static int lgdt330x_pll_rf_set(struct dvb_frontend* fe, int index)
Michael Krufky0ccef6d2005-07-27 11:45:55 -0700235{
236 struct cx8802_dev *dev= fe->dvb->priv;
237 struct cx88_core *core = dev->core;
238
239 dprintk(1, "%s: index = %d\n", __FUNCTION__, index);
240 if (index == 0)
241 cx_clear(MO_GP0_IO, 8);
242 else
243 cx_set(MO_GP0_IO, 8);
244 return 0;
245}
246
Michael Krufky6ddcc912005-07-27 11:46:00 -0700247static int lgdt330x_set_ts_param(struct dvb_frontend* fe, int is_punctured)
Michael Krufkyf1798492005-07-07 17:58:39 -0700248{
249 struct cx8802_dev *dev= fe->dvb->priv;
250 if (is_punctured)
251 dev->ts_gen_cntrl |= 0x04;
252 else
253 dev->ts_gen_cntrl &= ~0x04;
254 return 0;
255}
256
Michael Krufky6ddcc912005-07-27 11:46:00 -0700257static struct lgdt330x_config fusionhdtv_3_gold = {
Michael Krufkyf1798492005-07-07 17:58:39 -0700258 .demod_address = 0x0e,
Michael Krufky1963c902005-08-08 09:22:43 -0700259 .demod_chip = LGDT3302,
260 .serial_mpeg = 0x04, /* TPSERIAL for 3302 in TOP_CONTROL */
Michael Krufky6ddcc912005-07-27 11:46:00 -0700261 .pll_set = lgdt330x_pll_set,
262 .set_ts_params = lgdt330x_set_ts_param,
Michael Krufky0d723c02005-07-07 17:58:42 -0700263};
Michael Krufkyf1798492005-07-07 17:58:39 -0700264#endif
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static int dvb_register(struct cx8802_dev *dev)
267{
268 /* init struct videobuf_dvb */
269 dev->dvb.name = dev->core->name;
270 dev->ts_gen_cntrl = 0x0c;
271
272 /* init frontend */
273 switch (dev->core->board) {
Michael Krufky29780bb2005-07-27 11:45:59 -0700274#ifdef HAVE_CX22702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 case CX88_BOARD_HAUPPAUGE_DVB_T1:
276 dev->dvb.frontend = cx22702_attach(&hauppauge_novat_config,
277 &dev->core->i2c_adap);
278 break;
Michael Krufkye057ee12005-07-07 17:58:40 -0700279 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 case CX88_BOARD_CONEXANT_DVB_T1:
281 dev->dvb.frontend = cx22702_attach(&connexant_refboard_config,
282 &dev->core->i2c_adap);
283 break;
284#endif
Michael Krufky29780bb2005-07-27 11:45:59 -0700285#ifdef HAVE_MT352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T1:
287 dev->core->pll_addr = 0x61;
288 dev->core->pll_desc = &dvb_pll_lg_z201;
289 dev->dvb.frontend = mt352_attach(&dvico_fusionhdtv,
290 &dev->core->i2c_adap);
291 break;
292 case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PLUS:
293 dev->core->pll_addr = 0x60;
294 dev->core->pll_desc = &dvb_pll_thomson_dtt7579;
295 dev->dvb.frontend = mt352_attach(&dvico_fusionhdtv,
296 &dev->core->i2c_adap);
297 break;
298 case CX88_BOARD_KWORLD_DVB_T:
299 case CX88_BOARD_DNTV_LIVE_DVB_T:
Mauro Carvalho Chehaba82decf2005-07-07 17:58:36 -0700300 case CX88_BOARD_ADSTECH_DVB_T_PCI:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 dev->core->pll_addr = 0x61;
302 dev->core->pll_desc = &dvb_pll_unknown_1;
303 dev->dvb.frontend = mt352_attach(&dntv_live_dvbt_config,
304 &dev->core->i2c_adap);
305 break;
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -0700306#endif
Michael Krufky29780bb2005-07-27 11:45:59 -0700307#ifdef HAVE_OR51132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 case CX88_BOARD_PCHDTV_HD3000:
309 dev->dvb.frontend = or51132_attach(&pchdtv_hd3000,
310 &dev->core->i2c_adap);
311 break;
312#endif
Michael Krufky6ddcc912005-07-27 11:46:00 -0700313#ifdef HAVE_LGDT330X
Michael Krufkyf1798492005-07-07 17:58:39 -0700314 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
315 dev->ts_gen_cntrl = 0x08;
316 {
317 /* Do a hardware reset of chip before using it. */
318 struct cx88_core *core = dev->core;
319
320 cx_clear(MO_GP0_IO, 1);
321 mdelay(100);
Michael Krufky0ccef6d2005-07-27 11:45:55 -0700322 cx_set(MO_GP0_IO, 1);
Michael Krufkyf1798492005-07-07 17:58:39 -0700323 mdelay(200);
Michael Krufky0ccef6d2005-07-27 11:45:55 -0700324
325 /* Select RF connector callback */
Michael Krufky6ddcc912005-07-27 11:46:00 -0700326 fusionhdtv_3_gold.pll_rf_set = lgdt330x_pll_rf_set;
Michael Krufkyb6aef072005-07-27 11:45:54 -0700327 dev->core->pll_addr = 0x61;
328 dev->core->pll_desc = &dvb_pll_microtune_4042;
Michael Krufky6ddcc912005-07-27 11:46:00 -0700329 dev->dvb.frontend = lgdt330x_attach(&fusionhdtv_3_gold,
Michael Krufkyf1798492005-07-07 17:58:39 -0700330 &dev->core->i2c_adap);
331 }
332 break;
Michael Krufky0d723c02005-07-07 17:58:42 -0700333 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
334 dev->ts_gen_cntrl = 0x08;
335 {
336 /* Do a hardware reset of chip before using it. */
337 struct cx88_core *core = dev->core;
338
339 cx_clear(MO_GP0_IO, 1);
340 mdelay(100);
Michael Krufkyd9758722005-07-27 11:45:56 -0700341 cx_set(MO_GP0_IO, 9);
Michael Krufky0d723c02005-07-07 17:58:42 -0700342 mdelay(200);
Michael Krufkyb6aef072005-07-27 11:45:54 -0700343 dev->core->pll_addr = 0x61;
344 dev->core->pll_desc = &dvb_pll_thomson_dtt7611;
Michael Krufky6ddcc912005-07-27 11:46:00 -0700345 dev->dvb.frontend = lgdt330x_attach(&fusionhdtv_3_gold,
Michael Krufky0d723c02005-07-07 17:58:42 -0700346 &dev->core->i2c_adap);
347 }
348 break;
Michael Krufkyf1798492005-07-07 17:58:39 -0700349#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 default:
Gerd Knorr1622c3f2005-05-01 08:59:19 -0700351 printk("%s: The frontend of your DVB/ATSC card isn't supported yet\n",
352 dev->core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
354 }
355 if (NULL == dev->dvb.frontend) {
356 printk("%s: frontend initialization failed\n",dev->core->name);
357 return -1;
358 }
359
360 if (dev->core->pll_desc) {
361 dev->dvb.frontend->ops->info.frequency_min = dev->core->pll_desc->min;
362 dev->dvb.frontend->ops->info.frequency_max = dev->core->pll_desc->max;
363 }
364
365 /* Copy the board name into the DVB structure */
366 strlcpy(dev->dvb.frontend->ops->info.name,
367 cx88_boards[dev->core->board].name,
368 sizeof(dev->dvb.frontend->ops->info.name));
369
370 /* register everything */
371 return videobuf_dvb_register(&dev->dvb, THIS_MODULE, dev);
372}
373
374/* ----------------------------------------------------------- */
375
376static int __devinit dvb_probe(struct pci_dev *pci_dev,
377 const struct pci_device_id *pci_id)
378{
379 struct cx8802_dev *dev;
380 struct cx88_core *core;
381 int err;
382
383 /* general setup */
384 core = cx88_core_get(pci_dev);
385 if (NULL == core)
386 return -EINVAL;
387
388 err = -ENODEV;
389 if (!cx88_boards[core->board].dvb)
390 goto fail_core;
391
392 err = -ENOMEM;
393 dev = kmalloc(sizeof(*dev),GFP_KERNEL);
394 if (NULL == dev)
395 goto fail_core;
396 memset(dev,0,sizeof(*dev));
397 dev->pci = pci_dev;
398 dev->core = core;
399
400 err = cx8802_init_common(dev);
401 if (0 != err)
402 goto fail_free;
403
404 /* dvb stuff */
405 printk("%s/2: cx2388x based dvb card\n", core->name);
406 videobuf_queue_init(&dev->dvb.dvbq, &dvb_qops,
407 dev->pci, &dev->slock,
408 V4L2_BUF_TYPE_VIDEO_CAPTURE,
409 V4L2_FIELD_TOP,
410 sizeof(struct cx88_buffer),
411 dev);
412 err = dvb_register(dev);
413 if (0 != err)
Gerd Knorr1622c3f2005-05-01 08:59:19 -0700414 goto fail_fini;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return 0;
416
Gerd Knorr1622c3f2005-05-01 08:59:19 -0700417 fail_fini:
418 cx8802_fini_common(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 fail_free:
420 kfree(dev);
421 fail_core:
422 cx88_core_put(core,pci_dev);
423 return err;
424}
425
426static void __devexit dvb_remove(struct pci_dev *pci_dev)
427{
428 struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
429
430 /* dvb */
431 videobuf_dvb_unregister(&dev->dvb);
432
433 /* common */
434 cx8802_fini_common(dev);
435 cx88_core_put(dev->core,dev->pci);
436 kfree(dev);
437}
438
439static struct pci_device_id cx8802_pci_tbl[] = {
440 {
441 .vendor = 0x14f1,
442 .device = 0x8802,
443 .subvendor = PCI_ANY_ID,
444 .subdevice = PCI_ANY_ID,
445 },{
446 /* --- end of list --- */
447 }
448};
449MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
450
451static struct pci_driver dvb_pci_driver = {
452 .name = "cx88-dvb",
453 .id_table = cx8802_pci_tbl,
454 .probe = dvb_probe,
455 .remove = __devexit_p(dvb_remove),
456 .suspend = cx8802_suspend_common,
457 .resume = cx8802_resume_common,
458};
459
460static int dvb_init(void)
461{
462 printk(KERN_INFO "cx2388x dvb driver version %d.%d.%d loaded\n",
463 (CX88_VERSION_CODE >> 16) & 0xff,
464 (CX88_VERSION_CODE >> 8) & 0xff,
465 CX88_VERSION_CODE & 0xff);
466#ifdef SNAPSHOT
467 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
468 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
469#endif
470 return pci_register_driver(&dvb_pci_driver);
471}
472
473static void dvb_fini(void)
474{
475 pci_unregister_driver(&dvb_pci_driver);
476}
477
478module_init(dvb_init);
479module_exit(dvb_fini);
480
481/*
482 * Local variables:
483 * c-basic-offset: 8
484 * compile-command: "make DVB=1"
485 * End:
486 */