added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / media / radio / radio-gemtek.c
blob2b68be773f1381c443421fdf4ab8f40fe94009d3
1 /* GemTek radio card driver for Linux (C) 1998 Jonas Munsin <jmunsin@iki.fi>
3 * GemTek hasn't released any specs on the card, so the protocol had to
4 * be reverse engineered with dosemu.
6 * Besides the protocol changes, this is mostly a copy of:
8 * RadioTrack II driver for Linux radio support (C) 1998 Ben Pfaff
10 * Based on RadioTrack I/RadioReveal (C) 1997 M. Kirkwood
11 * Converted to new API by Alan Cox <alan@lxorguk.ukuu.org.uk>
12 * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
14 * TODO: Allow for more than one of these foolish entities :-)
16 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
19 #include <linux/module.h> /* Modules */
20 #include <linux/init.h> /* Initdata */
21 #include <linux/ioport.h> /* request_region */
22 #include <linux/delay.h> /* udelay */
23 #include <asm/io.h> /* outb, outb_p */
24 #include <asm/uaccess.h> /* copy to/from user */
25 #include <linux/videodev2.h> /* kernel radio structs */
26 #include <media/v4l2-ioctl.h>
27 #include <media/v4l2-common.h>
28 #include <linux/spinlock.h>
30 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
31 #define RADIO_VERSION KERNEL_VERSION(0,0,3)
32 #define RADIO_BANNER "GemTek Radio card driver: v0.0.3"
35 * Module info.
38 MODULE_AUTHOR("Jonas Munsin, Pekka Seppänen <pexu@kapsi.fi>");
39 MODULE_DESCRIPTION("A driver for the GemTek Radio card.");
40 MODULE_LICENSE("GPL");
43 * Module params.
46 #ifndef CONFIG_RADIO_GEMTEK_PORT
47 #define CONFIG_RADIO_GEMTEK_PORT -1
48 #endif
49 #ifndef CONFIG_RADIO_GEMTEK_PROBE
50 #define CONFIG_RADIO_GEMTEK_PROBE 1
51 #endif
53 static int io = CONFIG_RADIO_GEMTEK_PORT;
54 static int probe = CONFIG_RADIO_GEMTEK_PROBE;
55 static int hardmute;
56 static int shutdown = 1;
57 static int keepmuted = 1;
58 static int initmute = 1;
59 static int radio_nr = -1;
60 static unsigned long in_use;
62 module_param(io, int, 0444);
63 MODULE_PARM_DESC(io, "Force I/O port for the GemTek Radio card if automatic "
64 "probing is disabled or fails. The most common I/O ports are: 0x20c "
65 "0x30c, 0x24c or 0x34c (0x20c, 0x248 and 0x28c have been reported to "
66 "work for the combined sound/radiocard).");
68 module_param(probe, bool, 0444);
69 MODULE_PARM_DESC(probe, "Enable automatic device probing. Note: only the most "
70 "common I/O ports used by the card are probed.");
72 module_param(hardmute, bool, 0644);
73 MODULE_PARM_DESC(hardmute, "Enable `hard muting' by shutting down PLL, may "
74 "reduce static noise.");
76 module_param(shutdown, bool, 0644);
77 MODULE_PARM_DESC(shutdown, "Enable shutting down PLL and muting line when "
78 "module is unloaded.");
80 module_param(keepmuted, bool, 0644);
81 MODULE_PARM_DESC(keepmuted, "Keep card muted even when frequency is changed.");
83 module_param(initmute, bool, 0444);
84 MODULE_PARM_DESC(initmute, "Mute card when module is loaded.");
86 module_param(radio_nr, int, 0444);
89 * Functions for controlling the card.
91 #define GEMTEK_LOWFREQ (87*16000)
92 #define GEMTEK_HIGHFREQ (108*16000)
95 * Frequency calculation constants. Intermediate frequency 10.52 MHz (nominal
96 * value 10.7 MHz), reference divisor 6.39 kHz (nominal 6.25 kHz).
98 #define FSCALE 8
99 #define IF_OFFSET ((unsigned int)(10.52 * 16000 * (1<<FSCALE)))
100 #define REF_FREQ ((unsigned int)(6.39 * 16 * (1<<FSCALE)))
102 #define GEMTEK_CK 0x01 /* Clock signal */
103 #define GEMTEK_DA 0x02 /* Serial data */
104 #define GEMTEK_CE 0x04 /* Chip enable */
105 #define GEMTEK_NS 0x08 /* No signal */
106 #define GEMTEK_MT 0x10 /* Line mute */
107 #define GEMTEK_STDF_3_125_KHZ 0x01 /* Standard frequency 3.125 kHz */
108 #define GEMTEK_PLL_OFF 0x07 /* PLL off */
110 #define BU2614_BUS_SIZE 32 /* BU2614 / BU2614FS bus size */
112 #define SHORT_DELAY 5 /* usec */
113 #define LONG_DELAY 75 /* usec */
115 struct gemtek_device {
116 unsigned long lastfreq;
117 int muted;
118 u32 bu2614data;
121 #define BU2614_FREQ_BITS 16 /* D0..D15, Frequency data */
122 #define BU2614_PORT_BITS 3 /* P0..P2, Output port control data */
123 #define BU2614_VOID_BITS 4 /* unused */
124 #define BU2614_FMES_BITS 1 /* CT, Frequency measurement beginning data */
125 #define BU2614_STDF_BITS 3 /* R0..R2, Standard frequency data */
126 #define BU2614_SWIN_BITS 1 /* S, Switch between FMIN / AMIN */
127 #define BU2614_SWAL_BITS 1 /* PS, Swallow counter division (AMIN only)*/
128 #define BU2614_VOID2_BITS 1 /* unused */
129 #define BU2614_FMUN_BITS 1 /* GT, Frequency measurement time & unlock */
130 #define BU2614_TEST_BITS 1 /* TS, Test data is input */
132 #define BU2614_FREQ_SHIFT 0
133 #define BU2614_PORT_SHIFT (BU2614_FREQ_BITS + BU2614_FREQ_SHIFT)
134 #define BU2614_VOID_SHIFT (BU2614_PORT_BITS + BU2614_PORT_SHIFT)
135 #define BU2614_FMES_SHIFT (BU2614_VOID_BITS + BU2614_VOID_SHIFT)
136 #define BU2614_STDF_SHIFT (BU2614_FMES_BITS + BU2614_FMES_SHIFT)
137 #define BU2614_SWIN_SHIFT (BU2614_STDF_BITS + BU2614_STDF_SHIFT)
138 #define BU2614_SWAL_SHIFT (BU2614_SWIN_BITS + BU2614_SWIN_SHIFT)
139 #define BU2614_VOID2_SHIFT (BU2614_SWAL_BITS + BU2614_SWAL_SHIFT)
140 #define BU2614_FMUN_SHIFT (BU2614_VOID2_BITS + BU2614_VOID2_SHIFT)
141 #define BU2614_TEST_SHIFT (BU2614_FMUN_BITS + BU2614_FMUN_SHIFT)
143 #define MKMASK(field) (((1<<BU2614_##field##_BITS) - 1) << \
144 BU2614_##field##_SHIFT)
145 #define BU2614_PORT_MASK MKMASK(PORT)
146 #define BU2614_FREQ_MASK MKMASK(FREQ)
147 #define BU2614_VOID_MASK MKMASK(VOID)
148 #define BU2614_FMES_MASK MKMASK(FMES)
149 #define BU2614_STDF_MASK MKMASK(STDF)
150 #define BU2614_SWIN_MASK MKMASK(SWIN)
151 #define BU2614_SWAL_MASK MKMASK(SWAL)
152 #define BU2614_VOID2_MASK MKMASK(VOID2)
153 #define BU2614_FMUN_MASK MKMASK(FMUN)
154 #define BU2614_TEST_MASK MKMASK(TEST)
156 static struct gemtek_device gemtek_unit;
158 static spinlock_t lock;
161 * Set data which will be sent to BU2614FS.
163 #define gemtek_bu2614_set(dev, field, data) ((dev)->bu2614data = \
164 ((dev)->bu2614data & ~field##_MASK) | ((data) << field##_SHIFT))
167 * Transmit settings to BU2614FS over GemTek IC.
169 static void gemtek_bu2614_transmit(struct gemtek_device *dev)
171 int i, bit, q, mute;
173 spin_lock(&lock);
175 mute = dev->muted ? GEMTEK_MT : 0x00;
177 outb_p(mute | GEMTEK_DA | GEMTEK_CK, io);
178 udelay(SHORT_DELAY);
179 outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, io);
180 udelay(LONG_DELAY);
182 for (i = 0, q = dev->bu2614data; i < 32; i++, q >>= 1) {
183 bit = (q & 1) ? GEMTEK_DA : 0;
184 outb_p(mute | GEMTEK_CE | bit, io);
185 udelay(SHORT_DELAY);
186 outb_p(mute | GEMTEK_CE | bit | GEMTEK_CK, io);
187 udelay(SHORT_DELAY);
190 outb_p(mute | GEMTEK_DA | GEMTEK_CK, io);
191 udelay(SHORT_DELAY);
192 outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, io);
193 udelay(LONG_DELAY);
195 spin_unlock(&lock);
199 * Calculate divisor from FM-frequency for BU2614FS (3.125 KHz STDF expected).
201 static unsigned long gemtek_convfreq(unsigned long freq)
203 return ((freq<<FSCALE) + IF_OFFSET + REF_FREQ/2) / REF_FREQ;
207 * Set FM-frequency.
209 static void gemtek_setfreq(struct gemtek_device *dev, unsigned long freq)
212 if (keepmuted && hardmute && dev->muted)
213 return;
215 if (freq < GEMTEK_LOWFREQ)
216 freq = GEMTEK_LOWFREQ;
217 else if (freq > GEMTEK_HIGHFREQ)
218 freq = GEMTEK_HIGHFREQ;
220 dev->lastfreq = freq;
221 dev->muted = 0;
223 gemtek_bu2614_set(dev, BU2614_PORT, 0);
224 gemtek_bu2614_set(dev, BU2614_FMES, 0);
225 gemtek_bu2614_set(dev, BU2614_SWIN, 0); /* FM-mode */
226 gemtek_bu2614_set(dev, BU2614_SWAL, 0);
227 gemtek_bu2614_set(dev, BU2614_FMUN, 1); /* GT bit set */
228 gemtek_bu2614_set(dev, BU2614_TEST, 0);
230 gemtek_bu2614_set(dev, BU2614_STDF, GEMTEK_STDF_3_125_KHZ);
231 gemtek_bu2614_set(dev, BU2614_FREQ, gemtek_convfreq(freq));
233 gemtek_bu2614_transmit(dev);
237 * Set mute flag.
239 static void gemtek_mute(struct gemtek_device *dev)
241 int i;
242 dev->muted = 1;
244 if (hardmute) {
245 /* Turn off PLL, disable data output */
246 gemtek_bu2614_set(dev, BU2614_PORT, 0);
247 gemtek_bu2614_set(dev, BU2614_FMES, 0); /* CT bit off */
248 gemtek_bu2614_set(dev, BU2614_SWIN, 0); /* FM-mode */
249 gemtek_bu2614_set(dev, BU2614_SWAL, 0);
250 gemtek_bu2614_set(dev, BU2614_FMUN, 0); /* GT bit off */
251 gemtek_bu2614_set(dev, BU2614_TEST, 0);
252 gemtek_bu2614_set(dev, BU2614_STDF, GEMTEK_PLL_OFF);
253 gemtek_bu2614_set(dev, BU2614_FREQ, 0);
254 gemtek_bu2614_transmit(dev);
255 } else {
256 spin_lock(&lock);
258 /* Read bus contents (CE, CK and DA). */
259 i = inb_p(io);
260 /* Write it back with mute flag set. */
261 outb_p((i >> 5) | GEMTEK_MT, io);
262 udelay(SHORT_DELAY);
264 spin_unlock(&lock);
269 * Unset mute flag.
271 static void gemtek_unmute(struct gemtek_device *dev)
273 int i;
274 dev->muted = 0;
276 if (hardmute) {
277 /* Turn PLL back on. */
278 gemtek_setfreq(dev, dev->lastfreq);
279 } else {
280 spin_lock(&lock);
282 i = inb_p(io);
283 outb_p(i >> 5, io);
284 udelay(SHORT_DELAY);
286 spin_unlock(&lock);
291 * Get signal strength (= stereo status).
293 static inline int gemtek_getsigstr(void)
295 return inb_p(io) & GEMTEK_NS ? 0 : 1;
299 * Check if requested card acts like GemTek Radio card.
301 static int gemtek_verify(int port)
303 static int verified = -1;
304 int i, q;
306 if (verified == port)
307 return 1;
309 spin_lock(&lock);
311 q = inb_p(port); /* Read bus contents before probing. */
312 /* Try to turn on CE, CK and DA respectively and check if card responds
313 properly. */
314 for (i = 0; i < 3; ++i) {
315 outb_p(1 << i, port);
316 udelay(SHORT_DELAY);
318 if ((inb_p(port) & (~GEMTEK_NS)) != (0x17 | (1 << (i + 5)))) {
319 spin_unlock(&lock);
320 return 0;
323 outb_p(q >> 5, port); /* Write bus contents back. */
324 udelay(SHORT_DELAY);
326 spin_unlock(&lock);
327 verified = port;
329 return 1;
333 * Automatic probing for card.
335 static int gemtek_probe(void)
337 int ioports[] = { 0x20c, 0x30c, 0x24c, 0x34c, 0x248, 0x28c };
338 int i;
340 if (!probe) {
341 printk(KERN_INFO "Automatic device probing disabled.\n");
342 return -1;
345 printk(KERN_INFO "Automatic device probing enabled.\n");
347 for (i = 0; i < ARRAY_SIZE(ioports); ++i) {
348 printk(KERN_INFO "Trying I/O port 0x%x...\n", ioports[i]);
350 if (!request_region(ioports[i], 1, "gemtek-probe")) {
351 printk(KERN_WARNING "I/O port 0x%x busy!\n",
352 ioports[i]);
353 continue;
356 if (gemtek_verify(ioports[i])) {
357 printk(KERN_INFO "Card found from I/O port "
358 "0x%x!\n", ioports[i]);
360 release_region(ioports[i], 1);
362 io = ioports[i];
363 return io;
366 release_region(ioports[i], 1);
369 printk(KERN_ERR "Automatic probing failed!\n");
371 return -1;
375 * Video 4 Linux stuff.
378 static struct v4l2_queryctrl radio_qctrl[] = {
380 .id = V4L2_CID_AUDIO_MUTE,
381 .name = "Mute",
382 .minimum = 0,
383 .maximum = 1,
384 .default_value = 1,
385 .type = V4L2_CTRL_TYPE_BOOLEAN,
386 }, {
387 .id = V4L2_CID_AUDIO_VOLUME,
388 .name = "Volume",
389 .minimum = 0,
390 .maximum = 65535,
391 .step = 65535,
392 .default_value = 0xff,
393 .type = V4L2_CTRL_TYPE_INTEGER,
397 static int gemtek_exclusive_open(struct file *file)
399 return test_and_set_bit(0, &in_use) ? -EBUSY : 0;
402 static int gemtek_exclusive_release(struct file *file)
404 clear_bit(0, &in_use);
405 return 0;
408 static const struct v4l2_file_operations gemtek_fops = {
409 .owner = THIS_MODULE,
410 .open = gemtek_exclusive_open,
411 .release = gemtek_exclusive_release,
412 .ioctl = video_ioctl2,
415 static int vidioc_querycap(struct file *file, void *priv,
416 struct v4l2_capability *v)
418 strlcpy(v->driver, "radio-gemtek", sizeof(v->driver));
419 strlcpy(v->card, "GemTek", sizeof(v->card));
420 sprintf(v->bus_info, "ISA");
421 v->version = RADIO_VERSION;
422 v->capabilities = V4L2_CAP_TUNER;
423 return 0;
426 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
428 if (v->index > 0)
429 return -EINVAL;
431 strcpy(v->name, "FM");
432 v->type = V4L2_TUNER_RADIO;
433 v->rangelow = GEMTEK_LOWFREQ;
434 v->rangehigh = GEMTEK_HIGHFREQ;
435 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
436 v->signal = 0xffff * gemtek_getsigstr();
437 if (v->signal) {
438 v->audmode = V4L2_TUNER_MODE_STEREO;
439 v->rxsubchans = V4L2_TUNER_SUB_STEREO;
440 } else {
441 v->audmode = V4L2_TUNER_MODE_MONO;
442 v->rxsubchans = V4L2_TUNER_SUB_MONO;
445 return 0;
448 static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
450 if (v->index > 0)
451 return -EINVAL;
452 return 0;
455 static int vidioc_s_frequency(struct file *file, void *priv,
456 struct v4l2_frequency *f)
458 struct gemtek_device *rt = video_drvdata(file);
460 gemtek_setfreq(rt, f->frequency);
462 return 0;
465 static int vidioc_g_frequency(struct file *file, void *priv,
466 struct v4l2_frequency *f)
468 struct gemtek_device *rt = video_drvdata(file);
470 f->type = V4L2_TUNER_RADIO;
471 f->frequency = rt->lastfreq;
472 return 0;
475 static int vidioc_queryctrl(struct file *file, void *priv,
476 struct v4l2_queryctrl *qc)
478 int i;
480 for (i = 0; i < ARRAY_SIZE(radio_qctrl); ++i) {
481 if (qc->id && qc->id == radio_qctrl[i].id) {
482 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
483 return 0;
486 return -EINVAL;
489 static int vidioc_g_ctrl(struct file *file, void *priv,
490 struct v4l2_control *ctrl)
492 struct gemtek_device *rt = video_drvdata(file);
494 switch (ctrl->id) {
495 case V4L2_CID_AUDIO_MUTE:
496 ctrl->value = rt->muted;
497 return 0;
498 case V4L2_CID_AUDIO_VOLUME:
499 if (rt->muted)
500 ctrl->value = 0;
501 else
502 ctrl->value = 65535;
503 return 0;
505 return -EINVAL;
508 static int vidioc_s_ctrl(struct file *file, void *priv,
509 struct v4l2_control *ctrl)
511 struct gemtek_device *rt = video_drvdata(file);
513 switch (ctrl->id) {
514 case V4L2_CID_AUDIO_MUTE:
515 if (ctrl->value)
516 gemtek_mute(rt);
517 else
518 gemtek_unmute(rt);
519 return 0;
520 case V4L2_CID_AUDIO_VOLUME:
521 if (ctrl->value)
522 gemtek_unmute(rt);
523 else
524 gemtek_mute(rt);
525 return 0;
527 return -EINVAL;
530 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
532 if (a->index > 1)
533 return -EINVAL;
535 strcpy(a->name, "Radio");
536 a->capability = V4L2_AUDCAP_STEREO;
537 return 0;
540 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
542 *i = 0;
543 return 0;
546 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
548 if (i != 0)
549 return -EINVAL;
550 return 0;
553 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
555 if (a->index != 0)
556 return -EINVAL;
557 return 0;
560 static const struct v4l2_ioctl_ops gemtek_ioctl_ops = {
561 .vidioc_querycap = vidioc_querycap,
562 .vidioc_g_tuner = vidioc_g_tuner,
563 .vidioc_s_tuner = vidioc_s_tuner,
564 .vidioc_g_audio = vidioc_g_audio,
565 .vidioc_s_audio = vidioc_s_audio,
566 .vidioc_g_input = vidioc_g_input,
567 .vidioc_s_input = vidioc_s_input,
568 .vidioc_g_frequency = vidioc_g_frequency,
569 .vidioc_s_frequency = vidioc_s_frequency,
570 .vidioc_queryctrl = vidioc_queryctrl,
571 .vidioc_g_ctrl = vidioc_g_ctrl,
572 .vidioc_s_ctrl = vidioc_s_ctrl
575 static struct video_device gemtek_radio = {
576 .name = "GemTek Radio card",
577 .fops = &gemtek_fops,
578 .ioctl_ops = &gemtek_ioctl_ops,
579 .release = video_device_release_empty,
583 * Initialization / cleanup related stuff.
587 * Initilize card.
589 static int __init gemtek_init(void)
591 printk(KERN_INFO RADIO_BANNER "\n");
593 spin_lock_init(&lock);
595 gemtek_probe();
596 if (io) {
597 if (!request_region(io, 1, "gemtek")) {
598 printk(KERN_ERR "I/O port 0x%x already in use.\n", io);
599 return -EBUSY;
602 if (!gemtek_verify(io))
603 printk(KERN_WARNING "Card at I/O port 0x%x does not "
604 "respond properly, check your "
605 "configuration.\n", io);
606 else
607 printk(KERN_INFO "Using I/O port 0x%x.\n", io);
608 } else if (probe) {
609 printk(KERN_ERR "Automatic probing failed and no "
610 "fixed I/O port defined.\n");
611 return -ENODEV;
612 } else {
613 printk(KERN_ERR "Automatic probing disabled but no fixed "
614 "I/O port defined.");
615 return -EINVAL;
618 video_set_drvdata(&gemtek_radio, &gemtek_unit);
620 if (video_register_device(&gemtek_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
621 release_region(io, 1);
622 return -EBUSY;
625 /* Set defaults */
626 gemtek_unit.lastfreq = GEMTEK_LOWFREQ;
627 gemtek_unit.bu2614data = 0;
629 if (initmute)
630 gemtek_mute(&gemtek_unit);
632 return 0;
636 * Module cleanup
638 static void __exit gemtek_exit(void)
640 if (shutdown) {
641 hardmute = 1; /* Turn off PLL */
642 gemtek_mute(&gemtek_unit);
643 } else {
644 printk(KERN_INFO "Module unloaded but card not muted!\n");
647 video_unregister_device(&gemtek_radio);
648 release_region(io, 1);
651 module_init(gemtek_init);
652 module_exit(gemtek_exit);