Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / media / radio / radio-zoltrix.c
blob7bf1a4264891deca514dd6a5465497b46e02919d
1 /* zoltrix radio plus driver for Linux radio support
2 * (c) 1998 C. van Schaik <carl@leg.uct.ac.za>
4 * BUGS
5 * Due to the inconsistency in reading from the signal flags
6 * it is difficult to get an accurate tuned signal.
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.
12 * Some code derived from code by Romolo Manfredini
13 * romolo@bicnet.it
15 * 1999-05-06 - (C. van Schaik)
16 * - Make signal strength and stereo scans
17 * kinder to cpu while in delay
18 * 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
29 #include <linux/module.h> /* Modules */
30 #include <linux/init.h> /* Initdata */
31 #include <linux/ioport.h> /* request_region */
32 #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 */
38 #ifndef CONFIG_RADIO_ZOLTRIX_PORT
39 #define CONFIG_RADIO_ZOLTRIX_PORT -1
40 #endif
42 static int io = CONFIG_RADIO_ZOLTRIX_PORT;
43 static int radio_nr = -1;
45 struct zol_device {
46 int port;
47 int curvol;
48 unsigned long curfreq;
49 int muted;
50 unsigned int stereo;
51 struct mutex lock;
54 static int zol_setvol(struct zol_device *dev, int vol)
56 dev->curvol = vol;
57 if (dev->muted)
58 return 0;
60 mutex_lock(&dev->lock);
61 if (vol == 0) {
62 outb(0, io);
63 outb(0, io);
64 inb(io + 3); /* Zoltrix needs to be read to confirm */
65 mutex_unlock(&dev->lock);
66 return 0;
69 outb(dev->curvol-1, io);
70 msleep(10);
71 inb(io + 2);
72 mutex_unlock(&dev->lock);
73 return 0;
76 static void zol_mute(struct zol_device *dev)
78 dev->muted = 1;
79 mutex_lock(&dev->lock);
80 outb(0, io);
81 outb(0, io);
82 inb(io + 3); /* Zoltrix needs to be read to confirm */
83 mutex_unlock(&dev->lock);
86 static void zol_unmute(struct zol_device *dev)
88 dev->muted = 0;
89 zol_setvol(dev, dev->curvol);
92 static int zol_setfreq(struct zol_device *dev, unsigned long freq)
94 /* tunes the radio to the desired frequency */
95 unsigned long long bitmask, f, m;
96 unsigned int stereo = dev->stereo;
97 int i;
99 if (freq == 0)
100 return 1;
101 m = (freq / 160 - 8800) * 2;
102 f = (unsigned long long) m + 0x4d1c;
104 bitmask = 0xc480402c10080000ull;
105 i = 45;
107 mutex_lock(&dev->lock);
109 outb(0, io);
110 outb(0, io);
111 inb(io + 3); /* Zoltrix needs to be read to confirm */
113 outb(0x40, io);
114 outb(0xc0, io);
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);
133 bitmask *= 2;
135 /* termination sequence */
136 outb(0x80, io);
137 outb(0xc0, io);
138 outb(0x40, io);
139 udelay(1000);
140 inb(io+2);
142 udelay(1000);
144 if (dev->muted)
146 outb(0, io);
147 outb(0, io);
148 inb(io + 3);
149 udelay(1000);
152 mutex_unlock(&dev->lock);
154 if(!dev->muted)
156 zol_setvol(dev, dev->curvol);
158 return 0;
161 /* Get signal strength */
163 static int zol_getsigstr(struct zol_device *dev)
165 int a, b;
167 mutex_lock(&dev->lock);
168 outb(0x00, io); /* This stuff I found to do nothing */
169 outb(dev->curvol, io);
170 msleep(20);
172 a = inb(io);
173 msleep(10);
174 b = inb(io);
176 mutex_unlock(&dev->lock);
178 if (a != b)
179 return (0);
181 if ((a == 0xcf) || (a == 0xdf) /* I found this out by playing */
182 || (a == 0xef)) /* with a binary scanner on the card io */
183 return (1);
184 return (0);
187 static int zol_is_stereo (struct zol_device *dev)
189 int x1, x2;
191 mutex_lock(&dev->lock);
193 outb(0x00, io);
194 outb(dev->curvol, io);
195 msleep(20);
197 x1 = inb(io);
198 msleep(10);
199 x2 = inb(io);
201 mutex_unlock(&dev->lock);
203 if ((x1 == x2) && (x1 == 0xcf))
204 return 1;
205 return 0;
208 static int zol_do_ioctl(struct inode *inode, struct file *file,
209 unsigned int cmd, void *arg)
211 struct video_device *dev = video_devdata(file);
212 struct zol_device *zol = dev->priv;
214 switch (cmd) {
215 case VIDIOCGCAP:
217 struct video_capability *v = arg;
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;
226 case VIDIOCGTUNER:
228 struct video_tuner *v = arg;
229 if (v->tuner)
230 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;
241 case VIDIOCSTUNER:
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;
249 case VIDIOCGFREQ:
251 unsigned long *freq = arg;
252 *freq = zol->curfreq;
253 return 0;
255 case VIDIOCSFREQ:
257 unsigned long *freq = arg;
258 zol->curfreq = *freq;
259 zol_setfreq(zol, zol->curfreq);
260 return 0;
262 case VIDIOCGAUDIO:
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;
274 case VIDIOCSAUDIO:
276 struct video_audio *v = arg;
277 if (v->audio)
278 return -EINVAL;
280 if (v->flags & VIDEO_AUDIO_MUTE)
281 zol_mute(zol);
282 else {
283 zol_unmute(zol);
284 zol_setvol(zol, v->volume / 4096);
287 if (v->mode & VIDEO_SOUND_STEREO) {
288 zol->stereo = 1;
289 zol_setfreq(zol, zol->curfreq);
291 if (v->mode & VIDEO_SOUND_MONO) {
292 zol->stereo = 0;
293 zol_setfreq(zol, zol->curfreq);
295 return 0;
297 default:
298 return -ENOIOCTLCMD;
302 static int zol_ioctl(struct inode *inode, struct file *file,
303 unsigned int cmd, unsigned long arg)
305 return video_usercopy(inode, file, cmd, arg, zol_do_ioctl);
308 static struct zol_device zoltrix_unit;
310 static struct file_operations zoltrix_fops =
312 .owner = THIS_MODULE,
313 .open = video_exclusive_open,
314 .release = video_exclusive_release,
315 .ioctl = zol_ioctl,
316 .compat_ioctl = v4l_compat_ioctl32,
317 .llseek = no_llseek,
320 static struct video_device zoltrix_radio =
322 .owner = THIS_MODULE,
323 .name = "Zoltrix Radio Plus",
324 .type = VID_TYPE_TUNER,
325 .hardware = VID_HARDWARE_ZOLTRIX,
326 .fops = &zoltrix_fops,
329 static int __init zoltrix_init(void)
331 if (io == -1) {
332 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
333 return -EINVAL;
335 if ((io != 0x20c) && (io != 0x30c)) {
336 printk(KERN_ERR "zoltrix: invalid port, try 0x20c or 0x30c\n");
337 return -ENXIO;
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;
346 if (video_register_device(&zoltrix_radio, VFL_TYPE_RADIO, radio_nr) == -1)
348 release_region(io, 2);
349 return -EINVAL;
351 printk(KERN_INFO "Zoltrix Radio Plus card driver.\n");
353 mutex_init(&zoltrix_unit.lock);
355 /* mute card - prevents noisy bootups */
357 /* this ensures that the volume is all the way down */
359 outb(0, io);
360 outb(0, io);
361 msleep(20);
362 inb(io + 3);
364 zoltrix_unit.curvol = 0;
365 zoltrix_unit.stereo = 1;
367 return 0;
370 MODULE_AUTHOR("C.van Schaik");
371 MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
372 MODULE_LICENSE("GPL");
374 module_param(io, int, 0);
375 MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
376 module_param(radio_nr, int, 0);
378 static void __exit zoltrix_cleanup_module(void)
380 video_unregister_device(&zoltrix_radio);
381 release_region(io, 2);
384 module_init(zoltrix_init);
385 module_exit(zoltrix_cleanup_module);