V4L/DVB: saa7146/tuner: remove mxb hack
[pandora-kernel.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/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 "mt20xx.h"
24 #include "tda8290.h"
25 #include "tea5761.h"
26 #include "tea5767.h"
27 #include "tuner-xc2028.h"
28 #include "tuner-simple.h"
29 #include "tda9887.h"
30 #include "xc5000.h"
31 #include "tda18271.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         int tune_now = 1;
323
324         if (type == UNSET || type == TUNER_ABSENT) {
325                 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
326                 return;
327         }
328
329         t->type = type;
330         /* prevent invalid config values */
331         t->config = new_config < 256 ? new_config : 0;
332         if (tuner_callback != NULL) {
333                 tuner_dbg("defining GPIO callback\n");
334                 t->fe.callback = tuner_callback;
335         }
336
337         if (t->mode == T_UNINITIALIZED) {
338                 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
339
340                 return;
341         }
342
343         /* discard private data, in case set_type() was previously called */
344         tuner_detach(&t->fe);
345         t->fe.analog_demod_priv = NULL;
346
347         switch (t->type) {
348         case TUNER_MT2032:
349                 if (!dvb_attach(microtune_attach,
350                            &t->fe, t->i2c->adapter, t->i2c->addr))
351                         goto attach_failed;
352                 break;
353         case TUNER_PHILIPS_TDA8290:
354         {
355                 struct tda829x_config cfg = {
356                         .lna_cfg        = t->config,
357                 };
358                 if (!dvb_attach(tda829x_attach, &t->fe, t->i2c->adapter,
359                                 t->i2c->addr, &cfg))
360                         goto attach_failed;
361                 break;
362         }
363         case TUNER_TEA5767:
364                 if (!dvb_attach(tea5767_attach, &t->fe,
365                                 t->i2c->adapter, t->i2c->addr))
366                         goto attach_failed;
367                 t->mode_mask = T_RADIO;
368                 break;
369         case TUNER_TEA5761:
370                 if (!dvb_attach(tea5761_attach, &t->fe,
371                                 t->i2c->adapter, t->i2c->addr))
372                         goto attach_failed;
373                 t->mode_mask = T_RADIO;
374                 break;
375         case TUNER_PHILIPS_FMD1216ME_MK3:
376                 buffer[0] = 0x0b;
377                 buffer[1] = 0xdc;
378                 buffer[2] = 0x9c;
379                 buffer[3] = 0x60;
380                 i2c_master_send(c, buffer, 4);
381                 mdelay(1);
382                 buffer[2] = 0x86;
383                 buffer[3] = 0x54;
384                 i2c_master_send(c, buffer, 4);
385                 if (!dvb_attach(simple_tuner_attach, &t->fe,
386                                 t->i2c->adapter, t->i2c->addr, t->type))
387                         goto attach_failed;
388                 break;
389         case TUNER_PHILIPS_TD1316:
390                 buffer[0] = 0x0b;
391                 buffer[1] = 0xdc;
392                 buffer[2] = 0x86;
393                 buffer[3] = 0xa4;
394                 i2c_master_send(c, buffer, 4);
395                 if (!dvb_attach(simple_tuner_attach, &t->fe,
396                                 t->i2c->adapter, t->i2c->addr, t->type))
397                         goto attach_failed;
398                 break;
399         case TUNER_XC2028:
400         {
401                 struct xc2028_config cfg = {
402                         .i2c_adap  = t->i2c->adapter,
403                         .i2c_addr  = t->i2c->addr,
404                 };
405                 if (!dvb_attach(xc2028_attach, &t->fe, &cfg))
406                         goto attach_failed;
407                 tune_now = 0;
408                 break;
409         }
410         case TUNER_TDA9887:
411                 if (!dvb_attach(tda9887_attach,
412                            &t->fe, t->i2c->adapter, t->i2c->addr))
413                         goto attach_failed;
414                 break;
415         case TUNER_XC5000:
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                 tune_now = 0;
424                 break;
425         }
426         case TUNER_NXP_TDA18271:
427         {
428                 struct tda18271_config cfg = {
429                         .config = t->config,
430                 };
431
432                 if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr,
433                                 t->i2c->adapter, &cfg))
434                         goto attach_failed;
435                 tune_now = 0;
436                 break;
437         }
438         default:
439                 if (!dvb_attach(simple_tuner_attach, &t->fe,
440                                 t->i2c->adapter, t->i2c->addr, t->type))
441                         goto attach_failed;
442
443                 break;
444         }
445
446         if ((NULL == analog_ops->set_params) &&
447             (fe_tuner_ops->set_analog_params)) {
448
449                 t->name = fe_tuner_ops->info.name;
450
451                 t->fe.analog_demod_priv = t;
452                 memcpy(analog_ops, &tuner_analog_ops,
453                        sizeof(struct analog_demod_ops));
454
455         } else {
456                 t->name = analog_ops->info.name;
457         }
458
459         tuner_dbg("type set to %s\n", t->name);
460
461         if (t->mode_mask == T_UNINITIALIZED)
462                 t->mode_mask = new_mode_mask;
463
464         /* Some tuners require more initialization setup before use,
465            such as firmware download or device calibration.
466            trying to set a frequency here will just fail
467            FIXME: better to move set_freq to the tuner code. This is needed
468            on analog tuners for PLL to properly work
469          */
470         if (tune_now)
471                 set_freq(c, (V4L2_TUNER_RADIO == t->mode) ?
472                             t->radio_freq : t->tv_freq);
473
474         tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
475                   c->adapter->name, c->driver->driver.name, c->addr << 1, type,
476                   t->mode_mask);
477         return;
478
479 attach_failed:
480         tuner_dbg("Tuner attach for type = %d failed.\n", t->type);
481         t->type = TUNER_ABSENT;
482         t->mode_mask = T_UNINITIALIZED;
483
484         return;
485 }
486
487 /*
488  * This function apply tuner config to tuner specified
489  * by tun_setup structure. I addr is unset, then admin status
490  * and tun addr status is more precise then current status,
491  * it's applied. Otherwise status and type are applied only to
492  * tuner with exactly the same addr.
493 */
494
495 static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
496 {
497         struct tuner *t = to_tuner(i2c_get_clientdata(c));
498
499         if ( (t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
500                 (t->mode_mask & tun_setup->mode_mask))) ||
501                 (tun_setup->addr == c->addr)) {
502                         set_type(c, tun_setup->type, tun_setup->mode_mask,
503                                  tun_setup->config, tun_setup->tuner_callback);
504         } else
505                 tuner_dbg("set addr discarded for type %i, mask %x. "
506                           "Asked to change tuner at addr 0x%02x, with mask %x\n",
507                           t->type, t->mode_mask,
508                           tun_setup->addr, tun_setup->mode_mask);
509 }
510
511 static inline int check_mode(struct tuner *t, char *cmd)
512 {
513         if ((1 << t->mode & t->mode_mask) == 0) {
514                 return -EINVAL;
515         }
516
517         switch (t->mode) {
518         case V4L2_TUNER_RADIO:
519                 tuner_dbg("Cmd %s accepted for radio\n", cmd);
520                 break;
521         case V4L2_TUNER_ANALOG_TV:
522                 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
523                 break;
524         case V4L2_TUNER_DIGITAL_TV:
525                 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
526                 break;
527         }
528         return 0;
529 }
530
531 /* get more precise norm info from insmod option */
532 static int tuner_fixup_std(struct tuner *t)
533 {
534         if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
535                 switch (pal[0]) {
536                 case '6':
537                         tuner_dbg ("insmod fixup: PAL => PAL-60\n");
538                         t->std = V4L2_STD_PAL_60;
539                         break;
540                 case 'b':
541                 case 'B':
542                 case 'g':
543                 case 'G':
544                         tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
545                         t->std = V4L2_STD_PAL_BG;
546                         break;
547                 case 'i':
548                 case 'I':
549                         tuner_dbg ("insmod fixup: PAL => PAL-I\n");
550                         t->std = V4L2_STD_PAL_I;
551                         break;
552                 case 'd':
553                 case 'D':
554                 case 'k':
555                 case 'K':
556                         tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
557                         t->std = V4L2_STD_PAL_DK;
558                         break;
559                 case 'M':
560                 case 'm':
561                         tuner_dbg ("insmod fixup: PAL => PAL-M\n");
562                         t->std = V4L2_STD_PAL_M;
563                         break;
564                 case 'N':
565                 case 'n':
566                         if (pal[1] == 'c' || pal[1] == 'C') {
567                                 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
568                                 t->std = V4L2_STD_PAL_Nc;
569                         } else {
570                                 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
571                                 t->std = V4L2_STD_PAL_N;
572                         }
573                         break;
574                 case '-':
575                         /* default parameter, do nothing */
576                         break;
577                 default:
578                         tuner_warn ("pal= argument not recognised\n");
579                         break;
580                 }
581         }
582         if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
583                 switch (secam[0]) {
584                 case 'b':
585                 case 'B':
586                 case 'g':
587                 case 'G':
588                 case 'h':
589                 case 'H':
590                         tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
591                         t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
592                         break;
593                 case 'd':
594                 case 'D':
595                 case 'k':
596                 case 'K':
597                         tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
598                         t->std = V4L2_STD_SECAM_DK;
599                         break;
600                 case 'l':
601                 case 'L':
602                         if ((secam[1]=='C')||(secam[1]=='c')) {
603                                 tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n");
604                                 t->std = V4L2_STD_SECAM_LC;
605                         } else {
606                                 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
607                                 t->std = V4L2_STD_SECAM_L;
608                         }
609                         break;
610                 case '-':
611                         /* default parameter, do nothing */
612                         break;
613                 default:
614                         tuner_warn ("secam= argument not recognised\n");
615                         break;
616                 }
617         }
618
619         if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
620                 switch (ntsc[0]) {
621                 case 'm':
622                 case 'M':
623                         tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
624                         t->std = V4L2_STD_NTSC_M;
625                         break;
626                 case 'j':
627                 case 'J':
628                         tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
629                         t->std = V4L2_STD_NTSC_M_JP;
630                         break;
631                 case 'k':
632                 case 'K':
633                         tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
634                         t->std = V4L2_STD_NTSC_M_KR;
635                         break;
636                 case '-':
637                         /* default parameter, do nothing */
638                         break;
639                 default:
640                         tuner_info("ntsc= argument not recognised\n");
641                         break;
642                 }
643         }
644         return 0;
645 }
646
647 static void tuner_status(struct dvb_frontend *fe)
648 {
649         struct tuner *t = fe->analog_demod_priv;
650         unsigned long freq, freq_fraction;
651         struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
652         struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
653         const char *p;
654
655         switch (t->mode) {
656                 case V4L2_TUNER_RADIO:      p = "radio"; break;
657                 case V4L2_TUNER_ANALOG_TV:  p = "analog TV"; break;
658                 case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break;
659                 default: p = "undefined"; break;
660         }
661         if (t->mode == V4L2_TUNER_RADIO) {
662                 freq = t->radio_freq / 16000;
663                 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
664         } else {
665                 freq = t->tv_freq / 16;
666                 freq_fraction = (t->tv_freq % 16) * 100 / 16;
667         }
668         tuner_info("Tuner mode:      %s\n", p);
669         tuner_info("Frequency:       %lu.%02lu MHz\n", freq, freq_fraction);
670         tuner_info("Standard:        0x%08lx\n", (unsigned long)t->std);
671         if (t->mode != V4L2_TUNER_RADIO)
672                return;
673         if (fe_tuner_ops->get_status) {
674                 u32 tuner_status;
675
676                 fe_tuner_ops->get_status(&t->fe, &tuner_status);
677                 if (tuner_status & TUNER_STATUS_LOCKED)
678                         tuner_info("Tuner is locked.\n");
679                 if (tuner_status & TUNER_STATUS_STEREO)
680                         tuner_info("Stereo:          yes\n");
681         }
682         if (analog_ops->has_signal)
683                 tuner_info("Signal strength: %d\n",
684                            analog_ops->has_signal(fe));
685         if (analog_ops->is_stereo)
686                 tuner_info("Stereo:          %s\n",
687                            analog_ops->is_stereo(fe) ? "yes" : "no");
688 }
689
690 /* ---------------------------------------------------------------------- */
691
692 /*
693  * Switch tuner to other mode. If tuner support both tv and radio,
694  * set another frequency to some value (This is needed for some pal
695  * tuners to avoid locking). Otherwise, just put second tuner in
696  * standby mode.
697  */
698
699 static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
700 {
701         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
702
703         if (mode == t->mode)
704                 return 0;
705
706         t->mode = mode;
707
708         if (check_mode(t, cmd) == -EINVAL) {
709                 tuner_dbg("Tuner doesn't support this mode. "
710                           "Putting tuner to sleep\n");
711                 t->mode = T_STANDBY;
712                 if (analog_ops->standby)
713                         analog_ops->standby(&t->fe);
714                 return -EINVAL;
715         }
716         return 0;
717 }
718
719 #define switch_v4l2()   if (!t->using_v4l2) \
720                             tuner_dbg("switching to v4l2\n"); \
721                         t->using_v4l2 = 1;
722
723 static inline int check_v4l2(struct tuner *t)
724 {
725         /* bttv still uses both v4l1 and v4l2 calls to the tuner (v4l2 for
726            TV, v4l1 for radio), until that is fixed this code is disabled.
727            Otherwise the radio (v4l1) wouldn't tune after using the TV (v4l2)
728            first. */
729         return 0;
730 }
731
732 static int tuner_s_type_addr(struct v4l2_subdev *sd, struct tuner_setup *type)
733 {
734         struct tuner *t = to_tuner(sd);
735         struct i2c_client *client = v4l2_get_subdevdata(sd);
736
737         tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
738                         type->type,
739                         type->addr,
740                         type->mode_mask,
741                         type->config);
742
743         set_addr(client, type);
744         return 0;
745 }
746
747 static int tuner_s_radio(struct v4l2_subdev *sd)
748 {
749         struct tuner *t = to_tuner(sd);
750         struct i2c_client *client = v4l2_get_subdevdata(sd);
751
752         if (set_mode(client, t, V4L2_TUNER_RADIO, "s_radio") == -EINVAL)
753                 return 0;
754         if (t->radio_freq)
755                 set_freq(client, t->radio_freq);
756         return 0;
757 }
758
759 static int tuner_s_power(struct v4l2_subdev *sd, int on)
760 {
761         struct tuner *t = to_tuner(sd);
762         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
763
764         if (on)
765                 return 0;
766
767         tuner_dbg("Putting tuner to sleep\n");
768
769         if (check_mode(t, "s_power") == -EINVAL)
770                 return 0;
771         t->mode = T_STANDBY;
772         if (analog_ops->standby)
773                 analog_ops->standby(&t->fe);
774         return 0;
775 }
776
777 static int tuner_s_config(struct v4l2_subdev *sd, const struct v4l2_priv_tun_config *cfg)
778 {
779         struct tuner *t = to_tuner(sd);
780         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
781
782         if (t->type != cfg->tuner)
783                 return 0;
784
785         if (analog_ops->set_config) {
786                 analog_ops->set_config(&t->fe, cfg->priv);
787                 return 0;
788         }
789
790         tuner_dbg("Tuner frontend module has no way to set config\n");
791         return 0;
792 }
793
794 /* --- v4l ioctls --- */
795 /* take care: bttv does userspace copying, we'll get a
796    kernel pointer here... */
797 static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
798 {
799         struct tuner *t = to_tuner(sd);
800         struct i2c_client *client = v4l2_get_subdevdata(sd);
801
802         if (set_mode(client, t, V4L2_TUNER_ANALOG_TV, "s_std") == -EINVAL)
803                 return 0;
804
805         switch_v4l2();
806
807         t->std = std;
808         tuner_fixup_std(t);
809         if (t->tv_freq)
810                 set_freq(client, t->tv_freq);
811         return 0;
812 }
813
814 static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
815 {
816         struct tuner *t = to_tuner(sd);
817         struct i2c_client *client = v4l2_get_subdevdata(sd);
818
819         if (set_mode(client, t, f->type, "s_frequency") == -EINVAL)
820                 return 0;
821         switch_v4l2();
822         set_freq(client, f->frequency);
823
824         return 0;
825 }
826
827 static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
828 {
829         struct tuner *t = to_tuner(sd);
830         struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
831
832         if (check_mode(t, "g_frequency") == -EINVAL)
833                 return 0;
834         switch_v4l2();
835         f->type = t->mode;
836         if (fe_tuner_ops->get_frequency) {
837                 u32 abs_freq;
838
839                 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
840                 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
841                         DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
842                         DIV_ROUND_CLOSEST(abs_freq, 62500);
843                 return 0;
844         }
845         f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
846                 t->radio_freq : t->tv_freq;
847         return 0;
848 }
849
850 static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
851 {
852         struct tuner *t = to_tuner(sd);
853         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
854         struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
855
856         if (check_mode(t, "g_tuner") == -EINVAL)
857                 return 0;
858         switch_v4l2();
859
860         vt->type = t->mode;
861         if (analog_ops->get_afc)
862                 vt->afc = analog_ops->get_afc(&t->fe);
863         if (t->mode == V4L2_TUNER_ANALOG_TV)
864                 vt->capability |= V4L2_TUNER_CAP_NORM;
865         if (t->mode != V4L2_TUNER_RADIO) {
866                 vt->rangelow = tv_range[0] * 16;
867                 vt->rangehigh = tv_range[1] * 16;
868                 return 0;
869         }
870
871         /* radio mode */
872         vt->rxsubchans =
873                 V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
874         if (fe_tuner_ops->get_status) {
875                 u32 tuner_status;
876
877                 fe_tuner_ops->get_status(&t->fe, &tuner_status);
878                 vt->rxsubchans =
879                         (tuner_status & TUNER_STATUS_STEREO) ?
880                         V4L2_TUNER_SUB_STEREO :
881                         V4L2_TUNER_SUB_MONO;
882         } else {
883                 if (analog_ops->is_stereo) {
884                         vt->rxsubchans =
885                                 analog_ops->is_stereo(&t->fe) ?
886                                 V4L2_TUNER_SUB_STEREO :
887                                 V4L2_TUNER_SUB_MONO;
888                 }
889         }
890         if (analog_ops->has_signal)
891                 vt->signal = analog_ops->has_signal(&t->fe);
892         vt->capability |=
893                 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
894         vt->audmode = t->audmode;
895         vt->rangelow = radio_range[0] * 16000;
896         vt->rangehigh = radio_range[1] * 16000;
897         return 0;
898 }
899
900 static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
901 {
902         struct tuner *t = to_tuner(sd);
903         struct i2c_client *client = v4l2_get_subdevdata(sd);
904
905         if (check_mode(t, "s_tuner") == -EINVAL)
906                 return 0;
907
908         switch_v4l2();
909
910         /* do nothing unless we're a radio tuner */
911         if (t->mode != V4L2_TUNER_RADIO)
912                 return 0;
913         t->audmode = vt->audmode;
914         set_radio_freq(client, t->radio_freq);
915         return 0;
916 }
917
918 static int tuner_log_status(struct v4l2_subdev *sd)
919 {
920         struct tuner *t = to_tuner(sd);
921         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
922
923         if (analog_ops->tuner_status)
924                 analog_ops->tuner_status(&t->fe);
925         return 0;
926 }
927
928 static int tuner_suspend(struct i2c_client *c, pm_message_t state)
929 {
930         struct tuner *t = to_tuner(i2c_get_clientdata(c));
931
932         tuner_dbg("suspend\n");
933         /* FIXME: power down ??? */
934         return 0;
935 }
936
937 static int tuner_resume(struct i2c_client *c)
938 {
939         struct tuner *t = to_tuner(i2c_get_clientdata(c));
940
941         tuner_dbg("resume\n");
942         if (V4L2_TUNER_RADIO == t->mode) {
943                 if (t->radio_freq)
944                         set_freq(c, t->radio_freq);
945         } else {
946                 if (t->tv_freq)
947                         set_freq(c, t->tv_freq);
948         }
949         return 0;
950 }
951
952 static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
953 {
954         struct v4l2_subdev *sd = i2c_get_clientdata(client);
955
956         /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
957            to handle it here.
958            There must be a better way of doing this... */
959         switch (cmd) {
960         case TUNER_SET_CONFIG:
961                 return tuner_s_config(sd, arg);
962         }
963         return -ENOIOCTLCMD;
964 }
965
966 /* ----------------------------------------------------------------------- */
967
968 static const struct v4l2_subdev_core_ops tuner_core_ops = {
969         .log_status = tuner_log_status,
970         .s_std = tuner_s_std,
971         .s_power = tuner_s_power,
972 };
973
974 static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
975         .s_radio = tuner_s_radio,
976         .g_tuner = tuner_g_tuner,
977         .s_tuner = tuner_s_tuner,
978         .s_frequency = tuner_s_frequency,
979         .g_frequency = tuner_g_frequency,
980         .s_type_addr = tuner_s_type_addr,
981         .s_config = tuner_s_config,
982 };
983
984 static const struct v4l2_subdev_ops tuner_ops = {
985         .core = &tuner_core_ops,
986         .tuner = &tuner_tuner_ops,
987 };
988
989 /* ---------------------------------------------------------------------- */
990
991 static LIST_HEAD(tuner_list);
992
993 /* Search for existing radio and/or TV tuners on the given I2C adapter.
994    Note that when this function is called from tuner_probe you can be
995    certain no other devices will be added/deleted at the same time, I2C
996    core protects against that. */
997 static void tuner_lookup(struct i2c_adapter *adap,
998                 struct tuner **radio, struct tuner **tv)
999 {
1000         struct tuner *pos;
1001
1002         *radio = NULL;
1003         *tv = NULL;
1004
1005         list_for_each_entry(pos, &tuner_list, list) {
1006                 int mode_mask;
1007
1008                 if (pos->i2c->adapter != adap ||
1009                     strcmp(pos->i2c->driver->driver.name, "tuner"))
1010                         continue;
1011
1012                 mode_mask = pos->mode_mask & ~T_STANDBY;
1013                 if (*radio == NULL && mode_mask == T_RADIO)
1014                         *radio = pos;
1015                 /* Note: currently TDA9887 is the only demod-only
1016                    device. If other devices appear then we need to
1017                    make this test more general. */
1018                 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
1019                          (pos->mode_mask & (T_ANALOG_TV | T_DIGITAL_TV)))
1020                         *tv = pos;
1021         }
1022 }
1023
1024 /* During client attach, set_type is called by adapter's attach_inform callback.
1025    set_type must then be completed by tuner_probe.
1026  */
1027 static int tuner_probe(struct i2c_client *client,
1028                        const struct i2c_device_id *id)
1029 {
1030         struct tuner *t;
1031         struct tuner *radio;
1032         struct tuner *tv;
1033
1034         t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
1035         if (NULL == t)
1036                 return -ENOMEM;
1037         v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
1038         t->i2c = client;
1039         t->name = "(tuner unset)";
1040         t->type = UNSET;
1041         t->audmode = V4L2_TUNER_MODE_STEREO;
1042         t->mode_mask = T_UNINITIALIZED;
1043
1044         if (show_i2c) {
1045                 unsigned char buffer[16];
1046                 int i, rc;
1047
1048                 memset(buffer, 0, sizeof(buffer));
1049                 rc = i2c_master_recv(client, buffer, sizeof(buffer));
1050                 tuner_info("I2C RECV = ");
1051                 for (i = 0; i < rc; i++)
1052                         printk(KERN_CONT "%02x ", buffer[i]);
1053                 printk("\n");
1054         }
1055
1056         /* autodetection code based on the i2c addr */
1057         if (!no_autodetect) {
1058                 switch (client->addr) {
1059                 case 0x10:
1060                         if (tuner_symbol_probe(tea5761_autodetection,
1061                                                t->i2c->adapter,
1062                                                t->i2c->addr) >= 0) {
1063                                 t->type = TUNER_TEA5761;
1064                                 t->mode_mask = T_RADIO;
1065                                 t->mode = T_STANDBY;
1066                                 /* Sets freq to FM range */
1067                                 t->radio_freq = 87.5 * 16000;
1068                                 tuner_lookup(t->i2c->adapter, &radio, &tv);
1069                                 if (tv)
1070                                         tv->mode_mask &= ~T_RADIO;
1071
1072                                 goto register_client;
1073                         }
1074                         kfree(t);
1075                         return -ENODEV;
1076                 case 0x42:
1077                 case 0x43:
1078                 case 0x4a:
1079                 case 0x4b:
1080                         /* If chip is not tda8290, don't register.
1081                            since it can be tda9887*/
1082                         if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
1083                                                t->i2c->addr) >= 0) {
1084                                 tuner_dbg("tda829x detected\n");
1085                         } else {
1086                                 /* Default is being tda9887 */
1087                                 t->type = TUNER_TDA9887;
1088                                 t->mode_mask = T_RADIO | T_ANALOG_TV |
1089                                                T_DIGITAL_TV;
1090                                 t->mode = T_STANDBY;
1091                                 goto register_client;
1092                         }
1093                         break;
1094                 case 0x60:
1095                         if (tuner_symbol_probe(tea5767_autodetection,
1096                                                t->i2c->adapter, t->i2c->addr)
1097                                         >= 0) {
1098                                 t->type = TUNER_TEA5767;
1099                                 t->mode_mask = T_RADIO;
1100                                 t->mode = T_STANDBY;
1101                                 /* Sets freq to FM range */
1102                                 t->radio_freq = 87.5 * 16000;
1103                                 tuner_lookup(t->i2c->adapter, &radio, &tv);
1104                                 if (tv)
1105                                         tv->mode_mask &= ~T_RADIO;
1106
1107                                 goto register_client;
1108                         }
1109                         break;
1110                 }
1111         }
1112
1113         /* Initializes only the first TV tuner on this adapter. Why only the
1114            first? Because there are some devices (notably the ones with TI
1115            tuners) that have more than one i2c address for the *same* device.
1116            Experience shows that, except for just one case, the first
1117            address is the right one. The exception is a Russian tuner
1118            (ACORP_Y878F). So, the desired behavior is just to enable the
1119            first found TV tuner. */
1120         tuner_lookup(t->i2c->adapter, &radio, &tv);
1121         if (tv == NULL) {
1122                 t->mode_mask = T_ANALOG_TV | T_DIGITAL_TV;
1123                 if (radio == NULL)
1124                         t->mode_mask |= T_RADIO;
1125                 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
1126                 t->tv_freq = 400 * 16; /* Sets freq to VHF High */
1127                 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
1128         }
1129
1130         /* Should be just before return */
1131 register_client:
1132         tuner_info("chip found @ 0x%x (%s)\n", client->addr << 1,
1133                        client->adapter->name);
1134
1135         /* Sets a default mode */
1136         if (t->mode_mask & T_ANALOG_TV) {
1137                 t->mode = V4L2_TUNER_ANALOG_TV;
1138         } else  if (t->mode_mask & T_RADIO) {
1139                 t->mode = V4L2_TUNER_RADIO;
1140         } else {
1141                 t->mode = V4L2_TUNER_DIGITAL_TV;
1142         }
1143         set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
1144         list_add_tail(&t->list, &tuner_list);
1145         return 0;
1146 }
1147
1148 static int tuner_remove(struct i2c_client *client)
1149 {
1150         struct tuner *t = to_tuner(i2c_get_clientdata(client));
1151
1152         v4l2_device_unregister_subdev(&t->sd);
1153         tuner_detach(&t->fe);
1154         t->fe.analog_demod_priv = NULL;
1155
1156         list_del(&t->list);
1157         kfree(t);
1158         return 0;
1159 }
1160
1161 /* ----------------------------------------------------------------------- */
1162
1163 /* This driver supports many devices and the idea is to let the driver
1164    detect which device is present. So rather than listing all supported
1165    devices here, we pretend to support a single, fake device type. */
1166 static const struct i2c_device_id tuner_id[] = {
1167         { "tuner", }, /* autodetect */
1168         { }
1169 };
1170 MODULE_DEVICE_TABLE(i2c, tuner_id);
1171
1172 static struct i2c_driver tuner_driver = {
1173         .driver = {
1174                 .owner  = THIS_MODULE,
1175                 .name   = "tuner",
1176         },
1177         .probe          = tuner_probe,
1178         .remove         = tuner_remove,
1179         .command        = tuner_command,
1180         .suspend        = tuner_suspend,
1181         .resume         = tuner_resume,
1182         .id_table       = tuner_id,
1183 };
1184
1185 static __init int init_tuner(void)
1186 {
1187         return i2c_add_driver(&tuner_driver);
1188 }
1189
1190 static __exit void exit_tuner(void)
1191 {
1192         i2c_del_driver(&tuner_driver);
1193 }
1194
1195 module_init(init_tuner);
1196 module_exit(exit_tuner);
1197
1198 /*
1199  * Overrides for Emacs so that we follow Linus's tabbing style.
1200  * ---------------------------------------------------------------------------
1201  * Local variables:
1202  * c-basic-offset: 8
1203  * End:
1204  */