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/kernel.h>
9 #include <linux/string.h>
10 #include <linux/timer.h>
11 #include <linux/delay.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/poll.h>
15 #include <linux/i2c.h>
16 #include <linux/types.h>
17 #include <linux/init.h>
18 #include <linux/videodev.h>
19 #include <media/tuner.h>
20 #include <media/v4l2-common.h>
21 #include "tuner-driver.h"
25 /* standard i2c insmod options */
26 static unsigned short normal_i2c[] = {
27 #ifdef CONFIG_TUNER_TEA5761
30 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
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 static unsigned int no_autodetect = 0;
41 static unsigned int show_i2c = 0;
43 /* insmod options used at runtime => read/write */
46 static unsigned int tv_range[2] = { 44, 958 };
47 static unsigned int radio_range[2] = { 65, 108 };
49 static char pal[] = "--";
50 static char secam[] = "--";
51 static char ntsc[] = "-";
54 module_param(addr, int, 0444);
55 module_param(no_autodetect, int, 0444);
56 module_param(show_i2c, int, 0444);
57 module_param_named(debug,tuner_debug, int, 0644);
58 module_param_string(pal, pal, sizeof(pal), 0644);
59 module_param_string(secam, secam, sizeof(secam), 0644);
60 module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
61 module_param_array(tv_range, int, NULL, 0644);
62 module_param_array(radio_range, int, NULL, 0644);
64 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
65 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
66 MODULE_LICENSE("GPL");
68 static struct i2c_driver driver;
69 static struct i2c_client client_template;
71 /* ---------------------------------------------------------------------- */
73 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
74 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
76 struct tuner *t = i2c_get_clientdata(c);
78 if (t->type == UNSET) {
79 tuner_warn ("tuner type not set\n");
82 if (NULL == t->ops.set_tv_freq) {
83 tuner_warn ("Tuner has no way to set tv freq\n");
86 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
87 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
88 freq / 16, freq % 16 * 100 / 16, tv_range[0],
90 /* V4L2 spec: if the freq is not possible then the closest
91 possible value should be selected */
92 if (freq < tv_range[0] * 16)
93 freq = tv_range[0] * 16;
95 freq = tv_range[1] * 16;
97 t->ops.set_tv_freq(c, freq);
100 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
102 struct tuner *t = i2c_get_clientdata(c);
104 if (t->type == UNSET) {
105 tuner_warn ("tuner type not set\n");
108 if (NULL == t->ops.set_radio_freq) {
109 tuner_warn ("tuner has no way to set radio frequency\n");
112 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
113 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
114 freq / 16000, freq % 16000 * 100 / 16000,
115 radio_range[0], radio_range[1]);
116 /* V4L2 spec: if the freq is not possible then the closest
117 possible value should be selected */
118 if (freq < radio_range[0] * 16000)
119 freq = radio_range[0] * 16000;
121 freq = radio_range[1] * 16000;
124 t->ops.set_radio_freq(c, freq);
127 static void set_freq(struct i2c_client *c, unsigned long freq)
129 struct tuner *t = i2c_get_clientdata(c);
132 case V4L2_TUNER_RADIO:
133 tuner_dbg("radio freq set to %lu.%02lu\n",
134 freq / 16000, freq % 16000 * 100 / 16000);
135 set_radio_freq(c, freq);
136 t->radio_freq = freq;
138 case V4L2_TUNER_ANALOG_TV:
139 case V4L2_TUNER_DIGITAL_TV:
140 tuner_dbg("tv freq set to %lu.%02lu\n",
141 freq / 16, freq % 16 * 100 / 16);
142 set_tv_freq(c, freq);
148 static void set_type(struct i2c_client *c, unsigned int type,
149 unsigned int new_mode_mask, unsigned int new_config,
150 int (*tuner_callback) (void *dev, int command,int arg))
152 struct tuner *t = i2c_get_clientdata(c);
153 unsigned char buffer[4];
155 if (type == UNSET || type == TUNER_ABSENT) {
156 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
160 if (type >= tuner_count) {
161 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
166 t->config = new_config;
167 if (tuner_callback != NULL) {
168 tuner_dbg("defining GPIO callback\n");
169 t->tuner_callback = tuner_callback;
172 /* This code detects calls by card attach_inform */
173 if (NULL == t->i2c.dev.driver) {
174 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
179 /* discard private data, in case set_type() was previously called */
191 case TUNER_PHILIPS_TDA8290:
195 if (tea5767_tuner_init(c) == EINVAL) {
196 t->type = TUNER_ABSENT;
197 t->mode_mask = T_UNINITIALIZED;
200 t->mode_mask = T_RADIO;
202 #ifdef CONFIG_TUNER_TEA5761
204 if (tea5761_tuner_init(c) == EINVAL) {
205 t->type = TUNER_ABSENT;
206 t->mode_mask = T_UNINITIALIZED;
209 t->mode_mask = T_RADIO;
212 case TUNER_PHILIPS_FMD1216ME_MK3:
217 i2c_master_send(c, buffer, 4);
221 i2c_master_send(c, buffer, 4);
222 default_tuner_init(c);
224 case TUNER_PHILIPS_TD1316:
229 i2c_master_send(c,buffer,4);
230 default_tuner_init(c);
233 tda9887_tuner_init(c);
236 default_tuner_init(c);
240 if (t->mode_mask == T_UNINITIALIZED)
241 t->mode_mask = new_mode_mask;
243 set_freq(c, (V4L2_TUNER_RADIO == t->mode) ? t->radio_freq : t->tv_freq);
244 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
245 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
250 * This function apply tuner config to tuner specified
251 * by tun_setup structure. I addr is unset, then admin status
252 * and tun addr status is more precise then current status,
253 * it's applied. Otherwise status and type are applied only to
254 * tuner with exactly the same addr.
257 static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
259 struct tuner *t = i2c_get_clientdata(c);
261 tuner_dbg("set addr for type %i\n", t->type);
263 if ( (t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
264 (t->mode_mask & tun_setup->mode_mask))) ||
265 (tun_setup->addr == c->addr)) {
266 set_type(c, tun_setup->type, tun_setup->mode_mask,
267 tun_setup->config, tun_setup->tuner_callback);
271 static inline int check_mode(struct tuner *t, char *cmd)
273 if ((1 << t->mode & t->mode_mask) == 0) {
278 case V4L2_TUNER_RADIO:
279 tuner_dbg("Cmd %s accepted for radio\n", cmd);
281 case V4L2_TUNER_ANALOG_TV:
282 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
284 case V4L2_TUNER_DIGITAL_TV:
285 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
291 /* get more precise norm info from insmod option */
292 static int tuner_fixup_std(struct tuner *t)
294 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
297 tuner_dbg ("insmod fixup: PAL => PAL-60\n");
298 t->std = V4L2_STD_PAL_60;
304 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
305 t->std = V4L2_STD_PAL_BG;
309 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
310 t->std = V4L2_STD_PAL_I;
316 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
317 t->std = V4L2_STD_PAL_DK;
321 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
322 t->std = V4L2_STD_PAL_M;
326 if (pal[1] == 'c' || pal[1] == 'C') {
327 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
328 t->std = V4L2_STD_PAL_Nc;
330 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
331 t->std = V4L2_STD_PAL_N;
335 /* default parameter, do nothing */
338 tuner_warn ("pal= argument not recognised\n");
342 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
350 tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
351 t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
357 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
358 t->std = V4L2_STD_SECAM_DK;
362 if ((secam[1]=='C')||(secam[1]=='c')) {
363 tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n");
364 t->std = V4L2_STD_SECAM_LC;
366 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
367 t->std = V4L2_STD_SECAM_L;
371 /* default parameter, do nothing */
374 tuner_warn ("secam= argument not recognised\n");
379 if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
383 tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
384 t->std = V4L2_STD_NTSC_M;
388 tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
389 t->std = V4L2_STD_NTSC_M_JP;
393 tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
394 t->std = V4L2_STD_NTSC_M_KR;
397 /* default parameter, do nothing */
400 tuner_info("ntsc= argument not recognised\n");
407 static void tuner_status(struct i2c_client *client)
409 struct tuner *t = i2c_get_clientdata(client);
410 unsigned long freq, freq_fraction;
414 case V4L2_TUNER_RADIO: p = "radio"; break;
415 case V4L2_TUNER_ANALOG_TV: p = "analog TV"; break;
416 case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break;
417 default: p = "undefined"; break;
419 if (t->mode == V4L2_TUNER_RADIO) {
420 freq = t->radio_freq / 16000;
421 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
423 freq = t->tv_freq / 16;
424 freq_fraction = (t->tv_freq % 16) * 100 / 16;
426 tuner_info("Tuner mode: %s\n", p);
427 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
428 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
429 if (t->mode != V4L2_TUNER_RADIO)
431 if (t->ops.has_signal) {
432 tuner_info("Signal strength: %d\n", t->ops.has_signal(client));
434 if (t->ops.is_stereo) {
435 tuner_info("Stereo: %s\n", t->ops.is_stereo(client) ? "yes" : "no");
439 /* ---------------------------------------------------------------------- */
441 /* static vars: used only in tuner_attach and tuner_probe */
442 static unsigned default_mode_mask;
444 /* During client attach, set_type is called by adapter's attach_inform callback.
445 set_type must then be completed by tuner_attach.
447 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
451 client_template.adapter = adap;
452 client_template.addr = addr;
454 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
457 memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
458 i2c_set_clientdata(&t->i2c, t);
460 t->audmode = V4L2_TUNER_MODE_STEREO;
461 t->mode_mask = T_UNINITIALIZED;
462 t->ops.tuner_status = tuner_status;
465 unsigned char buffer[16];
468 memset(buffer, 0, sizeof(buffer));
469 rc = i2c_master_recv(&t->i2c, buffer, sizeof(buffer));
470 tuner_info("I2C RECV = ");
472 printk("%02x ",buffer[i]);
475 /* HACK: This test were added to avoid tuner to probe tda9840 and tea6415c on the MXB card */
476 if (adap->id == I2C_HW_SAA7146 && addr < 0x4a)
479 /* autodetection code based on the i2c addr */
480 if (!no_autodetect) {
482 #ifdef CONFIG_TUNER_TEA5761
484 if (tea5761_autodetection(&t->i2c) != EINVAL) {
485 t->type = TUNER_TEA5761;
486 t->mode_mask = T_RADIO;
488 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
489 default_mode_mask &= ~T_RADIO;
491 goto register_client;
499 /* If chip is not tda8290, don't register.
500 since it can be tda9887*/
501 if (tda8290_probe(&t->i2c) == 0) {
502 tuner_dbg("chip at addr %x is a tda8290\n", addr);
504 /* Default is being tda9887 */
505 t->type = TUNER_TDA9887;
506 t->mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
508 goto register_client;
512 if (tea5767_autodetection(&t->i2c) != EINVAL) {
513 t->type = TUNER_TEA5767;
514 t->mode_mask = T_RADIO;
516 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
517 default_mode_mask &= ~T_RADIO;
519 goto register_client;
525 /* Initializes only the first adapter found */
526 if (default_mode_mask != T_UNINITIALIZED) {
527 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
528 t->mode_mask = default_mode_mask;
529 t->tv_freq = 400 * 16; /* Sets freq to VHF High */
530 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
531 default_mode_mask = T_UNINITIALIZED;
534 /* Should be just before return */
536 tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
537 i2c_attach_client (&t->i2c);
538 set_type (&t->i2c,t->type, t->mode_mask, t->config, t->tuner_callback);
542 static int tuner_probe(struct i2c_adapter *adap)
545 normal_i2c[0] = addr;
546 normal_i2c[1] = I2C_CLIENT_END;
549 default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
551 if (adap->class & I2C_CLASS_TV_ANALOG)
552 return i2c_probe(adap, &addr_data, tuner_attach);
556 static int tuner_detach(struct i2c_client *client)
558 struct tuner *t = i2c_get_clientdata(client);
561 err = i2c_detach_client(&t->i2c);
564 ("Client deregistration failed, client not detached.\n");
569 t->ops.release(client);
578 * Switch tuner to other mode. If tuner support both tv and radio,
579 * set another frequency to some value (This is needed for some pal
580 * tuners to avoid locking). Otherwise, just put second tuner in
584 static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
591 if (check_mode(t, cmd) == EINVAL) {
594 t->ops.standby (client);
600 #define switch_v4l2() if (!t->using_v4l2) \
601 tuner_dbg("switching to v4l2\n"); \
604 static inline int check_v4l2(struct tuner *t)
606 /* bttv still uses both v4l1 and v4l2 calls to the tuner (v4l2 for
607 TV, v4l1 for radio), until that is fixed this code is disabled.
608 Otherwise the radio (v4l1) wouldn't tune after using the TV (v4l2)
613 static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
615 struct tuner *t = i2c_get_clientdata(client);
618 v4l_i2c_print_ioctl(&(t->i2c),cmd);
621 /* --- configuration --- */
622 case TUNER_SET_TYPE_ADDR:
623 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
624 ((struct tuner_setup *)arg)->type,
625 ((struct tuner_setup *)arg)->addr,
626 ((struct tuner_setup *)arg)->mode_mask,
627 ((struct tuner_setup *)arg)->config);
629 set_addr(client, (struct tuner_setup *)arg);
632 if (set_mode(client, t, V4L2_TUNER_RADIO, "AUDC_SET_RADIO")
636 set_freq(client, t->radio_freq);
638 case TUNER_SET_STANDBY:
639 if (check_mode(t, "TUNER_SET_STANDBY") == EINVAL)
643 t->ops.standby (client);
645 #ifdef CONFIG_VIDEO_V4L1
647 if (check_mode(t, "VIDIOCSAUDIO") == EINVAL)
649 if (check_v4l2(t) == EINVAL)
652 /* Should be implemented, since bttv calls it */
653 tuner_dbg("VIDIOCSAUDIO not implemented.\n");
657 static const v4l2_std_id map[] = {
658 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
659 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
660 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
661 [4 /* bttv */ ] = V4L2_STD_PAL_M,
662 [5 /* bttv */ ] = V4L2_STD_PAL_N,
663 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
665 struct video_channel *vc = arg;
667 if (check_v4l2(t) == EINVAL)
670 if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
673 if (vc->norm < ARRAY_SIZE(map))
674 t->std = map[vc->norm];
677 set_tv_freq(client, t->tv_freq);
682 unsigned long *v = arg;
684 if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
686 if (check_v4l2(t) == EINVAL)
689 set_freq(client, *v);
694 struct video_tuner *vt = arg;
696 if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
698 if (check_v4l2(t) == EINVAL)
701 if (V4L2_TUNER_RADIO == t->mode) {
702 if (t->ops.has_signal)
703 vt->signal = t->ops.has_signal(client);
704 if (t->ops.is_stereo) {
705 if (t->ops.is_stereo(client))
707 VIDEO_TUNER_STEREO_ON;
710 ~VIDEO_TUNER_STEREO_ON;
712 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
714 vt->rangelow = radio_range[0] * 16000;
715 vt->rangehigh = radio_range[1] * 16000;
718 vt->rangelow = tv_range[0] * 16;
719 vt->rangehigh = tv_range[1] * 16;
726 struct video_audio *va = arg;
728 if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
730 if (check_v4l2(t) == EINVAL)
733 if (V4L2_TUNER_RADIO == t->mode && t->ops.is_stereo)
734 va->mode = t->ops.is_stereo(client)
735 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
739 case TDA9887_SET_CONFIG:
740 if (t->type == TUNER_TDA9887) {
743 t->tda9887_config = *i;
744 set_freq(client, t->tv_freq);
747 /* --- v4l ioctls --- */
748 /* take care: bttv does userspace copying, we'll get a
749 kernel pointer here... */
752 v4l2_std_id *id = arg;
754 if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
763 set_freq(client, t->tv_freq);
766 case VIDIOC_S_FREQUENCY:
768 struct v4l2_frequency *f = arg;
770 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
774 set_freq(client,f->frequency);
778 case VIDIOC_G_FREQUENCY:
780 struct v4l2_frequency *f = arg;
782 if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
786 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
787 t->radio_freq : t->tv_freq;
792 struct v4l2_tuner *tuner = arg;
794 if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
798 tuner->type = t->mode;
800 tuner->afc=t->ops.get_afc(client);
801 if (t->mode == V4L2_TUNER_ANALOG_TV)
802 tuner->capability |= V4L2_TUNER_CAP_NORM;
803 if (t->mode != V4L2_TUNER_RADIO) {
804 tuner->rangelow = tv_range[0] * 16;
805 tuner->rangehigh = tv_range[1] * 16;
810 if (t->ops.has_signal)
811 tuner->signal = t->ops.has_signal(client);
814 V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
815 if (t->ops.is_stereo) {
816 tuner->rxsubchans = t->ops.is_stereo(client) ?
817 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
821 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
822 tuner->audmode = t->audmode;
823 tuner->rangelow = radio_range[0] * 16000;
824 tuner->rangehigh = radio_range[1] * 16000;
829 struct v4l2_tuner *tuner = arg;
831 if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
836 /* do nothing unless we're a radio tuner */
837 if (t->mode != V4L2_TUNER_RADIO)
839 t->audmode = tuner->audmode;
840 set_radio_freq(client, t->radio_freq);
843 case VIDIOC_LOG_STATUS:
844 if (t->ops.tuner_status)
845 t->ops.tuner_status(client);
852 static int tuner_suspend(struct i2c_client *c, pm_message_t state)
854 struct tuner *t = i2c_get_clientdata (c);
856 tuner_dbg ("suspend\n");
857 /* FIXME: power down ??? */
861 static int tuner_resume(struct i2c_client *c)
863 struct tuner *t = i2c_get_clientdata (c);
865 tuner_dbg ("resume\n");
866 if (V4L2_TUNER_RADIO == t->mode) {
868 set_freq(c, t->radio_freq);
871 set_freq(c, t->tv_freq);
876 /* ----------------------------------------------------------------------- */
878 static struct i2c_driver driver = {
879 .id = I2C_DRIVERID_TUNER,
880 .attach_adapter = tuner_probe,
881 .detach_client = tuner_detach,
882 .command = tuner_command,
883 .suspend = tuner_suspend,
884 .resume = tuner_resume,
889 static struct i2c_client client_template = {
890 .name = "(tuner unset)",
894 static int __init tuner_init_module(void)
896 return i2c_add_driver(&driver);
899 static void __exit tuner_cleanup_module(void)
901 i2c_del_driver(&driver);
904 module_init(tuner_init_module);
905 module_exit(tuner_cleanup_module);
908 * Overrides for Emacs so that we follow Linus's tabbing style.
909 * ---------------------------------------------------------------------------