1
2
3
4
5
6
7
8
9
10
11
12#include <linux/module.h>
13
14#include "../comedidev.h"
15
16#include "8255.h"
17#include "comedi_8254.h"
18#include "das08.h"
19
20
21
22
23
24
25
26
27
28
29
30
31
32#define DAS08_AI_LSB_REG 0x00
33#define DAS08_AI_MSB_REG 0x01
34#define DAS08_AI_TRIG_REG 0x01
35#define DAS08_STATUS_REG 0x02
36#define DAS08_STATUS_AI_BUSY BIT(7)
37
38
39
40
41
42#define DAS08_STATUS_IRQ BIT(3)
43
44#define DAS08_STATUS_DI(x) (((x) & 0x70) >> 4)
45#define DAS08_CONTROL_REG 0x02
46
47
48
49
50#define DAS08_CONTROL_MUX_MASK 0x7
51#define DAS08_CONTROL_MUX(x) ((x) & DAS08_CONTROL_MUX_MASK)
52#define DAS08_CONTROL_INTE BIT(3)
53#define DAS08_CONTROL_DO_MASK 0xf0
54
55#define DAS08_CONTROL_DO(x) (((x) << 4) & DAS08_CONTROL_DO_MASK)
56
57
58
59
60
61
62#define DAS08_GAIN_REG 0x03
63
64#define DAS08JR_DI_REG 0x03
65#define DAS08JR_DO_REG 0x03
66
67#define DAS08JR_AO_LSB_REG(x) ((x) ? 0x06 : 0x04)
68
69#define DAS08JR_AO_MSB_REG(x) ((x) ? 0x07 : 0x05)
70
71
72
73
74#define DAS08JR_AO_UPDATE_REG 0x03
75
76
77#define DAS08AOX_AO_LSB_REG(x) ((x) ? 0x0a : 0x08)
78
79#define DAS08AOX_AO_MSB_REG(x) ((x) ? 0x0b : 0x09)
80
81
82
83
84#define DAS08AOX_AO_UPDATE_REG 0x08
85
86
87
88static const struct comedi_lrange das08_pgl_ai_range = {
89 9, {
90 BIP_RANGE(10),
91 BIP_RANGE(5),
92 BIP_RANGE(2.5),
93 BIP_RANGE(1.25),
94 BIP_RANGE(0.625),
95 UNI_RANGE(10),
96 UNI_RANGE(5),
97 UNI_RANGE(2.5),
98 UNI_RANGE(1.25)
99 }
100};
101
102static const struct comedi_lrange das08_pgh_ai_range = {
103 12, {
104 BIP_RANGE(10),
105 BIP_RANGE(5),
106 BIP_RANGE(1),
107 BIP_RANGE(0.5),
108 BIP_RANGE(0.1),
109 BIP_RANGE(0.05),
110 BIP_RANGE(0.01),
111 BIP_RANGE(0.005),
112 UNI_RANGE(10),
113 UNI_RANGE(1),
114 UNI_RANGE(0.1),
115 UNI_RANGE(0.01)
116 }
117};
118
119static const struct comedi_lrange das08_pgm_ai_range = {
120 9, {
121 BIP_RANGE(10),
122 BIP_RANGE(5),
123 BIP_RANGE(0.5),
124 BIP_RANGE(0.05),
125 BIP_RANGE(0.01),
126 UNI_RANGE(10),
127 UNI_RANGE(1),
128 UNI_RANGE(0.1),
129 UNI_RANGE(0.01)
130 }
131};
132
133static const struct comedi_lrange *const das08_ai_lranges[] = {
134 [das08_pg_none] = &range_unknown,
135 [das08_bipolar5] = &range_bipolar5,
136 [das08_pgh] = &das08_pgh_ai_range,
137 [das08_pgl] = &das08_pgl_ai_range,
138 [das08_pgm] = &das08_pgm_ai_range,
139};
140
141static const int das08_pgh_ai_gainlist[] = {
142 8, 0, 10, 2, 12, 4, 14, 6, 1, 3, 5, 7
143};
144static const int das08_pgl_ai_gainlist[] = { 8, 0, 2, 4, 6, 1, 3, 5, 7 };
145static const int das08_pgm_ai_gainlist[] = { 8, 0, 10, 12, 14, 9, 11, 13, 15 };
146
147static const int *const das08_ai_gainlists[] = {
148 [das08_pg_none] = NULL,
149 [das08_bipolar5] = NULL,
150 [das08_pgh] = das08_pgh_ai_gainlist,
151 [das08_pgl] = das08_pgl_ai_gainlist,
152 [das08_pgm] = das08_pgm_ai_gainlist,
153};
154
155static int das08_ai_eoc(struct comedi_device *dev,
156 struct comedi_subdevice *s,
157 struct comedi_insn *insn,
158 unsigned long context)
159{
160 unsigned int status;
161
162 status = inb(dev->iobase + DAS08_STATUS_REG);
163 if ((status & DAS08_STATUS_AI_BUSY) == 0)
164 return 0;
165 return -EBUSY;
166}
167
168static int das08_ai_insn_read(struct comedi_device *dev,
169 struct comedi_subdevice *s,
170 struct comedi_insn *insn, unsigned int *data)
171{
172 const struct das08_board_struct *board = dev->board_ptr;
173 struct das08_private_struct *devpriv = dev->private;
174 int n;
175 int chan;
176 int range;
177 int lsb, msb;
178 int ret;
179
180 chan = CR_CHAN(insn->chanspec);
181 range = CR_RANGE(insn->chanspec);
182
183
184 inb(dev->iobase + DAS08_AI_LSB_REG);
185 inb(dev->iobase + DAS08_AI_MSB_REG);
186
187
188
189 spin_lock(&dev->spinlock);
190 devpriv->do_mux_bits &= ~DAS08_CONTROL_MUX_MASK;
191 devpriv->do_mux_bits |= DAS08_CONTROL_MUX(chan);
192 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL_REG);
193 spin_unlock(&dev->spinlock);
194
195 if (devpriv->pg_gainlist) {
196
197 range = CR_RANGE(insn->chanspec);
198 outb(devpriv->pg_gainlist[range],
199 dev->iobase + DAS08_GAIN_REG);
200 }
201
202 for (n = 0; n < insn->n; n++) {
203
204 if (board->ai_nbits == 16)
205 if (inb(dev->iobase + DAS08_AI_MSB_REG) & 0x80)
206 dev_info(dev->class_dev, "over-range\n");
207
208
209 outb_p(0, dev->iobase + DAS08_AI_TRIG_REG);
210
211 ret = comedi_timeout(dev, s, insn, das08_ai_eoc, 0);
212 if (ret)
213 return ret;
214
215 msb = inb(dev->iobase + DAS08_AI_MSB_REG);
216 lsb = inb(dev->iobase + DAS08_AI_LSB_REG);
217 if (board->ai_encoding == das08_encode12) {
218 data[n] = (lsb >> 4) | (msb << 4);
219 } else if (board->ai_encoding == das08_pcm_encode12) {
220 data[n] = (msb << 8) + lsb;
221 } else if (board->ai_encoding == das08_encode16) {
222
223
224
225
226
227
228
229
230
231
232 unsigned int magnitude = lsb | ((msb & 0x7f) << 8);
233
234
235
236
237
238 if (msb & 0x80)
239 data[n] = BIT(15) + magnitude;
240 else
241 data[n] = BIT(15) - magnitude;
242 } else {
243 dev_err(dev->class_dev, "bug! unknown ai encoding\n");
244 return -1;
245 }
246 }
247
248 return n;
249}
250
251static int das08_di_insn_bits(struct comedi_device *dev,
252 struct comedi_subdevice *s,
253 struct comedi_insn *insn, unsigned int *data)
254{
255 data[0] = 0;
256 data[1] = DAS08_STATUS_DI(inb(dev->iobase + DAS08_STATUS_REG));
257
258 return insn->n;
259}
260
261static int das08_do_insn_bits(struct comedi_device *dev,
262 struct comedi_subdevice *s,
263 struct comedi_insn *insn, unsigned int *data)
264{
265 struct das08_private_struct *devpriv = dev->private;
266
267 if (comedi_dio_update_state(s, data)) {
268
269 spin_lock(&dev->spinlock);
270 devpriv->do_mux_bits &= ~DAS08_CONTROL_DO_MASK;
271 devpriv->do_mux_bits |= DAS08_CONTROL_DO(s->state);
272 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL_REG);
273 spin_unlock(&dev->spinlock);
274 }
275
276 data[1] = s->state;
277
278 return insn->n;
279}
280
281static int das08jr_di_insn_bits(struct comedi_device *dev,
282 struct comedi_subdevice *s,
283 struct comedi_insn *insn, unsigned int *data)
284{
285 data[0] = 0;
286 data[1] = inb(dev->iobase + DAS08JR_DI_REG);
287
288 return insn->n;
289}
290
291static int das08jr_do_insn_bits(struct comedi_device *dev,
292 struct comedi_subdevice *s,
293 struct comedi_insn *insn, unsigned int *data)
294{
295 if (comedi_dio_update_state(s, data))
296 outb(s->state, dev->iobase + DAS08JR_DO_REG);
297
298 data[1] = s->state;
299
300 return insn->n;
301}
302
303static void das08_ao_set_data(struct comedi_device *dev,
304 unsigned int chan, unsigned int data)
305{
306 const struct das08_board_struct *board = dev->board_ptr;
307 unsigned char lsb;
308 unsigned char msb;
309
310 lsb = data & 0xff;
311 msb = (data >> 8) & 0xff;
312 if (board->is_jr) {
313 outb(lsb, dev->iobase + DAS08JR_AO_LSB_REG(chan));
314 outb(msb, dev->iobase + DAS08JR_AO_MSB_REG(chan));
315
316 inb(dev->iobase + DAS08JR_AO_UPDATE_REG);
317 } else {
318 outb(lsb, dev->iobase + DAS08AOX_AO_LSB_REG(chan));
319 outb(msb, dev->iobase + DAS08AOX_AO_MSB_REG(chan));
320
321 inb(dev->iobase + DAS08AOX_AO_UPDATE_REG);
322 }
323}
324
325static int das08_ao_insn_write(struct comedi_device *dev,
326 struct comedi_subdevice *s,
327 struct comedi_insn *insn,
328 unsigned int *data)
329{
330 unsigned int chan = CR_CHAN(insn->chanspec);
331 unsigned int val = s->readback[chan];
332 int i;
333
334 for (i = 0; i < insn->n; i++) {
335 val = data[i];
336 das08_ao_set_data(dev, chan, val);
337 }
338 s->readback[chan] = val;
339
340 return insn->n;
341}
342
343int das08_common_attach(struct comedi_device *dev, unsigned long iobase)
344{
345 const struct das08_board_struct *board = dev->board_ptr;
346 struct das08_private_struct *devpriv = dev->private;
347 struct comedi_subdevice *s;
348 int ret;
349 int i;
350
351 dev->iobase = iobase;
352
353 dev->board_name = board->name;
354
355 ret = comedi_alloc_subdevices(dev, 6);
356 if (ret)
357 return ret;
358
359 s = &dev->subdevices[0];
360
361 if (board->ai_nbits) {
362 s->type = COMEDI_SUBD_AI;
363
364
365
366
367
368
369 s->subdev_flags = SDF_READABLE | SDF_GROUND;
370 s->n_chan = 8;
371 s->maxdata = (1 << board->ai_nbits) - 1;
372 s->range_table = das08_ai_lranges[board->ai_pg];
373 s->insn_read = das08_ai_insn_read;
374 devpriv->pg_gainlist = das08_ai_gainlists[board->ai_pg];
375 } else {
376 s->type = COMEDI_SUBD_UNUSED;
377 }
378
379 s = &dev->subdevices[1];
380
381 if (board->ao_nbits) {
382 s->type = COMEDI_SUBD_AO;
383 s->subdev_flags = SDF_WRITABLE;
384 s->n_chan = 2;
385 s->maxdata = (1 << board->ao_nbits) - 1;
386 s->range_table = &range_bipolar5;
387 s->insn_write = das08_ao_insn_write;
388
389 ret = comedi_alloc_subdev_readback(s);
390 if (ret)
391 return ret;
392
393
394 for (i = 0; i < s->n_chan; i++) {
395 s->readback[i] = s->maxdata / 2;
396 das08_ao_set_data(dev, i, s->readback[i]);
397 }
398 } else {
399 s->type = COMEDI_SUBD_UNUSED;
400 }
401
402 s = &dev->subdevices[2];
403
404 if (board->di_nchan) {
405 s->type = COMEDI_SUBD_DI;
406 s->subdev_flags = SDF_READABLE;
407 s->n_chan = board->di_nchan;
408 s->maxdata = 1;
409 s->range_table = &range_digital;
410 s->insn_bits = board->is_jr ? das08jr_di_insn_bits :
411 das08_di_insn_bits;
412 } else {
413 s->type = COMEDI_SUBD_UNUSED;
414 }
415
416 s = &dev->subdevices[3];
417
418 if (board->do_nchan) {
419 s->type = COMEDI_SUBD_DO;
420 s->subdev_flags = SDF_WRITABLE;
421 s->n_chan = board->do_nchan;
422 s->maxdata = 1;
423 s->range_table = &range_digital;
424 s->insn_bits = board->is_jr ? das08jr_do_insn_bits :
425 das08_do_insn_bits;
426 } else {
427 s->type = COMEDI_SUBD_UNUSED;
428 }
429
430 s = &dev->subdevices[4];
431
432 if (board->i8255_offset != 0) {
433 ret = subdev_8255_init(dev, s, NULL, board->i8255_offset);
434 if (ret)
435 return ret;
436 } else {
437 s->type = COMEDI_SUBD_UNUSED;
438 }
439
440
441 s = &dev->subdevices[5];
442 if (board->i8254_offset) {
443 dev->pacer = comedi_8254_init(dev->iobase + board->i8254_offset,
444 0, I8254_IO8, 0);
445 if (!dev->pacer)
446 return -ENOMEM;
447
448 comedi_8254_subdevice_init(s, dev->pacer);
449 } else {
450 s->type = COMEDI_SUBD_UNUSED;
451 }
452
453 return 0;
454}
455EXPORT_SYMBOL_GPL(das08_common_attach);
456
457static int __init das08_init(void)
458{
459 return 0;
460}
461module_init(das08_init);
462
463static void __exit das08_exit(void)
464{
465}
466module_exit(das08_exit);
467
468MODULE_AUTHOR("Comedi http://www.comedi.org");
469MODULE_DESCRIPTION("Comedi common DAS08 support module");
470MODULE_LICENSE("GPL");
471