V4L/DVB (11799): xc5000: don't load firmware until a tuning request is made
[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/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/videodev2.h>
19 #include <media/tuner.h>
20 #include <media/tuner-types.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/v4l2-i2c-drv.h>
24 #include "mt20xx.h"
25 #include "tda8290.h"
26 #include "tea5761.h"
27 #include "tea5767.h"
28 #include "tuner-xc2028.h"
29 #include "tuner-simple.h"
30 #include "tda9887.h"
31 #include "xc5000.h"
32
33 #define UNSET (-1U)
34
35 #define PREFIX t->i2c->driver->driver.name
36
37 /** This macro allows us to probe dynamically, avoiding static links */
38 #ifdef CONFIG_MEDIA_ATTACH
39 #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
40         int __r = -EINVAL; \
41         typeof(&FUNCTION) __a = symbol_request(FUNCTION); \
42         if (__a) { \
43                 __r = (int) __a(ARGS); \
44                 symbol_put(FUNCTION); \
45         } else { \
46                 printk(KERN_ERR "TUNER: Unable to find " \
47                                 "symbol "#FUNCTION"()\n"); \
48         } \
49         __r; \
50 })
51
52 static void tuner_detach(struct dvb_frontend *fe)
53 {
54         if (fe->ops.tuner_ops.release) {
55                 fe->ops.tuner_ops.release(fe);
56                 symbol_put_addr(fe->ops.tuner_ops.release);
57         }
58         if (fe->ops.analog_ops.release) {
59                 fe->ops.analog_ops.release(fe);
60                 symbol_put_addr(fe->ops.analog_ops.release);
61         }
62 }
63 #else
64 #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
65         FUNCTION(ARGS); \
66 })
67
68 static void tuner_detach(struct dvb_frontend *fe)
69 {
70         if (fe->ops.tuner_ops.release)
71                 fe->ops.tuner_ops.release(fe);
72         if (fe->ops.analog_ops.release)
73                 fe->ops.analog_ops.release(fe);
74 }
75 #endif
76
77 struct tuner {
78         /* device */
79         struct dvb_frontend fe;
80         struct i2c_client   *i2c;
81         struct v4l2_subdev  sd;
82         struct list_head    list;
83         unsigned int        using_v4l2:1;
84
85         /* keep track of the current settings */
86         v4l2_std_id         std;
87         unsigned int        tv_freq;
88         unsigned int        radio_freq;
89         unsigned int        audmode;
90
91         unsigned int        mode;
92         unsigned int        mode_mask; /* Combination of allowable modes */
93
94         unsigned int        type; /* chip type id */
95         unsigned int        config;
96         const char          *name;
97 };
98
99 static inline struct tuner *to_tuner(struct v4l2_subdev *sd)
100 {
101         return container_of(sd, struct tuner, sd);
102 }
103
104
105 /* insmod options used at init time => read/only */
106 static unsigned int addr;
107 static unsigned int no_autodetect;
108 static unsigned int show_i2c;
109
110 /* insmod options used at runtime => read/write */
111 static int tuner_debug;
112
113 #define tuner_warn(fmt, arg...) do {                    \
114         printk(KERN_WARNING "%s %d-%04x: " fmt, PREFIX, \
115                i2c_adapter_id(t->i2c->adapter),         \
116                t->i2c->addr, ##arg);                    \
117          } while (0)
118
119 #define tuner_info(fmt, arg...) do {                    \
120         printk(KERN_INFO "%s %d-%04x: " fmt, PREFIX,    \
121                i2c_adapter_id(t->i2c->adapter),         \
122                t->i2c->addr, ##arg);                    \
123          } while (0)
124
125 #define tuner_err(fmt, arg...) do {                     \
126         printk(KERN_ERR "%s %d-%04x: " fmt, PREFIX,     \
127                i2c_adapter_id(t->i2c->adapter),         \
128                t->i2c->addr, ##arg);                    \
129          } while (0)
130
131 #define tuner_dbg(fmt, arg...) do {                             \
132         if (tuner_debug)                                        \
133                 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX,   \
134                        i2c_adapter_id(t->i2c->adapter),         \
135                        t->i2c->addr, ##arg);                    \
136          } while (0)
137
138 /* ------------------------------------------------------------------------ */
139
140 static unsigned int tv_range[2] = { 44, 958 };
141 static unsigned int radio_range[2] = { 65, 108 };
142
143 static char pal[] = "--";
144 static char secam[] = "--";
145 static char ntsc[] = "-";
146
147
148 module_param(addr, int, 0444);
149 module_param(no_autodetect, int, 0444);
150 module_param(show_i2c, int, 0444);
151 module_param_named(debug,tuner_debug, int, 0644);
152 module_param_string(pal, pal, sizeof(pal), 0644);
153 module_param_string(secam, secam, sizeof(secam), 0644);
154 module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
155 module_param_array(tv_range, int, NULL, 0644);
156 module_param_array(radio_range, int, NULL, 0644);
157
158 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
159 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
160 MODULE_LICENSE("GPL");
161
162 /* ---------------------------------------------------------------------- */
163
164 static void fe_set_params(struct dvb_frontend *fe,
165                           struct analog_parameters *params)
166 {
167         struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
168         struct tuner *t = fe->analog_demod_priv;
169
170         if (NULL == fe_tuner_ops->set_analog_params) {
171                 tuner_warn("Tuner frontend module has no way to set freq\n");
172                 return;
173         }
174         fe_tuner_ops->set_analog_params(fe, params);
175 }
176
177 static void fe_standby(struct dvb_frontend *fe)
178 {
179         struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
180
181         if (fe_tuner_ops->sleep)
182                 fe_tuner_ops->sleep(fe);
183 }
184
185 static int fe_has_signal(struct dvb_frontend *fe)
186 {
187         u16 strength = 0;
188
189         if (fe->ops.tuner_ops.get_rf_strength)
190                 fe->ops.tuner_ops.get_rf_strength(fe, &strength);
191
192         return strength;
193 }
194
195 static int fe_set_config(struct dvb_frontend *fe, void *priv_cfg)
196 {
197         struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
198         struct tuner *t = fe->analog_demod_priv;
199
200         if (fe_tuner_ops->set_config)
201                 return fe_tuner_ops->set_config(fe, priv_cfg);
202
203         tuner_warn("Tuner frontend module has no way to set config\n");
204
205         return 0;
206 }
207
208 static void tuner_status(struct dvb_frontend *fe);
209
210 static struct analog_demod_ops tuner_analog_ops = {
211         .set_params     = fe_set_params,
212         .standby        = fe_standby,
213         .has_signal     = fe_has_signal,
214         .set_config     = fe_set_config,
215         .tuner_status   = tuner_status
216 };
217
218 /* Set tuner frequency,  freq in Units of 62.5kHz = 1/16MHz */
219 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
220 {
221         struct tuner *t = to_tuner(i2c_get_clientdata(c));
222         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
223
224         struct analog_parameters params = {
225                 .mode      = t->mode,
226                 .audmode   = t->audmode,
227                 .std       = t->std
228         };
229
230         if (t->type == UNSET) {
231                 tuner_warn ("tuner type not set\n");
232                 return;
233         }
234         if (NULL == analog_ops->set_params) {
235                 tuner_warn ("Tuner has no way to set tv freq\n");
236                 return;
237         }
238         if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
239                 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
240                            freq / 16, freq % 16 * 100 / 16, tv_range[0],
241                            tv_range[1]);
242                 /* V4L2 spec: if the freq is not possible then the closest
243                    possible value should be selected */
244                 if (freq < tv_range[0] * 16)
245                         freq = tv_range[0] * 16;
246                 else
247                         freq = tv_range[1] * 16;
248         }
249         params.frequency = freq;
250
251         analog_ops->set_params(&t->fe, &params);
252 }
253
254 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
255 {
256         struct tuner *t = to_tuner(i2c_get_clientdata(c));
257         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
258
259         struct analog_parameters params = {
260                 .mode      = t->mode,
261                 .audmode   = t->audmode,
262                 .std       = t->std
263         };
264
265         if (t->type == UNSET) {
266                 tuner_warn ("tuner type not set\n");
267                 return;
268         }
269         if (NULL == analog_ops->set_params) {
270                 tuner_warn ("tuner has no way to set radio frequency\n");
271                 return;
272         }
273         if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
274                 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
275                            freq / 16000, freq % 16000 * 100 / 16000,
276                            radio_range[0], radio_range[1]);
277                 /* V4L2 spec: if the freq is not possible then the closest
278                    possible value should be selected */
279                 if (freq < radio_range[0] * 16000)
280                         freq = radio_range[0] * 16000;
281                 else
282                         freq = radio_range[1] * 16000;
283         }
284         params.frequency = freq;
285
286         analog_ops->set_params(&t->fe, &params);
287 }
288
289 static void set_freq(struct i2c_client *c, unsigned long freq)
290 {
291         struct tuner *t = to_tuner(i2c_get_clientdata(c));
292
293         switch (t->mode) {
294         case V4L2_TUNER_RADIO:
295                 tuner_dbg("radio freq set to %lu.%02lu\n",
296                           freq / 16000, freq % 16000 * 100 / 16000);
297                 set_radio_freq(c, freq);
298                 t->radio_freq = freq;
299                 break;
300         case V4L2_TUNER_ANALOG_TV:
301         case V4L2_TUNER_DIGITAL_TV:
302                 tuner_dbg("tv freq set to %lu.%02lu\n",
303                           freq / 16, freq % 16 * 100 / 16);
304                 set_tv_freq(c, freq);
305                 t->tv_freq = freq;
306                 break;
307         default:
308                 tuner_dbg("freq set: unknown mode: 0x%04x!\n",t->mode);
309         }
310 }
311
312 static struct xc5000_config xc5000_cfg;
313
314 static void set_type(struct i2c_client *c, unsigned int type,
315                      unsigned int new_mode_mask, unsigned int new_config,
316                      int (*tuner_callback) (void *dev, int component, int cmd, int arg))
317 {
318         struct tuner *t = to_tuner(i2c_get_clientdata(c));
319         struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
320         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
321         unsigned char buffer[4];
322
323         if (type == UNSET || type == TUNER_ABSENT) {
324                 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
325                 return;
326         }
327
328         t->type = type;
329         /* prevent invalid config values */
330         t->config = ((new_config >= 0) && (new_config < 256)) ? new_config : 0;
331         if (tuner_callback != NULL) {
332                 tuner_dbg("defining GPIO callback\n");
333                 t->fe.callback = tuner_callback;
334         }
335
336         if (t->mode == T_UNINITIALIZED) {
337                 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
338
339                 return;
340         }
341
342         /* discard private data, in case set_type() was previously called */
343         tuner_detach(&t->fe);
344         t->fe.analog_demod_priv = NULL;
345
346         switch (t->type) {
347         case TUNER_MT2032:
348                 if (!dvb_attach(microtune_attach,
349                            &t->fe, t->i2c->adapter, t->i2c->addr))
350                         goto attach_failed;
351                 break;
352         case TUNER_PHILIPS_TDA8290:
353         {
354                 struct tda829x_config cfg = {
355                         .lna_cfg        = t->config,
356                 };
357                 if (!dvb_attach(tda829x_attach, &t->fe, t->i2c->adapter,
358                                 t->i2c->addr, &cfg))
359                         goto attach_failed;
360                 break;
361         }
362         case TUNER_TEA5767:
363                 if (!dvb_attach(tea5767_attach, &t->fe,
364                                 t->i2c->adapter, t->i2c->addr))
365                         goto attach_failed;
366                 t->mode_mask = T_RADIO;
367                 break;
368         case TUNER_TEA5761:
369                 if (!dvb_attach(tea5761_attach, &t->fe,
370                                 t->i2c->adapter, t->i2c->addr))
371                         goto attach_failed;
372                 t->mode_mask = T_RADIO;
373                 break;
374         case TUNER_PHILIPS_FMD1216ME_MK3:
375                 buffer[0] = 0x0b;
376                 buffer[1] = 0xdc;
377                 buffer[2] = 0x9c;
378                 buffer[3] = 0x60;
379                 i2c_master_send(c, buffer, 4);
380                 mdelay(1);
381                 buffer[2] = 0x86;
382                 buffer[3] = 0x54;
383                 i2c_master_send(c, buffer, 4);
384                 if (!dvb_attach(simple_tuner_attach, &t->fe,
385                                 t->i2c->adapter, t->i2c->addr, t->type))
386                         goto attach_failed;
387                 break;
388         case TUNER_PHILIPS_TD1316:
389                 buffer[0] = 0x0b;
390                 buffer[1] = 0xdc;
391                 buffer[2] = 0x86;
392                 buffer[3] = 0xa4;
393                 i2c_master_send(c, buffer, 4);
394                 if (!dvb_attach(simple_tuner_attach, &t->fe,
395                                 t->i2c->adapter, t->i2c->addr, t->type))
396                         goto attach_failed;
397                 break;
398         case TUNER_XC2028:
399         {
400                 struct xc2028_config cfg = {
401                         .i2c_adap  = t->i2c->adapter,
402                         .i2c_addr  = t->i2c->addr,
403                 };
404                 if (!dvb_attach(xc2028_attach, &t->fe, &cfg))
405                         goto attach_failed;
406                 break;
407         }
408         case TUNER_TDA9887:
409                 if (!dvb_attach(tda9887_attach,
410                            &t->fe, t->i2c->adapter, t->i2c->addr))
411                         goto attach_failed;
412                 break;
413         case TUNER_XC5000:
414         {
415                 struct dvb_tuner_ops *xc_tuner_ops;
416
417                 xc5000_cfg.i2c_address    = t->i2c->addr;
418                 /* if_khz will be set when the digital dvb_attach() occurs */
419                 xc5000_cfg.if_khz         = 0;
420                 if (!dvb_attach(xc5000_attach,
421                                 &t->fe, t->i2c->adapter, &xc5000_cfg))
422                         goto attach_failed;
423                 break;
424         }
425         default:
426                 if (!dvb_attach(simple_tuner_attach, &t->fe,
427                                 t->i2c->adapter, t->i2c->addr, t->type))
428                         goto attach_failed;
429
430                 break;
431         }
432
433         if ((NULL == analog_ops->set_params) &&
434             (fe_tuner_ops->set_analog_params)) {
435
436                 t->name = fe_tuner_ops->info.name;
437
438                 t->fe.analog_demod_priv = t;
439                 memcpy(analog_ops, &tuner_analog_ops,
440                        sizeof(struct analog_demod_ops));
441
442         } else {
443                 t->name = analog_ops->info.name;
444         }
445
446         tuner_dbg("type set to %s\n", t->name);
447
448         if (t->mode_mask == T_UNINITIALIZED)
449                 t->mode_mask = new_mode_mask;
450
451         /* xc2028/3028 and xc5000 requires a firmware to be set-up later
452            trying to set a frequency here will just fail
453            FIXME: better to move set_freq to the tuner code. This is needed
454            on analog tuners for PLL to properly work
455          */
456         if (t->type != TUNER_XC2028 && t->type != TUNER_XC5000)
457                 set_freq(c, (V4L2_TUNER_RADIO == t->mode) ?
458                             t->radio_freq : t->tv_freq);
459
460         tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
461                   c->adapter->name, c->driver->driver.name, c->addr << 1, type,
462                   t->mode_mask);
463         return;
464
465 attach_failed:
466         tuner_dbg("Tuner attach for type = %d failed.\n", t->type);
467         t->type = TUNER_ABSENT;
468         t->mode_mask = T_UNINITIALIZED;
469
470         return;
471 }
472
473 /*
474  * This function apply tuner config to tuner specified
475  * by tun_setup structure. I addr is unset, then admin status
476  * and tun addr status is more precise then current status,
477  * it's applied. Otherwise status and type are applied only to
478  * tuner with exactly the same addr.
479 */
480
481 static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
482 {
483         struct tuner *t = to_tuner(i2c_get_clientdata(c));
484
485         if ( (t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
486                 (t->mode_mask & tun_setup->mode_mask))) ||
487                 (tun_setup->addr == c->addr)) {
488                         set_type(c, tun_setup->type, tun_setup->mode_mask,
489                                  tun_setup->config, tun_setup->tuner_callback);
490         } else
491                 tuner_dbg("set addr discarded for type %i, mask %x. "
492                           "Asked to change tuner at addr 0x%02x, with mask %x\n",
493                           t->type, t->mode_mask,
494                           tun_setup->addr, tun_setup->mode_mask);
495 }
496
497 static inline int check_mode(struct tuner *t, char *cmd)
498 {
499         if ((1 << t->mode & t->mode_mask) == 0) {
500                 return -EINVAL;
501         }
502
503         switch (t->mode) {
504         case V4L2_TUNER_RADIO:
505                 tuner_dbg("Cmd %s accepted for radio\n", cmd);
506                 break;
507         case V4L2_TUNER_ANALOG_TV:
508                 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
509                 break;
510         case V4L2_TUNER_DIGITAL_TV:
511                 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
512                 break;
513         }
514         return 0;
515 }
516
517 /* get more precise norm info from insmod option */
518 static int tuner_fixup_std(struct tuner *t)
519 {
520         if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
521                 switch (pal[0]) {
522                 case '6':
523                         tuner_dbg ("insmod fixup: PAL => PAL-60\n");
524                         t->std = V4L2_STD_PAL_60;
525                         break;
526                 case 'b':
527                 case 'B':
528                 case 'g':
529                 case 'G':
530                         tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
531                         t->std = V4L2_STD_PAL_BG;
532                         break;
533                 case 'i':
534                 case 'I':
535                         tuner_dbg ("insmod fixup: PAL => PAL-I\n");
536                         t->std = V4L2_STD_PAL_I;
537                         break;
538                 case 'd':
539                 case 'D':
540                 case 'k':
541                 case 'K':
542                         tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
543                         t->std = V4L2_STD_PAL_DK;
544                         break;
545                 case 'M':
546                 case 'm':
547                         tuner_dbg ("insmod fixup: PAL => PAL-M\n");
548                         t->std = V4L2_STD_PAL_M;
549                         break;
550                 case 'N':
551                 case 'n':
552                         if (pal[1] == 'c' || pal[1] == 'C') {
553                                 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
554                                 t->std = V4L2_STD_PAL_Nc;
555                         } else {
556                                 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
557                                 t->std = V4L2_STD_PAL_N;
558                         }
559                         break;
560                 case '-':
561                         /* default parameter, do nothing */
562                         break;
563                 default:
564                         tuner_warn ("pal= argument not recognised\n");
565                         break;
566                 }
567         }
568         if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
569                 switch (secam[0]) {
570                 case 'b':
571                 case 'B':
572                 case 'g':
573                 case 'G':
574                 case 'h':
575                 case 'H':
576                         tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
577                         t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
578                         break;
579                 case 'd':
580                 case 'D':
581                 case 'k':
582                 case 'K':
583                         tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
584                         t->std = V4L2_STD_SECAM_DK;
585                         break;
586                 case 'l':
587                 case 'L':
588                         if ((secam[1]=='C')||(secam[1]=='c')) {
589                                 tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n");
590                                 t->std = V4L2_STD_SECAM_LC;
591                         } else {
592                                 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
593                                 t->std = V4L2_STD_SECAM_L;
594                         }
595                         break;
596                 case '-':
597                         /* default parameter, do nothing */
598                         break;
599                 default:
600                         tuner_warn ("secam= argument not recognised\n");
601                         break;
602                 }
603         }
604
605         if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
606                 switch (ntsc[0]) {
607                 case 'm':
608                 case 'M':
609                         tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
610                         t->std = V4L2_STD_NTSC_M;
611                         break;
612                 case 'j':
613                 case 'J':
614                         tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
615                         t->std = V4L2_STD_NTSC_M_JP;
616                         break;
617                 case 'k':
618                 case 'K':
619                         tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
620                         t->std = V4L2_STD_NTSC_M_KR;
621                         break;
622                 case '-':
623                         /* default parameter, do nothing */
624                         break;
625                 default:
626                         tuner_info("ntsc= argument not recognised\n");
627                         break;
628                 }
629         }
630         return 0;
631 }
632
633 static void tuner_status(struct dvb_frontend *fe)
634 {
635         struct tuner *t = fe->analog_demod_priv;
636         unsigned long freq, freq_fraction;
637         struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
638         struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
639         const char *p;
640
641         switch (t->mode) {
642                 case V4L2_TUNER_RADIO:      p = "radio"; break;
643                 case V4L2_TUNER_ANALOG_TV:  p = "analog TV"; break;
644                 case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break;
645                 default: p = "undefined"; break;
646         }
647         if (t->mode == V4L2_TUNER_RADIO) {
648                 freq = t->radio_freq / 16000;
649                 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
650         } else {
651                 freq = t->tv_freq / 16;
652                 freq_fraction = (t->tv_freq % 16) * 100 / 16;
653         }
654         tuner_info("Tuner mode:      %s\n", p);
655         tuner_info("Frequency:       %lu.%02lu MHz\n", freq, freq_fraction);
656         tuner_info("Standard:        0x%08lx\n", (unsigned long)t->std);
657         if (t->mode != V4L2_TUNER_RADIO)
658                return;
659         if (fe_tuner_ops->get_status) {
660                 u32 tuner_status;
661
662                 fe_tuner_ops->get_status(&t->fe, &tuner_status);
663                 if (tuner_status & TUNER_STATUS_LOCKED)
664                         tuner_info("Tuner is locked.\n");
665                 if (tuner_status & TUNER_STATUS_STEREO)
666                         tuner_info("Stereo:          yes\n");
667         }
668         if (analog_ops->has_signal)
669                 tuner_info("Signal strength: %d\n",
670                            analog_ops->has_signal(fe));
671         if (analog_ops->is_stereo)
672                 tuner_info("Stereo:          %s\n",
673                            analog_ops->is_stereo(fe) ? "yes" : "no");
674 }
675
676 /* ---------------------------------------------------------------------- */
677
678 /*
679  * Switch tuner to other mode. If tuner support both tv and radio,
680  * set another frequency to some value (This is needed for some pal
681  * tuners to avoid locking). Otherwise, just put second tuner in
682  * standby mode.
683  */
684
685 static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
686 {
687         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
688
689         if (mode == t->mode)
690                 return 0;
691
692         t->mode = mode;
693
694         if (check_mode(t, cmd) == -EINVAL) {
695                 tuner_dbg("Tuner doesn't support this mode. "
696                           "Putting tuner to sleep\n");
697                 t->mode = T_STANDBY;
698                 if (analog_ops->standby)
699                         analog_ops->standby(&t->fe);
700                 return -EINVAL;
701         }
702         return 0;
703 }
704
705 #define switch_v4l2()   if (!t->using_v4l2) \
706                             tuner_dbg("switching to v4l2\n"); \
707                         t->using_v4l2 = 1;
708
709 static inline int check_v4l2(struct tuner *t)
710 {
711         /* bttv still uses both v4l1 and v4l2 calls to the tuner (v4l2 for
712            TV, v4l1 for radio), until that is fixed this code is disabled.
713            Otherwise the radio (v4l1) wouldn't tune after using the TV (v4l2)
714            first. */
715         return 0;
716 }
717
718 static int tuner_s_type_addr(struct v4l2_subdev *sd, struct tuner_setup *type)
719 {
720         struct tuner *t = to_tuner(sd);
721         struct i2c_client *client = v4l2_get_subdevdata(sd);
722
723         tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
724                         type->type,
725                         type->addr,
726                         type->mode_mask,
727                         type->config);
728
729         set_addr(client, type);
730         return 0;
731 }
732
733 static int tuner_s_radio(struct v4l2_subdev *sd)
734 {
735         struct tuner *t = to_tuner(sd);
736         struct i2c_client *client = v4l2_get_subdevdata(sd);
737
738         if (set_mode(client, t, V4L2_TUNER_RADIO, "s_radio") == -EINVAL)
739                 return 0;
740         if (t->radio_freq)
741                 set_freq(client, t->radio_freq);
742         return 0;
743 }
744
745 static int tuner_s_standby(struct v4l2_subdev *sd)
746 {
747         struct tuner *t = to_tuner(sd);
748         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
749
750         tuner_dbg("Putting tuner to sleep\n");
751
752         if (check_mode(t, "s_standby") == -EINVAL)
753                 return 0;
754         t->mode = T_STANDBY;
755         if (analog_ops->standby)
756                 analog_ops->standby(&t->fe);
757         return 0;
758 }
759
760 static int tuner_s_config(struct v4l2_subdev *sd, const struct v4l2_priv_tun_config *cfg)
761 {
762         struct tuner *t = to_tuner(sd);
763         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
764
765         if (t->type != cfg->tuner)
766                 return 0;
767
768         if (analog_ops->set_config) {
769                 analog_ops->set_config(&t->fe, cfg->priv);
770                 return 0;
771         }
772
773         tuner_dbg("Tuner frontend module has no way to set config\n");
774         return 0;
775 }
776
777 /* --- v4l ioctls --- */
778 /* take care: bttv does userspace copying, we'll get a
779    kernel pointer here... */
780 static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
781 {
782         struct tuner *t = to_tuner(sd);
783         struct i2c_client *client = v4l2_get_subdevdata(sd);
784
785         if (set_mode(client, t, V4L2_TUNER_ANALOG_TV, "s_std") == -EINVAL)
786                 return 0;
787
788         switch_v4l2();
789
790         t->std = std;
791         tuner_fixup_std(t);
792         if (t->tv_freq)
793                 set_freq(client, t->tv_freq);
794         return 0;
795 }
796
797 static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
798 {
799         struct tuner *t = to_tuner(sd);
800         struct i2c_client *client = v4l2_get_subdevdata(sd);
801
802         if (set_mode(client, t, f->type, "s_frequency") == -EINVAL)
803                 return 0;
804         switch_v4l2();
805         set_freq(client, f->frequency);
806
807         return 0;
808 }
809
810 static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
811 {
812         struct tuner *t = to_tuner(sd);
813         struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
814
815         if (check_mode(t, "g_frequency") == -EINVAL)
816                 return 0;
817         switch_v4l2();
818         f->type = t->mode;
819         if (fe_tuner_ops->get_frequency) {
820                 u32 abs_freq;
821
822                 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
823                 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
824                         (abs_freq * 2 + 125/2) / 125 :
825                         (abs_freq + 62500/2) / 62500;
826                 return 0;
827         }
828         f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
829                 t->radio_freq : t->tv_freq;
830         return 0;
831 }
832
833 static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
834 {
835         struct tuner *t = to_tuner(sd);
836         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
837         struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
838
839         if (check_mode(t, "g_tuner") == -EINVAL)
840                 return 0;
841         switch_v4l2();
842
843         vt->type = t->mode;
844         if (analog_ops->get_afc)
845                 vt->afc = analog_ops->get_afc(&t->fe);
846         if (t->mode == V4L2_TUNER_ANALOG_TV)
847                 vt->capability |= V4L2_TUNER_CAP_NORM;
848         if (t->mode != V4L2_TUNER_RADIO) {
849                 vt->rangelow = tv_range[0] * 16;
850                 vt->rangehigh = tv_range[1] * 16;
851                 return 0;
852         }
853
854         /* radio mode */
855         vt->rxsubchans =
856                 V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
857         if (fe_tuner_ops->get_status) {
858                 u32 tuner_status;
859
860                 fe_tuner_ops->get_status(&t->fe, &tuner_status);
861                 vt->rxsubchans =
862                         (tuner_status & TUNER_STATUS_STEREO) ?
863                         V4L2_TUNER_SUB_STEREO :
864                         V4L2_TUNER_SUB_MONO;
865         } else {
866                 if (analog_ops->is_stereo) {
867                         vt->rxsubchans =
868                                 analog_ops->is_stereo(&t->fe) ?
869                                 V4L2_TUNER_SUB_STEREO :
870                                 V4L2_TUNER_SUB_MONO;
871                 }
872         }
873         if (analog_ops->has_signal)
874                 vt->signal = analog_ops->has_signal(&t->fe);
875         vt->capability |=
876                 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
877         vt->audmode = t->audmode;
878         vt->rangelow = radio_range[0] * 16000;
879         vt->rangehigh = radio_range[1] * 16000;
880         return 0;
881 }
882
883 static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
884 {
885         struct tuner *t = to_tuner(sd);
886         struct i2c_client *client = v4l2_get_subdevdata(sd);
887
888         if (check_mode(t, "s_tuner") == -EINVAL)
889                 return 0;
890
891         switch_v4l2();
892
893         /* do nothing unless we're a radio tuner */
894         if (t->mode != V4L2_TUNER_RADIO)
895                 return 0;
896         t->audmode = vt->audmode;
897         set_radio_freq(client, t->radio_freq);
898         return 0;
899 }
900
901 static int tuner_log_status(struct v4l2_subdev *sd)
902 {
903         struct tuner *t = to_tuner(sd);
904         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
905
906         if (analog_ops->tuner_status)
907                 analog_ops->tuner_status(&t->fe);
908         return 0;
909 }
910
911 static int tuner_suspend(struct i2c_client *c, pm_message_t state)
912 {
913         struct tuner *t = to_tuner(i2c_get_clientdata(c));
914
915         tuner_dbg("suspend\n");
916         /* FIXME: power down ??? */
917         return 0;
918 }
919
920 static int tuner_resume(struct i2c_client *c)
921 {
922         struct tuner *t = to_tuner(i2c_get_clientdata(c));
923
924         tuner_dbg("resume\n");
925         if (V4L2_TUNER_RADIO == t->mode) {
926                 if (t->radio_freq)
927                         set_freq(c, t->radio_freq);
928         } else {
929                 if (t->tv_freq)
930                         set_freq(c, t->tv_freq);
931         }
932         return 0;
933 }
934
935 static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
936 {
937         struct v4l2_subdev *sd = i2c_get_clientdata(client);
938
939         /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
940            to handle it here.
941            There must be a better way of doing this... */
942         switch (cmd) {
943         case TUNER_SET_CONFIG:
944                 return tuner_s_config(sd, arg);
945         }
946         return -ENOIOCTLCMD;
947 }
948
949 /* ----------------------------------------------------------------------- */
950
951 static const struct v4l2_subdev_core_ops tuner_core_ops = {
952         .log_status = tuner_log_status,
953         .s_std = tuner_s_std,
954 };
955
956 static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
957         .s_radio = tuner_s_radio,
958         .g_tuner = tuner_g_tuner,
959         .s_tuner = tuner_s_tuner,
960         .s_frequency = tuner_s_frequency,
961         .g_frequency = tuner_g_frequency,
962         .s_type_addr = tuner_s_type_addr,
963         .s_config = tuner_s_config,
964         .s_standby = tuner_s_standby,
965 };
966
967 static const struct v4l2_subdev_ops tuner_ops = {
968         .core = &tuner_core_ops,
969         .tuner = &tuner_tuner_ops,
970 };
971
972 /* ---------------------------------------------------------------------- */
973
974 static LIST_HEAD(tuner_list);
975
976 /* Search for existing radio and/or TV tuners on the given I2C adapter.
977    Note that when this function is called from tuner_probe you can be
978    certain no other devices will be added/deleted at the same time, I2C
979    core protects against that. */
980 static void tuner_lookup(struct i2c_adapter *adap,
981                 struct tuner **radio, struct tuner **tv)
982 {
983         struct tuner *pos;
984
985         *radio = NULL;
986         *tv = NULL;
987
988         list_for_each_entry(pos, &tuner_list, list) {
989                 int mode_mask;
990
991                 if (pos->i2c->adapter != adap ||
992                     strcmp(pos->i2c->driver->driver.name, "tuner"))
993                         continue;
994
995                 mode_mask = pos->mode_mask & ~T_STANDBY;
996                 if (*radio == NULL && mode_mask == T_RADIO)
997                         *radio = pos;
998                 /* Note: currently TDA9887 is the only demod-only
999                    device. If other devices appear then we need to
1000                    make this test more general. */
1001                 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
1002                          (pos->mode_mask & (T_ANALOG_TV | T_DIGITAL_TV)))
1003                         *tv = pos;
1004         }
1005 }
1006
1007 /* During client attach, set_type is called by adapter's attach_inform callback.
1008    set_type must then be completed by tuner_probe.
1009  */
1010 static int tuner_probe(struct i2c_client *client,
1011                        const struct i2c_device_id *id)
1012 {
1013         struct tuner *t;
1014         struct tuner *radio;
1015         struct tuner *tv;
1016
1017         t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
1018         if (NULL == t)
1019                 return -ENOMEM;
1020         v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
1021         t->i2c = client;
1022         t->name = "(tuner unset)";
1023         t->type = UNSET;
1024         t->audmode = V4L2_TUNER_MODE_STEREO;
1025         t->mode_mask = T_UNINITIALIZED;
1026
1027         if (show_i2c) {
1028                 unsigned char buffer[16];
1029                 int i, rc;
1030
1031                 memset(buffer, 0, sizeof(buffer));
1032                 rc = i2c_master_recv(client, buffer, sizeof(buffer));
1033                 tuner_info("I2C RECV = ");
1034                 for (i = 0; i < rc; i++)
1035                         printk(KERN_CONT "%02x ", buffer[i]);
1036                 printk("\n");
1037         }
1038         /* HACK: This test was added to avoid tuner to probe tda9840 and
1039            tea6415c on the MXB card */
1040         if (client->adapter->id == I2C_HW_SAA7146 && client->addr < 0x4a) {
1041                 kfree(t);
1042                 return -ENODEV;
1043         }
1044
1045         /* autodetection code based on the i2c addr */
1046         if (!no_autodetect) {
1047                 switch (client->addr) {
1048                 case 0x10:
1049                         if (tuner_symbol_probe(tea5761_autodetection,
1050                                                t->i2c->adapter,
1051                                                t->i2c->addr) >= 0) {
1052                                 t->type = TUNER_TEA5761;
1053                                 t->mode_mask = T_RADIO;
1054                                 t->mode = T_STANDBY;
1055                                 /* Sets freq to FM range */
1056                                 t->radio_freq = 87.5 * 16000;
1057                                 tuner_lookup(t->i2c->adapter, &radio, &tv);
1058                                 if (tv)
1059                                         tv->mode_mask &= ~T_RADIO;
1060
1061                                 goto register_client;
1062                         }
1063                         return -ENODEV;
1064                 case 0x42:
1065                 case 0x43:
1066                 case 0x4a:
1067                 case 0x4b:
1068                         /* If chip is not tda8290, don't register.
1069                            since it can be tda9887*/
1070                         if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
1071                                                t->i2c->addr) >= 0) {
1072                                 tuner_dbg("tda829x detected\n");
1073                         } else {
1074                                 /* Default is being tda9887 */
1075                                 t->type = TUNER_TDA9887;
1076                                 t->mode_mask = T_RADIO | T_ANALOG_TV |
1077                                                T_DIGITAL_TV;
1078                                 t->mode = T_STANDBY;
1079                                 goto register_client;
1080                         }
1081                         break;
1082                 case 0x60:
1083                         if (tuner_symbol_probe(tea5767_autodetection,
1084                                                t->i2c->adapter, t->i2c->addr)
1085                                         >= 0) {
1086                                 t->type = TUNER_TEA5767;
1087                                 t->mode_mask = T_RADIO;
1088                                 t->mode = T_STANDBY;
1089                                 /* Sets freq to FM range */
1090                                 t->radio_freq = 87.5 * 16000;
1091                                 tuner_lookup(t->i2c->adapter, &radio, &tv);
1092                                 if (tv)
1093                                         tv->mode_mask &= ~T_RADIO;
1094
1095                                 goto register_client;
1096                         }
1097                         break;
1098                 }
1099         }
1100
1101         /* Initializes only the first TV tuner on this adapter. Why only the
1102            first? Because there are some devices (notably the ones with TI
1103            tuners) that have more than one i2c address for the *same* device.
1104            Experience shows that, except for just one case, the first
1105            address is the right one. The exception is a Russian tuner
1106            (ACORP_Y878F). So, the desired behavior is just to enable the
1107            first found TV tuner. */
1108         tuner_lookup(t->i2c->adapter, &radio, &tv);
1109         if (tv == NULL) {
1110                 t->mode_mask = T_ANALOG_TV | T_DIGITAL_TV;
1111                 if (radio == NULL)
1112                         t->mode_mask |= T_RADIO;
1113                 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
1114                 t->tv_freq = 400 * 16; /* Sets freq to VHF High */
1115                 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
1116         }
1117
1118         /* Should be just before return */
1119 register_client:
1120         tuner_info("chip found @ 0x%x (%s)\n", client->addr << 1,
1121                        client->adapter->name);
1122
1123         /* Sets a default mode */
1124         if (t->mode_mask & T_ANALOG_TV) {
1125                 t->mode = V4L2_TUNER_ANALOG_TV;
1126         } else  if (t->mode_mask & T_RADIO) {
1127                 t->mode = V4L2_TUNER_RADIO;
1128         } else {
1129                 t->mode = V4L2_TUNER_DIGITAL_TV;
1130         }
1131         set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
1132         list_add_tail(&t->list, &tuner_list);
1133         return 0;
1134 }
1135
1136 static int tuner_remove(struct i2c_client *client)
1137 {
1138         struct tuner *t = to_tuner(i2c_get_clientdata(client));
1139
1140         v4l2_device_unregister_subdev(&t->sd);
1141         tuner_detach(&t->fe);
1142         t->fe.analog_demod_priv = NULL;
1143
1144         list_del(&t->list);
1145         kfree(t);
1146         return 0;
1147 }
1148
1149 /* ----------------------------------------------------------------------- */
1150
1151 /* This driver supports many devices and the idea is to let the driver
1152    detect which device is present. So rather than listing all supported
1153    devices here, we pretend to support a single, fake device type. */
1154 static const struct i2c_device_id tuner_id[] = {
1155         { "tuner", }, /* autodetect */
1156         { }
1157 };
1158 MODULE_DEVICE_TABLE(i2c, tuner_id);
1159
1160 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
1161         .name = "tuner",
1162         .probe = tuner_probe,
1163         .remove = tuner_remove,
1164         .command = tuner_command,
1165         .suspend = tuner_suspend,
1166         .resume = tuner_resume,
1167         .id_table = tuner_id,
1168 };
1169
1170 /*
1171  * Overrides for Emacs so that we follow Linus's tabbing style.
1172  * ---------------------------------------------------------------------------
1173  * Local variables:
1174  * c-basic-offset: 8
1175  * End:
1176  */