Merge branch 'for-linus' of git://git.o-hand.com/linux-rpurdie-backlight
[linux-2.6] / drivers / media / video / tuner-core.c
1 /*
2  *
3  * i2c tv tuner chip device driver
4  * core core, i.e. kernel interfaces, registering and so on
5  */
6
7 #include <linux/module.h>
8 #include <linux/moduleparam.h>
9 #include <linux/kernel.h>
10 #include <linux/string.h>
11 #include <linux/timer.h>
12 #include <linux/delay.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/poll.h>
16 #include <linux/i2c.h>
17 #include <linux/types.h>
18 #include <linux/videodev.h>
19 #include <linux/init.h>
20
21 #include <media/tuner.h>
22 #include <media/v4l2-common.h>
23 #include "tuner-driver.h"
24
25 #define UNSET (-1U)
26
27 /* standard i2c insmod options */
28 static unsigned short normal_i2c[] = {
29 #ifdef CONFIG_TUNER_TEA5761
30         0x10,
31 #endif
32         0x42, 0x43, 0x4a, 0x4b,                 /* tda8290 */
33         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
34         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
35         I2C_CLIENT_END
36 };
37
38 I2C_CLIENT_INSMOD;
39
40 /* insmod options used at init time => read/only */
41 static unsigned int addr = 0;
42 static unsigned int no_autodetect = 0;
43 static unsigned int show_i2c = 0;
44
45 /* insmod options used at runtime => read/write */
46 int tuner_debug = 0;
47
48 static unsigned int tv_range[2] = { 44, 958 };
49 static unsigned int radio_range[2] = { 65, 108 };
50
51 static char pal[] = "--";
52 static char secam[] = "--";
53 static char ntsc[] = "-";
54
55
56 module_param(addr, int, 0444);
57 module_param(no_autodetect, int, 0444);
58 module_param(show_i2c, int, 0444);
59 module_param_named(debug,tuner_debug, int, 0644);
60 module_param_string(pal, pal, sizeof(pal), 0644);
61 module_param_string(secam, secam, sizeof(secam), 0644);
62 module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
63 module_param_array(tv_range, int, NULL, 0644);
64 module_param_array(radio_range, int, NULL, 0644);
65
66 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
67 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
68 MODULE_LICENSE("GPL");
69
70 static struct i2c_driver driver;
71 static struct i2c_client client_template;
72
73 /* ---------------------------------------------------------------------- */
74
75 /* Set tuner frequency,  freq in Units of 62.5kHz = 1/16MHz */
76 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
77 {
78         struct tuner *t = i2c_get_clientdata(c);
79
80         if (t->type == UNSET) {
81                 tuner_warn ("tuner type not set\n");
82                 return;
83         }
84         if (NULL == t->ops.set_tv_freq) {
85                 tuner_warn ("Tuner has no way to set tv freq\n");
86                 return;
87         }
88         if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
89                 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
90                            freq / 16, freq % 16 * 100 / 16, tv_range[0],
91                            tv_range[1]);
92                 /* V4L2 spec: if the freq is not possible then the closest
93                    possible value should be selected */
94                 if (freq < tv_range[0] * 16)
95                         freq = tv_range[0] * 16;
96                 else
97                         freq = tv_range[1] * 16;
98         }
99         t->ops.set_tv_freq(c, freq);
100 }
101
102 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
103 {
104         struct tuner *t = i2c_get_clientdata(c);
105
106         if (t->type == UNSET) {
107                 tuner_warn ("tuner type not set\n");
108                 return;
109         }
110         if (NULL == t->ops.set_radio_freq) {
111                 tuner_warn ("tuner has no way to set radio frequency\n");
112                 return;
113         }
114         if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
115                 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
116                            freq / 16000, freq % 16000 * 100 / 16000,
117                            radio_range[0], radio_range[1]);
118                 /* V4L2 spec: if the freq is not possible then the closest
119                    possible value should be selected */
120                 if (freq < radio_range[0] * 16000)
121                         freq = radio_range[0] * 16000;
122                 else
123                         freq = radio_range[1] * 16000;
124         }
125
126         t->ops.set_radio_freq(c, freq);
127 }
128
129 static void set_freq(struct i2c_client *c, unsigned long freq)
130 {
131         struct tuner *t = i2c_get_clientdata(c);
132
133         switch (t->mode) {
134         case V4L2_TUNER_RADIO:
135                 tuner_dbg("radio freq set to %lu.%02lu\n",
136                           freq / 16000, freq % 16000 * 100 / 16000);
137                 set_radio_freq(c, freq);
138                 t->radio_freq = freq;
139                 break;
140         case V4L2_TUNER_ANALOG_TV:
141         case V4L2_TUNER_DIGITAL_TV:
142                 tuner_dbg("tv freq set to %lu.%02lu\n",
143                           freq / 16, freq % 16 * 100 / 16);
144                 set_tv_freq(c, freq);
145                 t->tv_freq = freq;
146                 break;
147         }
148 }
149
150 static void set_type(struct i2c_client *c, unsigned int type,
151                      unsigned int new_mode_mask, unsigned int new_config,
152                      int (*tuner_callback) (void *dev, int command,int arg))
153 {
154         struct tuner *t = i2c_get_clientdata(c);
155         unsigned char buffer[4];
156
157         if (type == UNSET || type == TUNER_ABSENT) {
158                 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
159                 return;
160         }
161
162         if (type >= tuner_count) {
163                 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
164                 return;
165         }
166
167         t->type = type;
168         t->config = new_config;
169         if (tuner_callback != NULL) {
170                 tuner_dbg("defining GPIO callback\n");
171                 t->tuner_callback = tuner_callback;
172         }
173
174         /* This code detects calls by card attach_inform */
175         if (NULL == t->i2c.dev.driver) {
176                 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
177
178                 return;
179         }
180
181         /* discard private data, in case set_type() was previously called */
182         if (t->ops.release)
183                 t->ops.release(c);
184         else {
185                 kfree(t->priv);
186                 t->priv = NULL;
187         }
188
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         case TUNER_TEA5767:
197                 if (tea5767_tuner_init(c) == EINVAL) {
198                         t->type = TUNER_ABSENT;
199                         t->mode_mask = T_UNINITIALIZED;
200                         return;
201                 }
202                 t->mode_mask = T_RADIO;
203                 break;
204 #ifdef CONFIG_TUNER_TEA5761
205         case TUNER_TEA5761:
206                 if (tea5761_tuner_init(c) == EINVAL) {
207                         t->type = TUNER_ABSENT;
208                         t->mode_mask = T_UNINITIALIZED;
209                         return;
210                 }
211                 t->mode_mask = T_RADIO;
212                 break;
213 #endif
214         case TUNER_PHILIPS_FMD1216ME_MK3:
215                 buffer[0] = 0x0b;
216                 buffer[1] = 0xdc;
217                 buffer[2] = 0x9c;
218                 buffer[3] = 0x60;
219                 i2c_master_send(c, buffer, 4);
220                 mdelay(1);
221                 buffer[2] = 0x86;
222                 buffer[3] = 0x54;
223                 i2c_master_send(c, buffer, 4);
224                 default_tuner_init(c);
225                 break;
226         case TUNER_PHILIPS_TD1316:
227                 buffer[0] = 0x0b;
228                 buffer[1] = 0xdc;
229                 buffer[2] = 0x86;
230                 buffer[3] = 0xa4;
231                 i2c_master_send(c,buffer,4);
232                 default_tuner_init(c);
233                 break;
234         case TUNER_TDA9887:
235                 tda9887_tuner_init(c);
236                 break;
237         default:
238                 default_tuner_init(c);
239                 break;
240         }
241
242         if (t->mode_mask == T_UNINITIALIZED)
243                 t->mode_mask = new_mode_mask;
244
245         set_freq(c, (V4L2_TUNER_RADIO == t->mode) ? t->radio_freq : t->tv_freq);
246         tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
247                   c->adapter->name, c->driver->driver.name, c->addr << 1, type,
248                   t->mode_mask);
249 }
250
251 /*
252  * This function apply tuner config to tuner specified
253  * by tun_setup structure. I addr is unset, then admin status
254  * and tun addr status is more precise then current status,
255  * it's applied. Otherwise status and type are applied only to
256  * tuner with exactly the same addr.
257 */
258
259 static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
260 {
261         struct tuner *t = i2c_get_clientdata(c);
262
263         tuner_dbg("set addr for type %i\n", t->type);
264
265         if ( (t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
266                 (t->mode_mask & tun_setup->mode_mask))) ||
267                 (tun_setup->addr == c->addr)) {
268                         set_type(c, tun_setup->type, tun_setup->mode_mask,
269                                  tun_setup->config, tun_setup->tuner_callback);
270         }
271 }
272
273 static inline int check_mode(struct tuner *t, char *cmd)
274 {
275         if ((1 << t->mode & t->mode_mask) == 0) {
276                 return EINVAL;
277         }
278
279         switch (t->mode) {
280         case V4L2_TUNER_RADIO:
281                 tuner_dbg("Cmd %s accepted for radio\n", cmd);
282                 break;
283         case V4L2_TUNER_ANALOG_TV:
284                 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
285                 break;
286         case V4L2_TUNER_DIGITAL_TV:
287                 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
288                 break;
289         }
290         return 0;
291 }
292
293 /* get more precise norm info from insmod option */
294 static int tuner_fixup_std(struct tuner *t)
295 {
296         if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
297                 switch (pal[0]) {
298                 case '6':
299                         tuner_dbg ("insmod fixup: PAL => PAL-60\n");
300                         t->std = V4L2_STD_PAL_60;
301                         break;
302                 case 'b':
303                 case 'B':
304                 case 'g':
305                 case 'G':
306                         tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
307                         t->std = V4L2_STD_PAL_BG;
308                         break;
309                 case 'i':
310                 case 'I':
311                         tuner_dbg ("insmod fixup: PAL => PAL-I\n");
312                         t->std = V4L2_STD_PAL_I;
313                         break;
314                 case 'd':
315                 case 'D':
316                 case 'k':
317                 case 'K':
318                         tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
319                         t->std = V4L2_STD_PAL_DK;
320                         break;
321                 case 'M':
322                 case 'm':
323                         tuner_dbg ("insmod fixup: PAL => PAL-M\n");
324                         t->std = V4L2_STD_PAL_M;
325                         break;
326                 case 'N':
327                 case 'n':
328                         if (pal[1] == 'c' || pal[1] == 'C') {
329                                 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
330                                 t->std = V4L2_STD_PAL_Nc;
331                         } else {
332                                 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
333                                 t->std = V4L2_STD_PAL_N;
334                         }
335                         break;
336                 case '-':
337                         /* default parameter, do nothing */
338                         break;
339                 default:
340                         tuner_warn ("pal= argument not recognised\n");
341                         break;
342                 }
343         }
344         if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
345                 switch (secam[0]) {
346                 case 'b':
347                 case 'B':
348                 case 'g':
349                 case 'G':
350                 case 'h':
351                 case 'H':
352                         tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
353                         t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
354                         break;
355                 case 'd':
356                 case 'D':
357                 case 'k':
358                 case 'K':
359                         tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
360                         t->std = V4L2_STD_SECAM_DK;
361                         break;
362                 case 'l':
363                 case 'L':
364                         if ((secam[1]=='C')||(secam[1]=='c')) {
365                                 tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n");
366                                 t->std = V4L2_STD_SECAM_LC;
367                         } else {
368                                 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
369                                 t->std = V4L2_STD_SECAM_L;
370                         }
371                         break;
372                 case '-':
373                         /* default parameter, do nothing */
374                         break;
375                 default:
376                         tuner_warn ("secam= argument not recognised\n");
377                         break;
378                 }
379         }
380
381         if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
382                 switch (ntsc[0]) {
383                 case 'm':
384                 case 'M':
385                         tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
386                         t->std = V4L2_STD_NTSC_M;
387                         break;
388                 case 'j':
389                 case 'J':
390                         tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
391                         t->std = V4L2_STD_NTSC_M_JP;
392                         break;
393                 case 'k':
394                 case 'K':
395                         tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
396                         t->std = V4L2_STD_NTSC_M_KR;
397                         break;
398                 case '-':
399                         /* default parameter, do nothing */
400                         break;
401                 default:
402                         tuner_info("ntsc= argument not recognised\n");
403                         break;
404                 }
405         }
406         return 0;
407 }
408
409 static void tuner_status(struct i2c_client *client)
410 {
411         struct tuner *t = i2c_get_clientdata(client);
412         unsigned long freq, freq_fraction;
413         const char *p;
414
415         switch (t->mode) {
416                 case V4L2_TUNER_RADIO:      p = "radio"; break;
417                 case V4L2_TUNER_ANALOG_TV:  p = "analog TV"; break;
418                 case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break;
419                 default: p = "undefined"; break;
420         }
421         if (t->mode == V4L2_TUNER_RADIO) {
422                 freq = t->radio_freq / 16000;
423                 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
424         } else {
425                 freq = t->tv_freq / 16;
426                 freq_fraction = (t->tv_freq % 16) * 100 / 16;
427         }
428         tuner_info("Tuner mode:      %s\n", p);
429         tuner_info("Frequency:       %lu.%02lu MHz\n", freq, freq_fraction);
430         tuner_info("Standard:        0x%08lx\n", (unsigned long)t->std);
431         if (t->mode != V4L2_TUNER_RADIO)
432                return;
433         if (t->ops.has_signal) {
434                 tuner_info("Signal strength: %d\n", t->ops.has_signal(client));
435         }
436         if (t->ops.is_stereo) {
437                 tuner_info("Stereo:          %s\n", t->ops.is_stereo(client) ? "yes" : "no");
438         }
439 }
440
441 /* ---------------------------------------------------------------------- */
442
443 /* static vars: used only in tuner_attach and tuner_probe */
444 static unsigned default_mode_mask;
445
446 /* During client attach, set_type is called by adapter's attach_inform callback.
447    set_type must then be completed by tuner_attach.
448  */
449 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
450 {
451         struct tuner *t;
452
453         client_template.adapter = adap;
454         client_template.addr = addr;
455
456         t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
457         if (NULL == t)
458                 return -ENOMEM;
459         memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
460         i2c_set_clientdata(&t->i2c, t);
461         t->type = UNSET;
462         t->audmode = V4L2_TUNER_MODE_STEREO;
463         t->mode_mask = T_UNINITIALIZED;
464         t->ops.tuner_status = tuner_status;
465
466         if (show_i2c) {
467                 unsigned char buffer[16];
468                 int i,rc;
469
470                 memset(buffer, 0, sizeof(buffer));
471                 rc = i2c_master_recv(&t->i2c, buffer, sizeof(buffer));
472                 tuner_info("I2C RECV = ");
473                 for (i=0;i<rc;i++)
474                         printk("%02x ",buffer[i]);
475                 printk("\n");
476         }
477         /* HACK: This test were added to avoid tuner to probe tda9840 and tea6415c on the MXB card */
478         if (adap->id == I2C_HW_SAA7146 && addr < 0x4a)
479                 return -ENODEV;
480
481         /* autodetection code based on the i2c addr */
482         if (!no_autodetect) {
483                 switch (addr) {
484 #ifdef CONFIG_TUNER_TEA5761
485                 case 0x10:
486                         if (tea5761_autodetection(&t->i2c) != EINVAL) {
487                                 t->type = TUNER_TEA5761;
488                                 t->mode_mask = T_RADIO;
489                                 t->mode = T_STANDBY;
490                                 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
491                                 default_mode_mask &= ~T_RADIO;
492
493                                 goto register_client;
494                         }
495                         break;
496 #endif
497                 case 0x42:
498                 case 0x43:
499                 case 0x4a:
500                 case 0x4b:
501                         /* If chip is not tda8290, don't register.
502                            since it can be tda9887*/
503                         if (tda8290_probe(&t->i2c) == 0) {
504                                 tuner_dbg("chip at addr %x is a tda8290\n", addr);
505                         } else {
506                                 /* Default is being tda9887 */
507                                 t->type = TUNER_TDA9887;
508                                 t->mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
509                                 t->mode = T_STANDBY;
510                                 goto register_client;
511                         }
512                         break;
513                 case 0x60:
514                         if (tea5767_autodetection(&t->i2c) != EINVAL) {
515                                 t->type = TUNER_TEA5767;
516                                 t->mode_mask = T_RADIO;
517                                 t->mode = T_STANDBY;
518                                 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
519                                 default_mode_mask &= ~T_RADIO;
520
521                                 goto register_client;
522                         }
523                         break;
524                 }
525         }
526
527         /* Initializes only the first adapter found */
528         if (default_mode_mask != T_UNINITIALIZED) {
529                 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
530                 t->mode_mask = default_mode_mask;
531                 t->tv_freq = 400 * 16; /* Sets freq to VHF High */
532                 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
533                 default_mode_mask = T_UNINITIALIZED;
534         }
535
536         /* Should be just before return */
537 register_client:
538         tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
539         i2c_attach_client (&t->i2c);
540         set_type (&t->i2c,t->type, t->mode_mask, t->config, t->tuner_callback);
541         return 0;
542 }
543
544 static int tuner_probe(struct i2c_adapter *adap)
545 {
546         if (0 != addr) {
547                 normal_i2c[0] = addr;
548                 normal_i2c[1] = I2C_CLIENT_END;
549         }
550
551         default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
552
553         if (adap->class & I2C_CLASS_TV_ANALOG)
554                 return i2c_probe(adap, &addr_data, tuner_attach);
555         return 0;
556 }
557
558 static int tuner_detach(struct i2c_client *client)
559 {
560         struct tuner *t = i2c_get_clientdata(client);
561         int err;
562
563         err = i2c_detach_client(&t->i2c);
564         if (err) {
565                 tuner_warn
566                     ("Client deregistration failed, client not detached.\n");
567                 return err;
568         }
569
570         if (t->ops.release)
571                 t->ops.release(client);
572         else {
573                 kfree(t->priv);
574         }
575         kfree(t);
576         return 0;
577 }
578
579 /*
580  * Switch tuner to other mode. If tuner support both tv and radio,
581  * set another frequency to some value (This is needed for some pal
582  * tuners to avoid locking). Otherwise, just put second tuner in
583  * standby mode.
584  */
585
586 static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
587 {
588         if (mode == t->mode)
589                 return 0;
590
591         t->mode = mode;
592
593         if (check_mode(t, cmd) == EINVAL) {
594                 t->mode = T_STANDBY;
595                 if (t->ops.standby)
596                         t->ops.standby (client);
597                 return EINVAL;
598         }
599         return 0;
600 }
601
602 #define switch_v4l2()   if (!t->using_v4l2) \
603                             tuner_dbg("switching to v4l2\n"); \
604                         t->using_v4l2 = 1;
605
606 static inline int check_v4l2(struct tuner *t)
607 {
608         /* bttv still uses both v4l1 and v4l2 calls to the tuner (v4l2 for
609            TV, v4l1 for radio), until that is fixed this code is disabled.
610            Otherwise the radio (v4l1) wouldn't tune after using the TV (v4l2)
611            first. */
612         return 0;
613 }
614
615 static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
616 {
617         struct tuner *t = i2c_get_clientdata(client);
618
619         if (tuner_debug>1)
620                 v4l_i2c_print_ioctl(&(t->i2c),cmd);
621
622         switch (cmd) {
623         /* --- configuration --- */
624         case TUNER_SET_TYPE_ADDR:
625                 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
626                                 ((struct tuner_setup *)arg)->type,
627                                 ((struct tuner_setup *)arg)->addr,
628                                 ((struct tuner_setup *)arg)->mode_mask,
629                                 ((struct tuner_setup *)arg)->config);
630
631                 set_addr(client, (struct tuner_setup *)arg);
632                 break;
633         case AUDC_SET_RADIO:
634                 if (set_mode(client, t, V4L2_TUNER_RADIO, "AUDC_SET_RADIO")
635                                 == EINVAL)
636                         return 0;
637                 if (t->radio_freq)
638                         set_freq(client, t->radio_freq);
639                 break;
640         case TUNER_SET_STANDBY:
641                 if (check_mode(t, "TUNER_SET_STANDBY") == EINVAL)
642                         return 0;
643                 t->mode = T_STANDBY;
644                 if (t->ops.standby)
645                         t->ops.standby (client);
646                 break;
647 #ifdef CONFIG_VIDEO_V4L1
648         case VIDIOCSAUDIO:
649                 if (check_mode(t, "VIDIOCSAUDIO") == EINVAL)
650                         return 0;
651                 if (check_v4l2(t) == EINVAL)
652                         return 0;
653
654                 /* Should be implemented, since bttv calls it */
655                 tuner_dbg("VIDIOCSAUDIO not implemented.\n");
656                 break;
657         case VIDIOCSCHAN:
658                 {
659                         static const v4l2_std_id map[] = {
660                                 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
661                                 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
662                                 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
663                                 [4 /* bttv */ ] = V4L2_STD_PAL_M,
664                                 [5 /* bttv */ ] = V4L2_STD_PAL_N,
665                                 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
666                         };
667                         struct video_channel *vc = arg;
668
669                         if (check_v4l2(t) == EINVAL)
670                                 return 0;
671
672                         if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
673                                 return 0;
674
675                         if (vc->norm < ARRAY_SIZE(map))
676                                 t->std = map[vc->norm];
677                         tuner_fixup_std(t);
678                         if (t->tv_freq)
679                                 set_tv_freq(client, t->tv_freq);
680                         return 0;
681                 }
682         case VIDIOCSFREQ:
683                 {
684                         unsigned long *v = arg;
685
686                         if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
687                                 return 0;
688                         if (check_v4l2(t) == EINVAL)
689                                 return 0;
690
691                         set_freq(client, *v);
692                         return 0;
693                 }
694         case VIDIOCGTUNER:
695                 {
696                         struct video_tuner *vt = arg;
697
698                         if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
699                                 return 0;
700                         if (check_v4l2(t) == EINVAL)
701                                 return 0;
702
703                         if (V4L2_TUNER_RADIO == t->mode) {
704                                 if (t->ops.has_signal)
705                                         vt->signal = t->ops.has_signal(client);
706                                 if (t->ops.is_stereo) {
707                                         if (t->ops.is_stereo(client))
708                                                 vt->flags |=
709                                                     VIDEO_TUNER_STEREO_ON;
710                                         else
711                                                 vt->flags &=
712                                                     ~VIDEO_TUNER_STEREO_ON;
713                                 }
714                                 vt->flags |= VIDEO_TUNER_LOW;   /* Allow freqs at 62.5 Hz */
715
716                                 vt->rangelow = radio_range[0] * 16000;
717                                 vt->rangehigh = radio_range[1] * 16000;
718
719                         } else {
720                                 vt->rangelow = tv_range[0] * 16;
721                                 vt->rangehigh = tv_range[1] * 16;
722                         }
723
724                         return 0;
725                 }
726         case VIDIOCGAUDIO:
727                 {
728                         struct video_audio *va = arg;
729
730                         if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
731                                 return 0;
732                         if (check_v4l2(t) == EINVAL)
733                                 return 0;
734
735                         if (V4L2_TUNER_RADIO == t->mode && t->ops.is_stereo)
736                                 va->mode = t->ops.is_stereo(client)
737                                     ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
738                         return 0;
739                 }
740 #endif
741         case TDA9887_SET_CONFIG:
742                 if (t->type == TUNER_TDA9887) {
743                         int *i = arg;
744
745                         t->tda9887_config = *i;
746                         set_freq(client, t->tv_freq);
747                 }
748                 break;
749         /* --- v4l ioctls --- */
750         /* take care: bttv does userspace copying, we'll get a
751            kernel pointer here... */
752         case VIDIOC_S_STD:
753                 {
754                         v4l2_std_id *id = arg;
755
756                         if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
757                                         == EINVAL)
758                                 return 0;
759
760                         switch_v4l2();
761
762                         t->std = *id;
763                         tuner_fixup_std(t);
764                         if (t->tv_freq)
765                                 set_freq(client, t->tv_freq);
766                         break;
767                 }
768         case VIDIOC_S_FREQUENCY:
769                 {
770                         struct v4l2_frequency *f = arg;
771
772                         if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
773                                         == EINVAL)
774                                 return 0;
775                         switch_v4l2();
776                         set_freq(client,f->frequency);
777
778                         break;
779                 }
780         case VIDIOC_G_FREQUENCY:
781                 {
782                         struct v4l2_frequency *f = arg;
783
784                         if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
785                                 return 0;
786                         switch_v4l2();
787                         f->type = t->mode;
788                         f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
789                                 t->radio_freq : t->tv_freq;
790                         break;
791                 }
792         case VIDIOC_G_TUNER:
793                 {
794                         struct v4l2_tuner *tuner = arg;
795
796                         if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
797                                 return 0;
798                         switch_v4l2();
799
800                         tuner->type = t->mode;
801                         if (t->ops.get_afc)
802                                 tuner->afc=t->ops.get_afc(client);
803                         if (t->mode == V4L2_TUNER_ANALOG_TV)
804                                 tuner->capability |= V4L2_TUNER_CAP_NORM;
805                         if (t->mode != V4L2_TUNER_RADIO) {
806                                 tuner->rangelow = tv_range[0] * 16;
807                                 tuner->rangehigh = tv_range[1] * 16;
808                                 break;
809                         }
810
811                         /* radio mode */
812                         if (t->ops.has_signal)
813                                 tuner->signal = t->ops.has_signal(client);
814
815                         tuner->rxsubchans =
816                                 V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
817                         if (t->ops.is_stereo) {
818                                 tuner->rxsubchans = t->ops.is_stereo(client) ?
819                                         V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
820                         }
821
822                         tuner->capability |=
823                             V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
824                         tuner->audmode = t->audmode;
825                         tuner->rangelow = radio_range[0] * 16000;
826                         tuner->rangehigh = radio_range[1] * 16000;
827                         break;
828                 }
829         case VIDIOC_S_TUNER:
830                 {
831                         struct v4l2_tuner *tuner = arg;
832
833                         if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
834                                 return 0;
835
836                         switch_v4l2();
837
838                         /* do nothing unless we're a radio tuner */
839                         if (t->mode != V4L2_TUNER_RADIO)
840                                 break;
841                         t->audmode = tuner->audmode;
842                         set_radio_freq(client, t->radio_freq);
843                         break;
844                 }
845         case VIDIOC_LOG_STATUS:
846                 if (t->ops.tuner_status)
847                         t->ops.tuner_status(client);
848                 break;
849         }
850
851         return 0;
852 }
853
854 static int tuner_suspend(struct i2c_client *c, pm_message_t state)
855 {
856         struct tuner *t = i2c_get_clientdata (c);
857
858         tuner_dbg ("suspend\n");
859         /* FIXME: power down ??? */
860         return 0;
861 }
862
863 static int tuner_resume(struct i2c_client *c)
864 {
865         struct tuner *t = i2c_get_clientdata (c);
866
867         tuner_dbg ("resume\n");
868         if (V4L2_TUNER_RADIO == t->mode) {
869                 if (t->radio_freq)
870                         set_freq(c, t->radio_freq);
871         } else {
872                 if (t->tv_freq)
873                         set_freq(c, t->tv_freq);
874         }
875         return 0;
876 }
877
878 /* ----------------------------------------------------------------------- */
879
880 static struct i2c_driver driver = {
881         .id = I2C_DRIVERID_TUNER,
882         .attach_adapter = tuner_probe,
883         .detach_client = tuner_detach,
884         .command = tuner_command,
885         .suspend = tuner_suspend,
886         .resume  = tuner_resume,
887         .driver = {
888                 .name    = "tuner",
889         },
890 };
891 static struct i2c_client client_template = {
892         .name = "(tuner unset)",
893         .driver = &driver,
894 };
895
896 static int __init tuner_init_module(void)
897 {
898         return i2c_add_driver(&driver);
899 }
900
901 static void __exit tuner_cleanup_module(void)
902 {
903         i2c_del_driver(&driver);
904 }
905
906 module_init(tuner_init_module);
907 module_exit(tuner_cleanup_module);
908
909 /*
910  * Overrides for Emacs so that we follow Linus's tabbing style.
911  * ---------------------------------------------------------------------------
912  * Local variables:
913  * c-basic-offset: 8
914  * End:
915  */