2 * Comedi driver for Data Translation DT2811
4 * COMEDI - Linux Control and Measurement Device Interface
5 * Copyright (C) David A. Schleef <ds@schleef.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
20 * Description: Data Translation DT2811
22 * Devices: [Data Translation] DT2811-PGL (dt2811-pgl), DT2811-PGH (dt2811-pgh)
25 * Configuration options:
26 * [0] - I/O port base address
27 * [1] - IRQ (optional, needed for async command support)
28 * [2] - A/D reference (# of analog inputs)
29 * 0 = single-ended (16 channels)
30 * 1 = differential (8 channels)
31 * 2 = pseudo-differential (16 channels)
32 * [3] - A/D range (deprecated, see below)
33 * [4] - D/A 0 range (deprecated, see below)
34 * [5] - D/A 1 range (deprecated, see below)
37 * - A/D ranges are not programmable but the gain is. The AI subdevice has
38 * a range_table containing all the possible analog input range/gain
39 * options for the dt2811-pgh or dt2811-pgl. Use the range that matches
40 * your board configuration and the desired gain to correctly convert
41 * between data values and physical units and to set the correct output
43 * - D/A ranges are not programmable. The AO subdevice has a range_table
44 * containing all the possible analog output ranges. Use the range
45 * that matches your board configuration to convert between data
46 * values and physical units.
49 #include <linux/module.h>
50 #include <linux/interrupt.h>
51 #include <linux/delay.h>
53 #include "../comedidev.h"
58 #define DT2811_ADCSR_REG 0x00 /* r/w A/D Control/Status */
59 #define DT2811_ADCSR_ADDONE BIT(7) /* r 1=A/D conv done */
60 #define DT2811_ADCSR_ADERROR BIT(6) /* r 1=A/D error */
61 #define DT2811_ADCSR_ADBUSY BIT(5) /* r 1=A/D busy */
62 #define DT2811_ADCSR_CLRERROR BIT(4)
63 #define DT2811_ADCSR_DMAENB BIT(3) /* r/w 1=dma ena */
64 #define DT2811_ADCSR_INTENB BIT(2) /* r/w 1=interupts ena */
65 #define DT2811_ADCSR_ADMODE(x) (((x) & 0x3) << 0)
67 #define DT2811_ADGCR_REG 0x01 /* r/w A/D Gain/Channel */
68 #define DT2811_ADGCR_GAIN(x) (((x) & 0x3) << 6)
69 #define DT2811_ADGCR_CHAN(x) (((x) & 0xf) << 0)
71 #define DT2811_ADDATA_LO_REG 0x02 /* r A/D Data low byte */
72 #define DT2811_ADDATA_HI_REG 0x03 /* r A/D Data high byte */
74 #define DT2811_DADATA_LO_REG(x) (0x02 + ((x) * 2)) /* w D/A Data low */
75 #define DT2811_DADATA_HI_REG(x) (0x03 + ((x) * 2)) /* w D/A Data high */
77 #define DT2811_DI_REG 0x06 /* r Digital Input Port 0 */
78 #define DT2811_DO_REG 0x06 /* w Digital Output Port 1 */
80 #define DT2811_TMRCTR_REG 0x07 /* r/w Timer/Counter */
81 #define DT2811_TMRCTR_MANTISSA(x) (((x) & 0x7) << 3)
82 #define DT2811_TMRCTR_EXPONENT(x) (((x) & 0x7) << 0)
84 #define DT2811_OSC_BASE 1666 /* 600 kHz = 1666.6667ns */
87 * Timer frequency control:
88 * DT2811_TMRCTR_MANTISSA DT2811_TMRCTR_EXPONENT
89 * val divisor frequency val multiply divisor/divide frequency by
95 * 5 5 120 kHz 5 100000
96 * 6 6 100 kHz 6 1000000
97 * 7 12 50 kHz 7 10000000
99 const unsigned int dt2811_clk_dividers[] = {
100 1, 10, 2, 3, 4, 5, 6, 12
103 const unsigned int dt2811_clk_multipliers[] = {
104 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
108 * The Analog Input range is set using jumpers on the board.
112 * -2.5V to +2.5V In In
115 * The gain may be set to 1, 2, 4, or 8 (on the dt2811-pgh) or to
116 * 1, 10, 100, 500 (on the dt2811-pgl).
118 static const struct comedi_lrange dt2811_pgh_ai_ranges = {
120 BIP_RANGE(5), /* range 0: gain=1 */
121 BIP_RANGE(2.5), /* range 1: gain=2 */
122 BIP_RANGE(1.25), /* range 2: gain=4 */
123 BIP_RANGE(0.625), /* range 3: gain=8 */
125 BIP_RANGE(2.5), /* range 0+4: gain=1 */
126 BIP_RANGE(1.25), /* range 1+4: gain=2 */
127 BIP_RANGE(0.625), /* range 2+4: gain=4 */
128 BIP_RANGE(0.3125), /* range 3+4: gain=8 */
130 UNI_RANGE(5), /* range 0+8: gain=1 */
131 UNI_RANGE(2.5), /* range 1+8: gain=2 */
132 UNI_RANGE(1.25), /* range 2+8: gain=4 */
133 UNI_RANGE(0.625) /* range 3+8: gain=8 */
137 static const struct comedi_lrange dt2811_pgl_ai_ranges = {
139 BIP_RANGE(5), /* range 0: gain=1 */
140 BIP_RANGE(0.5), /* range 1: gain=10 */
141 BIP_RANGE(0.05), /* range 2: gain=100 */
142 BIP_RANGE(0.01), /* range 3: gain=500 */
144 BIP_RANGE(2.5), /* range 0+4: gain=1 */
145 BIP_RANGE(0.25), /* range 1+4: gain=10 */
146 BIP_RANGE(0.025), /* range 2+4: gain=100 */
147 BIP_RANGE(0.005), /* range 3+4: gain=500 */
149 UNI_RANGE(5), /* range 0+8: gain=1 */
150 UNI_RANGE(0.5), /* range 1+8: gain=10 */
151 UNI_RANGE(0.05), /* range 2+8: gain=100 */
152 UNI_RANGE(0.01) /* range 3+8: gain=500 */
157 * The Analog Output range is set per-channel using jumpers on the board.
159 * DAC0 Jumpers DAC1 Jumpers
160 * Output Range W5 W6 W7 W8 W1 W2 W3 W4
161 * -5V to +5V In Out In Out In Out In Out
162 * -2.5V to +2.5V In Out Out In In Out Out In
163 * 0 to +5V Out In Out In Out In Out In
165 static const struct comedi_lrange dt2811_ao_ranges = {
167 BIP_RANGE(5), /* default setting from factory */
173 struct dt2811_board {
175 unsigned int is_pgh:1;
178 static const struct dt2811_board dt2811_boards[] = {
180 .name = "dt2811-pgh",
183 .name = "dt2811-pgl",
187 struct dt2811_private {
188 unsigned int ai_divisor;
191 static unsigned int dt2811_ai_read_sample(struct comedi_device *dev,
192 struct comedi_subdevice *s)
196 val = inb(dev->iobase + DT2811_ADDATA_LO_REG) |
197 (inb(dev->iobase + DT2811_ADDATA_HI_REG) << 8);
199 return val & s->maxdata;
202 static irqreturn_t dt2811_interrupt(int irq, void *d)
204 struct comedi_device *dev = d;
205 struct comedi_subdevice *s = dev->read_subdev;
206 struct comedi_async *async = s->async;
207 struct comedi_cmd *cmd = &async->cmd;
213 status = inb(dev->iobase + DT2811_ADCSR_REG);
215 if (status & DT2811_ADCSR_ADERROR) {
216 async->events |= COMEDI_CB_OVERFLOW;
218 outb(status | DT2811_ADCSR_CLRERROR,
219 dev->iobase + DT2811_ADCSR_REG);
222 if (status & DT2811_ADCSR_ADDONE) {
225 val = dt2811_ai_read_sample(dev, s);
226 comedi_buf_write_samples(s, &val, 1);
229 if (cmd->stop_src == TRIG_COUNT && async->scans_done >= cmd->stop_arg)
230 async->events |= COMEDI_CB_EOA;
232 comedi_handle_events(dev, s);
237 static int dt2811_ai_cancel(struct comedi_device *dev,
238 struct comedi_subdevice *s)
244 * Loading a chanspec will trigger a conversion.
246 outb(DT2811_ADCSR_ADMODE(0), dev->iobase + DT2811_ADCSR_REG);
251 static void dt2811_ai_set_chanspec(struct comedi_device *dev,
252 unsigned int chanspec)
254 unsigned int chan = CR_CHAN(chanspec);
255 unsigned int range = CR_RANGE(chanspec);
257 outb(DT2811_ADGCR_CHAN(chan) | DT2811_ADGCR_GAIN(range),
258 dev->iobase + DT2811_ADGCR_REG);
261 static int dt2811_ai_cmd(struct comedi_device *dev,
262 struct comedi_subdevice *s)
264 struct dt2811_private *devpriv = dev->private;
265 struct comedi_cmd *cmd = &s->async->cmd;
268 if (cmd->start_src == TRIG_NOW) {
271 * Continuous conversion, internal trigger and clock
273 * This resets the trigger flip-flop, disabling A/D strobes.
274 * The timer/counter register is loaded with the division
275 * ratio which will give the required sample rate.
277 * Loading the first chanspec sets the trigger flip-flop,
278 * enabling the timer/counter. A/D strobes are then generated
279 * at the rate set by the internal clock/divider.
281 mode = DT2811_ADCSR_ADMODE(1);
282 } else { /* TRIG_EXT */
283 if (cmd->convert_src == TRIG_TIMER) {
286 * Continuous conversion, external trigger
288 * Similar to Mode 1, with the exception that the
289 * trigger flip-flop must be set by a negative edge
290 * on the external trigger input.
292 mode = DT2811_ADCSR_ADMODE(2);
293 } else { /* TRIG_EXT */
296 * Continuous conversion, external trigger, clock
298 * Similar to Mode 2, with the exception that the
299 * conversion rate is set by the frequency on the
300 * external clock/divider.
302 mode = DT2811_ADCSR_ADMODE(3);
305 outb(mode | DT2811_ADCSR_INTENB, dev->iobase + DT2811_ADCSR_REG);
308 outb(devpriv->ai_divisor, dev->iobase + DT2811_TMRCTR_REG);
310 /* load chanspec - enables timer */
311 dt2811_ai_set_chanspec(dev, cmd->chanlist[0]);
316 static unsigned int dt2811_ns_to_timer(unsigned int *nanosec,
319 unsigned long long ns = *nanosec;
320 unsigned int ns_lo = COMEDI_MIN_SPEED;
321 unsigned int ns_hi = 0;
322 unsigned int divisor_hi = 0;
323 unsigned int divisor_lo = 0;
328 * Work through all the divider/multiplier values to find the two
329 * closest divisors to generate the requested nanosecond timing.
331 for (_div = 0; _div <= 7; _div++) {
332 for (_mult = 0; _mult <= 7; _mult++) {
333 unsigned int div = dt2811_clk_dividers[_div];
334 unsigned int mult = dt2811_clk_multipliers[_mult];
335 unsigned long long divider = div * mult;
336 unsigned int divisor = DT2811_TMRCTR_MANTISSA(_div) |
337 DT2811_TMRCTR_EXPONENT(_mult);
340 * The timer can be configured to run at a slowest
341 * speed of 0.005hz (600 Khz/120000000), which requires
342 * 37-bits to represent the nanosecond value. Limit the
343 * slowest timing to what comedi handles (32-bits).
345 ns = divider * DT2811_OSC_BASE;
346 if (ns > COMEDI_MIN_SPEED)
349 /* Check for fastest found timing */
350 if (ns <= *nanosec && ns > ns_hi) {
352 divisor_hi = divisor;
354 /* Check for slowest found timing */
355 if (ns >= *nanosec && ns < ns_lo) {
357 divisor_lo = divisor;
363 * The slowest found timing will be invalid if the requested timing
364 * is faster than what can be generated by the timer. Fix it so that
365 * CMDF_ROUND_UP returns valid timing.
367 if (ns_lo == COMEDI_MIN_SPEED) {
369 divisor_lo = divisor_hi;
372 * The fastest found timing will be invalid if the requested timing
373 * is less than what can be generated by the timer. Fix it so that
374 * CMDF_ROUND_NEAREST and CMDF_ROUND_DOWN return valid timing.
378 divisor_hi = divisor_lo;
381 switch (flags & CMDF_ROUND_MASK) {
382 case CMDF_ROUND_NEAREST:
384 if (ns_hi - *nanosec < *nanosec - ns_lo) {
393 case CMDF_ROUND_DOWN:
399 static int dt2811_ai_cmdtest(struct comedi_device *dev,
400 struct comedi_subdevice *s,
401 struct comedi_cmd *cmd)
403 struct dt2811_private *devpriv = dev->private;
407 /* Step 1 : check if triggers are trivially valid */
409 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_EXT);
410 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_FOLLOW);
411 err |= comedi_check_trigger_src(&cmd->convert_src,
412 TRIG_TIMER | TRIG_EXT);
413 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
414 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
419 /* Step 2a : make sure trigger sources are unique */
421 err |= comedi_check_trigger_is_unique(cmd->start_src);
422 err |= comedi_check_trigger_is_unique(cmd->convert_src);
423 err |= comedi_check_trigger_is_unique(cmd->stop_src);
425 /* Step 2b : and mutually compatible */
427 if (cmd->convert_src == TRIG_EXT && cmd->start_src != TRIG_EXT)
433 /* Step 3: check if arguments are trivially valid */
435 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
436 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
437 if (cmd->convert_src == TRIG_TIMER)
438 err |= comedi_check_trigger_arg_min(&cmd->convert_arg, 12500);
439 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
441 if (cmd->stop_src == TRIG_COUNT)
442 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
444 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
449 /* Step 4: fix up any arguments */
451 if (cmd->convert_src == TRIG_TIMER) {
452 arg = cmd->convert_arg;
453 devpriv->ai_divisor = dt2811_ns_to_timer(&arg, cmd->flags);
454 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
455 } else { /* TRIG_EXT */
456 /* The convert_arg is used to set the divisor. */
457 devpriv->ai_divisor = cmd->convert_arg;
463 /* Step 5: check channel list if it exists */
468 static int dt2811_ai_eoc(struct comedi_device *dev,
469 struct comedi_subdevice *s,
470 struct comedi_insn *insn,
471 unsigned long context)
475 status = inb(dev->iobase + DT2811_ADCSR_REG);
476 if ((status & DT2811_ADCSR_ADBUSY) == 0)
481 static int dt2811_ai_insn_read(struct comedi_device *dev,
482 struct comedi_subdevice *s,
483 struct comedi_insn *insn,
489 /* We will already be in Mode 0 */
490 for (i = 0; i < insn->n; i++) {
491 /* load chanspec and trigger conversion */
492 dt2811_ai_set_chanspec(dev, insn->chanspec);
494 ret = comedi_timeout(dev, s, insn, dt2811_ai_eoc, 0);
498 data[i] = dt2811_ai_read_sample(dev, s);
504 static int dt2811_ao_insn_write(struct comedi_device *dev,
505 struct comedi_subdevice *s,
506 struct comedi_insn *insn,
509 unsigned int chan = CR_CHAN(insn->chanspec);
510 unsigned int val = s->readback[chan];
513 for (i = 0; i < insn->n; i++) {
515 outb(val & 0xff, dev->iobase + DT2811_DADATA_LO_REG(chan));
516 outb((val >> 8) & 0xff,
517 dev->iobase + DT2811_DADATA_HI_REG(chan));
519 s->readback[chan] = val;
524 static int dt2811_di_insn_bits(struct comedi_device *dev,
525 struct comedi_subdevice *s,
526 struct comedi_insn *insn,
529 data[1] = inb(dev->iobase + DT2811_DI_REG);
534 static int dt2811_do_insn_bits(struct comedi_device *dev,
535 struct comedi_subdevice *s,
536 struct comedi_insn *insn,
539 if (comedi_dio_update_state(s, data))
540 outb(s->state, dev->iobase + DT2811_DO_REG);
547 static void dt2811_reset(struct comedi_device *dev)
549 /* This is the initialization sequence from the users manual */
550 outb(DT2811_ADCSR_ADMODE(0), dev->iobase + DT2811_ADCSR_REG);
551 usleep_range(100, 1000);
552 inb(dev->iobase + DT2811_ADDATA_LO_REG);
553 inb(dev->iobase + DT2811_ADDATA_HI_REG);
554 outb(DT2811_ADCSR_ADMODE(0) | DT2811_ADCSR_CLRERROR,
555 dev->iobase + DT2811_ADCSR_REG);
558 static int dt2811_attach(struct comedi_device *dev, struct comedi_devconfig *it)
560 const struct dt2811_board *board = dev->board_ptr;
561 struct dt2811_private *devpriv;
562 struct comedi_subdevice *s;
565 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
569 ret = comedi_request_region(dev, it->options[0], 0x8);
575 /* IRQ's 2,3,5,7 are valid for async command support */
576 if (it->options[1] <= 7 && (BIT(it->options[1]) & 0xac)) {
577 ret = request_irq(it->options[1], dt2811_interrupt, 0,
578 dev->board_name, dev);
580 dev->irq = it->options[1];
583 ret = comedi_alloc_subdevices(dev, 4);
587 /* Analog Input subdevice */
588 s = &dev->subdevices[0];
589 s->type = COMEDI_SUBD_AI;
590 s->subdev_flags = SDF_READABLE |
591 (it->options[2] == 1) ? SDF_DIFF :
592 (it->options[2] == 2) ? SDF_COMMON : SDF_GROUND;
593 s->n_chan = (it->options[2] == 1) ? 8 : 16;
595 s->range_table = board->is_pgh ? &dt2811_pgh_ai_ranges
596 : &dt2811_pgl_ai_ranges;
597 s->insn_read = dt2811_ai_insn_read;
599 dev->read_subdev = s;
600 s->subdev_flags |= SDF_CMD_READ;
602 s->do_cmdtest = dt2811_ai_cmdtest;
603 s->do_cmd = dt2811_ai_cmd;
604 s->cancel = dt2811_ai_cancel;
607 /* Analog Output subdevice */
608 s = &dev->subdevices[1];
609 s->type = COMEDI_SUBD_AO;
610 s->subdev_flags = SDF_WRITABLE;
613 s->range_table = &dt2811_ao_ranges;
614 s->insn_write = dt2811_ao_insn_write;
616 ret = comedi_alloc_subdev_readback(s);
620 /* Digital Input subdevice */
621 s = &dev->subdevices[2];
622 s->type = COMEDI_SUBD_DI;
623 s->subdev_flags = SDF_READABLE;
626 s->range_table = &range_digital;
627 s->insn_bits = dt2811_di_insn_bits;
629 /* Digital Output subdevice */
630 s = &dev->subdevices[3];
631 s->type = COMEDI_SUBD_DO;
632 s->subdev_flags = SDF_WRITABLE;
635 s->range_table = &range_digital;
636 s->insn_bits = dt2811_do_insn_bits;
641 static struct comedi_driver dt2811_driver = {
642 .driver_name = "dt2811",
643 .module = THIS_MODULE,
644 .attach = dt2811_attach,
645 .detach = comedi_legacy_detach,
646 .board_name = &dt2811_boards[0].name,
647 .num_names = ARRAY_SIZE(dt2811_boards),
648 .offset = sizeof(struct dt2811_board),
650 module_comedi_driver(dt2811_driver);
652 MODULE_AUTHOR("Comedi http://www.comedi.org");
653 MODULE_DESCRIPTION("Comedi driver for Data Translation DT2811 series boards");
654 MODULE_LICENSE("GPL");