1 #include <linux/module.h>
2 #include <linux/kernel.h>
4 #include <linux/types.h>
5 #include <linux/videodev.h>
6 #include <linux/init.h>
7 #include <linux/errno.h>
8 #include <linux/slab.h>
10 #include <media/audiochip.h>
15 TDA9886 (PAL, SECAM, NTSC)
16 TDA9887 (PAL, SECAM, NTSC, FM Radio)
19 - Pinnacle PCTV (Jul.2002 Version with MT2032, bttv)
20 TDA9887 (world), TDA9885 (USA)
21 Note: OP2 of tda988x must be set to 1, else MT2032 is disabled!
22 - KNC One TV-Station RDS (saa7134)
26 /* Addresses to scan */
27 static unsigned short normal_i2c[] = {
32 static unsigned short normal_i2c_range[] = {I2C_CLIENT_END,I2C_CLIENT_END};
36 static unsigned int debug = 0;
37 MODULE_PARM(debug,"i");
38 MODULE_LICENSE("GPL");
40 /* ---------------------------------------------------------------------- */
43 #define PREFIX "tda9885/6/7: "
44 #define dprintk if (debug) printk
47 struct i2c_client client;
50 unsigned int pinnacle_id;
51 unsigned int using_v4l2;
62 static struct i2c_driver driver;
63 static struct i2c_client client_template;
65 /* ---------------------------------------------------------------------- */
72 #define cVideoTrapBypassOFF 0x00 // bit b0
73 #define cVideoTrapBypassON 0x01 // bit b0
75 #define cAutoMuteFmInactive 0x00 // bit b1
76 #define cAutoMuteFmActive 0x02 // bit b1
78 #define cIntercarrier 0x00 // bit b2
79 #define cQSS 0x04 // bit b2
81 #define cPositiveAmTV 0x00 // bit b3:4
82 #define cFmRadio 0x08 // bit b3:4
83 #define cNegativeFmTV 0x10 // bit b3:4
86 #define cForcedMuteAudioON 0x20 // bit b5
87 #define cForcedMuteAudioOFF 0x00 // bit b5
89 #define cOutputPort1Active 0x00 // bit b6
90 #define cOutputPort1Inactive 0x40 // bit b6
92 #define cOutputPort2Active 0x00 // bit b7
93 #define cOutputPort2Inactive 0x80 // bit b7
97 #define cDeemphasisOFF 0x00 // bit c5
98 #define cDeemphasisON 0x20 // bit c5
100 #define cDeemphasis75 0x00 // bit c6
101 #define cDeemphasis50 0x40 // bit c6
103 #define cAudioGain0 0x00 // bit c7
104 #define cAudioGain6 0x80 // bit c7
108 #define cAudioIF_4_5 0x00 // bit e0:1
109 #define cAudioIF_5_5 0x01 // bit e0:1
110 #define cAudioIF_6_0 0x02 // bit e0:1
111 #define cAudioIF_6_5 0x03 // bit e0:1
114 #define cVideoIF_58_75 0x00 // bit e2:4
115 #define cVideoIF_45_75 0x04 // bit e2:4
116 #define cVideoIF_38_90 0x08 // bit e2:4
117 #define cVideoIF_38_00 0x0C // bit e2:4
118 #define cVideoIF_33_90 0x10 // bit e2:4
119 #define cVideoIF_33_40 0x14 // bit e2:4
120 #define cRadioIF_45_75 0x18 // bit e2:4
121 #define cRadioIF_38_90 0x1C // bit e2:4
124 #define cTunerGainNormal 0x00 // bit e5
125 #define cTunerGainLow 0x20 // bit e5
127 #define cGating_18 0x00 // bit e6
128 #define cGating_36 0x40 // bit e6
130 #define cAgcOutON 0x80 // bit e7
131 #define cAgcOutOFF 0x00 // bit e7
133 /* ---------------------------------------------------------------------- */
135 static struct tvnorm tvnorms[] = {
137 .std = V4L2_STD_PAL_BG,
139 .b = ( cNegativeFmTV |
141 .c = ( cDeemphasisON |
143 .e = ( cAudioIF_5_5 |
146 .std = V4L2_STD_PAL_I,
148 .b = ( cNegativeFmTV |
150 .c = ( cDeemphasisON |
152 .e = ( cAudioIF_6_0 |
155 .std = V4L2_STD_PAL_DK,
157 .b = ( cNegativeFmTV |
159 .c = ( cDeemphasisON |
161 .e = ( cAudioIF_6_5 |
164 .std = V4L2_STD_PAL_M | V4L2_STD_PAL_N,
166 .b = ( cNegativeFmTV |
168 .c = ( cDeemphasisON |
170 .e = ( cAudioIF_4_5 |
173 .std = V4L2_STD_SECAM_L,
175 .b = ( cPositiveAmTV |
177 .e = ( cAudioIF_6_5 |
180 .std = V4L2_STD_SECAM_DK,
182 .b = ( cNegativeFmTV |
184 .c = ( cDeemphasisON |
186 .e = ( cAudioIF_6_5 |
189 .std = V4L2_STD_NTSC_M,
191 .b = ( cNegativeFmTV |
193 .c = ( cDeemphasisON |
199 .std = V4L2_STD_NTSC_M_JP,
201 .b = ( cNegativeFmTV |
203 .c = ( cDeemphasisON |
211 static struct tvnorm radio = {
215 .c = ( cDeemphasisON |
217 .e = ( cAudioIF_5_5 |
221 /* ---------------------------------------------------------------------- */
223 static void dump_read_message(unsigned char *buf)
225 static char *afc[16] = {
243 printk(PREFIX "read: 0x%2x\n", buf[0]);
244 printk(" after power on : %s\n", (buf[0] & 0x01) ? "yes" : "no");
245 printk(" afc : %s\n", afc[(buf[0] >> 1) & 0x0f]);
246 printk(" afc window : %s\n", (buf[0] & 0x40) ? "in" : "out");
247 printk(" vfi level : %s\n", (buf[0] & 0x80) ? "high" : "low");
250 static void dump_write_message(unsigned char *buf)
252 static char *sound[4] = {
258 static char *adjust[32] = {
259 "-16", "-15", "-14", "-13", "-12", "-11", "-10", "-9",
260 "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1",
261 "0", "+1", "+2", "+3", "+4", "+5", "+6", "+7",
262 "+8", "+9", "+10", "+11", "+12", "+13", "+14", "+15"
264 static char *deemph[4] = {
265 "no", "no", "75", "50"
267 static char *carrier[4] = {
273 static char *vif[8] = {
283 static char *rif[4] = {
290 printk(PREFIX "write: byte B 0x%02x\n",buf[1]);
291 printk(" B0 video mode : %s\n",
292 (buf[1] & 0x01) ? "video trap" : "sound trap");
293 printk(" B1 auto mute fm : %s\n",
294 (buf[1] & 0x02) ? "yes" : "no");
295 printk(" B2 carrier mode : %s\n",
296 (buf[1] & 0x04) ? "QSS" : "Intercarrier");
297 printk(" B3-4 tv sound/radio : %s\n",
298 sound[(buf[1] & 0x18) >> 3]);
299 printk(" B5 force mute audio: %s\n",
300 (buf[1] & 0x20) ? "yes" : "no");
301 printk(" B6 output port 1 : %s\n",
302 (buf[1] & 0x40) ? "high" : "low");
303 printk(" B7 output port 2 : %s\n",
304 (buf[1] & 0x80) ? "high" : "low");
306 printk(PREFIX "write: byte C 0x%02x\n",buf[2]);
307 printk(" C0-4 top adjustment : %s dB\n", adjust[buf[2] & 0x1f]);
308 printk(" C5-6 de-emphasis : %s\n", deemph[(buf[2] & 0x60) >> 5]);
309 printk(" C7 audio gain : %s\n",
310 (buf[2] & 0x80) ? "-6" : "0");
312 printk(PREFIX "write: byte E 0x%02x\n",buf[3]);
313 printk(" E0-1 sound carrier : %s\n",
314 carrier[(buf[3] & 0x03)]);
315 printk(" E6 l pll ganting : %s\n",
316 (buf[3] & 0x40) ? "36" : "13");
320 printk(" E2-4 video if : %s\n",
321 rif[(buf[3] & 0x0c) >> 2]);
322 printk(" E7 vif agc output : %s\n",
324 ? ((buf[3] & 0x10) ? "fm-agc radio" : "sif-agc radio")
325 : "fm radio carrier afc");
328 printk(" E2-4 video if : %s\n",
329 vif[(buf[3] & 0x1c) >> 2]);
330 printk(" E5 tuner gain : %s\n",
332 ? ((buf[3] & 0x20) ? "external" : "normal")
333 : ((buf[3] & 0x20) ? "minimum" : "normal"));
334 printk(" E7 vif agc output : %s\n",
337 ? "pin3 port, pin22 vif agc out"
338 : "pin22 port, pin3 vif acg ext in")
339 : "pin3+pin22 port");
344 /* ---------------------------------------------------------------------- */
346 static int tda9887_set_tvnorm(struct tda9887 *t, char *buf)
348 struct tvnorm *norm = NULL;
354 for (i = 0; i < ARRAY_SIZE(tvnorms); i++) {
355 if (tvnorms[i].std & t->std) {
362 dprintk(PREFIX "Oops: no tvnorm entry found\n");
366 dprintk(PREFIX "configure for: %s\n",norm->name);
373 static unsigned int port1 = 1;
374 static unsigned int port2 = 1;
375 static unsigned int qss = UNSET;
376 static unsigned int adjust = 0x10;
377 MODULE_PARM(port1,"i");
378 MODULE_PARM(port2,"i");
379 MODULE_PARM(qss,"i");
380 MODULE_PARM(adjust,"i");
382 static int tda9887_set_insmod(struct tda9887 *t, char *buf)
385 buf[1] |= cOutputPort1Inactive;
387 buf[1] |= cOutputPort2Inactive;
395 if (adjust >= 0x00 && adjust < 0x20)
400 /* ---------------------------------------------------------------------- */
402 static int tda9887_set_pinnacle(struct tda9887 *t, char *buf)
404 unsigned int bCarrierMode = UNSET;
406 if (t->std & V4L2_STD_PAL) {
407 if ((1 == t->pinnacle_id) || (7 == t->pinnacle_id)) {
408 bCarrierMode = cIntercarrier;
413 if (t->std & V4L2_STD_NTSC) {
414 if ((5 == t->pinnacle_id) || (6 == t->pinnacle_id)) {
415 bCarrierMode = cIntercarrier;
421 if (bCarrierMode != UNSET) {
423 buf[1] |= bCarrierMode;
428 /* ---------------------------------------------------------------------- */
430 static char *pal = "-";
431 MODULE_PARM(pal,"s");
432 static char *secam = "-";
433 MODULE_PARM(secam,"s");
435 static int tda9887_fixup_std(struct tda9887 *t)
437 /* get more precise norm info from insmod option */
438 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
444 dprintk(PREFIX "insmod fixup: PAL => PAL-BG\n");
445 t->std = V4L2_STD_PAL_BG;
449 dprintk(PREFIX "insmod fixup: PAL => PAL-I\n");
450 t->std = V4L2_STD_PAL_I;
456 dprintk(PREFIX "insmod fixup: PAL => PAL-DK\n");
457 t->std = V4L2_STD_PAL_DK;
461 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
467 dprintk(PREFIX "insmod fixup: SECAM => SECAM-DK\n");
468 t->std = V4L2_STD_SECAM_DK;
472 dprintk(PREFIX "insmod fixup: SECAM => SECAM-L\n");
473 t->std = V4L2_STD_SECAM_L;
480 static int tda9887_status(struct tda9887 *t)
482 unsigned char buf[1];
485 memset(buf,0,sizeof(buf));
486 if (1 != (rc = i2c_master_recv(&t->client,buf,1)))
487 printk(PREFIX "i2c i/o error: rc == %d (should be 1)\n",rc);
488 dump_read_message(buf);
492 static int tda9887_configure(struct tda9887 *t)
494 unsigned char buf[4];
497 memset(buf,0,sizeof(buf));
498 tda9887_set_tvnorm(t,buf);
499 if (UNSET != t->pinnacle_id) {
500 tda9887_set_pinnacle(t,buf);
502 tda9887_set_insmod(t,buf);
504 dprintk(PREFIX "writing: b=0x%02x c=0x%02x e=0x%02x\n",
505 buf[1],buf[2],buf[3]);
507 dump_write_message(buf);
509 if (4 != (rc = i2c_master_send(&t->client,buf,4)))
510 printk(PREFIX "i2c i/o error: rc == %d (should be 4)\n",rc);
513 set_current_state(TASK_INTERRUPTIBLE);
514 schedule_timeout(HZ);
520 /* ---------------------------------------------------------------------- */
522 static int tda9887_attach(struct i2c_adapter *adap, int addr, int kind)
526 client_template.adapter = adap;
527 client_template.addr = addr;
529 printk(PREFIX "chip found @ 0x%x\n", addr<<1);
531 if (NULL == (t = kmalloc(sizeof(*t), GFP_KERNEL)))
533 memset(t,0,sizeof(*t));
534 t->client = client_template;
536 t->pinnacle_id = UNSET;
537 i2c_set_clientdata(&t->client, t);
538 i2c_attach_client(&t->client);
543 static int tda9887_probe(struct i2c_adapter *adap)
545 #ifdef I2C_CLASS_TV_ANALOG
546 if (adap->class & I2C_CLASS_TV_ANALOG)
547 return i2c_probe(adap, &addr_data, tda9887_attach);
550 case I2C_ALGO_BIT | I2C_HW_B_BT848:
551 case I2C_ALGO_BIT | I2C_HW_B_RIVA:
552 case I2C_ALGO_SAA7134:
553 return i2c_probe(adap, &addr_data, tda9887_attach);
560 static int tda9887_detach(struct i2c_client *client)
562 struct tda9887 *t = i2c_get_clientdata(client);
564 i2c_detach_client(client);
569 #define SWITCH_V4L2 if (!t->using_v4l2 && debug) \
570 printk(PREFIX "switching to v4l2\n"); \
572 #define CHECK_V4L2 if (t->using_v4l2) { if (debug) \
573 printk(PREFIX "ignore v4l1 call\n"); \
577 tda9887_command(struct i2c_client *client, unsigned int cmd, void *arg)
579 struct tda9887 *t = i2c_get_clientdata(client);
583 /* --- configuration --- */
586 tda9887_configure(t);
589 case AUDC_CONFIG_PINNACLE:
594 tda9887_configure(t);
597 /* --- v4l ioctls --- */
598 /* take care: bttv does userspace copying, we'll get a
599 kernel pointer here... */
602 static const v4l2_std_id map[] = {
603 [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
604 [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
605 [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
606 [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
607 [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
608 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
610 struct video_channel *vc = arg;
614 if (vc->norm < ARRAY_SIZE(map))
615 t->std = map[vc->norm];
616 tda9887_fixup_std(t);
617 tda9887_configure(t);
622 v4l2_std_id *id = arg;
627 tda9887_fixup_std(t);
628 tda9887_configure(t);
631 case VIDIOC_S_FREQUENCY:
633 struct v4l2_frequency *f = arg;
636 if (V4L2_TUNER_ANALOG_TV == f->type) {
641 if (V4L2_TUNER_RADIO == f->type) {
646 tda9887_configure(t);
655 /* ----------------------------------------------------------------------- */
657 static struct i2c_driver driver = {
658 .owner = THIS_MODULE,
659 .name = "i2c tda9887 driver",
660 .id = -1, /* FIXME */
661 .flags = I2C_DF_NOTIFY,
662 .attach_adapter = tda9887_probe,
663 .detach_client = tda9887_detach,
664 .command = tda9887_command,
666 static struct i2c_client client_template =
668 I2C_DEVNAME("tda9887"),
669 .flags = I2C_CLIENT_ALLOW_USE,
673 static int tda9887_init_module(void)
675 i2c_add_driver(&driver);
679 static void tda9887_cleanup_module(void)
681 i2c_del_driver(&driver);
684 module_init(tda9887_init_module);
685 module_exit(tda9887_cleanup_module);
688 * Overrides for Emacs so that we follow Linus's tabbing style.
689 * ---------------------------------------------------------------------------