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