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