blob: 4d76e710194b47414d9a5d9d80e64809e9469504 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* zoltrix radio plus driver for Linux radio support
2 * (c) 1998 C. van Schaik <carl@leg.uct.ac.za>
3 *
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03004 * BUGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Due to the inconsistency in reading from the signal flags
6 * it is difficult to get an accurate tuned signal.
7 *
8 * It seems that the card is not linear to 0 volume. It cuts off
9 * at a low volume, and it is not possible (at least I have not found)
10 * to get fine volume control over the low volume range.
11 *
12 * Some code derived from code by Romolo Manfredini
13 * romolo@bicnet.it
14 *
15 * 1999-05-06 - (C. van Schaik)
16 * - Make signal strength and stereo scans
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030017 * kinder to cpu while in delay
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * 1999-01-05 - (C. van Schaik)
19 * - Changed tuning to 1/160Mhz accuracy
20 * - Added stereo support
21 * (card defaults to stereo)
22 * (can explicitly force mono on the card)
23 * (can detect if station is in stereo)
24 * - Added unmute function
25 * - Reworked ioctl functions
26 * 2002-07-15 - Fix Stereo typo
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -030027 *
28 * 2006-07-24 - Converted to V4L2 API
29 * by Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
32#include <linux/module.h> /* Modules */
33#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070034#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/delay.h> /* udelay, msleep */
36#include <asm/io.h> /* outb, outb_p */
37#include <asm/uaccess.h> /* copy to/from user */
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -030038#include <linux/videodev2.h> /* kernel radio structs */
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030039#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030040#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -030042#include <linux/version.h> /* for KERNEL_VERSION MACRO */
43#define RADIO_VERSION KERNEL_VERSION(0,0,2)
44
45static struct v4l2_queryctrl radio_qctrl[] = {
46 {
47 .id = V4L2_CID_AUDIO_MUTE,
48 .name = "Mute",
49 .minimum = 0,
50 .maximum = 1,
51 .default_value = 1,
52 .type = V4L2_CTRL_TYPE_BOOLEAN,
53 },{
54 .id = V4L2_CID_AUDIO_VOLUME,
55 .name = "Volume",
56 .minimum = 0,
57 .maximum = 65535,
58 .step = 4096,
59 .default_value = 0xff,
60 .type = V4L2_CTRL_TYPE_INTEGER,
61 }
62};
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#ifndef CONFIG_RADIO_ZOLTRIX_PORT
65#define CONFIG_RADIO_ZOLTRIX_PORT -1
66#endif
67
68static int io = CONFIG_RADIO_ZOLTRIX_PORT;
69static int radio_nr = -1;
70
71struct zol_device {
Hans Verkuil3ca685a2008-08-23 04:49:13 -030072 unsigned long in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 int port;
74 int curvol;
75 unsigned long curfreq;
76 int muted;
77 unsigned int stereo;
Ingo Molnar3593cab2006-02-07 06:49:14 -020078 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
81static int zol_setvol(struct zol_device *dev, int vol)
82{
83 dev->curvol = vol;
84 if (dev->muted)
85 return 0;
86
Ingo Molnar3593cab2006-02-07 06:49:14 -020087 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (vol == 0) {
89 outb(0, io);
90 outb(0, io);
91 inb(io + 3); /* Zoltrix needs to be read to confirm */
Ingo Molnar3593cab2006-02-07 06:49:14 -020092 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return 0;
94 }
95
96 outb(dev->curvol-1, io);
97 msleep(10);
98 inb(io + 2);
Ingo Molnar3593cab2006-02-07 06:49:14 -020099 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return 0;
101}
102
103static void zol_mute(struct zol_device *dev)
104{
105 dev->muted = 1;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200106 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 outb(0, io);
108 outb(0, io);
109 inb(io + 3); /* Zoltrix needs to be read to confirm */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200110 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113static void zol_unmute(struct zol_device *dev)
114{
115 dev->muted = 0;
116 zol_setvol(dev, dev->curvol);
117}
118
119static int zol_setfreq(struct zol_device *dev, unsigned long freq)
120{
121 /* tunes the radio to the desired frequency */
122 unsigned long long bitmask, f, m;
123 unsigned int stereo = dev->stereo;
124 int i;
125
126 if (freq == 0)
127 return 1;
128 m = (freq / 160 - 8800) * 2;
129 f = (unsigned long long) m + 0x4d1c;
130
131 bitmask = 0xc480402c10080000ull;
132 i = 45;
133
Ingo Molnar3593cab2006-02-07 06:49:14 -0200134 mutex_lock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 outb(0, io);
137 outb(0, io);
138 inb(io + 3); /* Zoltrix needs to be read to confirm */
139
140 outb(0x40, io);
141 outb(0xc0, io);
142
143 bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ ( stereo << 31));
144 while (i--) {
145 if ((bitmask & 0x8000000000000000ull) != 0) {
146 outb(0x80, io);
147 udelay(50);
148 outb(0x00, io);
149 udelay(50);
150 outb(0x80, io);
151 udelay(50);
152 } else {
153 outb(0xc0, io);
154 udelay(50);
155 outb(0x40, io);
156 udelay(50);
157 outb(0xc0, io);
158 udelay(50);
159 }
160 bitmask *= 2;
161 }
162 /* termination sequence */
163 outb(0x80, io);
164 outb(0xc0, io);
165 outb(0x40, io);
166 udelay(1000);
167 inb(io+2);
168
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300169 udelay(1000);
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (dev->muted)
172 {
173 outb(0, io);
174 outb(0, io);
175 inb(io + 3);
176 udelay(1000);
177 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300178
Ingo Molnar3593cab2006-02-07 06:49:14 -0200179 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if(!dev->muted)
182 {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300183 zol_setvol(dev, dev->curvol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185 return 0;
186}
187
188/* Get signal strength */
189
190static int zol_getsigstr(struct zol_device *dev)
191{
192 int a, b;
193
Ingo Molnar3593cab2006-02-07 06:49:14 -0200194 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 outb(0x00, io); /* This stuff I found to do nothing */
196 outb(dev->curvol, io);
197 msleep(20);
198
199 a = inb(io);
200 msleep(10);
201 b = inb(io);
202
Ingo Molnar3593cab2006-02-07 06:49:14 -0200203 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (a != b)
206 return (0);
207
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300208 if ((a == 0xcf) || (a == 0xdf) /* I found this out by playing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 || (a == 0xef)) /* with a binary scanner on the card io */
210 return (1);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300211 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214static int zol_is_stereo (struct zol_device *dev)
215{
216 int x1, x2;
217
Ingo Molnar3593cab2006-02-07 06:49:14 -0200218 mutex_lock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 outb(0x00, io);
221 outb(dev->curvol, io);
222 msleep(20);
223
224 x1 = inb(io);
225 msleep(10);
226 x2 = inb(io);
227
Ingo Molnar3593cab2006-02-07 06:49:14 -0200228 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if ((x1 == x2) && (x1 == 0xcf))
231 return 1;
232 return 0;
233}
234
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300235static int vidioc_querycap(struct file *file, void *priv,
236 struct v4l2_capability *v)
237{
238 strlcpy(v->driver, "radio-zoltrix", sizeof(v->driver));
239 strlcpy(v->card, "Zoltrix Radio", sizeof(v->card));
240 sprintf(v->bus_info, "ISA");
241 v->version = RADIO_VERSION;
242 v->capabilities = V4L2_CAP_TUNER;
243 return 0;
244}
245
246static int vidioc_g_tuner(struct file *file, void *priv,
247 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct video_device *dev = video_devdata(file);
250 struct zol_device *zol = dev->priv;
251
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300252 if (v->index > 0)
253 return -EINVAL;
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300254
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300255 strcpy(v->name, "FM");
256 v->type = V4L2_TUNER_RADIO;
257 v->rangelow = (88*16000);
258 v->rangehigh = (108*16000);
259 v->rxsubchans = V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
260 v->capability = V4L2_TUNER_CAP_LOW;
261 if (zol_is_stereo(zol))
262 v->audmode = V4L2_TUNER_MODE_STEREO;
263 else
264 v->audmode = V4L2_TUNER_MODE_MONO;
265 v->signal = 0xFFFF*zol_getsigstr(zol);
266 return 0;
267}
268
269static int vidioc_s_tuner(struct file *file, void *priv,
270 struct v4l2_tuner *v)
271{
272 if (v->index > 0)
273 return -EINVAL;
274 return 0;
275}
276
277static int vidioc_s_frequency(struct file *file, void *priv,
278 struct v4l2_frequency *f)
279{
280 struct video_device *dev = video_devdata(file);
281 struct zol_device *zol = dev->priv;
282
283 zol->curfreq = f->frequency;
284 zol_setfreq(zol, zol->curfreq);
285 return 0;
286}
287
288static int vidioc_g_frequency(struct file *file, void *priv,
289 struct v4l2_frequency *f)
290{
291 struct video_device *dev = video_devdata(file);
292 struct zol_device *zol = dev->priv;
293
294 f->type = V4L2_TUNER_RADIO;
295 f->frequency = zol->curfreq;
296 return 0;
297}
298
299static int vidioc_queryctrl(struct file *file, void *priv,
300 struct v4l2_queryctrl *qc)
301{
302 int i;
303
304 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
305 if (qc->id && qc->id == radio_qctrl[i].id) {
306 memcpy(qc, &(radio_qctrl[i]),
307 sizeof(*qc));
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300308 return 0;
309 }
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300310 }
311 return -EINVAL;
312}
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300313
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300314static int vidioc_g_ctrl(struct file *file, void *priv,
315 struct v4l2_control *ctrl)
316{
317 struct video_device *dev = video_devdata(file);
318 struct zol_device *zol = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300320 switch (ctrl->id) {
321 case V4L2_CID_AUDIO_MUTE:
322 ctrl->value = zol->muted;
323 return 0;
324 case V4L2_CID_AUDIO_VOLUME:
325 ctrl->value = zol->curvol * 4096;
326 return 0;
327 }
328 return -EINVAL;
329}
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300330
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300331static int vidioc_s_ctrl(struct file *file, void *priv,
332 struct v4l2_control *ctrl)
333{
334 struct video_device *dev = video_devdata(file);
335 struct zol_device *zol = dev->priv;
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300336
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300337 switch (ctrl->id) {
338 case V4L2_CID_AUDIO_MUTE:
339 if (ctrl->value)
340 zol_mute(zol);
341 else {
342 zol_unmute(zol);
343 zol_setvol(zol,zol->curvol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300345 return 0;
346 case V4L2_CID_AUDIO_VOLUME:
347 zol_setvol(zol,ctrl->value/4096);
348 return 0;
349 }
350 zol->stereo = 1;
351 zol_setfreq(zol, zol->curfreq);
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300352#if 0
353/* FIXME: Implement stereo/mono switch on V4L2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (v->mode & VIDEO_SOUND_STEREO) {
355 zol->stereo = 1;
356 zol_setfreq(zol, zol->curfreq);
357 }
358 if (v->mode & VIDEO_SOUND_MONO) {
359 zol->stereo = 0;
360 zol_setfreq(zol, zol->curfreq);
361 }
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300362#endif
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300363 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300366static int vidioc_g_audio(struct file *file, void *priv,
367 struct v4l2_audio *a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300369 if (a->index > 1)
370 return -EINVAL;
371
372 strcpy(a->name, "Radio");
373 a->capability = V4L2_AUDCAP_STEREO;
374 return 0;
375}
376
377static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
378{
379 *i = 0;
380 return 0;
381}
382
383static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
384{
385 if (i != 0)
386 return -EINVAL;
387 return 0;
388}
389
390static int vidioc_s_audio(struct file *file, void *priv,
391 struct v4l2_audio *a)
392{
393 if (a->index != 0)
394 return -EINVAL;
395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398static struct zol_device zoltrix_unit;
399
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300400static int zoltrix_exclusive_open(struct inode *inode, struct file *file)
401{
402 return test_and_set_bit(0, &zoltrix_unit.in_use) ? -EBUSY : 0;
403}
404
405static int zoltrix_exclusive_release(struct inode *inode, struct file *file)
406{
407 clear_bit(0, &zoltrix_unit.in_use);
408 return 0;
409}
410
Arjan van de Venfa027c22007-02-12 00:55:33 -0800411static const struct file_operations zoltrix_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 .owner = THIS_MODULE,
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300414 .open = zoltrix_exclusive_open,
415 .release = zoltrix_exclusive_release,
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300416 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300417#ifdef CONFIG_COMPAT
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200418 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300419#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 .llseek = no_llseek,
421};
422
Hans Verkuila3998102008-07-21 02:57:38 -0300423static const struct v4l2_ioctl_ops zoltrix_ioctl_ops = {
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300424 .vidioc_querycap = vidioc_querycap,
425 .vidioc_g_tuner = vidioc_g_tuner,
426 .vidioc_s_tuner = vidioc_s_tuner,
427 .vidioc_g_audio = vidioc_g_audio,
428 .vidioc_s_audio = vidioc_s_audio,
429 .vidioc_g_input = vidioc_g_input,
430 .vidioc_s_input = vidioc_s_input,
431 .vidioc_g_frequency = vidioc_g_frequency,
432 .vidioc_s_frequency = vidioc_s_frequency,
433 .vidioc_queryctrl = vidioc_queryctrl,
434 .vidioc_g_ctrl = vidioc_g_ctrl,
435 .vidioc_s_ctrl = vidioc_s_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436};
437
Hans Verkuila3998102008-07-21 02:57:38 -0300438static struct video_device zoltrix_radio = {
Hans Verkuila3998102008-07-21 02:57:38 -0300439 .name = "Zoltrix Radio Plus",
Hans Verkuila3998102008-07-21 02:57:38 -0300440 .fops = &zoltrix_fops,
441 .ioctl_ops = &zoltrix_ioctl_ops,
442};
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444static int __init zoltrix_init(void)
445{
446 if (io == -1) {
447 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
448 return -EINVAL;
449 }
450 if ((io != 0x20c) && (io != 0x30c)) {
451 printk(KERN_ERR "zoltrix: invalid port, try 0x20c or 0x30c\n");
452 return -ENXIO;
453 }
454
455 zoltrix_radio.priv = &zoltrix_unit;
456 if (!request_region(io, 2, "zoltrix")) {
457 printk(KERN_ERR "zoltrix: port 0x%x already in use\n", io);
458 return -EBUSY;
459 }
460
Hans Verkuilcba99ae2008-09-03 17:11:58 -0300461 if (video_register_device(&zoltrix_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 release_region(io, 2);
463 return -EINVAL;
464 }
465 printk(KERN_INFO "Zoltrix Radio Plus card driver.\n");
466
Ingo Molnar3593cab2006-02-07 06:49:14 -0200467 mutex_init(&zoltrix_unit.lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* mute card - prevents noisy bootups */
470
471 /* this ensures that the volume is all the way down */
472
473 outb(0, io);
474 outb(0, io);
475 msleep(20);
476 inb(io + 3);
477
478 zoltrix_unit.curvol = 0;
479 zoltrix_unit.stereo = 1;
480
481 return 0;
482}
483
484MODULE_AUTHOR("C.van Schaik");
485MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
486MODULE_LICENSE("GPL");
487
488module_param(io, int, 0);
489MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
490module_param(radio_nr, int, 0);
491
492static void __exit zoltrix_cleanup_module(void)
493{
494 video_unregister_device(&zoltrix_radio);
495 release_region(io, 2);
496}
497
498module_init(zoltrix_init);
499module_exit(zoltrix_cleanup_module);
500