1 // SPDX-License-Identifier: GPL-2.0+
3 * Comedi driver for Data Translation DT2811
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) David A. Schleef <ds@schleef.org>
11 * Description: Data Translation DT2811
13 * Devices: [Data Translation] DT2811-PGL (dt2811-pgl), DT2811-PGH (dt2811-pgh)
16 * Configuration options:
17 * [0] - I/O port base address
18 * [1] - IRQ (optional, needed for async command support)
19 * [2] - A/D reference (# of analog inputs)
20 * 0 = single-ended (16 channels)
21 * 1 = differential (8 channels)
22 * 2 = pseudo-differential (16 channels)
23 * [3] - A/D range (deprecated, see below)
24 * [4] - D/A 0 range (deprecated, see below)
25 * [5] - D/A 1 range (deprecated, see below)
28 * - A/D ranges are not programmable but the gain is. The AI subdevice has
29 * a range_table containing all the possible analog input range/gain
30 * options for the dt2811-pgh or dt2811-pgl. Use the range that matches
31 * your board configuration and the desired gain to correctly convert
32 * between data values and physical units and to set the correct output
34 * - D/A ranges are not programmable. The AO subdevice has a range_table
35 * containing all the possible analog output ranges. Use the range
36 * that matches your board configuration to convert between data
37 * values and physical units.
40 #include <linux/module.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
44 #include "../comedidev.h"
49 #define DT2811_ADCSR_REG 0x00 /* r/w A/D Control/Status */
50 #define DT2811_ADCSR_ADDONE BIT(7) /* r 1=A/D conv done */
51 #define DT2811_ADCSR_ADERROR BIT(6) /* r 1=A/D error */
52 #define DT2811_ADCSR_ADBUSY BIT(5) /* r 1=A/D busy */
53 #define DT2811_ADCSR_CLRERROR BIT(4)
54 #define DT2811_ADCSR_DMAENB BIT(3) /* r/w 1=dma ena */
55 #define DT2811_ADCSR_INTENB BIT(2) /* r/w 1=interrupts ena */
56 #define DT2811_ADCSR_ADMODE(x) (((x) & 0x3) << 0)
58 #define DT2811_ADGCR_REG 0x01 /* r/w A/D Gain/Channel */
59 #define DT2811_ADGCR_GAIN(x) (((x) & 0x3) << 6)
60 #define DT2811_ADGCR_CHAN(x) (((x) & 0xf) << 0)
62 #define DT2811_ADDATA_LO_REG 0x02 /* r A/D Data low byte */
63 #define DT2811_ADDATA_HI_REG 0x03 /* r A/D Data high byte */
65 #define DT2811_DADATA_LO_REG(x) (0x02 + ((x) * 2)) /* w D/A Data low */
66 #define DT2811_DADATA_HI_REG(x) (0x03 + ((x) * 2)) /* w D/A Data high */
68 #define DT2811_DI_REG 0x06 /* r Digital Input Port 0 */
69 #define DT2811_DO_REG 0x06 /* w Digital Output Port 1 */
71 #define DT2811_TMRCTR_REG 0x07 /* r/w Timer/Counter */
72 #define DT2811_TMRCTR_MANTISSA(x) (((x) & 0x7) << 3)
73 #define DT2811_TMRCTR_EXPONENT(x) (((x) & 0x7) << 0)
75 #define DT2811_OSC_BASE 1666 /* 600 kHz = 1666.6667ns */
78 * Timer frequency control:
79 * DT2811_TMRCTR_MANTISSA DT2811_TMRCTR_EXPONENT
80 * val divisor frequency val multiply divisor/divide frequency by
86 * 5 5 120 kHz 5 100000
87 * 6 6 100 kHz 6 1000000
88 * 7 12 50 kHz 7 10000000
90 static const unsigned int dt2811_clk_dividers[] = {
91 1, 10, 2, 3, 4, 5, 6, 12
94 static const unsigned int dt2811_clk_multipliers[] = {
95 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
99 * The Analog Input range is set using jumpers on the board.
103 * -2.5V to +2.5V In In
106 * The gain may be set to 1, 2, 4, or 8 (on the dt2811-pgh) or to
107 * 1, 10, 100, 500 (on the dt2811-pgl).
109 static const struct comedi_lrange dt2811_pgh_ai_ranges = {
111 BIP_RANGE(5), /* range 0: gain=1 */
112 BIP_RANGE(2.5), /* range 1: gain=2 */
113 BIP_RANGE(1.25), /* range 2: gain=4 */
114 BIP_RANGE(0.625), /* range 3: gain=8 */
116 BIP_RANGE(2.5), /* range 0+4: gain=1 */
117 BIP_RANGE(1.25), /* range 1+4: gain=2 */
118 BIP_RANGE(0.625), /* range 2+4: gain=4 */
119 BIP_RANGE(0.3125), /* range 3+4: gain=8 */
121 UNI_RANGE(5), /* range 0+8: gain=1 */
122 UNI_RANGE(2.5), /* range 1+8: gain=2 */
123 UNI_RANGE(1.25), /* range 2+8: gain=4 */
124 UNI_RANGE(0.625) /* range 3+8: gain=8 */
128 static const struct comedi_lrange dt2811_pgl_ai_ranges = {
130 BIP_RANGE(5), /* range 0: gain=1 */
131 BIP_RANGE(0.5), /* range 1: gain=10 */
132 BIP_RANGE(0.05), /* range 2: gain=100 */
133 BIP_RANGE(0.01), /* range 3: gain=500 */
135 BIP_RANGE(2.5), /* range 0+4: gain=1 */
136 BIP_RANGE(0.25), /* range 1+4: gain=10 */
137 BIP_RANGE(0.025), /* range 2+4: gain=100 */
138 BIP_RANGE(0.005), /* range 3+4: gain=500 */
140 UNI_RANGE(5), /* range 0+8: gain=1 */
141 UNI_RANGE(0.5), /* range 1+8: gain=10 */
142 UNI_RANGE(0.05), /* range 2+8: gain=100 */
143 UNI_RANGE(0.01) /* range 3+8: gain=500 */
148 * The Analog Output range is set per-channel using jumpers on the board.
150 * DAC0 Jumpers DAC1 Jumpers
151 * Output Range W5 W6 W7 W8 W1 W2 W3 W4
152 * -5V to +5V In Out In Out In Out In Out
153 * -2.5V to +2.5V In Out Out In In Out Out In
154 * 0 to +5V Out In Out In Out In Out In
156 static const struct comedi_lrange dt2811_ao_ranges = {
158 BIP_RANGE(5), /* default setting from factory */
164 struct dt2811_board {
166 unsigned int is_pgh:1;
169 static const struct dt2811_board dt2811_boards[] = {
171 .name = "dt2811-pgh",
174 .name = "dt2811-pgl",
178 struct dt2811_private {
179 unsigned int ai_divisor;
182 static unsigned int dt2811_ai_read_sample(struct comedi_device *dev,
183 struct comedi_subdevice *s)
187 val = inb(dev->iobase + DT2811_ADDATA_LO_REG) |
188 (inb(dev->iobase + DT2811_ADDATA_HI_REG) << 8);
190 return val & s->maxdata;
193 static irqreturn_t dt2811_interrupt(int irq, void *d)
195 struct comedi_device *dev = d;
196 struct comedi_subdevice *s = dev->read_subdev;
197 struct comedi_async *async = s->async;
198 struct comedi_cmd *cmd = &async->cmd;
204 status = inb(dev->iobase + DT2811_ADCSR_REG);
206 if (status & DT2811_ADCSR_ADERROR) {
207 async->events |= COMEDI_CB_OVERFLOW;
209 outb(status | DT2811_ADCSR_CLRERROR,
210 dev->iobase + DT2811_ADCSR_REG);
213 if (status & DT2811_ADCSR_ADDONE) {
216 val = dt2811_ai_read_sample(dev, s);
217 comedi_buf_write_samples(s, &val, 1);
220 if (cmd->stop_src == TRIG_COUNT && async->scans_done >= cmd->stop_arg)
221 async->events |= COMEDI_CB_EOA;
223 comedi_handle_events(dev, s);
228 static int dt2811_ai_cancel(struct comedi_device *dev,
229 struct comedi_subdevice *s)
235 * Loading a chanspec will trigger a conversion.
237 outb(DT2811_ADCSR_ADMODE(0), dev->iobase + DT2811_ADCSR_REG);
242 static void dt2811_ai_set_chanspec(struct comedi_device *dev,
243 unsigned int chanspec)
245 unsigned int chan = CR_CHAN(chanspec);
246 unsigned int range = CR_RANGE(chanspec);
248 outb(DT2811_ADGCR_CHAN(chan) | DT2811_ADGCR_GAIN(range),
249 dev->iobase + DT2811_ADGCR_REG);
252 static int dt2811_ai_cmd(struct comedi_device *dev,
253 struct comedi_subdevice *s)
255 struct dt2811_private *devpriv = dev->private;
256 struct comedi_cmd *cmd = &s->async->cmd;
259 if (cmd->start_src == TRIG_NOW) {
262 * Continuous conversion, internal trigger and clock
264 * This resets the trigger flip-flop, disabling A/D strobes.
265 * The timer/counter register is loaded with the division
266 * ratio which will give the required sample rate.
268 * Loading the first chanspec sets the trigger flip-flop,
269 * enabling the timer/counter. A/D strobes are then generated
270 * at the rate set by the internal clock/divider.
272 mode = DT2811_ADCSR_ADMODE(1);
273 } else { /* TRIG_EXT */
274 if (cmd->convert_src == TRIG_TIMER) {
277 * Continuous conversion, external trigger
279 * Similar to Mode 1, with the exception that the
280 * trigger flip-flop must be set by a negative edge
281 * on the external trigger input.
283 mode = DT2811_ADCSR_ADMODE(2);
284 } else { /* TRIG_EXT */
287 * Continuous conversion, external trigger, clock
289 * Similar to Mode 2, with the exception that the
290 * conversion rate is set by the frequency on the
291 * external clock/divider.
293 mode = DT2811_ADCSR_ADMODE(3);
296 outb(mode | DT2811_ADCSR_INTENB, dev->iobase + DT2811_ADCSR_REG);
299 outb(devpriv->ai_divisor, dev->iobase + DT2811_TMRCTR_REG);
301 /* load chanspec - enables timer */
302 dt2811_ai_set_chanspec(dev, cmd->chanlist[0]);
307 static unsigned int dt2811_ns_to_timer(unsigned int *nanosec,
310 unsigned long long ns;
311 unsigned int ns_lo = COMEDI_MIN_SPEED;
312 unsigned int ns_hi = 0;
313 unsigned int divisor_hi = 0;
314 unsigned int divisor_lo = 0;
319 * Work through all the divider/multiplier values to find the two
320 * closest divisors to generate the requested nanosecond timing.
322 for (_div = 0; _div <= 7; _div++) {
323 for (_mult = 0; _mult <= 7; _mult++) {
324 unsigned int div = dt2811_clk_dividers[_div];
325 unsigned int mult = dt2811_clk_multipliers[_mult];
326 unsigned long long divider = div * mult;
327 unsigned int divisor = DT2811_TMRCTR_MANTISSA(_div) |
328 DT2811_TMRCTR_EXPONENT(_mult);
331 * The timer can be configured to run at a slowest
332 * speed of 0.005hz (600 Khz/120000000), which requires
333 * 37-bits to represent the nanosecond value. Limit the
334 * slowest timing to what comedi handles (32-bits).
336 ns = divider * DT2811_OSC_BASE;
337 if (ns > COMEDI_MIN_SPEED)
340 /* Check for fastest found timing */
341 if (ns <= *nanosec && ns > ns_hi) {
343 divisor_hi = divisor;
345 /* Check for slowest found timing */
346 if (ns >= *nanosec && ns < ns_lo) {
348 divisor_lo = divisor;
354 * The slowest found timing will be invalid if the requested timing
355 * is faster than what can be generated by the timer. Fix it so that
356 * CMDF_ROUND_UP returns valid timing.
358 if (ns_lo == COMEDI_MIN_SPEED) {
360 divisor_lo = divisor_hi;
363 * The fastest found timing will be invalid if the requested timing
364 * is less than what can be generated by the timer. Fix it so that
365 * CMDF_ROUND_NEAREST and CMDF_ROUND_DOWN return valid timing.
369 divisor_hi = divisor_lo;
372 switch (flags & CMDF_ROUND_MASK) {
373 case CMDF_ROUND_NEAREST:
375 if (ns_hi - *nanosec < *nanosec - ns_lo) {
384 case CMDF_ROUND_DOWN:
390 static int dt2811_ai_cmdtest(struct comedi_device *dev,
391 struct comedi_subdevice *s,
392 struct comedi_cmd *cmd)
394 struct dt2811_private *devpriv = dev->private;
398 /* Step 1 : check if triggers are trivially valid */
400 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_EXT);
401 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_FOLLOW);
402 err |= comedi_check_trigger_src(&cmd->convert_src,
403 TRIG_TIMER | TRIG_EXT);
404 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
405 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
410 /* Step 2a : make sure trigger sources are unique */
412 err |= comedi_check_trigger_is_unique(cmd->start_src);
413 err |= comedi_check_trigger_is_unique(cmd->convert_src);
414 err |= comedi_check_trigger_is_unique(cmd->stop_src);
416 /* Step 2b : and mutually compatible */
418 if (cmd->convert_src == TRIG_EXT && cmd->start_src != TRIG_EXT)
424 /* Step 3: check if arguments are trivially valid */
426 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
427 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
428 if (cmd->convert_src == TRIG_TIMER)
429 err |= comedi_check_trigger_arg_min(&cmd->convert_arg, 12500);
430 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
432 if (cmd->stop_src == TRIG_COUNT)
433 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
435 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
440 /* Step 4: fix up any arguments */
442 if (cmd->convert_src == TRIG_TIMER) {
443 arg = cmd->convert_arg;
444 devpriv->ai_divisor = dt2811_ns_to_timer(&arg, cmd->flags);
445 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
446 } else { /* TRIG_EXT */
447 /* The convert_arg is used to set the divisor. */
448 devpriv->ai_divisor = cmd->convert_arg;
454 /* Step 5: check channel list if it exists */
459 static int dt2811_ai_eoc(struct comedi_device *dev,
460 struct comedi_subdevice *s,
461 struct comedi_insn *insn,
462 unsigned long context)
466 status = inb(dev->iobase + DT2811_ADCSR_REG);
467 if ((status & DT2811_ADCSR_ADBUSY) == 0)
472 static int dt2811_ai_insn_read(struct comedi_device *dev,
473 struct comedi_subdevice *s,
474 struct comedi_insn *insn,
480 /* We will already be in Mode 0 */
481 for (i = 0; i < insn->n; i++) {
482 /* load chanspec and trigger conversion */
483 dt2811_ai_set_chanspec(dev, insn->chanspec);
485 ret = comedi_timeout(dev, s, insn, dt2811_ai_eoc, 0);
489 data[i] = dt2811_ai_read_sample(dev, s);
495 static int dt2811_ao_insn_write(struct comedi_device *dev,
496 struct comedi_subdevice *s,
497 struct comedi_insn *insn,
500 unsigned int chan = CR_CHAN(insn->chanspec);
501 unsigned int val = s->readback[chan];
504 for (i = 0; i < insn->n; i++) {
506 outb(val & 0xff, dev->iobase + DT2811_DADATA_LO_REG(chan));
507 outb((val >> 8) & 0xff,
508 dev->iobase + DT2811_DADATA_HI_REG(chan));
510 s->readback[chan] = val;
515 static int dt2811_di_insn_bits(struct comedi_device *dev,
516 struct comedi_subdevice *s,
517 struct comedi_insn *insn,
520 data[1] = inb(dev->iobase + DT2811_DI_REG);
525 static int dt2811_do_insn_bits(struct comedi_device *dev,
526 struct comedi_subdevice *s,
527 struct comedi_insn *insn,
530 if (comedi_dio_update_state(s, data))
531 outb(s->state, dev->iobase + DT2811_DO_REG);
538 static void dt2811_reset(struct comedi_device *dev)
540 /* This is the initialization sequence from the users manual */
541 outb(DT2811_ADCSR_ADMODE(0), dev->iobase + DT2811_ADCSR_REG);
542 usleep_range(100, 1000);
543 inb(dev->iobase + DT2811_ADDATA_LO_REG);
544 inb(dev->iobase + DT2811_ADDATA_HI_REG);
545 outb(DT2811_ADCSR_ADMODE(0) | DT2811_ADCSR_CLRERROR,
546 dev->iobase + DT2811_ADCSR_REG);
549 static int dt2811_attach(struct comedi_device *dev, struct comedi_devconfig *it)
551 const struct dt2811_board *board = dev->board_ptr;
552 struct dt2811_private *devpriv;
553 struct comedi_subdevice *s;
556 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
560 ret = comedi_request_region(dev, it->options[0], 0x8);
566 /* IRQ's 2,3,5,7 are valid for async command support */
567 if (it->options[1] <= 7 && (BIT(it->options[1]) & 0xac)) {
568 ret = request_irq(it->options[1], dt2811_interrupt, 0,
569 dev->board_name, dev);
571 dev->irq = it->options[1];
574 ret = comedi_alloc_subdevices(dev, 4);
578 /* Analog Input subdevice */
579 s = &dev->subdevices[0];
580 s->type = COMEDI_SUBD_AI;
581 s->subdev_flags = SDF_READABLE |
582 ((it->options[2] == 1) ? SDF_DIFF :
583 (it->options[2] == 2) ? SDF_COMMON : SDF_GROUND);
584 s->n_chan = (it->options[2] == 1) ? 8 : 16;
586 s->range_table = board->is_pgh ? &dt2811_pgh_ai_ranges
587 : &dt2811_pgl_ai_ranges;
588 s->insn_read = dt2811_ai_insn_read;
590 dev->read_subdev = s;
591 s->subdev_flags |= SDF_CMD_READ;
593 s->do_cmdtest = dt2811_ai_cmdtest;
594 s->do_cmd = dt2811_ai_cmd;
595 s->cancel = dt2811_ai_cancel;
598 /* Analog Output subdevice */
599 s = &dev->subdevices[1];
600 s->type = COMEDI_SUBD_AO;
601 s->subdev_flags = SDF_WRITABLE;
604 s->range_table = &dt2811_ao_ranges;
605 s->insn_write = dt2811_ao_insn_write;
607 ret = comedi_alloc_subdev_readback(s);
611 /* Digital Input subdevice */
612 s = &dev->subdevices[2];
613 s->type = COMEDI_SUBD_DI;
614 s->subdev_flags = SDF_READABLE;
617 s->range_table = &range_digital;
618 s->insn_bits = dt2811_di_insn_bits;
620 /* Digital Output subdevice */
621 s = &dev->subdevices[3];
622 s->type = COMEDI_SUBD_DO;
623 s->subdev_flags = SDF_WRITABLE;
626 s->range_table = &range_digital;
627 s->insn_bits = dt2811_do_insn_bits;
632 static struct comedi_driver dt2811_driver = {
633 .driver_name = "dt2811",
634 .module = THIS_MODULE,
635 .attach = dt2811_attach,
636 .detach = comedi_legacy_detach,
637 .board_name = &dt2811_boards[0].name,
638 .num_names = ARRAY_SIZE(dt2811_boards),
639 .offset = sizeof(struct dt2811_board),
641 module_comedi_driver(dt2811_driver);
643 MODULE_AUTHOR("Comedi https://www.comedi.org");
644 MODULE_DESCRIPTION("Comedi driver for Data Translation DT2811 series boards");
645 MODULE_LICENSE("GPL");