blob: cf0355bb6ef75748d21432d4f3a115b3cc9c9af0 [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 {
72 int port;
73 int curvol;
74 unsigned long curfreq;
75 int muted;
76 unsigned int stereo;
Ingo Molnar3593cab2006-02-07 06:49:14 -020077 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
80static int zol_setvol(struct zol_device *dev, int vol)
81{
82 dev->curvol = vol;
83 if (dev->muted)
84 return 0;
85
Ingo Molnar3593cab2006-02-07 06:49:14 -020086 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (vol == 0) {
88 outb(0, io);
89 outb(0, io);
90 inb(io + 3); /* Zoltrix needs to be read to confirm */
Ingo Molnar3593cab2006-02-07 06:49:14 -020091 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return 0;
93 }
94
95 outb(dev->curvol-1, io);
96 msleep(10);
97 inb(io + 2);
Ingo Molnar3593cab2006-02-07 06:49:14 -020098 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return 0;
100}
101
102static void zol_mute(struct zol_device *dev)
103{
104 dev->muted = 1;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200105 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 outb(0, io);
107 outb(0, io);
108 inb(io + 3); /* Zoltrix needs to be read to confirm */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200109 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112static void zol_unmute(struct zol_device *dev)
113{
114 dev->muted = 0;
115 zol_setvol(dev, dev->curvol);
116}
117
118static int zol_setfreq(struct zol_device *dev, unsigned long freq)
119{
120 /* tunes the radio to the desired frequency */
121 unsigned long long bitmask, f, m;
122 unsigned int stereo = dev->stereo;
123 int i;
124
125 if (freq == 0)
126 return 1;
127 m = (freq / 160 - 8800) * 2;
128 f = (unsigned long long) m + 0x4d1c;
129
130 bitmask = 0xc480402c10080000ull;
131 i = 45;
132
Ingo Molnar3593cab2006-02-07 06:49:14 -0200133 mutex_lock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 outb(0, io);
136 outb(0, io);
137 inb(io + 3); /* Zoltrix needs to be read to confirm */
138
139 outb(0x40, io);
140 outb(0xc0, io);
141
142 bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ ( stereo << 31));
143 while (i--) {
144 if ((bitmask & 0x8000000000000000ull) != 0) {
145 outb(0x80, io);
146 udelay(50);
147 outb(0x00, io);
148 udelay(50);
149 outb(0x80, io);
150 udelay(50);
151 } else {
152 outb(0xc0, io);
153 udelay(50);
154 outb(0x40, io);
155 udelay(50);
156 outb(0xc0, io);
157 udelay(50);
158 }
159 bitmask *= 2;
160 }
161 /* termination sequence */
162 outb(0x80, io);
163 outb(0xc0, io);
164 outb(0x40, io);
165 udelay(1000);
166 inb(io+2);
167
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300168 udelay(1000);
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (dev->muted)
171 {
172 outb(0, io);
173 outb(0, io);
174 inb(io + 3);
175 udelay(1000);
176 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300177
Ingo Molnar3593cab2006-02-07 06:49:14 -0200178 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if(!dev->muted)
181 {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300182 zol_setvol(dev, dev->curvol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184 return 0;
185}
186
187/* Get signal strength */
188
189static int zol_getsigstr(struct zol_device *dev)
190{
191 int a, b;
192
Ingo Molnar3593cab2006-02-07 06:49:14 -0200193 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 outb(0x00, io); /* This stuff I found to do nothing */
195 outb(dev->curvol, io);
196 msleep(20);
197
198 a = inb(io);
199 msleep(10);
200 b = inb(io);
201
Ingo Molnar3593cab2006-02-07 06:49:14 -0200202 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (a != b)
205 return (0);
206
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300207 if ((a == 0xcf) || (a == 0xdf) /* I found this out by playing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 || (a == 0xef)) /* with a binary scanner on the card io */
209 return (1);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300210 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213static int zol_is_stereo (struct zol_device *dev)
214{
215 int x1, x2;
216
Ingo Molnar3593cab2006-02-07 06:49:14 -0200217 mutex_lock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 outb(0x00, io);
220 outb(dev->curvol, io);
221 msleep(20);
222
223 x1 = inb(io);
224 msleep(10);
225 x2 = inb(io);
226
Ingo Molnar3593cab2006-02-07 06:49:14 -0200227 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if ((x1 == x2) && (x1 == 0xcf))
230 return 1;
231 return 0;
232}
233
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300234static int vidioc_querycap(struct file *file, void *priv,
235 struct v4l2_capability *v)
236{
237 strlcpy(v->driver, "radio-zoltrix", sizeof(v->driver));
238 strlcpy(v->card, "Zoltrix Radio", sizeof(v->card));
239 sprintf(v->bus_info, "ISA");
240 v->version = RADIO_VERSION;
241 v->capabilities = V4L2_CAP_TUNER;
242 return 0;
243}
244
245static int vidioc_g_tuner(struct file *file, void *priv,
246 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct video_device *dev = video_devdata(file);
249 struct zol_device *zol = dev->priv;
250
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300251 if (v->index > 0)
252 return -EINVAL;
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300253
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300254 strcpy(v->name, "FM");
255 v->type = V4L2_TUNER_RADIO;
256 v->rangelow = (88*16000);
257 v->rangehigh = (108*16000);
258 v->rxsubchans = V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
259 v->capability = V4L2_TUNER_CAP_LOW;
260 if (zol_is_stereo(zol))
261 v->audmode = V4L2_TUNER_MODE_STEREO;
262 else
263 v->audmode = V4L2_TUNER_MODE_MONO;
264 v->signal = 0xFFFF*zol_getsigstr(zol);
265 return 0;
266}
267
268static int vidioc_s_tuner(struct file *file, void *priv,
269 struct v4l2_tuner *v)
270{
271 if (v->index > 0)
272 return -EINVAL;
273 return 0;
274}
275
276static int vidioc_s_frequency(struct file *file, void *priv,
277 struct v4l2_frequency *f)
278{
279 struct video_device *dev = video_devdata(file);
280 struct zol_device *zol = dev->priv;
281
282 zol->curfreq = f->frequency;
283 zol_setfreq(zol, zol->curfreq);
284 return 0;
285}
286
287static int vidioc_g_frequency(struct file *file, void *priv,
288 struct v4l2_frequency *f)
289{
290 struct video_device *dev = video_devdata(file);
291 struct zol_device *zol = dev->priv;
292
293 f->type = V4L2_TUNER_RADIO;
294 f->frequency = zol->curfreq;
295 return 0;
296}
297
298static int vidioc_queryctrl(struct file *file, void *priv,
299 struct v4l2_queryctrl *qc)
300{
301 int i;
302
303 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
304 if (qc->id && qc->id == radio_qctrl[i].id) {
305 memcpy(qc, &(radio_qctrl[i]),
306 sizeof(*qc));
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300307 return 0;
308 }
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300309 }
310 return -EINVAL;
311}
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300312
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300313static int vidioc_g_ctrl(struct file *file, void *priv,
314 struct v4l2_control *ctrl)
315{
316 struct video_device *dev = video_devdata(file);
317 struct zol_device *zol = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300319 switch (ctrl->id) {
320 case V4L2_CID_AUDIO_MUTE:
321 ctrl->value = zol->muted;
322 return 0;
323 case V4L2_CID_AUDIO_VOLUME:
324 ctrl->value = zol->curvol * 4096;
325 return 0;
326 }
327 return -EINVAL;
328}
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300329
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300330static int vidioc_s_ctrl(struct file *file, void *priv,
331 struct v4l2_control *ctrl)
332{
333 struct video_device *dev = video_devdata(file);
334 struct zol_device *zol = dev->priv;
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300335
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300336 switch (ctrl->id) {
337 case V4L2_CID_AUDIO_MUTE:
338 if (ctrl->value)
339 zol_mute(zol);
340 else {
341 zol_unmute(zol);
342 zol_setvol(zol,zol->curvol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300344 return 0;
345 case V4L2_CID_AUDIO_VOLUME:
346 zol_setvol(zol,ctrl->value/4096);
347 return 0;
348 }
349 zol->stereo = 1;
350 zol_setfreq(zol, zol->curfreq);
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300351#if 0
352/* FIXME: Implement stereo/mono switch on V4L2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (v->mode & VIDEO_SOUND_STEREO) {
354 zol->stereo = 1;
355 zol_setfreq(zol, zol->curfreq);
356 }
357 if (v->mode & VIDEO_SOUND_MONO) {
358 zol->stereo = 0;
359 zol_setfreq(zol, zol->curfreq);
360 }
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -0300361#endif
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300362 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300365static int vidioc_g_audio(struct file *file, void *priv,
366 struct v4l2_audio *a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300368 if (a->index > 1)
369 return -EINVAL;
370
371 strcpy(a->name, "Radio");
372 a->capability = V4L2_AUDCAP_STEREO;
373 return 0;
374}
375
376static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
377{
378 *i = 0;
379 return 0;
380}
381
382static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
383{
384 if (i != 0)
385 return -EINVAL;
386 return 0;
387}
388
389static int vidioc_s_audio(struct file *file, void *priv,
390 struct v4l2_audio *a)
391{
392 if (a->index != 0)
393 return -EINVAL;
394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397static struct zol_device zoltrix_unit;
398
Arjan van de Venfa027c22007-02-12 00:55:33 -0800399static const struct file_operations zoltrix_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 .owner = THIS_MODULE,
402 .open = video_exclusive_open,
403 .release = video_exclusive_release,
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300404 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300405#ifdef CONFIG_COMPAT
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200406 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300407#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 .llseek = no_llseek,
409};
410
411static struct video_device zoltrix_radio =
412{
413 .owner = THIS_MODULE,
414 .name = "Zoltrix Radio Plus",
415 .type = VID_TYPE_TUNER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 .fops = &zoltrix_fops,
Douglas Landgrafa1314b12007-04-20 18:23:38 -0300417 .vidioc_querycap = vidioc_querycap,
418 .vidioc_g_tuner = vidioc_g_tuner,
419 .vidioc_s_tuner = vidioc_s_tuner,
420 .vidioc_g_audio = vidioc_g_audio,
421 .vidioc_s_audio = vidioc_s_audio,
422 .vidioc_g_input = vidioc_g_input,
423 .vidioc_s_input = vidioc_s_input,
424 .vidioc_g_frequency = vidioc_g_frequency,
425 .vidioc_s_frequency = vidioc_s_frequency,
426 .vidioc_queryctrl = vidioc_queryctrl,
427 .vidioc_g_ctrl = vidioc_g_ctrl,
428 .vidioc_s_ctrl = vidioc_s_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429};
430
431static int __init zoltrix_init(void)
432{
433 if (io == -1) {
434 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
435 return -EINVAL;
436 }
437 if ((io != 0x20c) && (io != 0x30c)) {
438 printk(KERN_ERR "zoltrix: invalid port, try 0x20c or 0x30c\n");
439 return -ENXIO;
440 }
441
442 zoltrix_radio.priv = &zoltrix_unit;
443 if (!request_region(io, 2, "zoltrix")) {
444 printk(KERN_ERR "zoltrix: port 0x%x already in use\n", io);
445 return -EBUSY;
446 }
447
448 if (video_register_device(&zoltrix_radio, VFL_TYPE_RADIO, radio_nr) == -1)
449 {
450 release_region(io, 2);
451 return -EINVAL;
452 }
453 printk(KERN_INFO "Zoltrix Radio Plus card driver.\n");
454
Ingo Molnar3593cab2006-02-07 06:49:14 -0200455 mutex_init(&zoltrix_unit.lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /* mute card - prevents noisy bootups */
458
459 /* this ensures that the volume is all the way down */
460
461 outb(0, io);
462 outb(0, io);
463 msleep(20);
464 inb(io + 3);
465
466 zoltrix_unit.curvol = 0;
467 zoltrix_unit.stereo = 1;
468
469 return 0;
470}
471
472MODULE_AUTHOR("C.van Schaik");
473MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
474MODULE_LICENSE("GPL");
475
476module_param(io, int, 0);
477MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
478module_param(radio_nr, int, 0);
479
480static void __exit zoltrix_cleanup_module(void)
481{
482 video_unregister_device(&zoltrix_radio);
483 release_region(io, 2);
484}
485
486module_init(zoltrix_init);
487module_exit(zoltrix_cleanup_module);
488