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/v4l2-common.h>
24 #include <media/audiochip.h>
30 /* standard i2c insmod options */
31 static unsigned short normal_i2c[] = {
32 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
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 static unsigned int no_autodetect = 0;
43 static unsigned int show_i2c = 0;
45 /* insmod options used at runtime => read/write */
46 unsigned int tuner_debug = 0;
48 static unsigned int tv_range[2] = { 44, 958 };
49 static unsigned int radio_range[2] = { 65, 108 };
51 static char pal[] = "--";
52 static char secam[] = "--";
53 static char ntsc[] = "-";
55 module_param(addr, int, 0444);
56 module_param(no_autodetect, int, 0444);
57 module_param(show_i2c, int, 0444);
58 module_param(tuner_debug, int, 0644);
60 module_param_string(pal, pal, sizeof(pal), 0644);
61 module_param_string(secam, secam, sizeof(secam), 0644);
62 module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
63 module_param_array(tv_range, int, NULL, 0644);
64 module_param_array(radio_range, int, NULL, 0644);
66 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
67 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
68 MODULE_LICENSE("GPL");
70 static struct i2c_driver driver;
71 static struct i2c_client client_template;
73 /* ---------------------------------------------------------------------- */
75 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
76 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
78 struct tuner *t = i2c_get_clientdata(c);
80 if (t->type == UNSET) {
81 tuner_warn ("tuner type not set\n");
84 if (NULL == t->tv_freq) {
85 tuner_warn ("Tuner has no way to set tv freq\n");
88 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
89 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
90 freq / 16, freq % 16 * 100 / 16, tv_range[0],
96 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
98 struct tuner *t = i2c_get_clientdata(c);
100 if (t->type == UNSET) {
101 tuner_warn ("tuner type not set\n");
104 if (NULL == t->radio_freq) {
105 tuner_warn ("tuner has no way to set radio frequency\n");
108 if (freq <= radio_range[0] * 16000 || freq >= radio_range[1] * 16000) {
109 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
110 freq / 16000, freq % 16000 * 100 / 16000,
111 radio_range[0], radio_range[1]);
114 t->radio_freq(c, freq);
118 static void set_freq(struct i2c_client *c, unsigned long freq)
120 struct tuner *t = i2c_get_clientdata(c);
123 case V4L2_TUNER_RADIO:
124 tuner_dbg("radio freq set to %lu.%02lu\n",
125 freq / 16000, freq % 16000 * 100 / 16000);
126 set_radio_freq(c, freq);
128 case V4L2_TUNER_ANALOG_TV:
129 case V4L2_TUNER_DIGITAL_TV:
130 tuner_dbg("tv freq set to %lu.%02lu\n",
131 freq / 16, freq % 16 * 100 / 16);
132 set_tv_freq(c, freq);
138 static void set_type(struct i2c_client *c, unsigned int type,
139 unsigned int new_mode_mask)
141 struct tuner *t = i2c_get_clientdata(c);
142 unsigned char buffer[4];
144 if (type == UNSET || type == TUNER_ABSENT) {
145 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
149 if (type >= tuner_count) {
150 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
154 /* This code detects calls by card attach_inform */
155 if (NULL == t->i2c.dev.driver) {
156 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
168 case TUNER_PHILIPS_TDA8290:
172 if (tea5767_tuner_init(c) == EINVAL) {
173 t->type = TUNER_ABSENT;
174 t->mode_mask = T_UNINITIALIZED;
177 t->mode_mask = T_RADIO;
179 case TUNER_PHILIPS_FMD1216ME_MK3:
184 i2c_master_send(c, buffer, 4);
188 i2c_master_send(c, buffer, 4);
189 default_tuner_init(c);
191 case TUNER_LG_TDVS_H062F:
192 /* Set the Auxiliary Byte. */
196 i2c_master_send(c, buffer, 4);
197 default_tuner_init(c);
199 case TUNER_PHILIPS_TD1316:
204 i2c_master_send(c,buffer,4);
205 default_tuner_init(c);
207 default_tuner_init(c);
211 if (t->mode_mask == T_UNINITIALIZED)
212 t->mode_mask = new_mode_mask;
214 set_freq(c, t->freq);
215 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
216 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
221 * This function apply tuner config to tuner specified
222 * by tun_setup structure. I addr is unset, then admin status
223 * and tun addr status is more precise then current status,
224 * it's applied. Otherwise status and type are applied only to
225 * tuner with exactly the same addr.
228 static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
230 struct tuner *t = i2c_get_clientdata(c);
232 if ( t->type == UNSET && ((tun_setup->addr == ADDR_UNSET &&
233 (t->mode_mask & tun_setup->mode_mask)) ||
234 tun_setup->addr == c->addr)) {
235 set_type(c, tun_setup->type, tun_setup->mode_mask);
239 static inline int check_mode(struct tuner *t, char *cmd)
241 if ((1 << t->mode & t->mode_mask) == 0) {
246 case V4L2_TUNER_RADIO:
247 tuner_dbg("Cmd %s accepted for radio\n", cmd);
249 case V4L2_TUNER_ANALOG_TV:
250 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
252 case V4L2_TUNER_DIGITAL_TV:
253 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
259 /* get more precise norm info from insmod option */
260 static int tuner_fixup_std(struct tuner *t)
262 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
268 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
269 t->std = V4L2_STD_PAL_BG;
273 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
274 t->std = V4L2_STD_PAL_I;
280 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
281 t->std = V4L2_STD_PAL_DK;
285 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
286 t->std = V4L2_STD_PAL_M;
290 if (pal[1] == 'c' || pal[1] == 'C') {
291 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
292 t->std = V4L2_STD_PAL_Nc;
294 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
295 t->std = V4L2_STD_PAL_N;
299 /* default parameter, do nothing */
302 tuner_warn ("pal= argument not recognised\n");
306 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
314 tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
315 t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
321 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
322 t->std = V4L2_STD_SECAM_DK;
326 if ((secam[1]=='C')||(secam[1]=='c')) {
327 tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n");
328 t->std = V4L2_STD_SECAM_LC;
330 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
331 t->std = V4L2_STD_SECAM_L;
335 /* default parameter, do nothing */
338 tuner_warn ("secam= argument not recognised\n");
343 if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
347 tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
348 t->std = V4L2_STD_NTSC_M;
352 tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
353 t->std = V4L2_STD_NTSC_M_JP;
356 /* default parameter, do nothing */
359 tuner_info("ntsc= argument not recognised\n");
366 static void tuner_status(struct i2c_client *client)
368 struct tuner *t = i2c_get_clientdata(client);
369 unsigned long freq, freq_fraction;
373 case V4L2_TUNER_RADIO: p = "radio"; break;
374 case V4L2_TUNER_ANALOG_TV: p = "analog TV"; break;
375 case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break;
376 default: p = "undefined"; break;
378 if (t->mode == V4L2_TUNER_RADIO) {
379 freq = t->freq / 16000;
380 freq_fraction = (t->freq % 16000) * 100 / 16000;
383 freq_fraction = (t->freq % 16) * 100 / 16;
385 tuner_info("Tuner mode: %s\n", p);
386 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
387 tuner_info("Standard: 0x%08llx\n", t->std);
388 if (t->mode == V4L2_TUNER_RADIO) {
390 tuner_info("Signal strength: %d\n", t->has_signal(client));
393 tuner_info("Stereo: %s\n", t->is_stereo(client) ? "yes" : "no");
397 /* ---------------------------------------------------------------------- */
399 /* static var Used only in tuner_attach and tuner_probe */
400 static unsigned default_mode_mask;
402 /* During client attach, set_type is called by adapter's attach_inform callback.
403 set_type must then be completed by tuner_attach.
405 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
409 client_template.adapter = adap;
410 client_template.addr = addr;
412 t = kmalloc(sizeof(struct tuner), GFP_KERNEL);
415 memset(t, 0, sizeof(struct tuner));
416 memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
417 i2c_set_clientdata(&t->i2c, t);
419 t->radio_if2 = 10700 * 1000; /* 10.7MHz - FM radio */
420 t->audmode = V4L2_TUNER_MODE_STEREO;
421 t->mode_mask = T_UNINITIALIZED;
424 unsigned char buffer[16];
427 memset(buffer, 0, sizeof(buffer));
428 rc = i2c_master_recv(&t->i2c, buffer, sizeof(buffer));
429 tuner_info("I2C RECV = ");
431 printk("%02x ",buffer[i]);
434 /* TEA5767 autodetection code - only for addr = 0xc0 */
435 if (!no_autodetect) {
441 /* If chip is not tda8290, don't register.
442 since it can be tda9887*/
443 if (tda8290_probe(&t->i2c) != 0) {
444 tuner_dbg("chip at addr %x is not a tda8290\n", addr);
450 if (tea5767_autodetection(&t->i2c) != EINVAL) {
451 t->type = TUNER_TEA5767;
452 t->mode_mask = T_RADIO;
454 t->freq = 87.5 * 16; /* Sets freq to FM range */
455 default_mode_mask &= ~T_RADIO;
457 goto register_client;
463 /* Initializes only the first adapter found */
464 if (default_mode_mask != T_UNINITIALIZED) {
465 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
466 t->mode_mask = default_mode_mask;
467 t->freq = 400 * 16; /* Sets freq to VHF High */
468 default_mode_mask = T_UNINITIALIZED;
471 /* Should be just before return */
473 tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
474 i2c_attach_client (&t->i2c);
475 set_type (&t->i2c,t->type, t->mode_mask);
479 static int tuner_probe(struct i2c_adapter *adap)
482 normal_i2c[0] = addr;
483 normal_i2c[1] = I2C_CLIENT_END;
486 default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
488 if (adap->class & I2C_CLASS_TV_ANALOG)
489 return i2c_probe(adap, &addr_data, tuner_attach);
493 static int tuner_detach(struct i2c_client *client)
495 struct tuner *t = i2c_get_clientdata(client);
498 err = i2c_detach_client(&t->i2c);
501 ("Client deregistration failed, client not detached.\n");
510 * Switch tuner to other mode. If tuner support both tv and radio,
511 * set another frequency to some value (This is needed for some pal
512 * tuners to avoid locking). Otherwise, just put second tuner in
516 static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
523 if (check_mode(t, cmd) == EINVAL) {
532 #define switch_v4l2() if (!t->using_v4l2) \
533 tuner_dbg("switching to v4l2\n"); \
536 static inline int check_v4l2(struct tuner *t)
539 tuner_dbg ("ignore v4l1 call\n");
545 static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
547 struct tuner *t = i2c_get_clientdata(client);
550 v4l_i2c_print_ioctl(&(t->i2c),cmd);
553 /* --- configuration --- */
554 case TUNER_SET_TYPE_ADDR:
555 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x\n",
556 ((struct tuner_setup *)arg)->type,
557 ((struct tuner_setup *)arg)->addr,
558 ((struct tuner_setup *)arg)->mode_mask);
560 set_addr(client, (struct tuner_setup *)arg);
563 set_mode(client,t,V4L2_TUNER_RADIO, "AUDC_SET_RADIO");
565 case TUNER_SET_STANDBY:
567 if (check_mode(t, "TUNER_SET_STANDBY") == EINVAL)
574 if (check_mode(t, "VIDIOCSAUDIO") == EINVAL)
576 if (check_v4l2(t) == EINVAL)
579 /* Should be implemented, since bttv calls it */
580 tuner_dbg("VIDIOCSAUDIO not implemented.\n");
583 /* --- v4l ioctls --- */
584 /* take care: bttv does userspace copying, we'll get a
585 kernel pointer here... */
588 static const v4l2_std_id map[] = {
589 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
590 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
591 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
592 [4 /* bttv */ ] = V4L2_STD_PAL_M,
593 [5 /* bttv */ ] = V4L2_STD_PAL_N,
594 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
596 struct video_channel *vc = arg;
598 if (check_v4l2(t) == EINVAL)
601 if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
604 if (vc->norm < ARRAY_SIZE(map))
605 t->std = map[vc->norm];
608 set_tv_freq(client, t->freq);
613 unsigned long *v = arg;
615 if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
617 if (check_v4l2(t) == EINVAL)
620 set_freq(client, *v);
625 struct video_tuner *vt = arg;
627 if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
629 if (check_v4l2(t) == EINVAL)
632 if (V4L2_TUNER_RADIO == t->mode) {
634 vt->signal = t->has_signal(client);
636 if (t->is_stereo(client))
638 VIDEO_TUNER_STEREO_ON;
641 ~VIDEO_TUNER_STEREO_ON;
643 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
645 vt->rangelow = radio_range[0] * 16000;
646 vt->rangehigh = radio_range[1] * 16000;
649 vt->rangelow = tv_range[0] * 16;
650 vt->rangehigh = tv_range[1] * 16;
657 struct video_audio *va = arg;
659 if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
661 if (check_v4l2(t) == EINVAL)
664 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
665 va->mode = t->is_stereo(client)
666 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
672 v4l2_std_id *id = arg;
674 if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
683 set_freq(client, t->freq);
686 case VIDIOC_S_FREQUENCY:
688 struct v4l2_frequency *f = arg;
690 t->freq = f->frequency;
692 if (V4L2_TUNER_RADIO == f->type &&
693 V4L2_TUNER_RADIO != t->mode) {
694 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
698 set_freq(client,t->freq);
702 case VIDIOC_G_FREQUENCY:
704 struct v4l2_frequency *f = arg;
706 if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
710 f->frequency = t->freq;
715 struct v4l2_tuner *tuner = arg;
717 if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
721 if (V4L2_TUNER_RADIO == t->mode) {
724 tuner->signal = t->has_signal(client);
727 if (t->is_stereo(client)) {
729 V4L2_TUNER_SUB_STEREO |
738 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
740 tuner->audmode = t->audmode;
742 tuner->rangelow = radio_range[0] * 16000;
743 tuner->rangehigh = radio_range[1] * 16000;
745 tuner->rangelow = tv_range[0] * 16;
746 tuner->rangehigh = tv_range[1] * 16;
752 struct v4l2_tuner *tuner = arg;
754 if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
759 if (V4L2_TUNER_RADIO == t->mode) {
760 t->audmode = tuner->audmode;
761 set_radio_freq(client, t->freq);
765 case VIDIOC_LOG_STATUS:
766 tuner_status(client);
773 static int tuner_suspend(struct device *dev, pm_message_t state)
775 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
776 struct tuner *t = i2c_get_clientdata (c);
778 tuner_dbg ("suspend\n");
779 /* FIXME: power down ??? */
783 static int tuner_resume(struct device *dev)
785 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
786 struct tuner *t = i2c_get_clientdata (c);
788 tuner_dbg ("resume\n");
790 set_freq(c, t->freq);
794 /* ----------------------------------------------------------------------- */
796 static struct i2c_driver driver = {
797 .id = I2C_DRIVERID_TUNER,
798 .attach_adapter = tuner_probe,
799 .detach_client = tuner_detach,
800 .command = tuner_command,
803 .suspend = tuner_suspend,
804 .resume = tuner_resume,
807 static struct i2c_client client_template = {
808 .name = "(tuner unset)",
812 static int __init tuner_init_module(void)
814 return i2c_add_driver(&driver);
817 static void __exit tuner_cleanup_module(void)
819 i2c_del_driver(&driver);
822 module_init(tuner_init_module);
823 module_exit(tuner_cleanup_module);
826 * Overrides for Emacs so that we follow Linus's tabbing style.
827 * ---------------------------------------------------------------------------