[PATCH] tuner-core.c improvments and Ymec Tvision TVF8533MF support
[linux-2.6.git] / drivers / media / video / tuner-core.c
1 /*
2  * $Id: tuner-core.c,v 1.7 2005/05/30 02:02:47 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 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                 /* FIXME: better do that chip-specific, but
85                    right now we don't have that in the config
86                    struct and this way is still better than no
87                    check at all */
88                 tuner_info("TV freq (%d.%02d) out of range (%d-%d)\n",
89                            freq/16,freq%16*100/16,tv_range[0],tv_range[1]);
90                 return;
91         }
92         t->tv_freq(c,freq);
93 }
94
95 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
96 {
97         struct tuner *t = i2c_get_clientdata(c);
98
99         if (t->type == UNSET) {
100                 tuner_info("tuner type not set\n");
101                 return;
102         }
103         if (NULL == t->radio_freq) {
104                 tuner_info("no radio tuning for this one, sorry.\n");
105                 return;
106         }
107         if (freq < radio_range[0]*16 || freq > radio_range[1]*16) {
108                 tuner_info("radio freq (%d.%02d) out of range (%d-%d)\n",
109                            freq/16,freq%16*100/16,
110                            radio_range[0],radio_range[1]);
111                 return;
112         }
113         t->radio_freq(c,freq);
114 }
115
116 static void set_freq(struct i2c_client *c, unsigned long freq)
117 {
118         struct tuner *t = i2c_get_clientdata(c);
119
120         switch (t->mode) {
121         case V4L2_TUNER_RADIO:
122                 tuner_dbg("radio freq set to %lu.%02lu\n",
123                           freq/16,freq%16*100/16);
124                 set_radio_freq(c,freq);
125                 break;
126         case V4L2_TUNER_ANALOG_TV:
127         case V4L2_TUNER_DIGITAL_TV:
128                 tuner_dbg("tv freq set to %lu.%02lu\n",
129                           freq/16,freq%16*100/16);
130                 set_tv_freq(c, freq);
131                 break;
132         }
133         t->freq = freq;
134 }
135
136 #ifdef CONFIG_TUNER_MULTI_I2C
137 static void set_addr(struct i2c_client *c, struct tuner_addr *tun_addr)
138 {
139         struct tuner *t = i2c_get_clientdata(c);
140
141         switch (tun_addr->type) {
142         case V4L2_TUNER_RADIO:
143                 radio_tuner=tun_addr->addr;
144                 tuner_dbg("radio tuner set to I2C address 0x%02x\n",radio_tuner<<1);
145
146                 break;
147         default:
148                 tv_tuner=tun_addr->addr;
149                 tuner_dbg("TV tuner set to I2C address 0x%02x\n",tv_tuner<<1);
150                 break;
151         }
152 }
153 #else
154 #define set_addr(c,tun_addr) \
155                 tuner_warn("It is recommended to enable CONFIG_TUNER_MULTI_I2C for this card.\n");
156 #endif
157
158 static void set_type(struct i2c_client *c, unsigned int type)
159 {
160         struct tuner *t = i2c_get_clientdata(c);
161
162         /* sanity check */
163         if (type == UNSET  ||  type == TUNER_ABSENT)
164                 return;
165         if (type >= tuner_count)
166                 return;
167
168         if (NULL == t->i2c.dev.driver) {
169                 /* not registered yet */
170                 t->type = type;
171                 return;
172         }
173         if (t->initialized)
174                 /* run only once */
175                 return;
176
177         t->initialized = 1;
178         t->type = type;
179         switch (t->type) {
180         case TUNER_MT2032:
181                 microtune_init(c);
182                 break;
183         case TUNER_PHILIPS_TDA8290:
184                 tda8290_init(c);
185                 break;
186         default:
187                 default_tuner_init(c);
188                 break;
189         }
190 }
191
192 static char pal[] = "-";
193 module_param_string(pal, pal, sizeof(pal), 0644);
194
195 static int tuner_fixup_std(struct tuner *t)
196 {
197         if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
198                 /* get more precise norm info from insmod option */
199                 switch (pal[0]) {
200                 case 'b':
201                 case 'B':
202                 case 'g':
203                 case 'G':
204                         tuner_dbg("insmod fixup: PAL => PAL-BG\n");
205                         t->std = V4L2_STD_PAL_BG;
206                         break;
207                 case 'i':
208                 case 'I':
209                         tuner_dbg("insmod fixup: PAL => PAL-I\n");
210                         t->std = V4L2_STD_PAL_I;
211                         break;
212                 case 'd':
213                 case 'D':
214                 case 'k':
215                 case 'K':
216                         tuner_dbg("insmod fixup: PAL => PAL-DK\n");
217                         t->std = V4L2_STD_PAL_DK;
218                         break;
219                 }
220         }
221         return 0;
222 }
223
224 /* ---------------------------------------------------------------------- */
225
226 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
227 {
228         struct tuner *t;
229
230 #ifndef CONFIG_TUNER_MULTI_I2C
231         if (this_adap > 0)
232                 return -1;
233 #else
234         /* by default, first I2C card is both tv and radio tuner */
235         if (this_adap == 0) {
236                 tv_tuner = addr;
237                 radio_tuner = addr;
238         }
239 #endif
240         this_adap++;
241
242         client_template.adapter = adap;
243         client_template.addr = addr;
244
245         t = kmalloc(sizeof(struct tuner),GFP_KERNEL);
246         if (NULL == t)
247                 return -ENOMEM;
248         memset(t,0,sizeof(struct tuner));
249         memcpy(&t->i2c,&client_template,sizeof(struct i2c_client));
250         i2c_set_clientdata(&t->i2c, t);
251         t->type       = UNSET;
252         t->radio_if2  = 10700*1000; // 10.7MHz - FM radio
253
254         i2c_attach_client(&t->i2c);
255         tuner_info("chip found @ 0x%x (%s)\n",
256                    addr << 1, adap->name);
257         set_type(&t->i2c, t->type);
258         return 0;
259 }
260
261 static int tuner_probe(struct i2c_adapter *adap)
262 {
263         if (0 != addr) {
264                 normal_i2c[0] = addr;
265                 normal_i2c[1] = I2C_CLIENT_END;
266         }
267         this_adap = 0;
268
269 #ifdef CONFIG_TUNER_MULTI_I2C
270         tv_tuner = 0;
271         radio_tuner = 0;
272 #endif
273
274         if (adap->class & I2C_CLASS_TV_ANALOG)
275                 return i2c_probe(adap, &addr_data, tuner_attach);
276         return 0;
277 }
278
279 static int tuner_detach(struct i2c_client *client)
280 {
281         struct tuner *t = i2c_get_clientdata(client);
282         int err;
283
284         err=i2c_detach_client(&t->i2c);
285         if (err) {
286                 tuner_warn ("Client deregistration failed, client not detached.\n");
287                 return err;
288         }
289
290         kfree(t);
291         return 0;
292 }
293
294 #define SWITCH_V4L2     if (!t->using_v4l2 && tuner_debug) \
295                           tuner_info("switching to v4l2\n"); \
296                           t->using_v4l2 = 1;
297 #define CHECK_V4L2      if (t->using_v4l2) { if (tuner_debug) \
298                           tuner_info("ignore v4l1 call\n"); \
299                           return 0; }
300
301 #ifdef CONFIG_TUNER_MULTI_I2C
302 #define CHECK_ADDR(tp,cmd)      if (client->addr!=tp) { \
303                           tuner_info ("Cmd %s to addr 0x%02x rejected.\n",cmd,client->addr<<1); \
304                           return 0; }
305 #define CHECK_MODE(cmd) if (t->mode == V4L2_TUNER_RADIO) { \
306                           CHECK_ADDR(radio_tuner,cmd) } else { CHECK_ADDR(tv_tuner,cmd); }
307 #else
308 #define CHECK_ADDR(tp,cmd)
309 #define CHECK_MODE(cmd)
310 #endif
311
312 static int
313 tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
314 {
315         struct tuner *t = i2c_get_clientdata(client);
316         unsigned int *iarg = (int*)arg;
317
318         switch (cmd) {
319         /* --- configuration --- */
320         case TUNER_SET_TYPE:
321                 set_type(client,*iarg);
322                 break;
323         case TUNER_SET_ADDR:
324                 set_addr(client,(struct tuner_addr *)arg);
325                 break;
326         case AUDC_SET_RADIO:
327                 CHECK_ADDR(radio_tuner,"AUDC_SET_RADIO");
328
329                 if (V4L2_TUNER_RADIO != t->mode) {
330                         set_tv_freq(client,400 * 16);
331                         t->mode = V4L2_TUNER_RADIO;
332                 }
333                 break;
334         case AUDC_CONFIG_PINNACLE:
335                 CHECK_ADDR(tv_tuner,"AUDC_CONFIG_PINNACLE");
336                 switch (*iarg) {
337                 case 2:
338                         tuner_dbg("pinnacle pal\n");
339                         t->radio_if2 = 33300 * 1000;
340                         break;
341                 case 3:
342                         tuner_dbg("pinnacle ntsc\n");
343                         t->radio_if2 = 41300 * 1000;
344                         break;
345                 }
346                 break;
347
348         /* --- v4l ioctls --- */
349         /* take care: bttv does userspace copying, we'll get a
350            kernel pointer here... */
351         case VIDIOCSCHAN:
352         {
353                 static const v4l2_std_id map[] = {
354                         [ VIDEO_MODE_PAL   ] = V4L2_STD_PAL,
355                         [ VIDEO_MODE_NTSC  ] = V4L2_STD_NTSC_M,
356                         [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
357                         [ 4 /* bttv */     ] = V4L2_STD_PAL_M,
358                         [ 5 /* bttv */     ] = V4L2_STD_PAL_N,
359                         [ 6 /* bttv */     ] = V4L2_STD_NTSC_M_JP,
360                 };
361                 struct video_channel *vc = arg;
362
363                 CHECK_ADDR(tv_tuner,"VIDIOCSCHAN");
364                 CHECK_V4L2;
365                 t->mode = V4L2_TUNER_ANALOG_TV;
366                 if (vc->norm < ARRAY_SIZE(map))
367                         t->std = map[vc->norm];
368                 tuner_fixup_std(t);
369                 if (t->freq)
370                         set_tv_freq(client,t->freq);
371                 return 0;
372         }
373         case VIDIOCSFREQ:
374         {
375                 unsigned long *v = arg;
376
377                 CHECK_MODE("VIDIOCSFREQ");
378                 CHECK_V4L2;
379                 set_freq(client,*v);
380                 return 0;
381         }
382         case VIDIOCGTUNER:
383         {
384                 struct video_tuner *vt = arg;
385
386                 CHECK_ADDR(radio_tuner,"VIDIOCGTUNER:");
387                 CHECK_V4L2;
388                 if (V4L2_TUNER_RADIO == t->mode  &&  t->has_signal)
389                         vt->signal = t->has_signal(client);
390                 return 0;
391         }
392         case VIDIOCGAUDIO:
393         {
394                 struct video_audio *va = arg;
395
396                 CHECK_ADDR(radio_tuner,"VIDIOCGAUDIO");
397                 CHECK_V4L2;
398                 if (V4L2_TUNER_RADIO == t->mode  &&  t->is_stereo)
399                         va->mode = t->is_stereo(client)
400                                 ? VIDEO_SOUND_STEREO
401                                 : VIDEO_SOUND_MONO;
402                 return 0;
403         }
404
405         case VIDIOC_S_STD:
406         {
407                 v4l2_std_id *id = arg;
408
409                 CHECK_ADDR(tv_tuner,"VIDIOC_S_STD");
410                 SWITCH_V4L2;
411                 t->mode = V4L2_TUNER_ANALOG_TV;
412                 t->std = *id;
413                 tuner_fixup_std(t);
414                 if (t->freq)
415                         set_freq(client,t->freq);
416                 break;
417         }
418         case VIDIOC_S_FREQUENCY:
419         {
420                 struct v4l2_frequency *f = arg;
421
422                 CHECK_MODE("VIDIOC_S_FREQUENCY");
423                 SWITCH_V4L2;
424                 if (V4L2_TUNER_RADIO == f->type &&
425                     V4L2_TUNER_RADIO != t->mode)
426                         set_tv_freq(client,400*16);
427                 t->mode  = f->type;
428                 set_freq(client,f->frequency);
429                 break;
430         }
431         case VIDIOC_G_FREQUENCY:
432         {
433                 struct v4l2_frequency *f = arg;
434
435                 CHECK_MODE("VIDIOC_G_FREQUENCY");
436                 SWITCH_V4L2;
437                 f->type = t->mode;
438                 f->frequency = t->freq;
439                 break;
440         }
441         case VIDIOC_G_TUNER:
442         {
443                 struct v4l2_tuner *tuner = arg;
444
445                 CHECK_MODE("VIDIOC_G_TUNER");
446                 SWITCH_V4L2;
447                 if (V4L2_TUNER_RADIO == t->mode  &&  t->has_signal)
448                         tuner->signal = t->has_signal(client);
449                 tuner->rangelow = tv_range[0] * 16;
450                 tuner->rangehigh = tv_range[1] * 16;
451                 break;
452         }
453         default:
454                 /* nothing */
455                 break;
456         }
457
458         return 0;
459 }
460
461 static int tuner_suspend(struct device * dev, pm_message_t state, u32 level)
462 {
463         struct i2c_client *c = container_of(dev, struct i2c_client, dev);
464         struct tuner *t = i2c_get_clientdata(c);
465
466         tuner_dbg("suspend\n");
467         /* FIXME: power down ??? */
468         return 0;
469 }
470
471 static int tuner_resume(struct device * dev, u32 level)
472 {
473         struct i2c_client *c = container_of(dev, struct i2c_client, dev);
474         struct tuner *t = i2c_get_clientdata(c);
475
476         tuner_dbg("resume\n");
477         if (t->freq)
478                 set_freq(c,t->freq);
479         return 0;
480 }
481
482 /* ----------------------------------------------------------------------- */
483
484 static struct i2c_driver driver = {
485         .owner          = THIS_MODULE,
486         .name           = "tuner",
487         .id             = I2C_DRIVERID_TUNER,
488         .flags          = I2C_DF_NOTIFY,
489         .attach_adapter = tuner_probe,
490         .detach_client  = tuner_detach,
491         .command        = tuner_command,
492         .driver = {
493                 .suspend = tuner_suspend,
494                 .resume  = tuner_resume,
495         },
496 };
497 static struct i2c_client client_template =
498 {
499         I2C_DEVNAME("(tuner unset)"),
500         .flags      = I2C_CLIENT_ALLOW_USE,
501         .driver     = &driver,
502 };
503
504 static int __init tuner_init_module(void)
505 {
506         return i2c_add_driver(&driver);
507 }
508
509 static void __exit tuner_cleanup_module(void)
510 {
511         i2c_del_driver(&driver);
512 }
513
514 module_init(tuner_init_module);
515 module_exit(tuner_cleanup_module);
516
517 /*
518  * Overrides for Emacs so that we follow Linus's tabbing style.
519  * ---------------------------------------------------------------------------
520  * Local variables:
521  * c-basic-offset: 8
522  * End:
523  */