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 PREFIX "tda9885/6/7: "
48 #define dprintk if (debug) printk
51 struct i2c_client client;
55 unsigned int pinnacle_id;
56 unsigned int using_v4l2;
57 unsigned int radio_mode;
68 static struct i2c_driver driver;
69 static struct i2c_client client_template;
71 /* ---------------------------------------------------------------------- */
78 #define cVideoTrapBypassOFF 0x00 // bit b0
79 #define cVideoTrapBypassON 0x01 // bit b0
81 #define cAutoMuteFmInactive 0x00 // bit b1
82 #define cAutoMuteFmActive 0x02 // bit b1
84 #define cIntercarrier 0x00 // bit b2
85 #define cQSS 0x04 // bit b2
87 #define cPositiveAmTV 0x00 // bit b3:4
88 #define cFmRadio 0x08 // bit b3:4
89 #define cNegativeFmTV 0x10 // bit b3:4
92 #define cForcedMuteAudioON 0x20 // bit b5
93 #define cForcedMuteAudioOFF 0x00 // bit b5
95 #define cOutputPort1Active 0x00 // bit b6
96 #define cOutputPort1Inactive 0x40 // bit b6
98 #define cOutputPort2Active 0x00 // bit b7
99 #define cOutputPort2Inactive 0x80 // bit b7
103 #define cDeemphasisOFF 0x00 // bit c5
104 #define cDeemphasisON 0x20 // bit c5
106 #define cDeemphasis75 0x00 // bit c6
107 #define cDeemphasis50 0x40 // bit c6
109 #define cAudioGain0 0x00 // bit c7
110 #define cAudioGain6 0x80 // bit c7
114 #define cAudioIF_4_5 0x00 // bit e0:1
115 #define cAudioIF_5_5 0x01 // bit e0:1
116 #define cAudioIF_6_0 0x02 // bit e0:1
117 #define cAudioIF_6_5 0x03 // bit e0:1
120 #define cVideoIF_58_75 0x00 // bit e2:4
121 #define cVideoIF_45_75 0x04 // bit e2:4
122 #define cVideoIF_38_90 0x08 // bit e2:4
123 #define cVideoIF_38_00 0x0C // bit e2:4
124 #define cVideoIF_33_90 0x10 // bit e2:4
125 #define cVideoIF_33_40 0x14 // bit e2:4
126 #define cRadioIF_45_75 0x18 // bit e2:4
127 #define cRadioIF_38_90 0x1C // bit e2:4
130 #define cTunerGainNormal 0x00 // bit e5
131 #define cTunerGainLow 0x20 // bit e5
133 #define cGating_18 0x00 // bit e6
134 #define cGating_36 0x40 // bit e6
136 #define cAgcOutON 0x80 // bit e7
137 #define cAgcOutOFF 0x00 // bit e7
139 /* ---------------------------------------------------------------------- */
141 static struct tvnorm tvnorms[] = {
143 .std = V4L2_STD_PAL_BG,
145 .b = ( cNegativeFmTV |
147 .c = ( cDeemphasisON |
149 .e = ( cAudioIF_5_5 |
152 .std = V4L2_STD_PAL_I,
154 .b = ( cNegativeFmTV |
156 .c = ( cDeemphasisON |
158 .e = ( cAudioIF_6_0 |
161 .std = V4L2_STD_PAL_DK,
163 .b = ( cNegativeFmTV |
165 .c = ( cDeemphasisON |
167 .e = ( cAudioIF_6_5 |
170 .std = V4L2_STD_PAL_M | V4L2_STD_PAL_N,
172 .b = ( cNegativeFmTV |
174 .c = ( cDeemphasisON |
176 .e = ( cAudioIF_4_5 |
179 .std = V4L2_STD_SECAM_L,
181 .b = ( cPositiveAmTV |
183 .e = ( cAudioIF_6_5 |
186 .std = V4L2_STD_SECAM_DK,
188 .b = ( cNegativeFmTV |
190 .c = ( cDeemphasisON |
192 .e = ( cAudioIF_6_5 |
195 .std = V4L2_STD_NTSC_M,
197 .b = ( cNegativeFmTV |
199 .c = ( cDeemphasisON |
205 .std = V4L2_STD_NTSC_M_JP,
207 .b = ( cNegativeFmTV |
209 .c = ( cDeemphasisON |
217 static struct tvnorm radio_stereo = {
218 .name = "Radio Stereo",
221 .c = ( cDeemphasisOFF |
223 .e = ( cAudioIF_5_5 |
227 static struct tvnorm radio_mono = {
228 .name = "Radio Mono",
231 .c = ( cDeemphasisON |
233 .e = ( cAudioIF_5_5 |
237 /* ---------------------------------------------------------------------- */
239 static void dump_read_message(unsigned char *buf)
241 static char *afc[16] = {
259 printk(PREFIX "read: 0x%2x\n", buf[0]);
260 printk(" after power on : %s\n", (buf[0] & 0x01) ? "yes" : "no");
261 printk(" afc : %s\n", afc[(buf[0] >> 1) & 0x0f]);
262 printk(" fmif level : %s\n", (buf[0] & 0x20) ? "high" : "low");
263 printk(" afc window : %s\n", (buf[0] & 0x40) ? "in" : "out");
264 printk(" vfi level : %s\n", (buf[0] & 0x80) ? "high" : "low");
267 static void dump_write_message(unsigned char *buf)
269 static char *sound[4] = {
275 static char *adjust[32] = {
276 "-16", "-15", "-14", "-13", "-12", "-11", "-10", "-9",
277 "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1",
278 "0", "+1", "+2", "+3", "+4", "+5", "+6", "+7",
279 "+8", "+9", "+10", "+11", "+12", "+13", "+14", "+15"
281 static char *deemph[4] = {
282 "no", "no", "75", "50"
284 static char *carrier[4] = {
290 static char *vif[8] = {
300 static char *rif[4] = {
307 printk(PREFIX "write: byte B 0x%02x\n",buf[1]);
308 printk(" B0 video mode : %s\n",
309 (buf[1] & 0x01) ? "video trap" : "sound trap");
310 printk(" B1 auto mute fm : %s\n",
311 (buf[1] & 0x02) ? "yes" : "no");
312 printk(" B2 carrier mode : %s\n",
313 (buf[1] & 0x04) ? "QSS" : "Intercarrier");
314 printk(" B3-4 tv sound/radio : %s\n",
315 sound[(buf[1] & 0x18) >> 3]);
316 printk(" B5 force mute audio: %s\n",
317 (buf[1] & 0x20) ? "yes" : "no");
318 printk(" B6 output port 1 : %s\n",
319 (buf[1] & 0x40) ? "high (inactive)" : "low (active)");
320 printk(" B7 output port 2 : %s\n",
321 (buf[1] & 0x80) ? "high (inactive)" : "low (active)");
323 printk(PREFIX "write: byte C 0x%02x\n",buf[2]);
324 printk(" C0-4 top adjustment : %s dB\n", adjust[buf[2] & 0x1f]);
325 printk(" C5-6 de-emphasis : %s\n", deemph[(buf[2] & 0x60) >> 5]);
326 printk(" C7 audio gain : %s\n",
327 (buf[2] & 0x80) ? "-6" : "0");
329 printk(PREFIX "write: byte E 0x%02x\n",buf[3]);
330 printk(" E0-1 sound carrier : %s\n",
331 carrier[(buf[3] & 0x03)]);
332 printk(" E6 l pll ganting : %s\n",
333 (buf[3] & 0x40) ? "36" : "13");
337 printk(" E2-4 video if : %s\n",
338 rif[(buf[3] & 0x0c) >> 2]);
339 printk(" E7 vif agc output : %s\n",
341 ? ((buf[3] & 0x10) ? "fm-agc radio" : "sif-agc radio")
342 : "fm radio carrier afc");
345 printk(" E2-4 video if : %s\n",
346 vif[(buf[3] & 0x1c) >> 2]);
347 printk(" E5 tuner gain : %s\n",
349 ? ((buf[3] & 0x20) ? "external" : "normal")
350 : ((buf[3] & 0x20) ? "minimum" : "normal"));
351 printk(" E7 vif agc output : %s\n",
354 ? "pin3 port, pin22 vif agc out"
355 : "pin22 port, pin3 vif acg ext in")
356 : "pin3+pin22 port");
361 /* ---------------------------------------------------------------------- */
363 static int tda9887_set_tvnorm(struct tda9887 *t, char *buf)
365 struct tvnorm *norm = NULL;
368 if (t->mode == T_RADIO) {
369 if (t->radio_mode == V4L2_TUNER_MODE_MONO)
372 norm = &radio_stereo;
374 for (i = 0; i < ARRAY_SIZE(tvnorms); i++) {
375 if (tvnorms[i].std & t->std) {
382 dprintk(PREFIX "Unsupported tvnorm entry - audio muted\n");
386 dprintk(PREFIX "configure for: %s\n",norm->name);
393 static unsigned int port1 = UNSET;
394 static unsigned int port2 = UNSET;
395 static unsigned int qss = UNSET;
396 static unsigned int adjust = 0x10;
397 module_param(port1, int, 0644);
398 module_param(port2, int, 0644);
399 module_param(qss, int, 0644);
400 module_param(adjust, int, 0644);
402 static int tda9887_set_insmod(struct tda9887 *t, char *buf)
404 if (UNSET != port1) {
406 buf[1] |= cOutputPort1Inactive;
408 buf[1] &= ~cOutputPort1Inactive;
410 if (UNSET != port2) {
412 buf[1] |= cOutputPort2Inactive;
414 buf[1] &= ~cOutputPort2Inactive;
424 if (adjust >= 0x00 && adjust < 0x20)
429 static int tda9887_set_config(struct tda9887 *t, char *buf)
431 if (t->config & TDA9887_PORT1_ACTIVE)
432 buf[1] &= ~cOutputPort1Inactive;
433 if (t->config & TDA9887_PORT1_INACTIVE)
434 buf[1] |= cOutputPort1Inactive;
435 if (t->config & TDA9887_PORT2_ACTIVE)
436 buf[1] &= ~cOutputPort2Inactive;
437 if (t->config & TDA9887_PORT2_INACTIVE)
438 buf[1] |= cOutputPort2Inactive;
440 if (t->config & TDA9887_QSS)
442 if (t->config & TDA9887_INTERCARRIER)
445 if (t->config & TDA9887_AUTOMUTE)
446 buf[1] |= cAutoMuteFmActive;
447 if (t->config & TDA9887_DEEMPHASIS_MASK) {
449 switch (t->config & TDA9887_DEEMPHASIS_MASK) {
450 case TDA9887_DEEMPHASIS_NONE:
451 buf[2] |= cDeemphasisOFF;
453 case TDA9887_DEEMPHASIS_50:
454 buf[2] |= cDeemphasisON | cDeemphasis50;
456 case TDA9887_DEEMPHASIS_75:
457 buf[2] |= cDeemphasisON | cDeemphasis75;
464 /* ---------------------------------------------------------------------- */
466 static int tda9887_set_pinnacle(struct tda9887 *t, char *buf)
468 unsigned int bCarrierMode = UNSET;
470 if (t->std & V4L2_STD_625_50) {
471 if ((1 == t->pinnacle_id) || (7 == t->pinnacle_id)) {
472 bCarrierMode = cIntercarrier;
477 if (t->std & V4L2_STD_525_60) {
478 if ((5 == t->pinnacle_id) || (6 == t->pinnacle_id)) {
479 bCarrierMode = cIntercarrier;
485 if (bCarrierMode != UNSET) {
487 buf[1] |= bCarrierMode;
492 /* ---------------------------------------------------------------------- */
494 static char pal[] = "-";
495 module_param_string(pal, pal, sizeof(pal), 0644);
496 static char secam[] = "-";
497 module_param_string(secam, secam, sizeof(secam), 0644);
499 static int tda9887_fixup_std(struct tda9887 *t)
501 /* get more precise norm info from insmod option */
502 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
508 dprintk(PREFIX "insmod fixup: PAL => PAL-BG\n");
509 t->std = V4L2_STD_PAL_BG;
513 dprintk(PREFIX "insmod fixup: PAL => PAL-I\n");
514 t->std = V4L2_STD_PAL_I;
520 dprintk(PREFIX "insmod fixup: PAL => PAL-DK\n");
521 t->std = V4L2_STD_PAL_DK;
524 /* default parameter, do nothing */
527 printk(PREFIX "pal= argument not recognised\n");
531 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
537 dprintk(PREFIX "insmod fixup: SECAM => SECAM-DK\n");
538 t->std = V4L2_STD_SECAM_DK;
542 dprintk(PREFIX "insmod fixup: SECAM => SECAM-L\n");
543 t->std = V4L2_STD_SECAM_L;
546 /* default parameter, do nothing */
549 printk(PREFIX "secam= argument not recognised\n");
556 static int tda9887_status(struct tda9887 *t)
558 unsigned char buf[1];
561 memset(buf,0,sizeof(buf));
562 if (1 != (rc = i2c_master_recv(&t->client,buf,1)))
563 printk(PREFIX "i2c i/o error: rc == %d (should be 1)\n",rc);
564 dump_read_message(buf);
568 static int tda9887_configure(struct tda9887 *t)
570 unsigned char buf[4];
573 memset(buf,0,sizeof(buf));
574 tda9887_set_tvnorm(t,buf);
576 buf[1] |= cOutputPort1Inactive;
577 buf[1] |= cOutputPort2Inactive;
579 if (UNSET != t->pinnacle_id) {
580 tda9887_set_pinnacle(t,buf);
582 tda9887_set_config(t,buf);
583 tda9887_set_insmod(t,buf);
585 if (t->mode == T_STANDBY) {
586 buf[1] |= cForcedMuteAudioON;
590 dprintk(PREFIX "writing: b=0x%02x c=0x%02x e=0x%02x\n",
591 buf[1],buf[2],buf[3]);
593 dump_write_message(buf);
595 if (4 != (rc = i2c_master_send(&t->client,buf,4)))
596 printk(PREFIX "i2c i/o error: rc == %d (should be 4)\n",rc);
599 msleep_interruptible(1000);
605 /* ---------------------------------------------------------------------- */
607 static int tda9887_attach(struct i2c_adapter *adap, int addr, int kind)
611 client_template.adapter = adap;
612 client_template.addr = addr;
614 printk(PREFIX "chip found @ 0x%x\n", addr<<1);
616 if (NULL == (t = kmalloc(sizeof(*t), GFP_KERNEL)))
618 memset(t,0,sizeof(*t));
620 t->client = client_template;
622 t->pinnacle_id = UNSET;
623 t->radio_mode = V4L2_TUNER_MODE_STEREO;
625 i2c_set_clientdata(&t->client, t);
626 i2c_attach_client(&t->client);
631 static int tda9887_probe(struct i2c_adapter *adap)
633 #ifdef I2C_CLASS_TV_ANALOG
634 if (adap->class & I2C_CLASS_TV_ANALOG)
635 return i2c_probe(adap, &addr_data, tda9887_attach);
641 return i2c_probe(adap, &addr_data, tda9887_attach);
648 static int tda9887_detach(struct i2c_client *client)
650 struct tda9887 *t = i2c_get_clientdata(client);
652 i2c_detach_client(client);
657 #define SWITCH_V4L2 if (!t->using_v4l2 && debug) \
658 printk(PREFIX "switching to v4l2\n"); \
660 #define CHECK_V4L2 if (t->using_v4l2) { if (debug) \
661 printk(PREFIX "ignore v4l1 call\n"); \
665 tda9887_command(struct i2c_client *client, unsigned int cmd, void *arg)
667 struct tda9887 *t = i2c_get_clientdata(client);
671 /* --- configuration --- */
675 tda9887_configure(t);
678 case TUNER_SET_STANDBY:
681 tda9887_configure(t);
684 case AUDC_CONFIG_PINNACLE:
689 tda9887_configure(t);
692 case TDA9887_SET_CONFIG:
697 tda9887_configure(t);
700 /* --- v4l ioctls --- */
701 /* take care: bttv does userspace copying, we'll get a
702 kernel pointer here... */
705 static const v4l2_std_id map[] = {
706 [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
707 [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
708 [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
709 [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
710 [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
711 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
713 struct video_channel *vc = arg;
716 t->mode = T_ANALOG_TV;
717 if (vc->norm < ARRAY_SIZE(map))
718 t->std = map[vc->norm];
719 tda9887_fixup_std(t);
720 tda9887_configure(t);
725 v4l2_std_id *id = arg;
728 t->mode = T_ANALOG_TV;
730 tda9887_fixup_std(t);
731 tda9887_configure(t);
734 case VIDIOC_S_FREQUENCY:
736 struct v4l2_frequency *f = arg;
739 if (V4L2_TUNER_ANALOG_TV == f->type) {
740 if (t->mode == T_ANALOG_TV)
742 t->mode = T_ANALOG_TV;
744 if (V4L2_TUNER_RADIO == f->type) {
745 if (t->mode == T_RADIO)
749 tda9887_configure(t);
754 static int AFC_BITS_2_kHz[] = {
755 -12500, -37500, -62500, -97500,
756 -112500, -137500, -162500, -187500,
757 187500, 162500, 137500, 112500,
758 97500 , 62500, 37500 , 12500
760 struct v4l2_tuner* tuner = arg;
762 if (t->mode == T_RADIO) {
765 if (1 == i2c_master_recv(&t->client,®,1))
766 tuner->afc = AFC_BITS_2_kHz[(reg>>1)&0x0f];
772 struct v4l2_tuner* tuner = arg;
774 if (t->mode == T_RADIO) {
775 t->radio_mode = tuner->audmode;
776 tda9887_configure (t);
787 static int tda9887_suspend(struct device * dev, pm_message_t state)
789 dprintk("tda9887: suspend\n");
793 static int tda9887_resume(struct device * dev)
795 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
796 struct tda9887 *t = i2c_get_clientdata(c);
798 dprintk("tda9887: resume\n");
799 tda9887_configure(t);
803 /* ----------------------------------------------------------------------- */
805 static struct i2c_driver driver = {
806 .owner = THIS_MODULE,
807 .name = "i2c tda9887 driver",
808 .id = -1, /* FIXME */
809 .flags = I2C_DF_NOTIFY,
810 .attach_adapter = tda9887_probe,
811 .detach_client = tda9887_detach,
812 .command = tda9887_command,
814 .suspend = tda9887_suspend,
815 .resume = tda9887_resume,
818 static struct i2c_client client_template =
821 .flags = I2C_CLIENT_ALLOW_USE,
825 static int __init tda9887_init_module(void)
827 return i2c_add_driver(&driver);
830 static void __exit tda9887_cleanup_module(void)
832 i2c_del_driver(&driver);
835 module_init(tda9887_init_module);
836 module_exit(tda9887_cleanup_module);
839 * Overrides for Emacs so that we follow Linus's tabbing style.
840 * ---------------------------------------------------------------------------