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