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 #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 struct dt2801_private {
230 const struct comedi_lrange *dac_range_types[2];
231 unsigned int ao_readback[2];
234 /* These are the low-level routines:
235 writecommand: write a command to the board
236 writedata: write data byte
237 readdata: read data byte
240 /* Only checks DataOutReady-flag, not the Ready-flag as it is done
241 in the examples of the manual. I don't see why this should be
243 static int dt2801_readdata(struct comedi_device *dev, int *data)
246 int timeout = DT2801_TIMEOUT;
249 stat = inb_p(dev->iobase + DT2801_STATUS);
250 if (stat & (DT_S_COMPOSITE_ERROR | DT_S_READY))
252 if (stat & DT_S_DATA_OUT_READY) {
253 *data = inb_p(dev->iobase + DT2801_DATA);
256 } while (--timeout > 0);
261 static int dt2801_readdata2(struct comedi_device *dev, int *data)
267 ret = dt2801_readdata(dev, &lb);
270 ret = dt2801_readdata(dev, &hb);
274 *data = (hb << 8) + lb;
278 static int dt2801_writedata(struct comedi_device *dev, unsigned int data)
281 int timeout = DT2801_TIMEOUT;
284 stat = inb_p(dev->iobase + DT2801_STATUS);
286 if (stat & DT_S_COMPOSITE_ERROR)
288 if (!(stat & DT_S_DATA_IN_FULL)) {
289 outb_p(data & 0xff, dev->iobase + DT2801_DATA);
293 if (stat & DT_S_READY) {
295 ("dt2801: ready flag set (bad!) in dt2801_writedata()\n");
299 } while (--timeout > 0);
304 static int dt2801_writedata2(struct comedi_device *dev, unsigned int data)
308 ret = dt2801_writedata(dev, data & 0xff);
311 ret = dt2801_writedata(dev, (data >> 8));
318 static int dt2801_wait_for_ready(struct comedi_device *dev)
320 int timeout = DT2801_TIMEOUT;
323 stat = inb_p(dev->iobase + DT2801_STATUS);
324 if (stat & DT_S_READY)
327 stat = inb_p(dev->iobase + DT2801_STATUS);
329 if (stat & DT_S_COMPOSITE_ERROR)
331 if (stat & DT_S_READY)
333 } while (--timeout > 0);
338 static int dt2801_writecmd(struct comedi_device *dev, int command)
342 dt2801_wait_for_ready(dev);
344 stat = inb_p(dev->iobase + DT2801_STATUS);
345 if (stat & DT_S_COMPOSITE_ERROR) {
347 ("dt2801: composite-error in dt2801_writecmd(), ignoring\n");
349 if (!(stat & DT_S_READY))
350 printk("dt2801: !ready in dt2801_writecmd(), ignoring\n");
351 outb_p(command, dev->iobase + DT2801_CMD);
356 static int dt2801_reset(struct comedi_device *dev)
362 DPRINTK("dt2801: resetting board...\n");
363 DPRINTK("fingerprint: 0x%02x 0x%02x\n", inb_p(dev->iobase),
364 inb_p(dev->iobase + 1));
366 /* pull random data from data port */
367 inb_p(dev->iobase + DT2801_DATA);
368 inb_p(dev->iobase + DT2801_DATA);
369 inb_p(dev->iobase + DT2801_DATA);
370 inb_p(dev->iobase + DT2801_DATA);
372 DPRINTK("dt2801: stop\n");
373 /* dt2801_writecmd(dev,DT_C_STOP); */
374 outb_p(DT_C_STOP, dev->iobase + DT2801_CMD);
376 /* dt2801_wait_for_ready(dev); */
380 stat = inb_p(dev->iobase + DT2801_STATUS);
381 if (stat & DT_S_READY)
385 printk("dt2801: timeout 1 status=0x%02x\n", stat);
387 /* printk("dt2801: reading dummy\n"); */
388 /* dt2801_readdata(dev,&board_code); */
390 DPRINTK("dt2801: reset\n");
391 outb_p(DT_C_RESET, dev->iobase + DT2801_CMD);
392 /* dt2801_writecmd(dev,DT_C_RESET); */
397 stat = inb_p(dev->iobase + DT2801_STATUS);
398 if (stat & DT_S_READY)
402 printk("dt2801: timeout 2 status=0x%02x\n", stat);
404 DPRINTK("dt2801: reading code\n");
405 dt2801_readdata(dev, &board_code);
407 DPRINTK("dt2801: ok. code=0x%02x\n", board_code);
412 static int probe_number_of_ai_chans(struct comedi_device *dev)
418 for (n_chans = 0; n_chans < 16; n_chans++) {
419 stat = dt2801_writecmd(dev, DT_C_READ_ADIM);
420 dt2801_writedata(dev, 0);
421 dt2801_writedata(dev, n_chans);
422 stat = dt2801_readdata2(dev, &data);
434 static const struct comedi_lrange *dac_range_table[] = {
442 static const struct comedi_lrange *dac_range_lkup(int opt)
444 if (opt < 0 || opt >= 5)
445 return &range_unknown;
446 return dac_range_table[opt];
449 static const struct comedi_lrange *ai_range_lkup(int type, int opt)
454 &range_dt2801_ai_pgl_unipolar :
455 &range_dt2801_ai_pgl_bipolar;
457 return (opt) ? &range_unipolar10 : &range_bipolar10;
459 return &range_unipolar5;
461 return &range_unknown;
464 static int dt2801_error(struct comedi_device *dev, int stat)
468 printk("dt2801: timeout\n");
470 printk("dt2801: error %d\n", stat);
473 printk("dt2801: error status 0x%02x, resetting...\n", stat);
481 static int dt2801_ai_insn_read(struct comedi_device *dev,
482 struct comedi_subdevice *s,
483 struct comedi_insn *insn, unsigned int *data)
489 for (i = 0; i < insn->n; i++) {
490 stat = dt2801_writecmd(dev, DT_C_READ_ADIM);
491 dt2801_writedata(dev, CR_RANGE(insn->chanspec));
492 dt2801_writedata(dev, CR_CHAN(insn->chanspec));
493 stat = dt2801_readdata2(dev, &d);
496 return dt2801_error(dev, stat);
504 static int dt2801_ao_insn_read(struct comedi_device *dev,
505 struct comedi_subdevice *s,
506 struct comedi_insn *insn, unsigned int *data)
508 struct dt2801_private *devpriv = dev->private;
510 data[0] = devpriv->ao_readback[CR_CHAN(insn->chanspec)];
515 static int dt2801_ao_insn_write(struct comedi_device *dev,
516 struct comedi_subdevice *s,
517 struct comedi_insn *insn, unsigned int *data)
519 struct dt2801_private *devpriv = dev->private;
521 dt2801_writecmd(dev, DT_C_WRITE_DAIM);
522 dt2801_writedata(dev, CR_CHAN(insn->chanspec));
523 dt2801_writedata2(dev, data[0]);
525 devpriv->ao_readback[CR_CHAN(insn->chanspec)] = data[0];
530 static int dt2801_dio_insn_bits(struct comedi_device *dev,
531 struct comedi_subdevice *s,
532 struct comedi_insn *insn,
535 int which = (s == &dev->subdevices[3]) ? 1 : 0;
536 unsigned int val = 0;
538 if (comedi_dio_update_state(s, data)) {
539 dt2801_writecmd(dev, DT_C_WRITE_DIG);
540 dt2801_writedata(dev, which);
541 dt2801_writedata(dev, s->state);
544 dt2801_writecmd(dev, DT_C_READ_DIG);
545 dt2801_writedata(dev, which);
546 dt2801_readdata(dev, &val);
553 static int dt2801_dio_insn_config(struct comedi_device *dev,
554 struct comedi_subdevice *s,
555 struct comedi_insn *insn,
560 ret = comedi_dio_insn_config(dev, s, insn, data, 0xff);
564 dt2801_writecmd(dev, s->io_bits ? DT_C_SET_DIGOUT : DT_C_SET_DIGIN);
565 dt2801_writedata(dev, (s == &dev->subdevices[3]) ? 1 : 0);
574 [2] - a/d 0=differential, 1=single-ended
575 [3] - a/d range 0=[-10,10], 1=[0,10]
576 [4] - dac0 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
577 [5] - dac1 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
579 static int dt2801_attach(struct comedi_device *dev, struct comedi_devconfig *it)
581 const struct dt2801_board *board = comedi_board(dev);
582 struct dt2801_private *devpriv;
583 struct comedi_subdevice *s;
584 int board_code, type;
588 ret = comedi_request_region(dev, it->options[0], DT2801_IOSIZE);
592 /* do some checking */
594 board_code = dt2801_reset(dev);
596 /* heh. if it didn't work, try it again. */
598 board_code = dt2801_reset(dev);
600 for (type = 0; type < ARRAY_SIZE(boardtypes); type++) {
601 if (boardtypes[type].boardcode == board_code)
604 printk("dt2801: unrecognized board code=0x%02x, contact author\n",
609 dev->board_ptr = boardtypes + type;
610 board = comedi_board(dev);
612 n_ai_chans = probe_number_of_ai_chans(dev);
614 ret = comedi_alloc_subdevices(dev, 4);
618 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
622 dev->board_name = board->name;
624 s = &dev->subdevices[0];
626 s->type = COMEDI_SUBD_AI;
627 s->subdev_flags = SDF_READABLE | SDF_GROUND;
629 s->n_chan = n_ai_chans;
632 s->n_chan = board->ad_chan;
634 s->n_chan = board->ad_chan / 2;
636 s->maxdata = (1 << board->adbits) - 1;
637 s->range_table = ai_range_lkup(board->adrangetype, it->options[3]);
638 s->insn_read = dt2801_ai_insn_read;
640 s = &dev->subdevices[1];
642 s->type = COMEDI_SUBD_AO;
643 s->subdev_flags = SDF_WRITABLE;
645 s->maxdata = (1 << board->dabits) - 1;
646 s->range_table_list = devpriv->dac_range_types;
647 devpriv->dac_range_types[0] = dac_range_lkup(it->options[4]);
648 devpriv->dac_range_types[1] = dac_range_lkup(it->options[5]);
649 s->insn_read = dt2801_ao_insn_read;
650 s->insn_write = dt2801_ao_insn_write;
652 s = &dev->subdevices[2];
653 /* 1st digital subdevice */
654 s->type = COMEDI_SUBD_DIO;
655 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
658 s->range_table = &range_digital;
659 s->insn_bits = dt2801_dio_insn_bits;
660 s->insn_config = dt2801_dio_insn_config;
662 s = &dev->subdevices[3];
663 /* 2nd digital subdevice */
664 s->type = COMEDI_SUBD_DIO;
665 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
668 s->range_table = &range_digital;
669 s->insn_bits = dt2801_dio_insn_bits;
670 s->insn_config = dt2801_dio_insn_config;
677 static struct comedi_driver dt2801_driver = {
678 .driver_name = "dt2801",
679 .module = THIS_MODULE,
680 .attach = dt2801_attach,
681 .detach = comedi_legacy_detach,
683 module_comedi_driver(dt2801_driver);
685 MODULE_AUTHOR("Comedi http://www.comedi.org");
686 MODULE_DESCRIPTION("Comedi low-level driver");
687 MODULE_LICENSE("GPL");