Merge tag 'v3.5-rc7' into late/soc
[firefly-linux-kernel-4.4.55.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 #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_XC5000C:
384         {
385                 struct xc5000_config xc5000c_cfg = {
386                         .i2c_address = t->i2c->addr,
387                         /* if_khz will be set at dvb_attach() */
388                         .if_khz   = 0,
389                         .chip_id  = XC5000C,
390                 };
391
392                 if (!dvb_attach(xc5000_attach,
393                                 &t->fe, t->i2c->adapter, &xc5000c_cfg))
394                         goto attach_failed;
395                 tune_now = 0;
396                 break;
397         }
398         case TUNER_NXP_TDA18271:
399         {
400                 struct tda18271_config cfg = {
401                         .config = t->config,
402                         .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
403                 };
404
405                 if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr,
406                                 t->i2c->adapter, &cfg))
407                         goto attach_failed;
408                 tune_now = 0;
409                 break;
410         }
411         case TUNER_XC4000:
412         {
413                 struct xc4000_config xc4000_cfg = {
414                         .i2c_address      = t->i2c->addr,
415                         /* FIXME: the correct parameters will be set */
416                         /* only when the digital dvb_attach() occurs */
417                         .default_pm       = 0,
418                         .dvb_amplitude    = 0,
419                         .set_smoothedcvbs = 0,
420                         .if_khz           = 0
421                 };
422                 if (!dvb_attach(xc4000_attach,
423                                 &t->fe, t->i2c->adapter, &xc4000_cfg))
424                         goto attach_failed;
425                 tune_now = 0;
426                 break;
427         }
428         default:
429                 if (!dvb_attach(simple_tuner_attach, &t->fe,
430                                 t->i2c->adapter, t->i2c->addr, t->type))
431                         goto attach_failed;
432
433                 break;
434         }
435
436         if ((NULL == analog_ops->set_params) &&
437             (fe_tuner_ops->set_analog_params)) {
438
439                 t->name = fe_tuner_ops->info.name;
440
441                 t->fe.analog_demod_priv = t;
442                 memcpy(analog_ops, &tuner_analog_ops,
443                        sizeof(struct analog_demod_ops));
444
445         } else {
446                 t->name = analog_ops->info.name;
447         }
448
449         tuner_dbg("type set to %s\n", t->name);
450
451         t->mode_mask = new_mode_mask;
452
453         /* Some tuners require more initialization setup before use,
454            such as firmware download or device calibration.
455            trying to set a frequency here will just fail
456            FIXME: better to move set_freq to the tuner code. This is needed
457            on analog tuners for PLL to properly work
458          */
459         if (tune_now) {
460                 if (V4L2_TUNER_RADIO == t->mode)
461                         set_radio_freq(c, t->radio_freq);
462                 else
463                         set_tv_freq(c, t->tv_freq);
464         }
465
466         tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
467                   c->adapter->name, c->driver->driver.name, c->addr << 1, type,
468                   t->mode_mask);
469         return;
470
471 attach_failed:
472         tuner_dbg("Tuner attach for type = %d failed.\n", t->type);
473         t->type = TUNER_ABSENT;
474
475         return;
476 }
477
478 /**
479  * tuner_s_type_addr - Sets the tuner type for a device
480  *
481  * @sd:         subdev descriptor
482  * @tun_setup:  type to be associated to a given tuner i2c address
483  *
484  * This function applys the tuner config to tuner specified
485  * by tun_setup structure.
486  * If tuner I2C address is UNSET, then it will only set the device
487  * if the tuner supports the mode specified in the call.
488  * If the address is specified, the change will be applied only if
489  * tuner I2C address matches.
490  * The call can change the tuner number and the tuner mode.
491  */
492 static int tuner_s_type_addr(struct v4l2_subdev *sd,
493                              struct tuner_setup *tun_setup)
494 {
495         struct tuner *t = to_tuner(sd);
496         struct i2c_client *c = v4l2_get_subdevdata(sd);
497
498         tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
499                         tun_setup->type,
500                         tun_setup->addr,
501                         tun_setup->mode_mask,
502                         tun_setup->config);
503
504         if ((t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
505             (t->mode_mask & tun_setup->mode_mask))) ||
506             (tun_setup->addr == c->addr)) {
507                 set_type(c, tun_setup->type, tun_setup->mode_mask,
508                          tun_setup->config, tun_setup->tuner_callback);
509         } else
510                 tuner_dbg("set addr discarded for type %i, mask %x. "
511                           "Asked to change tuner at addr 0x%02x, with mask %x\n",
512                           t->type, t->mode_mask,
513                           tun_setup->addr, tun_setup->mode_mask);
514
515         return 0;
516 }
517
518 /**
519  * tuner_s_config - Sets tuner configuration
520  *
521  * @sd:         subdev descriptor
522  * @cfg:        tuner configuration
523  *
524  * Calls tuner set_config() private function to set some tuner-internal
525  * parameters
526  */
527 static int tuner_s_config(struct v4l2_subdev *sd,
528                           const struct v4l2_priv_tun_config *cfg)
529 {
530         struct tuner *t = to_tuner(sd);
531         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
532
533         if (t->type != cfg->tuner)
534                 return 0;
535
536         if (analog_ops->set_config) {
537                 analog_ops->set_config(&t->fe, cfg->priv);
538                 return 0;
539         }
540
541         tuner_dbg("Tuner frontend module has no way to set config\n");
542         return 0;
543 }
544
545 /**
546  * tuner_lookup - Seek for tuner adapters
547  *
548  * @adap:       i2c_adapter struct
549  * @radio:      pointer to be filled if the adapter is radio
550  * @tv:         pointer to be filled if the adapter is TV
551  *
552  * Search for existing radio and/or TV tuners on the given I2C adapter,
553  * discarding demod-only adapters (tda9887).
554  *
555  * Note that when this function is called from tuner_probe you can be
556  * certain no other devices will be added/deleted at the same time, I2C
557  * core protects against that.
558  */
559 static void tuner_lookup(struct i2c_adapter *adap,
560                 struct tuner **radio, struct tuner **tv)
561 {
562         struct tuner *pos;
563
564         *radio = NULL;
565         *tv = NULL;
566
567         list_for_each_entry(pos, &tuner_list, list) {
568                 int mode_mask;
569
570                 if (pos->i2c->adapter != adap ||
571                     strcmp(pos->i2c->driver->driver.name, "tuner"))
572                         continue;
573
574                 mode_mask = pos->mode_mask;
575                 if (*radio == NULL && mode_mask == T_RADIO)
576                         *radio = pos;
577                 /* Note: currently TDA9887 is the only demod-only
578                    device. If other devices appear then we need to
579                    make this test more general. */
580                 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
581                          (pos->mode_mask & T_ANALOG_TV))
582                         *tv = pos;
583         }
584 }
585
586 /**
587  *tuner_probe - Probes the existing tuners on an I2C bus
588  *
589  * @client:     i2c_client descriptor
590  * @id:         not used
591  *
592  * This routine probes for tuners at the expected I2C addresses. On most
593  * cases, if a device answers to a given I2C address, it assumes that the
594  * device is a tuner. On a few cases, however, an additional logic is needed
595  * to double check if the device is really a tuner, or to identify the tuner
596  * type, like on tea5767/5761 devices.
597  *
598  * During client attach, set_type is called by adapter's attach_inform callback.
599  * set_type must then be completed by tuner_probe.
600  */
601 static int tuner_probe(struct i2c_client *client,
602                        const struct i2c_device_id *id)
603 {
604         struct tuner *t;
605         struct tuner *radio;
606         struct tuner *tv;
607
608         t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
609         if (NULL == t)
610                 return -ENOMEM;
611         v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
612         t->i2c = client;
613         t->name = "(tuner unset)";
614         t->type = UNSET;
615         t->audmode = V4L2_TUNER_MODE_STEREO;
616         t->standby = 1;
617         t->radio_freq = 87.5 * 16000;   /* Initial freq range */
618         t->tv_freq = 400 * 16; /* Sets freq to VHF High - needed for some PLL's to properly start */
619
620         if (show_i2c) {
621                 unsigned char buffer[16];
622                 int i, rc;
623
624                 memset(buffer, 0, sizeof(buffer));
625                 rc = i2c_master_recv(client, buffer, sizeof(buffer));
626                 tuner_info("I2C RECV = ");
627                 for (i = 0; i < rc; i++)
628                         printk(KERN_CONT "%02x ", buffer[i]);
629                 printk("\n");
630         }
631
632         /* autodetection code based on the i2c addr */
633         if (!no_autodetect) {
634                 switch (client->addr) {
635                 case 0x10:
636                         if (tuner_symbol_probe(tea5761_autodetection,
637                                                t->i2c->adapter,
638                                                t->i2c->addr) >= 0) {
639                                 t->type = TUNER_TEA5761;
640                                 t->mode_mask = T_RADIO;
641                                 tuner_lookup(t->i2c->adapter, &radio, &tv);
642                                 if (tv)
643                                         tv->mode_mask &= ~T_RADIO;
644
645                                 goto register_client;
646                         }
647                         kfree(t);
648                         return -ENODEV;
649                 case 0x42:
650                 case 0x43:
651                 case 0x4a:
652                 case 0x4b:
653                         /* If chip is not tda8290, don't register.
654                            since it can be tda9887*/
655                         if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
656                                                t->i2c->addr) >= 0) {
657                                 tuner_dbg("tda829x detected\n");
658                         } else {
659                                 /* Default is being tda9887 */
660                                 t->type = TUNER_TDA9887;
661                                 t->mode_mask = T_RADIO | T_ANALOG_TV;
662                                 goto register_client;
663                         }
664                         break;
665                 case 0x60:
666                         if (tuner_symbol_probe(tea5767_autodetection,
667                                                t->i2c->adapter, t->i2c->addr)
668                                         >= 0) {
669                                 t->type = TUNER_TEA5767;
670                                 t->mode_mask = T_RADIO;
671                                 /* Sets freq to FM range */
672                                 tuner_lookup(t->i2c->adapter, &radio, &tv);
673                                 if (tv)
674                                         tv->mode_mask &= ~T_RADIO;
675
676                                 goto register_client;
677                         }
678                         break;
679                 }
680         }
681
682         /* Initializes only the first TV tuner on this adapter. Why only the
683            first? Because there are some devices (notably the ones with TI
684            tuners) that have more than one i2c address for the *same* device.
685            Experience shows that, except for just one case, the first
686            address is the right one. The exception is a Russian tuner
687            (ACORP_Y878F). So, the desired behavior is just to enable the
688            first found TV tuner. */
689         tuner_lookup(t->i2c->adapter, &radio, &tv);
690         if (tv == NULL) {
691                 t->mode_mask = T_ANALOG_TV;
692                 if (radio == NULL)
693                         t->mode_mask |= T_RADIO;
694                 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
695         }
696
697         /* Should be just before return */
698 register_client:
699         /* Sets a default mode */
700         if (t->mode_mask & T_ANALOG_TV)
701                 t->mode = V4L2_TUNER_ANALOG_TV;
702         else
703                 t->mode = V4L2_TUNER_RADIO;
704         set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
705         list_add_tail(&t->list, &tuner_list);
706
707         tuner_info("Tuner %d found with type(s)%s%s.\n",
708                    t->type,
709                    t->mode_mask & T_RADIO ? " Radio" : "",
710                    t->mode_mask & T_ANALOG_TV ? " TV" : "");
711         return 0;
712 }
713
714 /**
715  * tuner_remove - detaches a tuner
716  *
717  * @client:     i2c_client descriptor
718  */
719
720 static int tuner_remove(struct i2c_client *client)
721 {
722         struct tuner *t = to_tuner(i2c_get_clientdata(client));
723
724         v4l2_device_unregister_subdev(&t->sd);
725         tuner_detach(&t->fe);
726         t->fe.analog_demod_priv = NULL;
727
728         list_del(&t->list);
729         kfree(t);
730         return 0;
731 }
732
733 /*
734  * Functions to switch between Radio and TV
735  *
736  * A few cards have a separate I2C tuner for radio. Those routines
737  * take care of switching between TV/Radio mode, filtering only the
738  * commands that apply to the Radio or TV tuner.
739  */
740
741 /**
742  * check_mode - Verify if tuner supports the requested mode
743  * @t: a pointer to the module's internal struct_tuner
744  *
745  * This function checks if the tuner is capable of tuning analog TV,
746  * digital TV or radio, depending on what the caller wants. If the
747  * tuner can't support that mode, it returns -EINVAL. Otherwise, it
748  * returns 0.
749  * This function is needed for boards that have a separate tuner for
750  * radio (like devices with tea5767).
751  * NOTE: mt20xx uses V4L2_TUNER_DIGITAL_TV and calls set_tv_freq to
752  *       select a TV frequency. So, t_mode = T_ANALOG_TV could actually
753  *       be used to represent a Digital TV too.
754  */
755 static inline int check_mode(struct tuner *t, enum v4l2_tuner_type mode)
756 {
757         int t_mode;
758         if (mode == V4L2_TUNER_RADIO)
759                 t_mode = T_RADIO;
760         else
761                 t_mode = T_ANALOG_TV;
762
763         if ((t_mode & t->mode_mask) == 0)
764                 return -EINVAL;
765
766         return 0;
767 }
768
769 /**
770  * set_mode - Switch tuner to other mode.
771  * @t:          a pointer to the module's internal struct_tuner
772  * @mode:       enum v4l2_type (radio or TV)
773  *
774  * If tuner doesn't support the needed mode (radio or TV), prints a
775  * debug message and returns -EINVAL, changing its state to standby.
776  * Otherwise, changes the mode and returns 0.
777  */
778 static int set_mode(struct tuner *t, enum v4l2_tuner_type mode)
779 {
780         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
781
782         if (mode != t->mode) {
783                 if (check_mode(t, mode) == -EINVAL) {
784                         tuner_dbg("Tuner doesn't support mode %d. "
785                                   "Putting tuner to sleep\n", mode);
786                         t->standby = true;
787                         if (analog_ops->standby)
788                                 analog_ops->standby(&t->fe);
789                         return -EINVAL;
790                 }
791                 t->mode = mode;
792                 tuner_dbg("Changing to mode %d\n", mode);
793         }
794         return 0;
795 }
796
797 /**
798  * set_freq - Set the tuner to the desired frequency.
799  * @t:          a pointer to the module's internal struct_tuner
800  * @freq:       frequency to set (0 means to use the current frequency)
801  */
802 static void set_freq(struct tuner *t, unsigned int freq)
803 {
804         struct i2c_client *client = v4l2_get_subdevdata(&t->sd);
805
806         if (t->mode == V4L2_TUNER_RADIO) {
807                 if (!freq)
808                         freq = t->radio_freq;
809                 set_radio_freq(client, freq);
810         } else {
811                 if (!freq)
812                         freq = t->tv_freq;
813                 set_tv_freq(client, freq);
814         }
815 }
816
817 /*
818  * Functions that are specific for TV mode
819  */
820
821 /**
822  * set_tv_freq - Set tuner frequency,  freq in Units of 62.5 kHz = 1/16MHz
823  *
824  * @c:  i2c_client descriptor
825  * @freq: frequency
826  */
827 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
828 {
829         struct tuner *t = to_tuner(i2c_get_clientdata(c));
830         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
831
832         struct analog_parameters params = {
833                 .mode      = t->mode,
834                 .audmode   = t->audmode,
835                 .std       = t->std
836         };
837
838         if (t->type == UNSET) {
839                 tuner_warn("tuner type not set\n");
840                 return;
841         }
842         if (NULL == analog_ops->set_params) {
843                 tuner_warn("Tuner has no way to set tv freq\n");
844                 return;
845         }
846         if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
847                 tuner_dbg("TV freq (%d.%02d) out of range (%d-%d)\n",
848                            freq / 16, freq % 16 * 100 / 16, tv_range[0],
849                            tv_range[1]);
850                 /* V4L2 spec: if the freq is not possible then the closest
851                    possible value should be selected */
852                 if (freq < tv_range[0] * 16)
853                         freq = tv_range[0] * 16;
854                 else
855                         freq = tv_range[1] * 16;
856         }
857         params.frequency = freq;
858         tuner_dbg("tv freq set to %d.%02d\n",
859                         freq / 16, freq % 16 * 100 / 16);
860         t->tv_freq = freq;
861         t->standby = false;
862
863         analog_ops->set_params(&t->fe, &params);
864 }
865
866 /**
867  * tuner_fixup_std - force a given video standard variant
868  *
869  * @t: tuner internal struct
870  * @std:        TV standard
871  *
872  * A few devices or drivers have problem to detect some standard variations.
873  * On other operational systems, the drivers generally have a per-country
874  * code, and some logic to apply per-country hacks. V4L2 API doesn't provide
875  * such hacks. Instead, it relies on a proper video standard selection from
876  * the userspace application. However, as some apps are buggy, not allowing
877  * to distinguish all video standard variations, a modprobe parameter can
878  * be used to force a video standard match.
879  */
880 static v4l2_std_id tuner_fixup_std(struct tuner *t, v4l2_std_id std)
881 {
882         if (pal[0] != '-' && (std & V4L2_STD_PAL) == V4L2_STD_PAL) {
883                 switch (pal[0]) {
884                 case '6':
885                         return V4L2_STD_PAL_60;
886                 case 'b':
887                 case 'B':
888                 case 'g':
889                 case 'G':
890                         return V4L2_STD_PAL_BG;
891                 case 'i':
892                 case 'I':
893                         return V4L2_STD_PAL_I;
894                 case 'd':
895                 case 'D':
896                 case 'k':
897                 case 'K':
898                         return V4L2_STD_PAL_DK;
899                 case 'M':
900                 case 'm':
901                         return V4L2_STD_PAL_M;
902                 case 'N':
903                 case 'n':
904                         if (pal[1] == 'c' || pal[1] == 'C')
905                                 return V4L2_STD_PAL_Nc;
906                         return V4L2_STD_PAL_N;
907                 default:
908                         tuner_warn("pal= argument not recognised\n");
909                         break;
910                 }
911         }
912         if (secam[0] != '-' && (std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
913                 switch (secam[0]) {
914                 case 'b':
915                 case 'B':
916                 case 'g':
917                 case 'G':
918                 case 'h':
919                 case 'H':
920                         return V4L2_STD_SECAM_B |
921                                V4L2_STD_SECAM_G |
922                                V4L2_STD_SECAM_H;
923                 case 'd':
924                 case 'D':
925                 case 'k':
926                 case 'K':
927                         return V4L2_STD_SECAM_DK;
928                 case 'l':
929                 case 'L':
930                         if ((secam[1] == 'C') || (secam[1] == 'c'))
931                                 return V4L2_STD_SECAM_LC;
932                         return V4L2_STD_SECAM_L;
933                 default:
934                         tuner_warn("secam= argument not recognised\n");
935                         break;
936                 }
937         }
938
939         if (ntsc[0] != '-' && (std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
940                 switch (ntsc[0]) {
941                 case 'm':
942                 case 'M':
943                         return V4L2_STD_NTSC_M;
944                 case 'j':
945                 case 'J':
946                         return V4L2_STD_NTSC_M_JP;
947                 case 'k':
948                 case 'K':
949                         return V4L2_STD_NTSC_M_KR;
950                 default:
951                         tuner_info("ntsc= argument not recognised\n");
952                         break;
953                 }
954         }
955         return std;
956 }
957
958 /*
959  * Functions that are specific for Radio mode
960  */
961
962 /**
963  * set_radio_freq - Set tuner frequency,  freq in Units of 62.5 Hz  = 1/16kHz
964  *
965  * @c:  i2c_client descriptor
966  * @freq: frequency
967  */
968 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
969 {
970         struct tuner *t = to_tuner(i2c_get_clientdata(c));
971         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
972
973         struct analog_parameters params = {
974                 .mode      = t->mode,
975                 .audmode   = t->audmode,
976                 .std       = t->std
977         };
978
979         if (t->type == UNSET) {
980                 tuner_warn("tuner type not set\n");
981                 return;
982         }
983         if (NULL == analog_ops->set_params) {
984                 tuner_warn("tuner has no way to set radio frequency\n");
985                 return;
986         }
987         if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
988                 tuner_dbg("radio freq (%d.%02d) out of range (%d-%d)\n",
989                            freq / 16000, freq % 16000 * 100 / 16000,
990                            radio_range[0], radio_range[1]);
991                 /* V4L2 spec: if the freq is not possible then the closest
992                    possible value should be selected */
993                 if (freq < radio_range[0] * 16000)
994                         freq = radio_range[0] * 16000;
995                 else
996                         freq = radio_range[1] * 16000;
997         }
998         params.frequency = freq;
999         tuner_dbg("radio freq set to %d.%02d\n",
1000                         freq / 16000, freq % 16000 * 100 / 16000);
1001         t->radio_freq = freq;
1002         t->standby = false;
1003
1004         analog_ops->set_params(&t->fe, &params);
1005 }
1006
1007 /*
1008  * Debug function for reporting tuner status to userspace
1009  */
1010
1011 /**
1012  * tuner_status - Dumps the current tuner status at dmesg
1013  * @fe: pointer to struct dvb_frontend
1014  *
1015  * This callback is used only for driver debug purposes, answering to
1016  * VIDIOC_LOG_STATUS. No changes should happen on this call.
1017  */
1018 static void tuner_status(struct dvb_frontend *fe)
1019 {
1020         struct tuner *t = fe->analog_demod_priv;
1021         unsigned long freq, freq_fraction;
1022         struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
1023         struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
1024         const char *p;
1025
1026         switch (t->mode) {
1027         case V4L2_TUNER_RADIO:
1028                 p = "radio";
1029                 break;
1030         case V4L2_TUNER_DIGITAL_TV: /* Used by mt20xx */
1031                 p = "digital TV";
1032                 break;
1033         case V4L2_TUNER_ANALOG_TV:
1034         default:
1035                 p = "analog TV";
1036                 break;
1037         }
1038         if (t->mode == V4L2_TUNER_RADIO) {
1039                 freq = t->radio_freq / 16000;
1040                 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
1041         } else {
1042                 freq = t->tv_freq / 16;
1043                 freq_fraction = (t->tv_freq % 16) * 100 / 16;
1044         }
1045         tuner_info("Tuner mode:      %s%s\n", p,
1046                    t->standby ? " on standby mode" : "");
1047         tuner_info("Frequency:       %lu.%02lu MHz\n", freq, freq_fraction);
1048         tuner_info("Standard:        0x%08lx\n", (unsigned long)t->std);
1049         if (t->mode != V4L2_TUNER_RADIO)
1050                 return;
1051         if (fe_tuner_ops->get_status) {
1052                 u32 tuner_status;
1053
1054                 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1055                 if (tuner_status & TUNER_STATUS_LOCKED)
1056                         tuner_info("Tuner is locked.\n");
1057                 if (tuner_status & TUNER_STATUS_STEREO)
1058                         tuner_info("Stereo:          yes\n");
1059         }
1060         if (analog_ops->has_signal)
1061                 tuner_info("Signal strength: %d\n",
1062                            analog_ops->has_signal(fe));
1063 }
1064
1065 /*
1066  * Function to splicitly change mode to radio. Probably not needed anymore
1067  */
1068
1069 static int tuner_s_radio(struct v4l2_subdev *sd)
1070 {
1071         struct tuner *t = to_tuner(sd);
1072
1073         if (set_mode(t, V4L2_TUNER_RADIO) == 0)
1074                 set_freq(t, 0);
1075         return 0;
1076 }
1077
1078 /*
1079  * Tuner callbacks to handle userspace ioctl's
1080  */
1081
1082 /**
1083  * tuner_s_power - controls the power state of the tuner
1084  * @sd: pointer to struct v4l2_subdev
1085  * @on: a zero value puts the tuner to sleep, non-zero wakes it up
1086  */
1087 static int tuner_s_power(struct v4l2_subdev *sd, int on)
1088 {
1089         struct tuner *t = to_tuner(sd);
1090         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1091
1092         if (on) {
1093                 if (t->standby && set_mode(t, t->mode) == 0) {
1094                         tuner_dbg("Waking up tuner\n");
1095                         set_freq(t, 0);
1096                 }
1097                 return 0;
1098         }
1099
1100         tuner_dbg("Putting tuner to sleep\n");
1101         t->standby = true;
1102         if (analog_ops->standby)
1103                 analog_ops->standby(&t->fe);
1104         return 0;
1105 }
1106
1107 static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1108 {
1109         struct tuner *t = to_tuner(sd);
1110
1111         if (set_mode(t, V4L2_TUNER_ANALOG_TV))
1112                 return 0;
1113
1114         t->std = tuner_fixup_std(t, std);
1115         if (t->std != std)
1116                 tuner_dbg("Fixup standard %llx to %llx\n", std, t->std);
1117         set_freq(t, 0);
1118         return 0;
1119 }
1120
1121 static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1122 {
1123         struct tuner *t = to_tuner(sd);
1124
1125         if (set_mode(t, f->type) == 0)
1126                 set_freq(t, f->frequency);
1127         return 0;
1128 }
1129
1130 /**
1131  * tuner_g_frequency - Get the tuned frequency for the tuner
1132  * @sd: pointer to struct v4l2_subdev
1133  * @f: pointer to struct v4l2_frequency
1134  *
1135  * At return, the structure f will be filled with tuner frequency
1136  * if the tuner matches the f->type.
1137  * Note: f->type should be initialized before calling it.
1138  * This is done by either video_ioctl2 or by the bridge driver.
1139  */
1140 static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1141 {
1142         struct tuner *t = to_tuner(sd);
1143         struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1144
1145         if (check_mode(t, f->type) == -EINVAL)
1146                 return 0;
1147         if (f->type == t->mode && fe_tuner_ops->get_frequency && !t->standby) {
1148                 u32 abs_freq;
1149
1150                 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
1151                 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
1152                         DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
1153                         DIV_ROUND_CLOSEST(abs_freq, 62500);
1154         } else {
1155                 f->frequency = (V4L2_TUNER_RADIO == f->type) ?
1156                         t->radio_freq : t->tv_freq;
1157         }
1158         return 0;
1159 }
1160
1161 /**
1162  * tuner_g_tuner - Fill in tuner information
1163  * @sd: pointer to struct v4l2_subdev
1164  * @vt: pointer to struct v4l2_tuner
1165  *
1166  * At return, the structure vt will be filled with tuner information
1167  * if the tuner matches vt->type.
1168  * Note: vt->type should be initialized before calling it.
1169  * This is done by either video_ioctl2 or by the bridge driver.
1170  */
1171 static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1172 {
1173         struct tuner *t = to_tuner(sd);
1174         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1175         struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1176
1177         if (check_mode(t, vt->type) == -EINVAL)
1178                 return 0;
1179         if (vt->type == t->mode && analog_ops->get_afc)
1180                 vt->afc = analog_ops->get_afc(&t->fe);
1181         if (vt->type != V4L2_TUNER_RADIO) {
1182                 vt->capability |= V4L2_TUNER_CAP_NORM;
1183                 vt->rangelow = tv_range[0] * 16;
1184                 vt->rangehigh = tv_range[1] * 16;
1185                 return 0;
1186         }
1187
1188         /* radio mode */
1189         if (vt->type == t->mode) {
1190                 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1191                 if (fe_tuner_ops->get_status) {
1192                         u32 tuner_status;
1193
1194                         fe_tuner_ops->get_status(&t->fe, &tuner_status);
1195                         vt->rxsubchans =
1196                                 (tuner_status & TUNER_STATUS_STEREO) ?
1197                                 V4L2_TUNER_SUB_STEREO :
1198                                 V4L2_TUNER_SUB_MONO;
1199                 }
1200                 if (analog_ops->has_signal)
1201                         vt->signal = analog_ops->has_signal(&t->fe);
1202                 vt->audmode = t->audmode;
1203         }
1204         vt->capability |= V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1205         vt->rangelow = radio_range[0] * 16000;
1206         vt->rangehigh = radio_range[1] * 16000;
1207
1208         return 0;
1209 }
1210
1211 /**
1212  * tuner_s_tuner - Set the tuner's audio mode
1213  * @sd: pointer to struct v4l2_subdev
1214  * @vt: pointer to struct v4l2_tuner
1215  *
1216  * Sets the audio mode if the tuner matches vt->type.
1217  * Note: vt->type should be initialized before calling it.
1218  * This is done by either video_ioctl2 or by the bridge driver.
1219  */
1220 static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1221 {
1222         struct tuner *t = to_tuner(sd);
1223
1224         if (set_mode(t, vt->type))
1225                 return 0;
1226
1227         if (t->mode == V4L2_TUNER_RADIO)
1228                 t->audmode = vt->audmode;
1229         set_freq(t, 0);
1230
1231         return 0;
1232 }
1233
1234 static int tuner_log_status(struct v4l2_subdev *sd)
1235 {
1236         struct tuner *t = to_tuner(sd);
1237         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1238
1239         if (analog_ops->tuner_status)
1240                 analog_ops->tuner_status(&t->fe);
1241         return 0;
1242 }
1243
1244 #ifdef CONFIG_PM_SLEEP
1245 static int tuner_suspend(struct device *dev)
1246 {
1247         struct i2c_client *c = to_i2c_client(dev);
1248         struct tuner *t = to_tuner(i2c_get_clientdata(c));
1249         struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1250
1251         tuner_dbg("suspend\n");
1252
1253         if (!t->standby && analog_ops->standby)
1254                 analog_ops->standby(&t->fe);
1255
1256         return 0;
1257 }
1258
1259 static int tuner_resume(struct device *dev)
1260 {
1261         struct i2c_client *c = to_i2c_client(dev);
1262         struct tuner *t = to_tuner(i2c_get_clientdata(c));
1263
1264         tuner_dbg("resume\n");
1265
1266         if (!t->standby)
1267                 if (set_mode(t, t->mode) == 0)
1268                         set_freq(t, 0);
1269
1270         return 0;
1271 }
1272 #endif
1273
1274 static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
1275 {
1276         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1277
1278         /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
1279            to handle it here.
1280            There must be a better way of doing this... */
1281         switch (cmd) {
1282         case TUNER_SET_CONFIG:
1283                 return tuner_s_config(sd, arg);
1284         }
1285         return -ENOIOCTLCMD;
1286 }
1287
1288 /*
1289  * Callback structs
1290  */
1291
1292 static const struct v4l2_subdev_core_ops tuner_core_ops = {
1293         .log_status = tuner_log_status,
1294         .s_std = tuner_s_std,
1295         .s_power = tuner_s_power,
1296 };
1297
1298 static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
1299         .s_radio = tuner_s_radio,
1300         .g_tuner = tuner_g_tuner,
1301         .s_tuner = tuner_s_tuner,
1302         .s_frequency = tuner_s_frequency,
1303         .g_frequency = tuner_g_frequency,
1304         .s_type_addr = tuner_s_type_addr,
1305         .s_config = tuner_s_config,
1306 };
1307
1308 static const struct v4l2_subdev_ops tuner_ops = {
1309         .core = &tuner_core_ops,
1310         .tuner = &tuner_tuner_ops,
1311 };
1312
1313 /*
1314  * I2C structs and module init functions
1315  */
1316
1317 static const struct dev_pm_ops tuner_pm_ops = {
1318         SET_SYSTEM_SLEEP_PM_OPS(tuner_suspend, tuner_resume)
1319 };
1320
1321 static const struct i2c_device_id tuner_id[] = {
1322         { "tuner", }, /* autodetect */
1323         { }
1324 };
1325 MODULE_DEVICE_TABLE(i2c, tuner_id);
1326
1327 static struct i2c_driver tuner_driver = {
1328         .driver = {
1329                 .owner  = THIS_MODULE,
1330                 .name   = "tuner",
1331                 .pm     = &tuner_pm_ops,
1332         },
1333         .probe          = tuner_probe,
1334         .remove         = tuner_remove,
1335         .command        = tuner_command,
1336         .id_table       = tuner_id,
1337 };
1338
1339 module_i2c_driver(tuner_driver);
1340
1341 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
1342 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1343 MODULE_LICENSE("GPL");