1 /* SF16FMR2 radio driver for Linux radio support
2 * heavily based on fmi driver...
3 * (c) 2000-2002 Ziglio Frediano, freddy77@angelfire.com
5 * Notes on the hardware
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
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)
14 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
17 #include <linux/module.h> /* Modules */
18 #include <linux/init.h> /* Initdata */
19 #include <linux/ioport.h> /* request_region */
20 #include <linux/delay.h> /* udelay */
21 #include <linux/videodev2.h> /* kernel radio structs */
22 #include <linux/mutex.h>
23 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
24 #include <linux/io.h> /* outb, outb_p */
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-ioctl.h>
28 MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
29 MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
30 MODULE_LICENSE("GPL");
32 static int io = 0x384;
33 static int radio_nr = -1;
35 module_param(io, int, 0);
36 MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
37 module_param(radio_nr, int, 0);
39 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
41 #define AUD_VOL_INDEX 1
47 # define debug_print(s) printk s
49 # define debug_print(s)
52 /* this should be static vars for module size */
55 struct v4l2_device v4l2_dev;
56 struct video_device vdev;
59 int curvol; /* 0-15 */
61 int stereo; /* card is producing stereo audio */
62 unsigned long curfreq; /* freq in kHz */
66 static struct fmr2 fmr2_card;
68 /* hw precision is 12.5 kHz
69 * It is only useful to give freq in interval of 200 (=0.0125Mhz),
70 * other bits will be truncated
72 #define RSF16_ENCODE(x) ((x) / 200 + 856)
73 #define RSF16_MINFREQ (87 * 16000)
74 #define RSF16_MAXFREQ (108 * 16000)
76 static inline void wait(int n, int io)
82 static void outbits(int bits, unsigned int data, int nWait, int io)
86 for (; --bits >= 0;) {
87 bit = (data >> bits) & 1;
97 static inline void fmr2_mute(int io)
103 static inline void fmr2_unmute(int io)
109 static inline int fmr2_stereo_mode(int io)
115 n = ((n >> 3) & 1) ^ 1;
116 debug_print((KERN_DEBUG "stereo: %d\n", n));
120 static int fmr2_product_info(struct fmr2 *dev)
122 int n = inb(dev->io);
126 /* this should support volume set */
130 /* not volume (mine is 11) */
131 dev->card_type = (n == 128) ? 11 : 0;
135 static inline int fmr2_getsigstr(struct fmr2 *dev)
137 /* !!! works only if scanning freq */
142 if (!(inb(dev->io) & 1))
144 debug_print((KERN_DEBUG "signal: %d\n", res));
148 /* set frequency and unmute card */
149 static int fmr2_setfreq(struct fmr2 *dev)
151 unsigned long freq = dev->curfreq;
155 /* 0x42 for mono output
156 * 0x102 forward scanning
157 * 0x182 scansione avanti
159 outbits(9, 0x2, 3, dev->io);
160 outbits(16, RSF16_ENCODE(freq), 2, dev->io);
162 fmr2_unmute(dev->io);
167 /* NOTE if mute this stop radio
168 you must set freq on unmute */
169 dev->stereo = fmr2_stereo_mode(dev->io);
173 /* !!! not tested, in my card this does't work !!! */
174 static int fmr2_setvolume(struct fmr2 *dev)
176 int vol[16] = { 0x021, 0x084, 0x090, 0x104,
177 0x110, 0x204, 0x210, 0x402,
178 0x404, 0x408, 0x410, 0x801,
179 0x802, 0x804, 0x808, 0x810 };
181 int n = vol[dev->curvol & 0x0f];
183 if (dev->card_type != 11)
186 for (i = 12; --i >= 0; ) {
187 a = ((n >> i) & 1) << 6; /* if (a==0) a = 0; else a = 0x40; */
188 outb(a | 4, dev->io);
190 outb(a | 0x24, dev->io);
192 outb(a | 4, dev->io);
195 for (i = 6; --i >= 0; ) {
196 a = ((0x18 >> i) & 1) << 6;
197 outb(a | 4, dev->io);
199 outb(a | 0x24, dev->io);
201 outb(a | 4, dev->io);
209 static int vidioc_querycap(struct file *file, void *priv,
210 struct v4l2_capability *v)
212 strlcpy(v->driver, "radio-sf16fmr2", sizeof(v->driver));
213 strlcpy(v->card, "SF16-FMR2 radio", sizeof(v->card));
214 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
215 v->version = RADIO_VERSION;
216 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
220 static int vidioc_g_tuner(struct file *file, void *priv,
221 struct v4l2_tuner *v)
223 struct fmr2 *fmr2 = video_drvdata(file);
228 strlcpy(v->name, "FM", sizeof(v->name));
229 v->type = V4L2_TUNER_RADIO;
231 v->rangelow = RSF16_MINFREQ;
232 v->rangehigh = RSF16_MAXFREQ;
233 v->rxsubchans = fmr2->stereo ? V4L2_TUNER_SUB_STEREO :
235 v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW;
236 v->audmode = V4L2_TUNER_MODE_STEREO;
237 mutex_lock(&fmr2->lock);
238 v->signal = fmr2_getsigstr(fmr2);
239 mutex_unlock(&fmr2->lock);
243 static int vidioc_s_tuner(struct file *file, void *priv,
244 struct v4l2_tuner *v)
246 return v->index ? -EINVAL : 0;
249 static int vidioc_s_frequency(struct file *file, void *priv,
250 struct v4l2_frequency *f)
252 struct fmr2 *fmr2 = video_drvdata(file);
254 if (f->frequency < RSF16_MINFREQ ||
255 f->frequency > RSF16_MAXFREQ)
257 /* rounding in steps of 200 to match the freq
259 fmr2->curfreq = (f->frequency / 200) * 200;
261 /* set card freq (if not muted) */
262 if (fmr2->curvol && !fmr2->mute) {
263 mutex_lock(&fmr2->lock);
265 mutex_unlock(&fmr2->lock);
270 static int vidioc_g_frequency(struct file *file, void *priv,
271 struct v4l2_frequency *f)
273 struct fmr2 *fmr2 = video_drvdata(file);
275 f->type = V4L2_TUNER_RADIO;
276 f->frequency = fmr2->curfreq;
280 static int vidioc_queryctrl(struct file *file, void *priv,
281 struct v4l2_queryctrl *qc)
283 struct fmr2 *fmr2 = video_drvdata(file);
286 case V4L2_CID_AUDIO_MUTE:
287 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
288 case V4L2_CID_AUDIO_VOLUME:
289 /* Only card_type == 11 implements volume */
290 if (fmr2->card_type == 11)
291 return v4l2_ctrl_query_fill(qc, 0, 15, 1, 0);
292 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
297 static int vidioc_g_ctrl(struct file *file, void *priv,
298 struct v4l2_control *ctrl)
300 struct fmr2 *fmr2 = video_drvdata(file);
303 case V4L2_CID_AUDIO_MUTE:
304 ctrl->value = fmr2->mute;
306 case V4L2_CID_AUDIO_VOLUME:
307 ctrl->value = fmr2->curvol;
313 static int vidioc_s_ctrl(struct file *file, void *priv,
314 struct v4l2_control *ctrl)
316 struct fmr2 *fmr2 = video_drvdata(file);
319 case V4L2_CID_AUDIO_MUTE:
320 fmr2->mute = ctrl->value;
322 case V4L2_CID_AUDIO_VOLUME:
323 fmr2->curvol = ctrl->value;
330 if (fmr2->curvol && !fmr2->mute)
331 printk(KERN_DEBUG "unmute\n");
333 printk(KERN_DEBUG "mute\n");
336 mutex_lock(&fmr2->lock);
337 if (fmr2->curvol && !fmr2->mute) {
338 fmr2_setvolume(fmr2);
339 /* Set frequency and unmute card */
343 mutex_unlock(&fmr2->lock);
347 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
353 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
355 return i ? -EINVAL : 0;
358 static int vidioc_g_audio(struct file *file, void *priv,
359 struct v4l2_audio *a)
362 strlcpy(a->name, "Radio", sizeof(a->name));
363 a->capability = V4L2_AUDCAP_STEREO;
367 static int vidioc_s_audio(struct file *file, void *priv,
368 struct v4l2_audio *a)
370 return a->index ? -EINVAL : 0;
373 static const struct v4l2_file_operations fmr2_fops = {
374 .owner = THIS_MODULE,
375 .ioctl = video_ioctl2,
378 static const struct v4l2_ioctl_ops fmr2_ioctl_ops = {
379 .vidioc_querycap = vidioc_querycap,
380 .vidioc_g_tuner = vidioc_g_tuner,
381 .vidioc_s_tuner = vidioc_s_tuner,
382 .vidioc_g_audio = vidioc_g_audio,
383 .vidioc_s_audio = vidioc_s_audio,
384 .vidioc_g_input = vidioc_g_input,
385 .vidioc_s_input = vidioc_s_input,
386 .vidioc_g_frequency = vidioc_g_frequency,
387 .vidioc_s_frequency = vidioc_s_frequency,
388 .vidioc_queryctrl = vidioc_queryctrl,
389 .vidioc_g_ctrl = vidioc_g_ctrl,
390 .vidioc_s_ctrl = vidioc_s_ctrl,
393 static int __init fmr2_init(void)
395 struct fmr2 *fmr2 = &fmr2_card;
396 struct v4l2_device *v4l2_dev = &fmr2->v4l2_dev;
399 strlcpy(v4l2_dev->name, "sf16fmr2", sizeof(v4l2_dev->name));
402 mutex_init(&fmr2->lock);
404 if (!request_region(fmr2->io, 2, "sf16fmr2")) {
405 v4l2_err(v4l2_dev, "request_region failed!\n");
409 res = v4l2_device_register(NULL, v4l2_dev);
411 release_region(fmr2->io, 2);
412 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
416 strlcpy(fmr2->vdev.name, v4l2_dev->name, sizeof(fmr2->vdev.name));
417 fmr2->vdev.v4l2_dev = v4l2_dev;
418 fmr2->vdev.fops = &fmr2_fops;
419 fmr2->vdev.ioctl_ops = &fmr2_ioctl_ops;
420 fmr2->vdev.release = video_device_release_empty;
421 video_set_drvdata(&fmr2->vdev, fmr2);
423 if (video_register_device(&fmr2->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
424 v4l2_device_unregister(v4l2_dev);
425 release_region(fmr2->io, 2);
429 v4l2_info(v4l2_dev, "SF16FMR2 radio card driver at 0x%x.\n", fmr2->io);
430 /* mute card - prevents noisy bootups */
431 mutex_lock(&fmr2->lock);
433 fmr2_product_info(fmr2);
434 mutex_unlock(&fmr2->lock);
435 debug_print((KERN_DEBUG "card_type %d\n", fmr2->card_type));
439 static void __exit fmr2_exit(void)
441 struct fmr2 *fmr2 = &fmr2_card;
443 video_unregister_device(&fmr2->vdev);
444 v4l2_device_unregister(&fmr2->v4l2_dev);
445 release_region(fmr2->io, 2);
448 module_init(fmr2_init);
449 module_exit(fmr2_exit);
453 static int __init fmr2_setup_io(char *str)
455 get_option(&str, &io);
459 __setup("sf16fmr2=", fmr2_setup_io);