2 * $Id: tuner-core.c,v 1.58 2005/07/14 03:06:43 mchehab Exp $
4 * i2c tv tuner chip device driver
5 * core core, i.e. kernel interfaces, registering and so on
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/string.h>
13 #include <linux/timer.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/poll.h>
18 #include <linux/i2c.h>
19 #include <linux/types.h>
20 #include <linux/videodev.h>
21 #include <linux/init.h>
23 #include <media/tuner.h>
24 #include <media/audiochip.h>
28 /* standard i2c insmod options */
29 static unsigned short normal_i2c
[] = {
31 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
32 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
38 /* insmod options used at init time => read/only */
39 static unsigned int addr
= 0;
40 module_param(addr
, int, 0444);
42 static unsigned int no_autodetect
= 0;
43 module_param(no_autodetect
, int, 0444);
45 /* insmod options used at runtime => read/write */
46 unsigned int tuner_debug
= 0;
47 module_param(tuner_debug
, int, 0644);
49 static unsigned int tv_range
[2] = { 44, 958 };
50 static unsigned int radio_range
[2] = { 65, 108 };
52 module_param_array(tv_range
, int, NULL
, 0644);
53 module_param_array(radio_range
, int, NULL
, 0644);
55 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
56 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
57 MODULE_LICENSE("GPL");
59 static struct i2c_driver driver
;
60 static struct i2c_client client_template
;
62 /* ---------------------------------------------------------------------- */
64 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
65 static void set_tv_freq(struct i2c_client
*c
, unsigned int freq
)
67 struct tuner
*t
= i2c_get_clientdata(c
);
69 if (t
->type
== UNSET
) {
70 tuner_warn ("tuner type not set\n");
73 if (NULL
== t
->tv_freq
) {
74 tuner_warn ("Tuner has no way to set tv freq\n");
77 if (freq
< tv_range
[0] * 16 || freq
> tv_range
[1] * 16) {
78 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
79 freq
/ 16, freq
% 16 * 100 / 16, tv_range
[0],
85 static void set_radio_freq(struct i2c_client
*c
, unsigned int freq
)
87 struct tuner
*t
= i2c_get_clientdata(c
);
89 if (t
->type
== UNSET
) {
90 tuner_warn ("tuner type not set\n");
93 if (NULL
== t
->radio_freq
) {
94 tuner_warn ("tuner has no way to set radio frequency\n");
97 if (freq
<= radio_range
[0] * 16000 || freq
>= radio_range
[1] * 16000) {
98 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
99 freq
/ 16000, freq
% 16000 * 100 / 16000,
100 radio_range
[0], radio_range
[1]);
103 t
->radio_freq(c
, freq
);
107 static void set_freq(struct i2c_client
*c
, unsigned long freq
)
109 struct tuner
*t
= i2c_get_clientdata(c
);
112 case V4L2_TUNER_RADIO
:
113 tuner_dbg("radio freq set to %lu.%02lu\n",
114 freq
/ 16000, freq
% 16000 * 100 / 16000);
115 set_radio_freq(c
, freq
);
117 case V4L2_TUNER_ANALOG_TV
:
118 case V4L2_TUNER_DIGITAL_TV
:
119 tuner_dbg("tv freq set to %lu.%02lu\n",
120 freq
/ 16, freq
% 16 * 100 / 16);
121 set_tv_freq(c
, freq
);
127 static void set_type(struct i2c_client
*c
, unsigned int type
,
128 unsigned int new_mode_mask
)
130 struct tuner
*t
= i2c_get_clientdata(c
);
131 unsigned char buffer
[4];
133 if (type
== UNSET
|| type
== TUNER_ABSENT
) {
134 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c
->addr
);
138 if (type
>= tuner_count
) {
139 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c
->addr
,tuner_count
);
143 /* This code detects calls by card attach_inform */
144 if (NULL
== t
->i2c
.dev
.driver
) {
145 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c
->addr
);
157 case TUNER_PHILIPS_TDA8290
:
161 if (tea5767_tuner_init(c
) == EINVAL
) {
162 t
->type
= TUNER_ABSENT
;
163 t
->mode_mask
= T_UNINITIALIZED
;
166 t
->mode_mask
= T_RADIO
;
168 case TUNER_PHILIPS_FMD1216ME_MK3
:
173 i2c_master_send(c
, buffer
, 4);
177 i2c_master_send(c
, buffer
, 4);
178 default_tuner_init(c
);
181 default_tuner_init(c
);
185 if (t
->mode_mask
== T_UNINITIALIZED
)
186 t
->mode_mask
= new_mode_mask
;
188 set_freq(c
, t
->freq
);
189 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
190 c
->adapter
->name
, c
->driver
->name
, c
->addr
<< 1, type
,
195 * This function apply tuner config to tuner specified
196 * by tun_setup structure. I addr is unset, then admin status
197 * and tun addr status is more precise then current status,
198 * it's applied. Otherwise status and type are applied only to
199 * tuner with exactly the same addr.
202 static void set_addr(struct i2c_client
*c
, struct tuner_setup
*tun_setup
)
204 struct tuner
*t
= i2c_get_clientdata(c
);
206 if (tun_setup
->addr
== ADDR_UNSET
) {
207 if (t
->mode_mask
& tun_setup
->mode_mask
)
208 set_type(c
, tun_setup
->type
, tun_setup
->mode_mask
);
209 } else if (tun_setup
->addr
== c
->addr
) {
210 set_type(c
, tun_setup
->type
, tun_setup
->mode_mask
);
214 static inline int check_mode(struct tuner
*t
, char *cmd
)
216 if (1 << t
->mode
& t
->mode_mask
) {
218 case V4L2_TUNER_RADIO
:
219 tuner_dbg("Cmd %s accepted for radio\n", cmd
);
221 case V4L2_TUNER_ANALOG_TV
:
222 tuner_dbg("Cmd %s accepted for analog TV\n", cmd
);
224 case V4L2_TUNER_DIGITAL_TV
:
225 tuner_dbg("Cmd %s accepted for digital TV\n", cmd
);
233 static char pal
[] = "-";
234 module_param_string(pal
, pal
, sizeof(pal
), 0644);
235 static char secam
[] = "-";
236 module_param_string(secam
, secam
, sizeof(secam
), 0644);
238 /* get more precise norm info from insmod option */
239 static int tuner_fixup_std(struct tuner
*t
)
241 if ((t
->std
& V4L2_STD_PAL
) == V4L2_STD_PAL
) {
247 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
248 t
->std
= V4L2_STD_PAL_BG
;
252 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
253 t
->std
= V4L2_STD_PAL_I
;
259 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
260 t
->std
= V4L2_STD_PAL_DK
;
264 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
265 t
->std
= V4L2_STD_PAL_M
;
269 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
270 t
->std
= V4L2_STD_PAL_N
;
274 if ((t
->std
& V4L2_STD_SECAM
) == V4L2_STD_SECAM
) {
280 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
281 t
->std
= V4L2_STD_SECAM_DK
;
285 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
286 t
->std
= V4L2_STD_SECAM_L
;
294 /* ---------------------------------------------------------------------- */
296 /* static var Used only in tuner_attach and tuner_probe */
297 static unsigned default_mode_mask
;
299 /* During client attach, set_type is called by adapter's attach_inform callback.
300 set_type must then be completed by tuner_attach.
302 static int tuner_attach(struct i2c_adapter
*adap
, int addr
, int kind
)
306 client_template
.adapter
= adap
;
307 client_template
.addr
= addr
;
309 t
= kmalloc(sizeof(struct tuner
), GFP_KERNEL
);
312 memset(t
, 0, sizeof(struct tuner
));
313 memcpy(&t
->i2c
, &client_template
, sizeof(struct i2c_client
));
314 i2c_set_clientdata(&t
->i2c
, t
);
316 t
->radio_if2
= 10700 * 1000; /* 10.7MHz - FM radio */
317 t
->audmode
= V4L2_TUNER_MODE_STEREO
;
318 t
->mode_mask
= T_UNINITIALIZED
;
321 tuner_info("chip found @ 0x%x (%s)\n", addr
<< 1, adap
->name
);
323 /* TEA5767 autodetection code - only for addr = 0xc0 */
324 if (!no_autodetect
) {
326 if (tea5767_autodetection(&t
->i2c
) != EINVAL
) {
327 t
->type
= TUNER_TEA5767
;
328 t
->mode_mask
= T_RADIO
;
330 t
->freq
= 87.5 * 16; /* Sets freq to FM range */
331 default_mode_mask
&= ~T_RADIO
;
333 i2c_attach_client (&t
->i2c
);
334 set_type(&t
->i2c
,t
->type
, t
->mode_mask
);
340 /* Initializes only the first adapter found */
341 if (default_mode_mask
!= T_UNINITIALIZED
) {
342 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask
);
343 t
->mode_mask
= default_mode_mask
;
344 t
->freq
= 400 * 16; /* Sets freq to VHF High */
345 default_mode_mask
= T_UNINITIALIZED
;
348 /* Should be just before return */
349 i2c_attach_client (&t
->i2c
);
350 set_type (&t
->i2c
,t
->type
, t
->mode_mask
);
354 static int tuner_probe(struct i2c_adapter
*adap
)
357 normal_i2c
[0] = addr
;
358 normal_i2c
[1] = I2C_CLIENT_END
;
361 default_mode_mask
= T_RADIO
| T_ANALOG_TV
| T_DIGITAL_TV
;
363 if (adap
->class & I2C_CLASS_TV_ANALOG
)
364 return i2c_probe(adap
, &addr_data
, tuner_attach
);
368 static int tuner_detach(struct i2c_client
*client
)
370 struct tuner
*t
= i2c_get_clientdata(client
);
373 err
= i2c_detach_client(&t
->i2c
);
376 ("Client deregistration failed, client not detached.\n");
385 * Switch tuner to other mode. If tuner support both tv and radio,
386 * set another frequency to some value (This is needed for some pal
387 * tuners to avoid locking). Otherwise, just put second tuner in
391 static inline int set_mode(struct i2c_client
*client
, struct tuner
*t
, int mode
, char *cmd
)
393 if (mode
!= t
->mode
) {
396 if (check_mode(t
, cmd
) == EINVAL
) {
398 if (V4L2_TUNER_RADIO
== mode
) {
399 set_tv_freq(client
, 400 * 16);
401 set_radio_freq(client
, 87.5 * 16000);
409 #define switch_v4l2() if (!t->using_v4l2) \
410 tuner_dbg("switching to v4l2\n"); \
413 static inline int check_v4l2(struct tuner
*t
)
416 tuner_dbg ("ignore v4l1 call\n");
422 static int tuner_command(struct i2c_client
*client
, unsigned int cmd
, void *arg
)
424 struct tuner
*t
= i2c_get_clientdata(client
);
425 unsigned int *iarg
= (int *)arg
;
428 /* --- configuration --- */
429 case TUNER_SET_TYPE_ADDR
:
430 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x\n",
431 ((struct tuner_setup
*)arg
)->type
,
432 ((struct tuner_setup
*)arg
)->addr
,
433 ((struct tuner_setup
*)arg
)->mode_mask
);
435 set_addr(client
, (struct tuner_setup
*)arg
);
438 set_mode(client
,t
,V4L2_TUNER_RADIO
, "AUDC_SET_RADIO");
440 case AUDC_CONFIG_PINNACLE
:
441 if (check_mode(t
, "AUDC_CONFIG_PINNACLE") == EINVAL
)
445 tuner_dbg("pinnacle pal\n");
446 t
->radio_if2
= 33300 * 1000;
449 tuner_dbg("pinnacle ntsc\n");
450 t
->radio_if2
= 41300 * 1000;
454 case TDA9887_SET_CONFIG
:
456 /* --- v4l ioctls --- */
457 /* take care: bttv does userspace copying, we'll get a
458 kernel pointer here... */
461 static const v4l2_std_id map
[] = {
462 [VIDEO_MODE_PAL
] = V4L2_STD_PAL
,
463 [VIDEO_MODE_NTSC
] = V4L2_STD_NTSC_M
,
464 [VIDEO_MODE_SECAM
] = V4L2_STD_SECAM
,
465 [4 /* bttv */ ] = V4L2_STD_PAL_M
,
466 [5 /* bttv */ ] = V4L2_STD_PAL_N
,
467 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP
,
469 struct video_channel
*vc
= arg
;
471 if (check_v4l2(t
) == EINVAL
)
474 if (set_mode(client
,t
,V4L2_TUNER_ANALOG_TV
, "VIDIOCSCHAN")==EINVAL
)
477 if (vc
->norm
< ARRAY_SIZE(map
))
478 t
->std
= map
[vc
->norm
];
481 set_tv_freq(client
, t
->freq
);
486 unsigned long *v
= arg
;
488 if (check_mode(t
, "VIDIOCSFREQ") == EINVAL
)
490 if (check_v4l2(t
) == EINVAL
)
493 set_freq(client
, *v
);
498 struct video_tuner
*vt
= arg
;
500 if (check_mode(t
, "VIDIOCGTUNER") == EINVAL
)
502 if (check_v4l2(t
) == EINVAL
)
505 if (V4L2_TUNER_RADIO
== t
->mode
) {
507 vt
->signal
= t
->has_signal(client
);
509 if (t
->is_stereo(client
))
511 VIDEO_TUNER_STEREO_ON
;
514 ~VIDEO_TUNER_STEREO_ON
;
516 vt
->flags
|= VIDEO_TUNER_LOW
; /* Allow freqs at 62.5 Hz */
518 vt
->rangelow
= radio_range
[0] * 16000;
519 vt
->rangehigh
= radio_range
[1] * 16000;
522 vt
->rangelow
= tv_range
[0] * 16;
523 vt
->rangehigh
= tv_range
[1] * 16;
530 struct video_audio
*va
= arg
;
532 if (check_mode(t
, "VIDIOCGAUDIO") == EINVAL
)
534 if (check_v4l2(t
) == EINVAL
)
537 if (V4L2_TUNER_RADIO
== t
->mode
&& t
->is_stereo
)
538 va
->mode
= t
->is_stereo(client
)
539 ? VIDEO_SOUND_STEREO
: VIDEO_SOUND_MONO
;
545 v4l2_std_id
*id
= arg
;
547 if (set_mode (client
, t
, V4L2_TUNER_ANALOG_TV
, "VIDIOC_S_STD")
556 set_freq(client
, t
->freq
);
559 case VIDIOC_S_FREQUENCY
:
561 struct v4l2_frequency
*f
= arg
;
563 t
->freq
= f
->frequency
;
565 if (V4L2_TUNER_RADIO
== f
->type
&&
566 V4L2_TUNER_RADIO
!= t
->mode
) {
567 if (set_mode (client
, t
, f
->type
, "VIDIOC_S_FREQUENCY")
571 set_freq(client
,t
->freq
);
575 case VIDIOC_G_FREQUENCY
:
577 struct v4l2_frequency
*f
= arg
;
579 if (check_mode(t
, "VIDIOC_G_FREQUENCY") == EINVAL
)
583 f
->frequency
= t
->freq
;
588 struct v4l2_tuner
*tuner
= arg
;
590 if (check_mode(t
, "VIDIOC_G_TUNER") == EINVAL
)
594 if (V4L2_TUNER_RADIO
== t
->mode
) {
597 tuner
->signal
= t
->has_signal(client
);
600 if (t
->is_stereo(client
)) {
602 V4L2_TUNER_SUB_STEREO
|
611 V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_STEREO
;
613 tuner
->audmode
= t
->audmode
;
615 tuner
->rangelow
= radio_range
[0] * 16000;
616 tuner
->rangehigh
= radio_range
[1] * 16000;
618 tuner
->rangelow
= tv_range
[0] * 16;
619 tuner
->rangehigh
= tv_range
[1] * 16;
625 struct v4l2_tuner
*tuner
= arg
;
627 if (check_mode(t
, "VIDIOC_S_TUNER") == EINVAL
)
632 if (V4L2_TUNER_RADIO
== t
->mode
) {
633 t
->audmode
= tuner
->audmode
;
634 set_radio_freq(client
, t
->freq
);
639 tuner_dbg("Unimplemented IOCTL 0x%08x(dir=%d,tp=0x%02x,nr=%d,sz=%d)\n",
640 cmd
, _IOC_DIR(cmd
), _IOC_TYPE(cmd
),
641 _IOC_NR(cmd
), _IOC_SIZE(cmd
));
648 static int tuner_suspend(struct device
*dev
, u32 state
, u32 level
)
650 struct i2c_client
*c
= container_of (dev
, struct i2c_client
, dev
);
651 struct tuner
*t
= i2c_get_clientdata (c
);
653 tuner_dbg ("suspend\n");
654 /* FIXME: power down ??? */
658 static int tuner_resume(struct device
*dev
, u32 level
)
660 struct i2c_client
*c
= container_of (dev
, struct i2c_client
, dev
);
661 struct tuner
*t
= i2c_get_clientdata (c
);
663 tuner_dbg ("resume\n");
665 set_freq(c
, t
->freq
);
669 /* ----------------------------------------------------------------------- */
671 static struct i2c_driver driver
= {
672 .owner
= THIS_MODULE
,
674 .id
= I2C_DRIVERID_TUNER
,
675 .flags
= I2C_DF_NOTIFY
,
676 .attach_adapter
= tuner_probe
,
677 .detach_client
= tuner_detach
,
678 .command
= tuner_command
,
680 .suspend
= tuner_suspend
,
681 .resume
= tuner_resume
,
684 static struct i2c_client client_template
= {
685 I2C_DEVNAME("(tuner unset)"),
686 .flags
= I2C_CLIENT_ALLOW_USE
,
690 static int __init
tuner_init_module(void)
692 return i2c_add_driver(&driver
);
695 static void __exit
tuner_cleanup_module(void)
697 i2c_del_driver(&driver
);
700 module_init(tuner_init_module
);
701 module_exit(tuner_cleanup_module
);
704 * Overrides for Emacs so that we follow Linus's tabbing style.
705 * ---------------------------------------------------------------------------