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 "../comedidev.h"
32 #include <linux/delay.h>
41 #define DT2814_FINISH 0x80
42 #define DT2814_ERR 0x40
43 #define DT2814_BUSY 0x20
44 #define DT2814_ENB 0x10
45 #define DT2814_CHANMASK 0x0f
47 struct dt2814_private
{
52 #define DT2814_TIMEOUT 10
53 #define DT2814_MAX_SPEED 100000 /* Arbitrary 10 khz limit */
55 static int dt2814_ai_notbusy(struct comedi_device
*dev
,
56 struct comedi_subdevice
*s
,
57 struct comedi_insn
*insn
,
58 unsigned long context
)
62 status
= inb(dev
->iobase
+ DT2814_CSR
);
64 *(unsigned int *)context
= status
;
65 if (status
& DT2814_BUSY
)
70 static int dt2814_ai_clear(struct comedi_device
*dev
)
72 unsigned int status
= 0;
75 /* Wait until not busy and get status register value. */
76 ret
= comedi_timeout(dev
, NULL
, NULL
, dt2814_ai_notbusy
,
77 (unsigned long)&status
);
81 if (status
& (DT2814_FINISH
| DT2814_ERR
)) {
83 * There unread data, or the error flag is set.
84 * Read the data register twice to clear the condition.
86 inb(dev
->iobase
+ DT2814_DATA
);
87 inb(dev
->iobase
+ DT2814_DATA
);
92 static int dt2814_ai_eoc(struct comedi_device
*dev
,
93 struct comedi_subdevice
*s
,
94 struct comedi_insn
*insn
,
95 unsigned long context
)
99 status
= inb(dev
->iobase
+ DT2814_CSR
);
100 if (status
& DT2814_FINISH
)
105 static int dt2814_ai_insn_read(struct comedi_device
*dev
,
106 struct comedi_subdevice
*s
,
107 struct comedi_insn
*insn
, unsigned int *data
)
113 dt2814_ai_clear(dev
); /* clear stale data or error */
114 for (n
= 0; n
< insn
->n
; n
++) {
115 chan
= CR_CHAN(insn
->chanspec
);
117 outb(chan
, dev
->iobase
+ DT2814_CSR
);
119 ret
= comedi_timeout(dev
, s
, insn
, dt2814_ai_eoc
, 0);
123 hi
= inb(dev
->iobase
+ DT2814_DATA
);
124 lo
= inb(dev
->iobase
+ DT2814_DATA
);
126 data
[n
] = (hi
<< 4) | (lo
>> 4);
132 static int dt2814_ns_to_timer(unsigned int *ns
, unsigned int flags
)
137 /* XXX ignores flags */
140 for (i
= 0; i
< 8; i
++) {
141 if ((2 * (*ns
)) < (f
* 11))
151 static int dt2814_ai_cmdtest(struct comedi_device
*dev
,
152 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
)
157 /* Step 1 : check if triggers are trivially valid */
159 err
|= comedi_check_trigger_src(&cmd
->start_src
, TRIG_NOW
);
160 err
|= comedi_check_trigger_src(&cmd
->scan_begin_src
, TRIG_TIMER
);
161 err
|= comedi_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
162 err
|= comedi_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
163 err
|= comedi_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
168 /* Step 2a : make sure trigger sources are unique */
170 err
|= comedi_check_trigger_is_unique(cmd
->stop_src
);
172 /* Step 2b : and mutually compatible */
177 /* Step 3: check if arguments are trivially valid */
179 err
|= comedi_check_trigger_arg_is(&cmd
->start_arg
, 0);
181 err
|= comedi_check_trigger_arg_max(&cmd
->scan_begin_arg
, 1000000000);
182 err
|= comedi_check_trigger_arg_min(&cmd
->scan_begin_arg
,
185 err
|= comedi_check_trigger_arg_is(&cmd
->scan_end_arg
,
188 if (cmd
->stop_src
== TRIG_COUNT
)
189 err
|= comedi_check_trigger_arg_min(&cmd
->stop_arg
, 2);
191 err
|= comedi_check_trigger_arg_is(&cmd
->stop_arg
, 0);
196 /* step 4: fix up any arguments */
198 arg
= cmd
->scan_begin_arg
;
199 dt2814_ns_to_timer(&arg
, cmd
->flags
);
200 err
|= comedi_check_trigger_arg_is(&cmd
->scan_begin_arg
, arg
);
208 static int dt2814_ai_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
210 struct dt2814_private
*devpriv
= dev
->private;
211 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
215 dt2814_ai_clear(dev
); /* clear stale data or error */
216 trigvar
= dt2814_ns_to_timer(&cmd
->scan_begin_arg
, cmd
->flags
);
218 chan
= CR_CHAN(cmd
->chanlist
[0]);
220 devpriv
->ntrig
= cmd
->stop_arg
;
221 outb(chan
| DT2814_ENB
| (trigvar
<< 5), dev
->iobase
+ DT2814_CSR
);
226 static irqreturn_t
dt2814_interrupt(int irq
, void *d
)
228 struct comedi_device
*dev
= d
;
229 struct dt2814_private
*devpriv
= dev
->private;
230 struct comedi_subdevice
*s
= dev
->read_subdev
;
232 if (!dev
->attached
) {
233 dev_err(dev
->class_dev
, "spurious interrupt\n");
237 inb(dev
->iobase
+ DT2814_DATA
);
238 inb(dev
->iobase
+ DT2814_DATA
);
240 if (!(--devpriv
->ntrig
)) {
241 outb(0, dev
->iobase
+ DT2814_CSR
);
243 * Note: turning off timed mode triggers another
244 * sample. This will be mopped up by the calls to
248 s
->async
->events
|= COMEDI_CB_EOA
;
250 comedi_handle_events(dev
, s
);
254 static int dt2814_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
256 struct dt2814_private
*devpriv
;
257 struct comedi_subdevice
*s
;
260 ret
= comedi_request_region(dev
, it
->options
[0], 0x2);
264 outb(0, dev
->iobase
+ DT2814_CSR
);
265 if (dt2814_ai_clear(dev
)) {
266 dev_err(dev
->class_dev
, "reset error (fatal)\n");
270 if (it
->options
[1]) {
271 ret
= request_irq(it
->options
[1], dt2814_interrupt
, 0,
272 dev
->board_name
, dev
);
274 dev
->irq
= it
->options
[1];
277 ret
= comedi_alloc_subdevices(dev
, 1);
281 devpriv
= comedi_alloc_devpriv(dev
, sizeof(*devpriv
));
285 s
= &dev
->subdevices
[0];
286 s
->type
= COMEDI_SUBD_AI
;
287 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
;
288 s
->n_chan
= 16; /* XXX */
289 s
->insn_read
= dt2814_ai_insn_read
;
291 s
->range_table
= &range_unknown
; /* XXX */
293 dev
->read_subdev
= s
;
294 s
->subdev_flags
|= SDF_CMD_READ
;
296 s
->do_cmd
= dt2814_ai_cmd
;
297 s
->do_cmdtest
= dt2814_ai_cmdtest
;
303 static struct comedi_driver dt2814_driver
= {
304 .driver_name
= "dt2814",
305 .module
= THIS_MODULE
,
306 .attach
= dt2814_attach
,
307 .detach
= comedi_legacy_detach
,
309 module_comedi_driver(dt2814_driver
);
311 MODULE_AUTHOR("Comedi https://www.comedi.org");
312 MODULE_DESCRIPTION("Comedi low-level driver");
313 MODULE_LICENSE("GPL");