2 comedi/drivers/dt2814.c
3 Hardware driver for Data Translation DT2814
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1998 David A. Schleef <ds@schleef.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 Description: Data Translation DT2814
28 Devices: [Data Translation] DT2814 (dt2814)
30 Configuration options:
31 [0] - I/O port base address
34 This card has 16 analog inputs multiplexed onto a 12 bit ADC. There
35 is a minimally useful onboard clock. The base frequency for the
36 clock is selected by jumpers, and the clock divider can be selected
37 via programmed I/O. Unfortunately, the clock divider can only be
38 a power of 10, from 1 to 10^7, of which only 3 or 4 are useful. In
39 addition, the clock does not seem to be very accurate.
42 #include "../comedidev.h"
44 #include <linux/ioport.h>
45 #include <linux/delay.h>
56 #define DT2814_FINISH 0x80
57 #define DT2814_ERR 0x40
58 #define DT2814_BUSY 0x20
59 #define DT2814_ENB 0x10
60 #define DT2814_CHANMASK 0x0f
62 static int dt2814_attach(comedi_device * dev, comedi_devconfig * it);
63 static int dt2814_detach(comedi_device * dev);
64 static comedi_driver driver_dt2814 = {
71 COMEDI_INITCLEANUP(driver_dt2814);
73 static irqreturn_t dt2814_interrupt(int irq, void *dev PT_REGS_ARG);
79 #define devpriv ((dt2814_private *)dev->private)
81 #define DT2814_TIMEOUT 10
82 #define DT2814_MAX_SPEED 100000 /* Arbitrary 10 khz limit */
84 static int dt2814_ai_insn_read(comedi_device * dev, comedi_subdevice * s,
85 comedi_insn * insn, unsigned int * data)
91 for (n = 0; n < insn->n; n++) {
92 chan = CR_CHAN(insn->chanspec);
94 outb(chan, dev->iobase + DT2814_CSR);
95 for (i = 0; i < DT2814_TIMEOUT; i++) {
96 status = inb(dev->iobase + DT2814_CSR);
97 printk("dt2814: status: %02x\n", status);
99 if (status & DT2814_FINISH)
102 if (i >= DT2814_TIMEOUT) {
103 printk("dt2814: status: %02x\n", status);
107 hi = inb(dev->iobase + DT2814_DATA);
108 lo = inb(dev->iobase + DT2814_DATA);
110 data[n] = (hi << 4) | (lo >> 4);
116 static int dt2814_ns_to_timer(unsigned int *ns, unsigned int flags)
121 /* XXX ignores flags */
124 for (i = 0; i < 8; i++) {
125 if ((2 * (*ns)) < (f * 11))
135 static int dt2814_ai_cmdtest(comedi_device * dev, comedi_subdevice * s,
141 /* step 1: make sure trigger sources are trivially valid */
143 tmp = cmd->start_src;
144 cmd->start_src &= TRIG_NOW;
145 if (!cmd->start_src || tmp != cmd->start_src)
148 tmp = cmd->scan_begin_src;
149 cmd->scan_begin_src &= TRIG_TIMER;
150 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
153 tmp = cmd->convert_src;
154 cmd->convert_src &= TRIG_NOW;
155 if (!cmd->convert_src || tmp != cmd->convert_src)
158 tmp = cmd->scan_end_src;
159 cmd->scan_end_src &= TRIG_COUNT;
160 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
164 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
165 if (!cmd->stop_src || tmp != cmd->stop_src)
171 /* step 2: make sure trigger sources are unique and mutually compatible */
173 /* note that mutual compatiblity is not an issue here */
174 if (cmd->stop_src != TRIG_TIMER && cmd->stop_src != TRIG_EXT)
180 /* step 3: make sure arguments are trivially compatible */
182 if (cmd->start_arg != 0) {
186 if (cmd->scan_begin_arg > 1000000000) {
187 cmd->scan_begin_arg = 1000000000;
190 if (cmd->scan_begin_arg < DT2814_MAX_SPEED) {
191 cmd->scan_begin_arg = DT2814_MAX_SPEED;
194 if (cmd->scan_end_arg != cmd->chanlist_len) {
195 cmd->scan_end_arg = cmd->chanlist_len;
198 if (cmd->stop_src == TRIG_COUNT) {
199 if (cmd->stop_arg < 2) {
205 if (cmd->stop_arg != 0) {
214 /* step 4: fix up any arguments */
216 tmp = cmd->scan_begin_arg;
217 dt2814_ns_to_timer(&cmd->scan_begin_arg, cmd->flags & TRIG_ROUND_MASK);
218 if (tmp != cmd->scan_begin_arg)
227 static int dt2814_ai_cmd(comedi_device * dev, comedi_subdevice * s)
229 comedi_cmd *cmd = &s->async->cmd;
234 dt2814_ns_to_timer(&cmd->scan_begin_arg,
235 cmd->flags & TRIG_ROUND_MASK);
237 chan = CR_CHAN(cmd->chanlist[0]);
239 devpriv->ntrig = cmd->stop_arg;
240 outb(chan | DT2814_ENB | (trigvar << 5), dev->iobase + DT2814_CSR);
246 static int dt2814_attach(comedi_device * dev, comedi_devconfig * it)
251 unsigned long iobase;
253 iobase = it->options[0];
254 printk("comedi%d: dt2814: 0x%04lx ", dev->minor, iobase);
255 if (!request_region(iobase, DT2814_SIZE, "dt2814")) {
256 printk("I/O port conflict\n");
259 dev->iobase = iobase;
260 dev->board_name = "dt2814";
262 outb(0, dev->iobase + DT2814_CSR);
264 if (inb(dev->iobase + DT2814_CSR) & DT2814_ERR) {
265 printk("reset error (fatal)\n");
268 i = inb(dev->iobase + DT2814_DATA);
269 i = inb(dev->iobase + DT2814_DATA);
271 irq = it->options[1];
276 irqs = probe_irq_on();
278 outb(0, dev->iobase + DT2814_CSR);
282 irq = probe_irq_off(irqs);
283 restore_flags(flags);
284 if (inb(dev->iobase + DT2814_CSR) & DT2814_ERR) {
285 printk("error probing irq (bad) \n");
288 i = inb(dev->iobase + DT2814_DATA);
289 i = inb(dev->iobase + DT2814_DATA);
294 if (comedi_request_irq(irq, dt2814_interrupt, 0, "dt2814", dev)) {
295 printk("(irq %d unavailable)\n", irq);
297 printk("( irq = %d )\n", irq);
300 } else if (irq == 0) {
301 printk("(no irq)\n");
304 printk("(probe returned multiple irqs--bad)\n");
306 printk("(irq probe not implemented)\n");
310 if ((ret = alloc_subdevices(dev, 1)) < 0)
312 if ((ret = alloc_private(dev, sizeof(dt2814_private))) < 0)
315 s = dev->subdevices + 0;
316 dev->read_subdev = s;
317 s->type = COMEDI_SUBD_AI;
318 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
319 s->n_chan = 16; /* XXX */
321 s->insn_read = dt2814_ai_insn_read;
322 s->do_cmd = dt2814_ai_cmd;
323 s->do_cmdtest = dt2814_ai_cmdtest;
325 s->range_table = &range_unknown; /* XXX */
330 static int dt2814_detach(comedi_device * dev)
332 printk("comedi%d: dt2814: remove\n", dev->minor);
335 comedi_free_irq(dev->irq, dev);
338 release_region(dev->iobase, DT2814_SIZE);
344 static irqreturn_t dt2814_interrupt(int irq, void *d PT_REGS_ARG)
347 comedi_device *dev = d;
351 if (!dev->attached) {
352 comedi_error(dev, "spurious interrupt");
356 s = dev->subdevices + 0;
358 hi = inb(dev->iobase + DT2814_DATA);
359 lo = inb(dev->iobase + DT2814_DATA);
361 data = (hi << 4) | (lo >> 4);
363 if (!(--devpriv->ntrig)) {
366 outb(0, dev->iobase + DT2814_CSR);
367 /* note: turning off timed mode triggers another
370 for (i = 0; i < DT2814_TIMEOUT; i++) {
371 if (inb(dev->iobase + DT2814_CSR) & DT2814_FINISH)
374 inb(dev->iobase + DT2814_DATA);
375 inb(dev->iobase + DT2814_DATA);
377 s->async->events |= COMEDI_CB_EOA;
379 comedi_event(dev, s);