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 COMEDI_INITCLEANUP(driver_dt2801);
104 /* ignore 'defined but not used' warning */
105 static const struct comedi_lrange range_dt2801_ai_pgh_bipolar = { 4, {
119 static const struct comedi_lrange range_dt2801_ai_pgl_bipolar = { 4, {
134 /* ignore 'defined but not used' warning */
135 static const struct comedi_lrange range_dt2801_ai_pgh_unipolar = { 4, {
147 static const struct comedi_lrange range_dt2801_ai_pgl_unipolar = { 4, {
159 struct dt2801_board {
170 /* Typeid's for the different boards of the DT2801-series
171 (taken from the test-software, that comes with the board)
173 static const struct dt2801_board boardtypes[] = {
191 .name = "dt2801/5716a",
207 .name = "dt2805/5716a",
240 #define boardtype (*(const struct dt2801_board *)dev->board_ptr)
242 struct dt2801_private {
244 const struct comedi_lrange *dac_range_types[2];
245 unsigned int ao_readback[2];
248 #define devpriv ((struct dt2801_private *)dev->private)
250 static int dt2801_ai_insn_read(struct comedi_device *dev,
251 struct comedi_subdevice *s,
252 struct comedi_insn *insn, unsigned int *data);
253 static int dt2801_ao_insn_read(struct comedi_device *dev,
254 struct comedi_subdevice *s,
255 struct comedi_insn *insn, unsigned int *data);
256 static int dt2801_ao_insn_write(struct comedi_device *dev,
257 struct comedi_subdevice *s,
258 struct comedi_insn *insn, unsigned int *data);
259 static int dt2801_dio_insn_bits(struct comedi_device *dev,
260 struct comedi_subdevice *s,
261 struct comedi_insn *insn, unsigned int *data);
262 static int dt2801_dio_insn_config(struct comedi_device *dev,
263 struct comedi_subdevice *s,
264 struct comedi_insn *insn, unsigned int *data);
266 /* These are the low-level routines:
267 writecommand: write a command to the board
268 writedata: write data byte
269 readdata: read data byte
272 /* Only checks DataOutReady-flag, not the Ready-flag as it is done
273 in the examples of the manual. I don't see why this should be
275 static int dt2801_readdata(struct comedi_device *dev, int *data)
278 int timeout = DT2801_TIMEOUT;
281 stat = inb_p(dev->iobase + DT2801_STATUS);
282 if (stat & (DT_S_COMPOSITE_ERROR | DT_S_READY))
284 if (stat & DT_S_DATA_OUT_READY) {
285 *data = inb_p(dev->iobase + DT2801_DATA);
288 } while (--timeout > 0);
293 static int dt2801_readdata2(struct comedi_device *dev, int *data)
298 ret = dt2801_readdata(dev, &lb);
301 ret = dt2801_readdata(dev, &hb);
305 *data = (hb << 8) + lb;
309 static int dt2801_writedata(struct comedi_device *dev, unsigned int data)
312 int timeout = DT2801_TIMEOUT;
315 stat = inb_p(dev->iobase + DT2801_STATUS);
317 if (stat & DT_S_COMPOSITE_ERROR)
319 if (!(stat & DT_S_DATA_IN_FULL)) {
320 outb_p(data & 0xff, dev->iobase + DT2801_DATA);
324 if (stat & DT_S_READY) {
326 ("dt2801: ready flag set (bad!) in dt2801_writedata()\n");
330 } while (--timeout > 0);
335 static int dt2801_writedata2(struct comedi_device *dev, unsigned int data)
339 ret = dt2801_writedata(dev, data & 0xff);
342 ret = dt2801_writedata(dev, (data >> 8));
349 static int dt2801_wait_for_ready(struct comedi_device *dev)
351 int timeout = DT2801_TIMEOUT;
354 stat = inb_p(dev->iobase + DT2801_STATUS);
355 if (stat & DT_S_READY)
358 stat = inb_p(dev->iobase + DT2801_STATUS);
360 if (stat & DT_S_COMPOSITE_ERROR)
362 if (stat & DT_S_READY)
364 } while (--timeout > 0);
369 static int dt2801_writecmd(struct comedi_device *dev, int command)
373 dt2801_wait_for_ready(dev);
375 stat = inb_p(dev->iobase + DT2801_STATUS);
376 if (stat & DT_S_COMPOSITE_ERROR) {
378 ("dt2801: composite-error in dt2801_writecmd(), ignoring\n");
380 if (!(stat & DT_S_READY))
381 printk("dt2801: !ready in dt2801_writecmd(), ignoring\n");
382 outb_p(command, dev->iobase + DT2801_CMD);
387 static int dt2801_reset(struct comedi_device *dev)
393 DPRINTK("dt2801: resetting board...\n");
394 DPRINTK("fingerprint: 0x%02x 0x%02x\n", inb_p(dev->iobase),
395 inb_p(dev->iobase + 1));
397 /* pull random data from data port */
398 inb_p(dev->iobase + DT2801_DATA);
399 inb_p(dev->iobase + DT2801_DATA);
400 inb_p(dev->iobase + DT2801_DATA);
401 inb_p(dev->iobase + DT2801_DATA);
403 DPRINTK("dt2801: stop\n");
404 /* dt2801_writecmd(dev,DT_C_STOP); */
405 outb_p(DT_C_STOP, dev->iobase + DT2801_CMD);
407 /* dt2801_wait_for_ready(dev); */
411 stat = inb_p(dev->iobase + DT2801_STATUS);
412 if (stat & DT_S_READY)
416 printk("dt2801: timeout 1 status=0x%02x\n", stat);
418 /* printk("dt2801: reading dummy\n"); */
419 /* dt2801_readdata(dev,&board_code); */
421 DPRINTK("dt2801: reset\n");
422 outb_p(DT_C_RESET, dev->iobase + DT2801_CMD);
423 /* dt2801_writecmd(dev,DT_C_RESET); */
428 stat = inb_p(dev->iobase + DT2801_STATUS);
429 if (stat & DT_S_READY)
433 printk("dt2801: timeout 2 status=0x%02x\n", stat);
435 DPRINTK("dt2801: reading code\n");
436 dt2801_readdata(dev, &board_code);
438 DPRINTK("dt2801: ok. code=0x%02x\n", board_code);
443 static int probe_number_of_ai_chans(struct comedi_device *dev)
449 for (n_chans = 0; n_chans < 16; n_chans++) {
450 stat = dt2801_writecmd(dev, DT_C_READ_ADIM);
451 dt2801_writedata(dev, 0);
452 dt2801_writedata(dev, n_chans);
453 stat = dt2801_readdata2(dev, &data);
465 static const struct comedi_lrange *dac_range_table[] = {
473 static const struct comedi_lrange *dac_range_lkup(int opt)
475 if (opt < 0 || opt > 5)
476 return &range_unknown;
477 return dac_range_table[opt];
480 static const struct comedi_lrange *ai_range_lkup(int type, int opt)
485 &range_dt2801_ai_pgl_unipolar :
486 &range_dt2801_ai_pgl_bipolar;
488 return (opt) ? &range_unipolar10 : &range_bipolar10;
490 return &range_unipolar5;
492 return &range_unknown;
499 [2] - a/d 0=differential, 1=single-ended
500 [3] - a/d range 0=[-10,10], 1=[0,10]
501 [4] - dac0 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
502 [5] - dac1 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
504 static int dt2801_attach(struct comedi_device *dev, struct comedi_devconfig *it)
506 struct comedi_subdevice *s;
507 unsigned long iobase;
508 int board_code, type;
512 iobase = it->options[0];
513 if (!request_region(iobase, DT2801_IOSIZE, "dt2801")) {
514 comedi_error(dev, "I/O port conflict");
517 dev->iobase = iobase;
519 /* do some checking */
521 board_code = dt2801_reset(dev);
523 /* heh. if it didn't work, try it again. */
525 board_code = dt2801_reset(dev);
527 for (type = 0; type < ARRAY_SIZE(boardtypes); type++) {
528 if (boardtypes[type].boardcode == board_code)
531 printk("dt2801: unrecognized board code=0x%02x, contact author\n",
536 dev->board_ptr = boardtypes + type;
537 printk("dt2801: %s at port 0x%lx", boardtype.name, iobase);
539 n_ai_chans = probe_number_of_ai_chans(dev);
540 printk(" (ai channels = %d)", n_ai_chans);
542 ret = alloc_subdevices(dev, 4);
546 ret = alloc_private(dev, sizeof(struct dt2801_private));
550 dev->board_name = boardtype.name;
552 s = dev->subdevices + 0;
554 s->type = COMEDI_SUBD_AI;
555 s->subdev_flags = SDF_READABLE | SDF_GROUND;
557 s->n_chan = n_ai_chans;
560 s->n_chan = boardtype.ad_chan;
562 s->n_chan = boardtype.ad_chan / 2;
564 s->maxdata = (1 << boardtype.adbits) - 1;
565 s->range_table = ai_range_lkup(boardtype.adrangetype, it->options[3]);
566 s->insn_read = dt2801_ai_insn_read;
570 s->type = COMEDI_SUBD_AO;
571 s->subdev_flags = SDF_WRITABLE;
573 s->maxdata = (1 << boardtype.dabits) - 1;
574 s->range_table_list = devpriv->dac_range_types;
575 devpriv->dac_range_types[0] = dac_range_lkup(it->options[4]);
576 devpriv->dac_range_types[1] = dac_range_lkup(it->options[5]);
577 s->insn_read = dt2801_ao_insn_read;
578 s->insn_write = dt2801_ao_insn_write;
581 /* 1st digital subdevice */
582 s->type = COMEDI_SUBD_DIO;
583 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
586 s->range_table = &range_digital;
587 s->insn_bits = dt2801_dio_insn_bits;
588 s->insn_config = dt2801_dio_insn_config;
591 /* 2nd digital subdevice */
592 s->type = COMEDI_SUBD_DIO;
593 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
596 s->range_table = &range_digital;
597 s->insn_bits = dt2801_dio_insn_bits;
598 s->insn_config = dt2801_dio_insn_config;
607 static int dt2801_detach(struct comedi_device *dev)
610 release_region(dev->iobase, DT2801_IOSIZE);
615 static int dt2801_error(struct comedi_device *dev, int stat)
619 printk("dt2801: timeout\n");
621 printk("dt2801: error %d\n", stat);
624 printk("dt2801: error status 0x%02x, resetting...\n", stat);
632 static int dt2801_ai_insn_read(struct comedi_device *dev,
633 struct comedi_subdevice *s,
634 struct comedi_insn *insn, unsigned int *data)
640 for (i = 0; i < insn->n; i++) {
641 stat = dt2801_writecmd(dev, DT_C_READ_ADIM);
642 dt2801_writedata(dev, CR_RANGE(insn->chanspec));
643 dt2801_writedata(dev, CR_CHAN(insn->chanspec));
644 stat = dt2801_readdata2(dev, &d);
647 return dt2801_error(dev, stat);
655 static int dt2801_ao_insn_read(struct comedi_device *dev,
656 struct comedi_subdevice *s,
657 struct comedi_insn *insn, unsigned int *data)
659 data[0] = devpriv->ao_readback[CR_CHAN(insn->chanspec)];
664 static int dt2801_ao_insn_write(struct comedi_device *dev,
665 struct comedi_subdevice *s,
666 struct comedi_insn *insn, unsigned int *data)
668 dt2801_writecmd(dev, DT_C_WRITE_DAIM);
669 dt2801_writedata(dev, CR_CHAN(insn->chanspec));
670 dt2801_writedata2(dev, data[0]);
672 devpriv->ao_readback[CR_CHAN(insn->chanspec)] = data[0];
677 static int dt2801_dio_insn_bits(struct comedi_device *dev,
678 struct comedi_subdevice *s,
679 struct comedi_insn *insn, unsigned int *data)
683 if (s == dev->subdevices + 4)
689 s->state &= ~data[0];
690 s->state |= (data[0] & data[1]);
691 dt2801_writecmd(dev, DT_C_WRITE_DIG);
692 dt2801_writedata(dev, which);
693 dt2801_writedata(dev, s->state);
695 dt2801_writecmd(dev, DT_C_READ_DIG);
696 dt2801_writedata(dev, which);
697 dt2801_readdata(dev, data + 1);
702 static int dt2801_dio_insn_config(struct comedi_device *dev,
703 struct comedi_subdevice *s,
704 struct comedi_insn *insn, unsigned int *data)
708 if (s == dev->subdevices + 4)
714 dt2801_writecmd(dev, DT_C_SET_DIGOUT);
717 dt2801_writecmd(dev, DT_C_SET_DIGIN);
719 dt2801_writedata(dev, which);