blob: 4444dce864a932ddeaad7ec8790ca42354d5c9bd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* SF16FMR2 radio driver for Linux radio support
2 * heavily based on fmi driver...
3 * (c) 2000-2002 Ziglio Frediano, freddy77@angelfire.com
4 *
5 * Notes on the hardware
6 *
7 * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
8 * No volume control - only mute/unmute - you have to use line volume
9 *
10 * For read stereo/mono you must wait 0.1 sec after set frequency and
11 * card unmuted so I set frequency on unmute
12 * Signal handling seem to work only on autoscanning (not implemented)
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -030013 *
14 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
17#include <linux/module.h> /* Modules */
18#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070019#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h> /* udelay */
21#include <asm/io.h> /* outb, outb_p */
22#include <asm/uaccess.h> /* copy to/from user */
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -030023#include <linux/videodev2.h> /* kernel radio structs */
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030024#include <media/v4l2-common.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020025#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Mauro Carvalho Chehab1ddf5bc2006-08-08 09:10:06 -030027static struct mutex lock;
28
29#include <linux/version.h> /* for KERNEL_VERSION MACRO */
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -030030#define RADIO_VERSION KERNEL_VERSION(0,0,2)
31
32static struct v4l2_queryctrl radio_qctrl[] = {
33 {
34 .id = V4L2_CID_AUDIO_MUTE,
35 .name = "Mute",
36 .minimum = 0,
37 .maximum = 1,
38 .default_value = 1,
39 .type = V4L2_CTRL_TYPE_BOOLEAN,
40 },{
41 .id = V4L2_CID_AUDIO_VOLUME,
42 .name = "Volume",
43 .minimum = 0,
44 .maximum = 65535,
45 .step = 1<<12,
46 .default_value = 0xff,
47 .type = V4L2_CTRL_TYPE_INTEGER,
48 }
49};
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#undef DEBUG
52//#define DEBUG 1
53
54#ifdef DEBUG
55# define debug_print(s) printk s
56#else
57# define debug_print(s)
58#endif
59
60/* this should be static vars for module size */
61struct fmr2_device
62{
63 int port;
64 int curvol; /* 0-65535, if not volume 0 or 65535 */
65 int mute;
66 int stereo; /* card is producing stereo audio */
67 unsigned long curfreq; /* freq in kHz */
68 int card_type;
69 __u32 flags;
70};
71
72static int io = 0x384;
73static int radio_nr = -1;
74
75/* hw precision is 12.5 kHz
Adrian Bunka58a4142006-01-10 00:08:17 +010076 * It is only useful to give freq in intervall of 200 (=0.0125Mhz),
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * other bits will be truncated
78 */
79#define RSF16_ENCODE(x) ((x)/200+856)
80#define RSF16_MINFREQ 87*16000
81#define RSF16_MAXFREQ 108*16000
82
83static inline void wait(int n,int port)
84{
85 for (;n;--n) inb(port);
86}
87
88static void outbits(int bits, unsigned int data, int nWait, int port)
89{
90 int bit;
91 for(;--bits>=0;) {
92 bit = (data>>bits) & 1;
93 outb(bit,port);
94 wait(nWait,port);
95 outb(bit|2,port);
96 wait(nWait,port);
97 outb(bit,port);
98 wait(nWait,port);
99 }
100}
101
102static inline void fmr2_mute(int port)
103{
104 outb(0x00, port);
105 wait(4,port);
106}
107
108static inline void fmr2_unmute(int port)
109{
110 outb(0x04, port);
111 wait(4,port);
112}
113
114static inline int fmr2_stereo_mode(int port)
115{
116 int n = inb(port);
117 outb(6,port);
118 inb(port);
119 n = ((n>>3)&1)^1;
120 debug_print((KERN_DEBUG "stereo: %d\n", n));
121 return n;
122}
123
124static int fmr2_product_info(struct fmr2_device *dev)
125{
126 int n = inb(dev->port);
127 n &= 0xC1;
128 if (n == 0)
129 {
130 /* this should support volume set */
131 dev->card_type = 12;
132 return 0;
133 }
134 /* not volume (mine is 11) */
135 dev->card_type = (n==128)?11:0;
136 return n;
137}
138
139static inline int fmr2_getsigstr(struct fmr2_device *dev)
140{
141 /* !!! work only if scanning freq */
142 int port = dev->port, res = 0xffff;
143 outb(5,port);
144 wait(4,port);
145 if (!(inb(port)&1)) res = 0;
146 debug_print((KERN_DEBUG "signal: %d\n", res));
147 return res;
148}
149
150/* set frequency and unmute card */
151static int fmr2_setfreq(struct fmr2_device *dev)
152{
153 int port = dev->port;
154 unsigned long freq = dev->curfreq;
155
156 fmr2_mute(port);
157
158 /* 0x42 for mono output
159 * 0x102 forward scanning
160 * 0x182 scansione avanti
161 */
162 outbits(9,0x2,3,port);
163 outbits(16,RSF16_ENCODE(freq),2,port);
164
165 fmr2_unmute(port);
166
167 /* wait 0.11 sec */
168 msleep(110);
169
170 /* NOTE if mute this stop radio
171 you must set freq on unmute */
172 dev->stereo = fmr2_stereo_mode(port);
173 return 0;
174}
175
176/* !!! not tested, in my card this does't work !!! */
177static int fmr2_setvolume(struct fmr2_device *dev)
178{
179 int i,a,n, port = dev->port;
180
181 if (dev->card_type != 11) return 1;
182
183 switch( (dev->curvol+(1<<11)) >> 12 )
184 {
185 case 0: case 1: n = 0x21; break;
186 case 2: n = 0x84; break;
187 case 3: n = 0x90; break;
188 case 4: n = 0x104; break;
189 case 5: n = 0x110; break;
190 case 6: n = 0x204; break;
191 case 7: n = 0x210; break;
192 case 8: n = 0x402; break;
193 case 9: n = 0x404; break;
194 default:
195 case 10: n = 0x408; break;
196 case 11: n = 0x410; break;
197 case 12: n = 0x801; break;
198 case 13: n = 0x802; break;
199 case 14: n = 0x804; break;
200 case 15: n = 0x808; break;
201 case 16: n = 0x810; break;
202 }
203 for(i=12;--i>=0;)
204 {
205 a = ((n >> i) & 1) << 6; /* if (a=0) a= 0; else a= 0x40; */
206 outb(a|4, port);
207 wait(4,port);
208 outb(a|0x24, port);
209 wait(4,port);
210 outb(a|4, port);
211 wait(4,port);
212 }
213 for(i=6;--i>=0;)
214 {
215 a = ((0x18 >> i) & 1) << 6;
216 outb(a|4, port);
217 wait(4,port);
218 outb(a|0x24, port);
219 wait(4,port);
220 outb(a|4, port);
221 wait(4,port);
222 }
223 wait(4,port);
224 outb(0x14, port);
225
226 return 0;
227}
228
229static int fmr2_do_ioctl(struct inode *inode, struct file *file,
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300230 unsigned int cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 struct video_device *dev = video_devdata(file);
233 struct fmr2_device *fmr2 = dev->priv;
234 debug_print((KERN_DEBUG "freq %ld flags %d vol %d mute %d "
235 "stereo %d type %d\n",
236 fmr2->curfreq, fmr2->flags, fmr2->curvol, fmr2->mute,
237 fmr2->stereo, fmr2->card_type));
238
239 switch(cmd)
240 {
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300241 case VIDIOC_QUERYCAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 {
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300243 struct v4l2_capability *v = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 memset(v,0,sizeof(*v));
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300245 strlcpy(v->driver, "radio-sf16fmr2", sizeof (v->driver));
246 strlcpy(v->card, "SF16-FMR2 radio", sizeof (v->card));
247 sprintf(v->bus_info,"ISA");
248 v->version = RADIO_VERSION;
249 v->capabilities = V4L2_CAP_TUNER;
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return 0;
252 }
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300253 case VIDIOC_G_TUNER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 {
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300255 struct v4l2_tuner *v = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 int mult;
257
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300258 if (v->index > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return -EINVAL;
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300260
261 memset(v,0,sizeof(*v));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 strcpy(v->name, "FM");
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300263 v->type = V4L2_TUNER_RADIO;
264
265 mult = (fmr2->flags & V4L2_TUNER_CAP_LOW) ? 1 : 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 v->rangelow = RSF16_MINFREQ/mult;
267 v->rangehigh = RSF16_MAXFREQ/mult;
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300268 v->rxsubchans =V4L2_TUNER_SUB_MONO | V4L2_TUNER_MODE_STEREO;
269 v->capability=fmr2->flags&V4L2_TUNER_CAP_LOW;
270
271 v->audmode = fmr2->stereo ? V4L2_TUNER_MODE_STEREO:
272 V4L2_TUNER_MODE_MONO;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200273 mutex_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 v->signal = fmr2_getsigstr(fmr2);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200275 mutex_unlock(&lock);
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return 0;
278 }
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300279 case VIDIOC_S_TUNER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 {
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300281 struct v4l2_tuner *v = arg;
282
283 if (v->index > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return -EINVAL;
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return 0;
287 }
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300288 case VIDIOC_S_FREQUENCY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 {
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300290 struct v4l2_frequency *f = arg;
291
292 if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
293 f->frequency *= 1000;
294 if (f->frequency < RSF16_MINFREQ ||
295 f->frequency > RSF16_MAXFREQ )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return -EINVAL;
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300297 /*rounding in steps of 200 to match th freq
298 that will be used */
299 fmr2->curfreq = (f->frequency/200)*200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /* set card freq (if not muted) */
302 if (fmr2->curvol && !fmr2->mute)
303 {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200304 mutex_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 fmr2_setfreq(fmr2);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200306 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return 0;
310 }
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300311 case VIDIOC_G_FREQUENCY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 {
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300313 struct v4l2_frequency *f = arg;
314
315 f->type = V4L2_TUNER_RADIO;
316 f->frequency = fmr2->curfreq;
317 if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
318 f->frequency /= 1000;
319
320 return 0;
321 }
322 case VIDIOC_QUERYCTRL:
323 {
324 struct v4l2_queryctrl *qc = arg;
325 int i;
326
327 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
328 if ((fmr2->card_type != 11)
329 && V4L2_CID_AUDIO_VOLUME)
330 radio_qctrl[i].step=65535;
331 if (qc->id && qc->id == radio_qctrl[i].id) {
332 memcpy(qc, &(radio_qctrl[i]),
333 sizeof(*qc));
334 return (0);
335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300337 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300339 case VIDIOC_G_CTRL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 {
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300341 struct v4l2_control *ctrl= arg;
342
343 switch (ctrl->id) {
344 case V4L2_CID_AUDIO_MUTE:
345 ctrl->value=fmr2->mute;
346 return (0);
347 case V4L2_CID_AUDIO_VOLUME:
348 ctrl->value=fmr2->curvol;
349 return (0);
350 }
351 return -EINVAL;
352 }
353 case VIDIOC_S_CTRL:
354 {
355 struct v4l2_control *ctrl= arg;
356
357 switch (ctrl->id) {
358 case V4L2_CID_AUDIO_MUTE:
359 fmr2->mute=ctrl->value;
360 if (fmr2->card_type != 11) {
361 if (!fmr2->mute) {
362 fmr2->curvol = 65535;
363 } else {
364 fmr2->curvol = 0;
365 }
366 }
367 break;
368 case V4L2_CID_AUDIO_VOLUME:
369 fmr2->curvol = ctrl->value;
370 if (fmr2->card_type != 11) {
371 if (fmr2->curvol) {
372 fmr2->curvol = 65535;
373 fmr2->mute = 0;
374 } else {
375 fmr2->curvol = 0;
376 fmr2->mute = 1;
377 }
378 }
379 break;
380 default:
381 return -EINVAL;
382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#ifdef DEBUG
384 if (fmr2->curvol && !fmr2->mute)
385 printk(KERN_DEBUG "unmute\n");
386 else
387 printk(KERN_DEBUG "mute\n");
388#endif
Ingo Molnar3593cab2006-02-07 06:49:14 -0200389 mutex_lock(&lock);
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300390 if (fmr2->curvol && !fmr2->mute) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 fmr2_setvolume(fmr2);
392 fmr2_setfreq(fmr2);
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300393 } else
394 fmr2_mute(fmr2->port);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200395 mutex_unlock(&lock);
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300396 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 default:
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300399 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
Mauro Carvalho Chehab3ca57162006-08-08 09:10:06 -0300400 fmr2_do_ioctl);
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403}
404
405static int fmr2_ioctl(struct inode *inode, struct file *file,
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300406 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 {
408 return video_usercopy(inode, file, cmd, arg, fmr2_do_ioctl);
409}
410
411static struct fmr2_device fmr2_unit;
412
413static struct file_operations fmr2_fops = {
414 .owner = THIS_MODULE,
415 .open = video_exclusive_open,
416 .release = video_exclusive_release,
417 .ioctl = fmr2_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200418 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 .llseek = no_llseek,
420};
421
422static struct video_device fmr2_radio=
423{
424 .owner = THIS_MODULE,
425 .name = "SF16FMR2 radio",
426 . type = VID_TYPE_TUNER,
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300427 .hardware = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 .fops = &fmr2_fops,
429};
430
431static int __init fmr2_init(void)
432{
433 fmr2_unit.port = io;
434 fmr2_unit.curvol = 0;
435 fmr2_unit.mute = 0;
436 fmr2_unit.curfreq = 0;
437 fmr2_unit.stereo = 1;
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300438 fmr2_unit.flags = V4L2_TUNER_CAP_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 fmr2_unit.card_type = 0;
440 fmr2_radio.priv = &fmr2_unit;
441
Ingo Molnar3593cab2006-02-07 06:49:14 -0200442 mutex_init(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (request_region(io, 2, "sf16fmr2"))
445 {
446 printk(KERN_ERR "fmr2: port 0x%x already in use\n", io);
447 return -EBUSY;
448 }
449
450 if(video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr)==-1)
451 {
452 release_region(io, 2);
453 return -EINVAL;
454 }
455
456 printk(KERN_INFO "SF16FMR2 radio card driver at 0x%x.\n", io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /* mute card - prevents noisy bootups */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200458 mutex_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 fmr2_mute(io);
460 fmr2_product_info(&fmr2_unit);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200461 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 debug_print((KERN_DEBUG "card_type %d\n", fmr2_unit.card_type));
463 return 0;
464}
465
466MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
467MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
468MODULE_LICENSE("GPL");
469
470module_param(io, int, 0);
471MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
472module_param(radio_nr, int, 0);
473
474static void __exit fmr2_cleanup_module(void)
475{
476 video_unregister_device(&fmr2_radio);
477 release_region(io,2);
478}
479
480module_init(fmr2_init);
481module_exit(fmr2_cleanup_module);
482
483#ifndef MODULE
484
485static int __init fmr2_setup_io(char *str)
486{
487 get_option(&str, &io);
488 return 1;
489}
490
491__setup("sf16fmr2=", fmr2_setup_io);
492
493#endif