xref: /linux/drivers/media/radio/radio-sf16fmi.c (revision 5e8d780d745c1619aba81fe7166c5a4b5cad2b84)
1 /* SF16FMI 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.cox@linux.org>
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 SF16FMI
15  *
16  */
17 
18 #include <linux/kernel.h>	/* __setup			*/
19 #include <linux/module.h>	/* Modules 			*/
20 #include <linux/init.h>		/* Initdata			*/
21 #include <linux/ioport.h>	/* request_region		*/
22 #include <linux/delay.h>	/* udelay			*/
23 #include <linux/videodev.h>	/* kernel radio structs		*/
24 #include <media/v4l2-common.h>
25 #include <linux/isapnp.h>
26 #include <asm/io.h>		/* outb, outb_p			*/
27 #include <asm/uaccess.h>	/* copy to/from user		*/
28 #include <linux/mutex.h>
29 
30 struct fmi_device
31 {
32 	int port;
33 	int curvol; /* 1 or 0 */
34 	unsigned long curfreq; /* freq in kHz */
35 	__u32 flags;
36 };
37 
38 static int io = -1;
39 static int radio_nr = -1;
40 static struct pnp_dev *dev = NULL;
41 static struct mutex lock;
42 
43 /* freq is in 1/16 kHz to internal number, hw precision is 50 kHz */
44 /* It is only useful to give freq in intervall of 800 (=0.05Mhz),
45  * other bits will be truncated, e.g 92.7400016 -> 92.7, but
46  * 92.7400017 -> 92.75
47  */
48 #define RSF16_ENCODE(x)	((x)/800+214)
49 #define RSF16_MINFREQ 87*16000
50 #define RSF16_MAXFREQ 108*16000
51 
52 static void outbits(int bits, unsigned int data, int port)
53 {
54 	while(bits--) {
55 		if(data & 1) {
56 			outb(5, port);
57 			udelay(6);
58 			outb(7, port);
59 			udelay(6);
60 		} else {
61 			outb(1, port);
62 			udelay(6);
63 			outb(3, port);
64 			udelay(6);
65 		}
66 		data>>=1;
67 	}
68 }
69 
70 static inline void fmi_mute(int port)
71 {
72 	mutex_lock(&lock);
73 	outb(0x00, port);
74 	mutex_unlock(&lock);
75 }
76 
77 static inline void fmi_unmute(int port)
78 {
79 	mutex_lock(&lock);
80 	outb(0x08, port);
81 	mutex_unlock(&lock);
82 }
83 
84 static inline int fmi_setfreq(struct fmi_device *dev)
85 {
86 	int myport = dev->port;
87 	unsigned long freq = dev->curfreq;
88 
89 	mutex_lock(&lock);
90 
91 	outbits(16, RSF16_ENCODE(freq), myport);
92 	outbits(8, 0xC0, myport);
93 	msleep(143);		/* was schedule_timeout(HZ/7) */
94 	mutex_unlock(&lock);
95 	if (dev->curvol) fmi_unmute(myport);
96 	return 0;
97 }
98 
99 static inline int fmi_getsigstr(struct fmi_device *dev)
100 {
101 	int val;
102 	int res;
103 	int myport = dev->port;
104 
105 
106 	mutex_lock(&lock);
107 	val = dev->curvol ? 0x08 : 0x00;	/* unmute/mute */
108 	outb(val, myport);
109 	outb(val | 0x10, myport);
110 	msleep(143); 		/* was schedule_timeout(HZ/7) */
111 	res = (int)inb(myport+1);
112 	outb(val, myport);
113 
114 	mutex_unlock(&lock);
115 	return (res & 2) ? 0 : 0xFFFF;
116 }
117 
118 static int fmi_do_ioctl(struct inode *inode, struct file *file,
119 			unsigned int cmd, void *arg)
120 {
121 	struct video_device *dev = video_devdata(file);
122 	struct fmi_device *fmi=dev->priv;
123 
124 	switch(cmd)
125 	{
126 		case VIDIOCGCAP:
127 		{
128 			struct video_capability *v = arg;
129 			memset(v,0,sizeof(*v));
130 			strcpy(v->name, "SF16-FMx radio");
131 			v->type=VID_TYPE_TUNER;
132 			v->channels=1;
133 			v->audios=1;
134 			return 0;
135 		}
136 		case VIDIOCGTUNER:
137 		{
138 			struct video_tuner *v = arg;
139 			int mult;
140 
141 			if(v->tuner)	/* Only 1 tuner */
142 				return -EINVAL;
143 			strcpy(v->name, "FM");
144 			mult = (fmi->flags & VIDEO_TUNER_LOW) ? 1 : 1000;
145 			v->rangelow = RSF16_MINFREQ/mult;
146 			v->rangehigh = RSF16_MAXFREQ/mult;
147 			v->flags=fmi->flags;
148 			v->mode=VIDEO_MODE_AUTO;
149 			v->signal = fmi_getsigstr(fmi);
150 			return 0;
151 		}
152 		case VIDIOCSTUNER:
153 		{
154 			struct video_tuner *v = arg;
155 			if(v->tuner!=0)
156 				return -EINVAL;
157 			fmi->flags = v->flags & VIDEO_TUNER_LOW;
158 			/* Only 1 tuner so no setting needed ! */
159 			return 0;
160 		}
161 		case VIDIOCGFREQ:
162 		{
163 			unsigned long *freq = arg;
164 			*freq = fmi->curfreq;
165 			if (!(fmi->flags & VIDEO_TUNER_LOW))
166 			    *freq /= 1000;
167 			return 0;
168 		}
169 		case VIDIOCSFREQ:
170 		{
171 			unsigned long *freq = arg;
172 			if (!(fmi->flags & VIDEO_TUNER_LOW))
173 				*freq *= 1000;
174 			if (*freq < RSF16_MINFREQ || *freq > RSF16_MAXFREQ )
175 				return -EINVAL;
176 			/*rounding in steps of 800 to match th freq
177 			  that will be used */
178 			fmi->curfreq = (*freq/800)*800;
179 			fmi_setfreq(fmi);
180 			return 0;
181 		}
182 		case VIDIOCGAUDIO:
183 		{
184 			struct video_audio *v = arg;
185 			memset(v,0,sizeof(*v));
186 			v->flags=( (!fmi->curvol)*VIDEO_AUDIO_MUTE | VIDEO_AUDIO_MUTABLE);
187 			strcpy(v->name, "Radio");
188 			v->mode=VIDEO_SOUND_STEREO;
189 			return 0;
190 		}
191 		case VIDIOCSAUDIO:
192 		{
193 			struct video_audio *v = arg;
194 			if(v->audio)
195 				return -EINVAL;
196 			fmi->curvol= v->flags&VIDEO_AUDIO_MUTE ? 0 : 1;
197 			fmi->curvol ?
198 				fmi_unmute(fmi->port) : fmi_mute(fmi->port);
199 			return 0;
200 		}
201 		case VIDIOCGUNIT:
202 		{
203 			struct video_unit *v = arg;
204 			v->video=VIDEO_NO_UNIT;
205 			v->vbi=VIDEO_NO_UNIT;
206 			v->radio=dev->minor;
207 			v->audio=0; /* How do we find out this??? */
208 			v->teletext=VIDEO_NO_UNIT;
209 			return 0;
210 		}
211 		default:
212 			return -ENOIOCTLCMD;
213 	}
214 }
215 
216 static int fmi_ioctl(struct inode *inode, struct file *file,
217 		     unsigned int cmd, unsigned long arg)
218 {
219 	return video_usercopy(inode, file, cmd, arg, fmi_do_ioctl);
220 }
221 
222 static struct fmi_device fmi_unit;
223 
224 static struct file_operations fmi_fops = {
225 	.owner		= THIS_MODULE,
226 	.open           = video_exclusive_open,
227 	.release        = video_exclusive_release,
228 	.ioctl		= fmi_ioctl,
229 	.compat_ioctl	= v4l_compat_ioctl32,
230 	.llseek         = no_llseek,
231 };
232 
233 static struct video_device fmi_radio=
234 {
235 	.owner		= THIS_MODULE,
236 	.name		= "SF16FMx radio",
237 	.type		= VID_TYPE_TUNER,
238 	.hardware	= VID_HARDWARE_SF16MI,
239 	.fops           = &fmi_fops,
240 };
241 
242 /* ladis: this is my card. does any other types exist? */
243 static struct isapnp_device_id id_table[] __devinitdata = {
244 	{	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
245 		ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
246 	{	ISAPNP_CARD_END, },
247 };
248 
249 MODULE_DEVICE_TABLE(isapnp, id_table);
250 
251 static int isapnp_fmi_probe(void)
252 {
253 	int i = 0;
254 
255 	while (id_table[i].card_vendor != 0 && dev == NULL) {
256 		dev = pnp_find_dev(NULL, id_table[i].vendor,
257 				   id_table[i].function, NULL);
258 		i++;
259 	}
260 
261 	if (!dev)
262 		return -ENODEV;
263 	if (pnp_device_attach(dev) < 0)
264 		return -EAGAIN;
265 	if (pnp_activate_dev(dev) < 0) {
266 		printk ("radio-sf16fmi: PnP configure failed (out of resources?)\n");
267 		pnp_device_detach(dev);
268 		return -ENOMEM;
269 	}
270 	if (!pnp_port_valid(dev, 0)) {
271 		pnp_device_detach(dev);
272 		return -ENODEV;
273 	}
274 
275 	i = pnp_port_start(dev, 0);
276 	printk ("radio-sf16fmi: PnP reports card at %#x\n", i);
277 
278 	return i;
279 }
280 
281 static int __init fmi_init(void)
282 {
283 	if (io < 0)
284 		io = isapnp_fmi_probe();
285 	if (io < 0) {
286 		printk(KERN_ERR "radio-sf16fmi: No PnP card found.\n");
287 		return io;
288 	}
289 	if (!request_region(io, 2, "radio-sf16fmi")) {
290 		printk(KERN_ERR "radio-sf16fmi: port 0x%x already in use\n", io);
291 		return -EBUSY;
292 	}
293 
294 	fmi_unit.port = io;
295 	fmi_unit.curvol = 0;
296 	fmi_unit.curfreq = 0;
297 	fmi_unit.flags = VIDEO_TUNER_LOW;
298 	fmi_radio.priv = &fmi_unit;
299 
300 	mutex_init(&lock);
301 
302 	if (video_register_device(&fmi_radio, VFL_TYPE_RADIO, radio_nr) == -1) {
303 		release_region(io, 2);
304 		return -EINVAL;
305 	}
306 
307 	printk(KERN_INFO "SF16FMx radio card driver at 0x%x\n", io);
308 	/* mute card - prevents noisy bootups */
309 	fmi_mute(io);
310 	return 0;
311 }
312 
313 MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood");
314 MODULE_DESCRIPTION("A driver for the SF16MI radio.");
315 MODULE_LICENSE("GPL");
316 
317 module_param(io, int, 0);
318 MODULE_PARM_DESC(io, "I/O address of the SF16MI card (0x284 or 0x384)");
319 module_param(radio_nr, int, 0);
320 
321 static void __exit fmi_cleanup_module(void)
322 {
323 	video_unregister_device(&fmi_radio);
324 	release_region(io, 2);
325 	if (dev)
326 		pnp_device_detach(dev);
327 }
328 
329 module_init(fmi_init);
330 module_exit(fmi_cleanup_module);
331