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>
37 #include "tuner-xc2028.h"
38 #include "tuner-simple.h"
45 #define PREFIX (t->i2c->driver->driver.name)
48 * Driver modprobe parameters
51 /* insmod options used at init time => read/only */
52 static unsigned int addr;
53 static unsigned int no_autodetect;
54 static unsigned int show_i2c;
56 module_param(addr, int, 0444);
57 module_param(no_autodetect, int, 0444);
58 module_param(show_i2c, int, 0444);
60 /* insmod options used at runtime => read/write */
61 static int tuner_debug;
62 static unsigned int tv_range[2] = { 44, 958 };
63 static unsigned int radio_range[2] = { 65, 108 };
64 static char pal[] = "--";
65 static char secam[] = "--";
66 static char ntsc[] = "-";
68 module_param_named(debug, tuner_debug, int, 0644);
69 module_param_array(tv_range, int, NULL, 0644);
70 module_param_array(radio_range, int, NULL, 0644);
71 module_param_string(pal, pal, sizeof(pal), 0644);
72 module_param_string(secam, secam, sizeof(secam), 0644);
73 module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
79 static LIST_HEAD(tuner_list);
80 static const struct v4l2_subdev_ops tuner_ops;
86 #define tuner_warn(fmt, arg...) do { \
87 printk(KERN_WARNING "%s %d-%04x: " fmt, PREFIX, \
88 i2c_adapter_id(t->i2c->adapter), \
89 t->i2c->addr, ##arg); \
92 #define tuner_info(fmt, arg...) do { \
93 printk(KERN_INFO "%s %d-%04x: " fmt, PREFIX, \
94 i2c_adapter_id(t->i2c->adapter), \
95 t->i2c->addr, ##arg); \
98 #define tuner_err(fmt, arg...) do { \
99 printk(KERN_ERR "%s %d-%04x: " fmt, PREFIX, \
100 i2c_adapter_id(t->i2c->adapter), \
101 t->i2c->addr, ##arg); \
104 #define tuner_dbg(fmt, arg...) do { \
106 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX, \
107 i2c_adapter_id(t->i2c->adapter), \
108 t->i2c->addr, ##arg); \
112 * Internal struct used inside the driver
117 struct dvb_frontend fe;
118 struct i2c_client *i2c;
119 struct v4l2_subdev sd;
120 struct list_head list;
122 /* keep track of the current settings */
124 unsigned int tv_freq;
125 unsigned int radio_freq;
126 unsigned int audmode;
128 enum v4l2_tuner_type mode;
129 unsigned int mode_mask; /* Combination of allowable modes */
131 bool standby; /* Standby mode */
133 unsigned int type; /* chip type id */
139 * Function prototypes
142 static void set_tv_freq(struct i2c_client *c, unsigned int freq);
143 static void set_radio_freq(struct i2c_client *c, unsigned int freq);
146 * tuner attach/detach logic
149 /* This macro allows us to probe dynamically, avoiding static links */
150 #ifdef CONFIG_MEDIA_ATTACH
151 #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
153 typeof(&FUNCTION) __a = symbol_request(FUNCTION); \
155 __r = (int) __a(ARGS); \
156 symbol_put(FUNCTION); \
158 printk(KERN_ERR "TUNER: Unable to find " \
159 "symbol "#FUNCTION"()\n"); \
164 static void tuner_detach(struct dvb_frontend *fe)
166 if (fe->ops.tuner_ops.release) {
167 fe->ops.tuner_ops.release(fe);
168 symbol_put_addr(fe->ops.tuner_ops.release);
170 if (fe->ops.analog_ops.release) {
171 fe->ops.analog_ops.release(fe);
172 symbol_put_addr(fe->ops.analog_ops.release);
176 #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
180 static void tuner_detach(struct dvb_frontend *fe)
182 if (fe->ops.tuner_ops.release)
183 fe->ops.tuner_ops.release(fe);
184 if (fe->ops.analog_ops.release)
185 fe->ops.analog_ops.release(fe);
190 static inline struct tuner *to_tuner(struct v4l2_subdev *sd)
192 return container_of(sd, struct tuner, sd);
196 * struct analog_demod_ops callbacks
199 static void fe_set_params(struct dvb_frontend *fe,
200 struct analog_parameters *params)
202 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
203 struct tuner *t = fe->analog_demod_priv;
205 if (NULL == fe_tuner_ops->set_analog_params) {
206 tuner_warn("Tuner frontend module has no way to set freq\n");
209 fe_tuner_ops->set_analog_params(fe, params);
212 static void fe_standby(struct dvb_frontend *fe)
214 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
216 if (fe_tuner_ops->sleep)
217 fe_tuner_ops->sleep(fe);
220 static int fe_has_signal(struct dvb_frontend *fe)
224 if (fe->ops.tuner_ops.get_rf_strength)
225 fe->ops.tuner_ops.get_rf_strength(fe, &strength);
230 static int fe_set_config(struct dvb_frontend *fe, void *priv_cfg)
232 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
233 struct tuner *t = fe->analog_demod_priv;
235 if (fe_tuner_ops->set_config)
236 return fe_tuner_ops->set_config(fe, priv_cfg);
238 tuner_warn("Tuner frontend module has no way to set config\n");
243 static void tuner_status(struct dvb_frontend *fe);
245 static struct analog_demod_ops tuner_analog_ops = {
246 .set_params = fe_set_params,
247 .standby = fe_standby,
248 .has_signal = fe_has_signal,
249 .set_config = fe_set_config,
250 .tuner_status = tuner_status
254 * Functions to select between radio and TV and tuner probe/remove functions
258 * set_type - Sets the tuner type for a given device
260 * @c: i2c_client descriptoy
261 * @type: type of the tuner (e. g. tuner number)
262 * @new_mode_mask: Indicates if tuner supports TV and/or Radio
263 * @new_config: an optional parameter ranging from 0-255 used by
264 a few tuners to adjust an internal parameter,
266 * @tuner_callback: an optional function to be called when switching
269 * This function applys the tuner config to tuner specified
270 * by tun_setup structure. It contains several per-tuner initialization "magic"
272 static void set_type(struct i2c_client *c, unsigned int type,
273 unsigned int new_mode_mask, unsigned int new_config,
274 int (*tuner_callback) (void *dev, int component, int cmd, int arg))
276 struct tuner *t = to_tuner(i2c_get_clientdata(c));
277 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
278 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
279 unsigned char buffer[4];
282 if (type == UNSET || type == TUNER_ABSENT) {
283 tuner_dbg("tuner 0x%02x: Tuner type absent\n", c->addr);
288 /* prevent invalid config values */
289 t->config = new_config < 256 ? new_config : 0;
290 if (tuner_callback != NULL) {
291 tuner_dbg("defining GPIO callback\n");
292 t->fe.callback = tuner_callback;
295 /* discard private data, in case set_type() was previously called */
296 tuner_detach(&t->fe);
297 t->fe.analog_demod_priv = NULL;
301 if (!dvb_attach(microtune_attach,
302 &t->fe, t->i2c->adapter, t->i2c->addr))
305 case TUNER_PHILIPS_TDA8290:
307 struct tda829x_config cfg = {
308 .lna_cfg = t->config,
310 if (!dvb_attach(tda829x_attach, &t->fe, t->i2c->adapter,
316 if (!dvb_attach(tea5767_attach, &t->fe,
317 t->i2c->adapter, t->i2c->addr))
319 t->mode_mask = T_RADIO;
322 if (!dvb_attach(tea5761_attach, &t->fe,
323 t->i2c->adapter, t->i2c->addr))
325 t->mode_mask = T_RADIO;
327 case TUNER_PHILIPS_FMD1216ME_MK3:
332 i2c_master_send(c, buffer, 4);
336 i2c_master_send(c, buffer, 4);
337 if (!dvb_attach(simple_tuner_attach, &t->fe,
338 t->i2c->adapter, t->i2c->addr, t->type))
341 case TUNER_PHILIPS_TD1316:
346 i2c_master_send(c, buffer, 4);
347 if (!dvb_attach(simple_tuner_attach, &t->fe,
348 t->i2c->adapter, t->i2c->addr, t->type))
353 struct xc2028_config cfg = {
354 .i2c_adap = t->i2c->adapter,
355 .i2c_addr = t->i2c->addr,
357 if (!dvb_attach(xc2028_attach, &t->fe, &cfg))
363 if (!dvb_attach(tda9887_attach,
364 &t->fe, t->i2c->adapter, t->i2c->addr))
369 struct xc5000_config xc5000_cfg = {
370 .i2c_address = t->i2c->addr,
371 /* if_khz will be set at dvb_attach() */
375 if (!dvb_attach(xc5000_attach,
376 &t->fe, t->i2c->adapter, &xc5000_cfg))
381 case TUNER_NXP_TDA18271:
383 struct tda18271_config cfg = {
385 .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
388 if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr,
389 t->i2c->adapter, &cfg))
395 if (!dvb_attach(simple_tuner_attach, &t->fe,
396 t->i2c->adapter, t->i2c->addr, t->type))
402 if ((NULL == analog_ops->set_params) &&
403 (fe_tuner_ops->set_analog_params)) {
405 t->name = fe_tuner_ops->info.name;
407 t->fe.analog_demod_priv = t;
408 memcpy(analog_ops, &tuner_analog_ops,
409 sizeof(struct analog_demod_ops));
412 t->name = analog_ops->info.name;
415 tuner_dbg("type set to %s\n", t->name);
417 t->mode_mask = new_mode_mask;
419 /* Some tuners require more initialization setup before use,
420 such as firmware download or device calibration.
421 trying to set a frequency here will just fail
422 FIXME: better to move set_freq to the tuner code. This is needed
423 on analog tuners for PLL to properly work
426 if (V4L2_TUNER_RADIO == t->mode)
427 set_radio_freq(c, t->radio_freq);
429 set_tv_freq(c, t->tv_freq);
432 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
433 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
438 tuner_dbg("Tuner attach for type = %d failed.\n", t->type);
439 t->type = TUNER_ABSENT;
445 * tuner_s_type_addr - Sets the tuner type for a device
447 * @sd: subdev descriptor
448 * @tun_setup: type to be associated to a given tuner i2c address
450 * This function applys the tuner config to tuner specified
451 * by tun_setup structure.
452 * If tuner I2C address is UNSET, then it will only set the device
453 * if the tuner supports the mode specified in the call.
454 * If the address is specified, the change will be applied only if
455 * tuner I2C address matches.
456 * The call can change the tuner number and the tuner mode.
458 static int tuner_s_type_addr(struct v4l2_subdev *sd,
459 struct tuner_setup *tun_setup)
461 struct tuner *t = to_tuner(sd);
462 struct i2c_client *c = v4l2_get_subdevdata(sd);
464 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
467 tun_setup->mode_mask,
470 if ((t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
471 (t->mode_mask & tun_setup->mode_mask))) ||
472 (tun_setup->addr == c->addr)) {
473 set_type(c, tun_setup->type, tun_setup->mode_mask,
474 tun_setup->config, tun_setup->tuner_callback);
476 tuner_dbg("set addr discarded for type %i, mask %x. "
477 "Asked to change tuner at addr 0x%02x, with mask %x\n",
478 t->type, t->mode_mask,
479 tun_setup->addr, tun_setup->mode_mask);
485 * tuner_s_config - Sets tuner configuration
487 * @sd: subdev descriptor
488 * @cfg: tuner configuration
490 * Calls tuner set_config() private function to set some tuner-internal
493 static int tuner_s_config(struct v4l2_subdev *sd,
494 const struct v4l2_priv_tun_config *cfg)
496 struct tuner *t = to_tuner(sd);
497 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
499 if (t->type != cfg->tuner)
502 if (analog_ops->set_config) {
503 analog_ops->set_config(&t->fe, cfg->priv);
507 tuner_dbg("Tuner frontend module has no way to set config\n");
512 * tuner_lookup - Seek for tuner adapters
514 * @adap: i2c_adapter struct
515 * @radio: pointer to be filled if the adapter is radio
516 * @tv: pointer to be filled if the adapter is TV
518 * Search for existing radio and/or TV tuners on the given I2C adapter,
519 * discarding demod-only adapters (tda9887).
521 * Note that when this function is called from tuner_probe you can be
522 * certain no other devices will be added/deleted at the same time, I2C
523 * core protects against that.
525 static void tuner_lookup(struct i2c_adapter *adap,
526 struct tuner **radio, struct tuner **tv)
533 list_for_each_entry(pos, &tuner_list, list) {
536 if (pos->i2c->adapter != adap ||
537 strcmp(pos->i2c->driver->driver.name, "tuner"))
540 mode_mask = pos->mode_mask;
541 if (*radio == NULL && mode_mask == T_RADIO)
543 /* Note: currently TDA9887 is the only demod-only
544 device. If other devices appear then we need to
545 make this test more general. */
546 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
547 (pos->mode_mask & T_ANALOG_TV))
553 *tuner_probe - Probes the existing tuners on an I2C bus
555 * @client: i2c_client descriptor
558 * This routine probes for tuners at the expected I2C addresses. On most
559 * cases, if a device answers to a given I2C address, it assumes that the
560 * device is a tuner. On a few cases, however, an additional logic is needed
561 * to double check if the device is really a tuner, or to identify the tuner
562 * type, like on tea5767/5761 devices.
564 * During client attach, set_type is called by adapter's attach_inform callback.
565 * set_type must then be completed by tuner_probe.
567 static int tuner_probe(struct i2c_client *client,
568 const struct i2c_device_id *id)
574 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
577 v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
579 t->name = "(tuner unset)";
581 t->audmode = V4L2_TUNER_MODE_STEREO;
583 t->radio_freq = 87.5 * 16000; /* Initial freq range */
584 t->tv_freq = 400 * 16; /* Sets freq to VHF High - needed for some PLL's to properly start */
587 unsigned char buffer[16];
590 memset(buffer, 0, sizeof(buffer));
591 rc = i2c_master_recv(client, buffer, sizeof(buffer));
592 tuner_info("I2C RECV = ");
593 for (i = 0; i < rc; i++)
594 printk(KERN_CONT "%02x ", buffer[i]);
598 /* autodetection code based on the i2c addr */
599 if (!no_autodetect) {
600 switch (client->addr) {
602 if (tuner_symbol_probe(tea5761_autodetection,
604 t->i2c->addr) >= 0) {
605 t->type = TUNER_TEA5761;
606 t->mode_mask = T_RADIO;
607 tuner_lookup(t->i2c->adapter, &radio, &tv);
609 tv->mode_mask &= ~T_RADIO;
611 goto register_client;
619 /* If chip is not tda8290, don't register.
620 since it can be tda9887*/
621 if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
622 t->i2c->addr) >= 0) {
623 tuner_dbg("tda829x detected\n");
625 /* Default is being tda9887 */
626 t->type = TUNER_TDA9887;
627 t->mode_mask = T_RADIO | T_ANALOG_TV;
628 goto register_client;
632 if (tuner_symbol_probe(tea5767_autodetection,
633 t->i2c->adapter, t->i2c->addr)
635 t->type = TUNER_TEA5767;
636 t->mode_mask = T_RADIO;
637 /* Sets freq to FM range */
638 tuner_lookup(t->i2c->adapter, &radio, &tv);
640 tv->mode_mask &= ~T_RADIO;
642 goto register_client;
648 /* Initializes only the first TV tuner on this adapter. Why only the
649 first? Because there are some devices (notably the ones with TI
650 tuners) that have more than one i2c address for the *same* device.
651 Experience shows that, except for just one case, the first
652 address is the right one. The exception is a Russian tuner
653 (ACORP_Y878F). So, the desired behavior is just to enable the
654 first found TV tuner. */
655 tuner_lookup(t->i2c->adapter, &radio, &tv);
657 t->mode_mask = T_ANALOG_TV;
659 t->mode_mask |= T_RADIO;
660 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
663 /* Should be just before return */
665 /* Sets a default mode */
666 if (t->mode_mask & T_ANALOG_TV)
667 t->mode = V4L2_TUNER_ANALOG_TV;
669 t->mode = V4L2_TUNER_RADIO;
670 set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
671 list_add_tail(&t->list, &tuner_list);
673 tuner_info("Tuner %d found with type(s)%s%s.\n",
675 t->mode_mask & T_RADIO ? " Radio" : "",
676 t->mode_mask & T_ANALOG_TV ? " TV" : "");
681 * tuner_remove - detaches a tuner
683 * @client: i2c_client descriptor
686 static int tuner_remove(struct i2c_client *client)
688 struct tuner *t = to_tuner(i2c_get_clientdata(client));
690 v4l2_device_unregister_subdev(&t->sd);
691 tuner_detach(&t->fe);
692 t->fe.analog_demod_priv = NULL;
700 * Functions to switch between Radio and TV
702 * A few cards have a separate I2C tuner for radio. Those routines
703 * take care of switching between TV/Radio mode, filtering only the
704 * commands that apply to the Radio or TV tuner.
708 * check_mode - Verify if tuner supports the requested mode
709 * @t: a pointer to the module's internal struct_tuner
711 * This function checks if the tuner is capable of tuning analog TV,
712 * digital TV or radio, depending on what the caller wants. If the
713 * tuner can't support that mode, it returns -EINVAL. Otherwise, it
715 * This function is needed for boards that have a separate tuner for
716 * radio (like devices with tea5767).
717 * NOTE: mt20xx uses V4L2_TUNER_DIGITAL_TV and calls set_tv_freq to
718 * select a TV frequency. So, t_mode = T_ANALOG_TV could actually
719 * be used to represent a Digital TV too.
721 static inline int check_mode(struct tuner *t, enum v4l2_tuner_type mode)
724 if (mode == V4L2_TUNER_RADIO)
727 t_mode = T_ANALOG_TV;
729 if ((t_mode & t->mode_mask) == 0)
736 * set_mode - Switch tuner to other mode.
737 * @t: a pointer to the module's internal struct_tuner
738 * @mode: enum v4l2_type (radio or TV)
740 * If tuner doesn't support the needed mode (radio or TV), prints a
741 * debug message and returns -EINVAL, changing its state to standby.
742 * Otherwise, changes the mode and returns 0.
744 static int set_mode(struct tuner *t, enum v4l2_tuner_type mode)
746 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
748 if (mode != t->mode) {
749 if (check_mode(t, mode) == -EINVAL) {
750 tuner_dbg("Tuner doesn't support mode %d. "
751 "Putting tuner to sleep\n", mode);
753 if (analog_ops->standby)
754 analog_ops->standby(&t->fe);
758 tuner_dbg("Changing to mode %d\n", mode);
764 * set_freq - Set the tuner to the desired frequency.
765 * @t: a pointer to the module's internal struct_tuner
766 * @freq: frequency to set (0 means to use the current frequency)
768 static void set_freq(struct tuner *t, unsigned int freq)
770 struct i2c_client *client = v4l2_get_subdevdata(&t->sd);
772 if (t->mode == V4L2_TUNER_RADIO) {
774 freq = t->radio_freq;
775 set_radio_freq(client, freq);
779 set_tv_freq(client, freq);
784 * Functions that are specific for TV mode
788 * set_tv_freq - Set tuner frequency, freq in Units of 62.5 kHz = 1/16MHz
790 * @c: i2c_client descriptor
793 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
795 struct tuner *t = to_tuner(i2c_get_clientdata(c));
796 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
798 struct analog_parameters params = {
800 .audmode = t->audmode,
804 if (t->type == UNSET) {
805 tuner_warn("tuner type not set\n");
808 if (NULL == analog_ops->set_params) {
809 tuner_warn("Tuner has no way to set tv freq\n");
812 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
813 tuner_dbg("TV freq (%d.%02d) out of range (%d-%d)\n",
814 freq / 16, freq % 16 * 100 / 16, tv_range[0],
816 /* V4L2 spec: if the freq is not possible then the closest
817 possible value should be selected */
818 if (freq < tv_range[0] * 16)
819 freq = tv_range[0] * 16;
821 freq = tv_range[1] * 16;
823 params.frequency = freq;
824 tuner_dbg("tv freq set to %d.%02d\n",
825 freq / 16, freq % 16 * 100 / 16);
829 analog_ops->set_params(&t->fe, ¶ms);
833 * tuner_fixup_std - force a given video standard variant
835 * @t: tuner internal struct
838 * A few devices or drivers have problem to detect some standard variations.
839 * On other operational systems, the drivers generally have a per-country
840 * code, and some logic to apply per-country hacks. V4L2 API doesn't provide
841 * such hacks. Instead, it relies on a proper video standard selection from
842 * the userspace application. However, as some apps are buggy, not allowing
843 * to distinguish all video standard variations, a modprobe parameter can
844 * be used to force a video standard match.
846 static v4l2_std_id tuner_fixup_std(struct tuner *t, v4l2_std_id std)
848 if (pal[0] != '-' && (std & V4L2_STD_PAL) == V4L2_STD_PAL) {
851 return V4L2_STD_PAL_60;
856 return V4L2_STD_PAL_BG;
859 return V4L2_STD_PAL_I;
864 return V4L2_STD_PAL_DK;
867 return V4L2_STD_PAL_M;
870 if (pal[1] == 'c' || pal[1] == 'C')
871 return V4L2_STD_PAL_Nc;
872 return V4L2_STD_PAL_N;
874 tuner_warn("pal= argument not recognised\n");
878 if (secam[0] != '-' && (std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
886 return V4L2_STD_SECAM_B |
893 return V4L2_STD_SECAM_DK;
896 if ((secam[1] == 'C') || (secam[1] == 'c'))
897 return V4L2_STD_SECAM_LC;
898 return V4L2_STD_SECAM_L;
900 tuner_warn("secam= argument not recognised\n");
905 if (ntsc[0] != '-' && (std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
909 return V4L2_STD_NTSC_M;
912 return V4L2_STD_NTSC_M_JP;
915 return V4L2_STD_NTSC_M_KR;
917 tuner_info("ntsc= argument not recognised\n");
925 * Functions that are specific for Radio mode
929 * set_radio_freq - Set tuner frequency, freq in Units of 62.5 Hz = 1/16kHz
931 * @c: i2c_client descriptor
934 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
936 struct tuner *t = to_tuner(i2c_get_clientdata(c));
937 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
939 struct analog_parameters params = {
941 .audmode = t->audmode,
945 if (t->type == UNSET) {
946 tuner_warn("tuner type not set\n");
949 if (NULL == analog_ops->set_params) {
950 tuner_warn("tuner has no way to set radio frequency\n");
953 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
954 tuner_dbg("radio freq (%d.%02d) out of range (%d-%d)\n",
955 freq / 16000, freq % 16000 * 100 / 16000,
956 radio_range[0], radio_range[1]);
957 /* V4L2 spec: if the freq is not possible then the closest
958 possible value should be selected */
959 if (freq < radio_range[0] * 16000)
960 freq = radio_range[0] * 16000;
962 freq = radio_range[1] * 16000;
964 params.frequency = freq;
965 tuner_dbg("radio freq set to %d.%02d\n",
966 freq / 16000, freq % 16000 * 100 / 16000);
967 t->radio_freq = freq;
970 analog_ops->set_params(&t->fe, ¶ms);
974 * Debug function for reporting tuner status to userspace
978 * tuner_status - Dumps the current tuner status at dmesg
979 * @fe: pointer to struct dvb_frontend
981 * This callback is used only for driver debug purposes, answering to
982 * VIDIOC_LOG_STATUS. No changes should happen on this call.
984 static void tuner_status(struct dvb_frontend *fe)
986 struct tuner *t = fe->analog_demod_priv;
987 unsigned long freq, freq_fraction;
988 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
989 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
993 case V4L2_TUNER_RADIO:
996 case V4L2_TUNER_DIGITAL_TV: /* Used by mt20xx */
999 case V4L2_TUNER_ANALOG_TV:
1004 if (t->mode == V4L2_TUNER_RADIO) {
1005 freq = t->radio_freq / 16000;
1006 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
1008 freq = t->tv_freq / 16;
1009 freq_fraction = (t->tv_freq % 16) * 100 / 16;
1011 tuner_info("Tuner mode: %s%s\n", p,
1012 t->standby ? " on standby mode" : "");
1013 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
1014 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
1015 if (t->mode != V4L2_TUNER_RADIO)
1017 if (fe_tuner_ops->get_status) {
1020 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1021 if (tuner_status & TUNER_STATUS_LOCKED)
1022 tuner_info("Tuner is locked.\n");
1023 if (tuner_status & TUNER_STATUS_STEREO)
1024 tuner_info("Stereo: yes\n");
1026 if (analog_ops->has_signal)
1027 tuner_info("Signal strength: %d\n",
1028 analog_ops->has_signal(fe));
1032 * Function to splicitly change mode to radio. Probably not needed anymore
1035 static int tuner_s_radio(struct v4l2_subdev *sd)
1037 struct tuner *t = to_tuner(sd);
1039 if (set_mode(t, V4L2_TUNER_RADIO) == 0)
1045 * Tuner callbacks to handle userspace ioctl's
1049 * tuner_s_power - controls the power state of the tuner
1050 * @sd: pointer to struct v4l2_subdev
1051 * @on: a zero value puts the tuner to sleep, non-zero wakes it up
1053 static int tuner_s_power(struct v4l2_subdev *sd, int on)
1055 struct tuner *t = to_tuner(sd);
1056 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1059 if (t->standby && set_mode(t, t->mode) == 0) {
1060 tuner_dbg("Waking up tuner\n");
1066 tuner_dbg("Putting tuner to sleep\n");
1068 if (analog_ops->standby)
1069 analog_ops->standby(&t->fe);
1073 static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1075 struct tuner *t = to_tuner(sd);
1077 if (set_mode(t, V4L2_TUNER_ANALOG_TV))
1080 t->std = tuner_fixup_std(t, std);
1082 tuner_dbg("Fixup standard %llx to %llx\n", std, t->std);
1087 static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1089 struct tuner *t = to_tuner(sd);
1091 if (set_mode(t, f->type) == 0)
1092 set_freq(t, f->frequency);
1097 * tuner_g_frequency - Get the tuned frequency for the tuner
1098 * @sd: pointer to struct v4l2_subdev
1099 * @f: pointer to struct v4l2_frequency
1101 * At return, the structure f will be filled with tuner frequency
1102 * if the tuner matches the f->type.
1103 * Note: f->type should be initialized before calling it.
1104 * This is done by either video_ioctl2 or by the bridge driver.
1106 static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1108 struct tuner *t = to_tuner(sd);
1109 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1111 if (check_mode(t, f->type) == -EINVAL)
1113 if (f->type == t->mode && fe_tuner_ops->get_frequency && !t->standby) {
1116 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
1117 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
1118 DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
1119 DIV_ROUND_CLOSEST(abs_freq, 62500);
1121 f->frequency = (V4L2_TUNER_RADIO == f->type) ?
1122 t->radio_freq : t->tv_freq;
1128 * tuner_g_tuner - Fill in tuner information
1129 * @sd: pointer to struct v4l2_subdev
1130 * @vt: pointer to struct v4l2_tuner
1132 * At return, the structure vt will be filled with tuner information
1133 * if the tuner matches vt->type.
1134 * Note: vt->type should be initialized before calling it.
1135 * This is done by either video_ioctl2 or by the bridge driver.
1137 static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1139 struct tuner *t = to_tuner(sd);
1140 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1141 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1143 if (check_mode(t, vt->type) == -EINVAL)
1145 if (vt->type == t->mode && analog_ops->get_afc)
1146 vt->afc = analog_ops->get_afc(&t->fe);
1147 if (t->mode != V4L2_TUNER_RADIO) {
1148 vt->capability |= V4L2_TUNER_CAP_NORM;
1149 vt->rangelow = tv_range[0] * 16;
1150 vt->rangehigh = tv_range[1] * 16;
1155 if (vt->type == t->mode) {
1156 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1157 if (fe_tuner_ops->get_status) {
1160 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1162 (tuner_status & TUNER_STATUS_STEREO) ?
1163 V4L2_TUNER_SUB_STEREO :
1164 V4L2_TUNER_SUB_MONO;
1166 if (analog_ops->has_signal)
1167 vt->signal = analog_ops->has_signal(&t->fe);
1168 vt->audmode = t->audmode;
1170 vt->capability |= V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1171 vt->rangelow = radio_range[0] * 16000;
1172 vt->rangehigh = radio_range[1] * 16000;
1178 * tuner_s_tuner - Set the tuner's audio mode
1179 * @sd: pointer to struct v4l2_subdev
1180 * @vt: pointer to struct v4l2_tuner
1182 * Sets the audio mode if the tuner matches vt->type.
1183 * Note: vt->type should be initialized before calling it.
1184 * This is done by either video_ioctl2 or by the bridge driver.
1186 static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1188 struct tuner *t = to_tuner(sd);
1190 if (set_mode(t, vt->type))
1193 if (t->mode == V4L2_TUNER_RADIO)
1194 t->audmode = vt->audmode;
1200 static int tuner_log_status(struct v4l2_subdev *sd)
1202 struct tuner *t = to_tuner(sd);
1203 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1205 if (analog_ops->tuner_status)
1206 analog_ops->tuner_status(&t->fe);
1210 static int tuner_suspend(struct i2c_client *c, pm_message_t state)
1212 struct tuner *t = to_tuner(i2c_get_clientdata(c));
1213 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1215 tuner_dbg("suspend\n");
1217 if (!t->standby && analog_ops->standby)
1218 analog_ops->standby(&t->fe);
1223 static int tuner_resume(struct i2c_client *c)
1225 struct tuner *t = to_tuner(i2c_get_clientdata(c));
1227 tuner_dbg("resume\n");
1230 if (set_mode(t, t->mode) == 0)
1236 static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
1238 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1240 /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
1242 There must be a better way of doing this... */
1244 case TUNER_SET_CONFIG:
1245 return tuner_s_config(sd, arg);
1247 return -ENOIOCTLCMD;
1254 static const struct v4l2_subdev_core_ops tuner_core_ops = {
1255 .log_status = tuner_log_status,
1256 .s_std = tuner_s_std,
1257 .s_power = tuner_s_power,
1260 static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
1261 .s_radio = tuner_s_radio,
1262 .g_tuner = tuner_g_tuner,
1263 .s_tuner = tuner_s_tuner,
1264 .s_frequency = tuner_s_frequency,
1265 .g_frequency = tuner_g_frequency,
1266 .s_type_addr = tuner_s_type_addr,
1267 .s_config = tuner_s_config,
1270 static const struct v4l2_subdev_ops tuner_ops = {
1271 .core = &tuner_core_ops,
1272 .tuner = &tuner_tuner_ops,
1276 * I2C structs and module init functions
1279 static const struct i2c_device_id tuner_id[] = {
1280 { "tuner", }, /* autodetect */
1283 MODULE_DEVICE_TABLE(i2c, tuner_id);
1285 static struct i2c_driver tuner_driver = {
1287 .owner = THIS_MODULE,
1290 .probe = tuner_probe,
1291 .remove = tuner_remove,
1292 .command = tuner_command,
1293 .suspend = tuner_suspend,
1294 .resume = tuner_resume,
1295 .id_table = tuner_id,
1298 static __init int init_tuner(void)
1300 return i2c_add_driver(&tuner_driver);
1303 static __exit void exit_tuner(void)
1305 i2c_del_driver(&tuner_driver);
1308 module_init(init_tuner);
1309 module_exit(exit_tuner);
1311 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
1312 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1313 MODULE_LICENSE("GPL");