aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video
diff options
context:
space:
mode:
authorMauro Carvalho Chehab <mchehab@redhat.com>2011-02-03 21:32:07 -0500
committerMauro Carvalho Chehab <mchehab@redhat.com>2011-03-21 19:32:01 -0400
commit9f3f71ef6d9a6179a16f82287f062cbf3f112ec6 (patch)
tree876e436a763bd3580ee2b36134c4480483a81a81 /drivers/media/video
parentb77bdb028807bb1f78b5b50127456e1f664bd501 (diff)
[media] tuner-core: Reorganize the functions internally
This is a big patch with no functional changes. It just rearranges everything inside the driver, and prepares to break TV and Radio into two separate fops groups. Currently, it has an heuristics logic to determine if the call came from radio or video. However, the caller driver knows for sure, so tuner-core shouldn't try to guess it. Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'drivers/media/video')
-rw-r--r--drivers/media/video/tuner-core.c893
1 files changed, 462 insertions, 431 deletions
diff --git a/drivers/media/video/tuner-core.c b/drivers/media/video/tuner-core.c
index f497f5299e88..5c3da21d188e 100644
--- a/drivers/media/video/tuner-core.c
+++ b/drivers/media/video/tuner-core.c
@@ -34,6 +34,99 @@
34 34
35#define PREFIX t->i2c->driver->driver.name 35#define PREFIX t->i2c->driver->driver.name
36 36
37/*
38 * Driver modprobe parameters
39 */
40
41/* insmod options used at init time => read/only */
42static unsigned int addr;
43static unsigned int no_autodetect;
44static unsigned int show_i2c;
45
46module_param(addr, int, 0444);
47module_param(no_autodetect, int, 0444);
48module_param(show_i2c, int, 0444);
49
50/* insmod options used at runtime => read/write */
51static int tuner_debug;
52static unsigned int tv_range[2] = { 44, 958 };
53static unsigned int radio_range[2] = { 65, 108 };
54static char pal[] = "--";
55static char secam[] = "--";
56static char ntsc[] = "-";
57
58module_param_named(debug,tuner_debug, int, 0644);
59module_param_array(tv_range, int, NULL, 0644);
60module_param_array(radio_range, int, NULL, 0644);
61module_param_string(pal, pal, sizeof(pal), 0644);
62module_param_string(secam, secam, sizeof(secam), 0644);
63module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
64
65/*
66 * Static vars
67 */
68
69static struct xc5000_config xc5000_cfg;
70static LIST_HEAD(tuner_list);
71
72/*
73 * Debug macros
74 */
75
76#define tuner_warn(fmt, arg...) do { \
77 printk(KERN_WARNING "%s %d-%04x: " fmt, PREFIX, \
78 i2c_adapter_id(t->i2c->adapter), \
79 t->i2c->addr, ##arg); \
80 } while (0)
81
82#define tuner_info(fmt, arg...) do { \
83 printk(KERN_INFO "%s %d-%04x: " fmt, PREFIX, \
84 i2c_adapter_id(t->i2c->adapter), \
85 t->i2c->addr, ##arg); \
86 } while (0)
87
88#define tuner_err(fmt, arg...) do { \
89 printk(KERN_ERR "%s %d-%04x: " fmt, PREFIX, \
90 i2c_adapter_id(t->i2c->adapter), \
91 t->i2c->addr, ##arg); \
92 } while (0)
93
94#define tuner_dbg(fmt, arg...) do { \
95 if (tuner_debug) \
96 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX, \
97 i2c_adapter_id(t->i2c->adapter), \
98 t->i2c->addr, ##arg); \
99 } while (0)
100
101/*
102 * Internal struct used inside the driver
103 */
104
105struct tuner {
106 /* device */
107 struct dvb_frontend fe;
108 struct i2c_client *i2c;
109 struct v4l2_subdev sd;
110 struct list_head list;
111
112 /* keep track of the current settings */
113 v4l2_std_id std;
114 unsigned int tv_freq;
115 unsigned int radio_freq;
116 unsigned int audmode;
117
118 unsigned int mode;
119 unsigned int mode_mask; /* Combination of allowable modes */
120
121 unsigned int type; /* chip type id */
122 unsigned int config;
123 const char *name;
124};
125
126/*
127 * tuner attach/detach logic
128 */
129
37/** This macro allows us to probe dynamically, avoiding static links */ 130/** This macro allows us to probe dynamically, avoiding static links */
38#ifdef CONFIG_MEDIA_ATTACH 131#ifdef CONFIG_MEDIA_ATTACH
39#define tuner_symbol_probe(FUNCTION, ARGS...) ({ \ 132#define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
@@ -74,91 +167,15 @@ static void tuner_detach(struct dvb_frontend *fe)
74} 167}
75#endif 168#endif
76 169
77struct tuner {
78 /* device */
79 struct dvb_frontend fe;
80 struct i2c_client *i2c;
81 struct v4l2_subdev sd;
82 struct list_head list;
83
84 /* keep track of the current settings */
85 v4l2_std_id std;
86 unsigned int tv_freq;
87 unsigned int radio_freq;
88 unsigned int audmode;
89
90 unsigned int mode;
91 unsigned int mode_mask; /* Combination of allowable modes */
92
93 unsigned int type; /* chip type id */
94 unsigned int config;
95 const char *name;
96};
97 170
98static inline struct tuner *to_tuner(struct v4l2_subdev *sd) 171static inline struct tuner *to_tuner(struct v4l2_subdev *sd)
99{ 172{
100 return container_of(sd, struct tuner, sd); 173 return container_of(sd, struct tuner, sd);
101} 174}
102 175
103 176/*
104/* insmod options used at init time => read/only */ 177 * struct analog_demod_ops callbacks
105static unsigned int addr; 178 */
106static unsigned int no_autodetect;
107static unsigned int show_i2c;
108
109/* insmod options used at runtime => read/write */
110static int tuner_debug;
111
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); \
116 } while (0)
117
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); \
122 } while (0)
123
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); \
128 } while (0)
129
130#define tuner_dbg(fmt, arg...) do { \
131 if (tuner_debug) \
132 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX, \
133 i2c_adapter_id(t->i2c->adapter), \
134 t->i2c->addr, ##arg); \
135 } while (0)
136
137/* ------------------------------------------------------------------------ */
138
139static unsigned int tv_range[2] = { 44, 958 };
140static unsigned int radio_range[2] = { 65, 108 };
141
142static char pal[] = "--";
143static char secam[] = "--";
144static char ntsc[] = "-";
145
146
147module_param(addr, int, 0444);
148module_param(no_autodetect, int, 0444);
149module_param(show_i2c, int, 0444);
150module_param_named(debug,tuner_debug, int, 0644);
151module_param_string(pal, pal, sizeof(pal), 0644);
152module_param_string(secam, secam, sizeof(secam), 0644);
153module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
154module_param_array(tv_range, int, NULL, 0644);
155module_param_array(radio_range, int, NULL, 0644);
156
157MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
158MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
159MODULE_LICENSE("GPL");
160
161/* ---------------------------------------------------------------------- */
162 179
163static void fe_set_params(struct dvb_frontend *fe, 180static void fe_set_params(struct dvb_frontend *fe,
164 struct analog_parameters *params) 181 struct analog_parameters *params)
@@ -214,101 +231,13 @@ static struct analog_demod_ops tuner_analog_ops = {
214 .tuner_status = tuner_status 231 .tuner_status = tuner_status
215}; 232};
216 233
217/* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */ 234/*
218static void set_tv_freq(struct i2c_client *c, unsigned int freq) 235 * Functions that are common to both TV and radio
219{ 236 */
220 struct tuner *t = to_tuner(i2c_get_clientdata(c));
221 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
222
223 struct analog_parameters params = {
224 .mode = t->mode,
225 .audmode = t->audmode,
226 .std = t->std
227 };
228
229 if (t->type == UNSET) {
230 tuner_warn ("tuner type not set\n");
231 return;
232 }
233 if (NULL == analog_ops->set_params) {
234 tuner_warn ("Tuner has no way to set tv freq\n");
235 return;
236 }
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],
240 tv_range[1]);
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;
245 else
246 freq = tv_range[1] * 16;
247 }
248 params.frequency = freq;
249 tuner_dbg("tv freq set to %lu.%02lu\n",
250 freq / 16, freq % 16 * 100 / 16);
251 t->tv_freq = freq;
252
253 analog_ops->set_params(&t->fe, &params);
254}
255
256static void set_radio_freq(struct i2c_client *c, unsigned int freq)
257{
258 struct tuner *t = to_tuner(i2c_get_clientdata(c));
259 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
260
261 struct analog_parameters params = {
262 .mode = t->mode,
263 .audmode = t->audmode,
264 .std = t->std
265 };
266
267 if (t->type == UNSET) {
268 tuner_warn ("tuner type not set\n");
269 return;
270 }
271 if (NULL == analog_ops->set_params) {
272 tuner_warn ("tuner has no way to set radio frequency\n");
273 return;
274 }
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;
283 else
284 freq = radio_range[1] * 16000;
285 }
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;
290
291 analog_ops->set_params(&t->fe, &params);
292}
293
294static void set_freq(struct i2c_client *c, unsigned long freq)
295{
296 struct tuner *t = to_tuner(i2c_get_clientdata(c));
297
298 switch (t->mode) {
299 case V4L2_TUNER_RADIO:
300 set_radio_freq(c, freq);
301 break;
302 case V4L2_TUNER_ANALOG_TV:
303 case V4L2_TUNER_DIGITAL_TV:
304 set_tv_freq(c, freq);
305 break;
306 default:
307 tuner_dbg("freq set: unknown mode: 0x%04x!\n",t->mode);
308 }
309}
310 237
311static struct xc5000_config xc5000_cfg; 238static void set_tv_freq(struct i2c_client *c, unsigned int freq);
239static void set_radio_freq(struct i2c_client *c, unsigned int freq);
240static const struct v4l2_subdev_ops tuner_ops;
312 241
313static void set_type(struct i2c_client *c, unsigned int type, 242static void set_type(struct i2c_client *c, unsigned int type,
314 unsigned int new_mode_mask, unsigned int new_config, 243 unsigned int new_mode_mask, unsigned int new_config,
@@ -467,9 +396,12 @@ static void set_type(struct i2c_client *c, unsigned int type,
467 FIXME: better to move set_freq to the tuner code. This is needed 396 FIXME: better to move set_freq to the tuner code. This is needed
468 on analog tuners for PLL to properly work 397 on analog tuners for PLL to properly work
469 */ 398 */
470 if (tune_now) 399 if (tune_now) {
471 set_freq(c, (V4L2_TUNER_RADIO == t->mode) ? 400 if (V4L2_TUNER_RADIO == t->mode)
472 t->radio_freq : t->tv_freq); 401 set_radio_freq(c, t->radio_freq);
402 else
403 set_tv_freq(c, t->tv_freq);
404 }
473 405
474 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n", 406 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, 407 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
@@ -508,26 +440,249 @@ static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
508 tun_setup->addr, tun_setup->mode_mask); 440 tun_setup->addr, tun_setup->mode_mask);
509} 441}
510 442
511static inline int check_mode(struct tuner *t, char *cmd) 443static int tuner_s_type_addr(struct v4l2_subdev *sd, struct tuner_setup *type)
512{ 444{
513 if ((1 << t->mode & t->mode_mask) == 0) { 445 struct tuner *t = to_tuner(sd);
514 return -EINVAL; 446 struct i2c_client *client = v4l2_get_subdevdata(sd);
447
448 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
449 type->type,
450 type->addr,
451 type->mode_mask,
452 type->config);
453
454 set_addr(client, type);
455 return 0;
456}
457
458static int tuner_s_config(struct v4l2_subdev *sd, const struct v4l2_priv_tun_config *cfg)
459{
460 struct tuner *t = to_tuner(sd);
461 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
462
463 if (t->type != cfg->tuner)
464 return 0;
465
466 if (analog_ops->set_config) {
467 analog_ops->set_config(&t->fe, cfg->priv);
468 return 0;
515 } 469 }
516 470
517 switch (t->mode) { 471 tuner_dbg("Tuner frontend module has no way to set config\n");
518 case V4L2_TUNER_RADIO: 472 return 0;
519 tuner_dbg("Cmd %s accepted for radio\n", cmd); 473}
520 break; 474
521 case V4L2_TUNER_ANALOG_TV: 475/* Search for existing radio and/or TV tuners on the given I2C adapter.
522 tuner_dbg("Cmd %s accepted for analog TV\n", cmd); 476 Note that when this function is called from tuner_probe you can be
523 break; 477 certain no other devices will be added/deleted at the same time, I2C
524 case V4L2_TUNER_DIGITAL_TV: 478 core protects against that. */
525 tuner_dbg("Cmd %s accepted for digital TV\n", cmd); 479static void tuner_lookup(struct i2c_adapter *adap,
526 break; 480 struct tuner **radio, struct tuner **tv)
481{
482 struct tuner *pos;
483
484 *radio = NULL;
485 *tv = NULL;
486
487 list_for_each_entry(pos, &tuner_list, list) {
488 int mode_mask;
489
490 if (pos->i2c->adapter != adap ||
491 strcmp(pos->i2c->driver->driver.name, "tuner"))
492 continue;
493
494 mode_mask = pos->mode_mask & ~T_STANDBY;
495 if (*radio == NULL && mode_mask == T_RADIO)
496 *radio = pos;
497 /* Note: currently TDA9887 is the only demod-only
498 device. If other devices appear then we need to
499 make this test more general. */
500 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
501 (pos->mode_mask & (T_ANALOG_TV | T_DIGITAL_TV)))
502 *tv = pos;
503 }
504}
505
506/* During client attach, set_type is called by adapter's attach_inform callback.
507 set_type must then be completed by tuner_probe.
508 */
509static int tuner_probe(struct i2c_client *client,
510 const struct i2c_device_id *id)
511{
512 struct tuner *t;
513 struct tuner *radio;
514 struct tuner *tv;
515
516 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
517 if (NULL == t)
518 return -ENOMEM;
519 v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
520 t->i2c = client;
521 t->name = "(tuner unset)";
522 t->type = UNSET;
523 t->audmode = V4L2_TUNER_MODE_STEREO;
524 t->mode_mask = T_UNINITIALIZED;
525
526 if (show_i2c) {
527 unsigned char buffer[16];
528 int i, rc;
529
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]);
535 printk("\n");
536 }
537
538 /* autodetection code based on the i2c addr */
539 if (!no_autodetect) {
540 switch (client->addr) {
541 case 0x10:
542 if (tuner_symbol_probe(tea5761_autodetection,
543 t->i2c->adapter,
544 t->i2c->addr) >= 0) {
545 t->type = TUNER_TEA5761;
546 t->mode_mask = T_RADIO;
547 t->mode = T_STANDBY;
548 /* Sets freq to FM range */
549 t->radio_freq = 87.5 * 16000;
550 tuner_lookup(t->i2c->adapter, &radio, &tv);
551 if (tv)
552 tv->mode_mask &= ~T_RADIO;
553
554 goto register_client;
555 }
556 kfree(t);
557 return -ENODEV;
558 case 0x42:
559 case 0x43:
560 case 0x4a:
561 case 0x4b:
562 /* If chip is not tda8290, don't register.
563 since it can be tda9887*/
564 if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
565 t->i2c->addr) >= 0) {
566 tuner_dbg("tda829x detected\n");
567 } else {
568 /* Default is being tda9887 */
569 t->type = TUNER_TDA9887;
570 t->mode_mask = T_RADIO | T_ANALOG_TV |
571 T_DIGITAL_TV;
572 t->mode = T_STANDBY;
573 goto register_client;
574 }
575 break;
576 case 0x60:
577 if (tuner_symbol_probe(tea5767_autodetection,
578 t->i2c->adapter, t->i2c->addr)
579 >= 0) {
580 t->type = TUNER_TEA5767;
581 t->mode_mask = T_RADIO;
582 t->mode = T_STANDBY;
583 /* Sets freq to FM range */
584 t->radio_freq = 87.5 * 16000;
585 tuner_lookup(t->i2c->adapter, &radio, &tv);
586 if (tv)
587 tv->mode_mask &= ~T_RADIO;
588
589 goto register_client;
590 }
591 break;
592 }
527 } 593 }
594
595 /* Initializes only the first TV tuner on this adapter. Why only the
596 first? Because there are some devices (notably the ones with TI
597 tuners) that have more than one i2c address for the *same* device.
598 Experience shows that, except for just one case, the first
599 address is the right one. The exception is a Russian tuner
600 (ACORP_Y878F). So, the desired behavior is just to enable the
601 first found TV tuner. */
602 tuner_lookup(t->i2c->adapter, &radio, &tv);
603 if (tv == NULL) {
604 t->mode_mask = T_ANALOG_TV | T_DIGITAL_TV;
605 if (radio == NULL)
606 t->mode_mask |= T_RADIO;
607 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
608 t->tv_freq = 400 * 16; /* Sets freq to VHF High */
609 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
610 }
611
612 /* Should be just before return */
613register_client:
614 tuner_info("chip found @ 0x%x (%s)\n", client->addr << 1,
615 client->adapter->name);
616
617 /* Sets a default mode */
618 if (t->mode_mask & T_ANALOG_TV) {
619 t->mode = V4L2_TUNER_ANALOG_TV;
620 } else if (t->mode_mask & T_RADIO) {
621 t->mode = V4L2_TUNER_RADIO;
622 } else {
623 t->mode = V4L2_TUNER_DIGITAL_TV;
624 }
625 set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
626 list_add_tail(&t->list, &tuner_list);
627 return 0;
628}
629
630static int tuner_remove(struct i2c_client *client)
631{
632 struct tuner *t = to_tuner(i2c_get_clientdata(client));
633
634 v4l2_device_unregister_subdev(&t->sd);
635 tuner_detach(&t->fe);
636 t->fe.analog_demod_priv = NULL;
637
638 list_del(&t->list);
639 kfree(t);
528 return 0; 640 return 0;
529} 641}
530 642
643/*
644 * Functions that are specific for TV mode
645 */
646
647/* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
648static void set_tv_freq(struct i2c_client *c, unsigned int freq)
649{
650 struct tuner *t = to_tuner(i2c_get_clientdata(c));
651 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
652
653 struct analog_parameters params = {
654 .mode = t->mode,
655 .audmode = t->audmode,
656 .std = t->std
657 };
658
659 if (t->type == UNSET) {
660 tuner_warn ("tuner type not set\n");
661 return;
662 }
663 if (NULL == analog_ops->set_params) {
664 tuner_warn ("Tuner has no way to set tv freq\n");
665 return;
666 }
667 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
668 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
669 freq / 16, freq % 16 * 100 / 16, tv_range[0],
670 tv_range[1]);
671 /* V4L2 spec: if the freq is not possible then the closest
672 possible value should be selected */
673 if (freq < tv_range[0] * 16)
674 freq = tv_range[0] * 16;
675 else
676 freq = tv_range[1] * 16;
677 }
678 params.frequency = freq;
679 tuner_dbg("tv freq set to %d.%02d\n",
680 freq / 16, freq % 16 * 100 / 16);
681 t->tv_freq = freq;
682
683 analog_ops->set_params(&t->fe, &params);
684}
685
531/* get more precise norm info from insmod option */ 686/* get more precise norm info from insmod option */
532static int tuner_fixup_std(struct tuner *t) 687static int tuner_fixup_std(struct tuner *t)
533{ 688{
@@ -644,47 +799,71 @@ static int tuner_fixup_std(struct tuner *t)
644 return 0; 799 return 0;
645} 800}
646 801
647static void tuner_status(struct dvb_frontend *fe) 802/*
803 * Functions that are specific for Radio mode
804 */
805
806static void set_radio_freq(struct i2c_client *c, unsigned int freq)
648{ 807{
649 struct tuner *t = fe->analog_demod_priv; 808 struct tuner *t = to_tuner(i2c_get_clientdata(c));
650 unsigned long freq, freq_fraction; 809 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
651 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
652 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
653 const char *p;
654 810
655 switch (t->mode) { 811 struct analog_parameters params = {
656 case V4L2_TUNER_RADIO: p = "radio"; break; 812 .mode = t->mode,
657 case V4L2_TUNER_ANALOG_TV: p = "analog TV"; break; 813 .audmode = t->audmode,
658 case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break; 814 .std = t->std
659 default: p = "undefined"; break; 815 };
816
817 if (t->type == UNSET) {
818 tuner_warn ("tuner type not set\n");
819 return;
660 } 820 }
661 if (t->mode == V4L2_TUNER_RADIO) { 821 if (NULL == analog_ops->set_params) {
662 freq = t->radio_freq / 16000; 822 tuner_warn ("tuner has no way to set radio frequency\n");
663 freq_fraction = (t->radio_freq % 16000) * 100 / 16000; 823 return;
664 } else {
665 freq = t->tv_freq / 16;
666 freq_fraction = (t->tv_freq % 16) * 100 / 16;
667 } 824 }
668 tuner_info("Tuner mode: %s\n", p); 825 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
669 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction); 826 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
670 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std); 827 freq / 16000, freq % 16000 * 100 / 16000,
671 if (t->mode != V4L2_TUNER_RADIO) 828 radio_range[0], radio_range[1]);
672 return; 829 /* V4L2 spec: if the freq is not possible then the closest
673 if (fe_tuner_ops->get_status) { 830 possible value should be selected */
674 u32 tuner_status; 831 if (freq < radio_range[0] * 16000)
675 832 freq = radio_range[0] * 16000;
676 fe_tuner_ops->get_status(&t->fe, &tuner_status); 833 else
677 if (tuner_status & TUNER_STATUS_LOCKED) 834 freq = radio_range[1] * 16000;
678 tuner_info("Tuner is locked.\n");
679 if (tuner_status & TUNER_STATUS_STEREO)
680 tuner_info("Stereo: yes\n");
681 } 835 }
682 if (analog_ops->has_signal) 836 params.frequency = freq;
683 tuner_info("Signal strength: %d\n", 837 tuner_dbg("radio freq set to %d.%02d\n",
684 analog_ops->has_signal(fe)); 838 freq / 16000, freq % 16000 * 100 / 16000);
839 t->radio_freq = freq;
840
841 analog_ops->set_params(&t->fe, &params);
685} 842}
686 843
687/* ---------------------------------------------------------------------- */ 844/*
845 * Functions that should be broken into separate radio/TV functions
846 */
847
848static inline int check_mode(struct tuner *t, char *cmd)
849{
850 if ((1 << t->mode & t->mode_mask) == 0) {
851 return -EINVAL;
852 }
853
854 switch (t->mode) {
855 case V4L2_TUNER_RADIO:
856 tuner_dbg("Cmd %s accepted for radio\n", cmd);
857 break;
858 case V4L2_TUNER_ANALOG_TV:
859 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
860 break;
861 case V4L2_TUNER_DIGITAL_TV:
862 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
863 break;
864 }
865 return 0;
866}
688 867
689/* 868/*
690 * Switch tuner to other mode. If tuner support both tv and radio, 869 * Switch tuner to other mode. If tuner support both tv and radio,
@@ -713,31 +892,61 @@ static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode,
713 return 0; 892 return 0;
714} 893}
715 894
716static int tuner_s_type_addr(struct v4l2_subdev *sd, struct tuner_setup *type) 895static void set_freq(struct i2c_client *c, unsigned long freq)
717{ 896{
718 struct tuner *t = to_tuner(sd); 897 struct tuner *t = to_tuner(i2c_get_clientdata(c));
719 struct i2c_client *client = v4l2_get_subdevdata(sd);
720
721 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
722 type->type,
723 type->addr,
724 type->mode_mask,
725 type->config);
726 898
727 set_addr(client, type); 899 switch (t->mode) {
728 return 0; 900 case V4L2_TUNER_RADIO:
901 set_radio_freq(c, freq);
902 break;
903 case V4L2_TUNER_ANALOG_TV:
904 case V4L2_TUNER_DIGITAL_TV:
905 set_tv_freq(c, freq);
906 break;
907 default:
908 tuner_dbg("freq set: unknown mode: 0x%04x!\n",t->mode);
909 }
729} 910}
730 911
731static int tuner_s_radio(struct v4l2_subdev *sd) 912static void tuner_status(struct dvb_frontend *fe)
732{ 913{
733 struct tuner *t = to_tuner(sd); 914 struct tuner *t = fe->analog_demod_priv;
734 struct i2c_client *client = v4l2_get_subdevdata(sd); 915 unsigned long freq, freq_fraction;
916 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
917 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
918 const char *p;
735 919
736 if (set_mode(client, t, V4L2_TUNER_RADIO, "s_radio") == -EINVAL) 920 switch (t->mode) {
737 return 0; 921 case V4L2_TUNER_RADIO: p = "radio"; break;
738 if (t->radio_freq) 922 case V4L2_TUNER_ANALOG_TV: p = "analog TV"; break;
739 set_freq(client, t->radio_freq); 923 case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break;
740 return 0; 924 default: p = "undefined"; break;
925 }
926 if (t->mode == V4L2_TUNER_RADIO) {
927 freq = t->radio_freq / 16000;
928 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
929 } else {
930 freq = t->tv_freq / 16;
931 freq_fraction = (t->tv_freq % 16) * 100 / 16;
932 }
933 tuner_info("Tuner mode: %s\n", p);
934 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
935 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
936 if (t->mode != V4L2_TUNER_RADIO)
937 return;
938 if (fe_tuner_ops->get_status) {
939 u32 tuner_status;
940
941 fe_tuner_ops->get_status(&t->fe, &tuner_status);
942 if (tuner_status & TUNER_STATUS_LOCKED)
943 tuner_info("Tuner is locked.\n");
944 if (tuner_status & TUNER_STATUS_STEREO)
945 tuner_info("Stereo: yes\n");
946 }
947 if (analog_ops->has_signal)
948 tuner_info("Signal strength: %d\n",
949 analog_ops->has_signal(fe));
741} 950}
742 951
743static int tuner_s_power(struct v4l2_subdev *sd, int on) 952static int tuner_s_power(struct v4l2_subdev *sd, int on)
@@ -758,20 +967,18 @@ static int tuner_s_power(struct v4l2_subdev *sd, int on)
758 return 0; 967 return 0;
759} 968}
760 969
761static int tuner_s_config(struct v4l2_subdev *sd, const struct v4l2_priv_tun_config *cfg) 970/* ---------------------------------------------------------------------- */
971
972
973static int tuner_s_radio(struct v4l2_subdev *sd)
762{ 974{
763 struct tuner *t = to_tuner(sd); 975 struct tuner *t = to_tuner(sd);
764 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; 976 struct i2c_client *client = v4l2_get_subdevdata(sd);
765
766 if (t->type != cfg->tuner)
767 return 0;
768 977
769 if (analog_ops->set_config) { 978 if (set_mode(client, t, V4L2_TUNER_RADIO, "s_radio") == -EINVAL)
770 analog_ops->set_config(&t->fe, cfg->priv);
771 return 0; 979 return 0;
772 } 980 if (t->radio_freq)
773 981 set_freq(client, t->radio_freq);
774 tuner_dbg("Tuner frontend module has no way to set config\n");
775 return 0; 982 return 0;
776} 983}
777 984
@@ -956,178 +1163,6 @@ static const struct v4l2_subdev_ops tuner_ops = {
956 .tuner = &tuner_tuner_ops, 1163 .tuner = &tuner_tuner_ops,
957}; 1164};
958 1165
959/* ---------------------------------------------------------------------- */
960
961static LIST_HEAD(tuner_list);
962
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. */
967static void tuner_lookup(struct i2c_adapter *adap,
968 struct tuner **radio, struct tuner **tv)
969{
970 struct tuner *pos;
971
972 *radio = NULL;
973 *tv = NULL;
974
975 list_for_each_entry(pos, &tuner_list, list) {
976 int mode_mask;
977
978 if (pos->i2c->adapter != adap ||
979 strcmp(pos->i2c->driver->driver.name, "tuner"))
980 continue;
981
982 mode_mask = pos->mode_mask & ~T_STANDBY;
983 if (*radio == NULL && mode_mask == T_RADIO)
984 *radio = pos;
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)))
990 *tv = pos;
991 }
992}
993
994/* During client attach, set_type is called by adapter's attach_inform callback.
995 set_type must then be completed by tuner_probe.
996 */
997static int tuner_probe(struct i2c_client *client,
998 const struct i2c_device_id *id)
999{
1000 struct tuner *t;
1001 struct tuner *radio;
1002 struct tuner *tv;
1003
1004 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
1005 if (NULL == t)
1006 return -ENOMEM;
1007 v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
1008 t->i2c = client;
1009 t->name = "(tuner unset)";
1010 t->type = UNSET;
1011 t->audmode = V4L2_TUNER_MODE_STEREO;
1012 t->mode_mask = T_UNINITIALIZED;
1013
1014 if (show_i2c) {
1015 unsigned char buffer[16];
1016 int i, rc;
1017
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]);
1023 printk("\n");
1024 }
1025
1026 /* autodetection code based on the i2c addr */
1027 if (!no_autodetect) {
1028 switch (client->addr) {
1029 case 0x10:
1030 if (tuner_symbol_probe(tea5761_autodetection,
1031 t->i2c->adapter,
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);
1039 if (tv)
1040 tv->mode_mask &= ~T_RADIO;
1041
1042 goto register_client;
1043 }
1044 kfree(t);
1045 return -ENODEV;
1046 case 0x42:
1047 case 0x43:
1048 case 0x4a:
1049 case 0x4b:
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");
1055 } else {
1056 /* Default is being tda9887 */
1057 t->type = TUNER_TDA9887;
1058 t->mode_mask = T_RADIO | T_ANALOG_TV |
1059 T_DIGITAL_TV;
1060 t->mode = T_STANDBY;
1061 goto register_client;
1062 }
1063 break;
1064 case 0x60:
1065 if (tuner_symbol_probe(tea5767_autodetection,
1066 t->i2c->adapter, t->i2c->addr)
1067 >= 0) {
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);
1074 if (tv)
1075 tv->mode_mask &= ~T_RADIO;
1076
1077 goto register_client;
1078 }
1079 break;
1080 }
1081 }
1082
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);
1091 if (tv == NULL) {
1092 t->mode_mask = T_ANALOG_TV | T_DIGITAL_TV;
1093 if (radio == NULL)
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 */
1098 }
1099
1100 /* Should be just before return */
1101register_client:
1102 tuner_info("chip found @ 0x%x (%s)\n", client->addr << 1,
1103 client->adapter->name);
1104
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;
1110 } else {
1111 t->mode = V4L2_TUNER_DIGITAL_TV;
1112 }
1113 set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
1114 list_add_tail(&t->list, &tuner_list);
1115 return 0;
1116}
1117
1118static int tuner_remove(struct i2c_client *client)
1119{
1120 struct tuner *t = to_tuner(i2c_get_clientdata(client));
1121
1122 v4l2_device_unregister_subdev(&t->sd);
1123 tuner_detach(&t->fe);
1124 t->fe.analog_demod_priv = NULL;
1125
1126 list_del(&t->list);
1127 kfree(t);
1128 return 0;
1129}
1130
1131/* ----------------------------------------------------------------------- */ 1166/* ----------------------------------------------------------------------- */
1132 1167
1133/* This driver supports many devices and the idea is to let the driver 1168/* This driver supports many devices and the idea is to let the driver
@@ -1165,10 +1200,6 @@ static __exit void exit_tuner(void)
1165module_init(init_tuner); 1200module_init(init_tuner);
1166module_exit(exit_tuner); 1201module_exit(exit_tuner);
1167 1202
1168/* 1203MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
1169 * Overrides for Emacs so that we follow Linus's tabbing style. 1204MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1170 * --------------------------------------------------------------------------- 1205MODULE_LICENSE("GPL");
1171 * Local variables:
1172 * c-basic-offset: 8
1173 * End:
1174 */