linux/drivers/staging/comedi/drivers/dt2815.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * comedi/drivers/dt2815.c
   4 * Hardware driver for Data Translation DT2815
   5 *
   6 * COMEDI - Linux Control and Measurement Device Interface
   7 * Copyright (C) 1999 Anders Blomdell <anders.blomdell@control.lth.se>
   8 */
   9/*
  10 * Driver: dt2815
  11 * Description: Data Translation DT2815
  12 * Author: ds
  13 * Status: mostly complete, untested
  14 * Devices: [Data Translation] DT2815 (dt2815)
  15 *
  16 * I'm not sure anyone has ever tested this board.  If you have information
  17 * contrary, please update.
  18 *
  19 * Configuration options:
  20 * [0] - I/O port base base address
  21 * [1] - IRQ (unused)
  22 * [2] - Voltage unipolar/bipolar configuration
  23 *      0 == unipolar 5V  (0V -- +5V)
  24 *      1 == bipolar 5V  (-5V -- +5V)
  25 * [3] - Current offset configuration
  26 *      0 == disabled  (0mA -- +32mAV)
  27 *      1 == enabled  (+4mA -- +20mAV)
  28 * [4] - Firmware program configuration
  29 *      0 == program 1 (see manual table 5-4)
  30 *      1 == program 2 (see manual table 5-4)
  31 *      2 == program 3 (see manual table 5-4)
  32 *      3 == program 4 (see manual table 5-4)
  33 * [5] - Analog output 0 range configuration
  34 *      0 == voltage
  35 *      1 == current
  36 * [6] - Analog output 1 range configuration (same options)
  37 * [7] - Analog output 2 range configuration (same options)
  38 * [8] - Analog output 3 range configuration (same options)
  39 * [9] - Analog output 4 range configuration (same options)
  40 * [10] - Analog output 5 range configuration (same options)
  41 * [11] - Analog output 6 range configuration (same options)
  42 * [12] - Analog output 7 range configuration (same options)
  43 */
  44
  45#include <linux/module.h>
  46#include "../comedidev.h"
  47
  48#include <linux/delay.h>
  49
  50#define DT2815_DATA 0
  51#define DT2815_STATUS 1
  52
  53struct dt2815_private {
  54        const struct comedi_lrange *range_type_list[8];
  55        unsigned int ao_readback[8];
  56};
  57
  58static int dt2815_ao_status(struct comedi_device *dev,
  59                            struct comedi_subdevice *s,
  60                            struct comedi_insn *insn,
  61                            unsigned long context)
  62{
  63        unsigned int status;
  64
  65        status = inb(dev->iobase + DT2815_STATUS);
  66        if (status == context)
  67                return 0;
  68        return -EBUSY;
  69}
  70
  71static int dt2815_ao_insn_read(struct comedi_device *dev,
  72                               struct comedi_subdevice *s,
  73                               struct comedi_insn *insn, unsigned int *data)
  74{
  75        struct dt2815_private *devpriv = dev->private;
  76        int i;
  77        int chan = CR_CHAN(insn->chanspec);
  78
  79        for (i = 0; i < insn->n; i++)
  80                data[i] = devpriv->ao_readback[chan];
  81
  82        return i;
  83}
  84
  85static int dt2815_ao_insn(struct comedi_device *dev, struct comedi_subdevice *s,
  86                          struct comedi_insn *insn, unsigned int *data)
  87{
  88        struct dt2815_private *devpriv = dev->private;
  89        int i;
  90        int chan = CR_CHAN(insn->chanspec);
  91        unsigned int lo, hi;
  92        int ret;
  93
  94        for (i = 0; i < insn->n; i++) {
  95                lo = ((data[i] & 0x0f) << 4) | (chan << 1) | 0x01;
  96                hi = (data[i] & 0xff0) >> 4;
  97
  98                ret = comedi_timeout(dev, s, insn, dt2815_ao_status, 0x00);
  99                if (ret)
 100                        return ret;
 101
 102                outb(lo, dev->iobase + DT2815_DATA);
 103
 104                ret = comedi_timeout(dev, s, insn, dt2815_ao_status, 0x10);
 105                if (ret)
 106                        return ret;
 107
 108                devpriv->ao_readback[chan] = data[i];
 109        }
 110        return i;
 111}
 112
 113/*
 114 * options[0]   Board base address
 115 * options[1]   IRQ (not applicable)
 116 * options[2]   Voltage unipolar/bipolar configuration
 117 *              0 == unipolar 5V  (0V -- +5V)
 118 *              1 == bipolar 5V  (-5V -- +5V)
 119 * options[3]   Current offset configuration
 120 *              0 == disabled  (0mA -- +32mAV)
 121 *              1 == enabled  (+4mA -- +20mAV)
 122 * options[4]   Firmware program configuration
 123 *              0 == program 1 (see manual table 5-4)
 124 *              1 == program 2 (see manual table 5-4)
 125 *              2 == program 3 (see manual table 5-4)
 126 *              3 == program 4 (see manual table 5-4)
 127 * options[5]   Analog output 0 range configuration
 128 *              0 == voltage
 129 *              1 == current
 130 * options[6]   Analog output 1 range configuration
 131 * ...
 132 * options[12]   Analog output 7 range configuration
 133 *              0 == voltage
 134 *              1 == current
 135 */
 136
 137static int dt2815_attach(struct comedi_device *dev, struct comedi_devconfig *it)
 138{
 139        struct dt2815_private *devpriv;
 140        struct comedi_subdevice *s;
 141        int i;
 142        const struct comedi_lrange *current_range_type, *voltage_range_type;
 143        int ret;
 144
 145        ret = comedi_request_region(dev, it->options[0], 0x2);
 146        if (ret)
 147                return ret;
 148
 149        ret = comedi_alloc_subdevices(dev, 1);
 150        if (ret)
 151                return ret;
 152
 153        devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
 154        if (!devpriv)
 155                return -ENOMEM;
 156
 157        s = &dev->subdevices[0];
 158        /* ao subdevice */
 159        s->type = COMEDI_SUBD_AO;
 160        s->subdev_flags = SDF_WRITABLE;
 161        s->maxdata = 0xfff;
 162        s->n_chan = 8;
 163        s->insn_write = dt2815_ao_insn;
 164        s->insn_read = dt2815_ao_insn_read;
 165        s->range_table_list = devpriv->range_type_list;
 166
 167        current_range_type = (it->options[3])
 168            ? &range_4_20mA : &range_0_32mA;
 169        voltage_range_type = (it->options[2])
 170            ? &range_bipolar5 : &range_unipolar5;
 171        for (i = 0; i < 8; i++) {
 172                devpriv->range_type_list[i] = (it->options[5 + i])
 173                    ? current_range_type : voltage_range_type;
 174        }
 175
 176        /* Init the 2815 */
 177        outb(0x00, dev->iobase + DT2815_STATUS);
 178        for (i = 0; i < 100; i++) {
 179                /* This is incredibly slow (approx 20 ms) */
 180                unsigned int status;
 181
 182                usleep_range(1000, 3000);
 183                status = inb(dev->iobase + DT2815_STATUS);
 184                if (status == 4) {
 185                        unsigned int program;
 186
 187                        program = (it->options[4] & 0x3) << 3 | 0x7;
 188                        outb(program, dev->iobase + DT2815_DATA);
 189                        dev_dbg(dev->class_dev, "program: 0x%x (@t=%d)\n",
 190                                program, i);
 191                        break;
 192                } else if (status != 0x00) {
 193                        dev_dbg(dev->class_dev,
 194                                "unexpected status 0x%x (@t=%d)\n",
 195                                status, i);
 196                        if (status & 0x60)
 197                                outb(0x00, dev->iobase + DT2815_STATUS);
 198                }
 199        }
 200
 201        return 0;
 202}
 203
 204static struct comedi_driver dt2815_driver = {
 205        .driver_name    = "dt2815",
 206        .module         = THIS_MODULE,
 207        .attach         = dt2815_attach,
 208        .detach         = comedi_legacy_detach,
 209};
 210module_comedi_driver(dt2815_driver);
 211
 212MODULE_AUTHOR("Comedi http://www.comedi.org");
 213MODULE_DESCRIPTION("Comedi low-level driver");
 214MODULE_LICENSE("GPL");
 215