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