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
46
47
48
49#include <linux/module.h>
50#include <linux/delay.h>
51
52#include "../comedidev.h"
53
54
55#define ADQ12B_CTREG 0x00
56#define ADQ12B_CTREG_MSKP BIT(7)
57#define ADQ12B_CTREG_GTP BIT(6)
58#define ADQ12B_CTREG_RANGE(x) ((x) << 4)
59#define ADQ12B_CTREG_CHAN(x) ((x) << 0)
60#define ADQ12B_STINR 0x00
61#define ADQ12B_STINR_OUT2 BIT(7)
62#define ADQ12B_STINR_OUTP BIT(6)
63#define ADQ12B_STINR_EOC BIT(5)
64#define ADQ12B_STINR_IN_MASK (0x1f << 0)
65#define ADQ12B_OUTBR 0x04
66#define ADQ12B_ADLOW 0x08
67#define ADQ12B_ADHIG 0x09
68#define ADQ12B_TIMER_BASE 0x0c
69
70
71static const struct comedi_lrange range_adq12b_ai_bipolar = {
72 4, {
73 BIP_RANGE(5),
74 BIP_RANGE(2),
75 BIP_RANGE(1),
76 BIP_RANGE(0.5)
77 }
78};
79
80static const struct comedi_lrange range_adq12b_ai_unipolar = {
81 4, {
82 UNI_RANGE(5),
83 UNI_RANGE(2),
84 UNI_RANGE(1),
85 UNI_RANGE(0.5)
86 }
87};
88
89struct adq12b_private {
90 unsigned int last_ctreg;
91};
92
93static int adq12b_ai_eoc(struct comedi_device *dev,
94 struct comedi_subdevice *s,
95 struct comedi_insn *insn,
96 unsigned long context)
97{
98 unsigned char status;
99
100 status = inb(dev->iobase + ADQ12B_STINR);
101 if (status & ADQ12B_STINR_EOC)
102 return 0;
103 return -EBUSY;
104}
105
106static int adq12b_ai_insn_read(struct comedi_device *dev,
107 struct comedi_subdevice *s,
108 struct comedi_insn *insn,
109 unsigned int *data)
110{
111 struct adq12b_private *devpriv = dev->private;
112 unsigned int chan = CR_CHAN(insn->chanspec);
113 unsigned int range = CR_RANGE(insn->chanspec);
114 unsigned int val;
115 int ret;
116 int i;
117
118
119 val = ADQ12B_CTREG_RANGE(range) | ADQ12B_CTREG_CHAN(chan);
120 if (val != devpriv->last_ctreg) {
121 outb(val, dev->iobase + ADQ12B_CTREG);
122 devpriv->last_ctreg = val;
123 usleep_range(50, 100);
124 }
125
126 val = inb(dev->iobase + ADQ12B_ADLOW);
127
128 for (i = 0; i < insn->n; i++) {
129 ret = comedi_timeout(dev, s, insn, adq12b_ai_eoc, 0);
130 if (ret)
131 return ret;
132
133 val = inb(dev->iobase + ADQ12B_ADHIG) << 8;
134 val |= inb(dev->iobase + ADQ12B_ADLOW);
135
136 data[i] = val;
137 }
138
139 return insn->n;
140}
141
142static int adq12b_di_insn_bits(struct comedi_device *dev,
143 struct comedi_subdevice *s,
144 struct comedi_insn *insn, unsigned int *data)
145{
146
147 data[1] = (inb(dev->iobase + ADQ12B_STINR) & ADQ12B_STINR_IN_MASK);
148
149 return insn->n;
150}
151
152static int adq12b_do_insn_bits(struct comedi_device *dev,
153 struct comedi_subdevice *s,
154 struct comedi_insn *insn,
155 unsigned int *data)
156{
157 unsigned int mask;
158 unsigned int chan;
159 unsigned int val;
160
161 mask = comedi_dio_update_state(s, data);
162 if (mask) {
163 for (chan = 0; chan < 8; chan++) {
164 if ((mask >> chan) & 0x01) {
165 val = (s->state >> chan) & 0x01;
166 outb((val << 3) | chan,
167 dev->iobase + ADQ12B_OUTBR);
168 }
169 }
170 }
171
172 data[1] = s->state;
173
174 return insn->n;
175}
176
177static int adq12b_attach(struct comedi_device *dev, struct comedi_devconfig *it)
178{
179 struct adq12b_private *devpriv;
180 struct comedi_subdevice *s;
181 int ret;
182
183 ret = comedi_request_region(dev, it->options[0], 0x10);
184 if (ret)
185 return ret;
186
187 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
188 if (!devpriv)
189 return -ENOMEM;
190
191 devpriv->last_ctreg = -1;
192
193 ret = comedi_alloc_subdevices(dev, 3);
194 if (ret)
195 return ret;
196
197
198 s = &dev->subdevices[0];
199 s->type = COMEDI_SUBD_AI;
200 if (it->options[2]) {
201 s->subdev_flags = SDF_READABLE | SDF_DIFF;
202 s->n_chan = 8;
203 } else {
204 s->subdev_flags = SDF_READABLE | SDF_GROUND;
205 s->n_chan = 16;
206 }
207 s->maxdata = 0xfff;
208 s->range_table = it->options[1] ? &range_adq12b_ai_unipolar
209 : &range_adq12b_ai_bipolar;
210 s->insn_read = adq12b_ai_insn_read;
211
212
213 s = &dev->subdevices[1];
214 s->type = COMEDI_SUBD_DI;
215 s->subdev_flags = SDF_READABLE;
216 s->n_chan = 5;
217 s->maxdata = 1;
218 s->range_table = &range_digital;
219 s->insn_bits = adq12b_di_insn_bits;
220
221
222 s = &dev->subdevices[2];
223 s->type = COMEDI_SUBD_DO;
224 s->subdev_flags = SDF_WRITABLE;
225 s->n_chan = 8;
226 s->maxdata = 1;
227 s->range_table = &range_digital;
228 s->insn_bits = adq12b_do_insn_bits;
229
230 return 0;
231}
232
233static struct comedi_driver adq12b_driver = {
234 .driver_name = "adq12b",
235 .module = THIS_MODULE,
236 .attach = adq12b_attach,
237 .detach = comedi_legacy_detach,
238};
239module_comedi_driver(adq12b_driver);
240
241MODULE_AUTHOR("Comedi http://www.comedi.org");
242MODULE_DESCRIPTION("Comedi low-level driver");
243MODULE_LICENSE("GPL");
244