2 * $Id: tuner-core.c,v 1.55 2005/07/08 13:20:33 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 /* insmod options used at runtime => read/write */
43 unsigned int tuner_debug = 0;
44 module_param(tuner_debug, int, 0644);
46 static unsigned int tv_range[2] = { 44, 958 };
47 static unsigned int radio_range[2] = { 65, 108 };
49 module_param_array(tv_range, int, NULL, 0644);
50 module_param_array(radio_range, int, NULL, 0644);
52 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
53 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
54 MODULE_LICENSE("GPL");
56 static struct i2c_driver driver;
57 static struct i2c_client client_template;
59 /* ---------------------------------------------------------------------- */
61 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
62 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
64 struct tuner *t = i2c_get_clientdata(c);
66 if (t->type == UNSET) {
67 tuner_warn ("tuner type not set\n");
70 if (NULL == t->tv_freq) {
71 tuner_warn ("Tuner has no way to set tv freq\n");
74 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
75 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
76 freq / 16, freq % 16 * 100 / 16, tv_range[0],
82 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
84 struct tuner *t = i2c_get_clientdata(c);
86 if (t->type == UNSET) {
87 tuner_warn ("tuner type not set\n");
90 if (NULL == t->radio_freq) {
91 tuner_warn ("tuner has no way to set radio frequency\n");
94 if (freq <= radio_range[0] * 16000 || freq >= radio_range[1] * 16000) {
95 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
96 freq / 16000, freq % 16000 * 100 / 16000,
97 radio_range[0], radio_range[1]);
100 t->radio_freq(c, freq);
104 static void set_freq(struct i2c_client *c, unsigned long freq)
106 struct tuner *t = i2c_get_clientdata(c);
109 case V4L2_TUNER_RADIO:
110 tuner_dbg("radio freq set to %lu.%02lu\n",
111 freq / 16000, freq % 16000 * 100 / 16000);
112 set_radio_freq(c, freq);
114 case V4L2_TUNER_ANALOG_TV:
115 case V4L2_TUNER_DIGITAL_TV:
116 tuner_dbg("tv freq set to %lu.%02lu\n",
117 freq / 16, freq % 16 * 100 / 16);
118 set_tv_freq(c, freq);
124 static void set_type(struct i2c_client *c, unsigned int type,
125 unsigned int new_mode_mask)
127 struct tuner *t = i2c_get_clientdata(c);
128 unsigned char buffer[4];
130 if (type == UNSET || type == TUNER_ABSENT) {
131 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
135 if (type >= tuner_count) {
136 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
140 /* This code detects calls by card attach_inform */
141 if (NULL == t->i2c.dev.driver) {
142 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
154 case TUNER_PHILIPS_TDA8290:
158 if (tea5767_tuner_init(c) == EINVAL) {
159 t->type = TUNER_ABSENT;
160 t->mode_mask = T_UNINITIALIZED;
163 t->mode_mask = T_RADIO;
165 case TUNER_PHILIPS_FMD1216ME_MK3:
170 i2c_master_send(c, buffer, 4);
174 i2c_master_send(c, buffer, 4);
175 default_tuner_init(c);
178 default_tuner_init(c);
182 if (t->mode_mask == T_UNINITIALIZED)
183 t->mode_mask = new_mode_mask;
185 set_freq(c, t->freq);
186 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
187 c->adapter->name, c->driver->name, c->addr << 1, type,
192 * This function apply tuner config to tuner specified
193 * by tun_setup structure. I addr is unset, then admin status
194 * and tun addr status is more precise then current status,
195 * it's applied. Otherwise status and type are applied only to
196 * tuner with exactly the same addr.
199 static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
201 struct tuner *t = i2c_get_clientdata(c);
203 if (tun_setup->addr == ADDR_UNSET) {
204 if (t->mode_mask & tun_setup->mode_mask)
205 set_type(c, tun_setup->type, tun_setup->mode_mask);
206 } else if (tun_setup->addr == c->addr) {
207 set_type(c, tun_setup->type, tun_setup->mode_mask);
211 static inline int check_mode(struct tuner *t, char *cmd)
213 if (1 << t->mode & t->mode_mask) {
215 case V4L2_TUNER_RADIO:
216 tuner_dbg("Cmd %s accepted for radio\n", cmd);
218 case V4L2_TUNER_ANALOG_TV:
219 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
221 case V4L2_TUNER_DIGITAL_TV:
222 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
230 static char pal[] = "-";
231 module_param_string(pal, pal, sizeof(pal), 0644);
232 static char secam[] = "-";
233 module_param_string(secam, secam, sizeof(secam), 0644);
235 /* get more precise norm info from insmod option */
236 static int tuner_fixup_std(struct tuner *t)
238 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
244 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
245 t->std = V4L2_STD_PAL_BG;
249 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
250 t->std = V4L2_STD_PAL_I;
256 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
257 t->std = V4L2_STD_PAL_DK;
261 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
262 t->std = V4L2_STD_PAL_M;
266 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
267 t->std = V4L2_STD_PAL_N;
271 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
277 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
278 t->std = V4L2_STD_SECAM_DK;
282 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
283 t->std = V4L2_STD_SECAM_L;
291 /* ---------------------------------------------------------------------- */
293 /* static var Used only in tuner_attach and tuner_probe */
294 static unsigned default_mode_mask;
296 /* During client attach, set_type is called by adapter's attach_inform callback.
297 set_type must then be completed by tuner_attach.
299 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
303 client_template.adapter = adap;
304 client_template.addr = addr;
306 t = kmalloc(sizeof(struct tuner), GFP_KERNEL);
309 memset(t, 0, sizeof(struct tuner));
310 memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
311 i2c_set_clientdata(&t->i2c, t);
313 t->radio_if2 = 10700 * 1000; /* 10.7MHz - FM radio */
314 t->audmode = V4L2_TUNER_MODE_STEREO;
315 t->mode_mask = T_UNINITIALIZED;
318 tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
320 /* TEA5767 autodetection code - only for addr = 0xc0 */
322 if (tea5767_autodetection(&t->i2c) != EINVAL) {
323 t->type = TUNER_TEA5767;
324 t->mode_mask = T_RADIO;
326 t->freq = 87.5 * 16; /* Sets freq to FM range */
327 default_mode_mask &= ~T_RADIO;
329 i2c_attach_client (&t->i2c);
330 set_type(&t->i2c,t->type, t->mode_mask);
335 /* Initializes only the first adapter found */
336 if (default_mode_mask != T_UNINITIALIZED) {
337 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
338 t->mode_mask = default_mode_mask;
339 t->freq = 400 * 16; /* Sets freq to VHF High */
340 default_mode_mask = T_UNINITIALIZED;
343 /* Should be just before return */
344 i2c_attach_client (&t->i2c);
345 set_type (&t->i2c,t->type, t->mode_mask);
349 static int tuner_probe(struct i2c_adapter *adap)
352 normal_i2c[0] = addr;
353 normal_i2c[1] = I2C_CLIENT_END;
356 default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
358 if (adap->class & I2C_CLASS_TV_ANALOG)
359 return i2c_probe(adap, &addr_data, tuner_attach);
363 static int tuner_detach(struct i2c_client *client)
365 struct tuner *t = i2c_get_clientdata(client);
368 err = i2c_detach_client(&t->i2c);
371 ("Client deregistration failed, client not detached.\n");
380 * Switch tuner to other mode. If tuner support both tv and radio,
381 * set another frequency to some value (This is needed for some pal
382 * tuners to avoid locking). Otherwise, just put second tuner in
386 static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
388 if (mode != t->mode) {
391 if (check_mode(t, cmd) == EINVAL) {
393 if (V4L2_TUNER_RADIO == mode) {
394 set_tv_freq(client, 400 * 16);
396 set_radio_freq(client, 87.5 * 16000);
404 #define switch_v4l2() if (!t->using_v4l2) \
405 tuner_dbg("switching to v4l2\n"); \
408 static inline int check_v4l2(struct tuner *t)
411 tuner_dbg ("ignore v4l1 call\n");
417 static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
419 struct tuner *t = i2c_get_clientdata(client);
420 unsigned int *iarg = (int *)arg;
423 /* --- configuration --- */
424 case TUNER_SET_TYPE_ADDR:
425 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x\n",
426 ((struct tuner_setup *)arg)->type,
427 ((struct tuner_setup *)arg)->addr,
428 ((struct tuner_setup *)arg)->mode_mask);
430 set_addr(client, (struct tuner_setup *)arg);
433 set_mode(client,t,V4L2_TUNER_RADIO, "AUDC_SET_RADIO");
435 case AUDC_CONFIG_PINNACLE:
436 if (check_mode(t, "AUDC_CONFIG_PINNACLE") == EINVAL)
440 tuner_dbg("pinnacle pal\n");
441 t->radio_if2 = 33300 * 1000;
444 tuner_dbg("pinnacle ntsc\n");
445 t->radio_if2 = 41300 * 1000;
449 case TDA9887_SET_CONFIG:
451 /* --- v4l ioctls --- */
452 /* take care: bttv does userspace copying, we'll get a
453 kernel pointer here... */
456 static const v4l2_std_id map[] = {
457 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
458 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
459 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
460 [4 /* bttv */ ] = V4L2_STD_PAL_M,
461 [5 /* bttv */ ] = V4L2_STD_PAL_N,
462 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
464 struct video_channel *vc = arg;
466 if (check_v4l2(t) == EINVAL)
469 if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
472 if (vc->norm < ARRAY_SIZE(map))
473 t->std = map[vc->norm];
476 set_tv_freq(client, t->freq);
481 unsigned long *v = arg;
483 if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
485 if (check_v4l2(t) == EINVAL)
488 set_freq(client, *v);
493 struct video_tuner *vt = arg;
495 if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
497 if (check_v4l2(t) == EINVAL)
500 if (V4L2_TUNER_RADIO == t->mode) {
502 vt->signal = t->has_signal(client);
504 if (t->is_stereo(client))
506 VIDEO_TUNER_STEREO_ON;
509 ~VIDEO_TUNER_STEREO_ON;
511 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
513 vt->rangelow = radio_range[0] * 16000;
514 vt->rangehigh = radio_range[1] * 16000;
517 vt->rangelow = tv_range[0] * 16;
518 vt->rangehigh = tv_range[1] * 16;
525 struct video_audio *va = arg;
527 if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
529 if (check_v4l2(t) == EINVAL)
532 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
533 va->mode = t->is_stereo(client)
534 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
540 v4l2_std_id *id = arg;
542 if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
551 set_freq(client, t->freq);
554 case VIDIOC_S_FREQUENCY:
556 struct v4l2_frequency *f = arg;
558 t->freq = f->frequency;
560 if (V4L2_TUNER_RADIO == f->type &&
561 V4L2_TUNER_RADIO != t->mode) {
562 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
566 set_freq(client,t->freq);
570 case VIDIOC_G_FREQUENCY:
572 struct v4l2_frequency *f = arg;
574 if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
578 f->frequency = t->freq;
583 struct v4l2_tuner *tuner = arg;
585 if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
589 if (V4L2_TUNER_RADIO == t->mode) {
592 tuner->signal = t->has_signal(client);
595 if (t->is_stereo(client)) {
597 V4L2_TUNER_SUB_STEREO |
606 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
608 tuner->audmode = t->audmode;
610 tuner->rangelow = radio_range[0] * 16000;
611 tuner->rangehigh = radio_range[1] * 16000;
613 tuner->rangelow = tv_range[0] * 16;
614 tuner->rangehigh = tv_range[1] * 16;
620 struct v4l2_tuner *tuner = arg;
622 if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
627 if (V4L2_TUNER_RADIO == t->mode) {
628 t->audmode = tuner->audmode;
629 set_radio_freq(client, t->freq);
634 tuner_dbg("Unimplemented IOCTL 0x%08x called to tuner.\n", cmd);
641 static int tuner_suspend(struct device *dev, u32 state, u32 level)
643 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
644 struct tuner *t = i2c_get_clientdata (c);
646 tuner_dbg ("suspend\n");
647 /* FIXME: power down ??? */
651 static int tuner_resume(struct device *dev, u32 level)
653 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
654 struct tuner *t = i2c_get_clientdata (c);
656 tuner_dbg ("resume\n");
658 set_freq(c, t->freq);
662 /* ----------------------------------------------------------------------- */
664 static struct i2c_driver driver = {
665 .owner = THIS_MODULE,
667 .id = I2C_DRIVERID_TUNER,
668 .flags = I2C_DF_NOTIFY,
669 .attach_adapter = tuner_probe,
670 .detach_client = tuner_detach,
671 .command = tuner_command,
673 .suspend = tuner_suspend,
674 .resume = tuner_resume,
677 static struct i2c_client client_template = {
678 I2C_DEVNAME("(tuner unset)"),
679 .flags = I2C_CLIENT_ALLOW_USE,
683 static int __init tuner_init_module(void)
685 return i2c_add_driver(&driver);
688 static void __exit tuner_cleanup_module(void)
690 i2c_del_driver(&driver);
693 module_init(tuner_init_module);
694 module_exit(tuner_cleanup_module);
697 * Overrides for Emacs so that we follow Linus's tabbing style.
698 * ---------------------------------------------------------------------------