3 * i2c tv tuner chip device driver
4 * core core, i.e. kernel interfaces, registering and so on
7 #include <linux/module.h>
8 #include <linux/moduleparam.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/string.h>
12 #include <linux/timer.h>
13 #include <linux/delay.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/poll.h>
17 #include <linux/i2c.h>
18 #include <linux/types.h>
19 #include <linux/videodev.h>
20 #include <linux/init.h>
22 #include <media/tuner.h>
23 #include <media/audiochip.h>
29 /* standard i2c insmod options */
30 static unsigned short normal_i2c
[] = {
31 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
32 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
33 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
39 /* insmod options used at init time => read/only */
40 static unsigned int addr
= 0;
41 module_param(addr
, int, 0444);
43 static unsigned int no_autodetect
= 0;
44 module_param(no_autodetect
, int, 0444);
46 static unsigned int show_i2c
= 0;
47 module_param(show_i2c
, int, 0444);
49 /* insmod options used at runtime => read/write */
50 unsigned int tuner_debug
= 0;
51 module_param(tuner_debug
, int, 0644);
53 static unsigned int tv_range
[2] = { 44, 958 };
54 static unsigned int radio_range
[2] = { 65, 108 };
56 module_param_array(tv_range
, int, NULL
, 0644);
57 module_param_array(radio_range
, int, NULL
, 0644);
59 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
60 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
61 MODULE_LICENSE("GPL");
63 static struct i2c_driver driver
;
64 static struct i2c_client client_template
;
66 /* ---------------------------------------------------------------------- */
68 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
69 static void set_tv_freq(struct i2c_client
*c
, unsigned int freq
)
71 struct tuner
*t
= i2c_get_clientdata(c
);
73 if (t
->type
== UNSET
) {
74 tuner_warn ("tuner type not set\n");
77 if (NULL
== t
->tv_freq
) {
78 tuner_warn ("Tuner has no way to set tv freq\n");
81 if (freq
< tv_range
[0] * 16 || freq
> tv_range
[1] * 16) {
82 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
83 freq
/ 16, freq
% 16 * 100 / 16, tv_range
[0],
89 static void set_radio_freq(struct i2c_client
*c
, unsigned int freq
)
91 struct tuner
*t
= i2c_get_clientdata(c
);
93 if (t
->type
== UNSET
) {
94 tuner_warn ("tuner type not set\n");
97 if (NULL
== t
->radio_freq
) {
98 tuner_warn ("tuner has no way to set radio frequency\n");
101 if (freq
<= radio_range
[0] * 16000 || freq
>= radio_range
[1] * 16000) {
102 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
103 freq
/ 16000, freq
% 16000 * 100 / 16000,
104 radio_range
[0], radio_range
[1]);
107 t
->radio_freq(c
, freq
);
111 static void set_freq(struct i2c_client
*c
, unsigned long freq
)
113 struct tuner
*t
= i2c_get_clientdata(c
);
116 case V4L2_TUNER_RADIO
:
117 tuner_dbg("radio freq set to %lu.%02lu\n",
118 freq
/ 16000, freq
% 16000 * 100 / 16000);
119 set_radio_freq(c
, freq
);
121 case V4L2_TUNER_ANALOG_TV
:
122 case V4L2_TUNER_DIGITAL_TV
:
123 tuner_dbg("tv freq set to %lu.%02lu\n",
124 freq
/ 16, freq
% 16 * 100 / 16);
125 set_tv_freq(c
, freq
);
131 static void set_type(struct i2c_client
*c
, unsigned int type
,
132 unsigned int new_mode_mask
)
134 struct tuner
*t
= i2c_get_clientdata(c
);
135 unsigned char buffer
[4];
137 if (type
== UNSET
|| type
== TUNER_ABSENT
) {
138 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c
->addr
);
142 if (type
>= tuner_count
) {
143 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c
->addr
,tuner_count
);
147 /* This code detects calls by card attach_inform */
148 if (NULL
== t
->i2c
.dev
.driver
) {
149 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c
->addr
);
161 case TUNER_PHILIPS_TDA8290
:
165 if (tea5767_tuner_init(c
) == EINVAL
) {
166 t
->type
= TUNER_ABSENT
;
167 t
->mode_mask
= T_UNINITIALIZED
;
170 t
->mode_mask
= T_RADIO
;
172 case TUNER_PHILIPS_FMD1216ME_MK3
:
177 i2c_master_send(c
, buffer
, 4);
181 i2c_master_send(c
, buffer
, 4);
182 default_tuner_init(c
);
184 case TUNER_LG_TDVS_H062F
:
185 /* Set the Auxiliary Byte. */
189 i2c_master_send(c
, buffer
, 4);
190 default_tuner_init(c
);
192 case TUNER_PHILIPS_TD1316
:
197 i2c_master_send(c
,buffer
,4);
198 default_tuner_init(c
);
200 default_tuner_init(c
);
204 if (t
->mode_mask
== T_UNINITIALIZED
)
205 t
->mode_mask
= new_mode_mask
;
207 set_freq(c
, t
->freq
);
208 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
209 c
->adapter
->name
, c
->driver
->name
, c
->addr
<< 1, type
,
214 * This function apply tuner config to tuner specified
215 * by tun_setup structure. I addr is unset, then admin status
216 * and tun addr status is more precise then current status,
217 * it's applied. Otherwise status and type are applied only to
218 * tuner with exactly the same addr.
221 static void set_addr(struct i2c_client
*c
, struct tuner_setup
*tun_setup
)
223 struct tuner
*t
= i2c_get_clientdata(c
);
225 if ( t
->type
== UNSET
&& ((tun_setup
->addr
== ADDR_UNSET
&&
226 (t
->mode_mask
& tun_setup
->mode_mask
)) ||
227 tun_setup
->addr
== c
->addr
)) {
228 set_type(c
, tun_setup
->type
, tun_setup
->mode_mask
);
232 static inline int check_mode(struct tuner
*t
, char *cmd
)
234 if ((1 << t
->mode
& t
->mode_mask
) == 0) {
239 case V4L2_TUNER_RADIO
:
240 tuner_dbg("Cmd %s accepted for radio\n", cmd
);
242 case V4L2_TUNER_ANALOG_TV
:
243 tuner_dbg("Cmd %s accepted for analog TV\n", cmd
);
245 case V4L2_TUNER_DIGITAL_TV
:
246 tuner_dbg("Cmd %s accepted for digital TV\n", cmd
);
252 static char pal
[] = "-";
253 module_param_string(pal
, pal
, sizeof(pal
), 0644);
254 static char secam
[] = "-";
255 module_param_string(secam
, secam
, sizeof(secam
), 0644);
257 /* get more precise norm info from insmod option */
258 static int tuner_fixup_std(struct tuner
*t
)
260 if ((t
->std
& V4L2_STD_PAL
) == V4L2_STD_PAL
) {
266 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
267 t
->std
= V4L2_STD_PAL_BG
;
271 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
272 t
->std
= V4L2_STD_PAL_I
;
278 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
279 t
->std
= V4L2_STD_PAL_DK
;
283 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
284 t
->std
= V4L2_STD_PAL_M
;
288 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
289 t
->std
= V4L2_STD_PAL_N
;
292 /* default parameter, do nothing */
295 tuner_warn ("pal= argument not recognised\n");
299 if ((t
->std
& V4L2_STD_SECAM
) == V4L2_STD_SECAM
) {
305 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
306 t
->std
= V4L2_STD_SECAM_DK
;
310 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
311 t
->std
= V4L2_STD_SECAM_L
;
314 /* default parameter, do nothing */
317 tuner_warn ("secam= argument not recognised\n");
325 /* ---------------------------------------------------------------------- */
327 /* static var Used only in tuner_attach and tuner_probe */
328 static unsigned default_mode_mask
;
330 /* During client attach, set_type is called by adapter's attach_inform callback.
331 set_type must then be completed by tuner_attach.
333 static int tuner_attach(struct i2c_adapter
*adap
, int addr
, int kind
)
337 client_template
.adapter
= adap
;
338 client_template
.addr
= addr
;
340 t
= kmalloc(sizeof(struct tuner
), GFP_KERNEL
);
343 memset(t
, 0, sizeof(struct tuner
));
344 memcpy(&t
->i2c
, &client_template
, sizeof(struct i2c_client
));
345 i2c_set_clientdata(&t
->i2c
, t
);
347 t
->radio_if2
= 10700 * 1000; /* 10.7MHz - FM radio */
348 t
->audmode
= V4L2_TUNER_MODE_STEREO
;
349 t
->mode_mask
= T_UNINITIALIZED
;
352 unsigned char buffer
[16];
355 memset(buffer
, 0, sizeof(buffer
));
356 rc
= i2c_master_recv(&t
->i2c
, buffer
, sizeof(buffer
));
357 tuner_info("I2C RECV = ");
359 printk("%02x ",buffer
[i
]);
362 /* TEA5767 autodetection code - only for addr = 0xc0 */
363 if (!no_autodetect
) {
369 /* If chip is not tda8290, don't register.
370 since it can be tda9887*/
371 if (tda8290_probe(&t
->i2c
) != 0) {
372 tuner_dbg("chip at addr %x is not a tda8290\n", addr
);
378 if (tea5767_autodetection(&t
->i2c
) != EINVAL
) {
379 t
->type
= TUNER_TEA5767
;
380 t
->mode_mask
= T_RADIO
;
382 t
->freq
= 87.5 * 16; /* Sets freq to FM range */
383 default_mode_mask
&= ~T_RADIO
;
385 goto register_client
;
391 /* Initializes only the first adapter found */
392 if (default_mode_mask
!= T_UNINITIALIZED
) {
393 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask
);
394 t
->mode_mask
= default_mode_mask
;
395 t
->freq
= 400 * 16; /* Sets freq to VHF High */
396 default_mode_mask
= T_UNINITIALIZED
;
399 /* Should be just before return */
401 tuner_info("chip found @ 0x%x (%s)\n", addr
<< 1, adap
->name
);
402 i2c_attach_client (&t
->i2c
);
403 set_type (&t
->i2c
,t
->type
, t
->mode_mask
);
407 static int tuner_probe(struct i2c_adapter
*adap
)
410 normal_i2c
[0] = addr
;
411 normal_i2c
[1] = I2C_CLIENT_END
;
414 default_mode_mask
= T_RADIO
| T_ANALOG_TV
| T_DIGITAL_TV
;
416 if (adap
->class & I2C_CLASS_TV_ANALOG
)
417 return i2c_probe(adap
, &addr_data
, tuner_attach
);
421 static int tuner_detach(struct i2c_client
*client
)
423 struct tuner
*t
= i2c_get_clientdata(client
);
426 err
= i2c_detach_client(&t
->i2c
);
429 ("Client deregistration failed, client not detached.\n");
438 * Switch tuner to other mode. If tuner support both tv and radio,
439 * set another frequency to some value (This is needed for some pal
440 * tuners to avoid locking). Otherwise, just put second tuner in
444 static inline int set_mode(struct i2c_client
*client
, struct tuner
*t
, int mode
, char *cmd
)
451 if (check_mode(t
, cmd
) == EINVAL
) {
460 #define switch_v4l2() if (!t->using_v4l2) \
461 tuner_dbg("switching to v4l2\n"); \
464 static inline int check_v4l2(struct tuner
*t
)
467 tuner_dbg ("ignore v4l1 call\n");
473 static int tuner_command(struct i2c_client
*client
, unsigned int cmd
, void *arg
)
475 struct tuner
*t
= i2c_get_clientdata(client
);
476 unsigned int *iarg
= (int *)arg
;
479 /* --- configuration --- */
480 case TUNER_SET_TYPE_ADDR
:
481 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x\n",
482 ((struct tuner_setup
*)arg
)->type
,
483 ((struct tuner_setup
*)arg
)->addr
,
484 ((struct tuner_setup
*)arg
)->mode_mask
);
486 set_addr(client
, (struct tuner_setup
*)arg
);
489 set_mode(client
,t
,V4L2_TUNER_RADIO
, "AUDC_SET_RADIO");
491 case TUNER_SET_STANDBY
:
493 if (check_mode(t
, "TUNER_SET_STANDBY") == EINVAL
)
499 case AUDC_CONFIG_PINNACLE
:
502 tuner_dbg("pinnacle pal\n");
503 t
->radio_if2
= 33300 * 1000;
506 tuner_dbg("pinnacle ntsc\n");
507 t
->radio_if2
= 41300 * 1000;
512 if (check_mode(t
, "VIDIOCSAUDIO") == EINVAL
)
514 if (check_v4l2(t
) == EINVAL
)
517 /* Should be implemented, since bttv calls it */
518 tuner_dbg("VIDIOCSAUDIO not implemented.\n");
522 case TDA9887_SET_CONFIG
:
524 /* --- v4l ioctls --- */
525 /* take care: bttv does userspace copying, we'll get a
526 kernel pointer here... */
529 static const v4l2_std_id map
[] = {
530 [VIDEO_MODE_PAL
] = V4L2_STD_PAL
,
531 [VIDEO_MODE_NTSC
] = V4L2_STD_NTSC_M
,
532 [VIDEO_MODE_SECAM
] = V4L2_STD_SECAM
,
533 [4 /* bttv */ ] = V4L2_STD_PAL_M
,
534 [5 /* bttv */ ] = V4L2_STD_PAL_N
,
535 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP
,
537 struct video_channel
*vc
= arg
;
539 if (check_v4l2(t
) == EINVAL
)
542 if (set_mode(client
,t
,V4L2_TUNER_ANALOG_TV
, "VIDIOCSCHAN")==EINVAL
)
545 if (vc
->norm
< ARRAY_SIZE(map
))
546 t
->std
= map
[vc
->norm
];
549 set_tv_freq(client
, t
->freq
);
554 unsigned long *v
= arg
;
556 if (check_mode(t
, "VIDIOCSFREQ") == EINVAL
)
558 if (check_v4l2(t
) == EINVAL
)
561 set_freq(client
, *v
);
566 struct video_tuner
*vt
= arg
;
568 if (check_mode(t
, "VIDIOCGTUNER") == EINVAL
)
570 if (check_v4l2(t
) == EINVAL
)
573 if (V4L2_TUNER_RADIO
== t
->mode
) {
575 vt
->signal
= t
->has_signal(client
);
577 if (t
->is_stereo(client
))
579 VIDEO_TUNER_STEREO_ON
;
582 ~VIDEO_TUNER_STEREO_ON
;
584 vt
->flags
|= VIDEO_TUNER_LOW
; /* Allow freqs at 62.5 Hz */
586 vt
->rangelow
= radio_range
[0] * 16000;
587 vt
->rangehigh
= radio_range
[1] * 16000;
590 vt
->rangelow
= tv_range
[0] * 16;
591 vt
->rangehigh
= tv_range
[1] * 16;
598 struct video_audio
*va
= arg
;
600 if (check_mode(t
, "VIDIOCGAUDIO") == EINVAL
)
602 if (check_v4l2(t
) == EINVAL
)
605 if (V4L2_TUNER_RADIO
== t
->mode
&& t
->is_stereo
)
606 va
->mode
= t
->is_stereo(client
)
607 ? VIDEO_SOUND_STEREO
: VIDEO_SOUND_MONO
;
613 v4l2_std_id
*id
= arg
;
615 if (set_mode (client
, t
, V4L2_TUNER_ANALOG_TV
, "VIDIOC_S_STD")
624 set_freq(client
, t
->freq
);
627 case VIDIOC_S_FREQUENCY
:
629 struct v4l2_frequency
*f
= arg
;
631 t
->freq
= f
->frequency
;
633 if (V4L2_TUNER_RADIO
== f
->type
&&
634 V4L2_TUNER_RADIO
!= t
->mode
) {
635 if (set_mode (client
, t
, f
->type
, "VIDIOC_S_FREQUENCY")
639 set_freq(client
,t
->freq
);
643 case VIDIOC_G_FREQUENCY
:
645 struct v4l2_frequency
*f
= arg
;
647 if (check_mode(t
, "VIDIOC_G_FREQUENCY") == EINVAL
)
651 f
->frequency
= t
->freq
;
656 struct v4l2_tuner
*tuner
= arg
;
658 if (check_mode(t
, "VIDIOC_G_TUNER") == EINVAL
)
662 if (V4L2_TUNER_RADIO
== t
->mode
) {
665 tuner
->signal
= t
->has_signal(client
);
668 if (t
->is_stereo(client
)) {
670 V4L2_TUNER_SUB_STEREO
|
679 V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_STEREO
;
681 tuner
->audmode
= t
->audmode
;
683 tuner
->rangelow
= radio_range
[0] * 16000;
684 tuner
->rangehigh
= radio_range
[1] * 16000;
686 tuner
->rangelow
= tv_range
[0] * 16;
687 tuner
->rangehigh
= tv_range
[1] * 16;
693 struct v4l2_tuner
*tuner
= arg
;
695 if (check_mode(t
, "VIDIOC_S_TUNER") == EINVAL
)
700 if (V4L2_TUNER_RADIO
== t
->mode
) {
701 t
->audmode
= tuner
->audmode
;
702 set_radio_freq(client
, t
->freq
);
707 tuner_dbg("Unimplemented IOCTL 0x%08x(dir=%d,tp='%c',nr=%d,sz=%d)\n",
708 cmd
, _IOC_DIR(cmd
), _IOC_TYPE(cmd
),
709 _IOC_NR(cmd
), _IOC_SIZE(cmd
));
716 static int tuner_suspend(struct device
*dev
, pm_message_t state
)
718 struct i2c_client
*c
= container_of (dev
, struct i2c_client
, dev
);
719 struct tuner
*t
= i2c_get_clientdata (c
);
721 tuner_dbg ("suspend\n");
722 /* FIXME: power down ??? */
726 static int tuner_resume(struct device
*dev
)
728 struct i2c_client
*c
= container_of (dev
, struct i2c_client
, dev
);
729 struct tuner
*t
= i2c_get_clientdata (c
);
731 tuner_dbg ("resume\n");
733 set_freq(c
, t
->freq
);
737 /* ----------------------------------------------------------------------- */
739 static struct i2c_driver driver
= {
740 .owner
= THIS_MODULE
,
742 .id
= I2C_DRIVERID_TUNER
,
743 .flags
= I2C_DF_NOTIFY
,
744 .attach_adapter
= tuner_probe
,
745 .detach_client
= tuner_detach
,
746 .command
= tuner_command
,
748 .suspend
= tuner_suspend
,
749 .resume
= tuner_resume
,
752 static struct i2c_client client_template
= {
753 .name
= "(tuner unset)",
754 .flags
= I2C_CLIENT_ALLOW_USE
,
758 static int __init
tuner_init_module(void)
760 return i2c_add_driver(&driver
);
763 static void __exit
tuner_cleanup_module(void)
765 i2c_del_driver(&driver
);
768 module_init(tuner_init_module
);
769 module_exit(tuner_cleanup_module
);
772 * Overrides for Emacs so that we follow Linus's tabbing style.
773 * ---------------------------------------------------------------------------