1 // SPDX-License-Identifier: GPL-2.0
3 * comedi/drivers/dt2801.c
4 * Device Driver for DataTranslation DT2801
9 * Description: Data Translation DT2801 series and DT01-EZ
12 * Devices: [Data Translation] DT2801 (dt2801), DT2801-A, DT2801/5716A,
13 * DT2805, DT2805/5716A, DT2808, DT2818, DT2809, DT01-EZ
15 * This driver can autoprobe the type of board.
17 * Configuration options:
18 * [0] - I/O port base address
20 * [2] - A/D reference 0=differential, 1=single-ended
30 * [5] - D/A 1 range (same choices)
33 #include <linux/module.h>
34 #include <linux/comedi/comedidev.h>
35 #include <linux/delay.h>
37 #define DT2801_TIMEOUT 1000
39 /* Hardware Configuration */
40 /* ====================== */
42 #define DT2801_MAX_DMA_SIZE (64 * 1024)
45 /* ====================== */
48 #define DT_C_RESET 0x0
49 #define DT_C_CLEAR_ERR 0x1
50 #define DT_C_READ_ERRREG 0x2
51 #define DT_C_SET_CLOCK 0x3
56 #define DT_C_SET_DIGIN 0x4
57 #define DT_C_SET_DIGOUT 0x5
58 #define DT_C_READ_DIG 0x6
59 #define DT_C_WRITE_DIG 0x7
61 #define DT_C_WRITE_DAIM 0x8
62 #define DT_C_SET_DA 0x9
63 #define DT_C_WRITE_DA 0xa
65 #define DT_C_READ_ADIM 0xc
66 #define DT_C_SET_AD 0xd
67 #define DT_C_READ_AD 0xe
70 * Command modifiers (only used with read/write), EXTTRIG can be
71 * used with some other commands.
73 #define DT_MOD_DMA BIT(4)
74 #define DT_MOD_CONT BIT(5)
75 #define DT_MOD_EXTCLK BIT(6)
76 #define DT_MOD_EXTTRIG BIT(7)
78 /* Bits in status register */
79 #define DT_S_DATA_OUT_READY BIT(0)
80 #define DT_S_DATA_IN_FULL BIT(1)
81 #define DT_S_READY BIT(2)
82 #define DT_S_COMMAND BIT(3)
83 #define DT_S_COMPOSITE_ERROR BIT(7)
87 #define DT2801_STATUS 1
91 /* ignore 'defined but not used' warning */
92 static const struct comedi_lrange range_dt2801_ai_pgh_bipolar
= {
101 static const struct comedi_lrange range_dt2801_ai_pgl_bipolar
= {
111 /* ignore 'defined but not used' warning */
112 static const struct comedi_lrange range_dt2801_ai_pgh_unipolar
= {
121 static const struct comedi_lrange range_dt2801_ai_pgl_unipolar
= {
130 struct dt2801_board
{
141 * Typeid's for the different boards of the DT2801-series
142 * (taken from the test-software, that comes with the board)
144 static const struct dt2801_board boardtypes
[] = {
162 .name
= "dt2801/5716a",
178 .name
= "dt2805/5716a",
211 struct dt2801_private
{
212 const struct comedi_lrange
*dac_range_types
[2];
216 * These are the low-level routines:
217 * writecommand: write a command to the board
218 * writedata: write data byte
219 * readdata: read data byte
223 * Only checks DataOutReady-flag, not the Ready-flag as it is done
224 * in the examples of the manual. I don't see why this should be
227 static int dt2801_readdata(struct comedi_device
*dev
, int *data
)
230 int timeout
= DT2801_TIMEOUT
;
233 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
234 if (stat
& (DT_S_COMPOSITE_ERROR
| DT_S_READY
))
236 if (stat
& DT_S_DATA_OUT_READY
) {
237 *data
= inb_p(dev
->iobase
+ DT2801_DATA
);
240 } while (--timeout
> 0);
245 static int dt2801_readdata2(struct comedi_device
*dev
, int *data
)
251 ret
= dt2801_readdata(dev
, &lb
);
254 ret
= dt2801_readdata(dev
, &hb
);
258 *data
= (hb
<< 8) + lb
;
262 static int dt2801_writedata(struct comedi_device
*dev
, unsigned int data
)
265 int timeout
= DT2801_TIMEOUT
;
268 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
270 if (stat
& DT_S_COMPOSITE_ERROR
)
272 if (!(stat
& DT_S_DATA_IN_FULL
)) {
273 outb_p(data
& 0xff, dev
->iobase
+ DT2801_DATA
);
276 } while (--timeout
> 0);
281 static int dt2801_writedata2(struct comedi_device
*dev
, unsigned int data
)
285 ret
= dt2801_writedata(dev
, data
& 0xff);
288 ret
= dt2801_writedata(dev
, data
>> 8);
295 static int dt2801_wait_for_ready(struct comedi_device
*dev
)
297 int timeout
= DT2801_TIMEOUT
;
300 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
301 if (stat
& DT_S_READY
)
304 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
306 if (stat
& DT_S_COMPOSITE_ERROR
)
308 if (stat
& DT_S_READY
)
310 } while (--timeout
> 0);
315 static void dt2801_writecmd(struct comedi_device
*dev
, int command
)
319 dt2801_wait_for_ready(dev
);
321 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
322 if (stat
& DT_S_COMPOSITE_ERROR
) {
323 dev_dbg(dev
->class_dev
,
324 "composite-error in %s, ignoring\n", __func__
);
326 if (!(stat
& DT_S_READY
))
327 dev_dbg(dev
->class_dev
, "!ready in %s, ignoring\n", __func__
);
328 outb_p(command
, dev
->iobase
+ DT2801_CMD
);
331 static int dt2801_reset(struct comedi_device
*dev
)
337 /* pull random data from data port */
338 inb_p(dev
->iobase
+ DT2801_DATA
);
339 inb_p(dev
->iobase
+ DT2801_DATA
);
340 inb_p(dev
->iobase
+ DT2801_DATA
);
341 inb_p(dev
->iobase
+ DT2801_DATA
);
343 /* dt2801_writecmd(dev,DT_C_STOP); */
344 outb_p(DT_C_STOP
, dev
->iobase
+ DT2801_CMD
);
346 /* dt2801_wait_for_ready(dev); */
347 usleep_range(100, 200);
350 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
351 if (stat
& DT_S_READY
)
355 dev_dbg(dev
->class_dev
, "timeout 1 status=0x%02x\n", stat
);
357 /* dt2801_readdata(dev,&board_code); */
359 outb_p(DT_C_RESET
, dev
->iobase
+ DT2801_CMD
);
360 /* dt2801_writecmd(dev,DT_C_RESET); */
362 usleep_range(100, 200);
365 stat
= inb_p(dev
->iobase
+ DT2801_STATUS
);
366 if (stat
& DT_S_READY
)
370 dev_dbg(dev
->class_dev
, "timeout 2 status=0x%02x\n", stat
);
372 dt2801_readdata(dev
, &board_code
);
377 static int probe_number_of_ai_chans(struct comedi_device
*dev
)
383 for (n_chans
= 0; n_chans
< 16; n_chans
++) {
384 dt2801_writecmd(dev
, DT_C_READ_ADIM
);
385 dt2801_writedata(dev
, 0);
386 dt2801_writedata(dev
, n_chans
);
387 stat
= dt2801_readdata2(dev
, &data
);
399 static const struct comedi_lrange
*dac_range_table
[] = {
407 static const struct comedi_lrange
*dac_range_lkup(int opt
)
409 if (opt
< 0 || opt
>= 5)
410 return &range_unknown
;
411 return dac_range_table
[opt
];
414 static const struct comedi_lrange
*ai_range_lkup(int type
, int opt
)
419 &range_dt2801_ai_pgl_unipolar
:
420 &range_dt2801_ai_pgl_bipolar
;
422 return (opt
) ? &range_unipolar10
: &range_bipolar10
;
424 return &range_unipolar5
;
426 return &range_unknown
;
429 static int dt2801_error(struct comedi_device
*dev
, int stat
)
433 dev_dbg(dev
->class_dev
, "timeout\n");
435 dev_dbg(dev
->class_dev
, "error %d\n", stat
);
438 dev_dbg(dev
->class_dev
, "error status 0x%02x, resetting...\n", stat
);
446 static int dt2801_ai_insn_read(struct comedi_device
*dev
,
447 struct comedi_subdevice
*s
,
448 struct comedi_insn
*insn
, unsigned int *data
)
454 for (i
= 0; i
< insn
->n
; i
++) {
455 dt2801_writecmd(dev
, DT_C_READ_ADIM
);
456 dt2801_writedata(dev
, CR_RANGE(insn
->chanspec
));
457 dt2801_writedata(dev
, CR_CHAN(insn
->chanspec
));
458 stat
= dt2801_readdata2(dev
, &d
);
461 return dt2801_error(dev
, stat
);
469 static int dt2801_ao_insn_write(struct comedi_device
*dev
,
470 struct comedi_subdevice
*s
,
471 struct comedi_insn
*insn
,
474 unsigned int chan
= CR_CHAN(insn
->chanspec
);
476 dt2801_writecmd(dev
, DT_C_WRITE_DAIM
);
477 dt2801_writedata(dev
, chan
);
478 dt2801_writedata2(dev
, data
[0]);
480 s
->readback
[chan
] = data
[0];
485 static int dt2801_dio_insn_bits(struct comedi_device
*dev
,
486 struct comedi_subdevice
*s
,
487 struct comedi_insn
*insn
,
490 int which
= (s
== &dev
->subdevices
[3]) ? 1 : 0;
491 unsigned int val
= 0;
493 if (comedi_dio_update_state(s
, data
)) {
494 dt2801_writecmd(dev
, DT_C_WRITE_DIG
);
495 dt2801_writedata(dev
, which
);
496 dt2801_writedata(dev
, s
->state
);
499 dt2801_writecmd(dev
, DT_C_READ_DIG
);
500 dt2801_writedata(dev
, which
);
501 dt2801_readdata(dev
, &val
);
508 static int dt2801_dio_insn_config(struct comedi_device
*dev
,
509 struct comedi_subdevice
*s
,
510 struct comedi_insn
*insn
,
515 ret
= comedi_dio_insn_config(dev
, s
, insn
, data
, 0xff);
519 dt2801_writecmd(dev
, s
->io_bits
? DT_C_SET_DIGOUT
: DT_C_SET_DIGIN
);
520 dt2801_writedata(dev
, (s
== &dev
->subdevices
[3]) ? 1 : 0);
529 * [2] - a/d 0=differential, 1=single-ended
530 * [3] - a/d range 0=[-10,10], 1=[0,10]
531 * [4] - dac0 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
532 * [5] - dac1 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
534 static int dt2801_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
536 const struct dt2801_board
*board
;
537 struct dt2801_private
*devpriv
;
538 struct comedi_subdevice
*s
;
539 int board_code
, type
;
543 ret
= comedi_request_region(dev
, it
->options
[0], 0x2);
547 /* do some checking */
549 board_code
= dt2801_reset(dev
);
551 /* heh. if it didn't work, try it again. */
553 board_code
= dt2801_reset(dev
);
555 for (type
= 0; type
< ARRAY_SIZE(boardtypes
); type
++) {
556 if (boardtypes
[type
].boardcode
== board_code
)
559 dev_dbg(dev
->class_dev
,
560 "unrecognized board code=0x%02x, contact author\n", board_code
);
564 dev
->board_ptr
= boardtypes
+ type
;
565 board
= dev
->board_ptr
;
567 n_ai_chans
= probe_number_of_ai_chans(dev
);
569 ret
= comedi_alloc_subdevices(dev
, 4);
573 devpriv
= comedi_alloc_devpriv(dev
, sizeof(*devpriv
));
577 dev
->board_name
= board
->name
;
579 s
= &dev
->subdevices
[0];
581 s
->type
= COMEDI_SUBD_AI
;
582 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
;
584 s
->n_chan
= n_ai_chans
;
587 s
->n_chan
= board
->ad_chan
;
589 s
->n_chan
= board
->ad_chan
/ 2;
591 s
->maxdata
= (1 << board
->adbits
) - 1;
592 s
->range_table
= ai_range_lkup(board
->adrangetype
, it
->options
[3]);
593 s
->insn_read
= dt2801_ai_insn_read
;
595 s
= &dev
->subdevices
[1];
597 s
->type
= COMEDI_SUBD_AO
;
598 s
->subdev_flags
= SDF_WRITABLE
;
600 s
->maxdata
= (1 << board
->dabits
) - 1;
601 s
->range_table_list
= devpriv
->dac_range_types
;
602 devpriv
->dac_range_types
[0] = dac_range_lkup(it
->options
[4]);
603 devpriv
->dac_range_types
[1] = dac_range_lkup(it
->options
[5]);
604 s
->insn_write
= dt2801_ao_insn_write
;
606 ret
= comedi_alloc_subdev_readback(s
);
610 s
= &dev
->subdevices
[2];
611 /* 1st digital subdevice */
612 s
->type
= COMEDI_SUBD_DIO
;
613 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
616 s
->range_table
= &range_digital
;
617 s
->insn_bits
= dt2801_dio_insn_bits
;
618 s
->insn_config
= dt2801_dio_insn_config
;
620 s
= &dev
->subdevices
[3];
621 /* 2nd digital subdevice */
622 s
->type
= COMEDI_SUBD_DIO
;
623 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
626 s
->range_table
= &range_digital
;
627 s
->insn_bits
= dt2801_dio_insn_bits
;
628 s
->insn_config
= dt2801_dio_insn_config
;
635 static struct comedi_driver dt2801_driver
= {
636 .driver_name
= "dt2801",
637 .module
= THIS_MODULE
,
638 .attach
= dt2801_attach
,
639 .detach
= comedi_legacy_detach
,
641 module_comedi_driver(dt2801_driver
);
643 MODULE_AUTHOR("Comedi https://www.comedi.org");
644 MODULE_DESCRIPTION("Comedi low-level driver");
645 MODULE_LICENSE("GPL");