]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/radio/radio-sf16fmi.c
Merge branch 'linus' into patchwork
[karo-tx-linux.git] / drivers / media / radio / radio-sf16fmi.c
1 /* SF16-FMI, SF16-FMP and SF16-FMD 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/SF16-FMD
15  *
16  * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
17  */
18
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>
30 #include <media/v4l2-ctrls.h>
31 #include <media/v4l2-event.h>
32 #include "lm7000.h"
33
34 MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood");
35 MODULE_DESCRIPTION("A driver for the SF16-FMI, SF16-FMP and SF16-FMD radio.");
36 MODULE_LICENSE("GPL");
37 MODULE_VERSION("0.0.3");
38
39 static int io = -1;
40 static int radio_nr = -1;
41
42 module_param(io, int, 0);
43 MODULE_PARM_DESC(io, "I/O address of the SF16-FMI/SF16-FMP/SF16-FMD card (0x284 or 0x384)");
44 module_param(radio_nr, int, 0);
45
46 struct fmi
47 {
48         struct v4l2_device v4l2_dev;
49         struct v4l2_ctrl_handler hdl;
50         struct video_device vdev;
51         int io;
52         bool mute;
53         unsigned long curfreq; /* freq in kHz */
54         struct mutex lock;
55 };
56
57 static struct fmi fmi_card;
58 static struct pnp_dev *dev;
59 bool pnp_attached;
60
61 #define RSF16_MINFREQ (87U * 16000)
62 #define RSF16_MAXFREQ (108U * 16000)
63
64 #define FMI_BIT_TUN_CE          (1 << 0)
65 #define FMI_BIT_TUN_CLK         (1 << 1)
66 #define FMI_BIT_TUN_DATA        (1 << 2)
67 #define FMI_BIT_VOL_SW          (1 << 3)
68 #define FMI_BIT_TUN_STRQ        (1 << 4)
69
70 static void fmi_set_pins(void *handle, u8 pins)
71 {
72         struct fmi *fmi = handle;
73         u8 bits = FMI_BIT_TUN_STRQ;
74
75         if (!fmi->mute)
76                 bits |= FMI_BIT_VOL_SW;
77
78         if (pins & LM7000_DATA)
79                 bits |= FMI_BIT_TUN_DATA;
80         if (pins & LM7000_CLK)
81                 bits |= FMI_BIT_TUN_CLK;
82         if (pins & LM7000_CE)
83                 bits |= FMI_BIT_TUN_CE;
84
85         mutex_lock(&fmi->lock);
86         outb_p(bits, fmi->io);
87         mutex_unlock(&fmi->lock);
88 }
89
90 static inline void fmi_mute(struct fmi *fmi)
91 {
92         mutex_lock(&fmi->lock);
93         outb(0x00, fmi->io);
94         mutex_unlock(&fmi->lock);
95 }
96
97 static inline void fmi_unmute(struct fmi *fmi)
98 {
99         mutex_lock(&fmi->lock);
100         outb(0x08, fmi->io);
101         mutex_unlock(&fmi->lock);
102 }
103
104 static inline int fmi_getsigstr(struct fmi *fmi)
105 {
106         int val;
107         int res;
108
109         mutex_lock(&fmi->lock);
110         val = fmi->mute ? 0x00 : 0x08;  /* mute/unmute */
111         outb(val, fmi->io);
112         outb(val | 0x10, fmi->io);
113         msleep(143);            /* was schedule_timeout(HZ/7) */
114         res = (int)inb(fmi->io + 1);
115         outb(val, fmi->io);
116
117         mutex_unlock(&fmi->lock);
118         return (res & 2) ? 0 : 0xFFFF;
119 }
120
121 static int vidioc_querycap(struct file *file, void  *priv,
122                                         struct v4l2_capability *v)
123 {
124         strlcpy(v->driver, "radio-sf16fmi", sizeof(v->driver));
125         strlcpy(v->card, "SF16-FMI/FMP/FMD radio", sizeof(v->card));
126         strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
127         v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
128         v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
129         return 0;
130 }
131
132 static int vidioc_g_tuner(struct file *file, void *priv,
133                                         struct v4l2_tuner *v)
134 {
135         struct fmi *fmi = video_drvdata(file);
136
137         if (v->index > 0)
138                 return -EINVAL;
139
140         strlcpy(v->name, "FM", sizeof(v->name));
141         v->type = V4L2_TUNER_RADIO;
142         v->rangelow = RSF16_MINFREQ;
143         v->rangehigh = RSF16_MAXFREQ;
144         v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
145         v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW;
146         v->audmode = V4L2_TUNER_MODE_STEREO;
147         v->signal = fmi_getsigstr(fmi);
148         return 0;
149 }
150
151 static int vidioc_s_tuner(struct file *file, void *priv,
152                                         const struct v4l2_tuner *v)
153 {
154         return v->index ? -EINVAL : 0;
155 }
156
157 static int vidioc_s_frequency(struct file *file, void *priv,
158                                         const struct v4l2_frequency *f)
159 {
160         struct fmi *fmi = video_drvdata(file);
161         unsigned freq = f->frequency;
162
163         if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
164                 return -EINVAL;
165         clamp(freq, RSF16_MINFREQ, RSF16_MAXFREQ);
166         /* rounding in steps of 800 to match the freq
167            that will be used */
168         lm7000_set_freq((freq / 800) * 800, fmi, fmi_set_pins);
169         return 0;
170 }
171
172 static int vidioc_g_frequency(struct file *file, void *priv,
173                                         struct v4l2_frequency *f)
174 {
175         struct fmi *fmi = video_drvdata(file);
176
177         if (f->tuner != 0)
178                 return -EINVAL;
179         f->type = V4L2_TUNER_RADIO;
180         f->frequency = fmi->curfreq;
181         return 0;
182 }
183
184 static int fmi_s_ctrl(struct v4l2_ctrl *ctrl)
185 {
186         struct fmi *fmi = container_of(ctrl->handler, struct fmi, hdl);
187
188         switch (ctrl->id) {
189         case V4L2_CID_AUDIO_MUTE:
190                 if (ctrl->val)
191                         fmi_mute(fmi);
192                 else
193                         fmi_unmute(fmi);
194                 fmi->mute = ctrl->val;
195                 return 0;
196         }
197         return -EINVAL;
198 }
199
200 static const struct v4l2_ctrl_ops fmi_ctrl_ops = {
201         .s_ctrl = fmi_s_ctrl,
202 };
203
204 static const struct v4l2_file_operations fmi_fops = {
205         .owner          = THIS_MODULE,
206         .open           = v4l2_fh_open,
207         .release        = v4l2_fh_release,
208         .poll           = v4l2_ctrl_poll,
209         .unlocked_ioctl = video_ioctl2,
210 };
211
212 static const struct v4l2_ioctl_ops fmi_ioctl_ops = {
213         .vidioc_querycap    = vidioc_querycap,
214         .vidioc_g_tuner     = vidioc_g_tuner,
215         .vidioc_s_tuner     = vidioc_s_tuner,
216         .vidioc_g_frequency = vidioc_g_frequency,
217         .vidioc_s_frequency = vidioc_s_frequency,
218         .vidioc_log_status  = v4l2_ctrl_log_status,
219         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
220         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
221 };
222
223 /* ladis: this is my card. does any other types exist? */
224 static struct isapnp_device_id id_table[] = {
225                 /* SF16-FMI */
226         {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
227                 ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
228                 /* SF16-FMD */
229         {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
230                 ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad12), 0},
231         {       ISAPNP_CARD_END, },
232 };
233
234 MODULE_DEVICE_TABLE(isapnp, id_table);
235
236 static int __init isapnp_fmi_probe(void)
237 {
238         int i = 0;
239
240         while (id_table[i].card_vendor != 0 && dev == NULL) {
241                 dev = pnp_find_dev(NULL, id_table[i].vendor,
242                                    id_table[i].function, NULL);
243                 i++;
244         }
245
246         if (!dev)
247                 return -ENODEV;
248         if (pnp_device_attach(dev) < 0)
249                 return -EAGAIN;
250         if (pnp_activate_dev(dev) < 0) {
251                 printk(KERN_ERR "radio-sf16fmi: PnP configure failed (out of resources?)\n");
252                 pnp_device_detach(dev);
253                 return -ENOMEM;
254         }
255         if (!pnp_port_valid(dev, 0)) {
256                 pnp_device_detach(dev);
257                 return -ENODEV;
258         }
259
260         i = pnp_port_start(dev, 0);
261         printk(KERN_INFO "radio-sf16fmi: PnP reports card at %#x\n", i);
262
263         return i;
264 }
265
266 static int __init fmi_init(void)
267 {
268         struct fmi *fmi = &fmi_card;
269         struct v4l2_device *v4l2_dev = &fmi->v4l2_dev;
270         struct v4l2_ctrl_handler *hdl = &fmi->hdl;
271         int res, i;
272         int probe_ports[] = { 0, 0x284, 0x384 };
273
274         if (io < 0) {
275                 for (i = 0; i < ARRAY_SIZE(probe_ports); i++) {
276                         io = probe_ports[i];
277                         if (io == 0) {
278                                 io = isapnp_fmi_probe();
279                                 if (io < 0)
280                                         continue;
281                                 pnp_attached = 1;
282                         }
283                         if (!request_region(io, 2, "radio-sf16fmi")) {
284                                 if (pnp_attached)
285                                         pnp_device_detach(dev);
286                                 io = -1;
287                                 continue;
288                         }
289                         if (pnp_attached ||
290                             ((inb(io) & 0xf9) == 0xf9 && (inb(io) & 0x4) == 0))
291                                 break;
292                         release_region(io, 2);
293                         io = -1;
294                 }
295         } else {
296                 if (!request_region(io, 2, "radio-sf16fmi")) {
297                         printk(KERN_ERR "radio-sf16fmi: port %#x already in use\n", io);
298                         return -EBUSY;
299                 }
300                 if (inb(io) == 0xff) {
301                         printk(KERN_ERR "radio-sf16fmi: card not present at %#x\n", io);
302                         release_region(io, 2);
303                         return -ENODEV;
304                 }
305         }
306         if (io < 0) {
307                 printk(KERN_ERR "radio-sf16fmi: no cards found\n");
308                 return -ENODEV;
309         }
310
311         strlcpy(v4l2_dev->name, "sf16fmi", sizeof(v4l2_dev->name));
312         fmi->io = io;
313
314         res = v4l2_device_register(NULL, v4l2_dev);
315         if (res < 0) {
316                 release_region(fmi->io, 2);
317                 if (pnp_attached)
318                         pnp_device_detach(dev);
319                 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
320                 return res;
321         }
322
323         v4l2_ctrl_handler_init(hdl, 1);
324         v4l2_ctrl_new_std(hdl, &fmi_ctrl_ops,
325                         V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
326         v4l2_dev->ctrl_handler = hdl;
327         if (hdl->error) {
328                 res = hdl->error;
329                 v4l2_err(v4l2_dev, "Could not register controls\n");
330                 v4l2_ctrl_handler_free(hdl);
331                 v4l2_device_unregister(v4l2_dev);
332                 return res;
333         }
334
335         strlcpy(fmi->vdev.name, v4l2_dev->name, sizeof(fmi->vdev.name));
336         fmi->vdev.v4l2_dev = v4l2_dev;
337         fmi->vdev.fops = &fmi_fops;
338         fmi->vdev.ioctl_ops = &fmi_ioctl_ops;
339         fmi->vdev.release = video_device_release_empty;
340         set_bit(V4L2_FL_USE_FH_PRIO, &fmi->vdev.flags);
341         video_set_drvdata(&fmi->vdev, fmi);
342
343         mutex_init(&fmi->lock);
344
345         /* mute card - prevents noisy bootups */
346         fmi_mute(fmi);
347
348         if (video_register_device(&fmi->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
349                 v4l2_ctrl_handler_free(hdl);
350                 v4l2_device_unregister(v4l2_dev);
351                 release_region(fmi->io, 2);
352                 if (pnp_attached)
353                         pnp_device_detach(dev);
354                 return -EINVAL;
355         }
356
357         v4l2_info(v4l2_dev, "card driver at 0x%x\n", fmi->io);
358         return 0;
359 }
360
361 static void __exit fmi_exit(void)
362 {
363         struct fmi *fmi = &fmi_card;
364
365         v4l2_ctrl_handler_free(&fmi->hdl);
366         video_unregister_device(&fmi->vdev);
367         v4l2_device_unregister(&fmi->v4l2_dev);
368         release_region(fmi->io, 2);
369         if (dev && pnp_attached)
370                 pnp_device_detach(dev);
371 }
372
373 module_init(fmi_init);
374 module_exit(fmi_exit);