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