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
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;
84 /* keep track of the current settings */
87 unsigned int radio_freq;
91 unsigned int mode_mask; /* Combination of allowable modes */
93 unsigned int type; /* chip type id */
98 static inline struct tuner *to_tuner(struct v4l2_subdev *sd)
100 return container_of(sd, struct tuner, sd);
104 /* insmod options used at init time => read/only */
105 static unsigned int addr;
106 static unsigned int no_autodetect;
107 static unsigned int show_i2c;
109 /* insmod options used at runtime => read/write */
110 static int tuner_debug;
112 #define tuner_warn(fmt, arg...) do { \
113 printk(KERN_WARNING "%s %d-%04x: " fmt, PREFIX, \
114 i2c_adapter_id(t->i2c->adapter), \
115 t->i2c->addr, ##arg); \
118 #define tuner_info(fmt, arg...) do { \
119 printk(KERN_INFO "%s %d-%04x: " fmt, PREFIX, \
120 i2c_adapter_id(t->i2c->adapter), \
121 t->i2c->addr, ##arg); \
124 #define tuner_err(fmt, arg...) do { \
125 printk(KERN_ERR "%s %d-%04x: " fmt, PREFIX, \
126 i2c_adapter_id(t->i2c->adapter), \
127 t->i2c->addr, ##arg); \
130 #define tuner_dbg(fmt, arg...) do { \
132 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX, \
133 i2c_adapter_id(t->i2c->adapter), \
134 t->i2c->addr, ##arg); \
137 /* ------------------------------------------------------------------------ */
139 static unsigned int tv_range[2] = { 44, 958 };
140 static unsigned int radio_range[2] = { 65, 108 };
142 static char pal[] = "--";
143 static char secam[] = "--";
144 static char ntsc[] = "-";
147 module_param(addr, int, 0444);
148 module_param(no_autodetect, int, 0444);
149 module_param(show_i2c, int, 0444);
150 module_param_named(debug,tuner_debug, int, 0644);
151 module_param_string(pal, pal, sizeof(pal), 0644);
152 module_param_string(secam, secam, sizeof(secam), 0644);
153 module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
154 module_param_array(tv_range, int, NULL, 0644);
155 module_param_array(radio_range, int, NULL, 0644);
157 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
158 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
159 MODULE_LICENSE("GPL");
161 /* ---------------------------------------------------------------------- */
163 static void fe_set_params(struct dvb_frontend *fe,
164 struct analog_parameters *params)
166 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
167 struct tuner *t = fe->analog_demod_priv;
169 if (NULL == fe_tuner_ops->set_analog_params) {
170 tuner_warn("Tuner frontend module has no way to set freq\n");
173 fe_tuner_ops->set_analog_params(fe, params);
176 static void fe_standby(struct dvb_frontend *fe)
178 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
180 if (fe_tuner_ops->sleep)
181 fe_tuner_ops->sleep(fe);
184 static int fe_has_signal(struct dvb_frontend *fe)
188 if (fe->ops.tuner_ops.get_rf_strength)
189 fe->ops.tuner_ops.get_rf_strength(fe, &strength);
194 static int fe_set_config(struct dvb_frontend *fe, void *priv_cfg)
196 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
197 struct tuner *t = fe->analog_demod_priv;
199 if (fe_tuner_ops->set_config)
200 return fe_tuner_ops->set_config(fe, priv_cfg);
202 tuner_warn("Tuner frontend module has no way to set config\n");
207 static void tuner_status(struct dvb_frontend *fe);
209 static struct analog_demod_ops tuner_analog_ops = {
210 .set_params = fe_set_params,
211 .standby = fe_standby,
212 .has_signal = fe_has_signal,
213 .set_config = fe_set_config,
214 .tuner_status = tuner_status
217 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
218 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
220 struct tuner *t = to_tuner(i2c_get_clientdata(c));
221 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
223 struct analog_parameters params = {
225 .audmode = t->audmode,
229 if (t->type == UNSET) {
230 tuner_warn ("tuner type not set\n");
233 if (NULL == analog_ops->set_params) {
234 tuner_warn ("Tuner has no way to set tv freq\n");
237 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
238 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
239 freq / 16, freq % 16 * 100 / 16, tv_range[0],
241 /* V4L2 spec: if the freq is not possible then the closest
242 possible value should be selected */
243 if (freq < tv_range[0] * 16)
244 freq = tv_range[0] * 16;
246 freq = tv_range[1] * 16;
248 params.frequency = freq;
249 tuner_dbg("tv freq set to %lu.%02lu\n",
250 freq / 16, freq % 16 * 100 / 16);
253 analog_ops->set_params(&t->fe, ¶ms);
256 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
258 struct tuner *t = to_tuner(i2c_get_clientdata(c));
259 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
261 struct analog_parameters params = {
263 .audmode = t->audmode,
267 if (t->type == UNSET) {
268 tuner_warn ("tuner type not set\n");
271 if (NULL == analog_ops->set_params) {
272 tuner_warn ("tuner has no way to set radio frequency\n");
275 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
276 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
277 freq / 16000, freq % 16000 * 100 / 16000,
278 radio_range[0], radio_range[1]);
279 /* V4L2 spec: if the freq is not possible then the closest
280 possible value should be selected */
281 if (freq < radio_range[0] * 16000)
282 freq = radio_range[0] * 16000;
284 freq = radio_range[1] * 16000;
286 params.frequency = freq;
287 tuner_dbg("radio freq set to %lu.%02lu\n",
288 freq / 16000, freq % 16000 * 100 / 16000);
289 t->radio_freq = freq;
291 analog_ops->set_params(&t->fe, ¶ms);
294 static void set_freq(struct i2c_client *c, unsigned long freq)
296 struct tuner *t = to_tuner(i2c_get_clientdata(c));
299 case V4L2_TUNER_RADIO:
300 set_radio_freq(c, freq);
302 case V4L2_TUNER_ANALOG_TV:
303 case V4L2_TUNER_DIGITAL_TV:
304 set_tv_freq(c, freq);
307 tuner_dbg("freq set: unknown mode: 0x%04x!\n",t->mode);
311 static struct xc5000_config xc5000_cfg;
313 static void set_type(struct i2c_client *c, unsigned int type,
314 unsigned int new_mode_mask, unsigned int new_config,
315 int (*tuner_callback) (void *dev, int component, int cmd, int arg))
317 struct tuner *t = to_tuner(i2c_get_clientdata(c));
318 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
319 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
320 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 < 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))
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))
425 case TUNER_NXP_TDA18271:
427 struct tda18271_config cfg = {
429 .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
432 if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr,
433 t->i2c->adapter, &cfg))
439 if (!dvb_attach(simple_tuner_attach, &t->fe,
440 t->i2c->adapter, t->i2c->addr, t->type))
446 if ((NULL == analog_ops->set_params) &&
447 (fe_tuner_ops->set_analog_params)) {
449 t->name = fe_tuner_ops->info.name;
451 t->fe.analog_demod_priv = t;
452 memcpy(analog_ops, &tuner_analog_ops,
453 sizeof(struct analog_demod_ops));
456 t->name = analog_ops->info.name;
459 tuner_dbg("type set to %s\n", t->name);
461 if (t->mode_mask == T_UNINITIALIZED)
462 t->mode_mask = new_mode_mask;
464 /* Some tuners require more initialization setup before use,
465 such as firmware download or device calibration.
466 trying to set a frequency here will just fail
467 FIXME: better to move set_freq to the tuner code. This is needed
468 on analog tuners for PLL to properly work
471 set_freq(c, (V4L2_TUNER_RADIO == t->mode) ?
472 t->radio_freq : t->tv_freq);
474 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
475 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
480 tuner_dbg("Tuner attach for type = %d failed.\n", t->type);
481 t->type = TUNER_ABSENT;
482 t->mode_mask = T_UNINITIALIZED;
488 * This function apply tuner config to tuner specified
489 * by tun_setup structure. I addr is unset, then admin status
490 * and tun addr status is more precise then current status,
491 * it's applied. Otherwise status and type are applied only to
492 * tuner with exactly the same addr.
495 static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
497 struct tuner *t = to_tuner(i2c_get_clientdata(c));
499 if ( (t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
500 (t->mode_mask & tun_setup->mode_mask))) ||
501 (tun_setup->addr == c->addr)) {
502 set_type(c, tun_setup->type, tun_setup->mode_mask,
503 tun_setup->config, tun_setup->tuner_callback);
505 tuner_dbg("set addr discarded for type %i, mask %x. "
506 "Asked to change tuner at addr 0x%02x, with mask %x\n",
507 t->type, t->mode_mask,
508 tun_setup->addr, tun_setup->mode_mask);
511 static inline int check_mode(struct tuner *t, char *cmd)
513 if ((1 << t->mode & t->mode_mask) == 0) {
518 case V4L2_TUNER_RADIO:
519 tuner_dbg("Cmd %s accepted for radio\n", cmd);
521 case V4L2_TUNER_ANALOG_TV:
522 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
524 case V4L2_TUNER_DIGITAL_TV:
525 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
531 /* get more precise norm info from insmod option */
532 static int tuner_fixup_std(struct tuner *t)
534 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
537 tuner_dbg ("insmod fixup: PAL => PAL-60\n");
538 t->std = V4L2_STD_PAL_60;
544 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
545 t->std = V4L2_STD_PAL_BG;
549 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
550 t->std = V4L2_STD_PAL_I;
556 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
557 t->std = V4L2_STD_PAL_DK;
561 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
562 t->std = V4L2_STD_PAL_M;
566 if (pal[1] == 'c' || pal[1] == 'C') {
567 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
568 t->std = V4L2_STD_PAL_Nc;
570 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
571 t->std = V4L2_STD_PAL_N;
575 /* default parameter, do nothing */
578 tuner_warn ("pal= argument not recognised\n");
582 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
590 tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
591 t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
597 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
598 t->std = V4L2_STD_SECAM_DK;
602 if ((secam[1]=='C')||(secam[1]=='c')) {
603 tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n");
604 t->std = V4L2_STD_SECAM_LC;
606 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
607 t->std = V4L2_STD_SECAM_L;
611 /* default parameter, do nothing */
614 tuner_warn ("secam= argument not recognised\n");
619 if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
623 tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
624 t->std = V4L2_STD_NTSC_M;
628 tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
629 t->std = V4L2_STD_NTSC_M_JP;
633 tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
634 t->std = V4L2_STD_NTSC_M_KR;
637 /* default parameter, do nothing */
640 tuner_info("ntsc= argument not recognised\n");
647 static void tuner_status(struct dvb_frontend *fe)
649 struct tuner *t = fe->analog_demod_priv;
650 unsigned long freq, freq_fraction;
651 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
652 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
656 case V4L2_TUNER_RADIO: p = "radio"; break;
657 case V4L2_TUNER_ANALOG_TV: p = "analog TV"; break;
658 case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break;
659 default: p = "undefined"; break;
661 if (t->mode == V4L2_TUNER_RADIO) {
662 freq = t->radio_freq / 16000;
663 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
665 freq = t->tv_freq / 16;
666 freq_fraction = (t->tv_freq % 16) * 100 / 16;
668 tuner_info("Tuner mode: %s\n", p);
669 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
670 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
671 if (t->mode != V4L2_TUNER_RADIO)
673 if (fe_tuner_ops->get_status) {
676 fe_tuner_ops->get_status(&t->fe, &tuner_status);
677 if (tuner_status & TUNER_STATUS_LOCKED)
678 tuner_info("Tuner is locked.\n");
679 if (tuner_status & TUNER_STATUS_STEREO)
680 tuner_info("Stereo: yes\n");
682 if (analog_ops->has_signal)
683 tuner_info("Signal strength: %d\n",
684 analog_ops->has_signal(fe));
687 /* ---------------------------------------------------------------------- */
690 * Switch tuner to other mode. If tuner support both tv and radio,
691 * set another frequency to some value (This is needed for some pal
692 * tuners to avoid locking). Otherwise, just put second tuner in
696 static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
698 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
705 if (check_mode(t, cmd) == -EINVAL) {
706 tuner_dbg("Tuner doesn't support this mode. "
707 "Putting tuner to sleep\n");
709 if (analog_ops->standby)
710 analog_ops->standby(&t->fe);
716 static int tuner_s_type_addr(struct v4l2_subdev *sd, struct tuner_setup *type)
718 struct tuner *t = to_tuner(sd);
719 struct i2c_client *client = v4l2_get_subdevdata(sd);
721 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
727 set_addr(client, type);
731 static int tuner_s_radio(struct v4l2_subdev *sd)
733 struct tuner *t = to_tuner(sd);
734 struct i2c_client *client = v4l2_get_subdevdata(sd);
736 if (set_mode(client, t, V4L2_TUNER_RADIO, "s_radio") == -EINVAL)
739 set_freq(client, t->radio_freq);
743 static int tuner_s_power(struct v4l2_subdev *sd, int on)
745 struct tuner *t = to_tuner(sd);
746 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
751 tuner_dbg("Putting tuner to sleep\n");
753 if (check_mode(t, "s_power") == -EINVAL)
756 if (analog_ops->standby)
757 analog_ops->standby(&t->fe);
761 static int tuner_s_config(struct v4l2_subdev *sd, const struct v4l2_priv_tun_config *cfg)
763 struct tuner *t = to_tuner(sd);
764 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
766 if (t->type != cfg->tuner)
769 if (analog_ops->set_config) {
770 analog_ops->set_config(&t->fe, cfg->priv);
774 tuner_dbg("Tuner frontend module has no way to set config\n");
778 /* --- v4l ioctls --- */
779 /* take care: bttv does userspace copying, we'll get a
780 kernel pointer here... */
781 static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
783 struct tuner *t = to_tuner(sd);
784 struct i2c_client *client = v4l2_get_subdevdata(sd);
786 if (set_mode(client, t, V4L2_TUNER_ANALOG_TV, "s_std") == -EINVAL)
792 set_freq(client, t->tv_freq);
796 static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
798 struct tuner *t = to_tuner(sd);
799 struct i2c_client *client = v4l2_get_subdevdata(sd);
801 if (set_mode(client, t, f->type, "s_frequency") == -EINVAL)
803 set_freq(client, f->frequency);
808 static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
810 struct tuner *t = to_tuner(sd);
811 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
813 if (check_mode(t, "g_frequency") == -EINVAL)
816 if (fe_tuner_ops->get_frequency) {
819 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
820 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
821 DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
822 DIV_ROUND_CLOSEST(abs_freq, 62500);
825 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
826 t->radio_freq : t->tv_freq;
830 static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
832 struct tuner *t = to_tuner(sd);
833 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
834 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
836 if (check_mode(t, "g_tuner") == -EINVAL)
840 if (analog_ops->get_afc)
841 vt->afc = analog_ops->get_afc(&t->fe);
842 if (t->mode == V4L2_TUNER_ANALOG_TV)
843 vt->capability |= V4L2_TUNER_CAP_NORM;
844 if (t->mode != V4L2_TUNER_RADIO) {
845 vt->rangelow = tv_range[0] * 16;
846 vt->rangehigh = tv_range[1] * 16;
852 V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
853 if (fe_tuner_ops->get_status) {
856 fe_tuner_ops->get_status(&t->fe, &tuner_status);
858 (tuner_status & TUNER_STATUS_STEREO) ?
859 V4L2_TUNER_SUB_STEREO :
862 if (analog_ops->has_signal)
863 vt->signal = analog_ops->has_signal(&t->fe);
865 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
866 vt->audmode = t->audmode;
867 vt->rangelow = radio_range[0] * 16000;
868 vt->rangehigh = radio_range[1] * 16000;
872 static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
874 struct tuner *t = to_tuner(sd);
875 struct i2c_client *client = v4l2_get_subdevdata(sd);
877 if (check_mode(t, "s_tuner") == -EINVAL)
880 /* do nothing unless we're a radio tuner */
881 if (t->mode != V4L2_TUNER_RADIO)
883 t->audmode = vt->audmode;
884 set_radio_freq(client, t->radio_freq);
888 static int tuner_log_status(struct v4l2_subdev *sd)
890 struct tuner *t = to_tuner(sd);
891 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
893 if (analog_ops->tuner_status)
894 analog_ops->tuner_status(&t->fe);
898 static int tuner_suspend(struct i2c_client *c, pm_message_t state)
900 struct tuner *t = to_tuner(i2c_get_clientdata(c));
902 tuner_dbg("suspend\n");
903 /* FIXME: power down ??? */
907 static int tuner_resume(struct i2c_client *c)
909 struct tuner *t = to_tuner(i2c_get_clientdata(c));
911 tuner_dbg("resume\n");
912 if (V4L2_TUNER_RADIO == t->mode) {
914 set_freq(c, t->radio_freq);
917 set_freq(c, t->tv_freq);
922 static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
924 struct v4l2_subdev *sd = i2c_get_clientdata(client);
926 /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
928 There must be a better way of doing this... */
930 case TUNER_SET_CONFIG:
931 return tuner_s_config(sd, arg);
936 /* ----------------------------------------------------------------------- */
938 static const struct v4l2_subdev_core_ops tuner_core_ops = {
939 .log_status = tuner_log_status,
940 .s_std = tuner_s_std,
941 .s_power = tuner_s_power,
944 static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
945 .s_radio = tuner_s_radio,
946 .g_tuner = tuner_g_tuner,
947 .s_tuner = tuner_s_tuner,
948 .s_frequency = tuner_s_frequency,
949 .g_frequency = tuner_g_frequency,
950 .s_type_addr = tuner_s_type_addr,
951 .s_config = tuner_s_config,
954 static const struct v4l2_subdev_ops tuner_ops = {
955 .core = &tuner_core_ops,
956 .tuner = &tuner_tuner_ops,
959 /* ---------------------------------------------------------------------- */
961 static LIST_HEAD(tuner_list);
963 /* Search for existing radio and/or TV tuners on the given I2C adapter.
964 Note that when this function is called from tuner_probe you can be
965 certain no other devices will be added/deleted at the same time, I2C
966 core protects against that. */
967 static void tuner_lookup(struct i2c_adapter *adap,
968 struct tuner **radio, struct tuner **tv)
975 list_for_each_entry(pos, &tuner_list, list) {
978 if (pos->i2c->adapter != adap ||
979 strcmp(pos->i2c->driver->driver.name, "tuner"))
982 mode_mask = pos->mode_mask & ~T_STANDBY;
983 if (*radio == NULL && mode_mask == T_RADIO)
985 /* Note: currently TDA9887 is the only demod-only
986 device. If other devices appear then we need to
987 make this test more general. */
988 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
989 (pos->mode_mask & (T_ANALOG_TV | T_DIGITAL_TV)))
994 /* During client attach, set_type is called by adapter's attach_inform callback.
995 set_type must then be completed by tuner_probe.
997 static int tuner_probe(struct i2c_client *client,
998 const struct i2c_device_id *id)
1001 struct tuner *radio;
1004 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
1007 v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
1009 t->name = "(tuner unset)";
1011 t->audmode = V4L2_TUNER_MODE_STEREO;
1012 t->mode_mask = T_UNINITIALIZED;
1015 unsigned char buffer[16];
1018 memset(buffer, 0, sizeof(buffer));
1019 rc = i2c_master_recv(client, buffer, sizeof(buffer));
1020 tuner_info("I2C RECV = ");
1021 for (i = 0; i < rc; i++)
1022 printk(KERN_CONT "%02x ", buffer[i]);
1026 /* autodetection code based on the i2c addr */
1027 if (!no_autodetect) {
1028 switch (client->addr) {
1030 if (tuner_symbol_probe(tea5761_autodetection,
1032 t->i2c->addr) >= 0) {
1033 t->type = TUNER_TEA5761;
1034 t->mode_mask = T_RADIO;
1035 t->mode = T_STANDBY;
1036 /* Sets freq to FM range */
1037 t->radio_freq = 87.5 * 16000;
1038 tuner_lookup(t->i2c->adapter, &radio, &tv);
1040 tv->mode_mask &= ~T_RADIO;
1042 goto register_client;
1050 /* If chip is not tda8290, don't register.
1051 since it can be tda9887*/
1052 if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
1053 t->i2c->addr) >= 0) {
1054 tuner_dbg("tda829x detected\n");
1056 /* Default is being tda9887 */
1057 t->type = TUNER_TDA9887;
1058 t->mode_mask = T_RADIO | T_ANALOG_TV |
1060 t->mode = T_STANDBY;
1061 goto register_client;
1065 if (tuner_symbol_probe(tea5767_autodetection,
1066 t->i2c->adapter, t->i2c->addr)
1068 t->type = TUNER_TEA5767;
1069 t->mode_mask = T_RADIO;
1070 t->mode = T_STANDBY;
1071 /* Sets freq to FM range */
1072 t->radio_freq = 87.5 * 16000;
1073 tuner_lookup(t->i2c->adapter, &radio, &tv);
1075 tv->mode_mask &= ~T_RADIO;
1077 goto register_client;
1083 /* Initializes only the first TV tuner on this adapter. Why only the
1084 first? Because there are some devices (notably the ones with TI
1085 tuners) that have more than one i2c address for the *same* device.
1086 Experience shows that, except for just one case, the first
1087 address is the right one. The exception is a Russian tuner
1088 (ACORP_Y878F). So, the desired behavior is just to enable the
1089 first found TV tuner. */
1090 tuner_lookup(t->i2c->adapter, &radio, &tv);
1092 t->mode_mask = T_ANALOG_TV | T_DIGITAL_TV;
1094 t->mode_mask |= T_RADIO;
1095 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
1096 t->tv_freq = 400 * 16; /* Sets freq to VHF High */
1097 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
1100 /* Should be just before return */
1102 tuner_info("chip found @ 0x%x (%s)\n", client->addr << 1,
1103 client->adapter->name);
1105 /* Sets a default mode */
1106 if (t->mode_mask & T_ANALOG_TV) {
1107 t->mode = V4L2_TUNER_ANALOG_TV;
1108 } else if (t->mode_mask & T_RADIO) {
1109 t->mode = V4L2_TUNER_RADIO;
1111 t->mode = V4L2_TUNER_DIGITAL_TV;
1113 set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
1114 list_add_tail(&t->list, &tuner_list);
1118 static int tuner_remove(struct i2c_client *client)
1120 struct tuner *t = to_tuner(i2c_get_clientdata(client));
1122 v4l2_device_unregister_subdev(&t->sd);
1123 tuner_detach(&t->fe);
1124 t->fe.analog_demod_priv = NULL;
1131 /* ----------------------------------------------------------------------- */
1133 /* This driver supports many devices and the idea is to let the driver
1134 detect which device is present. So rather than listing all supported
1135 devices here, we pretend to support a single, fake device type. */
1136 static const struct i2c_device_id tuner_id[] = {
1137 { "tuner", }, /* autodetect */
1140 MODULE_DEVICE_TABLE(i2c, tuner_id);
1142 static struct i2c_driver tuner_driver = {
1144 .owner = THIS_MODULE,
1147 .probe = tuner_probe,
1148 .remove = tuner_remove,
1149 .command = tuner_command,
1150 .suspend = tuner_suspend,
1151 .resume = tuner_resume,
1152 .id_table = tuner_id,
1155 static __init int init_tuner(void)
1157 return i2c_add_driver(&tuner_driver);
1160 static __exit void exit_tuner(void)
1162 i2c_del_driver(&tuner_driver);
1165 module_init(init_tuner);
1166 module_exit(exit_tuner);
1169 * Overrides for Emacs so that we follow Linus's tabbing style.
1170 * ---------------------------------------------------------------------------