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