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