2 * comedi/drivers/dt2801.c
3 * Device Driver for DataTranslation DT2801
8 Description: Data Translation DT2801 series and DT01-EZ
11 Devices: [Data Translation] DT2801 (dt2801), DT2801-A, DT2801/5716A,
12 DT2805, DT2805/5716A, DT2808, DT2818, DT2809, DT01-EZ
14 This driver can autoprobe the type of board.
16 Configuration options:
17 [0] - I/O port base address
19 [2] - A/D reference 0=differential, 1=single-ended
29 [5] - D/A 1 range (same choices)
32 #include "../comedidev.h"
33 #include <linux/delay.h>
34 #include <linux/ioport.h>
36 #define DT2801_TIMEOUT 1000
38 /* Hardware Configuration */
39 /* ====================== */
41 #define DT2801_MAX_DMA_SIZE (64 * 1024)
44 #define DT2801_IOSIZE 2
47 /* ====================== */
50 #define DT_C_RESET 0x0
51 #define DT_C_CLEAR_ERR 0x1
52 #define DT_C_READ_ERRREG 0x2
53 #define DT_C_SET_CLOCK 0x3
58 #define DT_C_SET_DIGIN 0x4
59 #define DT_C_SET_DIGOUT 0x5
60 #define DT_C_READ_DIG 0x6
61 #define DT_C_WRITE_DIG 0x7
63 #define DT_C_WRITE_DAIM 0x8
64 #define DT_C_SET_DA 0x9
65 #define DT_C_WRITE_DA 0xa
67 #define DT_C_READ_ADIM 0xc
68 #define DT_C_SET_AD 0xd
69 #define DT_C_READ_AD 0xe
71 /* Command modifiers (only used with read/write), EXTTRIG can be
72 used with some other commands.
74 #define DT_MOD_DMA (1<<4)
75 #define DT_MOD_CONT (1<<5)
76 #define DT_MOD_EXTCLK (1<<6)
77 #define DT_MOD_EXTTRIG (1<<7)
79 /* Bits in status register */
80 #define DT_S_DATA_OUT_READY (1<<0)
81 #define DT_S_DATA_IN_FULL (1<<1)
82 #define DT_S_READY (1<<2)
83 #define DT_S_COMMAND (1<<3)
84 #define DT_S_COMPOSITE_ERROR (1<<7)
88 #define DT2801_STATUS 1
91 static int dt2801_attach(struct comedi_device
*dev
,
92 struct comedi_devconfig
*it
);
93 static int dt2801_detach(struct comedi_device
*dev
);
94 static struct comedi_driver driver_dt2801
= {
95 .driver_name
= "dt2801",
96 .module
= THIS_MODULE
,
97 .attach
= dt2801_attach
,
98 .detach
= dt2801_detach
,
101 static int __init
driver_dt2801_init_module(void)
103 return comedi_driver_register(&driver_dt2801
);
106 static void __exit
driver_dt2801_cleanup_module(void)
108 comedi_driver_unregister(&driver_dt2801
);
111 module_init(driver_dt2801_init_module
);
112 module_exit(driver_dt2801_cleanup_module
);
115 /* ignore 'defined but not used' warning */
116 static const struct comedi_lrange range_dt2801_ai_pgh_bipolar
= { 4, {
130 static const struct comedi_lrange range_dt2801_ai_pgl_bipolar
= { 4, {
145 /* ignore 'defined but not used' warning */
146 static const struct comedi_lrange range_dt2801_ai_pgh_unipolar
= { 4, {
158 static const struct comedi_lrange range_dt2801_ai_pgl_unipolar
= { 4, {
170 struct dt2801_board
{
181 /* Typeid's for the different boards of the DT2801-series
182 (taken from the test-software, that comes with the board)
184 static const struct dt2801_board boardtypes
[] = {
202 .name
= "dt2801/5716a",
218 .name
= "dt2805/5716a",
251 #define boardtype (*(const struct dt2801_board *)dev->board_ptr)
253 struct dt2801_private
{
255 const struct comedi_lrange
*dac_range_types
[2];
256 unsigned int ao_readback
[2];
259 #define devpriv ((struct dt2801_private *)dev->private)
261 static int dt2801_ai_insn_read(struct comedi_device
*dev
,
262 struct comedi_subdevice
*s
,
263 struct comedi_insn
*insn
, unsigned int *data
);
264 static int dt2801_ao_insn_read(struct comedi_device
*dev
,
265 struct comedi_subdevice
*s
,
266 struct comedi_insn
*insn
, unsigned int *data
);
267 static int dt2801_ao_insn_write(struct comedi_device
*dev
,
268 struct comedi_subdevice
*s
,
269 struct comedi_insn
*insn
, unsigned int *data
);
270 static int dt2801_dio_insn_bits(struct comedi_device
*dev
,
271 struct comedi_subdevice
*s
,
272 struct comedi_insn
*insn
, unsigned int *data
);
273 static int dt2801_dio_insn_config(struct comedi_device
*dev
,
274 struct comedi_subdevice
*s
,
275 struct comedi_insn
*insn
, unsigned int *data
);
277 /* These are the low-level routines:
278 writecommand: write a command to the board
279 writedata: write data byte
280 readdata: read data byte
283 /* Only checks DataOutReady-flag, not the Ready-flag as it is done
284 in the examples of the manual. I don't see why this should be
286 static int dt2801_readdata(struct comedi_device
*dev
, int *data
)
289 int timeout
= DT2801_TIMEOUT
;
292 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
293 if (stat
& (DT_S_COMPOSITE_ERROR
| DT_S_READY
))
295 if (stat
& DT_S_DATA_OUT_READY
) {
296 *data
= inb_p(dev
->iobase
+ DT2801_DATA
);
299 } while (--timeout
> 0);
304 static int dt2801_readdata2(struct comedi_device
*dev
, int *data
)
309 ret
= dt2801_readdata(dev
, &lb
);
312 ret
= dt2801_readdata(dev
, &hb
);
316 *data
= (hb
<< 8) + lb
;
320 static int dt2801_writedata(struct comedi_device
*dev
, unsigned int data
)
323 int timeout
= DT2801_TIMEOUT
;
326 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
328 if (stat
& DT_S_COMPOSITE_ERROR
)
330 if (!(stat
& DT_S_DATA_IN_FULL
)) {
331 outb_p(data
& 0xff, dev
->iobase
+ DT2801_DATA
);
335 if (stat
& DT_S_READY
) {
337 ("dt2801: ready flag set (bad!) in dt2801_writedata()\n");
341 } while (--timeout
> 0);
346 static int dt2801_writedata2(struct comedi_device
*dev
, unsigned int data
)
350 ret
= dt2801_writedata(dev
, data
& 0xff);
353 ret
= dt2801_writedata(dev
, (data
>> 8));
360 static int dt2801_wait_for_ready(struct comedi_device
*dev
)
362 int timeout
= DT2801_TIMEOUT
;
365 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
366 if (stat
& DT_S_READY
)
369 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
371 if (stat
& DT_S_COMPOSITE_ERROR
)
373 if (stat
& DT_S_READY
)
375 } while (--timeout
> 0);
380 static int dt2801_writecmd(struct comedi_device
*dev
, int command
)
384 dt2801_wait_for_ready(dev
);
386 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
387 if (stat
& DT_S_COMPOSITE_ERROR
) {
389 ("dt2801: composite-error in dt2801_writecmd(), ignoring\n");
391 if (!(stat
& DT_S_READY
))
392 printk("dt2801: !ready in dt2801_writecmd(), ignoring\n");
393 outb_p(command
, dev
->iobase
+ DT2801_CMD
);
398 static int dt2801_reset(struct comedi_device
*dev
)
404 DPRINTK("dt2801: resetting board...\n");
405 DPRINTK("fingerprint: 0x%02x 0x%02x\n", inb_p(dev
->iobase
),
406 inb_p(dev
->iobase
+ 1));
408 /* pull random data from data port */
409 inb_p(dev
->iobase
+ DT2801_DATA
);
410 inb_p(dev
->iobase
+ DT2801_DATA
);
411 inb_p(dev
->iobase
+ DT2801_DATA
);
412 inb_p(dev
->iobase
+ DT2801_DATA
);
414 DPRINTK("dt2801: stop\n");
415 /* dt2801_writecmd(dev,DT_C_STOP); */
416 outb_p(DT_C_STOP
, dev
->iobase
+ DT2801_CMD
);
418 /* dt2801_wait_for_ready(dev); */
422 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
423 if (stat
& DT_S_READY
)
427 printk("dt2801: timeout 1 status=0x%02x\n", stat
);
429 /* printk("dt2801: reading dummy\n"); */
430 /* dt2801_readdata(dev,&board_code); */
432 DPRINTK("dt2801: reset\n");
433 outb_p(DT_C_RESET
, dev
->iobase
+ DT2801_CMD
);
434 /* dt2801_writecmd(dev,DT_C_RESET); */
439 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
440 if (stat
& DT_S_READY
)
444 printk("dt2801: timeout 2 status=0x%02x\n", stat
);
446 DPRINTK("dt2801: reading code\n");
447 dt2801_readdata(dev
, &board_code
);
449 DPRINTK("dt2801: ok. code=0x%02x\n", board_code
);
454 static int probe_number_of_ai_chans(struct comedi_device
*dev
)
460 for (n_chans
= 0; n_chans
< 16; n_chans
++) {
461 stat
= dt2801_writecmd(dev
, DT_C_READ_ADIM
);
462 dt2801_writedata(dev
, 0);
463 dt2801_writedata(dev
, n_chans
);
464 stat
= dt2801_readdata2(dev
, &data
);
476 static const struct comedi_lrange
*dac_range_table
[] = {
484 static const struct comedi_lrange
*dac_range_lkup(int opt
)
486 if (opt
< 0 || opt
>= 5)
487 return &range_unknown
;
488 return dac_range_table
[opt
];
491 static const struct comedi_lrange
*ai_range_lkup(int type
, int opt
)
496 &range_dt2801_ai_pgl_unipolar
:
497 &range_dt2801_ai_pgl_bipolar
;
499 return (opt
) ? &range_unipolar10
: &range_bipolar10
;
501 return &range_unipolar5
;
503 return &range_unknown
;
510 [2] - a/d 0=differential, 1=single-ended
511 [3] - a/d range 0=[-10,10], 1=[0,10]
512 [4] - dac0 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
513 [5] - dac1 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
515 static int dt2801_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
517 struct comedi_subdevice
*s
;
518 unsigned long iobase
;
519 int board_code
, type
;
523 iobase
= it
->options
[0];
524 if (!request_region(iobase
, DT2801_IOSIZE
, "dt2801")) {
525 comedi_error(dev
, "I/O port conflict");
528 dev
->iobase
= iobase
;
530 /* do some checking */
532 board_code
= dt2801_reset(dev
);
534 /* heh. if it didn't work, try it again. */
536 board_code
= dt2801_reset(dev
);
538 for (type
= 0; type
< ARRAY_SIZE(boardtypes
); type
++) {
539 if (boardtypes
[type
].boardcode
== board_code
)
542 printk("dt2801: unrecognized board code=0x%02x, contact author\n",
547 dev
->board_ptr
= boardtypes
+ type
;
548 printk("dt2801: %s at port 0x%lx", boardtype
.name
, iobase
);
550 n_ai_chans
= probe_number_of_ai_chans(dev
);
551 printk(" (ai channels = %d)", n_ai_chans
);
553 ret
= alloc_subdevices(dev
, 4);
557 ret
= alloc_private(dev
, sizeof(struct dt2801_private
));
561 dev
->board_name
= boardtype
.name
;
563 s
= dev
->subdevices
+ 0;
565 s
->type
= COMEDI_SUBD_AI
;
566 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
;
568 s
->n_chan
= n_ai_chans
;
571 s
->n_chan
= boardtype
.ad_chan
;
573 s
->n_chan
= boardtype
.ad_chan
/ 2;
575 s
->maxdata
= (1 << boardtype
.adbits
) - 1;
576 s
->range_table
= ai_range_lkup(boardtype
.adrangetype
, it
->options
[3]);
577 s
->insn_read
= dt2801_ai_insn_read
;
581 s
->type
= COMEDI_SUBD_AO
;
582 s
->subdev_flags
= SDF_WRITABLE
;
584 s
->maxdata
= (1 << boardtype
.dabits
) - 1;
585 s
->range_table_list
= devpriv
->dac_range_types
;
586 devpriv
->dac_range_types
[0] = dac_range_lkup(it
->options
[4]);
587 devpriv
->dac_range_types
[1] = dac_range_lkup(it
->options
[5]);
588 s
->insn_read
= dt2801_ao_insn_read
;
589 s
->insn_write
= dt2801_ao_insn_write
;
592 /* 1st digital subdevice */
593 s
->type
= COMEDI_SUBD_DIO
;
594 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
597 s
->range_table
= &range_digital
;
598 s
->insn_bits
= dt2801_dio_insn_bits
;
599 s
->insn_config
= dt2801_dio_insn_config
;
602 /* 2nd digital subdevice */
603 s
->type
= COMEDI_SUBD_DIO
;
604 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
607 s
->range_table
= &range_digital
;
608 s
->insn_bits
= dt2801_dio_insn_bits
;
609 s
->insn_config
= dt2801_dio_insn_config
;
618 static int dt2801_detach(struct comedi_device
*dev
)
621 release_region(dev
->iobase
, DT2801_IOSIZE
);
626 static int dt2801_error(struct comedi_device
*dev
, int stat
)
630 printk("dt2801: timeout\n");
632 printk("dt2801: error %d\n", stat
);
635 printk("dt2801: error status 0x%02x, resetting...\n", stat
);
643 static int dt2801_ai_insn_read(struct comedi_device
*dev
,
644 struct comedi_subdevice
*s
,
645 struct comedi_insn
*insn
, unsigned int *data
)
651 for (i
= 0; i
< insn
->n
; i
++) {
652 stat
= dt2801_writecmd(dev
, DT_C_READ_ADIM
);
653 dt2801_writedata(dev
, CR_RANGE(insn
->chanspec
));
654 dt2801_writedata(dev
, CR_CHAN(insn
->chanspec
));
655 stat
= dt2801_readdata2(dev
, &d
);
658 return dt2801_error(dev
, stat
);
666 static int dt2801_ao_insn_read(struct comedi_device
*dev
,
667 struct comedi_subdevice
*s
,
668 struct comedi_insn
*insn
, unsigned int *data
)
670 data
[0] = devpriv
->ao_readback
[CR_CHAN(insn
->chanspec
)];
675 static int dt2801_ao_insn_write(struct comedi_device
*dev
,
676 struct comedi_subdevice
*s
,
677 struct comedi_insn
*insn
, unsigned int *data
)
679 dt2801_writecmd(dev
, DT_C_WRITE_DAIM
);
680 dt2801_writedata(dev
, CR_CHAN(insn
->chanspec
));
681 dt2801_writedata2(dev
, data
[0]);
683 devpriv
->ao_readback
[CR_CHAN(insn
->chanspec
)] = data
[0];
688 static int dt2801_dio_insn_bits(struct comedi_device
*dev
,
689 struct comedi_subdevice
*s
,
690 struct comedi_insn
*insn
, unsigned int *data
)
694 if (s
== dev
->subdevices
+ 4)
700 s
->state
&= ~data
[0];
701 s
->state
|= (data
[0] & data
[1]);
702 dt2801_writecmd(dev
, DT_C_WRITE_DIG
);
703 dt2801_writedata(dev
, which
);
704 dt2801_writedata(dev
, s
->state
);
706 dt2801_writecmd(dev
, DT_C_READ_DIG
);
707 dt2801_writedata(dev
, which
);
708 dt2801_readdata(dev
, data
+ 1);
713 static int dt2801_dio_insn_config(struct comedi_device
*dev
,
714 struct comedi_subdevice
*s
,
715 struct comedi_insn
*insn
, unsigned int *data
)
719 if (s
== dev
->subdevices
+ 4)
725 dt2801_writecmd(dev
, DT_C_SET_DIGOUT
);
728 dt2801_writecmd(dev
, DT_C_SET_DIGIN
);
730 dt2801_writedata(dev
, which
);
735 MODULE_AUTHOR("Comedi http://www.comedi.org");
736 MODULE_DESCRIPTION("Comedi low-level driver");
737 MODULE_LICENSE("GPL");