[media] tuner-core: Fix a few comments on it
[linux-2.6.git] / drivers / media / video / tuner-core.c
blobdcf03face265e45ffa1d880873b99c597deda3b2
1 /*
3 * i2c tv tuner chip device driver
4 * core core, i.e. kernel interfaces, registering and so on
5 */
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 "mt20xx.h"
24 #include "tda8290.h"
25 #include "tea5761.h"
26 #include "tea5767.h"
27 #include "tuner-xc2028.h"
28 #include "tuner-simple.h"
29 #include "tda9887.h"
30 #include "xc5000.h"
31 #include "tda18271.h"
33 #define UNSET (-1U)
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);
66 * Static vars
69 static LIST_HEAD(tuner_list);
72 * Debug macros
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); \
79 } while (0)
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); \
85 } while (0)
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); \
91 } while (0)
93 #define tuner_dbg(fmt, arg...) do { \
94 if (tuner_debug) \
95 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX, \
96 i2c_adapter_id(t->i2c->adapter), \
97 t->i2c->addr, ##arg); \
98 } while (0)
101 * Internal struct used inside the driver
104 struct tuner {
105 /* device */
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 */
112 v4l2_std_id std;
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 */
123 unsigned int config;
124 const char *name;
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...) ({ \
134 int __r = -EINVAL; \
135 typeof(&FUNCTION) __a = symbol_request(FUNCTION); \
136 if (__a) { \
137 __r = (int) __a(ARGS); \
138 symbol_put(FUNCTION); \
139 } else { \
140 printk(KERN_ERR "TUNER: Unable to find " \
141 "symbol "#FUNCTION"()\n"); \
143 __r; \
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);
157 #else
158 #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
159 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);
169 #endif
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");
189 return;
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)
204 u16 strength = 0;
206 if (fe->ops.tuner_ops.get_rf_strength)
207 fe->ops.tuner_ops.get_rf_strength(fe, &strength);
209 return 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");
222 return 0;
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];
251 int tune_now = 1;
253 if (type == UNSET || type == TUNER_ABSENT) {
254 tuner_dbg("tuner 0x%02x: Tuner type absent\n", c->addr);
255 return;
258 t->type = type;
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;
270 switch (t->type) {
271 case TUNER_MT2032:
272 if (!dvb_attach(microtune_attach,
273 &t->fe, t->i2c->adapter, t->i2c->addr))
274 goto attach_failed;
275 break;
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,
282 t->i2c->addr, &cfg))
283 goto attach_failed;
284 break;
286 case TUNER_TEA5767:
287 if (!dvb_attach(tea5767_attach, &t->fe,
288 t->i2c->adapter, t->i2c->addr))
289 goto attach_failed;
290 t->mode_mask = T_RADIO;
291 break;
292 case TUNER_TEA5761:
293 if (!dvb_attach(tea5761_attach, &t->fe,
294 t->i2c->adapter, t->i2c->addr))
295 goto attach_failed;
296 t->mode_mask = T_RADIO;
297 break;
298 case TUNER_PHILIPS_FMD1216ME_MK3:
299 buffer[0] = 0x0b;
300 buffer[1] = 0xdc;
301 buffer[2] = 0x9c;
302 buffer[3] = 0x60;
303 i2c_master_send(c, buffer, 4);
304 mdelay(1);
305 buffer[2] = 0x86;
306 buffer[3] = 0x54;
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))
310 goto attach_failed;
311 break;
312 case TUNER_PHILIPS_TD1316:
313 buffer[0] = 0x0b;
314 buffer[1] = 0xdc;
315 buffer[2] = 0x86;
316 buffer[3] = 0xa4;
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))
320 goto attach_failed;
321 break;
322 case TUNER_XC2028:
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))
329 goto attach_failed;
330 tune_now = 0;
331 break;
333 case TUNER_TDA9887:
334 if (!dvb_attach(tda9887_attach,
335 &t->fe, t->i2c->adapter, t->i2c->addr))
336 goto attach_failed;
337 break;
338 case TUNER_XC5000:
340 struct xc5000_config xc5000_cfg = {
341 .i2c_address = t->i2c->addr,
342 /* if_khz will be set at dvb_attach() */
343 .if_khz = 0,
346 if (!dvb_attach(xc5000_attach,
347 &t->fe, t->i2c->adapter, &xc5000_cfg))
348 goto attach_failed;
349 tune_now = 0;
350 break;
352 case TUNER_NXP_TDA18271:
354 struct tda18271_config cfg = {
355 .config = t->config,
356 .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
359 if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr,
360 t->i2c->adapter, &cfg))
361 goto attach_failed;
362 tune_now = 0;
363 break;
365 default:
366 if (!dvb_attach(simple_tuner_attach, &t->fe,
367 t->i2c->adapter, t->i2c->addr, t->type))
368 goto attach_failed;
370 break;
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));
382 } else {
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
396 if (tune_now) {
397 if (V4L2_TUNER_RADIO == t->mode)
398 set_radio_freq(c, t->radio_freq);
399 else
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,
405 t->mode_mask);
406 return;
408 attach_failed:
409 tuner_dbg("Tuner attach for type = %d failed.\n", t->type);
410 t->type = TUNER_ABSENT;
412 return;
416 * tuner_s_type_addr - Sets the tuner type for a device
418 * @sd: subdev descriptor
419 * @tun_setup: type to be associated to a given tuner i2c address
421 * This function applys the tuner config to tuner specified
422 * by tun_setup structure.
423 * If tuner I2C address is UNSET, then it will only set the device
424 * if the tuner supports the mode specified in the call.
425 * If the address is specified, the change will be applied only if
426 * tuner I2C address matches.
427 * The call can change the tuner number and the tuner mode.
429 static int tuner_s_type_addr(struct v4l2_subdev *sd,
430 struct tuner_setup *tun_setup)
432 struct tuner *t = to_tuner(sd);
433 struct i2c_client *c = v4l2_get_subdevdata(sd);
435 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
436 tun_setup->type,
437 tun_setup->addr,
438 tun_setup->mode_mask,
439 tun_setup->config);
441 if ((t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
442 (t->mode_mask & tun_setup->mode_mask))) ||
443 (tun_setup->addr == c->addr)) {
444 set_type(c, tun_setup->type, tun_setup->mode_mask,
445 tun_setup->config, tun_setup->tuner_callback);
446 } else
447 tuner_dbg("set addr discarded for type %i, mask %x. "
448 "Asked to change tuner at addr 0x%02x, with mask %x\n",
449 t->type, t->mode_mask,
450 tun_setup->addr, tun_setup->mode_mask);
452 return 0;
455 static int tuner_s_config(struct v4l2_subdev *sd,
456 const struct v4l2_priv_tun_config *cfg)
458 struct tuner *t = to_tuner(sd);
459 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
461 if (t->type != cfg->tuner)
462 return 0;
464 if (analog_ops->set_config) {
465 analog_ops->set_config(&t->fe, cfg->priv);
466 return 0;
469 tuner_dbg("Tuner frontend module has no way to set config\n");
470 return 0;
473 /* Search for existing radio and/or TV tuners on the given I2C adapter.
474 Note that when this function is called from tuner_probe you can be
475 certain no other devices will be added/deleted at the same time, I2C
476 core protects against that. */
477 static void tuner_lookup(struct i2c_adapter *adap,
478 struct tuner **radio, struct tuner **tv)
480 struct tuner *pos;
482 *radio = NULL;
483 *tv = NULL;
485 list_for_each_entry(pos, &tuner_list, list) {
486 int mode_mask;
488 if (pos->i2c->adapter != adap ||
489 strcmp(pos->i2c->driver->driver.name, "tuner"))
490 continue;
492 mode_mask = pos->mode_mask;
493 pos->standby = 1;
494 if (*radio == NULL && mode_mask == T_RADIO)
495 *radio = pos;
496 /* Note: currently TDA9887 is the only demod-only
497 device. If other devices appear then we need to
498 make this test more general. */
499 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
500 (pos->mode_mask & (T_ANALOG_TV | T_DIGITAL_TV)))
501 *tv = pos;
505 /* During client attach, set_type is called by adapter's attach_inform callback.
506 set_type must then be completed by tuner_probe.
508 static int tuner_probe(struct i2c_client *client,
509 const struct i2c_device_id *id)
511 struct tuner *t;
512 struct tuner *radio;
513 struct tuner *tv;
515 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
516 if (NULL == t)
517 return -ENOMEM;
518 v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
519 t->i2c = client;
520 t->name = "(tuner unset)";
521 t->type = UNSET;
522 t->audmode = V4L2_TUNER_MODE_STEREO;
523 t->standby = 1;
524 t->radio_freq = 87.5 * 16000; /* Initial freq range */
525 t->tv_freq = 400 * 16; /* Sets freq to VHF High - needed for some PLL's to properly start */
527 if (show_i2c) {
528 unsigned char buffer[16];
529 int i, rc;
531 memset(buffer, 0, sizeof(buffer));
532 rc = i2c_master_recv(client, buffer, sizeof(buffer));
533 tuner_info("I2C RECV = ");
534 for (i = 0; i < rc; i++)
535 printk(KERN_CONT "%02x ", buffer[i]);
536 printk("\n");
539 /* autodetection code based on the i2c addr */
540 if (!no_autodetect) {
541 switch (client->addr) {
542 case 0x10:
543 if (tuner_symbol_probe(tea5761_autodetection,
544 t->i2c->adapter,
545 t->i2c->addr) >= 0) {
546 t->type = TUNER_TEA5761;
547 t->mode_mask = T_RADIO;
548 tuner_lookup(t->i2c->adapter, &radio, &tv);
549 if (tv)
550 tv->mode_mask &= ~T_RADIO;
552 goto register_client;
554 kfree(t);
555 return -ENODEV;
556 case 0x42:
557 case 0x43:
558 case 0x4a:
559 case 0x4b:
560 /* If chip is not tda8290, don't register.
561 since it can be tda9887*/
562 if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
563 t->i2c->addr) >= 0) {
564 tuner_dbg("tda829x detected\n");
565 } else {
566 /* Default is being tda9887 */
567 t->type = TUNER_TDA9887;
568 t->mode_mask = T_RADIO | T_ANALOG_TV |
569 T_DIGITAL_TV;
570 goto register_client;
572 break;
573 case 0x60:
574 if (tuner_symbol_probe(tea5767_autodetection,
575 t->i2c->adapter, t->i2c->addr)
576 >= 0) {
577 t->type = TUNER_TEA5767;
578 t->mode_mask = T_RADIO;
579 /* Sets freq to FM range */
580 tuner_lookup(t->i2c->adapter, &radio, &tv);
581 if (tv)
582 tv->mode_mask &= ~T_RADIO;
584 goto register_client;
586 break;
590 /* Initializes only the first TV tuner on this adapter. Why only the
591 first? Because there are some devices (notably the ones with TI
592 tuners) that have more than one i2c address for the *same* device.
593 Experience shows that, except for just one case, the first
594 address is the right one. The exception is a Russian tuner
595 (ACORP_Y878F). So, the desired behavior is just to enable the
596 first found TV tuner. */
597 tuner_lookup(t->i2c->adapter, &radio, &tv);
598 if (tv == NULL) {
599 t->mode_mask = T_ANALOG_TV | T_DIGITAL_TV;
600 if (radio == NULL)
601 t->mode_mask |= T_RADIO;
602 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
605 /* Should be just before return */
606 register_client:
607 /* Sets a default mode */
608 if (t->mode_mask & T_ANALOG_TV)
609 t->mode = V4L2_TUNER_ANALOG_TV;
610 else if (t->mode_mask & T_RADIO)
611 t->mode = V4L2_TUNER_RADIO;
612 else
613 t->mode = V4L2_TUNER_DIGITAL_TV;
614 set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
615 list_add_tail(&t->list, &tuner_list);
617 tuner_info("Tuner %d found with type(s)%s%s%s.\n",
618 t->type,
619 t->mode_mask & T_RADIO ? " radio" : "",
620 t->mode_mask & T_ANALOG_TV ? " TV" : "",
621 t->mode_mask & T_ANALOG_TV ? " DTV" : "");
622 return 0;
625 static int tuner_remove(struct i2c_client *client)
627 struct tuner *t = to_tuner(i2c_get_clientdata(client));
629 v4l2_device_unregister_subdev(&t->sd);
630 tuner_detach(&t->fe);
631 t->fe.analog_demod_priv = NULL;
633 list_del(&t->list);
634 kfree(t);
635 return 0;
639 * Functions that are specific for TV mode
642 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
643 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
645 struct tuner *t = to_tuner(i2c_get_clientdata(c));
646 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
648 struct analog_parameters params = {
649 .mode = t->mode,
650 .audmode = t->audmode,
651 .std = t->std
654 if (t->type == UNSET) {
655 tuner_warn("tuner type not set\n");
656 return;
658 if (NULL == analog_ops->set_params) {
659 tuner_warn("Tuner has no way to set tv freq\n");
660 return;
662 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
663 tuner_dbg("TV freq (%d.%02d) out of range (%d-%d)\n",
664 freq / 16, freq % 16 * 100 / 16, tv_range[0],
665 tv_range[1]);
666 /* V4L2 spec: if the freq is not possible then the closest
667 possible value should be selected */
668 if (freq < tv_range[0] * 16)
669 freq = tv_range[0] * 16;
670 else
671 freq = tv_range[1] * 16;
673 params.frequency = freq;
674 tuner_dbg("tv freq set to %d.%02d\n",
675 freq / 16, freq % 16 * 100 / 16);
676 t->tv_freq = freq;
677 t->standby = false;
679 analog_ops->set_params(&t->fe, &params);
682 /* get more precise norm info from insmod option */
683 static int tuner_fixup_std(struct tuner *t)
685 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
686 switch (pal[0]) {
687 case '6':
688 tuner_dbg("insmod fixup: PAL => PAL-60\n");
689 t->std = V4L2_STD_PAL_60;
690 break;
691 case 'b':
692 case 'B':
693 case 'g':
694 case 'G':
695 tuner_dbg("insmod fixup: PAL => PAL-BG\n");
696 t->std = V4L2_STD_PAL_BG;
697 break;
698 case 'i':
699 case 'I':
700 tuner_dbg("insmod fixup: PAL => PAL-I\n");
701 t->std = V4L2_STD_PAL_I;
702 break;
703 case 'd':
704 case 'D':
705 case 'k':
706 case 'K':
707 tuner_dbg("insmod fixup: PAL => PAL-DK\n");
708 t->std = V4L2_STD_PAL_DK;
709 break;
710 case 'M':
711 case 'm':
712 tuner_dbg("insmod fixup: PAL => PAL-M\n");
713 t->std = V4L2_STD_PAL_M;
714 break;
715 case 'N':
716 case 'n':
717 if (pal[1] == 'c' || pal[1] == 'C') {
718 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
719 t->std = V4L2_STD_PAL_Nc;
720 } else {
721 tuner_dbg("insmod fixup: PAL => PAL-N\n");
722 t->std = V4L2_STD_PAL_N;
724 break;
725 case '-':
726 /* default parameter, do nothing */
727 break;
728 default:
729 tuner_warn("pal= argument not recognised\n");
730 break;
733 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
734 switch (secam[0]) {
735 case 'b':
736 case 'B':
737 case 'g':
738 case 'G':
739 case 'h':
740 case 'H':
741 tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
742 t->std = V4L2_STD_SECAM_B |
743 V4L2_STD_SECAM_G |
744 V4L2_STD_SECAM_H;
745 break;
746 case 'd':
747 case 'D':
748 case 'k':
749 case 'K':
750 tuner_dbg("insmod fixup: SECAM => SECAM-DK\n");
751 t->std = V4L2_STD_SECAM_DK;
752 break;
753 case 'l':
754 case 'L':
755 if ((secam[1] == 'C') || (secam[1] == 'c')) {
756 tuner_dbg("insmod fixup: SECAM => SECAM-L'\n");
757 t->std = V4L2_STD_SECAM_LC;
758 } else {
759 tuner_dbg("insmod fixup: SECAM => SECAM-L\n");
760 t->std = V4L2_STD_SECAM_L;
762 break;
763 case '-':
764 /* default parameter, do nothing */
765 break;
766 default:
767 tuner_warn("secam= argument not recognised\n");
768 break;
772 if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
773 switch (ntsc[0]) {
774 case 'm':
775 case 'M':
776 tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
777 t->std = V4L2_STD_NTSC_M;
778 break;
779 case 'j':
780 case 'J':
781 tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
782 t->std = V4L2_STD_NTSC_M_JP;
783 break;
784 case 'k':
785 case 'K':
786 tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
787 t->std = V4L2_STD_NTSC_M_KR;
788 break;
789 case '-':
790 /* default parameter, do nothing */
791 break;
792 default:
793 tuner_info("ntsc= argument not recognised\n");
794 break;
797 return 0;
801 * Functions that are specific for Radio mode
804 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
806 struct tuner *t = to_tuner(i2c_get_clientdata(c));
807 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
809 struct analog_parameters params = {
810 .mode = t->mode,
811 .audmode = t->audmode,
812 .std = t->std
815 if (t->type == UNSET) {
816 tuner_warn("tuner type not set\n");
817 return;
819 if (NULL == analog_ops->set_params) {
820 tuner_warn("tuner has no way to set radio frequency\n");
821 return;
823 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
824 tuner_dbg("radio freq (%d.%02d) out of range (%d-%d)\n",
825 freq / 16000, freq % 16000 * 100 / 16000,
826 radio_range[0], radio_range[1]);
827 /* V4L2 spec: if the freq is not possible then the closest
828 possible value should be selected */
829 if (freq < radio_range[0] * 16000)
830 freq = radio_range[0] * 16000;
831 else
832 freq = radio_range[1] * 16000;
834 params.frequency = freq;
835 tuner_dbg("radio freq set to %d.%02d\n",
836 freq / 16000, freq % 16000 * 100 / 16000);
837 t->radio_freq = freq;
838 t->standby = false;
840 analog_ops->set_params(&t->fe, &params);
844 * check_mode - Verify if tuner supports the requested mode
845 * @t: a pointer to the module's internal struct_tuner
847 * This function checks if the tuner is capable of tuning analog TV,
848 * digital TV or radio, depending on what the caller wants. If the
849 * tuner can't support that mode, it returns -EINVAL. Otherwise, it
850 * returns 0.
851 * This function is needed for boards that have a separate tuner for
852 * radio (like devices with tea5767).
854 static inline int check_mode(struct tuner *t, enum v4l2_tuner_type mode)
856 if ((1 << mode & t->mode_mask) == 0)
857 return -EINVAL;
859 return 0;
863 * set_mode_freq - Switch tuner to other mode.
864 * @client: struct i2c_client pointer
865 * @t: a pointer to the module's internal struct_tuner
866 * @mode: enum v4l2_type (radio or TV)
867 * @freq: frequency to set (0 means to use the previous one)
869 * If tuner doesn't support the needed mode (radio or TV), prints a
870 * debug message and returns -EINVAL, changing internal state to T_STANDBY.
871 * Otherwise, changes the state and sets frequency to the last value, if
872 * the tuner can sleep or if it supports both Radio and TV.
874 static int set_mode_freq(struct i2c_client *client, struct tuner *t,
875 enum v4l2_tuner_type mode, unsigned int freq)
877 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
879 if (mode != t->mode) {
880 if (check_mode(t, mode) == -EINVAL) {
881 tuner_dbg("Tuner doesn't support mode %d. "
882 "Putting tuner to sleep\n", mode);
883 t->standby = true;
884 if (analog_ops->standby)
885 analog_ops->standby(&t->fe);
886 return -EINVAL;
888 t->mode = mode;
889 tuner_dbg("Changing to mode %d\n", mode);
891 if (t->mode == V4L2_TUNER_RADIO) {
892 if (freq)
893 t->radio_freq = freq;
894 set_radio_freq(client, t->radio_freq);
895 } else {
896 if (freq)
897 t->tv_freq = freq;
898 set_tv_freq(client, t->tv_freq);
901 return 0;
905 * tuner_status - Dumps the current tuner status at dmesg
906 * @fe: pointer to struct dvb_frontend
908 * This callback is used only for driver debug purposes, answering to
909 * VIDIOC_LOG_STATUS. No changes should happen on this call.
911 static void tuner_status(struct dvb_frontend *fe)
913 struct tuner *t = fe->analog_demod_priv;
914 unsigned long freq, freq_fraction;
915 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
916 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
917 const char *p;
919 switch (t->mode) {
920 case V4L2_TUNER_RADIO:
921 p = "radio";
922 break;
923 case V4L2_TUNER_DIGITAL_TV:
924 p = "digital TV";
925 break;
926 case V4L2_TUNER_ANALOG_TV:
927 default:
928 p = "analog TV";
929 break;
931 if (t->mode == V4L2_TUNER_RADIO) {
932 freq = t->radio_freq / 16000;
933 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
934 } else {
935 freq = t->tv_freq / 16;
936 freq_fraction = (t->tv_freq % 16) * 100 / 16;
938 tuner_info("Tuner mode: %s%s\n", p,
939 t->standby ? " on standby mode" : "");
940 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
941 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
942 if (t->mode != V4L2_TUNER_RADIO)
943 return;
944 if (fe_tuner_ops->get_status) {
945 u32 tuner_status;
947 fe_tuner_ops->get_status(&t->fe, &tuner_status);
948 if (tuner_status & TUNER_STATUS_LOCKED)
949 tuner_info("Tuner is locked.\n");
950 if (tuner_status & TUNER_STATUS_STEREO)
951 tuner_info("Stereo: yes\n");
953 if (analog_ops->has_signal)
954 tuner_info("Signal strength: %d\n",
955 analog_ops->has_signal(fe));
959 * tuner_s_power - controls the power state of the tuner
960 * @sd: pointer to struct v4l2_subdev
961 * @on: a zero value puts the tuner to sleep
963 static int tuner_s_power(struct v4l2_subdev *sd, int on)
965 struct tuner *t = to_tuner(sd);
966 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
968 /* FIXME: Why this function don't wake the tuner if on != 0 ? */
969 if (on)
970 return 0;
972 tuner_dbg("Putting tuner to sleep\n");
973 t->standby = true;
974 if (analog_ops->standby)
975 analog_ops->standby(&t->fe);
976 return 0;
979 /* ---------------------------------------------------------------------- */
981 static int tuner_s_radio(struct v4l2_subdev *sd)
983 struct tuner *t = to_tuner(sd);
984 struct i2c_client *client = v4l2_get_subdevdata(sd);
986 if (set_mode_freq(client, t, V4L2_TUNER_RADIO, 0) == -EINVAL)
987 return 0;
988 return 0;
991 /* --- v4l ioctls --- */
992 /* take care: bttv does userspace copying, we'll get a
993 kernel pointer here... */
994 static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
996 struct tuner *t = to_tuner(sd);
997 struct i2c_client *client = v4l2_get_subdevdata(sd);
999 if (set_mode_freq(client, t, V4L2_TUNER_ANALOG_TV, 0) == -EINVAL)
1000 return 0;
1002 t->std = std;
1003 tuner_fixup_std(t);
1005 return 0;
1008 static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1010 struct tuner *t = to_tuner(sd);
1011 struct i2c_client *client = v4l2_get_subdevdata(sd);
1013 if (set_mode_freq(client, t, f->type, f->frequency) == -EINVAL)
1014 return 0;
1016 return 0;
1019 static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1021 struct tuner *t = to_tuner(sd);
1022 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1024 if (check_mode(t, f->type) == -EINVAL)
1025 return 0;
1026 f->type = t->mode;
1027 if (fe_tuner_ops->get_frequency && !t->standby) {
1028 u32 abs_freq;
1030 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
1031 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
1032 DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
1033 DIV_ROUND_CLOSEST(abs_freq, 62500);
1034 } else {
1035 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
1036 t->radio_freq : t->tv_freq;
1038 return 0;
1041 static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1043 struct tuner *t = to_tuner(sd);
1044 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1045 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1047 if (check_mode(t, vt->type) == -EINVAL)
1048 return 0;
1049 vt->type = t->mode;
1050 if (analog_ops->get_afc)
1051 vt->afc = analog_ops->get_afc(&t->fe);
1052 if (t->mode == V4L2_TUNER_ANALOG_TV)
1053 vt->capability |= V4L2_TUNER_CAP_NORM;
1054 if (t->mode != V4L2_TUNER_RADIO) {
1055 vt->rangelow = tv_range[0] * 16;
1056 vt->rangehigh = tv_range[1] * 16;
1057 return 0;
1060 /* radio mode */
1061 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1062 if (fe_tuner_ops->get_status) {
1063 u32 tuner_status;
1065 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1066 vt->rxsubchans =
1067 (tuner_status & TUNER_STATUS_STEREO) ?
1068 V4L2_TUNER_SUB_STEREO :
1069 V4L2_TUNER_SUB_MONO;
1071 if (analog_ops->has_signal)
1072 vt->signal = analog_ops->has_signal(&t->fe);
1073 vt->capability |= V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1074 vt->audmode = t->audmode;
1075 vt->rangelow = radio_range[0] * 16000;
1076 vt->rangehigh = radio_range[1] * 16000;
1078 return 0;
1081 static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1083 struct tuner *t = to_tuner(sd);
1084 struct i2c_client *client = v4l2_get_subdevdata(sd);
1086 if (set_mode_freq(client, t, vt->type, 0) == -EINVAL)
1087 return 0;
1089 if (t->mode == V4L2_TUNER_RADIO)
1090 t->audmode = vt->audmode;
1092 return 0;
1095 static int tuner_log_status(struct v4l2_subdev *sd)
1097 struct tuner *t = to_tuner(sd);
1098 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1100 if (analog_ops->tuner_status)
1101 analog_ops->tuner_status(&t->fe);
1102 return 0;
1105 static int tuner_suspend(struct i2c_client *c, pm_message_t state)
1107 struct tuner *t = to_tuner(i2c_get_clientdata(c));
1108 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1110 tuner_dbg("suspend\n");
1112 if (!t->standby && analog_ops->standby)
1113 analog_ops->standby(&t->fe);
1115 return 0;
1118 static int tuner_resume(struct i2c_client *c)
1120 struct tuner *t = to_tuner(i2c_get_clientdata(c));
1122 tuner_dbg("resume\n");
1124 if (!t->standby)
1125 set_mode_freq(c, t, t->type, 0);
1127 return 0;
1130 static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
1132 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1134 /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
1135 to handle it here.
1136 There must be a better way of doing this... */
1137 switch (cmd) {
1138 case TUNER_SET_CONFIG:
1139 return tuner_s_config(sd, arg);
1141 return -ENOIOCTLCMD;
1144 /* ----------------------------------------------------------------------- */
1146 static const struct v4l2_subdev_core_ops tuner_core_ops = {
1147 .log_status = tuner_log_status,
1148 .s_std = tuner_s_std,
1149 .s_power = tuner_s_power,
1152 static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
1153 .s_radio = tuner_s_radio,
1154 .g_tuner = tuner_g_tuner,
1155 .s_tuner = tuner_s_tuner,
1156 .s_frequency = tuner_s_frequency,
1157 .g_frequency = tuner_g_frequency,
1158 .s_type_addr = tuner_s_type_addr,
1159 .s_config = tuner_s_config,
1162 static const struct v4l2_subdev_ops tuner_ops = {
1163 .core = &tuner_core_ops,
1164 .tuner = &tuner_tuner_ops,
1167 /* ----------------------------------------------------------------------- */
1169 /* This driver supports many devices and the idea is to let the driver
1170 detect which device is present. So rather than listing all supported
1171 devices here, we pretend to support a single, fake device type. */
1172 static const struct i2c_device_id tuner_id[] = {
1173 { "tuner", }, /* autodetect */
1176 MODULE_DEVICE_TABLE(i2c, tuner_id);
1178 static struct i2c_driver tuner_driver = {
1179 .driver = {
1180 .owner = THIS_MODULE,
1181 .name = "tuner",
1183 .probe = tuner_probe,
1184 .remove = tuner_remove,
1185 .command = tuner_command,
1186 .suspend = tuner_suspend,
1187 .resume = tuner_resume,
1188 .id_table = tuner_id,
1191 static __init int init_tuner(void)
1193 return i2c_add_driver(&tuner_driver);
1196 static __exit void exit_tuner(void)
1198 i2c_del_driver(&tuner_driver);
1201 module_init(init_tuner);
1202 module_exit(exit_tuner);
1204 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
1205 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1206 MODULE_LICENSE("GPL");