1 #include <linux/module.h>
2 #include <linux/moduleparam.h>
3 #include <linux/kernel.h>
5 #include <linux/types.h>
6 #include <linux/videodev.h>
7 #include <linux/init.h>
8 #include <linux/errno.h>
9 #include <linux/slab.h>
10 #include <linux/delay.h>
12 #include <media/audiochip.h>
13 #include <media/tuner.h>
18 TDA9886 (PAL, SECAM, NTSC)
19 TDA9887 (PAL, SECAM, NTSC, FM Radio)
22 - Pinnacle PCTV (Jul.2002 Version with MT2032, bttv)
23 TDA9887 (world), TDA9885 (USA)
24 Note: OP2 of tda988x must be set to 1, else MT2032 is disabled!
25 - KNC One TV-Station RDS (saa7134)
29 /* Addresses to scan */
30 static unsigned short normal_i2c[] = {
39 static unsigned int debug = 0;
40 module_param(debug, int, 0644);
41 MODULE_LICENSE("GPL");
43 /* ---------------------------------------------------------------------- */
46 #define PREFIX "tda9885/6/7: "
47 #define dprintk if (debug) printk
50 struct i2c_client client;
54 unsigned int pinnacle_id;
55 unsigned int using_v4l2;
56 unsigned int radio_mode;
67 static struct i2c_driver driver;
68 static struct i2c_client client_template;
70 /* ---------------------------------------------------------------------- */
77 #define cVideoTrapBypassOFF 0x00 // bit b0
78 #define cVideoTrapBypassON 0x01 // bit b0
80 #define cAutoMuteFmInactive 0x00 // bit b1
81 #define cAutoMuteFmActive 0x02 // bit b1
83 #define cIntercarrier 0x00 // bit b2
84 #define cQSS 0x04 // bit b2
86 #define cPositiveAmTV 0x00 // bit b3:4
87 #define cFmRadio 0x08 // bit b3:4
88 #define cNegativeFmTV 0x10 // bit b3:4
91 #define cForcedMuteAudioON 0x20 // bit b5
92 #define cForcedMuteAudioOFF 0x00 // bit b5
94 #define cOutputPort1Active 0x00 // bit b6
95 #define cOutputPort1Inactive 0x40 // bit b6
97 #define cOutputPort2Active 0x00 // bit b7
98 #define cOutputPort2Inactive 0x80 // bit b7
102 #define cDeemphasisOFF 0x00 // bit c5
103 #define cDeemphasisON 0x20 // bit c5
105 #define cDeemphasis75 0x00 // bit c6
106 #define cDeemphasis50 0x40 // bit c6
108 #define cAudioGain0 0x00 // bit c7
109 #define cAudioGain6 0x80 // bit c7
113 #define cAudioIF_4_5 0x00 // bit e0:1
114 #define cAudioIF_5_5 0x01 // bit e0:1
115 #define cAudioIF_6_0 0x02 // bit e0:1
116 #define cAudioIF_6_5 0x03 // bit e0:1
119 #define cVideoIF_58_75 0x00 // bit e2:4
120 #define cVideoIF_45_75 0x04 // bit e2:4
121 #define cVideoIF_38_90 0x08 // bit e2:4
122 #define cVideoIF_38_00 0x0C // bit e2:4
123 #define cVideoIF_33_90 0x10 // bit e2:4
124 #define cVideoIF_33_40 0x14 // bit e2:4
125 #define cRadioIF_45_75 0x18 // bit e2:4
126 #define cRadioIF_38_90 0x1C // bit e2:4
129 #define cTunerGainNormal 0x00 // bit e5
130 #define cTunerGainLow 0x20 // bit e5
132 #define cGating_18 0x00 // bit e6
133 #define cGating_36 0x40 // bit e6
135 #define cAgcOutON 0x80 // bit e7
136 #define cAgcOutOFF 0x00 // bit e7
138 /* ---------------------------------------------------------------------- */
140 static struct tvnorm tvnorms[] = {
142 .std = V4L2_STD_PAL_BG,
144 .b = ( cNegativeFmTV |
146 .c = ( cDeemphasisON |
148 .e = ( cAudioIF_5_5 |
151 .std = V4L2_STD_PAL_I,
153 .b = ( cNegativeFmTV |
155 .c = ( cDeemphasisON |
157 .e = ( cAudioIF_6_0 |
160 .std = V4L2_STD_PAL_DK,
162 .b = ( cNegativeFmTV |
164 .c = ( cDeemphasisON |
166 .e = ( cAudioIF_6_5 |
169 .std = V4L2_STD_PAL_M | V4L2_STD_PAL_N,
171 .b = ( cNegativeFmTV |
173 .c = ( cDeemphasisON |
175 .e = ( cAudioIF_4_5 |
178 .std = V4L2_STD_SECAM_L,
180 .b = ( cPositiveAmTV |
182 .e = ( cAudioIF_6_5 |
185 .std = V4L2_STD_SECAM_DK,
187 .b = ( cNegativeFmTV |
189 .c = ( cDeemphasisON |
191 .e = ( cAudioIF_6_5 |
194 .std = V4L2_STD_NTSC_M,
196 .b = ( cNegativeFmTV |
198 .c = ( cDeemphasisON |
204 .std = V4L2_STD_NTSC_M_JP,
206 .b = ( cNegativeFmTV |
208 .c = ( cDeemphasisON |
216 static struct tvnorm radio_stereo = {
217 .name = "Radio Stereo",
220 .c = ( cDeemphasisOFF |
222 .e = ( cAudioIF_5_5 |
226 static struct tvnorm radio_mono = {
227 .name = "Radio Mono",
230 .c = ( cDeemphasisON |
232 .e = ( cAudioIF_5_5 |
236 /* ---------------------------------------------------------------------- */
238 static void dump_read_message(unsigned char *buf)
240 static char *afc[16] = {
258 printk(PREFIX "read: 0x%2x\n", buf[0]);
259 printk(" after power on : %s\n", (buf[0] & 0x01) ? "yes" : "no");
260 printk(" afc : %s\n", afc[(buf[0] >> 1) & 0x0f]);
261 printk(" fmif level : %s\n", (buf[0] & 0x20) ? "high" : "low");
262 printk(" afc window : %s\n", (buf[0] & 0x40) ? "in" : "out");
263 printk(" vfi level : %s\n", (buf[0] & 0x80) ? "high" : "low");
266 static void dump_write_message(unsigned char *buf)
268 static char *sound[4] = {
274 static char *adjust[32] = {
275 "-16", "-15", "-14", "-13", "-12", "-11", "-10", "-9",
276 "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1",
277 "0", "+1", "+2", "+3", "+4", "+5", "+6", "+7",
278 "+8", "+9", "+10", "+11", "+12", "+13", "+14", "+15"
280 static char *deemph[4] = {
281 "no", "no", "75", "50"
283 static char *carrier[4] = {
289 static char *vif[8] = {
299 static char *rif[4] = {
306 printk(PREFIX "write: byte B 0x%02x\n",buf[1]);
307 printk(" B0 video mode : %s\n",
308 (buf[1] & 0x01) ? "video trap" : "sound trap");
309 printk(" B1 auto mute fm : %s\n",
310 (buf[1] & 0x02) ? "yes" : "no");
311 printk(" B2 carrier mode : %s\n",
312 (buf[1] & 0x04) ? "QSS" : "Intercarrier");
313 printk(" B3-4 tv sound/radio : %s\n",
314 sound[(buf[1] & 0x18) >> 3]);
315 printk(" B5 force mute audio: %s\n",
316 (buf[1] & 0x20) ? "yes" : "no");
317 printk(" B6 output port 1 : %s\n",
318 (buf[1] & 0x40) ? "high (inactive)" : "low (active)");
319 printk(" B7 output port 2 : %s\n",
320 (buf[1] & 0x80) ? "high (inactive)" : "low (active)");
322 printk(PREFIX "write: byte C 0x%02x\n",buf[2]);
323 printk(" C0-4 top adjustment : %s dB\n", adjust[buf[2] & 0x1f]);
324 printk(" C5-6 de-emphasis : %s\n", deemph[(buf[2] & 0x60) >> 5]);
325 printk(" C7 audio gain : %s\n",
326 (buf[2] & 0x80) ? "-6" : "0");
328 printk(PREFIX "write: byte E 0x%02x\n",buf[3]);
329 printk(" E0-1 sound carrier : %s\n",
330 carrier[(buf[3] & 0x03)]);
331 printk(" E6 l pll ganting : %s\n",
332 (buf[3] & 0x40) ? "36" : "13");
336 printk(" E2-4 video if : %s\n",
337 rif[(buf[3] & 0x0c) >> 2]);
338 printk(" E7 vif agc output : %s\n",
340 ? ((buf[3] & 0x10) ? "fm-agc radio" : "sif-agc radio")
341 : "fm radio carrier afc");
344 printk(" E2-4 video if : %s\n",
345 vif[(buf[3] & 0x1c) >> 2]);
346 printk(" E5 tuner gain : %s\n",
348 ? ((buf[3] & 0x20) ? "external" : "normal")
349 : ((buf[3] & 0x20) ? "minimum" : "normal"));
350 printk(" E7 vif agc output : %s\n",
353 ? "pin3 port, pin22 vif agc out"
354 : "pin22 port, pin3 vif acg ext in")
355 : "pin3+pin22 port");
360 /* ---------------------------------------------------------------------- */
362 static int tda9887_set_tvnorm(struct tda9887 *t, char *buf)
364 struct tvnorm *norm = NULL;
368 if (t->radio_mode == V4L2_TUNER_MODE_MONO)
371 norm = &radio_stereo;
373 for (i = 0; i < ARRAY_SIZE(tvnorms); i++) {
374 if (tvnorms[i].std & t->std) {
381 dprintk(PREFIX "Oops: no tvnorm entry found\n");
385 dprintk(PREFIX "configure for: %s\n",norm->name);
392 static unsigned int port1 = UNSET;
393 static unsigned int port2 = UNSET;
394 static unsigned int qss = UNSET;
395 static unsigned int adjust = 0x10;
396 module_param(port1, int, 0644);
397 module_param(port2, int, 0644);
398 module_param(qss, int, 0644);
399 module_param(adjust, int, 0644);
401 static int tda9887_set_insmod(struct tda9887 *t, char *buf)
403 if (UNSET != port1) {
405 buf[1] |= cOutputPort1Inactive;
407 buf[1] &= ~cOutputPort1Inactive;
409 if (UNSET != port2) {
411 buf[1] |= cOutputPort2Inactive;
413 buf[1] &= ~cOutputPort2Inactive;
423 if (adjust >= 0x00 && adjust < 0x20)
428 static int tda9887_set_config(struct tda9887 *t, char *buf)
430 if (t->config & TDA9887_PORT1_ACTIVE)
431 buf[1] &= ~cOutputPort1Inactive;
432 if (t->config & TDA9887_PORT1_INACTIVE)
433 buf[1] |= cOutputPort1Inactive;
434 if (t->config & TDA9887_PORT2_ACTIVE)
435 buf[1] &= ~cOutputPort2Inactive;
436 if (t->config & TDA9887_PORT2_INACTIVE)
437 buf[1] |= cOutputPort2Inactive;
439 if (t->config & TDA9887_QSS)
441 if (t->config & TDA9887_INTERCARRIER)
444 if (t->config & TDA9887_AUTOMUTE)
445 buf[1] |= cAutoMuteFmActive;
446 if (t->config & TDA9887_DEEMPHASIS_MASK) {
448 switch (t->config & TDA9887_DEEMPHASIS_MASK) {
449 case TDA9887_DEEMPHASIS_NONE:
450 buf[2] |= cDeemphasisOFF;
452 case TDA9887_DEEMPHASIS_50:
453 buf[2] |= cDeemphasisON | cDeemphasis50;
455 case TDA9887_DEEMPHASIS_75:
456 buf[2] |= cDeemphasisON | cDeemphasis75;
463 /* ---------------------------------------------------------------------- */
465 static int tda9887_set_pinnacle(struct tda9887 *t, char *buf)
467 unsigned int bCarrierMode = UNSET;
469 if (t->std & V4L2_STD_625_50) {
470 if ((1 == t->pinnacle_id) || (7 == t->pinnacle_id)) {
471 bCarrierMode = cIntercarrier;
476 if (t->std & V4L2_STD_525_60) {
477 if ((5 == t->pinnacle_id) || (6 == t->pinnacle_id)) {
478 bCarrierMode = cIntercarrier;
484 if (bCarrierMode != UNSET) {
486 buf[1] |= bCarrierMode;
491 /* ---------------------------------------------------------------------- */
493 static char pal[] = "-";
494 module_param_string(pal, pal, sizeof(pal), 0644);
495 static char secam[] = "-";
496 module_param_string(secam, secam, sizeof(secam), 0644);
498 static int tda9887_fixup_std(struct tda9887 *t)
500 /* get more precise norm info from insmod option */
501 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
507 dprintk(PREFIX "insmod fixup: PAL => PAL-BG\n");
508 t->std = V4L2_STD_PAL_BG;
512 dprintk(PREFIX "insmod fixup: PAL => PAL-I\n");
513 t->std = V4L2_STD_PAL_I;
519 dprintk(PREFIX "insmod fixup: PAL => PAL-DK\n");
520 t->std = V4L2_STD_PAL_DK;
524 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
530 dprintk(PREFIX "insmod fixup: SECAM => SECAM-DK\n");
531 t->std = V4L2_STD_SECAM_DK;
535 dprintk(PREFIX "insmod fixup: SECAM => SECAM-L\n");
536 t->std = V4L2_STD_SECAM_L;
543 static int tda9887_status(struct tda9887 *t)
545 unsigned char buf[1];
548 memset(buf,0,sizeof(buf));
549 if (1 != (rc = i2c_master_recv(&t->client,buf,1)))
550 printk(PREFIX "i2c i/o error: rc == %d (should be 1)\n",rc);
551 dump_read_message(buf);
555 static int tda9887_configure(struct tda9887 *t)
557 unsigned char buf[4];
560 memset(buf,0,sizeof(buf));
561 tda9887_set_tvnorm(t,buf);
563 buf[1] |= cOutputPort1Inactive;
564 buf[1] |= cOutputPort2Inactive;
566 if (UNSET != t->pinnacle_id) {
567 tda9887_set_pinnacle(t,buf);
569 tda9887_set_config(t,buf);
570 tda9887_set_insmod(t,buf);
573 dprintk(PREFIX "writing: b=0x%02x c=0x%02x e=0x%02x\n",
574 buf[1],buf[2],buf[3]);
576 dump_write_message(buf);
578 if (4 != (rc = i2c_master_send(&t->client,buf,4)))
579 printk(PREFIX "i2c i/o error: rc == %d (should be 4)\n",rc);
582 msleep_interruptible(1000);
588 /* ---------------------------------------------------------------------- */
590 static int tda9887_attach(struct i2c_adapter *adap, int addr, int kind)
594 client_template.adapter = adap;
595 client_template.addr = addr;
597 printk(PREFIX "chip found @ 0x%x\n", addr<<1);
599 if (NULL == (t = kmalloc(sizeof(*t), GFP_KERNEL)))
601 memset(t,0,sizeof(*t));
603 t->client = client_template;
605 t->pinnacle_id = UNSET;
606 t->radio_mode = V4L2_TUNER_MODE_STEREO;
608 i2c_set_clientdata(&t->client, t);
609 i2c_attach_client(&t->client);
614 static int tda9887_probe(struct i2c_adapter *adap)
616 #ifdef I2C_CLASS_TV_ANALOG
617 if (adap->class & I2C_CLASS_TV_ANALOG)
618 return i2c_probe(adap, &addr_data, tda9887_attach);
621 case I2C_ALGO_BIT | I2C_HW_B_BT848:
622 case I2C_ALGO_BIT | I2C_HW_B_RIVA:
623 case I2C_ALGO_SAA7134:
624 return i2c_probe(adap, &addr_data, tda9887_attach);
631 static int tda9887_detach(struct i2c_client *client)
633 struct tda9887 *t = i2c_get_clientdata(client);
635 i2c_detach_client(client);
640 #define SWITCH_V4L2 if (!t->using_v4l2 && debug) \
641 printk(PREFIX "switching to v4l2\n"); \
643 #define CHECK_V4L2 if (t->using_v4l2) { if (debug) \
644 printk(PREFIX "ignore v4l1 call\n"); \
648 tda9887_command(struct i2c_client *client, unsigned int cmd, void *arg)
650 struct tda9887 *t = i2c_get_clientdata(client);
654 /* --- configuration --- */
657 tda9887_configure(t);
660 case AUDC_CONFIG_PINNACLE:
665 tda9887_configure(t);
668 case TDA9887_SET_CONFIG:
673 tda9887_configure(t);
676 /* --- v4l ioctls --- */
677 /* take care: bttv does userspace copying, we'll get a
678 kernel pointer here... */
681 static const v4l2_std_id map[] = {
682 [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
683 [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
684 [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
685 [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
686 [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
687 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
689 struct video_channel *vc = arg;
693 if (vc->norm < ARRAY_SIZE(map))
694 t->std = map[vc->norm];
695 tda9887_fixup_std(t);
696 tda9887_configure(t);
701 v4l2_std_id *id = arg;
706 tda9887_fixup_std(t);
707 tda9887_configure(t);
710 case VIDIOC_S_FREQUENCY:
712 struct v4l2_frequency *f = arg;
715 if (V4L2_TUNER_ANALOG_TV == f->type) {
720 if (V4L2_TUNER_RADIO == f->type) {
725 tda9887_configure(t);
730 static int AFC_BITS_2_kHz[] = {
731 -12500, -37500, -62500, -97500,
732 -112500, -137500, -162500, -187500,
733 187500, 162500, 137500, 112500,
734 97500 , 62500, 37500 , 12500
736 struct v4l2_tuner* tuner = arg;
741 if (1 == i2c_master_recv(&t->client,®,1))
742 tuner->afc = AFC_BITS_2_kHz[(reg>>1)&0x0f];
748 struct v4l2_tuner* tuner = arg;
751 t->radio_mode = tuner->audmode;
752 tda9887_configure (t);
763 static int tda9887_suspend(struct device * dev, u32 state, u32 level)
765 dprintk("tda9887: suspend\n");
769 static int tda9887_resume(struct device * dev, u32 level)
771 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
772 struct tda9887 *t = i2c_get_clientdata(c);
774 dprintk("tda9887: resume\n");
775 tda9887_configure(t);
779 /* ----------------------------------------------------------------------- */
781 static struct i2c_driver driver = {
782 .owner = THIS_MODULE,
783 .name = "i2c tda9887 driver",
784 .id = -1, /* FIXME */
785 .flags = I2C_DF_NOTIFY,
786 .attach_adapter = tda9887_probe,
787 .detach_client = tda9887_detach,
788 .command = tda9887_command,
790 .suspend = tda9887_suspend,
791 .resume = tda9887_resume,
794 static struct i2c_client client_template =
796 I2C_DEVNAME("tda9887"),
797 .flags = I2C_CLIENT_ALLOW_USE,
801 static int __init tda9887_init_module(void)
803 return i2c_add_driver(&driver);
806 static void __exit tda9887_cleanup_module(void)
808 i2c_del_driver(&driver);
811 module_init(tda9887_init_module);
812 module_exit(tda9887_cleanup_module);
815 * Overrides for Emacs so that we follow Linus's tabbing style.
816 * ---------------------------------------------------------------------------