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
:
329 case TUNER_PHILIPS_FMD1216MEX_MK3
:
334 i2c_master_send(c
, buffer
, 4);
338 i2c_master_send(c
, buffer
, 4);
339 if (!dvb_attach(simple_tuner_attach
, &t
->fe
,
340 t
->i2c
->adapter
, t
->i2c
->addr
, t
->type
))
343 case TUNER_PHILIPS_TD1316
:
348 i2c_master_send(c
, buffer
, 4);
349 if (!dvb_attach(simple_tuner_attach
, &t
->fe
,
350 t
->i2c
->adapter
, t
->i2c
->addr
, t
->type
))
355 struct xc2028_config cfg
= {
356 .i2c_adap
= t
->i2c
->adapter
,
357 .i2c_addr
= t
->i2c
->addr
,
359 if (!dvb_attach(xc2028_attach
, &t
->fe
, &cfg
))
365 if (!dvb_attach(tda9887_attach
,
366 &t
->fe
, t
->i2c
->adapter
, t
->i2c
->addr
))
371 struct xc5000_config xc5000_cfg
= {
372 .i2c_address
= t
->i2c
->addr
,
373 /* if_khz will be set at dvb_attach() */
377 if (!dvb_attach(xc5000_attach
,
378 &t
->fe
, t
->i2c
->adapter
, &xc5000_cfg
))
383 case TUNER_NXP_TDA18271
:
385 struct tda18271_config cfg
= {
387 .small_i2c
= TDA18271_03_BYTE_CHUNK_INIT
,
390 if (!dvb_attach(tda18271_attach
, &t
->fe
, t
->i2c
->addr
,
391 t
->i2c
->adapter
, &cfg
))
398 struct xc4000_config xc4000_cfg
= {
399 .i2c_address
= t
->i2c
->addr
,
400 /* FIXME: the correct parameters will be set */
401 /* only when the digital dvb_attach() occurs */
404 .set_smoothedcvbs
= 0,
407 if (!dvb_attach(xc4000_attach
,
408 &t
->fe
, t
->i2c
->adapter
, &xc4000_cfg
))
414 if (!dvb_attach(simple_tuner_attach
, &t
->fe
,
415 t
->i2c
->adapter
, t
->i2c
->addr
, t
->type
))
421 if ((NULL
== analog_ops
->set_params
) &&
422 (fe_tuner_ops
->set_analog_params
)) {
424 t
->name
= fe_tuner_ops
->info
.name
;
426 t
->fe
.analog_demod_priv
= t
;
427 memcpy(analog_ops
, &tuner_analog_ops
,
428 sizeof(struct analog_demod_ops
));
431 t
->name
= analog_ops
->info
.name
;
434 tuner_dbg("type set to %s\n", t
->name
);
436 t
->mode_mask
= new_mode_mask
;
438 /* Some tuners require more initialization setup before use,
439 such as firmware download or device calibration.
440 trying to set a frequency here will just fail
441 FIXME: better to move set_freq to the tuner code. This is needed
442 on analog tuners for PLL to properly work
445 if (V4L2_TUNER_RADIO
== t
->mode
)
446 set_radio_freq(c
, t
->radio_freq
);
448 set_tv_freq(c
, t
->tv_freq
);
451 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
452 c
->adapter
->name
, c
->driver
->driver
.name
, c
->addr
<< 1, type
,
457 tuner_dbg("Tuner attach for type = %d failed.\n", t
->type
);
458 t
->type
= TUNER_ABSENT
;
464 * tuner_s_type_addr - Sets the tuner type for a device
466 * @sd: subdev descriptor
467 * @tun_setup: type to be associated to a given tuner i2c address
469 * This function applys the tuner config to tuner specified
470 * by tun_setup structure.
471 * If tuner I2C address is UNSET, then it will only set the device
472 * if the tuner supports the mode specified in the call.
473 * If the address is specified, the change will be applied only if
474 * tuner I2C address matches.
475 * The call can change the tuner number and the tuner mode.
477 static int tuner_s_type_addr(struct v4l2_subdev
*sd
,
478 struct tuner_setup
*tun_setup
)
480 struct tuner
*t
= to_tuner(sd
);
481 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
483 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
486 tun_setup
->mode_mask
,
489 if ((t
->type
== UNSET
&& ((tun_setup
->addr
== ADDR_UNSET
) &&
490 (t
->mode_mask
& tun_setup
->mode_mask
))) ||
491 (tun_setup
->addr
== c
->addr
)) {
492 set_type(c
, tun_setup
->type
, tun_setup
->mode_mask
,
493 tun_setup
->config
, tun_setup
->tuner_callback
);
495 tuner_dbg("set addr discarded for type %i, mask %x. "
496 "Asked to change tuner at addr 0x%02x, with mask %x\n",
497 t
->type
, t
->mode_mask
,
498 tun_setup
->addr
, tun_setup
->mode_mask
);
504 * tuner_s_config - Sets tuner configuration
506 * @sd: subdev descriptor
507 * @cfg: tuner configuration
509 * Calls tuner set_config() private function to set some tuner-internal
512 static int tuner_s_config(struct v4l2_subdev
*sd
,
513 const struct v4l2_priv_tun_config
*cfg
)
515 struct tuner
*t
= to_tuner(sd
);
516 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
518 if (t
->type
!= cfg
->tuner
)
521 if (analog_ops
->set_config
) {
522 analog_ops
->set_config(&t
->fe
, cfg
->priv
);
526 tuner_dbg("Tuner frontend module has no way to set config\n");
531 * tuner_lookup - Seek for tuner adapters
533 * @adap: i2c_adapter struct
534 * @radio: pointer to be filled if the adapter is radio
535 * @tv: pointer to be filled if the adapter is TV
537 * Search for existing radio and/or TV tuners on the given I2C adapter,
538 * discarding demod-only adapters (tda9887).
540 * Note that when this function is called from tuner_probe you can be
541 * certain no other devices will be added/deleted at the same time, I2C
542 * core protects against that.
544 static void tuner_lookup(struct i2c_adapter
*adap
,
545 struct tuner
**radio
, struct tuner
**tv
)
552 list_for_each_entry(pos
, &tuner_list
, list
) {
555 if (pos
->i2c
->adapter
!= adap
||
556 strcmp(pos
->i2c
->driver
->driver
.name
, "tuner"))
559 mode_mask
= pos
->mode_mask
;
560 if (*radio
== NULL
&& mode_mask
== T_RADIO
)
562 /* Note: currently TDA9887 is the only demod-only
563 device. If other devices appear then we need to
564 make this test more general. */
565 else if (*tv
== NULL
&& pos
->type
!= TUNER_TDA9887
&&
566 (pos
->mode_mask
& T_ANALOG_TV
))
572 *tuner_probe - Probes the existing tuners on an I2C bus
574 * @client: i2c_client descriptor
577 * This routine probes for tuners at the expected I2C addresses. On most
578 * cases, if a device answers to a given I2C address, it assumes that the
579 * device is a tuner. On a few cases, however, an additional logic is needed
580 * to double check if the device is really a tuner, or to identify the tuner
581 * type, like on tea5767/5761 devices.
583 * During client attach, set_type is called by adapter's attach_inform callback.
584 * set_type must then be completed by tuner_probe.
586 static int tuner_probe(struct i2c_client
*client
,
587 const struct i2c_device_id
*id
)
593 t
= kzalloc(sizeof(struct tuner
), GFP_KERNEL
);
596 v4l2_i2c_subdev_init(&t
->sd
, client
, &tuner_ops
);
598 t
->name
= "(tuner unset)";
600 t
->audmode
= V4L2_TUNER_MODE_STEREO
;
602 t
->radio_freq
= 87.5 * 16000; /* Initial freq range */
603 t
->tv_freq
= 400 * 16; /* Sets freq to VHF High - needed for some PLL's to properly start */
606 unsigned char buffer
[16];
609 memset(buffer
, 0, sizeof(buffer
));
610 rc
= i2c_master_recv(client
, buffer
, sizeof(buffer
));
611 tuner_info("I2C RECV = ");
612 for (i
= 0; i
< rc
; i
++)
613 printk(KERN_CONT
"%02x ", buffer
[i
]);
617 /* autodetection code based on the i2c addr */
618 if (!no_autodetect
) {
619 switch (client
->addr
) {
621 if (tuner_symbol_probe(tea5761_autodetection
,
623 t
->i2c
->addr
) >= 0) {
624 t
->type
= TUNER_TEA5761
;
625 t
->mode_mask
= T_RADIO
;
626 tuner_lookup(t
->i2c
->adapter
, &radio
, &tv
);
628 tv
->mode_mask
&= ~T_RADIO
;
630 goto register_client
;
638 /* If chip is not tda8290, don't register.
639 since it can be tda9887*/
640 if (tuner_symbol_probe(tda829x_probe
, t
->i2c
->adapter
,
641 t
->i2c
->addr
) >= 0) {
642 tuner_dbg("tda829x detected\n");
644 /* Default is being tda9887 */
645 t
->type
= TUNER_TDA9887
;
646 t
->mode_mask
= T_RADIO
| T_ANALOG_TV
;
647 goto register_client
;
651 if (tuner_symbol_probe(tea5767_autodetection
,
652 t
->i2c
->adapter
, t
->i2c
->addr
)
654 t
->type
= TUNER_TEA5767
;
655 t
->mode_mask
= T_RADIO
;
656 /* Sets freq to FM range */
657 tuner_lookup(t
->i2c
->adapter
, &radio
, &tv
);
659 tv
->mode_mask
&= ~T_RADIO
;
661 goto register_client
;
667 /* Initializes only the first TV tuner on this adapter. Why only the
668 first? Because there are some devices (notably the ones with TI
669 tuners) that have more than one i2c address for the *same* device.
670 Experience shows that, except for just one case, the first
671 address is the right one. The exception is a Russian tuner
672 (ACORP_Y878F). So, the desired behavior is just to enable the
673 first found TV tuner. */
674 tuner_lookup(t
->i2c
->adapter
, &radio
, &tv
);
676 t
->mode_mask
= T_ANALOG_TV
;
678 t
->mode_mask
|= T_RADIO
;
679 tuner_dbg("Setting mode_mask to 0x%02x\n", t
->mode_mask
);
682 /* Should be just before return */
684 /* Sets a default mode */
685 if (t
->mode_mask
& T_ANALOG_TV
)
686 t
->mode
= V4L2_TUNER_ANALOG_TV
;
688 t
->mode
= V4L2_TUNER_RADIO
;
689 set_type(client
, t
->type
, t
->mode_mask
, t
->config
, t
->fe
.callback
);
690 list_add_tail(&t
->list
, &tuner_list
);
692 tuner_info("Tuner %d found with type(s)%s%s.\n",
694 t
->mode_mask
& T_RADIO
? " Radio" : "",
695 t
->mode_mask
& T_ANALOG_TV
? " TV" : "");
700 * tuner_remove - detaches a tuner
702 * @client: i2c_client descriptor
705 static int tuner_remove(struct i2c_client
*client
)
707 struct tuner
*t
= to_tuner(i2c_get_clientdata(client
));
709 v4l2_device_unregister_subdev(&t
->sd
);
710 tuner_detach(&t
->fe
);
711 t
->fe
.analog_demod_priv
= NULL
;
719 * Functions to switch between Radio and TV
721 * A few cards have a separate I2C tuner for radio. Those routines
722 * take care of switching between TV/Radio mode, filtering only the
723 * commands that apply to the Radio or TV tuner.
727 * check_mode - Verify if tuner supports the requested mode
728 * @t: a pointer to the module's internal struct_tuner
730 * This function checks if the tuner is capable of tuning analog TV,
731 * digital TV or radio, depending on what the caller wants. If the
732 * tuner can't support that mode, it returns -EINVAL. Otherwise, it
734 * This function is needed for boards that have a separate tuner for
735 * radio (like devices with tea5767).
736 * NOTE: mt20xx uses V4L2_TUNER_DIGITAL_TV and calls set_tv_freq to
737 * select a TV frequency. So, t_mode = T_ANALOG_TV could actually
738 * be used to represent a Digital TV too.
740 static inline int check_mode(struct tuner
*t
, enum v4l2_tuner_type mode
)
743 if (mode
== V4L2_TUNER_RADIO
)
746 t_mode
= T_ANALOG_TV
;
748 if ((t_mode
& t
->mode_mask
) == 0)
755 * set_mode - Switch tuner to other mode.
756 * @t: a pointer to the module's internal struct_tuner
757 * @mode: enum v4l2_type (radio or TV)
759 * If tuner doesn't support the needed mode (radio or TV), prints a
760 * debug message and returns -EINVAL, changing its state to standby.
761 * Otherwise, changes the mode and returns 0.
763 static int set_mode(struct tuner
*t
, enum v4l2_tuner_type mode
)
765 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
767 if (mode
!= t
->mode
) {
768 if (check_mode(t
, mode
) == -EINVAL
) {
769 tuner_dbg("Tuner doesn't support mode %d. "
770 "Putting tuner to sleep\n", mode
);
772 if (analog_ops
->standby
)
773 analog_ops
->standby(&t
->fe
);
777 tuner_dbg("Changing to mode %d\n", mode
);
783 * set_freq - Set the tuner to the desired frequency.
784 * @t: a pointer to the module's internal struct_tuner
785 * @freq: frequency to set (0 means to use the current frequency)
787 static void set_freq(struct tuner
*t
, unsigned int freq
)
789 struct i2c_client
*client
= v4l2_get_subdevdata(&t
->sd
);
791 if (t
->mode
== V4L2_TUNER_RADIO
) {
793 freq
= t
->radio_freq
;
794 set_radio_freq(client
, freq
);
798 set_tv_freq(client
, freq
);
803 * Functions that are specific for TV mode
807 * set_tv_freq - Set tuner frequency, freq in Units of 62.5 kHz = 1/16MHz
809 * @c: i2c_client descriptor
812 static void set_tv_freq(struct i2c_client
*c
, unsigned int freq
)
814 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
815 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
817 struct analog_parameters params
= {
819 .audmode
= t
->audmode
,
823 if (t
->type
== UNSET
) {
824 tuner_warn("tuner type not set\n");
827 if (NULL
== analog_ops
->set_params
) {
828 tuner_warn("Tuner has no way to set tv freq\n");
831 if (freq
< tv_range
[0] * 16 || freq
> tv_range
[1] * 16) {
832 tuner_dbg("TV freq (%d.%02d) out of range (%d-%d)\n",
833 freq
/ 16, freq
% 16 * 100 / 16, tv_range
[0],
835 /* V4L2 spec: if the freq is not possible then the closest
836 possible value should be selected */
837 if (freq
< tv_range
[0] * 16)
838 freq
= tv_range
[0] * 16;
840 freq
= tv_range
[1] * 16;
842 params
.frequency
= freq
;
843 tuner_dbg("tv freq set to %d.%02d\n",
844 freq
/ 16, freq
% 16 * 100 / 16);
848 analog_ops
->set_params(&t
->fe
, ¶ms
);
852 * tuner_fixup_std - force a given video standard variant
854 * @t: tuner internal struct
857 * A few devices or drivers have problem to detect some standard variations.
858 * On other operational systems, the drivers generally have a per-country
859 * code, and some logic to apply per-country hacks. V4L2 API doesn't provide
860 * such hacks. Instead, it relies on a proper video standard selection from
861 * the userspace application. However, as some apps are buggy, not allowing
862 * to distinguish all video standard variations, a modprobe parameter can
863 * be used to force a video standard match.
865 static v4l2_std_id
tuner_fixup_std(struct tuner
*t
, v4l2_std_id std
)
867 if (pal
[0] != '-' && (std
& V4L2_STD_PAL
) == V4L2_STD_PAL
) {
870 return V4L2_STD_PAL_60
;
875 return V4L2_STD_PAL_BG
;
878 return V4L2_STD_PAL_I
;
883 return V4L2_STD_PAL_DK
;
886 return V4L2_STD_PAL_M
;
889 if (pal
[1] == 'c' || pal
[1] == 'C')
890 return V4L2_STD_PAL_Nc
;
891 return V4L2_STD_PAL_N
;
893 tuner_warn("pal= argument not recognised\n");
897 if (secam
[0] != '-' && (std
& V4L2_STD_SECAM
) == V4L2_STD_SECAM
) {
905 return V4L2_STD_SECAM_B
|
912 return V4L2_STD_SECAM_DK
;
915 if ((secam
[1] == 'C') || (secam
[1] == 'c'))
916 return V4L2_STD_SECAM_LC
;
917 return V4L2_STD_SECAM_L
;
919 tuner_warn("secam= argument not recognised\n");
924 if (ntsc
[0] != '-' && (std
& V4L2_STD_NTSC
) == V4L2_STD_NTSC
) {
928 return V4L2_STD_NTSC_M
;
931 return V4L2_STD_NTSC_M_JP
;
934 return V4L2_STD_NTSC_M_KR
;
936 tuner_info("ntsc= argument not recognised\n");
944 * Functions that are specific for Radio mode
948 * set_radio_freq - Set tuner frequency, freq in Units of 62.5 Hz = 1/16kHz
950 * @c: i2c_client descriptor
953 static void set_radio_freq(struct i2c_client
*c
, unsigned int freq
)
955 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
956 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
958 struct analog_parameters params
= {
960 .audmode
= t
->audmode
,
964 if (t
->type
== UNSET
) {
965 tuner_warn("tuner type not set\n");
968 if (NULL
== analog_ops
->set_params
) {
969 tuner_warn("tuner has no way to set radio frequency\n");
972 if (freq
< radio_range
[0] * 16000 || freq
> radio_range
[1] * 16000) {
973 tuner_dbg("radio freq (%d.%02d) out of range (%d-%d)\n",
974 freq
/ 16000, freq
% 16000 * 100 / 16000,
975 radio_range
[0], radio_range
[1]);
976 /* V4L2 spec: if the freq is not possible then the closest
977 possible value should be selected */
978 if (freq
< radio_range
[0] * 16000)
979 freq
= radio_range
[0] * 16000;
981 freq
= radio_range
[1] * 16000;
983 params
.frequency
= freq
;
984 tuner_dbg("radio freq set to %d.%02d\n",
985 freq
/ 16000, freq
% 16000 * 100 / 16000);
986 t
->radio_freq
= freq
;
989 analog_ops
->set_params(&t
->fe
, ¶ms
);
993 * Debug function for reporting tuner status to userspace
997 * tuner_status - Dumps the current tuner status at dmesg
998 * @fe: pointer to struct dvb_frontend
1000 * This callback is used only for driver debug purposes, answering to
1001 * VIDIOC_LOG_STATUS. No changes should happen on this call.
1003 static void tuner_status(struct dvb_frontend
*fe
)
1005 struct tuner
*t
= fe
->analog_demod_priv
;
1006 unsigned long freq
, freq_fraction
;
1007 struct dvb_tuner_ops
*fe_tuner_ops
= &fe
->ops
.tuner_ops
;
1008 struct analog_demod_ops
*analog_ops
= &fe
->ops
.analog_ops
;
1012 case V4L2_TUNER_RADIO
:
1015 case V4L2_TUNER_DIGITAL_TV
: /* Used by mt20xx */
1018 case V4L2_TUNER_ANALOG_TV
:
1023 if (t
->mode
== V4L2_TUNER_RADIO
) {
1024 freq
= t
->radio_freq
/ 16000;
1025 freq_fraction
= (t
->radio_freq
% 16000) * 100 / 16000;
1027 freq
= t
->tv_freq
/ 16;
1028 freq_fraction
= (t
->tv_freq
% 16) * 100 / 16;
1030 tuner_info("Tuner mode: %s%s\n", p
,
1031 t
->standby
? " on standby mode" : "");
1032 tuner_info("Frequency: %lu.%02lu MHz\n", freq
, freq_fraction
);
1033 tuner_info("Standard: 0x%08lx\n", (unsigned long)t
->std
);
1034 if (t
->mode
!= V4L2_TUNER_RADIO
)
1036 if (fe_tuner_ops
->get_status
) {
1039 fe_tuner_ops
->get_status(&t
->fe
, &tuner_status
);
1040 if (tuner_status
& TUNER_STATUS_LOCKED
)
1041 tuner_info("Tuner is locked.\n");
1042 if (tuner_status
& TUNER_STATUS_STEREO
)
1043 tuner_info("Stereo: yes\n");
1045 if (analog_ops
->has_signal
)
1046 tuner_info("Signal strength: %d\n",
1047 analog_ops
->has_signal(fe
));
1051 * Function to splicitly change mode to radio. Probably not needed anymore
1054 static int tuner_s_radio(struct v4l2_subdev
*sd
)
1056 struct tuner
*t
= to_tuner(sd
);
1058 if (set_mode(t
, V4L2_TUNER_RADIO
) == 0)
1064 * Tuner callbacks to handle userspace ioctl's
1068 * tuner_s_power - controls the power state of the tuner
1069 * @sd: pointer to struct v4l2_subdev
1070 * @on: a zero value puts the tuner to sleep, non-zero wakes it up
1072 static int tuner_s_power(struct v4l2_subdev
*sd
, int on
)
1074 struct tuner
*t
= to_tuner(sd
);
1075 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
1078 if (t
->standby
&& set_mode(t
, t
->mode
) == 0) {
1079 tuner_dbg("Waking up tuner\n");
1085 tuner_dbg("Putting tuner to sleep\n");
1087 if (analog_ops
->standby
)
1088 analog_ops
->standby(&t
->fe
);
1092 static int tuner_s_std(struct v4l2_subdev
*sd
, v4l2_std_id std
)
1094 struct tuner
*t
= to_tuner(sd
);
1096 if (set_mode(t
, V4L2_TUNER_ANALOG_TV
))
1099 t
->std
= tuner_fixup_std(t
, std
);
1101 tuner_dbg("Fixup standard %llx to %llx\n", std
, t
->std
);
1106 static int tuner_s_frequency(struct v4l2_subdev
*sd
, struct v4l2_frequency
*f
)
1108 struct tuner
*t
= to_tuner(sd
);
1110 if (set_mode(t
, f
->type
) == 0)
1111 set_freq(t
, f
->frequency
);
1116 * tuner_g_frequency - Get the tuned frequency for the tuner
1117 * @sd: pointer to struct v4l2_subdev
1118 * @f: pointer to struct v4l2_frequency
1120 * At return, the structure f will be filled with tuner frequency
1121 * if the tuner matches the f->type.
1122 * Note: f->type should be initialized before calling it.
1123 * This is done by either video_ioctl2 or by the bridge driver.
1125 static int tuner_g_frequency(struct v4l2_subdev
*sd
, struct v4l2_frequency
*f
)
1127 struct tuner
*t
= to_tuner(sd
);
1128 struct dvb_tuner_ops
*fe_tuner_ops
= &t
->fe
.ops
.tuner_ops
;
1130 if (check_mode(t
, f
->type
) == -EINVAL
)
1132 if (f
->type
== t
->mode
&& fe_tuner_ops
->get_frequency
&& !t
->standby
) {
1135 fe_tuner_ops
->get_frequency(&t
->fe
, &abs_freq
);
1136 f
->frequency
= (V4L2_TUNER_RADIO
== t
->mode
) ?
1137 DIV_ROUND_CLOSEST(abs_freq
* 2, 125) :
1138 DIV_ROUND_CLOSEST(abs_freq
, 62500);
1140 f
->frequency
= (V4L2_TUNER_RADIO
== f
->type
) ?
1141 t
->radio_freq
: t
->tv_freq
;
1147 * tuner_g_tuner - Fill in tuner information
1148 * @sd: pointer to struct v4l2_subdev
1149 * @vt: pointer to struct v4l2_tuner
1151 * At return, the structure vt will be filled with tuner information
1152 * if the tuner matches vt->type.
1153 * Note: vt->type should be initialized before calling it.
1154 * This is done by either video_ioctl2 or by the bridge driver.
1156 static int tuner_g_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*vt
)
1158 struct tuner
*t
= to_tuner(sd
);
1159 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
1160 struct dvb_tuner_ops
*fe_tuner_ops
= &t
->fe
.ops
.tuner_ops
;
1162 if (check_mode(t
, vt
->type
) == -EINVAL
)
1164 if (vt
->type
== t
->mode
&& analog_ops
->get_afc
)
1165 vt
->afc
= analog_ops
->get_afc(&t
->fe
);
1166 if (t
->mode
!= V4L2_TUNER_RADIO
) {
1167 vt
->capability
|= V4L2_TUNER_CAP_NORM
;
1168 vt
->rangelow
= tv_range
[0] * 16;
1169 vt
->rangehigh
= tv_range
[1] * 16;
1174 if (vt
->type
== t
->mode
) {
1175 vt
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
1176 if (fe_tuner_ops
->get_status
) {
1179 fe_tuner_ops
->get_status(&t
->fe
, &tuner_status
);
1181 (tuner_status
& TUNER_STATUS_STEREO
) ?
1182 V4L2_TUNER_SUB_STEREO
:
1183 V4L2_TUNER_SUB_MONO
;
1185 if (analog_ops
->has_signal
)
1186 vt
->signal
= analog_ops
->has_signal(&t
->fe
);
1187 vt
->audmode
= t
->audmode
;
1189 vt
->capability
|= V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_STEREO
;
1190 vt
->rangelow
= radio_range
[0] * 16000;
1191 vt
->rangehigh
= radio_range
[1] * 16000;
1197 * tuner_s_tuner - Set the tuner's audio mode
1198 * @sd: pointer to struct v4l2_subdev
1199 * @vt: pointer to struct v4l2_tuner
1201 * Sets the audio mode if the tuner matches vt->type.
1202 * Note: vt->type should be initialized before calling it.
1203 * This is done by either video_ioctl2 or by the bridge driver.
1205 static int tuner_s_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*vt
)
1207 struct tuner
*t
= to_tuner(sd
);
1209 if (set_mode(t
, vt
->type
))
1212 if (t
->mode
== V4L2_TUNER_RADIO
)
1213 t
->audmode
= vt
->audmode
;
1219 static int tuner_log_status(struct v4l2_subdev
*sd
)
1221 struct tuner
*t
= to_tuner(sd
);
1222 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
1224 if (analog_ops
->tuner_status
)
1225 analog_ops
->tuner_status(&t
->fe
);
1229 static int tuner_suspend(struct i2c_client
*c
, pm_message_t state
)
1231 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
1232 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
1234 tuner_dbg("suspend\n");
1236 if (!t
->standby
&& analog_ops
->standby
)
1237 analog_ops
->standby(&t
->fe
);
1242 static int tuner_resume(struct i2c_client
*c
)
1244 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
1246 tuner_dbg("resume\n");
1249 if (set_mode(t
, t
->mode
) == 0)
1255 static int tuner_command(struct i2c_client
*client
, unsigned cmd
, void *arg
)
1257 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1259 /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
1261 There must be a better way of doing this... */
1263 case TUNER_SET_CONFIG
:
1264 return tuner_s_config(sd
, arg
);
1266 return -ENOIOCTLCMD
;
1273 static const struct v4l2_subdev_core_ops tuner_core_ops
= {
1274 .log_status
= tuner_log_status
,
1275 .s_std
= tuner_s_std
,
1276 .s_power
= tuner_s_power
,
1279 static const struct v4l2_subdev_tuner_ops tuner_tuner_ops
= {
1280 .s_radio
= tuner_s_radio
,
1281 .g_tuner
= tuner_g_tuner
,
1282 .s_tuner
= tuner_s_tuner
,
1283 .s_frequency
= tuner_s_frequency
,
1284 .g_frequency
= tuner_g_frequency
,
1285 .s_type_addr
= tuner_s_type_addr
,
1286 .s_config
= tuner_s_config
,
1289 static const struct v4l2_subdev_ops tuner_ops
= {
1290 .core
= &tuner_core_ops
,
1291 .tuner
= &tuner_tuner_ops
,
1295 * I2C structs and module init functions
1298 static const struct i2c_device_id tuner_id
[] = {
1299 { "tuner", }, /* autodetect */
1302 MODULE_DEVICE_TABLE(i2c
, tuner_id
);
1304 static struct i2c_driver tuner_driver
= {
1306 .owner
= THIS_MODULE
,
1309 .probe
= tuner_probe
,
1310 .remove
= tuner_remove
,
1311 .command
= tuner_command
,
1312 .suspend
= tuner_suspend
,
1313 .resume
= tuner_resume
,
1314 .id_table
= tuner_id
,
1317 static __init
int init_tuner(void)
1319 return i2c_add_driver(&tuner_driver
);
1322 static __exit
void exit_tuner(void)
1324 i2c_del_driver(&tuner_driver
);
1327 module_init(init_tuner
);
1328 module_exit(exit_tuner
);
1330 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
1331 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1332 MODULE_LICENSE("GPL");