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"
35 #define PREFIX t->i2c->driver->driver.name
37 /** This macro allows us to probe dynamically, avoiding static links */
38 #ifdef CONFIG_MEDIA_ATTACH
39 #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
41 typeof(&FUNCTION) __a = symbol_request(FUNCTION); \
43 __r = (int) __a(ARGS); \
44 symbol_put(FUNCTION); \
46 printk(KERN_ERR "TUNER: Unable to find " \
47 "symbol "#FUNCTION"()\n"); \
52 static void tuner_detach(struct dvb_frontend *fe)
54 if (fe->ops.tuner_ops.release) {
55 fe->ops.tuner_ops.release(fe);
56 symbol_put_addr(fe->ops.tuner_ops.release);
58 if (fe->ops.analog_ops.release) {
59 fe->ops.analog_ops.release(fe);
60 symbol_put_addr(fe->ops.analog_ops.release);
64 #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
68 static void tuner_detach(struct dvb_frontend *fe)
70 if (fe->ops.tuner_ops.release)
71 fe->ops.tuner_ops.release(fe);
72 if (fe->ops.analog_ops.release)
73 fe->ops.analog_ops.release(fe);
79 struct dvb_frontend fe;
80 struct i2c_client *i2c;
81 struct v4l2_subdev sd;
82 struct list_head list;
83 unsigned int using_v4l2:1;
85 /* keep track of the current settings */
88 unsigned int radio_freq;
92 unsigned int mode_mask; /* Combination of allowable modes */
94 unsigned int type; /* chip type id */
99 static inline struct tuner *to_tuner(struct v4l2_subdev *sd)
101 return container_of(sd, struct tuner, sd);
105 /* insmod options used at init time => read/only */
106 static unsigned int addr;
107 static unsigned int no_autodetect;
108 static unsigned int show_i2c;
110 /* insmod options used at runtime => read/write */
111 static int tuner_debug;
113 #define tuner_warn(fmt, arg...) do { \
114 printk(KERN_WARNING "%s %d-%04x: " fmt, PREFIX, \
115 i2c_adapter_id(t->i2c->adapter), \
116 t->i2c->addr, ##arg); \
119 #define tuner_info(fmt, arg...) do { \
120 printk(KERN_INFO "%s %d-%04x: " fmt, PREFIX, \
121 i2c_adapter_id(t->i2c->adapter), \
122 t->i2c->addr, ##arg); \
125 #define tuner_err(fmt, arg...) do { \
126 printk(KERN_ERR "%s %d-%04x: " fmt, PREFIX, \
127 i2c_adapter_id(t->i2c->adapter), \
128 t->i2c->addr, ##arg); \
131 #define tuner_dbg(fmt, arg...) do { \
133 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX, \
134 i2c_adapter_id(t->i2c->adapter), \
135 t->i2c->addr, ##arg); \
138 /* ------------------------------------------------------------------------ */
140 static unsigned int tv_range[2] = { 44, 958 };
141 static unsigned int radio_range[2] = { 65, 108 };
143 static char pal[] = "--";
144 static char secam[] = "--";
145 static char ntsc[] = "-";
148 module_param(addr, int, 0444);
149 module_param(no_autodetect, int, 0444);
150 module_param(show_i2c, int, 0444);
151 module_param_named(debug,tuner_debug, int, 0644);
152 module_param_string(pal, pal, sizeof(pal), 0644);
153 module_param_string(secam, secam, sizeof(secam), 0644);
154 module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
155 module_param_array(tv_range, int, NULL, 0644);
156 module_param_array(radio_range, int, NULL, 0644);
158 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
159 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
160 MODULE_LICENSE("GPL");
162 /* ---------------------------------------------------------------------- */
164 static void fe_set_params(struct dvb_frontend *fe,
165 struct analog_parameters *params)
167 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
168 struct tuner *t = fe->analog_demod_priv;
170 if (NULL == fe_tuner_ops->set_analog_params) {
171 tuner_warn("Tuner frontend module has no way to set freq\n");
174 fe_tuner_ops->set_analog_params(fe, params);
177 static void fe_standby(struct dvb_frontend *fe)
179 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
181 if (fe_tuner_ops->sleep)
182 fe_tuner_ops->sleep(fe);
185 static int fe_has_signal(struct dvb_frontend *fe)
189 if (fe->ops.tuner_ops.get_rf_strength)
190 fe->ops.tuner_ops.get_rf_strength(fe, &strength);
195 static int fe_set_config(struct dvb_frontend *fe, void *priv_cfg)
197 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
198 struct tuner *t = fe->analog_demod_priv;
200 if (fe_tuner_ops->set_config)
201 return fe_tuner_ops->set_config(fe, priv_cfg);
203 tuner_warn("Tuner frontend module has no way to set config\n");
208 static void tuner_status(struct dvb_frontend *fe);
210 static struct analog_demod_ops tuner_analog_ops = {
211 .set_params = fe_set_params,
212 .standby = fe_standby,
213 .has_signal = fe_has_signal,
214 .set_config = fe_set_config,
215 .tuner_status = tuner_status
218 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
219 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
221 struct tuner *t = to_tuner(i2c_get_clientdata(c));
222 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
224 struct analog_parameters params = {
226 .audmode = t->audmode,
230 if (t->type == UNSET) {
231 tuner_warn ("tuner type not set\n");
234 if (NULL == analog_ops->set_params) {
235 tuner_warn ("Tuner has no way to set tv freq\n");
238 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
239 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
240 freq / 16, freq % 16 * 100 / 16, tv_range[0],
242 /* V4L2 spec: if the freq is not possible then the closest
243 possible value should be selected */
244 if (freq < tv_range[0] * 16)
245 freq = tv_range[0] * 16;
247 freq = tv_range[1] * 16;
249 params.frequency = freq;
251 analog_ops->set_params(&t->fe, ¶ms);
254 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
256 struct tuner *t = to_tuner(i2c_get_clientdata(c));
257 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
259 struct analog_parameters params = {
261 .audmode = t->audmode,
265 if (t->type == UNSET) {
266 tuner_warn ("tuner type not set\n");
269 if (NULL == analog_ops->set_params) {
270 tuner_warn ("tuner has no way to set radio frequency\n");
273 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
274 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
275 freq / 16000, freq % 16000 * 100 / 16000,
276 radio_range[0], radio_range[1]);
277 /* V4L2 spec: if the freq is not possible then the closest
278 possible value should be selected */
279 if (freq < radio_range[0] * 16000)
280 freq = radio_range[0] * 16000;
282 freq = radio_range[1] * 16000;
284 params.frequency = freq;
286 analog_ops->set_params(&t->fe, ¶ms);
289 static void set_freq(struct i2c_client *c, unsigned long freq)
291 struct tuner *t = to_tuner(i2c_get_clientdata(c));
294 case V4L2_TUNER_RADIO:
295 tuner_dbg("radio freq set to %lu.%02lu\n",
296 freq / 16000, freq % 16000 * 100 / 16000);
297 set_radio_freq(c, freq);
298 t->radio_freq = freq;
300 case V4L2_TUNER_ANALOG_TV:
301 case V4L2_TUNER_DIGITAL_TV:
302 tuner_dbg("tv freq set to %lu.%02lu\n",
303 freq / 16, freq % 16 * 100 / 16);
304 set_tv_freq(c, freq);
308 tuner_dbg("freq set: unknown mode: 0x%04x!\n",t->mode);
312 static struct xc5000_config xc5000_cfg;
314 static void set_type(struct i2c_client *c, unsigned int type,
315 unsigned int new_mode_mask, unsigned int new_config,
316 int (*tuner_callback) (void *dev, int component, int cmd, int arg))
318 struct tuner *t = to_tuner(i2c_get_clientdata(c));
319 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
320 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
321 unsigned char buffer[4];
323 if (type == UNSET || type == TUNER_ABSENT) {
324 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
329 /* prevent invalid config values */
330 t->config = ((new_config >= 0) && (new_config < 256)) ? new_config : 0;
331 if (tuner_callback != NULL) {
332 tuner_dbg("defining GPIO callback\n");
333 t->fe.callback = tuner_callback;
336 if (t->mode == T_UNINITIALIZED) {
337 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
342 /* discard private data, in case set_type() was previously called */
343 tuner_detach(&t->fe);
344 t->fe.analog_demod_priv = NULL;
348 if (!dvb_attach(microtune_attach,
349 &t->fe, t->i2c->adapter, t->i2c->addr))
352 case TUNER_PHILIPS_TDA8290:
354 struct tda829x_config cfg = {
355 .lna_cfg = t->config,
357 if (!dvb_attach(tda829x_attach, &t->fe, t->i2c->adapter,
363 if (!dvb_attach(tea5767_attach, &t->fe,
364 t->i2c->adapter, t->i2c->addr))
366 t->mode_mask = T_RADIO;
369 if (!dvb_attach(tea5761_attach, &t->fe,
370 t->i2c->adapter, t->i2c->addr))
372 t->mode_mask = T_RADIO;
374 case TUNER_PHILIPS_FMD1216ME_MK3:
379 i2c_master_send(c, buffer, 4);
383 i2c_master_send(c, buffer, 4);
384 if (!dvb_attach(simple_tuner_attach, &t->fe,
385 t->i2c->adapter, t->i2c->addr, t->type))
388 case TUNER_PHILIPS_TD1316:
393 i2c_master_send(c, buffer, 4);
394 if (!dvb_attach(simple_tuner_attach, &t->fe,
395 t->i2c->adapter, t->i2c->addr, t->type))
400 struct xc2028_config cfg = {
401 .i2c_adap = t->i2c->adapter,
402 .i2c_addr = t->i2c->addr,
404 if (!dvb_attach(xc2028_attach, &t->fe, &cfg))
409 if (!dvb_attach(tda9887_attach,
410 &t->fe, t->i2c->adapter, t->i2c->addr))
415 struct dvb_tuner_ops *xc_tuner_ops;
417 xc5000_cfg.i2c_address = t->i2c->addr;
418 /* if_khz will be set when the digital dvb_attach() occurs */
419 xc5000_cfg.if_khz = 0;
420 if (!dvb_attach(xc5000_attach,
421 &t->fe, t->i2c->adapter, &xc5000_cfg))
424 xc_tuner_ops = &t->fe.ops.tuner_ops;
425 if (xc_tuner_ops->init)
426 xc_tuner_ops->init(&t->fe);
430 if (!dvb_attach(simple_tuner_attach, &t->fe,
431 t->i2c->adapter, t->i2c->addr, t->type))
437 if ((NULL == analog_ops->set_params) &&
438 (fe_tuner_ops->set_analog_params)) {
440 t->name = fe_tuner_ops->info.name;
442 t->fe.analog_demod_priv = t;
443 memcpy(analog_ops, &tuner_analog_ops,
444 sizeof(struct analog_demod_ops));
447 t->name = analog_ops->info.name;
450 tuner_dbg("type set to %s\n", t->name);
452 if (t->mode_mask == T_UNINITIALIZED)
453 t->mode_mask = new_mode_mask;
455 /* xc2028/3028 and xc5000 requires a firmware to be set-up later
456 trying to set a frequency here will just fail
457 FIXME: better to move set_freq to the tuner code. This is needed
458 on analog tuners for PLL to properly work
460 if (t->type != TUNER_XC2028 && t->type != TUNER_XC5000)
461 set_freq(c, (V4L2_TUNER_RADIO == t->mode) ?
462 t->radio_freq : t->tv_freq);
464 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
465 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
470 tuner_dbg("Tuner attach for type = %d failed.\n", t->type);
471 t->type = TUNER_ABSENT;
472 t->mode_mask = T_UNINITIALIZED;
478 * This function apply tuner config to tuner specified
479 * by tun_setup structure. I addr is unset, then admin status
480 * and tun addr status is more precise then current status,
481 * it's applied. Otherwise status and type are applied only to
482 * tuner with exactly the same addr.
485 static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
487 struct tuner *t = to_tuner(i2c_get_clientdata(c));
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);
501 static inline int check_mode(struct tuner *t, char *cmd)
503 if ((1 << t->mode & t->mode_mask) == 0) {
508 case V4L2_TUNER_RADIO:
509 tuner_dbg("Cmd %s accepted for radio\n", cmd);
511 case V4L2_TUNER_ANALOG_TV:
512 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
514 case V4L2_TUNER_DIGITAL_TV:
515 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
521 /* get more precise norm info from insmod option */
522 static int tuner_fixup_std(struct tuner *t)
524 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
527 tuner_dbg ("insmod fixup: PAL => PAL-60\n");
528 t->std = V4L2_STD_PAL_60;
534 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
535 t->std = V4L2_STD_PAL_BG;
539 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
540 t->std = V4L2_STD_PAL_I;
546 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
547 t->std = V4L2_STD_PAL_DK;
551 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
552 t->std = V4L2_STD_PAL_M;
556 if (pal[1] == 'c' || pal[1] == 'C') {
557 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
558 t->std = V4L2_STD_PAL_Nc;
560 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
561 t->std = V4L2_STD_PAL_N;
565 /* default parameter, do nothing */
568 tuner_warn ("pal= argument not recognised\n");
572 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
580 tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
581 t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
587 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
588 t->std = V4L2_STD_SECAM_DK;
592 if ((secam[1]=='C')||(secam[1]=='c')) {
593 tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n");
594 t->std = V4L2_STD_SECAM_LC;
596 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
597 t->std = V4L2_STD_SECAM_L;
601 /* default parameter, do nothing */
604 tuner_warn ("secam= argument not recognised\n");
609 if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
613 tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
614 t->std = V4L2_STD_NTSC_M;
618 tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
619 t->std = V4L2_STD_NTSC_M_JP;
623 tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
624 t->std = V4L2_STD_NTSC_M_KR;
627 /* default parameter, do nothing */
630 tuner_info("ntsc= argument not recognised\n");
637 static void tuner_status(struct dvb_frontend *fe)
639 struct tuner *t = fe->analog_demod_priv;
640 unsigned long freq, freq_fraction;
641 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
642 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
646 case V4L2_TUNER_RADIO: p = "radio"; break;
647 case V4L2_TUNER_ANALOG_TV: p = "analog TV"; break;
648 case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break;
649 default: p = "undefined"; break;
651 if (t->mode == V4L2_TUNER_RADIO) {
652 freq = t->radio_freq / 16000;
653 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
655 freq = t->tv_freq / 16;
656 freq_fraction = (t->tv_freq % 16) * 100 / 16;
658 tuner_info("Tuner mode: %s\n", p);
659 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
660 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
661 if (t->mode != V4L2_TUNER_RADIO)
663 if (fe_tuner_ops->get_status) {
666 fe_tuner_ops->get_status(&t->fe, &tuner_status);
667 if (tuner_status & TUNER_STATUS_LOCKED)
668 tuner_info("Tuner is locked.\n");
669 if (tuner_status & TUNER_STATUS_STEREO)
670 tuner_info("Stereo: yes\n");
672 if (analog_ops->has_signal)
673 tuner_info("Signal strength: %d\n",
674 analog_ops->has_signal(fe));
675 if (analog_ops->is_stereo)
676 tuner_info("Stereo: %s\n",
677 analog_ops->is_stereo(fe) ? "yes" : "no");
680 /* ---------------------------------------------------------------------- */
683 * Switch tuner to other mode. If tuner support both tv and radio,
684 * set another frequency to some value (This is needed for some pal
685 * tuners to avoid locking). Otherwise, just put second tuner in
689 static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
691 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
698 if (check_mode(t, cmd) == -EINVAL) {
699 tuner_dbg("Tuner doesn't support this mode. "
700 "Putting tuner to sleep\n");
702 if (analog_ops->standby)
703 analog_ops->standby(&t->fe);
709 #define switch_v4l2() if (!t->using_v4l2) \
710 tuner_dbg("switching to v4l2\n"); \
713 static inline int check_v4l2(struct tuner *t)
715 /* bttv still uses both v4l1 and v4l2 calls to the tuner (v4l2 for
716 TV, v4l1 for radio), until that is fixed this code is disabled.
717 Otherwise the radio (v4l1) wouldn't tune after using the TV (v4l2)
722 static int tuner_s_type_addr(struct v4l2_subdev *sd, struct tuner_setup *type)
724 struct tuner *t = to_tuner(sd);
725 struct i2c_client *client = v4l2_get_subdevdata(sd);
727 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
733 set_addr(client, type);
737 static int tuner_s_radio(struct v4l2_subdev *sd)
739 struct tuner *t = to_tuner(sd);
740 struct i2c_client *client = v4l2_get_subdevdata(sd);
742 if (set_mode(client, t, V4L2_TUNER_RADIO, "s_radio") == -EINVAL)
745 set_freq(client, t->radio_freq);
749 static int tuner_s_standby(struct v4l2_subdev *sd)
751 struct tuner *t = to_tuner(sd);
752 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
754 tuner_dbg("Putting tuner to sleep\n");
756 if (check_mode(t, "s_standby") == -EINVAL)
759 if (analog_ops->standby)
760 analog_ops->standby(&t->fe);
764 static int tuner_s_config(struct v4l2_subdev *sd, const struct v4l2_priv_tun_config *cfg)
766 struct tuner *t = to_tuner(sd);
767 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
769 if (t->type != cfg->tuner)
772 if (analog_ops->set_config) {
773 analog_ops->set_config(&t->fe, cfg->priv);
777 tuner_dbg("Tuner frontend module has no way to set config\n");
781 /* --- v4l ioctls --- */
782 /* take care: bttv does userspace copying, we'll get a
783 kernel pointer here... */
784 static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
786 struct tuner *t = to_tuner(sd);
787 struct i2c_client *client = v4l2_get_subdevdata(sd);
789 if (set_mode(client, t, V4L2_TUNER_ANALOG_TV, "s_std") == -EINVAL)
797 set_freq(client, t->tv_freq);
801 static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
803 struct tuner *t = to_tuner(sd);
804 struct i2c_client *client = v4l2_get_subdevdata(sd);
806 if (set_mode(client, t, f->type, "s_frequency") == -EINVAL)
809 set_freq(client, f->frequency);
814 static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
816 struct tuner *t = to_tuner(sd);
817 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
819 if (check_mode(t, "g_frequency") == -EINVAL)
823 if (fe_tuner_ops->get_frequency) {
826 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
827 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
828 (abs_freq * 2 + 125/2) / 125 :
829 (abs_freq + 62500/2) / 62500;
832 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
833 t->radio_freq : t->tv_freq;
837 static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
839 struct tuner *t = to_tuner(sd);
840 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
841 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
843 if (check_mode(t, "g_tuner") == -EINVAL)
848 if (analog_ops->get_afc)
849 vt->afc = analog_ops->get_afc(&t->fe);
850 if (t->mode == V4L2_TUNER_ANALOG_TV)
851 vt->capability |= V4L2_TUNER_CAP_NORM;
852 if (t->mode != V4L2_TUNER_RADIO) {
853 vt->rangelow = tv_range[0] * 16;
854 vt->rangehigh = tv_range[1] * 16;
860 V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
861 if (fe_tuner_ops->get_status) {
864 fe_tuner_ops->get_status(&t->fe, &tuner_status);
866 (tuner_status & TUNER_STATUS_STEREO) ?
867 V4L2_TUNER_SUB_STEREO :
870 if (analog_ops->is_stereo) {
872 analog_ops->is_stereo(&t->fe) ?
873 V4L2_TUNER_SUB_STEREO :
877 if (analog_ops->has_signal)
878 vt->signal = analog_ops->has_signal(&t->fe);
880 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
881 vt->audmode = t->audmode;
882 vt->rangelow = radio_range[0] * 16000;
883 vt->rangehigh = radio_range[1] * 16000;
887 static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
889 struct tuner *t = to_tuner(sd);
890 struct i2c_client *client = v4l2_get_subdevdata(sd);
892 if (check_mode(t, "s_tuner") == -EINVAL)
897 /* do nothing unless we're a radio tuner */
898 if (t->mode != V4L2_TUNER_RADIO)
900 t->audmode = vt->audmode;
901 set_radio_freq(client, t->radio_freq);
905 static int tuner_log_status(struct v4l2_subdev *sd)
907 struct tuner *t = to_tuner(sd);
908 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
910 if (analog_ops->tuner_status)
911 analog_ops->tuner_status(&t->fe);
915 static int tuner_suspend(struct i2c_client *c, pm_message_t state)
917 struct tuner *t = to_tuner(i2c_get_clientdata(c));
919 tuner_dbg("suspend\n");
920 /* FIXME: power down ??? */
924 static int tuner_resume(struct i2c_client *c)
926 struct tuner *t = to_tuner(i2c_get_clientdata(c));
928 tuner_dbg("resume\n");
929 if (V4L2_TUNER_RADIO == t->mode) {
931 set_freq(c, t->radio_freq);
934 set_freq(c, t->tv_freq);
939 static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
941 struct v4l2_subdev *sd = i2c_get_clientdata(client);
943 /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
945 There must be a better way of doing this... */
947 case TUNER_SET_CONFIG:
948 return tuner_s_config(sd, arg);
953 /* ----------------------------------------------------------------------- */
955 static const struct v4l2_subdev_core_ops tuner_core_ops = {
956 .log_status = tuner_log_status,
957 .s_std = tuner_s_std,
960 static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
961 .s_radio = tuner_s_radio,
962 .g_tuner = tuner_g_tuner,
963 .s_tuner = tuner_s_tuner,
964 .s_frequency = tuner_s_frequency,
965 .g_frequency = tuner_g_frequency,
966 .s_type_addr = tuner_s_type_addr,
967 .s_config = tuner_s_config,
968 .s_standby = tuner_s_standby,
971 static const struct v4l2_subdev_ops tuner_ops = {
972 .core = &tuner_core_ops,
973 .tuner = &tuner_tuner_ops,
976 /* ---------------------------------------------------------------------- */
978 static LIST_HEAD(tuner_list);
980 /* Search for existing radio and/or TV tuners on the given I2C adapter.
981 Note that when this function is called from tuner_probe you can be
982 certain no other devices will be added/deleted at the same time, I2C
983 core protects against that. */
984 static void tuner_lookup(struct i2c_adapter *adap,
985 struct tuner **radio, struct tuner **tv)
992 list_for_each_entry(pos, &tuner_list, list) {
995 if (pos->i2c->adapter != adap ||
996 strcmp(pos->i2c->driver->driver.name, "tuner"))
999 mode_mask = pos->mode_mask & ~T_STANDBY;
1000 if (*radio == NULL && mode_mask == T_RADIO)
1002 /* Note: currently TDA9887 is the only demod-only
1003 device. If other devices appear then we need to
1004 make this test more general. */
1005 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
1006 (pos->mode_mask & (T_ANALOG_TV | T_DIGITAL_TV)))
1011 /* During client attach, set_type is called by adapter's attach_inform callback.
1012 set_type must then be completed by tuner_probe.
1014 static int tuner_probe(struct i2c_client *client,
1015 const struct i2c_device_id *id)
1018 struct tuner *radio;
1021 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
1024 v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
1026 t->name = "(tuner unset)";
1028 t->audmode = V4L2_TUNER_MODE_STEREO;
1029 t->mode_mask = T_UNINITIALIZED;
1032 unsigned char buffer[16];
1035 memset(buffer, 0, sizeof(buffer));
1036 rc = i2c_master_recv(client, buffer, sizeof(buffer));
1037 tuner_info("I2C RECV = ");
1038 for (i = 0; i < rc; i++)
1039 printk(KERN_CONT "%02x ", buffer[i]);
1042 /* HACK: This test was added to avoid tuner to probe tda9840 and
1043 tea6415c on the MXB card */
1044 if (client->adapter->id == I2C_HW_SAA7146 && client->addr < 0x4a) {
1049 /* autodetection code based on the i2c addr */
1050 if (!no_autodetect) {
1051 switch (client->addr) {
1053 if (tuner_symbol_probe(tea5761_autodetection,
1055 t->i2c->addr) >= 0) {
1056 t->type = TUNER_TEA5761;
1057 t->mode_mask = T_RADIO;
1058 t->mode = T_STANDBY;
1059 /* Sets freq to FM range */
1060 t->radio_freq = 87.5 * 16000;
1061 tuner_lookup(t->i2c->adapter, &radio, &tv);
1063 tv->mode_mask &= ~T_RADIO;
1065 goto register_client;
1072 /* If chip is not tda8290, don't register.
1073 since it can be tda9887*/
1074 if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
1075 t->i2c->addr) >= 0) {
1076 tuner_dbg("tda829x detected\n");
1078 /* Default is being tda9887 */
1079 t->type = TUNER_TDA9887;
1080 t->mode_mask = T_RADIO | T_ANALOG_TV |
1082 t->mode = T_STANDBY;
1083 goto register_client;
1087 if (tuner_symbol_probe(tea5767_autodetection,
1088 t->i2c->adapter, t->i2c->addr)
1090 t->type = TUNER_TEA5767;
1091 t->mode_mask = T_RADIO;
1092 t->mode = T_STANDBY;
1093 /* Sets freq to FM range */
1094 t->radio_freq = 87.5 * 16000;
1095 tuner_lookup(t->i2c->adapter, &radio, &tv);
1097 tv->mode_mask &= ~T_RADIO;
1099 goto register_client;
1105 /* Initializes only the first TV tuner on this adapter. Why only the
1106 first? Because there are some devices (notably the ones with TI
1107 tuners) that have more than one i2c address for the *same* device.
1108 Experience shows that, except for just one case, the first
1109 address is the right one. The exception is a Russian tuner
1110 (ACORP_Y878F). So, the desired behavior is just to enable the
1111 first found TV tuner. */
1112 tuner_lookup(t->i2c->adapter, &radio, &tv);
1114 t->mode_mask = T_ANALOG_TV | T_DIGITAL_TV;
1116 t->mode_mask |= T_RADIO;
1117 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
1118 t->tv_freq = 400 * 16; /* Sets freq to VHF High */
1119 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
1122 /* Should be just before return */
1124 tuner_info("chip found @ 0x%x (%s)\n", client->addr << 1,
1125 client->adapter->name);
1127 /* Sets a default mode */
1128 if (t->mode_mask & T_ANALOG_TV) {
1129 t->mode = V4L2_TUNER_ANALOG_TV;
1130 } else if (t->mode_mask & T_RADIO) {
1131 t->mode = V4L2_TUNER_RADIO;
1133 t->mode = V4L2_TUNER_DIGITAL_TV;
1135 set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
1136 list_add_tail(&t->list, &tuner_list);
1140 static int tuner_remove(struct i2c_client *client)
1142 struct tuner *t = to_tuner(i2c_get_clientdata(client));
1144 v4l2_device_unregister_subdev(&t->sd);
1145 tuner_detach(&t->fe);
1146 t->fe.analog_demod_priv = NULL;
1153 /* ----------------------------------------------------------------------- */
1155 /* This driver supports many devices and the idea is to let the driver
1156 detect which device is present. So rather than listing all supported
1157 devices here, we pretend to support a single, fake device type. */
1158 static const struct i2c_device_id tuner_id[] = {
1159 { "tuner", }, /* autodetect */
1162 MODULE_DEVICE_TABLE(i2c, tuner_id);
1164 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
1166 .probe = tuner_probe,
1167 .remove = tuner_remove,
1168 .command = tuner_command,
1169 .suspend = tuner_suspend,
1170 .resume = tuner_resume,
1171 .id_table = tuner_id,
1175 * Overrides for Emacs so that we follow Linus's tabbing style.
1176 * ---------------------------------------------------------------------------