xref: /linux/drivers/media/radio/radio-sf16fmi.c (revision 4b8303747474033d6d73828607eafab77c620d96)
1 /* SF16-FMI and SF16-FMP radio driver for Linux radio support
2  * heavily based on rtrack driver...
3  * (c) 1997 M. Kirkwood
4  * (c) 1998 Petr Vandrovec, vandrove@vc.cvut.cz
5  *
6  * Fitted to new interface by Alan Cox <alan@lxorguk.ukuu.org.uk>
7  * Made working and cleaned up functions <mikael.hedin@irf.se>
8  * Support for ISAPnP by Ladislav Michl <ladis@psi.cz>
9  *
10  * Notes on the hardware
11  *
12  *  Frequency control is done digitally -- ie out(port,encodefreq(95.8));
13  *  No volume control - only mute/unmute - you have to use line volume
14  *  control on SB-part of SF16-FMI/SF16-FMP
15  *
16  * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
17  */
18 
19 #include <linux/version.h>
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 
32 MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood");
33 MODULE_DESCRIPTION("A driver for the SF16-FMI and SF16-FMP radio.");
34 MODULE_LICENSE("GPL");
35 
36 static int io = -1;
37 static int radio_nr = -1;
38 
39 module_param(io, int, 0);
40 MODULE_PARM_DESC(io, "I/O address of the SF16-FMI or SF16-FMP card (0x284 or 0x384)");
41 module_param(radio_nr, int, 0);
42 
43 #define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
44 
45 struct fmi
46 {
47 	struct v4l2_device v4l2_dev;
48 	struct video_device vdev;
49 	int io;
50 	bool mute;
51 	unsigned long curfreq; /* freq in kHz */
52 	struct mutex lock;
53 };
54 
55 static struct fmi fmi_card;
56 static struct pnp_dev *dev;
57 
58 /* freq is in 1/16 kHz to internal number, hw precision is 50 kHz */
59 /* It is only useful to give freq in interval of 800 (=0.05Mhz),
60  * other bits will be truncated, e.g 92.7400016 -> 92.7, but
61  * 92.7400017 -> 92.75
62  */
63 #define RSF16_ENCODE(x)	((x) / 800 + 214)
64 #define RSF16_MINFREQ (87 * 16000)
65 #define RSF16_MAXFREQ (108 * 16000)
66 
67 static void outbits(int bits, unsigned int data, int io)
68 {
69 	while (bits--) {
70 		if (data & 1) {
71 			outb(5, io);
72 			udelay(6);
73 			outb(7, io);
74 			udelay(6);
75 		} else {
76 			outb(1, io);
77 			udelay(6);
78 			outb(3, io);
79 			udelay(6);
80 		}
81 		data >>= 1;
82 	}
83 }
84 
85 static inline void fmi_mute(struct fmi *fmi)
86 {
87 	mutex_lock(&fmi->lock);
88 	outb(0x00, fmi->io);
89 	mutex_unlock(&fmi->lock);
90 }
91 
92 static inline void fmi_unmute(struct fmi *fmi)
93 {
94 	mutex_lock(&fmi->lock);
95 	outb(0x08, fmi->io);
96 	mutex_unlock(&fmi->lock);
97 }
98 
99 static inline int fmi_setfreq(struct fmi *fmi, unsigned long freq)
100 {
101 	mutex_lock(&fmi->lock);
102 	fmi->curfreq = freq;
103 
104 	outbits(16, RSF16_ENCODE(freq), fmi->io);
105 	outbits(8, 0xC0, fmi->io);
106 	msleep(143);		/* was schedule_timeout(HZ/7) */
107 	mutex_unlock(&fmi->lock);
108 	if (!fmi->mute)
109 		fmi_unmute(fmi);
110 	return 0;
111 }
112 
113 static inline int fmi_getsigstr(struct fmi *fmi)
114 {
115 	int val;
116 	int res;
117 
118 	mutex_lock(&fmi->lock);
119 	val = fmi->mute ? 0x00 : 0x08;	/* mute/unmute */
120 	outb(val, fmi->io);
121 	outb(val | 0x10, fmi->io);
122 	msleep(143); 		/* was schedule_timeout(HZ/7) */
123 	res = (int)inb(fmi->io + 1);
124 	outb(val, fmi->io);
125 
126 	mutex_unlock(&fmi->lock);
127 	return (res & 2) ? 0 : 0xFFFF;
128 }
129 
130 static int vidioc_querycap(struct file *file, void  *priv,
131 					struct v4l2_capability *v)
132 {
133 	strlcpy(v->driver, "radio-sf16fmi", sizeof(v->driver));
134 	strlcpy(v->card, "SF16-FMx radio", sizeof(v->card));
135 	strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
136 	v->version = RADIO_VERSION;
137 	v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
138 	return 0;
139 }
140 
141 static int vidioc_g_tuner(struct file *file, void *priv,
142 					struct v4l2_tuner *v)
143 {
144 	struct fmi *fmi = video_drvdata(file);
145 
146 	if (v->index > 0)
147 		return -EINVAL;
148 
149 	strlcpy(v->name, "FM", sizeof(v->name));
150 	v->type = V4L2_TUNER_RADIO;
151 	v->rangelow = RSF16_MINFREQ;
152 	v->rangehigh = RSF16_MAXFREQ;
153 	v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
154 	v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW;
155 	v->audmode = V4L2_TUNER_MODE_STEREO;
156 	v->signal = fmi_getsigstr(fmi);
157 	return 0;
158 }
159 
160 static int vidioc_s_tuner(struct file *file, void *priv,
161 					struct v4l2_tuner *v)
162 {
163 	return v->index ? -EINVAL : 0;
164 }
165 
166 static int vidioc_s_frequency(struct file *file, void *priv,
167 					struct v4l2_frequency *f)
168 {
169 	struct fmi *fmi = video_drvdata(file);
170 
171 	if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
172 		return -EINVAL;
173 	if (f->frequency < RSF16_MINFREQ ||
174 			f->frequency > RSF16_MAXFREQ)
175 		return -EINVAL;
176 	/* rounding in steps of 800 to match the freq
177 	   that will be used */
178 	fmi_setfreq(fmi, (f->frequency / 800) * 800);
179 	return 0;
180 }
181 
182 static int vidioc_g_frequency(struct file *file, void *priv,
183 					struct v4l2_frequency *f)
184 {
185 	struct fmi *fmi = video_drvdata(file);
186 
187 	if (f->tuner != 0)
188 		return -EINVAL;
189 	f->type = V4L2_TUNER_RADIO;
190 	f->frequency = fmi->curfreq;
191 	return 0;
192 }
193 
194 static int vidioc_queryctrl(struct file *file, void *priv,
195 					struct v4l2_queryctrl *qc)
196 {
197 	switch (qc->id) {
198 	case V4L2_CID_AUDIO_MUTE:
199 		return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
200 	}
201 	return -EINVAL;
202 }
203 
204 static int vidioc_g_ctrl(struct file *file, void *priv,
205 					struct v4l2_control *ctrl)
206 {
207 	struct fmi *fmi = video_drvdata(file);
208 
209 	switch (ctrl->id) {
210 	case V4L2_CID_AUDIO_MUTE:
211 		ctrl->value = fmi->mute;
212 		return 0;
213 	}
214 	return -EINVAL;
215 }
216 
217 static int vidioc_s_ctrl(struct file *file, void *priv,
218 					struct v4l2_control *ctrl)
219 {
220 	struct fmi *fmi = video_drvdata(file);
221 
222 	switch (ctrl->id) {
223 	case V4L2_CID_AUDIO_MUTE:
224 		if (ctrl->value)
225 			fmi_mute(fmi);
226 		else
227 			fmi_unmute(fmi);
228 		fmi->mute = ctrl->value;
229 		return 0;
230 	}
231 	return -EINVAL;
232 }
233 
234 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
235 {
236 	*i = 0;
237 	return 0;
238 }
239 
240 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
241 {
242 	return i ? -EINVAL : 0;
243 }
244 
245 static int vidioc_g_audio(struct file *file, void *priv,
246 					struct v4l2_audio *a)
247 {
248 	a->index = 0;
249 	strlcpy(a->name, "Radio", sizeof(a->name));
250 	a->capability = V4L2_AUDCAP_STEREO;
251 	return 0;
252 }
253 
254 static int vidioc_s_audio(struct file *file, void *priv,
255 					struct v4l2_audio *a)
256 {
257 	return a->index ? -EINVAL : 0;
258 }
259 
260 static const struct v4l2_file_operations fmi_fops = {
261 	.owner		= THIS_MODULE,
262 	.ioctl		= video_ioctl2,
263 };
264 
265 static const struct v4l2_ioctl_ops fmi_ioctl_ops = {
266 	.vidioc_querycap    = vidioc_querycap,
267 	.vidioc_g_tuner     = vidioc_g_tuner,
268 	.vidioc_s_tuner     = vidioc_s_tuner,
269 	.vidioc_g_audio     = vidioc_g_audio,
270 	.vidioc_s_audio     = vidioc_s_audio,
271 	.vidioc_g_input     = vidioc_g_input,
272 	.vidioc_s_input     = vidioc_s_input,
273 	.vidioc_g_frequency = vidioc_g_frequency,
274 	.vidioc_s_frequency = vidioc_s_frequency,
275 	.vidioc_queryctrl   = vidioc_queryctrl,
276 	.vidioc_g_ctrl      = vidioc_g_ctrl,
277 	.vidioc_s_ctrl      = vidioc_s_ctrl,
278 };
279 
280 /* ladis: this is my card. does any other types exist? */
281 static struct isapnp_device_id id_table[] __devinitdata = {
282 	{	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
283 		ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
284 	{	ISAPNP_CARD_END, },
285 };
286 
287 MODULE_DEVICE_TABLE(isapnp, id_table);
288 
289 static int __init isapnp_fmi_probe(void)
290 {
291 	int i = 0;
292 
293 	while (id_table[i].card_vendor != 0 && dev == NULL) {
294 		dev = pnp_find_dev(NULL, id_table[i].vendor,
295 				   id_table[i].function, NULL);
296 		i++;
297 	}
298 
299 	if (!dev)
300 		return -ENODEV;
301 	if (pnp_device_attach(dev) < 0)
302 		return -EAGAIN;
303 	if (pnp_activate_dev(dev) < 0) {
304 		printk(KERN_ERR "radio-sf16fmi: PnP configure failed (out of resources?)\n");
305 		pnp_device_detach(dev);
306 		return -ENOMEM;
307 	}
308 	if (!pnp_port_valid(dev, 0)) {
309 		pnp_device_detach(dev);
310 		return -ENODEV;
311 	}
312 
313 	i = pnp_port_start(dev, 0);
314 	printk(KERN_INFO "radio-sf16fmi: PnP reports card at %#x\n", i);
315 
316 	return i;
317 }
318 
319 static int __init fmi_init(void)
320 {
321 	struct fmi *fmi = &fmi_card;
322 	struct v4l2_device *v4l2_dev = &fmi->v4l2_dev;
323 	int res;
324 
325 	if (io < 0)
326 		io = isapnp_fmi_probe();
327 	strlcpy(v4l2_dev->name, "sf16fmi", sizeof(v4l2_dev->name));
328 	fmi->io = io;
329 	if (fmi->io < 0) {
330 		v4l2_err(v4l2_dev, "No PnP card found.\n");
331 		return fmi->io;
332 	}
333 	if (!request_region(io, 2, "radio-sf16fmi")) {
334 		v4l2_err(v4l2_dev, "port 0x%x already in use\n", fmi->io);
335 		pnp_device_detach(dev);
336 		return -EBUSY;
337 	}
338 
339 	res = v4l2_device_register(NULL, v4l2_dev);
340 	if (res < 0) {
341 		release_region(fmi->io, 2);
342 		pnp_device_detach(dev);
343 		v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
344 		return res;
345 	}
346 
347 	strlcpy(fmi->vdev.name, v4l2_dev->name, sizeof(fmi->vdev.name));
348 	fmi->vdev.v4l2_dev = v4l2_dev;
349 	fmi->vdev.fops = &fmi_fops;
350 	fmi->vdev.ioctl_ops = &fmi_ioctl_ops;
351 	fmi->vdev.release = video_device_release_empty;
352 	video_set_drvdata(&fmi->vdev, fmi);
353 
354 	mutex_init(&fmi->lock);
355 
356 	if (video_register_device(&fmi->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
357 		v4l2_device_unregister(v4l2_dev);
358 		release_region(fmi->io, 2);
359 		pnp_device_detach(dev);
360 		return -EINVAL;
361 	}
362 
363 	v4l2_info(v4l2_dev, "card driver at 0x%x\n", fmi->io);
364 	/* mute card - prevents noisy bootups */
365 	fmi_mute(fmi);
366 	return 0;
367 }
368 
369 static void __exit fmi_exit(void)
370 {
371 	struct fmi *fmi = &fmi_card;
372 
373 	video_unregister_device(&fmi->vdev);
374 	v4l2_device_unregister(&fmi->v4l2_dev);
375 	release_region(fmi->io, 2);
376 	if (dev)
377 		pnp_device_detach(dev);
378 }
379 
380 module_init(fmi_init);
381 module_exit(fmi_exit);
382