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, struct comedi_devconfig *it);
92 static int dt2801_detach(struct comedi_device *dev);
93 static struct comedi_driver driver_dt2801 = {
100 COMEDI_INITCLEANUP(driver_dt2801);
103 /* ignore 'defined but not used' warning */
104 static const struct comedi_lrange range_dt2801_ai_pgh_bipolar = { 4, {
112 static const struct comedi_lrange range_dt2801_ai_pgl_bipolar = { 4, {
121 /* ignore 'defined but not used' warning */
122 static const struct comedi_lrange range_dt2801_ai_pgh_unipolar = { 4, {
130 static const struct comedi_lrange range_dt2801_ai_pgl_unipolar = { 4, {
138 struct dt2801_board {
150 /* Typeid's for the different boards of the DT2801-series
151 (taken from the test-software, that comes with the board)
153 static const struct dt2801_board boardtypes[] = {
171 name: "dt2801/5716a",
187 name: "dt2805/5716a",
220 #define n_boardtypes ((sizeof(boardtypes))/(sizeof(boardtypes[0])))
221 #define boardtype (*(const struct dt2801_board *)dev->board_ptr)
223 struct dt2801_private {
225 const struct comedi_lrange *dac_range_types[2];
226 unsigned int ao_readback[2];
229 #define devpriv ((struct dt2801_private *)dev->private)
231 static int dt2801_ai_insn_read(struct comedi_device *dev, struct comedi_subdevice *s,
232 struct comedi_insn *insn, unsigned int *data);
233 static int dt2801_ao_insn_read(struct comedi_device *dev, struct comedi_subdevice *s,
234 struct comedi_insn *insn, unsigned int *data);
235 static int dt2801_ao_insn_write(struct comedi_device *dev, struct comedi_subdevice *s,
236 struct comedi_insn *insn, unsigned int *data);
237 static int dt2801_dio_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s,
238 struct comedi_insn *insn, unsigned int *data);
239 static int dt2801_dio_insn_config(struct comedi_device *dev, struct comedi_subdevice *s,
240 struct comedi_insn *insn, unsigned int *data);
242 /* These are the low-level routines:
243 writecommand: write a command to the board
244 writedata: write data byte
245 readdata: read data byte
248 /* Only checks DataOutReady-flag, not the Ready-flag as it is done
249 in the examples of the manual. I don't see why this should be
251 static int dt2801_readdata(struct comedi_device *dev, int *data)
254 int timeout = DT2801_TIMEOUT;
257 stat = inb_p(dev->iobase + DT2801_STATUS);
258 if (stat & (DT_S_COMPOSITE_ERROR | DT_S_READY)) {
261 if (stat & DT_S_DATA_OUT_READY) {
262 *data = inb_p(dev->iobase + DT2801_DATA);
265 } while (--timeout > 0);
270 static int dt2801_readdata2(struct comedi_device *dev, int *data)
275 ret = dt2801_readdata(dev, &lb);
278 ret = dt2801_readdata(dev, &hb);
282 *data = (hb << 8) + lb;
286 static int dt2801_writedata(struct comedi_device *dev, unsigned int data)
289 int timeout = DT2801_TIMEOUT;
292 stat = inb_p(dev->iobase + DT2801_STATUS);
294 if (stat & DT_S_COMPOSITE_ERROR) {
297 if (!(stat & DT_S_DATA_IN_FULL)) {
298 outb_p(data & 0xff, dev->iobase + DT2801_DATA);
302 if (stat & DT_S_READY) {
303 printk("dt2801: ready flag set (bad!) in dt2801_writedata()\n");
307 } while (--timeout > 0);
312 static int dt2801_writedata2(struct comedi_device *dev, unsigned int data)
316 ret = dt2801_writedata(dev, data & 0xff);
319 ret = dt2801_writedata(dev, (data >> 8));
326 static int dt2801_wait_for_ready(struct comedi_device *dev)
328 int timeout = DT2801_TIMEOUT;
331 stat = inb_p(dev->iobase + DT2801_STATUS);
332 if (stat & DT_S_READY) {
336 stat = inb_p(dev->iobase + DT2801_STATUS);
338 if (stat & DT_S_COMPOSITE_ERROR) {
341 if (stat & DT_S_READY) {
344 } while (--timeout > 0);
349 static int dt2801_writecmd(struct comedi_device *dev, int command)
353 dt2801_wait_for_ready(dev);
355 stat = inb_p(dev->iobase + DT2801_STATUS);
356 if (stat & DT_S_COMPOSITE_ERROR) {
357 printk("dt2801: composite-error in dt2801_writecmd(), ignoring\n");
359 if (!(stat & DT_S_READY)) {
360 printk("dt2801: !ready in dt2801_writecmd(), ignoring\n");
362 outb_p(command, dev->iobase + DT2801_CMD);
367 static int dt2801_reset(struct comedi_device *dev)
373 DPRINTK("dt2801: resetting board...\n");
374 DPRINTK("fingerprint: 0x%02x 0x%02x\n", inb_p(dev->iobase),
375 inb_p(dev->iobase + 1));
377 /* pull random data from data port */
378 inb_p(dev->iobase + DT2801_DATA);
379 inb_p(dev->iobase + DT2801_DATA);
380 inb_p(dev->iobase + DT2801_DATA);
381 inb_p(dev->iobase + DT2801_DATA);
383 DPRINTK("dt2801: stop\n");
384 /* dt2801_writecmd(dev,DT_C_STOP); */
385 outb_p(DT_C_STOP, dev->iobase + DT2801_CMD);
387 /* dt2801_wait_for_ready(dev); */
391 stat = inb_p(dev->iobase + DT2801_STATUS);
392 if (stat & DT_S_READY)
396 printk("dt2801: timeout 1 status=0x%02x\n", stat);
399 /* printk("dt2801: reading dummy\n"); */
400 /* dt2801_readdata(dev,&board_code); */
402 DPRINTK("dt2801: reset\n");
403 outb_p(DT_C_RESET, dev->iobase + DT2801_CMD);
404 /* dt2801_writecmd(dev,DT_C_RESET); */
409 stat = inb_p(dev->iobase + DT2801_STATUS);
410 if (stat & DT_S_READY)
414 printk("dt2801: timeout 2 status=0x%02x\n", stat);
417 DPRINTK("dt2801: reading code\n");
418 dt2801_readdata(dev, &board_code);
420 DPRINTK("dt2801: ok. code=0x%02x\n", board_code);
425 static int probe_number_of_ai_chans(struct comedi_device *dev)
431 for (n_chans = 0; n_chans < 16; n_chans++) {
432 stat = dt2801_writecmd(dev, DT_C_READ_ADIM);
433 dt2801_writedata(dev, 0);
434 dt2801_writedata(dev, n_chans);
435 stat = dt2801_readdata2(dev, &data);
447 static const struct comedi_lrange *dac_range_table[] = {
455 static const struct comedi_lrange *dac_range_lkup(int opt)
457 if (opt < 0 || opt > 5)
458 return &range_unknown;
459 return dac_range_table[opt];
462 static const struct comedi_lrange *ai_range_lkup(int type, int opt)
467 &range_dt2801_ai_pgl_unipolar :
468 &range_dt2801_ai_pgl_bipolar;
470 return (opt) ? &range_unipolar10 : &range_bipolar10;
472 return &range_unipolar5;
474 return &range_unknown;
481 [2] - a/d 0=differential, 1=single-ended
482 [3] - a/d range 0=[-10,10], 1=[0,10]
483 [4] - dac0 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
484 [5] - dac1 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
486 static int dt2801_attach(struct comedi_device *dev, struct comedi_devconfig *it)
488 struct comedi_subdevice *s;
489 unsigned long iobase;
490 int board_code, type;
494 iobase = it->options[0];
495 if (!request_region(iobase, DT2801_IOSIZE, "dt2801")) {
496 comedi_error(dev, "I/O port conflict");
499 dev->iobase = iobase;
501 /* do some checking */
503 board_code = dt2801_reset(dev);
505 /* heh. if it didn't work, try it again. */
507 board_code = dt2801_reset(dev);
509 for (type = 0; type < n_boardtypes; type++) {
510 if (boardtypes[type].boardcode == board_code)
513 printk("dt2801: unrecognized board code=0x%02x, contact author\n",
518 dev->board_ptr = boardtypes + type;
519 printk("dt2801: %s at port 0x%lx", boardtype.name, iobase);
521 n_ai_chans = probe_number_of_ai_chans(dev);
522 printk(" (ai channels = %d)", n_ai_chans);
524 if ((ret = alloc_subdevices(dev, 4)) < 0)
527 if ((ret = alloc_private(dev, sizeof(struct dt2801_private))) < 0)
530 dev->board_name = boardtype.name;
532 s = dev->subdevices + 0;
534 s->type = COMEDI_SUBD_AI;
535 s->subdev_flags = SDF_READABLE | SDF_GROUND;
537 s->n_chan = n_ai_chans;
540 s->n_chan = boardtype.ad_chan;
542 s->n_chan = boardtype.ad_chan / 2;
544 s->maxdata = (1 << boardtype.adbits) - 1;
545 s->range_table = ai_range_lkup(boardtype.adrangetype, it->options[3]);
546 s->insn_read = dt2801_ai_insn_read;
550 s->type = COMEDI_SUBD_AO;
551 s->subdev_flags = SDF_WRITABLE;
553 s->maxdata = (1 << boardtype.dabits) - 1;
554 s->range_table_list = devpriv->dac_range_types;
555 devpriv->dac_range_types[0] = dac_range_lkup(it->options[4]);
556 devpriv->dac_range_types[1] = dac_range_lkup(it->options[5]);
557 s->insn_read = dt2801_ao_insn_read;
558 s->insn_write = dt2801_ao_insn_write;
561 /* 1st digital subdevice */
562 s->type = COMEDI_SUBD_DIO;
563 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
566 s->range_table = &range_digital;
567 s->insn_bits = dt2801_dio_insn_bits;
568 s->insn_config = dt2801_dio_insn_config;
571 /* 2nd digital subdevice */
572 s->type = COMEDI_SUBD_DIO;
573 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
576 s->range_table = &range_digital;
577 s->insn_bits = dt2801_dio_insn_bits;
578 s->insn_config = dt2801_dio_insn_config;
587 static int dt2801_detach(struct comedi_device *dev)
590 release_region(dev->iobase, DT2801_IOSIZE);
595 static int dt2801_error(struct comedi_device *dev, int stat)
598 if (stat == -ETIME) {
599 printk("dt2801: timeout\n");
601 printk("dt2801: error %d\n", stat);
605 printk("dt2801: error status 0x%02x, resetting...\n", stat);
613 static int dt2801_ai_insn_read(struct comedi_device *dev, struct comedi_subdevice *s,
614 struct comedi_insn *insn, unsigned int *data)
620 for (i = 0; i < insn->n; i++) {
621 stat = dt2801_writecmd(dev, DT_C_READ_ADIM);
622 dt2801_writedata(dev, CR_RANGE(insn->chanspec));
623 dt2801_writedata(dev, CR_CHAN(insn->chanspec));
624 stat = dt2801_readdata2(dev, &d);
627 return dt2801_error(dev, stat);
635 static int dt2801_ao_insn_read(struct comedi_device *dev, struct comedi_subdevice *s,
636 struct comedi_insn *insn, unsigned int *data)
638 data[0] = devpriv->ao_readback[CR_CHAN(insn->chanspec)];
643 static int dt2801_ao_insn_write(struct comedi_device *dev, struct comedi_subdevice *s,
644 struct comedi_insn *insn, unsigned int *data)
646 dt2801_writecmd(dev, DT_C_WRITE_DAIM);
647 dt2801_writedata(dev, CR_CHAN(insn->chanspec));
648 dt2801_writedata2(dev, data[0]);
650 devpriv->ao_readback[CR_CHAN(insn->chanspec)] = data[0];
655 static int dt2801_dio_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s,
656 struct comedi_insn *insn, unsigned int *data)
660 if (s == dev->subdevices + 4)
666 s->state &= ~data[0];
667 s->state |= (data[0] & data[1]);
668 dt2801_writecmd(dev, DT_C_WRITE_DIG);
669 dt2801_writedata(dev, which);
670 dt2801_writedata(dev, s->state);
672 dt2801_writecmd(dev, DT_C_READ_DIG);
673 dt2801_writedata(dev, which);
674 dt2801_readdata(dev, data + 1);
679 static int dt2801_dio_insn_config(struct comedi_device *dev, struct comedi_subdevice *s,
680 struct comedi_insn *insn, unsigned int *data)
684 if (s == dev->subdevices + 4)
690 dt2801_writecmd(dev, DT_C_SET_DIGOUT);
693 dt2801_writecmd(dev, DT_C_SET_DIGIN);
695 dt2801_writedata(dev, which);