blob: fcd85475e4294a6c49e96d93a6738a1408407fbf [file] [log] [blame]
David Schleef3c443712009-02-19 10:08:26 -08001/*
H Hartley Sweeten8a8f5482016-05-04 12:47:06 -07002 * Comedi driver for Data Translation DT2811
3 *
4 * COMEDI - Linux Control and Measurement Device Interface
H Hartley Sweetenf2975a92016-05-04 15:25:38 -07005 * Copyright (C) David A. Schleef <ds@schleef.org>
H Hartley Sweeten8a8f5482016-05-04 12:47:06 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
David Schleef3c443712009-02-19 10:08:26 -080016 */
David Schleef3c443712009-02-19 10:08:26 -080017
H Hartley Sweeten8a8f5482016-05-04 12:47:06 -070018/*
19 * Driver: dt2811
20 * Description: Data Translation DT2811
21 * Author: ds
22 * Devices: [Data Translation] DT2811-PGL (dt2811-pgl), DT2811-PGH (dt2811-pgh)
23 * Status: works
24 *
25 * Configuration options:
26 * [0] - I/O port base address
H Hartley Sweetenf2975a92016-05-04 15:25:38 -070027 * [1] - IRQ (optional, needed for async command support)
H Hartley Sweeten7c957402016-05-04 12:47:18 -070028 * [2] - A/D reference (# of analog inputs)
29 * 0 = single-ended (16 channels)
30 * 1 = differential (8 channels)
31 * 2 = pseudo-differential (16 channels)
H Hartley Sweeten048e27a2016-05-04 12:47:20 -070032 * [3] - A/D range (deprecated, see below)
H Hartley Sweeten5fba2732016-05-04 12:47:16 -070033 * [4] - D/A 0 range (deprecated, see below)
34 * [5] - D/A 1 range (deprecated, see below)
35 *
36 * Notes:
H Hartley Sweeten048e27a2016-05-04 12:47:20 -070037 * - A/D ranges are not programmable but the gain is. The AI subdevice has
38 * a range_table containing all the possible analog input range/gain
39 * options for the dt2811-pgh or dt2811-pgl. Use the range that matches
40 * your board configuration and the desired gain to correctly convert
41 * between data values and physical units and to set the correct output
42 * gain.
H Hartley Sweeten5fba2732016-05-04 12:47:16 -070043 * - D/A ranges are not programmable. The AO subdevice has a range_table
44 * containing all the possible analog output ranges. Use the range
45 * that matches your board configuration to convert between data
46 * values and physical units.
H Hartley Sweeten8a8f5482016-05-04 12:47:06 -070047 */
David Schleef3c443712009-02-19 10:08:26 -080048
H Hartley Sweetence157f82013-06-24 17:04:43 -070049#include <linux/module.h>
H Hartley Sweetenf2975a92016-05-04 15:25:38 -070050#include <linux/interrupt.h>
51#include <linux/delay.h>
52
David Schleef3c443712009-02-19 10:08:26 -080053#include "../comedidev.h"
54
H Hartley Sweeten022ac952016-05-04 12:47:09 -070055/*
56 * Register I/O map
57 */
58#define DT2811_ADCSR_REG 0x00 /* r/w A/D Control/Status */
59#define DT2811_ADCSR_ADDONE BIT(7) /* r 1=A/D conv done */
60#define DT2811_ADCSR_ADERROR BIT(6) /* r 1=A/D error */
61#define DT2811_ADCSR_ADBUSY BIT(5) /* r 1=A/D busy */
62#define DT2811_ADCSR_CLRERROR BIT(4)
H Hartley Sweetenf2975a92016-05-04 15:25:38 -070063#define DT2811_ADCSR_DMAENB BIT(3) /* r/w 1=dma ena */
64#define DT2811_ADCSR_INTENB BIT(2) /* r/w 1=interupts ena */
H Hartley Sweeten022ac952016-05-04 12:47:09 -070065#define DT2811_ADCSR_ADMODE(x) (((x) & 0x3) << 0)
H Hartley Sweeten022ac952016-05-04 12:47:09 -070066
H Hartley Sweetenb99c8592016-05-04 12:47:10 -070067#define DT2811_ADGCR_REG 0x01 /* r/w A/D Gain/Channel */
68#define DT2811_ADGCR_GAIN(x) (((x) & 0x3) << 6)
69#define DT2811_ADGCR_CHAN(x) (((x) & 0xf) << 0)
70
H Hartley Sweetend82985f2016-05-04 12:47:11 -070071#define DT2811_ADDATA_LO_REG 0x02 /* r A/D Data low byte */
72#define DT2811_ADDATA_HI_REG 0x03 /* r A/D Data high byte */
73
H Hartley Sweetenaddb85b2016-05-04 12:47:12 -070074#define DT2811_DADATA_LO_REG(x) (0x02 + ((x) * 2)) /* w D/A Data low */
75#define DT2811_DADATA_HI_REG(x) (0x03 + ((x) * 2)) /* w D/A Data high */
76
H Hartley Sweeten3d6dc782016-05-04 12:47:13 -070077#define DT2811_DI_REG 0x06 /* r Digital Input Port 0 */
78#define DT2811_DO_REG 0x06 /* w Digital Output Port 1 */
79
H Hartley Sweetenf2975a92016-05-04 15:25:38 -070080#define DT2811_TMRCTR_REG 0x07 /* r/w Timer/Counter */
81#define DT2811_TMRCTR_MANTISSA(x) (((x) & 0x7) << 3)
82#define DT2811_TMRCTR_EXPONENT(x) (((x) & 0x7) << 0)
83
84#define DT2811_OSC_BASE 1666 /* 600 kHz = 1666.6667ns */
85
H Hartley Sweetenef0af0c2016-05-04 12:47:14 -070086/*
87 * Timer frequency control:
88 * DT2811_TMRCTR_MANTISSA DT2811_TMRCTR_EXPONENT
89 * val divisor frequency val multiply divisor/divide frequency by
90 * 0 1 600 kHz 0 1
91 * 1 10 60 kHz 1 10
92 * 2 2 300 kHz 2 100
93 * 3 3 200 kHz 3 1000
94 * 4 4 150 kHz 4 10000
95 * 5 5 120 kHz 5 100000
96 * 6 6 100 kHz 6 1000000
97 * 7 12 50 kHz 7 10000000
98 */
Nayeemahmed Badebade49c9d6a2016-07-30 22:08:15 +053099static const unsigned int dt2811_clk_dividers[] = {
H Hartley Sweetenf2975a92016-05-04 15:25:38 -0700100 1, 10, 2, 3, 4, 5, 6, 12
101};
102
Nayeemahmed Badebade49c9d6a2016-07-30 22:08:15 +0530103static const unsigned int dt2811_clk_multipliers[] = {
H Hartley Sweetenf2975a92016-05-04 15:25:38 -0700104 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
105};
H Hartley Sweetenef0af0c2016-05-04 12:47:14 -0700106
H Hartley Sweeten048e27a2016-05-04 12:47:20 -0700107/*
108 * The Analog Input range is set using jumpers on the board.
109 *
110 * Input Range W9 W10
111 * -5V to +5V In Out
112 * -2.5V to +2.5V In In
113 * 0V to +5V Out In
114 *
115 * The gain may be set to 1, 2, 4, or 8 (on the dt2811-pgh) or to
116 * 1, 10, 100, 500 (on the dt2811-pgl).
117 */
118static const struct comedi_lrange dt2811_pgh_ai_ranges = {
119 12, {
120 BIP_RANGE(5), /* range 0: gain=1 */
121 BIP_RANGE(2.5), /* range 1: gain=2 */
122 BIP_RANGE(1.25), /* range 2: gain=4 */
123 BIP_RANGE(0.625), /* range 3: gain=8 */
124
125 BIP_RANGE(2.5), /* range 0+4: gain=1 */
126 BIP_RANGE(1.25), /* range 1+4: gain=2 */
127 BIP_RANGE(0.625), /* range 2+4: gain=4 */
128 BIP_RANGE(0.3125), /* range 3+4: gain=8 */
129
130 UNI_RANGE(5), /* range 0+8: gain=1 */
131 UNI_RANGE(2.5), /* range 1+8: gain=2 */
132 UNI_RANGE(1.25), /* range 2+8: gain=4 */
133 UNI_RANGE(0.625) /* range 3+8: gain=8 */
Iain Churcher3b9fdcd2010-05-03 11:54:53 +0100134 }
David Schleef3c443712009-02-19 10:08:26 -0800135};
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530136
H Hartley Sweeten048e27a2016-05-04 12:47:20 -0700137static const struct comedi_lrange dt2811_pgl_ai_ranges = {
138 12, {
139 BIP_RANGE(5), /* range 0: gain=1 */
140 BIP_RANGE(0.5), /* range 1: gain=10 */
141 BIP_RANGE(0.05), /* range 2: gain=100 */
142 BIP_RANGE(0.01), /* range 3: gain=500 */
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530143
H Hartley Sweeten048e27a2016-05-04 12:47:20 -0700144 BIP_RANGE(2.5), /* range 0+4: gain=1 */
145 BIP_RANGE(0.25), /* range 1+4: gain=10 */
146 BIP_RANGE(0.025), /* range 2+4: gain=100 */
147 BIP_RANGE(0.005), /* range 3+4: gain=500 */
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530148
H Hartley Sweeten048e27a2016-05-04 12:47:20 -0700149 UNI_RANGE(5), /* range 0+8: gain=1 */
150 UNI_RANGE(0.5), /* range 1+8: gain=10 */
151 UNI_RANGE(0.05), /* range 2+8: gain=100 */
152 UNI_RANGE(0.01) /* range 3+8: gain=500 */
Iain Churcher3b9fdcd2010-05-03 11:54:53 +0100153 }
David Schleef3c443712009-02-19 10:08:26 -0800154};
155
H Hartley Sweeten5fba2732016-05-04 12:47:16 -0700156/*
157 * The Analog Output range is set per-channel using jumpers on the board.
158 *
159 * DAC0 Jumpers DAC1 Jumpers
160 * Output Range W5 W6 W7 W8 W1 W2 W3 W4
161 * -5V to +5V In Out In Out In Out In Out
162 * -2.5V to +2.5V In Out Out In In Out Out In
163 * 0 to +5V Out In Out In Out In Out In
164 */
165static const struct comedi_lrange dt2811_ao_ranges = {
166 3, {
167 BIP_RANGE(5), /* default setting from factory */
168 BIP_RANGE(2.5),
169 UNI_RANGE(5)
170 }
171};
172
Bill Pemberton42f18842009-03-16 22:14:46 -0400173struct dt2811_board {
David Schleef3c443712009-02-19 10:08:26 -0800174 const char *name;
H Hartley Sweeten048e27a2016-05-04 12:47:20 -0700175 unsigned int is_pgh:1;
Bill Pemberton42f18842009-03-16 22:14:46 -0400176};
177
H Hartley Sweeten99101312016-05-04 12:47:23 -0700178static const struct dt2811_board dt2811_boards[] = {
H Hartley Sweeten048e27a2016-05-04 12:47:20 -0700179 {
180 .name = "dt2811-pgh",
181 .is_pgh = 1,
182 }, {
183 .name = "dt2811-pgl",
184 },
185};
Bill Pembertond89da612009-03-16 22:18:59 -0400186
H Hartley Sweetenf2975a92016-05-04 15:25:38 -0700187struct dt2811_private {
188 unsigned int ai_divisor;
189};
190
191static unsigned int dt2811_ai_read_sample(struct comedi_device *dev,
192 struct comedi_subdevice *s)
193{
194 unsigned int val;
195
196 val = inb(dev->iobase + DT2811_ADDATA_LO_REG) |
197 (inb(dev->iobase + DT2811_ADDATA_HI_REG) << 8);
198
199 return val & s->maxdata;
200}
201
202static irqreturn_t dt2811_interrupt(int irq, void *d)
203{
204 struct comedi_device *dev = d;
205 struct comedi_subdevice *s = dev->read_subdev;
206 struct comedi_async *async = s->async;
207 struct comedi_cmd *cmd = &async->cmd;
208 unsigned int status;
209
210 if (!dev->attached)
211 return IRQ_NONE;
212
213 status = inb(dev->iobase + DT2811_ADCSR_REG);
214
215 if (status & DT2811_ADCSR_ADERROR) {
216 async->events |= COMEDI_CB_OVERFLOW;
217
218 outb(status | DT2811_ADCSR_CLRERROR,
219 dev->iobase + DT2811_ADCSR_REG);
220 }
221
222 if (status & DT2811_ADCSR_ADDONE) {
223 unsigned short val;
224
225 val = dt2811_ai_read_sample(dev, s);
226 comedi_buf_write_samples(s, &val, 1);
227 }
228
229 if (cmd->stop_src == TRIG_COUNT && async->scans_done >= cmd->stop_arg)
230 async->events |= COMEDI_CB_EOA;
231
232 comedi_handle_events(dev, s);
233
234 return IRQ_HANDLED;
235}
236
237static int dt2811_ai_cancel(struct comedi_device *dev,
238 struct comedi_subdevice *s)
239{
240 /*
241 * Mode 0
242 * Single conversion
243 *
244 * Loading a chanspec will trigger a conversion.
245 */
246 outb(DT2811_ADCSR_ADMODE(0), dev->iobase + DT2811_ADCSR_REG);
247
248 return 0;
249}
250
251static void dt2811_ai_set_chanspec(struct comedi_device *dev,
252 unsigned int chanspec)
253{
254 unsigned int chan = CR_CHAN(chanspec);
255 unsigned int range = CR_RANGE(chanspec);
256
257 outb(DT2811_ADGCR_CHAN(chan) | DT2811_ADGCR_GAIN(range),
258 dev->iobase + DT2811_ADGCR_REG);
259}
260
261static int dt2811_ai_cmd(struct comedi_device *dev,
262 struct comedi_subdevice *s)
263{
264 struct dt2811_private *devpriv = dev->private;
265 struct comedi_cmd *cmd = &s->async->cmd;
266 unsigned int mode;
267
268 if (cmd->start_src == TRIG_NOW) {
269 /*
270 * Mode 1
271 * Continuous conversion, internal trigger and clock
272 *
273 * This resets the trigger flip-flop, disabling A/D strobes.
274 * The timer/counter register is loaded with the division
275 * ratio which will give the required sample rate.
276 *
277 * Loading the first chanspec sets the trigger flip-flop,
278 * enabling the timer/counter. A/D strobes are then generated
279 * at the rate set by the internal clock/divider.
280 */
281 mode = DT2811_ADCSR_ADMODE(1);
282 } else { /* TRIG_EXT */
283 if (cmd->convert_src == TRIG_TIMER) {
284 /*
285 * Mode 2
286 * Continuous conversion, external trigger
287 *
288 * Similar to Mode 1, with the exception that the
289 * trigger flip-flop must be set by a negative edge
290 * on the external trigger input.
291 */
292 mode = DT2811_ADCSR_ADMODE(2);
293 } else { /* TRIG_EXT */
294 /*
295 * Mode 3
296 * Continuous conversion, external trigger, clock
297 *
298 * Similar to Mode 2, with the exception that the
299 * conversion rate is set by the frequency on the
300 * external clock/divider.
301 */
302 mode = DT2811_ADCSR_ADMODE(3);
303 }
304 }
305 outb(mode | DT2811_ADCSR_INTENB, dev->iobase + DT2811_ADCSR_REG);
306
307 /* load timer */
308 outb(devpriv->ai_divisor, dev->iobase + DT2811_TMRCTR_REG);
309
310 /* load chanspec - enables timer */
311 dt2811_ai_set_chanspec(dev, cmd->chanlist[0]);
312
313 return 0;
314}
315
316static unsigned int dt2811_ns_to_timer(unsigned int *nanosec,
317 unsigned int flags)
318{
319 unsigned long long ns = *nanosec;
320 unsigned int ns_lo = COMEDI_MIN_SPEED;
321 unsigned int ns_hi = 0;
322 unsigned int divisor_hi = 0;
323 unsigned int divisor_lo = 0;
324 unsigned int _div;
325 unsigned int _mult;
326
327 /*
328 * Work through all the divider/multiplier values to find the two
329 * closest divisors to generate the requested nanosecond timing.
330 */
331 for (_div = 0; _div <= 7; _div++) {
332 for (_mult = 0; _mult <= 7; _mult++) {
333 unsigned int div = dt2811_clk_dividers[_div];
334 unsigned int mult = dt2811_clk_multipliers[_mult];
335 unsigned long long divider = div * mult;
336 unsigned int divisor = DT2811_TMRCTR_MANTISSA(_div) |
337 DT2811_TMRCTR_EXPONENT(_mult);
338
339 /*
340 * The timer can be configured to run at a slowest
341 * speed of 0.005hz (600 Khz/120000000), which requires
342 * 37-bits to represent the nanosecond value. Limit the
343 * slowest timing to what comedi handles (32-bits).
344 */
345 ns = divider * DT2811_OSC_BASE;
346 if (ns > COMEDI_MIN_SPEED)
347 continue;
348
349 /* Check for fastest found timing */
350 if (ns <= *nanosec && ns > ns_hi) {
351 ns_hi = ns;
352 divisor_hi = divisor;
353 }
354 /* Check for slowest found timing */
355 if (ns >= *nanosec && ns < ns_lo) {
356 ns_lo = ns;
357 divisor_lo = divisor;
358 }
359 }
360 }
361
362 /*
363 * The slowest found timing will be invalid if the requested timing
364 * is faster than what can be generated by the timer. Fix it so that
365 * CMDF_ROUND_UP returns valid timing.
366 */
367 if (ns_lo == COMEDI_MIN_SPEED) {
368 ns_lo = ns_hi;
369 divisor_lo = divisor_hi;
370 }
371 /*
372 * The fastest found timing will be invalid if the requested timing
373 * is less than what can be generated by the timer. Fix it so that
374 * CMDF_ROUND_NEAREST and CMDF_ROUND_DOWN return valid timing.
375 */
376 if (ns_hi == 0) {
377 ns_hi = ns_lo;
378 divisor_hi = divisor_lo;
379 }
380
381 switch (flags & CMDF_ROUND_MASK) {
382 case CMDF_ROUND_NEAREST:
383 default:
384 if (ns_hi - *nanosec < *nanosec - ns_lo) {
385 *nanosec = ns_lo;
386 return divisor_lo;
387 }
388 *nanosec = ns_hi;
389 return divisor_hi;
390 case CMDF_ROUND_UP:
391 *nanosec = ns_lo;
392 return divisor_lo;
393 case CMDF_ROUND_DOWN:
394 *nanosec = ns_hi;
395 return divisor_hi;
396 }
397}
398
399static int dt2811_ai_cmdtest(struct comedi_device *dev,
400 struct comedi_subdevice *s,
401 struct comedi_cmd *cmd)
402{
403 struct dt2811_private *devpriv = dev->private;
404 unsigned int arg;
405 int err = 0;
406
407 /* Step 1 : check if triggers are trivially valid */
408
409 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_EXT);
410 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_FOLLOW);
411 err |= comedi_check_trigger_src(&cmd->convert_src,
412 TRIG_TIMER | TRIG_EXT);
413 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
414 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
415
416 if (err)
417 return 1;
418
419 /* Step 2a : make sure trigger sources are unique */
420
421 err |= comedi_check_trigger_is_unique(cmd->start_src);
422 err |= comedi_check_trigger_is_unique(cmd->convert_src);
423 err |= comedi_check_trigger_is_unique(cmd->stop_src);
424
425 /* Step 2b : and mutually compatible */
426
427 if (cmd->convert_src == TRIG_EXT && cmd->start_src != TRIG_EXT)
428 err |= -EINVAL;
429
430 if (err)
431 return 2;
432
433 /* Step 3: check if arguments are trivially valid */
434
435 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
436 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
437 if (cmd->convert_src == TRIG_TIMER)
438 err |= comedi_check_trigger_arg_min(&cmd->convert_arg, 12500);
439 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
440 cmd->chanlist_len);
441 if (cmd->stop_src == TRIG_COUNT)
442 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
443 else /* TRIG_NONE */
444 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
445
446 if (err)
447 return 3;
448
449 /* Step 4: fix up any arguments */
450
451 if (cmd->convert_src == TRIG_TIMER) {
452 arg = cmd->convert_arg;
453 devpriv->ai_divisor = dt2811_ns_to_timer(&arg, cmd->flags);
454 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
455 } else { /* TRIG_EXT */
456 /* The convert_arg is used to set the divisor. */
457 devpriv->ai_divisor = cmd->convert_arg;
458 }
459
460 if (err)
461 return 4;
462
463 /* Step 5: check channel list if it exists */
464
465 return 0;
466}
467
H Hartley Sweeten7c4ede32014-02-10 11:49:20 -0700468static int dt2811_ai_eoc(struct comedi_device *dev,
469 struct comedi_subdevice *s,
470 struct comedi_insn *insn,
471 unsigned long context)
472{
473 unsigned int status;
474
H Hartley Sweeten022ac952016-05-04 12:47:09 -0700475 status = inb(dev->iobase + DT2811_ADCSR_REG);
476 if ((status & DT2811_ADCSR_ADBUSY) == 0)
H Hartley Sweeten7c4ede32014-02-10 11:49:20 -0700477 return 0;
478 return -EBUSY;
479}
David Schleef3c443712009-02-19 10:08:26 -0800480
H Hartley Sweetenf52e8d02016-05-04 12:47:21 -0700481static int dt2811_ai_insn_read(struct comedi_device *dev,
482 struct comedi_subdevice *s,
483 struct comedi_insn *insn,
484 unsigned int *data)
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700485{
H Hartley Sweeten7c4ede32014-02-10 11:49:20 -0700486 int ret;
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700487 int i;
488
H Hartley Sweetenf2975a92016-05-04 15:25:38 -0700489 /* We will already be in Mode 0 */
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700490 for (i = 0; i < insn->n; i++) {
H Hartley Sweetenf2975a92016-05-04 15:25:38 -0700491 /* load chanspec and trigger conversion */
492 dt2811_ai_set_chanspec(dev, insn->chanspec);
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700493
H Hartley Sweeten7c4ede32014-02-10 11:49:20 -0700494 ret = comedi_timeout(dev, s, insn, dt2811_ai_eoc, 0);
495 if (ret)
496 return ret;
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700497
H Hartley Sweetenf2975a92016-05-04 15:25:38 -0700498 data[i] = dt2811_ai_read_sample(dev, s);
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700499 }
500
H Hartley Sweetenf2975a92016-05-04 15:25:38 -0700501 return insn->n;
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700502}
503
H Hartley Sweetenb33bad92014-08-25 16:04:20 -0700504static int dt2811_ao_insn_write(struct comedi_device *dev,
505 struct comedi_subdevice *s,
506 struct comedi_insn *insn,
507 unsigned int *data)
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700508{
H Hartley Sweetenb33bad92014-08-25 16:04:20 -0700509 unsigned int chan = CR_CHAN(insn->chanspec);
510 unsigned int val = s->readback[chan];
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700511 int i;
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700512
513 for (i = 0; i < insn->n; i++) {
H Hartley Sweetenb33bad92014-08-25 16:04:20 -0700514 val = data[i];
H Hartley Sweetenaddb85b2016-05-04 12:47:12 -0700515 outb(val & 0xff, dev->iobase + DT2811_DADATA_LO_REG(chan));
H Hartley Sweetenb33bad92014-08-25 16:04:20 -0700516 outb((val >> 8) & 0xff,
H Hartley Sweetenaddb85b2016-05-04 12:47:12 -0700517 dev->iobase + DT2811_DADATA_HI_REG(chan));
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700518 }
H Hartley Sweetenb33bad92014-08-25 16:04:20 -0700519 s->readback[chan] = val;
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700520
H Hartley Sweetenb33bad92014-08-25 16:04:20 -0700521 return insn->n;
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700522}
523
524static int dt2811_di_insn_bits(struct comedi_device *dev,
525 struct comedi_subdevice *s,
H Hartley Sweeten1d1209f2016-05-04 12:47:15 -0700526 struct comedi_insn *insn,
527 unsigned int *data)
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700528{
H Hartley Sweeten3d6dc782016-05-04 12:47:13 -0700529 data[1] = inb(dev->iobase + DT2811_DI_REG);
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700530
H Hartley Sweetena2714e32012-06-18 13:16:35 -0700531 return insn->n;
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700532}
533
534static int dt2811_do_insn_bits(struct comedi_device *dev,
535 struct comedi_subdevice *s,
H Hartley Sweeten97f42892013-08-30 11:05:58 -0700536 struct comedi_insn *insn,
537 unsigned int *data)
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700538{
H Hartley Sweeten97f42892013-08-30 11:05:58 -0700539 if (comedi_dio_update_state(s, data))
H Hartley Sweeten3d6dc782016-05-04 12:47:13 -0700540 outb(s->state, dev->iobase + DT2811_DO_REG);
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700541
542 data[1] = s->state;
543
H Hartley Sweetena2714e32012-06-18 13:16:35 -0700544 return insn->n;
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700545}
546
H Hartley Sweetenf2975a92016-05-04 15:25:38 -0700547static void dt2811_reset(struct comedi_device *dev)
548{
549 /* This is the initialization sequence from the users manual */
550 outb(DT2811_ADCSR_ADMODE(0), dev->iobase + DT2811_ADCSR_REG);
551 usleep_range(100, 1000);
552 inb(dev->iobase + DT2811_ADDATA_LO_REG);
553 inb(dev->iobase + DT2811_ADDATA_HI_REG);
554 outb(DT2811_ADCSR_ADMODE(0) | DT2811_ADCSR_CLRERROR,
555 dev->iobase + DT2811_ADCSR_REG);
556}
557
Bill Pembertonda91b262009-04-09 16:07:03 -0400558static int dt2811_attach(struct comedi_device *dev, struct comedi_devconfig *it)
David Schleef3c443712009-02-19 10:08:26 -0800559{
Ian Abbotta5a74072014-09-09 11:26:32 +0100560 const struct dt2811_board *board = dev->board_ptr;
H Hartley Sweetenf2975a92016-05-04 15:25:38 -0700561 struct dt2811_private *devpriv;
Bill Pemberton34c43922009-03-16 22:05:14 -0400562 struct comedi_subdevice *s;
H Hartley Sweeten6e4e38b2016-05-04 12:47:08 -0700563 int ret;
David Schleef3c443712009-02-19 10:08:26 -0800564
H Hartley Sweetenf2975a92016-05-04 15:25:38 -0700565 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
566 if (!devpriv)
567 return -ENOMEM;
568
H Hartley Sweeten862755e2014-07-18 17:01:22 -0700569 ret = comedi_request_region(dev, it->options[0], 0x8);
H Hartley Sweeten6ca3f282013-04-09 16:18:23 -0700570 if (ret)
571 return ret;
David Schleef3c443712009-02-19 10:08:26 -0800572
H Hartley Sweetenf2975a92016-05-04 15:25:38 -0700573 dt2811_reset(dev);
574
575 /* IRQ's 2,3,5,7 are valid for async command support */
576 if (it->options[1] <= 7 && (BIT(it->options[1]) & 0xac)) {
577 ret = request_irq(it->options[1], dt2811_interrupt, 0,
578 dev->board_name, dev);
579 if (ret == 0)
580 dev->irq = it->options[1];
581 }
582
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -0700583 ret = comedi_alloc_subdevices(dev, 4);
H Hartley Sweeten8b6c5692012-06-12 11:59:33 -0700584 if (ret)
David Schleef3c443712009-02-19 10:08:26 -0800585 return ret;
Bill Pembertonc3744132009-04-22 21:11:47 -0400586
H Hartley Sweetenf52e8d02016-05-04 12:47:21 -0700587 /* Analog Input subdevice */
H Hartley Sweeten4ea49892012-09-05 18:39:51 -0700588 s = &dev->subdevices[0];
H Hartley Sweetenf52e8d02016-05-04 12:47:21 -0700589 s->type = COMEDI_SUBD_AI;
590 s->subdev_flags = SDF_READABLE |
Dan Carpenter5ac5c3b2016-06-21 14:46:21 +0300591 ((it->options[2] == 1) ? SDF_DIFF :
592 (it->options[2] == 2) ? SDF_COMMON : SDF_GROUND);
H Hartley Sweetenf52e8d02016-05-04 12:47:21 -0700593 s->n_chan = (it->options[2] == 1) ? 8 : 16;
594 s->maxdata = 0x0fff;
H Hartley Sweeten048e27a2016-05-04 12:47:20 -0700595 s->range_table = board->is_pgh ? &dt2811_pgh_ai_ranges
596 : &dt2811_pgl_ai_ranges;
H Hartley Sweetenf52e8d02016-05-04 12:47:21 -0700597 s->insn_read = dt2811_ai_insn_read;
H Hartley Sweetenf2975a92016-05-04 15:25:38 -0700598 if (dev->irq) {
599 dev->read_subdev = s;
600 s->subdev_flags |= SDF_CMD_READ;
601 s->len_chanlist = 1;
602 s->do_cmdtest = dt2811_ai_cmdtest;
603 s->do_cmd = dt2811_ai_cmd;
604 s->cancel = dt2811_ai_cancel;
605 }
David Schleef3c443712009-02-19 10:08:26 -0800606
H Hartley Sweetendedfdf92016-05-04 12:47:17 -0700607 /* Analog Output subdevice */
H Hartley Sweeten4ea49892012-09-05 18:39:51 -0700608 s = &dev->subdevices[1];
H Hartley Sweetendedfdf92016-05-04 12:47:17 -0700609 s->type = COMEDI_SUBD_AO;
610 s->subdev_flags = SDF_WRITABLE;
611 s->n_chan = 2;
612 s->maxdata = 0x0fff;
613 s->range_table = &dt2811_ao_ranges;
614 s->insn_write = dt2811_ao_insn_write;
H Hartley Sweetenb33bad92014-08-25 16:04:20 -0700615
616 ret = comedi_alloc_subdev_readback(s);
617 if (ret)
618 return ret;
David Schleef3c443712009-02-19 10:08:26 -0800619
H Hartley Sweeten1d1209f2016-05-04 12:47:15 -0700620 /* Digital Input subdevice */
H Hartley Sweeten4ea49892012-09-05 18:39:51 -0700621 s = &dev->subdevices[2];
H Hartley Sweeten1d1209f2016-05-04 12:47:15 -0700622 s->type = COMEDI_SUBD_DI;
623 s->subdev_flags = SDF_READABLE;
624 s->n_chan = 8;
625 s->maxdata = 1;
626 s->range_table = &range_digital;
627 s->insn_bits = dt2811_di_insn_bits;
David Schleef3c443712009-02-19 10:08:26 -0800628
H Hartley Sweeten1d1209f2016-05-04 12:47:15 -0700629 /* Digital Output subdevice */
H Hartley Sweeten4ea49892012-09-05 18:39:51 -0700630 s = &dev->subdevices[3];
H Hartley Sweeten1d1209f2016-05-04 12:47:15 -0700631 s->type = COMEDI_SUBD_DO;
632 s->subdev_flags = SDF_WRITABLE;
633 s->n_chan = 8;
634 s->maxdata = 1;
635 s->range_table = &range_digital;
636 s->insn_bits = dt2811_do_insn_bits;
David Schleef3c443712009-02-19 10:08:26 -0800637
638 return 0;
639}
640
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700641static struct comedi_driver dt2811_driver = {
642 .driver_name = "dt2811",
643 .module = THIS_MODULE,
644 .attach = dt2811_attach,
H Hartley Sweeten3d1fe3f2013-04-18 14:34:37 -0700645 .detach = comedi_legacy_detach,
H Hartley Sweeten99101312016-05-04 12:47:23 -0700646 .board_name = &dt2811_boards[0].name,
647 .num_names = ARRAY_SIZE(dt2811_boards),
H Hartley Sweeten5675d892012-05-15 16:35:38 -0700648 .offset = sizeof(struct dt2811_board),
649};
650module_comedi_driver(dt2811_driver);
Arun Thomas90f703d2010-06-06 22:23:29 +0200651
652MODULE_AUTHOR("Comedi http://www.comedi.org");
H Hartley Sweeten277b8612016-05-04 12:47:24 -0700653MODULE_DESCRIPTION("Comedi driver for Data Translation DT2811 series boards");
Arun Thomas90f703d2010-06-06 22:23:29 +0200654MODULE_LICENSE("GPL");