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/v4l2-common.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 using_v4l2;
61 unsigned int radio_mode;
62 unsigned char data[4];
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
117 #define cTopMask 0x1f // bit c0:4
118 #define cTopPalSecamDefault 0x14 // bit c0:4
119 #define cTopNtscRadioDefault 0x10 // bit c0:4
122 #define cAudioIF_4_5 0x00 // bit e0:1
123 #define cAudioIF_5_5 0x01 // bit e0:1
124 #define cAudioIF_6_0 0x02 // bit e0:1
125 #define cAudioIF_6_5 0x03 // bit e0:1
128 #define cVideoIF_58_75 0x00 // bit e2:4
129 #define cVideoIF_45_75 0x04 // bit e2:4
130 #define cVideoIF_38_90 0x08 // bit e2:4
131 #define cVideoIF_38_00 0x0C // bit e2:4
132 #define cVideoIF_33_90 0x10 // bit e2:4
133 #define cVideoIF_33_40 0x14 // bit e2:4
134 #define cRadioIF_45_75 0x18 // bit e2:4
135 #define cRadioIF_38_90 0x1C // bit e2:4
138 #define cTunerGainNormal 0x00 // bit e5
139 #define cTunerGainLow 0x20 // bit e5
141 #define cGating_18 0x00 // bit e6
142 #define cGating_36 0x40 // bit e6
144 #define cAgcOutON 0x80 // bit e7
145 #define cAgcOutOFF 0x00 // bit e7
147 /* ---------------------------------------------------------------------- */
149 static struct tvnorm tvnorms[] = {
151 .std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H | V4L2_STD_PAL_N,
153 .b = ( cNegativeFmTV |
155 .c = ( cDeemphasisON |
157 cTopPalSecamDefault),
162 .std = V4L2_STD_PAL_I,
164 .b = ( cNegativeFmTV |
166 .c = ( cDeemphasisON |
168 cTopPalSecamDefault),
173 .std = V4L2_STD_PAL_DK,
175 .b = ( cNegativeFmTV |
177 .c = ( cDeemphasisON |
179 cTopPalSecamDefault),
184 .std = V4L2_STD_PAL_M | V4L2_STD_PAL_Nc,
186 .b = ( cNegativeFmTV |
188 .c = ( cDeemphasisON |
190 cTopNtscRadioDefault),
195 .std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H,
197 .b = ( cPositiveAmTV |
199 .c = ( cTopPalSecamDefault),
204 .std = V4L2_STD_SECAM_L,
206 .b = ( cPositiveAmTV |
208 .c = ( cTopPalSecamDefault),
213 .std = V4L2_STD_SECAM_LC,
215 .b = ( cOutputPort2Inactive |
218 .c = ( cTopPalSecamDefault),
223 .std = V4L2_STD_SECAM_DK,
225 .b = ( cNegativeFmTV |
227 .c = ( cDeemphasisON |
229 cTopPalSecamDefault),
234 .std = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
236 .b = ( cNegativeFmTV |
238 .c = ( cDeemphasisON |
240 cTopNtscRadioDefault),
245 .std = V4L2_STD_NTSC_M_JP,
247 .b = ( cNegativeFmTV |
249 .c = ( cDeemphasisON |
251 cTopNtscRadioDefault),
258 static struct tvnorm radio_stereo = {
259 .name = "Radio Stereo",
262 .c = ( cDeemphasisOFF |
264 cTopNtscRadioDefault),
265 .e = ( cTunerGainLow |
270 static struct tvnorm radio_mono = {
271 .name = "Radio Mono",
274 .c = ( cDeemphasisON |
276 cTopNtscRadioDefault),
277 .e = ( cTunerGainLow |
282 /* ---------------------------------------------------------------------- */
284 static void dump_read_message(struct tda9887 *t, unsigned char *buf)
286 static char *afc[16] = {
304 tda9887_info("read: 0x%2x\n", buf[0]);
305 tda9887_info(" after power on : %s\n", (buf[0] & 0x01) ? "yes" : "no");
306 tda9887_info(" afc : %s\n", afc[(buf[0] >> 1) & 0x0f]);
307 tda9887_info(" fmif level : %s\n", (buf[0] & 0x20) ? "high" : "low");
308 tda9887_info(" afc window : %s\n", (buf[0] & 0x40) ? "in" : "out");
309 tda9887_info(" vfi level : %s\n", (buf[0] & 0x80) ? "high" : "low");
312 static void dump_write_message(struct tda9887 *t, unsigned char *buf)
314 static char *sound[4] = {
320 static char *adjust[32] = {
321 "-16", "-15", "-14", "-13", "-12", "-11", "-10", "-9",
322 "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1",
323 "0", "+1", "+2", "+3", "+4", "+5", "+6", "+7",
324 "+8", "+9", "+10", "+11", "+12", "+13", "+14", "+15"
326 static char *deemph[4] = {
327 "no", "no", "75", "50"
329 static char *carrier[4] = {
335 static char *vif[8] = {
345 static char *rif[4] = {
352 tda9887_info("write: byte B 0x%02x\n",buf[1]);
353 tda9887_info(" B0 video mode : %s\n",
354 (buf[1] & 0x01) ? "video trap" : "sound trap");
355 tda9887_info(" B1 auto mute fm : %s\n",
356 (buf[1] & 0x02) ? "yes" : "no");
357 tda9887_info(" B2 carrier mode : %s\n",
358 (buf[1] & 0x04) ? "QSS" : "Intercarrier");
359 tda9887_info(" B3-4 tv sound/radio : %s\n",
360 sound[(buf[1] & 0x18) >> 3]);
361 tda9887_info(" B5 force mute audio: %s\n",
362 (buf[1] & 0x20) ? "yes" : "no");
363 tda9887_info(" B6 output port 1 : %s\n",
364 (buf[1] & 0x40) ? "high (inactive)" : "low (active)");
365 tda9887_info(" B7 output port 2 : %s\n",
366 (buf[1] & 0x80) ? "high (inactive)" : "low (active)");
368 tda9887_info("write: byte C 0x%02x\n",buf[2]);
369 tda9887_info(" C0-4 top adjustment : %s dB\n", adjust[buf[2] & 0x1f]);
370 tda9887_info(" C5-6 de-emphasis : %s\n", deemph[(buf[2] & 0x60) >> 5]);
371 tda9887_info(" C7 audio gain : %s\n",
372 (buf[2] & 0x80) ? "-6" : "0");
374 tda9887_info("write: byte E 0x%02x\n",buf[3]);
375 tda9887_info(" E0-1 sound carrier : %s\n",
376 carrier[(buf[3] & 0x03)]);
377 tda9887_info(" E6 l pll gating : %s\n",
378 (buf[3] & 0x40) ? "36" : "13");
382 tda9887_info(" E2-4 video if : %s\n",
383 rif[(buf[3] & 0x0c) >> 2]);
384 tda9887_info(" E7 vif agc output : %s\n",
386 ? ((buf[3] & 0x10) ? "fm-agc radio" : "sif-agc radio")
387 : "fm radio carrier afc");
390 tda9887_info(" E2-4 video if : %s\n",
391 vif[(buf[3] & 0x1c) >> 2]);
392 tda9887_info(" E5 tuner gain : %s\n",
394 ? ((buf[3] & 0x20) ? "external" : "normal")
395 : ((buf[3] & 0x20) ? "minimum" : "normal"));
396 tda9887_info(" E7 vif agc output : %s\n",
399 ? "pin3 port, pin22 vif agc out"
400 : "pin22 port, pin3 vif acg ext in")
401 : "pin3+pin22 port");
403 tda9887_info("--\n");
406 /* ---------------------------------------------------------------------- */
408 static int tda9887_set_tvnorm(struct tda9887 *t, char *buf)
410 struct tvnorm *norm = NULL;
413 if (t->mode == T_RADIO) {
414 if (t->radio_mode == V4L2_TUNER_MODE_MONO)
417 norm = &radio_stereo;
419 for (i = 0; i < ARRAY_SIZE(tvnorms); i++) {
420 if (tvnorms[i].std & t->std) {
427 tda9887_dbg("Unsupported tvnorm entry - audio muted\n");
431 tda9887_dbg("configure for: %s\n",norm->name);
438 static unsigned int port1 = UNSET;
439 static unsigned int port2 = UNSET;
440 static unsigned int qss = UNSET;
441 static unsigned int adjust = UNSET;
443 module_param(port1, int, 0644);
444 module_param(port2, int, 0644);
445 module_param(qss, int, 0644);
446 module_param(adjust, int, 0644);
448 static int tda9887_set_insmod(struct tda9887 *t, char *buf)
450 if (UNSET != port1) {
452 buf[1] |= cOutputPort1Inactive;
454 buf[1] &= ~cOutputPort1Inactive;
456 if (UNSET != port2) {
458 buf[1] |= cOutputPort2Inactive;
460 buf[1] &= ~cOutputPort2Inactive;
470 if (adjust >= 0x00 && adjust < 0x20) {
477 static int tda9887_set_config(struct tda9887 *t, char *buf)
479 if (t->config & TDA9887_PORT1_ACTIVE)
480 buf[1] &= ~cOutputPort1Inactive;
481 if (t->config & TDA9887_PORT1_INACTIVE)
482 buf[1] |= cOutputPort1Inactive;
483 if (t->config & TDA9887_PORT2_ACTIVE)
484 buf[1] &= ~cOutputPort2Inactive;
485 if (t->config & TDA9887_PORT2_INACTIVE)
486 buf[1] |= cOutputPort2Inactive;
488 if (t->config & TDA9887_QSS)
490 if (t->config & TDA9887_INTERCARRIER)
493 if (t->config & TDA9887_AUTOMUTE)
494 buf[1] |= cAutoMuteFmActive;
495 if (t->config & TDA9887_DEEMPHASIS_MASK) {
497 switch (t->config & TDA9887_DEEMPHASIS_MASK) {
498 case TDA9887_DEEMPHASIS_NONE:
499 buf[2] |= cDeemphasisOFF;
501 case TDA9887_DEEMPHASIS_50:
502 buf[2] |= cDeemphasisON | cDeemphasis50;
504 case TDA9887_DEEMPHASIS_75:
505 buf[2] |= cDeemphasisON | cDeemphasis75;
509 if (t->config & TDA9887_TOP_SET) {
511 buf[2] |= (t->config >> 8) & cTopMask;
513 if ((t->config & TDA9887_INTERCARRIER_NTSC) && (t->std & V4L2_STD_NTSC))
518 /* ---------------------------------------------------------------------- */
520 static char pal[] = "--";
521 static char secam[] = "--";
522 static char ntsc[] = "-";
524 module_param_string(pal, pal, sizeof(pal), 0644);
525 module_param_string(secam, secam, sizeof(secam), 0644);
526 module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
528 static int tda9887_fixup_std(struct tda9887 *t)
530 /* get more precise norm info from insmod option */
531 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
541 if (pal[1] == 'c' || pal[1] == 'C') {
542 tda9887_dbg("insmod fixup: PAL => PAL-Nc\n");
543 t->std = V4L2_STD_PAL_Nc;
545 tda9887_dbg("insmod fixup: PAL => PAL-BGHN\n");
546 t->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H | V4L2_STD_PAL_N;
551 tda9887_dbg("insmod fixup: PAL => PAL-I\n");
552 t->std = V4L2_STD_PAL_I;
558 tda9887_dbg("insmod fixup: PAL => PAL-DK\n");
559 t->std = V4L2_STD_PAL_DK;
563 tda9887_dbg("insmod fixup: PAL => PAL-M\n");
564 t->std = V4L2_STD_PAL_M;
567 /* default parameter, do nothing */
570 tda9887_info("pal= argument not recognised\n");
574 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
582 tda9887_dbg("insmod fixup: SECAM => SECAM-BGH\n");
583 t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
589 tda9887_dbg("insmod fixup: SECAM => SECAM-DK\n");
590 t->std = V4L2_STD_SECAM_DK;
594 if (secam[1] == 'c' || secam[1] == 'C') {
595 tda9887_dbg("insmod fixup: SECAM => SECAM-L'\n");
596 t->std = V4L2_STD_SECAM_LC;
598 tda9887_dbg("insmod fixup: SECAM => SECAM-L\n");
599 t->std = V4L2_STD_SECAM_L;
603 /* default parameter, do nothing */
606 tda9887_info("secam= argument not recognised\n");
610 if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
614 tda9887_dbg("insmod fixup: NTSC => NTSC-M\n");
615 t->std = V4L2_STD_NTSC_M;
619 tda9887_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
620 t->std = V4L2_STD_NTSC_M_JP;
624 tda9887_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
625 t->std = V4L2_STD_NTSC_M_KR;
628 /* default parameter, do nothing */
631 tda9887_info("ntsc= argument not recognised\n");
638 static int tda9887_status(struct tda9887 *t)
640 unsigned char buf[1];
643 memset(buf,0,sizeof(buf));
644 if (1 != (rc = i2c_master_recv(&t->client,buf,1)))
645 tda9887_info("i2c i/o error: rc == %d (should be 1)\n",rc);
646 dump_read_message(t, buf);
650 static int tda9887_configure(struct tda9887 *t)
654 memset(t->data,0,sizeof(t->data));
655 tda9887_set_tvnorm(t,t->data);
657 /* A note on the port settings:
658 These settings tend to depend on the specifics of the board.
659 By default they are set to inactive (bit value 1) by this driver,
660 overwriting any changes made by the tvnorm. This means that it
661 is the responsibility of the module using the tda9887 to set
662 these values in case of changes in the tvnorm.
663 In many cases port 2 should be made active (0) when selecting
664 SECAM-L, and port 2 should remain inactive (1) for SECAM-L'.
666 For the other standards the tda9887 application note says that
667 the ports should be set to active (0), but, again, that may
668 differ depending on the precise hardware configuration.
670 t->data[1] |= cOutputPort1Inactive;
671 t->data[1] |= cOutputPort2Inactive;
673 tda9887_set_config(t,t->data);
674 tda9887_set_insmod(t,t->data);
676 if (t->mode == T_STANDBY) {
677 t->data[1] |= cForcedMuteAudioON;
680 tda9887_dbg("writing: b=0x%02x c=0x%02x e=0x%02x\n",
681 t->data[1],t->data[2],t->data[3]);
683 dump_write_message(t, t->data);
685 if (4 != (rc = i2c_master_send(&t->client,t->data,4)))
686 tda9887_info("i2c i/o error: rc == %d (should be 4)\n",rc);
689 msleep_interruptible(1000);
695 /* ---------------------------------------------------------------------- */
697 static int tda9887_attach(struct i2c_adapter *adap, int addr, int kind)
701 client_template.adapter = adap;
702 client_template.addr = addr;
704 if (NULL == (t = kzalloc(sizeof(*t), GFP_KERNEL)))
707 t->client = client_template;
709 t->radio_mode = V4L2_TUNER_MODE_STEREO;
711 tda9887_info("chip found @ 0x%x (%s)\n", addr<<1, adap->name);
713 i2c_set_clientdata(&t->client, t);
714 i2c_attach_client(&t->client);
719 static int tda9887_probe(struct i2c_adapter *adap)
721 if (adap->class & I2C_CLASS_TV_ANALOG)
722 return i2c_probe(adap, &addr_data, tda9887_attach);
726 static int tda9887_detach(struct i2c_client *client)
728 struct tda9887 *t = i2c_get_clientdata(client);
730 i2c_detach_client(client);
735 #define SWITCH_V4L2 if (!t->using_v4l2 && debug) \
736 tda9887_info("switching to v4l2\n"); \
738 #define CHECK_V4L2 if (t->using_v4l2) { if (debug) \
739 tda9887_info("ignore v4l1 call\n"); \
743 tda9887_command(struct i2c_client *client, unsigned int cmd, void *arg)
745 struct tda9887 *t = i2c_get_clientdata(client);
749 /* --- configuration --- */
753 tda9887_configure(t);
756 case TUNER_SET_STANDBY:
759 tda9887_configure(t);
762 case TDA9887_SET_CONFIG:
767 tda9887_configure(t);
770 /* --- v4l ioctls --- */
771 /* take care: bttv does userspace copying, we'll get a
772 kernel pointer here... */
775 static const v4l2_std_id map[] = {
776 [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
777 [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
778 [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
779 [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
780 [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
781 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
783 struct video_channel *vc = arg;
786 t->mode = T_ANALOG_TV;
787 if (vc->norm < ARRAY_SIZE(map))
788 t->std = map[vc->norm];
789 tda9887_fixup_std(t);
790 tda9887_configure(t);
795 v4l2_std_id *id = arg;
798 t->mode = T_ANALOG_TV;
800 tda9887_fixup_std(t);
801 tda9887_configure(t);
804 case VIDIOC_S_FREQUENCY:
806 struct v4l2_frequency *f = arg;
809 if (V4L2_TUNER_ANALOG_TV == f->type) {
810 if (t->mode == T_ANALOG_TV)
812 t->mode = T_ANALOG_TV;
814 if (V4L2_TUNER_RADIO == f->type) {
815 if (t->mode == T_RADIO)
819 tda9887_configure(t);
824 static int AFC_BITS_2_kHz[] = {
825 -12500, -37500, -62500, -97500,
826 -112500, -137500, -162500, -187500,
827 187500, 162500, 137500, 112500,
828 97500 , 62500, 37500 , 12500
830 struct v4l2_tuner* tuner = arg;
832 if (t->mode == T_RADIO) {
835 if (1 == i2c_master_recv(&t->client,®,1))
836 tuner->afc = AFC_BITS_2_kHz[(reg>>1)&0x0f];
842 struct v4l2_tuner* tuner = arg;
844 if (t->mode == T_RADIO) {
845 t->radio_mode = tuner->audmode;
846 tda9887_configure (t);
850 case VIDIOC_LOG_STATUS:
852 tda9887_info("Data bytes: b=0x%02x c=0x%02x e=0x%02x\n", t->data[1], t->data[2], t->data[3]);
862 static int tda9887_suspend(struct device * dev, pm_message_t state)
864 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
865 struct tda9887 *t = i2c_get_clientdata(c);
867 tda9887_dbg("suspend\n");
871 static int tda9887_resume(struct device * dev)
873 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
874 struct tda9887 *t = i2c_get_clientdata(c);
876 tda9887_dbg("resume\n");
877 tda9887_configure(t);
881 /* ----------------------------------------------------------------------- */
883 static struct i2c_driver driver = {
884 .id = I2C_DRIVERID_TDA9887,
885 .attach_adapter = tda9887_probe,
886 .detach_client = tda9887_detach,
887 .command = tda9887_command,
890 .suspend = tda9887_suspend,
891 .resume = tda9887_resume,
894 static struct i2c_client client_template =
900 static int __init tda9887_init_module(void)
902 return i2c_add_driver(&driver);
905 static void __exit tda9887_cleanup_module(void)
907 i2c_del_driver(&driver);
910 module_init(tda9887_init_module);
911 module_exit(tda9887_cleanup_module);
914 * Overrides for Emacs so that we follow Linus's tabbing style.
915 * ---------------------------------------------------------------------------