3 * i2c tv tuner chip device driver
4 * core core, i.e. kernel interfaces, registering and so on
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/string.h>
10 #include <linux/timer.h>
11 #include <linux/delay.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/poll.h>
15 #include <linux/i2c.h>
16 #include <linux/types.h>
17 #include <linux/init.h>
18 #include <linux/videodev2.h>
19 #include <media/tuner.h>
20 #include <media/tuner-types.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-ioctl.h>
27 #include "tuner-xc2028.h"
28 #include "tuner-simple.h"
35 #define PREFIX (t->i2c->driver->driver.name)
38 * Driver modprobe parameters
41 /* insmod options used at init time => read/only */
42 static unsigned int addr
;
43 static unsigned int no_autodetect
;
44 static unsigned int show_i2c
;
46 module_param(addr
, int, 0444);
47 module_param(no_autodetect
, int, 0444);
48 module_param(show_i2c
, int, 0444);
50 /* insmod options used at runtime => read/write */
51 static int tuner_debug
;
52 static unsigned int tv_range
[2] = { 44, 958 };
53 static unsigned int radio_range
[2] = { 65, 108 };
54 static char pal
[] = "--";
55 static char secam
[] = "--";
56 static char ntsc
[] = "-";
58 module_param_named(debug
, tuner_debug
, int, 0644);
59 module_param_array(tv_range
, int, NULL
, 0644);
60 module_param_array(radio_range
, int, NULL
, 0644);
61 module_param_string(pal
, pal
, sizeof(pal
), 0644);
62 module_param_string(secam
, secam
, sizeof(secam
), 0644);
63 module_param_string(ntsc
, ntsc
, sizeof(ntsc
), 0644);
69 static LIST_HEAD(tuner_list
);
75 #define tuner_warn(fmt, arg...) do { \
76 printk(KERN_WARNING "%s %d-%04x: " fmt, PREFIX, \
77 i2c_adapter_id(t->i2c->adapter), \
78 t->i2c->addr, ##arg); \
81 #define tuner_info(fmt, arg...) do { \
82 printk(KERN_INFO "%s %d-%04x: " fmt, PREFIX, \
83 i2c_adapter_id(t->i2c->adapter), \
84 t->i2c->addr, ##arg); \
87 #define tuner_err(fmt, arg...) do { \
88 printk(KERN_ERR "%s %d-%04x: " fmt, PREFIX, \
89 i2c_adapter_id(t->i2c->adapter), \
90 t->i2c->addr, ##arg); \
93 #define tuner_dbg(fmt, arg...) do { \
95 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX, \
96 i2c_adapter_id(t->i2c->adapter), \
97 t->i2c->addr, ##arg); \
101 * Internal struct used inside the driver
106 struct dvb_frontend fe
;
107 struct i2c_client
*i2c
;
108 struct v4l2_subdev sd
;
109 struct list_head list
;
111 /* keep track of the current settings */
113 unsigned int tv_freq
;
114 unsigned int radio_freq
;
115 unsigned int audmode
;
117 enum v4l2_tuner_type mode
;
118 unsigned int mode_mask
; /* Combination of allowable modes */
120 bool standby
; /* Standby mode */
122 unsigned int type
; /* chip type id */
128 * tuner attach/detach logic
131 /* This macro allows us to probe dynamically, avoiding static links */
132 #ifdef CONFIG_MEDIA_ATTACH
133 #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
135 typeof(&FUNCTION) __a = symbol_request(FUNCTION); \
137 __r = (int) __a(ARGS); \
138 symbol_put(FUNCTION); \
140 printk(KERN_ERR "TUNER: Unable to find " \
141 "symbol "#FUNCTION"()\n"); \
146 static void tuner_detach(struct dvb_frontend
*fe
)
148 if (fe
->ops
.tuner_ops
.release
) {
149 fe
->ops
.tuner_ops
.release(fe
);
150 symbol_put_addr(fe
->ops
.tuner_ops
.release
);
152 if (fe
->ops
.analog_ops
.release
) {
153 fe
->ops
.analog_ops
.release(fe
);
154 symbol_put_addr(fe
->ops
.analog_ops
.release
);
158 #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
162 static void tuner_detach(struct dvb_frontend
*fe
)
164 if (fe
->ops
.tuner_ops
.release
)
165 fe
->ops
.tuner_ops
.release(fe
);
166 if (fe
->ops
.analog_ops
.release
)
167 fe
->ops
.analog_ops
.release(fe
);
172 static inline struct tuner
*to_tuner(struct v4l2_subdev
*sd
)
174 return container_of(sd
, struct tuner
, sd
);
178 * struct analog_demod_ops callbacks
181 static void fe_set_params(struct dvb_frontend
*fe
,
182 struct analog_parameters
*params
)
184 struct dvb_tuner_ops
*fe_tuner_ops
= &fe
->ops
.tuner_ops
;
185 struct tuner
*t
= fe
->analog_demod_priv
;
187 if (NULL
== fe_tuner_ops
->set_analog_params
) {
188 tuner_warn("Tuner frontend module has no way to set freq\n");
191 fe_tuner_ops
->set_analog_params(fe
, params
);
194 static void fe_standby(struct dvb_frontend
*fe
)
196 struct dvb_tuner_ops
*fe_tuner_ops
= &fe
->ops
.tuner_ops
;
198 if (fe_tuner_ops
->sleep
)
199 fe_tuner_ops
->sleep(fe
);
202 static int fe_has_signal(struct dvb_frontend
*fe
)
206 if (fe
->ops
.tuner_ops
.get_rf_strength
)
207 fe
->ops
.tuner_ops
.get_rf_strength(fe
, &strength
);
212 static int fe_set_config(struct dvb_frontend
*fe
, void *priv_cfg
)
214 struct dvb_tuner_ops
*fe_tuner_ops
= &fe
->ops
.tuner_ops
;
215 struct tuner
*t
= fe
->analog_demod_priv
;
217 if (fe_tuner_ops
->set_config
)
218 return fe_tuner_ops
->set_config(fe
, priv_cfg
);
220 tuner_warn("Tuner frontend module has no way to set config\n");
225 static void tuner_status(struct dvb_frontend
*fe
);
227 static struct analog_demod_ops tuner_analog_ops
= {
228 .set_params
= fe_set_params
,
229 .standby
= fe_standby
,
230 .has_signal
= fe_has_signal
,
231 .set_config
= fe_set_config
,
232 .tuner_status
= tuner_status
236 * Functions that are common to both TV and radio
239 static void set_tv_freq(struct i2c_client
*c
, unsigned int freq
);
240 static void set_radio_freq(struct i2c_client
*c
, unsigned int freq
);
241 static const struct v4l2_subdev_ops tuner_ops
;
243 static void set_type(struct i2c_client
*c
, unsigned int type
,
244 unsigned int new_mode_mask
, unsigned int new_config
,
245 int (*tuner_callback
) (void *dev
, int component
, int cmd
, int arg
))
247 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
248 struct dvb_tuner_ops
*fe_tuner_ops
= &t
->fe
.ops
.tuner_ops
;
249 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
250 unsigned char buffer
[4];
253 if (type
== UNSET
|| type
== TUNER_ABSENT
) {
254 tuner_dbg("tuner 0x%02x: Tuner type absent\n", c
->addr
);
259 /* prevent invalid config values */
260 t
->config
= new_config
< 256 ? new_config
: 0;
261 if (tuner_callback
!= NULL
) {
262 tuner_dbg("defining GPIO callback\n");
263 t
->fe
.callback
= tuner_callback
;
266 /* discard private data, in case set_type() was previously called */
267 tuner_detach(&t
->fe
);
268 t
->fe
.analog_demod_priv
= NULL
;
272 if (!dvb_attach(microtune_attach
,
273 &t
->fe
, t
->i2c
->adapter
, t
->i2c
->addr
))
276 case TUNER_PHILIPS_TDA8290
:
278 struct tda829x_config cfg
= {
279 .lna_cfg
= t
->config
,
281 if (!dvb_attach(tda829x_attach
, &t
->fe
, t
->i2c
->adapter
,
287 if (!dvb_attach(tea5767_attach
, &t
->fe
,
288 t
->i2c
->adapter
, t
->i2c
->addr
))
290 t
->mode_mask
= T_RADIO
;
293 if (!dvb_attach(tea5761_attach
, &t
->fe
,
294 t
->i2c
->adapter
, t
->i2c
->addr
))
296 t
->mode_mask
= T_RADIO
;
298 case TUNER_PHILIPS_FMD1216ME_MK3
:
303 i2c_master_send(c
, buffer
, 4);
307 i2c_master_send(c
, buffer
, 4);
308 if (!dvb_attach(simple_tuner_attach
, &t
->fe
,
309 t
->i2c
->adapter
, t
->i2c
->addr
, t
->type
))
312 case TUNER_PHILIPS_TD1316
:
317 i2c_master_send(c
, buffer
, 4);
318 if (!dvb_attach(simple_tuner_attach
, &t
->fe
,
319 t
->i2c
->adapter
, t
->i2c
->addr
, t
->type
))
324 struct xc2028_config cfg
= {
325 .i2c_adap
= t
->i2c
->adapter
,
326 .i2c_addr
= t
->i2c
->addr
,
328 if (!dvb_attach(xc2028_attach
, &t
->fe
, &cfg
))
334 if (!dvb_attach(tda9887_attach
,
335 &t
->fe
, t
->i2c
->adapter
, t
->i2c
->addr
))
340 struct xc5000_config xc5000_cfg
= {
341 .i2c_address
= t
->i2c
->addr
,
342 /* if_khz will be set at dvb_attach() */
346 if (!dvb_attach(xc5000_attach
,
347 &t
->fe
, t
->i2c
->adapter
, &xc5000_cfg
))
352 case TUNER_NXP_TDA18271
:
354 struct tda18271_config cfg
= {
356 .small_i2c
= TDA18271_03_BYTE_CHUNK_INIT
,
359 if (!dvb_attach(tda18271_attach
, &t
->fe
, t
->i2c
->addr
,
360 t
->i2c
->adapter
, &cfg
))
366 if (!dvb_attach(simple_tuner_attach
, &t
->fe
,
367 t
->i2c
->adapter
, t
->i2c
->addr
, t
->type
))
373 if ((NULL
== analog_ops
->set_params
) &&
374 (fe_tuner_ops
->set_analog_params
)) {
376 t
->name
= fe_tuner_ops
->info
.name
;
378 t
->fe
.analog_demod_priv
= t
;
379 memcpy(analog_ops
, &tuner_analog_ops
,
380 sizeof(struct analog_demod_ops
));
383 t
->name
= analog_ops
->info
.name
;
386 tuner_dbg("type set to %s\n", t
->name
);
388 t
->mode_mask
= new_mode_mask
;
390 /* Some tuners require more initialization setup before use,
391 such as firmware download or device calibration.
392 trying to set a frequency here will just fail
393 FIXME: better to move set_freq to the tuner code. This is needed
394 on analog tuners for PLL to properly work
397 if (V4L2_TUNER_RADIO
== t
->mode
)
398 set_radio_freq(c
, t
->radio_freq
);
400 set_tv_freq(c
, t
->tv_freq
);
403 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
404 c
->adapter
->name
, c
->driver
->driver
.name
, c
->addr
<< 1, type
,
409 tuner_dbg("Tuner attach for type = %d failed.\n", t
->type
);
410 t
->type
= TUNER_ABSENT
;
416 * tuner_s_type_addr - Sets the tuner type for a device
418 * @sd: subdev descriptor
419 * @tun_setup: type to be associated to a given tuner i2c address
421 * This function applys the tuner config to tuner specified
422 * by tun_setup structure.
423 * If tuner I2C address is UNSET, then it will only set the device
424 * if the tuner supports the mode specified in the call.
425 * If the address is specified, the change will be applied only if
426 * tuner I2C address matches.
427 * The call can change the tuner number and the tuner mode.
429 static int tuner_s_type_addr(struct v4l2_subdev
*sd
,
430 struct tuner_setup
*tun_setup
)
432 struct tuner
*t
= to_tuner(sd
);
433 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
435 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
438 tun_setup
->mode_mask
,
441 if ((t
->type
== UNSET
&& ((tun_setup
->addr
== ADDR_UNSET
) &&
442 (t
->mode_mask
& tun_setup
->mode_mask
))) ||
443 (tun_setup
->addr
== c
->addr
)) {
444 set_type(c
, tun_setup
->type
, tun_setup
->mode_mask
,
445 tun_setup
->config
, tun_setup
->tuner_callback
);
447 tuner_dbg("set addr discarded for type %i, mask %x. "
448 "Asked to change tuner at addr 0x%02x, with mask %x\n",
449 t
->type
, t
->mode_mask
,
450 tun_setup
->addr
, tun_setup
->mode_mask
);
455 static int tuner_s_config(struct v4l2_subdev
*sd
,
456 const struct v4l2_priv_tun_config
*cfg
)
458 struct tuner
*t
= to_tuner(sd
);
459 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
461 if (t
->type
!= cfg
->tuner
)
464 if (analog_ops
->set_config
) {
465 analog_ops
->set_config(&t
->fe
, cfg
->priv
);
469 tuner_dbg("Tuner frontend module has no way to set config\n");
473 /* Search for existing radio and/or TV tuners on the given I2C adapter.
474 Note that when this function is called from tuner_probe you can be
475 certain no other devices will be added/deleted at the same time, I2C
476 core protects against that. */
477 static void tuner_lookup(struct i2c_adapter
*adap
,
478 struct tuner
**radio
, struct tuner
**tv
)
485 list_for_each_entry(pos
, &tuner_list
, list
) {
488 if (pos
->i2c
->adapter
!= adap
||
489 strcmp(pos
->i2c
->driver
->driver
.name
, "tuner"))
492 mode_mask
= pos
->mode_mask
;
494 if (*radio
== NULL
&& mode_mask
== T_RADIO
)
496 /* Note: currently TDA9887 is the only demod-only
497 device. If other devices appear then we need to
498 make this test more general. */
499 else if (*tv
== NULL
&& pos
->type
!= TUNER_TDA9887
&&
500 (pos
->mode_mask
& (T_ANALOG_TV
| T_DIGITAL_TV
)))
505 /* During client attach, set_type is called by adapter's attach_inform callback.
506 set_type must then be completed by tuner_probe.
508 static int tuner_probe(struct i2c_client
*client
,
509 const struct i2c_device_id
*id
)
515 t
= kzalloc(sizeof(struct tuner
), GFP_KERNEL
);
518 v4l2_i2c_subdev_init(&t
->sd
, client
, &tuner_ops
);
520 t
->name
= "(tuner unset)";
522 t
->audmode
= V4L2_TUNER_MODE_STEREO
;
524 t
->radio_freq
= 87.5 * 16000; /* Initial freq range */
525 t
->tv_freq
= 400 * 16; /* Sets freq to VHF High - needed for some PLL's to properly start */
528 unsigned char buffer
[16];
531 memset(buffer
, 0, sizeof(buffer
));
532 rc
= i2c_master_recv(client
, buffer
, sizeof(buffer
));
533 tuner_info("I2C RECV = ");
534 for (i
= 0; i
< rc
; i
++)
535 printk(KERN_CONT
"%02x ", buffer
[i
]);
539 /* autodetection code based on the i2c addr */
540 if (!no_autodetect
) {
541 switch (client
->addr
) {
543 if (tuner_symbol_probe(tea5761_autodetection
,
545 t
->i2c
->addr
) >= 0) {
546 t
->type
= TUNER_TEA5761
;
547 t
->mode_mask
= T_RADIO
;
548 tuner_lookup(t
->i2c
->adapter
, &radio
, &tv
);
550 tv
->mode_mask
&= ~T_RADIO
;
552 goto register_client
;
560 /* If chip is not tda8290, don't register.
561 since it can be tda9887*/
562 if (tuner_symbol_probe(tda829x_probe
, t
->i2c
->adapter
,
563 t
->i2c
->addr
) >= 0) {
564 tuner_dbg("tda829x detected\n");
566 /* Default is being tda9887 */
567 t
->type
= TUNER_TDA9887
;
568 t
->mode_mask
= T_RADIO
| T_ANALOG_TV
|
570 goto register_client
;
574 if (tuner_symbol_probe(tea5767_autodetection
,
575 t
->i2c
->adapter
, t
->i2c
->addr
)
577 t
->type
= TUNER_TEA5767
;
578 t
->mode_mask
= T_RADIO
;
579 /* Sets freq to FM range */
580 tuner_lookup(t
->i2c
->adapter
, &radio
, &tv
);
582 tv
->mode_mask
&= ~T_RADIO
;
584 goto register_client
;
590 /* Initializes only the first TV tuner on this adapter. Why only the
591 first? Because there are some devices (notably the ones with TI
592 tuners) that have more than one i2c address for the *same* device.
593 Experience shows that, except for just one case, the first
594 address is the right one. The exception is a Russian tuner
595 (ACORP_Y878F). So, the desired behavior is just to enable the
596 first found TV tuner. */
597 tuner_lookup(t
->i2c
->adapter
, &radio
, &tv
);
599 t
->mode_mask
= T_ANALOG_TV
| T_DIGITAL_TV
;
601 t
->mode_mask
|= T_RADIO
;
602 tuner_dbg("Setting mode_mask to 0x%02x\n", t
->mode_mask
);
605 /* Should be just before return */
607 /* Sets a default mode */
608 if (t
->mode_mask
& T_ANALOG_TV
)
609 t
->mode
= V4L2_TUNER_ANALOG_TV
;
610 else if (t
->mode_mask
& T_RADIO
)
611 t
->mode
= V4L2_TUNER_RADIO
;
613 t
->mode
= V4L2_TUNER_DIGITAL_TV
;
614 set_type(client
, t
->type
, t
->mode_mask
, t
->config
, t
->fe
.callback
);
615 list_add_tail(&t
->list
, &tuner_list
);
617 tuner_info("Tuner %d found with type(s)%s%s%s.\n",
619 t
->mode_mask
& T_RADIO
? " radio" : "",
620 t
->mode_mask
& T_ANALOG_TV
? " TV" : "",
621 t
->mode_mask
& T_ANALOG_TV
? " DTV" : "");
625 static int tuner_remove(struct i2c_client
*client
)
627 struct tuner
*t
= to_tuner(i2c_get_clientdata(client
));
629 v4l2_device_unregister_subdev(&t
->sd
);
630 tuner_detach(&t
->fe
);
631 t
->fe
.analog_demod_priv
= NULL
;
639 * Functions that are specific for TV mode
642 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
643 static void set_tv_freq(struct i2c_client
*c
, unsigned int freq
)
645 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
646 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
648 struct analog_parameters params
= {
650 .audmode
= t
->audmode
,
654 if (t
->type
== UNSET
) {
655 tuner_warn("tuner type not set\n");
658 if (NULL
== analog_ops
->set_params
) {
659 tuner_warn("Tuner has no way to set tv freq\n");
662 if (freq
< tv_range
[0] * 16 || freq
> tv_range
[1] * 16) {
663 tuner_dbg("TV freq (%d.%02d) out of range (%d-%d)\n",
664 freq
/ 16, freq
% 16 * 100 / 16, tv_range
[0],
666 /* V4L2 spec: if the freq is not possible then the closest
667 possible value should be selected */
668 if (freq
< tv_range
[0] * 16)
669 freq
= tv_range
[0] * 16;
671 freq
= tv_range
[1] * 16;
673 params
.frequency
= freq
;
674 tuner_dbg("tv freq set to %d.%02d\n",
675 freq
/ 16, freq
% 16 * 100 / 16);
679 analog_ops
->set_params(&t
->fe
, ¶ms
);
682 /* get more precise norm info from insmod option */
683 static int tuner_fixup_std(struct tuner
*t
)
685 if ((t
->std
& V4L2_STD_PAL
) == V4L2_STD_PAL
) {
688 tuner_dbg("insmod fixup: PAL => PAL-60\n");
689 t
->std
= V4L2_STD_PAL_60
;
695 tuner_dbg("insmod fixup: PAL => PAL-BG\n");
696 t
->std
= V4L2_STD_PAL_BG
;
700 tuner_dbg("insmod fixup: PAL => PAL-I\n");
701 t
->std
= V4L2_STD_PAL_I
;
707 tuner_dbg("insmod fixup: PAL => PAL-DK\n");
708 t
->std
= V4L2_STD_PAL_DK
;
712 tuner_dbg("insmod fixup: PAL => PAL-M\n");
713 t
->std
= V4L2_STD_PAL_M
;
717 if (pal
[1] == 'c' || pal
[1] == 'C') {
718 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
719 t
->std
= V4L2_STD_PAL_Nc
;
721 tuner_dbg("insmod fixup: PAL => PAL-N\n");
722 t
->std
= V4L2_STD_PAL_N
;
726 /* default parameter, do nothing */
729 tuner_warn("pal= argument not recognised\n");
733 if ((t
->std
& V4L2_STD_SECAM
) == V4L2_STD_SECAM
) {
741 tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
742 t
->std
= V4L2_STD_SECAM_B
|
750 tuner_dbg("insmod fixup: SECAM => SECAM-DK\n");
751 t
->std
= V4L2_STD_SECAM_DK
;
755 if ((secam
[1] == 'C') || (secam
[1] == 'c')) {
756 tuner_dbg("insmod fixup: SECAM => SECAM-L'\n");
757 t
->std
= V4L2_STD_SECAM_LC
;
759 tuner_dbg("insmod fixup: SECAM => SECAM-L\n");
760 t
->std
= V4L2_STD_SECAM_L
;
764 /* default parameter, do nothing */
767 tuner_warn("secam= argument not recognised\n");
772 if ((t
->std
& V4L2_STD_NTSC
) == V4L2_STD_NTSC
) {
776 tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
777 t
->std
= V4L2_STD_NTSC_M
;
781 tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
782 t
->std
= V4L2_STD_NTSC_M_JP
;
786 tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
787 t
->std
= V4L2_STD_NTSC_M_KR
;
790 /* default parameter, do nothing */
793 tuner_info("ntsc= argument not recognised\n");
801 * Functions that are specific for Radio mode
804 static void set_radio_freq(struct i2c_client
*c
, unsigned int freq
)
806 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
807 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
809 struct analog_parameters params
= {
811 .audmode
= t
->audmode
,
815 if (t
->type
== UNSET
) {
816 tuner_warn("tuner type not set\n");
819 if (NULL
== analog_ops
->set_params
) {
820 tuner_warn("tuner has no way to set radio frequency\n");
823 if (freq
< radio_range
[0] * 16000 || freq
> radio_range
[1] * 16000) {
824 tuner_dbg("radio freq (%d.%02d) out of range (%d-%d)\n",
825 freq
/ 16000, freq
% 16000 * 100 / 16000,
826 radio_range
[0], radio_range
[1]);
827 /* V4L2 spec: if the freq is not possible then the closest
828 possible value should be selected */
829 if (freq
< radio_range
[0] * 16000)
830 freq
= radio_range
[0] * 16000;
832 freq
= radio_range
[1] * 16000;
834 params
.frequency
= freq
;
835 tuner_dbg("radio freq set to %d.%02d\n",
836 freq
/ 16000, freq
% 16000 * 100 / 16000);
837 t
->radio_freq
= freq
;
840 analog_ops
->set_params(&t
->fe
, ¶ms
);
844 * check_mode - Verify if tuner supports the requested mode
845 * @t: a pointer to the module's internal struct_tuner
847 * This function checks if the tuner is capable of tuning analog TV,
848 * digital TV or radio, depending on what the caller wants. If the
849 * tuner can't support that mode, it returns -EINVAL. Otherwise, it
851 * This function is needed for boards that have a separate tuner for
852 * radio (like devices with tea5767).
854 static inline int check_mode(struct tuner
*t
, enum v4l2_tuner_type mode
)
856 if ((1 << mode
& t
->mode_mask
) == 0)
863 * set_mode_freq - Switch tuner to other mode.
864 * @client: struct i2c_client pointer
865 * @t: a pointer to the module's internal struct_tuner
866 * @mode: enum v4l2_type (radio or TV)
867 * @freq: frequency to set (0 means to use the previous one)
869 * If tuner doesn't support the needed mode (radio or TV), prints a
870 * debug message and returns -EINVAL, changing internal state to T_STANDBY.
871 * Otherwise, changes the state and sets frequency to the last value, if
872 * the tuner can sleep or if it supports both Radio and TV.
874 static int set_mode_freq(struct i2c_client
*client
, struct tuner
*t
,
875 enum v4l2_tuner_type mode
, unsigned int freq
)
877 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
879 if (mode
!= t
->mode
) {
880 if (check_mode(t
, mode
) == -EINVAL
) {
881 tuner_dbg("Tuner doesn't support mode %d. "
882 "Putting tuner to sleep\n", mode
);
884 if (analog_ops
->standby
)
885 analog_ops
->standby(&t
->fe
);
889 tuner_dbg("Changing to mode %d\n", mode
);
891 if (t
->mode
== V4L2_TUNER_RADIO
) {
893 t
->radio_freq
= freq
;
894 set_radio_freq(client
, t
->radio_freq
);
898 set_tv_freq(client
, t
->tv_freq
);
905 * tuner_status - Dumps the current tuner status at dmesg
906 * @fe: pointer to struct dvb_frontend
908 * This callback is used only for driver debug purposes, answering to
909 * VIDIOC_LOG_STATUS. No changes should happen on this call.
911 static void tuner_status(struct dvb_frontend
*fe
)
913 struct tuner
*t
= fe
->analog_demod_priv
;
914 unsigned long freq
, freq_fraction
;
915 struct dvb_tuner_ops
*fe_tuner_ops
= &fe
->ops
.tuner_ops
;
916 struct analog_demod_ops
*analog_ops
= &fe
->ops
.analog_ops
;
920 case V4L2_TUNER_RADIO
:
923 case V4L2_TUNER_DIGITAL_TV
:
926 case V4L2_TUNER_ANALOG_TV
:
931 if (t
->mode
== V4L2_TUNER_RADIO
) {
932 freq
= t
->radio_freq
/ 16000;
933 freq_fraction
= (t
->radio_freq
% 16000) * 100 / 16000;
935 freq
= t
->tv_freq
/ 16;
936 freq_fraction
= (t
->tv_freq
% 16) * 100 / 16;
938 tuner_info("Tuner mode: %s%s\n", p
,
939 t
->standby
? " on standby mode" : "");
940 tuner_info("Frequency: %lu.%02lu MHz\n", freq
, freq_fraction
);
941 tuner_info("Standard: 0x%08lx\n", (unsigned long)t
->std
);
942 if (t
->mode
!= V4L2_TUNER_RADIO
)
944 if (fe_tuner_ops
->get_status
) {
947 fe_tuner_ops
->get_status(&t
->fe
, &tuner_status
);
948 if (tuner_status
& TUNER_STATUS_LOCKED
)
949 tuner_info("Tuner is locked.\n");
950 if (tuner_status
& TUNER_STATUS_STEREO
)
951 tuner_info("Stereo: yes\n");
953 if (analog_ops
->has_signal
)
954 tuner_info("Signal strength: %d\n",
955 analog_ops
->has_signal(fe
));
959 * tuner_s_power - controls the power state of the tuner
960 * @sd: pointer to struct v4l2_subdev
961 * @on: a zero value puts the tuner to sleep
963 static int tuner_s_power(struct v4l2_subdev
*sd
, int on
)
965 struct tuner
*t
= to_tuner(sd
);
966 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
968 /* FIXME: Why this function don't wake the tuner if on != 0 ? */
972 tuner_dbg("Putting tuner to sleep\n");
974 if (analog_ops
->standby
)
975 analog_ops
->standby(&t
->fe
);
979 /* ---------------------------------------------------------------------- */
981 static int tuner_s_radio(struct v4l2_subdev
*sd
)
983 struct tuner
*t
= to_tuner(sd
);
984 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
986 if (set_mode_freq(client
, t
, V4L2_TUNER_RADIO
, 0) == -EINVAL
)
991 /* --- v4l ioctls --- */
992 /* take care: bttv does userspace copying, we'll get a
993 kernel pointer here... */
994 static int tuner_s_std(struct v4l2_subdev
*sd
, v4l2_std_id std
)
996 struct tuner
*t
= to_tuner(sd
);
997 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
999 if (set_mode_freq(client
, t
, V4L2_TUNER_ANALOG_TV
, 0) == -EINVAL
)
1008 static int tuner_s_frequency(struct v4l2_subdev
*sd
, struct v4l2_frequency
*f
)
1010 struct tuner
*t
= to_tuner(sd
);
1011 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1013 if (set_mode_freq(client
, t
, f
->type
, f
->frequency
) == -EINVAL
)
1019 static int tuner_g_frequency(struct v4l2_subdev
*sd
, struct v4l2_frequency
*f
)
1021 struct tuner
*t
= to_tuner(sd
);
1022 struct dvb_tuner_ops
*fe_tuner_ops
= &t
->fe
.ops
.tuner_ops
;
1024 if (check_mode(t
, f
->type
) == -EINVAL
)
1027 if (fe_tuner_ops
->get_frequency
&& !t
->standby
) {
1030 fe_tuner_ops
->get_frequency(&t
->fe
, &abs_freq
);
1031 f
->frequency
= (V4L2_TUNER_RADIO
== t
->mode
) ?
1032 DIV_ROUND_CLOSEST(abs_freq
* 2, 125) :
1033 DIV_ROUND_CLOSEST(abs_freq
, 62500);
1035 f
->frequency
= (V4L2_TUNER_RADIO
== t
->mode
) ?
1036 t
->radio_freq
: t
->tv_freq
;
1041 static int tuner_g_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*vt
)
1043 struct tuner
*t
= to_tuner(sd
);
1044 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
1045 struct dvb_tuner_ops
*fe_tuner_ops
= &t
->fe
.ops
.tuner_ops
;
1047 if (check_mode(t
, vt
->type
) == -EINVAL
)
1050 if (analog_ops
->get_afc
)
1051 vt
->afc
= analog_ops
->get_afc(&t
->fe
);
1052 if (t
->mode
== V4L2_TUNER_ANALOG_TV
)
1053 vt
->capability
|= V4L2_TUNER_CAP_NORM
;
1054 if (t
->mode
!= V4L2_TUNER_RADIO
) {
1055 vt
->rangelow
= tv_range
[0] * 16;
1056 vt
->rangehigh
= tv_range
[1] * 16;
1061 vt
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
1062 if (fe_tuner_ops
->get_status
) {
1065 fe_tuner_ops
->get_status(&t
->fe
, &tuner_status
);
1067 (tuner_status
& TUNER_STATUS_STEREO
) ?
1068 V4L2_TUNER_SUB_STEREO
:
1069 V4L2_TUNER_SUB_MONO
;
1071 if (analog_ops
->has_signal
)
1072 vt
->signal
= analog_ops
->has_signal(&t
->fe
);
1073 vt
->capability
|= V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_STEREO
;
1074 vt
->audmode
= t
->audmode
;
1075 vt
->rangelow
= radio_range
[0] * 16000;
1076 vt
->rangehigh
= radio_range
[1] * 16000;
1081 static int tuner_s_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*vt
)
1083 struct tuner
*t
= to_tuner(sd
);
1084 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1086 if (set_mode_freq(client
, t
, vt
->type
, 0) == -EINVAL
)
1089 if (t
->mode
== V4L2_TUNER_RADIO
)
1090 t
->audmode
= vt
->audmode
;
1095 static int tuner_log_status(struct v4l2_subdev
*sd
)
1097 struct tuner
*t
= to_tuner(sd
);
1098 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
1100 if (analog_ops
->tuner_status
)
1101 analog_ops
->tuner_status(&t
->fe
);
1105 static int tuner_suspend(struct i2c_client
*c
, pm_message_t state
)
1107 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
1108 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
1110 tuner_dbg("suspend\n");
1112 if (!t
->standby
&& analog_ops
->standby
)
1113 analog_ops
->standby(&t
->fe
);
1118 static int tuner_resume(struct i2c_client
*c
)
1120 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
1122 tuner_dbg("resume\n");
1125 set_mode_freq(c
, t
, t
->type
, 0);
1130 static int tuner_command(struct i2c_client
*client
, unsigned cmd
, void *arg
)
1132 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1134 /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
1136 There must be a better way of doing this... */
1138 case TUNER_SET_CONFIG
:
1139 return tuner_s_config(sd
, arg
);
1141 return -ENOIOCTLCMD
;
1144 /* ----------------------------------------------------------------------- */
1146 static const struct v4l2_subdev_core_ops tuner_core_ops
= {
1147 .log_status
= tuner_log_status
,
1148 .s_std
= tuner_s_std
,
1149 .s_power
= tuner_s_power
,
1152 static const struct v4l2_subdev_tuner_ops tuner_tuner_ops
= {
1153 .s_radio
= tuner_s_radio
,
1154 .g_tuner
= tuner_g_tuner
,
1155 .s_tuner
= tuner_s_tuner
,
1156 .s_frequency
= tuner_s_frequency
,
1157 .g_frequency
= tuner_g_frequency
,
1158 .s_type_addr
= tuner_s_type_addr
,
1159 .s_config
= tuner_s_config
,
1162 static const struct v4l2_subdev_ops tuner_ops
= {
1163 .core
= &tuner_core_ops
,
1164 .tuner
= &tuner_tuner_ops
,
1167 /* ----------------------------------------------------------------------- */
1169 /* This driver supports many devices and the idea is to let the driver
1170 detect which device is present. So rather than listing all supported
1171 devices here, we pretend to support a single, fake device type. */
1172 static const struct i2c_device_id tuner_id
[] = {
1173 { "tuner", }, /* autodetect */
1176 MODULE_DEVICE_TABLE(i2c
, tuner_id
);
1178 static struct i2c_driver tuner_driver
= {
1180 .owner
= THIS_MODULE
,
1183 .probe
= tuner_probe
,
1184 .remove
= tuner_remove
,
1185 .command
= tuner_command
,
1186 .suspend
= tuner_suspend
,
1187 .resume
= tuner_resume
,
1188 .id_table
= tuner_id
,
1191 static __init
int init_tuner(void)
1193 return i2c_add_driver(&tuner_driver
);
1196 static __exit
void exit_tuner(void)
1198 i2c_del_driver(&tuner_driver
);
1201 module_init(init_tuner
);
1202 module_exit(exit_tuner
);
1204 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
1205 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1206 MODULE_LICENSE("GPL");