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 */
67 static struct fmr2 fmr2_card
;
69 /* hw precision is 12.5 kHz
70 * It is only useful to give freq in intervall of 200 (=0.0125Mhz),
71 * other bits will be truncated
73 #define RSF16_ENCODE(x) ((x) / 200 + 856)
74 #define RSF16_MINFREQ (87 * 16000)
75 #define RSF16_MAXFREQ (108 * 16000)
77 static inline void wait(int n
, int io
)
83 static void outbits(int bits
, unsigned int data
, int nWait
, int io
)
87 for (; --bits
>= 0;) {
88 bit
= (data
>> bits
) & 1;
98 static inline void fmr2_mute(int io
)
104 static inline void fmr2_unmute(int io
)
110 static inline int fmr2_stereo_mode(int io
)
116 n
= ((n
>> 3) & 1) ^ 1;
117 debug_print((KERN_DEBUG
"stereo: %d\n", n
));
121 static int fmr2_product_info(struct fmr2
*dev
)
123 int n
= inb(dev
->io
);
127 /* this should support volume set */
131 /* not volume (mine is 11) */
132 dev
->card_type
= (n
== 128) ? 11 : 0;
136 static inline int fmr2_getsigstr(struct fmr2
*dev
)
138 /* !!! works only if scanning freq */
143 if (!(inb(dev
->io
) & 1))
145 debug_print((KERN_DEBUG
"signal: %d\n", res
));
149 /* set frequency and unmute card */
150 static int fmr2_setfreq(struct fmr2
*dev
)
152 unsigned long freq
= dev
->curfreq
;
156 /* 0x42 for mono output
157 * 0x102 forward scanning
158 * 0x182 scansione avanti
160 outbits(9, 0x2, 3, dev
->io
);
161 outbits(16, RSF16_ENCODE(freq
), 2, dev
->io
);
163 fmr2_unmute(dev
->io
);
168 /* NOTE if mute this stop radio
169 you must set freq on unmute */
170 dev
->stereo
= fmr2_stereo_mode(dev
->io
);
174 /* !!! not tested, in my card this does't work !!! */
175 static int fmr2_setvolume(struct fmr2
*dev
)
177 int vol
[16] = { 0x021, 0x084, 0x090, 0x104,
178 0x110, 0x204, 0x210, 0x402,
179 0x404, 0x408, 0x410, 0x801,
180 0x802, 0x804, 0x808, 0x810 };
182 int n
= vol
[dev
->curvol
& 0x0f];
184 if (dev
->card_type
!= 11)
187 for (i
= 12; --i
>= 0; ) {
188 a
= ((n
>> i
) & 1) << 6; /* if (a==0) a = 0; else a = 0x40; */
189 outb(a
| 4, dev
->io
);
191 outb(a
| 0x24, dev
->io
);
193 outb(a
| 4, dev
->io
);
196 for (i
= 6; --i
>= 0; ) {
197 a
= ((0x18 >> i
) & 1) << 6;
198 outb(a
| 4, dev
->io
);
200 outb(a
| 0x24, dev
->io
);
202 outb(a
| 4, dev
->io
);
210 static int vidioc_querycap(struct file
*file
, void *priv
,
211 struct v4l2_capability
*v
)
213 strlcpy(v
->driver
, "radio-sf16fmr2", sizeof(v
->driver
));
214 strlcpy(v
->card
, "SF16-FMR2 radio", sizeof(v
->card
));
215 strlcpy(v
->bus_info
, "ISA", sizeof(v
->bus_info
));
216 v
->version
= RADIO_VERSION
;
217 v
->capabilities
= V4L2_CAP_TUNER
| V4L2_CAP_RADIO
;
221 static int vidioc_g_tuner(struct file
*file
, void *priv
,
222 struct v4l2_tuner
*v
)
225 struct fmr2
*fmr2
= video_drvdata(file
);
230 strlcpy(v
->name
, "FM", sizeof(v
->name
));
231 v
->type
= V4L2_TUNER_RADIO
;
233 mult
= (fmr2
->flags
& V4L2_TUNER_CAP_LOW
) ? 1 : 1000;
234 v
->rangelow
= RSF16_MINFREQ
/ mult
;
235 v
->rangehigh
= RSF16_MAXFREQ
/ mult
;
236 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_MODE_STEREO
;
237 v
->capability
= fmr2
->flags
&V4L2_TUNER_CAP_LOW
;
238 v
->audmode
= fmr2
->stereo
? V4L2_TUNER_MODE_STEREO
:
239 V4L2_TUNER_MODE_MONO
;
240 mutex_lock(&fmr2
->lock
);
241 v
->signal
= fmr2_getsigstr(fmr2
);
242 mutex_unlock(&fmr2
->lock
);
246 static int vidioc_s_tuner(struct file
*file
, void *priv
,
247 struct v4l2_tuner
*v
)
249 return v
->index
? -EINVAL
: 0;
252 static int vidioc_s_frequency(struct file
*file
, void *priv
,
253 struct v4l2_frequency
*f
)
255 struct fmr2
*fmr2
= video_drvdata(file
);
257 if (!(fmr2
->flags
& V4L2_TUNER_CAP_LOW
))
258 f
->frequency
*= 1000;
259 if (f
->frequency
< RSF16_MINFREQ
||
260 f
->frequency
> RSF16_MAXFREQ
)
262 /* rounding in steps of 200 to match the freq
264 fmr2
->curfreq
= (f
->frequency
/ 200) * 200;
266 /* set card freq (if not muted) */
267 if (fmr2
->curvol
&& !fmr2
->mute
) {
268 mutex_lock(&fmr2
->lock
);
270 mutex_unlock(&fmr2
->lock
);
275 static int vidioc_g_frequency(struct file
*file
, void *priv
,
276 struct v4l2_frequency
*f
)
278 struct fmr2
*fmr2
= video_drvdata(file
);
280 f
->type
= V4L2_TUNER_RADIO
;
281 f
->frequency
= fmr2
->curfreq
;
282 if (!(fmr2
->flags
& V4L2_TUNER_CAP_LOW
))
283 f
->frequency
/= 1000;
287 static int vidioc_queryctrl(struct file
*file
, void *priv
,
288 struct v4l2_queryctrl
*qc
)
290 struct fmr2
*fmr2
= video_drvdata(file
);
293 case V4L2_CID_AUDIO_MUTE
:
294 return v4l2_ctrl_query_fill(qc
, 0, 1, 1, 1);
295 case V4L2_CID_AUDIO_VOLUME
:
296 /* Only card_type == 11 implements volume */
297 if (fmr2
->card_type
== 11)
298 return v4l2_ctrl_query_fill(qc
, 0, 15, 1, 0);
299 return v4l2_ctrl_query_fill(qc
, 0, 1, 1, 0);
304 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
305 struct v4l2_control
*ctrl
)
307 struct fmr2
*fmr2
= video_drvdata(file
);
310 case V4L2_CID_AUDIO_MUTE
:
311 ctrl
->value
= fmr2
->mute
;
313 case V4L2_CID_AUDIO_VOLUME
:
314 ctrl
->value
= fmr2
->curvol
;
320 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
321 struct v4l2_control
*ctrl
)
323 struct fmr2
*fmr2
= video_drvdata(file
);
326 case V4L2_CID_AUDIO_MUTE
:
327 fmr2
->mute
= ctrl
->value
;
329 case V4L2_CID_AUDIO_VOLUME
:
330 fmr2
->curvol
= ctrl
->value
;
337 if (fmr2
->curvol
&& !fmr2
->mute
)
338 printk(KERN_DEBUG
"unmute\n");
340 printk(KERN_DEBUG
"mute\n");
343 mutex_lock(&fmr2
->lock
);
344 if (fmr2
->curvol
&& !fmr2
->mute
) {
345 fmr2_setvolume(fmr2
);
346 /* Set frequency and unmute card */
350 mutex_unlock(&fmr2
->lock
);
354 static int vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
360 static int vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
362 return i
? -EINVAL
: 0;
365 static int vidioc_g_audio(struct file
*file
, void *priv
,
366 struct v4l2_audio
*a
)
369 strlcpy(a
->name
, "Radio", sizeof(a
->name
));
370 a
->capability
= V4L2_AUDCAP_STEREO
;
374 static int vidioc_s_audio(struct file
*file
, void *priv
,
375 struct v4l2_audio
*a
)
377 return a
->index
? -EINVAL
: 0;
380 static const struct v4l2_file_operations fmr2_fops
= {
381 .owner
= THIS_MODULE
,
382 .ioctl
= video_ioctl2
,
385 static const struct v4l2_ioctl_ops fmr2_ioctl_ops
= {
386 .vidioc_querycap
= vidioc_querycap
,
387 .vidioc_g_tuner
= vidioc_g_tuner
,
388 .vidioc_s_tuner
= vidioc_s_tuner
,
389 .vidioc_g_audio
= vidioc_g_audio
,
390 .vidioc_s_audio
= vidioc_s_audio
,
391 .vidioc_g_input
= vidioc_g_input
,
392 .vidioc_s_input
= vidioc_s_input
,
393 .vidioc_g_frequency
= vidioc_g_frequency
,
394 .vidioc_s_frequency
= vidioc_s_frequency
,
395 .vidioc_queryctrl
= vidioc_queryctrl
,
396 .vidioc_g_ctrl
= vidioc_g_ctrl
,
397 .vidioc_s_ctrl
= vidioc_s_ctrl
,
400 static int __init
fmr2_init(void)
402 struct fmr2
*fmr2
= &fmr2_card
;
403 struct v4l2_device
*v4l2_dev
= &fmr2
->v4l2_dev
;
406 strlcpy(v4l2_dev
->name
, "sf16fmr2", sizeof(v4l2_dev
->name
));
409 fmr2
->flags
= V4L2_TUNER_CAP_LOW
;
410 mutex_init(&fmr2
->lock
);
412 if (!request_region(fmr2
->io
, 2, "sf16fmr2")) {
413 v4l2_err(v4l2_dev
, "request_region failed!\n");
417 res
= v4l2_device_register(NULL
, v4l2_dev
);
419 release_region(fmr2
->io
, 2);
420 v4l2_err(v4l2_dev
, "Could not register v4l2_device\n");
424 strlcpy(fmr2
->vdev
.name
, v4l2_dev
->name
, sizeof(fmr2
->vdev
.name
));
425 fmr2
->vdev
.v4l2_dev
= v4l2_dev
;
426 fmr2
->vdev
.fops
= &fmr2_fops
;
427 fmr2
->vdev
.ioctl_ops
= &fmr2_ioctl_ops
;
428 fmr2
->vdev
.release
= video_device_release_empty
;
429 video_set_drvdata(&fmr2
->vdev
, fmr2
);
431 if (video_register_device(&fmr2
->vdev
, VFL_TYPE_RADIO
, radio_nr
) < 0) {
432 v4l2_device_unregister(v4l2_dev
);
433 release_region(fmr2
->io
, 2);
437 v4l2_info(v4l2_dev
, "SF16FMR2 radio card driver at 0x%x.\n", fmr2
->io
);
438 /* mute card - prevents noisy bootups */
439 mutex_lock(&fmr2
->lock
);
441 fmr2_product_info(fmr2
);
442 mutex_unlock(&fmr2
->lock
);
443 debug_print((KERN_DEBUG
"card_type %d\n", fmr2
->card_type
));
447 static void __exit
fmr2_exit(void)
449 struct fmr2
*fmr2
= &fmr2_card
;
451 video_unregister_device(&fmr2
->vdev
);
452 v4l2_device_unregister(&fmr2
->v4l2_dev
);
453 release_region(fmr2
->io
, 2);
456 module_init(fmr2_init
);
457 module_exit(fmr2_exit
);
461 static int __init
fmr2_setup_io(char *str
)
463 get_option(&str
, &io
);
467 __setup("sf16fmr2=", fmr2_setup_io
);