2 * $Id: tuner-core.c,v 1.63 2005/07/28 18:19:55 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>
30 /* standard i2c insmod options */
31 static unsigned short normal_i2c[] = {
33 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
34 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
40 /* insmod options used at init time => read/only */
41 static unsigned int addr = 0;
42 module_param(addr, int, 0444);
44 static unsigned int no_autodetect = 0;
45 module_param(no_autodetect, int, 0444);
47 static unsigned int show_i2c = 0;
48 module_param(show_i2c, int, 0444);
50 /* insmod options used at runtime => read/write */
51 unsigned int tuner_debug = 0;
52 module_param(tuner_debug, int, 0644);
54 static unsigned int tv_range[2] = { 44, 958 };
55 static unsigned int radio_range[2] = { 65, 108 };
57 module_param_array(tv_range, int, NULL, 0644);
58 module_param_array(radio_range, int, NULL, 0644);
60 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
61 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
62 MODULE_LICENSE("GPL");
64 static struct i2c_driver driver;
65 static struct i2c_client client_template;
67 /* ---------------------------------------------------------------------- */
69 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
70 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
72 struct tuner *t = i2c_get_clientdata(c);
74 if (t->type == UNSET) {
75 tuner_warn ("tuner type not set\n");
78 if (NULL == t->tv_freq) {
79 tuner_warn ("Tuner has no way to set tv freq\n");
82 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
83 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
84 freq / 16, freq % 16 * 100 / 16, tv_range[0],
90 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
92 struct tuner *t = i2c_get_clientdata(c);
94 if (t->type == UNSET) {
95 tuner_warn ("tuner type not set\n");
98 if (NULL == t->radio_freq) {
99 tuner_warn ("tuner has no way to set radio frequency\n");
102 if (freq <= radio_range[0] * 16000 || freq >= radio_range[1] * 16000) {
103 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
104 freq / 16000, freq % 16000 * 100 / 16000,
105 radio_range[0], radio_range[1]);
108 t->radio_freq(c, freq);
112 static void set_freq(struct i2c_client *c, unsigned long freq)
114 struct tuner *t = i2c_get_clientdata(c);
117 case V4L2_TUNER_RADIO:
118 tuner_dbg("radio freq set to %lu.%02lu\n",
119 freq / 16000, freq % 16000 * 100 / 16000);
120 set_radio_freq(c, freq);
122 case V4L2_TUNER_ANALOG_TV:
123 case V4L2_TUNER_DIGITAL_TV:
124 tuner_dbg("tv freq set to %lu.%02lu\n",
125 freq / 16, freq % 16 * 100 / 16);
126 set_tv_freq(c, freq);
132 static void set_type(struct i2c_client *c, unsigned int type,
133 unsigned int new_mode_mask)
135 struct tuner *t = i2c_get_clientdata(c);
136 unsigned char buffer[4];
138 if (type == UNSET || type == TUNER_ABSENT) {
139 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
143 if (type >= tuner_count) {
144 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
148 /* This code detects calls by card attach_inform */
149 if (NULL == t->i2c.dev.driver) {
150 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
162 case TUNER_PHILIPS_TDA8290:
166 if (tea5767_tuner_init(c) == EINVAL) {
167 t->type = TUNER_ABSENT;
168 t->mode_mask = T_UNINITIALIZED;
171 t->mode_mask = T_RADIO;
173 case TUNER_PHILIPS_FMD1216ME_MK3:
178 i2c_master_send(c, buffer, 4);
182 i2c_master_send(c, buffer, 4);
183 default_tuner_init(c);
186 default_tuner_init(c);
190 if (t->mode_mask == T_UNINITIALIZED)
191 t->mode_mask = new_mode_mask;
193 set_freq(c, t->freq);
194 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
195 c->adapter->name, c->driver->name, c->addr << 1, type,
200 * This function apply tuner config to tuner specified
201 * by tun_setup structure. I addr is unset, then admin status
202 * and tun addr status is more precise then current status,
203 * it's applied. Otherwise status and type are applied only to
204 * tuner with exactly the same addr.
207 static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
209 struct tuner *t = i2c_get_clientdata(c);
211 if (tun_setup->addr == ADDR_UNSET) {
212 if (t->mode_mask & tun_setup->mode_mask)
213 set_type(c, tun_setup->type, tun_setup->mode_mask);
214 } else if (tun_setup->addr == c->addr) {
215 set_type(c, tun_setup->type, tun_setup->mode_mask);
219 static inline int check_mode(struct tuner *t, char *cmd)
221 if (1 << t->mode & t->mode_mask) {
223 case V4L2_TUNER_RADIO:
224 tuner_dbg("Cmd %s accepted for radio\n", cmd);
226 case V4L2_TUNER_ANALOG_TV:
227 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
229 case V4L2_TUNER_DIGITAL_TV:
230 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
238 static char pal[] = "-";
239 module_param_string(pal, pal, sizeof(pal), 0644);
240 static char secam[] = "-";
241 module_param_string(secam, secam, sizeof(secam), 0644);
243 /* get more precise norm info from insmod option */
244 static int tuner_fixup_std(struct tuner *t)
246 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
252 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
253 t->std = V4L2_STD_PAL_BG;
257 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
258 t->std = V4L2_STD_PAL_I;
264 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
265 t->std = V4L2_STD_PAL_DK;
269 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
270 t->std = V4L2_STD_PAL_M;
274 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
275 t->std = V4L2_STD_PAL_N;
279 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
285 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
286 t->std = V4L2_STD_SECAM_DK;
290 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
291 t->std = V4L2_STD_SECAM_L;
299 /* ---------------------------------------------------------------------- */
301 /* static var Used only in tuner_attach and tuner_probe */
302 static unsigned default_mode_mask;
304 /* During client attach, set_type is called by adapter's attach_inform callback.
305 set_type must then be completed by tuner_attach.
307 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
311 client_template.adapter = adap;
312 client_template.addr = addr;
314 t = kmalloc(sizeof(struct tuner), GFP_KERNEL);
317 memset(t, 0, sizeof(struct tuner));
318 memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
319 i2c_set_clientdata(&t->i2c, t);
321 t->radio_if2 = 10700 * 1000; /* 10.7MHz - FM radio */
322 t->audmode = V4L2_TUNER_MODE_STEREO;
323 t->mode_mask = T_UNINITIALIZED;
326 tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
329 unsigned char buffer[16];
332 memset(buffer, 0, sizeof(buffer));
333 rc = i2c_master_recv(&t->i2c, buffer, sizeof(buffer));
334 printk("tuner-%04x I2C RECV = ",addr);
336 printk("%02x ",buffer[i]);
339 /* TEA5767 autodetection code - only for addr = 0xc0 */
340 if (!no_autodetect) {
342 if (tea5767_autodetection(&t->i2c) != EINVAL) {
343 t->type = TUNER_TEA5767;
344 t->mode_mask = T_RADIO;
346 t->freq = 87.5 * 16; /* Sets freq to FM range */
347 default_mode_mask &= ~T_RADIO;
349 i2c_attach_client (&t->i2c);
350 set_type(&t->i2c,t->type, t->mode_mask);
356 /* Initializes only the first adapter found */
357 if (default_mode_mask != T_UNINITIALIZED) {
358 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
359 t->mode_mask = default_mode_mask;
360 t->freq = 400 * 16; /* Sets freq to VHF High */
361 default_mode_mask = T_UNINITIALIZED;
364 /* Should be just before return */
365 i2c_attach_client (&t->i2c);
366 set_type (&t->i2c,t->type, t->mode_mask);
370 static int tuner_probe(struct i2c_adapter *adap)
373 normal_i2c[0] = addr;
374 normal_i2c[1] = I2C_CLIENT_END;
377 default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
379 if (adap->class & I2C_CLASS_TV_ANALOG)
380 return i2c_probe(adap, &addr_data, tuner_attach);
384 static int tuner_detach(struct i2c_client *client)
386 struct tuner *t = i2c_get_clientdata(client);
389 err = i2c_detach_client(&t->i2c);
392 ("Client deregistration failed, client not detached.\n");
401 * Switch tuner to other mode. If tuner support both tv and radio,
402 * set another frequency to some value (This is needed for some pal
403 * tuners to avoid locking). Otherwise, just put second tuner in
407 static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
409 if (mode != t->mode) {
412 if (check_mode(t, cmd) == EINVAL) {
414 if (V4L2_TUNER_RADIO == mode) {
415 set_tv_freq(client, 400 * 16);
417 set_radio_freq(client, 87.5 * 16000);
425 #define switch_v4l2() if (!t->using_v4l2) \
426 tuner_dbg("switching to v4l2\n"); \
429 static inline int check_v4l2(struct tuner *t)
432 tuner_dbg ("ignore v4l1 call\n");
438 static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
440 struct tuner *t = i2c_get_clientdata(client);
441 unsigned int *iarg = (int *)arg;
444 /* --- configuration --- */
445 case TUNER_SET_TYPE_ADDR:
446 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x\n",
447 ((struct tuner_setup *)arg)->type,
448 ((struct tuner_setup *)arg)->addr,
449 ((struct tuner_setup *)arg)->mode_mask);
451 set_addr(client, (struct tuner_setup *)arg);
454 set_mode(client,t,V4L2_TUNER_RADIO, "AUDC_SET_RADIO");
456 case AUDC_CONFIG_PINNACLE:
457 if (check_mode(t, "AUDC_CONFIG_PINNACLE") == EINVAL)
461 tuner_dbg("pinnacle pal\n");
462 t->radio_if2 = 33300 * 1000;
465 tuner_dbg("pinnacle ntsc\n");
466 t->radio_if2 = 41300 * 1000;
471 if (check_mode(t, "VIDIOCSAUDIO") == EINVAL)
473 if (check_v4l2(t) == EINVAL)
476 /* Should be implemented, since bttv calls it */
477 tuner_dbg("VIDIOCSAUDIO not implemented.\n");
481 case TDA9887_SET_CONFIG:
483 /* --- v4l ioctls --- */
484 /* take care: bttv does userspace copying, we'll get a
485 kernel pointer here... */
488 static const v4l2_std_id map[] = {
489 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
490 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
491 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
492 [4 /* bttv */ ] = V4L2_STD_PAL_M,
493 [5 /* bttv */ ] = V4L2_STD_PAL_N,
494 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
496 struct video_channel *vc = arg;
498 if (check_v4l2(t) == EINVAL)
501 if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
504 if (vc->norm < ARRAY_SIZE(map))
505 t->std = map[vc->norm];
508 set_tv_freq(client, t->freq);
513 unsigned long *v = arg;
515 if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
517 if (check_v4l2(t) == EINVAL)
520 set_freq(client, *v);
525 struct video_tuner *vt = arg;
527 if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
529 if (check_v4l2(t) == EINVAL)
532 if (V4L2_TUNER_RADIO == t->mode) {
534 vt->signal = t->has_signal(client);
536 if (t->is_stereo(client))
538 VIDEO_TUNER_STEREO_ON;
541 ~VIDEO_TUNER_STEREO_ON;
543 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
545 vt->rangelow = radio_range[0] * 16000;
546 vt->rangehigh = radio_range[1] * 16000;
549 vt->rangelow = tv_range[0] * 16;
550 vt->rangehigh = tv_range[1] * 16;
557 struct video_audio *va = arg;
559 if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
561 if (check_v4l2(t) == EINVAL)
564 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
565 va->mode = t->is_stereo(client)
566 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
572 v4l2_std_id *id = arg;
574 if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
583 set_freq(client, t->freq);
586 case VIDIOC_S_FREQUENCY:
588 struct v4l2_frequency *f = arg;
590 t->freq = f->frequency;
592 if (V4L2_TUNER_RADIO == f->type &&
593 V4L2_TUNER_RADIO != t->mode) {
594 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
598 set_freq(client,t->freq);
602 case VIDIOC_G_FREQUENCY:
604 struct v4l2_frequency *f = arg;
606 if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
610 f->frequency = t->freq;
615 struct v4l2_tuner *tuner = arg;
617 if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
621 if (V4L2_TUNER_RADIO == t->mode) {
624 tuner->signal = t->has_signal(client);
627 if (t->is_stereo(client)) {
629 V4L2_TUNER_SUB_STEREO |
638 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
640 tuner->audmode = t->audmode;
642 tuner->rangelow = radio_range[0] * 16000;
643 tuner->rangehigh = radio_range[1] * 16000;
645 tuner->rangelow = tv_range[0] * 16;
646 tuner->rangehigh = tv_range[1] * 16;
652 struct v4l2_tuner *tuner = arg;
654 if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
659 if (V4L2_TUNER_RADIO == t->mode) {
660 t->audmode = tuner->audmode;
661 set_radio_freq(client, t->freq);
666 tuner_dbg("Unimplemented IOCTL 0x%08x(dir=%d,tp=0x%02x,nr=%d,sz=%d)\n",
667 cmd, _IOC_DIR(cmd), _IOC_TYPE(cmd),
668 _IOC_NR(cmd), _IOC_SIZE(cmd));
675 static int tuner_suspend(struct device *dev, u32 state, u32 level)
677 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
678 struct tuner *t = i2c_get_clientdata (c);
680 tuner_dbg ("suspend\n");
681 /* FIXME: power down ??? */
685 static int tuner_resume(struct device *dev, u32 level)
687 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
688 struct tuner *t = i2c_get_clientdata (c);
690 tuner_dbg ("resume\n");
692 set_freq(c, t->freq);
696 /* ----------------------------------------------------------------------- */
698 static struct i2c_driver driver = {
699 .owner = THIS_MODULE,
701 .id = I2C_DRIVERID_TUNER,
702 .flags = I2C_DF_NOTIFY,
703 .attach_adapter = tuner_probe,
704 .detach_client = tuner_detach,
705 .command = tuner_command,
707 .suspend = tuner_suspend,
708 .resume = tuner_resume,
711 static struct i2c_client client_template = {
712 I2C_DEVNAME("(tuner unset)"),
713 .flags = I2C_CLIENT_ALLOW_USE,
717 static int __init tuner_init_module(void)
719 return i2c_add_driver(&driver);
722 static void __exit tuner_cleanup_module(void)
724 i2c_del_driver(&driver);
727 module_init(tuner_init_module);
728 module_exit(tuner_cleanup_module);
731 * Overrides for Emacs so that we follow Linus's tabbing style.
732 * ---------------------------------------------------------------------------