1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140static 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
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
180 outb(0x00, dev->iobase + DT2815_STATUS);
181 for (i = 0; i < 100; i++) {
182
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
207static struct comedi_driver dt2815_driver = {
208 .driver_name = "dt2815",
209 .module = THIS_MODULE,
210 .attach = dt2815_attach,
211 .detach = comedi_legacy_detach,
212};
213module_comedi_driver(dt2815_driver);
214
215MODULE_AUTHOR("Comedi https://www.comedi.org");
216MODULE_DESCRIPTION("Comedi low-level driver");
217MODULE_LICENSE("GPL");
218