2 * $Id: tuner-core.c,v 1.7 2005/05/30 02:02:47 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 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) {
84 /* FIXME: better do that chip-specific, but
85 right now we don't have that in the config
86 struct and this way is still better than no
88 tuner_info("TV freq (%d.%02d) out of range (%d-%d)\n",
89 freq/16,freq%16*100/16,tv_range[0],tv_range[1]);
95 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
97 struct tuner *t = i2c_get_clientdata(c);
99 if (t->type == UNSET) {
100 tuner_info("tuner type not set\n");
103 if (NULL == t->radio_freq) {
104 tuner_info("no radio tuning for this one, sorry.\n");
107 if (freq < radio_range[0]*16 || freq > radio_range[1]*16) {
108 tuner_info("radio freq (%d.%02d) out of range (%d-%d)\n",
109 freq/16,freq%16*100/16,
110 radio_range[0],radio_range[1]);
113 t->radio_freq(c,freq);
116 static void set_freq(struct i2c_client *c, unsigned long freq)
118 struct tuner *t = i2c_get_clientdata(c);
121 case V4L2_TUNER_RADIO:
122 tuner_dbg("radio freq set to %lu.%02lu\n",
123 freq/16,freq%16*100/16);
124 set_radio_freq(c,freq);
126 case V4L2_TUNER_ANALOG_TV:
127 case V4L2_TUNER_DIGITAL_TV:
128 tuner_dbg("tv freq set to %lu.%02lu\n",
129 freq/16,freq%16*100/16);
130 set_tv_freq(c, freq);
136 #ifdef CONFIG_TUNER_MULTI_I2C
137 static void set_addr(struct i2c_client *c, struct tuner_addr *tun_addr)
139 struct tuner *t = i2c_get_clientdata(c);
141 switch (tun_addr->type) {
142 case V4L2_TUNER_RADIO:
143 radio_tuner=tun_addr->addr;
144 tuner_dbg("radio tuner set to I2C address 0x%02x\n",radio_tuner<<1);
148 tv_tuner=tun_addr->addr;
149 tuner_dbg("TV tuner set to I2C address 0x%02x\n",tv_tuner<<1);
154 #define set_addr(c,tun_addr) \
155 tuner_warn("It is recommended to enable CONFIG_TUNER_MULTI_I2C for this card.\n");
158 static void set_type(struct i2c_client *c, unsigned int type)
160 struct tuner *t = i2c_get_clientdata(c);
163 if (type == UNSET || type == TUNER_ABSENT)
165 if (type >= tuner_count)
168 if (NULL == t->i2c.dev.driver) {
169 /* not registered yet */
183 case TUNER_PHILIPS_TDA8290:
187 default_tuner_init(c);
192 static char pal[] = "-";
193 module_param_string(pal, pal, sizeof(pal), 0644);
195 static int tuner_fixup_std(struct tuner *t)
197 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
198 /* get more precise norm info from insmod option */
204 tuner_dbg("insmod fixup: PAL => PAL-BG\n");
205 t->std = V4L2_STD_PAL_BG;
209 tuner_dbg("insmod fixup: PAL => PAL-I\n");
210 t->std = V4L2_STD_PAL_I;
216 tuner_dbg("insmod fixup: PAL => PAL-DK\n");
217 t->std = V4L2_STD_PAL_DK;
224 /* ---------------------------------------------------------------------- */
226 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
230 #ifndef CONFIG_TUNER_MULTI_I2C
234 /* by default, first I2C card is both tv and radio tuner */
235 if (this_adap == 0) {
242 client_template.adapter = adap;
243 client_template.addr = addr;
245 t = kmalloc(sizeof(struct tuner),GFP_KERNEL);
248 memset(t,0,sizeof(struct tuner));
249 memcpy(&t->i2c,&client_template,sizeof(struct i2c_client));
250 i2c_set_clientdata(&t->i2c, t);
252 t->radio_if2 = 10700*1000; // 10.7MHz - FM radio
254 i2c_attach_client(&t->i2c);
255 tuner_info("chip found @ 0x%x (%s)\n",
256 addr << 1, adap->name);
257 set_type(&t->i2c, t->type);
261 static int tuner_probe(struct i2c_adapter *adap)
264 normal_i2c[0] = addr;
265 normal_i2c[1] = I2C_CLIENT_END;
269 #ifdef CONFIG_TUNER_MULTI_I2C
274 if (adap->class & I2C_CLASS_TV_ANALOG)
275 return i2c_probe(adap, &addr_data, tuner_attach);
279 static int tuner_detach(struct i2c_client *client)
281 struct tuner *t = i2c_get_clientdata(client);
284 err=i2c_detach_client(&t->i2c);
286 tuner_warn ("Client deregistration failed, client not detached.\n");
294 #define SWITCH_V4L2 if (!t->using_v4l2 && tuner_debug) \
295 tuner_info("switching to v4l2\n"); \
297 #define CHECK_V4L2 if (t->using_v4l2) { if (tuner_debug) \
298 tuner_info("ignore v4l1 call\n"); \
301 #ifdef CONFIG_TUNER_MULTI_I2C
302 #define CHECK_ADDR(tp,cmd) if (client->addr!=tp) { \
303 tuner_info ("Cmd %s to addr 0x%02x rejected.\n",cmd,client->addr<<1); \
305 #define CHECK_MODE(cmd) if (t->mode == V4L2_TUNER_RADIO) { \
306 CHECK_ADDR(radio_tuner,cmd) } else { CHECK_ADDR(tv_tuner,cmd); }
308 #define CHECK_ADDR(tp,cmd)
309 #define CHECK_MODE(cmd)
313 tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
315 struct tuner *t = i2c_get_clientdata(client);
316 unsigned int *iarg = (int*)arg;
319 /* --- configuration --- */
321 set_type(client,*iarg);
324 set_addr(client,(struct tuner_addr *)arg);
327 CHECK_ADDR(radio_tuner,"AUDC_SET_RADIO");
329 if (V4L2_TUNER_RADIO != t->mode) {
330 set_tv_freq(client,400 * 16);
331 t->mode = V4L2_TUNER_RADIO;
334 case AUDC_CONFIG_PINNACLE:
335 CHECK_ADDR(tv_tuner,"AUDC_CONFIG_PINNACLE");
338 tuner_dbg("pinnacle pal\n");
339 t->radio_if2 = 33300 * 1000;
342 tuner_dbg("pinnacle ntsc\n");
343 t->radio_if2 = 41300 * 1000;
348 /* --- v4l ioctls --- */
349 /* take care: bttv does userspace copying, we'll get a
350 kernel pointer here... */
353 static const v4l2_std_id map[] = {
354 [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
355 [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
356 [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
357 [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
358 [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
359 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
361 struct video_channel *vc = arg;
363 CHECK_ADDR(tv_tuner,"VIDIOCSCHAN");
365 t->mode = V4L2_TUNER_ANALOG_TV;
366 if (vc->norm < ARRAY_SIZE(map))
367 t->std = map[vc->norm];
370 set_tv_freq(client,t->freq);
375 unsigned long *v = arg;
377 CHECK_MODE("VIDIOCSFREQ");
384 struct video_tuner *vt = arg;
386 CHECK_ADDR(radio_tuner,"VIDIOCGTUNER:");
388 if (V4L2_TUNER_RADIO == t->mode && t->has_signal)
389 vt->signal = t->has_signal(client);
394 struct video_audio *va = arg;
396 CHECK_ADDR(radio_tuner,"VIDIOCGAUDIO");
398 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
399 va->mode = t->is_stereo(client)
407 v4l2_std_id *id = arg;
409 CHECK_ADDR(tv_tuner,"VIDIOC_S_STD");
411 t->mode = V4L2_TUNER_ANALOG_TV;
415 set_freq(client,t->freq);
418 case VIDIOC_S_FREQUENCY:
420 struct v4l2_frequency *f = arg;
422 CHECK_MODE("VIDIOC_S_FREQUENCY");
424 if (V4L2_TUNER_RADIO == f->type &&
425 V4L2_TUNER_RADIO != t->mode)
426 set_tv_freq(client,400*16);
428 set_freq(client,f->frequency);
431 case VIDIOC_G_FREQUENCY:
433 struct v4l2_frequency *f = arg;
435 CHECK_MODE("VIDIOC_G_FREQUENCY");
438 f->frequency = t->freq;
443 struct v4l2_tuner *tuner = arg;
445 CHECK_MODE("VIDIOC_G_TUNER");
447 if (V4L2_TUNER_RADIO == t->mode && t->has_signal)
448 tuner->signal = t->has_signal(client);
449 tuner->rangelow = tv_range[0] * 16;
450 tuner->rangehigh = tv_range[1] * 16;
461 static int tuner_suspend(struct device * dev, pm_message_t state, u32 level)
463 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
464 struct tuner *t = i2c_get_clientdata(c);
466 tuner_dbg("suspend\n");
467 /* FIXME: power down ??? */
471 static int tuner_resume(struct device * dev, u32 level)
473 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
474 struct tuner *t = i2c_get_clientdata(c);
476 tuner_dbg("resume\n");
482 /* ----------------------------------------------------------------------- */
484 static struct i2c_driver driver = {
485 .owner = THIS_MODULE,
487 .id = I2C_DRIVERID_TUNER,
488 .flags = I2C_DF_NOTIFY,
489 .attach_adapter = tuner_probe,
490 .detach_client = tuner_detach,
491 .command = tuner_command,
493 .suspend = tuner_suspend,
494 .resume = tuner_resume,
497 static struct i2c_client client_template =
499 I2C_DEVNAME("(tuner unset)"),
500 .flags = I2C_CLIENT_ALLOW_USE,
504 static int __init tuner_init_module(void)
506 return i2c_add_driver(&driver);
509 static void __exit tuner_cleanup_module(void)
511 i2c_del_driver(&driver);
514 module_init(tuner_init_module);
515 module_exit(tuner_cleanup_module);
518 * Overrides for Emacs so that we follow Linus's tabbing style.
519 * ---------------------------------------------------------------------------