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