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"
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);
80 static LIST_HEAD(tuner_list
);
81 static const struct v4l2_subdev_ops tuner_ops
;
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); \
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); \
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); \
105 #define tuner_dbg(fmt, arg...) do { \
107 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX, \
108 i2c_adapter_id(t->i2c->adapter), \
109 t->i2c->addr, ##arg); \
113 * Internal struct used inside the driver
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 */
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 */
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...) ({ \
154 typeof(&FUNCTION) __a = symbol_request(FUNCTION); \
156 __r = (int) __a(ARGS); \
157 symbol_put(FUNCTION); \
159 printk(KERN_ERR "TUNER: Unable to find " \
160 "symbol "#FUNCTION"()\n"); \
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
);
177 #define tuner_symbol_probe(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
);
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");
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
)
225 if (fe
->ops
.tuner_ops
.get_rf_strength
)
226 fe
->ops
.tuner_ops
.get_rf_strength(fe
, &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");
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,
267 * @tuner_callback: an optional function to be called when switching
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];
283 if (type
== UNSET
|| type
== TUNER_ABSENT
) {
284 tuner_dbg("tuner 0x%02x: Tuner type absent\n", c
->addr
);
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
;
302 if (!dvb_attach(microtune_attach
,
303 &t
->fe
, t
->i2c
->adapter
, t
->i2c
->addr
))
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
,
317 if (!dvb_attach(tea5767_attach
, &t
->fe
,
318 t
->i2c
->adapter
, t
->i2c
->addr
))
320 t
->mode_mask
= T_RADIO
;
323 if (!dvb_attach(tea5761_attach
, &t
->fe
,
324 t
->i2c
->adapter
, t
->i2c
->addr
))
326 t
->mode_mask
= T_RADIO
;
328 case TUNER_PHILIPS_FMD1216ME_MK3
:
333 i2c_master_send(c
, buffer
, 4);
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
))
342 case TUNER_PHILIPS_TD1316
:
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
))
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
))
364 if (!dvb_attach(tda9887_attach
,
365 &t
->fe
, t
->i2c
->adapter
, t
->i2c
->addr
))
370 struct xc5000_config xc5000_cfg
= {
371 .i2c_address
= t
->i2c
->addr
,
372 /* if_khz will be set at dvb_attach() */
376 if (!dvb_attach(xc5000_attach
,
377 &t
->fe
, t
->i2c
->adapter
, &xc5000_cfg
))
382 case TUNER_NXP_TDA18271
:
384 struct tda18271_config cfg
= {
386 .small_i2c
= TDA18271_03_BYTE_CHUNK_INIT
,
389 if (!dvb_attach(tda18271_attach
, &t
->fe
, t
->i2c
->addr
,
390 t
->i2c
->adapter
, &cfg
))
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 */
403 .set_smoothedcvbs
= 0,
406 if (!dvb_attach(xc4000_attach
,
407 &t
->fe
, t
->i2c
->adapter
, &xc4000_cfg
))
413 if (!dvb_attach(simple_tuner_attach
, &t
->fe
,
414 t
->i2c
->adapter
, t
->i2c
->addr
, t
->type
))
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
));
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
444 if (V4L2_TUNER_RADIO
== t
->mode
)
445 set_radio_freq(c
, t
->radio_freq
);
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
,
456 tuner_dbg("Tuner attach for type = %d failed.\n", t
->type
);
457 t
->type
= TUNER_ABSENT
;
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",
485 tun_setup
->mode_mask
,
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
);
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
);
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
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
)
520 if (analog_ops
->set_config
) {
521 analog_ops
->set_config(&t
->fe
, cfg
->priv
);
525 tuner_dbg("Tuner frontend module has no way to set config\n");
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
)
551 list_for_each_entry(pos
, &tuner_list
, list
) {
554 if (pos
->i2c
->adapter
!= adap
||
555 strcmp(pos
->i2c
->driver
->driver
.name
, "tuner"))
558 mode_mask
= pos
->mode_mask
;
559 if (*radio
== NULL
&& mode_mask
== T_RADIO
)
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
))
571 *tuner_probe - Probes the existing tuners on an I2C bus
573 * @client: i2c_client descriptor
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
)
592 t
= kzalloc(sizeof(struct tuner
), GFP_KERNEL
);
595 v4l2_i2c_subdev_init(&t
->sd
, client
, &tuner_ops
);
597 t
->name
= "(tuner unset)";
599 t
->audmode
= V4L2_TUNER_MODE_STEREO
;
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 */
605 unsigned char buffer
[16];
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
]);
616 /* autodetection code based on the i2c addr */
617 if (!no_autodetect
) {
618 switch (client
->addr
) {
620 if (tuner_symbol_probe(tea5761_autodetection
,
622 t
->i2c
->addr
) >= 0) {
623 t
->type
= TUNER_TEA5761
;
624 t
->mode_mask
= T_RADIO
;
625 tuner_lookup(t
->i2c
->adapter
, &radio
, &tv
);
627 tv
->mode_mask
&= ~T_RADIO
;
629 goto register_client
;
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");
643 /* Default is being tda9887 */
644 t
->type
= TUNER_TDA9887
;
645 t
->mode_mask
= T_RADIO
| T_ANALOG_TV
;
646 goto register_client
;
650 if (tuner_symbol_probe(tea5767_autodetection
,
651 t
->i2c
->adapter
, t
->i2c
->addr
)
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
);
658 tv
->mode_mask
&= ~T_RADIO
;
660 goto register_client
;
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
);
675 t
->mode_mask
= T_ANALOG_TV
;
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 */
683 /* Sets a default mode */
684 if (t
->mode_mask
& T_ANALOG_TV
)
685 t
->mode
= V4L2_TUNER_ANALOG_TV
;
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",
693 t
->mode_mask
& T_RADIO
? " Radio" : "",
694 t
->mode_mask
& T_ANALOG_TV
? " TV" : "");
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
;
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
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
)
742 if (mode
== V4L2_TUNER_RADIO
)
745 t_mode
= T_ANALOG_TV
;
747 if ((t_mode
& t
->mode_mask
) == 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
);
771 if (analog_ops
->standby
)
772 analog_ops
->standby(&t
->fe
);
776 tuner_dbg("Changing to mode %d\n", mode
);
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
) {
792 freq
= t
->radio_freq
;
793 set_radio_freq(client
, 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
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
= {
818 .audmode
= t
->audmode
,
822 if (t
->type
== UNSET
) {
823 tuner_warn("tuner type not set\n");
826 if (NULL
== analog_ops
->set_params
) {
827 tuner_warn("Tuner has no way to set tv freq\n");
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],
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;
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);
847 analog_ops
->set_params(&t
->fe
, ¶ms
);
851 * tuner_fixup_std - force a given video standard variant
853 * @t: tuner internal struct
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
) {
869 return V4L2_STD_PAL_60
;
874 return V4L2_STD_PAL_BG
;
877 return V4L2_STD_PAL_I
;
882 return V4L2_STD_PAL_DK
;
885 return V4L2_STD_PAL_M
;
888 if (pal
[1] == 'c' || pal
[1] == 'C')
889 return V4L2_STD_PAL_Nc
;
890 return V4L2_STD_PAL_N
;
892 tuner_warn("pal= argument not recognised\n");
896 if (secam
[0] != '-' && (std
& V4L2_STD_SECAM
) == V4L2_STD_SECAM
) {
904 return V4L2_STD_SECAM_B
|
911 return V4L2_STD_SECAM_DK
;
914 if ((secam
[1] == 'C') || (secam
[1] == 'c'))
915 return V4L2_STD_SECAM_LC
;
916 return V4L2_STD_SECAM_L
;
918 tuner_warn("secam= argument not recognised\n");
923 if (ntsc
[0] != '-' && (std
& V4L2_STD_NTSC
) == V4L2_STD_NTSC
) {
927 return V4L2_STD_NTSC_M
;
930 return V4L2_STD_NTSC_M_JP
;
933 return V4L2_STD_NTSC_M_KR
;
935 tuner_info("ntsc= argument not recognised\n");
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
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
= {
959 .audmode
= t
->audmode
,
963 if (t
->type
== UNSET
) {
964 tuner_warn("tuner type not set\n");
967 if (NULL
== analog_ops
->set_params
) {
968 tuner_warn("tuner has no way to set radio frequency\n");
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;
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
;
988 analog_ops
->set_params(&t
->fe
, ¶ms
);
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
;
1011 case V4L2_TUNER_RADIO
:
1014 case V4L2_TUNER_DIGITAL_TV
: /* Used by mt20xx */
1017 case V4L2_TUNER_ANALOG_TV
:
1022 if (t
->mode
== V4L2_TUNER_RADIO
) {
1023 freq
= t
->radio_freq
/ 16000;
1024 freq_fraction
= (t
->radio_freq
% 16000) * 100 / 16000;
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
)
1035 if (fe_tuner_ops
->get_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)
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
;
1077 if (t
->standby
&& set_mode(t
, t
->mode
) == 0) {
1078 tuner_dbg("Waking up tuner\n");
1084 tuner_dbg("Putting tuner to sleep\n");
1086 if (analog_ops
->standby
)
1087 analog_ops
->standby(&t
->fe
);
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
))
1098 t
->std
= tuner_fixup_std(t
, std
);
1100 tuner_dbg("Fixup standard %llx to %llx\n", std
, t
->std
);
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
);
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
)
1131 if (f
->type
== t
->mode
&& fe_tuner_ops
->get_frequency
&& !t
->standby
) {
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);
1139 f
->frequency
= (V4L2_TUNER_RADIO
== f
->type
) ?
1140 t
->radio_freq
: t
->tv_freq
;
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
)
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;
1173 if (vt
->type
== t
->mode
) {
1174 vt
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
1175 if (fe_tuner_ops
->get_status
) {
1178 fe_tuner_ops
->get_status(&t
->fe
, &tuner_status
);
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;
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
))
1211 if (t
->mode
== V4L2_TUNER_RADIO
)
1212 t
->audmode
= vt
->audmode
;
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
);
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
);
1241 static int tuner_resume(struct i2c_client
*c
)
1243 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
1245 tuner_dbg("resume\n");
1248 if (set_mode(t
, t
->mode
) == 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
1260 There must be a better way of doing this... */
1262 case TUNER_SET_CONFIG
:
1263 return tuner_s_config(sd
, arg
);
1265 return -ENOIOCTLCMD
;
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
= {
1305 .owner
= THIS_MODULE
,
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");