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 <linux/interrupt.h>
43 #include "../comedidev.h"
45 #include <linux/ioport.h>
46 #include <linux/delay.h>
57 #define DT2814_FINISH 0x80
58 #define DT2814_ERR 0x40
59 #define DT2814_BUSY 0x20
60 #define DT2814_ENB 0x10
61 #define DT2814_CHANMASK 0x0f
63 static int dt2814_attach(struct comedi_device *dev,
64 struct comedi_devconfig *it);
65 static int dt2814_detach(struct comedi_device *dev);
66 static struct comedi_driver driver_dt2814 = {
67 .driver_name = "dt2814",
68 .module = THIS_MODULE,
69 .attach = dt2814_attach,
70 .detach = dt2814_detach,
73 COMEDI_INITCLEANUP(driver_dt2814);
75 static irqreturn_t dt2814_interrupt(int irq, void *dev);
77 struct dt2814_private {
83 #define devpriv ((struct dt2814_private *)dev->private)
85 #define DT2814_TIMEOUT 10
86 #define DT2814_MAX_SPEED 100000 /* Arbitrary 10 khz limit */
88 static int dt2814_ai_insn_read(struct comedi_device *dev,
89 struct comedi_subdevice *s,
90 struct comedi_insn *insn, unsigned int *data)
96 for (n = 0; n < insn->n; n++) {
97 chan = CR_CHAN(insn->chanspec);
99 outb(chan, dev->iobase + DT2814_CSR);
100 for (i = 0; i < DT2814_TIMEOUT; i++) {
101 status = inb(dev->iobase + DT2814_CSR);
102 printk(KERN_INFO "dt2814: status: %02x\n", status);
104 if (status & DT2814_FINISH)
107 if (i >= DT2814_TIMEOUT) {
108 printk(KERN_INFO "dt2814: status: %02x\n", status);
112 hi = inb(dev->iobase + DT2814_DATA);
113 lo = inb(dev->iobase + DT2814_DATA);
115 data[n] = (hi << 4) | (lo >> 4);
121 static int dt2814_ns_to_timer(unsigned int *ns, unsigned int flags)
126 /* XXX ignores flags */
129 for (i = 0; i < 8; i++) {
130 if ((2 * (*ns)) < (f * 11))
140 static int dt2814_ai_cmdtest(struct comedi_device *dev,
141 struct comedi_subdevice *s, struct comedi_cmd *cmd)
146 /* step 1: make sure trigger sources are trivially valid */
148 tmp = cmd->start_src;
149 cmd->start_src &= TRIG_NOW;
150 if (!cmd->start_src || tmp != cmd->start_src)
153 tmp = cmd->scan_begin_src;
154 cmd->scan_begin_src &= TRIG_TIMER;
155 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
158 tmp = cmd->convert_src;
159 cmd->convert_src &= TRIG_NOW;
160 if (!cmd->convert_src || tmp != cmd->convert_src)
163 tmp = cmd->scan_end_src;
164 cmd->scan_end_src &= TRIG_COUNT;
165 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
169 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
170 if (!cmd->stop_src || tmp != cmd->stop_src)
176 /* step 2: make sure trigger sources are
177 * unique and mutually compatible */
179 /* note that mutual compatibility is not an issue here */
180 if (cmd->stop_src != TRIG_TIMER && cmd->stop_src != TRIG_EXT)
186 /* step 3: make sure arguments are trivially compatible */
188 if (cmd->start_arg != 0) {
192 if (cmd->scan_begin_arg > 1000000000) {
193 cmd->scan_begin_arg = 1000000000;
196 if (cmd->scan_begin_arg < DT2814_MAX_SPEED) {
197 cmd->scan_begin_arg = DT2814_MAX_SPEED;
200 if (cmd->scan_end_arg != cmd->chanlist_len) {
201 cmd->scan_end_arg = cmd->chanlist_len;
204 if (cmd->stop_src == TRIG_COUNT) {
205 if (cmd->stop_arg < 2) {
211 if (cmd->stop_arg != 0) {
220 /* step 4: fix up any arguments */
222 tmp = cmd->scan_begin_arg;
223 dt2814_ns_to_timer(&cmd->scan_begin_arg, cmd->flags & TRIG_ROUND_MASK);
224 if (tmp != cmd->scan_begin_arg)
233 static int dt2814_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
235 struct comedi_cmd *cmd = &s->async->cmd;
240 dt2814_ns_to_timer(&cmd->scan_begin_arg,
241 cmd->flags & TRIG_ROUND_MASK);
243 chan = CR_CHAN(cmd->chanlist[0]);
245 devpriv->ntrig = cmd->stop_arg;
246 outb(chan | DT2814_ENB | (trigvar << 5), dev->iobase + DT2814_CSR);
252 static int dt2814_attach(struct comedi_device *dev, struct comedi_devconfig *it)
256 struct comedi_subdevice *s;
257 unsigned long iobase;
259 iobase = it->options[0];
260 printk(KERN_INFO "comedi%d: dt2814: 0x%04lx ", dev->minor, iobase);
261 if (!request_region(iobase, DT2814_SIZE, "dt2814")) {
262 printk(KERN_ERR "I/O port conflict\n");
265 dev->iobase = iobase;
266 dev->board_name = "dt2814";
268 outb(0, dev->iobase + DT2814_CSR);
270 if (inb(dev->iobase + DT2814_CSR) & DT2814_ERR) {
271 printk(KERN_ERR "reset error (fatal)\n");
274 i = inb(dev->iobase + DT2814_DATA);
275 i = inb(dev->iobase + DT2814_DATA);
277 irq = it->options[1];
282 irqs = probe_irq_on();
284 outb(0, dev->iobase + DT2814_CSR);
288 irq = probe_irq_off(irqs);
289 restore_flags(flags);
290 if (inb(dev->iobase + DT2814_CSR) & DT2814_ERR)
291 printk(KERN_DEBUG "error probing irq (bad)\n");
294 i = inb(dev->iobase + DT2814_DATA);
295 i = inb(dev->iobase + DT2814_DATA);
300 if (request_irq(irq, dt2814_interrupt, 0, "dt2814", dev)) {
301 printk(KERN_WARNING "(irq %d unavailable)\n", irq);
303 printk(KERN_INFO "( irq = %d )\n", irq);
306 } else if (irq == 0) {
307 printk(KERN_WARNING "(no irq)\n");
310 printk(KERN_DEBUG "(probe returned multiple irqs--bad)\n");
312 printk(KERN_WARNING "(irq probe not implemented)\n");
316 ret = alloc_subdevices(dev, 1);
320 ret = alloc_private(dev, sizeof(struct dt2814_private));
324 s = dev->subdevices + 0;
325 dev->read_subdev = s;
326 s->type = COMEDI_SUBD_AI;
327 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
328 s->n_chan = 16; /* XXX */
330 s->insn_read = dt2814_ai_insn_read;
331 s->do_cmd = dt2814_ai_cmd;
332 s->do_cmdtest = dt2814_ai_cmdtest;
334 s->range_table = &range_unknown; /* XXX */
339 static int dt2814_detach(struct comedi_device *dev)
341 printk(KERN_INFO "comedi%d: dt2814: remove\n", dev->minor);
344 free_irq(dev->irq, dev);
347 release_region(dev->iobase, DT2814_SIZE);
352 static irqreturn_t dt2814_interrupt(int irq, void *d)
355 struct comedi_device *dev = d;
356 struct comedi_subdevice *s;
359 if (!dev->attached) {
360 comedi_error(dev, "spurious interrupt");
364 s = dev->subdevices + 0;
366 hi = inb(dev->iobase + DT2814_DATA);
367 lo = inb(dev->iobase + DT2814_DATA);
369 data = (hi << 4) | (lo >> 4);
371 if (!(--devpriv->ntrig)) {
374 outb(0, dev->iobase + DT2814_CSR);
375 /* note: turning off timed mode triggers another
378 for (i = 0; i < DT2814_TIMEOUT; i++) {
379 if (inb(dev->iobase + DT2814_CSR) & DT2814_FINISH)
382 inb(dev->iobase + DT2814_DATA);
383 inb(dev->iobase + DT2814_DATA);
385 s->async->events |= COMEDI_CB_EOA;
387 comedi_event(dev, s);