1 // SPDX-License-Identifier: GPL-2.0+
3 * comedi/drivers/dt2814.c
4 * Hardware driver for Data Translation DT2814
6 * COMEDI - Linux Control and Measurement Device Interface
7 * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
11 * Description: Data Translation DT2814
14 * Devices: [Data Translation] DT2814 (dt2814)
16 * Configuration options:
17 * [0] - I/O port base address
20 * This card has 16 analog inputs multiplexed onto a 12 bit ADC. There
21 * is a minimally useful onboard clock. The base frequency for the
22 * clock is selected by jumpers, and the clock divider can be selected
23 * via programmed I/O. Unfortunately, the clock divider can only be
24 * a power of 10, from 1 to 10^7, of which only 3 or 4 are useful. In
25 * addition, the clock does not seem to be very accurate.
28 #include <linux/module.h>
29 #include <linux/interrupt.h>
30 #include <linux/comedi/comedidev.h>
31 #include <linux/delay.h>
40 #define DT2814_FINISH 0x80
41 #define DT2814_ERR 0x40
42 #define DT2814_BUSY 0x20
43 #define DT2814_ENB 0x10
44 #define DT2814_CHANMASK 0x0f
46 #define DT2814_TIMEOUT 10
47 #define DT2814_MAX_SPEED 100000 /* Arbitrary 10 khz limit */
49 static int dt2814_ai_notbusy(struct comedi_device *dev,
50 struct comedi_subdevice *s,
51 struct comedi_insn *insn,
52 unsigned long context)
56 status = inb(dev->iobase + DT2814_CSR);
58 *(unsigned int *)context = status;
59 if (status & DT2814_BUSY)
64 static int dt2814_ai_clear(struct comedi_device *dev)
66 unsigned int status = 0;
69 /* Wait until not busy and get status register value. */
70 ret = comedi_timeout(dev, NULL, NULL, dt2814_ai_notbusy,
71 (unsigned long)&status);
75 if (status & (DT2814_FINISH | DT2814_ERR)) {
77 * There unread data, or the error flag is set.
78 * Read the data register twice to clear the condition.
80 inb(dev->iobase + DT2814_DATA);
81 inb(dev->iobase + DT2814_DATA);
86 static int dt2814_ai_eoc(struct comedi_device *dev,
87 struct comedi_subdevice *s,
88 struct comedi_insn *insn,
89 unsigned long context)
93 status = inb(dev->iobase + DT2814_CSR);
94 if (status & DT2814_FINISH)
99 static int dt2814_ai_insn_read(struct comedi_device *dev,
100 struct comedi_subdevice *s,
101 struct comedi_insn *insn, unsigned int *data)
107 dt2814_ai_clear(dev); /* clear stale data or error */
108 for (n = 0; n < insn->n; n++) {
109 chan = CR_CHAN(insn->chanspec);
111 outb(chan, dev->iobase + DT2814_CSR);
113 ret = comedi_timeout(dev, s, insn, dt2814_ai_eoc, 0);
117 hi = inb(dev->iobase + DT2814_DATA);
118 lo = inb(dev->iobase + DT2814_DATA);
120 data[n] = (hi << 4) | (lo >> 4);
126 static int dt2814_ns_to_timer(unsigned int *ns, unsigned int flags)
131 /* XXX ignores flags */
134 for (i = 0; i < 8; i++) {
135 if ((2 * (*ns)) < (f * 11))
145 static int dt2814_ai_cmdtest(struct comedi_device *dev,
146 struct comedi_subdevice *s, struct comedi_cmd *cmd)
151 /* Step 1 : check if triggers are trivially valid */
153 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
154 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_TIMER);
155 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
156 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
157 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
162 /* Step 2a : make sure trigger sources are unique */
164 err |= comedi_check_trigger_is_unique(cmd->stop_src);
166 /* Step 2b : and mutually compatible */
171 /* Step 3: check if arguments are trivially valid */
173 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
175 err |= comedi_check_trigger_arg_max(&cmd->scan_begin_arg, 1000000000);
176 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
179 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
182 if (cmd->stop_src == TRIG_COUNT)
183 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 2);
185 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
190 /* step 4: fix up any arguments */
192 arg = cmd->scan_begin_arg;
193 dt2814_ns_to_timer(&arg, cmd->flags);
194 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, arg);
202 static int dt2814_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
204 struct comedi_cmd *cmd = &s->async->cmd;
208 dt2814_ai_clear(dev); /* clear stale data or error */
209 trigvar = dt2814_ns_to_timer(&cmd->scan_begin_arg, cmd->flags);
211 chan = CR_CHAN(cmd->chanlist[0]);
213 outb(chan | DT2814_ENB | (trigvar << 5), dev->iobase + DT2814_CSR);
218 static int dt2814_ai_cancel(struct comedi_device *dev,
219 struct comedi_subdevice *s)
224 spin_lock_irqsave(&dev->spinlock, flags);
225 status = inb(dev->iobase + DT2814_CSR);
226 if (status & DT2814_ENB) {
228 * Clear the timed trigger enable bit.
230 * Note: turning off timed mode triggers another
231 * sample. This will be mopped up by the calls to
234 outb(status & DT2814_CHANMASK, dev->iobase + DT2814_CSR);
236 spin_unlock_irqrestore(&dev->spinlock, flags);
240 static irqreturn_t dt2814_interrupt(int irq, void *d)
242 struct comedi_device *dev = d;
243 struct comedi_subdevice *s = dev->read_subdev;
244 struct comedi_async *async;
249 if (!dev->attached) {
250 dev_err(dev->class_dev, "spurious interrupt\n");
256 spin_lock(&dev->spinlock);
258 status = inb(dev->iobase + DT2814_CSR);
259 if (!(status & DT2814_ENB)) {
260 /* Timed acquisition not enabled. Nothing to do. */
261 spin_unlock(&dev->spinlock);
265 if (!(status & (DT2814_FINISH | DT2814_ERR))) {
266 /* Spurious interrupt? */
267 spin_unlock(&dev->spinlock);
271 /* Read data or clear error. */
272 hi = inb(dev->iobase + DT2814_DATA);
273 lo = inb(dev->iobase + DT2814_DATA);
275 data = (hi << 4) | (lo >> 4);
277 if (status & DT2814_ERR) {
278 async->events |= COMEDI_CB_ERROR;
280 comedi_buf_write_samples(s, &data, 1);
281 if (async->cmd.stop_src == TRIG_COUNT &&
282 async->scans_done >= async->cmd.stop_arg) {
283 async->events |= COMEDI_CB_EOA;
286 if (async->events & COMEDI_CB_CANCEL_MASK) {
288 * Disable timed mode.
290 * Note: turning off timed mode triggers another
291 * sample. This will be mopped up by the calls to
294 outb(status & DT2814_CHANMASK, dev->iobase + DT2814_CSR);
297 spin_unlock(&dev->spinlock);
299 comedi_handle_events(dev, s);
303 static int dt2814_attach(struct comedi_device *dev, struct comedi_devconfig *it)
305 struct comedi_subdevice *s;
308 ret = comedi_request_region(dev, it->options[0], 0x2);
312 outb(0, dev->iobase + DT2814_CSR);
313 if (dt2814_ai_clear(dev)) {
314 dev_err(dev->class_dev, "reset error (fatal)\n");
318 if (it->options[1]) {
319 ret = request_irq(it->options[1], dt2814_interrupt, 0,
320 dev->board_name, dev);
322 dev->irq = it->options[1];
325 ret = comedi_alloc_subdevices(dev, 1);
329 s = &dev->subdevices[0];
330 s->type = COMEDI_SUBD_AI;
331 s->subdev_flags = SDF_READABLE | SDF_GROUND;
332 s->n_chan = 16; /* XXX */
333 s->insn_read = dt2814_ai_insn_read;
335 s->range_table = &range_unknown; /* XXX */
337 dev->read_subdev = s;
338 s->subdev_flags |= SDF_CMD_READ;
340 s->do_cmd = dt2814_ai_cmd;
341 s->do_cmdtest = dt2814_ai_cmdtest;
342 s->cancel = dt2814_ai_cancel;
348 static void dt2814_detach(struct comedi_device *dev)
352 * An extra conversion triggered on termination of an
353 * asynchronous command may still be in progress. Wait for
354 * it to finish and clear the data or error status.
356 dt2814_ai_clear(dev);
358 comedi_legacy_detach(dev);
361 static struct comedi_driver dt2814_driver = {
362 .driver_name = "dt2814",
363 .module = THIS_MODULE,
364 .attach = dt2814_attach,
365 .detach = dt2814_detach,
367 module_comedi_driver(dt2814_driver);
369 MODULE_AUTHOR("Comedi https://www.comedi.org");
370 MODULE_DESCRIPTION("Comedi low-level driver");
371 MODULE_LICENSE("GPL");