2 * $Id: tuner-core.c,v 1.15 2005/06/12 01:36:14 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>
27 * comment line bellow to return to old behavor, where only one I2C device is supported
29 #define CONFIG_TUNER_MULTI_I2C /**/
33 /* standard i2c insmod options */
34 static unsigned short normal_i2c[] = {
38 static unsigned short normal_i2c_range[] = {
44 /* insmod options used at init time => read/only */
45 static unsigned int addr = 0;
46 module_param(addr, int, 0444);
48 /* insmod options used at runtime => read/write */
49 unsigned int tuner_debug = 0;
50 module_param(tuner_debug, int, 0644);
52 static unsigned int tv_range[2] = { 44, 958 };
53 static unsigned int radio_range[2] = { 65, 108 };
55 module_param_array(tv_range, int, NULL, 0644);
56 module_param_array(radio_range, int, NULL, 0644);
58 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
59 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
60 MODULE_LICENSE("GPL");
63 #ifdef CONFIG_TUNER_MULTI_I2C
64 static unsigned short first_tuner, tv_tuner, radio_tuner;
67 static struct i2c_driver driver;
68 static struct i2c_client client_template;
70 /* ---------------------------------------------------------------------- */
72 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
73 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
75 struct tuner *t = i2c_get_clientdata(c);
77 if (t->type == UNSET) {
78 tuner_info("tuner type not set\n");
81 if (NULL == t->tv_freq) {
82 tuner_info("Huh? tv_set is NULL?\n");
85 if (freq < tv_range[0]*16 || freq > tv_range[1]*16) {
87 if (freq >= tv_range[0]*16364 && freq <= tv_range[1]*16384) {
88 /* V4L2_TUNER_CAP_LOW frequency */
90 tuner_dbg("V4L2_TUNER_CAP_LOW freq selected for TV. Tuners yet doesn't support converting it to valid freq.\n");
92 t->tv_freq(c,freq>>10);
96 /* FIXME: better do that chip-specific, but
97 right now we don't have that in the config
98 struct and this way is still better than no
100 tuner_info("TV freq (%d.%02d) out of range (%d-%d)\n",
101 freq/16,freq%16*100/16,tv_range[0],tv_range[1]);
105 tuner_dbg("62.5 Khz freq step selected for TV.\n");
109 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
111 struct tuner *t = i2c_get_clientdata(c);
113 if (t->type == UNSET) {
114 tuner_info("tuner type not set\n");
117 if (NULL == t->radio_freq) {
118 tuner_info("no radio tuning for this one, sorry.\n");
121 if (freq < radio_range[0]*16 || freq > radio_range[1]*16) {
122 if (freq >= tv_range[0]*16364 && freq <= tv_range[1]*16384) {
123 /* V4L2_TUNER_CAP_LOW frequency */
124 if (t->type == TUNER_TEA5767) {
125 tuner_info("radio freq step 62.5Hz (%d.%06d)\n",(freq>>14),freq%(1<<14)*10000);
126 t->radio_freq(c,freq>>10);
130 tuner_dbg("V4L2_TUNER_CAP_LOW freq selected for Radio. Tuners yet doesn't support converting it to valid freq.\n");
132 tuner_info("radio freq (%d.%06d)\n",(freq>>14),freq%(1<<14)*10000);
134 t->radio_freq(c,freq>>10);
138 tuner_info("radio freq (%d.%02d) out of range (%d-%d)\n",
139 freq/16,freq%16*100/16,
140 radio_range[0],radio_range[1]);
144 tuner_dbg("62.5 Khz freq step selected for Radio.\n");
145 t->radio_freq(c,freq);
148 static void set_freq(struct i2c_client *c, unsigned long freq)
150 struct tuner *t = i2c_get_clientdata(c);
153 case V4L2_TUNER_RADIO:
154 tuner_dbg("radio freq set to %lu.%02lu\n",
155 freq/16,freq%16*100/16);
156 set_radio_freq(c,freq);
158 case V4L2_TUNER_ANALOG_TV:
159 case V4L2_TUNER_DIGITAL_TV:
160 tuner_dbg("tv freq set to %lu.%02lu\n",
161 freq/16,freq%16*100/16);
162 set_tv_freq(c, freq);
168 static void set_type(struct i2c_client *c, unsigned int type)
170 struct tuner *t = i2c_get_clientdata(c);
172 tuner_dbg ("I2C addr 0x%02x with type %d\n",c->addr<<1,type);
174 if (type == UNSET || type == TUNER_ABSENT)
176 if (type >= tuner_count)
179 if (NULL == t->i2c.dev.driver) {
180 /* not registered yet */
195 case TUNER_PHILIPS_TDA8290:
199 default_tuner_init(c);
204 #ifdef CONFIG_TUNER_MULTI_I2C
205 #define CHECK_ADDR(tp,cmd,tun) if (client->addr!=tp) { \
207 tuner_info ("Cmd %s accepted to "tun"\n",cmd);
208 #define CHECK_MODE(cmd) if (t->mode == V4L2_TUNER_RADIO) { \
209 CHECK_ADDR(radio_tuner,cmd,"radio") } else \
210 { CHECK_ADDR(tv_tuner,cmd,"TV"); }
212 #define CHECK_ADDR(tp,cmd,tun) tuner_info ("Cmd %s accepted to "tun"\n",cmd);
213 #define CHECK_MODE(cmd) tuner_info ("Cmd %s accepted\n",cmd);
216 #ifdef CONFIG_TUNER_MULTI_I2C
218 static void set_addr(struct i2c_client *c, struct tuner_addr *tun_addr)
220 /* ADDR_UNSET defaults to first available tuner */
221 if ( tun_addr->addr == ADDR_UNSET ) {
222 if (first_tuner != c->addr)
224 switch (tun_addr->v4l2_tuner) {
225 case V4L2_TUNER_RADIO:
233 /* Sets tuner to its configured value */
234 switch (tun_addr->v4l2_tuner) {
235 case V4L2_TUNER_RADIO:
236 radio_tuner=tun_addr->addr;
237 if ( tun_addr->addr == c->addr ) set_type(c,tun_addr->type);
240 tv_tuner=tun_addr->addr;
241 if ( tun_addr->addr == c->addr ) set_type(c,tun_addr->type);
245 set_type(c,tun_addr->type);
248 #define set_addr(c,tun_addr) set_type(c,(tun_addr)->type)
251 static char pal[] = "-";
252 module_param_string(pal, pal, sizeof(pal), 0644);
254 static int tuner_fixup_std(struct tuner *t)
256 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
257 /* get more precise norm info from insmod option */
263 tuner_dbg("insmod fixup: PAL => PAL-BG\n");
264 t->std = V4L2_STD_PAL_BG;
268 tuner_dbg("insmod fixup: PAL => PAL-I\n");
269 t->std = V4L2_STD_PAL_I;
275 tuner_dbg("insmod fixup: PAL => PAL-DK\n");
276 t->std = V4L2_STD_PAL_DK;
283 /* ---------------------------------------------------------------------- */
285 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
289 #ifndef CONFIG_TUNER_MULTI_I2C
293 /* by default, first I2C card is both tv and radio tuner */
294 if (this_adap == 0) {
302 client_template.adapter = adap;
303 client_template.addr = addr;
305 t = kmalloc(sizeof(struct tuner),GFP_KERNEL);
308 memset(t,0,sizeof(struct tuner));
309 memcpy(&t->i2c,&client_template,sizeof(struct i2c_client));
310 i2c_set_clientdata(&t->i2c, t);
312 t->radio_if2 = 10700*1000; /* 10.7MHz - FM radio */
314 i2c_attach_client(&t->i2c);
315 tuner_info("chip found @ 0x%x (%s)\n",
316 addr << 1, adap->name);
318 set_type(&t->i2c, t->type);
322 static int tuner_probe(struct i2c_adapter *adap)
325 normal_i2c[0] = addr;
326 normal_i2c_range[0] = addr;
327 normal_i2c_range[1] = addr;
331 #ifdef CONFIG_TUNER_MULTI_I2C
337 if (adap->class & I2C_CLASS_TV_ANALOG)
338 return i2c_probe(adap, &addr_data, tuner_attach);
342 static int tuner_detach(struct i2c_client *client)
344 struct tuner *t = i2c_get_clientdata(client);
347 err=i2c_detach_client(&t->i2c);
349 tuner_warn ("Client deregistration failed, client not detached.\n");
357 #define SWITCH_V4L2 if (!t->using_v4l2 && tuner_debug) \
358 tuner_info("switching to v4l2\n"); \
360 #define CHECK_V4L2 if (t->using_v4l2) { if (tuner_debug) \
361 tuner_info("ignore v4l1 call\n"); \
365 tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
367 struct tuner *t = i2c_get_clientdata(client);
368 unsigned int *iarg = (int*)arg;
371 /* --- configuration --- */
373 set_type(client,*iarg);
375 case TUNER_SET_TYPE_ADDR:
376 set_addr(client,(struct tuner_addr *)arg);
379 t->mode = V4L2_TUNER_RADIO;
380 CHECK_ADDR(tv_tuner,"AUDC_SET_RADIO","TV");
382 if (V4L2_TUNER_RADIO != t->mode) {
383 set_tv_freq(client,400 * 16);
386 case AUDC_CONFIG_PINNACLE:
387 CHECK_ADDR(tv_tuner,"AUDC_CONFIG_PINNACLE","TV");
390 tuner_dbg("pinnacle pal\n");
391 t->radio_if2 = 33300 * 1000;
394 tuner_dbg("pinnacle ntsc\n");
395 t->radio_if2 = 41300 * 1000;
400 /* --- v4l ioctls --- */
401 /* take care: bttv does userspace copying, we'll get a
402 kernel pointer here... */
405 static const v4l2_std_id map[] = {
406 [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
407 [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
408 [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
409 [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
410 [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
411 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
413 struct video_channel *vc = arg;
416 t->mode = V4L2_TUNER_ANALOG_TV;
417 CHECK_ADDR(tv_tuner,"VIDIOCSCHAN","TV");
419 if (vc->norm < ARRAY_SIZE(map))
420 t->std = map[vc->norm];
423 set_tv_freq(client,t->freq);
428 unsigned long *v = arg;
430 CHECK_MODE("VIDIOCSFREQ");
437 struct video_tuner *vt = arg;
439 CHECK_ADDR(radio_tuner,"VIDIOCGTUNER","radio");
441 if (V4L2_TUNER_RADIO == t->mode) {
443 vt->signal = t->has_signal(client);
445 if (t->is_stereo(client))
446 vt-> flags |= VIDEO_TUNER_STEREO_ON;
448 vt-> flags &= 0xffff ^ VIDEO_TUNER_STEREO_ON;
450 vt->flags |= V4L2_TUNER_CAP_LOW; /* Allow freqs at 62.5 Hz */
457 struct video_audio *va = arg;
459 CHECK_ADDR(radio_tuner,"VIDIOCGAUDIO","radio");
461 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
462 va->mode = t->is_stereo(client)
470 v4l2_std_id *id = arg;
473 t->mode = V4L2_TUNER_ANALOG_TV;
474 CHECK_ADDR(tv_tuner,"VIDIOC_S_STD","TV");
479 set_freq(client,t->freq);
482 case VIDIOC_S_FREQUENCY:
484 struct v4l2_frequency *f = arg;
486 CHECK_MODE("VIDIOC_S_FREQUENCY");
488 if (V4L2_TUNER_RADIO == f->type &&
489 V4L2_TUNER_RADIO != t->mode)
490 set_tv_freq(client,400*16);
492 set_freq(client,f->frequency);
495 case VIDIOC_G_FREQUENCY:
497 struct v4l2_frequency *f = arg;
499 CHECK_MODE("VIDIOC_G_FREQUENCY");
502 f->frequency = t->freq;
507 struct v4l2_tuner *tuner = arg;
509 CHECK_MODE("VIDIOC_G_TUNER");
511 if (V4L2_TUNER_RADIO == t->mode) {
513 tuner -> signal = t->has_signal(client);
515 if (t->is_stereo(client)) {
516 tuner -> capability |= V4L2_TUNER_CAP_STEREO;
517 tuner -> rxsubchans |= V4L2_TUNER_SUB_STEREO;
519 tuner -> rxsubchans &= 0xffff ^ V4L2_TUNER_SUB_STEREO;
523 /* Wow to deal with V4L2_TUNER_CAP_LOW ? For now, it accepts from low at 62.5KHz step to high at 62.5 Hz */
524 tuner->rangelow = tv_range[0] * 16;
525 // tuner->rangehigh = tv_range[1] * 16;
526 // tuner->rangelow = tv_range[0] * 16384;
527 tuner->rangehigh = tv_range[1] * 16384;
531 tuner_dbg ("Unimplemented IOCTL 0x%08x called to tuner.\n", cmd);
539 static int tuner_suspend(struct device * dev, u32 state, u32 level)
541 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
542 struct tuner *t = i2c_get_clientdata(c);
544 tuner_dbg("suspend\n");
545 /* FIXME: power down ??? */
549 static int tuner_resume(struct device * dev, u32 level)
551 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
552 struct tuner *t = i2c_get_clientdata(c);
554 tuner_dbg("resume\n");
560 /* ----------------------------------------------------------------------- */
562 static struct i2c_driver driver = {
563 .owner = THIS_MODULE,
565 .id = I2C_DRIVERID_TUNER,
566 .flags = I2C_DF_NOTIFY,
567 .attach_adapter = tuner_probe,
568 .detach_client = tuner_detach,
569 .command = tuner_command,
571 .suspend = tuner_suspend,
572 .resume = tuner_resume,
575 static struct i2c_client client_template =
577 I2C_DEVNAME("(tuner unset)"),
578 .flags = I2C_CLIENT_ALLOW_USE,
582 static int __init tuner_init_module(void)
584 return i2c_add_driver(&driver);
587 static void __exit tuner_cleanup_module(void)
589 i2c_del_driver(&driver);
592 module_init(tuner_init_module);
593 module_exit(tuner_cleanup_module);
596 * Overrides for Emacs so that we follow Linus's tabbing style.
597 * ---------------------------------------------------------------------------