Merge /spare/repo/netdev-2.6/ branch 'ieee80211'
[linux-2.6] / drivers / media / video / tuner-core.c
1 /*
2  * $Id: tuner-core.c,v 1.15 2005/06/12 01:36:14 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 /*
27  * comment line bellow to return to old behavor, where only one I2C device is supported
28  */
29 #define CONFIG_TUNER_MULTI_I2C /**/
30
31 #define UNSET (-1U)
32
33 /* standard i2c insmod options */
34 static unsigned short normal_i2c[] = {
35         0x4b, /* tda8290 */
36         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
37         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
38         I2C_CLIENT_END
39 };
40 I2C_CLIENT_INSMOD;
41
42 /* insmod options used at init time => read/only */
43 static unsigned int addr  =  0;
44 module_param(addr, int, 0444);
45
46 /* insmod options used at runtime => read/write */
47 unsigned int tuner_debug   = 0;
48 module_param(tuner_debug,       int, 0644);
49
50 static unsigned int tv_range[2]    = { 44, 958 };
51 static unsigned int radio_range[2] = { 65, 108 };
52
53 module_param_array(tv_range,    int, NULL, 0644);
54 module_param_array(radio_range, int, NULL, 0644);
55
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");
59
60 static int this_adap;
61 #ifdef CONFIG_TUNER_MULTI_I2C
62 static unsigned short first_tuner, tv_tuner, radio_tuner;
63 #endif
64
65 static struct i2c_driver driver;
66 static struct i2c_client client_template;
67
68 /* ---------------------------------------------------------------------- */
69
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)
72 {
73         struct tuner *t = i2c_get_clientdata(c);
74
75         if (t->type == UNSET) {
76                 tuner_info("tuner type not set\n");
77                 return;
78         }
79         if (NULL == t->tv_freq) {
80                 tuner_info("Huh? tv_set is NULL?\n");
81                 return;
82         }
83         if (freq < tv_range[0]*16 || freq > tv_range[1]*16) {
84
85                 if (freq >= tv_range[0]*16364 && freq <= tv_range[1]*16384) {
86                         /* V4L2_TUNER_CAP_LOW frequency */
87
88                         tuner_dbg("V4L2_TUNER_CAP_LOW freq selected for TV. Tuners yet doesn't support converting it to valid freq.\n");
89
90                         t->tv_freq(c,freq>>10);
91
92                         return;
93                 } else {
94                         /* FIXME: better do that chip-specific, but
95                            right now we don't have that in the config
96                            struct and this way is still better than no
97                            check at all */
98                         tuner_info("TV freq (%d.%02d) out of range (%d-%d)\n",
99                                    freq/16,freq%16*100/16,tv_range[0],tv_range[1]);
100                         return;
101                 }
102         }
103         tuner_dbg("62.5 Khz freq step selected for TV.\n");
104         t->tv_freq(c,freq);
105 }
106
107 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
108 {
109         struct tuner *t = i2c_get_clientdata(c);
110
111         if (t->type == UNSET) {
112                 tuner_info("tuner type not set\n");
113                 return;
114         }
115         if (NULL == t->radio_freq) {
116                 tuner_info("no radio tuning for this one, sorry.\n");
117                 return;
118         }
119         if (freq < radio_range[0]*16 || freq > radio_range[1]*16) {
120                 if (freq >= tv_range[0]*16364 && freq <= tv_range[1]*16384) {
121                         /* V4L2_TUNER_CAP_LOW frequency */
122                         if (t->type == TUNER_TEA5767) {
123                                 tuner_info("radio freq step 62.5Hz (%d.%06d)\n",(freq>>14),freq%(1<<14)*10000);
124                                 t->radio_freq(c,freq>>10);
125                                 return;
126                         }
127
128                         tuner_dbg("V4L2_TUNER_CAP_LOW freq selected for Radio. Tuners yet doesn't support converting it to valid freq.\n");
129
130                         tuner_info("radio freq (%d.%06d)\n",(freq>>14),freq%(1<<14)*10000);
131
132                         t->radio_freq(c,freq>>10);
133                         return;
134
135                 } else {
136                         tuner_info("radio freq (%d.%02d) out of range (%d-%d)\n",
137                            freq/16,freq%16*100/16,
138                                    radio_range[0],radio_range[1]);
139                         return;
140                 }
141         }
142         tuner_dbg("62.5 Khz freq step selected for Radio.\n");
143         t->radio_freq(c,freq);
144 }
145
146 static void set_freq(struct i2c_client *c, unsigned long freq)
147 {
148         struct tuner *t = i2c_get_clientdata(c);
149
150         switch (t->mode) {
151         case V4L2_TUNER_RADIO:
152                 tuner_dbg("radio freq set to %lu.%02lu\n",
153                           freq/16,freq%16*100/16);
154                 set_radio_freq(c,freq);
155                 break;
156         case V4L2_TUNER_ANALOG_TV:
157         case V4L2_TUNER_DIGITAL_TV:
158                 tuner_dbg("tv freq set to %lu.%02lu\n",
159                           freq/16,freq%16*100/16);
160                 set_tv_freq(c, freq);
161                 break;
162         }
163         t->freq = freq;
164 }
165
166 static void set_type(struct i2c_client *c, unsigned int type)
167 {
168         struct tuner *t = i2c_get_clientdata(c);
169
170         tuner_dbg ("I2C addr 0x%02x with type %d\n",c->addr<<1,type);
171         /* sanity check */
172         if (type == UNSET || type == TUNER_ABSENT)
173                 return;
174         if (type >= tuner_count)
175                 return;
176
177         if (NULL == t->i2c.dev.driver) {
178                 /* not registered yet */
179                 t->type = type;
180                 return;
181         }
182         if (t->initialized)
183                 /* run only once */
184                 return;
185
186         t->initialized = 1;
187
188         t->type = type;
189         switch (t->type) {
190         case TUNER_MT2032:
191                 microtune_init(c);
192                 break;
193         case TUNER_PHILIPS_TDA8290:
194                 tda8290_init(c);
195                 break;
196         default:
197                 default_tuner_init(c);
198                 break;
199         }
200 }
201
202 #ifdef CONFIG_TUNER_MULTI_I2C
203 #define CHECK_ADDR(tp,cmd,tun)  if (client->addr!=tp) { \
204                           return 0; } else \
205                           tuner_info ("Cmd %s accepted to "tun"\n",cmd);
206 #define CHECK_MODE(cmd) if (t->mode == V4L2_TUNER_RADIO) { \
207                         CHECK_ADDR(radio_tuner,cmd,"radio") } else \
208                         { CHECK_ADDR(tv_tuner,cmd,"TV"); }
209 #else
210 #define CHECK_ADDR(tp,cmd,tun) tuner_info ("Cmd %s accepted to "tun"\n",cmd);
211 #define CHECK_MODE(cmd) tuner_info ("Cmd %s accepted\n",cmd);
212 #endif
213
214 #ifdef CONFIG_TUNER_MULTI_I2C
215
216 static void set_addr(struct i2c_client *c, struct tuner_addr *tun_addr)
217 {
218         /* ADDR_UNSET defaults to first available tuner */
219         if ( tun_addr->addr == ADDR_UNSET ) {
220                 if (first_tuner != c->addr)
221                         return;
222                 switch (tun_addr->v4l2_tuner) {
223                 case V4L2_TUNER_RADIO:
224                         radio_tuner=c->addr;
225                         break;
226                 default:
227                         tv_tuner=c->addr;
228                         break;
229                 }
230         } else {
231                 /* Sets tuner to its configured value */
232                 switch (tun_addr->v4l2_tuner) {
233                 case V4L2_TUNER_RADIO:
234                         radio_tuner=tun_addr->addr;
235                         if ( tun_addr->addr == c->addr ) set_type(c,tun_addr->type);
236                         return;
237                 default:
238                         tv_tuner=tun_addr->addr;
239                         if ( tun_addr->addr == c->addr ) set_type(c,tun_addr->type);
240                         return;
241                 }
242         }
243         set_type(c,tun_addr->type);
244 }
245 #else
246 #define set_addr(c,tun_addr) set_type(c,(tun_addr)->type)
247 #endif
248
249 static char pal[] = "-";
250 module_param_string(pal, pal, sizeof(pal), 0644);
251
252 static int tuner_fixup_std(struct tuner *t)
253 {
254         if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
255                 /* get more precise norm info from insmod option */
256                 switch (pal[0]) {
257                 case 'b':
258                 case 'B':
259                 case 'g':
260                 case 'G':
261                         tuner_dbg("insmod fixup: PAL => PAL-BG\n");
262                         t->std = V4L2_STD_PAL_BG;
263                         break;
264                 case 'i':
265                 case 'I':
266                         tuner_dbg("insmod fixup: PAL => PAL-I\n");
267                         t->std = V4L2_STD_PAL_I;
268                         break;
269                 case 'd':
270                 case 'D':
271                 case 'k':
272                 case 'K':
273                         tuner_dbg("insmod fixup: PAL => PAL-DK\n");
274                         t->std = V4L2_STD_PAL_DK;
275                         break;
276                 }
277         }
278         return 0;
279 }
280
281 /* ---------------------------------------------------------------------- */
282
283 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
284 {
285         struct tuner *t;
286
287 #ifndef CONFIG_TUNER_MULTI_I2C
288         if (this_adap > 0)
289                 return -1;
290 #else
291         /* by default, first I2C card is both tv and radio tuner */
292         if (this_adap == 0) {
293                 first_tuner = addr;
294                 tv_tuner = addr;
295                 radio_tuner = addr;
296         }
297 #endif
298         this_adap++;
299
300         client_template.adapter = adap;
301         client_template.addr = addr;
302
303         t = kmalloc(sizeof(struct tuner),GFP_KERNEL);
304         if (NULL == t)
305                 return -ENOMEM;
306         memset(t,0,sizeof(struct tuner));
307         memcpy(&t->i2c,&client_template,sizeof(struct i2c_client));
308         i2c_set_clientdata(&t->i2c, t);
309         t->type       = UNSET;
310         t->radio_if2  = 10700*1000; /* 10.7MHz - FM radio */
311
312         i2c_attach_client(&t->i2c);
313         tuner_info("chip found @ 0x%x (%s)\n",
314                    addr << 1, adap->name);
315
316         set_type(&t->i2c, t->type);
317         return 0;
318 }
319
320 static int tuner_probe(struct i2c_adapter *adap)
321 {
322         if (0 != addr) {
323                 normal_i2c[0] = addr;
324                 normal_i2c[1] = I2C_CLIENT_END;
325         }
326         this_adap = 0;
327
328 #ifdef CONFIG_TUNER_MULTI_I2C
329         first_tuner = 0;
330         tv_tuner = 0;
331         radio_tuner = 0;
332 #endif
333
334         if (adap->class & I2C_CLASS_TV_ANALOG)
335                 return i2c_probe(adap, &addr_data, tuner_attach);
336         return 0;
337 }
338
339 static int tuner_detach(struct i2c_client *client)
340 {
341         struct tuner *t = i2c_get_clientdata(client);
342         int err;
343
344         err=i2c_detach_client(&t->i2c);
345         if (err) {
346                 tuner_warn ("Client deregistration failed, client not detached.\n");
347                 return err;
348         }
349
350         kfree(t);
351         return 0;
352 }
353
354 #define SWITCH_V4L2     if (!t->using_v4l2 && tuner_debug) \
355                           tuner_info("switching to v4l2\n"); \
356                           t->using_v4l2 = 1;
357 #define CHECK_V4L2      if (t->using_v4l2) { if (tuner_debug) \
358                           tuner_info("ignore v4l1 call\n"); \
359                           return 0; }
360
361 static int
362 tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
363 {
364         struct tuner *t = i2c_get_clientdata(client);
365         unsigned int *iarg = (int*)arg;
366
367         switch (cmd) {
368         /* --- configuration --- */
369         case TUNER_SET_TYPE:
370                 set_type(client,*iarg);
371                 break;
372         case TUNER_SET_TYPE_ADDR:
373                 set_addr(client,(struct tuner_addr *)arg);
374                 break;
375         case AUDC_SET_RADIO:
376                 t->mode = V4L2_TUNER_RADIO;
377                 CHECK_ADDR(tv_tuner,"AUDC_SET_RADIO","TV");
378
379                 if (V4L2_TUNER_RADIO != t->mode) {
380                         set_tv_freq(client,400 * 16);
381                 }
382                 break;
383         case AUDC_CONFIG_PINNACLE:
384                 CHECK_ADDR(tv_tuner,"AUDC_CONFIG_PINNACLE","TV");
385                 switch (*iarg) {
386                 case 2:
387                         tuner_dbg("pinnacle pal\n");
388                         t->radio_if2 = 33300 * 1000;
389                         break;
390                 case 3:
391                         tuner_dbg("pinnacle ntsc\n");
392                         t->radio_if2 = 41300 * 1000;
393                         break;
394                 }
395                 break;
396
397         /* --- v4l ioctls --- */
398         /* take care: bttv does userspace copying, we'll get a
399            kernel pointer here... */
400         case VIDIOCSCHAN:
401         {
402                 static const v4l2_std_id map[] = {
403                         [ VIDEO_MODE_PAL   ] = V4L2_STD_PAL,
404                         [ VIDEO_MODE_NTSC  ] = V4L2_STD_NTSC_M,
405                         [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
406                         [ 4 /* bttv */     ] = V4L2_STD_PAL_M,
407                         [ 5 /* bttv */     ] = V4L2_STD_PAL_N,
408                         [ 6 /* bttv */     ] = V4L2_STD_NTSC_M_JP,
409                 };
410                 struct video_channel *vc = arg;
411
412                 CHECK_V4L2;
413                 t->mode = V4L2_TUNER_ANALOG_TV;
414                 CHECK_ADDR(tv_tuner,"VIDIOCSCHAN","TV");
415
416                 if (vc->norm < ARRAY_SIZE(map))
417                         t->std = map[vc->norm];
418                 tuner_fixup_std(t);
419                 if (t->freq)
420                         set_tv_freq(client,t->freq);
421                 return 0;
422         }
423         case VIDIOCSFREQ:
424         {
425                 unsigned long *v = arg;
426
427                 CHECK_MODE("VIDIOCSFREQ");
428                 CHECK_V4L2;
429                 set_freq(client,*v);
430                 return 0;
431         }
432         case VIDIOCGTUNER:
433         {
434                 struct video_tuner *vt = arg;
435
436                 CHECK_ADDR(radio_tuner,"VIDIOCGTUNER","radio");
437                 CHECK_V4L2;
438                 if (V4L2_TUNER_RADIO == t->mode) {
439                         if (t->has_signal)
440                                 vt->signal = t->has_signal(client);
441                         if (t->is_stereo) {
442                                 if (t->is_stereo(client))
443                                         vt-> flags |= VIDEO_TUNER_STEREO_ON;
444                                 else
445                                         vt-> flags &= 0xffff ^ VIDEO_TUNER_STEREO_ON;
446                         }
447                         vt->flags |= V4L2_TUNER_CAP_LOW; /* Allow freqs at 62.5 Hz */
448                 }
449
450                 return 0;
451         }
452         case VIDIOCGAUDIO:
453         {
454                 struct video_audio *va = arg;
455
456                 CHECK_ADDR(radio_tuner,"VIDIOCGAUDIO","radio");
457                 CHECK_V4L2;
458                 if (V4L2_TUNER_RADIO == t->mode  &&  t->is_stereo)
459                         va->mode = t->is_stereo(client)
460                                 ? VIDEO_SOUND_STEREO
461                                 : VIDEO_SOUND_MONO;
462                 return 0;
463         }
464
465         case VIDIOC_S_STD:
466         {
467                 v4l2_std_id *id = arg;
468
469                 SWITCH_V4L2;
470                 t->mode = V4L2_TUNER_ANALOG_TV;
471                 CHECK_ADDR(tv_tuner,"VIDIOC_S_STD","TV");
472
473                 t->std = *id;
474                 tuner_fixup_std(t);
475                 if (t->freq)
476                         set_freq(client,t->freq);
477                 break;
478         }
479         case VIDIOC_S_FREQUENCY:
480         {
481                 struct v4l2_frequency *f = arg;
482
483                 CHECK_MODE("VIDIOC_S_FREQUENCY");
484                 SWITCH_V4L2;
485                 if (V4L2_TUNER_RADIO == f->type &&
486                     V4L2_TUNER_RADIO != t->mode)
487                         set_tv_freq(client,400*16);
488                 t->mode  = f->type;
489                 set_freq(client,f->frequency);
490                 break;
491         }
492         case VIDIOC_G_FREQUENCY:
493         {
494                 struct v4l2_frequency *f = arg;
495
496                 CHECK_MODE("VIDIOC_G_FREQUENCY");
497                 SWITCH_V4L2;
498                 f->type = t->mode;
499                 f->frequency = t->freq;
500                 break;
501         }
502         case VIDIOC_G_TUNER:
503         {
504                 struct v4l2_tuner *tuner = arg;
505
506                 CHECK_MODE("VIDIOC_G_TUNER");
507                 SWITCH_V4L2;
508                 if (V4L2_TUNER_RADIO == t->mode) {
509                         if (t->has_signal)
510                                 tuner -> signal = t->has_signal(client);
511                         if (t->is_stereo) {
512                                 if (t->is_stereo(client)) {
513                                         tuner -> capability |= V4L2_TUNER_CAP_STEREO;
514                                         tuner -> rxsubchans |= V4L2_TUNER_SUB_STEREO;
515                                 } else {
516                                         tuner -> rxsubchans &= 0xffff ^ V4L2_TUNER_SUB_STEREO;
517                                 }
518                         }
519                 }
520                 /* Wow to deal with V4L2_TUNER_CAP_LOW ? For now, it accepts from low at 62.5KHz step  to high at 62.5 Hz */
521                 tuner->rangelow = tv_range[0] * 16;
522 //              tuner->rangehigh = tv_range[1] * 16;
523 //              tuner->rangelow = tv_range[0] * 16384;
524                 tuner->rangehigh = tv_range[1] * 16384;
525                 break;
526         }
527         default:
528                 tuner_dbg ("Unimplemented IOCTL 0x%08x called to tuner.\n", cmd);
529                 /* nothing */
530                 break;
531         }
532
533         return 0;
534 }
535
536 static int tuner_suspend(struct device * dev, u32 state, u32 level)
537 {
538         struct i2c_client *c = container_of(dev, struct i2c_client, dev);
539         struct tuner *t = i2c_get_clientdata(c);
540
541         tuner_dbg("suspend\n");
542         /* FIXME: power down ??? */
543         return 0;
544 }
545
546 static int tuner_resume(struct device * dev, u32 level)
547 {
548         struct i2c_client *c = container_of(dev, struct i2c_client, dev);
549         struct tuner *t = i2c_get_clientdata(c);
550
551         tuner_dbg("resume\n");
552         if (t->freq)
553                 set_freq(c,t->freq);
554         return 0;
555 }
556
557 /* ----------------------------------------------------------------------- */
558
559 static struct i2c_driver driver = {
560         .owner          = THIS_MODULE,
561         .name           = "tuner",
562         .id             = I2C_DRIVERID_TUNER,
563         .flags          = I2C_DF_NOTIFY,
564         .attach_adapter = tuner_probe,
565         .detach_client  = tuner_detach,
566         .command        = tuner_command,
567         .driver = {
568                 .suspend = tuner_suspend,
569                 .resume  = tuner_resume,
570         },
571 };
572 static struct i2c_client client_template =
573 {
574         I2C_DEVNAME("(tuner unset)"),
575         .flags      = I2C_CLIENT_ALLOW_USE,
576         .driver     = &driver,
577 };
578
579 static int __init tuner_init_module(void)
580 {
581         return i2c_add_driver(&driver);
582 }
583
584 static void __exit tuner_cleanup_module(void)
585 {
586         i2c_del_driver(&driver);
587 }
588
589 module_init(tuner_init_module);
590 module_exit(tuner_cleanup_module);
591
592 /*
593  * Overrides for Emacs so that we follow Linus's tabbing style.
594  * ---------------------------------------------------------------------------
595  * Local variables:
596  * c-basic-offset: 8
597  * End:
598  */