2 comedi/drivers/dt2811.c
3 Hardware driver for Data Translation DT2811
5 COMEDI - Linux Control and Measurement Device Interface
7 Base Version - David A. Schleef <ds@schleef.org>
8 December 1998 - Updated to work. David does not have a DT2811
9 board any longer so this was suffering from bitrot.
10 Updated performed by ...
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 Description: Data Translation DT2811
30 Devices: [Data Translation] DT2811-PGL (dt2811-pgl), DT2811-PGH (dt2811-pgh)
33 Configuration options:
34 [0] - I/O port base address
35 [1] - IRQ, although this is currently unused
39 2 = pseudo-differential (common reference)
44 [4] - D/A 0 range (same choices)
45 [4] - D/A 1 range (same choices)
48 #include <linux/interrupt.h>
49 #include "../comedidev.h"
51 #include <linux/ioport.h>
53 static const char *driver_name = "dt2811";
55 static const struct comedi_lrange range_dt2811_pgh_ai_5_unipolar = {
64 static const struct comedi_lrange range_dt2811_pgh_ai_2_5_bipolar = {
69 RANGE(-0.3125, 0.3125)
73 static const struct comedi_lrange range_dt2811_pgh_ai_5_bipolar = {
82 static const struct comedi_lrange range_dt2811_pgl_ai_5_unipolar = {
91 static const struct comedi_lrange range_dt2811_pgl_ai_2_5_bipolar = {
100 static const struct comedi_lrange range_dt2811_pgl_ai_5_bipolar = {
111 0x00 ADCSR R/W A/D Control/Status Register
112 bit 7 - (R) 1 indicates A/D conversion done
113 reading ADDAT clears bit
115 bit 6 - (R) 1 indicates A/D error
117 bit 5 - (R) 1 indicates A/D busy, cleared at end
123 bit 2 - (R/W) 1 indicates interrupts enabled
124 bits 1,0 - (R/W) mode bits
125 00 single conversion on ADGCR load
126 01 continuous conversion, internal clock,
127 (clock enabled on ADGCR load)
128 10 continuous conversion, internal clock,
130 11 continuous conversion, external clock,
133 0x01 ADGCR R/W A/D Gain/Channel Register
134 bit 6,7 - (R/W) gain select
135 00 gain=1, both PGH, PGL models
136 01 gain=2 PGH, 10 PGL
137 10 gain=4 PGH, 100 PGL
138 11 gain=8 PGH, 500 PGL
140 bit 3-0 - (R/W) channel select
141 channel number from 0-15
143 0x02,0x03 (R) ADDAT A/D Data Register
144 (W) DADAT0 D/A Data Register 0
148 0x04,0x05 (W) DADAT0 D/A Data Register 1
150 0x06 (R) DIO0 Digital Input Port 0
151 (W) DIO1 Digital Output Port 1
153 0x07 TMRCTR (R/W) Timer/Counter Register
155 bits 5-3 - Timer frequency control (mantissa)
156 543 divisor freqency (kHz)
165 bits 2-0 - Timer frequency control (exponent)
166 210 multiply divisor/divide frequency by
178 #define TIMEOUT 10000
180 #define DT2811_SIZE 8
182 #define DT2811_ADCSR 0
183 #define DT2811_ADGCR 1
184 #define DT2811_ADDATLO 2
185 #define DT2811_ADDATHI 3
186 #define DT2811_DADAT0LO 2
187 #define DT2811_DADAT0HI 3
188 #define DT2811_DADAT1LO 4
189 #define DT2811_DADAT1HI 5
191 #define DT2811_TMRCTR 7
199 #define DT2811_ADDONE 0x80
200 #define DT2811_ADERROR 0x40
201 #define DT2811_ADBUSY 0x20
202 #define DT2811_CLRERROR 0x10
203 #define DT2811_INTENB 0x04
204 #define DT2811_ADMODE 0x03
206 struct dt2811_board {
209 const struct comedi_lrange *bip_5;
210 const struct comedi_lrange *bip_2_5;
211 const struct comedi_lrange *unip_5;
214 static const struct dt2811_board boardtypes[] = {
216 &range_dt2811_pgh_ai_5_bipolar,
217 &range_dt2811_pgh_ai_2_5_bipolar,
218 &range_dt2811_pgh_ai_5_unipolar,
221 &range_dt2811_pgl_ai_5_bipolar,
222 &range_dt2811_pgl_ai_2_5_bipolar,
223 &range_dt2811_pgl_ai_5_unipolar,
227 #define this_board ((const struct dt2811_board *)dev->board_ptr)
229 static int dt2811_attach(struct comedi_device *dev,
230 struct comedi_devconfig *it);
231 static int dt2811_detach(struct comedi_device *dev);
232 static struct comedi_driver driver_dt2811 = {
233 .driver_name = "dt2811",
234 .module = THIS_MODULE,
235 .attach = dt2811_attach,
236 .detach = dt2811_detach,
237 .board_name = &boardtypes[0].name,
238 .num_names = ARRAY_SIZE(boardtypes),
239 .offset = sizeof(struct dt2811_board),
242 COMEDI_INITCLEANUP(driver_dt2811);
244 static int dt2811_ai_insn(struct comedi_device *dev, struct comedi_subdevice *s,
245 struct comedi_insn *insn, unsigned int *data);
246 static int dt2811_ao_insn(struct comedi_device *dev, struct comedi_subdevice *s,
247 struct comedi_insn *insn, unsigned int *data);
248 static int dt2811_ao_insn_read(struct comedi_device *dev,
249 struct comedi_subdevice *s,
250 struct comedi_insn *insn, unsigned int *data);
251 static int dt2811_di_insn_bits(struct comedi_device *dev,
252 struct comedi_subdevice *s,
253 struct comedi_insn *insn, unsigned int *data);
254 static int dt2811_do_insn_bits(struct comedi_device *dev,
255 struct comedi_subdevice *s,
256 struct comedi_insn *insn, unsigned int *data);
258 enum { card_2811_pgh, card_2811_pgl };
260 struct dt2811_private {
264 adc_singleended, adc_diff, adc_pseudo_diff
267 dac_bipolar_5, dac_bipolar_2_5, dac_unipolar_5
269 const struct comedi_lrange *range_type_list[2];
270 unsigned int ao_readback[2];
273 #define devpriv ((struct dt2811_private *)dev->private)
275 static const struct comedi_lrange *dac_range_types[] = {
281 #define DT2811_TIMEOUT 5
284 static irqreturn_t dt2811_interrupt(int irq, void *d)
288 struct comedi_device *dev = d;
290 if (!dev->attached) {
291 comedi_error(dev, "spurious interrupt");
295 lo = inb(dev->iobase + DT2811_ADDATLO);
296 hi = inb(dev->iobase + DT2811_ADDATHI);
298 data = lo + (hi << 8);
300 if (!(--devpriv->ntrig)) {
301 /* how to turn off acquisition */
302 s->async->events |= COMEDI_SB_EOA;
304 comedi_event(dev, s);
310 options[0] Board base address
312 options[2] Input configuration
315 2 == pseudo-differential
316 options[3] Analog input range configuration
317 0 == bipolar 5 (-5V -- +5V)
318 1 == bipolar 2.5V (-2.5V -- +2.5V)
319 2 == unipolar 5V (0V -- +5V)
320 options[4] Analog output 0 range configuration
321 0 == bipolar 5 (-5V -- +5V)
322 1 == bipolar 2.5V (-2.5V -- +2.5V)
323 2 == unipolar 5V (0V -- +5V)
324 options[5] Analog output 1 range configuration
325 0 == bipolar 5 (-5V -- +5V)
326 1 == bipolar 2.5V (-2.5V -- +2.5V)
327 2 == unipolar 5V (0V -- +5V)
330 static int dt2811_attach(struct comedi_device *dev, struct comedi_devconfig *it)
333 /* unsigned long irqs; */
337 struct comedi_subdevice *s;
338 unsigned long iobase;
340 iobase = it->options[0];
342 printk(KERN_INFO "comedi%d: dt2811:base=0x%04lx\n", dev->minor, iobase);
344 if (!request_region(iobase, DT2811_SIZE, driver_name)) {
345 printk(KERN_ERR "I/O port conflict\n");
349 dev->iobase = iobase;
350 dev->board_name = this_board->name;
353 outb(0, dev->iobase + DT2811_ADCSR);
355 i = inb(dev->iobase + DT2811_ADDATLO);
356 i = inb(dev->iobase + DT2811_ADDATHI);
360 irq = it->options[1];
364 irqs = probe_irq_on();
366 outb(DT2811_CLRERROR | DT2811_INTENB,
367 dev->iobase + DT2811_ADCSR);
368 outb(0, dev->iobase + DT2811_ADGCR);
372 irq = probe_irq_off(irqs);
373 restore_flags(flags);
375 /*outb(DT2811_CLRERROR|DT2811_INTENB,
376 dev->iobase+DT2811_ADCSR);*/
378 if (inb(dev->iobase + DT2811_ADCSR) & DT2811_ADERROR)
379 printk(KERN_ERR "error probing irq (bad)\n");
382 i = inb(dev->iobase + DT2811_ADDATLO);
383 i = inb(dev->iobase + DT2811_ADDATHI);
384 printk(KERN_INFO "(irq = %d)\n", irq);
385 ret = request_irq(irq, dt2811_interrupt, 0,
390 } else if (irq == 0) {
391 printk(KERN_INFO "(no irq)\n");
393 printk(KERN_ERR "( multiple irq's -- this is bad! )\n");
398 ret = alloc_subdevices(dev, 4);
402 ret = alloc_private(dev, sizeof(struct dt2811_private));
406 switch (it->options[2]) {
408 devpriv->adc_mux = adc_singleended;
411 devpriv->adc_mux = adc_diff;
414 devpriv->adc_mux = adc_pseudo_diff;
417 devpriv->adc_mux = adc_singleended;
420 switch (it->options[4]) {
422 devpriv->dac_range[0] = dac_bipolar_5;
425 devpriv->dac_range[0] = dac_bipolar_2_5;
428 devpriv->dac_range[0] = dac_unipolar_5;
431 devpriv->dac_range[0] = dac_bipolar_5;
434 switch (it->options[5]) {
436 devpriv->dac_range[1] = dac_bipolar_5;
439 devpriv->dac_range[1] = dac_bipolar_2_5;
442 devpriv->dac_range[1] = dac_unipolar_5;
445 devpriv->dac_range[1] = dac_bipolar_5;
449 s = dev->subdevices + 0;
450 /* initialize the ADC subdevice */
451 s->type = COMEDI_SUBD_AI;
452 s->subdev_flags = SDF_READABLE | SDF_GROUND;
453 s->n_chan = devpriv->adc_mux == adc_diff ? 8 : 16;
454 s->insn_read = dt2811_ai_insn;
456 switch (it->options[3]) {
459 s->range_table = this_board->bip_5;
462 s->range_table = this_board->bip_2_5;
465 s->range_table = this_board->unip_5;
469 s = dev->subdevices + 1;
471 s->type = COMEDI_SUBD_AO;
472 s->subdev_flags = SDF_WRITABLE;
474 s->insn_write = dt2811_ao_insn;
475 s->insn_read = dt2811_ao_insn_read;
477 s->range_table_list = devpriv->range_type_list;
478 devpriv->range_type_list[0] = dac_range_types[devpriv->dac_range[0]];
479 devpriv->range_type_list[1] = dac_range_types[devpriv->dac_range[1]];
481 s = dev->subdevices + 2;
483 s->type = COMEDI_SUBD_DI;
484 s->subdev_flags = SDF_READABLE;
486 s->insn_bits = dt2811_di_insn_bits;
488 s->range_table = &range_digital;
490 s = dev->subdevices + 3;
492 s->type = COMEDI_SUBD_DO;
493 s->subdev_flags = SDF_WRITABLE;
495 s->insn_bits = dt2811_do_insn_bits;
498 s->range_table = &range_digital;
503 static int dt2811_detach(struct comedi_device *dev)
505 printk(KERN_INFO "comedi%d: dt2811: remove\n", dev->minor);
508 free_irq(dev->irq, dev);
510 release_region(dev->iobase, DT2811_SIZE);
515 static int dt2811_ai_insn(struct comedi_device *dev, struct comedi_subdevice *s,
516 struct comedi_insn *insn, unsigned int *data)
518 int chan = CR_CHAN(insn->chanspec);
519 int timeout = DT2811_TIMEOUT;
522 for (i = 0; i < insn->n; i++) {
523 outb(chan, dev->iobase + DT2811_ADGCR);
526 && inb(dev->iobase + DT2811_ADCSR) & DT2811_ADBUSY)
531 data[i] = inb(dev->iobase + DT2811_ADDATLO);
532 data[i] |= inb(dev->iobase + DT2811_ADDATHI) << 8;
540 /* Wow. This is code from the Comedi stone age. But it hasn't been
541 * replaced, so I'll let it stay. */
542 int dt2811_adtrig(kdev_t minor, comedi_adtrig *adtrig)
544 struct comedi_device *dev = comedi_devices + minor;
548 dev->curadchan = adtrig->chan;
549 switch (dev->i_admode) {
551 dev->ntrig = adtrig->n - 1;
553 /*printk("dt2811: AD soft trigger\n"); */
554 /*outb(DT2811_CLRERROR|DT2811_INTENB,
555 dev->iobase+DT2811_ADCSR); */
556 outb(dev->curadchan, dev->iobase + DT2811_ADGCR);
557 do_gettimeofday(&trigtime);
560 dev->ntrig = adtrig->n;
568 static int dt2811_ao_insn(struct comedi_device *dev, struct comedi_subdevice *s,
569 struct comedi_insn *insn, unsigned int *data)
574 chan = CR_CHAN(insn->chanspec);
576 for (i = 0; i < insn->n; i++) {
577 outb(data[i] & 0xff, dev->iobase + DT2811_DADAT0LO + 2 * chan);
578 outb((data[i] >> 8) & 0xff,
579 dev->iobase + DT2811_DADAT0HI + 2 * chan);
580 devpriv->ao_readback[chan] = data[i];
586 static int dt2811_ao_insn_read(struct comedi_device *dev,
587 struct comedi_subdevice *s,
588 struct comedi_insn *insn, unsigned int *data)
593 chan = CR_CHAN(insn->chanspec);
595 for (i = 0; i < insn->n; i++)
596 data[i] = devpriv->ao_readback[chan];
601 static int dt2811_di_insn_bits(struct comedi_device *dev,
602 struct comedi_subdevice *s,
603 struct comedi_insn *insn, unsigned int *data)
608 data[1] = inb(dev->iobase + DT2811_DIO);
613 static int dt2811_do_insn_bits(struct comedi_device *dev,
614 struct comedi_subdevice *s,
615 struct comedi_insn *insn, unsigned int *data)
620 s->state &= ~data[0];
621 s->state |= data[0] & data[1];
622 outb(s->state, dev->iobase + DT2811_DIO);