blob: 15b10bad679660efa22b59bc3d70f161841c07f0 [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
Alexey Klimovb4be2042008-10-09 13:46:59 -0300126 if (freq == 0) {
127 printk(KERN_WARNING "zoltrix: received zero freq. Failed to set.\n");
128 return -EINVAL;
129 }
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 m = (freq / 160 - 8800) * 2;
132 f = (unsigned long long) m + 0x4d1c;
133
134 bitmask = 0xc480402c10080000ull;
135 i = 45;
136
Ingo Molnar3593cab2006-02-07 06:49:14 -0200137 mutex_lock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 outb(0, io);
140 outb(0, io);
141 inb(io + 3); /* Zoltrix needs to be read to confirm */
142
143 outb(0x40, io);
144 outb(0xc0, io);
145
146 bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ ( stereo << 31));
147 while (i--) {
148 if ((bitmask & 0x8000000000000000ull) != 0) {
149 outb(0x80, io);
150 udelay(50);
151 outb(0x00, io);
152 udelay(50);
153 outb(0x80, io);
154 udelay(50);
155 } else {
156 outb(0xc0, io);
157 udelay(50);
158 outb(0x40, io);
159 udelay(50);
160 outb(0xc0, io);
161 udelay(50);
162 }
163 bitmask *= 2;
164 }
165 /* termination sequence */
166 outb(0x80, io);
167 outb(0xc0, io);
168 outb(0x40, io);
169 udelay(1000);
170 inb(io+2);
171
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300172 udelay(1000);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (dev->muted)
175 {
176 outb(0, io);
177 outb(0, io);
178 inb(io + 3);
179 udelay(1000);
180 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300181
Ingo Molnar3593cab2006-02-07 06:49:14 -0200182 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if(!dev->muted)
185 {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300186 zol_setvol(dev, dev->curvol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188 return 0;
189}
190
191/* Get signal strength */
192
193static int zol_getsigstr(struct zol_device *dev)
194{
195 int a, b;
196
Ingo Molnar3593cab2006-02-07 06:49:14 -0200197 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 outb(0x00, io); /* This stuff I found to do nothing */
199 outb(dev->curvol, io);
200 msleep(20);
201
202 a = inb(io);
203 msleep(10);
204 b = inb(io);
205
Ingo Molnar3593cab2006-02-07 06:49:14 -0200206 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (a != b)
209 return (0);
210
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300211 if ((a == 0xcf) || (a == 0xdf) /* I found this out by playing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 || (a == 0xef)) /* with a binary scanner on the card io */
213 return (1);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300214 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217static int zol_is_stereo (struct zol_device *dev)
218{
219 int x1, x2;
220
Ingo Molnar3593cab2006-02-07 06:49:14 -0200221 mutex_lock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 outb(0x00, io);
224 outb(dev->curvol, io);
225 msleep(20);
226
227 x1 = inb(io);
228 msleep(10);
229 x2 = inb(io);
230
Ingo Molnar3593cab2006-02-07 06:49:14 -0200231 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if ((x1 == x2) && (x1 == 0xcf))
234 return 1;
235 return 0;
236}
237
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300238static int vidioc_querycap(struct file *file, void *priv,
239 struct v4l2_capability *v)
240{
241 strlcpy(v->driver, "radio-zoltrix", sizeof(v->driver));
242 strlcpy(v->card, "Zoltrix Radio", sizeof(v->card));
243 sprintf(v->bus_info, "ISA");
244 v->version = RADIO_VERSION;
245 v->capabilities = V4L2_CAP_TUNER;
246 return 0;
247}
248
249static int vidioc_g_tuner(struct file *file, void *priv,
250 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300252 struct zol_device *zol = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300254 if (v->index > 0)
255 return -EINVAL;
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300256
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300257 strcpy(v->name, "FM");
258 v->type = V4L2_TUNER_RADIO;
259 v->rangelow = (88*16000);
260 v->rangehigh = (108*16000);
261 v->rxsubchans = V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
262 v->capability = V4L2_TUNER_CAP_LOW;
263 if (zol_is_stereo(zol))
264 v->audmode = V4L2_TUNER_MODE_STEREO;
265 else
266 v->audmode = V4L2_TUNER_MODE_MONO;
267 v->signal = 0xFFFF*zol_getsigstr(zol);
268 return 0;
269}
270
271static int vidioc_s_tuner(struct file *file, void *priv,
272 struct v4l2_tuner *v)
273{
274 if (v->index > 0)
275 return -EINVAL;
276 return 0;
277}
278
279static int vidioc_s_frequency(struct file *file, void *priv,
280 struct v4l2_frequency *f)
281{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300282 struct zol_device *zol = video_drvdata(file);
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300283
284 zol->curfreq = f->frequency;
Alexey Klimovb4be2042008-10-09 13:46:59 -0300285 if (zol_setfreq(zol, zol->curfreq) != 0) {
286 printk(KERN_WARNING "zoltrix: Set frequency failed.\n");
287 return -EINVAL;
288 }
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300289 return 0;
290}
291
292static int vidioc_g_frequency(struct file *file, void *priv,
293 struct v4l2_frequency *f)
294{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300295 struct zol_device *zol = video_drvdata(file);
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300296
297 f->type = V4L2_TUNER_RADIO;
298 f->frequency = zol->curfreq;
299 return 0;
300}
301
302static int vidioc_queryctrl(struct file *file, void *priv,
303 struct v4l2_queryctrl *qc)
304{
305 int i;
306
307 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
308 if (qc->id && qc->id == radio_qctrl[i].id) {
309 memcpy(qc, &(radio_qctrl[i]),
310 sizeof(*qc));
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300311 return 0;
312 }
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300313 }
314 return -EINVAL;
315}
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300316
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300317static int vidioc_g_ctrl(struct file *file, void *priv,
318 struct v4l2_control *ctrl)
319{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300320 struct zol_device *zol = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300322 switch (ctrl->id) {
323 case V4L2_CID_AUDIO_MUTE:
324 ctrl->value = zol->muted;
325 return 0;
326 case V4L2_CID_AUDIO_VOLUME:
327 ctrl->value = zol->curvol * 4096;
328 return 0;
329 }
330 return -EINVAL;
331}
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300332
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300333static int vidioc_s_ctrl(struct file *file, void *priv,
334 struct v4l2_control *ctrl)
335{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300336 struct zol_device *zol = video_drvdata(file);
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300337
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300338 switch (ctrl->id) {
339 case V4L2_CID_AUDIO_MUTE:
340 if (ctrl->value)
341 zol_mute(zol);
342 else {
343 zol_unmute(zol);
344 zol_setvol(zol,zol->curvol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300346 return 0;
347 case V4L2_CID_AUDIO_VOLUME:
348 zol_setvol(zol,ctrl->value/4096);
349 return 0;
350 }
351 zol->stereo = 1;
Alexey Klimovb4be2042008-10-09 13:46:59 -0300352 if (zol_setfreq(zol, zol->curfreq) != 0) {
353 printk(KERN_WARNING "zoltrix: Set frequency failed.\n");
354 return -EINVAL;
355 }
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300356#if 0
357/* FIXME: Implement stereo/mono switch on V4L2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (v->mode & VIDEO_SOUND_STEREO) {
359 zol->stereo = 1;
360 zol_setfreq(zol, zol->curfreq);
361 }
362 if (v->mode & VIDEO_SOUND_MONO) {
363 zol->stereo = 0;
364 zol_setfreq(zol, zol->curfreq);
365 }
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300366#endif
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300367 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300370static int vidioc_g_audio(struct file *file, void *priv,
371 struct v4l2_audio *a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300373 if (a->index > 1)
374 return -EINVAL;
375
376 strcpy(a->name, "Radio");
377 a->capability = V4L2_AUDCAP_STEREO;
378 return 0;
379}
380
381static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
382{
383 *i = 0;
384 return 0;
385}
386
387static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
388{
389 if (i != 0)
390 return -EINVAL;
391 return 0;
392}
393
394static int vidioc_s_audio(struct file *file, void *priv,
395 struct v4l2_audio *a)
396{
397 if (a->index != 0)
398 return -EINVAL;
399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402static struct zol_device zoltrix_unit;
403
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300404static int zoltrix_exclusive_open(struct inode *inode, struct file *file)
405{
406 return test_and_set_bit(0, &zoltrix_unit.in_use) ? -EBUSY : 0;
407}
408
409static int zoltrix_exclusive_release(struct inode *inode, struct file *file)
410{
411 clear_bit(0, &zoltrix_unit.in_use);
412 return 0;
413}
414
Arjan van de Venfa027c22007-02-12 00:55:33 -0800415static const struct file_operations zoltrix_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 .owner = THIS_MODULE,
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300418 .open = zoltrix_exclusive_open,
419 .release = zoltrix_exclusive_release,
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300420 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300421#ifdef CONFIG_COMPAT
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200422 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300423#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 .llseek = no_llseek,
425};
426
Hans Verkuila3998102008-07-21 02:57:38 -0300427static const struct v4l2_ioctl_ops zoltrix_ioctl_ops = {
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300428 .vidioc_querycap = vidioc_querycap,
429 .vidioc_g_tuner = vidioc_g_tuner,
430 .vidioc_s_tuner = vidioc_s_tuner,
431 .vidioc_g_audio = vidioc_g_audio,
432 .vidioc_s_audio = vidioc_s_audio,
433 .vidioc_g_input = vidioc_g_input,
434 .vidioc_s_input = vidioc_s_input,
435 .vidioc_g_frequency = vidioc_g_frequency,
436 .vidioc_s_frequency = vidioc_s_frequency,
437 .vidioc_queryctrl = vidioc_queryctrl,
438 .vidioc_g_ctrl = vidioc_g_ctrl,
439 .vidioc_s_ctrl = vidioc_s_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440};
441
Hans Verkuila3998102008-07-21 02:57:38 -0300442static struct video_device zoltrix_radio = {
Hans Verkuila3998102008-07-21 02:57:38 -0300443 .name = "Zoltrix Radio Plus",
Hans Verkuila3998102008-07-21 02:57:38 -0300444 .fops = &zoltrix_fops,
445 .ioctl_ops = &zoltrix_ioctl_ops,
Hans Verkuilaa5e90a2008-08-23 06:23:55 -0300446 .release = video_device_release_empty,
Hans Verkuila3998102008-07-21 02:57:38 -0300447};
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static int __init zoltrix_init(void)
450{
451 if (io == -1) {
452 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
453 return -EINVAL;
454 }
455 if ((io != 0x20c) && (io != 0x30c)) {
456 printk(KERN_ERR "zoltrix: invalid port, try 0x20c or 0x30c\n");
457 return -ENXIO;
458 }
459
Hans Verkuil601e9442008-08-23 07:24:07 -0300460 video_set_drvdata(&zoltrix_radio, &zoltrix_unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (!request_region(io, 2, "zoltrix")) {
462 printk(KERN_ERR "zoltrix: port 0x%x already in use\n", io);
463 return -EBUSY;
464 }
465
Hans Verkuilcba99ae2008-09-03 17:11:58 -0300466 if (video_register_device(&zoltrix_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 release_region(io, 2);
468 return -EINVAL;
469 }
470 printk(KERN_INFO "Zoltrix Radio Plus card driver.\n");
471
Ingo Molnar3593cab2006-02-07 06:49:14 -0200472 mutex_init(&zoltrix_unit.lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* mute card - prevents noisy bootups */
475
476 /* this ensures that the volume is all the way down */
477
478 outb(0, io);
479 outb(0, io);
480 msleep(20);
481 inb(io + 3);
482
483 zoltrix_unit.curvol = 0;
484 zoltrix_unit.stereo = 1;
485
486 return 0;
487}
488
489MODULE_AUTHOR("C.van Schaik");
490MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
491MODULE_LICENSE("GPL");
492
493module_param(io, int, 0);
494MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
495module_param(radio_nr, int, 0);
496
497static void __exit zoltrix_cleanup_module(void)
498{
499 video_unregister_device(&zoltrix_radio);
500 release_region(io, 2);
501}
502
503module_init(zoltrix_init);
504module_exit(zoltrix_cleanup_module);
505