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;
63 unsigned char data[4];
74 static struct i2c_driver driver;
75 static struct i2c_client client_template;
77 /* ---------------------------------------------------------------------- */
84 #define cVideoTrapBypassOFF 0x00 // bit b0
85 #define cVideoTrapBypassON 0x01 // bit b0
87 #define cAutoMuteFmInactive 0x00 // bit b1
88 #define cAutoMuteFmActive 0x02 // bit b1
90 #define cIntercarrier 0x00 // bit b2
91 #define cQSS 0x04 // bit b2
93 #define cPositiveAmTV 0x00 // bit b3:4
94 #define cFmRadio 0x08 // bit b3:4
95 #define cNegativeFmTV 0x10 // bit b3:4
98 #define cForcedMuteAudioON 0x20 // bit b5
99 #define cForcedMuteAudioOFF 0x00 // bit b5
101 #define cOutputPort1Active 0x00 // bit b6
102 #define cOutputPort1Inactive 0x40 // bit b6
104 #define cOutputPort2Active 0x00 // bit b7
105 #define cOutputPort2Inactive 0x80 // bit b7
109 #define cDeemphasisOFF 0x00 // bit c5
110 #define cDeemphasisON 0x20 // bit c5
112 #define cDeemphasis75 0x00 // bit c6
113 #define cDeemphasis50 0x40 // bit c6
115 #define cAudioGain0 0x00 // bit c7
116 #define cAudioGain6 0x80 // bit c7
120 #define cAudioIF_4_5 0x00 // bit e0:1
121 #define cAudioIF_5_5 0x01 // bit e0:1
122 #define cAudioIF_6_0 0x02 // bit e0:1
123 #define cAudioIF_6_5 0x03 // bit e0:1
126 #define cVideoIF_58_75 0x00 // bit e2:4
127 #define cVideoIF_45_75 0x04 // bit e2:4
128 #define cVideoIF_38_90 0x08 // bit e2:4
129 #define cVideoIF_38_00 0x0C // bit e2:4
130 #define cVideoIF_33_90 0x10 // bit e2:4
131 #define cVideoIF_33_40 0x14 // bit e2:4
132 #define cRadioIF_45_75 0x18 // bit e2:4
133 #define cRadioIF_38_90 0x1C // bit e2:4
136 #define cTunerGainNormal 0x00 // bit e5
137 #define cTunerGainLow 0x20 // bit e5
139 #define cGating_18 0x00 // bit e6
140 #define cGating_36 0x40 // bit e6
142 #define cAgcOutON 0x80 // bit e7
143 #define cAgcOutOFF 0x00 // bit e7
145 /* ---------------------------------------------------------------------- */
147 static struct tvnorm tvnorms[] = {
149 .std = V4L2_STD_PAL_BG,
151 .b = ( cNegativeFmTV |
153 .c = ( cDeemphasisON |
155 .e = ( cAudioIF_5_5 |
158 .std = V4L2_STD_PAL_I,
160 .b = ( cNegativeFmTV |
162 .c = ( cDeemphasisON |
164 .e = ( cAudioIF_6_0 |
167 .std = V4L2_STD_PAL_DK,
169 .b = ( cNegativeFmTV |
171 .c = ( cDeemphasisON |
173 .e = ( cAudioIF_6_5 |
176 .std = V4L2_STD_PAL_M | V4L2_STD_PAL_N,
178 .b = ( cNegativeFmTV |
180 .c = ( cDeemphasisON |
182 .e = ( cAudioIF_4_5 |
185 .std = V4L2_STD_SECAM_L,
187 .b = ( cPositiveAmTV |
193 .std = V4L2_STD_SECAM_LC,
195 .b = ( cOutputPort2Inactive |
202 .std = V4L2_STD_SECAM_DK,
204 .b = ( cNegativeFmTV |
206 .c = ( cDeemphasisON |
208 .e = ( cAudioIF_6_5 |
211 .std = V4L2_STD_NTSC_M,
213 .b = ( cNegativeFmTV |
215 .c = ( cDeemphasisON |
221 .std = V4L2_STD_NTSC_M_JP,
223 .b = ( cNegativeFmTV |
225 .c = ( cDeemphasisON |
233 static struct tvnorm radio_stereo = {
234 .name = "Radio Stereo",
237 .c = ( cDeemphasisOFF |
239 .e = ( cAudioIF_5_5 |
243 static struct tvnorm radio_mono = {
244 .name = "Radio Mono",
247 .c = ( cDeemphasisON |
249 .e = ( cAudioIF_5_5 |
253 /* ---------------------------------------------------------------------- */
255 static void dump_read_message(struct tda9887 *t, unsigned char *buf)
257 static char *afc[16] = {
275 tda9887_info("read: 0x%2x\n", buf[0]);
276 tda9887_info(" after power on : %s\n", (buf[0] & 0x01) ? "yes" : "no");
277 tda9887_info(" afc : %s\n", afc[(buf[0] >> 1) & 0x0f]);
278 tda9887_info(" fmif level : %s\n", (buf[0] & 0x20) ? "high" : "low");
279 tda9887_info(" afc window : %s\n", (buf[0] & 0x40) ? "in" : "out");
280 tda9887_info(" vfi level : %s\n", (buf[0] & 0x80) ? "high" : "low");
283 static void dump_write_message(struct tda9887 *t, unsigned char *buf)
285 static char *sound[4] = {
291 static char *adjust[32] = {
292 "-16", "-15", "-14", "-13", "-12", "-11", "-10", "-9",
293 "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1",
294 "0", "+1", "+2", "+3", "+4", "+5", "+6", "+7",
295 "+8", "+9", "+10", "+11", "+12", "+13", "+14", "+15"
297 static char *deemph[4] = {
298 "no", "no", "75", "50"
300 static char *carrier[4] = {
306 static char *vif[8] = {
316 static char *rif[4] = {
323 tda9887_info("write: byte B 0x%02x\n",buf[1]);
324 tda9887_info(" B0 video mode : %s\n",
325 (buf[1] & 0x01) ? "video trap" : "sound trap");
326 tda9887_info(" B1 auto mute fm : %s\n",
327 (buf[1] & 0x02) ? "yes" : "no");
328 tda9887_info(" B2 carrier mode : %s\n",
329 (buf[1] & 0x04) ? "QSS" : "Intercarrier");
330 tda9887_info(" B3-4 tv sound/radio : %s\n",
331 sound[(buf[1] & 0x18) >> 3]);
332 tda9887_info(" B5 force mute audio: %s\n",
333 (buf[1] & 0x20) ? "yes" : "no");
334 tda9887_info(" B6 output port 1 : %s\n",
335 (buf[1] & 0x40) ? "high (inactive)" : "low (active)");
336 tda9887_info(" B7 output port 2 : %s\n",
337 (buf[1] & 0x80) ? "high (inactive)" : "low (active)");
339 tda9887_info("write: byte C 0x%02x\n",buf[2]);
340 tda9887_info(" C0-4 top adjustment : %s dB\n", adjust[buf[2] & 0x1f]);
341 tda9887_info(" C5-6 de-emphasis : %s\n", deemph[(buf[2] & 0x60) >> 5]);
342 tda9887_info(" C7 audio gain : %s\n",
343 (buf[2] & 0x80) ? "-6" : "0");
345 tda9887_info("write: byte E 0x%02x\n",buf[3]);
346 tda9887_info(" E0-1 sound carrier : %s\n",
347 carrier[(buf[3] & 0x03)]);
348 tda9887_info(" E6 l pll gating : %s\n",
349 (buf[3] & 0x40) ? "36" : "13");
353 tda9887_info(" E2-4 video if : %s\n",
354 rif[(buf[3] & 0x0c) >> 2]);
355 tda9887_info(" E7 vif agc output : %s\n",
357 ? ((buf[3] & 0x10) ? "fm-agc radio" : "sif-agc radio")
358 : "fm radio carrier afc");
361 tda9887_info(" E2-4 video if : %s\n",
362 vif[(buf[3] & 0x1c) >> 2]);
363 tda9887_info(" E5 tuner gain : %s\n",
365 ? ((buf[3] & 0x20) ? "external" : "normal")
366 : ((buf[3] & 0x20) ? "minimum" : "normal"));
367 tda9887_info(" E7 vif agc output : %s\n",
370 ? "pin3 port, pin22 vif agc out"
371 : "pin22 port, pin3 vif acg ext in")
372 : "pin3+pin22 port");
374 tda9887_info("--\n");
377 /* ---------------------------------------------------------------------- */
379 static int tda9887_set_tvnorm(struct tda9887 *t, char *buf)
381 struct tvnorm *norm = NULL;
384 if (t->mode == T_RADIO) {
385 if (t->radio_mode == V4L2_TUNER_MODE_MONO)
388 norm = &radio_stereo;
390 for (i = 0; i < ARRAY_SIZE(tvnorms); i++) {
391 if (tvnorms[i].std & t->std) {
398 tda9887_dbg("Unsupported tvnorm entry - audio muted\n");
402 tda9887_dbg("configure for: %s\n",norm->name);
409 static unsigned int port1 = UNSET;
410 static unsigned int port2 = UNSET;
411 static unsigned int qss = UNSET;
412 static unsigned int adjust = 0x10;
413 module_param(port1, int, 0644);
414 module_param(port2, int, 0644);
415 module_param(qss, int, 0644);
416 module_param(adjust, int, 0644);
418 static int tda9887_set_insmod(struct tda9887 *t, char *buf)
420 if (UNSET != port1) {
422 buf[1] |= cOutputPort1Inactive;
424 buf[1] &= ~cOutputPort1Inactive;
426 if (UNSET != port2) {
428 buf[1] |= cOutputPort2Inactive;
430 buf[1] &= ~cOutputPort2Inactive;
440 if (adjust >= 0x00 && adjust < 0x20)
445 static int tda9887_set_config(struct tda9887 *t, char *buf)
447 if (t->config & TDA9887_PORT1_ACTIVE)
448 buf[1] &= ~cOutputPort1Inactive;
449 if (t->config & TDA9887_PORT1_INACTIVE)
450 buf[1] |= cOutputPort1Inactive;
451 if (t->config & TDA9887_PORT2_ACTIVE)
452 buf[1] &= ~cOutputPort2Inactive;
453 if (t->config & TDA9887_PORT2_INACTIVE)
454 buf[1] |= cOutputPort2Inactive;
456 if (t->config & TDA9887_QSS)
458 if (t->config & TDA9887_INTERCARRIER)
461 if (t->config & TDA9887_AUTOMUTE)
462 buf[1] |= cAutoMuteFmActive;
463 if (t->config & TDA9887_DEEMPHASIS_MASK) {
465 switch (t->config & TDA9887_DEEMPHASIS_MASK) {
466 case TDA9887_DEEMPHASIS_NONE:
467 buf[2] |= cDeemphasisOFF;
469 case TDA9887_DEEMPHASIS_50:
470 buf[2] |= cDeemphasisON | cDeemphasis50;
472 case TDA9887_DEEMPHASIS_75:
473 buf[2] |= cDeemphasisON | cDeemphasis75;
477 if ((t->config & TDA9887_INTERCARRIER_NTSC) && (t->std & V4L2_STD_NTSC))
482 /* ---------------------------------------------------------------------- */
484 static int tda9887_set_pinnacle(struct tda9887 *t, char *buf)
486 unsigned int bCarrierMode = UNSET;
488 if (t->std & V4L2_STD_625_50) {
489 if ((1 == t->pinnacle_id) || (7 == t->pinnacle_id)) {
490 bCarrierMode = cIntercarrier;
495 if (t->std & V4L2_STD_525_60) {
496 if ((5 == t->pinnacle_id) || (6 == t->pinnacle_id)) {
497 bCarrierMode = cIntercarrier;
503 if (bCarrierMode != UNSET) {
505 buf[1] |= bCarrierMode;
510 /* ---------------------------------------------------------------------- */
512 static char pal[] = "-";
513 module_param_string(pal, pal, sizeof(pal), 0644);
514 static char secam[] = "-";
515 module_param_string(secam, secam, sizeof(secam), 0644);
517 static int tda9887_fixup_std(struct tda9887 *t)
519 /* get more precise norm info from insmod option */
520 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
526 tda9887_dbg("insmod fixup: PAL => PAL-BG\n");
527 t->std = V4L2_STD_PAL_BG;
531 tda9887_dbg("insmod fixup: PAL => PAL-I\n");
532 t->std = V4L2_STD_PAL_I;
538 tda9887_dbg("insmod fixup: PAL => PAL-DK\n");
539 t->std = V4L2_STD_PAL_DK;
542 /* default parameter, do nothing */
545 tda9887_info("pal= argument not recognised\n");
549 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
555 tda9887_dbg("insmod fixup: SECAM => SECAM-DK\n");
556 t->std = V4L2_STD_SECAM_DK;
560 tda9887_dbg("insmod fixup: SECAM => SECAM-L\n");
561 t->std = V4L2_STD_SECAM_L;
564 /* default parameter, do nothing */
567 tda9887_info("secam= argument not recognised\n");
574 static int tda9887_status(struct tda9887 *t)
576 unsigned char buf[1];
579 memset(buf,0,sizeof(buf));
580 if (1 != (rc = i2c_master_recv(&t->client,buf,1)))
581 tda9887_info("i2c i/o error: rc == %d (should be 1)\n",rc);
582 dump_read_message(t, buf);
586 static int tda9887_configure(struct tda9887 *t)
590 memset(t->data,0,sizeof(t->data));
591 tda9887_set_tvnorm(t,t->data);
593 t->data[1] |= cOutputPort1Inactive;
594 t->data[1] |= cOutputPort2Inactive;
596 if (UNSET != t->pinnacle_id) {
597 tda9887_set_pinnacle(t,t->data);
599 tda9887_set_config(t,t->data);
600 tda9887_set_insmod(t,t->data);
602 if (t->mode == T_STANDBY) {
603 t->data[1] |= cForcedMuteAudioON;
607 tda9887_dbg("writing: b=0x%02x c=0x%02x e=0x%02x\n",
608 t->data[1],t->data[2],t->data[3]);
610 dump_write_message(t, t->data);
612 if (4 != (rc = i2c_master_send(&t->client,t->data,4)))
613 tda9887_info("i2c i/o error: rc == %d (should be 4)\n",rc);
616 msleep_interruptible(1000);
622 /* ---------------------------------------------------------------------- */
624 static int tda9887_attach(struct i2c_adapter *adap, int addr, int kind)
628 client_template.adapter = adap;
629 client_template.addr = addr;
631 if (NULL == (t = kmalloc(sizeof(*t), GFP_KERNEL)))
633 memset(t,0,sizeof(*t));
635 t->client = client_template;
637 t->pinnacle_id = UNSET;
638 t->radio_mode = V4L2_TUNER_MODE_STEREO;
640 tda9887_info("chip found @ 0x%x (%s)\n", addr<<1, adap->name);
642 i2c_set_clientdata(&t->client, t);
643 i2c_attach_client(&t->client);
648 static int tda9887_probe(struct i2c_adapter *adap)
650 #ifdef I2C_CLASS_TV_ANALOG
651 if (adap->class & I2C_CLASS_TV_ANALOG)
652 return i2c_probe(adap, &addr_data, tda9887_attach);
658 return i2c_probe(adap, &addr_data, tda9887_attach);
665 static int tda9887_detach(struct i2c_client *client)
667 struct tda9887 *t = i2c_get_clientdata(client);
669 i2c_detach_client(client);
674 #define SWITCH_V4L2 if (!t->using_v4l2 && debug) \
675 tda9887_info("switching to v4l2\n"); \
677 #define CHECK_V4L2 if (t->using_v4l2) { if (debug) \
678 tda9887_info("ignore v4l1 call\n"); \
682 tda9887_command(struct i2c_client *client, unsigned int cmd, void *arg)
684 struct tda9887 *t = i2c_get_clientdata(client);
688 /* --- configuration --- */
692 tda9887_configure(t);
695 case TUNER_SET_STANDBY:
698 tda9887_configure(t);
701 case AUDC_CONFIG_PINNACLE:
706 tda9887_configure(t);
709 case TDA9887_SET_CONFIG:
714 tda9887_configure(t);
717 /* --- v4l ioctls --- */
718 /* take care: bttv does userspace copying, we'll get a
719 kernel pointer here... */
722 static const v4l2_std_id map[] = {
723 [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
724 [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
725 [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
726 [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
727 [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
728 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
730 struct video_channel *vc = arg;
733 t->mode = T_ANALOG_TV;
734 if (vc->norm < ARRAY_SIZE(map))
735 t->std = map[vc->norm];
736 tda9887_fixup_std(t);
737 tda9887_configure(t);
742 v4l2_std_id *id = arg;
745 t->mode = T_ANALOG_TV;
747 tda9887_fixup_std(t);
748 tda9887_configure(t);
751 case VIDIOC_S_FREQUENCY:
753 struct v4l2_frequency *f = arg;
756 if (V4L2_TUNER_ANALOG_TV == f->type) {
757 if (t->mode == T_ANALOG_TV)
759 t->mode = T_ANALOG_TV;
761 if (V4L2_TUNER_RADIO == f->type) {
762 if (t->mode == T_RADIO)
766 tda9887_configure(t);
771 static int AFC_BITS_2_kHz[] = {
772 -12500, -37500, -62500, -97500,
773 -112500, -137500, -162500, -187500,
774 187500, 162500, 137500, 112500,
775 97500 , 62500, 37500 , 12500
777 struct v4l2_tuner* tuner = arg;
779 if (t->mode == T_RADIO) {
782 if (1 == i2c_master_recv(&t->client,®,1))
783 tuner->afc = AFC_BITS_2_kHz[(reg>>1)&0x0f];
789 struct v4l2_tuner* tuner = arg;
791 if (t->mode == T_RADIO) {
792 t->radio_mode = tuner->audmode;
793 tda9887_configure (t);
797 case VIDIOC_LOG_STATUS:
799 tda9887_info("Data bytes: b=%02x c=%02x e=%02x\n", t->data[1], t->data[2], t->data[3]);
809 static int tda9887_suspend(struct device * dev, pm_message_t state)
811 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
812 struct tda9887 *t = i2c_get_clientdata(c);
814 tda9887_dbg("suspend\n");
818 static int tda9887_resume(struct device * dev)
820 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
821 struct tda9887 *t = i2c_get_clientdata(c);
823 tda9887_dbg("resume\n");
824 tda9887_configure(t);
828 /* ----------------------------------------------------------------------- */
830 static struct i2c_driver driver = {
831 .id = -1, /* FIXME */
832 .attach_adapter = tda9887_probe,
833 .detach_client = tda9887_detach,
834 .command = tda9887_command,
836 .name = "i2c tda9887 driver",
837 .suspend = tda9887_suspend,
838 .resume = tda9887_resume,
841 static struct i2c_client client_template =
847 static int __init tda9887_init_module(void)
849 return i2c_add_driver(&driver);
852 static void __exit tda9887_cleanup_module(void)
854 i2c_del_driver(&driver);
857 module_init(tda9887_init_module);
858 module_exit(tda9887_cleanup_module);
861 * Overrides for Emacs so that we follow Linus's tabbing style.
862 * ---------------------------------------------------------------------------