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
92 /* ignore 'defined but not used' warning */
93 static const struct comedi_lrange range_dt2801_ai_pgh_bipolar
= { 4, {
107 static const struct comedi_lrange range_dt2801_ai_pgl_bipolar
= { 4, {
122 /* ignore 'defined but not used' warning */
123 static const struct comedi_lrange range_dt2801_ai_pgh_unipolar
= { 4, {
135 static const struct comedi_lrange range_dt2801_ai_pgl_unipolar
= { 4, {
147 struct dt2801_board
{
158 /* Typeid's for the different boards of the DT2801-series
159 (taken from the test-software, that comes with the board)
161 static const struct dt2801_board boardtypes
[] = {
179 .name
= "dt2801/5716a",
195 .name
= "dt2805/5716a",
228 #define boardtype (*(const struct dt2801_board *)dev->board_ptr)
230 struct dt2801_private
{
232 const struct comedi_lrange
*dac_range_types
[2];
233 unsigned int ao_readback
[2];
236 /* These are the low-level routines:
237 writecommand: write a command to the board
238 writedata: write data byte
239 readdata: read data byte
242 /* Only checks DataOutReady-flag, not the Ready-flag as it is done
243 in the examples of the manual. I don't see why this should be
245 static int dt2801_readdata(struct comedi_device
*dev
, int *data
)
248 int timeout
= DT2801_TIMEOUT
;
251 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
252 if (stat
& (DT_S_COMPOSITE_ERROR
| DT_S_READY
))
254 if (stat
& DT_S_DATA_OUT_READY
) {
255 *data
= inb_p(dev
->iobase
+ DT2801_DATA
);
258 } while (--timeout
> 0);
263 static int dt2801_readdata2(struct comedi_device
*dev
, int *data
)
268 ret
= dt2801_readdata(dev
, &lb
);
271 ret
= dt2801_readdata(dev
, &hb
);
275 *data
= (hb
<< 8) + lb
;
279 static int dt2801_writedata(struct comedi_device
*dev
, unsigned int data
)
282 int timeout
= DT2801_TIMEOUT
;
285 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
287 if (stat
& DT_S_COMPOSITE_ERROR
)
289 if (!(stat
& DT_S_DATA_IN_FULL
)) {
290 outb_p(data
& 0xff, dev
->iobase
+ DT2801_DATA
);
294 if (stat
& DT_S_READY
) {
296 ("dt2801: ready flag set (bad!) in dt2801_writedata()\n");
300 } while (--timeout
> 0);
305 static int dt2801_writedata2(struct comedi_device
*dev
, unsigned int data
)
309 ret
= dt2801_writedata(dev
, data
& 0xff);
312 ret
= dt2801_writedata(dev
, (data
>> 8));
319 static int dt2801_wait_for_ready(struct comedi_device
*dev
)
321 int timeout
= DT2801_TIMEOUT
;
324 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
325 if (stat
& DT_S_READY
)
328 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
330 if (stat
& DT_S_COMPOSITE_ERROR
)
332 if (stat
& DT_S_READY
)
334 } while (--timeout
> 0);
339 static int dt2801_writecmd(struct comedi_device
*dev
, int command
)
343 dt2801_wait_for_ready(dev
);
345 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
346 if (stat
& DT_S_COMPOSITE_ERROR
) {
348 ("dt2801: composite-error in dt2801_writecmd(), ignoring\n");
350 if (!(stat
& DT_S_READY
))
351 printk("dt2801: !ready in dt2801_writecmd(), ignoring\n");
352 outb_p(command
, dev
->iobase
+ DT2801_CMD
);
357 static int dt2801_reset(struct comedi_device
*dev
)
363 DPRINTK("dt2801: resetting board...\n");
364 DPRINTK("fingerprint: 0x%02x 0x%02x\n", inb_p(dev
->iobase
),
365 inb_p(dev
->iobase
+ 1));
367 /* pull random data from data port */
368 inb_p(dev
->iobase
+ DT2801_DATA
);
369 inb_p(dev
->iobase
+ DT2801_DATA
);
370 inb_p(dev
->iobase
+ DT2801_DATA
);
371 inb_p(dev
->iobase
+ DT2801_DATA
);
373 DPRINTK("dt2801: stop\n");
374 /* dt2801_writecmd(dev,DT_C_STOP); */
375 outb_p(DT_C_STOP
, dev
->iobase
+ DT2801_CMD
);
377 /* dt2801_wait_for_ready(dev); */
381 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
382 if (stat
& DT_S_READY
)
386 printk("dt2801: timeout 1 status=0x%02x\n", stat
);
388 /* printk("dt2801: reading dummy\n"); */
389 /* dt2801_readdata(dev,&board_code); */
391 DPRINTK("dt2801: reset\n");
392 outb_p(DT_C_RESET
, dev
->iobase
+ DT2801_CMD
);
393 /* dt2801_writecmd(dev,DT_C_RESET); */
398 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
399 if (stat
& DT_S_READY
)
403 printk("dt2801: timeout 2 status=0x%02x\n", stat
);
405 DPRINTK("dt2801: reading code\n");
406 dt2801_readdata(dev
, &board_code
);
408 DPRINTK("dt2801: ok. code=0x%02x\n", board_code
);
413 static int probe_number_of_ai_chans(struct comedi_device
*dev
)
419 for (n_chans
= 0; n_chans
< 16; n_chans
++) {
420 stat
= dt2801_writecmd(dev
, DT_C_READ_ADIM
);
421 dt2801_writedata(dev
, 0);
422 dt2801_writedata(dev
, n_chans
);
423 stat
= dt2801_readdata2(dev
, &data
);
435 static const struct comedi_lrange
*dac_range_table
[] = {
443 static const struct comedi_lrange
*dac_range_lkup(int opt
)
445 if (opt
< 0 || opt
>= 5)
446 return &range_unknown
;
447 return dac_range_table
[opt
];
450 static const struct comedi_lrange
*ai_range_lkup(int type
, int opt
)
455 &range_dt2801_ai_pgl_unipolar
:
456 &range_dt2801_ai_pgl_bipolar
;
458 return (opt
) ? &range_unipolar10
: &range_bipolar10
;
460 return &range_unipolar5
;
462 return &range_unknown
;
465 static int dt2801_error(struct comedi_device
*dev
, int stat
)
469 printk("dt2801: timeout\n");
471 printk("dt2801: error %d\n", stat
);
474 printk("dt2801: error status 0x%02x, resetting...\n", stat
);
482 static int dt2801_ai_insn_read(struct comedi_device
*dev
,
483 struct comedi_subdevice
*s
,
484 struct comedi_insn
*insn
, unsigned int *data
)
490 for (i
= 0; i
< insn
->n
; i
++) {
491 stat
= dt2801_writecmd(dev
, DT_C_READ_ADIM
);
492 dt2801_writedata(dev
, CR_RANGE(insn
->chanspec
));
493 dt2801_writedata(dev
, CR_CHAN(insn
->chanspec
));
494 stat
= dt2801_readdata2(dev
, &d
);
497 return dt2801_error(dev
, stat
);
505 static int dt2801_ao_insn_read(struct comedi_device
*dev
,
506 struct comedi_subdevice
*s
,
507 struct comedi_insn
*insn
, unsigned int *data
)
509 struct dt2801_private
*devpriv
= dev
->private;
511 data
[0] = devpriv
->ao_readback
[CR_CHAN(insn
->chanspec
)];
516 static int dt2801_ao_insn_write(struct comedi_device
*dev
,
517 struct comedi_subdevice
*s
,
518 struct comedi_insn
*insn
, unsigned int *data
)
520 struct dt2801_private
*devpriv
= dev
->private;
522 dt2801_writecmd(dev
, DT_C_WRITE_DAIM
);
523 dt2801_writedata(dev
, CR_CHAN(insn
->chanspec
));
524 dt2801_writedata2(dev
, data
[0]);
526 devpriv
->ao_readback
[CR_CHAN(insn
->chanspec
)] = data
[0];
531 static int dt2801_dio_insn_bits(struct comedi_device
*dev
,
532 struct comedi_subdevice
*s
,
533 struct comedi_insn
*insn
, unsigned int *data
)
537 if (s
== &dev
->subdevices
[3])
541 s
->state
&= ~data
[0];
542 s
->state
|= (data
[0] & data
[1]);
543 dt2801_writecmd(dev
, DT_C_WRITE_DIG
);
544 dt2801_writedata(dev
, which
);
545 dt2801_writedata(dev
, s
->state
);
547 dt2801_writecmd(dev
, DT_C_READ_DIG
);
548 dt2801_writedata(dev
, which
);
549 dt2801_readdata(dev
, data
+ 1);
554 static int dt2801_dio_insn_config(struct comedi_device
*dev
,
555 struct comedi_subdevice
*s
,
556 struct comedi_insn
*insn
, unsigned int *data
)
560 if (s
== &dev
->subdevices
[3])
565 case INSN_CONFIG_DIO_OUTPUT
:
567 dt2801_writecmd(dev
, DT_C_SET_DIGOUT
);
569 case INSN_CONFIG_DIO_INPUT
:
571 dt2801_writecmd(dev
, DT_C_SET_DIGIN
);
573 case INSN_CONFIG_DIO_QUERY
:
574 data
[1] = s
->io_bits
? COMEDI_OUTPUT
: COMEDI_INPUT
;
579 dt2801_writedata(dev
, which
);
588 [2] - a/d 0=differential, 1=single-ended
589 [3] - a/d range 0=[-10,10], 1=[0,10]
590 [4] - dac0 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
591 [5] - dac1 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
593 static int dt2801_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
595 struct dt2801_private
*devpriv
;
596 struct comedi_subdevice
*s
;
597 unsigned long iobase
;
598 int board_code
, type
;
602 iobase
= it
->options
[0];
603 if (!request_region(iobase
, DT2801_IOSIZE
, "dt2801")) {
604 comedi_error(dev
, "I/O port conflict");
607 dev
->iobase
= iobase
;
609 /* do some checking */
611 board_code
= dt2801_reset(dev
);
613 /* heh. if it didn't work, try it again. */
615 board_code
= dt2801_reset(dev
);
617 for (type
= 0; type
< ARRAY_SIZE(boardtypes
); type
++) {
618 if (boardtypes
[type
].boardcode
== board_code
)
621 printk("dt2801: unrecognized board code=0x%02x, contact author\n",
626 dev
->board_ptr
= boardtypes
+ type
;
627 printk("dt2801: %s at port 0x%lx", boardtype
.name
, iobase
);
629 n_ai_chans
= probe_number_of_ai_chans(dev
);
630 printk(" (ai channels = %d)\n", n_ai_chans
);
632 ret
= comedi_alloc_subdevices(dev
, 4);
636 devpriv
= kzalloc(sizeof(*devpriv
), GFP_KERNEL
);
639 dev
->private = devpriv
;
641 dev
->board_name
= boardtype
.name
;
643 s
= &dev
->subdevices
[0];
645 s
->type
= COMEDI_SUBD_AI
;
646 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
;
648 s
->n_chan
= n_ai_chans
;
651 s
->n_chan
= boardtype
.ad_chan
;
653 s
->n_chan
= boardtype
.ad_chan
/ 2;
655 s
->maxdata
= (1 << boardtype
.adbits
) - 1;
656 s
->range_table
= ai_range_lkup(boardtype
.adrangetype
, it
->options
[3]);
657 s
->insn_read
= dt2801_ai_insn_read
;
659 s
= &dev
->subdevices
[1];
661 s
->type
= COMEDI_SUBD_AO
;
662 s
->subdev_flags
= SDF_WRITABLE
;
664 s
->maxdata
= (1 << boardtype
.dabits
) - 1;
665 s
->range_table_list
= devpriv
->dac_range_types
;
666 devpriv
->dac_range_types
[0] = dac_range_lkup(it
->options
[4]);
667 devpriv
->dac_range_types
[1] = dac_range_lkup(it
->options
[5]);
668 s
->insn_read
= dt2801_ao_insn_read
;
669 s
->insn_write
= dt2801_ao_insn_write
;
671 s
= &dev
->subdevices
[2];
672 /* 1st digital subdevice */
673 s
->type
= COMEDI_SUBD_DIO
;
674 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
677 s
->range_table
= &range_digital
;
678 s
->insn_bits
= dt2801_dio_insn_bits
;
679 s
->insn_config
= dt2801_dio_insn_config
;
681 s
= &dev
->subdevices
[3];
682 /* 2nd digital subdevice */
683 s
->type
= COMEDI_SUBD_DIO
;
684 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
687 s
->range_table
= &range_digital
;
688 s
->insn_bits
= dt2801_dio_insn_bits
;
689 s
->insn_config
= dt2801_dio_insn_config
;
696 static void dt2801_detach(struct comedi_device
*dev
)
699 release_region(dev
->iobase
, DT2801_IOSIZE
);
702 static struct comedi_driver dt2801_driver
= {
703 .driver_name
= "dt2801",
704 .module
= THIS_MODULE
,
705 .attach
= dt2801_attach
,
706 .detach
= dt2801_detach
,
708 module_comedi_driver(dt2801_driver
);
710 MODULE_AUTHOR("Comedi http://www.comedi.org");
711 MODULE_DESCRIPTION("Comedi low-level driver");
712 MODULE_LICENSE("GPL");