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
69 * Command modifiers (only used with read/write), EXTTRIG can be
70 * used with some other commands.
72 #define DT_MOD_DMA BIT(4)
73 #define DT_MOD_CONT BIT(5)
74 #define DT_MOD_EXTCLK BIT(6)
75 #define DT_MOD_EXTTRIG BIT(7)
77 /* Bits in status register */
78 #define DT_S_DATA_OUT_READY BIT(0)
79 #define DT_S_DATA_IN_FULL BIT(1)
80 #define DT_S_READY BIT(2)
81 #define DT_S_COMMAND BIT(3)
82 #define DT_S_COMPOSITE_ERROR BIT(7)
86 #define DT2801_STATUS 1
90 /* ignore 'defined but not used' warning */
91 static const struct comedi_lrange range_dt2801_ai_pgh_bipolar = {
100 static const struct comedi_lrange range_dt2801_ai_pgl_bipolar = {
110 /* ignore 'defined but not used' warning */
111 static const struct comedi_lrange range_dt2801_ai_pgh_unipolar = {
120 static const struct comedi_lrange range_dt2801_ai_pgl_unipolar = {
129 struct dt2801_board {
140 * Typeid's for the different boards of the DT2801-series
141 * (taken from the test-software, that comes with the board)
143 static const struct dt2801_board boardtypes[] = {
161 .name = "dt2801/5716a",
177 .name = "dt2805/5716a",
210 struct dt2801_private {
211 const struct comedi_lrange *dac_range_types[2];
215 * These are the low-level routines:
216 * writecommand: write a command to the board
217 * writedata: write data byte
218 * readdata: read data byte
222 * Only checks DataOutReady-flag, not the Ready-flag as it is done
223 * in the examples of the manual. I don't see why this should be
226 static int dt2801_readdata(struct comedi_device *dev, int *data)
229 int timeout = DT2801_TIMEOUT;
232 stat = inb_p(dev->iobase + DT2801_STATUS);
233 if (stat & (DT_S_COMPOSITE_ERROR | DT_S_READY))
235 if (stat & DT_S_DATA_OUT_READY) {
236 *data = inb_p(dev->iobase + DT2801_DATA);
239 } while (--timeout > 0);
244 static int dt2801_readdata2(struct comedi_device *dev, int *data)
250 ret = dt2801_readdata(dev, &lb);
253 ret = dt2801_readdata(dev, &hb);
257 *data = (hb << 8) + lb;
261 static int dt2801_writedata(struct comedi_device *dev, unsigned int data)
264 int timeout = DT2801_TIMEOUT;
267 stat = inb_p(dev->iobase + DT2801_STATUS);
269 if (stat & DT_S_COMPOSITE_ERROR)
271 if (!(stat & DT_S_DATA_IN_FULL)) {
272 outb_p(data & 0xff, dev->iobase + DT2801_DATA);
275 } while (--timeout > 0);
280 static int dt2801_writedata2(struct comedi_device *dev, unsigned int data)
284 ret = dt2801_writedata(dev, data & 0xff);
287 ret = dt2801_writedata(dev, data >> 8);
294 static int dt2801_wait_for_ready(struct comedi_device *dev)
296 int timeout = DT2801_TIMEOUT;
299 stat = inb_p(dev->iobase + DT2801_STATUS);
300 if (stat & DT_S_READY)
303 stat = inb_p(dev->iobase + DT2801_STATUS);
305 if (stat & DT_S_COMPOSITE_ERROR)
307 if (stat & DT_S_READY)
309 } while (--timeout > 0);
314 static void dt2801_writecmd(struct comedi_device *dev, int command)
318 dt2801_wait_for_ready(dev);
320 stat = inb_p(dev->iobase + DT2801_STATUS);
321 if (stat & DT_S_COMPOSITE_ERROR) {
322 dev_dbg(dev->class_dev,
323 "composite-error in %s, ignoring\n", __func__);
325 if (!(stat & DT_S_READY))
326 dev_dbg(dev->class_dev, "!ready in %s, ignoring\n", __func__);
327 outb_p(command, dev->iobase + DT2801_CMD);
330 static int dt2801_reset(struct comedi_device *dev)
336 /* pull random data from data port */
337 inb_p(dev->iobase + DT2801_DATA);
338 inb_p(dev->iobase + DT2801_DATA);
339 inb_p(dev->iobase + DT2801_DATA);
340 inb_p(dev->iobase + DT2801_DATA);
342 /* dt2801_writecmd(dev,DT_C_STOP); */
343 outb_p(DT_C_STOP, dev->iobase + DT2801_CMD);
345 /* dt2801_wait_for_ready(dev); */
349 stat = inb_p(dev->iobase + DT2801_STATUS);
350 if (stat & DT_S_READY)
354 dev_dbg(dev->class_dev, "timeout 1 status=0x%02x\n", stat);
356 /* dt2801_readdata(dev,&board_code); */
358 outb_p(DT_C_RESET, dev->iobase + DT2801_CMD);
359 /* dt2801_writecmd(dev,DT_C_RESET); */
364 stat = inb_p(dev->iobase + DT2801_STATUS);
365 if (stat & DT_S_READY)
369 dev_dbg(dev->class_dev, "timeout 2 status=0x%02x\n", stat);
371 dt2801_readdata(dev, &board_code);
376 static int probe_number_of_ai_chans(struct comedi_device *dev)
382 for (n_chans = 0; n_chans < 16; n_chans++) {
383 dt2801_writecmd(dev, DT_C_READ_ADIM);
384 dt2801_writedata(dev, 0);
385 dt2801_writedata(dev, n_chans);
386 stat = dt2801_readdata2(dev, &data);
398 static const struct comedi_lrange *dac_range_table[] = {
406 static const struct comedi_lrange *dac_range_lkup(int opt)
408 if (opt < 0 || opt >= 5)
409 return &range_unknown;
410 return dac_range_table[opt];
413 static const struct comedi_lrange *ai_range_lkup(int type, int opt)
418 &range_dt2801_ai_pgl_unipolar :
419 &range_dt2801_ai_pgl_bipolar;
421 return (opt) ? &range_unipolar10 : &range_bipolar10;
423 return &range_unipolar5;
425 return &range_unknown;
428 static int dt2801_error(struct comedi_device *dev, int stat)
432 dev_dbg(dev->class_dev, "timeout\n");
434 dev_dbg(dev->class_dev, "error %d\n", stat);
437 dev_dbg(dev->class_dev, "error status 0x%02x, resetting...\n", stat);
445 static int dt2801_ai_insn_read(struct comedi_device *dev,
446 struct comedi_subdevice *s,
447 struct comedi_insn *insn, unsigned int *data)
453 for (i = 0; i < insn->n; i++) {
454 dt2801_writecmd(dev, DT_C_READ_ADIM);
455 dt2801_writedata(dev, CR_RANGE(insn->chanspec));
456 dt2801_writedata(dev, CR_CHAN(insn->chanspec));
457 stat = dt2801_readdata2(dev, &d);
460 return dt2801_error(dev, stat);
468 static int dt2801_ao_insn_write(struct comedi_device *dev,
469 struct comedi_subdevice *s,
470 struct comedi_insn *insn,
473 unsigned int chan = CR_CHAN(insn->chanspec);
475 dt2801_writecmd(dev, DT_C_WRITE_DAIM);
476 dt2801_writedata(dev, chan);
477 dt2801_writedata2(dev, data[0]);
479 s->readback[chan] = data[0];
484 static int dt2801_dio_insn_bits(struct comedi_device *dev,
485 struct comedi_subdevice *s,
486 struct comedi_insn *insn,
489 int which = (s == &dev->subdevices[3]) ? 1 : 0;
490 unsigned int val = 0;
492 if (comedi_dio_update_state(s, data)) {
493 dt2801_writecmd(dev, DT_C_WRITE_DIG);
494 dt2801_writedata(dev, which);
495 dt2801_writedata(dev, s->state);
498 dt2801_writecmd(dev, DT_C_READ_DIG);
499 dt2801_writedata(dev, which);
500 dt2801_readdata(dev, &val);
507 static int dt2801_dio_insn_config(struct comedi_device *dev,
508 struct comedi_subdevice *s,
509 struct comedi_insn *insn,
514 ret = comedi_dio_insn_config(dev, s, insn, data, 0xff);
518 dt2801_writecmd(dev, s->io_bits ? DT_C_SET_DIGOUT : DT_C_SET_DIGIN);
519 dt2801_writedata(dev, (s == &dev->subdevices[3]) ? 1 : 0);
528 * [2] - a/d 0=differential, 1=single-ended
529 * [3] - a/d range 0=[-10,10], 1=[0,10]
530 * [4] - dac0 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
531 * [5] - dac1 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
533 static int dt2801_attach(struct comedi_device *dev, struct comedi_devconfig *it)
535 const struct dt2801_board *board;
536 struct dt2801_private *devpriv;
537 struct comedi_subdevice *s;
538 int board_code, type;
542 ret = comedi_request_region(dev, it->options[0], 0x2);
546 /* do some checking */
548 board_code = dt2801_reset(dev);
550 /* heh. if it didn't work, try it again. */
552 board_code = dt2801_reset(dev);
554 for (type = 0; type < ARRAY_SIZE(boardtypes); type++) {
555 if (boardtypes[type].boardcode == board_code)
558 dev_dbg(dev->class_dev,
559 "unrecognized board code=0x%02x, contact author\n", board_code);
563 dev->board_ptr = boardtypes + type;
564 board = dev->board_ptr;
566 n_ai_chans = probe_number_of_ai_chans(dev);
568 ret = comedi_alloc_subdevices(dev, 4);
572 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
576 dev->board_name = board->name;
578 s = &dev->subdevices[0];
580 s->type = COMEDI_SUBD_AI;
581 s->subdev_flags = SDF_READABLE | SDF_GROUND;
583 s->n_chan = n_ai_chans;
586 s->n_chan = board->ad_chan;
588 s->n_chan = board->ad_chan / 2;
590 s->maxdata = (1 << board->adbits) - 1;
591 s->range_table = ai_range_lkup(board->adrangetype, it->options[3]);
592 s->insn_read = dt2801_ai_insn_read;
594 s = &dev->subdevices[1];
596 s->type = COMEDI_SUBD_AO;
597 s->subdev_flags = SDF_WRITABLE;
599 s->maxdata = (1 << board->dabits) - 1;
600 s->range_table_list = devpriv->dac_range_types;
601 devpriv->dac_range_types[0] = dac_range_lkup(it->options[4]);
602 devpriv->dac_range_types[1] = dac_range_lkup(it->options[5]);
603 s->insn_write = dt2801_ao_insn_write;
605 ret = comedi_alloc_subdev_readback(s);
609 s = &dev->subdevices[2];
610 /* 1st digital subdevice */
611 s->type = COMEDI_SUBD_DIO;
612 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
615 s->range_table = &range_digital;
616 s->insn_bits = dt2801_dio_insn_bits;
617 s->insn_config = dt2801_dio_insn_config;
619 s = &dev->subdevices[3];
620 /* 2nd digital subdevice */
621 s->type = COMEDI_SUBD_DIO;
622 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
625 s->range_table = &range_digital;
626 s->insn_bits = dt2801_dio_insn_bits;
627 s->insn_config = dt2801_dio_insn_config;
634 static struct comedi_driver dt2801_driver = {
635 .driver_name = "dt2801",
636 .module = THIS_MODULE,
637 .attach = dt2801_attach,
638 .detach = comedi_legacy_detach,
640 module_comedi_driver(dt2801_driver);
642 MODULE_AUTHOR("Comedi http://www.comedi.org");
643 MODULE_DESCRIPTION("Comedi low-level driver");
644 MODULE_LICENSE("GPL");