2 * $Id: tuner-core.c,v 1.29 2005/06/21 15:40:33 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
32 /* standard i2c insmod options */
33 static unsigned short normal_i2c[] = {
35 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
36 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
41 /* insmod options used at init time => read/only */
42 static unsigned int addr = 0;
43 module_param(addr, int, 0444);
45 /* insmod options used at runtime => read/write */
46 unsigned int tuner_debug = 0;
47 module_param(tuner_debug, int, 0644);
49 static unsigned int tv_range[2] = { 44, 958 };
50 static unsigned int radio_range[2] = { 65, 108 };
52 module_param_array(tv_range, int, NULL, 0644);
53 module_param_array(radio_range, int, NULL, 0644);
55 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
56 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
57 MODULE_LICENSE("GPL");
60 static unsigned short first_tuner, tv_tuner, radio_tuner;
62 static struct i2c_driver driver;
63 static struct i2c_client client_template;
65 /* ---------------------------------------------------------------------- */
67 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
68 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
70 struct tuner *t = i2c_get_clientdata(c);
72 if (t->type == UNSET) {
73 tuner_info("tuner type not set\n");
76 if (NULL == t->tv_freq) {
77 tuner_info("Huh? tv_set is NULL?\n");
80 if (freq < tv_range[0]*16 || freq > tv_range[1]*16) {
81 tuner_info("TV freq (%d.%02d) out of range (%d-%d)\n",
82 freq/16,freq%16*100/16,tv_range[0],tv_range[1]);
87 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
89 struct tuner *t = i2c_get_clientdata(c);
91 if (t->type == UNSET) {
92 tuner_info("tuner type not set\n");
95 if (NULL == t->radio_freq) {
96 tuner_info("no radio tuning for this one, sorry.\n");
99 if (freq >= radio_range[0]*16000 && freq <= radio_range[1]*16000) {
101 tuner_info("radio freq step 62.5Hz (%d.%06d)\n",
102 freq/16000,freq%16000*1000/16);
103 t->radio_freq(c,freq);
105 tuner_info("radio freq (%d.%02d) out of range (%d-%d)\n",
106 freq/16,freq%16*100/16,
107 radio_range[0],radio_range[1]);
113 static void set_freq(struct i2c_client *c, unsigned long freq)
115 struct tuner *t = i2c_get_clientdata(c);
118 case V4L2_TUNER_RADIO:
119 tuner_dbg("radio freq set to %lu.%02lu\n",
120 freq/16,freq%16*100/16);
121 set_radio_freq(c,freq);
123 case V4L2_TUNER_ANALOG_TV:
124 case V4L2_TUNER_DIGITAL_TV:
125 tuner_dbg("tv freq set to %lu.%02lu\n",
126 freq/16,freq%16*100/16);
127 set_tv_freq(c, freq);
133 static void set_type(struct i2c_client *c, unsigned int type)
135 struct tuner *t = i2c_get_clientdata(c);
136 unsigned char buffer[4];
139 if (type == UNSET || type == TUNER_ABSENT)
141 if (type >= tuner_count)
144 if (NULL == t->i2c.dev.driver) {
145 /* not registered yet */
149 if ((t->initialized) && (t->type == type))
150 /* run only once except type change Hac 04/05*/
160 case TUNER_PHILIPS_TDA8290:
164 if (tea5767_tuner_init(c)==EINVAL) t->type=TUNER_ABSENT;
166 case TUNER_PHILIPS_FMD1216ME_MK3:
171 i2c_master_send(c,buffer,4);
175 i2c_master_send(c,buffer,4);
176 default_tuner_init(c);
179 /* TEA5767 autodetection code */
180 if (tea5767_tuner_init(c)!=EINVAL) {
181 t->type = TUNER_TEA5767;
182 if (first_tuner == 0x60)
187 default_tuner_init(c);
190 tuner_dbg ("I2C addr 0x%02x with type %d\n",c->addr<<1,type);
193 #define CHECK_ADDR(tp,cmd,tun) if (client->addr!=tp) { \
194 return 0; } else if (tuner_debug) \
195 tuner_info ("Cmd %s accepted to "tun"\n",cmd);
196 #define CHECK_MODE(cmd) if (t->mode == V4L2_TUNER_RADIO) { \
197 CHECK_ADDR(radio_tuner,cmd,"radio") } else \
198 { CHECK_ADDR(tv_tuner,cmd,"TV"); }
200 static void set_addr(struct i2c_client *c, struct tuner_addr *tun_addr)
202 /* ADDR_UNSET defaults to first available tuner */
203 if ( tun_addr->addr == ADDR_UNSET ) {
204 if (first_tuner != c->addr)
206 switch (tun_addr->v4l2_tuner) {
207 case V4L2_TUNER_RADIO:
215 /* Sets tuner to its configured value */
216 switch (tun_addr->v4l2_tuner) {
217 case V4L2_TUNER_RADIO:
218 radio_tuner=tun_addr->addr;
219 if ( tun_addr->addr == c->addr ) set_type(c,tun_addr->type);
222 tv_tuner=tun_addr->addr;
223 if ( tun_addr->addr == c->addr ) set_type(c,tun_addr->type);
227 set_type(c,tun_addr->type);
230 static char pal[] = "-";
231 module_param_string(pal, pal, sizeof(pal), 0644);
233 static int tuner_fixup_std(struct tuner *t)
235 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
236 /* get more precise norm info from insmod option */
242 tuner_dbg("insmod fixup: PAL => PAL-BG\n");
243 t->std = V4L2_STD_PAL_BG;
247 tuner_dbg("insmod fixup: PAL => PAL-I\n");
248 t->std = V4L2_STD_PAL_I;
254 tuner_dbg("insmod fixup: PAL => PAL-DK\n");
255 t->std = V4L2_STD_PAL_DK;
262 /* ---------------------------------------------------------------------- */
264 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
268 /* by default, first I2C card is both tv and radio tuner */
269 if (this_adap == 0) {
276 client_template.adapter = adap;
277 client_template.addr = addr;
279 t = kmalloc(sizeof(struct tuner),GFP_KERNEL);
282 memset(t,0,sizeof(struct tuner));
283 memcpy(&t->i2c,&client_template,sizeof(struct i2c_client));
284 i2c_set_clientdata(&t->i2c, t);
286 t->radio_if2 = 10700*1000; /* 10.7MHz - FM radio */
287 t->audmode = V4L2_TUNER_MODE_STEREO;
289 i2c_attach_client(&t->i2c);
290 tuner_info("chip found @ 0x%x (%s)\n",
291 addr << 1, adap->name);
293 set_type(&t->i2c, t->type);
297 static int tuner_probe(struct i2c_adapter *adap)
300 normal_i2c[0] = addr;
301 normal_i2c[1] = I2C_CLIENT_END;
309 if (adap->class & I2C_CLASS_TV_ANALOG)
310 return i2c_probe(adap, &addr_data, tuner_attach);
314 static int tuner_detach(struct i2c_client *client)
316 struct tuner *t = i2c_get_clientdata(client);
319 err=i2c_detach_client(&t->i2c);
321 tuner_warn ("Client deregistration failed, client not detached.\n");
329 #define SWITCH_V4L2 if (!t->using_v4l2 && tuner_debug) \
330 tuner_info("switching to v4l2\n"); \
332 #define CHECK_V4L2 if (t->using_v4l2) { if (tuner_debug) \
333 tuner_info("ignore v4l1 call\n"); \
337 tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
339 struct tuner *t = i2c_get_clientdata(client);
340 unsigned int *iarg = (int*)arg;
343 /* --- configuration --- */
345 set_type(client,*iarg);
347 case TUNER_SET_TYPE_ADDR:
348 set_addr(client,(struct tuner_addr *)arg);
351 t->mode = V4L2_TUNER_RADIO;
352 CHECK_ADDR(tv_tuner,"AUDC_SET_RADIO","TV");
354 if (V4L2_TUNER_RADIO != t->mode) {
355 set_tv_freq(client,400 * 16);
358 case AUDC_CONFIG_PINNACLE:
359 CHECK_ADDR(tv_tuner,"AUDC_CONFIG_PINNACLE","TV");
362 tuner_dbg("pinnacle pal\n");
363 t->radio_if2 = 33300 * 1000;
366 tuner_dbg("pinnacle ntsc\n");
367 t->radio_if2 = 41300 * 1000;
371 /* --- v4l ioctls --- */
372 /* take care: bttv does userspace copying, we'll get a
373 kernel pointer here... */
376 static const v4l2_std_id map[] = {
377 [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
378 [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
379 [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
380 [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
381 [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
382 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
384 struct video_channel *vc = arg;
387 t->mode = V4L2_TUNER_ANALOG_TV;
388 CHECK_ADDR(tv_tuner,"VIDIOCSCHAN","TV");
390 if (vc->norm < ARRAY_SIZE(map))
391 t->std = map[vc->norm];
394 set_tv_freq(client,t->freq);
399 unsigned long *v = arg;
401 CHECK_MODE("VIDIOCSFREQ");
408 struct video_tuner *vt = arg;
410 CHECK_ADDR(radio_tuner,"VIDIOCGTUNER","radio");
412 if (V4L2_TUNER_RADIO == t->mode) {
414 vt->signal = t->has_signal(client);
416 if (t->is_stereo(client))
417 vt->flags |= VIDEO_TUNER_STEREO_ON;
419 vt->flags &= ~VIDEO_TUNER_STEREO_ON;
421 vt->flags |= V4L2_TUNER_CAP_LOW; /* Allow freqs at 62.5 Hz */
423 vt->rangelow = radio_range[0] * 16000;
424 vt->rangehigh = radio_range[1] * 16000;
427 vt->rangelow = tv_range[0] * 16;
428 vt->rangehigh = tv_range[1] * 16;
435 struct video_audio *va = arg;
437 CHECK_ADDR(radio_tuner,"VIDIOCGAUDIO","radio");
439 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
440 va->mode = t->is_stereo(client)
448 v4l2_std_id *id = arg;
451 t->mode = V4L2_TUNER_ANALOG_TV;
452 CHECK_ADDR(tv_tuner,"VIDIOC_S_STD","TV");
457 set_freq(client,t->freq);
460 case VIDIOC_S_FREQUENCY:
462 struct v4l2_frequency *f = arg;
464 CHECK_MODE("VIDIOC_S_FREQUENCY");
466 if (V4L2_TUNER_RADIO == f->type &&
467 V4L2_TUNER_RADIO != t->mode)
468 set_tv_freq(client,400*16);
470 set_freq(client,f->frequency);
473 case VIDIOC_G_FREQUENCY:
475 struct v4l2_frequency *f = arg;
477 CHECK_MODE("VIDIOC_G_FREQUENCY");
480 f->frequency = t->freq;
485 struct v4l2_tuner *tuner = arg;
487 CHECK_MODE("VIDIOC_G_TUNER");
489 if (V4L2_TUNER_RADIO == t->mode) {
491 tuner -> signal = t->has_signal(client);
493 if (t->is_stereo(client)) {
494 tuner -> rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
496 tuner -> rxsubchans = V4L2_TUNER_SUB_MONO;
499 tuner->capability |= V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
500 tuner->audmode = t->audmode;
502 tuner->rangelow = radio_range[0] * 16000;
503 tuner->rangehigh = radio_range[1] * 16000;
505 tuner->rangelow = tv_range[0] * 16;
506 tuner->rangehigh = tv_range[1] * 16;
510 case VIDIOC_S_TUNER: /* Allow changing radio range and audio mode */
512 struct v4l2_tuner *tuner = arg;
514 CHECK_ADDR(radio_tuner,"VIDIOC_S_TUNER","radio");
517 /* To switch the audio mode, applications initialize the
518 index and audmode fields and the reserved array and
519 call the VIDIOC_S_TUNER ioctl. */
520 /* rxsubchannels: V4L2_TUNER_MODE_MONO, V4L2_TUNER_MODE_STEREO,
521 V4L2_TUNER_MODE_LANG1, V4L2_TUNER_MODE_LANG2,
522 V4L2_TUNER_MODE_SAP */
524 if (tuner->audmode == V4L2_TUNER_MODE_MONO)
525 t->audmode = V4L2_TUNER_MODE_MONO;
527 t->audmode = V4L2_TUNER_MODE_STEREO;
529 set_radio_freq(client, t->freq);
532 case TDA9887_SET_CONFIG: /* Nothing to do on tuner-core */
535 tuner_dbg ("Unimplemented IOCTL 0x%08x called to tuner.\n", cmd);
543 static int tuner_suspend(struct device * dev, u32 state, u32 level)
545 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
546 struct tuner *t = i2c_get_clientdata(c);
548 tuner_dbg("suspend\n");
549 /* FIXME: power down ??? */
553 static int tuner_resume(struct device * dev, u32 level)
555 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
556 struct tuner *t = i2c_get_clientdata(c);
558 tuner_dbg("resume\n");
564 /* ----------------------------------------------------------------------- */
566 static struct i2c_driver driver = {
567 .owner = THIS_MODULE,
569 .id = I2C_DRIVERID_TUNER,
570 .flags = I2C_DF_NOTIFY,
571 .attach_adapter = tuner_probe,
572 .detach_client = tuner_detach,
573 .command = tuner_command,
575 .suspend = tuner_suspend,
576 .resume = tuner_resume,
579 static struct i2c_client client_template =
581 I2C_DEVNAME("(tuner unset)"),
582 .flags = I2C_CLIENT_ALLOW_USE,
586 static int __init tuner_init_module(void)
588 return i2c_add_driver(&driver);
591 static void __exit tuner_cleanup_module(void)
593 i2c_del_driver(&driver);
596 module_init(tuner_init_module);
597 module_exit(tuner_cleanup_module);
600 * Overrides for Emacs so that we follow Linus's tabbing style.
601 * ---------------------------------------------------------------------------