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