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.
20 Description: Data Translation DT2814
23 Devices: [Data Translation] DT2814 (dt2814)
25 Configuration options:
26 [0] - I/O port base address
29 This card has 16 analog inputs multiplexed onto a 12 bit ADC. There
30 is a minimally useful onboard clock. The base frequency for the
31 clock is selected by jumpers, and the clock divider can be selected
32 via programmed I/O. Unfortunately, the clock divider can only be
33 a power of 10, from 1 to 10^7, of which only 3 or 4 are useful. In
34 addition, the clock does not seem to be very accurate.
37 #include <linux/module.h>
38 #include <linux/interrupt.h>
39 #include "../comedidev.h"
41 #include <linux/ioport.h>
42 #include <linux/delay.h>
44 #include "comedi_fc.h"
55 #define DT2814_FINISH 0x80
56 #define DT2814_ERR 0x40
57 #define DT2814_BUSY 0x20
58 #define DT2814_ENB 0x10
59 #define DT2814_CHANMASK 0x0f
61 struct dt2814_private {
67 #define DT2814_TIMEOUT 10
68 #define DT2814_MAX_SPEED 100000 /* Arbitrary 10 khz limit */
70 static int dt2814_ai_insn_read(struct comedi_device *dev,
71 struct comedi_subdevice *s,
72 struct comedi_insn *insn, unsigned int *data)
78 for (n = 0; n < insn->n; n++) {
79 chan = CR_CHAN(insn->chanspec);
81 outb(chan, dev->iobase + DT2814_CSR);
82 for (i = 0; i < DT2814_TIMEOUT; i++) {
83 status = inb(dev->iobase + DT2814_CSR);
84 printk(KERN_INFO "dt2814: status: %02x\n", status);
86 if (status & DT2814_FINISH)
89 if (i >= DT2814_TIMEOUT) {
90 printk(KERN_INFO "dt2814: status: %02x\n", status);
94 hi = inb(dev->iobase + DT2814_DATA);
95 lo = inb(dev->iobase + DT2814_DATA);
97 data[n] = (hi << 4) | (lo >> 4);
103 static int dt2814_ns_to_timer(unsigned int *ns, unsigned int flags)
108 /* XXX ignores flags */
111 for (i = 0; i < 8; i++) {
112 if ((2 * (*ns)) < (f * 11))
122 static int dt2814_ai_cmdtest(struct comedi_device *dev,
123 struct comedi_subdevice *s, struct comedi_cmd *cmd)
128 /* Step 1 : check if triggers are trivially valid */
130 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
131 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_TIMER);
132 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
133 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
134 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
139 /* Step 2a : make sure trigger sources are unique */
141 err |= cfc_check_trigger_is_unique(cmd->stop_src);
143 /* Step 2b : and mutually compatible */
148 /* Step 3: check if arguments are trivially valid */
150 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
152 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg, 1000000000);
153 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
156 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
158 if (cmd->stop_src == TRIG_COUNT)
159 err |= cfc_check_trigger_arg_min(&cmd->stop_arg, 2);
161 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
166 /* step 4: fix up any arguments */
168 tmp = cmd->scan_begin_arg;
169 dt2814_ns_to_timer(&cmd->scan_begin_arg, cmd->flags & TRIG_ROUND_MASK);
170 if (tmp != cmd->scan_begin_arg)
179 static int dt2814_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
181 struct dt2814_private *devpriv = dev->private;
182 struct comedi_cmd *cmd = &s->async->cmd;
187 dt2814_ns_to_timer(&cmd->scan_begin_arg,
188 cmd->flags & TRIG_ROUND_MASK);
190 chan = CR_CHAN(cmd->chanlist[0]);
192 devpriv->ntrig = cmd->stop_arg;
193 outb(chan | DT2814_ENB | (trigvar << 5), dev->iobase + DT2814_CSR);
199 static irqreturn_t dt2814_interrupt(int irq, void *d)
202 struct comedi_device *dev = d;
203 struct dt2814_private *devpriv = dev->private;
204 struct comedi_subdevice *s;
207 if (!dev->attached) {
208 comedi_error(dev, "spurious interrupt");
212 s = &dev->subdevices[0];
214 hi = inb(dev->iobase + DT2814_DATA);
215 lo = inb(dev->iobase + DT2814_DATA);
217 data = (hi << 4) | (lo >> 4);
219 if (!(--devpriv->ntrig)) {
222 outb(0, dev->iobase + DT2814_CSR);
223 /* note: turning off timed mode triggers another
226 for (i = 0; i < DT2814_TIMEOUT; i++) {
227 if (inb(dev->iobase + DT2814_CSR) & DT2814_FINISH)
230 inb(dev->iobase + DT2814_DATA);
231 inb(dev->iobase + DT2814_DATA);
233 s->async->events |= COMEDI_CB_EOA;
235 comedi_event(dev, s);
239 static int dt2814_attach(struct comedi_device *dev, struct comedi_devconfig *it)
241 struct dt2814_private *devpriv;
244 struct comedi_subdevice *s;
246 ret = comedi_request_region(dev, it->options[0], DT2814_SIZE);
250 outb(0, dev->iobase + DT2814_CSR);
252 if (inb(dev->iobase + DT2814_CSR) & DT2814_ERR) {
253 printk(KERN_ERR "reset error (fatal)\n");
256 i = inb(dev->iobase + DT2814_DATA);
257 i = inb(dev->iobase + DT2814_DATA);
259 irq = it->options[1];
264 irqs = probe_irq_on();
266 outb(0, dev->iobase + DT2814_CSR);
270 irq = probe_irq_off(irqs);
271 restore_flags(flags);
272 if (inb(dev->iobase + DT2814_CSR) & DT2814_ERR)
273 printk(KERN_DEBUG "error probing irq (bad)\n");
276 i = inb(dev->iobase + DT2814_DATA);
277 i = inb(dev->iobase + DT2814_DATA);
282 if (request_irq(irq, dt2814_interrupt, 0, "dt2814", dev)) {
283 printk(KERN_WARNING "(irq %d unavailable)\n", irq);
285 printk(KERN_INFO "( irq = %d )\n", irq);
288 } else if (irq == 0) {
289 printk(KERN_WARNING "(no irq)\n");
292 printk(KERN_DEBUG "(probe returned multiple irqs--bad)\n");
294 printk(KERN_WARNING "(irq probe not implemented)\n");
298 ret = comedi_alloc_subdevices(dev, 1);
302 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
306 s = &dev->subdevices[0];
307 dev->read_subdev = s;
308 s->type = COMEDI_SUBD_AI;
309 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
310 s->n_chan = 16; /* XXX */
312 s->insn_read = dt2814_ai_insn_read;
313 s->do_cmd = dt2814_ai_cmd;
314 s->do_cmdtest = dt2814_ai_cmdtest;
316 s->range_table = &range_unknown; /* XXX */
321 static struct comedi_driver dt2814_driver = {
322 .driver_name = "dt2814",
323 .module = THIS_MODULE,
324 .attach = dt2814_attach,
325 .detach = comedi_legacy_detach,
327 module_comedi_driver(dt2814_driver);
329 MODULE_AUTHOR("Comedi http://www.comedi.org");
330 MODULE_DESCRIPTION("Comedi low-level driver");
331 MODULE_LICENSE("GPL");