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>
48 #include "comedi_fc.h"
59 #define DT2814_FINISH 0x80
60 #define DT2814_ERR 0x40
61 #define DT2814_BUSY 0x20
62 #define DT2814_ENB 0x10
63 #define DT2814_CHANMASK 0x0f
65 struct dt2814_private {
71 #define DT2814_TIMEOUT 10
72 #define DT2814_MAX_SPEED 100000 /* Arbitrary 10 khz limit */
74 static int dt2814_ai_insn_read(struct comedi_device *dev,
75 struct comedi_subdevice *s,
76 struct comedi_insn *insn, unsigned int *data)
82 for (n = 0; n < insn->n; n++) {
83 chan = CR_CHAN(insn->chanspec);
85 outb(chan, dev->iobase + DT2814_CSR);
86 for (i = 0; i < DT2814_TIMEOUT; i++) {
87 status = inb(dev->iobase + DT2814_CSR);
88 printk(KERN_INFO "dt2814: status: %02x\n", status);
90 if (status & DT2814_FINISH)
93 if (i >= DT2814_TIMEOUT) {
94 printk(KERN_INFO "dt2814: status: %02x\n", status);
98 hi = inb(dev->iobase + DT2814_DATA);
99 lo = inb(dev->iobase + DT2814_DATA);
101 data[n] = (hi << 4) | (lo >> 4);
107 static int dt2814_ns_to_timer(unsigned int *ns, unsigned int flags)
112 /* XXX ignores flags */
115 for (i = 0; i < 8; i++) {
116 if ((2 * (*ns)) < (f * 11))
126 static int dt2814_ai_cmdtest(struct comedi_device *dev,
127 struct comedi_subdevice *s, struct comedi_cmd *cmd)
132 /* Step 1 : check if triggers are trivially valid */
134 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
135 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_TIMER);
136 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
137 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
138 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
143 /* Step 2a : make sure trigger sources are unique */
145 err |= cfc_check_trigger_is_unique(cmd->stop_src);
147 /* Step 2b : and mutually compatible */
152 /* step 3: make sure arguments are trivially compatible */
154 if (cmd->start_arg != 0) {
158 if (cmd->scan_begin_arg > 1000000000) {
159 cmd->scan_begin_arg = 1000000000;
162 if (cmd->scan_begin_arg < DT2814_MAX_SPEED) {
163 cmd->scan_begin_arg = DT2814_MAX_SPEED;
166 if (cmd->scan_end_arg != cmd->chanlist_len) {
167 cmd->scan_end_arg = cmd->chanlist_len;
170 if (cmd->stop_src == TRIG_COUNT) {
171 if (cmd->stop_arg < 2) {
177 if (cmd->stop_arg != 0) {
186 /* step 4: fix up any arguments */
188 tmp = cmd->scan_begin_arg;
189 dt2814_ns_to_timer(&cmd->scan_begin_arg, cmd->flags & TRIG_ROUND_MASK);
190 if (tmp != cmd->scan_begin_arg)
199 static int dt2814_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
201 struct dt2814_private *devpriv = dev->private;
202 struct comedi_cmd *cmd = &s->async->cmd;
207 dt2814_ns_to_timer(&cmd->scan_begin_arg,
208 cmd->flags & TRIG_ROUND_MASK);
210 chan = CR_CHAN(cmd->chanlist[0]);
212 devpriv->ntrig = cmd->stop_arg;
213 outb(chan | DT2814_ENB | (trigvar << 5), dev->iobase + DT2814_CSR);
219 static irqreturn_t dt2814_interrupt(int irq, void *d)
222 struct comedi_device *dev = d;
223 struct dt2814_private *devpriv = dev->private;
224 struct comedi_subdevice *s;
227 if (!dev->attached) {
228 comedi_error(dev, "spurious interrupt");
232 s = &dev->subdevices[0];
234 hi = inb(dev->iobase + DT2814_DATA);
235 lo = inb(dev->iobase + DT2814_DATA);
237 data = (hi << 4) | (lo >> 4);
239 if (!(--devpriv->ntrig)) {
242 outb(0, dev->iobase + DT2814_CSR);
243 /* note: turning off timed mode triggers another
246 for (i = 0; i < DT2814_TIMEOUT; i++) {
247 if (inb(dev->iobase + DT2814_CSR) & DT2814_FINISH)
250 inb(dev->iobase + DT2814_DATA);
251 inb(dev->iobase + DT2814_DATA);
253 s->async->events |= COMEDI_CB_EOA;
255 comedi_event(dev, s);
259 static int dt2814_attach(struct comedi_device *dev, struct comedi_devconfig *it)
261 struct dt2814_private *devpriv;
264 struct comedi_subdevice *s;
265 unsigned long iobase;
267 iobase = it->options[0];
268 printk(KERN_INFO "comedi%d: dt2814: 0x%04lx ", dev->minor, iobase);
269 if (!request_region(iobase, DT2814_SIZE, "dt2814")) {
270 printk(KERN_ERR "I/O port conflict\n");
273 dev->iobase = iobase;
274 dev->board_name = "dt2814";
276 outb(0, dev->iobase + DT2814_CSR);
278 if (inb(dev->iobase + DT2814_CSR) & DT2814_ERR) {
279 printk(KERN_ERR "reset error (fatal)\n");
282 i = inb(dev->iobase + DT2814_DATA);
283 i = inb(dev->iobase + DT2814_DATA);
285 irq = it->options[1];
290 irqs = probe_irq_on();
292 outb(0, dev->iobase + DT2814_CSR);
296 irq = probe_irq_off(irqs);
297 restore_flags(flags);
298 if (inb(dev->iobase + DT2814_CSR) & DT2814_ERR)
299 printk(KERN_DEBUG "error probing irq (bad)\n");
302 i = inb(dev->iobase + DT2814_DATA);
303 i = inb(dev->iobase + DT2814_DATA);
308 if (request_irq(irq, dt2814_interrupt, 0, "dt2814", dev)) {
309 printk(KERN_WARNING "(irq %d unavailable)\n", irq);
311 printk(KERN_INFO "( irq = %d )\n", irq);
314 } else if (irq == 0) {
315 printk(KERN_WARNING "(no irq)\n");
318 printk(KERN_DEBUG "(probe returned multiple irqs--bad)\n");
320 printk(KERN_WARNING "(irq probe not implemented)\n");
324 ret = comedi_alloc_subdevices(dev, 1);
328 ret = alloc_private(dev, sizeof(*devpriv));
331 devpriv = dev->private;
333 s = &dev->subdevices[0];
334 dev->read_subdev = s;
335 s->type = COMEDI_SUBD_AI;
336 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
337 s->n_chan = 16; /* XXX */
339 s->insn_read = dt2814_ai_insn_read;
340 s->do_cmd = dt2814_ai_cmd;
341 s->do_cmdtest = dt2814_ai_cmdtest;
343 s->range_table = &range_unknown; /* XXX */
348 static void dt2814_detach(struct comedi_device *dev)
351 free_irq(dev->irq, dev);
353 release_region(dev->iobase, DT2814_SIZE);
356 static struct comedi_driver dt2814_driver = {
357 .driver_name = "dt2814",
358 .module = THIS_MODULE,
359 .attach = dt2814_attach,
360 .detach = dt2814_detach,
362 module_comedi_driver(dt2814_driver);
364 MODULE_AUTHOR("Comedi http://www.comedi.org");
365 MODULE_DESCRIPTION("Comedi low-level driver");
366 MODULE_LICENSE("GPL");