xref: /linux/drivers/media/radio/radio-sf16fmr2.c (revision 0a1340c185734a57fbf4775927966ad4a1347b02)
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 
15 #include <linux/module.h>	/* Modules 			*/
16 #include <linux/init.h>		/* Initdata			*/
17 #include <linux/ioport.h>	/* request_region		*/
18 #include <linux/delay.h>	/* udelay			*/
19 #include <asm/io.h>		/* outb, outb_p			*/
20 #include <asm/uaccess.h>	/* copy to/from user		*/
21 #include <linux/videodev.h>	/* kernel radio structs		*/
22 #include <media/v4l2-common.h>
23 #include <linux/mutex.h>
24 
25 static struct mutex lock;
26 
27 #undef DEBUG
28 //#define DEBUG 1
29 
30 #ifdef DEBUG
31 # define  debug_print(s) printk s
32 #else
33 # define  debug_print(s)
34 #endif
35 
36 /* this should be static vars for module size */
37 struct fmr2_device
38 {
39 	int port;
40 	int curvol; /* 0-65535, if not volume 0 or 65535 */
41 	int mute;
42 	int stereo; /* card is producing stereo audio */
43 	unsigned long curfreq; /* freq in kHz */
44 	int card_type;
45 	__u32 flags;
46 };
47 
48 static int io = 0x384;
49 static int radio_nr = -1;
50 
51 /* hw precision is 12.5 kHz
52  * It is only useful to give freq in intervall of 200 (=0.0125Mhz),
53  * other bits will be truncated
54  */
55 #define RSF16_ENCODE(x)	((x)/200+856)
56 #define RSF16_MINFREQ 87*16000
57 #define RSF16_MAXFREQ 108*16000
58 
59 static inline void wait(int n,int port)
60 {
61 	for (;n;--n) inb(port);
62 }
63 
64 static void outbits(int bits, unsigned int data, int nWait, int port)
65 {
66 	int bit;
67 	for(;--bits>=0;) {
68 		bit = (data>>bits) & 1;
69 		outb(bit,port);
70 		wait(nWait,port);
71 		outb(bit|2,port);
72 		wait(nWait,port);
73 		outb(bit,port);
74 		wait(nWait,port);
75 	}
76 }
77 
78 static inline void fmr2_mute(int port)
79 {
80 	outb(0x00, port);
81 	wait(4,port);
82 }
83 
84 static inline void fmr2_unmute(int port)
85 {
86 	outb(0x04, port);
87 	wait(4,port);
88 }
89 
90 static inline int fmr2_stereo_mode(int port)
91 {
92 	int n = inb(port);
93 	outb(6,port);
94 	inb(port);
95 	n = ((n>>3)&1)^1;
96 	debug_print((KERN_DEBUG "stereo: %d\n", n));
97 	return n;
98 }
99 
100 static int fmr2_product_info(struct fmr2_device *dev)
101 {
102 	int n = inb(dev->port);
103 	n &= 0xC1;
104 	if (n == 0)
105 	{
106 		/* this should support volume set */
107 		dev->card_type = 12;
108 		return 0;
109 	}
110 	/* not volume (mine is 11) */
111 	dev->card_type = (n==128)?11:0;
112 	return n;
113 }
114 
115 static inline int fmr2_getsigstr(struct fmr2_device *dev)
116 {
117 	/* !!! work only if scanning freq */
118 	int port = dev->port, res = 0xffff;
119 	outb(5,port);
120 	wait(4,port);
121 	if (!(inb(port)&1)) res = 0;
122 	debug_print((KERN_DEBUG "signal: %d\n", res));
123 	return res;
124 }
125 
126 /* set frequency and unmute card */
127 static int fmr2_setfreq(struct fmr2_device *dev)
128 {
129 	int port = dev->port;
130 	unsigned long freq = dev->curfreq;
131 
132 	fmr2_mute(port);
133 
134 	/* 0x42 for mono output
135 	 * 0x102 forward scanning
136 	 * 0x182 scansione avanti
137 	 */
138 	outbits(9,0x2,3,port);
139 	outbits(16,RSF16_ENCODE(freq),2,port);
140 
141 	fmr2_unmute(port);
142 
143 	/* wait 0.11 sec */
144 	msleep(110);
145 
146 	/* NOTE if mute this stop radio
147 	   you must set freq on unmute */
148 	dev->stereo = fmr2_stereo_mode(port);
149 	return 0;
150 }
151 
152 /* !!! not tested, in my card this does't work !!! */
153 static int fmr2_setvolume(struct fmr2_device *dev)
154 {
155 	int i,a,n, port = dev->port;
156 
157 	if (dev->card_type != 11) return 1;
158 
159 	switch( (dev->curvol+(1<<11)) >> 12 )
160 	{
161 	case 0: case 1: n = 0x21; break;
162 	case 2: n = 0x84; break;
163 	case 3: n = 0x90; break;
164 	case 4: n = 0x104; break;
165 	case 5: n = 0x110; break;
166 	case 6: n = 0x204; break;
167 	case 7: n = 0x210; break;
168 	case 8: n = 0x402; break;
169 	case 9: n = 0x404; break;
170 	default:
171 	case 10: n = 0x408; break;
172 	case 11: n = 0x410; break;
173 	case 12: n = 0x801; break;
174 	case 13: n = 0x802; break;
175 	case 14: n = 0x804; break;
176 	case 15: n = 0x808; break;
177 	case 16: n = 0x810; break;
178 	}
179 	for(i=12;--i>=0;)
180 	{
181 		a = ((n >> i) & 1) << 6; /* if (a=0) a= 0; else a= 0x40; */
182 		outb(a|4, port);
183 		wait(4,port);
184 		outb(a|0x24, port);
185 		wait(4,port);
186 		outb(a|4, port);
187 		wait(4,port);
188 	}
189 	for(i=6;--i>=0;)
190 	{
191 		a = ((0x18 >> i) & 1) << 6;
192 		outb(a|4, port);
193 		wait(4,port);
194 		outb(a|0x24, port);
195 		wait(4,port);
196 		outb(a|4, port);
197 		wait(4,port);
198 	}
199 	wait(4,port);
200 	outb(0x14, port);
201 
202 	return 0;
203 }
204 
205 static int fmr2_do_ioctl(struct inode *inode, struct file *file,
206 		      unsigned int cmd, void *arg)
207 {
208 	struct video_device *dev = video_devdata(file);
209 	struct fmr2_device *fmr2 = dev->priv;
210 	debug_print((KERN_DEBUG "freq %ld flags %d vol %d mute %d "
211 		"stereo %d type %d\n",
212 		fmr2->curfreq, fmr2->flags, fmr2->curvol, fmr2->mute,
213 		fmr2->stereo, fmr2->card_type));
214 
215 	switch(cmd)
216 	{
217 		case VIDIOCGCAP:
218 		{
219 			struct video_capability *v = arg;
220 			memset(v,0,sizeof(*v));
221 			strcpy(v->name, "SF16-FMR2 radio");
222 			v->type=VID_TYPE_TUNER;
223 			v->channels=1;
224 			v->audios=1;
225 			return 0;
226 		}
227 		case VIDIOCGTUNER:
228 		{
229 			struct video_tuner *v = arg;
230 			int mult;
231 
232 			if(v->tuner)     /* Only 1 tuner */
233 				return -EINVAL;
234 			strcpy(v->name, "FM");
235 			mult = (fmr2->flags & VIDEO_TUNER_LOW) ? 1 : 1000;
236 			v->rangelow = RSF16_MINFREQ/mult;
237 			v->rangehigh = RSF16_MAXFREQ/mult;
238 			v->flags = fmr2->flags | VIDEO_AUDIO_MUTABLE;
239 			if (fmr2->mute)
240 				v->flags |= VIDEO_AUDIO_MUTE;
241 			v->mode=VIDEO_MODE_AUTO;
242 			mutex_lock(&lock);
243 			v->signal = fmr2_getsigstr(fmr2);
244 			mutex_unlock(&lock);
245 			return 0;
246 		}
247 		case VIDIOCSTUNER:
248 		{
249 			struct video_tuner *v = arg;
250 			if (v->tuner!=0)
251 				return -EINVAL;
252 			fmr2->flags = v->flags & VIDEO_TUNER_LOW;
253 			return 0;
254 		}
255 		case VIDIOCGFREQ:
256 		{
257 			unsigned long *freq = arg;
258 			*freq = fmr2->curfreq;
259 			if (!(fmr2->flags & VIDEO_TUNER_LOW))
260 				*freq /= 1000;
261 			return 0;
262 		}
263 		case VIDIOCSFREQ:
264 		{
265 			unsigned long *freq = arg;
266 			if (!(fmr2->flags & VIDEO_TUNER_LOW))
267 				*freq *= 1000;
268 			if ( *freq < RSF16_MINFREQ || *freq > RSF16_MAXFREQ )
269 				return -EINVAL;
270 			/* rounding in steps of 200 to match th freq
271 			 * that will be used
272 			 */
273 			fmr2->curfreq = (*freq/200)*200;
274 
275 			/* set card freq (if not muted) */
276 			if (fmr2->curvol && !fmr2->mute)
277 			{
278 				mutex_lock(&lock);
279 				fmr2_setfreq(fmr2);
280 				mutex_unlock(&lock);
281 			}
282 			return 0;
283 		}
284 		case VIDIOCGAUDIO:
285 		{
286 			struct video_audio *v = arg;
287 			memset(v,0,sizeof(*v));
288 			/* !!! do not return VIDEO_AUDIO_MUTE */
289 			v->flags = VIDEO_AUDIO_MUTABLE;
290 			strcpy(v->name, "Radio");
291 			/* get current stereo mode */
292 			v->mode = fmr2->stereo ? VIDEO_SOUND_STEREO: VIDEO_SOUND_MONO;
293 			/* volume supported ? */
294 			if (fmr2->card_type == 11)
295 			{
296 				v->flags |= VIDEO_AUDIO_VOLUME;
297 				v->step = 1 << 12;
298 				v->volume = fmr2->curvol;
299 			}
300 			debug_print((KERN_DEBUG "Get flags %d vol %d\n", v->flags, v->volume));
301 			return 0;
302 		}
303 		case VIDIOCSAUDIO:
304 		{
305 			struct video_audio *v = arg;
306 			if(v->audio)
307 				return -EINVAL;
308 			debug_print((KERN_DEBUG "Set flags %d vol %d\n", v->flags, v->volume));
309 			/* set volume */
310 			if (v->flags & VIDEO_AUDIO_VOLUME)
311 				fmr2->curvol = v->volume; /* !!! set with precision */
312 			if (fmr2->card_type != 11) fmr2->curvol = 65535;
313 			fmr2->mute = 0;
314 			if (v->flags & VIDEO_AUDIO_MUTE)
315 				fmr2->mute = 1;
316 #ifdef DEBUG
317 			if (fmr2->curvol && !fmr2->mute)
318 				printk(KERN_DEBUG "unmute\n");
319 			else
320 				printk(KERN_DEBUG "mute\n");
321 #endif
322 			mutex_lock(&lock);
323 			if (fmr2->curvol && !fmr2->mute)
324 			{
325 				fmr2_setvolume(fmr2);
326 				fmr2_setfreq(fmr2);
327 			}
328 			else fmr2_mute(fmr2->port);
329 			mutex_unlock(&lock);
330 			return 0;
331 		}
332 		case VIDIOCGUNIT:
333 		{
334 			struct video_unit *v = arg;
335 			v->video=VIDEO_NO_UNIT;
336 			v->vbi=VIDEO_NO_UNIT;
337 			v->radio=dev->minor;
338 			v->audio=0; /* How do we find out this??? */
339 			v->teletext=VIDEO_NO_UNIT;
340 			return 0;
341 		}
342 		default:
343 			return -ENOIOCTLCMD;
344 	}
345 }
346 
347 static int fmr2_ioctl(struct inode *inode, struct file *file,
348 		      unsigned int cmd, unsigned long arg)
349  {
350 	return video_usercopy(inode, file, cmd, arg, fmr2_do_ioctl);
351 }
352 
353 static struct fmr2_device fmr2_unit;
354 
355 static struct file_operations fmr2_fops = {
356 	.owner          = THIS_MODULE,
357 	.open           = video_exclusive_open,
358 	.release        = video_exclusive_release,
359 	.ioctl          = fmr2_ioctl,
360 	.compat_ioctl	= v4l_compat_ioctl32,
361 	.llseek         = no_llseek,
362 };
363 
364 static struct video_device fmr2_radio=
365 {
366 	.owner		= THIS_MODULE,
367 	.name		= "SF16FMR2 radio",
368 	. type		= VID_TYPE_TUNER,
369 	.hardware	= VID_HARDWARE_SF16FMR2,
370 	.fops		= &fmr2_fops,
371 };
372 
373 static int __init fmr2_init(void)
374 {
375 	fmr2_unit.port = io;
376 	fmr2_unit.curvol = 0;
377 	fmr2_unit.mute = 0;
378 	fmr2_unit.curfreq = 0;
379 	fmr2_unit.stereo = 1;
380 	fmr2_unit.flags = VIDEO_TUNER_LOW;
381 	fmr2_unit.card_type = 0;
382 	fmr2_radio.priv = &fmr2_unit;
383 
384 	mutex_init(&lock);
385 
386 	if (request_region(io, 2, "sf16fmr2"))
387 	{
388 		printk(KERN_ERR "fmr2: port 0x%x already in use\n", io);
389 		return -EBUSY;
390 	}
391 
392 	if(video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr)==-1)
393 	{
394 		release_region(io, 2);
395 		return -EINVAL;
396 	}
397 
398 	printk(KERN_INFO "SF16FMR2 radio card driver at 0x%x.\n", io);
399 	debug_print((KERN_DEBUG "Mute %d Low %d\n",VIDEO_AUDIO_MUTE,VIDEO_TUNER_LOW));
400 	/* mute card - prevents noisy bootups */
401 	mutex_lock(&lock);
402 	fmr2_mute(io);
403 	fmr2_product_info(&fmr2_unit);
404 	mutex_unlock(&lock);
405 	debug_print((KERN_DEBUG "card_type %d\n", fmr2_unit.card_type));
406 	return 0;
407 }
408 
409 MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
410 MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
411 MODULE_LICENSE("GPL");
412 
413 module_param(io, int, 0);
414 MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
415 module_param(radio_nr, int, 0);
416 
417 static void __exit fmr2_cleanup_module(void)
418 {
419 	video_unregister_device(&fmr2_radio);
420 	release_region(io,2);
421 }
422 
423 module_init(fmr2_init);
424 module_exit(fmr2_cleanup_module);
425 
426 #ifndef MODULE
427 
428 static int __init fmr2_setup_io(char *str)
429 {
430 	get_option(&str, &io);
431 	return 1;
432 }
433 
434 __setup("sf16fmr2=", fmr2_setup_io);
435 
436 #endif
437