2 comedi/drivers/dt282x.c
3 Hardware driver for Data Translation DT2821 series
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-8 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.
20 Description: Data Translation DT2821 series (including DT-EZ)
22 Devices: [Data Translation] DT2821 (dt2821),
23 DT2821-F-16SE (dt2821-f), DT2821-F-8DI (dt2821-f),
24 DT2821-G-16SE (dt2821-f), DT2821-G-8DI (dt2821-g),
26 DT2824-PGH (dt2824-pgh), DT2824-PGL (dt2824-pgl), DT2825 (dt2825),
27 DT2827 (dt2827), DT2828 (dt2828), DT21-EZ (dt21-ez), DT23-EZ (dt23-ez),
28 DT24-EZ (dt24-ez), DT24-EZ-PGL (dt24-ez-pgl)
30 Updated: Wed, 22 Aug 2001 17:11:34 -0700
32 Configuration options:
33 [0] - I/O port base address
37 [4] - AI jumpered for 0=single ended, 1=differential
38 [5] - AI jumpered for 0=straight binary, 1=2's complement
39 [6] - AO 0 jumpered for 0=straight binary, 1=2's complement
40 [7] - AO 1 jumpered for 0=straight binary, 1=2's complement
41 [8] - AI jumpered for 0=[-10,10]V, 1=[0,10], 2=[-5,5], 3=[0,5]
42 [9] - AO 0 jumpered for 0=[-10,10]V, 1=[0,10], 2=[-5,5], 3=[0,5],
44 [10]- A0 1 jumpered for 0=[-10,10]V, 1=[0,10], 2=[-5,5], 3=[0,5],
48 - AO commands might be broken.
49 - If you try to run a command on both the AI and AO subdevices
50 simultaneously, bad things will happen. The driver needs to
51 be fixed to check for this situation and return an error.
54 #include <linux/module.h>
55 #include "../comedidev.h"
57 #include <linux/delay.h>
58 #include <linux/gfp.h>
59 #include <linux/interrupt.h>
64 #include "comedi_fc.h"
66 #define DT2821_TIMEOUT 100 /* 500 us */
67 #define DT2821_SIZE 0x10
70 * Registers in the DT282x
73 #define DT2821_ADCSR 0x00 /* A/D Control/Status */
74 #define DT2821_CHANCSR 0x02 /* Channel Control/Status */
75 #define DT2821_ADDAT 0x04 /* A/D data */
76 #define DT2821_DACSR 0x06 /* D/A Control/Status */
77 #define DT2821_DADAT 0x08 /* D/A data */
78 #define DT2821_DIODAT 0x0a /* digital data */
79 #define DT2821_SUPCSR 0x0c /* Supervisor Control/Status */
80 #define DT2821_TMRCTR 0x0e /* Timer/Counter */
83 * At power up, some registers are in a well-known state. The
84 * masks and values are as follows:
87 #define DT2821_ADCSR_MASK 0xfff0
88 #define DT2821_ADCSR_VAL 0x7c00
90 #define DT2821_CHANCSR_MASK 0xf0f0
91 #define DT2821_CHANCSR_VAL 0x70f0
93 #define DT2821_DACSR_MASK 0x7c93
94 #define DT2821_DACSR_VAL 0x7c90
96 #define DT2821_SUPCSR_MASK 0xf8ff
97 #define DT2821_SUPCSR_VAL 0x0000
99 #define DT2821_TMRCTR_MASK 0xff00
100 #define DT2821_TMRCTR_VAL 0xf000
103 * Bit fields of each register
108 #define DT2821_ADERR 0x8000 /* (R) 1 for A/D error */
109 #define DT2821_ADCLK 0x0200 /* (R/W) A/D clock enable */
110 /* 0x7c00 read as 1's */
111 #define DT2821_MUXBUSY 0x0100 /* (R) multiplexer busy */
112 #define DT2821_ADDONE 0x0080 /* (R) A/D done */
113 #define DT2821_IADDONE 0x0040 /* (R/W) interrupt on A/D done */
114 /* 0x0030 gain select */
115 /* 0x000f channel select */
119 #define DT2821_LLE 0x8000 /* (R/W) Load List Enable */
120 /* 0x7000 read as 1's */
121 /* 0x0f00 (R) present address */
122 /* 0x00f0 read as 1's */
123 /* 0x000f (R) number of entries - 1 */
127 #define DT2821_DAERR 0x8000 /* (R) D/A error */
128 #define DT2821_YSEL 0x0200 /* (R/W) DAC 1 select */
129 #define DT2821_SSEL 0x0100 /* (R/W) single channel select */
130 #define DT2821_DACRDY 0x0080 /* (R) DAC ready */
131 #define DT2821_IDARDY 0x0040 /* (R/W) interrupt on DAC ready */
132 #define DT2821_DACLK 0x0020 /* (R/W) D/A clock enable */
133 #define DT2821_HBOE 0x0002 /* (R/W) DIO high byte output enable */
134 #define DT2821_LBOE 0x0001 /* (R/W) DIO low byte output enable */
138 #define DT2821_DMAD 0x8000 /* (R) DMA done */
139 #define DT2821_ERRINTEN 0x4000 /* (R/W) interrupt on error */
140 #define DT2821_CLRDMADNE 0x2000 /* (W) clear DMA done */
141 #define DT2821_DDMA 0x1000 /* (R/W) dual DMA */
142 #define DT2821_DS1 0x0800 /* (R/W) DMA select 1 */
143 #define DT2821_DS0 0x0400 /* (R/W) DMA select 0 */
144 #define DT2821_BUFFB 0x0200 /* (R/W) buffer B selected */
145 #define DT2821_SCDN 0x0100 /* (R) scan done */
146 #define DT2821_DACON 0x0080 /* (W) DAC single conversion */
147 #define DT2821_ADCINIT 0x0040 /* (W) A/D initialize */
148 #define DT2821_DACINIT 0x0020 /* (W) D/A initialize */
149 #define DT2821_PRLD 0x0010 /* (W) preload multiplexer */
150 #define DT2821_STRIG 0x0008 /* (W) software trigger */
151 #define DT2821_XTRIG 0x0004 /* (R/W) external trigger enable */
152 #define DT2821_XCLK 0x0002 /* (R/W) external clock enable */
153 #define DT2821_BDINIT 0x0001 /* (W) initialize board */
155 static const struct comedi_lrange range_dt282x_ai_lo_bipolar
= {
164 static const struct comedi_lrange range_dt282x_ai_lo_unipolar
= {
173 static const struct comedi_lrange range_dt282x_ai_5_bipolar
= {
182 static const struct comedi_lrange range_dt282x_ai_5_unipolar
= {
191 static const struct comedi_lrange range_dt282x_ai_hi_bipolar
= {
200 static const struct comedi_lrange range_dt282x_ai_hi_unipolar
= {
209 struct dt282x_board
{
220 struct dt282x_private
{
221 int ad_2scomp
; /* we have 2's comp jumper set */
222 int da0_2scomp
; /* same, for DAC0 */
223 int da1_2scomp
; /* same, for DAC1 */
225 const struct comedi_lrange
*darangelist
[2];
227 unsigned short ao
[2];
229 volatile int dacsr
; /* software copies of registers */
238 unsigned short *buf
; /* DMA buffer */
239 volatile int size
; /* size of current transfer */
241 int dma_maxsize
; /* max size of DMA transfer (in bytes) */
242 int usedma
; /* driver uses DMA */
243 volatile int current_dma_index
;
248 * Some useless abstractions
250 #define chan_to_DAC(a) ((a)&1)
251 #define mux_busy() (inw(dev->iobase+DT2821_ADCSR)&DT2821_MUXBUSY)
252 #define ad_done() (inw(dev->iobase+DT2821_ADCSR)&DT2821_ADDONE)
255 * danger! macro abuse... a is the expression to wait on, and b is
256 * the statement(s) to execute if it doesn't happen.
258 #define wait_for(a, b) \
261 for (_i = 0; _i < DT2821_TIMEOUT; _i++) { \
273 static int prep_ai_dma(struct comedi_device
*dev
, int chan
, int size
);
274 static int prep_ao_dma(struct comedi_device
*dev
, int chan
, int size
);
275 static int dt282x_ai_cancel(struct comedi_device
*dev
,
276 struct comedi_subdevice
*s
);
277 static int dt282x_ao_cancel(struct comedi_device
*dev
,
278 struct comedi_subdevice
*s
);
279 static int dt282x_ns_to_timer(int *nanosec
, int round_mode
);
280 static void dt282x_disable_dma(struct comedi_device
*dev
);
282 static int dt282x_grab_dma(struct comedi_device
*dev
, int dma1
, int dma2
);
284 static void dt282x_munge(struct comedi_device
*dev
, unsigned short *buf
,
287 const struct dt282x_board
*board
= comedi_board(dev
);
288 struct dt282x_private
*devpriv
= dev
->private;
290 unsigned short mask
= (1 << board
->adbits
) - 1;
291 unsigned short sign
= 1 << (board
->adbits
- 1);
294 if (devpriv
->ad_2scomp
)
295 sign
= 1 << (board
->adbits
- 1);
300 comedi_error(dev
, "bug! odd number of bytes from dma xfer");
302 for (i
= 0; i
< n
; i
++)
303 buf
[i
] = (buf
[i
] & mask
) ^ sign
;
306 static void dt282x_ao_dma_interrupt(struct comedi_device
*dev
)
308 struct dt282x_private
*devpriv
= dev
->private;
312 struct comedi_subdevice
*s
= &dev
->subdevices
[1];
314 outw(devpriv
->supcsr
| DT2821_CLRDMADNE
, dev
->iobase
+ DT2821_SUPCSR
);
316 if (!s
->async
->prealloc_buf
) {
317 dev_err(dev
->class_dev
, "no buffer in %s\n", __func__
);
321 i
= devpriv
->current_dma_index
;
322 ptr
= devpriv
->dma
[i
].buf
;
324 disable_dma(devpriv
->dma
[i
].chan
);
326 devpriv
->current_dma_index
= 1 - i
;
328 size
= cfc_read_array_from_buffer(s
, ptr
, devpriv
->dma_maxsize
);
330 dev_err(dev
->class_dev
, "AO underrun\n");
331 dt282x_ao_cancel(dev
, s
);
332 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
335 prep_ao_dma(dev
, i
, size
);
339 static void dt282x_ai_dma_interrupt(struct comedi_device
*dev
)
341 struct dt282x_private
*devpriv
= dev
->private;
342 struct comedi_subdevice
*s
= dev
->read_subdev
;
348 outw(devpriv
->supcsr
| DT2821_CLRDMADNE
, dev
->iobase
+ DT2821_SUPCSR
);
350 if (!s
->async
->prealloc_buf
) {
351 dev_err(dev
->class_dev
, "no buffer in %s\n", __func__
);
355 i
= devpriv
->current_dma_index
;
356 ptr
= devpriv
->dma
[i
].buf
;
357 size
= devpriv
->dma
[i
].size
;
359 disable_dma(devpriv
->dma
[i
].chan
);
361 devpriv
->current_dma_index
= 1 - i
;
363 dt282x_munge(dev
, ptr
, size
);
364 ret
= cfc_write_array_to_buffer(s
, ptr
, size
);
366 dt282x_ai_cancel(dev
, s
);
369 devpriv
->nread
-= size
/ 2;
371 if (devpriv
->nread
< 0) {
372 dev_info(dev
->class_dev
, "nread off by one\n");
375 if (!devpriv
->nread
) {
376 dt282x_ai_cancel(dev
, s
);
377 s
->async
->events
|= COMEDI_CB_EOA
;
381 /* clear the dual dma flag, making this the last dma segment */
382 /* XXX probably wrong */
383 if (!devpriv
->ntrig
) {
384 devpriv
->supcsr
&= ~(DT2821_DDMA
);
385 outw(devpriv
->supcsr
, dev
->iobase
+ DT2821_SUPCSR
);
388 /* restart the channel */
389 prep_ai_dma(dev
, i
, 0);
392 static int prep_ai_dma(struct comedi_device
*dev
, int dma_index
, int n
)
394 struct dt282x_private
*devpriv
= dev
->private;
396 unsigned long dma_ptr
;
403 n
= devpriv
->dma_maxsize
;
404 if (n
> devpriv
->ntrig
* 2)
405 n
= devpriv
->ntrig
* 2;
406 devpriv
->ntrig
-= n
/ 2;
408 devpriv
->dma
[dma_index
].size
= n
;
409 dma_chan
= devpriv
->dma
[dma_index
].chan
;
410 dma_ptr
= virt_to_bus(devpriv
->dma
[dma_index
].buf
);
412 set_dma_mode(dma_chan
, DMA_MODE_READ
);
413 flags
= claim_dma_lock();
414 clear_dma_ff(dma_chan
);
415 set_dma_addr(dma_chan
, dma_ptr
);
416 set_dma_count(dma_chan
, n
);
417 release_dma_lock(flags
);
419 enable_dma(dma_chan
);
424 static int prep_ao_dma(struct comedi_device
*dev
, int dma_index
, int n
)
426 struct dt282x_private
*devpriv
= dev
->private;
428 unsigned long dma_ptr
;
431 devpriv
->dma
[dma_index
].size
= n
;
432 dma_chan
= devpriv
->dma
[dma_index
].chan
;
433 dma_ptr
= virt_to_bus(devpriv
->dma
[dma_index
].buf
);
435 set_dma_mode(dma_chan
, DMA_MODE_WRITE
);
436 flags
= claim_dma_lock();
437 clear_dma_ff(dma_chan
);
438 set_dma_addr(dma_chan
, dma_ptr
);
439 set_dma_count(dma_chan
, n
);
440 release_dma_lock(flags
);
442 enable_dma(dma_chan
);
447 static irqreturn_t
dt282x_interrupt(int irq
, void *d
)
449 struct comedi_device
*dev
= d
;
450 struct dt282x_private
*devpriv
= dev
->private;
451 struct comedi_subdevice
*s
= dev
->read_subdev
;
452 struct comedi_subdevice
*s_ao
;
453 unsigned int supcsr
, adcsr
, dacsr
;
456 if (!dev
->attached
) {
457 comedi_error(dev
, "spurious interrupt");
461 s_ao
= &dev
->subdevices
[1];
462 adcsr
= inw(dev
->iobase
+ DT2821_ADCSR
);
463 dacsr
= inw(dev
->iobase
+ DT2821_DACSR
);
464 supcsr
= inw(dev
->iobase
+ DT2821_SUPCSR
);
465 if (supcsr
& DT2821_DMAD
) {
466 if (devpriv
->dma_dir
== DMA_MODE_READ
)
467 dt282x_ai_dma_interrupt(dev
);
469 dt282x_ao_dma_interrupt(dev
);
472 if (adcsr
& DT2821_ADERR
) {
473 if (devpriv
->nread
!= 0) {
474 comedi_error(dev
, "A/D error");
475 dt282x_ai_cancel(dev
, s
);
476 s
->async
->events
|= COMEDI_CB_ERROR
;
480 if (dacsr
& DT2821_DAERR
) {
481 comedi_error(dev
, "D/A error");
482 dt282x_ao_cancel(dev
, s_ao
);
483 s
->async
->events
|= COMEDI_CB_ERROR
;
487 if (adcsr
& DT2821_ADDONE
) {
491 data
= inw(dev
->iobase
+ DT2821_ADDAT
);
492 data
&= (1 << board
->adbits
) - 1;
494 if (devpriv
->ad_2scomp
)
495 data
^= 1 << (board
->adbits
- 1);
496 ret
= comedi_buf_put(s
->async
, data
);
499 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
502 if (!devpriv
->nread
) {
503 s
->async
->events
|= COMEDI_CB_EOA
;
505 if (supcsr
& DT2821_SCDN
)
506 outw(devpriv
->supcsr
| DT2821_STRIG
,
507 dev
->iobase
+ DT2821_SUPCSR
);
512 comedi_event(dev
, s
);
514 return IRQ_RETVAL(handled
);
517 static void dt282x_load_changain(struct comedi_device
*dev
, int n
,
518 unsigned int *chanlist
)
520 struct dt282x_private
*devpriv
= dev
->private;
522 unsigned int chan
, range
;
524 outw(DT2821_LLE
| (n
- 1), dev
->iobase
+ DT2821_CHANCSR
);
525 for (i
= 0; i
< n
; i
++) {
526 chan
= CR_CHAN(chanlist
[i
]);
527 range
= CR_RANGE(chanlist
[i
]);
528 outw(devpriv
->adcsr
| (range
<< 4) | chan
,
529 dev
->iobase
+ DT2821_ADCSR
);
531 outw(n
- 1, dev
->iobase
+ DT2821_CHANCSR
);
535 * Performs a single A/D conversion.
536 * - Put channel/gain into channel-gain list
537 * - preload multiplexer
538 * - trigger conversion and wait for it to finish
540 static int dt282x_ai_insn_read(struct comedi_device
*dev
,
541 struct comedi_subdevice
*s
,
542 struct comedi_insn
*insn
, unsigned int *data
)
544 const struct dt282x_board
*board
= comedi_board(dev
);
545 struct dt282x_private
*devpriv
= dev
->private;
548 /* XXX should we really be enabling the ad clock here? */
549 devpriv
->adcsr
= DT2821_ADCLK
;
550 outw(devpriv
->adcsr
, dev
->iobase
+ DT2821_ADCSR
);
552 dt282x_load_changain(dev
, 1, &insn
->chanspec
);
554 outw(devpriv
->supcsr
| DT2821_PRLD
, dev
->iobase
+ DT2821_SUPCSR
);
555 wait_for(!mux_busy(), comedi_error(dev
, "timeout\n"); return -ETIME
;);
557 for (i
= 0; i
< insn
->n
; i
++) {
558 outw(devpriv
->supcsr
| DT2821_STRIG
,
559 dev
->iobase
+ DT2821_SUPCSR
);
560 wait_for(ad_done(), comedi_error(dev
, "timeout\n");
565 DT2821_ADDAT
) & ((1 << board
->adbits
) - 1);
566 if (devpriv
->ad_2scomp
)
567 data
[i
] ^= (1 << (board
->adbits
- 1));
573 static int dt282x_ai_cmdtest(struct comedi_device
*dev
,
574 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
)
576 const struct dt282x_board
*board
= comedi_board(dev
);
580 /* Step 1 : check if triggers are trivially valid */
582 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_NOW
);
583 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
,
584 TRIG_FOLLOW
| TRIG_EXT
);
585 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_TIMER
);
586 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
587 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
592 /* Step 2a : make sure trigger sources are unique */
594 err
|= cfc_check_trigger_is_unique(cmd
->scan_begin_src
);
595 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
597 /* Step 2b : and mutually compatible */
602 /* Step 3: check if arguments are trivially valid */
604 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, 0);
606 if (cmd
->scan_begin_src
== TRIG_FOLLOW
) {
607 /* internal trigger */
608 err
|= cfc_check_trigger_arg_is(&cmd
->scan_begin_arg
, 0);
610 /* external trigger */
611 /* should be level/edge, hi/lo specification here */
612 err
|= cfc_check_trigger_arg_is(&cmd
->scan_begin_arg
, 0);
615 err
|= cfc_check_trigger_arg_min(&cmd
->convert_arg
, 4000);
617 #define SLOWEST_TIMER (250*(1<<15)*255)
618 err
|= cfc_check_trigger_arg_max(&cmd
->convert_arg
, SLOWEST_TIMER
);
619 err
|= cfc_check_trigger_arg_min(&cmd
->convert_arg
, board
->ai_speed
);
620 err
|= cfc_check_trigger_arg_is(&cmd
->scan_end_arg
, cmd
->chanlist_len
);
622 if (cmd
->stop_src
== TRIG_COUNT
) {
623 /* any count is allowed */
624 } else { /* TRIG_NONE */
625 err
|= cfc_check_trigger_arg_is(&cmd
->stop_arg
, 0);
631 /* step 4: fix up any arguments */
633 tmp
= cmd
->convert_arg
;
634 dt282x_ns_to_timer(&cmd
->convert_arg
, cmd
->flags
& TRIG_ROUND_MASK
);
635 if (tmp
!= cmd
->convert_arg
)
644 static int dt282x_ai_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
646 const struct dt282x_board
*board
= comedi_board(dev
);
647 struct dt282x_private
*devpriv
= dev
->private;
648 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
651 if (devpriv
->usedma
== 0) {
653 "driver requires 2 dma channels"
654 " to execute command");
658 dt282x_disable_dma(dev
);
660 if (cmd
->convert_arg
< board
->ai_speed
)
661 cmd
->convert_arg
= board
->ai_speed
;
662 timer
= dt282x_ns_to_timer(&cmd
->convert_arg
, TRIG_ROUND_NEAREST
);
663 outw(timer
, dev
->iobase
+ DT2821_TMRCTR
);
665 if (cmd
->scan_begin_src
== TRIG_FOLLOW
) {
666 /* internal trigger */
667 devpriv
->supcsr
= DT2821_ERRINTEN
| DT2821_DS0
;
669 /* external trigger */
670 devpriv
->supcsr
= DT2821_ERRINTEN
| DT2821_DS0
| DT2821_DS1
;
672 outw(devpriv
->supcsr
| DT2821_CLRDMADNE
| DT2821_BUFFB
| DT2821_ADCINIT
,
673 dev
->iobase
+ DT2821_SUPCSR
);
675 devpriv
->ntrig
= cmd
->stop_arg
* cmd
->scan_end_arg
;
676 devpriv
->nread
= devpriv
->ntrig
;
678 devpriv
->dma_dir
= DMA_MODE_READ
;
679 devpriv
->current_dma_index
= 0;
680 prep_ai_dma(dev
, 0, 0);
681 if (devpriv
->ntrig
) {
682 prep_ai_dma(dev
, 1, 0);
683 devpriv
->supcsr
|= DT2821_DDMA
;
684 outw(devpriv
->supcsr
, dev
->iobase
+ DT2821_SUPCSR
);
689 dt282x_load_changain(dev
, cmd
->chanlist_len
, cmd
->chanlist
);
691 devpriv
->adcsr
= DT2821_ADCLK
| DT2821_IADDONE
;
692 outw(devpriv
->adcsr
, dev
->iobase
+ DT2821_ADCSR
);
694 outw(devpriv
->supcsr
| DT2821_PRLD
, dev
->iobase
+ DT2821_SUPCSR
);
695 wait_for(!mux_busy(), comedi_error(dev
, "timeout\n"); return -ETIME
;);
697 if (cmd
->scan_begin_src
== TRIG_FOLLOW
) {
698 outw(devpriv
->supcsr
| DT2821_STRIG
,
699 dev
->iobase
+ DT2821_SUPCSR
);
701 devpriv
->supcsr
|= DT2821_XTRIG
;
702 outw(devpriv
->supcsr
, dev
->iobase
+ DT2821_SUPCSR
);
708 static void dt282x_disable_dma(struct comedi_device
*dev
)
710 struct dt282x_private
*devpriv
= dev
->private;
712 if (devpriv
->usedma
) {
713 disable_dma(devpriv
->dma
[0].chan
);
714 disable_dma(devpriv
->dma
[1].chan
);
718 static int dt282x_ai_cancel(struct comedi_device
*dev
,
719 struct comedi_subdevice
*s
)
721 struct dt282x_private
*devpriv
= dev
->private;
723 dt282x_disable_dma(dev
);
726 outw(devpriv
->adcsr
, dev
->iobase
+ DT2821_ADCSR
);
729 outw(devpriv
->supcsr
| DT2821_ADCINIT
, dev
->iobase
+ DT2821_SUPCSR
);
734 static int dt282x_ns_to_timer(int *nanosec
, int round_mode
)
736 int prescale
, base
, divider
;
738 for (prescale
= 0; prescale
< 16; prescale
++) {
741 base
= 250 * (1 << prescale
);
742 switch (round_mode
) {
743 case TRIG_ROUND_NEAREST
:
745 divider
= (*nanosec
+ base
/ 2) / base
;
747 case TRIG_ROUND_DOWN
:
748 divider
= (*nanosec
) / base
;
751 divider
= (*nanosec
+ base
- 1) / base
;
755 *nanosec
= divider
* base
;
756 return (prescale
<< 8) | (255 - divider
);
759 base
= 250 * (1 << 15);
761 *nanosec
= divider
* base
;
762 return (15 << 8) | (255 - divider
);
766 * Analog output routine. Selects single channel conversion,
767 * selects correct channel, converts from 2's compliment to
768 * offset binary if necessary, loads the data into the DAC
769 * data register, and performs the conversion.
771 static int dt282x_ao_insn_read(struct comedi_device
*dev
,
772 struct comedi_subdevice
*s
,
773 struct comedi_insn
*insn
, unsigned int *data
)
775 struct dt282x_private
*devpriv
= dev
->private;
777 data
[0] = devpriv
->ao
[CR_CHAN(insn
->chanspec
)];
782 static int dt282x_ao_insn_write(struct comedi_device
*dev
,
783 struct comedi_subdevice
*s
,
784 struct comedi_insn
*insn
, unsigned int *data
)
786 const struct dt282x_board
*board
= comedi_board(dev
);
787 struct dt282x_private
*devpriv
= dev
->private;
791 chan
= CR_CHAN(insn
->chanspec
);
793 d
&= (1 << board
->dabits
) - 1;
794 devpriv
->ao
[chan
] = d
;
796 devpriv
->dacsr
|= DT2821_SSEL
;
800 devpriv
->dacsr
|= DT2821_YSEL
;
801 if (devpriv
->da0_2scomp
)
802 d
^= (1 << (board
->dabits
- 1));
804 devpriv
->dacsr
&= ~DT2821_YSEL
;
805 if (devpriv
->da1_2scomp
)
806 d
^= (1 << (board
->dabits
- 1));
809 outw(devpriv
->dacsr
, dev
->iobase
+ DT2821_DACSR
);
811 outw(d
, dev
->iobase
+ DT2821_DADAT
);
813 outw(devpriv
->supcsr
| DT2821_DACON
, dev
->iobase
+ DT2821_SUPCSR
);
818 static int dt282x_ao_cmdtest(struct comedi_device
*dev
,
819 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
)
824 /* Step 1 : check if triggers are trivially valid */
826 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_INT
);
827 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
, TRIG_TIMER
);
828 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
829 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
830 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
835 /* Step 2a : make sure trigger sources are unique */
837 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
839 /* Step 2b : and mutually compatible */
844 /* Step 3: check if arguments are trivially valid */
846 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, 0);
847 err
|= cfc_check_trigger_arg_min(&cmd
->scan_begin_arg
, 5000);
848 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, 0);
849 err
|= cfc_check_trigger_arg_max(&cmd
->scan_end_arg
, 2);
851 if (cmd
->stop_src
== TRIG_COUNT
) {
852 /* any count is allowed */
853 } else { /* TRIG_NONE */
854 err
|= cfc_check_trigger_arg_is(&cmd
->stop_arg
, 0);
860 /* step 4: fix up any arguments */
862 tmp
= cmd
->scan_begin_arg
;
863 dt282x_ns_to_timer(&cmd
->scan_begin_arg
, cmd
->flags
& TRIG_ROUND_MASK
);
864 if (tmp
!= cmd
->scan_begin_arg
)
874 static int dt282x_ao_inttrig(struct comedi_device
*dev
,
875 struct comedi_subdevice
*s
, unsigned int x
)
877 struct dt282x_private
*devpriv
= dev
->private;
883 size
= cfc_read_array_from_buffer(s
, devpriv
->dma
[0].buf
,
884 devpriv
->dma_maxsize
);
886 dev_err(dev
->class_dev
, "AO underrun\n");
889 prep_ao_dma(dev
, 0, size
);
891 size
= cfc_read_array_from_buffer(s
, devpriv
->dma
[1].buf
,
892 devpriv
->dma_maxsize
);
894 dev_err(dev
->class_dev
, "AO underrun\n");
897 prep_ao_dma(dev
, 1, size
);
899 outw(devpriv
->supcsr
| DT2821_STRIG
, dev
->iobase
+ DT2821_SUPCSR
);
900 s
->async
->inttrig
= NULL
;
905 static int dt282x_ao_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
907 struct dt282x_private
*devpriv
= dev
->private;
909 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
911 if (devpriv
->usedma
== 0) {
913 "driver requires 2 dma channels"
914 " to execute command");
918 dt282x_disable_dma(dev
);
920 devpriv
->supcsr
= DT2821_ERRINTEN
| DT2821_DS1
| DT2821_DDMA
;
921 outw(devpriv
->supcsr
| DT2821_CLRDMADNE
| DT2821_BUFFB
| DT2821_DACINIT
,
922 dev
->iobase
+ DT2821_SUPCSR
);
924 devpriv
->ntrig
= cmd
->stop_arg
* cmd
->chanlist_len
;
925 devpriv
->nread
= devpriv
->ntrig
;
927 devpriv
->dma_dir
= DMA_MODE_WRITE
;
928 devpriv
->current_dma_index
= 0;
930 timer
= dt282x_ns_to_timer(&cmd
->scan_begin_arg
, TRIG_ROUND_NEAREST
);
931 outw(timer
, dev
->iobase
+ DT2821_TMRCTR
);
933 devpriv
->dacsr
= DT2821_SSEL
| DT2821_DACLK
| DT2821_IDARDY
;
934 outw(devpriv
->dacsr
, dev
->iobase
+ DT2821_DACSR
);
936 s
->async
->inttrig
= dt282x_ao_inttrig
;
941 static int dt282x_ao_cancel(struct comedi_device
*dev
,
942 struct comedi_subdevice
*s
)
944 struct dt282x_private
*devpriv
= dev
->private;
946 dt282x_disable_dma(dev
);
949 outw(devpriv
->dacsr
, dev
->iobase
+ DT2821_DACSR
);
952 outw(devpriv
->supcsr
| DT2821_DACINIT
, dev
->iobase
+ DT2821_SUPCSR
);
957 static int dt282x_dio_insn_bits(struct comedi_device
*dev
,
958 struct comedi_subdevice
*s
,
959 struct comedi_insn
*insn
,
962 if (comedi_dio_update_state(s
, data
))
963 outw(s
->state
, dev
->iobase
+ DT2821_DIODAT
);
965 data
[1] = inw(dev
->iobase
+ DT2821_DIODAT
);
970 static int dt282x_dio_insn_config(struct comedi_device
*dev
,
971 struct comedi_subdevice
*s
,
972 struct comedi_insn
*insn
,
975 struct dt282x_private
*devpriv
= dev
->private;
976 unsigned int chan
= CR_CHAN(insn
->chanspec
);
985 ret
= comedi_dio_insn_config(dev
, s
, insn
, data
, mask
);
989 devpriv
->dacsr
&= ~(DT2821_LBOE
| DT2821_HBOE
);
990 if (s
->io_bits
& 0x00ff)
991 devpriv
->dacsr
|= DT2821_LBOE
;
992 if (s
->io_bits
& 0xff00)
993 devpriv
->dacsr
|= DT2821_HBOE
;
995 outw(devpriv
->dacsr
, dev
->iobase
+ DT2821_DACSR
);
1000 static const struct comedi_lrange
*const ai_range_table
[] = {
1001 &range_dt282x_ai_lo_bipolar
,
1002 &range_dt282x_ai_lo_unipolar
,
1003 &range_dt282x_ai_5_bipolar
,
1004 &range_dt282x_ai_5_unipolar
1007 static const struct comedi_lrange
*const ai_range_pgl_table
[] = {
1008 &range_dt282x_ai_hi_bipolar
,
1009 &range_dt282x_ai_hi_unipolar
1012 static const struct comedi_lrange
*opt_ai_range_lkup(int ispgl
, int x
)
1015 if (x
< 0 || x
>= 2)
1017 return ai_range_pgl_table
[x
];
1019 if (x
< 0 || x
>= 4)
1021 return ai_range_table
[x
];
1025 static const struct comedi_lrange
*const ao_range_table
[] = {
1033 static const struct comedi_lrange
*opt_ao_range_lkup(int x
)
1035 if (x
< 0 || x
>= 5)
1037 return ao_range_table
[x
];
1040 enum { /* i/o base, irq, dma channels */
1041 opt_iobase
= 0, opt_irq
, opt_dma1
, opt_dma2
,
1042 opt_diff
, /* differential */
1043 opt_ai_twos
, opt_ao0_twos
, opt_ao1_twos
, /* twos comp */
1044 opt_ai_range
, opt_ao0_range
, opt_ao1_range
, /* range */
1047 static int dt282x_grab_dma(struct comedi_device
*dev
, int dma1
, int dma2
)
1049 struct dt282x_private
*devpriv
= dev
->private;
1052 devpriv
->usedma
= 0;
1057 if (dma1
== dma2
|| dma1
< 5 || dma2
< 5 || dma1
> 7 || dma2
> 7)
1067 ret
= request_dma(dma1
, "dt282x A");
1070 devpriv
->dma
[0].chan
= dma1
;
1072 ret
= request_dma(dma2
, "dt282x B");
1075 devpriv
->dma
[1].chan
= dma2
;
1077 devpriv
->dma_maxsize
= PAGE_SIZE
;
1078 devpriv
->dma
[0].buf
= (void *)__get_free_page(GFP_KERNEL
| GFP_DMA
);
1079 devpriv
->dma
[1].buf
= (void *)__get_free_page(GFP_KERNEL
| GFP_DMA
);
1080 if (!devpriv
->dma
[0].buf
|| !devpriv
->dma
[1].buf
)
1083 devpriv
->usedma
= 1;
1094 4 0=single ended, 1=differential
1095 5 ai 0=straight binary, 1=2's comp
1096 6 ao0 0=straight binary, 1=2's comp
1097 7 ao1 0=straight binary, 1=2's comp
1098 8 ai 0=±10 V, 1=0-10 V, 2=±5 V, 3=0-5 V
1099 9 ao0 0=±10 V, 1=0-10 V, 2=±5 V, 3=0-5 V, 4=±2.5 V
1100 10 ao1 0=±10 V, 1=0-10 V, 2=±5 V, 3=0-5 V, 4=±2.5 V
1102 static int dt282x_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
1104 const struct dt282x_board
*board
= comedi_board(dev
);
1105 struct dt282x_private
*devpriv
;
1106 struct comedi_subdevice
*s
;
1110 ret
= comedi_request_region(dev
, it
->options
[0], DT2821_SIZE
);
1114 outw(DT2821_BDINIT
, dev
->iobase
+ DT2821_SUPCSR
);
1115 i
= inw(dev
->iobase
+ DT2821_ADCSR
);
1117 if (((inw(dev
->iobase
+ DT2821_ADCSR
) & DT2821_ADCSR_MASK
)
1118 != DT2821_ADCSR_VAL
) ||
1119 ((inw(dev
->iobase
+ DT2821_CHANCSR
) & DT2821_CHANCSR_MASK
)
1120 != DT2821_CHANCSR_VAL
) ||
1121 ((inw(dev
->iobase
+ DT2821_DACSR
) & DT2821_DACSR_MASK
)
1122 != DT2821_DACSR_VAL
) ||
1123 ((inw(dev
->iobase
+ DT2821_SUPCSR
) & DT2821_SUPCSR_MASK
)
1124 != DT2821_SUPCSR_VAL
) ||
1125 ((inw(dev
->iobase
+ DT2821_TMRCTR
) & DT2821_TMRCTR_MASK
)
1126 != DT2821_TMRCTR_VAL
)) {
1127 dev_err(dev
->class_dev
, "board not found\n");
1130 /* should do board test */
1132 if (it
->options
[opt_irq
] > 0) {
1133 ret
= request_irq(it
->options
[opt_irq
], dt282x_interrupt
, 0,
1134 dev
->board_name
, dev
);
1136 dev
->irq
= it
->options
[opt_irq
];
1139 devpriv
= comedi_alloc_devpriv(dev
, sizeof(*devpriv
));
1144 ret
= dt282x_grab_dma(dev
, it
->options
[opt_dma1
],
1145 it
->options
[opt_dma2
]);
1150 ret
= comedi_alloc_subdevices(dev
, 3);
1154 s
= &dev
->subdevices
[0];
1157 s
->type
= COMEDI_SUBD_AI
;
1158 s
->subdev_flags
= SDF_READABLE
|
1159 ((it
->options
[opt_diff
]) ? SDF_DIFF
: SDF_COMMON
);
1161 (it
->options
[opt_diff
]) ? board
->adchan_di
: board
->adchan_se
;
1162 s
->insn_read
= dt282x_ai_insn_read
;
1163 s
->maxdata
= (1 << board
->adbits
) - 1;
1165 opt_ai_range_lkup(board
->ispgl
, it
->options
[opt_ai_range
]);
1166 devpriv
->ad_2scomp
= it
->options
[opt_ai_twos
];
1168 dev
->read_subdev
= s
;
1169 s
->subdev_flags
|= SDF_CMD_READ
;
1170 s
->len_chanlist
= 16;
1171 s
->do_cmdtest
= dt282x_ai_cmdtest
;
1172 s
->do_cmd
= dt282x_ai_cmd
;
1173 s
->cancel
= dt282x_ai_cancel
;
1176 s
= &dev
->subdevices
[1];
1178 s
->n_chan
= board
->dachan
;
1181 s
->type
= COMEDI_SUBD_AO
;
1182 s
->subdev_flags
= SDF_WRITABLE
;
1183 s
->insn_read
= dt282x_ao_insn_read
;
1184 s
->insn_write
= dt282x_ao_insn_write
;
1185 s
->maxdata
= (1 << board
->dabits
) - 1;
1186 s
->range_table_list
= devpriv
->darangelist
;
1187 devpriv
->darangelist
[0] =
1188 opt_ao_range_lkup(it
->options
[opt_ao0_range
]);
1189 devpriv
->darangelist
[1] =
1190 opt_ao_range_lkup(it
->options
[opt_ao1_range
]);
1191 devpriv
->da0_2scomp
= it
->options
[opt_ao0_twos
];
1192 devpriv
->da1_2scomp
= it
->options
[opt_ao1_twos
];
1194 dev
->write_subdev
= s
;
1195 s
->subdev_flags
|= SDF_CMD_WRITE
;
1196 s
->len_chanlist
= 2;
1197 s
->do_cmdtest
= dt282x_ao_cmdtest
;
1198 s
->do_cmd
= dt282x_ao_cmd
;
1199 s
->cancel
= dt282x_ao_cancel
;
1202 s
->type
= COMEDI_SUBD_UNUSED
;
1205 s
= &dev
->subdevices
[2];
1207 s
->type
= COMEDI_SUBD_DIO
;
1208 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
1210 s
->insn_bits
= dt282x_dio_insn_bits
;
1211 s
->insn_config
= dt282x_dio_insn_config
;
1213 s
->range_table
= &range_digital
;
1218 static void dt282x_detach(struct comedi_device
*dev
)
1220 struct dt282x_private
*devpriv
= dev
->private;
1223 if (devpriv
->dma
[0].chan
)
1224 free_dma(devpriv
->dma
[0].chan
);
1225 if (devpriv
->dma
[1].chan
)
1226 free_dma(devpriv
->dma
[1].chan
);
1227 if (devpriv
->dma
[0].buf
)
1228 free_page((unsigned long)devpriv
->dma
[0].buf
);
1229 if (devpriv
->dma
[1].buf
)
1230 free_page((unsigned long)devpriv
->dma
[1].buf
);
1232 comedi_legacy_detach(dev
);
1235 static const struct dt282x_board boardtypes
[] = {
1273 .name
= "dt2824-pgh",
1282 .name
= "dt2824-pgl",
1354 .name
= "dt24-ez-pgl",
1365 static struct comedi_driver dt282x_driver
= {
1366 .driver_name
= "dt282x",
1367 .module
= THIS_MODULE
,
1368 .attach
= dt282x_attach
,
1369 .detach
= dt282x_detach
,
1370 .board_name
= &boardtypes
[0].name
,
1371 .num_names
= ARRAY_SIZE(boardtypes
),
1372 .offset
= sizeof(struct dt282x_board
),
1374 module_comedi_driver(dt282x_driver
);
1376 MODULE_AUTHOR("Comedi http://www.comedi.org");
1377 MODULE_DESCRIPTION("Comedi low-level driver");
1378 MODULE_LICENSE("GPL");