[PATCH] v4l: I2C Tuner
[linux-2.6.git] / drivers / media / video / tuner-core.c
1 /*
2  * $Id: tuner-core.c,v 1.55 2005/07/08 13:20:33 mchehab Exp $
3  *
4  * i2c tv tuner chip device driver
5  * core core, i.e. kernel interfaces, registering and so on
6  */
7
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>
22
23 #include <media/tuner.h>
24 #include <media/audiochip.h>
25
26 #define UNSET (-1U)
27
28 /* standard i2c insmod options */
29 static unsigned short normal_i2c[] = {
30         0x4b,                   /* tda8290 */
31         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
32         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
33         I2C_CLIENT_END
34 };
35
36 I2C_CLIENT_INSMOD;
37
38 /* insmod options used at init time => read/only */
39 static unsigned int addr = 0;
40 module_param(addr, int, 0444);
41
42 /* insmod options used at runtime => read/write */
43 unsigned int tuner_debug = 0;
44 module_param(tuner_debug, int, 0644);
45
46 static unsigned int tv_range[2] = { 44, 958 };
47 static unsigned int radio_range[2] = { 65, 108 };
48
49 module_param_array(tv_range, int, NULL, 0644);
50 module_param_array(radio_range, int, NULL, 0644);
51
52 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
53 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
54 MODULE_LICENSE("GPL");
55
56 static struct i2c_driver driver;
57 static struct i2c_client client_template;
58
59 /* ---------------------------------------------------------------------- */
60
61 /* Set tuner frequency,  freq in Units of 62.5kHz = 1/16MHz */
62 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
63 {
64         struct tuner *t = i2c_get_clientdata(c);
65
66         if (t->type == UNSET) {
67                 tuner_warn ("tuner type not set\n");
68                 return;
69         }
70         if (NULL == t->tv_freq) {
71                 tuner_warn ("Tuner has no way to set tv freq\n");
72                 return;
73         }
74         if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
75                 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
76                            freq / 16, freq % 16 * 100 / 16, tv_range[0],
77                            tv_range[1]);
78         }
79         t->tv_freq(c, freq);
80 }
81
82 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
83 {
84         struct tuner *t = i2c_get_clientdata(c);
85
86         if (t->type == UNSET) {
87                 tuner_warn ("tuner type not set\n");
88                 return;
89         }
90         if (NULL == t->radio_freq) {
91                 tuner_warn ("tuner has no way to set radio frequency\n");
92                 return;
93         }
94         if (freq <= radio_range[0] * 16000 || freq >= radio_range[1] * 16000) {
95                 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
96                            freq / 16000, freq % 16000 * 100 / 16000,
97                            radio_range[0], radio_range[1]);
98         }
99
100         t->radio_freq(c, freq);
101         return;
102 }
103
104 static void set_freq(struct i2c_client *c, unsigned long freq)
105 {
106         struct tuner *t = i2c_get_clientdata(c);
107
108         switch (t->mode) {
109         case V4L2_TUNER_RADIO:
110                 tuner_dbg("radio freq set to %lu.%02lu\n",
111                           freq / 16000, freq % 16000 * 100 / 16000);
112                 set_radio_freq(c, freq);
113                 break;
114         case V4L2_TUNER_ANALOG_TV:
115         case V4L2_TUNER_DIGITAL_TV:
116                 tuner_dbg("tv freq set to %lu.%02lu\n",
117                           freq / 16, freq % 16 * 100 / 16);
118                 set_tv_freq(c, freq);
119                 break;
120         }
121         t->freq = freq;
122 }
123
124 static void set_type(struct i2c_client *c, unsigned int type,
125                      unsigned int new_mode_mask)
126 {
127         struct tuner *t = i2c_get_clientdata(c);
128         unsigned char buffer[4];
129
130         if (type == UNSET || type == TUNER_ABSENT) {
131                 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
132                 return;
133         }
134
135         if (type >= tuner_count) {
136                 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
137                 return;
138         }
139
140         /* This code detects calls by card attach_inform */
141         if (NULL == t->i2c.dev.driver) {
142                 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
143
144                 t->type=type;
145                 return;
146         }
147
148         t->type = type;
149
150         switch (t->type) {
151         case TUNER_MT2032:
152                 microtune_init(c);
153                 break;
154         case TUNER_PHILIPS_TDA8290:
155                 tda8290_init(c);
156                 break;
157         case TUNER_TEA5767:
158                 if (tea5767_tuner_init(c) == EINVAL) {
159                         t->type = TUNER_ABSENT;
160                         t->mode_mask = T_UNINITIALIZED;
161                         return;
162                 }
163                 t->mode_mask = T_RADIO;
164                 break;
165         case TUNER_PHILIPS_FMD1216ME_MK3:
166                 buffer[0] = 0x0b;
167                 buffer[1] = 0xdc;
168                 buffer[2] = 0x9c;
169                 buffer[3] = 0x60;
170                 i2c_master_send(c, buffer, 4);
171                 mdelay(1);
172                 buffer[2] = 0x86;
173                 buffer[3] = 0x54;
174                 i2c_master_send(c, buffer, 4);
175                 default_tuner_init(c);
176                 break;
177         default:
178                 default_tuner_init(c);
179                 break;
180         }
181
182         if (t->mode_mask == T_UNINITIALIZED)
183                 t->mode_mask = new_mode_mask;
184
185         set_freq(c, t->freq);
186         tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
187                   c->adapter->name, c->driver->name, c->addr << 1, type,
188                   t->mode_mask);
189 }
190
191 /*
192  * This function apply tuner config to tuner specified
193  * by tun_setup structure. I addr is unset, then admin status
194  * and tun addr status is more precise then current status,
195  * it's applied. Otherwise status and type are applied only to
196  * tuner with exactly the same addr.
197 */
198
199 static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
200 {
201         struct tuner *t = i2c_get_clientdata(c);
202
203         if (tun_setup->addr == ADDR_UNSET) {
204                 if (t->mode_mask & tun_setup->mode_mask)
205                         set_type(c, tun_setup->type, tun_setup->mode_mask);
206         } else if (tun_setup->addr == c->addr) {
207                 set_type(c, tun_setup->type, tun_setup->mode_mask);
208         }
209 }
210
211 static inline int check_mode(struct tuner *t, char *cmd)
212 {
213         if (1 << t->mode & t->mode_mask) {
214                 switch (t->mode) {
215                 case V4L2_TUNER_RADIO:
216                         tuner_dbg("Cmd %s accepted for radio\n", cmd);
217                         break;
218                 case V4L2_TUNER_ANALOG_TV:
219                         tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
220                         break;
221                 case V4L2_TUNER_DIGITAL_TV:
222                         tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
223                         break;
224                 }
225                 return 0;
226         }
227         return EINVAL;
228 }
229
230 static char pal[] = "-";
231 module_param_string(pal, pal, sizeof(pal), 0644);
232 static char secam[] = "-";
233 module_param_string(secam, secam, sizeof(secam), 0644);
234
235 /* get more precise norm info from insmod option */
236 static int tuner_fixup_std(struct tuner *t)
237 {
238         if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
239                 switch (pal[0]) {
240                 case 'b':
241                 case 'B':
242                 case 'g':
243                 case 'G':
244                         tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
245                         t->std = V4L2_STD_PAL_BG;
246                         break;
247                 case 'i':
248                 case 'I':
249                         tuner_dbg ("insmod fixup: PAL => PAL-I\n");
250                         t->std = V4L2_STD_PAL_I;
251                         break;
252                 case 'd':
253                 case 'D':
254                 case 'k':
255                 case 'K':
256                         tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
257                         t->std = V4L2_STD_PAL_DK;
258                         break;
259                 case 'M':
260                 case 'm':
261                         tuner_dbg ("insmod fixup: PAL => PAL-M\n");
262                         t->std = V4L2_STD_PAL_M;
263                         break;
264                 case 'N':
265                 case 'n':
266                         tuner_dbg ("insmod fixup: PAL => PAL-N\n");
267                         t->std = V4L2_STD_PAL_N;
268                         break;
269                 }
270         }
271         if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
272                 switch (secam[0]) {
273                 case 'd':
274                 case 'D':
275                 case 'k':
276                 case 'K':
277                         tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
278                         t->std = V4L2_STD_SECAM_DK;
279                         break;
280                 case 'l':
281                 case 'L':
282                         tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
283                         t->std = V4L2_STD_SECAM_L;
284                         break;
285                 }
286         }
287
288         return 0;
289 }
290
291 /* ---------------------------------------------------------------------- */
292
293 /* static var Used only in tuner_attach and tuner_probe */
294 static unsigned default_mode_mask;
295
296 /* During client attach, set_type is called by adapter's attach_inform callback.
297    set_type must then be completed by tuner_attach.
298  */
299 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
300 {
301         struct tuner *t;
302
303         client_template.adapter = adap;
304         client_template.addr = addr;
305
306         t = kmalloc(sizeof(struct tuner), GFP_KERNEL);
307         if (NULL == t)
308                 return -ENOMEM;
309         memset(t, 0, sizeof(struct tuner));
310         memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
311         i2c_set_clientdata(&t->i2c, t);
312         t->type = UNSET;
313         t->radio_if2 = 10700 * 1000;    /* 10.7MHz - FM radio */
314         t->audmode = V4L2_TUNER_MODE_STEREO;
315         t->mode_mask = T_UNINITIALIZED;
316
317
318         tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
319
320         /* TEA5767 autodetection code - only for addr = 0xc0 */
321         if (addr == 0x60) {
322                 if (tea5767_autodetection(&t->i2c) != EINVAL) {
323                         t->type = TUNER_TEA5767;
324                         t->mode_mask = T_RADIO;
325                         t->mode = T_STANDBY;
326                         t->freq = 87.5 * 16; /* Sets freq to FM range */
327                         default_mode_mask &= ~T_RADIO;
328
329                         i2c_attach_client (&t->i2c);
330                         set_type(&t->i2c,t->type, t->mode_mask);
331                         return 0;
332                 }
333         }
334
335         /* Initializes only the first adapter found */
336         if (default_mode_mask != T_UNINITIALIZED) {
337                 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
338                 t->mode_mask = default_mode_mask;
339                 t->freq = 400 * 16; /* Sets freq to VHF High */
340                 default_mode_mask = T_UNINITIALIZED;
341         }
342
343         /* Should be just before return */
344         i2c_attach_client (&t->i2c);
345         set_type (&t->i2c,t->type, t->mode_mask);
346         return 0;
347 }
348
349 static int tuner_probe(struct i2c_adapter *adap)
350 {
351         if (0 != addr) {
352                 normal_i2c[0] = addr;
353                 normal_i2c[1] = I2C_CLIENT_END;
354         }
355
356         default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
357
358         if (adap->class & I2C_CLASS_TV_ANALOG)
359                 return i2c_probe(adap, &addr_data, tuner_attach);
360         return 0;
361 }
362
363 static int tuner_detach(struct i2c_client *client)
364 {
365         struct tuner *t = i2c_get_clientdata(client);
366         int err;
367
368         err = i2c_detach_client(&t->i2c);
369         if (err) {
370                 tuner_warn
371                     ("Client deregistration failed, client not detached.\n");
372                 return err;
373         }
374
375         kfree(t);
376         return 0;
377 }
378
379 /*
380  * Switch tuner to other mode. If tuner support both tv and radio,
381  * set another frequency to some value (This is needed for some pal
382  * tuners to avoid locking). Otherwise, just put second tuner in
383  * standby mode.
384  */
385
386 static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
387 {
388         if (mode != t->mode) {
389
390                 t->mode = mode;
391                 if (check_mode(t, cmd) == EINVAL) {
392                         t->mode = T_STANDBY;
393                         if (V4L2_TUNER_RADIO == mode) {
394                                 set_tv_freq(client, 400 * 16);
395                         } else {
396                                 set_radio_freq(client, 87.5 * 16000);
397                         }
398                         return EINVAL;
399                 }
400         }
401         return 0;
402 }
403
404 #define switch_v4l2()   if (!t->using_v4l2) \
405                             tuner_dbg("switching to v4l2\n"); \
406                         t->using_v4l2 = 1;
407
408 static inline int check_v4l2(struct tuner *t)
409 {
410         if (t->using_v4l2) {
411                 tuner_dbg ("ignore v4l1 call\n");
412                 return EINVAL;
413         }
414         return 0;
415 }
416
417 static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
418 {
419         struct tuner *t = i2c_get_clientdata(client);
420         unsigned int *iarg = (int *)arg;
421
422         switch (cmd) {
423         /* --- configuration --- */
424         case TUNER_SET_TYPE_ADDR:
425                 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x\n",
426                                 ((struct tuner_setup *)arg)->type,
427                                 ((struct tuner_setup *)arg)->addr,
428                                 ((struct tuner_setup *)arg)->mode_mask);
429
430                 set_addr(client, (struct tuner_setup *)arg);
431                 break;
432         case AUDC_SET_RADIO:
433                 set_mode(client,t,V4L2_TUNER_RADIO, "AUDC_SET_RADIO");
434                 break;
435         case AUDC_CONFIG_PINNACLE:
436                 if (check_mode(t, "AUDC_CONFIG_PINNACLE") == EINVAL)
437                         return 0;
438                 switch (*iarg) {
439                 case 2:
440                         tuner_dbg("pinnacle pal\n");
441                         t->radio_if2 = 33300 * 1000;
442                         break;
443                 case 3:
444                         tuner_dbg("pinnacle ntsc\n");
445                         t->radio_if2 = 41300 * 1000;
446                         break;
447                 }
448                 break;
449         case TDA9887_SET_CONFIG:
450                 break;
451         /* --- v4l ioctls --- */
452         /* take care: bttv does userspace copying, we'll get a
453            kernel pointer here... */
454         case VIDIOCSCHAN:
455                 {
456                         static const v4l2_std_id map[] = {
457                                 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
458                                 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
459                                 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
460                                 [4 /* bttv */ ] = V4L2_STD_PAL_M,
461                                 [5 /* bttv */ ] = V4L2_STD_PAL_N,
462                                 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
463                         };
464                         struct video_channel *vc = arg;
465
466                         if (check_v4l2(t) == EINVAL)
467                                 return 0;
468
469                         if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
470                                 return 0;
471
472                         if (vc->norm < ARRAY_SIZE(map))
473                                 t->std = map[vc->norm];
474                         tuner_fixup_std(t);
475                         if (t->freq)
476                                 set_tv_freq(client, t->freq);
477                         return 0;
478                 }
479         case VIDIOCSFREQ:
480                 {
481                         unsigned long *v = arg;
482
483                         if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
484                                 return 0;
485                         if (check_v4l2(t) == EINVAL)
486                                 return 0;
487
488                         set_freq(client, *v);
489                         return 0;
490                 }
491         case VIDIOCGTUNER:
492                 {
493                         struct video_tuner *vt = arg;
494
495                         if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
496                                 return 0;
497                         if (check_v4l2(t) == EINVAL)
498                                 return 0;
499
500                         if (V4L2_TUNER_RADIO == t->mode) {
501                                 if (t->has_signal)
502                                         vt->signal = t->has_signal(client);
503                                 if (t->is_stereo) {
504                                         if (t->is_stereo(client))
505                                                 vt->flags |=
506                                                     VIDEO_TUNER_STEREO_ON;
507                                         else
508                                                 vt->flags &=
509                                                     ~VIDEO_TUNER_STEREO_ON;
510                                 }
511                                 vt->flags |= VIDEO_TUNER_LOW;   /* Allow freqs at 62.5 Hz */
512
513                                 vt->rangelow = radio_range[0] * 16000;
514                                 vt->rangehigh = radio_range[1] * 16000;
515
516                         } else {
517                                 vt->rangelow = tv_range[0] * 16;
518                                 vt->rangehigh = tv_range[1] * 16;
519                         }
520
521                         return 0;
522                 }
523         case VIDIOCGAUDIO:
524                 {
525                         struct video_audio *va = arg;
526
527                         if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
528                                 return 0;
529                         if (check_v4l2(t) == EINVAL)
530                                 return 0;
531
532                         if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
533                                 va->mode = t->is_stereo(client)
534                                     ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
535                         return 0;
536                 }
537
538         case VIDIOC_S_STD:
539                 {
540                         v4l2_std_id *id = arg;
541
542                         if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
543                                         == EINVAL)
544                                 return 0;
545
546                         switch_v4l2();
547
548                         t->std = *id;
549                         tuner_fixup_std(t);
550                         if (t->freq)
551                                 set_freq(client, t->freq);
552                         break;
553                 }
554         case VIDIOC_S_FREQUENCY:
555                 {
556                         struct v4l2_frequency *f = arg;
557
558                         t->freq = f->frequency;
559                         switch_v4l2();
560                         if (V4L2_TUNER_RADIO == f->type &&
561                             V4L2_TUNER_RADIO != t->mode) {
562                                 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
563                                             == EINVAL)
564                                         return 0;
565                         }
566                         set_freq(client,t->freq);
567
568                         break;
569                 }
570         case VIDIOC_G_FREQUENCY:
571                 {
572                         struct v4l2_frequency *f = arg;
573
574                         if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
575                                 return 0;
576                         switch_v4l2();
577                         f->type = t->mode;
578                         f->frequency = t->freq;
579                         break;
580                 }
581         case VIDIOC_G_TUNER:
582                 {
583                         struct v4l2_tuner *tuner = arg;
584
585                         if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
586                                 return 0;
587                         switch_v4l2();
588
589                         if (V4L2_TUNER_RADIO == t->mode) {
590
591                                 if (t->has_signal)
592                                         tuner->signal = t->has_signal(client);
593
594                                 if (t->is_stereo) {
595                                         if (t->is_stereo(client)) {
596                                                 tuner->rxsubchans =
597                                                     V4L2_TUNER_SUB_STEREO |
598                                                     V4L2_TUNER_SUB_MONO;
599                                         } else {
600                                                 tuner->rxsubchans =
601                                                     V4L2_TUNER_SUB_MONO;
602                                         }
603                                 }
604
605                                 tuner->capability |=
606                                     V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
607
608                                 tuner->audmode = t->audmode;
609
610                                 tuner->rangelow = radio_range[0] * 16000;
611                                 tuner->rangehigh = radio_range[1] * 16000;
612                         } else {
613                                 tuner->rangelow = tv_range[0] * 16;
614                                 tuner->rangehigh = tv_range[1] * 16;
615                         }
616                         break;
617                 }
618         case VIDIOC_S_TUNER:
619                 {
620                         struct v4l2_tuner *tuner = arg;
621
622                         if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
623                                 return 0;
624
625                         switch_v4l2();
626
627                         if (V4L2_TUNER_RADIO == t->mode) {
628                                 t->audmode = tuner->audmode;
629                                 set_radio_freq(client, t->freq);
630                         }
631                         break;
632                 }
633         default:
634                 tuner_dbg("Unimplemented IOCTL 0x%08x called to tuner.\n", cmd);
635                 break;
636         }
637
638         return 0;
639 }
640
641 static int tuner_suspend(struct device *dev, u32 state, u32 level)
642 {
643         struct i2c_client *c = container_of (dev, struct i2c_client, dev);
644         struct tuner *t = i2c_get_clientdata (c);
645
646         tuner_dbg ("suspend\n");
647         /* FIXME: power down ??? */
648         return 0;
649 }
650
651 static int tuner_resume(struct device *dev, u32 level)
652 {
653         struct i2c_client *c = container_of (dev, struct i2c_client, dev);
654         struct tuner *t = i2c_get_clientdata (c);
655
656         tuner_dbg ("resume\n");
657         if (t->freq)
658                 set_freq(c, t->freq);
659         return 0;
660 }
661
662 /* ----------------------------------------------------------------------- */
663
664 static struct i2c_driver driver = {
665         .owner = THIS_MODULE,
666         .name = "tuner",
667         .id = I2C_DRIVERID_TUNER,
668         .flags = I2C_DF_NOTIFY,
669         .attach_adapter = tuner_probe,
670         .detach_client = tuner_detach,
671         .command = tuner_command,
672         .driver = {
673                    .suspend = tuner_suspend,
674                    .resume = tuner_resume,
675                    },
676 };
677 static struct i2c_client client_template = {
678         I2C_DEVNAME("(tuner unset)"),
679         .flags = I2C_CLIENT_ALLOW_USE,
680         .driver = &driver,
681 };
682
683 static int __init tuner_init_module(void)
684 {
685         return i2c_add_driver(&driver);
686 }
687
688 static void __exit tuner_cleanup_module(void)
689 {
690         i2c_del_driver(&driver);
691 }
692
693 module_init(tuner_init_module);
694 module_exit(tuner_cleanup_module);
695
696 /*
697  * Overrides for Emacs so that we follow Linus's tabbing style.
698  * ---------------------------------------------------------------------------
699  * Local variables:
700  * c-basic-offset: 8
701  * End:
702  */