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 {
138 /* Typeid's for the different boards of the DT2801-series
139 (taken from the test-software, that comes with the board)
141 static const struct dt2801_board boardtypes[] = {
159 .name = "dt2801/5716a",
175 .name = "dt2805/5716a",
208 struct dt2801_private {
209 const struct comedi_lrange *dac_range_types[2];
212 /* These are the low-level routines:
213 writecommand: write a command to the board
214 writedata: write data byte
215 readdata: read data byte
218 /* Only checks DataOutReady-flag, not the Ready-flag as it is done
219 in the examples of the manual. I don't see why this should be
221 static int dt2801_readdata(struct comedi_device *dev, int *data)
224 int timeout = DT2801_TIMEOUT;
227 stat = inb_p(dev->iobase + DT2801_STATUS);
228 if (stat & (DT_S_COMPOSITE_ERROR | DT_S_READY))
230 if (stat & DT_S_DATA_OUT_READY) {
231 *data = inb_p(dev->iobase + DT2801_DATA);
234 } while (--timeout > 0);
239 static int dt2801_readdata2(struct comedi_device *dev, int *data)
245 ret = dt2801_readdata(dev, &lb);
248 ret = dt2801_readdata(dev, &hb);
252 *data = (hb << 8) + lb;
256 static int dt2801_writedata(struct comedi_device *dev, unsigned int data)
259 int timeout = DT2801_TIMEOUT;
262 stat = inb_p(dev->iobase + DT2801_STATUS);
264 if (stat & DT_S_COMPOSITE_ERROR)
266 if (!(stat & DT_S_DATA_IN_FULL)) {
267 outb_p(data & 0xff, dev->iobase + DT2801_DATA);
270 } while (--timeout > 0);
275 static int dt2801_writedata2(struct comedi_device *dev, unsigned int data)
279 ret = dt2801_writedata(dev, data & 0xff);
282 ret = dt2801_writedata(dev, data >> 8);
289 static int dt2801_wait_for_ready(struct comedi_device *dev)
291 int timeout = DT2801_TIMEOUT;
294 stat = inb_p(dev->iobase + DT2801_STATUS);
295 if (stat & DT_S_READY)
298 stat = inb_p(dev->iobase + DT2801_STATUS);
300 if (stat & DT_S_COMPOSITE_ERROR)
302 if (stat & DT_S_READY)
304 } while (--timeout > 0);
309 static void dt2801_writecmd(struct comedi_device *dev, int command)
313 dt2801_wait_for_ready(dev);
315 stat = inb_p(dev->iobase + DT2801_STATUS);
316 if (stat & DT_S_COMPOSITE_ERROR) {
317 dev_dbg(dev->class_dev,
318 "composite-error in %s, ignoring\n", __func__);
320 if (!(stat & DT_S_READY))
321 dev_dbg(dev->class_dev, "!ready in %s, ignoring\n", __func__);
322 outb_p(command, dev->iobase + DT2801_CMD);
325 static int dt2801_reset(struct comedi_device *dev)
331 /* pull random data from data port */
332 inb_p(dev->iobase + DT2801_DATA);
333 inb_p(dev->iobase + DT2801_DATA);
334 inb_p(dev->iobase + DT2801_DATA);
335 inb_p(dev->iobase + DT2801_DATA);
337 /* dt2801_writecmd(dev,DT_C_STOP); */
338 outb_p(DT_C_STOP, dev->iobase + DT2801_CMD);
340 /* dt2801_wait_for_ready(dev); */
344 stat = inb_p(dev->iobase + DT2801_STATUS);
345 if (stat & DT_S_READY)
349 dev_dbg(dev->class_dev, "timeout 1 status=0x%02x\n", stat);
351 /* dt2801_readdata(dev,&board_code); */
353 outb_p(DT_C_RESET, dev->iobase + DT2801_CMD);
354 /* dt2801_writecmd(dev,DT_C_RESET); */
359 stat = inb_p(dev->iobase + DT2801_STATUS);
360 if (stat & DT_S_READY)
364 dev_dbg(dev->class_dev, "timeout 2 status=0x%02x\n", stat);
366 dt2801_readdata(dev, &board_code);
371 static int probe_number_of_ai_chans(struct comedi_device *dev)
377 for (n_chans = 0; n_chans < 16; n_chans++) {
378 dt2801_writecmd(dev, DT_C_READ_ADIM);
379 dt2801_writedata(dev, 0);
380 dt2801_writedata(dev, n_chans);
381 stat = dt2801_readdata2(dev, &data);
393 static const struct comedi_lrange *dac_range_table[] = {
401 static const struct comedi_lrange *dac_range_lkup(int opt)
403 if (opt < 0 || opt >= 5)
404 return &range_unknown;
405 return dac_range_table[opt];
408 static const struct comedi_lrange *ai_range_lkup(int type, int opt)
413 &range_dt2801_ai_pgl_unipolar :
414 &range_dt2801_ai_pgl_bipolar;
416 return (opt) ? &range_unipolar10 : &range_bipolar10;
418 return &range_unipolar5;
420 return &range_unknown;
423 static int dt2801_error(struct comedi_device *dev, int stat)
427 dev_dbg(dev->class_dev, "timeout\n");
429 dev_dbg(dev->class_dev, "error %d\n", stat);
432 dev_dbg(dev->class_dev, "error status 0x%02x, resetting...\n", stat);
440 static int dt2801_ai_insn_read(struct comedi_device *dev,
441 struct comedi_subdevice *s,
442 struct comedi_insn *insn, unsigned int *data)
448 for (i = 0; i < insn->n; i++) {
449 dt2801_writecmd(dev, DT_C_READ_ADIM);
450 dt2801_writedata(dev, CR_RANGE(insn->chanspec));
451 dt2801_writedata(dev, CR_CHAN(insn->chanspec));
452 stat = dt2801_readdata2(dev, &d);
455 return dt2801_error(dev, stat);
463 static int dt2801_ao_insn_write(struct comedi_device *dev,
464 struct comedi_subdevice *s,
465 struct comedi_insn *insn,
468 unsigned int chan = CR_CHAN(insn->chanspec);
470 dt2801_writecmd(dev, DT_C_WRITE_DAIM);
471 dt2801_writedata(dev, chan);
472 dt2801_writedata2(dev, data[0]);
474 s->readback[chan] = data[0];
479 static int dt2801_dio_insn_bits(struct comedi_device *dev,
480 struct comedi_subdevice *s,
481 struct comedi_insn *insn,
484 int which = (s == &dev->subdevices[3]) ? 1 : 0;
485 unsigned int val = 0;
487 if (comedi_dio_update_state(s, data)) {
488 dt2801_writecmd(dev, DT_C_WRITE_DIG);
489 dt2801_writedata(dev, which);
490 dt2801_writedata(dev, s->state);
493 dt2801_writecmd(dev, DT_C_READ_DIG);
494 dt2801_writedata(dev, which);
495 dt2801_readdata(dev, &val);
502 static int dt2801_dio_insn_config(struct comedi_device *dev,
503 struct comedi_subdevice *s,
504 struct comedi_insn *insn,
509 ret = comedi_dio_insn_config(dev, s, insn, data, 0xff);
513 dt2801_writecmd(dev, s->io_bits ? DT_C_SET_DIGOUT : DT_C_SET_DIGIN);
514 dt2801_writedata(dev, (s == &dev->subdevices[3]) ? 1 : 0);
523 [2] - a/d 0=differential, 1=single-ended
524 [3] - a/d range 0=[-10,10], 1=[0,10]
525 [4] - dac0 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
526 [5] - dac1 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
528 static int dt2801_attach(struct comedi_device *dev, struct comedi_devconfig *it)
530 const struct dt2801_board *board;
531 struct dt2801_private *devpriv;
532 struct comedi_subdevice *s;
533 int board_code, type;
537 ret = comedi_request_region(dev, it->options[0], 0x2);
541 /* do some checking */
543 board_code = dt2801_reset(dev);
545 /* heh. if it didn't work, try it again. */
547 board_code = dt2801_reset(dev);
549 for (type = 0; type < ARRAY_SIZE(boardtypes); type++) {
550 if (boardtypes[type].boardcode == board_code)
553 dev_dbg(dev->class_dev,
554 "unrecognized board code=0x%02x, contact author\n", board_code);
558 dev->board_ptr = boardtypes + type;
559 board = dev->board_ptr;
561 n_ai_chans = probe_number_of_ai_chans(dev);
563 ret = comedi_alloc_subdevices(dev, 4);
567 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
571 dev->board_name = board->name;
573 s = &dev->subdevices[0];
575 s->type = COMEDI_SUBD_AI;
576 s->subdev_flags = SDF_READABLE | SDF_GROUND;
578 s->n_chan = n_ai_chans;
581 s->n_chan = board->ad_chan;
583 s->n_chan = board->ad_chan / 2;
585 s->maxdata = (1 << board->adbits) - 1;
586 s->range_table = ai_range_lkup(board->adrangetype, it->options[3]);
587 s->insn_read = dt2801_ai_insn_read;
589 s = &dev->subdevices[1];
591 s->type = COMEDI_SUBD_AO;
592 s->subdev_flags = SDF_WRITABLE;
594 s->maxdata = (1 << board->dabits) - 1;
595 s->range_table_list = devpriv->dac_range_types;
596 devpriv->dac_range_types[0] = dac_range_lkup(it->options[4]);
597 devpriv->dac_range_types[1] = dac_range_lkup(it->options[5]);
598 s->insn_write = dt2801_ao_insn_write;
600 ret = comedi_alloc_subdev_readback(s);
604 s = &dev->subdevices[2];
605 /* 1st digital subdevice */
606 s->type = COMEDI_SUBD_DIO;
607 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
610 s->range_table = &range_digital;
611 s->insn_bits = dt2801_dio_insn_bits;
612 s->insn_config = dt2801_dio_insn_config;
614 s = &dev->subdevices[3];
615 /* 2nd digital subdevice */
616 s->type = COMEDI_SUBD_DIO;
617 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
620 s->range_table = &range_digital;
621 s->insn_bits = dt2801_dio_insn_bits;
622 s->insn_config = dt2801_dio_insn_config;
629 static struct comedi_driver dt2801_driver = {
630 .driver_name = "dt2801",
631 .module = THIS_MODULE,
632 .attach = dt2801_attach,
633 .detach = comedi_legacy_detach,
635 module_comedi_driver(dt2801_driver);
637 MODULE_AUTHOR("Comedi http://www.comedi.org");
638 MODULE_DESCRIPTION("Comedi low-level driver");
639 MODULE_LICENSE("GPL");