[PATCH] I2C: Kill address ranges in non-sensors i2c chip drivers
[safe/jmp/linux-2.6] / drivers / media / video / tuner-core.c
1 /*
2  * $Id: tuner-core.c,v 1.5 2005/02/15 15:59:35 kraxel 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 I2C_CLIENT_INSMOD;
36
37 /* insmod options used at init time => read/only */
38 static unsigned int addr  =  0;
39 module_param(addr, int, 0444);
40
41 /* insmod options used at runtime => read/write */
42 unsigned int tuner_debug   = 0;
43 module_param(tuner_debug,       int, 0644);
44
45 static unsigned int tv_range[2]    = { 44, 958 };
46 static unsigned int radio_range[2] = { 65, 108 };
47
48 module_param_array(tv_range,    int, NULL, 0644);
49 module_param_array(radio_range, int, NULL, 0644);
50
51 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
52 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
53 MODULE_LICENSE("GPL");
54
55 static int this_adap;
56
57 static struct i2c_driver driver;
58 static struct i2c_client client_template;
59
60 /* ---------------------------------------------------------------------- */
61
62 // Set tuner frequency,  freq in Units of 62.5kHz = 1/16MHz
63 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
64 {
65         struct tuner *t = i2c_get_clientdata(c);
66
67         if (t->type == UNSET) {
68                 tuner_info("tuner type not set\n");
69                 return;
70         }
71         if (NULL == t->tv_freq) {
72                 tuner_info("Huh? tv_set is NULL?\n");
73                 return;
74         }
75         if (freq < tv_range[0]*16 || freq > tv_range[1]*16) {
76                 /* FIXME: better do that chip-specific, but
77                    right now we don't have that in the config
78                    struct and this way is still better than no
79                    check at all */
80                 tuner_info("TV freq (%d.%02d) out of range (%d-%d)\n",
81                            freq/16,freq%16*100/16,tv_range[0],tv_range[1]);
82                 return;
83         }
84         t->tv_freq(c,freq);
85 }
86
87 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
88 {
89         struct tuner *t = i2c_get_clientdata(c);
90
91         if (t->type == UNSET) {
92                 tuner_info("tuner type not set\n");
93                 return;
94         }
95         if (NULL == t->radio_freq) {
96                 tuner_info("no radio tuning for this one, sorry.\n");
97                 return;
98         }
99         if (freq < radio_range[0]*16 || freq > radio_range[1]*16) {
100                 tuner_info("radio freq (%d.%02d) out of range (%d-%d)\n",
101                            freq/16,freq%16*100/16,
102                            radio_range[0],radio_range[1]);
103                 return;
104         }
105         t->radio_freq(c,freq);
106 }
107
108 static void set_freq(struct i2c_client *c, unsigned long freq)
109 {
110         struct tuner *t = i2c_get_clientdata(c);
111
112         switch (t->mode) {
113         case V4L2_TUNER_RADIO:
114                 tuner_dbg("radio freq set to %lu.%02lu\n",
115                           freq/16,freq%16*100/16);
116                 set_radio_freq(c,freq);
117                 break;
118         case V4L2_TUNER_ANALOG_TV:
119         case V4L2_TUNER_DIGITAL_TV:
120                 tuner_dbg("tv freq set to %lu.%02lu\n",
121                           freq/16,freq%16*100/16);
122                 set_tv_freq(c, freq);
123                 break;
124         }
125         t->freq = freq;
126 }
127
128 static void set_type(struct i2c_client *c, unsigned int type)
129 {
130         struct tuner *t = i2c_get_clientdata(c);
131
132         /* sanity check */
133         if (type == UNSET  ||  type == TUNER_ABSENT)
134                 return;
135         if (type >= tuner_count)
136                 return;
137
138         if (NULL == t->i2c.dev.driver) {
139                 /* not registered yet */
140                 t->type = type;
141                 return;
142         }
143         if (t->initialized)
144                 /* run only once */
145                 return;
146
147         t->initialized = 1;
148         t->type = type;
149         switch (t->type) {
150         case TUNER_MT2032:
151                 microtune_init(c);
152                 break;
153         case TUNER_PHILIPS_TDA8290:
154                 tda8290_init(c);
155                 break;
156         default:
157                 default_tuner_init(c);
158                 break;
159         }
160 }
161
162 static char pal[] = "-";
163 module_param_string(pal, pal, sizeof(pal), 0644);
164
165 static int tuner_fixup_std(struct tuner *t)
166 {
167         if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
168                 /* get more precise norm info from insmod option */
169                 switch (pal[0]) {
170                 case 'b':
171                 case 'B':
172                 case 'g':
173                 case 'G':
174                         tuner_dbg("insmod fixup: PAL => PAL-BG\n");
175                         t->std = V4L2_STD_PAL_BG;
176                         break;
177                 case 'i':
178                 case 'I':
179                         tuner_dbg("insmod fixup: PAL => PAL-I\n");
180                         t->std = V4L2_STD_PAL_I;
181                         break;
182                 case 'd':
183                 case 'D':
184                 case 'k':
185                 case 'K':
186                         tuner_dbg("insmod fixup: PAL => PAL-DK\n");
187                         t->std = V4L2_STD_PAL_DK;
188                         break;
189                 }
190         }
191         return 0;
192 }
193
194 /* ---------------------------------------------------------------------- */
195
196 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
197 {
198         struct tuner *t;
199
200         if (this_adap > 0)
201                 return -1;
202         this_adap++;
203
204         client_template.adapter = adap;
205         client_template.addr = addr;
206
207         t = kmalloc(sizeof(struct tuner),GFP_KERNEL);
208         if (NULL == t)
209                 return -ENOMEM;
210         memset(t,0,sizeof(struct tuner));
211         memcpy(&t->i2c,&client_template,sizeof(struct i2c_client));
212         i2c_set_clientdata(&t->i2c, t);
213         t->type       = UNSET;
214         t->radio_if2  = 10700*1000; // 10.7MHz - FM radio
215
216         i2c_attach_client(&t->i2c);
217         tuner_info("chip found @ 0x%x (%s)\n",
218                    addr << 1, adap->name);
219         set_type(&t->i2c, t->type);
220         return 0;
221 }
222
223 static int tuner_probe(struct i2c_adapter *adap)
224 {
225         if (0 != addr) {
226                 normal_i2c[0] = addr;
227                 normal_i2c[1] = I2C_CLIENT_END;
228         }
229         this_adap = 0;
230
231         if (adap->class & I2C_CLASS_TV_ANALOG)
232                 return i2c_probe(adap, &addr_data, tuner_attach);
233         return 0;
234 }
235
236 static int tuner_detach(struct i2c_client *client)
237 {
238         struct tuner *t = i2c_get_clientdata(client);
239
240         i2c_detach_client(&t->i2c);
241         kfree(t);
242         return 0;
243 }
244
245 #define SWITCH_V4L2     if (!t->using_v4l2 && tuner_debug) \
246                           tuner_info("switching to v4l2\n"); \
247                           t->using_v4l2 = 1;
248 #define CHECK_V4L2      if (t->using_v4l2) { if (tuner_debug) \
249                           tuner_info("ignore v4l1 call\n"); \
250                           return 0; }
251
252 static int
253 tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
254 {
255         struct tuner *t = i2c_get_clientdata(client);
256         unsigned int *iarg = (int*)arg;
257
258         switch (cmd) {
259
260         /* --- configuration --- */
261         case TUNER_SET_TYPE:
262                 set_type(client,*iarg);
263                 break;
264         case AUDC_SET_RADIO:
265                 if (V4L2_TUNER_RADIO != t->mode) {
266                         set_tv_freq(client,400 * 16);
267                         t->mode = V4L2_TUNER_RADIO;
268                 }
269                 break;
270         case AUDC_CONFIG_PINNACLE:
271                 switch (*iarg) {
272                 case 2:
273                         tuner_dbg("pinnacle pal\n");
274                         t->radio_if2 = 33300 * 1000;
275                         break;
276                 case 3:
277                         tuner_dbg("pinnacle ntsc\n");
278                         t->radio_if2 = 41300 * 1000;
279                         break;
280                 }
281                 break;
282
283         /* --- v4l ioctls --- */
284         /* take care: bttv does userspace copying, we'll get a
285            kernel pointer here... */
286         case VIDIOCSCHAN:
287         {
288                 static const v4l2_std_id map[] = {
289                         [ VIDEO_MODE_PAL   ] = V4L2_STD_PAL,
290                         [ VIDEO_MODE_NTSC  ] = V4L2_STD_NTSC_M,
291                         [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
292                         [ 4 /* bttv */     ] = V4L2_STD_PAL_M,
293                         [ 5 /* bttv */     ] = V4L2_STD_PAL_N,
294                         [ 6 /* bttv */     ] = V4L2_STD_NTSC_M_JP,
295                 };
296                 struct video_channel *vc = arg;
297
298                 CHECK_V4L2;
299                 t->mode = V4L2_TUNER_ANALOG_TV;
300                 if (vc->norm < ARRAY_SIZE(map))
301                         t->std = map[vc->norm];
302                 tuner_fixup_std(t);
303                 if (t->freq)
304                         set_tv_freq(client,t->freq);
305                 return 0;
306         }
307         case VIDIOCSFREQ:
308         {
309                 unsigned long *v = arg;
310
311                 CHECK_V4L2;
312                 set_freq(client,*v);
313                 return 0;
314         }
315         case VIDIOCGTUNER:
316         {
317                 struct video_tuner *vt = arg;
318
319                 CHECK_V4L2;
320                 if (V4L2_TUNER_RADIO == t->mode  &&  t->has_signal)
321                         vt->signal = t->has_signal(client);
322                 return 0;
323         }
324         case VIDIOCGAUDIO:
325         {
326                 struct video_audio *va = arg;
327
328                 CHECK_V4L2;
329                 if (V4L2_TUNER_RADIO == t->mode  &&  t->is_stereo)
330                         va->mode = t->is_stereo(client)
331                                 ? VIDEO_SOUND_STEREO
332                                 : VIDEO_SOUND_MONO;
333                 return 0;
334         }
335
336         case VIDIOC_S_STD:
337         {
338                 v4l2_std_id *id = arg;
339
340                 SWITCH_V4L2;
341                 t->mode = V4L2_TUNER_ANALOG_TV;
342                 t->std = *id;
343                 tuner_fixup_std(t);
344                 if (t->freq)
345                         set_freq(client,t->freq);
346                 break;
347         }
348         case VIDIOC_S_FREQUENCY:
349         {
350                 struct v4l2_frequency *f = arg;
351
352                 SWITCH_V4L2;
353                 if (V4L2_TUNER_RADIO == f->type &&
354                     V4L2_TUNER_RADIO != t->mode)
355                         set_tv_freq(client,400*16);
356                 t->mode  = f->type;
357                 set_freq(client,f->frequency);
358                 break;
359         }
360         case VIDIOC_G_FREQUENCY:
361         {
362                 struct v4l2_frequency *f = arg;
363
364                 SWITCH_V4L2;
365                 f->type = t->mode;
366                 f->frequency = t->freq;
367                 break;
368         }
369         case VIDIOC_G_TUNER:
370         {
371                 struct v4l2_tuner *tuner = arg;
372
373                 SWITCH_V4L2;
374                 if (V4L2_TUNER_RADIO == t->mode  &&  t->has_signal)
375                         tuner->signal = t->has_signal(client);
376                 tuner->rangelow = tv_range[0] * 16;
377                 tuner->rangehigh = tv_range[1] * 16;
378                 break;
379         }
380         default:
381                 /* nothing */
382                 break;
383         }
384
385         return 0;
386 }
387
388 static int tuner_suspend(struct device * dev, pm_message_t state, u32 level)
389 {
390         struct i2c_client *c = container_of(dev, struct i2c_client, dev);
391         struct tuner *t = i2c_get_clientdata(c);
392
393         tuner_dbg("suspend\n");
394         /* FIXME: power down ??? */
395         return 0;
396 }
397
398 static int tuner_resume(struct device * dev, u32 level)
399 {
400         struct i2c_client *c = container_of(dev, struct i2c_client, dev);
401         struct tuner *t = i2c_get_clientdata(c);
402
403         tuner_dbg("resume\n");
404         if (t->freq)
405                 set_freq(c,t->freq);
406         return 0;
407 }
408
409 /* ----------------------------------------------------------------------- */
410
411 static struct i2c_driver driver = {
412         .owner          = THIS_MODULE,
413         .name           = "tuner",
414         .id             = I2C_DRIVERID_TUNER,
415         .flags          = I2C_DF_NOTIFY,
416         .attach_adapter = tuner_probe,
417         .detach_client  = tuner_detach,
418         .command        = tuner_command,
419         .driver = {
420                 .suspend = tuner_suspend,
421                 .resume  = tuner_resume,
422         },
423 };
424 static struct i2c_client client_template =
425 {
426         I2C_DEVNAME("(tuner unset)"),
427         .flags      = I2C_CLIENT_ALLOW_USE,
428         .driver     = &driver,
429 };
430
431 static int __init tuner_init_module(void)
432 {
433         return i2c_add_driver(&driver);
434 }
435
436 static void __exit tuner_cleanup_module(void)
437 {
438         i2c_del_driver(&driver);
439 }
440
441 module_init(tuner_init_module);
442 module_exit(tuner_cleanup_module);
443
444 /*
445  * Overrides for Emacs so that we follow Linus's tabbing style.
446  * ---------------------------------------------------------------------------
447  * Local variables:
448  * c-basic-offset: 8
449  * End:
450  */