1 #include <linux/module.h>
2 #include <linux/kernel.h>
4 #include <linux/types.h>
5 #include <linux/videodev.h>
6 #include <linux/init.h>
7 #include <linux/errno.h>
8 #include <linux/slab.h>
10 #include <media/audiochip.h>
11 #include <media/tuner.h>
16 TDA9886 (PAL, SECAM, NTSC)
17 TDA9887 (PAL, SECAM, NTSC, FM Radio)
20 - Pinnacle PCTV (Jul.2002 Version with MT2032, bttv)
21 TDA9887 (world), TDA9885 (USA)
22 Note: OP2 of tda988x must be set to 1, else MT2032 is disabled!
23 - KNC One TV-Station RDS (saa7134)
27 /* Addresses to scan */
28 static unsigned short normal_i2c[] = {
33 static unsigned short normal_i2c_range[] = {I2C_CLIENT_END,I2C_CLIENT_END};
37 static unsigned int debug = 0;
38 MODULE_PARM(debug,"i");
39 MODULE_LICENSE("GPL");
41 /* ---------------------------------------------------------------------- */
44 #define PREFIX "tda9885/6/7: "
45 #define dprintk if (debug) printk
48 struct i2c_client client;
52 unsigned int pinnacle_id;
53 unsigned int using_v4l2;
64 static struct i2c_driver driver;
65 static struct i2c_client client_template;
67 /* ---------------------------------------------------------------------- */
74 #define cVideoTrapBypassOFF 0x00 // bit b0
75 #define cVideoTrapBypassON 0x01 // bit b0
77 #define cAutoMuteFmInactive 0x00 // bit b1
78 #define cAutoMuteFmActive 0x02 // bit b1
80 #define cIntercarrier 0x00 // bit b2
81 #define cQSS 0x04 // bit b2
83 #define cPositiveAmTV 0x00 // bit b3:4
84 #define cFmRadio 0x08 // bit b3:4
85 #define cNegativeFmTV 0x10 // bit b3:4
88 #define cForcedMuteAudioON 0x20 // bit b5
89 #define cForcedMuteAudioOFF 0x00 // bit b5
91 #define cOutputPort1Active 0x00 // bit b6
92 #define cOutputPort1Inactive 0x40 // bit b6
94 #define cOutputPort2Active 0x00 // bit b7
95 #define cOutputPort2Inactive 0x80 // bit b7
99 #define cDeemphasisOFF 0x00 // bit c5
100 #define cDeemphasisON 0x20 // bit c5
102 #define cDeemphasis75 0x00 // bit c6
103 #define cDeemphasis50 0x40 // bit c6
105 #define cAudioGain0 0x00 // bit c7
106 #define cAudioGain6 0x80 // bit c7
110 #define cAudioIF_4_5 0x00 // bit e0:1
111 #define cAudioIF_5_5 0x01 // bit e0:1
112 #define cAudioIF_6_0 0x02 // bit e0:1
113 #define cAudioIF_6_5 0x03 // bit e0:1
116 #define cVideoIF_58_75 0x00 // bit e2:4
117 #define cVideoIF_45_75 0x04 // bit e2:4
118 #define cVideoIF_38_90 0x08 // bit e2:4
119 #define cVideoIF_38_00 0x0C // bit e2:4
120 #define cVideoIF_33_90 0x10 // bit e2:4
121 #define cVideoIF_33_40 0x14 // bit e2:4
122 #define cRadioIF_45_75 0x18 // bit e2:4
123 #define cRadioIF_38_90 0x1C // bit e2:4
126 #define cTunerGainNormal 0x00 // bit e5
127 #define cTunerGainLow 0x20 // bit e5
129 #define cGating_18 0x00 // bit e6
130 #define cGating_36 0x40 // bit e6
132 #define cAgcOutON 0x80 // bit e7
133 #define cAgcOutOFF 0x00 // bit e7
135 /* ---------------------------------------------------------------------- */
137 static struct tvnorm tvnorms[] = {
139 .std = V4L2_STD_PAL_BG,
141 .b = ( cNegativeFmTV |
143 .c = ( cDeemphasisON |
145 .e = ( cAudioIF_5_5 |
148 .std = V4L2_STD_PAL_I,
150 .b = ( cNegativeFmTV |
152 .c = ( cDeemphasisON |
154 .e = ( cAudioIF_6_0 |
157 .std = V4L2_STD_PAL_DK,
159 .b = ( cNegativeFmTV |
161 .c = ( cDeemphasisON |
163 .e = ( cAudioIF_6_5 |
166 .std = V4L2_STD_PAL_M | V4L2_STD_PAL_N,
168 .b = ( cNegativeFmTV |
170 .c = ( cDeemphasisON |
172 .e = ( cAudioIF_4_5 |
175 .std = V4L2_STD_SECAM_L,
177 .b = ( cPositiveAmTV |
179 .e = ( cAudioIF_6_5 |
182 .std = V4L2_STD_SECAM_DK,
184 .b = ( cNegativeFmTV |
186 .c = ( cDeemphasisON |
188 .e = ( cAudioIF_6_5 |
191 .std = V4L2_STD_NTSC_M,
193 .b = ( cNegativeFmTV |
195 .c = ( cDeemphasisON |
201 .std = V4L2_STD_NTSC_M_JP,
203 .b = ( cNegativeFmTV |
205 .c = ( cDeemphasisON |
213 static struct tvnorm radio = {
217 .c = ( cDeemphasisON |
219 .e = ( cAudioIF_5_5 |
223 /* ---------------------------------------------------------------------- */
225 static void dump_read_message(unsigned char *buf)
227 static char *afc[16] = {
245 printk(PREFIX "read: 0x%2x\n", buf[0]);
246 printk(" after power on : %s\n", (buf[0] & 0x01) ? "yes" : "no");
247 printk(" afc : %s\n", afc[(buf[0] >> 1) & 0x0f]);
248 printk(" afc window : %s\n", (buf[0] & 0x40) ? "in" : "out");
249 printk(" vfi level : %s\n", (buf[0] & 0x80) ? "high" : "low");
252 static void dump_write_message(unsigned char *buf)
254 static char *sound[4] = {
260 static char *adjust[32] = {
261 "-16", "-15", "-14", "-13", "-12", "-11", "-10", "-9",
262 "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1",
263 "0", "+1", "+2", "+3", "+4", "+5", "+6", "+7",
264 "+8", "+9", "+10", "+11", "+12", "+13", "+14", "+15"
266 static char *deemph[4] = {
267 "no", "no", "75", "50"
269 static char *carrier[4] = {
275 static char *vif[8] = {
285 static char *rif[4] = {
292 printk(PREFIX "write: byte B 0x%02x\n",buf[1]);
293 printk(" B0 video mode : %s\n",
294 (buf[1] & 0x01) ? "video trap" : "sound trap");
295 printk(" B1 auto mute fm : %s\n",
296 (buf[1] & 0x02) ? "yes" : "no");
297 printk(" B2 carrier mode : %s\n",
298 (buf[1] & 0x04) ? "QSS" : "Intercarrier");
299 printk(" B3-4 tv sound/radio : %s\n",
300 sound[(buf[1] & 0x18) >> 3]);
301 printk(" B5 force mute audio: %s\n",
302 (buf[1] & 0x20) ? "yes" : "no");
303 printk(" B6 output port 1 : %s\n",
304 (buf[1] & 0x40) ? "high" : "low");
305 printk(" B7 output port 2 : %s\n",
306 (buf[1] & 0x80) ? "high" : "low");
308 printk(PREFIX "write: byte C 0x%02x\n",buf[2]);
309 printk(" C0-4 top adjustment : %s dB\n", adjust[buf[2] & 0x1f]);
310 printk(" C5-6 de-emphasis : %s\n", deemph[(buf[2] & 0x60) >> 5]);
311 printk(" C7 audio gain : %s\n",
312 (buf[2] & 0x80) ? "-6" : "0");
314 printk(PREFIX "write: byte E 0x%02x\n",buf[3]);
315 printk(" E0-1 sound carrier : %s\n",
316 carrier[(buf[3] & 0x03)]);
317 printk(" E6 l pll ganting : %s\n",
318 (buf[3] & 0x40) ? "36" : "13");
322 printk(" E2-4 video if : %s\n",
323 rif[(buf[3] & 0x0c) >> 2]);
324 printk(" E7 vif agc output : %s\n",
326 ? ((buf[3] & 0x10) ? "fm-agc radio" : "sif-agc radio")
327 : "fm radio carrier afc");
330 printk(" E2-4 video if : %s\n",
331 vif[(buf[3] & 0x1c) >> 2]);
332 printk(" E5 tuner gain : %s\n",
334 ? ((buf[3] & 0x20) ? "external" : "normal")
335 : ((buf[3] & 0x20) ? "minimum" : "normal"));
336 printk(" E7 vif agc output : %s\n",
339 ? "pin3 port, pin22 vif agc out"
340 : "pin22 port, pin3 vif acg ext in")
341 : "pin3+pin22 port");
346 /* ---------------------------------------------------------------------- */
348 static int tda9887_set_tvnorm(struct tda9887 *t, char *buf)
350 struct tvnorm *norm = NULL;
356 for (i = 0; i < ARRAY_SIZE(tvnorms); i++) {
357 if (tvnorms[i].std & t->std) {
364 dprintk(PREFIX "Oops: no tvnorm entry found\n");
368 dprintk(PREFIX "configure for: %s\n",norm->name);
375 static unsigned int port1 = 1;
376 static unsigned int port2 = 1;
377 static unsigned int qss = UNSET;
378 static unsigned int adjust = 0x10;
379 MODULE_PARM(port1,"i");
380 MODULE_PARM(port2,"i");
381 MODULE_PARM(qss,"i");
382 MODULE_PARM(adjust,"i");
384 static int tda9887_set_insmod(struct tda9887 *t, char *buf)
387 buf[1] |= cOutputPort1Inactive;
389 buf[1] |= cOutputPort2Inactive;
397 if (adjust >= 0x00 && adjust < 0x20)
402 static int tda9887_set_config(struct tda9887 *t, char *buf)
404 if (t->config & TDA9887_PORT1)
405 buf[1] |= cOutputPort1Inactive;
406 if (t->config & TDA9887_PORT2)
407 buf[1] |= cOutputPort2Inactive;
408 if (t->config & TDA9887_QSS)
410 if (t->config & TDA9887_INTERCARRIER)
413 if (t->config & TDA9887_AUTOMUTE)
414 buf[1] |= cAutoMuteFmActive;
415 if (t->config & TDA9887_DEEMPHASIS_MASK) {
417 switch (t->config & TDA9887_DEEMPHASIS_MASK) {
418 case TDA9887_DEEMPHASIS_NONE:
419 buf[2] |= cDeemphasisOFF;
421 case TDA9887_DEEMPHASIS_50:
422 buf[2] |= cDeemphasisON | cDeemphasis50;
424 case TDA9887_DEEMPHASIS_75:
425 buf[2] |= cDeemphasisON | cDeemphasis75;
432 /* ---------------------------------------------------------------------- */
434 static int tda9887_set_pinnacle(struct tda9887 *t, char *buf)
436 unsigned int bCarrierMode = UNSET;
438 if (t->std & V4L2_STD_PAL) {
439 if ((1 == t->pinnacle_id) || (7 == t->pinnacle_id)) {
440 bCarrierMode = cIntercarrier;
445 if (t->std & V4L2_STD_NTSC) {
446 if ((5 == t->pinnacle_id) || (6 == t->pinnacle_id)) {
447 bCarrierMode = cIntercarrier;
453 if (bCarrierMode != UNSET) {
455 buf[1] |= bCarrierMode;
460 /* ---------------------------------------------------------------------- */
462 static char *pal = "-";
463 MODULE_PARM(pal,"s");
464 static char *secam = "-";
465 MODULE_PARM(secam,"s");
467 static int tda9887_fixup_std(struct tda9887 *t)
469 /* get more precise norm info from insmod option */
470 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
476 dprintk(PREFIX "insmod fixup: PAL => PAL-BG\n");
477 t->std = V4L2_STD_PAL_BG;
481 dprintk(PREFIX "insmod fixup: PAL => PAL-I\n");
482 t->std = V4L2_STD_PAL_I;
488 dprintk(PREFIX "insmod fixup: PAL => PAL-DK\n");
489 t->std = V4L2_STD_PAL_DK;
493 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
499 dprintk(PREFIX "insmod fixup: SECAM => SECAM-DK\n");
500 t->std = V4L2_STD_SECAM_DK;
504 dprintk(PREFIX "insmod fixup: SECAM => SECAM-L\n");
505 t->std = V4L2_STD_SECAM_L;
512 static int tda9887_status(struct tda9887 *t)
514 unsigned char buf[1];
517 memset(buf,0,sizeof(buf));
518 if (1 != (rc = i2c_master_recv(&t->client,buf,1)))
519 printk(PREFIX "i2c i/o error: rc == %d (should be 1)\n",rc);
520 dump_read_message(buf);
524 static int tda9887_configure(struct tda9887 *t)
526 unsigned char buf[4];
529 memset(buf,0,sizeof(buf));
530 tda9887_set_tvnorm(t,buf);
531 if (UNSET != t->pinnacle_id) {
532 tda9887_set_pinnacle(t,buf);
534 tda9887_set_config(t,buf);
535 tda9887_set_insmod(t,buf);
537 dprintk(PREFIX "writing: b=0x%02x c=0x%02x e=0x%02x\n",
538 buf[1],buf[2],buf[3]);
540 dump_write_message(buf);
542 if (4 != (rc = i2c_master_send(&t->client,buf,4)))
543 printk(PREFIX "i2c i/o error: rc == %d (should be 4)\n",rc);
546 set_current_state(TASK_INTERRUPTIBLE);
547 schedule_timeout(HZ);
553 /* ---------------------------------------------------------------------- */
555 static int tda9887_attach(struct i2c_adapter *adap, int addr, int kind)
559 client_template.adapter = adap;
560 client_template.addr = addr;
562 printk(PREFIX "chip found @ 0x%x\n", addr<<1);
564 if (NULL == (t = kmalloc(sizeof(*t), GFP_KERNEL)))
566 memset(t,0,sizeof(*t));
567 t->client = client_template;
569 t->pinnacle_id = UNSET;
570 i2c_set_clientdata(&t->client, t);
571 i2c_attach_client(&t->client);
576 static int tda9887_probe(struct i2c_adapter *adap)
578 #ifdef I2C_CLASS_TV_ANALOG
579 if (adap->class & I2C_CLASS_TV_ANALOG)
580 return i2c_probe(adap, &addr_data, tda9887_attach);
583 case I2C_ALGO_BIT | I2C_HW_B_BT848:
584 case I2C_ALGO_BIT | I2C_HW_B_RIVA:
585 case I2C_ALGO_SAA7134:
586 return i2c_probe(adap, &addr_data, tda9887_attach);
593 static int tda9887_detach(struct i2c_client *client)
595 struct tda9887 *t = i2c_get_clientdata(client);
597 i2c_detach_client(client);
602 #define SWITCH_V4L2 if (!t->using_v4l2 && debug) \
603 printk(PREFIX "switching to v4l2\n"); \
605 #define CHECK_V4L2 if (t->using_v4l2) { if (debug) \
606 printk(PREFIX "ignore v4l1 call\n"); \
610 tda9887_command(struct i2c_client *client, unsigned int cmd, void *arg)
612 struct tda9887 *t = i2c_get_clientdata(client);
616 /* --- configuration --- */
619 tda9887_configure(t);
622 case AUDC_CONFIG_PINNACLE:
627 tda9887_configure(t);
630 case TDA9887_SET_CONFIG:
635 tda9887_configure(t);
638 /* --- v4l ioctls --- */
639 /* take care: bttv does userspace copying, we'll get a
640 kernel pointer here... */
643 static const v4l2_std_id map[] = {
644 [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
645 [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
646 [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
647 [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
648 [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
649 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
651 struct video_channel *vc = arg;
655 if (vc->norm < ARRAY_SIZE(map))
656 t->std = map[vc->norm];
657 tda9887_fixup_std(t);
658 tda9887_configure(t);
663 v4l2_std_id *id = arg;
668 tda9887_fixup_std(t);
669 tda9887_configure(t);
672 case VIDIOC_S_FREQUENCY:
674 struct v4l2_frequency *f = arg;
677 if (V4L2_TUNER_ANALOG_TV == f->type) {
682 if (V4L2_TUNER_RADIO == f->type) {
687 tda9887_configure(t);
692 static int AFC_BITS_2_kHz[] = {
693 -12500, -37500, -62500, -97500,
694 -112500, -137500, -162500, -187500,
695 187500, 162500, 137500, 112500,
696 97500 , 62500, 37500 , 12500
698 struct v4l2_tuner* tuner = arg;
703 if (1 == i2c_master_recv(&t->client,®,1))
704 tuner->afc = AFC_BITS_2_kHz[(reg>>1)&0x0f];
715 /* ----------------------------------------------------------------------- */
717 static struct i2c_driver driver = {
718 .owner = THIS_MODULE,
719 .name = "i2c tda9887 driver",
720 .id = -1, /* FIXME */
721 .flags = I2C_DF_NOTIFY,
722 .attach_adapter = tda9887_probe,
723 .detach_client = tda9887_detach,
724 .command = tda9887_command,
726 static struct i2c_client client_template =
728 I2C_DEVNAME("tda9887"),
729 .flags = I2C_CLIENT_ALLOW_USE,
733 static int __init tda9887_init_module(void)
735 return i2c_add_driver(&driver);
738 static void __exit tda9887_cleanup_module(void)
740 i2c_del_driver(&driver);
743 module_init(tda9887_init_module);
744 module_exit(tda9887_cleanup_module);
747 * Overrides for Emacs so that we follow Linus's tabbing style.
748 * ---------------------------------------------------------------------------