linux/drivers/comedi/drivers/icp_multi.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * icp_multi.c
   4 * Comedi driver for Inova ICP_MULTI board
   5 *
   6 * COMEDI - Linux Control and Measurement Device Interface
   7 * Copyright (C) 1997-2002 David A. Schleef <ds@schleef.org>
   8 */
   9
  10/*
  11 * Driver: icp_multi
  12 * Description: Inova ICP_MULTI
  13 * Devices: [Inova] ICP_MULTI (icp_multi)
  14 * Author: Anne Smorthit <anne.smorthit@sfwte.ch>
  15 * Status: works
  16 *
  17 * Configuration options: not applicable, uses PCI auto config
  18 *
  19 * The driver works for analog input and output and digital input and
  20 * output. It does not work with interrupts or with the counters. Currently
  21 * no support for DMA.
  22 *
  23 * It has 16 single-ended or 8 differential Analogue Input channels with
  24 * 12-bit resolution.  Ranges : 5V, 10V, +/-5V, +/-10V, 0..20mA and 4..20mA.
  25 * Input ranges can be individually programmed for each channel.  Voltage or
  26 * current measurement is selected by jumper.
  27 *
  28 * There are 4 x 12-bit Analogue Outputs.  Ranges : 5V, 10V, +/-5V, +/-10V
  29 *
  30 * 16 x Digital Inputs, 24V
  31 *
  32 * 8 x Digital Outputs, 24V, 1A
  33 *
  34 * 4 x 16-bit counters - not implemented
  35 */
  36
  37#include <linux/module.h>
  38#include <linux/delay.h>
  39
  40#include "../comedi_pci.h"
  41
  42#define ICP_MULTI_ADC_CSR       0x00    /* R/W: ADC command/status register */
  43#define ICP_MULTI_ADC_CSR_ST    BIT(0)  /* Start ADC */
  44#define ICP_MULTI_ADC_CSR_BSY   BIT(0)  /* ADC busy */
  45#define ICP_MULTI_ADC_CSR_BI    BIT(4)  /* Bipolar input range */
  46#define ICP_MULTI_ADC_CSR_RA    BIT(5)  /* Input range 0 = 5V, 1 = 10V */
  47#define ICP_MULTI_ADC_CSR_DI    BIT(6)  /* Input mode 1 = differential */
  48#define ICP_MULTI_ADC_CSR_DI_CHAN(x) (((x) & 0x7) << 9)
  49#define ICP_MULTI_ADC_CSR_SE_CHAN(x) (((x) & 0xf) << 8)
  50#define ICP_MULTI_AI            2       /* R:   Analogue input data */
  51#define ICP_MULTI_DAC_CSR       0x04    /* R/W: DAC command/status register */
  52#define ICP_MULTI_DAC_CSR_ST    BIT(0)  /* Start DAC */
  53#define ICP_MULTI_DAC_CSR_BSY   BIT(0)  /* DAC busy */
  54#define ICP_MULTI_DAC_CSR_BI    BIT(4)  /* Bipolar output range */
  55#define ICP_MULTI_DAC_CSR_RA    BIT(5)  /* Output range 0 = 5V, 1 = 10V */
  56#define ICP_MULTI_DAC_CSR_CHAN(x) (((x) & 0x3) << 8)
  57#define ICP_MULTI_AO            6       /* R/W: Analogue output data */
  58#define ICP_MULTI_DI            8       /* R/W: Digital inputs */
  59#define ICP_MULTI_DO            0x0A    /* R/W: Digital outputs */
  60#define ICP_MULTI_INT_EN        0x0c    /* R/W: Interrupt enable register */
  61#define ICP_MULTI_INT_STAT      0x0e    /* R/W: Interrupt status register */
  62#define ICP_MULTI_INT_ADC_RDY   BIT(0)  /* A/D conversion ready interrupt */
  63#define ICP_MULTI_INT_DAC_RDY   BIT(1)  /* D/A conversion ready interrupt */
  64#define ICP_MULTI_INT_DOUT_ERR  BIT(2)  /* Digital output error interrupt */
  65#define ICP_MULTI_INT_DIN_STAT  BIT(3)  /* Digital input status change int. */
  66#define ICP_MULTI_INT_CIE0      BIT(4)  /* Counter 0 overrun interrupt */
  67#define ICP_MULTI_INT_CIE1      BIT(5)  /* Counter 1 overrun interrupt */
  68#define ICP_MULTI_INT_CIE2      BIT(6)  /* Counter 2 overrun interrupt */
  69#define ICP_MULTI_INT_CIE3      BIT(7)  /* Counter 3 overrun interrupt */
  70#define ICP_MULTI_INT_MASK      0xff    /* All interrupts */
  71#define ICP_MULTI_CNTR0         0x10    /* R/W: Counter 0 */
  72#define ICP_MULTI_CNTR1         0x12    /* R/W: counter 1 */
  73#define ICP_MULTI_CNTR2         0x14    /* R/W: Counter 2 */
  74#define ICP_MULTI_CNTR3         0x16    /* R/W: Counter 3 */
  75
  76/* analog input and output have the same range options */
  77static const struct comedi_lrange icp_multi_ranges = {
  78        4, {
  79                UNI_RANGE(5),
  80                UNI_RANGE(10),
  81                BIP_RANGE(5),
  82                BIP_RANGE(10)
  83        }
  84};
  85
  86static const char range_codes_analog[] = { 0x00, 0x20, 0x10, 0x30 };
  87
  88static int icp_multi_ai_eoc(struct comedi_device *dev,
  89                            struct comedi_subdevice *s,
  90                            struct comedi_insn *insn,
  91                            unsigned long context)
  92{
  93        unsigned int status;
  94
  95        status = readw(dev->mmio + ICP_MULTI_ADC_CSR);
  96        if ((status & ICP_MULTI_ADC_CSR_BSY) == 0)
  97                return 0;
  98        return -EBUSY;
  99}
 100
 101static int icp_multi_ai_insn_read(struct comedi_device *dev,
 102                                  struct comedi_subdevice *s,
 103                                  struct comedi_insn *insn,
 104                                  unsigned int *data)
 105{
 106        unsigned int chan = CR_CHAN(insn->chanspec);
 107        unsigned int range = CR_RANGE(insn->chanspec);
 108        unsigned int aref = CR_AREF(insn->chanspec);
 109        unsigned int adc_csr;
 110        int ret = 0;
 111        int n;
 112
 113        /* Set mode and range data for specified channel */
 114        if (aref == AREF_DIFF) {
 115                adc_csr = ICP_MULTI_ADC_CSR_DI_CHAN(chan) |
 116                          ICP_MULTI_ADC_CSR_DI;
 117        } else {
 118                adc_csr = ICP_MULTI_ADC_CSR_SE_CHAN(chan);
 119        }
 120        adc_csr |= range_codes_analog[range];
 121        writew(adc_csr, dev->mmio + ICP_MULTI_ADC_CSR);
 122
 123        for (n = 0; n < insn->n; n++) {
 124                /*  Set start ADC bit */
 125                writew(adc_csr | ICP_MULTI_ADC_CSR_ST,
 126                       dev->mmio + ICP_MULTI_ADC_CSR);
 127
 128                udelay(1);
 129
 130                /*  Wait for conversion to complete, or get fed up waiting */
 131                ret = comedi_timeout(dev, s, insn, icp_multi_ai_eoc, 0);
 132                if (ret)
 133                        break;
 134
 135                data[n] = (readw(dev->mmio + ICP_MULTI_AI) >> 4) & 0x0fff;
 136        }
 137
 138        return ret ? ret : n;
 139}
 140
 141static int icp_multi_ao_ready(struct comedi_device *dev,
 142                              struct comedi_subdevice *s,
 143                              struct comedi_insn *insn,
 144                              unsigned long context)
 145{
 146        unsigned int status;
 147
 148        status = readw(dev->mmio + ICP_MULTI_DAC_CSR);
 149        if ((status & ICP_MULTI_DAC_CSR_BSY) == 0)
 150                return 0;
 151        return -EBUSY;
 152}
 153
 154static int icp_multi_ao_insn_write(struct comedi_device *dev,
 155                                   struct comedi_subdevice *s,
 156                                   struct comedi_insn *insn,
 157                                   unsigned int *data)
 158{
 159        unsigned int chan = CR_CHAN(insn->chanspec);
 160        unsigned int range = CR_RANGE(insn->chanspec);
 161        unsigned int dac_csr;
 162        int i;
 163
 164        /* Select channel and range */
 165        dac_csr = ICP_MULTI_DAC_CSR_CHAN(chan);
 166        dac_csr |= range_codes_analog[range];
 167        writew(dac_csr, dev->mmio + ICP_MULTI_DAC_CSR);
 168
 169        for (i = 0; i < insn->n; i++) {
 170                unsigned int val = data[i];
 171                int ret;
 172
 173                /* Wait for analog output to be ready for new data */
 174                ret = comedi_timeout(dev, s, insn, icp_multi_ao_ready, 0);
 175                if (ret)
 176                        return ret;
 177
 178                writew(val, dev->mmio + ICP_MULTI_AO);
 179
 180                /* Set start conversion bit to write data to channel */
 181                writew(dac_csr | ICP_MULTI_DAC_CSR_ST,
 182                       dev->mmio + ICP_MULTI_DAC_CSR);
 183
 184                s->readback[chan] = val;
 185        }
 186
 187        return insn->n;
 188}
 189
 190static int icp_multi_di_insn_bits(struct comedi_device *dev,
 191                                  struct comedi_subdevice *s,
 192                                  struct comedi_insn *insn,
 193                                  unsigned int *data)
 194{
 195        data[1] = readw(dev->mmio + ICP_MULTI_DI);
 196
 197        return insn->n;
 198}
 199
 200static int icp_multi_do_insn_bits(struct comedi_device *dev,
 201                                  struct comedi_subdevice *s,
 202                                  struct comedi_insn *insn,
 203                                  unsigned int *data)
 204{
 205        if (comedi_dio_update_state(s, data))
 206                writew(s->state, dev->mmio + ICP_MULTI_DO);
 207
 208        data[1] = s->state;
 209
 210        return insn->n;
 211}
 212
 213static int icp_multi_reset(struct comedi_device *dev)
 214{
 215        int i;
 216
 217        /* Disable all interrupts and clear any requests */
 218        writew(0, dev->mmio + ICP_MULTI_INT_EN);
 219        writew(ICP_MULTI_INT_MASK, dev->mmio + ICP_MULTI_INT_STAT);
 220
 221        /* Reset the analog output channels to 0V */
 222        for (i = 0; i < 4; i++) {
 223                unsigned int dac_csr = ICP_MULTI_DAC_CSR_CHAN(i);
 224
 225                /* Select channel and 0..5V range */
 226                writew(dac_csr, dev->mmio + ICP_MULTI_DAC_CSR);
 227
 228                /* Output 0V */
 229                writew(0, dev->mmio + ICP_MULTI_AO);
 230
 231                /* Set start conversion bit to write data to channel */
 232                writew(dac_csr | ICP_MULTI_DAC_CSR_ST,
 233                       dev->mmio + ICP_MULTI_DAC_CSR);
 234                udelay(1);
 235        }
 236
 237        /* Digital outputs to 0 */
 238        writew(0, dev->mmio + ICP_MULTI_DO);
 239
 240        return 0;
 241}
 242
 243static int icp_multi_auto_attach(struct comedi_device *dev,
 244                                 unsigned long context_unused)
 245{
 246        struct pci_dev *pcidev = comedi_to_pci_dev(dev);
 247        struct comedi_subdevice *s;
 248        int ret;
 249
 250        ret = comedi_pci_enable(dev);
 251        if (ret)
 252                return ret;
 253
 254        dev->mmio = pci_ioremap_bar(pcidev, 2);
 255        if (!dev->mmio)
 256                return -ENOMEM;
 257
 258        ret = comedi_alloc_subdevices(dev, 4);
 259        if (ret)
 260                return ret;
 261
 262        icp_multi_reset(dev);
 263
 264        /* Analog Input subdevice */
 265        s = &dev->subdevices[0];
 266        s->type         = COMEDI_SUBD_AI;
 267        s->subdev_flags = SDF_READABLE | SDF_COMMON | SDF_GROUND | SDF_DIFF;
 268        s->n_chan       = 16;
 269        s->maxdata      = 0x0fff;
 270        s->range_table  = &icp_multi_ranges;
 271        s->insn_read    = icp_multi_ai_insn_read;
 272
 273        /* Analog Output subdevice */
 274        s = &dev->subdevices[1];
 275        s->type         = COMEDI_SUBD_AO;
 276        s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
 277        s->n_chan       = 4;
 278        s->maxdata      = 0x0fff;
 279        s->range_table  = &icp_multi_ranges;
 280        s->insn_write   = icp_multi_ao_insn_write;
 281
 282        ret = comedi_alloc_subdev_readback(s);
 283        if (ret)
 284                return ret;
 285
 286        /* Digital Input subdevice */
 287        s = &dev->subdevices[2];
 288        s->type         = COMEDI_SUBD_DI;
 289        s->subdev_flags = SDF_READABLE;
 290        s->n_chan       = 16;
 291        s->maxdata      = 1;
 292        s->range_table  = &range_digital;
 293        s->insn_bits    = icp_multi_di_insn_bits;
 294
 295        /* Digital Output subdevice */
 296        s = &dev->subdevices[3];
 297        s->type         = COMEDI_SUBD_DO;
 298        s->subdev_flags = SDF_WRITABLE;
 299        s->n_chan       = 8;
 300        s->maxdata      = 1;
 301        s->range_table  = &range_digital;
 302        s->insn_bits    = icp_multi_do_insn_bits;
 303
 304        return 0;
 305}
 306
 307static struct comedi_driver icp_multi_driver = {
 308        .driver_name    = "icp_multi",
 309        .module         = THIS_MODULE,
 310        .auto_attach    = icp_multi_auto_attach,
 311        .detach         = comedi_pci_detach,
 312};
 313
 314static int icp_multi_pci_probe(struct pci_dev *dev,
 315                               const struct pci_device_id *id)
 316{
 317        return comedi_pci_auto_config(dev, &icp_multi_driver, id->driver_data);
 318}
 319
 320static const struct pci_device_id icp_multi_pci_table[] = {
 321        { PCI_DEVICE(PCI_VENDOR_ID_ICP, 0x8000) },
 322        { 0 }
 323};
 324MODULE_DEVICE_TABLE(pci, icp_multi_pci_table);
 325
 326static struct pci_driver icp_multi_pci_driver = {
 327        .name           = "icp_multi",
 328        .id_table       = icp_multi_pci_table,
 329        .probe          = icp_multi_pci_probe,
 330        .remove         = comedi_pci_auto_unconfig,
 331};
 332module_comedi_pci_driver(icp_multi_driver, icp_multi_pci_driver);
 333
 334MODULE_AUTHOR("Comedi https://www.comedi.org");
 335MODULE_DESCRIPTION("Comedi driver for Inova ICP_MULTI board");
 336MODULE_LICENSE("GPL");
 337