[PATCH] I2C: Kill i2c_algorithm.id (6/7)
[linux-2.6/mini2440.git] / drivers / media / video / tuner-core.c
bloba155e99a263b5bfb26a006c42065da2017fda76c
1 /*
2 * $Id: tuner-core.c,v 1.63 2005/07/28 18:19:55 mchehab Exp $
4 * i2c tv tuner chip device driver
5 * core core, i.e. kernel interfaces, registering and so on
6 */
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>
26 #include "msp3400.h"
28 #define UNSET (-1U)
30 /* standard i2c insmod options */
31 static unsigned short normal_i2c[] = {
32 0x4b, /* tda8290 */
33 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
34 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
35 I2C_CLIENT_END
38 I2C_CLIENT_INSMOD;
40 /* insmod options used at init time => read/only */
41 static unsigned int addr = 0;
42 module_param(addr, int, 0444);
44 static unsigned int no_autodetect = 0;
45 module_param(no_autodetect, int, 0444);
47 static unsigned int show_i2c = 0;
48 module_param(show_i2c, int, 0444);
50 /* insmod options used at runtime => read/write */
51 unsigned int tuner_debug = 0;
52 module_param(tuner_debug, int, 0644);
54 static unsigned int tv_range[2] = { 44, 958 };
55 static unsigned int radio_range[2] = { 65, 108 };
57 module_param_array(tv_range, int, NULL, 0644);
58 module_param_array(radio_range, int, NULL, 0644);
60 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
61 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
62 MODULE_LICENSE("GPL");
64 static struct i2c_driver driver;
65 static struct i2c_client client_template;
67 /* ---------------------------------------------------------------------- */
69 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
70 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
72 struct tuner *t = i2c_get_clientdata(c);
74 if (t->type == UNSET) {
75 tuner_warn ("tuner type not set\n");
76 return;
78 if (NULL == t->tv_freq) {
79 tuner_warn ("Tuner has no way to set tv freq\n");
80 return;
82 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
83 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
84 freq / 16, freq % 16 * 100 / 16, tv_range[0],
85 tv_range[1]);
87 t->tv_freq(c, freq);
90 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
92 struct tuner *t = i2c_get_clientdata(c);
94 if (t->type == UNSET) {
95 tuner_warn ("tuner type not set\n");
96 return;
98 if (NULL == t->radio_freq) {
99 tuner_warn ("tuner has no way to set radio frequency\n");
100 return;
102 if (freq <= radio_range[0] * 16000 || freq >= radio_range[1] * 16000) {
103 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
104 freq / 16000, freq % 16000 * 100 / 16000,
105 radio_range[0], radio_range[1]);
108 t->radio_freq(c, freq);
109 return;
112 static void set_freq(struct i2c_client *c, unsigned long freq)
114 struct tuner *t = i2c_get_clientdata(c);
116 switch (t->mode) {
117 case V4L2_TUNER_RADIO:
118 tuner_dbg("radio freq set to %lu.%02lu\n",
119 freq / 16000, freq % 16000 * 100 / 16000);
120 set_radio_freq(c, freq);
121 break;
122 case V4L2_TUNER_ANALOG_TV:
123 case V4L2_TUNER_DIGITAL_TV:
124 tuner_dbg("tv freq set to %lu.%02lu\n",
125 freq / 16, freq % 16 * 100 / 16);
126 set_tv_freq(c, freq);
127 break;
129 t->freq = freq;
132 static void set_type(struct i2c_client *c, unsigned int type,
133 unsigned int new_mode_mask)
135 struct tuner *t = i2c_get_clientdata(c);
136 unsigned char buffer[4];
138 if (type == UNSET || type == TUNER_ABSENT) {
139 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
140 return;
143 if (type >= tuner_count) {
144 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
145 return;
148 /* This code detects calls by card attach_inform */
149 if (NULL == t->i2c.dev.driver) {
150 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
152 t->type=type;
153 return;
156 t->type = type;
158 switch (t->type) {
159 case TUNER_MT2032:
160 microtune_init(c);
161 break;
162 case TUNER_PHILIPS_TDA8290:
163 tda8290_init(c);
164 break;
165 case TUNER_TEA5767:
166 if (tea5767_tuner_init(c) == EINVAL) {
167 t->type = TUNER_ABSENT;
168 t->mode_mask = T_UNINITIALIZED;
169 return;
171 t->mode_mask = T_RADIO;
172 break;
173 case TUNER_PHILIPS_FMD1216ME_MK3:
174 buffer[0] = 0x0b;
175 buffer[1] = 0xdc;
176 buffer[2] = 0x9c;
177 buffer[3] = 0x60;
178 i2c_master_send(c, buffer, 4);
179 mdelay(1);
180 buffer[2] = 0x86;
181 buffer[3] = 0x54;
182 i2c_master_send(c, buffer, 4);
183 default_tuner_init(c);
184 break;
185 default:
186 default_tuner_init(c);
187 break;
190 if (t->mode_mask == T_UNINITIALIZED)
191 t->mode_mask = new_mode_mask;
193 set_freq(c, t->freq);
194 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
195 c->adapter->name, c->driver->name, c->addr << 1, type,
196 t->mode_mask);
200 * This function apply tuner config to tuner specified
201 * by tun_setup structure. I addr is unset, then admin status
202 * and tun addr status is more precise then current status,
203 * it's applied. Otherwise status and type are applied only to
204 * tuner with exactly the same addr.
207 static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
209 struct tuner *t = i2c_get_clientdata(c);
211 if (tun_setup->addr == ADDR_UNSET) {
212 if (t->mode_mask & tun_setup->mode_mask)
213 set_type(c, tun_setup->type, tun_setup->mode_mask);
214 } else if (tun_setup->addr == c->addr) {
215 set_type(c, tun_setup->type, tun_setup->mode_mask);
219 static inline int check_mode(struct tuner *t, char *cmd)
221 if (1 << t->mode & t->mode_mask) {
222 switch (t->mode) {
223 case V4L2_TUNER_RADIO:
224 tuner_dbg("Cmd %s accepted for radio\n", cmd);
225 break;
226 case V4L2_TUNER_ANALOG_TV:
227 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
228 break;
229 case V4L2_TUNER_DIGITAL_TV:
230 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
231 break;
233 return 0;
235 return EINVAL;
238 static char pal[] = "-";
239 module_param_string(pal, pal, sizeof(pal), 0644);
240 static char secam[] = "-";
241 module_param_string(secam, secam, sizeof(secam), 0644);
243 /* get more precise norm info from insmod option */
244 static int tuner_fixup_std(struct tuner *t)
246 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
247 switch (pal[0]) {
248 case 'b':
249 case 'B':
250 case 'g':
251 case 'G':
252 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
253 t->std = V4L2_STD_PAL_BG;
254 break;
255 case 'i':
256 case 'I':
257 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
258 t->std = V4L2_STD_PAL_I;
259 break;
260 case 'd':
261 case 'D':
262 case 'k':
263 case 'K':
264 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
265 t->std = V4L2_STD_PAL_DK;
266 break;
267 case 'M':
268 case 'm':
269 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
270 t->std = V4L2_STD_PAL_M;
271 break;
272 case 'N':
273 case 'n':
274 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
275 t->std = V4L2_STD_PAL_N;
276 break;
279 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
280 switch (secam[0]) {
281 case 'd':
282 case 'D':
283 case 'k':
284 case 'K':
285 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
286 t->std = V4L2_STD_SECAM_DK;
287 break;
288 case 'l':
289 case 'L':
290 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
291 t->std = V4L2_STD_SECAM_L;
292 break;
296 return 0;
299 /* ---------------------------------------------------------------------- */
301 /* static var Used only in tuner_attach and tuner_probe */
302 static unsigned default_mode_mask;
304 /* During client attach, set_type is called by adapter's attach_inform callback.
305 set_type must then be completed by tuner_attach.
307 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
309 struct tuner *t;
311 client_template.adapter = adap;
312 client_template.addr = addr;
314 t = kmalloc(sizeof(struct tuner), GFP_KERNEL);
315 if (NULL == t)
316 return -ENOMEM;
317 memset(t, 0, sizeof(struct tuner));
318 memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
319 i2c_set_clientdata(&t->i2c, t);
320 t->type = UNSET;
321 t->radio_if2 = 10700 * 1000; /* 10.7MHz - FM radio */
322 t->audmode = V4L2_TUNER_MODE_STEREO;
323 t->mode_mask = T_UNINITIALIZED;
326 tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
328 if (show_i2c) {
329 unsigned char buffer[16];
330 int i,rc;
332 memset(buffer, 0, sizeof(buffer));
333 rc = i2c_master_recv(&t->i2c, buffer, sizeof(buffer));
334 printk("tuner-%04x I2C RECV = ",addr);
335 for (i=0;i<rc;i++)
336 printk("%02x ",buffer[i]);
337 printk("\n");
339 /* TEA5767 autodetection code - only for addr = 0xc0 */
340 if (!no_autodetect) {
341 if (addr == 0x60) {
342 if (tea5767_autodetection(&t->i2c) != EINVAL) {
343 t->type = TUNER_TEA5767;
344 t->mode_mask = T_RADIO;
345 t->mode = T_STANDBY;
346 t->freq = 87.5 * 16; /* Sets freq to FM range */
347 default_mode_mask &= ~T_RADIO;
349 i2c_attach_client (&t->i2c);
350 set_type(&t->i2c,t->type, t->mode_mask);
351 return 0;
356 /* Initializes only the first adapter found */
357 if (default_mode_mask != T_UNINITIALIZED) {
358 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
359 t->mode_mask = default_mode_mask;
360 t->freq = 400 * 16; /* Sets freq to VHF High */
361 default_mode_mask = T_UNINITIALIZED;
364 /* Should be just before return */
365 i2c_attach_client (&t->i2c);
366 set_type (&t->i2c,t->type, t->mode_mask);
367 return 0;
370 static int tuner_probe(struct i2c_adapter *adap)
372 if (0 != addr) {
373 normal_i2c[0] = addr;
374 normal_i2c[1] = I2C_CLIENT_END;
377 default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
379 if (adap->class & I2C_CLASS_TV_ANALOG)
380 return i2c_probe(adap, &addr_data, tuner_attach);
381 return 0;
384 static int tuner_detach(struct i2c_client *client)
386 struct tuner *t = i2c_get_clientdata(client);
387 int err;
389 err = i2c_detach_client(&t->i2c);
390 if (err) {
391 tuner_warn
392 ("Client deregistration failed, client not detached.\n");
393 return err;
396 kfree(t);
397 return 0;
401 * Switch tuner to other mode. If tuner support both tv and radio,
402 * set another frequency to some value (This is needed for some pal
403 * tuners to avoid locking). Otherwise, just put second tuner in
404 * standby mode.
407 static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
409 if (mode != t->mode) {
411 t->mode = mode;
412 if (check_mode(t, cmd) == EINVAL) {
413 t->mode = T_STANDBY;
414 if (V4L2_TUNER_RADIO == mode) {
415 set_tv_freq(client, 400 * 16);
416 } else {
417 set_radio_freq(client, 87.5 * 16000);
419 return EINVAL;
422 return 0;
425 #define switch_v4l2() if (!t->using_v4l2) \
426 tuner_dbg("switching to v4l2\n"); \
427 t->using_v4l2 = 1;
429 static inline int check_v4l2(struct tuner *t)
431 if (t->using_v4l2) {
432 tuner_dbg ("ignore v4l1 call\n");
433 return EINVAL;
435 return 0;
438 static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
440 struct tuner *t = i2c_get_clientdata(client);
441 unsigned int *iarg = (int *)arg;
443 switch (cmd) {
444 /* --- configuration --- */
445 case TUNER_SET_TYPE_ADDR:
446 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x\n",
447 ((struct tuner_setup *)arg)->type,
448 ((struct tuner_setup *)arg)->addr,
449 ((struct tuner_setup *)arg)->mode_mask);
451 set_addr(client, (struct tuner_setup *)arg);
452 break;
453 case AUDC_SET_RADIO:
454 set_mode(client,t,V4L2_TUNER_RADIO, "AUDC_SET_RADIO");
455 break;
456 case AUDC_CONFIG_PINNACLE:
457 if (check_mode(t, "AUDC_CONFIG_PINNACLE") == EINVAL)
458 return 0;
459 switch (*iarg) {
460 case 2:
461 tuner_dbg("pinnacle pal\n");
462 t->radio_if2 = 33300 * 1000;
463 break;
464 case 3:
465 tuner_dbg("pinnacle ntsc\n");
466 t->radio_if2 = 41300 * 1000;
467 break;
469 break;
470 case VIDIOCSAUDIO:
471 if (check_mode(t, "VIDIOCSAUDIO") == EINVAL)
472 return 0;
473 if (check_v4l2(t) == EINVAL)
474 return 0;
476 /* Should be implemented, since bttv calls it */
477 tuner_dbg("VIDIOCSAUDIO not implemented.\n");
479 break;
480 case MSP_SET_MATRIX:
481 case TDA9887_SET_CONFIG:
482 break;
483 /* --- v4l ioctls --- */
484 /* take care: bttv does userspace copying, we'll get a
485 kernel pointer here... */
486 case VIDIOCSCHAN:
488 static const v4l2_std_id map[] = {
489 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
490 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
491 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
492 [4 /* bttv */ ] = V4L2_STD_PAL_M,
493 [5 /* bttv */ ] = V4L2_STD_PAL_N,
494 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
496 struct video_channel *vc = arg;
498 if (check_v4l2(t) == EINVAL)
499 return 0;
501 if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
502 return 0;
504 if (vc->norm < ARRAY_SIZE(map))
505 t->std = map[vc->norm];
506 tuner_fixup_std(t);
507 if (t->freq)
508 set_tv_freq(client, t->freq);
509 return 0;
511 case VIDIOCSFREQ:
513 unsigned long *v = arg;
515 if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
516 return 0;
517 if (check_v4l2(t) == EINVAL)
518 return 0;
520 set_freq(client, *v);
521 return 0;
523 case VIDIOCGTUNER:
525 struct video_tuner *vt = arg;
527 if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
528 return 0;
529 if (check_v4l2(t) == EINVAL)
530 return 0;
532 if (V4L2_TUNER_RADIO == t->mode) {
533 if (t->has_signal)
534 vt->signal = t->has_signal(client);
535 if (t->is_stereo) {
536 if (t->is_stereo(client))
537 vt->flags |=
538 VIDEO_TUNER_STEREO_ON;
539 else
540 vt->flags &=
541 ~VIDEO_TUNER_STEREO_ON;
543 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
545 vt->rangelow = radio_range[0] * 16000;
546 vt->rangehigh = radio_range[1] * 16000;
548 } else {
549 vt->rangelow = tv_range[0] * 16;
550 vt->rangehigh = tv_range[1] * 16;
553 return 0;
555 case VIDIOCGAUDIO:
557 struct video_audio *va = arg;
559 if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
560 return 0;
561 if (check_v4l2(t) == EINVAL)
562 return 0;
564 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
565 va->mode = t->is_stereo(client)
566 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
567 return 0;
570 case VIDIOC_S_STD:
572 v4l2_std_id *id = arg;
574 if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
575 == EINVAL)
576 return 0;
578 switch_v4l2();
580 t->std = *id;
581 tuner_fixup_std(t);
582 if (t->freq)
583 set_freq(client, t->freq);
584 break;
586 case VIDIOC_S_FREQUENCY:
588 struct v4l2_frequency *f = arg;
590 t->freq = f->frequency;
591 switch_v4l2();
592 if (V4L2_TUNER_RADIO == f->type &&
593 V4L2_TUNER_RADIO != t->mode) {
594 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
595 == EINVAL)
596 return 0;
598 set_freq(client,t->freq);
600 break;
602 case VIDIOC_G_FREQUENCY:
604 struct v4l2_frequency *f = arg;
606 if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
607 return 0;
608 switch_v4l2();
609 f->type = t->mode;
610 f->frequency = t->freq;
611 break;
613 case VIDIOC_G_TUNER:
615 struct v4l2_tuner *tuner = arg;
617 if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
618 return 0;
619 switch_v4l2();
621 if (V4L2_TUNER_RADIO == t->mode) {
623 if (t->has_signal)
624 tuner->signal = t->has_signal(client);
626 if (t->is_stereo) {
627 if (t->is_stereo(client)) {
628 tuner->rxsubchans =
629 V4L2_TUNER_SUB_STEREO |
630 V4L2_TUNER_SUB_MONO;
631 } else {
632 tuner->rxsubchans =
633 V4L2_TUNER_SUB_MONO;
637 tuner->capability |=
638 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
640 tuner->audmode = t->audmode;
642 tuner->rangelow = radio_range[0] * 16000;
643 tuner->rangehigh = radio_range[1] * 16000;
644 } else {
645 tuner->rangelow = tv_range[0] * 16;
646 tuner->rangehigh = tv_range[1] * 16;
648 break;
650 case VIDIOC_S_TUNER:
652 struct v4l2_tuner *tuner = arg;
654 if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
655 return 0;
657 switch_v4l2();
659 if (V4L2_TUNER_RADIO == t->mode) {
660 t->audmode = tuner->audmode;
661 set_radio_freq(client, t->freq);
663 break;
665 default:
666 tuner_dbg("Unimplemented IOCTL 0x%08x(dir=%d,tp=0x%02x,nr=%d,sz=%d)\n",
667 cmd, _IOC_DIR(cmd), _IOC_TYPE(cmd),
668 _IOC_NR(cmd), _IOC_SIZE(cmd));
669 break;
672 return 0;
675 static int tuner_suspend(struct device *dev, pm_message_t state, u32 level)
677 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
678 struct tuner *t = i2c_get_clientdata (c);
680 tuner_dbg ("suspend\n");
681 /* FIXME: power down ??? */
682 return 0;
685 static int tuner_resume(struct device *dev, u32 level)
687 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
688 struct tuner *t = i2c_get_clientdata (c);
690 tuner_dbg ("resume\n");
691 if (t->freq)
692 set_freq(c, t->freq);
693 return 0;
696 /* ----------------------------------------------------------------------- */
698 static struct i2c_driver driver = {
699 .owner = THIS_MODULE,
700 .name = "tuner",
701 .id = I2C_DRIVERID_TUNER,
702 .flags = I2C_DF_NOTIFY,
703 .attach_adapter = tuner_probe,
704 .detach_client = tuner_detach,
705 .command = tuner_command,
706 .driver = {
707 .suspend = tuner_suspend,
708 .resume = tuner_resume,
711 static struct i2c_client client_template = {
712 I2C_DEVNAME("(tuner unset)"),
713 .flags = I2C_CLIENT_ALLOW_USE,
714 .driver = &driver,
717 static int __init tuner_init_module(void)
719 return i2c_add_driver(&driver);
722 static void __exit tuner_cleanup_module(void)
724 i2c_del_driver(&driver);
727 module_init(tuner_init_module);
728 module_exit(tuner_cleanup_module);
731 * Overrides for Emacs so that we follow Linus's tabbing style.
732 * ---------------------------------------------------------------------------
733 * Local variables:
734 * c-basic-offset: 8
735 * End: