]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/media/video/tuner-core.c
[PATCH] v4l: bug fixes for tuner, cx88 and tea5767
[mirror_ubuntu-bionic-kernel.git] / drivers / media / video / tuner-core.c
1 /*
2 * $Id: tuner-core.c,v 1.58 2005/07/14 03:06:43 mchehab Exp $
3 *
4 * i2c tv tuner chip device driver
5 * core core, i.e. kernel interfaces, registering and so on
6 */
7
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/string.h>
13 #include <linux/timer.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/poll.h>
18 #include <linux/i2c.h>
19 #include <linux/types.h>
20 #include <linux/videodev.h>
21 #include <linux/init.h>
22
23 #include <media/tuner.h>
24 #include <media/audiochip.h>
25
26 #define UNSET (-1U)
27
28 /* standard i2c insmod options */
29 static unsigned short normal_i2c[] = {
30 0x4b, /* tda8290 */
31 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
32 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
33 I2C_CLIENT_END
34 };
35
36 I2C_CLIENT_INSMOD;
37
38 /* insmod options used at init time => read/only */
39 static unsigned int addr = 0;
40 module_param(addr, int, 0444);
41
42 static unsigned int no_autodetect = 0;
43 module_param(no_autodetect, int, 0444);
44
45 /* insmod options used at runtime => read/write */
46 unsigned int tuner_debug = 0;
47 module_param(tuner_debug, int, 0644);
48
49 static unsigned int tv_range[2] = { 44, 958 };
50 static unsigned int radio_range[2] = { 65, 108 };
51
52 module_param_array(tv_range, int, NULL, 0644);
53 module_param_array(radio_range, int, NULL, 0644);
54
55 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
56 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
57 MODULE_LICENSE("GPL");
58
59 static struct i2c_driver driver;
60 static struct i2c_client client_template;
61
62 /* ---------------------------------------------------------------------- */
63
64 /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
65 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
66 {
67 struct tuner *t = i2c_get_clientdata(c);
68
69 if (t->type == UNSET) {
70 tuner_warn ("tuner type not set\n");
71 return;
72 }
73 if (NULL == t->tv_freq) {
74 tuner_warn ("Tuner has no way to set tv freq\n");
75 return;
76 }
77 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
78 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
79 freq / 16, freq % 16 * 100 / 16, tv_range[0],
80 tv_range[1]);
81 }
82 t->tv_freq(c, freq);
83 }
84
85 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
86 {
87 struct tuner *t = i2c_get_clientdata(c);
88
89 if (t->type == UNSET) {
90 tuner_warn ("tuner type not set\n");
91 return;
92 }
93 if (NULL == t->radio_freq) {
94 tuner_warn ("tuner has no way to set radio frequency\n");
95 return;
96 }
97 if (freq <= radio_range[0] * 16000 || freq >= radio_range[1] * 16000) {
98 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
99 freq / 16000, freq % 16000 * 100 / 16000,
100 radio_range[0], radio_range[1]);
101 }
102
103 t->radio_freq(c, freq);
104 return;
105 }
106
107 static void set_freq(struct i2c_client *c, unsigned long freq)
108 {
109 struct tuner *t = i2c_get_clientdata(c);
110
111 switch (t->mode) {
112 case V4L2_TUNER_RADIO:
113 tuner_dbg("radio freq set to %lu.%02lu\n",
114 freq / 16000, freq % 16000 * 100 / 16000);
115 set_radio_freq(c, freq);
116 break;
117 case V4L2_TUNER_ANALOG_TV:
118 case V4L2_TUNER_DIGITAL_TV:
119 tuner_dbg("tv freq set to %lu.%02lu\n",
120 freq / 16, freq % 16 * 100 / 16);
121 set_tv_freq(c, freq);
122 break;
123 }
124 t->freq = freq;
125 }
126
127 static void set_type(struct i2c_client *c, unsigned int type,
128 unsigned int new_mode_mask)
129 {
130 struct tuner *t = i2c_get_clientdata(c);
131 unsigned char buffer[4];
132
133 if (type == UNSET || type == TUNER_ABSENT) {
134 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
135 return;
136 }
137
138 if (type >= tuner_count) {
139 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
140 return;
141 }
142
143 /* This code detects calls by card attach_inform */
144 if (NULL == t->i2c.dev.driver) {
145 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
146
147 t->type=type;
148 return;
149 }
150
151 t->type = type;
152
153 switch (t->type) {
154 case TUNER_MT2032:
155 microtune_init(c);
156 break;
157 case TUNER_PHILIPS_TDA8290:
158 tda8290_init(c);
159 break;
160 case TUNER_TEA5767:
161 if (tea5767_tuner_init(c) == EINVAL) {
162 t->type = TUNER_ABSENT;
163 t->mode_mask = T_UNINITIALIZED;
164 return;
165 }
166 t->mode_mask = T_RADIO;
167 break;
168 case TUNER_PHILIPS_FMD1216ME_MK3:
169 buffer[0] = 0x0b;
170 buffer[1] = 0xdc;
171 buffer[2] = 0x9c;
172 buffer[3] = 0x60;
173 i2c_master_send(c, buffer, 4);
174 mdelay(1);
175 buffer[2] = 0x86;
176 buffer[3] = 0x54;
177 i2c_master_send(c, buffer, 4);
178 default_tuner_init(c);
179 break;
180 default:
181 default_tuner_init(c);
182 break;
183 }
184
185 if (t->mode_mask == T_UNINITIALIZED)
186 t->mode_mask = new_mode_mask;
187
188 set_freq(c, t->freq);
189 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
190 c->adapter->name, c->driver->name, c->addr << 1, type,
191 t->mode_mask);
192 }
193
194 /*
195 * This function apply tuner config to tuner specified
196 * by tun_setup structure. I addr is unset, then admin status
197 * and tun addr status is more precise then current status,
198 * it's applied. Otherwise status and type are applied only to
199 * tuner with exactly the same addr.
200 */
201
202 static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
203 {
204 struct tuner *t = i2c_get_clientdata(c);
205
206 if (tun_setup->addr == ADDR_UNSET) {
207 if (t->mode_mask & tun_setup->mode_mask)
208 set_type(c, tun_setup->type, tun_setup->mode_mask);
209 } else if (tun_setup->addr == c->addr) {
210 set_type(c, tun_setup->type, tun_setup->mode_mask);
211 }
212 }
213
214 static inline int check_mode(struct tuner *t, char *cmd)
215 {
216 if (1 << t->mode & t->mode_mask) {
217 switch (t->mode) {
218 case V4L2_TUNER_RADIO:
219 tuner_dbg("Cmd %s accepted for radio\n", cmd);
220 break;
221 case V4L2_TUNER_ANALOG_TV:
222 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
223 break;
224 case V4L2_TUNER_DIGITAL_TV:
225 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
226 break;
227 }
228 return 0;
229 }
230 return EINVAL;
231 }
232
233 static char pal[] = "-";
234 module_param_string(pal, pal, sizeof(pal), 0644);
235 static char secam[] = "-";
236 module_param_string(secam, secam, sizeof(secam), 0644);
237
238 /* get more precise norm info from insmod option */
239 static int tuner_fixup_std(struct tuner *t)
240 {
241 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
242 switch (pal[0]) {
243 case 'b':
244 case 'B':
245 case 'g':
246 case 'G':
247 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
248 t->std = V4L2_STD_PAL_BG;
249 break;
250 case 'i':
251 case 'I':
252 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
253 t->std = V4L2_STD_PAL_I;
254 break;
255 case 'd':
256 case 'D':
257 case 'k':
258 case 'K':
259 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
260 t->std = V4L2_STD_PAL_DK;
261 break;
262 case 'M':
263 case 'm':
264 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
265 t->std = V4L2_STD_PAL_M;
266 break;
267 case 'N':
268 case 'n':
269 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
270 t->std = V4L2_STD_PAL_N;
271 break;
272 }
273 }
274 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
275 switch (secam[0]) {
276 case 'd':
277 case 'D':
278 case 'k':
279 case 'K':
280 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
281 t->std = V4L2_STD_SECAM_DK;
282 break;
283 case 'l':
284 case 'L':
285 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
286 t->std = V4L2_STD_SECAM_L;
287 break;
288 }
289 }
290
291 return 0;
292 }
293
294 /* ---------------------------------------------------------------------- */
295
296 /* static var Used only in tuner_attach and tuner_probe */
297 static unsigned default_mode_mask;
298
299 /* During client attach, set_type is called by adapter's attach_inform callback.
300 set_type must then be completed by tuner_attach.
301 */
302 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
303 {
304 struct tuner *t;
305
306 client_template.adapter = adap;
307 client_template.addr = addr;
308
309 t = kmalloc(sizeof(struct tuner), GFP_KERNEL);
310 if (NULL == t)
311 return -ENOMEM;
312 memset(t, 0, sizeof(struct tuner));
313 memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
314 i2c_set_clientdata(&t->i2c, t);
315 t->type = UNSET;
316 t->radio_if2 = 10700 * 1000; /* 10.7MHz - FM radio */
317 t->audmode = V4L2_TUNER_MODE_STEREO;
318 t->mode_mask = T_UNINITIALIZED;
319
320
321 tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
322
323 /* TEA5767 autodetection code - only for addr = 0xc0 */
324 if (!no_autodetect) {
325 if (addr == 0x60) {
326 if (tea5767_autodetection(&t->i2c) != EINVAL) {
327 t->type = TUNER_TEA5767;
328 t->mode_mask = T_RADIO;
329 t->mode = T_STANDBY;
330 t->freq = 87.5 * 16; /* Sets freq to FM range */
331 default_mode_mask &= ~T_RADIO;
332
333 i2c_attach_client (&t->i2c);
334 set_type(&t->i2c,t->type, t->mode_mask);
335 return 0;
336 }
337 }
338 }
339
340 /* Initializes only the first adapter found */
341 if (default_mode_mask != T_UNINITIALIZED) {
342 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
343 t->mode_mask = default_mode_mask;
344 t->freq = 400 * 16; /* Sets freq to VHF High */
345 default_mode_mask = T_UNINITIALIZED;
346 }
347
348 /* Should be just before return */
349 i2c_attach_client (&t->i2c);
350 set_type (&t->i2c,t->type, t->mode_mask);
351 return 0;
352 }
353
354 static int tuner_probe(struct i2c_adapter *adap)
355 {
356 if (0 != addr) {
357 normal_i2c[0] = addr;
358 normal_i2c[1] = I2C_CLIENT_END;
359 }
360
361 default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
362
363 if (adap->class & I2C_CLASS_TV_ANALOG)
364 return i2c_probe(adap, &addr_data, tuner_attach);
365 return 0;
366 }
367
368 static int tuner_detach(struct i2c_client *client)
369 {
370 struct tuner *t = i2c_get_clientdata(client);
371 int err;
372
373 err = i2c_detach_client(&t->i2c);
374 if (err) {
375 tuner_warn
376 ("Client deregistration failed, client not detached.\n");
377 return err;
378 }
379
380 kfree(t);
381 return 0;
382 }
383
384 /*
385 * Switch tuner to other mode. If tuner support both tv and radio,
386 * set another frequency to some value (This is needed for some pal
387 * tuners to avoid locking). Otherwise, just put second tuner in
388 * standby mode.
389 */
390
391 static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
392 {
393 if (mode != t->mode) {
394
395 t->mode = mode;
396 if (check_mode(t, cmd) == EINVAL) {
397 t->mode = T_STANDBY;
398 if (V4L2_TUNER_RADIO == mode) {
399 set_tv_freq(client, 400 * 16);
400 } else {
401 set_radio_freq(client, 87.5 * 16000);
402 }
403 return EINVAL;
404 }
405 }
406 return 0;
407 }
408
409 #define switch_v4l2() if (!t->using_v4l2) \
410 tuner_dbg("switching to v4l2\n"); \
411 t->using_v4l2 = 1;
412
413 static inline int check_v4l2(struct tuner *t)
414 {
415 if (t->using_v4l2) {
416 tuner_dbg ("ignore v4l1 call\n");
417 return EINVAL;
418 }
419 return 0;
420 }
421
422 static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
423 {
424 struct tuner *t = i2c_get_clientdata(client);
425 unsigned int *iarg = (int *)arg;
426
427 switch (cmd) {
428 /* --- configuration --- */
429 case TUNER_SET_TYPE_ADDR:
430 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x\n",
431 ((struct tuner_setup *)arg)->type,
432 ((struct tuner_setup *)arg)->addr,
433 ((struct tuner_setup *)arg)->mode_mask);
434
435 set_addr(client, (struct tuner_setup *)arg);
436 break;
437 case AUDC_SET_RADIO:
438 set_mode(client,t,V4L2_TUNER_RADIO, "AUDC_SET_RADIO");
439 break;
440 case AUDC_CONFIG_PINNACLE:
441 if (check_mode(t, "AUDC_CONFIG_PINNACLE") == EINVAL)
442 return 0;
443 switch (*iarg) {
444 case 2:
445 tuner_dbg("pinnacle pal\n");
446 t->radio_if2 = 33300 * 1000;
447 break;
448 case 3:
449 tuner_dbg("pinnacle ntsc\n");
450 t->radio_if2 = 41300 * 1000;
451 break;
452 }
453 break;
454 case TDA9887_SET_CONFIG:
455 break;
456 /* --- v4l ioctls --- */
457 /* take care: bttv does userspace copying, we'll get a
458 kernel pointer here... */
459 case VIDIOCSCHAN:
460 {
461 static const v4l2_std_id map[] = {
462 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
463 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
464 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
465 [4 /* bttv */ ] = V4L2_STD_PAL_M,
466 [5 /* bttv */ ] = V4L2_STD_PAL_N,
467 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
468 };
469 struct video_channel *vc = arg;
470
471 if (check_v4l2(t) == EINVAL)
472 return 0;
473
474 if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
475 return 0;
476
477 if (vc->norm < ARRAY_SIZE(map))
478 t->std = map[vc->norm];
479 tuner_fixup_std(t);
480 if (t->freq)
481 set_tv_freq(client, t->freq);
482 return 0;
483 }
484 case VIDIOCSFREQ:
485 {
486 unsigned long *v = arg;
487
488 if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
489 return 0;
490 if (check_v4l2(t) == EINVAL)
491 return 0;
492
493 set_freq(client, *v);
494 return 0;
495 }
496 case VIDIOCGTUNER:
497 {
498 struct video_tuner *vt = arg;
499
500 if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
501 return 0;
502 if (check_v4l2(t) == EINVAL)
503 return 0;
504
505 if (V4L2_TUNER_RADIO == t->mode) {
506 if (t->has_signal)
507 vt->signal = t->has_signal(client);
508 if (t->is_stereo) {
509 if (t->is_stereo(client))
510 vt->flags |=
511 VIDEO_TUNER_STEREO_ON;
512 else
513 vt->flags &=
514 ~VIDEO_TUNER_STEREO_ON;
515 }
516 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
517
518 vt->rangelow = radio_range[0] * 16000;
519 vt->rangehigh = radio_range[1] * 16000;
520
521 } else {
522 vt->rangelow = tv_range[0] * 16;
523 vt->rangehigh = tv_range[1] * 16;
524 }
525
526 return 0;
527 }
528 case VIDIOCGAUDIO:
529 {
530 struct video_audio *va = arg;
531
532 if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
533 return 0;
534 if (check_v4l2(t) == EINVAL)
535 return 0;
536
537 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
538 va->mode = t->is_stereo(client)
539 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
540 return 0;
541 }
542
543 case VIDIOC_S_STD:
544 {
545 v4l2_std_id *id = arg;
546
547 if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
548 == EINVAL)
549 return 0;
550
551 switch_v4l2();
552
553 t->std = *id;
554 tuner_fixup_std(t);
555 if (t->freq)
556 set_freq(client, t->freq);
557 break;
558 }
559 case VIDIOC_S_FREQUENCY:
560 {
561 struct v4l2_frequency *f = arg;
562
563 t->freq = f->frequency;
564 switch_v4l2();
565 if (V4L2_TUNER_RADIO == f->type &&
566 V4L2_TUNER_RADIO != t->mode) {
567 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
568 == EINVAL)
569 return 0;
570 }
571 set_freq(client,t->freq);
572
573 break;
574 }
575 case VIDIOC_G_FREQUENCY:
576 {
577 struct v4l2_frequency *f = arg;
578
579 if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
580 return 0;
581 switch_v4l2();
582 f->type = t->mode;
583 f->frequency = t->freq;
584 break;
585 }
586 case VIDIOC_G_TUNER:
587 {
588 struct v4l2_tuner *tuner = arg;
589
590 if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
591 return 0;
592 switch_v4l2();
593
594 if (V4L2_TUNER_RADIO == t->mode) {
595
596 if (t->has_signal)
597 tuner->signal = t->has_signal(client);
598
599 if (t->is_stereo) {
600 if (t->is_stereo(client)) {
601 tuner->rxsubchans =
602 V4L2_TUNER_SUB_STEREO |
603 V4L2_TUNER_SUB_MONO;
604 } else {
605 tuner->rxsubchans =
606 V4L2_TUNER_SUB_MONO;
607 }
608 }
609
610 tuner->capability |=
611 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
612
613 tuner->audmode = t->audmode;
614
615 tuner->rangelow = radio_range[0] * 16000;
616 tuner->rangehigh = radio_range[1] * 16000;
617 } else {
618 tuner->rangelow = tv_range[0] * 16;
619 tuner->rangehigh = tv_range[1] * 16;
620 }
621 break;
622 }
623 case VIDIOC_S_TUNER:
624 {
625 struct v4l2_tuner *tuner = arg;
626
627 if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
628 return 0;
629
630 switch_v4l2();
631
632 if (V4L2_TUNER_RADIO == t->mode) {
633 t->audmode = tuner->audmode;
634 set_radio_freq(client, t->freq);
635 }
636 break;
637 }
638 default:
639 tuner_dbg("Unimplemented IOCTL 0x%08x(dir=%d,tp=0x%02x,nr=%d,sz=%d)\n",
640 cmd, _IOC_DIR(cmd), _IOC_TYPE(cmd),
641 _IOC_NR(cmd), _IOC_SIZE(cmd));
642 break;
643 }
644
645 return 0;
646 }
647
648 static int tuner_suspend(struct device *dev, u32 state, u32 level)
649 {
650 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
651 struct tuner *t = i2c_get_clientdata (c);
652
653 tuner_dbg ("suspend\n");
654 /* FIXME: power down ??? */
655 return 0;
656 }
657
658 static int tuner_resume(struct device *dev, u32 level)
659 {
660 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
661 struct tuner *t = i2c_get_clientdata (c);
662
663 tuner_dbg ("resume\n");
664 if (t->freq)
665 set_freq(c, t->freq);
666 return 0;
667 }
668
669 /* ----------------------------------------------------------------------- */
670
671 static struct i2c_driver driver = {
672 .owner = THIS_MODULE,
673 .name = "tuner",
674 .id = I2C_DRIVERID_TUNER,
675 .flags = I2C_DF_NOTIFY,
676 .attach_adapter = tuner_probe,
677 .detach_client = tuner_detach,
678 .command = tuner_command,
679 .driver = {
680 .suspend = tuner_suspend,
681 .resume = tuner_resume,
682 },
683 };
684 static struct i2c_client client_template = {
685 I2C_DEVNAME("(tuner unset)"),
686 .flags = I2C_CLIENT_ALLOW_USE,
687 .driver = &driver,
688 };
689
690 static int __init tuner_init_module(void)
691 {
692 return i2c_add_driver(&driver);
693 }
694
695 static void __exit tuner_cleanup_module(void)
696 {
697 i2c_del_driver(&driver);
698 }
699
700 module_init(tuner_init_module);
701 module_exit(tuner_cleanup_module);
702
703 /*
704 * Overrides for Emacs so that we follow Linus's tabbing style.
705 * ---------------------------------------------------------------------------
706 * Local variables:
707 * c-basic-offset: 8
708 * End:
709 */