1 // SPDX-License-Identifier: GPL-2.0-only
2 /* SF16-FMI, SF16-FMP and SF16-FMD radio driver for Linux radio support
3 * heavily based on rtrack driver...
5 * (c) 1998 Petr Vandrovec, vandrove@vc.cvut.cz
7 * Fitted to new interface by Alan Cox <alan@lxorguk.ukuu.org.uk>
8 * Made working and cleaned up functions <mikael.hedin@irf.se>
9 * Support for ISAPnP by Ladislav Michl <ladis@psi.cz>
11 * Notes on the hardware
13 * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
14 * No volume control - only mute/unmute - you have to use line volume
15 * control on SB-part of SF16-FMI/SF16-FMP/SF16-FMD
17 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@kernel.org>
20 #include <linux/kernel.h> /* __setup */
21 #include <linux/module.h> /* Modules */
22 #include <linux/init.h> /* Initdata */
23 #include <linux/ioport.h> /* request_region */
24 #include <linux/delay.h> /* udelay */
25 #include <linux/isapnp.h>
26 #include <linux/mutex.h>
27 #include <linux/videodev2.h> /* kernel radio structs */
28 #include <linux/io.h> /* outb, outb_p */
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-ioctl.h>
31 #include <media/v4l2-ctrls.h>
32 #include <media/v4l2-event.h>
35 MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood");
36 MODULE_DESCRIPTION("A driver for the SF16-FMI, SF16-FMP and SF16-FMD radio.");
37 MODULE_LICENSE("GPL");
38 MODULE_VERSION("0.0.3");
41 static int radio_nr
= -1;
43 module_param(io
, int, 0);
44 MODULE_PARM_DESC(io
, "I/O address of the SF16-FMI/SF16-FMP/SF16-FMD card (0x284 or 0x384)");
45 module_param(radio_nr
, int, 0);
49 struct v4l2_device v4l2_dev
;
50 struct v4l2_ctrl_handler hdl
;
51 struct video_device vdev
;
54 u32 curfreq
; /* freq in kHz */
58 static struct fmi fmi_card
;
59 static struct pnp_dev
*dev
;
60 static bool pnp_attached
;
62 #define RSF16_MINFREQ (87U * 16000)
63 #define RSF16_MAXFREQ (108U * 16000)
65 #define FMI_BIT_TUN_CE (1 << 0)
66 #define FMI_BIT_TUN_CLK (1 << 1)
67 #define FMI_BIT_TUN_DATA (1 << 2)
68 #define FMI_BIT_VOL_SW (1 << 3)
69 #define FMI_BIT_TUN_STRQ (1 << 4)
71 static void fmi_set_pins(void *handle
, u8 pins
)
73 struct fmi
*fmi
= handle
;
74 u8 bits
= FMI_BIT_TUN_STRQ
;
77 bits
|= FMI_BIT_VOL_SW
;
79 if (pins
& LM7000_DATA
)
80 bits
|= FMI_BIT_TUN_DATA
;
81 if (pins
& LM7000_CLK
)
82 bits
|= FMI_BIT_TUN_CLK
;
84 bits
|= FMI_BIT_TUN_CE
;
86 mutex_lock(&fmi
->lock
);
87 outb_p(bits
, fmi
->io
);
88 mutex_unlock(&fmi
->lock
);
91 static inline void fmi_mute(struct fmi
*fmi
)
93 mutex_lock(&fmi
->lock
);
95 mutex_unlock(&fmi
->lock
);
98 static inline void fmi_unmute(struct fmi
*fmi
)
100 mutex_lock(&fmi
->lock
);
102 mutex_unlock(&fmi
->lock
);
105 static inline int fmi_getsigstr(struct fmi
*fmi
)
110 mutex_lock(&fmi
->lock
);
111 val
= fmi
->mute
? 0x00 : 0x08; /* mute/unmute */
113 outb(val
| 0x10, fmi
->io
);
114 msleep(143); /* was schedule_timeout(HZ/7) */
115 res
= (int)inb(fmi
->io
+ 1);
118 mutex_unlock(&fmi
->lock
);
119 return (res
& 2) ? 0 : 0xFFFF;
122 static void fmi_set_freq(struct fmi
*fmi
)
124 fmi
->curfreq
= clamp(fmi
->curfreq
, RSF16_MINFREQ
, RSF16_MAXFREQ
);
125 /* rounding in steps of 800 to match the freq
127 lm7000_set_freq((fmi
->curfreq
/ 800) * 800, fmi
, fmi_set_pins
);
130 static int vidioc_querycap(struct file
*file
, void *priv
,
131 struct v4l2_capability
*v
)
133 strscpy(v
->driver
, "radio-sf16fmi", sizeof(v
->driver
));
134 strscpy(v
->card
, "SF16-FMI/FMP/FMD radio", sizeof(v
->card
));
135 strscpy(v
->bus_info
, "ISA:radio-sf16fmi", sizeof(v
->bus_info
));
139 static int vidioc_g_tuner(struct file
*file
, void *priv
,
140 struct v4l2_tuner
*v
)
142 struct fmi
*fmi
= video_drvdata(file
);
147 strscpy(v
->name
, "FM", sizeof(v
->name
));
148 v
->type
= V4L2_TUNER_RADIO
;
149 v
->rangelow
= RSF16_MINFREQ
;
150 v
->rangehigh
= RSF16_MAXFREQ
;
151 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
152 v
->capability
= V4L2_TUNER_CAP_STEREO
| V4L2_TUNER_CAP_LOW
;
153 v
->audmode
= V4L2_TUNER_MODE_STEREO
;
154 v
->signal
= fmi_getsigstr(fmi
);
158 static int vidioc_s_tuner(struct file
*file
, void *priv
,
159 const struct v4l2_tuner
*v
)
161 return v
->index
? -EINVAL
: 0;
164 static int vidioc_s_frequency(struct file
*file
, void *priv
,
165 const struct v4l2_frequency
*f
)
167 struct fmi
*fmi
= video_drvdata(file
);
169 if (f
->tuner
!= 0 || f
->type
!= V4L2_TUNER_RADIO
)
172 fmi
->curfreq
= f
->frequency
;
178 static int vidioc_g_frequency(struct file
*file
, void *priv
,
179 struct v4l2_frequency
*f
)
181 struct fmi
*fmi
= video_drvdata(file
);
185 f
->type
= V4L2_TUNER_RADIO
;
186 f
->frequency
= fmi
->curfreq
;
190 static int fmi_s_ctrl(struct v4l2_ctrl
*ctrl
)
192 struct fmi
*fmi
= container_of(ctrl
->handler
, struct fmi
, hdl
);
195 case V4L2_CID_AUDIO_MUTE
:
200 fmi
->mute
= ctrl
->val
;
206 static const struct v4l2_ctrl_ops fmi_ctrl_ops
= {
207 .s_ctrl
= fmi_s_ctrl
,
210 static const struct v4l2_file_operations fmi_fops
= {
211 .owner
= THIS_MODULE
,
212 .open
= v4l2_fh_open
,
213 .release
= v4l2_fh_release
,
214 .poll
= v4l2_ctrl_poll
,
215 .unlocked_ioctl
= video_ioctl2
,
218 static const struct v4l2_ioctl_ops fmi_ioctl_ops
= {
219 .vidioc_querycap
= vidioc_querycap
,
220 .vidioc_g_tuner
= vidioc_g_tuner
,
221 .vidioc_s_tuner
= vidioc_s_tuner
,
222 .vidioc_g_frequency
= vidioc_g_frequency
,
223 .vidioc_s_frequency
= vidioc_s_frequency
,
224 .vidioc_log_status
= v4l2_ctrl_log_status
,
225 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
226 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
229 /* ladis: this is my card. does any other types exist? */
230 static struct isapnp_device_id id_table
[] = {
232 { ISAPNP_ANY_ID
, ISAPNP_ANY_ID
,
233 ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
235 { ISAPNP_ANY_ID
, ISAPNP_ANY_ID
,
236 ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad12), 0},
237 { ISAPNP_CARD_END
, },
240 MODULE_DEVICE_TABLE(isapnp
, id_table
);
242 static int __init
isapnp_fmi_probe(void)
246 while (id_table
[i
].card_vendor
!= 0 && dev
== NULL
) {
247 dev
= pnp_find_dev(NULL
, id_table
[i
].vendor
,
248 id_table
[i
].function
, NULL
);
254 if (pnp_device_attach(dev
) < 0)
256 if (pnp_activate_dev(dev
) < 0) {
257 printk(KERN_ERR
"radio-sf16fmi: PnP configure failed (out of resources?)\n");
258 pnp_device_detach(dev
);
261 if (!pnp_port_valid(dev
, 0)) {
262 pnp_device_detach(dev
);
266 i
= pnp_port_start(dev
, 0);
267 printk(KERN_INFO
"radio-sf16fmi: PnP reports card at %#x\n", i
);
272 static int __init
fmi_init(void)
274 struct fmi
*fmi
= &fmi_card
;
275 struct v4l2_device
*v4l2_dev
= &fmi
->v4l2_dev
;
276 struct v4l2_ctrl_handler
*hdl
= &fmi
->hdl
;
278 int probe_ports
[] = { 0, 0x284, 0x384 };
281 for (i
= 0; i
< ARRAY_SIZE(probe_ports
); i
++) {
284 io
= isapnp_fmi_probe();
289 if (!request_region(io
, 2, "radio-sf16fmi")) {
291 pnp_device_detach(dev
);
296 ((inb(io
) & 0xf9) == 0xf9 && (inb(io
) & 0x4) == 0))
298 release_region(io
, 2);
302 if (!request_region(io
, 2, "radio-sf16fmi")) {
303 printk(KERN_ERR
"radio-sf16fmi: port %#x already in use\n", io
);
306 if (inb(io
) == 0xff) {
307 printk(KERN_ERR
"radio-sf16fmi: card not present at %#x\n", io
);
308 release_region(io
, 2);
313 printk(KERN_ERR
"radio-sf16fmi: no cards found\n");
317 strscpy(v4l2_dev
->name
, "sf16fmi", sizeof(v4l2_dev
->name
));
320 res
= v4l2_device_register(NULL
, v4l2_dev
);
322 release_region(fmi
->io
, 2);
324 pnp_device_detach(dev
);
325 v4l2_err(v4l2_dev
, "Could not register v4l2_device\n");
329 v4l2_ctrl_handler_init(hdl
, 1);
330 v4l2_ctrl_new_std(hdl
, &fmi_ctrl_ops
,
331 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 1);
332 v4l2_dev
->ctrl_handler
= hdl
;
335 v4l2_err(v4l2_dev
, "Could not register controls\n");
336 v4l2_ctrl_handler_free(hdl
);
337 v4l2_device_unregister(v4l2_dev
);
341 strscpy(fmi
->vdev
.name
, v4l2_dev
->name
, sizeof(fmi
->vdev
.name
));
342 fmi
->vdev
.v4l2_dev
= v4l2_dev
;
343 fmi
->vdev
.fops
= &fmi_fops
;
344 fmi
->vdev
.ioctl_ops
= &fmi_ioctl_ops
;
345 fmi
->vdev
.release
= video_device_release_empty
;
346 fmi
->vdev
.device_caps
= V4L2_CAP_TUNER
| V4L2_CAP_RADIO
;
347 video_set_drvdata(&fmi
->vdev
, fmi
);
349 mutex_init(&fmi
->lock
);
351 /* mute card and set default frequency */
353 fmi
->curfreq
= RSF16_MINFREQ
;
356 if (video_register_device(&fmi
->vdev
, VFL_TYPE_RADIO
, radio_nr
) < 0) {
357 v4l2_ctrl_handler_free(hdl
);
358 v4l2_device_unregister(v4l2_dev
);
359 release_region(fmi
->io
, 2);
361 pnp_device_detach(dev
);
365 v4l2_info(v4l2_dev
, "card driver at 0x%x\n", fmi
->io
);
369 static void __exit
fmi_exit(void)
371 struct fmi
*fmi
= &fmi_card
;
373 v4l2_ctrl_handler_free(&fmi
->hdl
);
374 video_unregister_device(&fmi
->vdev
);
375 v4l2_device_unregister(&fmi
->v4l2_dev
);
376 release_region(fmi
->io
, 2);
377 if (dev
&& pnp_attached
)
378 pnp_device_detach(dev
);
381 module_init(fmi_init
);
382 module_exit(fmi_exit
);