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 <linux/comedi/comedidev.h>
47#include <linux/delay.h>
48
49#define DT2815_DATA 0
50#define DT2815_STATUS 1
51
52struct dt2815_private {
53 const struct comedi_lrange *range_type_list[8];
54 unsigned int ao_readback[8];
55};
56
57static int dt2815_ao_status(struct comedi_device *dev,
58 struct comedi_subdevice *s,
59 struct comedi_insn *insn,
60 unsigned long context)
61{
62 unsigned int status;
63
64 status = inb(port: dev->iobase + DT2815_STATUS);
65 if (status == context)
66 return 0;
67 return -EBUSY;
68}
69
70static int dt2815_ao_insn_read(struct comedi_device *dev,
71 struct comedi_subdevice *s,
72 struct comedi_insn *insn, unsigned int *data)
73{
74 struct dt2815_private *devpriv = dev->private;
75 int i;
76 int chan = CR_CHAN(insn->chanspec);
77
78 for (i = 0; i < insn->n; i++)
79 data[i] = devpriv->ao_readback[chan];
80
81 return i;
82}
83
84static int dt2815_ao_insn(struct comedi_device *dev, 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 unsigned int lo, hi;
91 int ret;
92
93 for (i = 0; i < insn->n; i++) {
94 /* FIXME: lo bit 0 chooses voltage output or current output */
95 lo = ((data[i] & 0x0f) << 4) | (chan << 1) | 0x01;
96 hi = (data[i] & 0xff0) >> 4;
97
98 ret = comedi_timeout(dev, s, insn, cb: dt2815_ao_status, context: 0x00);
99 if (ret)
100 return ret;
101
102 outb(value: lo, port: dev->iobase + DT2815_DATA);
103
104 ret = comedi_timeout(dev, s, insn, cb: dt2815_ao_status, context: 0x10);
105 if (ret)
106 return ret;
107
108 outb(value: hi, port: dev->iobase + DT2815_DATA);
109
110 devpriv->ao_readback[chan] = data[i];
111 }
112 return i;
113}
114
115/*
116 * options[0] Board base address
117 * options[1] IRQ (not applicable)
118 * options[2] Voltage unipolar/bipolar configuration
119 * 0 == unipolar 5V (0V -- +5V)
120 * 1 == bipolar 5V (-5V -- +5V)
121 * options[3] Current offset configuration
122 * 0 == disabled (0mA -- +32mAV)
123 * 1 == enabled (+4mA -- +20mAV)
124 * options[4] Firmware program configuration
125 * 0 == program 1 (see manual table 5-4)
126 * 1 == program 2 (see manual table 5-4)
127 * 2 == program 3 (see manual table 5-4)
128 * 3 == program 4 (see manual table 5-4)
129 * options[5] Analog output 0 range configuration
130 * 0 == voltage
131 * 1 == current
132 * options[6] Analog output 1 range configuration
133 * ...
134 * options[12] Analog output 7 range configuration
135 * 0 == voltage
136 * 1 == current
137 */
138
139static int dt2815_attach(struct comedi_device *dev, struct comedi_devconfig *it)
140{
141 struct dt2815_private *devpriv;
142 struct comedi_subdevice *s;
143 int i;
144 const struct comedi_lrange *current_range_type, *voltage_range_type;
145 int ret;
146
147 ret = comedi_request_region(dev, start: it->options[0], len: 0x2);
148 if (ret)
149 return ret;
150
151 ret = comedi_alloc_subdevices(dev, num_subdevices: 1);
152 if (ret)
153 return ret;
154
155 devpriv = comedi_alloc_devpriv(dev, size: sizeof(*devpriv));
156 if (!devpriv)
157 return -ENOMEM;
158
159 s = &dev->subdevices[0];
160 /* ao subdevice */
161 s->type = COMEDI_SUBD_AO;
162 s->subdev_flags = SDF_WRITABLE;
163 s->maxdata = 0xfff;
164 s->n_chan = 8;
165 s->insn_write = dt2815_ao_insn;
166 s->insn_read = dt2815_ao_insn_read;
167 s->range_table_list = devpriv->range_type_list;
168
169 current_range_type = (it->options[3])
170 ? &range_4_20mA : &range_0_32mA;
171 voltage_range_type = (it->options[2])
172 ? &range_bipolar5 : &range_unipolar5;
173 for (i = 0; i < 8; i++) {
174 devpriv->range_type_list[i] = (it->options[5 + i])
175 ? current_range_type : voltage_range_type;
176 }
177
178 /* Init the 2815 */
179 outb(value: 0x00, port: dev->iobase + DT2815_STATUS);
180 for (i = 0; i < 100; i++) {
181 /* This is incredibly slow (approx 20 ms) */
182 unsigned int status;
183
184 usleep_range(min: 1000, max: 3000);
185 status = inb(port: dev->iobase + DT2815_STATUS);
186 if (status == 4) {
187 unsigned int program;
188
189 program = (it->options[4] & 0x3) << 3 | 0x7;
190 outb(value: program, port: dev->iobase + DT2815_DATA);
191 dev_dbg(dev->class_dev, "program: 0x%x (@t=%d)\n",
192 program, i);
193 break;
194 } else if (status != 0x00) {
195 dev_dbg(dev->class_dev,
196 "unexpected status 0x%x (@t=%d)\n",
197 status, i);
198 if (status & 0x60)
199 outb(value: 0x00, port: dev->iobase + DT2815_STATUS);
200 }
201 }
202
203 return 0;
204}
205
206static struct comedi_driver dt2815_driver = {
207 .driver_name = "dt2815",
208 .module = THIS_MODULE,
209 .attach = dt2815_attach,
210 .detach = comedi_legacy_detach,
211};
212module_comedi_driver(dt2815_driver);
213
214MODULE_AUTHOR("Comedi https://www.comedi.org");
215MODULE_DESCRIPTION("Comedi low-level driver");
216MODULE_LICENSE("GPL");
217

source code of linux/drivers/comedi/drivers/dt2815.c