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 * This function apply tuner config to tuner specified
417 * by tun_setup structure. I addr is unset, then admin status
418 * and tun addr status is more precise then current status,
419 * it's applied. Otherwise status and type are applied only to
420 * tuner with exactly the same addr.
423 static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
425 struct tuner *t = to_tuner(i2c_get_clientdata(c));
427 if ((t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
428 (t->mode_mask & tun_setup->mode_mask))) ||
429 (tun_setup->addr == c->addr)) {
430 set_type(c, tun_setup->type, tun_setup->mode_mask,
431 tun_setup->config, tun_setup->tuner_callback);
433 tuner_dbg("set addr discarded for type %i, mask %x. "
434 "Asked to change tuner at addr 0x%02x, with mask %x\n",
435 t->type, t->mode_mask,
436 tun_setup->addr, tun_setup->mode_mask);
439 static int tuner_s_type_addr(struct v4l2_subdev *sd, struct tuner_setup *type)
441 struct tuner *t = to_tuner(sd);
442 struct i2c_client *client = v4l2_get_subdevdata(sd);
444 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
450 set_addr(client, type);
454 static int tuner_s_config(struct v4l2_subdev *sd,
455 const struct v4l2_priv_tun_config *cfg)
457 struct tuner *t = to_tuner(sd);
458 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
460 if (t->type != cfg->tuner)
463 if (analog_ops->set_config) {
464 analog_ops->set_config(&t->fe, cfg->priv);
468 tuner_dbg("Tuner frontend module has no way to set config\n");
472 /* Search for existing radio and/or TV tuners on the given I2C adapter.
473 Note that when this function is called from tuner_probe you can be
474 certain no other devices will be added/deleted at the same time, I2C
475 core protects against that. */
476 static void tuner_lookup(struct i2c_adapter *adap,
477 struct tuner **radio, struct tuner **tv)
484 list_for_each_entry(pos, &tuner_list, list) {
487 if (pos->i2c->adapter != adap ||
488 strcmp(pos->i2c->driver->driver.name, "tuner"))
491 mode_mask = pos->mode_mask;
493 if (*radio == NULL && mode_mask == T_RADIO)
495 /* Note: currently TDA9887 is the only demod-only
496 device. If other devices appear then we need to
497 make this test more general. */
498 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
499 (pos->mode_mask & (T_ANALOG_TV | T_DIGITAL_TV)))
504 /* During client attach, set_type is called by adapter's attach_inform callback.
505 set_type must then be completed by tuner_probe.
507 static int tuner_probe(struct i2c_client *client,
508 const struct i2c_device_id *id)
514 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
517 v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
519 t->name = "(tuner unset)";
521 t->audmode = V4L2_TUNER_MODE_STEREO;
523 t->radio_freq = 87.5 * 16000; /* Initial freq range */
524 t->tv_freq = 400 * 16; /* Sets freq to VHF High - needed for some PLL's to properly start */
527 unsigned char buffer[16];
530 memset(buffer, 0, sizeof(buffer));
531 rc = i2c_master_recv(client, buffer, sizeof(buffer));
532 tuner_info("I2C RECV = ");
533 for (i = 0; i < rc; i++)
534 printk(KERN_CONT "%02x ", buffer[i]);
538 /* autodetection code based on the i2c addr */
539 if (!no_autodetect) {
540 switch (client->addr) {
542 if (tuner_symbol_probe(tea5761_autodetection,
544 t->i2c->addr) >= 0) {
545 t->type = TUNER_TEA5761;
546 t->mode_mask = T_RADIO;
547 tuner_lookup(t->i2c->adapter, &radio, &tv);
549 tv->mode_mask &= ~T_RADIO;
551 goto register_client;
559 /* If chip is not tda8290, don't register.
560 since it can be tda9887*/
561 if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
562 t->i2c->addr) >= 0) {
563 tuner_dbg("tda829x detected\n");
565 /* Default is being tda9887 */
566 t->type = TUNER_TDA9887;
567 t->mode_mask = T_RADIO | T_ANALOG_TV |
569 goto register_client;
573 if (tuner_symbol_probe(tea5767_autodetection,
574 t->i2c->adapter, t->i2c->addr)
576 t->type = TUNER_TEA5767;
577 t->mode_mask = T_RADIO;
578 /* Sets freq to FM range */
579 tuner_lookup(t->i2c->adapter, &radio, &tv);
581 tv->mode_mask &= ~T_RADIO;
583 goto register_client;
589 /* Initializes only the first TV tuner on this adapter. Why only the
590 first? Because there are some devices (notably the ones with TI
591 tuners) that have more than one i2c address for the *same* device.
592 Experience shows that, except for just one case, the first
593 address is the right one. The exception is a Russian tuner
594 (ACORP_Y878F). So, the desired behavior is just to enable the
595 first found TV tuner. */
596 tuner_lookup(t->i2c->adapter, &radio, &tv);
598 t->mode_mask = T_ANALOG_TV | T_DIGITAL_TV;
600 t->mode_mask |= T_RADIO;
601 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
604 /* Should be just before return */
606 /* Sets a default mode */
607 if (t->mode_mask & T_ANALOG_TV)
608 t->mode = V4L2_TUNER_ANALOG_TV;
609 else if (t->mode_mask & T_RADIO)
610 t->mode = V4L2_TUNER_RADIO;
612 t->mode = V4L2_TUNER_DIGITAL_TV;
613 set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
614 list_add_tail(&t->list, &tuner_list);
616 tuner_info("Tuner %d found with type(s)%s%s%s.\n",
618 t->mode_mask & T_RADIO ? " radio" : "",
619 t->mode_mask & T_ANALOG_TV ? " TV" : "",
620 t->mode_mask & T_ANALOG_TV ? " DTV" : "");
624 static int tuner_remove(struct i2c_client *client)
626 struct tuner *t = to_tuner(i2c_get_clientdata(client));
628 v4l2_device_unregister_subdev(&t->sd);
629 tuner_detach(&t->fe);
630 t->fe.analog_demod_priv = NULL;
638 * Functions that are specific for TV mode
641 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
642 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
644 struct tuner *t = to_tuner(i2c_get_clientdata(c));
645 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
647 struct analog_parameters params = {
649 .audmode = t->audmode,
653 if (t->type == UNSET) {
654 tuner_warn("tuner type not set\n");
657 if (NULL == analog_ops->set_params) {
658 tuner_warn("Tuner has no way to set tv freq\n");
661 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
662 tuner_dbg("TV freq (%d.%02d) out of range (%d-%d)\n",
663 freq / 16, freq % 16 * 100 / 16, tv_range[0],
665 /* V4L2 spec: if the freq is not possible then the closest
666 possible value should be selected */
667 if (freq < tv_range[0] * 16)
668 freq = tv_range[0] * 16;
670 freq = tv_range[1] * 16;
672 params.frequency = freq;
673 tuner_dbg("tv freq set to %d.%02d\n",
674 freq / 16, freq % 16 * 100 / 16);
678 analog_ops->set_params(&t->fe, ¶ms);
681 /* get more precise norm info from insmod option */
682 static int tuner_fixup_std(struct tuner *t)
684 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
687 tuner_dbg("insmod fixup: PAL => PAL-60\n");
688 t->std = V4L2_STD_PAL_60;
694 tuner_dbg("insmod fixup: PAL => PAL-BG\n");
695 t->std = V4L2_STD_PAL_BG;
699 tuner_dbg("insmod fixup: PAL => PAL-I\n");
700 t->std = V4L2_STD_PAL_I;
706 tuner_dbg("insmod fixup: PAL => PAL-DK\n");
707 t->std = V4L2_STD_PAL_DK;
711 tuner_dbg("insmod fixup: PAL => PAL-M\n");
712 t->std = V4L2_STD_PAL_M;
716 if (pal[1] == 'c' || pal[1] == 'C') {
717 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
718 t->std = V4L2_STD_PAL_Nc;
720 tuner_dbg("insmod fixup: PAL => PAL-N\n");
721 t->std = V4L2_STD_PAL_N;
725 /* default parameter, do nothing */
728 tuner_warn("pal= argument not recognised\n");
732 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
740 tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
741 t->std = V4L2_STD_SECAM_B |
749 tuner_dbg("insmod fixup: SECAM => SECAM-DK\n");
750 t->std = V4L2_STD_SECAM_DK;
754 if ((secam[1] == 'C') || (secam[1] == 'c')) {
755 tuner_dbg("insmod fixup: SECAM => SECAM-L'\n");
756 t->std = V4L2_STD_SECAM_LC;
758 tuner_dbg("insmod fixup: SECAM => SECAM-L\n");
759 t->std = V4L2_STD_SECAM_L;
763 /* default parameter, do nothing */
766 tuner_warn("secam= argument not recognised\n");
771 if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
775 tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
776 t->std = V4L2_STD_NTSC_M;
780 tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
781 t->std = V4L2_STD_NTSC_M_JP;
785 tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
786 t->std = V4L2_STD_NTSC_M_KR;
789 /* default parameter, do nothing */
792 tuner_info("ntsc= argument not recognised\n");
800 * Functions that are specific for Radio mode
803 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
805 struct tuner *t = to_tuner(i2c_get_clientdata(c));
806 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
808 struct analog_parameters params = {
810 .audmode = t->audmode,
814 if (t->type == UNSET) {
815 tuner_warn("tuner type not set\n");
818 if (NULL == analog_ops->set_params) {
819 tuner_warn("tuner has no way to set radio frequency\n");
822 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
823 tuner_dbg("radio freq (%d.%02d) out of range (%d-%d)\n",
824 freq / 16000, freq % 16000 * 100 / 16000,
825 radio_range[0], radio_range[1]);
826 /* V4L2 spec: if the freq is not possible then the closest
827 possible value should be selected */
828 if (freq < radio_range[0] * 16000)
829 freq = radio_range[0] * 16000;
831 freq = radio_range[1] * 16000;
833 params.frequency = freq;
834 tuner_dbg("radio freq set to %d.%02d\n",
835 freq / 16000, freq % 16000 * 100 / 16000);
836 t->radio_freq = freq;
839 analog_ops->set_params(&t->fe, ¶ms);
843 * check_mode - Verify if tuner supports the requested mode
844 * @t: a pointer to the module's internal struct_tuner
846 * This function checks if the tuner is capable of tuning analog TV,
847 * digital TV or radio, depending on what the caller wants. If the
848 * tuner can't support that mode, it returns -EINVAL. Otherwise, it
850 * This function is needed for boards that have a separate tuner for
851 * radio (like devices with tea5767).
853 static inline int check_mode(struct tuner *t, enum v4l2_tuner_type mode)
855 if ((1 << mode & t->mode_mask) == 0)
862 * set_mode_freq - Switch tuner to other mode.
863 * @client: struct i2c_client pointer
864 * @t: a pointer to the module's internal struct_tuner
865 * @mode: enum v4l2_type (radio or TV)
866 * @freq: frequency to set (0 means to use the previous one)
868 * If tuner doesn't support the needed mode (radio or TV), prints a
869 * debug message and returns -EINVAL, changing internal state to T_STANDBY.
870 * Otherwise, changes the state and sets frequency to the last value, if
871 * the tuner can sleep or if it supports both Radio and TV.
873 static int set_mode_freq(struct i2c_client *client, struct tuner *t,
874 enum v4l2_tuner_type mode, unsigned int freq)
876 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
878 if (mode != t->mode) {
879 if (check_mode(t, mode) == -EINVAL) {
880 tuner_dbg("Tuner doesn't support mode %d. "
881 "Putting tuner to sleep\n", mode);
883 if (analog_ops->standby)
884 analog_ops->standby(&t->fe);
888 tuner_dbg("Changing to mode %d\n", mode);
890 if (t->mode == V4L2_TUNER_RADIO) {
892 t->radio_freq = freq;
893 set_radio_freq(client, t->radio_freq);
897 set_tv_freq(client, t->tv_freq);
904 * Functions that should be broken into separate radio/TV functions
907 static void set_freq(struct i2c_client *c, unsigned long freq)
909 struct tuner *t = to_tuner(i2c_get_clientdata(c));
912 case V4L2_TUNER_RADIO:
913 set_radio_freq(c, freq);
915 case V4L2_TUNER_ANALOG_TV:
916 case V4L2_TUNER_DIGITAL_TV:
917 set_tv_freq(c, freq);
920 tuner_dbg("freq set: unknown mode: 0x%04x!\n", t->mode);
925 * tuner_status - Dumps the current tuner status at dmesg
926 * @fe: pointer to struct dvb_frontend
928 * This callback is used only for driver debug purposes, answering to
929 * VIDIOC_LOG_STATUS. No changes should happen on this call.
931 static void tuner_status(struct dvb_frontend *fe)
933 struct tuner *t = fe->analog_demod_priv;
934 unsigned long freq, freq_fraction;
935 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
936 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
940 case V4L2_TUNER_RADIO:
943 case V4L2_TUNER_DIGITAL_TV:
946 case V4L2_TUNER_ANALOG_TV:
951 if (t->mode == V4L2_TUNER_RADIO) {
952 freq = t->radio_freq / 16000;
953 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
955 freq = t->tv_freq / 16;
956 freq_fraction = (t->tv_freq % 16) * 100 / 16;
958 tuner_info("Tuner mode: %s%s\n", p,
959 t->standby ? " on standby mode" : "");
960 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
961 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
962 if (t->mode != V4L2_TUNER_RADIO)
964 if (fe_tuner_ops->get_status) {
967 fe_tuner_ops->get_status(&t->fe, &tuner_status);
968 if (tuner_status & TUNER_STATUS_LOCKED)
969 tuner_info("Tuner is locked.\n");
970 if (tuner_status & TUNER_STATUS_STEREO)
971 tuner_info("Stereo: yes\n");
973 if (analog_ops->has_signal)
974 tuner_info("Signal strength: %d\n",
975 analog_ops->has_signal(fe));
979 * tuner_s_power - controls the power state of the tuner
980 * @sd: pointer to struct v4l2_subdev
981 * @on: a zero value puts the tuner to sleep
983 static int tuner_s_power(struct v4l2_subdev *sd, int on)
985 struct tuner *t = to_tuner(sd);
986 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
988 /* FIXME: Why this function don't wake the tuner if on != 0 ? */
992 tuner_dbg("Putting tuner to sleep\n");
994 if (analog_ops->standby)
995 analog_ops->standby(&t->fe);
999 /* ---------------------------------------------------------------------- */
1001 static int tuner_s_radio(struct v4l2_subdev *sd)
1003 struct tuner *t = to_tuner(sd);
1004 struct i2c_client *client = v4l2_get_subdevdata(sd);
1006 if (set_mode_freq(client, t, V4L2_TUNER_RADIO, 0) == -EINVAL)
1011 /* --- v4l ioctls --- */
1012 /* take care: bttv does userspace copying, we'll get a
1013 kernel pointer here... */
1014 static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1016 struct tuner *t = to_tuner(sd);
1017 struct i2c_client *client = v4l2_get_subdevdata(sd);
1019 if (set_mode_freq(client, t, V4L2_TUNER_ANALOG_TV, 0) == -EINVAL)
1028 static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1030 struct tuner *t = to_tuner(sd);
1031 struct i2c_client *client = v4l2_get_subdevdata(sd);
1033 if (set_mode_freq(client, t, f->type, f->frequency) == -EINVAL)
1039 static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1041 struct tuner *t = to_tuner(sd);
1042 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1044 if (check_mode(t, f->type) == -EINVAL)
1047 if (fe_tuner_ops->get_frequency && !t->standby) {
1050 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
1051 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
1052 DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
1053 DIV_ROUND_CLOSEST(abs_freq, 62500);
1055 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
1056 t->radio_freq : t->tv_freq;
1061 static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1063 struct tuner *t = to_tuner(sd);
1064 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1065 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1067 if (check_mode(t, vt->type) == -EINVAL)
1070 if (analog_ops->get_afc)
1071 vt->afc = analog_ops->get_afc(&t->fe);
1072 if (t->mode == V4L2_TUNER_ANALOG_TV)
1073 vt->capability |= V4L2_TUNER_CAP_NORM;
1074 if (t->mode != V4L2_TUNER_RADIO) {
1075 vt->rangelow = tv_range[0] * 16;
1076 vt->rangehigh = tv_range[1] * 16;
1081 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1082 if (fe_tuner_ops->get_status) {
1085 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1087 (tuner_status & TUNER_STATUS_STEREO) ?
1088 V4L2_TUNER_SUB_STEREO :
1089 V4L2_TUNER_SUB_MONO;
1091 if (analog_ops->has_signal)
1092 vt->signal = analog_ops->has_signal(&t->fe);
1093 vt->capability |= V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1094 vt->audmode = t->audmode;
1095 vt->rangelow = radio_range[0] * 16000;
1096 vt->rangehigh = radio_range[1] * 16000;
1101 static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1103 struct tuner *t = to_tuner(sd);
1104 struct i2c_client *client = v4l2_get_subdevdata(sd);
1106 if (set_mode_freq(client, t, vt->type, 0) == -EINVAL)
1109 if (t->mode == V4L2_TUNER_RADIO)
1110 t->audmode = vt->audmode;
1115 static int tuner_log_status(struct v4l2_subdev *sd)
1117 struct tuner *t = to_tuner(sd);
1118 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1120 if (analog_ops->tuner_status)
1121 analog_ops->tuner_status(&t->fe);
1125 static int tuner_suspend(struct i2c_client *c, pm_message_t state)
1127 struct tuner *t = to_tuner(i2c_get_clientdata(c));
1128 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1130 tuner_dbg("suspend\n");
1132 if (!t->standby && analog_ops->standby)
1133 analog_ops->standby(&t->fe);
1138 static int tuner_resume(struct i2c_client *c)
1140 struct tuner *t = to_tuner(i2c_get_clientdata(c));
1142 tuner_dbg("resume\n");
1145 set_mode_freq(c, t, t->type, 0);
1150 static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
1152 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1154 /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
1156 There must be a better way of doing this... */
1158 case TUNER_SET_CONFIG:
1159 return tuner_s_config(sd, arg);
1161 return -ENOIOCTLCMD;
1164 /* ----------------------------------------------------------------------- */
1166 static const struct v4l2_subdev_core_ops tuner_core_ops = {
1167 .log_status = tuner_log_status,
1168 .s_std = tuner_s_std,
1169 .s_power = tuner_s_power,
1172 static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
1173 .s_radio = tuner_s_radio,
1174 .g_tuner = tuner_g_tuner,
1175 .s_tuner = tuner_s_tuner,
1176 .s_frequency = tuner_s_frequency,
1177 .g_frequency = tuner_g_frequency,
1178 .s_type_addr = tuner_s_type_addr,
1179 .s_config = tuner_s_config,
1182 static const struct v4l2_subdev_ops tuner_ops = {
1183 .core = &tuner_core_ops,
1184 .tuner = &tuner_tuner_ops,
1187 /* ----------------------------------------------------------------------- */
1189 /* This driver supports many devices and the idea is to let the driver
1190 detect which device is present. So rather than listing all supported
1191 devices here, we pretend to support a single, fake device type. */
1192 static const struct i2c_device_id tuner_id[] = {
1193 { "tuner", }, /* autodetect */
1196 MODULE_DEVICE_TABLE(i2c, tuner_id);
1198 static struct i2c_driver tuner_driver = {
1200 .owner = THIS_MODULE,
1203 .probe = tuner_probe,
1204 .remove = tuner_remove,
1205 .command = tuner_command,
1206 .suspend = tuner_suspend,
1207 .resume = tuner_resume,
1208 .id_table = tuner_id,
1211 static __init int init_tuner(void)
1213 return i2c_add_driver(&tuner_driver);
1216 static __exit void exit_tuner(void)
1218 i2c_del_driver(&tuner_driver);
1221 module_init(init_tuner);
1222 module_exit(exit_tuner);
1224 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
1225 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1226 MODULE_LICENSE("GPL");