3 * Comedi driver for DAS6402 compatible boards
4 * Copyright(c) 2014 H Hartley Sweeten <hsweeten@visionengravers.com>
6 * Rewrite of an experimental driver by:
7 * Copyright (C) 1999 Oystein Svendsen <svendsen@pvv.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
22 * Description: Keithley Metrabyte DAS6402 (& compatibles)
23 * Devices: [Keithley Metrabyte] DAS6402-12 (das6402-12),
24 * DAS6402-16 (das6402-16)
25 * Author: H Hartley Sweeten <hsweeten@visionengravers.com>
26 * Updated: Fri, 14 Mar 2014 10:18:43 -0700
29 * Configuration Options:
30 * [0] - I/O base address
31 * [1] - IRQ (optional, needed for async command support)
34 #include <linux/module.h>
35 #include <linux/interrupt.h>
37 #include "../comedidev.h"
39 #include "comedi_8254.h"
44 #define DAS6402_AI_DATA_REG 0x00
45 #define DAS6402_AI_MUX_REG 0x02
46 #define DAS6402_AI_MUX_LO(x) (((x) & 0x3f) << 0)
47 #define DAS6402_AI_MUX_HI(x) (((x) & 0x3f) << 8)
48 #define DAS6402_DI_DO_REG 0x03
49 #define DAS6402_AO_DATA_REG(x) (0x04 + ((x) * 2))
50 #define DAS6402_AO_LSB_REG(x) (0x04 + ((x) * 2))
51 #define DAS6402_AO_MSB_REG(x) (0x05 + ((x) * 2))
52 #define DAS6402_STATUS_REG 0x08
53 #define DAS6402_STATUS_FFNE (1 << 0)
54 #define DAS6402_STATUS_FHALF (1 << 1)
55 #define DAS6402_STATUS_FFULL (1 << 2)
56 #define DAS6402_STATUS_XINT (1 << 3)
57 #define DAS6402_STATUS_INT (1 << 4)
58 #define DAS6402_STATUS_XTRIG (1 << 5)
59 #define DAS6402_STATUS_INDGT (1 << 6)
60 #define DAS6402_STATUS_10MHZ (1 << 7)
61 #define DAS6402_STATUS_W_CLRINT (1 << 0)
62 #define DAS6402_STATUS_W_CLRXTR (1 << 1)
63 #define DAS6402_STATUS_W_CLRXIN (1 << 2)
64 #define DAS6402_STATUS_W_EXTEND (1 << 4)
65 #define DAS6402_STATUS_W_ARMED (1 << 5)
66 #define DAS6402_STATUS_W_POSTMODE (1 << 6)
67 #define DAS6402_STATUS_W_10MHZ (1 << 7)
68 #define DAS6402_CTRL_REG 0x09
69 #define DAS6402_CTRL_SOFT_TRIG (0 << 0)
70 #define DAS6402_CTRL_EXT_FALL_TRIG (1 << 0)
71 #define DAS6402_CTRL_EXT_RISE_TRIG (2 << 0)
72 #define DAS6402_CTRL_PACER_TRIG (3 << 0)
73 #define DAS6402_CTRL_BURSTEN (1 << 2)
74 #define DAS6402_CTRL_XINTE (1 << 3)
75 #define DAS6402_CTRL_IRQ(x) ((x) << 4)
76 #define DAS6402_CTRL_INTE (1 << 7)
77 #define DAS6402_TRIG_REG 0x0a
78 #define DAS6402_TRIG_TGEN (1 << 0)
79 #define DAS6402_TRIG_TGSEL (1 << 1)
80 #define DAS6402_TRIG_TGPOL (1 << 2)
81 #define DAS6402_TRIG_PRETRIG (1 << 3)
82 #define DAS6402_AO_RANGE(_chan, _range) ((_range) << ((_chan) ? 6 : 4))
83 #define DAS6402_AO_RANGE_MASK(_chan) (3 << ((_chan) ? 6 : 4))
84 #define DAS6402_MODE_REG 0x0b
85 #define DAS6402_MODE_RANGE(x) ((x) << 0)
86 #define DAS6402_MODE_POLLED (0 << 2)
87 #define DAS6402_MODE_FIFONEPTY (1 << 2)
88 #define DAS6402_MODE_FIFOHFULL (2 << 2)
89 #define DAS6402_MODE_EOB (3 << 2)
90 #define DAS6402_MODE_ENHANCED (1 << 4)
91 #define DAS6402_MODE_SE (1 << 5)
92 #define DAS6402_MODE_UNI (1 << 6)
93 #define DAS6402_MODE_DMA1 (0 << 7)
94 #define DAS6402_MODE_DMA3 (1 << 7)
95 #define DAS6402_TIMER_BASE 0x0c
97 static const struct comedi_lrange das6402_ai_ranges = {
111 * Analog output ranges are programmable on the DAS6402/12.
112 * For the DAS6402/16 the range bits have no function, the
113 * DAC ranges are selected by switches on the board.
115 static const struct comedi_lrange das6402_ao_ranges = {
124 struct das6402_boardinfo {
126 unsigned int maxdata;
129 static struct das6402_boardinfo das6402_boards[] = {
131 .name = "das6402-12",
134 .name = "das6402-16",
139 struct das6402_private {
141 unsigned int ao_range;
144 static void das6402_set_mode(struct comedi_device *dev,
147 outb(DAS6402_MODE_ENHANCED | mode, dev->iobase + DAS6402_MODE_REG);
150 static void das6402_set_extended(struct comedi_device *dev,
153 outb(DAS6402_STATUS_W_EXTEND, dev->iobase + DAS6402_STATUS_REG);
154 outb(DAS6402_STATUS_W_EXTEND | val, dev->iobase + DAS6402_STATUS_REG);
155 outb(val, dev->iobase + DAS6402_STATUS_REG);
158 static void das6402_clear_all_interrupts(struct comedi_device *dev)
160 outb(DAS6402_STATUS_W_CLRINT |
161 DAS6402_STATUS_W_CLRXTR |
162 DAS6402_STATUS_W_CLRXIN, dev->iobase + DAS6402_STATUS_REG);
165 static void das6402_ai_clear_eoc(struct comedi_device *dev)
167 outb(DAS6402_STATUS_W_CLRINT, dev->iobase + DAS6402_STATUS_REG);
170 static unsigned int das6402_ai_read_sample(struct comedi_device *dev,
171 struct comedi_subdevice *s)
175 val = inw(dev->iobase + DAS6402_AI_DATA_REG);
176 if (s->maxdata == 0x0fff)
181 static irqreturn_t das6402_interrupt(int irq, void *d)
183 struct comedi_device *dev = d;
184 struct comedi_subdevice *s = dev->read_subdev;
185 struct comedi_async *async = s->async;
186 struct comedi_cmd *cmd = &async->cmd;
189 status = inb(dev->iobase + DAS6402_STATUS_REG);
190 if ((status & DAS6402_STATUS_INT) == 0)
193 if (status & DAS6402_STATUS_FFULL) {
194 async->events |= COMEDI_CB_OVERFLOW;
195 } else if (status & DAS6402_STATUS_FFNE) {
198 val = das6402_ai_read_sample(dev, s);
199 comedi_buf_write_samples(s, &val, 1);
201 if (cmd->stop_src == TRIG_COUNT &&
202 async->scans_done >= cmd->stop_arg)
203 async->events |= COMEDI_CB_EOA;
206 das6402_clear_all_interrupts(dev);
208 comedi_handle_events(dev, s);
213 static void das6402_ai_set_mode(struct comedi_device *dev,
214 struct comedi_subdevice *s,
215 unsigned int chanspec,
218 unsigned int range = CR_RANGE(chanspec);
219 unsigned int aref = CR_AREF(chanspec);
221 mode |= DAS6402_MODE_RANGE(range);
222 if (aref == AREF_GROUND)
223 mode |= DAS6402_MODE_SE;
224 if (comedi_range_is_unipolar(s, range))
225 mode |= DAS6402_MODE_UNI;
227 das6402_set_mode(dev, mode);
230 static int das6402_ai_cmd(struct comedi_device *dev,
231 struct comedi_subdevice *s)
233 struct das6402_private *devpriv = dev->private;
234 struct comedi_cmd *cmd = &s->async->cmd;
235 unsigned int chan_lo = CR_CHAN(cmd->chanlist[0]);
236 unsigned int chan_hi = CR_CHAN(cmd->chanlist[cmd->chanlist_len - 1]);
238 das6402_ai_set_mode(dev, s, cmd->chanlist[0], DAS6402_MODE_FIFONEPTY);
240 /* load the mux for chanlist conversion */
241 outw(DAS6402_AI_MUX_HI(chan_hi) | DAS6402_AI_MUX_LO(chan_lo),
242 dev->iobase + DAS6402_AI_MUX_REG);
244 comedi_8254_update_divisors(dev->pacer);
245 comedi_8254_pacer_enable(dev->pacer, 1, 2, true);
247 /* enable interrupt and pacer trigger */
248 outb(DAS6402_CTRL_INTE |
249 DAS6402_CTRL_IRQ(devpriv->irq) |
250 DAS6402_CTRL_PACER_TRIG, dev->iobase + DAS6402_CTRL_REG);
255 static int das6402_ai_check_chanlist(struct comedi_device *dev,
256 struct comedi_subdevice *s,
257 struct comedi_cmd *cmd)
259 unsigned int chan0 = CR_CHAN(cmd->chanlist[0]);
260 unsigned int range0 = CR_RANGE(cmd->chanlist[0]);
261 unsigned int aref0 = CR_AREF(cmd->chanlist[0]);
264 for (i = 1; i < cmd->chanlist_len; i++) {
265 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
266 unsigned int range = CR_RANGE(cmd->chanlist[i]);
267 unsigned int aref = CR_AREF(cmd->chanlist[i]);
269 if (chan != chan0 + i) {
270 dev_dbg(dev->class_dev,
271 "chanlist must be consecutive\n");
275 if (range != range0) {
276 dev_dbg(dev->class_dev,
277 "chanlist must have the same range\n");
282 dev_dbg(dev->class_dev,
283 "chanlist must have the same reference\n");
287 if (aref0 == AREF_DIFF && chan > (s->n_chan / 2)) {
288 dev_dbg(dev->class_dev,
289 "chanlist differential channel to large\n");
296 static int das6402_ai_cmdtest(struct comedi_device *dev,
297 struct comedi_subdevice *s,
298 struct comedi_cmd *cmd)
303 /* Step 1 : check if triggers are trivially valid */
305 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
306 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_FOLLOW);
307 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_TIMER);
308 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
309 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
314 /* Step 2a : make sure trigger sources are unique */
316 err |= comedi_check_trigger_is_unique(cmd->stop_src);
318 /* Step 2b : and mutually compatible */
323 /* Step 3: check if arguments are trivially valid */
325 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
326 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
327 err |= comedi_check_trigger_arg_min(&cmd->convert_arg, 10000);
328 err |= comedi_check_trigger_arg_min(&cmd->chanlist_len, 1);
329 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
332 if (cmd->stop_src == TRIG_COUNT)
333 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
335 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
340 /* step 4: fix up any arguments */
342 arg = cmd->convert_arg;
343 comedi_8254_cascade_ns_to_timer(dev->pacer, &arg, cmd->flags);
344 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
349 /* Step 5: check channel list if it exists */
350 if (cmd->chanlist && cmd->chanlist_len > 0)
351 err |= das6402_ai_check_chanlist(dev, s, cmd);
359 static int das6402_ai_cancel(struct comedi_device *dev,
360 struct comedi_subdevice *s)
362 outb(DAS6402_CTRL_SOFT_TRIG, dev->iobase + DAS6402_CTRL_REG);
367 static void das6402_ai_soft_trig(struct comedi_device *dev)
369 outw(0, dev->iobase + DAS6402_AI_DATA_REG);
372 static int das6402_ai_eoc(struct comedi_device *dev,
373 struct comedi_subdevice *s,
374 struct comedi_insn *insn,
375 unsigned long context)
379 status = inb(dev->iobase + DAS6402_STATUS_REG);
380 if (status & DAS6402_STATUS_FFNE)
385 static int das6402_ai_insn_read(struct comedi_device *dev,
386 struct comedi_subdevice *s,
387 struct comedi_insn *insn,
390 unsigned int chan = CR_CHAN(insn->chanspec);
391 unsigned int aref = CR_AREF(insn->chanspec);
395 if (aref == AREF_DIFF && chan > (s->n_chan / 2))
398 /* enable software conversion trigger */
399 outb(DAS6402_CTRL_SOFT_TRIG, dev->iobase + DAS6402_CTRL_REG);
401 das6402_ai_set_mode(dev, s, insn->chanspec, DAS6402_MODE_POLLED);
403 /* load the mux for single channel conversion */
404 outw(DAS6402_AI_MUX_HI(chan) | DAS6402_AI_MUX_LO(chan),
405 dev->iobase + DAS6402_AI_MUX_REG);
407 for (i = 0; i < insn->n; i++) {
408 das6402_ai_clear_eoc(dev);
409 das6402_ai_soft_trig(dev);
411 ret = comedi_timeout(dev, s, insn, das6402_ai_eoc, 0);
415 data[i] = das6402_ai_read_sample(dev, s);
418 das6402_ai_clear_eoc(dev);
423 static int das6402_ao_insn_write(struct comedi_device *dev,
424 struct comedi_subdevice *s,
425 struct comedi_insn *insn,
428 struct das6402_private *devpriv = dev->private;
429 unsigned int chan = CR_CHAN(insn->chanspec);
430 unsigned int range = CR_RANGE(insn->chanspec);
434 /* set the range for this channel */
435 val = devpriv->ao_range;
436 val &= ~DAS6402_AO_RANGE_MASK(chan);
437 val |= DAS6402_AO_RANGE(chan, range);
438 if (val != devpriv->ao_range) {
439 devpriv->ao_range = val;
440 outb(val, dev->iobase + DAS6402_TRIG_REG);
444 * The DAS6402/16 has a jumper to select either individual
445 * update (UPDATE) or simultaneous updating (XFER) of both
446 * DAC's. In UPDATE mode, when the MSB is written, that DAC
447 * is updated. In XFER mode, after both DAC's are loaded,
448 * a read cycle of any DAC register will update both DAC's
451 * If you have XFER mode enabled a (*insn_read) will need
452 * to be performed in order to update the DAC's with the
453 * last value written.
455 for (i = 0; i < insn->n; i++) {
458 s->readback[chan] = val;
460 if (s->maxdata == 0x0fff) {
462 * DAS6402/12 has the two 8-bit DAC registers, left
463 * justified (the 4 LSB bits are don't care). Data
464 * can be written as one word.
467 outw(val, dev->iobase + DAS6402_AO_DATA_REG(chan));
470 * DAS6402/16 uses both 8-bit DAC registers and needs
471 * to be written LSB then MSB.
474 dev->iobase + DAS6402_AO_LSB_REG(chan));
475 outb((val >> 8) & 0xff,
476 dev->iobase + DAS6402_AO_LSB_REG(chan));
483 static int das6402_ao_insn_read(struct comedi_device *dev,
484 struct comedi_subdevice *s,
485 struct comedi_insn *insn,
488 unsigned int chan = CR_CHAN(insn->chanspec);
491 * If XFER mode is enabled, reading any DAC register
492 * will update both DAC's simultaneously.
494 inw(dev->iobase + DAS6402_AO_LSB_REG(chan));
496 return comedi_readback_insn_read(dev, s, insn, data);
499 static int das6402_di_insn_bits(struct comedi_device *dev,
500 struct comedi_subdevice *s,
501 struct comedi_insn *insn,
504 data[1] = inb(dev->iobase + DAS6402_DI_DO_REG);
509 static int das6402_do_insn_bits(struct comedi_device *dev,
510 struct comedi_subdevice *s,
511 struct comedi_insn *insn,
514 if (comedi_dio_update_state(s, data))
515 outb(s->state, dev->iobase + DAS6402_DI_DO_REG);
522 static void das6402_reset(struct comedi_device *dev)
524 struct das6402_private *devpriv = dev->private;
526 /* enable "Enhanced" mode */
527 outb(DAS6402_MODE_ENHANCED, dev->iobase + DAS6402_MODE_REG);
529 /* enable 10MHz pacer clock */
530 das6402_set_extended(dev, DAS6402_STATUS_W_10MHZ);
532 /* enable software conversion trigger */
533 outb(DAS6402_CTRL_SOFT_TRIG, dev->iobase + DAS6402_CTRL_REG);
535 /* default ADC to single-ended unipolar 10V inputs */
536 das6402_set_mode(dev, DAS6402_MODE_RANGE(0) |
537 DAS6402_MODE_POLLED |
541 /* default mux for single channel conversion (channel 0) */
542 outw(DAS6402_AI_MUX_HI(0) | DAS6402_AI_MUX_LO(0),
543 dev->iobase + DAS6402_AI_MUX_REG);
545 /* set both DAC's for unipolar 5V output range */
546 devpriv->ao_range = DAS6402_AO_RANGE(0, 2) | DAS6402_AO_RANGE(1, 2);
547 outb(devpriv->ao_range, dev->iobase + DAS6402_TRIG_REG);
549 /* set both DAC's to 0V */
550 outw(0, dev->iobase + DAS6402_AO_DATA_REG(0));
551 outw(0, dev->iobase + DAS6402_AO_DATA_REG(0));
552 inw(dev->iobase + DAS6402_AO_LSB_REG(0));
554 /* set all digital outputs low */
555 outb(0, dev->iobase + DAS6402_DI_DO_REG);
557 das6402_clear_all_interrupts(dev);
560 static int das6402_attach(struct comedi_device *dev,
561 struct comedi_devconfig *it)
563 const struct das6402_boardinfo *board = dev->board_ptr;
564 struct das6402_private *devpriv;
565 struct comedi_subdevice *s;
568 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
572 ret = comedi_request_region(dev, it->options[0], 0x10);
578 /* IRQs 2,3,5,6,7, 10,11,15 are valid for "enhanced" mode */
579 if ((1 << it->options[1]) & 0x8cec) {
580 ret = request_irq(it->options[1], das6402_interrupt, 0,
581 dev->board_name, dev);
583 dev->irq = it->options[1];
596 devpriv->irq = dev->irq;
602 dev->pacer = comedi_8254_init(dev->iobase + DAS6402_TIMER_BASE,
603 I8254_OSC_BASE_10MHZ, I8254_IO8, 0);
607 ret = comedi_alloc_subdevices(dev, 4);
611 /* Analog Input subdevice */
612 s = &dev->subdevices[0];
613 s->type = COMEDI_SUBD_AI;
614 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
616 s->maxdata = board->maxdata;
617 s->range_table = &das6402_ai_ranges;
618 s->insn_read = das6402_ai_insn_read;
620 dev->read_subdev = s;
621 s->subdev_flags |= SDF_CMD_READ;
622 s->len_chanlist = s->n_chan;
623 s->do_cmdtest = das6402_ai_cmdtest;
624 s->do_cmd = das6402_ai_cmd;
625 s->cancel = das6402_ai_cancel;
628 /* Analog Output subdevice */
629 s = &dev->subdevices[1];
630 s->type = COMEDI_SUBD_AO;
631 s->subdev_flags = SDF_WRITABLE;
633 s->maxdata = board->maxdata;
634 s->range_table = &das6402_ao_ranges;
635 s->insn_write = das6402_ao_insn_write;
636 s->insn_read = das6402_ao_insn_read;
638 ret = comedi_alloc_subdev_readback(s);
642 /* Digital Input subdevice */
643 s = &dev->subdevices[2];
644 s->type = COMEDI_SUBD_DI;
645 s->subdev_flags = SDF_READABLE;
648 s->range_table = &range_digital;
649 s->insn_bits = das6402_di_insn_bits;
651 /* Digital Input subdevice */
652 s = &dev->subdevices[3];
653 s->type = COMEDI_SUBD_DO;
654 s->subdev_flags = SDF_WRITABLE;
657 s->range_table = &range_digital;
658 s->insn_bits = das6402_do_insn_bits;
663 static struct comedi_driver das6402_driver = {
664 .driver_name = "das6402",
665 .module = THIS_MODULE,
666 .attach = das6402_attach,
667 .detach = comedi_legacy_detach,
668 .board_name = &das6402_boards[0].name,
669 .num_names = ARRAY_SIZE(das6402_boards),
670 .offset = sizeof(struct das6402_boardinfo),
672 module_comedi_driver(das6402_driver)
674 MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
675 MODULE_DESCRIPTION("Comedi driver for DAS6402 compatible boards");
676 MODULE_LICENSE("GPL");