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)
26 - Hauppauge PVR-150/500 (possibly more)
30 /* Addresses to scan */
31 static unsigned short normal_i2c[] = {
40 static unsigned int debug = 0;
41 module_param(debug, int, 0644);
42 MODULE_LICENSE("GPL");
44 /* ---------------------------------------------------------------------- */
47 #define tda9887_info(fmt, arg...) do {\
48 printk(KERN_INFO "%s %d-%04x: " fmt, t->client.name, \
49 i2c_adapter_id(t->client.adapter), t->client.addr , ##arg); } while (0)
50 #define tda9887_dbg(fmt, arg...) do {\
52 printk(KERN_INFO "%s %d-%04x: " fmt, t->client.name, \
53 i2c_adapter_id(t->client.adapter), t->client.addr , ##arg); } while (0)
56 struct i2c_client client;
60 unsigned int pinnacle_id;
61 unsigned int using_v4l2;
62 unsigned int radio_mode;
73 static struct i2c_driver driver;
74 static struct i2c_client client_template;
76 /* ---------------------------------------------------------------------- */
83 #define cVideoTrapBypassOFF 0x00 // bit b0
84 #define cVideoTrapBypassON 0x01 // bit b0
86 #define cAutoMuteFmInactive 0x00 // bit b1
87 #define cAutoMuteFmActive 0x02 // bit b1
89 #define cIntercarrier 0x00 // bit b2
90 #define cQSS 0x04 // bit b2
92 #define cPositiveAmTV 0x00 // bit b3:4
93 #define cFmRadio 0x08 // bit b3:4
94 #define cNegativeFmTV 0x10 // bit b3:4
97 #define cForcedMuteAudioON 0x20 // bit b5
98 #define cForcedMuteAudioOFF 0x00 // bit b5
100 #define cOutputPort1Active 0x00 // bit b6
101 #define cOutputPort1Inactive 0x40 // bit b6
103 #define cOutputPort2Active 0x00 // bit b7
104 #define cOutputPort2Inactive 0x80 // bit b7
108 #define cDeemphasisOFF 0x00 // bit c5
109 #define cDeemphasisON 0x20 // bit c5
111 #define cDeemphasis75 0x00 // bit c6
112 #define cDeemphasis50 0x40 // bit c6
114 #define cAudioGain0 0x00 // bit c7
115 #define cAudioGain6 0x80 // bit c7
119 #define cAudioIF_4_5 0x00 // bit e0:1
120 #define cAudioIF_5_5 0x01 // bit e0:1
121 #define cAudioIF_6_0 0x02 // bit e0:1
122 #define cAudioIF_6_5 0x03 // bit e0:1
125 #define cVideoIF_58_75 0x00 // bit e2:4
126 #define cVideoIF_45_75 0x04 // bit e2:4
127 #define cVideoIF_38_90 0x08 // bit e2:4
128 #define cVideoIF_38_00 0x0C // bit e2:4
129 #define cVideoIF_33_90 0x10 // bit e2:4
130 #define cVideoIF_33_40 0x14 // bit e2:4
131 #define cRadioIF_45_75 0x18 // bit e2:4
132 #define cRadioIF_38_90 0x1C // bit e2:4
135 #define cTunerGainNormal 0x00 // bit e5
136 #define cTunerGainLow 0x20 // bit e5
138 #define cGating_18 0x00 // bit e6
139 #define cGating_36 0x40 // bit e6
141 #define cAgcOutON 0x80 // bit e7
142 #define cAgcOutOFF 0x00 // bit e7
144 /* ---------------------------------------------------------------------- */
146 static struct tvnorm tvnorms[] = {
148 .std = V4L2_STD_PAL_BG,
150 .b = ( cNegativeFmTV |
152 .c = ( cDeemphasisON |
154 .e = ( cAudioIF_5_5 |
157 .std = V4L2_STD_PAL_I,
159 .b = ( cNegativeFmTV |
161 .c = ( cDeemphasisON |
163 .e = ( cAudioIF_6_0 |
166 .std = V4L2_STD_PAL_DK,
168 .b = ( cNegativeFmTV |
170 .c = ( cDeemphasisON |
172 .e = ( cAudioIF_6_5 |
175 .std = V4L2_STD_PAL_M | V4L2_STD_PAL_N,
177 .b = ( cNegativeFmTV |
179 .c = ( cDeemphasisON |
181 .e = ( cAudioIF_4_5 |
184 .std = V4L2_STD_SECAM_L,
186 .b = ( cPositiveAmTV |
192 .std = V4L2_STD_SECAM_DK,
194 .b = ( cNegativeFmTV |
196 .c = ( cDeemphasisON |
198 .e = ( cAudioIF_6_5 |
201 .std = V4L2_STD_NTSC_M,
203 .b = ( cNegativeFmTV |
205 .c = ( cDeemphasisON |
211 .std = V4L2_STD_NTSC_M_JP,
213 .b = ( cNegativeFmTV |
215 .c = ( cDeemphasisON |
223 static struct tvnorm radio_stereo = {
224 .name = "Radio Stereo",
227 .c = ( cDeemphasisOFF |
229 .e = ( cAudioIF_5_5 |
233 static struct tvnorm radio_mono = {
234 .name = "Radio Mono",
237 .c = ( cDeemphasisON |
239 .e = ( cAudioIF_5_5 |
243 /* ---------------------------------------------------------------------- */
245 static void dump_read_message(struct tda9887 *t, unsigned char *buf)
247 static char *afc[16] = {
265 tda9887_info("read: 0x%2x\n", buf[0]);
266 tda9887_info(" after power on : %s\n", (buf[0] & 0x01) ? "yes" : "no");
267 tda9887_info(" afc : %s\n", afc[(buf[0] >> 1) & 0x0f]);
268 tda9887_info(" fmif level : %s\n", (buf[0] & 0x20) ? "high" : "low");
269 tda9887_info(" afc window : %s\n", (buf[0] & 0x40) ? "in" : "out");
270 tda9887_info(" vfi level : %s\n", (buf[0] & 0x80) ? "high" : "low");
273 static void dump_write_message(struct tda9887 *t, unsigned char *buf)
275 static char *sound[4] = {
281 static char *adjust[32] = {
282 "-16", "-15", "-14", "-13", "-12", "-11", "-10", "-9",
283 "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1",
284 "0", "+1", "+2", "+3", "+4", "+5", "+6", "+7",
285 "+8", "+9", "+10", "+11", "+12", "+13", "+14", "+15"
287 static char *deemph[4] = {
288 "no", "no", "75", "50"
290 static char *carrier[4] = {
296 static char *vif[8] = {
306 static char *rif[4] = {
313 tda9887_info("write: byte B 0x%02x\n",buf[1]);
314 tda9887_info(" B0 video mode : %s\n",
315 (buf[1] & 0x01) ? "video trap" : "sound trap");
316 tda9887_info(" B1 auto mute fm : %s\n",
317 (buf[1] & 0x02) ? "yes" : "no");
318 tda9887_info(" B2 carrier mode : %s\n",
319 (buf[1] & 0x04) ? "QSS" : "Intercarrier");
320 tda9887_info(" B3-4 tv sound/radio : %s\n",
321 sound[(buf[1] & 0x18) >> 3]);
322 tda9887_info(" B5 force mute audio: %s\n",
323 (buf[1] & 0x20) ? "yes" : "no");
324 tda9887_info(" B6 output port 1 : %s\n",
325 (buf[1] & 0x40) ? "high (inactive)" : "low (active)");
326 tda9887_info(" B7 output port 2 : %s\n",
327 (buf[1] & 0x80) ? "high (inactive)" : "low (active)");
329 tda9887_info("write: byte C 0x%02x\n",buf[2]);
330 tda9887_info(" C0-4 top adjustment : %s dB\n", adjust[buf[2] & 0x1f]);
331 tda9887_info(" C5-6 de-emphasis : %s\n", deemph[(buf[2] & 0x60) >> 5]);
332 tda9887_info(" C7 audio gain : %s\n",
333 (buf[2] & 0x80) ? "-6" : "0");
335 tda9887_info("write: byte E 0x%02x\n",buf[3]);
336 tda9887_info(" E0-1 sound carrier : %s\n",
337 carrier[(buf[3] & 0x03)]);
338 tda9887_info(" E6 l pll gating : %s\n",
339 (buf[3] & 0x40) ? "36" : "13");
343 tda9887_info(" E2-4 video if : %s\n",
344 rif[(buf[3] & 0x0c) >> 2]);
345 tda9887_info(" E7 vif agc output : %s\n",
347 ? ((buf[3] & 0x10) ? "fm-agc radio" : "sif-agc radio")
348 : "fm radio carrier afc");
351 tda9887_info(" E2-4 video if : %s\n",
352 vif[(buf[3] & 0x1c) >> 2]);
353 tda9887_info(" E5 tuner gain : %s\n",
355 ? ((buf[3] & 0x20) ? "external" : "normal")
356 : ((buf[3] & 0x20) ? "minimum" : "normal"));
357 tda9887_info(" E7 vif agc output : %s\n",
360 ? "pin3 port, pin22 vif agc out"
361 : "pin22 port, pin3 vif acg ext in")
362 : "pin3+pin22 port");
364 tda9887_info("--\n");
367 /* ---------------------------------------------------------------------- */
369 static int tda9887_set_tvnorm(struct tda9887 *t, char *buf)
371 struct tvnorm *norm = NULL;
374 if (t->mode == T_RADIO) {
375 if (t->radio_mode == V4L2_TUNER_MODE_MONO)
378 norm = &radio_stereo;
380 for (i = 0; i < ARRAY_SIZE(tvnorms); i++) {
381 if (tvnorms[i].std & t->std) {
388 tda9887_dbg("Unsupported tvnorm entry - audio muted\n");
392 tda9887_dbg("configure for: %s\n",norm->name);
399 static unsigned int port1 = UNSET;
400 static unsigned int port2 = UNSET;
401 static unsigned int qss = UNSET;
402 static unsigned int adjust = 0x10;
403 module_param(port1, int, 0644);
404 module_param(port2, int, 0644);
405 module_param(qss, int, 0644);
406 module_param(adjust, int, 0644);
408 static int tda9887_set_insmod(struct tda9887 *t, char *buf)
410 if (UNSET != port1) {
412 buf[1] |= cOutputPort1Inactive;
414 buf[1] &= ~cOutputPort1Inactive;
416 if (UNSET != port2) {
418 buf[1] |= cOutputPort2Inactive;
420 buf[1] &= ~cOutputPort2Inactive;
430 if (adjust >= 0x00 && adjust < 0x20)
435 static int tda9887_set_config(struct tda9887 *t, char *buf)
437 if (t->config & TDA9887_PORT1_ACTIVE)
438 buf[1] &= ~cOutputPort1Inactive;
439 if (t->config & TDA9887_PORT1_INACTIVE)
440 buf[1] |= cOutputPort1Inactive;
441 if (t->config & TDA9887_PORT2_ACTIVE)
442 buf[1] &= ~cOutputPort2Inactive;
443 if (t->config & TDA9887_PORT2_INACTIVE)
444 buf[1] |= cOutputPort2Inactive;
446 if (t->config & TDA9887_QSS)
448 if (t->config & TDA9887_INTERCARRIER)
451 if (t->config & TDA9887_AUTOMUTE)
452 buf[1] |= cAutoMuteFmActive;
453 if (t->config & TDA9887_DEEMPHASIS_MASK) {
455 switch (t->config & TDA9887_DEEMPHASIS_MASK) {
456 case TDA9887_DEEMPHASIS_NONE:
457 buf[2] |= cDeemphasisOFF;
459 case TDA9887_DEEMPHASIS_50:
460 buf[2] |= cDeemphasisON | cDeemphasis50;
462 case TDA9887_DEEMPHASIS_75:
463 buf[2] |= cDeemphasisON | cDeemphasis75;
467 if ((t->config & TDA9887_INTERCARRIER_NTSC) && (t->std & V4L2_STD_NTSC))
472 /* ---------------------------------------------------------------------- */
474 static int tda9887_set_pinnacle(struct tda9887 *t, char *buf)
476 unsigned int bCarrierMode = UNSET;
478 if (t->std & V4L2_STD_625_50) {
479 if ((1 == t->pinnacle_id) || (7 == t->pinnacle_id)) {
480 bCarrierMode = cIntercarrier;
485 if (t->std & V4L2_STD_525_60) {
486 if ((5 == t->pinnacle_id) || (6 == t->pinnacle_id)) {
487 bCarrierMode = cIntercarrier;
493 if (bCarrierMode != UNSET) {
495 buf[1] |= bCarrierMode;
500 /* ---------------------------------------------------------------------- */
502 static char pal[] = "-";
503 module_param_string(pal, pal, sizeof(pal), 0644);
504 static char secam[] = "-";
505 module_param_string(secam, secam, sizeof(secam), 0644);
507 static int tda9887_fixup_std(struct tda9887 *t)
509 /* get more precise norm info from insmod option */
510 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
516 tda9887_dbg("insmod fixup: PAL => PAL-BG\n");
517 t->std = V4L2_STD_PAL_BG;
521 tda9887_dbg("insmod fixup: PAL => PAL-I\n");
522 t->std = V4L2_STD_PAL_I;
528 tda9887_dbg("insmod fixup: PAL => PAL-DK\n");
529 t->std = V4L2_STD_PAL_DK;
532 /* default parameter, do nothing */
535 tda9887_info("pal= argument not recognised\n");
539 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
545 tda9887_dbg("insmod fixup: SECAM => SECAM-DK\n");
546 t->std = V4L2_STD_SECAM_DK;
550 tda9887_dbg("insmod fixup: SECAM => SECAM-L\n");
551 t->std = V4L2_STD_SECAM_L;
554 /* default parameter, do nothing */
557 tda9887_info("secam= argument not recognised\n");
564 static int tda9887_status(struct tda9887 *t)
566 unsigned char buf[1];
569 memset(buf,0,sizeof(buf));
570 if (1 != (rc = i2c_master_recv(&t->client,buf,1)))
571 tda9887_info("i2c i/o error: rc == %d (should be 1)\n",rc);
572 dump_read_message(t, buf);
576 static int tda9887_configure(struct tda9887 *t)
578 unsigned char buf[4];
581 memset(buf,0,sizeof(buf));
582 tda9887_set_tvnorm(t,buf);
584 buf[1] |= cOutputPort1Inactive;
585 buf[1] |= cOutputPort2Inactive;
587 if (UNSET != t->pinnacle_id) {
588 tda9887_set_pinnacle(t,buf);
590 tda9887_set_config(t,buf);
591 tda9887_set_insmod(t,buf);
593 if (t->mode == T_STANDBY) {
594 buf[1] |= cForcedMuteAudioON;
598 tda9887_dbg("writing: b=0x%02x c=0x%02x e=0x%02x\n",
599 buf[1],buf[2],buf[3]);
601 dump_write_message(t, buf);
603 if (4 != (rc = i2c_master_send(&t->client,buf,4)))
604 tda9887_info("i2c i/o error: rc == %d (should be 4)\n",rc);
607 msleep_interruptible(1000);
613 /* ---------------------------------------------------------------------- */
615 static int tda9887_attach(struct i2c_adapter *adap, int addr, int kind)
619 client_template.adapter = adap;
620 client_template.addr = addr;
622 if (NULL == (t = kmalloc(sizeof(*t), GFP_KERNEL)))
624 memset(t,0,sizeof(*t));
626 t->client = client_template;
628 t->pinnacle_id = UNSET;
629 t->radio_mode = V4L2_TUNER_MODE_STEREO;
631 tda9887_info("chip found @ 0x%x (%s)\n", addr<<1, adap->name);
633 i2c_set_clientdata(&t->client, t);
634 i2c_attach_client(&t->client);
639 static int tda9887_probe(struct i2c_adapter *adap)
641 #ifdef I2C_CLASS_TV_ANALOG
642 if (adap->class & I2C_CLASS_TV_ANALOG)
643 return i2c_probe(adap, &addr_data, tda9887_attach);
649 return i2c_probe(adap, &addr_data, tda9887_attach);
656 static int tda9887_detach(struct i2c_client *client)
658 struct tda9887 *t = i2c_get_clientdata(client);
660 i2c_detach_client(client);
665 #define SWITCH_V4L2 if (!t->using_v4l2 && debug) \
666 tda9887_info("switching to v4l2\n"); \
668 #define CHECK_V4L2 if (t->using_v4l2) { if (debug) \
669 tda9887_info("ignore v4l1 call\n"); \
673 tda9887_command(struct i2c_client *client, unsigned int cmd, void *arg)
675 struct tda9887 *t = i2c_get_clientdata(client);
679 /* --- configuration --- */
683 tda9887_configure(t);
686 case TUNER_SET_STANDBY:
689 tda9887_configure(t);
692 case AUDC_CONFIG_PINNACLE:
697 tda9887_configure(t);
700 case TDA9887_SET_CONFIG:
705 tda9887_configure(t);
708 /* --- v4l ioctls --- */
709 /* take care: bttv does userspace copying, we'll get a
710 kernel pointer here... */
713 static const v4l2_std_id map[] = {
714 [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
715 [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
716 [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
717 [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
718 [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
719 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
721 struct video_channel *vc = arg;
724 t->mode = T_ANALOG_TV;
725 if (vc->norm < ARRAY_SIZE(map))
726 t->std = map[vc->norm];
727 tda9887_fixup_std(t);
728 tda9887_configure(t);
733 v4l2_std_id *id = arg;
736 t->mode = T_ANALOG_TV;
738 tda9887_fixup_std(t);
739 tda9887_configure(t);
742 case VIDIOC_S_FREQUENCY:
744 struct v4l2_frequency *f = arg;
747 if (V4L2_TUNER_ANALOG_TV == f->type) {
748 if (t->mode == T_ANALOG_TV)
750 t->mode = T_ANALOG_TV;
752 if (V4L2_TUNER_RADIO == f->type) {
753 if (t->mode == T_RADIO)
757 tda9887_configure(t);
762 static int AFC_BITS_2_kHz[] = {
763 -12500, -37500, -62500, -97500,
764 -112500, -137500, -162500, -187500,
765 187500, 162500, 137500, 112500,
766 97500 , 62500, 37500 , 12500
768 struct v4l2_tuner* tuner = arg;
770 if (t->mode == T_RADIO) {
773 if (1 == i2c_master_recv(&t->client,®,1))
774 tuner->afc = AFC_BITS_2_kHz[(reg>>1)&0x0f];
780 struct v4l2_tuner* tuner = arg;
782 if (t->mode == T_RADIO) {
783 t->radio_mode = tuner->audmode;
784 tda9887_configure (t);
795 static int tda9887_suspend(struct device * dev, pm_message_t state)
797 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
798 struct tda9887 *t = i2c_get_clientdata(c);
800 tda9887_dbg("suspend\n");
804 static int tda9887_resume(struct device * dev)
806 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
807 struct tda9887 *t = i2c_get_clientdata(c);
809 tda9887_dbg("resume\n");
810 tda9887_configure(t);
814 /* ----------------------------------------------------------------------- */
816 static struct i2c_driver driver = {
817 .owner = THIS_MODULE,
818 .name = "i2c tda9887 driver",
819 .id = -1, /* FIXME */
820 .flags = I2C_DF_NOTIFY,
821 .attach_adapter = tda9887_probe,
822 .detach_client = tda9887_detach,
823 .command = tda9887_command,
825 .suspend = tda9887_suspend,
826 .resume = tda9887_resume,
829 static struct i2c_client client_template =
832 .flags = I2C_CLIENT_ALLOW_USE,
836 static int __init tda9887_init_module(void)
838 return i2c_add_driver(&driver);
841 static void __exit tda9887_cleanup_module(void)
843 i2c_del_driver(&driver);
846 module_init(tda9887_init_module);
847 module_exit(tda9887_cleanup_module);
850 * Overrides for Emacs so that we follow Linus's tabbing style.
851 * ---------------------------------------------------------------------------