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 <linux/module.h>
33 #include "../comedidev.h"
34 #include <linux/delay.h>
36 #define DT2801_TIMEOUT 1000
38 /* Hardware Configuration */
39 /* ====================== */
41 #define DT2801_MAX_DMA_SIZE (64 * 1024)
44 /* ====================== */
47 #define DT_C_RESET 0x0
48 #define DT_C_CLEAR_ERR 0x1
49 #define DT_C_READ_ERRREG 0x2
50 #define DT_C_SET_CLOCK 0x3
55 #define DT_C_SET_DIGIN 0x4
56 #define DT_C_SET_DIGOUT 0x5
57 #define DT_C_READ_DIG 0x6
58 #define DT_C_WRITE_DIG 0x7
60 #define DT_C_WRITE_DAIM 0x8
61 #define DT_C_SET_DA 0x9
62 #define DT_C_WRITE_DA 0xa
64 #define DT_C_READ_ADIM 0xc
65 #define DT_C_SET_AD 0xd
66 #define DT_C_READ_AD 0xe
68 /* Command modifiers (only used with read/write), EXTTRIG can be
69 used with some other commands.
71 #define DT_MOD_DMA (1<<4)
72 #define DT_MOD_CONT (1<<5)
73 #define DT_MOD_EXTCLK (1<<6)
74 #define DT_MOD_EXTTRIG (1<<7)
76 /* Bits in status register */
77 #define DT_S_DATA_OUT_READY (1<<0)
78 #define DT_S_DATA_IN_FULL (1<<1)
79 #define DT_S_READY (1<<2)
80 #define DT_S_COMMAND (1<<3)
81 #define DT_S_COMPOSITE_ERROR (1<<7)
85 #define DT2801_STATUS 1
89 /* ignore 'defined but not used' warning */
90 static const struct comedi_lrange range_dt2801_ai_pgh_bipolar
= {
99 static const struct comedi_lrange range_dt2801_ai_pgl_bipolar
= {
109 /* ignore 'defined but not used' warning */
110 static const struct comedi_lrange range_dt2801_ai_pgh_unipolar
= {
119 static const struct comedi_lrange range_dt2801_ai_pgl_unipolar
= {
128 struct dt2801_board
{
139 /* Typeid's for the different boards of the DT2801-series
140 (taken from the test-software, that comes with the board)
142 static const struct dt2801_board boardtypes
[] = {
160 .name
= "dt2801/5716a",
176 .name
= "dt2805/5716a",
209 struct dt2801_private
{
210 const struct comedi_lrange
*dac_range_types
[2];
213 /* These are the low-level routines:
214 writecommand: write a command to the board
215 writedata: write data byte
216 readdata: read data byte
219 /* Only checks DataOutReady-flag, not the Ready-flag as it is done
220 in the examples of the manual. I don't see why this should be
222 static int dt2801_readdata(struct comedi_device
*dev
, int *data
)
225 int timeout
= DT2801_TIMEOUT
;
228 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
229 if (stat
& (DT_S_COMPOSITE_ERROR
| DT_S_READY
))
231 if (stat
& DT_S_DATA_OUT_READY
) {
232 *data
= inb_p(dev
->iobase
+ DT2801_DATA
);
235 } while (--timeout
> 0);
240 static int dt2801_readdata2(struct comedi_device
*dev
, int *data
)
246 ret
= dt2801_readdata(dev
, &lb
);
249 ret
= dt2801_readdata(dev
, &hb
);
253 *data
= (hb
<< 8) + lb
;
257 static int dt2801_writedata(struct comedi_device
*dev
, unsigned int data
)
260 int timeout
= DT2801_TIMEOUT
;
263 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
265 if (stat
& DT_S_COMPOSITE_ERROR
)
267 if (!(stat
& DT_S_DATA_IN_FULL
)) {
268 outb_p(data
& 0xff, dev
->iobase
+ DT2801_DATA
);
271 } while (--timeout
> 0);
276 static int dt2801_writedata2(struct comedi_device
*dev
, unsigned int data
)
280 ret
= dt2801_writedata(dev
, data
& 0xff);
283 ret
= dt2801_writedata(dev
, (data
>> 8));
290 static int dt2801_wait_for_ready(struct comedi_device
*dev
)
292 int timeout
= DT2801_TIMEOUT
;
295 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
296 if (stat
& DT_S_READY
)
299 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
301 if (stat
& DT_S_COMPOSITE_ERROR
)
303 if (stat
& DT_S_READY
)
305 } while (--timeout
> 0);
310 static void dt2801_writecmd(struct comedi_device
*dev
, int command
)
314 dt2801_wait_for_ready(dev
);
316 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
317 if (stat
& DT_S_COMPOSITE_ERROR
) {
318 dev_dbg(dev
->class_dev
,
319 "composite-error in %s, ignoring\n", __func__
);
321 if (!(stat
& DT_S_READY
))
322 dev_dbg(dev
->class_dev
, "!ready in %s, ignoring\n", __func__
);
323 outb_p(command
, dev
->iobase
+ DT2801_CMD
);
326 static int dt2801_reset(struct comedi_device
*dev
)
332 /* pull random data from data port */
333 inb_p(dev
->iobase
+ DT2801_DATA
);
334 inb_p(dev
->iobase
+ DT2801_DATA
);
335 inb_p(dev
->iobase
+ DT2801_DATA
);
336 inb_p(dev
->iobase
+ DT2801_DATA
);
338 /* dt2801_writecmd(dev,DT_C_STOP); */
339 outb_p(DT_C_STOP
, dev
->iobase
+ DT2801_CMD
);
341 /* dt2801_wait_for_ready(dev); */
345 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
346 if (stat
& DT_S_READY
)
350 dev_dbg(dev
->class_dev
, "timeout 1 status=0x%02x\n", stat
);
352 /* dt2801_readdata(dev,&board_code); */
354 outb_p(DT_C_RESET
, dev
->iobase
+ DT2801_CMD
);
355 /* dt2801_writecmd(dev,DT_C_RESET); */
360 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
361 if (stat
& DT_S_READY
)
365 dev_dbg(dev
->class_dev
, "timeout 2 status=0x%02x\n", stat
);
367 dt2801_readdata(dev
, &board_code
);
372 static int probe_number_of_ai_chans(struct comedi_device
*dev
)
378 for (n_chans
= 0; n_chans
< 16; n_chans
++) {
379 dt2801_writecmd(dev
, DT_C_READ_ADIM
);
380 dt2801_writedata(dev
, 0);
381 dt2801_writedata(dev
, n_chans
);
382 stat
= dt2801_readdata2(dev
, &data
);
394 static const struct comedi_lrange
*dac_range_table
[] = {
402 static const struct comedi_lrange
*dac_range_lkup(int opt
)
404 if (opt
< 0 || opt
>= 5)
405 return &range_unknown
;
406 return dac_range_table
[opt
];
409 static const struct comedi_lrange
*ai_range_lkup(int type
, int opt
)
414 &range_dt2801_ai_pgl_unipolar
:
415 &range_dt2801_ai_pgl_bipolar
;
417 return (opt
) ? &range_unipolar10
: &range_bipolar10
;
419 return &range_unipolar5
;
421 return &range_unknown
;
424 static int dt2801_error(struct comedi_device
*dev
, int stat
)
428 dev_dbg(dev
->class_dev
, "timeout\n");
430 dev_dbg(dev
->class_dev
, "error %d\n", stat
);
433 dev_dbg(dev
->class_dev
, "error status 0x%02x, resetting...\n", stat
);
441 static int dt2801_ai_insn_read(struct comedi_device
*dev
,
442 struct comedi_subdevice
*s
,
443 struct comedi_insn
*insn
, unsigned int *data
)
449 for (i
= 0; i
< insn
->n
; i
++) {
450 dt2801_writecmd(dev
, DT_C_READ_ADIM
);
451 dt2801_writedata(dev
, CR_RANGE(insn
->chanspec
));
452 dt2801_writedata(dev
, CR_CHAN(insn
->chanspec
));
453 stat
= dt2801_readdata2(dev
, &d
);
456 return dt2801_error(dev
, stat
);
464 static int dt2801_ao_insn_write(struct comedi_device
*dev
,
465 struct comedi_subdevice
*s
,
466 struct comedi_insn
*insn
,
469 unsigned int chan
= CR_CHAN(insn
->chanspec
);
471 dt2801_writecmd(dev
, DT_C_WRITE_DAIM
);
472 dt2801_writedata(dev
, chan
);
473 dt2801_writedata2(dev
, data
[0]);
475 s
->readback
[chan
] = data
[0];
480 static int dt2801_dio_insn_bits(struct comedi_device
*dev
,
481 struct comedi_subdevice
*s
,
482 struct comedi_insn
*insn
,
485 int which
= (s
== &dev
->subdevices
[3]) ? 1 : 0;
486 unsigned int val
= 0;
488 if (comedi_dio_update_state(s
, data
)) {
489 dt2801_writecmd(dev
, DT_C_WRITE_DIG
);
490 dt2801_writedata(dev
, which
);
491 dt2801_writedata(dev
, s
->state
);
494 dt2801_writecmd(dev
, DT_C_READ_DIG
);
495 dt2801_writedata(dev
, which
);
496 dt2801_readdata(dev
, &val
);
503 static int dt2801_dio_insn_config(struct comedi_device
*dev
,
504 struct comedi_subdevice
*s
,
505 struct comedi_insn
*insn
,
510 ret
= comedi_dio_insn_config(dev
, s
, insn
, data
, 0xff);
514 dt2801_writecmd(dev
, s
->io_bits
? DT_C_SET_DIGOUT
: DT_C_SET_DIGIN
);
515 dt2801_writedata(dev
, (s
== &dev
->subdevices
[3]) ? 1 : 0);
524 [2] - a/d 0=differential, 1=single-ended
525 [3] - a/d range 0=[-10,10], 1=[0,10]
526 [4] - dac0 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
527 [5] - dac1 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
529 static int dt2801_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
531 const struct dt2801_board
*board
;
532 struct dt2801_private
*devpriv
;
533 struct comedi_subdevice
*s
;
534 int board_code
, type
;
538 ret
= comedi_request_region(dev
, it
->options
[0], 0x2);
542 /* do some checking */
544 board_code
= dt2801_reset(dev
);
546 /* heh. if it didn't work, try it again. */
548 board_code
= dt2801_reset(dev
);
550 for (type
= 0; type
< ARRAY_SIZE(boardtypes
); type
++) {
551 if (boardtypes
[type
].boardcode
== board_code
)
554 dev_dbg(dev
->class_dev
,
555 "unrecognized board code=0x%02x, contact author\n", board_code
);
559 dev
->board_ptr
= boardtypes
+ type
;
560 board
= dev
->board_ptr
;
562 n_ai_chans
= probe_number_of_ai_chans(dev
);
564 ret
= comedi_alloc_subdevices(dev
, 4);
568 devpriv
= comedi_alloc_devpriv(dev
, sizeof(*devpriv
));
572 dev
->board_name
= board
->name
;
574 s
= &dev
->subdevices
[0];
576 s
->type
= COMEDI_SUBD_AI
;
577 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
;
579 s
->n_chan
= n_ai_chans
;
582 s
->n_chan
= board
->ad_chan
;
584 s
->n_chan
= board
->ad_chan
/ 2;
586 s
->maxdata
= (1 << board
->adbits
) - 1;
587 s
->range_table
= ai_range_lkup(board
->adrangetype
, it
->options
[3]);
588 s
->insn_read
= dt2801_ai_insn_read
;
590 s
= &dev
->subdevices
[1];
592 s
->type
= COMEDI_SUBD_AO
;
593 s
->subdev_flags
= SDF_WRITABLE
;
595 s
->maxdata
= (1 << board
->dabits
) - 1;
596 s
->range_table_list
= devpriv
->dac_range_types
;
597 devpriv
->dac_range_types
[0] = dac_range_lkup(it
->options
[4]);
598 devpriv
->dac_range_types
[1] = dac_range_lkup(it
->options
[5]);
599 s
->insn_write
= dt2801_ao_insn_write
;
600 s
->insn_read
= comedi_readback_insn_read
;
602 ret
= comedi_alloc_subdev_readback(s
);
606 s
= &dev
->subdevices
[2];
607 /* 1st digital subdevice */
608 s
->type
= COMEDI_SUBD_DIO
;
609 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
612 s
->range_table
= &range_digital
;
613 s
->insn_bits
= dt2801_dio_insn_bits
;
614 s
->insn_config
= dt2801_dio_insn_config
;
616 s
= &dev
->subdevices
[3];
617 /* 2nd digital subdevice */
618 s
->type
= COMEDI_SUBD_DIO
;
619 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
622 s
->range_table
= &range_digital
;
623 s
->insn_bits
= dt2801_dio_insn_bits
;
624 s
->insn_config
= dt2801_dio_insn_config
;
631 static struct comedi_driver dt2801_driver
= {
632 .driver_name
= "dt2801",
633 .module
= THIS_MODULE
,
634 .attach
= dt2801_attach
,
635 .detach
= comedi_legacy_detach
,
637 module_comedi_driver(dt2801_driver
);
639 MODULE_AUTHOR("Comedi http://www.comedi.org");
640 MODULE_DESCRIPTION("Comedi low-level driver");
641 MODULE_LICENSE("GPL");