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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 Description: Data Translation DT2821 series (including DT-EZ)
27 Devices: [Data Translation] DT2821 (dt2821),
28 DT2821-F-16SE (dt2821-f), DT2821-F-8DI (dt2821-f),
29 DT2821-G-16SE (dt2821-f), DT2821-G-8DI (dt2821-g),
31 DT2824-PGH (dt2824-pgh), DT2824-PGL (dt2824-pgl), DT2825 (dt2825),
32 DT2827 (dt2827), DT2828 (dt2828), DT21-EZ (dt21-ez), DT23-EZ (dt23-ez),
33 DT24-EZ (dt24-ez), DT24-EZ-PGL (dt24-ez-pgl)
35 Updated: Wed, 22 Aug 2001 17:11:34 -0700
37 Configuration options:
38 [0] - I/O port base address
42 [4] - AI jumpered for 0=single ended, 1=differential
43 [5] - AI jumpered for 0=straight binary, 1=2's complement
44 [6] - AO 0 jumpered for 0=straight binary, 1=2's complement
45 [7] - AO 1 jumpered for 0=straight binary, 1=2's complement
46 [8] - AI jumpered for 0=[-10,10]V, 1=[0,10], 2=[-5,5], 3=[0,5]
47 [9] - AO 0 jumpered for 0=[-10,10]V, 1=[0,10], 2=[-5,5], 3=[0,5],
49 [10]- A0 1 jumpered for 0=[-10,10]V, 1=[0,10], 2=[-5,5], 3=[0,5],
53 - AO commands might be broken.
54 - If you try to run a command on both the AI and AO subdevices
55 simultaneously, bad things will happen. The driver needs to
56 be fixed to check for this situation and return an error.
59 #include "../comedidev.h"
61 #include <linux/gfp.h>
62 #include <linux/ioport.h>
63 #include <linux/interrupt.h>
66 #include "comedi_fc.h"
70 #define DT2821_TIMEOUT 100 /* 500 us */
71 #define DT2821_SIZE 0x10
74 * Registers in the DT282x
77 #define DT2821_ADCSR 0x00 /* A/D Control/Status */
78 #define DT2821_CHANCSR 0x02 /* Channel Control/Status */
79 #define DT2821_ADDAT 0x04 /* A/D data */
80 #define DT2821_DACSR 0x06 /* D/A Control/Status */
81 #define DT2821_DADAT 0x08 /* D/A data */
82 #define DT2821_DIODAT 0x0a /* digital data */
83 #define DT2821_SUPCSR 0x0c /* Supervisor Control/Status */
84 #define DT2821_TMRCTR 0x0e /* Timer/Counter */
87 * At power up, some registers are in a well-known state. The
88 * masks and values are as follows:
91 #define DT2821_ADCSR_MASK 0xfff0
92 #define DT2821_ADCSR_VAL 0x7c00
94 #define DT2821_CHANCSR_MASK 0xf0f0
95 #define DT2821_CHANCSR_VAL 0x70f0
97 #define DT2821_DACSR_MASK 0x7c93
98 #define DT2821_DACSR_VAL 0x7c90
100 #define DT2821_SUPCSR_MASK 0xf8ff
101 #define DT2821_SUPCSR_VAL 0x0000
103 #define DT2821_TMRCTR_MASK 0xff00
104 #define DT2821_TMRCTR_VAL 0xf000
107 * Bit fields of each register
112 #define DT2821_ADERR 0x8000 /* (R) 1 for A/D error */
113 #define DT2821_ADCLK 0x0200 /* (R/W) A/D clock enable */
114 /* 0x7c00 read as 1's */
115 #define DT2821_MUXBUSY 0x0100 /* (R) multiplexer busy */
116 #define DT2821_ADDONE 0x0080 /* (R) A/D done */
117 #define DT2821_IADDONE 0x0040 /* (R/W) interrupt on A/D done */
118 /* 0x0030 gain select */
119 /* 0x000f channel select */
123 #define DT2821_LLE 0x8000 /* (R/W) Load List Enable */
124 /* 0x7000 read as 1's */
125 /* 0x0f00 (R) present address */
126 /* 0x00f0 read as 1's */
127 /* 0x000f (R) number of entries - 1 */
131 #define DT2821_DAERR 0x8000 /* (R) D/A error */
132 #define DT2821_YSEL 0x0200 /* (R/W) DAC 1 select */
133 #define DT2821_SSEL 0x0100 /* (R/W) single channel select */
134 #define DT2821_DACRDY 0x0080 /* (R) DAC ready */
135 #define DT2821_IDARDY 0x0040 /* (R/W) interrupt on DAC ready */
136 #define DT2821_DACLK 0x0020 /* (R/W) D/A clock enable */
137 #define DT2821_HBOE 0x0002 /* (R/W) DIO high byte output enable */
138 #define DT2821_LBOE 0x0001 /* (R/W) DIO low byte output enable */
142 #define DT2821_DMAD 0x8000 /* (R) DMA done */
143 #define DT2821_ERRINTEN 0x4000 /* (R/W) interrupt on error */
144 #define DT2821_CLRDMADNE 0x2000 /* (W) clear DMA done */
145 #define DT2821_DDMA 0x1000 /* (R/W) dual DMA */
146 #define DT2821_DS1 0x0800 /* (R/W) DMA select 1 */
147 #define DT2821_DS0 0x0400 /* (R/W) DMA select 0 */
148 #define DT2821_BUFFB 0x0200 /* (R/W) buffer B selected */
149 #define DT2821_SCDN 0x0100 /* (R) scan done */
150 #define DT2821_DACON 0x0080 /* (W) DAC single conversion */
151 #define DT2821_ADCINIT 0x0040 /* (W) A/D initialize */
152 #define DT2821_DACINIT 0x0020 /* (W) D/A initialize */
153 #define DT2821_PRLD 0x0010 /* (W) preload multiplexer */
154 #define DT2821_STRIG 0x0008 /* (W) software trigger */
155 #define DT2821_XTRIG 0x0004 /* (R/W) external trigger enable */
156 #define DT2821_XCLK 0x0002 /* (R/W) external clock enable */
157 #define DT2821_BDINIT 0x0001 /* (W) initialize board */
159 static const struct comedi_lrange range_dt282x_ai_lo_bipolar
= {
168 static const struct comedi_lrange range_dt282x_ai_lo_unipolar
= {
177 static const struct comedi_lrange range_dt282x_ai_5_bipolar
= {
186 static const struct comedi_lrange range_dt282x_ai_5_unipolar
= {
195 static const struct comedi_lrange range_dt282x_ai_hi_bipolar
= {
204 static const struct comedi_lrange range_dt282x_ai_hi_unipolar
= {
213 struct dt282x_board
{
224 struct dt282x_private
{
225 int ad_2scomp
; /* we have 2's comp jumper set */
226 int da0_2scomp
; /* same, for DAC0 */
227 int da1_2scomp
; /* same, for DAC1 */
229 const struct comedi_lrange
*darangelist
[2];
233 volatile int dacsr
; /* software copies of registers */
242 short *buf
; /* DMA buffer */
243 volatile int size
; /* size of current transfer */
245 int dma_maxsize
; /* max size of DMA transfer (in bytes) */
246 int usedma
; /* driver uses DMA */
247 volatile int current_dma_index
;
251 #define boardtype (*(const struct dt282x_board *)dev->board_ptr)
254 * Some useless abstractions
256 #define chan_to_DAC(a) ((a)&1)
257 #define mux_busy() (inw(dev->iobase+DT2821_ADCSR)&DT2821_MUXBUSY)
258 #define ad_done() (inw(dev->iobase+DT2821_ADCSR)&DT2821_ADDONE)
261 * danger! macro abuse... a is the expression to wait on, and b is
262 * the statement(s) to execute if it doesn't happen.
264 #define wait_for(a, b) \
267 for (_i = 0; _i < DT2821_TIMEOUT; _i++) { \
278 static int prep_ai_dma(struct comedi_device
*dev
, int chan
, int size
);
279 static int prep_ao_dma(struct comedi_device
*dev
, int chan
, int size
);
280 static int dt282x_ai_cancel(struct comedi_device
*dev
,
281 struct comedi_subdevice
*s
);
282 static int dt282x_ao_cancel(struct comedi_device
*dev
,
283 struct comedi_subdevice
*s
);
284 static int dt282x_ns_to_timer(int *nanosec
, int round_mode
);
285 static void dt282x_disable_dma(struct comedi_device
*dev
);
287 static int dt282x_grab_dma(struct comedi_device
*dev
, int dma1
, int dma2
);
289 static void dt282x_munge(struct comedi_device
*dev
, short *buf
,
292 struct dt282x_private
*devpriv
= dev
->private;
294 unsigned short mask
= (1 << boardtype
.adbits
) - 1;
295 unsigned short sign
= 1 << (boardtype
.adbits
- 1);
298 if (devpriv
->ad_2scomp
)
299 sign
= 1 << (boardtype
.adbits
- 1);
304 comedi_error(dev
, "bug! odd number of bytes from dma xfer");
306 for (i
= 0; i
< n
; i
++)
307 buf
[i
] = (buf
[i
] & mask
) ^ sign
;
310 static void dt282x_ao_dma_interrupt(struct comedi_device
*dev
)
312 struct dt282x_private
*devpriv
= dev
->private;
316 struct comedi_subdevice
*s
= &dev
->subdevices
[1];
318 outw(devpriv
->supcsr
| DT2821_CLRDMADNE
, dev
->iobase
+ DT2821_SUPCSR
);
320 if (!s
->async
->prealloc_buf
) {
321 printk(KERN_ERR
"async->data disappeared. dang!\n");
325 i
= devpriv
->current_dma_index
;
326 ptr
= devpriv
->dma
[i
].buf
;
328 disable_dma(devpriv
->dma
[i
].chan
);
330 devpriv
->current_dma_index
= 1 - i
;
332 size
= cfc_read_array_from_buffer(s
, ptr
, devpriv
->dma_maxsize
);
334 printk(KERN_ERR
"dt282x: AO underrun\n");
335 dt282x_ao_cancel(dev
, s
);
336 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
339 prep_ao_dma(dev
, i
, size
);
343 static void dt282x_ai_dma_interrupt(struct comedi_device
*dev
)
345 struct dt282x_private
*devpriv
= dev
->private;
350 struct comedi_subdevice
*s
= &dev
->subdevices
[0];
352 outw(devpriv
->supcsr
| DT2821_CLRDMADNE
, dev
->iobase
+ DT2821_SUPCSR
);
354 if (!s
->async
->prealloc_buf
) {
355 printk(KERN_ERR
"async->data disappeared. dang!\n");
359 i
= devpriv
->current_dma_index
;
360 ptr
= devpriv
->dma
[i
].buf
;
361 size
= devpriv
->dma
[i
].size
;
363 disable_dma(devpriv
->dma
[i
].chan
);
365 devpriv
->current_dma_index
= 1 - i
;
367 dt282x_munge(dev
, ptr
, size
);
368 ret
= cfc_write_array_to_buffer(s
, ptr
, size
);
370 dt282x_ai_cancel(dev
, s
);
373 devpriv
->nread
-= size
/ 2;
375 if (devpriv
->nread
< 0) {
376 printk(KERN_INFO
"dt282x: off by one\n");
379 if (!devpriv
->nread
) {
380 dt282x_ai_cancel(dev
, s
);
381 s
->async
->events
|= COMEDI_CB_EOA
;
385 /* clear the dual dma flag, making this the last dma segment */
386 /* XXX probably wrong */
387 if (!devpriv
->ntrig
) {
388 devpriv
->supcsr
&= ~(DT2821_DDMA
);
389 outw(devpriv
->supcsr
, dev
->iobase
+ DT2821_SUPCSR
);
392 /* restart the channel */
393 prep_ai_dma(dev
, i
, 0);
396 static int prep_ai_dma(struct comedi_device
*dev
, int dma_index
, int n
)
398 struct dt282x_private
*devpriv
= dev
->private;
400 unsigned long dma_ptr
;
407 n
= devpriv
->dma_maxsize
;
408 if (n
> devpriv
->ntrig
* 2)
409 n
= devpriv
->ntrig
* 2;
410 devpriv
->ntrig
-= n
/ 2;
412 devpriv
->dma
[dma_index
].size
= n
;
413 dma_chan
= devpriv
->dma
[dma_index
].chan
;
414 dma_ptr
= virt_to_bus(devpriv
->dma
[dma_index
].buf
);
416 set_dma_mode(dma_chan
, DMA_MODE_READ
);
417 flags
= claim_dma_lock();
418 clear_dma_ff(dma_chan
);
419 set_dma_addr(dma_chan
, dma_ptr
);
420 set_dma_count(dma_chan
, n
);
421 release_dma_lock(flags
);
423 enable_dma(dma_chan
);
428 static int prep_ao_dma(struct comedi_device
*dev
, int dma_index
, int n
)
430 struct dt282x_private
*devpriv
= dev
->private;
432 unsigned long dma_ptr
;
435 devpriv
->dma
[dma_index
].size
= n
;
436 dma_chan
= devpriv
->dma
[dma_index
].chan
;
437 dma_ptr
= virt_to_bus(devpriv
->dma
[dma_index
].buf
);
439 set_dma_mode(dma_chan
, DMA_MODE_WRITE
);
440 flags
= claim_dma_lock();
441 clear_dma_ff(dma_chan
);
442 set_dma_addr(dma_chan
, dma_ptr
);
443 set_dma_count(dma_chan
, n
);
444 release_dma_lock(flags
);
446 enable_dma(dma_chan
);
451 static irqreturn_t
dt282x_interrupt(int irq
, void *d
)
453 struct comedi_device
*dev
= d
;
454 struct dt282x_private
*devpriv
= dev
->private;
455 struct comedi_subdevice
*s
;
456 struct comedi_subdevice
*s_ao
;
457 unsigned int supcsr
, adcsr
, dacsr
;
460 if (!dev
->attached
) {
461 comedi_error(dev
, "spurious interrupt");
465 s
= &dev
->subdevices
[0];
466 s_ao
= &dev
->subdevices
[1];
467 adcsr
= inw(dev
->iobase
+ DT2821_ADCSR
);
468 dacsr
= inw(dev
->iobase
+ DT2821_DACSR
);
469 supcsr
= inw(dev
->iobase
+ DT2821_SUPCSR
);
470 if (supcsr
& DT2821_DMAD
) {
471 if (devpriv
->dma_dir
== DMA_MODE_READ
)
472 dt282x_ai_dma_interrupt(dev
);
474 dt282x_ao_dma_interrupt(dev
);
477 if (adcsr
& DT2821_ADERR
) {
478 if (devpriv
->nread
!= 0) {
479 comedi_error(dev
, "A/D error");
480 dt282x_ai_cancel(dev
, s
);
481 s
->async
->events
|= COMEDI_CB_ERROR
;
485 if (dacsr
& DT2821_DAERR
) {
489 disable_irq(dev
->irq
);
490 printk(KERN_INFO
"disabling irq\n");
493 comedi_error(dev
, "D/A error");
494 dt282x_ao_cancel(dev
, s_ao
);
495 s
->async
->events
|= COMEDI_CB_ERROR
;
499 if (adcsr
& DT2821_ADDONE
) {
503 data
= (short)inw(dev
->iobase
+ DT2821_ADDAT
);
504 data
&= (1 << boardtype
.adbits
) - 1;
506 if (devpriv
->ad_2scomp
)
507 data
^= 1 << (boardtype
.adbits
- 1);
508 ret
= comedi_buf_put(s
->async
, data
);
511 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
514 if (!devpriv
->nread
) {
515 s
->async
->events
|= COMEDI_CB_EOA
;
517 if (supcsr
& DT2821_SCDN
)
518 outw(devpriv
->supcsr
| DT2821_STRIG
,
519 dev
->iobase
+ DT2821_SUPCSR
);
524 comedi_event(dev
, s
);
525 /* printk("adcsr=0x%02x dacsr-0x%02x supcsr=0x%02x\n",
526 adcsr, dacsr, supcsr); */
527 return IRQ_RETVAL(handled
);
530 static void dt282x_load_changain(struct comedi_device
*dev
, int n
,
531 unsigned int *chanlist
)
533 struct dt282x_private
*devpriv
= dev
->private;
535 unsigned int chan
, range
;
537 outw(DT2821_LLE
| (n
- 1), dev
->iobase
+ DT2821_CHANCSR
);
538 for (i
= 0; i
< n
; i
++) {
539 chan
= CR_CHAN(chanlist
[i
]);
540 range
= CR_RANGE(chanlist
[i
]);
541 outw(devpriv
->adcsr
| (range
<< 4) | chan
,
542 dev
->iobase
+ DT2821_ADCSR
);
544 outw(n
- 1, dev
->iobase
+ DT2821_CHANCSR
);
548 * Performs a single A/D conversion.
549 * - Put channel/gain into channel-gain list
550 * - preload multiplexer
551 * - trigger conversion and wait for it to finish
553 static int dt282x_ai_insn_read(struct comedi_device
*dev
,
554 struct comedi_subdevice
*s
,
555 struct comedi_insn
*insn
, unsigned int *data
)
557 struct dt282x_private
*devpriv
= dev
->private;
560 /* XXX should we really be enabling the ad clock here? */
561 devpriv
->adcsr
= DT2821_ADCLK
;
562 outw(devpriv
->adcsr
, dev
->iobase
+ DT2821_ADCSR
);
564 dt282x_load_changain(dev
, 1, &insn
->chanspec
);
566 outw(devpriv
->supcsr
| DT2821_PRLD
, dev
->iobase
+ DT2821_SUPCSR
);
567 wait_for(!mux_busy(), comedi_error(dev
, "timeout\n"); return -ETIME
;);
569 for (i
= 0; i
< insn
->n
; i
++) {
570 outw(devpriv
->supcsr
| DT2821_STRIG
,
571 dev
->iobase
+ DT2821_SUPCSR
);
572 wait_for(ad_done(), comedi_error(dev
, "timeout\n");
577 DT2821_ADDAT
) & ((1 << boardtype
.adbits
) - 1);
578 if (devpriv
->ad_2scomp
)
579 data
[i
] ^= (1 << (boardtype
.adbits
- 1));
585 static int dt282x_ai_cmdtest(struct comedi_device
*dev
,
586 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
)
588 const struct dt282x_board
*board
= comedi_board(dev
);
592 /* Step 1 : check if triggers are trivially valid */
594 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_NOW
);
595 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
,
596 TRIG_FOLLOW
| TRIG_EXT
);
597 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_TIMER
);
598 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
599 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
604 /* Step 2a : make sure trigger sources are unique */
606 err
|= cfc_check_trigger_is_unique(cmd
->scan_begin_src
);
607 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
609 /* Step 2b : and mutually compatible */
614 /* step 3: make sure arguments are trivially compatible */
616 if (cmd
->start_arg
!= 0) {
620 if (cmd
->scan_begin_src
== TRIG_FOLLOW
) {
621 /* internal trigger */
622 if (cmd
->scan_begin_arg
!= 0) {
623 cmd
->scan_begin_arg
= 0;
627 /* external trigger */
628 /* should be level/edge, hi/lo specification here */
629 if (cmd
->scan_begin_arg
!= 0) {
630 cmd
->scan_begin_arg
= 0;
634 if (cmd
->convert_arg
< 4000) {
635 /* XXX board dependent */
636 cmd
->convert_arg
= 4000;
639 #define SLOWEST_TIMER (250*(1<<15)*255)
640 if (cmd
->convert_arg
> SLOWEST_TIMER
) {
641 cmd
->convert_arg
= SLOWEST_TIMER
;
644 if (cmd
->convert_arg
< board
->ai_speed
) {
645 cmd
->convert_arg
= board
->ai_speed
;
648 if (cmd
->scan_end_arg
!= cmd
->chanlist_len
) {
649 cmd
->scan_end_arg
= cmd
->chanlist_len
;
652 if (cmd
->stop_src
== TRIG_COUNT
) {
653 /* any count is allowed */
656 if (cmd
->stop_arg
!= 0) {
665 /* step 4: fix up any arguments */
667 tmp
= cmd
->convert_arg
;
668 dt282x_ns_to_timer(&cmd
->convert_arg
, cmd
->flags
& TRIG_ROUND_MASK
);
669 if (tmp
!= cmd
->convert_arg
)
678 static int dt282x_ai_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
680 const struct dt282x_board
*board
= comedi_board(dev
);
681 struct dt282x_private
*devpriv
= dev
->private;
682 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
685 if (devpriv
->usedma
== 0) {
687 "driver requires 2 dma channels"
688 " to execute command");
692 dt282x_disable_dma(dev
);
694 if (cmd
->convert_arg
< board
->ai_speed
)
695 cmd
->convert_arg
= board
->ai_speed
;
696 timer
= dt282x_ns_to_timer(&cmd
->convert_arg
, TRIG_ROUND_NEAREST
);
697 outw(timer
, dev
->iobase
+ DT2821_TMRCTR
);
699 if (cmd
->scan_begin_src
== TRIG_FOLLOW
) {
700 /* internal trigger */
701 devpriv
->supcsr
= DT2821_ERRINTEN
| DT2821_DS0
;
703 /* external trigger */
704 devpriv
->supcsr
= DT2821_ERRINTEN
| DT2821_DS0
| DT2821_DS1
;
706 outw(devpriv
->supcsr
| DT2821_CLRDMADNE
| DT2821_BUFFB
| DT2821_ADCINIT
,
707 dev
->iobase
+ DT2821_SUPCSR
);
709 devpriv
->ntrig
= cmd
->stop_arg
* cmd
->scan_end_arg
;
710 devpriv
->nread
= devpriv
->ntrig
;
712 devpriv
->dma_dir
= DMA_MODE_READ
;
713 devpriv
->current_dma_index
= 0;
714 prep_ai_dma(dev
, 0, 0);
715 if (devpriv
->ntrig
) {
716 prep_ai_dma(dev
, 1, 0);
717 devpriv
->supcsr
|= DT2821_DDMA
;
718 outw(devpriv
->supcsr
, dev
->iobase
+ DT2821_SUPCSR
);
723 dt282x_load_changain(dev
, cmd
->chanlist_len
, cmd
->chanlist
);
725 devpriv
->adcsr
= DT2821_ADCLK
| DT2821_IADDONE
;
726 outw(devpriv
->adcsr
, dev
->iobase
+ DT2821_ADCSR
);
728 outw(devpriv
->supcsr
| DT2821_PRLD
, dev
->iobase
+ DT2821_SUPCSR
);
729 wait_for(!mux_busy(), comedi_error(dev
, "timeout\n"); return -ETIME
;);
731 if (cmd
->scan_begin_src
== TRIG_FOLLOW
) {
732 outw(devpriv
->supcsr
| DT2821_STRIG
,
733 dev
->iobase
+ DT2821_SUPCSR
);
735 devpriv
->supcsr
|= DT2821_XTRIG
;
736 outw(devpriv
->supcsr
, dev
->iobase
+ DT2821_SUPCSR
);
742 static void dt282x_disable_dma(struct comedi_device
*dev
)
744 struct dt282x_private
*devpriv
= dev
->private;
746 if (devpriv
->usedma
) {
747 disable_dma(devpriv
->dma
[0].chan
);
748 disable_dma(devpriv
->dma
[1].chan
);
752 static int dt282x_ai_cancel(struct comedi_device
*dev
,
753 struct comedi_subdevice
*s
)
755 struct dt282x_private
*devpriv
= dev
->private;
757 dt282x_disable_dma(dev
);
760 outw(devpriv
->adcsr
, dev
->iobase
+ DT2821_ADCSR
);
763 outw(devpriv
->supcsr
| DT2821_ADCINIT
, dev
->iobase
+ DT2821_SUPCSR
);
768 static int dt282x_ns_to_timer(int *nanosec
, int round_mode
)
770 int prescale
, base
, divider
;
772 for (prescale
= 0; prescale
< 16; prescale
++) {
775 base
= 250 * (1 << prescale
);
776 switch (round_mode
) {
777 case TRIG_ROUND_NEAREST
:
779 divider
= (*nanosec
+ base
/ 2) / base
;
781 case TRIG_ROUND_DOWN
:
782 divider
= (*nanosec
) / base
;
785 divider
= (*nanosec
+ base
- 1) / base
;
789 *nanosec
= divider
* base
;
790 return (prescale
<< 8) | (255 - divider
);
793 base
= 250 * (1 << 15);
795 *nanosec
= divider
* base
;
796 return (15 << 8) | (255 - divider
);
800 * Analog output routine. Selects single channel conversion,
801 * selects correct channel, converts from 2's compliment to
802 * offset binary if necessary, loads the data into the DAC
803 * data register, and performs the conversion.
805 static int dt282x_ao_insn_read(struct comedi_device
*dev
,
806 struct comedi_subdevice
*s
,
807 struct comedi_insn
*insn
, unsigned int *data
)
809 struct dt282x_private
*devpriv
= dev
->private;
811 data
[0] = devpriv
->ao
[CR_CHAN(insn
->chanspec
)];
816 static int dt282x_ao_insn_write(struct comedi_device
*dev
,
817 struct comedi_subdevice
*s
,
818 struct comedi_insn
*insn
, unsigned int *data
)
820 struct dt282x_private
*devpriv
= dev
->private;
824 chan
= CR_CHAN(insn
->chanspec
);
826 d
&= (1 << boardtype
.dabits
) - 1;
827 devpriv
->ao
[chan
] = d
;
829 devpriv
->dacsr
|= DT2821_SSEL
;
833 devpriv
->dacsr
|= DT2821_YSEL
;
834 if (devpriv
->da0_2scomp
)
835 d
^= (1 << (boardtype
.dabits
- 1));
837 devpriv
->dacsr
&= ~DT2821_YSEL
;
838 if (devpriv
->da1_2scomp
)
839 d
^= (1 << (boardtype
.dabits
- 1));
842 outw(devpriv
->dacsr
, dev
->iobase
+ DT2821_DACSR
);
844 outw(d
, dev
->iobase
+ DT2821_DADAT
);
846 outw(devpriv
->supcsr
| DT2821_DACON
, dev
->iobase
+ DT2821_SUPCSR
);
851 static int dt282x_ao_cmdtest(struct comedi_device
*dev
,
852 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
)
857 /* Step 1 : check if triggers are trivially valid */
859 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_INT
);
860 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
, TRIG_TIMER
);
861 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
862 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
863 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
868 /* Step 2a : make sure trigger sources are unique */
870 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
872 /* Step 2b : and mutually compatible */
877 /* step 3: make sure arguments are trivially compatible */
879 if (cmd
->start_arg
!= 0) {
883 if (cmd
->scan_begin_arg
< 5000 /* XXX unknown */) {
884 cmd
->scan_begin_arg
= 5000;
887 if (cmd
->convert_arg
!= 0) {
888 cmd
->convert_arg
= 0;
891 if (cmd
->scan_end_arg
> 2) {
892 /* XXX chanlist stuff? */
893 cmd
->scan_end_arg
= 2;
896 if (cmd
->stop_src
== TRIG_COUNT
) {
897 /* any count is allowed */
900 if (cmd
->stop_arg
!= 0) {
909 /* step 4: fix up any arguments */
911 tmp
= cmd
->scan_begin_arg
;
912 dt282x_ns_to_timer(&cmd
->scan_begin_arg
, cmd
->flags
& TRIG_ROUND_MASK
);
913 if (tmp
!= cmd
->scan_begin_arg
)
923 static int dt282x_ao_inttrig(struct comedi_device
*dev
,
924 struct comedi_subdevice
*s
, unsigned int x
)
926 struct dt282x_private
*devpriv
= dev
->private;
932 size
= cfc_read_array_from_buffer(s
, devpriv
->dma
[0].buf
,
933 devpriv
->dma_maxsize
);
935 printk(KERN_ERR
"dt282x: AO underrun\n");
938 prep_ao_dma(dev
, 0, size
);
940 size
= cfc_read_array_from_buffer(s
, devpriv
->dma
[1].buf
,
941 devpriv
->dma_maxsize
);
943 printk(KERN_ERR
"dt282x: AO underrun\n");
946 prep_ao_dma(dev
, 1, size
);
948 outw(devpriv
->supcsr
| DT2821_STRIG
, dev
->iobase
+ DT2821_SUPCSR
);
949 s
->async
->inttrig
= NULL
;
954 static int dt282x_ao_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
956 struct dt282x_private
*devpriv
= dev
->private;
958 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
960 if (devpriv
->usedma
== 0) {
962 "driver requires 2 dma channels"
963 " to execute command");
967 dt282x_disable_dma(dev
);
969 devpriv
->supcsr
= DT2821_ERRINTEN
| DT2821_DS1
| DT2821_DDMA
;
970 outw(devpriv
->supcsr
| DT2821_CLRDMADNE
| DT2821_BUFFB
| DT2821_DACINIT
,
971 dev
->iobase
+ DT2821_SUPCSR
);
973 devpriv
->ntrig
= cmd
->stop_arg
* cmd
->chanlist_len
;
974 devpriv
->nread
= devpriv
->ntrig
;
976 devpriv
->dma_dir
= DMA_MODE_WRITE
;
977 devpriv
->current_dma_index
= 0;
979 timer
= dt282x_ns_to_timer(&cmd
->scan_begin_arg
, TRIG_ROUND_NEAREST
);
980 outw(timer
, dev
->iobase
+ DT2821_TMRCTR
);
982 devpriv
->dacsr
= DT2821_SSEL
| DT2821_DACLK
| DT2821_IDARDY
;
983 outw(devpriv
->dacsr
, dev
->iobase
+ DT2821_DACSR
);
985 s
->async
->inttrig
= dt282x_ao_inttrig
;
990 static int dt282x_ao_cancel(struct comedi_device
*dev
,
991 struct comedi_subdevice
*s
)
993 struct dt282x_private
*devpriv
= dev
->private;
995 dt282x_disable_dma(dev
);
998 outw(devpriv
->dacsr
, dev
->iobase
+ DT2821_DACSR
);
1000 devpriv
->supcsr
= 0;
1001 outw(devpriv
->supcsr
| DT2821_DACINIT
, dev
->iobase
+ DT2821_SUPCSR
);
1006 static int dt282x_dio_insn_bits(struct comedi_device
*dev
,
1007 struct comedi_subdevice
*s
,
1008 struct comedi_insn
*insn
, unsigned int *data
)
1011 s
->state
&= ~data
[0];
1012 s
->state
|= (data
[0] & data
[1]);
1014 outw(s
->state
, dev
->iobase
+ DT2821_DIODAT
);
1016 data
[1] = inw(dev
->iobase
+ DT2821_DIODAT
);
1021 static int dt282x_dio_insn_config(struct comedi_device
*dev
,
1022 struct comedi_subdevice
*s
,
1023 struct comedi_insn
*insn
, unsigned int *data
)
1025 struct dt282x_private
*devpriv
= dev
->private;
1028 mask
= (CR_CHAN(insn
->chanspec
) < 8) ? 0x00ff : 0xff00;
1032 s
->io_bits
&= ~mask
;
1034 if (s
->io_bits
& 0x00ff)
1035 devpriv
->dacsr
|= DT2821_LBOE
;
1037 devpriv
->dacsr
&= ~DT2821_LBOE
;
1038 if (s
->io_bits
& 0xff00)
1039 devpriv
->dacsr
|= DT2821_HBOE
;
1041 devpriv
->dacsr
&= ~DT2821_HBOE
;
1043 outw(devpriv
->dacsr
, dev
->iobase
+ DT2821_DACSR
);
1048 static const struct comedi_lrange
*const ai_range_table
[] = {
1049 &range_dt282x_ai_lo_bipolar
,
1050 &range_dt282x_ai_lo_unipolar
,
1051 &range_dt282x_ai_5_bipolar
,
1052 &range_dt282x_ai_5_unipolar
1055 static const struct comedi_lrange
*const ai_range_pgl_table
[] = {
1056 &range_dt282x_ai_hi_bipolar
,
1057 &range_dt282x_ai_hi_unipolar
1060 static const struct comedi_lrange
*opt_ai_range_lkup(int ispgl
, int x
)
1063 if (x
< 0 || x
>= 2)
1065 return ai_range_pgl_table
[x
];
1067 if (x
< 0 || x
>= 4)
1069 return ai_range_table
[x
];
1073 static const struct comedi_lrange
*const ao_range_table
[] = {
1081 static const struct comedi_lrange
*opt_ao_range_lkup(int x
)
1083 if (x
< 0 || x
>= 5)
1085 return ao_range_table
[x
];
1088 enum { /* i/o base, irq, dma channels */
1089 opt_iobase
= 0, opt_irq
, opt_dma1
, opt_dma2
,
1090 opt_diff
, /* differential */
1091 opt_ai_twos
, opt_ao0_twos
, opt_ao1_twos
, /* twos comp */
1092 opt_ai_range
, opt_ao0_range
, opt_ao1_range
, /* range */
1095 static int dt282x_grab_dma(struct comedi_device
*dev
, int dma1
, int dma2
)
1097 struct dt282x_private
*devpriv
= dev
->private;
1100 devpriv
->usedma
= 0;
1102 if (!dma1
&& !dma2
) {
1103 printk(KERN_ERR
" (no dma)");
1107 if (dma1
== dma2
|| dma1
< 5 || dma2
< 5 || dma1
> 7 || dma2
> 7)
1117 ret
= request_dma(dma1
, "dt282x A");
1120 devpriv
->dma
[0].chan
= dma1
;
1122 ret
= request_dma(dma2
, "dt282x B");
1125 devpriv
->dma
[1].chan
= dma2
;
1127 devpriv
->dma_maxsize
= PAGE_SIZE
;
1128 devpriv
->dma
[0].buf
= (void *)__get_free_page(GFP_KERNEL
| GFP_DMA
);
1129 devpriv
->dma
[1].buf
= (void *)__get_free_page(GFP_KERNEL
| GFP_DMA
);
1130 if (!devpriv
->dma
[0].buf
|| !devpriv
->dma
[1].buf
) {
1131 printk(KERN_ERR
" can't get DMA memory");
1135 printk(KERN_INFO
" (dma=%d,%d)", dma1
, dma2
);
1137 devpriv
->usedma
= 1;
1148 4 0=single ended, 1=differential
1149 5 ai 0=straight binary, 1=2's comp
1150 6 ao0 0=straight binary, 1=2's comp
1151 7 ao1 0=straight binary, 1=2's comp
1152 8 ai 0=±10 V, 1=0-10 V, 2=±5 V, 3=0-5 V
1153 9 ao0 0=±10 V, 1=0-10 V, 2=±5 V, 3=0-5 V, 4=±2.5 V
1154 10 ao1 0=±10 V, 1=0-10 V, 2=±5 V, 3=0-5 V, 4=±2.5 V
1156 static int dt282x_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
1158 const struct dt282x_board
*board
= comedi_board(dev
);
1159 struct dt282x_private
*devpriv
;
1162 struct comedi_subdevice
*s
;
1163 unsigned long iobase
;
1165 dev
->board_name
= board
->name
;
1167 iobase
= it
->options
[opt_iobase
];
1171 printk(KERN_INFO
"comedi%d: dt282x: 0x%04lx", dev
->minor
, iobase
);
1172 if (!request_region(iobase
, DT2821_SIZE
, "dt282x")) {
1173 printk(KERN_INFO
" I/O port conflict\n");
1176 dev
->iobase
= iobase
;
1178 outw(DT2821_BDINIT
, dev
->iobase
+ DT2821_SUPCSR
);
1179 i
= inw(dev
->iobase
+ DT2821_ADCSR
);
1181 printk(KERN_DEBUG
" fingerprint=%x,%x,%x,%x,%x",
1182 inw(dev
->iobase
+ DT2821_ADCSR
),
1183 inw(dev
->iobase
+ DT2821_CHANCSR
),
1184 inw(dev
->iobase
+ DT2821_DACSR
),
1185 inw(dev
->iobase
+ DT2821_SUPCSR
),
1186 inw(dev
->iobase
+ DT2821_TMRCTR
));
1189 if (((inw(dev
->iobase
+ DT2821_ADCSR
) & DT2821_ADCSR_MASK
)
1190 != DT2821_ADCSR_VAL
) ||
1191 ((inw(dev
->iobase
+ DT2821_CHANCSR
) & DT2821_CHANCSR_MASK
)
1192 != DT2821_CHANCSR_VAL
) ||
1193 ((inw(dev
->iobase
+ DT2821_DACSR
) & DT2821_DACSR_MASK
)
1194 != DT2821_DACSR_VAL
) ||
1195 ((inw(dev
->iobase
+ DT2821_SUPCSR
) & DT2821_SUPCSR_MASK
)
1196 != DT2821_SUPCSR_VAL
) ||
1197 ((inw(dev
->iobase
+ DT2821_TMRCTR
) & DT2821_TMRCTR_MASK
)
1198 != DT2821_TMRCTR_VAL
)) {
1199 printk(KERN_ERR
" board not found");
1202 /* should do board test */
1204 irq
= it
->options
[opt_irq
];
1207 unsigned long flags
;
1212 irqs
= probe_irq_on();
1214 /* trigger interrupt */
1218 irq
= probe_irq_off(irqs
);
1219 restore_flags(flags
);
1221 printk(KERN_ERR
" error probing irq (bad)");
1225 printk(KERN_INFO
" ( irq = %d )", irq
);
1226 ret
= request_irq(irq
, dt282x_interrupt
, 0, "dt282x", dev
);
1228 printk(KERN_ERR
" failed to get irq\n");
1232 } else if (irq
== 0) {
1233 printk(KERN_INFO
" (no irq)");
1236 printk(KERN_INFO
" (probe returned multiple irqs--bad)");
1238 printk(KERN_INFO
" (irq probe not implemented)");
1242 devpriv
= kzalloc(sizeof(*devpriv
), GFP_KERNEL
);
1245 dev
->private = devpriv
;
1247 ret
= dt282x_grab_dma(dev
, it
->options
[opt_dma1
],
1248 it
->options
[opt_dma2
]);
1252 ret
= comedi_alloc_subdevices(dev
, 3);
1256 s
= &dev
->subdevices
[0];
1258 dev
->read_subdev
= s
;
1260 s
->type
= COMEDI_SUBD_AI
;
1261 s
->subdev_flags
= SDF_READABLE
| SDF_CMD_READ
|
1262 ((it
->options
[opt_diff
]) ? SDF_DIFF
: SDF_COMMON
);
1264 (it
->options
[opt_diff
]) ? boardtype
.adchan_di
: boardtype
.adchan_se
;
1265 s
->insn_read
= dt282x_ai_insn_read
;
1266 s
->do_cmdtest
= dt282x_ai_cmdtest
;
1267 s
->do_cmd
= dt282x_ai_cmd
;
1268 s
->cancel
= dt282x_ai_cancel
;
1269 s
->maxdata
= (1 << boardtype
.adbits
) - 1;
1270 s
->len_chanlist
= 16;
1272 opt_ai_range_lkup(boardtype
.ispgl
, it
->options
[opt_ai_range
]);
1273 devpriv
->ad_2scomp
= it
->options
[opt_ai_twos
];
1275 s
= &dev
->subdevices
[1];
1277 s
->n_chan
= boardtype
.dachan
;
1280 s
->type
= COMEDI_SUBD_AO
;
1281 dev
->write_subdev
= s
;
1282 s
->subdev_flags
= SDF_WRITABLE
| SDF_CMD_WRITE
;
1283 s
->insn_read
= dt282x_ao_insn_read
;
1284 s
->insn_write
= dt282x_ao_insn_write
;
1285 s
->do_cmdtest
= dt282x_ao_cmdtest
;
1286 s
->do_cmd
= dt282x_ao_cmd
;
1287 s
->cancel
= dt282x_ao_cancel
;
1288 s
->maxdata
= (1 << boardtype
.dabits
) - 1;
1289 s
->len_chanlist
= 2;
1290 s
->range_table_list
= devpriv
->darangelist
;
1291 devpriv
->darangelist
[0] =
1292 opt_ao_range_lkup(it
->options
[opt_ao0_range
]);
1293 devpriv
->darangelist
[1] =
1294 opt_ao_range_lkup(it
->options
[opt_ao1_range
]);
1295 devpriv
->da0_2scomp
= it
->options
[opt_ao0_twos
];
1296 devpriv
->da1_2scomp
= it
->options
[opt_ao1_twos
];
1298 s
->type
= COMEDI_SUBD_UNUSED
;
1301 s
= &dev
->subdevices
[2];
1303 s
->type
= COMEDI_SUBD_DIO
;
1304 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
1306 s
->insn_bits
= dt282x_dio_insn_bits
;
1307 s
->insn_config
= dt282x_dio_insn_config
;
1309 s
->range_table
= &range_digital
;
1311 printk(KERN_INFO
"\n");
1316 static void dt282x_detach(struct comedi_device
*dev
)
1318 struct dt282x_private
*devpriv
= dev
->private;
1321 free_irq(dev
->irq
, dev
);
1323 release_region(dev
->iobase
, DT2821_SIZE
);
1325 if (devpriv
->dma
[0].chan
)
1326 free_dma(devpriv
->dma
[0].chan
);
1327 if (devpriv
->dma
[1].chan
)
1328 free_dma(devpriv
->dma
[1].chan
);
1329 if (devpriv
->dma
[0].buf
)
1330 free_page((unsigned long)devpriv
->dma
[0].buf
);
1331 if (devpriv
->dma
[1].buf
)
1332 free_page((unsigned long)devpriv
->dma
[1].buf
);
1336 static const struct dt282x_board boardtypes
[] = {
1374 .name
= "dt2824-pgh",
1383 .name
= "dt2824-pgl",
1455 .name
= "dt24-ez-pgl",
1466 static struct comedi_driver dt282x_driver
= {
1467 .driver_name
= "dt282x",
1468 .module
= THIS_MODULE
,
1469 .attach
= dt282x_attach
,
1470 .detach
= dt282x_detach
,
1471 .board_name
= &boardtypes
[0].name
,
1472 .num_names
= ARRAY_SIZE(boardtypes
),
1473 .offset
= sizeof(struct dt282x_board
),
1475 module_comedi_driver(dt282x_driver
);
1477 MODULE_AUTHOR("Comedi http://www.comedi.org");
1478 MODULE_DESCRIPTION("Comedi low-level driver");
1479 MODULE_LICENSE("GPL");