3 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
5 * Extended 3 / 2005 by Hartmut Hackmann to support various
6 * cards with the tda10046 DVB-T channel decoder
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
29 #include <linux/kthread.h>
30 #include <linux/suspend.h>
33 #include "saa7134-reg.h"
38 # include "mt352_priv.h" /* FIXME */
41 # include "tda1004x.h"
44 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
45 MODULE_LICENSE("GPL");
47 static unsigned int antenna_pwr = 0;
49 module_param(antenna_pwr, int, 0444);
50 MODULE_PARM_DESC(antenna_pwr,"enable antenna power (Pinnacle 300i)");
52 /* ------------------------------------------------------------------ */
55 static int pinnacle_antenna_pwr(struct saa7134_dev *dev, int on)
60 saa_setl(SAA7134_GPIO_GPMODE0 >> 2, (1 << 26));
61 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 26));
65 saa_setl(SAA7134_GPIO_GPMODE0 >> 2, (1 << 26));
66 saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 26));
69 saa_setl(SAA7134_GPIO_GPMODE0 >> 2, (1 << 28));
70 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 28));
72 saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 28));
74 ok = saa_readl(SAA7134_GPIO_GPSTATUS0) & (1 << 27);
75 printk("%s: %s %s\n", dev->name, __FUNCTION__,
79 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 26));
83 static int mt352_pinnacle_init(struct dvb_frontend* fe)
85 static u8 clock_config [] = { CLOCK_CTL, 0x3d, 0x28 };
86 static u8 reset [] = { RESET, 0x80 };
87 static u8 adc_ctl_1_cfg [] = { ADC_CTL_1, 0x40 };
88 static u8 agc_cfg [] = { AGC_TARGET, 0x28, 0xa0 };
89 static u8 capt_range_cfg[] = { CAPT_RANGE, 0x31 };
90 static u8 fsm_ctl_cfg[] = { 0x7b, 0x04 };
91 static u8 gpp_ctl_cfg [] = { GPP_CTL, 0x0f };
92 static u8 scan_ctl_cfg [] = { SCAN_CTL, 0x0d };
93 static u8 irq_cfg [] = { INTERRUPT_EN_0, 0x00, 0x00, 0x00, 0x00 };
94 struct saa7134_dev *dev= fe->dvb->priv;
96 printk("%s: %s called\n",dev->name,__FUNCTION__);
98 mt352_write(fe, clock_config, sizeof(clock_config));
100 mt352_write(fe, reset, sizeof(reset));
101 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
102 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
103 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
104 mt352_write(fe, gpp_ctl_cfg, sizeof(gpp_ctl_cfg));
106 mt352_write(fe, fsm_ctl_cfg, sizeof(fsm_ctl_cfg));
107 mt352_write(fe, scan_ctl_cfg, sizeof(scan_ctl_cfg));
108 mt352_write(fe, irq_cfg, sizeof(irq_cfg));
112 static int mt352_pinnacle_pll_set(struct dvb_frontend* fe,
113 struct dvb_frontend_parameters* params,
116 static int on = TDA9887_PRESENT | TDA9887_PORT2_INACTIVE;
117 static int off = TDA9887_PRESENT | TDA9887_PORT2_ACTIVE;
118 struct saa7134_dev *dev = fe->dvb->priv;
119 struct v4l2_frequency f;
121 /* set frequency (mt2050) */
123 f.type = V4L2_TUNER_DIGITAL_TV;
124 f.frequency = params->frequency / 1000 * 16 / 1000;
125 saa7134_i2c_call_clients(dev,TDA9887_SET_CONFIG,&on);
126 saa7134_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,&f);
127 saa7134_i2c_call_clients(dev,TDA9887_SET_CONFIG,&off);
129 pinnacle_antenna_pwr(dev, antenna_pwr);
132 mt352_pinnacle_init(fe);
141 static struct mt352_config pinnacle_300i = {
142 .demod_address = 0x3c >> 1,
146 .demod_init = mt352_pinnacle_init,
147 .pll_set = mt352_pinnacle_pll_set,
151 /* ------------------------------------------------------------------ */
154 static int philips_tu1216_pll_init(struct dvb_frontend *fe)
156 struct saa7134_dev *dev = fe->dvb->priv;
157 static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
158 struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
160 /* setup PLL configuration */
161 if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
168 static int philips_tu1216_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
170 struct saa7134_dev *dev = fe->dvb->priv;
172 struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tuner_buf,.len =
174 int tuner_frequency = 0;
177 /* determine charge pump */
178 tuner_frequency = params->frequency + 36166000;
179 if (tuner_frequency < 87000000)
181 else if (tuner_frequency < 130000000)
183 else if (tuner_frequency < 160000000)
185 else if (tuner_frequency < 200000000)
187 else if (tuner_frequency < 290000000)
189 else if (tuner_frequency < 420000000)
191 else if (tuner_frequency < 480000000)
193 else if (tuner_frequency < 620000000)
195 else if (tuner_frequency < 830000000)
197 else if (tuner_frequency < 895000000)
203 if (params->frequency < 49000000)
205 else if (params->frequency < 161000000)
207 else if (params->frequency < 444000000)
209 else if (params->frequency < 861000000)
214 /* setup PLL filter */
215 switch (params->u.ofdm.bandwidth) {
216 case BANDWIDTH_6_MHZ:
220 case BANDWIDTH_7_MHZ:
224 case BANDWIDTH_8_MHZ:
233 * ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
235 tuner_frequency = (((params->frequency / 1000) * 6) + 217496) / 1000;
237 /* setup tuner buffer */
238 tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
239 tuner_buf[1] = tuner_frequency & 0xff;
241 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
243 if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
250 static int philips_tu1216_request_firmware(struct dvb_frontend *fe,
251 const struct firmware **fw, char *name)
253 struct saa7134_dev *dev = fe->dvb->priv;
254 return request_firmware(fw, name, &dev->pci->dev);
257 static struct tda1004x_config philips_tu1216_config = {
259 .demod_address = 0x8,
262 .xtal_freq = TDA10046_XTAL_4M,
263 .agc_config = TDA10046_AGC_DEFAULT,
264 .if_freq = TDA10046_FREQ_3617,
265 .pll_init = philips_tu1216_pll_init,
266 .pll_set = philips_tu1216_pll_set,
268 .request_firmware = philips_tu1216_request_firmware,
271 /* ------------------------------------------------------------------ */
274 static int philips_fmd1216_pll_init(struct dvb_frontend *fe)
276 struct saa7134_dev *dev = fe->dvb->priv;
277 /* this message is to set up ATC and ALC */
278 static u8 fmd1216_init[] = { 0x0b, 0xdc, 0x9c, 0xa0 };
279 struct i2c_msg tuner_msg = {.addr = 0x61,.flags = 0,.buf = fmd1216_init,.len = sizeof(fmd1216_init) };
281 if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
288 static void philips_fmd1216_analog(struct dvb_frontend *fe)
290 struct saa7134_dev *dev = fe->dvb->priv;
291 /* this message actually turns the tuner back to analog mode */
292 static u8 fmd1216_init[] = { 0x0b, 0xdc, 0x9c, 0x60 };
293 struct i2c_msg tuner_msg = {.addr = 0x61,.flags = 0,.buf = fmd1216_init,.len = sizeof(fmd1216_init) };
295 i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
297 fmd1216_init[2] = 0x86;
298 fmd1216_init[3] = 0x54;
299 i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
303 static int philips_fmd1216_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
305 struct saa7134_dev *dev = fe->dvb->priv;
307 struct i2c_msg tuner_msg = {.addr = 0x61,.flags = 0,.buf = tuner_buf,.len =
309 int tuner_frequency = 0;
313 /* determine charge pump */
314 tuner_frequency = params->frequency + 36130000;
315 if (tuner_frequency < 87000000)
318 else if (tuner_frequency < 180000000) {
322 } else if (tuner_frequency < 195000000) {
327 } else if (tuner_frequency < 366000000) {
328 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
335 } else if (tuner_frequency < 478000000) {
336 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
344 } else if (tuner_frequency < 662000000) {
345 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
352 } else if (tuner_frequency < 840000000) {
353 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
361 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
370 /* calculate divisor */
371 /* ((36166000 + Finput) / 166666) rounded! */
372 divider = (tuner_frequency + 83333) / 166667;
374 /* setup tuner buffer */
375 tuner_buf[0] = (divider >> 8) & 0x7f;
376 tuner_buf[1] = divider & 0xff;
377 tuner_buf[2] = 0x80 | (cp << 6) | (mode << 3) | 4;
378 tuner_buf[3] = 0x40 | band;
380 if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
386 static struct tda1004x_config medion_cardbus = {
387 .demod_address = 0x08,
390 .xtal_freq = TDA10046_XTAL_16M,
391 .agc_config = TDA10046_AGC_IFO_AUTO_NEG,
392 .if_freq = TDA10046_FREQ_3613,
393 .pll_init = philips_fmd1216_pll_init,
394 .pll_set = philips_fmd1216_pll_set,
395 .pll_sleep = philips_fmd1216_analog,
396 .request_firmware = NULL,
400 /* ------------------------------------------------------------------ */
402 struct tda827x_data {
412 static struct tda827x_data tda827x_dvbt[] = {
413 { .lomax = 62000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
414 { .lomax = 66000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
415 { .lomax = 76000000, .spd = 3, .bs = 1, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
416 { .lomax = 84000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
417 { .lomax = 93000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0},
418 { .lomax = 98000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0},
419 { .lomax = 109000000, .spd = 3, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
420 { .lomax = 123000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1},
421 { .lomax = 133000000, .spd = 2, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1},
422 { .lomax = 151000000, .spd = 2, .bs = 1, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
423 { .lomax = 154000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
424 { .lomax = 181000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 0, .div1p5 = 0},
425 { .lomax = 185000000, .spd = 2, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
426 { .lomax = 217000000, .spd = 2, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
427 { .lomax = 244000000, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1},
428 { .lomax = 265000000, .spd = 1, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1},
429 { .lomax = 302000000, .spd = 1, .bs = 1, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
430 { .lomax = 324000000, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
431 { .lomax = 370000000, .spd = 1, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
432 { .lomax = 454000000, .spd = 1, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
433 { .lomax = 493000000, .spd = 0, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1},
434 { .lomax = 530000000, .spd = 0, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1},
435 { .lomax = 554000000, .spd = 0, .bs = 1, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
436 { .lomax = 604000000, .spd = 0, .bs = 1, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
437 { .lomax = 696000000, .spd = 0, .bs = 2, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
438 { .lomax = 740000000, .spd = 0, .bs = 2, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0},
439 { .lomax = 820000000, .spd = 0, .bs = 3, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
440 { .lomax = 865000000, .spd = 0, .bs = 3, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0},
441 { .lomax = 0, .spd = 0, .bs = 0, .bp = 0, .cp = 0, .gc3 = 0, .div1p5 = 0}
444 static int philips_tda827x_pll_init(struct dvb_frontend *fe)
449 static int philips_tda827x_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
451 struct saa7134_dev *dev = fe->dvb->priv;
454 struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tuner_buf,
455 .len = sizeof(tuner_buf) };
456 int i, tuner_freq, if_freq;
458 switch (params->u.ofdm.bandwidth) {
459 case BANDWIDTH_6_MHZ:
462 case BANDWIDTH_7_MHZ:
465 default: /* 8 MHz or Auto */
469 tuner_freq = params->frequency + if_freq;
472 while (tda827x_dvbt[i].lomax < tuner_freq) {
473 if(tda827x_dvbt[i + 1].lomax == 0)
478 N = ((tuner_freq + 125000) / 250000) << (tda827x_dvbt[i].spd + 2);
480 tuner_buf[1] = (N>>8) | 0x40;
481 tuner_buf[2] = N & 0xff;
484 tuner_buf[5] = (tda827x_dvbt[i].spd << 6) + (tda827x_dvbt[i].div1p5 << 5) +
485 (tda827x_dvbt[i].bs << 3) + tda827x_dvbt[i].bp;
486 tuner_buf[6] = (tda827x_dvbt[i].gc3 << 4) + 0x8f;
490 tuner_buf[10] = 0xff;
491 tuner_buf[11] = 0x00;
492 tuner_buf[12] = 0x00;
493 tuner_buf[13] = 0x40;
496 if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
500 /* correct CP value */
502 tuner_buf[1] = 0x50 + tda827x_dvbt[i].cp;
504 i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
509 static void philips_tda827x_pll_sleep(struct dvb_frontend *fe)
511 struct saa7134_dev *dev = fe->dvb->priv;
512 static u8 tda827x_sleep[] = { 0x30, 0xd0};
513 struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tda827x_sleep,
514 .len = sizeof(tda827x_sleep) };
515 i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
518 static struct tda1004x_config tda827x_lifeview_config = {
519 .demod_address = 0x08,
522 .xtal_freq = TDA10046_XTAL_16M,
523 .agc_config = TDA10046_AGC_TDA827X,
524 .if_freq = TDA10046_FREQ_045,
525 .pll_init = philips_tda827x_pll_init,
526 .pll_set = philips_tda827x_pll_set,
527 .pll_sleep = philips_tda827x_pll_sleep,
528 .request_firmware = NULL,
532 /* ------------------------------------------------------------------ */
534 static int dvb_init(struct saa7134_dev *dev)
536 /* init struct videobuf_dvb */
537 dev->ts.nr_bufs = 32;
538 dev->ts.nr_packets = 32*4;
539 dev->dvb.name = dev->name;
540 videobuf_queue_init(&dev->dvb.dvbq, &saa7134_ts_qops,
541 dev->pci, &dev->slock,
542 V4L2_BUF_TYPE_VIDEO_CAPTURE,
543 V4L2_FIELD_ALTERNATE,
544 sizeof(struct saa7134_buf),
547 switch (dev->board) {
549 case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
550 printk("%s: pinnacle 300i dvb setup\n",dev->name);
551 dev->dvb.frontend = mt352_attach(&pinnacle_300i,
556 case SAA7134_BOARD_MD7134:
557 dev->dvb.frontend = tda10046_attach(&medion_cardbus,
560 case SAA7134_BOARD_PHILIPS_TOUGH:
561 dev->dvb.frontend = tda10046_attach(&philips_tu1216_config,
564 case SAA7134_BOARD_FLYDVBTDUO:
565 dev->dvb.frontend = tda10046_attach(&tda827x_lifeview_config,
568 case SAA7134_BOARD_THYPHOON_DVBT_DUO_CARDBUS:
569 dev->dvb.frontend = tda10046_attach(&tda827x_lifeview_config,
574 printk("%s: Huh? unknown DVB card?\n",dev->name);
578 if (NULL == dev->dvb.frontend) {
579 printk("%s: frontend initialization failed\n",dev->name);
583 /* register everything else */
584 return videobuf_dvb_register(&dev->dvb, THIS_MODULE, dev);
587 static int dvb_fini(struct saa7134_dev *dev)
589 static int on = TDA9887_PRESENT | TDA9887_PORT2_INACTIVE;
591 switch (dev->board) {
592 case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
593 /* otherwise we don't detect the tuner on next insmod */
594 saa7134_i2c_call_clients(dev,TDA9887_SET_CONFIG,&on);
597 videobuf_dvb_unregister(&dev->dvb);
601 static struct saa7134_mpeg_ops dvb_ops = {
602 .type = SAA7134_MPEG_DVB,
607 static int __init dvb_register(void)
609 return saa7134_ts_register(&dvb_ops);
612 static void __exit dvb_unregister(void)
614 saa7134_ts_unregister(&dvb_ops);
617 module_init(dvb_register);
618 module_exit(dvb_unregister);
620 /* ------------------------------------------------------------------ */