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 <asm/io.h> /* outb, outb_p */
22 #include <asm/uaccess.h> /* copy to/from user */
23 #include <linux/videodev2.h> /* kernel radio structs */
24 #include <media/v4l2-common.h>
25 #include <linux/mutex.h>
27 static struct mutex lock;
29 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
30 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
32 #define AUD_VOL_INDEX 1
34 static struct v4l2_queryctrl radio_qctrl[] = {
36 .id = V4L2_CID_AUDIO_MUTE,
41 .type = V4L2_CTRL_TYPE_BOOLEAN,
44 .id = V4L2_CID_AUDIO_VOLUME,
50 .type = V4L2_CTRL_TYPE_INTEGER,
58 # define debug_print(s) printk s
60 # define debug_print(s)
63 /* this should be static vars for module size */
67 int curvol; /* 0-15 */
69 int stereo; /* card is producing stereo audio */
70 unsigned long curfreq; /* freq in kHz */
75 static int io = 0x384;
76 static int radio_nr = -1;
78 /* hw precision is 12.5 kHz
79 * It is only useful to give freq in intervall of 200 (=0.0125Mhz),
80 * other bits will be truncated
82 #define RSF16_ENCODE(x) ((x)/200+856)
83 #define RSF16_MINFREQ 87*16000
84 #define RSF16_MAXFREQ 108*16000
86 static inline void wait(int n,int port)
88 for (;n;--n) inb(port);
91 static void outbits(int bits, unsigned int data, int nWait, int port)
95 bit = (data>>bits) & 1;
105 static inline void fmr2_mute(int port)
111 static inline void fmr2_unmute(int port)
117 static inline int fmr2_stereo_mode(int port)
123 debug_print((KERN_DEBUG "stereo: %d\n", n));
127 static int fmr2_product_info(struct fmr2_device *dev)
129 int n = inb(dev->port);
133 /* this should support volume set */
137 /* not volume (mine is 11) */
138 dev->card_type = (n==128)?11:0;
142 static inline int fmr2_getsigstr(struct fmr2_device *dev)
144 /* !!! work only if scanning freq */
145 int port = dev->port, res = 0xffff;
148 if (!(inb(port)&1)) res = 0;
149 debug_print((KERN_DEBUG "signal: %d\n", res));
153 /* set frequency and unmute card */
154 static int fmr2_setfreq(struct fmr2_device *dev)
156 int port = dev->port;
157 unsigned long freq = dev->curfreq;
161 /* 0x42 for mono output
162 * 0x102 forward scanning
163 * 0x182 scansione avanti
165 outbits(9,0x2,3,port);
166 outbits(16,RSF16_ENCODE(freq),2,port);
173 /* NOTE if mute this stop radio
174 you must set freq on unmute */
175 dev->stereo = fmr2_stereo_mode(port);
179 /* !!! not tested, in my card this does't work !!! */
180 static int fmr2_setvolume(struct fmr2_device *dev)
182 int vol[16] = { 0x021, 0x084, 0x090, 0x104,
183 0x110, 0x204, 0x210, 0x402,
184 0x404, 0x408, 0x410, 0x801,
185 0x802, 0x804, 0x808, 0x810 };
186 int i, a, port = dev->port;
187 int n = vol[dev->curvol & 0x0f];
189 if (dev->card_type != 11)
192 for (i = 12; --i >= 0; ) {
193 a = ((n >> i) & 1) << 6; /* if (a=0) a= 0; else a= 0x40; */
196 outb(a | 0x24, port);
201 for (i = 6; --i >= 0; ) {
202 a = ((0x18 >> i) & 1) << 6;
205 outb(a | 0x24, port);
216 static int vidioc_querycap(struct file *file, void *priv,
217 struct v4l2_capability *v)
219 strlcpy(v->driver, "radio-sf16fmr2", sizeof(v->driver));
220 strlcpy(v->card, "SF16-FMR2 radio", sizeof(v->card));
221 sprintf(v->bus_info, "ISA");
222 v->version = RADIO_VERSION;
223 v->capabilities = V4L2_CAP_TUNER;
227 static int vidioc_g_tuner(struct file *file, void *priv,
228 struct v4l2_tuner *v)
231 struct video_device *dev = video_devdata(file);
232 struct fmr2_device *fmr2 = dev->priv;
237 strcpy(v->name, "FM");
238 v->type = V4L2_TUNER_RADIO;
240 mult = (fmr2->flags & V4L2_TUNER_CAP_LOW) ? 1 : 1000;
241 v->rangelow = RSF16_MINFREQ/mult;
242 v->rangehigh = RSF16_MAXFREQ/mult;
243 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_MODE_STEREO;
244 v->capability = fmr2->flags&V4L2_TUNER_CAP_LOW;
245 v->audmode = fmr2->stereo ? V4L2_TUNER_MODE_STEREO:
246 V4L2_TUNER_MODE_MONO;
248 v->signal = fmr2_getsigstr(fmr2);
253 static int vidioc_s_tuner(struct file *file, void *priv,
254 struct v4l2_tuner *v)
261 static int vidioc_s_frequency(struct file *file, void *priv,
262 struct v4l2_frequency *f)
264 struct video_device *dev = video_devdata(file);
265 struct fmr2_device *fmr2 = dev->priv;
267 if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
268 f->frequency *= 1000;
269 if (f->frequency < RSF16_MINFREQ ||
270 f->frequency > RSF16_MAXFREQ )
272 /*rounding in steps of 200 to match th freq
274 fmr2->curfreq = (f->frequency/200)*200;
276 /* set card freq (if not muted) */
277 if (fmr2->curvol && !fmr2->mute) {
285 static int vidioc_g_frequency(struct file *file, void *priv,
286 struct v4l2_frequency *f)
288 struct video_device *dev = video_devdata(file);
289 struct fmr2_device *fmr2 = dev->priv;
291 f->type = V4L2_TUNER_RADIO;
292 f->frequency = fmr2->curfreq;
293 if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
294 f->frequency /= 1000;
298 static int vidioc_queryctrl(struct file *file, void *priv,
299 struct v4l2_queryctrl *qc)
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], sizeof(*qc));
312 static int vidioc_g_ctrl(struct file *file, void *priv,
313 struct v4l2_control *ctrl)
315 struct video_device *dev = video_devdata(file);
316 struct fmr2_device *fmr2 = dev->priv;
319 case V4L2_CID_AUDIO_MUTE:
320 ctrl->value = fmr2->mute;
322 case V4L2_CID_AUDIO_VOLUME:
323 ctrl->value = fmr2->curvol;
329 static int vidioc_s_ctrl(struct file *file, void *priv,
330 struct v4l2_control *ctrl)
332 struct video_device *dev = video_devdata(file);
333 struct fmr2_device *fmr2 = dev->priv;
336 case V4L2_CID_AUDIO_MUTE:
337 fmr2->mute = ctrl->value;
339 case V4L2_CID_AUDIO_VOLUME:
340 if (ctrl->value > radio_qctrl[AUD_VOL_INDEX].maximum)
341 fmr2->curvol = radio_qctrl[AUD_VOL_INDEX].maximum;
343 fmr2->curvol = ctrl->value;
351 if (fmr2->curvol && !fmr2->mute)
352 printk(KERN_DEBUG "unmute\n");
354 printk(KERN_DEBUG "mute\n");
358 if (fmr2->curvol && !fmr2->mute) {
359 fmr2_setvolume(fmr2);
360 /* Set frequency and unmute card */
363 fmr2_mute(fmr2->port);
368 static int vidioc_g_audio(struct file *file, void *priv,
369 struct v4l2_audio *a)
374 strcpy(a->name, "Radio");
375 a->capability = V4L2_AUDCAP_STEREO;
379 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
385 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
392 static int vidioc_s_audio(struct file *file, void *priv,
393 struct v4l2_audio *a)
400 static struct fmr2_device fmr2_unit;
402 static const struct file_operations fmr2_fops = {
403 .owner = THIS_MODULE,
404 .open = video_exclusive_open,
405 .release = video_exclusive_release,
406 .ioctl = video_ioctl2,
408 .compat_ioctl = v4l_compat_ioctl32,
413 static struct video_device fmr2_radio=
415 .owner = THIS_MODULE,
416 .name = "SF16FMR2 radio",
417 . type = VID_TYPE_TUNER,
419 .vidioc_querycap = vidioc_querycap,
420 .vidioc_g_tuner = vidioc_g_tuner,
421 .vidioc_s_tuner = vidioc_s_tuner,
422 .vidioc_g_audio = vidioc_g_audio,
423 .vidioc_s_audio = vidioc_s_audio,
424 .vidioc_g_input = vidioc_g_input,
425 .vidioc_s_input = vidioc_s_input,
426 .vidioc_g_frequency = vidioc_g_frequency,
427 .vidioc_s_frequency = vidioc_s_frequency,
428 .vidioc_queryctrl = vidioc_queryctrl,
429 .vidioc_g_ctrl = vidioc_g_ctrl,
430 .vidioc_s_ctrl = vidioc_s_ctrl,
433 static int __init fmr2_init(void)
436 fmr2_unit.curvol = 0;
438 fmr2_unit.curfreq = 0;
439 fmr2_unit.stereo = 1;
440 fmr2_unit.flags = V4L2_TUNER_CAP_LOW;
441 fmr2_unit.card_type = 0;
442 fmr2_radio.priv = &fmr2_unit;
446 if (!request_region(io, 2, "sf16fmr2")) {
447 printk(KERN_ERR "radio-sf16fmr2: request_region failed!\n");
451 if (video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
452 release_region(io, 2);
456 printk(KERN_INFO "SF16FMR2 radio card driver at 0x%x.\n", io);
457 /* mute card - prevents noisy bootups */
460 fmr2_product_info(&fmr2_unit);
462 debug_print((KERN_DEBUG "card_type %d\n", fmr2_unit.card_type));
464 /* Only card_type == 11 implements volume */
465 if (fmr2_unit.card_type != 11)
466 radio_qctrl[AUD_VOL_INDEX].maximum = 1;
471 MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
472 MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
473 MODULE_LICENSE("GPL");
475 module_param(io, int, 0);
476 MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
477 module_param(radio_nr, int, 0);
479 static void __exit fmr2_cleanup_module(void)
481 video_unregister_device(&fmr2_radio);
482 release_region(io,2);
485 module_init(fmr2_init);
486 module_exit(fmr2_cleanup_module);
490 static int __init fmr2_setup_io(char *str)
492 get_option(&str, &io);
496 __setup("sf16fmr2=", fmr2_setup_io);