1 /* SF16FMR2 radio driver for Linux radio support
2  * heavily based on fmi driver...
3  * (c) 2000-2002 Ziglio Frediano, freddy77@angelfire.com
4  *
5  * Notes on the hardware
6  *
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
9  *
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)
13  *
14  *  Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
15  */
16 
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>
26 
27 static struct mutex lock;
28 
29 #include <linux/version.h>      /* for KERNEL_VERSION MACRO     */
30 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
31 
32 #define AUD_VOL_INDEX 1
33 
34 static struct v4l2_queryctrl radio_qctrl[] = {
35 	{
36 		.id            = V4L2_CID_AUDIO_MUTE,
37 		.name          = "Mute",
38 		.minimum       = 0,
39 		.maximum       = 1,
40 		.default_value = 1,
41 		.type          = V4L2_CTRL_TYPE_BOOLEAN,
42 	},
43 	[AUD_VOL_INDEX] = {
44 		.id            = V4L2_CID_AUDIO_VOLUME,
45 		.name          = "Volume",
46 		.minimum       = 0,
47 		.maximum       = 15,
48 		.step          = 1,
49 		.default_value = 0,
50 		.type          = V4L2_CTRL_TYPE_INTEGER,
51 	}
52 };
53 
54 #undef DEBUG
55 //#define DEBUG 1
56 
57 #ifdef DEBUG
58 # define  debug_print(s) printk s
59 #else
60 # define  debug_print(s)
61 #endif
62 
63 /* this should be static vars for module size */
64 struct fmr2_device
65 {
66 	int port;
67 	int curvol; /* 0-15 */
68 	int mute;
69 	int stereo; /* card is producing stereo audio */
70 	unsigned long curfreq; /* freq in kHz */
71 	int card_type;
72 	__u32 flags;
73 };
74 
75 static int io = 0x384;
76 static int radio_nr = -1;
77 
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
81  */
82 #define RSF16_ENCODE(x)	((x)/200+856)
83 #define RSF16_MINFREQ 87*16000
84 #define RSF16_MAXFREQ 108*16000
85 
86 static inline void wait(int n,int port)
87 {
88 	for (;n;--n) inb(port);
89 }
90 
91 static void outbits(int bits, unsigned int data, int nWait, int port)
92 {
93 	int bit;
94 	for(;--bits>=0;) {
95 		bit = (data>>bits) & 1;
96 		outb(bit,port);
97 		wait(nWait,port);
98 		outb(bit|2,port);
99 		wait(nWait,port);
100 		outb(bit,port);
101 		wait(nWait,port);
102 	}
103 }
104 
105 static inline void fmr2_mute(int port)
106 {
107 	outb(0x00, port);
108 	wait(4,port);
109 }
110 
111 static inline void fmr2_unmute(int port)
112 {
113 	outb(0x04, port);
114 	wait(4,port);
115 }
116 
117 static inline int fmr2_stereo_mode(int port)
118 {
119 	int n = inb(port);
120 	outb(6,port);
121 	inb(port);
122 	n = ((n>>3)&1)^1;
123 	debug_print((KERN_DEBUG "stereo: %d\n", n));
124 	return n;
125 }
126 
127 static int fmr2_product_info(struct fmr2_device *dev)
128 {
129 	int n = inb(dev->port);
130 	n &= 0xC1;
131 	if (n == 0)
132 	{
133 		/* this should support volume set */
134 		dev->card_type = 12;
135 		return 0;
136 	}
137 	/* not volume (mine is 11) */
138 	dev->card_type = (n==128)?11:0;
139 	return n;
140 }
141 
142 static inline int fmr2_getsigstr(struct fmr2_device *dev)
143 {
144 	/* !!! work only if scanning freq */
145 	int port = dev->port, res = 0xffff;
146 	outb(5,port);
147 	wait(4,port);
148 	if (!(inb(port)&1)) res = 0;
149 	debug_print((KERN_DEBUG "signal: %d\n", res));
150 	return res;
151 }
152 
153 /* set frequency and unmute card */
154 static int fmr2_setfreq(struct fmr2_device *dev)
155 {
156 	int port = dev->port;
157 	unsigned long freq = dev->curfreq;
158 
159 	fmr2_mute(port);
160 
161 	/* 0x42 for mono output
162 	 * 0x102 forward scanning
163 	 * 0x182 scansione avanti
164 	 */
165 	outbits(9,0x2,3,port);
166 	outbits(16,RSF16_ENCODE(freq),2,port);
167 
168 	fmr2_unmute(port);
169 
170 	/* wait 0.11 sec */
171 	msleep(110);
172 
173 	/* NOTE if mute this stop radio
174 	   you must set freq on unmute */
175 	dev->stereo = fmr2_stereo_mode(port);
176 	return 0;
177 }
178 
179 /* !!! not tested, in my card this does't work !!! */
180 static int fmr2_setvolume(struct fmr2_device *dev)
181 {
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];
188 
189 	if (dev->card_type != 11)
190 		return 1;
191 
192 	for (i = 12; --i >= 0; ) {
193 		a = ((n >> i) & 1) << 6; /* if (a=0) a= 0; else a= 0x40; */
194 		outb(a | 4, port);
195 		wait(4, port);
196 		outb(a | 0x24, port);
197 		wait(4, port);
198 		outb(a | 4, port);
199 		wait(4, port);
200 	}
201 	for (i = 6; --i >= 0; ) {
202 		a = ((0x18 >> i) & 1) << 6;
203 		outb(a | 4, port);
204 		wait(4,port);
205 		outb(a | 0x24, port);
206 		wait(4,port);
207 		outb(a|4, port);
208 		wait(4,port);
209 	}
210 	wait(4, port);
211 	outb(0x14, port);
212 
213 	return 0;
214 }
215 
216 static int vidioc_querycap(struct file *file, void  *priv,
217 					struct v4l2_capability *v)
218 {
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;
224 	return 0;
225 }
226 
227 static int vidioc_g_tuner(struct file *file, void *priv,
228 					struct v4l2_tuner *v)
229 {
230 	int mult;
231 	struct video_device *dev = video_devdata(file);
232 	struct fmr2_device *fmr2 = dev->priv;
233 
234 	if (v->index > 0)
235 		return -EINVAL;
236 
237 	strcpy(v->name, "FM");
238 	v->type = V4L2_TUNER_RADIO;
239 
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;
247 	mutex_lock(&lock);
248 	v->signal = fmr2_getsigstr(fmr2);
249 	mutex_unlock(&lock);
250 	return 0;
251 }
252 
253 static int vidioc_s_tuner(struct file *file, void *priv,
254 					struct v4l2_tuner *v)
255 {
256 	if (v->index > 0)
257 		return -EINVAL;
258 	return 0;
259 }
260 
261 static int vidioc_s_frequency(struct file *file, void *priv,
262 					struct v4l2_frequency *f)
263 {
264 	struct video_device *dev = video_devdata(file);
265 	struct fmr2_device *fmr2 = dev->priv;
266 
267 	if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
268 		f->frequency *= 1000;
269 	if (f->frequency < RSF16_MINFREQ ||
270 			f->frequency > RSF16_MAXFREQ )
271 		return -EINVAL;
272 	/*rounding in steps of 200 to match th freq
273 	that will be used */
274 	fmr2->curfreq = (f->frequency/200)*200;
275 
276 	/* set card freq (if not muted) */
277 	if (fmr2->curvol && !fmr2->mute) {
278 		mutex_lock(&lock);
279 		fmr2_setfreq(fmr2);
280 		mutex_unlock(&lock);
281 	}
282 	return 0;
283 }
284 
285 static int vidioc_g_frequency(struct file *file, void *priv,
286 					struct v4l2_frequency *f)
287 {
288 	struct video_device *dev = video_devdata(file);
289 	struct fmr2_device *fmr2 = dev->priv;
290 
291 	f->type = V4L2_TUNER_RADIO;
292 	f->frequency = fmr2->curfreq;
293 	if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
294 		f->frequency /= 1000;
295 	return 0;
296 }
297 
298 static int vidioc_queryctrl(struct file *file, void *priv,
299 					struct v4l2_queryctrl *qc)
300 {
301 	int i;
302 
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));
306 			return 0;
307 		}
308 	}
309 	return -EINVAL;
310 }
311 
312 static int vidioc_g_ctrl(struct file *file, void *priv,
313 					struct v4l2_control *ctrl)
314 {
315 	struct video_device *dev = video_devdata(file);
316 	struct fmr2_device *fmr2 = dev->priv;
317 
318 	switch (ctrl->id) {
319 	case V4L2_CID_AUDIO_MUTE:
320 		ctrl->value = fmr2->mute;
321 		return 0;
322 	case V4L2_CID_AUDIO_VOLUME:
323 		ctrl->value = fmr2->curvol;
324 		return 0;
325 	}
326 	return -EINVAL;
327 }
328 
329 static int vidioc_s_ctrl(struct file *file, void *priv,
330 					struct v4l2_control *ctrl)
331 {
332 	struct video_device *dev = video_devdata(file);
333 	struct fmr2_device *fmr2 = dev->priv;
334 
335 	switch (ctrl->id) {
336 	case V4L2_CID_AUDIO_MUTE:
337 		fmr2->mute = ctrl->value;
338 		break;
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;
342 		else
343 			fmr2->curvol = ctrl->value;
344 
345 		break;
346 	default:
347 		return -EINVAL;
348 	}
349 
350 #ifdef DEBUG
351 	if (fmr2->curvol && !fmr2->mute)
352 		printk(KERN_DEBUG "unmute\n");
353 	else
354 		printk(KERN_DEBUG "mute\n");
355 #endif
356 
357 	mutex_lock(&lock);
358 	if (fmr2->curvol && !fmr2->mute) {
359 		fmr2_setvolume(fmr2);
360 		/* Set frequency and unmute card */
361 		fmr2_setfreq(fmr2);
362 	} else
363 		fmr2_mute(fmr2->port);
364 	mutex_unlock(&lock);
365 	return 0;
366 }
367 
368 static int vidioc_g_audio(struct file *file, void *priv,
369 					struct v4l2_audio *a)
370 {
371 	if (a->index > 1)
372 		return -EINVAL;
373 
374 	strcpy(a->name, "Radio");
375 	a->capability = V4L2_AUDCAP_STEREO;
376 	return 0;
377 }
378 
379 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
380 {
381 	*i = 0;
382 	return 0;
383 }
384 
385 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
386 {
387 	if (i != 0)
388 		return -EINVAL;
389 	return 0;
390 }
391 
392 static int vidioc_s_audio(struct file *file, void *priv,
393 					struct v4l2_audio *a)
394 {
395 	if (a->index != 0)
396 		return -EINVAL;
397 	return 0;
398 }
399 
400 static struct fmr2_device fmr2_unit;
401 
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,
407 #ifdef CONFIG_COMPAT
408 	.compat_ioctl	= v4l_compat_ioctl32,
409 #endif
410 	.llseek         = no_llseek,
411 };
412 
413 static struct video_device fmr2_radio=
414 {
415 	.owner		= THIS_MODULE,
416 	.name		= "SF16FMR2 radio",
417 	. type		= VID_TYPE_TUNER,
418 	.fops		= &fmr2_fops,
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,
431 };
432 
433 static int __init fmr2_init(void)
434 {
435 	fmr2_unit.port = io;
436 	fmr2_unit.curvol = 0;
437 	fmr2_unit.mute = 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;
443 
444 	mutex_init(&lock);
445 
446 	if (!request_region(io, 2, "sf16fmr2")) {
447 		printk(KERN_ERR "radio-sf16fmr2: request_region failed!\n");
448 		return -EBUSY;
449 	}
450 
451 	if (video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
452 		release_region(io, 2);
453 		return -EINVAL;
454 	}
455 
456 	printk(KERN_INFO "SF16FMR2 radio card driver at 0x%x.\n", io);
457 	/* mute card - prevents noisy bootups */
458 	mutex_lock(&lock);
459 	fmr2_mute(io);
460 	fmr2_product_info(&fmr2_unit);
461 	mutex_unlock(&lock);
462 	debug_print((KERN_DEBUG "card_type %d\n", fmr2_unit.card_type));
463 
464 	/* Only card_type == 11 implements volume */
465 	if (fmr2_unit.card_type != 11)
466 		radio_qctrl[AUD_VOL_INDEX].maximum = 1;
467 
468 	return 0;
469 }
470 
471 MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
472 MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
473 MODULE_LICENSE("GPL");
474 
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);
478 
479 static void __exit fmr2_cleanup_module(void)
480 {
481 	video_unregister_device(&fmr2_radio);
482 	release_region(io,2);
483 }
484 
485 module_init(fmr2_init);
486 module_exit(fmr2_cleanup_module);
487 
488 #ifndef MODULE
489 
490 static int __init fmr2_setup_io(char *str)
491 {
492 	get_option(&str, &io);
493 	return 1;
494 }
495 
496 __setup("sf16fmr2=", fmr2_setup_io);
497 
498 #endif
499