Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / media / video / tuner-core.c
blob11cc980b0cd5e7113da25b6b39297de8e33038da
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 buffer[0] = 0x0b;
330 buffer[1] = 0xdc;
331 buffer[2] = 0x9c;
332 buffer[3] = 0x60;
333 i2c_master_send(c, buffer, 4);
334 mdelay(1);
335 buffer[2] = 0x86;
336 buffer[3] = 0x54;
337 i2c_master_send(c, buffer, 4);
338 if (!dvb_attach(simple_tuner_attach, &t->fe,
339 t->i2c->adapter, t->i2c->addr, t->type))
340 goto attach_failed;
341 break;
342 case TUNER_PHILIPS_TD1316:
343 buffer[0] = 0x0b;
344 buffer[1] = 0xdc;
345 buffer[2] = 0x86;
346 buffer[3] = 0xa4;
347 i2c_master_send(c, buffer, 4);
348 if (!dvb_attach(simple_tuner_attach, &t->fe,
349 t->i2c->adapter, t->i2c->addr, t->type))
350 goto attach_failed;
351 break;
352 case TUNER_XC2028:
354 struct xc2028_config cfg = {
355 .i2c_adap = t->i2c->adapter,
356 .i2c_addr = t->i2c->addr,
358 if (!dvb_attach(xc2028_attach, &t->fe, &cfg))
359 goto attach_failed;
360 tune_now = 0;
361 break;
363 case TUNER_TDA9887:
364 if (!dvb_attach(tda9887_attach,
365 &t->fe, t->i2c->adapter, t->i2c->addr))
366 goto attach_failed;
367 break;
368 case TUNER_XC5000:
370 struct xc5000_config xc5000_cfg = {
371 .i2c_address = t->i2c->addr,
372 /* if_khz will be set at dvb_attach() */
373 .if_khz = 0,
376 if (!dvb_attach(xc5000_attach,
377 &t->fe, t->i2c->adapter, &xc5000_cfg))
378 goto attach_failed;
379 tune_now = 0;
380 break;
382 case TUNER_NXP_TDA18271:
384 struct tda18271_config cfg = {
385 .config = t->config,
386 .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
389 if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr,
390 t->i2c->adapter, &cfg))
391 goto attach_failed;
392 tune_now = 0;
393 break;
395 case TUNER_XC4000:
397 struct xc4000_config xc4000_cfg = {
398 .i2c_address = t->i2c->addr,
399 /* FIXME: the correct parameters will be set */
400 /* only when the digital dvb_attach() occurs */
401 .default_pm = 0,
402 .dvb_amplitude = 0,
403 .set_smoothedcvbs = 0,
404 .if_khz = 0
406 if (!dvb_attach(xc4000_attach,
407 &t->fe, t->i2c->adapter, &xc4000_cfg))
408 goto attach_failed;
409 tune_now = 0;
410 break;
412 default:
413 if (!dvb_attach(simple_tuner_attach, &t->fe,
414 t->i2c->adapter, t->i2c->addr, t->type))
415 goto attach_failed;
417 break;
420 if ((NULL == analog_ops->set_params) &&
421 (fe_tuner_ops->set_analog_params)) {
423 t->name = fe_tuner_ops->info.name;
425 t->fe.analog_demod_priv = t;
426 memcpy(analog_ops, &tuner_analog_ops,
427 sizeof(struct analog_demod_ops));
429 } else {
430 t->name = analog_ops->info.name;
433 tuner_dbg("type set to %s\n", t->name);
435 t->mode_mask = new_mode_mask;
437 /* Some tuners require more initialization setup before use,
438 such as firmware download or device calibration.
439 trying to set a frequency here will just fail
440 FIXME: better to move set_freq to the tuner code. This is needed
441 on analog tuners for PLL to properly work
443 if (tune_now) {
444 if (V4L2_TUNER_RADIO == t->mode)
445 set_radio_freq(c, t->radio_freq);
446 else
447 set_tv_freq(c, t->tv_freq);
450 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
451 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
452 t->mode_mask);
453 return;
455 attach_failed:
456 tuner_dbg("Tuner attach for type = %d failed.\n", t->type);
457 t->type = TUNER_ABSENT;
459 return;
463 * tuner_s_type_addr - Sets the tuner type for a device
465 * @sd: subdev descriptor
466 * @tun_setup: type to be associated to a given tuner i2c address
468 * This function applys the tuner config to tuner specified
469 * by tun_setup structure.
470 * If tuner I2C address is UNSET, then it will only set the device
471 * if the tuner supports the mode specified in the call.
472 * If the address is specified, the change will be applied only if
473 * tuner I2C address matches.
474 * The call can change the tuner number and the tuner mode.
476 static int tuner_s_type_addr(struct v4l2_subdev *sd,
477 struct tuner_setup *tun_setup)
479 struct tuner *t = to_tuner(sd);
480 struct i2c_client *c = v4l2_get_subdevdata(sd);
482 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
483 tun_setup->type,
484 tun_setup->addr,
485 tun_setup->mode_mask,
486 tun_setup->config);
488 if ((t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
489 (t->mode_mask & tun_setup->mode_mask))) ||
490 (tun_setup->addr == c->addr)) {
491 set_type(c, tun_setup->type, tun_setup->mode_mask,
492 tun_setup->config, tun_setup->tuner_callback);
493 } else
494 tuner_dbg("set addr discarded for type %i, mask %x. "
495 "Asked to change tuner at addr 0x%02x, with mask %x\n",
496 t->type, t->mode_mask,
497 tun_setup->addr, tun_setup->mode_mask);
499 return 0;
503 * tuner_s_config - Sets tuner configuration
505 * @sd: subdev descriptor
506 * @cfg: tuner configuration
508 * Calls tuner set_config() private function to set some tuner-internal
509 * parameters
511 static int tuner_s_config(struct v4l2_subdev *sd,
512 const struct v4l2_priv_tun_config *cfg)
514 struct tuner *t = to_tuner(sd);
515 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
517 if (t->type != cfg->tuner)
518 return 0;
520 if (analog_ops->set_config) {
521 analog_ops->set_config(&t->fe, cfg->priv);
522 return 0;
525 tuner_dbg("Tuner frontend module has no way to set config\n");
526 return 0;
530 * tuner_lookup - Seek for tuner adapters
532 * @adap: i2c_adapter struct
533 * @radio: pointer to be filled if the adapter is radio
534 * @tv: pointer to be filled if the adapter is TV
536 * Search for existing radio and/or TV tuners on the given I2C adapter,
537 * discarding demod-only adapters (tda9887).
539 * Note that when this function is called from tuner_probe you can be
540 * certain no other devices will be added/deleted at the same time, I2C
541 * core protects against that.
543 static void tuner_lookup(struct i2c_adapter *adap,
544 struct tuner **radio, struct tuner **tv)
546 struct tuner *pos;
548 *radio = NULL;
549 *tv = NULL;
551 list_for_each_entry(pos, &tuner_list, list) {
552 int mode_mask;
554 if (pos->i2c->adapter != adap ||
555 strcmp(pos->i2c->driver->driver.name, "tuner"))
556 continue;
558 mode_mask = pos->mode_mask;
559 if (*radio == NULL && mode_mask == T_RADIO)
560 *radio = pos;
561 /* Note: currently TDA9887 is the only demod-only
562 device. If other devices appear then we need to
563 make this test more general. */
564 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
565 (pos->mode_mask & T_ANALOG_TV))
566 *tv = pos;
571 *tuner_probe - Probes the existing tuners on an I2C bus
573 * @client: i2c_client descriptor
574 * @id: not used
576 * This routine probes for tuners at the expected I2C addresses. On most
577 * cases, if a device answers to a given I2C address, it assumes that the
578 * device is a tuner. On a few cases, however, an additional logic is needed
579 * to double check if the device is really a tuner, or to identify the tuner
580 * type, like on tea5767/5761 devices.
582 * During client attach, set_type is called by adapter's attach_inform callback.
583 * set_type must then be completed by tuner_probe.
585 static int tuner_probe(struct i2c_client *client,
586 const struct i2c_device_id *id)
588 struct tuner *t;
589 struct tuner *radio;
590 struct tuner *tv;
592 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
593 if (NULL == t)
594 return -ENOMEM;
595 v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
596 t->i2c = client;
597 t->name = "(tuner unset)";
598 t->type = UNSET;
599 t->audmode = V4L2_TUNER_MODE_STEREO;
600 t->standby = 1;
601 t->radio_freq = 87.5 * 16000; /* Initial freq range */
602 t->tv_freq = 400 * 16; /* Sets freq to VHF High - needed for some PLL's to properly start */
604 if (show_i2c) {
605 unsigned char buffer[16];
606 int i, rc;
608 memset(buffer, 0, sizeof(buffer));
609 rc = i2c_master_recv(client, buffer, sizeof(buffer));
610 tuner_info("I2C RECV = ");
611 for (i = 0; i < rc; i++)
612 printk(KERN_CONT "%02x ", buffer[i]);
613 printk("\n");
616 /* autodetection code based on the i2c addr */
617 if (!no_autodetect) {
618 switch (client->addr) {
619 case 0x10:
620 if (tuner_symbol_probe(tea5761_autodetection,
621 t->i2c->adapter,
622 t->i2c->addr) >= 0) {
623 t->type = TUNER_TEA5761;
624 t->mode_mask = T_RADIO;
625 tuner_lookup(t->i2c->adapter, &radio, &tv);
626 if (tv)
627 tv->mode_mask &= ~T_RADIO;
629 goto register_client;
631 kfree(t);
632 return -ENODEV;
633 case 0x42:
634 case 0x43:
635 case 0x4a:
636 case 0x4b:
637 /* If chip is not tda8290, don't register.
638 since it can be tda9887*/
639 if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
640 t->i2c->addr) >= 0) {
641 tuner_dbg("tda829x detected\n");
642 } else {
643 /* Default is being tda9887 */
644 t->type = TUNER_TDA9887;
645 t->mode_mask = T_RADIO | T_ANALOG_TV;
646 goto register_client;
648 break;
649 case 0x60:
650 if (tuner_symbol_probe(tea5767_autodetection,
651 t->i2c->adapter, t->i2c->addr)
652 >= 0) {
653 t->type = TUNER_TEA5767;
654 t->mode_mask = T_RADIO;
655 /* Sets freq to FM range */
656 tuner_lookup(t->i2c->adapter, &radio, &tv);
657 if (tv)
658 tv->mode_mask &= ~T_RADIO;
660 goto register_client;
662 break;
666 /* Initializes only the first TV tuner on this adapter. Why only the
667 first? Because there are some devices (notably the ones with TI
668 tuners) that have more than one i2c address for the *same* device.
669 Experience shows that, except for just one case, the first
670 address is the right one. The exception is a Russian tuner
671 (ACORP_Y878F). So, the desired behavior is just to enable the
672 first found TV tuner. */
673 tuner_lookup(t->i2c->adapter, &radio, &tv);
674 if (tv == NULL) {
675 t->mode_mask = T_ANALOG_TV;
676 if (radio == NULL)
677 t->mode_mask |= T_RADIO;
678 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
681 /* Should be just before return */
682 register_client:
683 /* Sets a default mode */
684 if (t->mode_mask & T_ANALOG_TV)
685 t->mode = V4L2_TUNER_ANALOG_TV;
686 else
687 t->mode = V4L2_TUNER_RADIO;
688 set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
689 list_add_tail(&t->list, &tuner_list);
691 tuner_info("Tuner %d found with type(s)%s%s.\n",
692 t->type,
693 t->mode_mask & T_RADIO ? " Radio" : "",
694 t->mode_mask & T_ANALOG_TV ? " TV" : "");
695 return 0;
699 * tuner_remove - detaches a tuner
701 * @client: i2c_client descriptor
704 static int tuner_remove(struct i2c_client *client)
706 struct tuner *t = to_tuner(i2c_get_clientdata(client));
708 v4l2_device_unregister_subdev(&t->sd);
709 tuner_detach(&t->fe);
710 t->fe.analog_demod_priv = NULL;
712 list_del(&t->list);
713 kfree(t);
714 return 0;
718 * Functions to switch between Radio and TV
720 * A few cards have a separate I2C tuner for radio. Those routines
721 * take care of switching between TV/Radio mode, filtering only the
722 * commands that apply to the Radio or TV tuner.
726 * check_mode - Verify if tuner supports the requested mode
727 * @t: a pointer to the module's internal struct_tuner
729 * This function checks if the tuner is capable of tuning analog TV,
730 * digital TV or radio, depending on what the caller wants. If the
731 * tuner can't support that mode, it returns -EINVAL. Otherwise, it
732 * returns 0.
733 * This function is needed for boards that have a separate tuner for
734 * radio (like devices with tea5767).
735 * NOTE: mt20xx uses V4L2_TUNER_DIGITAL_TV and calls set_tv_freq to
736 * select a TV frequency. So, t_mode = T_ANALOG_TV could actually
737 * be used to represent a Digital TV too.
739 static inline int check_mode(struct tuner *t, enum v4l2_tuner_type mode)
741 int t_mode;
742 if (mode == V4L2_TUNER_RADIO)
743 t_mode = T_RADIO;
744 else
745 t_mode = T_ANALOG_TV;
747 if ((t_mode & t->mode_mask) == 0)
748 return -EINVAL;
750 return 0;
754 * set_mode - Switch tuner to other mode.
755 * @t: a pointer to the module's internal struct_tuner
756 * @mode: enum v4l2_type (radio or TV)
758 * If tuner doesn't support the needed mode (radio or TV), prints a
759 * debug message and returns -EINVAL, changing its state to standby.
760 * Otherwise, changes the mode and returns 0.
762 static int set_mode(struct tuner *t, enum v4l2_tuner_type mode)
764 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
766 if (mode != t->mode) {
767 if (check_mode(t, mode) == -EINVAL) {
768 tuner_dbg("Tuner doesn't support mode %d. "
769 "Putting tuner to sleep\n", mode);
770 t->standby = true;
771 if (analog_ops->standby)
772 analog_ops->standby(&t->fe);
773 return -EINVAL;
775 t->mode = mode;
776 tuner_dbg("Changing to mode %d\n", mode);
778 return 0;
782 * set_freq - Set the tuner to the desired frequency.
783 * @t: a pointer to the module's internal struct_tuner
784 * @freq: frequency to set (0 means to use the current frequency)
786 static void set_freq(struct tuner *t, unsigned int freq)
788 struct i2c_client *client = v4l2_get_subdevdata(&t->sd);
790 if (t->mode == V4L2_TUNER_RADIO) {
791 if (!freq)
792 freq = t->radio_freq;
793 set_radio_freq(client, freq);
794 } else {
795 if (!freq)
796 freq = t->tv_freq;
797 set_tv_freq(client, freq);
802 * Functions that are specific for TV mode
806 * set_tv_freq - Set tuner frequency, freq in Units of 62.5 kHz = 1/16MHz
808 * @c: i2c_client descriptor
809 * @freq: frequency
811 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
813 struct tuner *t = to_tuner(i2c_get_clientdata(c));
814 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
816 struct analog_parameters params = {
817 .mode = t->mode,
818 .audmode = t->audmode,
819 .std = t->std
822 if (t->type == UNSET) {
823 tuner_warn("tuner type not set\n");
824 return;
826 if (NULL == analog_ops->set_params) {
827 tuner_warn("Tuner has no way to set tv freq\n");
828 return;
830 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
831 tuner_dbg("TV freq (%d.%02d) out of range (%d-%d)\n",
832 freq / 16, freq % 16 * 100 / 16, tv_range[0],
833 tv_range[1]);
834 /* V4L2 spec: if the freq is not possible then the closest
835 possible value should be selected */
836 if (freq < tv_range[0] * 16)
837 freq = tv_range[0] * 16;
838 else
839 freq = tv_range[1] * 16;
841 params.frequency = freq;
842 tuner_dbg("tv freq set to %d.%02d\n",
843 freq / 16, freq % 16 * 100 / 16);
844 t->tv_freq = freq;
845 t->standby = false;
847 analog_ops->set_params(&t->fe, &params);
851 * tuner_fixup_std - force a given video standard variant
853 * @t: tuner internal struct
854 * @std: TV standard
856 * A few devices or drivers have problem to detect some standard variations.
857 * On other operational systems, the drivers generally have a per-country
858 * code, and some logic to apply per-country hacks. V4L2 API doesn't provide
859 * such hacks. Instead, it relies on a proper video standard selection from
860 * the userspace application. However, as some apps are buggy, not allowing
861 * to distinguish all video standard variations, a modprobe parameter can
862 * be used to force a video standard match.
864 static v4l2_std_id tuner_fixup_std(struct tuner *t, v4l2_std_id std)
866 if (pal[0] != '-' && (std & V4L2_STD_PAL) == V4L2_STD_PAL) {
867 switch (pal[0]) {
868 case '6':
869 return V4L2_STD_PAL_60;
870 case 'b':
871 case 'B':
872 case 'g':
873 case 'G':
874 return V4L2_STD_PAL_BG;
875 case 'i':
876 case 'I':
877 return V4L2_STD_PAL_I;
878 case 'd':
879 case 'D':
880 case 'k':
881 case 'K':
882 return V4L2_STD_PAL_DK;
883 case 'M':
884 case 'm':
885 return V4L2_STD_PAL_M;
886 case 'N':
887 case 'n':
888 if (pal[1] == 'c' || pal[1] == 'C')
889 return V4L2_STD_PAL_Nc;
890 return V4L2_STD_PAL_N;
891 default:
892 tuner_warn("pal= argument not recognised\n");
893 break;
896 if (secam[0] != '-' && (std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
897 switch (secam[0]) {
898 case 'b':
899 case 'B':
900 case 'g':
901 case 'G':
902 case 'h':
903 case 'H':
904 return V4L2_STD_SECAM_B |
905 V4L2_STD_SECAM_G |
906 V4L2_STD_SECAM_H;
907 case 'd':
908 case 'D':
909 case 'k':
910 case 'K':
911 return V4L2_STD_SECAM_DK;
912 case 'l':
913 case 'L':
914 if ((secam[1] == 'C') || (secam[1] == 'c'))
915 return V4L2_STD_SECAM_LC;
916 return V4L2_STD_SECAM_L;
917 default:
918 tuner_warn("secam= argument not recognised\n");
919 break;
923 if (ntsc[0] != '-' && (std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
924 switch (ntsc[0]) {
925 case 'm':
926 case 'M':
927 return V4L2_STD_NTSC_M;
928 case 'j':
929 case 'J':
930 return V4L2_STD_NTSC_M_JP;
931 case 'k':
932 case 'K':
933 return V4L2_STD_NTSC_M_KR;
934 default:
935 tuner_info("ntsc= argument not recognised\n");
936 break;
939 return std;
943 * Functions that are specific for Radio mode
947 * set_radio_freq - Set tuner frequency, freq in Units of 62.5 Hz = 1/16kHz
949 * @c: i2c_client descriptor
950 * @freq: frequency
952 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
954 struct tuner *t = to_tuner(i2c_get_clientdata(c));
955 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
957 struct analog_parameters params = {
958 .mode = t->mode,
959 .audmode = t->audmode,
960 .std = t->std
963 if (t->type == UNSET) {
964 tuner_warn("tuner type not set\n");
965 return;
967 if (NULL == analog_ops->set_params) {
968 tuner_warn("tuner has no way to set radio frequency\n");
969 return;
971 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
972 tuner_dbg("radio freq (%d.%02d) out of range (%d-%d)\n",
973 freq / 16000, freq % 16000 * 100 / 16000,
974 radio_range[0], radio_range[1]);
975 /* V4L2 spec: if the freq is not possible then the closest
976 possible value should be selected */
977 if (freq < radio_range[0] * 16000)
978 freq = radio_range[0] * 16000;
979 else
980 freq = radio_range[1] * 16000;
982 params.frequency = freq;
983 tuner_dbg("radio freq set to %d.%02d\n",
984 freq / 16000, freq % 16000 * 100 / 16000);
985 t->radio_freq = freq;
986 t->standby = false;
988 analog_ops->set_params(&t->fe, &params);
992 * Debug function for reporting tuner status to userspace
996 * tuner_status - Dumps the current tuner status at dmesg
997 * @fe: pointer to struct dvb_frontend
999 * This callback is used only for driver debug purposes, answering to
1000 * VIDIOC_LOG_STATUS. No changes should happen on this call.
1002 static void tuner_status(struct dvb_frontend *fe)
1004 struct tuner *t = fe->analog_demod_priv;
1005 unsigned long freq, freq_fraction;
1006 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
1007 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
1008 const char *p;
1010 switch (t->mode) {
1011 case V4L2_TUNER_RADIO:
1012 p = "radio";
1013 break;
1014 case V4L2_TUNER_DIGITAL_TV: /* Used by mt20xx */
1015 p = "digital TV";
1016 break;
1017 case V4L2_TUNER_ANALOG_TV:
1018 default:
1019 p = "analog TV";
1020 break;
1022 if (t->mode == V4L2_TUNER_RADIO) {
1023 freq = t->radio_freq / 16000;
1024 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
1025 } else {
1026 freq = t->tv_freq / 16;
1027 freq_fraction = (t->tv_freq % 16) * 100 / 16;
1029 tuner_info("Tuner mode: %s%s\n", p,
1030 t->standby ? " on standby mode" : "");
1031 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
1032 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
1033 if (t->mode != V4L2_TUNER_RADIO)
1034 return;
1035 if (fe_tuner_ops->get_status) {
1036 u32 tuner_status;
1038 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1039 if (tuner_status & TUNER_STATUS_LOCKED)
1040 tuner_info("Tuner is locked.\n");
1041 if (tuner_status & TUNER_STATUS_STEREO)
1042 tuner_info("Stereo: yes\n");
1044 if (analog_ops->has_signal)
1045 tuner_info("Signal strength: %d\n",
1046 analog_ops->has_signal(fe));
1050 * Function to splicitly change mode to radio. Probably not needed anymore
1053 static int tuner_s_radio(struct v4l2_subdev *sd)
1055 struct tuner *t = to_tuner(sd);
1057 if (set_mode(t, V4L2_TUNER_RADIO) == 0)
1058 set_freq(t, 0);
1059 return 0;
1063 * Tuner callbacks to handle userspace ioctl's
1067 * tuner_s_power - controls the power state of the tuner
1068 * @sd: pointer to struct v4l2_subdev
1069 * @on: a zero value puts the tuner to sleep, non-zero wakes it up
1071 static int tuner_s_power(struct v4l2_subdev *sd, int on)
1073 struct tuner *t = to_tuner(sd);
1074 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1076 if (on) {
1077 if (t->standby && set_mode(t, t->mode) == 0) {
1078 tuner_dbg("Waking up tuner\n");
1079 set_freq(t, 0);
1081 return 0;
1084 tuner_dbg("Putting tuner to sleep\n");
1085 t->standby = true;
1086 if (analog_ops->standby)
1087 analog_ops->standby(&t->fe);
1088 return 0;
1091 static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1093 struct tuner *t = to_tuner(sd);
1095 if (set_mode(t, V4L2_TUNER_ANALOG_TV))
1096 return 0;
1098 t->std = tuner_fixup_std(t, std);
1099 if (t->std != std)
1100 tuner_dbg("Fixup standard %llx to %llx\n", std, t->std);
1101 set_freq(t, 0);
1102 return 0;
1105 static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1107 struct tuner *t = to_tuner(sd);
1109 if (set_mode(t, f->type) == 0)
1110 set_freq(t, f->frequency);
1111 return 0;
1115 * tuner_g_frequency - Get the tuned frequency for the tuner
1116 * @sd: pointer to struct v4l2_subdev
1117 * @f: pointer to struct v4l2_frequency
1119 * At return, the structure f will be filled with tuner frequency
1120 * if the tuner matches the f->type.
1121 * Note: f->type should be initialized before calling it.
1122 * This is done by either video_ioctl2 or by the bridge driver.
1124 static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1126 struct tuner *t = to_tuner(sd);
1127 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1129 if (check_mode(t, f->type) == -EINVAL)
1130 return 0;
1131 if (f->type == t->mode && fe_tuner_ops->get_frequency && !t->standby) {
1132 u32 abs_freq;
1134 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
1135 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
1136 DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
1137 DIV_ROUND_CLOSEST(abs_freq, 62500);
1138 } else {
1139 f->frequency = (V4L2_TUNER_RADIO == f->type) ?
1140 t->radio_freq : t->tv_freq;
1142 return 0;
1146 * tuner_g_tuner - Fill in tuner information
1147 * @sd: pointer to struct v4l2_subdev
1148 * @vt: pointer to struct v4l2_tuner
1150 * At return, the structure vt will be filled with tuner information
1151 * if the tuner matches vt->type.
1152 * Note: vt->type should be initialized before calling it.
1153 * This is done by either video_ioctl2 or by the bridge driver.
1155 static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1157 struct tuner *t = to_tuner(sd);
1158 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1159 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1161 if (check_mode(t, vt->type) == -EINVAL)
1162 return 0;
1163 if (vt->type == t->mode && analog_ops->get_afc)
1164 vt->afc = analog_ops->get_afc(&t->fe);
1165 if (t->mode != V4L2_TUNER_RADIO) {
1166 vt->capability |= V4L2_TUNER_CAP_NORM;
1167 vt->rangelow = tv_range[0] * 16;
1168 vt->rangehigh = tv_range[1] * 16;
1169 return 0;
1172 /* radio mode */
1173 if (vt->type == t->mode) {
1174 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1175 if (fe_tuner_ops->get_status) {
1176 u32 tuner_status;
1178 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1179 vt->rxsubchans =
1180 (tuner_status & TUNER_STATUS_STEREO) ?
1181 V4L2_TUNER_SUB_STEREO :
1182 V4L2_TUNER_SUB_MONO;
1184 if (analog_ops->has_signal)
1185 vt->signal = analog_ops->has_signal(&t->fe);
1186 vt->audmode = t->audmode;
1188 vt->capability |= V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1189 vt->rangelow = radio_range[0] * 16000;
1190 vt->rangehigh = radio_range[1] * 16000;
1192 return 0;
1196 * tuner_s_tuner - Set the tuner's audio mode
1197 * @sd: pointer to struct v4l2_subdev
1198 * @vt: pointer to struct v4l2_tuner
1200 * Sets the audio mode if the tuner matches vt->type.
1201 * Note: vt->type should be initialized before calling it.
1202 * This is done by either video_ioctl2 or by the bridge driver.
1204 static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1206 struct tuner *t = to_tuner(sd);
1208 if (set_mode(t, vt->type))
1209 return 0;
1211 if (t->mode == V4L2_TUNER_RADIO)
1212 t->audmode = vt->audmode;
1213 set_freq(t, 0);
1215 return 0;
1218 static int tuner_log_status(struct v4l2_subdev *sd)
1220 struct tuner *t = to_tuner(sd);
1221 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1223 if (analog_ops->tuner_status)
1224 analog_ops->tuner_status(&t->fe);
1225 return 0;
1228 static int tuner_suspend(struct i2c_client *c, pm_message_t state)
1230 struct tuner *t = to_tuner(i2c_get_clientdata(c));
1231 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1233 tuner_dbg("suspend\n");
1235 if (!t->standby && analog_ops->standby)
1236 analog_ops->standby(&t->fe);
1238 return 0;
1241 static int tuner_resume(struct i2c_client *c)
1243 struct tuner *t = to_tuner(i2c_get_clientdata(c));
1245 tuner_dbg("resume\n");
1247 if (!t->standby)
1248 if (set_mode(t, t->mode) == 0)
1249 set_freq(t, 0);
1251 return 0;
1254 static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
1256 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1258 /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
1259 to handle it here.
1260 There must be a better way of doing this... */
1261 switch (cmd) {
1262 case TUNER_SET_CONFIG:
1263 return tuner_s_config(sd, arg);
1265 return -ENOIOCTLCMD;
1269 * Callback structs
1272 static const struct v4l2_subdev_core_ops tuner_core_ops = {
1273 .log_status = tuner_log_status,
1274 .s_std = tuner_s_std,
1275 .s_power = tuner_s_power,
1278 static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
1279 .s_radio = tuner_s_radio,
1280 .g_tuner = tuner_g_tuner,
1281 .s_tuner = tuner_s_tuner,
1282 .s_frequency = tuner_s_frequency,
1283 .g_frequency = tuner_g_frequency,
1284 .s_type_addr = tuner_s_type_addr,
1285 .s_config = tuner_s_config,
1288 static const struct v4l2_subdev_ops tuner_ops = {
1289 .core = &tuner_core_ops,
1290 .tuner = &tuner_tuner_ops,
1294 * I2C structs and module init functions
1297 static const struct i2c_device_id tuner_id[] = {
1298 { "tuner", }, /* autodetect */
1301 MODULE_DEVICE_TABLE(i2c, tuner_id);
1303 static struct i2c_driver tuner_driver = {
1304 .driver = {
1305 .owner = THIS_MODULE,
1306 .name = "tuner",
1308 .probe = tuner_probe,
1309 .remove = tuner_remove,
1310 .command = tuner_command,
1311 .suspend = tuner_suspend,
1312 .resume = tuner_resume,
1313 .id_table = tuner_id,
1316 static __init int init_tuner(void)
1318 return i2c_add_driver(&tuner_driver);
1321 static __exit void exit_tuner(void)
1323 i2c_del_driver(&tuner_driver);
1326 module_init(init_tuner);
1327 module_exit(exit_tuner);
1329 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
1330 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1331 MODULE_LICENSE("GPL");