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[] = {
36 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
37 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
42 /* insmod options used at init time => read/only */
43 static unsigned int addr = 0;
44 module_param(addr, int, 0444);
46 /* insmod options used at runtime => read/write */
47 unsigned int tuner_debug = 0;
48 module_param(tuner_debug, int, 0644);
50 static unsigned int tv_range[2] = { 44, 958 };
51 static unsigned int radio_range[2] = { 65, 108 };
53 module_param_array(tv_range, int, NULL, 0644);
54 module_param_array(radio_range, int, NULL, 0644);
56 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
57 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
58 MODULE_LICENSE("GPL");
61 #ifdef CONFIG_TUNER_MULTI_I2C
62 static unsigned short first_tuner, tv_tuner, radio_tuner;
65 static struct i2c_driver driver;
66 static struct i2c_client client_template;
68 /* ---------------------------------------------------------------------- */
70 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
71 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
73 struct tuner *t = i2c_get_clientdata(c);
75 if (t->type == UNSET) {
76 tuner_info("tuner type not set\n");
79 if (NULL == t->tv_freq) {
80 tuner_info("Huh? tv_set is NULL?\n");
83 if (freq < tv_range[0]*16 || freq > tv_range[1]*16) {
85 if (freq >= tv_range[0]*16364 && freq <= tv_range[1]*16384) {
86 /* V4L2_TUNER_CAP_LOW frequency */
88 tuner_dbg("V4L2_TUNER_CAP_LOW freq selected for TV. Tuners yet doesn't support converting it to valid freq.\n");
90 t->tv_freq(c,freq>>10);
94 /* FIXME: better do that chip-specific, but
95 right now we don't have that in the config
96 struct and this way is still better than no
98 tuner_info("TV freq (%d.%02d) out of range (%d-%d)\n",
99 freq/16,freq%16*100/16,tv_range[0],tv_range[1]);
103 tuner_dbg("62.5 Khz freq step selected for TV.\n");
107 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
109 struct tuner *t = i2c_get_clientdata(c);
111 if (t->type == UNSET) {
112 tuner_info("tuner type not set\n");
115 if (NULL == t->radio_freq) {
116 tuner_info("no radio tuning for this one, sorry.\n");
119 if (freq < radio_range[0]*16 || freq > radio_range[1]*16) {
120 if (freq >= tv_range[0]*16364 && freq <= tv_range[1]*16384) {
121 /* V4L2_TUNER_CAP_LOW frequency */
122 if (t->type == TUNER_TEA5767) {
123 tuner_info("radio freq step 62.5Hz (%d.%06d)\n",(freq>>14),freq%(1<<14)*10000);
124 t->radio_freq(c,freq>>10);
128 tuner_dbg("V4L2_TUNER_CAP_LOW freq selected for Radio. Tuners yet doesn't support converting it to valid freq.\n");
130 tuner_info("radio freq (%d.%06d)\n",(freq>>14),freq%(1<<14)*10000);
132 t->radio_freq(c,freq>>10);
136 tuner_info("radio freq (%d.%02d) out of range (%d-%d)\n",
137 freq/16,freq%16*100/16,
138 radio_range[0],radio_range[1]);
142 tuner_dbg("62.5 Khz freq step selected for Radio.\n");
143 t->radio_freq(c,freq);
146 static void set_freq(struct i2c_client *c, unsigned long freq)
148 struct tuner *t = i2c_get_clientdata(c);
151 case V4L2_TUNER_RADIO:
152 tuner_dbg("radio freq set to %lu.%02lu\n",
153 freq/16,freq%16*100/16);
154 set_radio_freq(c,freq);
156 case V4L2_TUNER_ANALOG_TV:
157 case V4L2_TUNER_DIGITAL_TV:
158 tuner_dbg("tv freq set to %lu.%02lu\n",
159 freq/16,freq%16*100/16);
160 set_tv_freq(c, freq);
166 static void set_type(struct i2c_client *c, unsigned int type)
168 struct tuner *t = i2c_get_clientdata(c);
170 tuner_dbg ("I2C addr 0x%02x with type %d\n",c->addr<<1,type);
172 if (type == UNSET || type == TUNER_ABSENT)
174 if (type >= tuner_count)
177 if (NULL == t->i2c.dev.driver) {
178 /* not registered yet */
193 case TUNER_PHILIPS_TDA8290:
197 default_tuner_init(c);
202 #ifdef CONFIG_TUNER_MULTI_I2C
203 #define CHECK_ADDR(tp,cmd,tun) if (client->addr!=tp) { \
205 tuner_info ("Cmd %s accepted to "tun"\n",cmd);
206 #define CHECK_MODE(cmd) if (t->mode == V4L2_TUNER_RADIO) { \
207 CHECK_ADDR(radio_tuner,cmd,"radio") } else \
208 { CHECK_ADDR(tv_tuner,cmd,"TV"); }
210 #define CHECK_ADDR(tp,cmd,tun) tuner_info ("Cmd %s accepted to "tun"\n",cmd);
211 #define CHECK_MODE(cmd) tuner_info ("Cmd %s accepted\n",cmd);
214 #ifdef CONFIG_TUNER_MULTI_I2C
216 static void set_addr(struct i2c_client *c, struct tuner_addr *tun_addr)
218 /* ADDR_UNSET defaults to first available tuner */
219 if ( tun_addr->addr == ADDR_UNSET ) {
220 if (first_tuner != c->addr)
222 switch (tun_addr->v4l2_tuner) {
223 case V4L2_TUNER_RADIO:
231 /* Sets tuner to its configured value */
232 switch (tun_addr->v4l2_tuner) {
233 case V4L2_TUNER_RADIO:
234 radio_tuner=tun_addr->addr;
235 if ( tun_addr->addr == c->addr ) set_type(c,tun_addr->type);
238 tv_tuner=tun_addr->addr;
239 if ( tun_addr->addr == c->addr ) set_type(c,tun_addr->type);
243 set_type(c,tun_addr->type);
246 #define set_addr(c,tun_addr) set_type(c,(tun_addr)->type)
249 static char pal[] = "-";
250 module_param_string(pal, pal, sizeof(pal), 0644);
252 static int tuner_fixup_std(struct tuner *t)
254 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
255 /* get more precise norm info from insmod option */
261 tuner_dbg("insmod fixup: PAL => PAL-BG\n");
262 t->std = V4L2_STD_PAL_BG;
266 tuner_dbg("insmod fixup: PAL => PAL-I\n");
267 t->std = V4L2_STD_PAL_I;
273 tuner_dbg("insmod fixup: PAL => PAL-DK\n");
274 t->std = V4L2_STD_PAL_DK;
281 /* ---------------------------------------------------------------------- */
283 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
287 #ifndef CONFIG_TUNER_MULTI_I2C
291 /* by default, first I2C card is both tv and radio tuner */
292 if (this_adap == 0) {
300 client_template.adapter = adap;
301 client_template.addr = addr;
303 t = kmalloc(sizeof(struct tuner),GFP_KERNEL);
306 memset(t,0,sizeof(struct tuner));
307 memcpy(&t->i2c,&client_template,sizeof(struct i2c_client));
308 i2c_set_clientdata(&t->i2c, t);
310 t->radio_if2 = 10700*1000; /* 10.7MHz - FM radio */
312 i2c_attach_client(&t->i2c);
313 tuner_info("chip found @ 0x%x (%s)\n",
314 addr << 1, adap->name);
316 set_type(&t->i2c, t->type);
320 static int tuner_probe(struct i2c_adapter *adap)
323 normal_i2c[0] = addr;
324 normal_i2c[1] = I2C_CLIENT_END;
328 #ifdef CONFIG_TUNER_MULTI_I2C
334 if (adap->class & I2C_CLASS_TV_ANALOG)
335 return i2c_probe(adap, &addr_data, tuner_attach);
339 static int tuner_detach(struct i2c_client *client)
341 struct tuner *t = i2c_get_clientdata(client);
344 err=i2c_detach_client(&t->i2c);
346 tuner_warn ("Client deregistration failed, client not detached.\n");
354 #define SWITCH_V4L2 if (!t->using_v4l2 && tuner_debug) \
355 tuner_info("switching to v4l2\n"); \
357 #define CHECK_V4L2 if (t->using_v4l2) { if (tuner_debug) \
358 tuner_info("ignore v4l1 call\n"); \
362 tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
364 struct tuner *t = i2c_get_clientdata(client);
365 unsigned int *iarg = (int*)arg;
368 /* --- configuration --- */
370 set_type(client,*iarg);
372 case TUNER_SET_TYPE_ADDR:
373 set_addr(client,(struct tuner_addr *)arg);
376 t->mode = V4L2_TUNER_RADIO;
377 CHECK_ADDR(tv_tuner,"AUDC_SET_RADIO","TV");
379 if (V4L2_TUNER_RADIO != t->mode) {
380 set_tv_freq(client,400 * 16);
383 case AUDC_CONFIG_PINNACLE:
384 CHECK_ADDR(tv_tuner,"AUDC_CONFIG_PINNACLE","TV");
387 tuner_dbg("pinnacle pal\n");
388 t->radio_if2 = 33300 * 1000;
391 tuner_dbg("pinnacle ntsc\n");
392 t->radio_if2 = 41300 * 1000;
397 /* --- v4l ioctls --- */
398 /* take care: bttv does userspace copying, we'll get a
399 kernel pointer here... */
402 static const v4l2_std_id map[] = {
403 [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
404 [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
405 [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
406 [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
407 [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
408 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
410 struct video_channel *vc = arg;
413 t->mode = V4L2_TUNER_ANALOG_TV;
414 CHECK_ADDR(tv_tuner,"VIDIOCSCHAN","TV");
416 if (vc->norm < ARRAY_SIZE(map))
417 t->std = map[vc->norm];
420 set_tv_freq(client,t->freq);
425 unsigned long *v = arg;
427 CHECK_MODE("VIDIOCSFREQ");
434 struct video_tuner *vt = arg;
436 CHECK_ADDR(radio_tuner,"VIDIOCGTUNER","radio");
438 if (V4L2_TUNER_RADIO == t->mode) {
440 vt->signal = t->has_signal(client);
442 if (t->is_stereo(client))
443 vt-> flags |= VIDEO_TUNER_STEREO_ON;
445 vt-> flags &= 0xffff ^ VIDEO_TUNER_STEREO_ON;
447 vt->flags |= V4L2_TUNER_CAP_LOW; /* Allow freqs at 62.5 Hz */
454 struct video_audio *va = arg;
456 CHECK_ADDR(radio_tuner,"VIDIOCGAUDIO","radio");
458 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
459 va->mode = t->is_stereo(client)
467 v4l2_std_id *id = arg;
470 t->mode = V4L2_TUNER_ANALOG_TV;
471 CHECK_ADDR(tv_tuner,"VIDIOC_S_STD","TV");
476 set_freq(client,t->freq);
479 case VIDIOC_S_FREQUENCY:
481 struct v4l2_frequency *f = arg;
483 CHECK_MODE("VIDIOC_S_FREQUENCY");
485 if (V4L2_TUNER_RADIO == f->type &&
486 V4L2_TUNER_RADIO != t->mode)
487 set_tv_freq(client,400*16);
489 set_freq(client,f->frequency);
492 case VIDIOC_G_FREQUENCY:
494 struct v4l2_frequency *f = arg;
496 CHECK_MODE("VIDIOC_G_FREQUENCY");
499 f->frequency = t->freq;
504 struct v4l2_tuner *tuner = arg;
506 CHECK_MODE("VIDIOC_G_TUNER");
508 if (V4L2_TUNER_RADIO == t->mode) {
510 tuner -> signal = t->has_signal(client);
512 if (t->is_stereo(client)) {
513 tuner -> capability |= V4L2_TUNER_CAP_STEREO;
514 tuner -> rxsubchans |= V4L2_TUNER_SUB_STEREO;
516 tuner -> rxsubchans &= 0xffff ^ V4L2_TUNER_SUB_STEREO;
520 /* Wow to deal with V4L2_TUNER_CAP_LOW ? For now, it accepts from low at 62.5KHz step to high at 62.5 Hz */
521 tuner->rangelow = tv_range[0] * 16;
522 // tuner->rangehigh = tv_range[1] * 16;
523 // tuner->rangelow = tv_range[0] * 16384;
524 tuner->rangehigh = tv_range[1] * 16384;
528 tuner_dbg ("Unimplemented IOCTL 0x%08x called to tuner.\n", cmd);
536 static int tuner_suspend(struct device * dev, u32 state, u32 level)
538 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
539 struct tuner *t = i2c_get_clientdata(c);
541 tuner_dbg("suspend\n");
542 /* FIXME: power down ??? */
546 static int tuner_resume(struct device * dev, u32 level)
548 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
549 struct tuner *t = i2c_get_clientdata(c);
551 tuner_dbg("resume\n");
557 /* ----------------------------------------------------------------------- */
559 static struct i2c_driver driver = {
560 .owner = THIS_MODULE,
562 .id = I2C_DRIVERID_TUNER,
563 .flags = I2C_DF_NOTIFY,
564 .attach_adapter = tuner_probe,
565 .detach_client = tuner_detach,
566 .command = tuner_command,
568 .suspend = tuner_suspend,
569 .resume = tuner_resume,
572 static struct i2c_client client_template =
574 I2C_DEVNAME("(tuner unset)"),
575 .flags = I2C_CLIENT_ALLOW_USE,
579 static int __init tuner_init_module(void)
581 return i2c_add_driver(&driver);
584 static void __exit tuner_cleanup_module(void)
586 i2c_del_driver(&driver);
589 module_init(tuner_init_module);
590 module_exit(tuner_cleanup_module);
593 * Overrides for Emacs so that we follow Linus's tabbing style.
594 * ---------------------------------------------------------------------------