Avoid beyond bounds copy while caching ACL
[zen-stable.git] / drivers / media / video / tuner-core.c
blob4059ea178c2d95e680e8679319d8aa1c661fa1b1
1 /*
2 * i2c tv tuner chip device driver
3 * core core, i.e. kernel interfaces, registering and so on
5 * Copyright(c) by Ralph Metzler, Gerd Knorr, Gunther Mayer
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
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.
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"
44 #define UNSET (-1U)
46 #define PREFIX (t->i2c->driver->driver.name)
49 * Driver modprobe parameters
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;
57 module_param(addr, int, 0444);
58 module_param(no_autodetect, int, 0444);
59 module_param(show_i2c, int, 0444);
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[] = "-";
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);
77 * Static vars
80 static LIST_HEAD(tuner_list);
81 static const struct v4l2_subdev_ops tuner_ops;
84 * Debug macros
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)
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)
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)
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)
113 * Internal struct used inside the driver
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;
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;
129 enum v4l2_tuner_type mode;
130 unsigned int mode_mask; /* Combination of allowable modes */
132 bool standby; /* Standby mode */
134 unsigned int type; /* chip type id */
135 unsigned int config;
136 const char *name;
140 * Function prototypes
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);
147 * tuner attach/detach logic
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"); \
162 __r; \
165 static void tuner_detach(struct dvb_frontend *fe)
167 if (fe->ops.tuner_ops.release) {
168 fe->ops.tuner_ops.release(fe);
169 symbol_put_addr(fe->ops.tuner_ops.release);
171 if (fe->ops.analog_ops.release) {
172 fe->ops.analog_ops.release(fe);
173 symbol_put_addr(fe->ops.analog_ops.release);
176 #else
177 #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
178 FUNCTION(ARGS); \
181 static void tuner_detach(struct dvb_frontend *fe)
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);
188 #endif
191 static inline struct tuner *to_tuner(struct v4l2_subdev *sd)
193 return container_of(sd, struct tuner, sd);
197 * struct analog_demod_ops callbacks
200 static void fe_set_params(struct dvb_frontend *fe,
201 struct analog_parameters *params)
203 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
204 struct tuner *t = fe->analog_demod_priv;
206 if (NULL == fe_tuner_ops->set_analog_params) {
207 tuner_warn("Tuner frontend module has no way to set freq\n");
208 return;
210 fe_tuner_ops->set_analog_params(fe, params);
213 static void fe_standby(struct dvb_frontend *fe)
215 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
217 if (fe_tuner_ops->sleep)
218 fe_tuner_ops->sleep(fe);
221 static int fe_has_signal(struct dvb_frontend *fe)
223 u16 strength = 0;
225 if (fe->ops.tuner_ops.get_rf_strength)
226 fe->ops.tuner_ops.get_rf_strength(fe, &strength);
228 return strength;
231 static int fe_set_config(struct dvb_frontend *fe, void *priv_cfg)
233 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
234 struct tuner *t = fe->analog_demod_priv;
236 if (fe_tuner_ops->set_config)
237 return fe_tuner_ops->set_config(fe, priv_cfg);
239 tuner_warn("Tuner frontend module has no way to set config\n");
241 return 0;
244 static void tuner_status(struct dvb_frontend *fe);
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
255 * Functions to select between radio and TV and tuner probe/remove functions
259 * set_type - Sets the tuner type for a given device
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
270 * This function applys the tuner config to tuner specified
271 * by tun_setup structure. It contains several per-tuner initialization "magic"
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))
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;
283 if (type == UNSET || type == TUNER_ABSENT) {
284 tuner_dbg("tuner 0x%02x: Tuner type absent\n", c->addr);
285 return;
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;
296 /* discard private data, in case set_type() was previously called */
297 tuner_detach(&t->fe);
298 t->fe.analog_demod_priv = NULL;
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:
308 struct tda829x_config cfg = {
309 .lna_cfg = t->config,
311 if (!dvb_attach(tda829x_attach, &t->fe, t->i2c->adapter,
312 t->i2c->addr, &cfg))
313 goto attach_failed;
314 break;
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:
355 struct xc2028_config cfg = {
356 .i2c_adap = t->i2c->adapter,
357 .i2c_addr = t->i2c->addr,
359 if (!dvb_attach(xc2028_attach, &t->fe, &cfg))
360 goto attach_failed;
361 tune_now = 0;
362 break;
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:
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,
377 if (!dvb_attach(xc5000_attach,
378 &t->fe, t->i2c->adapter, &xc5000_cfg))
379 goto attach_failed;
380 tune_now = 0;
381 break;
383 case TUNER_NXP_TDA18271:
385 struct tda18271_config cfg = {
386 .config = t->config,
387 .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
390 if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr,
391 t->i2c->adapter, &cfg))
392 goto attach_failed;
393 tune_now = 0;
394 break;
396 case TUNER_XC4000:
398 struct xc4000_config xc4000_cfg = {
399 .i2c_address = t->i2c->addr,
400 /* FIXME: the correct parameters will be set */
401 /* only when the digital dvb_attach() occurs */
402 .default_pm = 0,
403 .dvb_amplitude = 0,
404 .set_smoothedcvbs = 0,
405 .if_khz = 0
407 if (!dvb_attach(xc4000_attach,
408 &t->fe, t->i2c->adapter, &xc4000_cfg))
409 goto attach_failed;
410 tune_now = 0;
411 break;
413 default:
414 if (!dvb_attach(simple_tuner_attach, &t->fe,
415 t->i2c->adapter, t->i2c->addr, t->type))
416 goto attach_failed;
418 break;
421 if ((NULL == analog_ops->set_params) &&
422 (fe_tuner_ops->set_analog_params)) {
424 t->name = fe_tuner_ops->info.name;
426 t->fe.analog_demod_priv = t;
427 memcpy(analog_ops, &tuner_analog_ops,
428 sizeof(struct analog_demod_ops));
430 } else {
431 t->name = analog_ops->info.name;
434 tuner_dbg("type set to %s\n", t->name);
436 t->mode_mask = new_mode_mask;
438 /* Some tuners require more initialization setup before use,
439 such as firmware download or device calibration.
440 trying to set a frequency here will just fail
441 FIXME: better to move set_freq to the tuner code. This is needed
442 on analog tuners for PLL to properly work
444 if (tune_now) {
445 if (V4L2_TUNER_RADIO == t->mode)
446 set_radio_freq(c, t->radio_freq);
447 else
448 set_tv_freq(c, t->tv_freq);
451 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
452 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
453 t->mode_mask);
454 return;
456 attach_failed:
457 tuner_dbg("Tuner attach for type = %d failed.\n", t->type);
458 t->type = TUNER_ABSENT;
460 return;
464 * tuner_s_type_addr - Sets the tuner type for a device
466 * @sd: subdev descriptor
467 * @tun_setup: type to be associated to a given tuner i2c address
469 * This function applys the tuner config to tuner specified
470 * by tun_setup structure.
471 * If tuner I2C address is UNSET, then it will only set the device
472 * if the tuner supports the mode specified in the call.
473 * If the address is specified, the change will be applied only if
474 * tuner I2C address matches.
475 * The call can change the tuner number and the tuner mode.
477 static int tuner_s_type_addr(struct v4l2_subdev *sd,
478 struct tuner_setup *tun_setup)
480 struct tuner *t = to_tuner(sd);
481 struct i2c_client *c = v4l2_get_subdevdata(sd);
483 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
484 tun_setup->type,
485 tun_setup->addr,
486 tun_setup->mode_mask,
487 tun_setup->config);
489 if ((t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
490 (t->mode_mask & tun_setup->mode_mask))) ||
491 (tun_setup->addr == c->addr)) {
492 set_type(c, tun_setup->type, tun_setup->mode_mask,
493 tun_setup->config, tun_setup->tuner_callback);
494 } else
495 tuner_dbg("set addr discarded for type %i, mask %x. "
496 "Asked to change tuner at addr 0x%02x, with mask %x\n",
497 t->type, t->mode_mask,
498 tun_setup->addr, tun_setup->mode_mask);
500 return 0;
504 * tuner_s_config - Sets tuner configuration
506 * @sd: subdev descriptor
507 * @cfg: tuner configuration
509 * Calls tuner set_config() private function to set some tuner-internal
510 * parameters
512 static int tuner_s_config(struct v4l2_subdev *sd,
513 const struct v4l2_priv_tun_config *cfg)
515 struct tuner *t = to_tuner(sd);
516 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
518 if (t->type != cfg->tuner)
519 return 0;
521 if (analog_ops->set_config) {
522 analog_ops->set_config(&t->fe, cfg->priv);
523 return 0;
526 tuner_dbg("Tuner frontend module has no way to set config\n");
527 return 0;
531 * tuner_lookup - Seek for tuner adapters
533 * @adap: i2c_adapter struct
534 * @radio: pointer to be filled if the adapter is radio
535 * @tv: pointer to be filled if the adapter is TV
537 * Search for existing radio and/or TV tuners on the given I2C adapter,
538 * discarding demod-only adapters (tda9887).
540 * Note that when this function is called from tuner_probe you can be
541 * certain no other devices will be added/deleted at the same time, I2C
542 * core protects against that.
544 static void tuner_lookup(struct i2c_adapter *adap,
545 struct tuner **radio, struct tuner **tv)
547 struct tuner *pos;
549 *radio = NULL;
550 *tv = NULL;
552 list_for_each_entry(pos, &tuner_list, list) {
553 int mode_mask;
555 if (pos->i2c->adapter != adap ||
556 strcmp(pos->i2c->driver->driver.name, "tuner"))
557 continue;
559 mode_mask = pos->mode_mask;
560 if (*radio == NULL && mode_mask == T_RADIO)
561 *radio = pos;
562 /* Note: currently TDA9887 is the only demod-only
563 device. If other devices appear then we need to
564 make this test more general. */
565 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
566 (pos->mode_mask & T_ANALOG_TV))
567 *tv = pos;
572 *tuner_probe - Probes the existing tuners on an I2C bus
574 * @client: i2c_client descriptor
575 * @id: not used
577 * This routine probes for tuners at the expected I2C addresses. On most
578 * cases, if a device answers to a given I2C address, it assumes that the
579 * device is a tuner. On a few cases, however, an additional logic is needed
580 * to double check if the device is really a tuner, or to identify the tuner
581 * type, like on tea5767/5761 devices.
583 * During client attach, set_type is called by adapter's attach_inform callback.
584 * set_type must then be completed by tuner_probe.
586 static int tuner_probe(struct i2c_client *client,
587 const struct i2c_device_id *id)
589 struct tuner *t;
590 struct tuner *radio;
591 struct tuner *tv;
593 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
594 if (NULL == t)
595 return -ENOMEM;
596 v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
597 t->i2c = client;
598 t->name = "(tuner unset)";
599 t->type = UNSET;
600 t->audmode = V4L2_TUNER_MODE_STEREO;
601 t->standby = 1;
602 t->radio_freq = 87.5 * 16000; /* Initial freq range */
603 t->tv_freq = 400 * 16; /* Sets freq to VHF High - needed for some PLL's to properly start */
605 if (show_i2c) {
606 unsigned char buffer[16];
607 int i, rc;
609 memset(buffer, 0, sizeof(buffer));
610 rc = i2c_master_recv(client, buffer, sizeof(buffer));
611 tuner_info("I2C RECV = ");
612 for (i = 0; i < rc; i++)
613 printk(KERN_CONT "%02x ", buffer[i]);
614 printk("\n");
617 /* autodetection code based on the i2c addr */
618 if (!no_autodetect) {
619 switch (client->addr) {
620 case 0x10:
621 if (tuner_symbol_probe(tea5761_autodetection,
622 t->i2c->adapter,
623 t->i2c->addr) >= 0) {
624 t->type = TUNER_TEA5761;
625 t->mode_mask = T_RADIO;
626 tuner_lookup(t->i2c->adapter, &radio, &tv);
627 if (tv)
628 tv->mode_mask &= ~T_RADIO;
630 goto register_client;
632 kfree(t);
633 return -ENODEV;
634 case 0x42:
635 case 0x43:
636 case 0x4a:
637 case 0x4b:
638 /* If chip is not tda8290, don't register.
639 since it can be tda9887*/
640 if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
641 t->i2c->addr) >= 0) {
642 tuner_dbg("tda829x detected\n");
643 } else {
644 /* Default is being tda9887 */
645 t->type = TUNER_TDA9887;
646 t->mode_mask = T_RADIO | T_ANALOG_TV;
647 goto register_client;
649 break;
650 case 0x60:
651 if (tuner_symbol_probe(tea5767_autodetection,
652 t->i2c->adapter, t->i2c->addr)
653 >= 0) {
654 t->type = TUNER_TEA5767;
655 t->mode_mask = T_RADIO;
656 /* Sets freq to FM range */
657 tuner_lookup(t->i2c->adapter, &radio, &tv);
658 if (tv)
659 tv->mode_mask &= ~T_RADIO;
661 goto register_client;
663 break;
667 /* Initializes only the first TV tuner on this adapter. Why only the
668 first? Because there are some devices (notably the ones with TI
669 tuners) that have more than one i2c address for the *same* device.
670 Experience shows that, except for just one case, the first
671 address is the right one. The exception is a Russian tuner
672 (ACORP_Y878F). So, the desired behavior is just to enable the
673 first found TV tuner. */
674 tuner_lookup(t->i2c->adapter, &radio, &tv);
675 if (tv == NULL) {
676 t->mode_mask = T_ANALOG_TV;
677 if (radio == NULL)
678 t->mode_mask |= T_RADIO;
679 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
682 /* Should be just before return */
683 register_client:
684 /* Sets a default mode */
685 if (t->mode_mask & T_ANALOG_TV)
686 t->mode = V4L2_TUNER_ANALOG_TV;
687 else
688 t->mode = V4L2_TUNER_RADIO;
689 set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
690 list_add_tail(&t->list, &tuner_list);
692 tuner_info("Tuner %d found with type(s)%s%s.\n",
693 t->type,
694 t->mode_mask & T_RADIO ? " Radio" : "",
695 t->mode_mask & T_ANALOG_TV ? " TV" : "");
696 return 0;
700 * tuner_remove - detaches a tuner
702 * @client: i2c_client descriptor
705 static int tuner_remove(struct i2c_client *client)
707 struct tuner *t = to_tuner(i2c_get_clientdata(client));
709 v4l2_device_unregister_subdev(&t->sd);
710 tuner_detach(&t->fe);
711 t->fe.analog_demod_priv = NULL;
713 list_del(&t->list);
714 kfree(t);
715 return 0;
719 * Functions to switch between Radio and TV
721 * A few cards have a separate I2C tuner for radio. Those routines
722 * take care of switching between TV/Radio mode, filtering only the
723 * commands that apply to the Radio or TV tuner.
727 * check_mode - Verify if tuner supports the requested mode
728 * @t: a pointer to the module's internal struct_tuner
730 * This function checks if the tuner is capable of tuning analog TV,
731 * digital TV or radio, depending on what the caller wants. If the
732 * tuner can't support that mode, it returns -EINVAL. Otherwise, it
733 * returns 0.
734 * This function is needed for boards that have a separate tuner for
735 * radio (like devices with tea5767).
736 * NOTE: mt20xx uses V4L2_TUNER_DIGITAL_TV and calls set_tv_freq to
737 * select a TV frequency. So, t_mode = T_ANALOG_TV could actually
738 * be used to represent a Digital TV too.
740 static inline int check_mode(struct tuner *t, enum v4l2_tuner_type mode)
742 int t_mode;
743 if (mode == V4L2_TUNER_RADIO)
744 t_mode = T_RADIO;
745 else
746 t_mode = T_ANALOG_TV;
748 if ((t_mode & t->mode_mask) == 0)
749 return -EINVAL;
751 return 0;
755 * set_mode - Switch tuner to other mode.
756 * @t: a pointer to the module's internal struct_tuner
757 * @mode: enum v4l2_type (radio or TV)
759 * If tuner doesn't support the needed mode (radio or TV), prints a
760 * debug message and returns -EINVAL, changing its state to standby.
761 * Otherwise, changes the mode and returns 0.
763 static int set_mode(struct tuner *t, enum v4l2_tuner_type mode)
765 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
767 if (mode != t->mode) {
768 if (check_mode(t, mode) == -EINVAL) {
769 tuner_dbg("Tuner doesn't support mode %d. "
770 "Putting tuner to sleep\n", mode);
771 t->standby = true;
772 if (analog_ops->standby)
773 analog_ops->standby(&t->fe);
774 return -EINVAL;
776 t->mode = mode;
777 tuner_dbg("Changing to mode %d\n", mode);
779 return 0;
783 * set_freq - Set the tuner to the desired frequency.
784 * @t: a pointer to the module's internal struct_tuner
785 * @freq: frequency to set (0 means to use the current frequency)
787 static void set_freq(struct tuner *t, unsigned int freq)
789 struct i2c_client *client = v4l2_get_subdevdata(&t->sd);
791 if (t->mode == V4L2_TUNER_RADIO) {
792 if (!freq)
793 freq = t->radio_freq;
794 set_radio_freq(client, freq);
795 } else {
796 if (!freq)
797 freq = t->tv_freq;
798 set_tv_freq(client, freq);
803 * Functions that are specific for TV mode
807 * set_tv_freq - Set tuner frequency, freq in Units of 62.5 kHz = 1/16MHz
809 * @c: i2c_client descriptor
810 * @freq: frequency
812 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
814 struct tuner *t = to_tuner(i2c_get_clientdata(c));
815 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
817 struct analog_parameters params = {
818 .mode = t->mode,
819 .audmode = t->audmode,
820 .std = t->std
823 if (t->type == UNSET) {
824 tuner_warn("tuner type not set\n");
825 return;
827 if (NULL == analog_ops->set_params) {
828 tuner_warn("Tuner has no way to set tv freq\n");
829 return;
831 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
832 tuner_dbg("TV freq (%d.%02d) out of range (%d-%d)\n",
833 freq / 16, freq % 16 * 100 / 16, tv_range[0],
834 tv_range[1]);
835 /* V4L2 spec: if the freq is not possible then the closest
836 possible value should be selected */
837 if (freq < tv_range[0] * 16)
838 freq = tv_range[0] * 16;
839 else
840 freq = tv_range[1] * 16;
842 params.frequency = freq;
843 tuner_dbg("tv freq set to %d.%02d\n",
844 freq / 16, freq % 16 * 100 / 16);
845 t->tv_freq = freq;
846 t->standby = false;
848 analog_ops->set_params(&t->fe, &params);
852 * tuner_fixup_std - force a given video standard variant
854 * @t: tuner internal struct
855 * @std: TV standard
857 * A few devices or drivers have problem to detect some standard variations.
858 * On other operational systems, the drivers generally have a per-country
859 * code, and some logic to apply per-country hacks. V4L2 API doesn't provide
860 * such hacks. Instead, it relies on a proper video standard selection from
861 * the userspace application. However, as some apps are buggy, not allowing
862 * to distinguish all video standard variations, a modprobe parameter can
863 * be used to force a video standard match.
865 static v4l2_std_id tuner_fixup_std(struct tuner *t, v4l2_std_id std)
867 if (pal[0] != '-' && (std & V4L2_STD_PAL) == V4L2_STD_PAL) {
868 switch (pal[0]) {
869 case '6':
870 return V4L2_STD_PAL_60;
871 case 'b':
872 case 'B':
873 case 'g':
874 case 'G':
875 return V4L2_STD_PAL_BG;
876 case 'i':
877 case 'I':
878 return V4L2_STD_PAL_I;
879 case 'd':
880 case 'D':
881 case 'k':
882 case 'K':
883 return V4L2_STD_PAL_DK;
884 case 'M':
885 case 'm':
886 return V4L2_STD_PAL_M;
887 case 'N':
888 case 'n':
889 if (pal[1] == 'c' || pal[1] == 'C')
890 return V4L2_STD_PAL_Nc;
891 return V4L2_STD_PAL_N;
892 default:
893 tuner_warn("pal= argument not recognised\n");
894 break;
897 if (secam[0] != '-' && (std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
898 switch (secam[0]) {
899 case 'b':
900 case 'B':
901 case 'g':
902 case 'G':
903 case 'h':
904 case 'H':
905 return V4L2_STD_SECAM_B |
906 V4L2_STD_SECAM_G |
907 V4L2_STD_SECAM_H;
908 case 'd':
909 case 'D':
910 case 'k':
911 case 'K':
912 return V4L2_STD_SECAM_DK;
913 case 'l':
914 case 'L':
915 if ((secam[1] == 'C') || (secam[1] == 'c'))
916 return V4L2_STD_SECAM_LC;
917 return V4L2_STD_SECAM_L;
918 default:
919 tuner_warn("secam= argument not recognised\n");
920 break;
924 if (ntsc[0] != '-' && (std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
925 switch (ntsc[0]) {
926 case 'm':
927 case 'M':
928 return V4L2_STD_NTSC_M;
929 case 'j':
930 case 'J':
931 return V4L2_STD_NTSC_M_JP;
932 case 'k':
933 case 'K':
934 return V4L2_STD_NTSC_M_KR;
935 default:
936 tuner_info("ntsc= argument not recognised\n");
937 break;
940 return std;
944 * Functions that are specific for Radio mode
948 * set_radio_freq - Set tuner frequency, freq in Units of 62.5 Hz = 1/16kHz
950 * @c: i2c_client descriptor
951 * @freq: frequency
953 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
955 struct tuner *t = to_tuner(i2c_get_clientdata(c));
956 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
958 struct analog_parameters params = {
959 .mode = t->mode,
960 .audmode = t->audmode,
961 .std = t->std
964 if (t->type == UNSET) {
965 tuner_warn("tuner type not set\n");
966 return;
968 if (NULL == analog_ops->set_params) {
969 tuner_warn("tuner has no way to set radio frequency\n");
970 return;
972 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
973 tuner_dbg("radio freq (%d.%02d) out of range (%d-%d)\n",
974 freq / 16000, freq % 16000 * 100 / 16000,
975 radio_range[0], radio_range[1]);
976 /* V4L2 spec: if the freq is not possible then the closest
977 possible value should be selected */
978 if (freq < radio_range[0] * 16000)
979 freq = radio_range[0] * 16000;
980 else
981 freq = radio_range[1] * 16000;
983 params.frequency = freq;
984 tuner_dbg("radio freq set to %d.%02d\n",
985 freq / 16000, freq % 16000 * 100 / 16000);
986 t->radio_freq = freq;
987 t->standby = false;
989 analog_ops->set_params(&t->fe, &params);
993 * Debug function for reporting tuner status to userspace
997 * tuner_status - Dumps the current tuner status at dmesg
998 * @fe: pointer to struct dvb_frontend
1000 * This callback is used only for driver debug purposes, answering to
1001 * VIDIOC_LOG_STATUS. No changes should happen on this call.
1003 static void tuner_status(struct dvb_frontend *fe)
1005 struct tuner *t = fe->analog_demod_priv;
1006 unsigned long freq, freq_fraction;
1007 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
1008 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
1009 const char *p;
1011 switch (t->mode) {
1012 case V4L2_TUNER_RADIO:
1013 p = "radio";
1014 break;
1015 case V4L2_TUNER_DIGITAL_TV: /* Used by mt20xx */
1016 p = "digital TV";
1017 break;
1018 case V4L2_TUNER_ANALOG_TV:
1019 default:
1020 p = "analog TV";
1021 break;
1023 if (t->mode == V4L2_TUNER_RADIO) {
1024 freq = t->radio_freq / 16000;
1025 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
1026 } else {
1027 freq = t->tv_freq / 16;
1028 freq_fraction = (t->tv_freq % 16) * 100 / 16;
1030 tuner_info("Tuner mode: %s%s\n", p,
1031 t->standby ? " on standby mode" : "");
1032 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
1033 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
1034 if (t->mode != V4L2_TUNER_RADIO)
1035 return;
1036 if (fe_tuner_ops->get_status) {
1037 u32 tuner_status;
1039 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1040 if (tuner_status & TUNER_STATUS_LOCKED)
1041 tuner_info("Tuner is locked.\n");
1042 if (tuner_status & TUNER_STATUS_STEREO)
1043 tuner_info("Stereo: yes\n");
1045 if (analog_ops->has_signal)
1046 tuner_info("Signal strength: %d\n",
1047 analog_ops->has_signal(fe));
1051 * Function to splicitly change mode to radio. Probably not needed anymore
1054 static int tuner_s_radio(struct v4l2_subdev *sd)
1056 struct tuner *t = to_tuner(sd);
1058 if (set_mode(t, V4L2_TUNER_RADIO) == 0)
1059 set_freq(t, 0);
1060 return 0;
1064 * Tuner callbacks to handle userspace ioctl's
1068 * tuner_s_power - controls the power state of the tuner
1069 * @sd: pointer to struct v4l2_subdev
1070 * @on: a zero value puts the tuner to sleep, non-zero wakes it up
1072 static int tuner_s_power(struct v4l2_subdev *sd, int on)
1074 struct tuner *t = to_tuner(sd);
1075 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1077 if (on) {
1078 if (t->standby && set_mode(t, t->mode) == 0) {
1079 tuner_dbg("Waking up tuner\n");
1080 set_freq(t, 0);
1082 return 0;
1085 tuner_dbg("Putting tuner to sleep\n");
1086 t->standby = true;
1087 if (analog_ops->standby)
1088 analog_ops->standby(&t->fe);
1089 return 0;
1092 static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1094 struct tuner *t = to_tuner(sd);
1096 if (set_mode(t, V4L2_TUNER_ANALOG_TV))
1097 return 0;
1099 t->std = tuner_fixup_std(t, std);
1100 if (t->std != std)
1101 tuner_dbg("Fixup standard %llx to %llx\n", std, t->std);
1102 set_freq(t, 0);
1103 return 0;
1106 static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1108 struct tuner *t = to_tuner(sd);
1110 if (set_mode(t, f->type) == 0)
1111 set_freq(t, f->frequency);
1112 return 0;
1116 * tuner_g_frequency - Get the tuned frequency for the tuner
1117 * @sd: pointer to struct v4l2_subdev
1118 * @f: pointer to struct v4l2_frequency
1120 * At return, the structure f will be filled with tuner frequency
1121 * if the tuner matches the f->type.
1122 * Note: f->type should be initialized before calling it.
1123 * This is done by either video_ioctl2 or by the bridge driver.
1125 static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1127 struct tuner *t = to_tuner(sd);
1128 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1130 if (check_mode(t, f->type) == -EINVAL)
1131 return 0;
1132 if (f->type == t->mode && fe_tuner_ops->get_frequency && !t->standby) {
1133 u32 abs_freq;
1135 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
1136 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
1137 DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
1138 DIV_ROUND_CLOSEST(abs_freq, 62500);
1139 } else {
1140 f->frequency = (V4L2_TUNER_RADIO == f->type) ?
1141 t->radio_freq : t->tv_freq;
1143 return 0;
1147 * tuner_g_tuner - Fill in tuner information
1148 * @sd: pointer to struct v4l2_subdev
1149 * @vt: pointer to struct v4l2_tuner
1151 * At return, the structure vt will be filled with tuner information
1152 * if the tuner matches vt->type.
1153 * Note: vt->type should be initialized before calling it.
1154 * This is done by either video_ioctl2 or by the bridge driver.
1156 static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1158 struct tuner *t = to_tuner(sd);
1159 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1160 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1162 if (check_mode(t, vt->type) == -EINVAL)
1163 return 0;
1164 if (vt->type == t->mode && analog_ops->get_afc)
1165 vt->afc = analog_ops->get_afc(&t->fe);
1166 if (t->mode != V4L2_TUNER_RADIO) {
1167 vt->capability |= V4L2_TUNER_CAP_NORM;
1168 vt->rangelow = tv_range[0] * 16;
1169 vt->rangehigh = tv_range[1] * 16;
1170 return 0;
1173 /* radio mode */
1174 if (vt->type == t->mode) {
1175 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1176 if (fe_tuner_ops->get_status) {
1177 u32 tuner_status;
1179 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1180 vt->rxsubchans =
1181 (tuner_status & TUNER_STATUS_STEREO) ?
1182 V4L2_TUNER_SUB_STEREO :
1183 V4L2_TUNER_SUB_MONO;
1185 if (analog_ops->has_signal)
1186 vt->signal = analog_ops->has_signal(&t->fe);
1187 vt->audmode = t->audmode;
1189 vt->capability |= V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1190 vt->rangelow = radio_range[0] * 16000;
1191 vt->rangehigh = radio_range[1] * 16000;
1193 return 0;
1197 * tuner_s_tuner - Set the tuner's audio mode
1198 * @sd: pointer to struct v4l2_subdev
1199 * @vt: pointer to struct v4l2_tuner
1201 * Sets the audio mode if the tuner matches vt->type.
1202 * Note: vt->type should be initialized before calling it.
1203 * This is done by either video_ioctl2 or by the bridge driver.
1205 static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1207 struct tuner *t = to_tuner(sd);
1209 if (set_mode(t, vt->type))
1210 return 0;
1212 if (t->mode == V4L2_TUNER_RADIO)
1213 t->audmode = vt->audmode;
1214 set_freq(t, 0);
1216 return 0;
1219 static int tuner_log_status(struct v4l2_subdev *sd)
1221 struct tuner *t = to_tuner(sd);
1222 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1224 if (analog_ops->tuner_status)
1225 analog_ops->tuner_status(&t->fe);
1226 return 0;
1229 static int tuner_suspend(struct i2c_client *c, pm_message_t state)
1231 struct tuner *t = to_tuner(i2c_get_clientdata(c));
1232 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1234 tuner_dbg("suspend\n");
1236 if (!t->standby && analog_ops->standby)
1237 analog_ops->standby(&t->fe);
1239 return 0;
1242 static int tuner_resume(struct i2c_client *c)
1244 struct tuner *t = to_tuner(i2c_get_clientdata(c));
1246 tuner_dbg("resume\n");
1248 if (!t->standby)
1249 if (set_mode(t, t->mode) == 0)
1250 set_freq(t, 0);
1252 return 0;
1255 static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
1257 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1259 /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
1260 to handle it here.
1261 There must be a better way of doing this... */
1262 switch (cmd) {
1263 case TUNER_SET_CONFIG:
1264 return tuner_s_config(sd, arg);
1266 return -ENOIOCTLCMD;
1270 * Callback structs
1273 static const struct v4l2_subdev_core_ops tuner_core_ops = {
1274 .log_status = tuner_log_status,
1275 .s_std = tuner_s_std,
1276 .s_power = tuner_s_power,
1279 static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
1280 .s_radio = tuner_s_radio,
1281 .g_tuner = tuner_g_tuner,
1282 .s_tuner = tuner_s_tuner,
1283 .s_frequency = tuner_s_frequency,
1284 .g_frequency = tuner_g_frequency,
1285 .s_type_addr = tuner_s_type_addr,
1286 .s_config = tuner_s_config,
1289 static const struct v4l2_subdev_ops tuner_ops = {
1290 .core = &tuner_core_ops,
1291 .tuner = &tuner_tuner_ops,
1295 * I2C structs and module init functions
1298 static const struct i2c_device_id tuner_id[] = {
1299 { "tuner", }, /* autodetect */
1302 MODULE_DEVICE_TABLE(i2c, tuner_id);
1304 static struct i2c_driver tuner_driver = {
1305 .driver = {
1306 .owner = THIS_MODULE,
1307 .name = "tuner",
1309 .probe = tuner_probe,
1310 .remove = tuner_remove,
1311 .command = tuner_command,
1312 .suspend = tuner_suspend,
1313 .resume = tuner_resume,
1314 .id_table = tuner_id,
1317 static __init int init_tuner(void)
1319 return i2c_add_driver(&tuner_driver);
1322 static __exit void exit_tuner(void)
1324 i2c_del_driver(&tuner_driver);
1327 module_init(init_tuner);
1328 module_exit(exit_tuner);
1330 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
1331 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1332 MODULE_LICENSE("GPL");