spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / drivers / media / radio / radio-sf16fmi.c
blob22c5743bf9db1d77a58bfa77949f14103892bcb8
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
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>
10 * Notes on the hardware
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
16 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
19 #include <linux/kernel.h> /* __setup */
20 #include <linux/module.h> /* Modules */
21 #include <linux/init.h> /* Initdata */
22 #include <linux/ioport.h> /* request_region */
23 #include <linux/delay.h> /* udelay */
24 #include <linux/isapnp.h>
25 #include <linux/mutex.h>
26 #include <linux/videodev2.h> /* kernel radio structs */
27 #include <linux/io.h> /* outb, outb_p */
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ioctl.h>
31 MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood");
32 MODULE_DESCRIPTION("A driver for the SF16-FMI and SF16-FMP radio.");
33 MODULE_LICENSE("GPL");
34 MODULE_VERSION("0.0.3");
36 static int io = -1;
37 static int radio_nr = -1;
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);
43 struct fmi
45 struct v4l2_device v4l2_dev;
46 struct video_device vdev;
47 int io;
48 bool mute;
49 unsigned long curfreq; /* freq in kHz */
50 struct mutex lock;
53 static struct fmi fmi_card;
54 static struct pnp_dev *dev;
55 bool pnp_attached;
57 /* freq is in 1/16 kHz to internal number, hw precision is 50 kHz */
58 /* It is only useful to give freq in interval of 800 (=0.05Mhz),
59 * other bits will be truncated, e.g 92.7400016 -> 92.7, but
60 * 92.7400017 -> 92.75
62 #define RSF16_ENCODE(x) ((x) / 800 + 214)
63 #define RSF16_MINFREQ (87 * 16000)
64 #define RSF16_MAXFREQ (108 * 16000)
66 static void outbits(int bits, unsigned int data, int io)
68 while (bits--) {
69 if (data & 1) {
70 outb(5, io);
71 udelay(6);
72 outb(7, io);
73 udelay(6);
74 } else {
75 outb(1, io);
76 udelay(6);
77 outb(3, io);
78 udelay(6);
80 data >>= 1;
84 static inline void fmi_mute(struct fmi *fmi)
86 mutex_lock(&fmi->lock);
87 outb(0x00, fmi->io);
88 mutex_unlock(&fmi->lock);
91 static inline void fmi_unmute(struct fmi *fmi)
93 mutex_lock(&fmi->lock);
94 outb(0x08, fmi->io);
95 mutex_unlock(&fmi->lock);
98 static inline int fmi_setfreq(struct fmi *fmi, unsigned long freq)
100 mutex_lock(&fmi->lock);
101 fmi->curfreq = freq;
103 outbits(16, RSF16_ENCODE(freq), fmi->io);
104 outbits(8, 0xC0, fmi->io);
105 msleep(143); /* was schedule_timeout(HZ/7) */
106 mutex_unlock(&fmi->lock);
107 if (!fmi->mute)
108 fmi_unmute(fmi);
109 return 0;
112 static inline int fmi_getsigstr(struct fmi *fmi)
114 int val;
115 int res;
117 mutex_lock(&fmi->lock);
118 val = fmi->mute ? 0x00 : 0x08; /* mute/unmute */
119 outb(val, fmi->io);
120 outb(val | 0x10, fmi->io);
121 msleep(143); /* was schedule_timeout(HZ/7) */
122 res = (int)inb(fmi->io + 1);
123 outb(val, fmi->io);
125 mutex_unlock(&fmi->lock);
126 return (res & 2) ? 0 : 0xFFFF;
129 static int vidioc_querycap(struct file *file, void *priv,
130 struct v4l2_capability *v)
132 strlcpy(v->driver, "radio-sf16fmi", sizeof(v->driver));
133 strlcpy(v->card, "SF16-FMx radio", sizeof(v->card));
134 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
135 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
136 return 0;
139 static int vidioc_g_tuner(struct file *file, void *priv,
140 struct v4l2_tuner *v)
142 struct fmi *fmi = video_drvdata(file);
144 if (v->index > 0)
145 return -EINVAL;
147 strlcpy(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);
155 return 0;
158 static int vidioc_s_tuner(struct file *file, void *priv,
159 struct v4l2_tuner *v)
161 return v->index ? -EINVAL : 0;
164 static int vidioc_s_frequency(struct file *file, void *priv,
165 struct v4l2_frequency *f)
167 struct fmi *fmi = video_drvdata(file);
169 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
170 return -EINVAL;
171 if (f->frequency < RSF16_MINFREQ ||
172 f->frequency > RSF16_MAXFREQ)
173 return -EINVAL;
174 /* rounding in steps of 800 to match the freq
175 that will be used */
176 fmi_setfreq(fmi, (f->frequency / 800) * 800);
177 return 0;
180 static int vidioc_g_frequency(struct file *file, void *priv,
181 struct v4l2_frequency *f)
183 struct fmi *fmi = video_drvdata(file);
185 if (f->tuner != 0)
186 return -EINVAL;
187 f->type = V4L2_TUNER_RADIO;
188 f->frequency = fmi->curfreq;
189 return 0;
192 static int vidioc_queryctrl(struct file *file, void *priv,
193 struct v4l2_queryctrl *qc)
195 switch (qc->id) {
196 case V4L2_CID_AUDIO_MUTE:
197 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
199 return -EINVAL;
202 static int vidioc_g_ctrl(struct file *file, void *priv,
203 struct v4l2_control *ctrl)
205 struct fmi *fmi = video_drvdata(file);
207 switch (ctrl->id) {
208 case V4L2_CID_AUDIO_MUTE:
209 ctrl->value = fmi->mute;
210 return 0;
212 return -EINVAL;
215 static int vidioc_s_ctrl(struct file *file, void *priv,
216 struct v4l2_control *ctrl)
218 struct fmi *fmi = video_drvdata(file);
220 switch (ctrl->id) {
221 case V4L2_CID_AUDIO_MUTE:
222 if (ctrl->value)
223 fmi_mute(fmi);
224 else
225 fmi_unmute(fmi);
226 fmi->mute = ctrl->value;
227 return 0;
229 return -EINVAL;
232 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
234 *i = 0;
235 return 0;
238 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
240 return i ? -EINVAL : 0;
243 static int vidioc_g_audio(struct file *file, void *priv,
244 struct v4l2_audio *a)
246 a->index = 0;
247 strlcpy(a->name, "Radio", sizeof(a->name));
248 a->capability = V4L2_AUDCAP_STEREO;
249 return 0;
252 static int vidioc_s_audio(struct file *file, void *priv,
253 struct v4l2_audio *a)
255 return a->index ? -EINVAL : 0;
258 static const struct v4l2_file_operations fmi_fops = {
259 .owner = THIS_MODULE,
260 .unlocked_ioctl = video_ioctl2,
263 static const struct v4l2_ioctl_ops fmi_ioctl_ops = {
264 .vidioc_querycap = vidioc_querycap,
265 .vidioc_g_tuner = vidioc_g_tuner,
266 .vidioc_s_tuner = vidioc_s_tuner,
267 .vidioc_g_audio = vidioc_g_audio,
268 .vidioc_s_audio = vidioc_s_audio,
269 .vidioc_g_input = vidioc_g_input,
270 .vidioc_s_input = vidioc_s_input,
271 .vidioc_g_frequency = vidioc_g_frequency,
272 .vidioc_s_frequency = vidioc_s_frequency,
273 .vidioc_queryctrl = vidioc_queryctrl,
274 .vidioc_g_ctrl = vidioc_g_ctrl,
275 .vidioc_s_ctrl = vidioc_s_ctrl,
278 /* ladis: this is my card. does any other types exist? */
279 static struct isapnp_device_id id_table[] __devinitdata = {
280 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
281 ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
282 { ISAPNP_CARD_END, },
285 MODULE_DEVICE_TABLE(isapnp, id_table);
287 static int __init isapnp_fmi_probe(void)
289 int i = 0;
291 while (id_table[i].card_vendor != 0 && dev == NULL) {
292 dev = pnp_find_dev(NULL, id_table[i].vendor,
293 id_table[i].function, NULL);
294 i++;
297 if (!dev)
298 return -ENODEV;
299 if (pnp_device_attach(dev) < 0)
300 return -EAGAIN;
301 if (pnp_activate_dev(dev) < 0) {
302 printk(KERN_ERR "radio-sf16fmi: PnP configure failed (out of resources?)\n");
303 pnp_device_detach(dev);
304 return -ENOMEM;
306 if (!pnp_port_valid(dev, 0)) {
307 pnp_device_detach(dev);
308 return -ENODEV;
311 i = pnp_port_start(dev, 0);
312 printk(KERN_INFO "radio-sf16fmi: PnP reports card at %#x\n", i);
314 return i;
317 static int __init fmi_init(void)
319 struct fmi *fmi = &fmi_card;
320 struct v4l2_device *v4l2_dev = &fmi->v4l2_dev;
321 int res, i;
322 int probe_ports[] = { 0, 0x284, 0x384 };
324 if (io < 0) {
325 for (i = 0; i < ARRAY_SIZE(probe_ports); i++) {
326 io = probe_ports[i];
327 if (io == 0) {
328 io = isapnp_fmi_probe();
329 if (io < 0)
330 continue;
331 pnp_attached = 1;
333 if (!request_region(io, 2, "radio-sf16fmi")) {
334 if (pnp_attached)
335 pnp_device_detach(dev);
336 io = -1;
337 continue;
339 if (pnp_attached ||
340 ((inb(io) & 0xf9) == 0xf9 && (inb(io) & 0x4) == 0))
341 break;
342 release_region(io, 2);
343 io = -1;
345 } else {
346 if (!request_region(io, 2, "radio-sf16fmi")) {
347 printk(KERN_ERR "radio-sf16fmi: port %#x already in use\n", io);
348 return -EBUSY;
350 if (inb(io) == 0xff) {
351 printk(KERN_ERR "radio-sf16fmi: card not present at %#x\n", io);
352 release_region(io, 2);
353 return -ENODEV;
356 if (io < 0) {
357 printk(KERN_ERR "radio-sf16fmi: no cards found\n");
358 return -ENODEV;
361 strlcpy(v4l2_dev->name, "sf16fmi", sizeof(v4l2_dev->name));
362 fmi->io = io;
364 res = v4l2_device_register(NULL, v4l2_dev);
365 if (res < 0) {
366 release_region(fmi->io, 2);
367 if (pnp_attached)
368 pnp_device_detach(dev);
369 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
370 return res;
373 strlcpy(fmi->vdev.name, v4l2_dev->name, sizeof(fmi->vdev.name));
374 fmi->vdev.v4l2_dev = v4l2_dev;
375 fmi->vdev.fops = &fmi_fops;
376 fmi->vdev.ioctl_ops = &fmi_ioctl_ops;
377 fmi->vdev.release = video_device_release_empty;
378 video_set_drvdata(&fmi->vdev, fmi);
380 mutex_init(&fmi->lock);
382 /* mute card - prevents noisy bootups */
383 fmi_mute(fmi);
385 if (video_register_device(&fmi->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
386 v4l2_device_unregister(v4l2_dev);
387 release_region(fmi->io, 2);
388 if (pnp_attached)
389 pnp_device_detach(dev);
390 return -EINVAL;
393 v4l2_info(v4l2_dev, "card driver at 0x%x\n", fmi->io);
394 return 0;
397 static void __exit fmi_exit(void)
399 struct fmi *fmi = &fmi_card;
401 video_unregister_device(&fmi->vdev);
402 v4l2_device_unregister(&fmi->v4l2_dev);
403 release_region(fmi->io, 2);
404 if (dev && pnp_attached)
405 pnp_device_detach(dev);
408 module_init(fmi_init);
409 module_exit(fmi_exit);