]> Pileus Git - ~andy/linux/blob - drivers/media/video/tuner-core.c
Merge branch 'rc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[~andy/linux] / drivers / media / video / tuner-core.c
1 /*
2  * i2c tv tuner chip device driver
3  * core core, i.e. kernel interfaces, registering and so on
4  *
5  * Copyright(c) by Ralph Metzler, Gerd Knorr, Gunther Mayer
6  *
7  * Copyright(c) 2005-2011 by Mauro Carvalho Chehab
8  *      - Added support for a separate Radio tuner
9  *      - Major rework and cleanups at the code
10  *
11  * This driver supports many devices and the idea is to let the driver
12  * detect which device is present. So rather than listing all supported
13  * devices here, we pretend to support a single, fake device type that will
14  * handle both radio and analog TV tuning.
15  */
16
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/timer.h>
21 #include <linux/delay.h>
22 #include <linux/errno.h>
23 #include <linux/slab.h>
24 #include <linux/poll.h>
25 #include <linux/i2c.h>
26 #include <linux/types.h>
27 #include <linux/init.h>
28 #include <linux/videodev2.h>
29 #include <media/tuner.h>
30 #include <media/tuner-types.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-ioctl.h>
33 #include "mt20xx.h"
34 #include "tda8290.h"
35 #include "tea5761.h"
36 #include "tea5767.h"
37 #include "tuner-xc2028.h"
38 #include "tuner-simple.h"
39 #include "tda9887.h"
40 #include "xc5000.h"
41 #include "tda18271.h"
42 #include "xc4000.h"
43
44 #define UNSET (-1U)
45
46 #define PREFIX (t->i2c->driver->driver.name)
47
48 /*
49  * Driver modprobe parameters
50  */
51
52 /* insmod options used at init time => read/only */
53 static unsigned int addr;
54 static unsigned int no_autodetect;
55 static unsigned int show_i2c;
56
57 module_param(addr, int, 0444);
58 module_param(no_autodetect, int, 0444);
59 module_param(show_i2c, int, 0444);
60
61 /* insmod options used at runtime => read/write */
62 static int tuner_debug;
63 static unsigned int tv_range[2] = { 44, 958 };
64 static unsigned int radio_range[2] = { 65, 108 };
65 static char pal[] = "--";
66 static char secam[] = "--";
67 static char ntsc[] = "-";
68
69 module_param_named(debug, tuner_debug, int, 0644);
70 module_param_array(tv_range, int, NULL, 0644);
71 module_param_array(radio_range, int, NULL, 0644);
72 module_param_string(pal, pal, sizeof(pal), 0644);
73 module_param_string(secam, secam, sizeof(secam), 0644);
74 module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
75
76 /*
77  * Static vars
78  */
79
80 static LIST_HEAD(tuner_list);
81 static const struct v4l2_subdev_ops tuner_ops;
82
83 /*
84  * Debug macros
85  */
86
87 #define tuner_warn(fmt, arg...) do {                    \
88         printk(KERN_WARNING "%s %d-%04x: " fmt, PREFIX, \
89                i2c_adapter_id(t->i2c->adapter),         \
90                t->i2c->addr, ##arg);                    \
91          } while (0)
92
93 #define tuner_info(fmt, arg...) do {                    \
94         printk(KERN_INFO "%s %d-%04x: " fmt, PREFIX,    \
95                i2c_adapter_id(t->i2c->adapter),         \
96                t->i2c->addr, ##arg);                    \
97          } while (0)
98
99 #define tuner_err(fmt, arg...) do {                     \
100         printk(KERN_ERR "%s %d-%04x: " fmt, PREFIX,     \
101                i2c_adapter_id(t->i2c->adapter),         \
102                t->i2c->addr, ##arg);                    \
103          } while (0)
104
105 #define tuner_dbg(fmt, arg...) do {                             \
106         if (tuner_debug)                                        \
107                 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX,   \
108                        i2c_adapter_id(t->i2c->adapter),         \
109                        t->i2c->addr, ##arg);                    \
110          } while (0)
111
112 /*
113  * Internal struct used inside the driver
114  */
115
116 struct tuner {
117         /* device */
118         struct dvb_frontend fe;
119         struct i2c_client   *i2c;
120         struct v4l2_subdev  sd;
121         struct list_head    list;
122
123         /* keep track of the current settings */
124         v4l2_std_id         std;
125         unsigned int        tv_freq;
126         unsigned int        radio_freq;
127         unsigned int        audmode;
128
129         enum v4l2_tuner_type mode;
130         unsigned int        mode_mask; /* Combination of allowable modes */
131
132         bool                standby;    /* Standby mode */
133
134         unsigned int        type; /* chip type id */
135         unsigned int        config;
136         const char          *name;
137 };
138
139 /*
140  * Function prototypes
141  */
142
143 static void set_tv_freq(struct i2c_client *c, unsigned int freq);
144 static void set_radio_freq(struct i2c_client *c, unsigned int freq);
145
146 /*
147  * tuner attach/detach logic
148  */
149
150 /* This macro allows us to probe dynamically, avoiding static links */
151 #ifdef CONFIG_MEDIA_ATTACH
152 #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
153         int __r = -EINVAL; \
154         typeof(&FUNCTION) __a = symbol_request(FUNCTION); \
155         if (__a) { \
156                 __r = (int) __a(ARGS); \
157                 symbol_put(FUNCTION); \
158         } else { \
159                 printk(KERN_ERR "TUNER: Unable to find " \
160                                 "symbol "#FUNCTION"()\n"); \
161         } \
162         __r; \
163 })
164
165 static void tuner_detach(struct dvb_frontend *fe)
166 {
167         if (fe->ops.tuner_ops.release) {
168                 fe->ops.tuner_ops.release(fe);
169                 symbol_put_addr(fe->ops.tuner_ops.release);
170         }
171         if (fe->ops.analog_ops.release) {
172                 fe->ops.analog_ops.release(fe);
173                 symbol_put_addr(fe->ops.analog_ops.release);
174         }
175 }
176 #else
177 #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
178         FUNCTION(ARGS); \
179 })
180
181 static void tuner_detach(struct dvb_frontend *fe)
182 {
183         if (fe->ops.tuner_ops.release)
184                 fe->ops.tuner_ops.release(fe);
185         if (fe->ops.analog_ops.release)
186                 fe->ops.analog_ops.release(fe);
187 }
188 #endif
189
190
191 static inline struct tuner *to_tuner(struct v4l2_subdev *sd)
192 {
193         return container_of(sd, struct tuner, sd);
194 }
195
196 /*
197  * struct analog_demod_ops callbacks
198  */
199
200 static void fe_set_params(struct dvb_frontend *fe,
201                           struct analog_parameters *params)
202 {
203         struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
204         struct tuner *t = fe->analog_demod_priv;
205
206         if (NULL == fe_tuner_ops->set_analog_params) {
207                 tuner_warn("Tuner frontend module has no way to set freq\n");
208                 return;
209         }
210         fe_tuner_ops->set_analog_params(fe, params);
211 }
212
213 static void fe_standby(struct dvb_frontend *fe)
214 {
215         struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
216
217         if (fe_tuner_ops->sleep)
218                 fe_tuner_ops->sleep(fe);
219 }
220
221 static int fe_has_signal(struct dvb_frontend *fe)
222 {
223         u16 strength = 0;
224
225         if (fe->ops.tuner_ops.get_rf_strength)
226                 fe->ops.tuner_ops.get_rf_strength(fe, &strength);
227
228         return strength;
229 }
230
231 static int fe_set_config(struct dvb_frontend *fe, void *priv_cfg)
232 {
233         struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
234         struct tuner *t = fe->analog_demod_priv;
235
236         if (fe_tuner_ops->set_config)
237                 return fe_tuner_ops->set_config(fe, priv_cfg);
238
239         tuner_warn("Tuner frontend module has no way to set config\n");
240
241         return 0;
242 }
243
244 static void tuner_status(struct dvb_frontend *fe);
245
246 static struct analog_demod_ops tuner_analog_ops = {
247         .set_params     = fe_set_params,
248         .standby        = fe_standby,
249         .has_signal     = fe_has_signal,
250         .set_config     = fe_set_config,
251         .tuner_status   = tuner_status
252 };
253
254 /*
255  * Functions to select between radio and TV and tuner probe/remove functions
256  */
257
258 /**
259  * set_type - Sets the tuner type for a given device
260  *
261  * @c:                  i2c_client descriptoy
262  * @type:               type of the tuner (e. g. tuner number)
263  * @new_mode_mask:      Indicates if tuner supports TV and/or Radio
264  * @new_config:         an optional parameter ranging from 0-255 used by
265                         a few tuners to adjust an internal parameter,
266                         like LNA mode
267  * @tuner_callback:     an optional function to be called when switching
268  *                      to analog mode
269  *
270  * This function applys the tuner config to tuner specified
271  * by tun_setup structure. It contains several per-tuner initialization "magic"
272  */
273 static void set_type(struct i2c_client *c, unsigned int type,
274                      unsigned int new_mode_mask, unsigned int new_config,
275                      int (*tuner_callback) (void *dev, int component, int cmd, int arg))
276 {
277         struct tuner *t = to_tuner(i2c_get_clientdata(c));
278         struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
279         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
280         unsigned char buffer[4];
281         int tune_now = 1;
282
283         if (type == UNSET || type == TUNER_ABSENT) {
284                 tuner_dbg("tuner 0x%02x: Tuner type absent\n", c->addr);
285                 return;
286         }
287
288         t->type = type;
289         /* prevent invalid config values */
290         t->config = new_config < 256 ? new_config : 0;
291         if (tuner_callback != NULL) {
292                 tuner_dbg("defining GPIO callback\n");
293                 t->fe.callback = tuner_callback;
294         }
295
296         /* discard private data, in case set_type() was previously called */
297         tuner_detach(&t->fe);
298         t->fe.analog_demod_priv = NULL;
299
300         switch (t->type) {
301         case TUNER_MT2032:
302                 if (!dvb_attach(microtune_attach,
303                            &t->fe, t->i2c->adapter, t->i2c->addr))
304                         goto attach_failed;
305                 break;
306         case TUNER_PHILIPS_TDA8290:
307         {
308                 struct tda829x_config cfg = {
309                         .lna_cfg        = t->config,
310                 };
311                 if (!dvb_attach(tda829x_attach, &t->fe, t->i2c->adapter,
312                                 t->i2c->addr, &cfg))
313                         goto attach_failed;
314                 break;
315         }
316         case TUNER_TEA5767:
317                 if (!dvb_attach(tea5767_attach, &t->fe,
318                                 t->i2c->adapter, t->i2c->addr))
319                         goto attach_failed;
320                 t->mode_mask = T_RADIO;
321                 break;
322         case TUNER_TEA5761:
323                 if (!dvb_attach(tea5761_attach, &t->fe,
324                                 t->i2c->adapter, t->i2c->addr))
325                         goto attach_failed;
326                 t->mode_mask = T_RADIO;
327                 break;
328         case TUNER_PHILIPS_FMD1216ME_MK3:
329         case TUNER_PHILIPS_FMD1216MEX_MK3:
330                 buffer[0] = 0x0b;
331                 buffer[1] = 0xdc;
332                 buffer[2] = 0x9c;
333                 buffer[3] = 0x60;
334                 i2c_master_send(c, buffer, 4);
335                 mdelay(1);
336                 buffer[2] = 0x86;
337                 buffer[3] = 0x54;
338                 i2c_master_send(c, buffer, 4);
339                 if (!dvb_attach(simple_tuner_attach, &t->fe,
340                                 t->i2c->adapter, t->i2c->addr, t->type))
341                         goto attach_failed;
342                 break;
343         case TUNER_PHILIPS_TD1316:
344                 buffer[0] = 0x0b;
345                 buffer[1] = 0xdc;
346                 buffer[2] = 0x86;
347                 buffer[3] = 0xa4;
348                 i2c_master_send(c, buffer, 4);
349                 if (!dvb_attach(simple_tuner_attach, &t->fe,
350                                 t->i2c->adapter, t->i2c->addr, t->type))
351                         goto attach_failed;
352                 break;
353         case TUNER_XC2028:
354         {
355                 struct xc2028_config cfg = {
356                         .i2c_adap  = t->i2c->adapter,
357                         .i2c_addr  = t->i2c->addr,
358                 };
359                 if (!dvb_attach(xc2028_attach, &t->fe, &cfg))
360                         goto attach_failed;
361                 tune_now = 0;
362                 break;
363         }
364         case TUNER_TDA9887:
365                 if (!dvb_attach(tda9887_attach,
366                            &t->fe, t->i2c->adapter, t->i2c->addr))
367                         goto attach_failed;
368                 break;
369         case TUNER_XC5000:
370         {
371                 struct xc5000_config xc5000_cfg = {
372                         .i2c_address = t->i2c->addr,
373                         /* if_khz will be set at dvb_attach() */
374                         .if_khz   = 0,
375                 };
376
377                 if (!dvb_attach(xc5000_attach,
378                                 &t->fe, t->i2c->adapter, &xc5000_cfg))
379                         goto attach_failed;
380                 tune_now = 0;
381                 break;
382         }
383         case TUNER_NXP_TDA18271:
384         {
385                 struct tda18271_config cfg = {
386                         .config = t->config,
387                         .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
388                 };
389
390                 if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr,
391                                 t->i2c->adapter, &cfg))
392                         goto attach_failed;
393                 tune_now = 0;
394                 break;
395         }
396         case TUNER_XC4000:
397         {
398                 struct xc4000_config xc4000_cfg = {
399                         .i2c_address      = t->i2c->addr,
400                         /* FIXME: the correct parameters will be set */
401                         /* only when the digital dvb_attach() occurs */
402                         .default_pm       = 0,
403                         .dvb_amplitude    = 0,
404                         .set_smoothedcvbs = 0,
405                         .if_khz           = 0
406                 };
407                 if (!dvb_attach(xc4000_attach,
408                                 &t->fe, t->i2c->adapter, &xc4000_cfg))
409                         goto attach_failed;
410                 tune_now = 0;
411                 break;
412         }
413         default:
414                 if (!dvb_attach(simple_tuner_attach, &t->fe,
415                                 t->i2c->adapter, t->i2c->addr, t->type))
416                         goto attach_failed;
417
418                 break;
419         }
420
421         if ((NULL == analog_ops->set_params) &&
422             (fe_tuner_ops->set_analog_params)) {
423
424                 t->name = fe_tuner_ops->info.name;
425
426                 t->fe.analog_demod_priv = t;
427                 memcpy(analog_ops, &tuner_analog_ops,
428                        sizeof(struct analog_demod_ops));
429
430         } else {
431                 t->name = analog_ops->info.name;
432         }
433
434         tuner_dbg("type set to %s\n", t->name);
435
436         t->mode_mask = new_mode_mask;
437
438         /* Some tuners require more initialization setup before use,
439            such as firmware download or device calibration.
440            trying to set a frequency here will just fail
441            FIXME: better to move set_freq to the tuner code. This is needed
442            on analog tuners for PLL to properly work
443          */
444         if (tune_now) {
445                 if (V4L2_TUNER_RADIO == t->mode)
446                         set_radio_freq(c, t->radio_freq);
447                 else
448                         set_tv_freq(c, t->tv_freq);
449         }
450
451         tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
452                   c->adapter->name, c->driver->driver.name, c->addr << 1, type,
453                   t->mode_mask);
454         return;
455
456 attach_failed:
457         tuner_dbg("Tuner attach for type = %d failed.\n", t->type);
458         t->type = TUNER_ABSENT;
459
460         return;
461 }
462
463 /**
464  * tuner_s_type_addr - Sets the tuner type for a device
465  *
466  * @sd:         subdev descriptor
467  * @tun_setup:  type to be associated to a given tuner i2c address
468  *
469  * This function applys the tuner config to tuner specified
470  * by tun_setup structure.
471  * If tuner I2C address is UNSET, then it will only set the device
472  * if the tuner supports the mode specified in the call.
473  * If the address is specified, the change will be applied only if
474  * tuner I2C address matches.
475  * The call can change the tuner number and the tuner mode.
476  */
477 static int tuner_s_type_addr(struct v4l2_subdev *sd,
478                              struct tuner_setup *tun_setup)
479 {
480         struct tuner *t = to_tuner(sd);
481         struct i2c_client *c = v4l2_get_subdevdata(sd);
482
483         tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
484                         tun_setup->type,
485                         tun_setup->addr,
486                         tun_setup->mode_mask,
487                         tun_setup->config);
488
489         if ((t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
490             (t->mode_mask & tun_setup->mode_mask))) ||
491             (tun_setup->addr == c->addr)) {
492                 set_type(c, tun_setup->type, tun_setup->mode_mask,
493                          tun_setup->config, tun_setup->tuner_callback);
494         } else
495                 tuner_dbg("set addr discarded for type %i, mask %x. "
496                           "Asked to change tuner at addr 0x%02x, with mask %x\n",
497                           t->type, t->mode_mask,
498                           tun_setup->addr, tun_setup->mode_mask);
499
500         return 0;
501 }
502
503 /**
504  * tuner_s_config - Sets tuner configuration
505  *
506  * @sd:         subdev descriptor
507  * @cfg:        tuner configuration
508  *
509  * Calls tuner set_config() private function to set some tuner-internal
510  * parameters
511  */
512 static int tuner_s_config(struct v4l2_subdev *sd,
513                           const struct v4l2_priv_tun_config *cfg)
514 {
515         struct tuner *t = to_tuner(sd);
516         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
517
518         if (t->type != cfg->tuner)
519                 return 0;
520
521         if (analog_ops->set_config) {
522                 analog_ops->set_config(&t->fe, cfg->priv);
523                 return 0;
524         }
525
526         tuner_dbg("Tuner frontend module has no way to set config\n");
527         return 0;
528 }
529
530 /**
531  * tuner_lookup - Seek for tuner adapters
532  *
533  * @adap:       i2c_adapter struct
534  * @radio:      pointer to be filled if the adapter is radio
535  * @tv:         pointer to be filled if the adapter is TV
536  *
537  * Search for existing radio and/or TV tuners on the given I2C adapter,
538  * discarding demod-only adapters (tda9887).
539  *
540  * Note that when this function is called from tuner_probe you can be
541  * certain no other devices will be added/deleted at the same time, I2C
542  * core protects against that.
543  */
544 static void tuner_lookup(struct i2c_adapter *adap,
545                 struct tuner **radio, struct tuner **tv)
546 {
547         struct tuner *pos;
548
549         *radio = NULL;
550         *tv = NULL;
551
552         list_for_each_entry(pos, &tuner_list, list) {
553                 int mode_mask;
554
555                 if (pos->i2c->adapter != adap ||
556                     strcmp(pos->i2c->driver->driver.name, "tuner"))
557                         continue;
558
559                 mode_mask = pos->mode_mask;
560                 if (*radio == NULL && mode_mask == T_RADIO)
561                         *radio = pos;
562                 /* Note: currently TDA9887 is the only demod-only
563                    device. If other devices appear then we need to
564                    make this test more general. */
565                 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
566                          (pos->mode_mask & T_ANALOG_TV))
567                         *tv = pos;
568         }
569 }
570
571 /**
572  *tuner_probe - Probes the existing tuners on an I2C bus
573  *
574  * @client:     i2c_client descriptor
575  * @id:         not used
576  *
577  * This routine probes for tuners at the expected I2C addresses. On most
578  * cases, if a device answers to a given I2C address, it assumes that the
579  * device is a tuner. On a few cases, however, an additional logic is needed
580  * to double check if the device is really a tuner, or to identify the tuner
581  * type, like on tea5767/5761 devices.
582  *
583  * During client attach, set_type is called by adapter's attach_inform callback.
584  * set_type must then be completed by tuner_probe.
585  */
586 static int tuner_probe(struct i2c_client *client,
587                        const struct i2c_device_id *id)
588 {
589         struct tuner *t;
590         struct tuner *radio;
591         struct tuner *tv;
592
593         t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
594         if (NULL == t)
595                 return -ENOMEM;
596         v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
597         t->i2c = client;
598         t->name = "(tuner unset)";
599         t->type = UNSET;
600         t->audmode = V4L2_TUNER_MODE_STEREO;
601         t->standby = 1;
602         t->radio_freq = 87.5 * 16000;   /* Initial freq range */
603         t->tv_freq = 400 * 16; /* Sets freq to VHF High - needed for some PLL's to properly start */
604
605         if (show_i2c) {
606                 unsigned char buffer[16];
607                 int i, rc;
608
609                 memset(buffer, 0, sizeof(buffer));
610                 rc = i2c_master_recv(client, buffer, sizeof(buffer));
611                 tuner_info("I2C RECV = ");
612                 for (i = 0; i < rc; i++)
613                         printk(KERN_CONT "%02x ", buffer[i]);
614                 printk("\n");
615         }
616
617         /* autodetection code based on the i2c addr */
618         if (!no_autodetect) {
619                 switch (client->addr) {
620                 case 0x10:
621                         if (tuner_symbol_probe(tea5761_autodetection,
622                                                t->i2c->adapter,
623                                                t->i2c->addr) >= 0) {
624                                 t->type = TUNER_TEA5761;
625                                 t->mode_mask = T_RADIO;
626                                 tuner_lookup(t->i2c->adapter, &radio, &tv);
627                                 if (tv)
628                                         tv->mode_mask &= ~T_RADIO;
629
630                                 goto register_client;
631                         }
632                         kfree(t);
633                         return -ENODEV;
634                 case 0x42:
635                 case 0x43:
636                 case 0x4a:
637                 case 0x4b:
638                         /* If chip is not tda8290, don't register.
639                            since it can be tda9887*/
640                         if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
641                                                t->i2c->addr) >= 0) {
642                                 tuner_dbg("tda829x detected\n");
643                         } else {
644                                 /* Default is being tda9887 */
645                                 t->type = TUNER_TDA9887;
646                                 t->mode_mask = T_RADIO | T_ANALOG_TV;
647                                 goto register_client;
648                         }
649                         break;
650                 case 0x60:
651                         if (tuner_symbol_probe(tea5767_autodetection,
652                                                t->i2c->adapter, t->i2c->addr)
653                                         >= 0) {
654                                 t->type = TUNER_TEA5767;
655                                 t->mode_mask = T_RADIO;
656                                 /* Sets freq to FM range */
657                                 tuner_lookup(t->i2c->adapter, &radio, &tv);
658                                 if (tv)
659                                         tv->mode_mask &= ~T_RADIO;
660
661                                 goto register_client;
662                         }
663                         break;
664                 }
665         }
666
667         /* Initializes only the first TV tuner on this adapter. Why only the
668            first? Because there are some devices (notably the ones with TI
669            tuners) that have more than one i2c address for the *same* device.
670            Experience shows that, except for just one case, the first
671            address is the right one. The exception is a Russian tuner
672            (ACORP_Y878F). So, the desired behavior is just to enable the
673            first found TV tuner. */
674         tuner_lookup(t->i2c->adapter, &radio, &tv);
675         if (tv == NULL) {
676                 t->mode_mask = T_ANALOG_TV;
677                 if (radio == NULL)
678                         t->mode_mask |= T_RADIO;
679                 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
680         }
681
682         /* Should be just before return */
683 register_client:
684         /* Sets a default mode */
685         if (t->mode_mask & T_ANALOG_TV)
686                 t->mode = V4L2_TUNER_ANALOG_TV;
687         else
688                 t->mode = V4L2_TUNER_RADIO;
689         set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
690         list_add_tail(&t->list, &tuner_list);
691
692         tuner_info("Tuner %d found with type(s)%s%s.\n",
693                    t->type,
694                    t->mode_mask & T_RADIO ? " Radio" : "",
695                    t->mode_mask & T_ANALOG_TV ? " TV" : "");
696         return 0;
697 }
698
699 /**
700  * tuner_remove - detaches a tuner
701  *
702  * @client:     i2c_client descriptor
703  */
704
705 static int tuner_remove(struct i2c_client *client)
706 {
707         struct tuner *t = to_tuner(i2c_get_clientdata(client));
708
709         v4l2_device_unregister_subdev(&t->sd);
710         tuner_detach(&t->fe);
711         t->fe.analog_demod_priv = NULL;
712
713         list_del(&t->list);
714         kfree(t);
715         return 0;
716 }
717
718 /*
719  * Functions to switch between Radio and TV
720  *
721  * A few cards have a separate I2C tuner for radio. Those routines
722  * take care of switching between TV/Radio mode, filtering only the
723  * commands that apply to the Radio or TV tuner.
724  */
725
726 /**
727  * check_mode - Verify if tuner supports the requested mode
728  * @t: a pointer to the module's internal struct_tuner
729  *
730  * This function checks if the tuner is capable of tuning analog TV,
731  * digital TV or radio, depending on what the caller wants. If the
732  * tuner can't support that mode, it returns -EINVAL. Otherwise, it
733  * returns 0.
734  * This function is needed for boards that have a separate tuner for
735  * radio (like devices with tea5767).
736  * NOTE: mt20xx uses V4L2_TUNER_DIGITAL_TV and calls set_tv_freq to
737  *       select a TV frequency. So, t_mode = T_ANALOG_TV could actually
738  *       be used to represent a Digital TV too.
739  */
740 static inline int check_mode(struct tuner *t, enum v4l2_tuner_type mode)
741 {
742         int t_mode;
743         if (mode == V4L2_TUNER_RADIO)
744                 t_mode = T_RADIO;
745         else
746                 t_mode = T_ANALOG_TV;
747
748         if ((t_mode & t->mode_mask) == 0)
749                 return -EINVAL;
750
751         return 0;
752 }
753
754 /**
755  * set_mode - Switch tuner to other mode.
756  * @t:          a pointer to the module's internal struct_tuner
757  * @mode:       enum v4l2_type (radio or TV)
758  *
759  * If tuner doesn't support the needed mode (radio or TV), prints a
760  * debug message and returns -EINVAL, changing its state to standby.
761  * Otherwise, changes the mode and returns 0.
762  */
763 static int set_mode(struct tuner *t, enum v4l2_tuner_type mode)
764 {
765         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
766
767         if (mode != t->mode) {
768                 if (check_mode(t, mode) == -EINVAL) {
769                         tuner_dbg("Tuner doesn't support mode %d. "
770                                   "Putting tuner to sleep\n", mode);
771                         t->standby = true;
772                         if (analog_ops->standby)
773                                 analog_ops->standby(&t->fe);
774                         return -EINVAL;
775                 }
776                 t->mode = mode;
777                 tuner_dbg("Changing to mode %d\n", mode);
778         }
779         return 0;
780 }
781
782 /**
783  * set_freq - Set the tuner to the desired frequency.
784  * @t:          a pointer to the module's internal struct_tuner
785  * @freq:       frequency to set (0 means to use the current frequency)
786  */
787 static void set_freq(struct tuner *t, unsigned int freq)
788 {
789         struct i2c_client *client = v4l2_get_subdevdata(&t->sd);
790
791         if (t->mode == V4L2_TUNER_RADIO) {
792                 if (!freq)
793                         freq = t->radio_freq;
794                 set_radio_freq(client, freq);
795         } else {
796                 if (!freq)
797                         freq = t->tv_freq;
798                 set_tv_freq(client, freq);
799         }
800 }
801
802 /*
803  * Functions that are specific for TV mode
804  */
805
806 /**
807  * set_tv_freq - Set tuner frequency,  freq in Units of 62.5 kHz = 1/16MHz
808  *
809  * @c:  i2c_client descriptor
810  * @freq: frequency
811  */
812 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
813 {
814         struct tuner *t = to_tuner(i2c_get_clientdata(c));
815         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
816
817         struct analog_parameters params = {
818                 .mode      = t->mode,
819                 .audmode   = t->audmode,
820                 .std       = t->std
821         };
822
823         if (t->type == UNSET) {
824                 tuner_warn("tuner type not set\n");
825                 return;
826         }
827         if (NULL == analog_ops->set_params) {
828                 tuner_warn("Tuner has no way to set tv freq\n");
829                 return;
830         }
831         if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
832                 tuner_dbg("TV freq (%d.%02d) out of range (%d-%d)\n",
833                            freq / 16, freq % 16 * 100 / 16, tv_range[0],
834                            tv_range[1]);
835                 /* V4L2 spec: if the freq is not possible then the closest
836                    possible value should be selected */
837                 if (freq < tv_range[0] * 16)
838                         freq = tv_range[0] * 16;
839                 else
840                         freq = tv_range[1] * 16;
841         }
842         params.frequency = freq;
843         tuner_dbg("tv freq set to %d.%02d\n",
844                         freq / 16, freq % 16 * 100 / 16);
845         t->tv_freq = freq;
846         t->standby = false;
847
848         analog_ops->set_params(&t->fe, &params);
849 }
850
851 /**
852  * tuner_fixup_std - force a given video standard variant
853  *
854  * @t: tuner internal struct
855  * @std:        TV standard
856  *
857  * A few devices or drivers have problem to detect some standard variations.
858  * On other operational systems, the drivers generally have a per-country
859  * code, and some logic to apply per-country hacks. V4L2 API doesn't provide
860  * such hacks. Instead, it relies on a proper video standard selection from
861  * the userspace application. However, as some apps are buggy, not allowing
862  * to distinguish all video standard variations, a modprobe parameter can
863  * be used to force a video standard match.
864  */
865 static v4l2_std_id tuner_fixup_std(struct tuner *t, v4l2_std_id std)
866 {
867         if (pal[0] != '-' && (std & V4L2_STD_PAL) == V4L2_STD_PAL) {
868                 switch (pal[0]) {
869                 case '6':
870                         return V4L2_STD_PAL_60;
871                 case 'b':
872                 case 'B':
873                 case 'g':
874                 case 'G':
875                         return V4L2_STD_PAL_BG;
876                 case 'i':
877                 case 'I':
878                         return V4L2_STD_PAL_I;
879                 case 'd':
880                 case 'D':
881                 case 'k':
882                 case 'K':
883                         return V4L2_STD_PAL_DK;
884                 case 'M':
885                 case 'm':
886                         return V4L2_STD_PAL_M;
887                 case 'N':
888                 case 'n':
889                         if (pal[1] == 'c' || pal[1] == 'C')
890                                 return V4L2_STD_PAL_Nc;
891                         return V4L2_STD_PAL_N;
892                 default:
893                         tuner_warn("pal= argument not recognised\n");
894                         break;
895                 }
896         }
897         if (secam[0] != '-' && (std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
898                 switch (secam[0]) {
899                 case 'b':
900                 case 'B':
901                 case 'g':
902                 case 'G':
903                 case 'h':
904                 case 'H':
905                         return V4L2_STD_SECAM_B |
906                                V4L2_STD_SECAM_G |
907                                V4L2_STD_SECAM_H;
908                 case 'd':
909                 case 'D':
910                 case 'k':
911                 case 'K':
912                         return V4L2_STD_SECAM_DK;
913                 case 'l':
914                 case 'L':
915                         if ((secam[1] == 'C') || (secam[1] == 'c'))
916                                 return V4L2_STD_SECAM_LC;
917                         return V4L2_STD_SECAM_L;
918                 default:
919                         tuner_warn("secam= argument not recognised\n");
920                         break;
921                 }
922         }
923
924         if (ntsc[0] != '-' && (std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
925                 switch (ntsc[0]) {
926                 case 'm':
927                 case 'M':
928                         return V4L2_STD_NTSC_M;
929                 case 'j':
930                 case 'J':
931                         return V4L2_STD_NTSC_M_JP;
932                 case 'k':
933                 case 'K':
934                         return V4L2_STD_NTSC_M_KR;
935                 default:
936                         tuner_info("ntsc= argument not recognised\n");
937                         break;
938                 }
939         }
940         return std;
941 }
942
943 /*
944  * Functions that are specific for Radio mode
945  */
946
947 /**
948  * set_radio_freq - Set tuner frequency,  freq in Units of 62.5 Hz  = 1/16kHz
949  *
950  * @c:  i2c_client descriptor
951  * @freq: frequency
952  */
953 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
954 {
955         struct tuner *t = to_tuner(i2c_get_clientdata(c));
956         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
957
958         struct analog_parameters params = {
959                 .mode      = t->mode,
960                 .audmode   = t->audmode,
961                 .std       = t->std
962         };
963
964         if (t->type == UNSET) {
965                 tuner_warn("tuner type not set\n");
966                 return;
967         }
968         if (NULL == analog_ops->set_params) {
969                 tuner_warn("tuner has no way to set radio frequency\n");
970                 return;
971         }
972         if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
973                 tuner_dbg("radio freq (%d.%02d) out of range (%d-%d)\n",
974                            freq / 16000, freq % 16000 * 100 / 16000,
975                            radio_range[0], radio_range[1]);
976                 /* V4L2 spec: if the freq is not possible then the closest
977                    possible value should be selected */
978                 if (freq < radio_range[0] * 16000)
979                         freq = radio_range[0] * 16000;
980                 else
981                         freq = radio_range[1] * 16000;
982         }
983         params.frequency = freq;
984         tuner_dbg("radio freq set to %d.%02d\n",
985                         freq / 16000, freq % 16000 * 100 / 16000);
986         t->radio_freq = freq;
987         t->standby = false;
988
989         analog_ops->set_params(&t->fe, &params);
990 }
991
992 /*
993  * Debug function for reporting tuner status to userspace
994  */
995
996 /**
997  * tuner_status - Dumps the current tuner status at dmesg
998  * @fe: pointer to struct dvb_frontend
999  *
1000  * This callback is used only for driver debug purposes, answering to
1001  * VIDIOC_LOG_STATUS. No changes should happen on this call.
1002  */
1003 static void tuner_status(struct dvb_frontend *fe)
1004 {
1005         struct tuner *t = fe->analog_demod_priv;
1006         unsigned long freq, freq_fraction;
1007         struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
1008         struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
1009         const char *p;
1010
1011         switch (t->mode) {
1012         case V4L2_TUNER_RADIO:
1013                 p = "radio";
1014                 break;
1015         case V4L2_TUNER_DIGITAL_TV: /* Used by mt20xx */
1016                 p = "digital TV";
1017                 break;
1018         case V4L2_TUNER_ANALOG_TV:
1019         default:
1020                 p = "analog TV";
1021                 break;
1022         }
1023         if (t->mode == V4L2_TUNER_RADIO) {
1024                 freq = t->radio_freq / 16000;
1025                 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
1026         } else {
1027                 freq = t->tv_freq / 16;
1028                 freq_fraction = (t->tv_freq % 16) * 100 / 16;
1029         }
1030         tuner_info("Tuner mode:      %s%s\n", p,
1031                    t->standby ? " on standby mode" : "");
1032         tuner_info("Frequency:       %lu.%02lu MHz\n", freq, freq_fraction);
1033         tuner_info("Standard:        0x%08lx\n", (unsigned long)t->std);
1034         if (t->mode != V4L2_TUNER_RADIO)
1035                 return;
1036         if (fe_tuner_ops->get_status) {
1037                 u32 tuner_status;
1038
1039                 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1040                 if (tuner_status & TUNER_STATUS_LOCKED)
1041                         tuner_info("Tuner is locked.\n");
1042                 if (tuner_status & TUNER_STATUS_STEREO)
1043                         tuner_info("Stereo:          yes\n");
1044         }
1045         if (analog_ops->has_signal)
1046                 tuner_info("Signal strength: %d\n",
1047                            analog_ops->has_signal(fe));
1048 }
1049
1050 /*
1051  * Function to splicitly change mode to radio. Probably not needed anymore
1052  */
1053
1054 static int tuner_s_radio(struct v4l2_subdev *sd)
1055 {
1056         struct tuner *t = to_tuner(sd);
1057
1058         if (set_mode(t, V4L2_TUNER_RADIO) == 0)
1059                 set_freq(t, 0);
1060         return 0;
1061 }
1062
1063 /*
1064  * Tuner callbacks to handle userspace ioctl's
1065  */
1066
1067 /**
1068  * tuner_s_power - controls the power state of the tuner
1069  * @sd: pointer to struct v4l2_subdev
1070  * @on: a zero value puts the tuner to sleep, non-zero wakes it up
1071  */
1072 static int tuner_s_power(struct v4l2_subdev *sd, int on)
1073 {
1074         struct tuner *t = to_tuner(sd);
1075         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1076
1077         if (on) {
1078                 if (t->standby && set_mode(t, t->mode) == 0) {
1079                         tuner_dbg("Waking up tuner\n");
1080                         set_freq(t, 0);
1081                 }
1082                 return 0;
1083         }
1084
1085         tuner_dbg("Putting tuner to sleep\n");
1086         t->standby = true;
1087         if (analog_ops->standby)
1088                 analog_ops->standby(&t->fe);
1089         return 0;
1090 }
1091
1092 static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1093 {
1094         struct tuner *t = to_tuner(sd);
1095
1096         if (set_mode(t, V4L2_TUNER_ANALOG_TV))
1097                 return 0;
1098
1099         t->std = tuner_fixup_std(t, std);
1100         if (t->std != std)
1101                 tuner_dbg("Fixup standard %llx to %llx\n", std, t->std);
1102         set_freq(t, 0);
1103         return 0;
1104 }
1105
1106 static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1107 {
1108         struct tuner *t = to_tuner(sd);
1109
1110         if (set_mode(t, f->type) == 0)
1111                 set_freq(t, f->frequency);
1112         return 0;
1113 }
1114
1115 /**
1116  * tuner_g_frequency - Get the tuned frequency for the tuner
1117  * @sd: pointer to struct v4l2_subdev
1118  * @f: pointer to struct v4l2_frequency
1119  *
1120  * At return, the structure f will be filled with tuner frequency
1121  * if the tuner matches the f->type.
1122  * Note: f->type should be initialized before calling it.
1123  * This is done by either video_ioctl2 or by the bridge driver.
1124  */
1125 static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1126 {
1127         struct tuner *t = to_tuner(sd);
1128         struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1129
1130         if (check_mode(t, f->type) == -EINVAL)
1131                 return 0;
1132         if (f->type == t->mode && fe_tuner_ops->get_frequency && !t->standby) {
1133                 u32 abs_freq;
1134
1135                 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
1136                 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
1137                         DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
1138                         DIV_ROUND_CLOSEST(abs_freq, 62500);
1139         } else {
1140                 f->frequency = (V4L2_TUNER_RADIO == f->type) ?
1141                         t->radio_freq : t->tv_freq;
1142         }
1143         return 0;
1144 }
1145
1146 /**
1147  * tuner_g_tuner - Fill in tuner information
1148  * @sd: pointer to struct v4l2_subdev
1149  * @vt: pointer to struct v4l2_tuner
1150  *
1151  * At return, the structure vt will be filled with tuner information
1152  * if the tuner matches vt->type.
1153  * Note: vt->type should be initialized before calling it.
1154  * This is done by either video_ioctl2 or by the bridge driver.
1155  */
1156 static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1157 {
1158         struct tuner *t = to_tuner(sd);
1159         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1160         struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1161
1162         if (check_mode(t, vt->type) == -EINVAL)
1163                 return 0;
1164         if (vt->type == t->mode && analog_ops->get_afc)
1165                 vt->afc = analog_ops->get_afc(&t->fe);
1166         if (t->mode != V4L2_TUNER_RADIO) {
1167                 vt->capability |= V4L2_TUNER_CAP_NORM;
1168                 vt->rangelow = tv_range[0] * 16;
1169                 vt->rangehigh = tv_range[1] * 16;
1170                 return 0;
1171         }
1172
1173         /* radio mode */
1174         if (vt->type == t->mode) {
1175                 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1176                 if (fe_tuner_ops->get_status) {
1177                         u32 tuner_status;
1178
1179                         fe_tuner_ops->get_status(&t->fe, &tuner_status);
1180                         vt->rxsubchans =
1181                                 (tuner_status & TUNER_STATUS_STEREO) ?
1182                                 V4L2_TUNER_SUB_STEREO :
1183                                 V4L2_TUNER_SUB_MONO;
1184                 }
1185                 if (analog_ops->has_signal)
1186                         vt->signal = analog_ops->has_signal(&t->fe);
1187                 vt->audmode = t->audmode;
1188         }
1189         vt->capability |= V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1190         vt->rangelow = radio_range[0] * 16000;
1191         vt->rangehigh = radio_range[1] * 16000;
1192
1193         return 0;
1194 }
1195
1196 /**
1197  * tuner_s_tuner - Set the tuner's audio mode
1198  * @sd: pointer to struct v4l2_subdev
1199  * @vt: pointer to struct v4l2_tuner
1200  *
1201  * Sets the audio mode if the tuner matches vt->type.
1202  * Note: vt->type should be initialized before calling it.
1203  * This is done by either video_ioctl2 or by the bridge driver.
1204  */
1205 static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1206 {
1207         struct tuner *t = to_tuner(sd);
1208
1209         if (set_mode(t, vt->type))
1210                 return 0;
1211
1212         if (t->mode == V4L2_TUNER_RADIO)
1213                 t->audmode = vt->audmode;
1214         set_freq(t, 0);
1215
1216         return 0;
1217 }
1218
1219 static int tuner_log_status(struct v4l2_subdev *sd)
1220 {
1221         struct tuner *t = to_tuner(sd);
1222         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1223
1224         if (analog_ops->tuner_status)
1225                 analog_ops->tuner_status(&t->fe);
1226         return 0;
1227 }
1228
1229 static int tuner_suspend(struct i2c_client *c, pm_message_t state)
1230 {
1231         struct tuner *t = to_tuner(i2c_get_clientdata(c));
1232         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1233
1234         tuner_dbg("suspend\n");
1235
1236         if (!t->standby && analog_ops->standby)
1237                 analog_ops->standby(&t->fe);
1238
1239         return 0;
1240 }
1241
1242 static int tuner_resume(struct i2c_client *c)
1243 {
1244         struct tuner *t = to_tuner(i2c_get_clientdata(c));
1245
1246         tuner_dbg("resume\n");
1247
1248         if (!t->standby)
1249                 if (set_mode(t, t->mode) == 0)
1250                         set_freq(t, 0);
1251
1252         return 0;
1253 }
1254
1255 static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
1256 {
1257         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1258
1259         /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
1260            to handle it here.
1261            There must be a better way of doing this... */
1262         switch (cmd) {
1263         case TUNER_SET_CONFIG:
1264                 return tuner_s_config(sd, arg);
1265         }
1266         return -ENOIOCTLCMD;
1267 }
1268
1269 /*
1270  * Callback structs
1271  */
1272
1273 static const struct v4l2_subdev_core_ops tuner_core_ops = {
1274         .log_status = tuner_log_status,
1275         .s_std = tuner_s_std,
1276         .s_power = tuner_s_power,
1277 };
1278
1279 static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
1280         .s_radio = tuner_s_radio,
1281         .g_tuner = tuner_g_tuner,
1282         .s_tuner = tuner_s_tuner,
1283         .s_frequency = tuner_s_frequency,
1284         .g_frequency = tuner_g_frequency,
1285         .s_type_addr = tuner_s_type_addr,
1286         .s_config = tuner_s_config,
1287 };
1288
1289 static const struct v4l2_subdev_ops tuner_ops = {
1290         .core = &tuner_core_ops,
1291         .tuner = &tuner_tuner_ops,
1292 };
1293
1294 /*
1295  * I2C structs and module init functions
1296  */
1297
1298 static const struct i2c_device_id tuner_id[] = {
1299         { "tuner", }, /* autodetect */
1300         { }
1301 };
1302 MODULE_DEVICE_TABLE(i2c, tuner_id);
1303
1304 static struct i2c_driver tuner_driver = {
1305         .driver = {
1306                 .owner  = THIS_MODULE,
1307                 .name   = "tuner",
1308         },
1309         .probe          = tuner_probe,
1310         .remove         = tuner_remove,
1311         .command        = tuner_command,
1312         .suspend        = tuner_suspend,
1313         .resume         = tuner_resume,
1314         .id_table       = tuner_id,
1315 };
1316
1317 static __init int init_tuner(void)
1318 {
1319         return i2c_add_driver(&tuner_driver);
1320 }
1321
1322 static __exit void exit_tuner(void)
1323 {
1324         i2c_del_driver(&tuner_driver);
1325 }
1326
1327 module_init(init_tuner);
1328 module_exit(exit_tuner);
1329
1330 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
1331 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1332 MODULE_LICENSE("GPL");