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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70#include "../comedidev.h"
71
72#include <linux/ioport.h>
73
74#undef ACL6126_IRQ
75
76#define PCL726_SIZE 16
77#define PCL727_SIZE 32
78#define PCL728_SIZE 8
79
80#define PCL726_DAC0_HI 0
81#define PCL726_DAC0_LO 1
82
83#define PCL726_DO_HI 12
84#define PCL726_DO_LO 13
85#define PCL726_DI_HI 14
86#define PCL726_DI_LO 15
87
88#define PCL727_DO_HI 24
89#define PCL727_DO_LO 25
90#define PCL727_DI_HI 0
91#define PCL727_DI_LO 1
92
93static const struct comedi_lrange range_4_20mA = { 1, {RANGE_mA(4, 20)} };
94static const struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} };
95
96static const struct comedi_lrange *const rangelist_726[] = {
97 &range_unipolar5, &range_unipolar10,
98 &range_bipolar5, &range_bipolar10,
99 &range_4_20mA, &range_unknown
100};
101
102static const struct comedi_lrange *const rangelist_727[] = {
103 &range_unipolar5, &range_unipolar10,
104 &range_bipolar5,
105 &range_4_20mA
106};
107
108static const struct comedi_lrange *const rangelist_728[] = {
109 &range_unipolar5, &range_unipolar10,
110 &range_bipolar5, &range_bipolar10,
111 &range_4_20mA, &range_0_20mA
112};
113
114static int pcl726_attach(struct comedi_device *dev,
115 struct comedi_devconfig *it);
116static int pcl726_detach(struct comedi_device *dev);
117
118struct pcl726_board {
119
120 const char *name;
121 int n_aochan;
122 int num_of_ranges;
123 unsigned int IRQbits;
124 unsigned int io_range;
125 char have_dio;
126 int di_hi;
127 int di_lo;
128 int do_hi;
129 int do_lo;
130 const struct comedi_lrange *const *range_type_list;
131};
132
133static const struct pcl726_board boardtypes[] = {
134 {"pcl726", 6, 6, 0x0000, PCL726_SIZE, 1,
135 PCL726_DI_HI, PCL726_DI_LO, PCL726_DO_HI, PCL726_DO_LO,
136 &rangelist_726[0],},
137 {"pcl727", 12, 4, 0x0000, PCL727_SIZE, 1,
138 PCL727_DI_HI, PCL727_DI_LO, PCL727_DO_HI, PCL727_DO_LO,
139 &rangelist_727[0],},
140 {"pcl728", 2, 6, 0x0000, PCL728_SIZE, 0,
141 0, 0, 0, 0,
142 &rangelist_728[0],},
143 {"acl6126", 6, 5, 0x96e8, PCL726_SIZE, 1,
144 PCL726_DI_HI, PCL726_DI_LO, PCL726_DO_HI, PCL726_DO_LO,
145 &rangelist_726[0],},
146 {"acl6128", 2, 6, 0x0000, PCL728_SIZE, 0,
147 0, 0, 0, 0,
148 &rangelist_728[0],},
149};
150
151#define n_boardtypes (sizeof(boardtypes)/sizeof(struct pcl726_board))
152#define this_board ((const struct pcl726_board *)dev->board_ptr)
153
154static struct comedi_driver driver_pcl726 = {
155 .driver_name = "pcl726",
156 .module = THIS_MODULE,
157 .attach = pcl726_attach,
158 .detach = pcl726_detach,
159 .board_name = &boardtypes[0].name,
160 .num_names = n_boardtypes,
161 .offset = sizeof(struct pcl726_board),
162};
163
164COMEDI_INITCLEANUP(driver_pcl726);
165
166struct pcl726_private {
167
168 int bipolar[12];
169 const struct comedi_lrange *rangelist[12];
170 unsigned int ao_readback[12];
171};
172
173#define devpriv ((struct pcl726_private *)dev->private)
174
175static int pcl726_ao_insn(struct comedi_device *dev, struct comedi_subdevice *s,
176 struct comedi_insn *insn, unsigned int *data)
177{
178 int hi, lo;
179 int n;
180 int chan = CR_CHAN(insn->chanspec);
181
182 for (n = 0; n < insn->n; n++) {
183 lo = data[n] & 0xff;
184 hi = (data[n] >> 8) & 0xf;
185 if (devpriv->bipolar[chan])
186 hi ^= 0x8;
187
188
189
190
191
192 outb(hi, dev->iobase + PCL726_DAC0_HI + 2 * chan);
193 outb(lo, dev->iobase + PCL726_DAC0_LO + 2 * chan);
194 devpriv->ao_readback[chan] = data[n];
195 }
196
197 return n;
198}
199
200static int pcl726_ao_insn_read(struct comedi_device *dev,
201 struct comedi_subdevice *s,
202 struct comedi_insn *insn, unsigned int *data)
203{
204 int chan = CR_CHAN(insn->chanspec);
205 int n;
206
207 for (n = 0; n < insn->n; n++) {
208 data[n] = devpriv->ao_readback[chan];
209 }
210 return n;
211}
212
213static int pcl726_di_insn_bits(struct comedi_device *dev,
214 struct comedi_subdevice *s,
215 struct comedi_insn *insn, unsigned int *data)
216{
217 if (insn->n != 2)
218 return -EINVAL;
219
220 data[1] = inb(dev->iobase + this_board->di_lo) |
221 (inb(dev->iobase + this_board->di_hi) << 8);
222
223 return 2;
224}
225
226static int pcl726_do_insn_bits(struct comedi_device *dev,
227 struct comedi_subdevice *s,
228 struct comedi_insn *insn, unsigned int *data)
229{
230 if (insn->n != 2)
231 return -EINVAL;
232
233 if (data[0]) {
234 s->state &= ~data[0];
235 s->state |= data[0] & data[1];
236 }
237 if (data[1] & 0x00ff)
238 outb(s->state & 0xff, dev->iobase + this_board->do_lo);
239 if (data[1] & 0xff00)
240 outb((s->state >> 8), dev->iobase + this_board->do_hi);
241
242 data[1] = s->state;
243
244 return 2;
245}
246
247static int pcl726_attach(struct comedi_device *dev, struct comedi_devconfig *it)
248{
249 struct comedi_subdevice *s;
250 unsigned long iobase;
251 unsigned int iorange;
252 int ret, i;
253#ifdef ACL6126_IRQ
254 unsigned int irq;
255#endif
256
257 iobase = it->options[0];
258 iorange = this_board->io_range;
259 printk("comedi%d: pcl726: board=%s, 0x%03lx ", dev->minor,
260 this_board->name, iobase);
261 if (!request_region(iobase, iorange, "pcl726")) {
262 printk("I/O port conflict\n");
263 return -EIO;
264 }
265
266 dev->iobase = iobase;
267
268 dev->board_name = this_board->name;
269
270 ret = alloc_private(dev, sizeof(struct pcl726_private));
271 if (ret < 0)
272 return -ENOMEM;
273
274 for (i = 0; i < 12; i++) {
275 devpriv->bipolar[i] = 0;
276 devpriv->rangelist[i] = &range_unknown;
277 }
278
279#ifdef ACL6126_IRQ
280 irq = 0;
281 if (boardtypes[board].IRQbits != 0) {
282 irq = it->options[1];
283 devpriv->first_chan = 2;
284 if (irq) {
285 if (((1 << irq) & boardtypes[board].IRQbits) == 0) {
286 printk
287 (", IRQ %d is out of allowed range, DISABLING IT",
288 irq);
289 irq = 0;
290 } else {
291 if (request_irq(irq, interrupt_pcl818, 0,
292 "pcl726", dev)) {
293 printk
294 (", unable to allocate IRQ %d, DISABLING IT",
295 irq);
296 irq = 0;
297 } else {
298 printk(", irq=%d", irq);
299 }
300 }
301 }
302 }
303
304 dev->irq = irq;
305#endif
306
307 printk("\n");
308
309 ret = alloc_subdevices(dev, 3);
310 if (ret < 0)
311 return ret;
312
313 s = dev->subdevices + 0;
314
315 s->type = COMEDI_SUBD_AO;
316 s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
317 s->n_chan = this_board->n_aochan;
318 s->maxdata = 0xfff;
319 s->len_chanlist = 1;
320 s->insn_write = pcl726_ao_insn;
321 s->insn_read = pcl726_ao_insn_read;
322 s->range_table_list = devpriv->rangelist;
323 for (i = 0; i < this_board->n_aochan; i++) {
324 int j;
325
326 j = it->options[2 + 1];
327 if ((j < 0) || (j >= this_board->num_of_ranges)) {
328 printk
329 ("Invalid range for channel %d! Must be 0<=%d<%d\n",
330 i, j, this_board->num_of_ranges - 1);
331 j = 0;
332 }
333 devpriv->rangelist[i] = this_board->range_type_list[j];
334 if (devpriv->rangelist[i]->range[0].min ==
335 -devpriv->rangelist[i]->range[0].max)
336 devpriv->bipolar[i] = 1;
337 }
338
339 s = dev->subdevices + 1;
340
341 if (!this_board->have_dio) {
342 s->type = COMEDI_SUBD_UNUSED;
343 } else {
344 s->type = COMEDI_SUBD_DI;
345 s->subdev_flags = SDF_READABLE | SDF_GROUND;
346 s->n_chan = 16;
347 s->maxdata = 1;
348 s->len_chanlist = 1;
349 s->insn_bits = pcl726_di_insn_bits;
350 s->range_table = &range_digital;
351 }
352
353 s = dev->subdevices + 2;
354
355 if (!this_board->have_dio) {
356 s->type = COMEDI_SUBD_UNUSED;
357 } else {
358 s->type = COMEDI_SUBD_DO;
359 s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
360 s->n_chan = 16;
361 s->maxdata = 1;
362 s->len_chanlist = 1;
363 s->insn_bits = pcl726_do_insn_bits;
364 s->range_table = &range_digital;
365 }
366
367 return 0;
368}
369
370static int pcl726_detach(struct comedi_device *dev)
371{
372
373
374#ifdef ACL6126_IRQ
375 if (dev->irq) {
376 free_irq(dev->irq, dev);
377 }
378#endif
379
380 if (dev->iobase)
381 release_region(dev->iobase, this_board->io_range);
382
383 return 0;
384}
385