2 * Comedi driver for CIO-DAS16/M1
3 * Author: Frank Mori Hess, based on code from the das16 driver.
4 * Copyright (C) 2001 Frank Mori Hess <fmhess@users.sourceforge.net>
6 * COMEDI - Linux Control and Measurement Device Interface
7 * Copyright (C) 2000 David A. Schleef <ds@schleef.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: CIO-DAS16/M1
23 * Author: Frank Mori Hess <fmhess@users.sourceforge.net>
24 * Devices: [Measurement Computing] CIO-DAS16/M1 (das16m1)
27 * This driver supports a single board - the CIO-DAS16/M1. As far as I know,
28 * there are no other boards that have the same register layout. Even the
29 * CIO-DAS16/M1/16 is significantly different.
31 * I was _barely_ able to reach the full 1 MHz capability of this board, using
32 * a hard real-time interrupt (set the TRIG_RT flag in your struct comedi_cmd
33 * and use rtlinux or RTAI). The board can't do dma, so the bottleneck is
34 * pulling the data across the ISA bus. I timed the interrupt handler, and it
35 * took my computer ~470 microseconds to pull 512 samples from the board. So
36 * at 1 Mhz sampling rate, expect your CPU to be spending almost all of its
37 * time in the interrupt handler.
39 * This board has some unusual restrictions for its channel/gain list. If the
40 * list has 2 or more channels in it, then two conditions must be satisfied:
41 * (1) - even/odd channels must appear at even/odd indices in the list
42 * (2) - the list must have an even number of entries.
44 * Configuration options:
45 * [0] - base io address
46 * [1] - irq (optional, but you probably want it)
48 * irq can be omitted, although the cmd interface will not work without it.
51 #include <linux/module.h>
52 #include <linux/slab.h>
53 #include <linux/interrupt.h>
54 #include "../comedidev.h"
57 #include "comedi_8254.h"
59 #define DAS16M1_SIZE2 8
61 #define FIFO_SIZE 1024 /* 1024 sample fifo */
68 0 a/d bits 0-3, mux start 12 bit
69 1 a/d bits 4-11 unused
72 4 unused clear interrupt
74 6 channel/gain queue address
75 7 channel/gain queue data
83 #define DAS16M1_AI 0 /* 16-bit wide register */
84 #define AI_CHAN(x) ((x) & 0xf)
86 #define EXT_TRIG_BIT 0x1
90 #define DAS16M1_CLEAR_INTR 4
91 #define DAS16M1_INTR_CONTROL 5
94 #define PACER_MASK 0x3
96 #define DAS16M1_QUEUE_ADDR 6
97 #define DAS16M1_QUEUE_DATA 7
98 #define Q_CHAN(x) ((x) & 0x7)
99 #define Q_RANGE(x) (((x) & 0xf) << 4)
100 #define UNIPOLAR 0x40
101 #define DAS16M1_8254_FIRST 0x8
102 #define DAS16M1_8254_SECOND 0xc
103 #define DAS16M1_82C55 0x400
104 #define DAS16M1_8254_THIRD 0x404
106 static const struct comedi_lrange range_das16m1 = {
120 struct das16m1_private_struct {
121 struct comedi_8254 *counter;
122 unsigned int control_state;
123 unsigned int adc_count; /* number of samples completed */
124 /* initial value in lower half of hardware conversion counter,
125 * needed to keep track of whether new count has been loaded into
126 * counter yet (loaded by first sample conversion) */
127 u16 initial_hw_count;
128 unsigned short ai_buffer[FIFO_SIZE];
129 unsigned long extra_iobase;
132 static inline unsigned short munge_sample(unsigned short data)
134 return (data >> 4) & 0xfff;
137 static void munge_sample_array(unsigned short *array, unsigned int num_elements)
141 for (i = 0; i < num_elements; i++)
142 array[i] = munge_sample(array[i]);
145 static int das16m1_ai_check_chanlist(struct comedi_device *dev,
146 struct comedi_subdevice *s,
147 struct comedi_cmd *cmd)
151 if (cmd->chanlist_len == 1)
154 if ((cmd->chanlist_len % 2) != 0) {
155 dev_dbg(dev->class_dev,
156 "chanlist must be of even length or length 1\n");
160 for (i = 0; i < cmd->chanlist_len; i++) {
161 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
163 if ((i % 2) != (chan % 2)) {
164 dev_dbg(dev->class_dev,
165 "even/odd channels must go have even/odd chanlist indices\n");
173 static int das16m1_cmd_test(struct comedi_device *dev,
174 struct comedi_subdevice *s, struct comedi_cmd *cmd)
178 /* Step 1 : check if triggers are trivially valid */
180 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_EXT);
181 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_FOLLOW);
182 err |= comedi_check_trigger_src(&cmd->convert_src,
183 TRIG_TIMER | TRIG_EXT);
184 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
185 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
190 /* Step 2a : make sure trigger sources are unique */
192 err |= comedi_check_trigger_is_unique(cmd->start_src);
193 err |= comedi_check_trigger_is_unique(cmd->convert_src);
194 err |= comedi_check_trigger_is_unique(cmd->stop_src);
196 /* Step 2b : and mutually compatible */
201 /* Step 3: check if arguments are trivially valid */
203 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
205 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
206 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
208 if (cmd->convert_src == TRIG_TIMER)
209 err |= comedi_check_trigger_arg_min(&cmd->convert_arg, 1000);
211 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
214 if (cmd->stop_src == TRIG_COUNT)
215 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
217 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
222 /* step 4: fix up arguments */
224 if (cmd->convert_src == TRIG_TIMER) {
225 unsigned int arg = cmd->convert_arg;
227 comedi_8254_cascade_ns_to_timer(dev->pacer, &arg, cmd->flags);
228 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
234 /* Step 5: check channel list if it exists */
235 if (cmd->chanlist && cmd->chanlist_len > 0)
236 err |= das16m1_ai_check_chanlist(dev, s, cmd);
244 static int das16m1_cmd_exec(struct comedi_device *dev,
245 struct comedi_subdevice *s)
247 struct das16m1_private_struct *devpriv = dev->private;
248 struct comedi_async *async = s->async;
249 struct comedi_cmd *cmd = &async->cmd;
250 unsigned int byte, i;
252 /* disable interrupts and internal pacer */
253 devpriv->control_state &= ~INTE & ~PACER_MASK;
254 outb(devpriv->control_state, dev->iobase + DAS16M1_INTR_CONTROL);
256 /* set software count */
257 devpriv->adc_count = 0;
260 * Initialize lower half of hardware counter, used to determine how
261 * many samples are in fifo. Value doesn't actually load into counter
262 * until counter's next clock (the next a/d conversion).
264 comedi_8254_set_mode(devpriv->counter, 1, I8254_MODE2 | I8254_BINARY);
265 comedi_8254_write(devpriv->counter, 1, 0);
268 * Remember current reading of counter so we know when counter has
269 * actually been loaded.
271 devpriv->initial_hw_count = comedi_8254_read(devpriv->counter, 1);
273 /* setup channel/gain queue */
274 for (i = 0; i < cmd->chanlist_len; i++) {
275 outb(i, dev->iobase + DAS16M1_QUEUE_ADDR);
277 Q_CHAN(CR_CHAN(cmd->chanlist[i])) |
278 Q_RANGE(CR_RANGE(cmd->chanlist[i]));
279 outb(byte, dev->iobase + DAS16M1_QUEUE_DATA);
282 /* enable interrupts and set internal pacer counter mode and counts */
283 devpriv->control_state &= ~PACER_MASK;
284 if (cmd->convert_src == TRIG_TIMER) {
285 comedi_8254_update_divisors(dev->pacer);
286 comedi_8254_pacer_enable(dev->pacer, 1, 2, true);
287 devpriv->control_state |= INT_PACER;
288 } else { /* TRIG_EXT */
289 devpriv->control_state |= EXT_PACER;
292 /* set control & status register */
294 /* if we are using external start trigger (also board dislikes having
295 * both start and conversion triggers external simultaneously) */
296 if (cmd->start_src == TRIG_EXT && cmd->convert_src != TRIG_EXT)
297 byte |= EXT_TRIG_BIT;
299 outb(byte, dev->iobase + DAS16M1_CS);
300 /* clear interrupt bit */
301 outb(0, dev->iobase + DAS16M1_CLEAR_INTR);
303 devpriv->control_state |= INTE;
304 outb(devpriv->control_state, dev->iobase + DAS16M1_INTR_CONTROL);
309 static int das16m1_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
311 struct das16m1_private_struct *devpriv = dev->private;
313 devpriv->control_state &= ~INTE & ~PACER_MASK;
314 outb(devpriv->control_state, dev->iobase + DAS16M1_INTR_CONTROL);
319 static int das16m1_ai_eoc(struct comedi_device *dev,
320 struct comedi_subdevice *s,
321 struct comedi_insn *insn,
322 unsigned long context)
326 status = inb(dev->iobase + DAS16M1_CS);
327 if (status & IRQDATA)
332 static int das16m1_ai_rinsn(struct comedi_device *dev,
333 struct comedi_subdevice *s,
334 struct comedi_insn *insn, unsigned int *data)
336 struct das16m1_private_struct *devpriv = dev->private;
341 /* disable interrupts and internal pacer */
342 devpriv->control_state &= ~INTE & ~PACER_MASK;
343 outb(devpriv->control_state, dev->iobase + DAS16M1_INTR_CONTROL);
345 /* setup channel/gain queue */
346 outb(0, dev->iobase + DAS16M1_QUEUE_ADDR);
348 Q_CHAN(CR_CHAN(insn->chanspec)) | Q_RANGE(CR_RANGE(insn->chanspec));
349 outb(byte, dev->iobase + DAS16M1_QUEUE_DATA);
351 for (n = 0; n < insn->n; n++) {
352 /* clear IRQDATA bit */
353 outb(0, dev->iobase + DAS16M1_CLEAR_INTR);
354 /* trigger conversion */
355 outb(0, dev->iobase);
357 ret = comedi_timeout(dev, s, insn, das16m1_ai_eoc, 0);
361 data[n] = munge_sample(inw(dev->iobase));
367 static int das16m1_di_rbits(struct comedi_device *dev,
368 struct comedi_subdevice *s,
369 struct comedi_insn *insn, unsigned int *data)
373 bits = inb(dev->iobase + DAS16M1_DIO) & 0xf;
380 static int das16m1_do_wbits(struct comedi_device *dev,
381 struct comedi_subdevice *s,
382 struct comedi_insn *insn,
385 if (comedi_dio_update_state(s, data))
386 outb(s->state, dev->iobase + DAS16M1_DIO);
393 static void das16m1_handler(struct comedi_device *dev, unsigned int status)
395 struct das16m1_private_struct *devpriv = dev->private;
396 struct comedi_subdevice *s;
397 struct comedi_async *async;
398 struct comedi_cmd *cmd;
402 s = dev->read_subdev;
406 /* figure out how many samples are in fifo */
407 hw_counter = comedi_8254_read(devpriv->counter, 1);
408 /* make sure hardware counter reading is not bogus due to initial value
409 * not having been loaded yet */
410 if (devpriv->adc_count == 0 &&
411 hw_counter == devpriv->initial_hw_count) {
414 /* The calculation of num_samples looks odd, but it uses the
415 * following facts. 16 bit hardware counter is initialized with
416 * value of zero (which really means 0x1000). The counter
417 * decrements by one on each conversion (when the counter
418 * decrements from zero it goes to 0xffff). num_samples is a
419 * 16 bit variable, so it will roll over in a similar fashion
420 * to the hardware counter. Work it out, and this is what you
422 num_samples = -hw_counter - devpriv->adc_count;
424 /* check if we only need some of the points */
425 if (cmd->stop_src == TRIG_COUNT) {
426 if (num_samples > cmd->stop_arg * cmd->chanlist_len)
427 num_samples = cmd->stop_arg * cmd->chanlist_len;
429 /* make sure we dont try to get too many points if fifo has overrun */
430 if (num_samples > FIFO_SIZE)
431 num_samples = FIFO_SIZE;
432 insw(dev->iobase, devpriv->ai_buffer, num_samples);
433 munge_sample_array(devpriv->ai_buffer, num_samples);
434 comedi_buf_write_samples(s, devpriv->ai_buffer, num_samples);
435 devpriv->adc_count += num_samples;
437 if (cmd->stop_src == TRIG_COUNT) {
438 if (devpriv->adc_count >= cmd->stop_arg * cmd->chanlist_len) {
439 /* end of acquisition */
440 async->events |= COMEDI_CB_EOA;
444 /* this probably won't catch overruns since the card doesn't generate
445 * overrun interrupts, but we might as well try */
446 if (status & OVRUN) {
447 async->events |= COMEDI_CB_ERROR;
448 dev_err(dev->class_dev, "fifo overflow\n");
451 comedi_handle_events(dev, s);
454 static int das16m1_poll(struct comedi_device *dev, struct comedi_subdevice *s)
459 /* prevent race with interrupt handler */
460 spin_lock_irqsave(&dev->spinlock, flags);
461 status = inb(dev->iobase + DAS16M1_CS);
462 das16m1_handler(dev, status);
463 spin_unlock_irqrestore(&dev->spinlock, flags);
465 return comedi_buf_n_bytes_ready(s);
468 static irqreturn_t das16m1_interrupt(int irq, void *d)
471 struct comedi_device *dev = d;
473 if (!dev->attached) {
474 dev_err(dev->class_dev, "premature interrupt\n");
477 /* prevent race with comedi_poll() */
478 spin_lock(&dev->spinlock);
480 status = inb(dev->iobase + DAS16M1_CS);
482 if ((status & (IRQDATA | OVRUN)) == 0) {
483 dev_err(dev->class_dev, "spurious interrupt\n");
484 spin_unlock(&dev->spinlock);
488 das16m1_handler(dev, status);
490 /* clear interrupt */
491 outb(0, dev->iobase + DAS16M1_CLEAR_INTR);
493 spin_unlock(&dev->spinlock);
497 static int das16m1_irq_bits(unsigned int irq)
526 static int das16m1_attach(struct comedi_device *dev,
527 struct comedi_devconfig *it)
529 struct das16m1_private_struct *devpriv;
530 struct comedi_subdevice *s;
533 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
537 ret = comedi_request_region(dev, it->options[0], 0x10);
540 /* Request an additional region for the 8255 */
541 ret = __comedi_request_region(dev, dev->iobase + DAS16M1_82C55,
545 devpriv->extra_iobase = dev->iobase + DAS16M1_82C55;
547 /* only irqs 2, 3, 4, 5, 6, 7, 10, 11, 12, 14, and 15 are valid */
548 if ((1 << it->options[1]) & 0xdcfc) {
549 ret = request_irq(it->options[1], das16m1_interrupt, 0,
550 dev->board_name, dev);
552 dev->irq = it->options[1];
555 dev->pacer = comedi_8254_init(dev->iobase + DAS16M1_8254_SECOND,
556 I8254_OSC_BASE_10MHZ, I8254_IO8, 0);
560 devpriv->counter = comedi_8254_init(dev->iobase + DAS16M1_8254_FIRST,
562 if (!devpriv->counter)
565 ret = comedi_alloc_subdevices(dev, 4);
569 s = &dev->subdevices[0];
571 s->type = COMEDI_SUBD_AI;
572 s->subdev_flags = SDF_READABLE | SDF_DIFF;
574 s->maxdata = (1 << 12) - 1;
575 s->range_table = &range_das16m1;
576 s->insn_read = das16m1_ai_rinsn;
578 dev->read_subdev = s;
579 s->subdev_flags |= SDF_CMD_READ;
580 s->len_chanlist = 256;
581 s->do_cmdtest = das16m1_cmd_test;
582 s->do_cmd = das16m1_cmd_exec;
583 s->cancel = das16m1_cancel;
584 s->poll = das16m1_poll;
587 s = &dev->subdevices[1];
589 s->type = COMEDI_SUBD_DI;
590 s->subdev_flags = SDF_READABLE;
593 s->range_table = &range_digital;
594 s->insn_bits = das16m1_di_rbits;
596 s = &dev->subdevices[2];
598 s->type = COMEDI_SUBD_DO;
599 s->subdev_flags = SDF_WRITABLE;
602 s->range_table = &range_digital;
603 s->insn_bits = das16m1_do_wbits;
605 s = &dev->subdevices[3];
607 ret = subdev_8255_init(dev, s, NULL, DAS16M1_82C55);
611 /* initialize digital output lines */
612 outb(0, dev->iobase + DAS16M1_DIO);
614 /* set the interrupt level */
615 devpriv->control_state = das16m1_irq_bits(dev->irq) << 4;
616 outb(devpriv->control_state, dev->iobase + DAS16M1_INTR_CONTROL);
621 static void das16m1_detach(struct comedi_device *dev)
623 struct das16m1_private_struct *devpriv = dev->private;
626 if (devpriv->extra_iobase)
627 release_region(devpriv->extra_iobase, DAS16M1_SIZE2);
628 kfree(devpriv->counter);
630 comedi_legacy_detach(dev);
633 static struct comedi_driver das16m1_driver = {
634 .driver_name = "das16m1",
635 .module = THIS_MODULE,
636 .attach = das16m1_attach,
637 .detach = das16m1_detach,
639 module_comedi_driver(das16m1_driver);
641 MODULE_AUTHOR("Comedi http://www.comedi.org");
642 MODULE_DESCRIPTION("Comedi low-level driver");
643 MODULE_LICENSE("GPL");