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_eoc(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
);
63 if (status
& DT2814_FINISH
)
68 static int dt2814_ai_insn_read(struct comedi_device
*dev
,
69 struct comedi_subdevice
*s
,
70 struct comedi_insn
*insn
, unsigned int *data
)
76 for (n
= 0; n
< insn
->n
; n
++) {
77 chan
= CR_CHAN(insn
->chanspec
);
79 outb(chan
, dev
->iobase
+ DT2814_CSR
);
81 ret
= comedi_timeout(dev
, s
, insn
, dt2814_ai_eoc
, 0);
85 hi
= inb(dev
->iobase
+ DT2814_DATA
);
86 lo
= inb(dev
->iobase
+ DT2814_DATA
);
88 data
[n
] = (hi
<< 4) | (lo
>> 4);
94 static int dt2814_ns_to_timer(unsigned int *ns
, unsigned int flags
)
99 /* XXX ignores flags */
102 for (i
= 0; i
< 8; i
++) {
103 if ((2 * (*ns
)) < (f
* 11))
113 static int dt2814_ai_cmdtest(struct comedi_device
*dev
,
114 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
)
119 /* Step 1 : check if triggers are trivially valid */
121 err
|= comedi_check_trigger_src(&cmd
->start_src
, TRIG_NOW
);
122 err
|= comedi_check_trigger_src(&cmd
->scan_begin_src
, TRIG_TIMER
);
123 err
|= comedi_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
124 err
|= comedi_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
125 err
|= comedi_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
130 /* Step 2a : make sure trigger sources are unique */
132 err
|= comedi_check_trigger_is_unique(cmd
->stop_src
);
134 /* Step 2b : and mutually compatible */
139 /* Step 3: check if arguments are trivially valid */
141 err
|= comedi_check_trigger_arg_is(&cmd
->start_arg
, 0);
143 err
|= comedi_check_trigger_arg_max(&cmd
->scan_begin_arg
, 1000000000);
144 err
|= comedi_check_trigger_arg_min(&cmd
->scan_begin_arg
,
147 err
|= comedi_check_trigger_arg_is(&cmd
->scan_end_arg
,
150 if (cmd
->stop_src
== TRIG_COUNT
)
151 err
|= comedi_check_trigger_arg_min(&cmd
->stop_arg
, 2);
153 err
|= comedi_check_trigger_arg_is(&cmd
->stop_arg
, 0);
158 /* step 4: fix up any arguments */
160 arg
= cmd
->scan_begin_arg
;
161 dt2814_ns_to_timer(&arg
, cmd
->flags
);
162 err
|= comedi_check_trigger_arg_is(&cmd
->scan_begin_arg
, arg
);
170 static int dt2814_ai_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
172 struct dt2814_private
*devpriv
= dev
->private;
173 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
177 trigvar
= dt2814_ns_to_timer(&cmd
->scan_begin_arg
, cmd
->flags
);
179 chan
= CR_CHAN(cmd
->chanlist
[0]);
181 devpriv
->ntrig
= cmd
->stop_arg
;
182 outb(chan
| DT2814_ENB
| (trigvar
<< 5), dev
->iobase
+ DT2814_CSR
);
187 static irqreturn_t
dt2814_interrupt(int irq
, void *d
)
190 struct comedi_device
*dev
= d
;
191 struct dt2814_private
*devpriv
= dev
->private;
192 struct comedi_subdevice
*s
= dev
->read_subdev
;
195 if (!dev
->attached
) {
196 dev_err(dev
->class_dev
, "spurious interrupt\n");
200 hi
= inb(dev
->iobase
+ DT2814_DATA
);
201 lo
= inb(dev
->iobase
+ DT2814_DATA
);
203 data
= (hi
<< 4) | (lo
>> 4);
205 if (!(--devpriv
->ntrig
)) {
208 outb(0, dev
->iobase
+ DT2814_CSR
);
210 * note: turning off timed mode triggers another
214 for (i
= 0; i
< DT2814_TIMEOUT
; i
++) {
215 if (inb(dev
->iobase
+ DT2814_CSR
) & DT2814_FINISH
)
218 inb(dev
->iobase
+ DT2814_DATA
);
219 inb(dev
->iobase
+ DT2814_DATA
);
221 s
->async
->events
|= COMEDI_CB_EOA
;
223 comedi_handle_events(dev
, s
);
227 static int dt2814_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
229 struct dt2814_private
*devpriv
;
230 struct comedi_subdevice
*s
;
234 ret
= comedi_request_region(dev
, it
->options
[0], 0x2);
238 outb(0, dev
->iobase
+ DT2814_CSR
);
239 usleep_range(100, 200);
240 if (inb(dev
->iobase
+ DT2814_CSR
) & DT2814_ERR
) {
241 dev_err(dev
->class_dev
, "reset error (fatal)\n");
244 i
= inb(dev
->iobase
+ DT2814_DATA
);
245 i
= inb(dev
->iobase
+ DT2814_DATA
);
247 if (it
->options
[1]) {
248 ret
= request_irq(it
->options
[1], dt2814_interrupt
, 0,
249 dev
->board_name
, dev
);
251 dev
->irq
= it
->options
[1];
254 ret
= comedi_alloc_subdevices(dev
, 1);
258 devpriv
= comedi_alloc_devpriv(dev
, sizeof(*devpriv
));
262 s
= &dev
->subdevices
[0];
263 s
->type
= COMEDI_SUBD_AI
;
264 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
;
265 s
->n_chan
= 16; /* XXX */
266 s
->insn_read
= dt2814_ai_insn_read
;
268 s
->range_table
= &range_unknown
; /* XXX */
270 dev
->read_subdev
= s
;
271 s
->subdev_flags
|= SDF_CMD_READ
;
273 s
->do_cmd
= dt2814_ai_cmd
;
274 s
->do_cmdtest
= dt2814_ai_cmdtest
;
280 static struct comedi_driver dt2814_driver
= {
281 .driver_name
= "dt2814",
282 .module
= THIS_MODULE
,
283 .attach
= dt2814_attach
,
284 .detach
= comedi_legacy_detach
,
286 module_comedi_driver(dt2814_driver
);
288 MODULE_AUTHOR("Comedi http://www.comedi.org");
289 MODULE_DESCRIPTION("Comedi low-level driver");
290 MODULE_LICENSE("GPL");