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>
23 #include <media/v4l2-i2c-drv.h>
28 #include "tuner-xc2028.h"
29 #include "tuner-simple.h"
36 #define PREFIX t->i2c->driver->driver.name
38 /** This macro allows us to probe dynamically, avoiding static links */
39 #ifdef CONFIG_MEDIA_ATTACH
40 #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
42 typeof(&FUNCTION) __a = symbol_request(FUNCTION); \
44 __r = (int) __a(ARGS); \
45 symbol_put(FUNCTION); \
47 printk(KERN_ERR "TUNER: Unable to find " \
48 "symbol "#FUNCTION"()\n"); \
53 static void tuner_detach(struct dvb_frontend
*fe
)
55 if (fe
->ops
.tuner_ops
.release
) {
56 fe
->ops
.tuner_ops
.release(fe
);
57 symbol_put_addr(fe
->ops
.tuner_ops
.release
);
59 if (fe
->ops
.analog_ops
.release
) {
60 fe
->ops
.analog_ops
.release(fe
);
61 symbol_put_addr(fe
->ops
.analog_ops
.release
);
65 #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
69 static void tuner_detach(struct dvb_frontend
*fe
)
71 if (fe
->ops
.tuner_ops
.release
)
72 fe
->ops
.tuner_ops
.release(fe
);
73 if (fe
->ops
.analog_ops
.release
)
74 fe
->ops
.analog_ops
.release(fe
);
80 struct dvb_frontend fe
;
81 struct i2c_client
*i2c
;
82 struct v4l2_subdev sd
;
83 struct list_head list
;
84 unsigned int using_v4l2
:1;
86 /* keep track of the current settings */
89 unsigned int radio_freq
;
93 unsigned int mode_mask
; /* Combination of allowable modes */
95 unsigned int type
; /* chip type id */
100 static inline struct tuner
*to_tuner(struct v4l2_subdev
*sd
)
102 return container_of(sd
, struct tuner
, sd
);
106 /* insmod options used at init time => read/only */
107 static unsigned int addr
;
108 static unsigned int no_autodetect
;
109 static unsigned int show_i2c
;
111 /* insmod options used at runtime => read/write */
112 static int tuner_debug
;
114 #define tuner_warn(fmt, arg...) do { \
115 printk(KERN_WARNING "%s %d-%04x: " fmt, PREFIX, \
116 i2c_adapter_id(t->i2c->adapter), \
117 t->i2c->addr, ##arg); \
120 #define tuner_info(fmt, arg...) do { \
121 printk(KERN_INFO "%s %d-%04x: " fmt, PREFIX, \
122 i2c_adapter_id(t->i2c->adapter), \
123 t->i2c->addr, ##arg); \
126 #define tuner_err(fmt, arg...) do { \
127 printk(KERN_ERR "%s %d-%04x: " fmt, PREFIX, \
128 i2c_adapter_id(t->i2c->adapter), \
129 t->i2c->addr, ##arg); \
132 #define tuner_dbg(fmt, arg...) do { \
134 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX, \
135 i2c_adapter_id(t->i2c->adapter), \
136 t->i2c->addr, ##arg); \
139 /* ------------------------------------------------------------------------ */
141 static unsigned int tv_range
[2] = { 44, 958 };
142 static unsigned int radio_range
[2] = { 65, 108 };
144 static char pal
[] = "--";
145 static char secam
[] = "--";
146 static char ntsc
[] = "-";
149 module_param(addr
, int, 0444);
150 module_param(no_autodetect
, int, 0444);
151 module_param(show_i2c
, int, 0444);
152 module_param_named(debug
,tuner_debug
, int, 0644);
153 module_param_string(pal
, pal
, sizeof(pal
), 0644);
154 module_param_string(secam
, secam
, sizeof(secam
), 0644);
155 module_param_string(ntsc
, ntsc
, sizeof(ntsc
), 0644);
156 module_param_array(tv_range
, int, NULL
, 0644);
157 module_param_array(radio_range
, int, NULL
, 0644);
159 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
160 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
161 MODULE_LICENSE("GPL");
163 /* ---------------------------------------------------------------------- */
165 static void fe_set_params(struct dvb_frontend
*fe
,
166 struct analog_parameters
*params
)
168 struct dvb_tuner_ops
*fe_tuner_ops
= &fe
->ops
.tuner_ops
;
169 struct tuner
*t
= fe
->analog_demod_priv
;
171 if (NULL
== fe_tuner_ops
->set_analog_params
) {
172 tuner_warn("Tuner frontend module has no way to set freq\n");
175 fe_tuner_ops
->set_analog_params(fe
, params
);
178 static void fe_standby(struct dvb_frontend
*fe
)
180 struct dvb_tuner_ops
*fe_tuner_ops
= &fe
->ops
.tuner_ops
;
182 if (fe_tuner_ops
->sleep
)
183 fe_tuner_ops
->sleep(fe
);
186 static int fe_has_signal(struct dvb_frontend
*fe
)
190 if (fe
->ops
.tuner_ops
.get_rf_strength
)
191 fe
->ops
.tuner_ops
.get_rf_strength(fe
, &strength
);
196 static int fe_set_config(struct dvb_frontend
*fe
, void *priv_cfg
)
198 struct dvb_tuner_ops
*fe_tuner_ops
= &fe
->ops
.tuner_ops
;
199 struct tuner
*t
= fe
->analog_demod_priv
;
201 if (fe_tuner_ops
->set_config
)
202 return fe_tuner_ops
->set_config(fe
, priv_cfg
);
204 tuner_warn("Tuner frontend module has no way to set config\n");
209 static void tuner_status(struct dvb_frontend
*fe
);
211 static struct analog_demod_ops tuner_analog_ops
= {
212 .set_params
= fe_set_params
,
213 .standby
= fe_standby
,
214 .has_signal
= fe_has_signal
,
215 .set_config
= fe_set_config
,
216 .tuner_status
= tuner_status
219 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
220 static void set_tv_freq(struct i2c_client
*c
, unsigned int freq
)
222 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
223 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
225 struct analog_parameters params
= {
227 .audmode
= t
->audmode
,
231 if (t
->type
== UNSET
) {
232 tuner_warn ("tuner type not set\n");
235 if (NULL
== analog_ops
->set_params
) {
236 tuner_warn ("Tuner has no way to set tv freq\n");
239 if (freq
< tv_range
[0] * 16 || freq
> tv_range
[1] * 16) {
240 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
241 freq
/ 16, freq
% 16 * 100 / 16, tv_range
[0],
243 /* V4L2 spec: if the freq is not possible then the closest
244 possible value should be selected */
245 if (freq
< tv_range
[0] * 16)
246 freq
= tv_range
[0] * 16;
248 freq
= tv_range
[1] * 16;
250 params
.frequency
= freq
;
252 analog_ops
->set_params(&t
->fe
, ¶ms
);
255 static void set_radio_freq(struct i2c_client
*c
, unsigned int freq
)
257 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
258 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
260 struct analog_parameters params
= {
262 .audmode
= t
->audmode
,
266 if (t
->type
== UNSET
) {
267 tuner_warn ("tuner type not set\n");
270 if (NULL
== analog_ops
->set_params
) {
271 tuner_warn ("tuner has no way to set radio frequency\n");
274 if (freq
< radio_range
[0] * 16000 || freq
> radio_range
[1] * 16000) {
275 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
276 freq
/ 16000, freq
% 16000 * 100 / 16000,
277 radio_range
[0], radio_range
[1]);
278 /* V4L2 spec: if the freq is not possible then the closest
279 possible value should be selected */
280 if (freq
< radio_range
[0] * 16000)
281 freq
= radio_range
[0] * 16000;
283 freq
= radio_range
[1] * 16000;
285 params
.frequency
= freq
;
287 analog_ops
->set_params(&t
->fe
, ¶ms
);
290 static void set_freq(struct i2c_client
*c
, unsigned long freq
)
292 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
295 case V4L2_TUNER_RADIO
:
296 tuner_dbg("radio freq set to %lu.%02lu\n",
297 freq
/ 16000, freq
% 16000 * 100 / 16000);
298 set_radio_freq(c
, freq
);
299 t
->radio_freq
= freq
;
301 case V4L2_TUNER_ANALOG_TV
:
302 case V4L2_TUNER_DIGITAL_TV
:
303 tuner_dbg("tv freq set to %lu.%02lu\n",
304 freq
/ 16, freq
% 16 * 100 / 16);
305 set_tv_freq(c
, freq
);
309 tuner_dbg("freq set: unknown mode: 0x%04x!\n",t
->mode
);
313 static struct xc5000_config xc5000_cfg
;
315 static void set_type(struct i2c_client
*c
, unsigned int type
,
316 unsigned int new_mode_mask
, unsigned int new_config
,
317 int (*tuner_callback
) (void *dev
, int component
, int cmd
, int arg
))
319 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
320 struct dvb_tuner_ops
*fe_tuner_ops
= &t
->fe
.ops
.tuner_ops
;
321 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
322 unsigned char buffer
[4];
324 if (type
== UNSET
|| type
== TUNER_ABSENT
) {
325 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c
->addr
);
330 /* prevent invalid config values */
331 t
->config
= ((new_config
>= 0) && (new_config
< 256)) ? new_config
: 0;
332 if (tuner_callback
!= NULL
) {
333 tuner_dbg("defining GPIO callback\n");
334 t
->fe
.callback
= tuner_callback
;
337 if (t
->mode
== T_UNINITIALIZED
) {
338 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c
->addr
);
343 /* discard private data, in case set_type() was previously called */
344 tuner_detach(&t
->fe
);
345 t
->fe
.analog_demod_priv
= NULL
;
349 if (!dvb_attach(microtune_attach
,
350 &t
->fe
, t
->i2c
->adapter
, t
->i2c
->addr
))
353 case TUNER_PHILIPS_TDA8290
:
355 struct tda829x_config cfg
= {
356 .lna_cfg
= t
->config
,
358 if (!dvb_attach(tda829x_attach
, &t
->fe
, t
->i2c
->adapter
,
364 if (!dvb_attach(tea5767_attach
, &t
->fe
,
365 t
->i2c
->adapter
, t
->i2c
->addr
))
367 t
->mode_mask
= T_RADIO
;
370 if (!dvb_attach(tea5761_attach
, &t
->fe
,
371 t
->i2c
->adapter
, t
->i2c
->addr
))
373 t
->mode_mask
= T_RADIO
;
375 case TUNER_PHILIPS_FMD1216ME_MK3
:
380 i2c_master_send(c
, buffer
, 4);
384 i2c_master_send(c
, buffer
, 4);
385 if (!dvb_attach(simple_tuner_attach
, &t
->fe
,
386 t
->i2c
->adapter
, t
->i2c
->addr
, t
->type
))
389 case TUNER_PHILIPS_TD1316
:
394 i2c_master_send(c
, buffer
, 4);
395 if (!dvb_attach(simple_tuner_attach
, &t
->fe
,
396 t
->i2c
->adapter
, t
->i2c
->addr
, t
->type
))
401 struct xc2028_config cfg
= {
402 .i2c_adap
= t
->i2c
->adapter
,
403 .i2c_addr
= t
->i2c
->addr
,
405 if (!dvb_attach(xc2028_attach
, &t
->fe
, &cfg
))
410 if (!dvb_attach(tda9887_attach
,
411 &t
->fe
, t
->i2c
->adapter
, t
->i2c
->addr
))
416 xc5000_cfg
.i2c_address
= t
->i2c
->addr
;
417 /* if_khz will be set when the digital dvb_attach() occurs */
418 xc5000_cfg
.if_khz
= 0;
419 if (!dvb_attach(xc5000_attach
,
420 &t
->fe
, t
->i2c
->adapter
, &xc5000_cfg
))
424 case TUNER_NXP_TDA18271
:
426 struct tda18271_config cfg
= {
430 if (!dvb_attach(tda18271_attach
, &t
->fe
, t
->i2c
->addr
,
431 t
->i2c
->adapter
, &cfg
))
436 if (!dvb_attach(simple_tuner_attach
, &t
->fe
,
437 t
->i2c
->adapter
, t
->i2c
->addr
, t
->type
))
443 if ((NULL
== analog_ops
->set_params
) &&
444 (fe_tuner_ops
->set_analog_params
)) {
446 t
->name
= fe_tuner_ops
->info
.name
;
448 t
->fe
.analog_demod_priv
= t
;
449 memcpy(analog_ops
, &tuner_analog_ops
,
450 sizeof(struct analog_demod_ops
));
453 t
->name
= analog_ops
->info
.name
;
456 tuner_dbg("type set to %s\n", t
->name
);
458 if (t
->mode_mask
== T_UNINITIALIZED
)
459 t
->mode_mask
= new_mode_mask
;
461 /* xc2028/3028 and xc5000 requires a firmware to be set-up later
462 trying to set a frequency here will just fail
463 FIXME: better to move set_freq to the tuner code. This is needed
464 on analog tuners for PLL to properly work
466 if (t
->type
!= TUNER_XC2028
&& t
->type
!= TUNER_XC5000
)
467 set_freq(c
, (V4L2_TUNER_RADIO
== t
->mode
) ?
468 t
->radio_freq
: t
->tv_freq
);
470 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
471 c
->adapter
->name
, c
->driver
->driver
.name
, c
->addr
<< 1, type
,
476 tuner_dbg("Tuner attach for type = %d failed.\n", t
->type
);
477 t
->type
= TUNER_ABSENT
;
478 t
->mode_mask
= T_UNINITIALIZED
;
484 * This function apply tuner config to tuner specified
485 * by tun_setup structure. I addr is unset, then admin status
486 * and tun addr status is more precise then current status,
487 * it's applied. Otherwise status and type are applied only to
488 * tuner with exactly the same addr.
491 static void set_addr(struct i2c_client
*c
, struct tuner_setup
*tun_setup
)
493 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
495 if ( (t
->type
== UNSET
&& ((tun_setup
->addr
== ADDR_UNSET
) &&
496 (t
->mode_mask
& tun_setup
->mode_mask
))) ||
497 (tun_setup
->addr
== c
->addr
)) {
498 set_type(c
, tun_setup
->type
, tun_setup
->mode_mask
,
499 tun_setup
->config
, tun_setup
->tuner_callback
);
501 tuner_dbg("set addr discarded for type %i, mask %x. "
502 "Asked to change tuner at addr 0x%02x, with mask %x\n",
503 t
->type
, t
->mode_mask
,
504 tun_setup
->addr
, tun_setup
->mode_mask
);
507 static inline int check_mode(struct tuner
*t
, char *cmd
)
509 if ((1 << t
->mode
& t
->mode_mask
) == 0) {
514 case V4L2_TUNER_RADIO
:
515 tuner_dbg("Cmd %s accepted for radio\n", cmd
);
517 case V4L2_TUNER_ANALOG_TV
:
518 tuner_dbg("Cmd %s accepted for analog TV\n", cmd
);
520 case V4L2_TUNER_DIGITAL_TV
:
521 tuner_dbg("Cmd %s accepted for digital TV\n", cmd
);
527 /* get more precise norm info from insmod option */
528 static int tuner_fixup_std(struct tuner
*t
)
530 if ((t
->std
& V4L2_STD_PAL
) == V4L2_STD_PAL
) {
533 tuner_dbg ("insmod fixup: PAL => PAL-60\n");
534 t
->std
= V4L2_STD_PAL_60
;
540 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
541 t
->std
= V4L2_STD_PAL_BG
;
545 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
546 t
->std
= V4L2_STD_PAL_I
;
552 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
553 t
->std
= V4L2_STD_PAL_DK
;
557 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
558 t
->std
= V4L2_STD_PAL_M
;
562 if (pal
[1] == 'c' || pal
[1] == 'C') {
563 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
564 t
->std
= V4L2_STD_PAL_Nc
;
566 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
567 t
->std
= V4L2_STD_PAL_N
;
571 /* default parameter, do nothing */
574 tuner_warn ("pal= argument not recognised\n");
578 if ((t
->std
& V4L2_STD_SECAM
) == V4L2_STD_SECAM
) {
586 tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
587 t
->std
= V4L2_STD_SECAM_B
| V4L2_STD_SECAM_G
| V4L2_STD_SECAM_H
;
593 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
594 t
->std
= V4L2_STD_SECAM_DK
;
598 if ((secam
[1]=='C')||(secam
[1]=='c')) {
599 tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n");
600 t
->std
= V4L2_STD_SECAM_LC
;
602 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
603 t
->std
= V4L2_STD_SECAM_L
;
607 /* default parameter, do nothing */
610 tuner_warn ("secam= argument not recognised\n");
615 if ((t
->std
& V4L2_STD_NTSC
) == V4L2_STD_NTSC
) {
619 tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
620 t
->std
= V4L2_STD_NTSC_M
;
624 tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
625 t
->std
= V4L2_STD_NTSC_M_JP
;
629 tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
630 t
->std
= V4L2_STD_NTSC_M_KR
;
633 /* default parameter, do nothing */
636 tuner_info("ntsc= argument not recognised\n");
643 static void tuner_status(struct dvb_frontend
*fe
)
645 struct tuner
*t
= fe
->analog_demod_priv
;
646 unsigned long freq
, freq_fraction
;
647 struct dvb_tuner_ops
*fe_tuner_ops
= &fe
->ops
.tuner_ops
;
648 struct analog_demod_ops
*analog_ops
= &fe
->ops
.analog_ops
;
652 case V4L2_TUNER_RADIO
: p
= "radio"; break;
653 case V4L2_TUNER_ANALOG_TV
: p
= "analog TV"; break;
654 case V4L2_TUNER_DIGITAL_TV
: p
= "digital TV"; break;
655 default: p
= "undefined"; break;
657 if (t
->mode
== V4L2_TUNER_RADIO
) {
658 freq
= t
->radio_freq
/ 16000;
659 freq_fraction
= (t
->radio_freq
% 16000) * 100 / 16000;
661 freq
= t
->tv_freq
/ 16;
662 freq_fraction
= (t
->tv_freq
% 16) * 100 / 16;
664 tuner_info("Tuner mode: %s\n", p
);
665 tuner_info("Frequency: %lu.%02lu MHz\n", freq
, freq_fraction
);
666 tuner_info("Standard: 0x%08lx\n", (unsigned long)t
->std
);
667 if (t
->mode
!= V4L2_TUNER_RADIO
)
669 if (fe_tuner_ops
->get_status
) {
672 fe_tuner_ops
->get_status(&t
->fe
, &tuner_status
);
673 if (tuner_status
& TUNER_STATUS_LOCKED
)
674 tuner_info("Tuner is locked.\n");
675 if (tuner_status
& TUNER_STATUS_STEREO
)
676 tuner_info("Stereo: yes\n");
678 if (analog_ops
->has_signal
)
679 tuner_info("Signal strength: %d\n",
680 analog_ops
->has_signal(fe
));
681 if (analog_ops
->is_stereo
)
682 tuner_info("Stereo: %s\n",
683 analog_ops
->is_stereo(fe
) ? "yes" : "no");
686 /* ---------------------------------------------------------------------- */
689 * Switch tuner to other mode. If tuner support both tv and radio,
690 * set another frequency to some value (This is needed for some pal
691 * tuners to avoid locking). Otherwise, just put second tuner in
695 static inline int set_mode(struct i2c_client
*client
, struct tuner
*t
, int mode
, char *cmd
)
697 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
704 if (check_mode(t
, cmd
) == -EINVAL
) {
705 tuner_dbg("Tuner doesn't support this mode. "
706 "Putting tuner to sleep\n");
708 if (analog_ops
->standby
)
709 analog_ops
->standby(&t
->fe
);
715 #define switch_v4l2() if (!t->using_v4l2) \
716 tuner_dbg("switching to v4l2\n"); \
719 static inline int check_v4l2(struct tuner
*t
)
721 /* bttv still uses both v4l1 and v4l2 calls to the tuner (v4l2 for
722 TV, v4l1 for radio), until that is fixed this code is disabled.
723 Otherwise the radio (v4l1) wouldn't tune after using the TV (v4l2)
728 static int tuner_s_type_addr(struct v4l2_subdev
*sd
, struct tuner_setup
*type
)
730 struct tuner
*t
= to_tuner(sd
);
731 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
733 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
739 set_addr(client
, type
);
743 static int tuner_s_radio(struct v4l2_subdev
*sd
)
745 struct tuner
*t
= to_tuner(sd
);
746 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
748 if (set_mode(client
, t
, V4L2_TUNER_RADIO
, "s_radio") == -EINVAL
)
751 set_freq(client
, t
->radio_freq
);
755 static int tuner_s_standby(struct v4l2_subdev
*sd
)
757 struct tuner
*t
= to_tuner(sd
);
758 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
760 tuner_dbg("Putting tuner to sleep\n");
762 if (check_mode(t
, "s_standby") == -EINVAL
)
765 if (analog_ops
->standby
)
766 analog_ops
->standby(&t
->fe
);
770 static int tuner_s_config(struct v4l2_subdev
*sd
, const struct v4l2_priv_tun_config
*cfg
)
772 struct tuner
*t
= to_tuner(sd
);
773 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
775 if (t
->type
!= cfg
->tuner
)
778 if (analog_ops
->set_config
) {
779 analog_ops
->set_config(&t
->fe
, cfg
->priv
);
783 tuner_dbg("Tuner frontend module has no way to set config\n");
787 /* --- v4l ioctls --- */
788 /* take care: bttv does userspace copying, we'll get a
789 kernel pointer here... */
790 static int tuner_s_std(struct v4l2_subdev
*sd
, v4l2_std_id std
)
792 struct tuner
*t
= to_tuner(sd
);
793 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
795 if (set_mode(client
, t
, V4L2_TUNER_ANALOG_TV
, "s_std") == -EINVAL
)
803 set_freq(client
, t
->tv_freq
);
807 static int tuner_s_frequency(struct v4l2_subdev
*sd
, struct v4l2_frequency
*f
)
809 struct tuner
*t
= to_tuner(sd
);
810 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
812 if (set_mode(client
, t
, f
->type
, "s_frequency") == -EINVAL
)
815 set_freq(client
, f
->frequency
);
820 static int tuner_g_frequency(struct v4l2_subdev
*sd
, struct v4l2_frequency
*f
)
822 struct tuner
*t
= to_tuner(sd
);
823 struct dvb_tuner_ops
*fe_tuner_ops
= &t
->fe
.ops
.tuner_ops
;
825 if (check_mode(t
, "g_frequency") == -EINVAL
)
829 if (fe_tuner_ops
->get_frequency
) {
832 fe_tuner_ops
->get_frequency(&t
->fe
, &abs_freq
);
833 f
->frequency
= (V4L2_TUNER_RADIO
== t
->mode
) ?
834 DIV_ROUND_CLOSEST(abs_freq
* 2, 125) :
835 DIV_ROUND_CLOSEST(abs_freq
, 62500);
838 f
->frequency
= (V4L2_TUNER_RADIO
== t
->mode
) ?
839 t
->radio_freq
: t
->tv_freq
;
843 static int tuner_g_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*vt
)
845 struct tuner
*t
= to_tuner(sd
);
846 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
847 struct dvb_tuner_ops
*fe_tuner_ops
= &t
->fe
.ops
.tuner_ops
;
849 if (check_mode(t
, "g_tuner") == -EINVAL
)
854 if (analog_ops
->get_afc
)
855 vt
->afc
= analog_ops
->get_afc(&t
->fe
);
856 if (t
->mode
== V4L2_TUNER_ANALOG_TV
)
857 vt
->capability
|= V4L2_TUNER_CAP_NORM
;
858 if (t
->mode
!= V4L2_TUNER_RADIO
) {
859 vt
->rangelow
= tv_range
[0] * 16;
860 vt
->rangehigh
= tv_range
[1] * 16;
866 V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
867 if (fe_tuner_ops
->get_status
) {
870 fe_tuner_ops
->get_status(&t
->fe
, &tuner_status
);
872 (tuner_status
& TUNER_STATUS_STEREO
) ?
873 V4L2_TUNER_SUB_STEREO
:
876 if (analog_ops
->is_stereo
) {
878 analog_ops
->is_stereo(&t
->fe
) ?
879 V4L2_TUNER_SUB_STEREO
:
883 if (analog_ops
->has_signal
)
884 vt
->signal
= analog_ops
->has_signal(&t
->fe
);
886 V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_STEREO
;
887 vt
->audmode
= t
->audmode
;
888 vt
->rangelow
= radio_range
[0] * 16000;
889 vt
->rangehigh
= radio_range
[1] * 16000;
893 static int tuner_s_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*vt
)
895 struct tuner
*t
= to_tuner(sd
);
896 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
898 if (check_mode(t
, "s_tuner") == -EINVAL
)
903 /* do nothing unless we're a radio tuner */
904 if (t
->mode
!= V4L2_TUNER_RADIO
)
906 t
->audmode
= vt
->audmode
;
907 set_radio_freq(client
, t
->radio_freq
);
911 static int tuner_log_status(struct v4l2_subdev
*sd
)
913 struct tuner
*t
= to_tuner(sd
);
914 struct analog_demod_ops
*analog_ops
= &t
->fe
.ops
.analog_ops
;
916 if (analog_ops
->tuner_status
)
917 analog_ops
->tuner_status(&t
->fe
);
921 static int tuner_suspend(struct i2c_client
*c
, pm_message_t state
)
923 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
925 tuner_dbg("suspend\n");
926 /* FIXME: power down ??? */
930 static int tuner_resume(struct i2c_client
*c
)
932 struct tuner
*t
= to_tuner(i2c_get_clientdata(c
));
934 tuner_dbg("resume\n");
935 if (V4L2_TUNER_RADIO
== t
->mode
) {
937 set_freq(c
, t
->radio_freq
);
940 set_freq(c
, t
->tv_freq
);
945 static int tuner_command(struct i2c_client
*client
, unsigned cmd
, void *arg
)
947 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
949 /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
951 There must be a better way of doing this... */
953 case TUNER_SET_CONFIG
:
954 return tuner_s_config(sd
, arg
);
959 /* ----------------------------------------------------------------------- */
961 static const struct v4l2_subdev_core_ops tuner_core_ops
= {
962 .log_status
= tuner_log_status
,
963 .s_std
= tuner_s_std
,
966 static const struct v4l2_subdev_tuner_ops tuner_tuner_ops
= {
967 .s_radio
= tuner_s_radio
,
968 .g_tuner
= tuner_g_tuner
,
969 .s_tuner
= tuner_s_tuner
,
970 .s_frequency
= tuner_s_frequency
,
971 .g_frequency
= tuner_g_frequency
,
972 .s_type_addr
= tuner_s_type_addr
,
973 .s_config
= tuner_s_config
,
974 .s_standby
= tuner_s_standby
,
977 static const struct v4l2_subdev_ops tuner_ops
= {
978 .core
= &tuner_core_ops
,
979 .tuner
= &tuner_tuner_ops
,
982 /* ---------------------------------------------------------------------- */
984 static LIST_HEAD(tuner_list
);
986 /* Search for existing radio and/or TV tuners on the given I2C adapter.
987 Note that when this function is called from tuner_probe you can be
988 certain no other devices will be added/deleted at the same time, I2C
989 core protects against that. */
990 static void tuner_lookup(struct i2c_adapter
*adap
,
991 struct tuner
**radio
, struct tuner
**tv
)
998 list_for_each_entry(pos
, &tuner_list
, list
) {
1001 if (pos
->i2c
->adapter
!= adap
||
1002 strcmp(pos
->i2c
->driver
->driver
.name
, "tuner"))
1005 mode_mask
= pos
->mode_mask
& ~T_STANDBY
;
1006 if (*radio
== NULL
&& mode_mask
== T_RADIO
)
1008 /* Note: currently TDA9887 is the only demod-only
1009 device. If other devices appear then we need to
1010 make this test more general. */
1011 else if (*tv
== NULL
&& pos
->type
!= TUNER_TDA9887
&&
1012 (pos
->mode_mask
& (T_ANALOG_TV
| T_DIGITAL_TV
)))
1017 /* During client attach, set_type is called by adapter's attach_inform callback.
1018 set_type must then be completed by tuner_probe.
1020 static int tuner_probe(struct i2c_client
*client
,
1021 const struct i2c_device_id
*id
)
1024 struct tuner
*radio
;
1027 t
= kzalloc(sizeof(struct tuner
), GFP_KERNEL
);
1030 v4l2_i2c_subdev_init(&t
->sd
, client
, &tuner_ops
);
1032 t
->name
= "(tuner unset)";
1034 t
->audmode
= V4L2_TUNER_MODE_STEREO
;
1035 t
->mode_mask
= T_UNINITIALIZED
;
1038 unsigned char buffer
[16];
1041 memset(buffer
, 0, sizeof(buffer
));
1042 rc
= i2c_master_recv(client
, buffer
, sizeof(buffer
));
1043 tuner_info("I2C RECV = ");
1044 for (i
= 0; i
< rc
; i
++)
1045 printk(KERN_CONT
"%02x ", buffer
[i
]);
1048 /* HACK: This test was added to avoid tuner to probe tda9840 and
1049 tea6415c on the MXB card */
1050 if (client
->adapter
->id
== I2C_HW_SAA7146
&& client
->addr
< 0x4a) {
1055 /* autodetection code based on the i2c addr */
1056 if (!no_autodetect
) {
1057 switch (client
->addr
) {
1059 if (tuner_symbol_probe(tea5761_autodetection
,
1061 t
->i2c
->addr
) >= 0) {
1062 t
->type
= TUNER_TEA5761
;
1063 t
->mode_mask
= T_RADIO
;
1064 t
->mode
= T_STANDBY
;
1065 /* Sets freq to FM range */
1066 t
->radio_freq
= 87.5 * 16000;
1067 tuner_lookup(t
->i2c
->adapter
, &radio
, &tv
);
1069 tv
->mode_mask
&= ~T_RADIO
;
1071 goto register_client
;
1078 /* If chip is not tda8290, don't register.
1079 since it can be tda9887*/
1080 if (tuner_symbol_probe(tda829x_probe
, t
->i2c
->adapter
,
1081 t
->i2c
->addr
) >= 0) {
1082 tuner_dbg("tda829x detected\n");
1084 /* Default is being tda9887 */
1085 t
->type
= TUNER_TDA9887
;
1086 t
->mode_mask
= T_RADIO
| T_ANALOG_TV
|
1088 t
->mode
= T_STANDBY
;
1089 goto register_client
;
1093 if (tuner_symbol_probe(tea5767_autodetection
,
1094 t
->i2c
->adapter
, t
->i2c
->addr
)
1096 t
->type
= TUNER_TEA5767
;
1097 t
->mode_mask
= T_RADIO
;
1098 t
->mode
= T_STANDBY
;
1099 /* Sets freq to FM range */
1100 t
->radio_freq
= 87.5 * 16000;
1101 tuner_lookup(t
->i2c
->adapter
, &radio
, &tv
);
1103 tv
->mode_mask
&= ~T_RADIO
;
1105 goto register_client
;
1111 /* Initializes only the first TV tuner on this adapter. Why only the
1112 first? Because there are some devices (notably the ones with TI
1113 tuners) that have more than one i2c address for the *same* device.
1114 Experience shows that, except for just one case, the first
1115 address is the right one. The exception is a Russian tuner
1116 (ACORP_Y878F). So, the desired behavior is just to enable the
1117 first found TV tuner. */
1118 tuner_lookup(t
->i2c
->adapter
, &radio
, &tv
);
1120 t
->mode_mask
= T_ANALOG_TV
| T_DIGITAL_TV
;
1122 t
->mode_mask
|= T_RADIO
;
1123 tuner_dbg("Setting mode_mask to 0x%02x\n", t
->mode_mask
);
1124 t
->tv_freq
= 400 * 16; /* Sets freq to VHF High */
1125 t
->radio_freq
= 87.5 * 16000; /* Sets freq to FM range */
1128 /* Should be just before return */
1130 tuner_info("chip found @ 0x%x (%s)\n", client
->addr
<< 1,
1131 client
->adapter
->name
);
1133 /* Sets a default mode */
1134 if (t
->mode_mask
& T_ANALOG_TV
) {
1135 t
->mode
= V4L2_TUNER_ANALOG_TV
;
1136 } else if (t
->mode_mask
& T_RADIO
) {
1137 t
->mode
= V4L2_TUNER_RADIO
;
1139 t
->mode
= V4L2_TUNER_DIGITAL_TV
;
1141 set_type(client
, t
->type
, t
->mode_mask
, t
->config
, t
->fe
.callback
);
1142 list_add_tail(&t
->list
, &tuner_list
);
1146 static int tuner_remove(struct i2c_client
*client
)
1148 struct tuner
*t
= to_tuner(i2c_get_clientdata(client
));
1150 v4l2_device_unregister_subdev(&t
->sd
);
1151 tuner_detach(&t
->fe
);
1152 t
->fe
.analog_demod_priv
= NULL
;
1159 /* ----------------------------------------------------------------------- */
1161 /* This driver supports many devices and the idea is to let the driver
1162 detect which device is present. So rather than listing all supported
1163 devices here, we pretend to support a single, fake device type. */
1164 static const struct i2c_device_id tuner_id
[] = {
1165 { "tuner", }, /* autodetect */
1168 MODULE_DEVICE_TABLE(i2c
, tuner_id
);
1170 static struct v4l2_i2c_driver_data v4l2_i2c_data
= {
1172 .probe
= tuner_probe
,
1173 .remove
= tuner_remove
,
1174 .command
= tuner_command
,
1175 .suspend
= tuner_suspend
,
1176 .resume
= tuner_resume
,
1177 .id_table
= tuner_id
,
1181 * Overrides for Emacs so that we follow Linus's tabbing style.
1182 * ---------------------------------------------------------------------------