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
|
187 .std
= V4L2_STD_SECAM_DK
,
189 .b
= ( cNegativeFmTV
|
191 .c
= ( cDeemphasisON
|
193 .e
= ( cAudioIF_6_5
|
196 .std
= V4L2_STD_NTSC_M
,
198 .b
= ( cNegativeFmTV
|
200 .c
= ( cDeemphasisON
|
206 .std
= V4L2_STD_NTSC_M_JP
,
208 .b
= ( cNegativeFmTV
|
210 .c
= ( cDeemphasisON
|
218 static struct tvnorm radio_stereo
= {
219 .name
= "Radio Stereo",
222 .c
= ( cDeemphasisOFF
|
224 .e
= ( cAudioIF_5_5
|
228 static struct tvnorm radio_mono
= {
229 .name
= "Radio Mono",
232 .c
= ( cDeemphasisON
|
234 .e
= ( cAudioIF_5_5
|
238 /* ---------------------------------------------------------------------- */
240 static void dump_read_message(unsigned char *buf
)
242 static char *afc
[16] = {
260 printk(PREFIX
"read: 0x%2x\n", buf
[0]);
261 printk(" after power on : %s\n", (buf
[0] & 0x01) ? "yes" : "no");
262 printk(" afc : %s\n", afc
[(buf
[0] >> 1) & 0x0f]);
263 printk(" fmif level : %s\n", (buf
[0] & 0x20) ? "high" : "low");
264 printk(" afc window : %s\n", (buf
[0] & 0x40) ? "in" : "out");
265 printk(" vfi level : %s\n", (buf
[0] & 0x80) ? "high" : "low");
268 static void dump_write_message(unsigned char *buf
)
270 static char *sound
[4] = {
276 static char *adjust
[32] = {
277 "-16", "-15", "-14", "-13", "-12", "-11", "-10", "-9",
278 "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1",
279 "0", "+1", "+2", "+3", "+4", "+5", "+6", "+7",
280 "+8", "+9", "+10", "+11", "+12", "+13", "+14", "+15"
282 static char *deemph
[4] = {
283 "no", "no", "75", "50"
285 static char *carrier
[4] = {
291 static char *vif
[8] = {
301 static char *rif
[4] = {
308 printk(PREFIX
"write: byte B 0x%02x\n",buf
[1]);
309 printk(" B0 video mode : %s\n",
310 (buf
[1] & 0x01) ? "video trap" : "sound trap");
311 printk(" B1 auto mute fm : %s\n",
312 (buf
[1] & 0x02) ? "yes" : "no");
313 printk(" B2 carrier mode : %s\n",
314 (buf
[1] & 0x04) ? "QSS" : "Intercarrier");
315 printk(" B3-4 tv sound/radio : %s\n",
316 sound
[(buf
[1] & 0x18) >> 3]);
317 printk(" B5 force mute audio: %s\n",
318 (buf
[1] & 0x20) ? "yes" : "no");
319 printk(" B6 output port 1 : %s\n",
320 (buf
[1] & 0x40) ? "high (inactive)" : "low (active)");
321 printk(" B7 output port 2 : %s\n",
322 (buf
[1] & 0x80) ? "high (inactive)" : "low (active)");
324 printk(PREFIX
"write: byte C 0x%02x\n",buf
[2]);
325 printk(" C0-4 top adjustment : %s dB\n", adjust
[buf
[2] & 0x1f]);
326 printk(" C5-6 de-emphasis : %s\n", deemph
[(buf
[2] & 0x60) >> 5]);
327 printk(" C7 audio gain : %s\n",
328 (buf
[2] & 0x80) ? "-6" : "0");
330 printk(PREFIX
"write: byte E 0x%02x\n",buf
[3]);
331 printk(" E0-1 sound carrier : %s\n",
332 carrier
[(buf
[3] & 0x03)]);
333 printk(" E6 l pll gaiting : %s\n",
334 (buf
[3] & 0x40) ? "36" : "13");
338 printk(" E2-4 video if : %s\n",
339 rif
[(buf
[3] & 0x0c) >> 2]);
340 printk(" E7 vif agc output : %s\n",
342 ? ((buf
[3] & 0x10) ? "fm-agc radio" : "sif-agc radio")
343 : "fm radio carrier afc");
346 printk(" E2-4 video if : %s\n",
347 vif
[(buf
[3] & 0x1c) >> 2]);
348 printk(" E5 tuner gain : %s\n",
350 ? ((buf
[3] & 0x20) ? "external" : "normal")
351 : ((buf
[3] & 0x20) ? "minimum" : "normal"));
352 printk(" E7 vif agc output : %s\n",
355 ? "pin3 port, pin22 vif agc out"
356 : "pin22 port, pin3 vif acg ext in")
357 : "pin3+pin22 port");
362 /* ---------------------------------------------------------------------- */
364 static int tda9887_set_tvnorm(struct tda9887
*t
, char *buf
)
366 struct tvnorm
*norm
= NULL
;
369 if (t
->mode
== T_RADIO
) {
370 if (t
->radio_mode
== V4L2_TUNER_MODE_MONO
)
373 norm
= &radio_stereo
;
375 for (i
= 0; i
< ARRAY_SIZE(tvnorms
); i
++) {
376 if (tvnorms
[i
].std
& t
->std
) {
383 dprintk(PREFIX
"Unsupported tvnorm entry - audio muted\n");
387 dprintk(PREFIX
"configure for: %s\n",norm
->name
);
394 static unsigned int port1
= UNSET
;
395 static unsigned int port2
= UNSET
;
396 static unsigned int qss
= UNSET
;
397 static unsigned int adjust
= 0x10;
398 module_param(port1
, int, 0644);
399 module_param(port2
, int, 0644);
400 module_param(qss
, int, 0644);
401 module_param(adjust
, int, 0644);
403 static int tda9887_set_insmod(struct tda9887
*t
, char *buf
)
405 if (UNSET
!= port1
) {
407 buf
[1] |= cOutputPort1Inactive
;
409 buf
[1] &= ~cOutputPort1Inactive
;
411 if (UNSET
!= port2
) {
413 buf
[1] |= cOutputPort2Inactive
;
415 buf
[1] &= ~cOutputPort2Inactive
;
425 if (adjust
>= 0x00 && adjust
< 0x20)
430 static int tda9887_set_config(struct tda9887
*t
, char *buf
)
432 if (t
->config
& TDA9887_PORT1_ACTIVE
)
433 buf
[1] &= ~cOutputPort1Inactive
;
434 if (t
->config
& TDA9887_PORT1_INACTIVE
)
435 buf
[1] |= cOutputPort1Inactive
;
436 if (t
->config
& TDA9887_PORT2_ACTIVE
)
437 buf
[1] &= ~cOutputPort2Inactive
;
438 if (t
->config
& TDA9887_PORT2_INACTIVE
)
439 buf
[1] |= cOutputPort2Inactive
;
441 if (t
->config
& TDA9887_QSS
)
443 if (t
->config
& TDA9887_INTERCARRIER
)
446 if (t
->config
& TDA9887_AUTOMUTE
)
447 buf
[1] |= cAutoMuteFmActive
;
448 if (t
->config
& TDA9887_DEEMPHASIS_MASK
) {
450 switch (t
->config
& TDA9887_DEEMPHASIS_MASK
) {
451 case TDA9887_DEEMPHASIS_NONE
:
452 buf
[2] |= cDeemphasisOFF
;
454 case TDA9887_DEEMPHASIS_50
:
455 buf
[2] |= cDeemphasisON
| cDeemphasis50
;
457 case TDA9887_DEEMPHASIS_75
:
458 buf
[2] |= cDeemphasisON
| cDeemphasis75
;
465 /* ---------------------------------------------------------------------- */
467 static int tda9887_set_pinnacle(struct tda9887
*t
, char *buf
)
469 unsigned int bCarrierMode
= UNSET
;
471 if (t
->std
& V4L2_STD_625_50
) {
472 if ((1 == t
->pinnacle_id
) || (7 == t
->pinnacle_id
)) {
473 bCarrierMode
= cIntercarrier
;
478 if (t
->std
& V4L2_STD_525_60
) {
479 if ((5 == t
->pinnacle_id
) || (6 == t
->pinnacle_id
)) {
480 bCarrierMode
= cIntercarrier
;
486 if (bCarrierMode
!= UNSET
) {
488 buf
[1] |= bCarrierMode
;
493 /* ---------------------------------------------------------------------- */
495 static char pal
[] = "-";
496 module_param_string(pal
, pal
, sizeof(pal
), 0644);
497 static char secam
[] = "-";
498 module_param_string(secam
, secam
, sizeof(secam
), 0644);
500 static int tda9887_fixup_std(struct tda9887
*t
)
502 /* get more precise norm info from insmod option */
503 if ((t
->std
& V4L2_STD_PAL
) == V4L2_STD_PAL
) {
509 dprintk(PREFIX
"insmod fixup: PAL => PAL-BG\n");
510 t
->std
= V4L2_STD_PAL_BG
;
514 dprintk(PREFIX
"insmod fixup: PAL => PAL-I\n");
515 t
->std
= V4L2_STD_PAL_I
;
521 dprintk(PREFIX
"insmod fixup: PAL => PAL-DK\n");
522 t
->std
= V4L2_STD_PAL_DK
;
525 /* default parameter, do nothing */
528 printk(PREFIX
"pal= argument not recognised\n");
532 if ((t
->std
& V4L2_STD_SECAM
) == V4L2_STD_SECAM
) {
538 dprintk(PREFIX
"insmod fixup: SECAM => SECAM-DK\n");
539 t
->std
= V4L2_STD_SECAM_DK
;
543 dprintk(PREFIX
"insmod fixup: SECAM => SECAM-L\n");
544 t
->std
= V4L2_STD_SECAM_L
;
547 /* default parameter, do nothing */
550 printk(PREFIX
"secam= argument not recognised\n");
557 static int tda9887_status(struct tda9887
*t
)
559 unsigned char buf
[1];
562 memset(buf
,0,sizeof(buf
));
563 if (1 != (rc
= i2c_master_recv(&t
->client
,buf
,1)))
564 printk(PREFIX
"i2c i/o error: rc == %d (should be 1)\n",rc
);
565 dump_read_message(buf
);
569 static int tda9887_configure(struct tda9887
*t
)
571 unsigned char buf
[4];
574 memset(buf
,0,sizeof(buf
));
575 tda9887_set_tvnorm(t
,buf
);
577 buf
[1] |= cOutputPort1Inactive
;
578 buf
[1] |= cOutputPort2Inactive
;
580 if (UNSET
!= t
->pinnacle_id
) {
581 tda9887_set_pinnacle(t
,buf
);
583 tda9887_set_config(t
,buf
);
584 tda9887_set_insmod(t
,buf
);
586 if (t
->mode
== T_STANDBY
) {
587 buf
[1] |= cForcedMuteAudioON
;
591 dprintk(PREFIX
"writing: b=0x%02x c=0x%02x e=0x%02x\n",
592 buf
[1],buf
[2],buf
[3]);
594 dump_write_message(buf
);
596 if (4 != (rc
= i2c_master_send(&t
->client
,buf
,4)))
597 printk(PREFIX
"i2c i/o error: rc == %d (should be 4)\n",rc
);
600 msleep_interruptible(1000);
606 /* ---------------------------------------------------------------------- */
608 static int tda9887_attach(struct i2c_adapter
*adap
, int addr
, int kind
)
612 client_template
.adapter
= adap
;
613 client_template
.addr
= addr
;
615 printk(PREFIX
"chip found @ 0x%x\n", addr
<<1);
617 if (NULL
== (t
= kmalloc(sizeof(*t
), GFP_KERNEL
)))
619 memset(t
,0,sizeof(*t
));
621 t
->client
= client_template
;
623 t
->pinnacle_id
= UNSET
;
624 t
->radio_mode
= V4L2_TUNER_MODE_STEREO
;
626 i2c_set_clientdata(&t
->client
, t
);
627 i2c_attach_client(&t
->client
);
632 static int tda9887_probe(struct i2c_adapter
*adap
)
634 #ifdef I2C_CLASS_TV_ANALOG
635 if (adap
->class & I2C_CLASS_TV_ANALOG
)
636 return i2c_probe(adap
, &addr_data
, tda9887_attach
);
642 return i2c_probe(adap
, &addr_data
, tda9887_attach
);
649 static int tda9887_detach(struct i2c_client
*client
)
651 struct tda9887
*t
= i2c_get_clientdata(client
);
653 i2c_detach_client(client
);
658 #define SWITCH_V4L2 if (!t->using_v4l2 && debug) \
659 printk(PREFIX "switching to v4l2\n"); \
661 #define CHECK_V4L2 if (t->using_v4l2) { if (debug) \
662 printk(PREFIX "ignore v4l1 call\n"); \
666 tda9887_command(struct i2c_client
*client
, unsigned int cmd
, void *arg
)
668 struct tda9887
*t
= i2c_get_clientdata(client
);
672 /* --- configuration --- */
676 tda9887_configure(t
);
679 case TUNER_SET_STANDBY
:
682 tda9887_configure(t
);
685 case AUDC_CONFIG_PINNACLE
:
690 tda9887_configure(t
);
693 case TDA9887_SET_CONFIG
:
698 tda9887_configure(t
);
701 /* --- v4l ioctls --- */
702 /* take care: bttv does userspace copying, we'll get a
703 kernel pointer here... */
706 static const v4l2_std_id map
[] = {
707 [ VIDEO_MODE_PAL
] = V4L2_STD_PAL
,
708 [ VIDEO_MODE_NTSC
] = V4L2_STD_NTSC_M
,
709 [ VIDEO_MODE_SECAM
] = V4L2_STD_SECAM
,
710 [ 4 /* bttv */ ] = V4L2_STD_PAL_M
,
711 [ 5 /* bttv */ ] = V4L2_STD_PAL_N
,
712 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP
,
714 struct video_channel
*vc
= arg
;
717 t
->mode
= T_ANALOG_TV
;
718 if (vc
->norm
< ARRAY_SIZE(map
))
719 t
->std
= map
[vc
->norm
];
720 tda9887_fixup_std(t
);
721 tda9887_configure(t
);
726 v4l2_std_id
*id
= arg
;
729 t
->mode
= T_ANALOG_TV
;
731 tda9887_fixup_std(t
);
732 tda9887_configure(t
);
735 case VIDIOC_S_FREQUENCY
:
737 struct v4l2_frequency
*f
= arg
;
740 if (V4L2_TUNER_ANALOG_TV
== f
->type
) {
741 if (t
->mode
== T_ANALOG_TV
)
743 t
->mode
= T_ANALOG_TV
;
745 if (V4L2_TUNER_RADIO
== f
->type
) {
746 if (t
->mode
== T_RADIO
)
750 tda9887_configure(t
);
755 static int AFC_BITS_2_kHz
[] = {
756 -12500, -37500, -62500, -97500,
757 -112500, -137500, -162500, -187500,
758 187500, 162500, 137500, 112500,
759 97500 , 62500, 37500 , 12500
761 struct v4l2_tuner
* tuner
= arg
;
763 if (t
->mode
== T_RADIO
) {
766 if (1 == i2c_master_recv(&t
->client
,®
,1))
767 tuner
->afc
= AFC_BITS_2_kHz
[(reg
>>1)&0x0f];
773 struct v4l2_tuner
* tuner
= arg
;
775 if (t
->mode
== T_RADIO
) {
776 t
->radio_mode
= tuner
->audmode
;
777 tda9887_configure (t
);
788 static int tda9887_suspend(struct device
* dev
, pm_message_t state
)
790 dprintk("tda9887: suspend\n");
794 static int tda9887_resume(struct device
* dev
)
796 struct i2c_client
*c
= container_of(dev
, struct i2c_client
, dev
);
797 struct tda9887
*t
= i2c_get_clientdata(c
);
799 dprintk("tda9887: resume\n");
800 tda9887_configure(t
);
804 /* ----------------------------------------------------------------------- */
806 static struct i2c_driver driver
= {
807 .owner
= THIS_MODULE
,
808 .name
= "i2c tda9887 driver",
809 .id
= -1, /* FIXME */
810 .flags
= I2C_DF_NOTIFY
,
811 .attach_adapter
= tda9887_probe
,
812 .detach_client
= tda9887_detach
,
813 .command
= tda9887_command
,
815 .suspend
= tda9887_suspend
,
816 .resume
= tda9887_resume
,
819 static struct i2c_client client_template
=
822 .flags
= I2C_CLIENT_ALLOW_USE
,
826 static int __init
tda9887_init_module(void)
828 return i2c_add_driver(&driver
);
831 static void __exit
tda9887_cleanup_module(void)
833 i2c_del_driver(&driver
);
836 module_init(tda9887_init_module
);
837 module_exit(tda9887_cleanup_module
);
840 * Overrides for Emacs so that we follow Linus's tabbing style.
841 * ---------------------------------------------------------------------------