blob: 8aceea0839808397e6a366f002789b8b8a7647c4 [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
27 */
28
29#include <linux/module.h> /* Modules */
30#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070031#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/delay.h> /* udelay, msleep */
33#include <asm/io.h> /* outb, outb_p */
34#include <asm/uaccess.h> /* copy to/from user */
35#include <linux/videodev.h> /* kernel radio structs */
36#include <linux/config.h> /* CONFIG_RADIO_ZOLTRIX_PORT */
37
38#ifndef CONFIG_RADIO_ZOLTRIX_PORT
39#define CONFIG_RADIO_ZOLTRIX_PORT -1
40#endif
41
42static int io = CONFIG_RADIO_ZOLTRIX_PORT;
43static int radio_nr = -1;
44
45struct zol_device {
46 int port;
47 int curvol;
48 unsigned long curfreq;
49 int muted;
50 unsigned int stereo;
Ingo Molnar3593cab2006-02-07 06:49:14 -020051 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
54static int zol_setvol(struct zol_device *dev, int vol)
55{
56 dev->curvol = vol;
57 if (dev->muted)
58 return 0;
59
Ingo Molnar3593cab2006-02-07 06:49:14 -020060 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (vol == 0) {
62 outb(0, io);
63 outb(0, io);
64 inb(io + 3); /* Zoltrix needs to be read to confirm */
Ingo Molnar3593cab2006-02-07 06:49:14 -020065 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return 0;
67 }
68
69 outb(dev->curvol-1, io);
70 msleep(10);
71 inb(io + 2);
Ingo Molnar3593cab2006-02-07 06:49:14 -020072 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return 0;
74}
75
76static void zol_mute(struct zol_device *dev)
77{
78 dev->muted = 1;
Ingo Molnar3593cab2006-02-07 06:49:14 -020079 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 outb(0, io);
81 outb(0, io);
82 inb(io + 3); /* Zoltrix needs to be read to confirm */
Ingo Molnar3593cab2006-02-07 06:49:14 -020083 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86static void zol_unmute(struct zol_device *dev)
87{
88 dev->muted = 0;
89 zol_setvol(dev, dev->curvol);
90}
91
92static int zol_setfreq(struct zol_device *dev, unsigned long freq)
93{
94 /* tunes the radio to the desired frequency */
95 unsigned long long bitmask, f, m;
96 unsigned int stereo = dev->stereo;
97 int i;
98
99 if (freq == 0)
100 return 1;
101 m = (freq / 160 - 8800) * 2;
102 f = (unsigned long long) m + 0x4d1c;
103
104 bitmask = 0xc480402c10080000ull;
105 i = 45;
106
Ingo Molnar3593cab2006-02-07 06:49:14 -0200107 mutex_lock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 outb(0, io);
110 outb(0, io);
111 inb(io + 3); /* Zoltrix needs to be read to confirm */
112
113 outb(0x40, io);
114 outb(0xc0, io);
115
116 bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ ( stereo << 31));
117 while (i--) {
118 if ((bitmask & 0x8000000000000000ull) != 0) {
119 outb(0x80, io);
120 udelay(50);
121 outb(0x00, io);
122 udelay(50);
123 outb(0x80, io);
124 udelay(50);
125 } else {
126 outb(0xc0, io);
127 udelay(50);
128 outb(0x40, io);
129 udelay(50);
130 outb(0xc0, io);
131 udelay(50);
132 }
133 bitmask *= 2;
134 }
135 /* termination sequence */
136 outb(0x80, io);
137 outb(0xc0, io);
138 outb(0x40, io);
139 udelay(1000);
140 inb(io+2);
141
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300142 udelay(1000);
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (dev->muted)
145 {
146 outb(0, io);
147 outb(0, io);
148 inb(io + 3);
149 udelay(1000);
150 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300151
Ingo Molnar3593cab2006-02-07 06:49:14 -0200152 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if(!dev->muted)
155 {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300156 zol_setvol(dev, dev->curvol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158 return 0;
159}
160
161/* Get signal strength */
162
163static int zol_getsigstr(struct zol_device *dev)
164{
165 int a, b;
166
Ingo Molnar3593cab2006-02-07 06:49:14 -0200167 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 outb(0x00, io); /* This stuff I found to do nothing */
169 outb(dev->curvol, io);
170 msleep(20);
171
172 a = inb(io);
173 msleep(10);
174 b = inb(io);
175
Ingo Molnar3593cab2006-02-07 06:49:14 -0200176 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (a != b)
179 return (0);
180
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300181 if ((a == 0xcf) || (a == 0xdf) /* I found this out by playing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 || (a == 0xef)) /* with a binary scanner on the card io */
183 return (1);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300184 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187static int zol_is_stereo (struct zol_device *dev)
188{
189 int x1, x2;
190
Ingo Molnar3593cab2006-02-07 06:49:14 -0200191 mutex_lock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 outb(0x00, io);
194 outb(dev->curvol, io);
195 msleep(20);
196
197 x1 = inb(io);
198 msleep(10);
199 x2 = inb(io);
200
Ingo Molnar3593cab2006-02-07 06:49:14 -0200201 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if ((x1 == x2) && (x1 == 0xcf))
204 return 1;
205 return 0;
206}
207
208static int zol_do_ioctl(struct inode *inode, struct file *file,
209 unsigned int cmd, void *arg)
210{
211 struct video_device *dev = video_devdata(file);
212 struct zol_device *zol = dev->priv;
213
214 switch (cmd) {
215 case VIDIOCGCAP:
216 {
217 struct video_capability *v = arg;
218
219 memset(v,0,sizeof(*v));
220 v->type = VID_TYPE_TUNER;
221 v->channels = 1 + zol->stereo;
222 v->audios = 1;
223 strcpy(v->name, "Zoltrix Radio");
224 return 0;
225 }
226 case VIDIOCGTUNER:
227 {
228 struct video_tuner *v = arg;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300229 if (v->tuner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -EINVAL;
231 strcpy(v->name, "FM");
232 v->rangelow = (int) (88.0 * 16000);
233 v->rangehigh = (int) (108.0 * 16000);
234 v->flags = zol_is_stereo(zol)
235 ? VIDEO_TUNER_STEREO_ON : 0;
236 v->flags |= VIDEO_TUNER_LOW;
237 v->mode = VIDEO_MODE_AUTO;
238 v->signal = 0xFFFF * zol_getsigstr(zol);
239 return 0;
240 }
241 case VIDIOCSTUNER:
242 {
243 struct video_tuner *v = arg;
244 if (v->tuner != 0)
245 return -EINVAL;
246 /* Only 1 tuner so no setting needed ! */
247 return 0;
248 }
249 case VIDIOCGFREQ:
250 {
251 unsigned long *freq = arg;
252 *freq = zol->curfreq;
253 return 0;
254 }
255 case VIDIOCSFREQ:
256 {
257 unsigned long *freq = arg;
258 zol->curfreq = *freq;
259 zol_setfreq(zol, zol->curfreq);
260 return 0;
261 }
262 case VIDIOCGAUDIO:
263 {
264 struct video_audio *v = arg;
265 memset(v, 0, sizeof(*v));
266 v->flags |= VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME;
267 v->mode |= zol_is_stereo(zol)
268 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
269 v->volume = zol->curvol * 4096;
270 v->step = 4096;
271 strcpy(v->name, "Zoltrix Radio");
272 return 0;
273 }
274 case VIDIOCSAUDIO:
275 {
276 struct video_audio *v = arg;
277 if (v->audio)
278 return -EINVAL;
279
280 if (v->flags & VIDEO_AUDIO_MUTE)
281 zol_mute(zol);
282 else {
283 zol_unmute(zol);
284 zol_setvol(zol, v->volume / 4096);
285 }
286
287 if (v->mode & VIDEO_SOUND_STEREO) {
288 zol->stereo = 1;
289 zol_setfreq(zol, zol->curfreq);
290 }
291 if (v->mode & VIDEO_SOUND_MONO) {
292 zol->stereo = 0;
293 zol_setfreq(zol, zol->curfreq);
294 }
295 return 0;
296 }
297 default:
298 return -ENOIOCTLCMD;
299 }
300}
301
302static int zol_ioctl(struct inode *inode, struct file *file,
303 unsigned int cmd, unsigned long arg)
304{
305 return video_usercopy(inode, file, cmd, arg, zol_do_ioctl);
306}
307
308static struct zol_device zoltrix_unit;
309
310static struct file_operations zoltrix_fops =
311{
312 .owner = THIS_MODULE,
313 .open = video_exclusive_open,
314 .release = video_exclusive_release,
315 .ioctl = zol_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200316 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 .llseek = no_llseek,
318};
319
320static struct video_device zoltrix_radio =
321{
322 .owner = THIS_MODULE,
323 .name = "Zoltrix Radio Plus",
324 .type = VID_TYPE_TUNER,
325 .hardware = VID_HARDWARE_ZOLTRIX,
326 .fops = &zoltrix_fops,
327};
328
329static int __init zoltrix_init(void)
330{
331 if (io == -1) {
332 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
333 return -EINVAL;
334 }
335 if ((io != 0x20c) && (io != 0x30c)) {
336 printk(KERN_ERR "zoltrix: invalid port, try 0x20c or 0x30c\n");
337 return -ENXIO;
338 }
339
340 zoltrix_radio.priv = &zoltrix_unit;
341 if (!request_region(io, 2, "zoltrix")) {
342 printk(KERN_ERR "zoltrix: port 0x%x already in use\n", io);
343 return -EBUSY;
344 }
345
346 if (video_register_device(&zoltrix_radio, VFL_TYPE_RADIO, radio_nr) == -1)
347 {
348 release_region(io, 2);
349 return -EINVAL;
350 }
351 printk(KERN_INFO "Zoltrix Radio Plus card driver.\n");
352
Ingo Molnar3593cab2006-02-07 06:49:14 -0200353 mutex_init(&zoltrix_unit.lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /* mute card - prevents noisy bootups */
356
357 /* this ensures that the volume is all the way down */
358
359 outb(0, io);
360 outb(0, io);
361 msleep(20);
362 inb(io + 3);
363
364 zoltrix_unit.curvol = 0;
365 zoltrix_unit.stereo = 1;
366
367 return 0;
368}
369
370MODULE_AUTHOR("C.van Schaik");
371MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
372MODULE_LICENSE("GPL");
373
374module_param(io, int, 0);
375MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
376module_param(radio_nr, int, 0);
377
378static void __exit zoltrix_cleanup_module(void)
379{
380 video_unregister_device(&zoltrix_radio);
381 release_region(io, 2);
382}
383
384module_init(zoltrix_init);
385module_exit(zoltrix_cleanup_module);
386