2 * $Id: tuner-core.c,v 1.5 2005/02/15 15:59:35 kraxel 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>
28 /* standard i2c insmod options */
29 static unsigned short normal_i2c[] = {
31 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
32 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
37 /* insmod options used at init time => read/only */
38 static unsigned int addr = 0;
39 module_param(addr, int, 0444);
41 /* insmod options used at runtime => read/write */
42 unsigned int tuner_debug = 0;
43 module_param(tuner_debug, int, 0644);
45 static unsigned int tv_range[2] = { 44, 958 };
46 static unsigned int radio_range[2] = { 65, 108 };
48 module_param_array(tv_range, int, NULL, 0644);
49 module_param_array(radio_range, int, NULL, 0644);
51 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
52 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
53 MODULE_LICENSE("GPL");
57 static struct i2c_driver driver;
58 static struct i2c_client client_template;
60 /* ---------------------------------------------------------------------- */
62 // Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz
63 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
65 struct tuner *t = i2c_get_clientdata(c);
67 if (t->type == UNSET) {
68 tuner_info("tuner type not set\n");
71 if (NULL == t->tv_freq) {
72 tuner_info("Huh? tv_set is NULL?\n");
75 if (freq < tv_range[0]*16 || freq > tv_range[1]*16) {
76 /* FIXME: better do that chip-specific, but
77 right now we don't have that in the config
78 struct and this way is still better than no
80 tuner_info("TV freq (%d.%02d) out of range (%d-%d)\n",
81 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]*16 || freq > radio_range[1]*16) {
100 tuner_info("radio freq (%d.%02d) out of range (%d-%d)\n",
101 freq/16,freq%16*100/16,
102 radio_range[0],radio_range[1]);
105 t->radio_freq(c,freq);
108 static void set_freq(struct i2c_client *c, unsigned long freq)
110 struct tuner *t = i2c_get_clientdata(c);
113 case V4L2_TUNER_RADIO:
114 tuner_dbg("radio freq set to %lu.%02lu\n",
115 freq/16,freq%16*100/16);
116 set_radio_freq(c,freq);
118 case V4L2_TUNER_ANALOG_TV:
119 case V4L2_TUNER_DIGITAL_TV:
120 tuner_dbg("tv freq set to %lu.%02lu\n",
121 freq/16,freq%16*100/16);
122 set_tv_freq(c, freq);
128 static void set_type(struct i2c_client *c, unsigned int type)
130 struct tuner *t = i2c_get_clientdata(c);
133 if (type == UNSET || type == TUNER_ABSENT)
135 if (type >= tuner_count)
138 if (NULL == t->i2c.dev.driver) {
139 /* not registered yet */
153 case TUNER_PHILIPS_TDA8290:
157 default_tuner_init(c);
162 static char pal[] = "-";
163 module_param_string(pal, pal, sizeof(pal), 0644);
165 static int tuner_fixup_std(struct tuner *t)
167 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
168 /* get more precise norm info from insmod option */
174 tuner_dbg("insmod fixup: PAL => PAL-BG\n");
175 t->std = V4L2_STD_PAL_BG;
179 tuner_dbg("insmod fixup: PAL => PAL-I\n");
180 t->std = V4L2_STD_PAL_I;
186 tuner_dbg("insmod fixup: PAL => PAL-DK\n");
187 t->std = V4L2_STD_PAL_DK;
194 /* ---------------------------------------------------------------------- */
196 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
204 client_template.adapter = adap;
205 client_template.addr = addr;
207 t = kmalloc(sizeof(struct tuner),GFP_KERNEL);
210 memset(t,0,sizeof(struct tuner));
211 memcpy(&t->i2c,&client_template,sizeof(struct i2c_client));
212 i2c_set_clientdata(&t->i2c, t);
214 t->radio_if2 = 10700*1000; // 10.7MHz - FM radio
216 i2c_attach_client(&t->i2c);
217 tuner_info("chip found @ 0x%x (%s)\n",
218 addr << 1, adap->name);
219 set_type(&t->i2c, t->type);
223 static int tuner_probe(struct i2c_adapter *adap)
226 normal_i2c[0] = addr;
227 normal_i2c[1] = I2C_CLIENT_END;
231 if (adap->class & I2C_CLASS_TV_ANALOG)
232 return i2c_probe(adap, &addr_data, tuner_attach);
236 static int tuner_detach(struct i2c_client *client)
238 struct tuner *t = i2c_get_clientdata(client);
240 i2c_detach_client(&t->i2c);
245 #define SWITCH_V4L2 if (!t->using_v4l2 && tuner_debug) \
246 tuner_info("switching to v4l2\n"); \
248 #define CHECK_V4L2 if (t->using_v4l2) { if (tuner_debug) \
249 tuner_info("ignore v4l1 call\n"); \
253 tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
255 struct tuner *t = i2c_get_clientdata(client);
256 unsigned int *iarg = (int*)arg;
260 /* --- configuration --- */
262 set_type(client,*iarg);
265 if (V4L2_TUNER_RADIO != t->mode) {
266 set_tv_freq(client,400 * 16);
267 t->mode = V4L2_TUNER_RADIO;
270 case AUDC_CONFIG_PINNACLE:
273 tuner_dbg("pinnacle pal\n");
274 t->radio_if2 = 33300 * 1000;
277 tuner_dbg("pinnacle ntsc\n");
278 t->radio_if2 = 41300 * 1000;
283 /* --- v4l ioctls --- */
284 /* take care: bttv does userspace copying, we'll get a
285 kernel pointer here... */
288 static const v4l2_std_id map[] = {
289 [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
290 [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
291 [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
292 [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
293 [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
294 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
296 struct video_channel *vc = arg;
299 t->mode = V4L2_TUNER_ANALOG_TV;
300 if (vc->norm < ARRAY_SIZE(map))
301 t->std = map[vc->norm];
304 set_tv_freq(client,t->freq);
309 unsigned long *v = arg;
317 struct video_tuner *vt = arg;
320 if (V4L2_TUNER_RADIO == t->mode && t->has_signal)
321 vt->signal = t->has_signal(client);
326 struct video_audio *va = arg;
329 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
330 va->mode = t->is_stereo(client)
338 v4l2_std_id *id = arg;
341 t->mode = V4L2_TUNER_ANALOG_TV;
345 set_freq(client,t->freq);
348 case VIDIOC_S_FREQUENCY:
350 struct v4l2_frequency *f = arg;
353 if (V4L2_TUNER_RADIO == f->type &&
354 V4L2_TUNER_RADIO != t->mode)
355 set_tv_freq(client,400*16);
357 set_freq(client,f->frequency);
360 case VIDIOC_G_FREQUENCY:
362 struct v4l2_frequency *f = arg;
366 f->frequency = t->freq;
371 struct v4l2_tuner *tuner = arg;
374 if (V4L2_TUNER_RADIO == t->mode && t->has_signal)
375 tuner->signal = t->has_signal(client);
376 tuner->rangelow = tv_range[0] * 16;
377 tuner->rangehigh = tv_range[1] * 16;
388 static int tuner_suspend(struct device * dev, pm_message_t state, u32 level)
390 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
391 struct tuner *t = i2c_get_clientdata(c);
393 tuner_dbg("suspend\n");
394 /* FIXME: power down ??? */
398 static int tuner_resume(struct device * dev, u32 level)
400 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
401 struct tuner *t = i2c_get_clientdata(c);
403 tuner_dbg("resume\n");
409 /* ----------------------------------------------------------------------- */
411 static struct i2c_driver driver = {
412 .owner = THIS_MODULE,
414 .id = I2C_DRIVERID_TUNER,
415 .flags = I2C_DF_NOTIFY,
416 .attach_adapter = tuner_probe,
417 .detach_client = tuner_detach,
418 .command = tuner_command,
420 .suspend = tuner_suspend,
421 .resume = tuner_resume,
424 static struct i2c_client client_template =
426 I2C_DEVNAME("(tuner unset)"),
427 .flags = I2C_CLIENT_ALLOW_USE,
431 static int __init tuner_init_module(void)
433 return i2c_add_driver(&driver);
436 static void __exit tuner_cleanup_module(void)
438 i2c_del_driver(&driver);
441 module_init(tuner_init_module);
442 module_exit(tuner_cleanup_module);
445 * Overrides for Emacs so that we follow Linus's tabbing style.
446 * ---------------------------------------------------------------------------