RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / comedi / drivers / dt2801.c
blob7d1141daf3f83a45dfcd48c00cadc0873e2b5acb
1 /*
2 * comedi/drivers/dt2801.c
3 * Device Driver for DataTranslation DT2801
5 */
6 /*
7 Driver: dt2801
8 Description: Data Translation DT2801 series and DT01-EZ
9 Author: ds
10 Status: works
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
18 [1] - unused
19 [2] - A/D reference 0=differential, 1=single-ended
20 [3] - A/D range
21 0 = [-10, 10]
22 1 = [0,10]
23 [4] - D/A 0 range
24 0 = [-10, 10]
25 1 = [-5,5]
26 2 = [-2.5,2.5]
27 3 = [0,10]
28 4 = [0,5]
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)
43 /* Ports */
44 #define DT2801_IOSIZE 2
46 /* define's */
47 /* ====================== */
49 /* Commands */
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
55 #define DT_C_TEST 0xb
56 #define DT_C_STOP 0xf
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)
86 /* registers */
87 #define DT2801_DATA 0
88 #define DT2801_STATUS 1
89 #define DT2801_CMD 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 static int __init driver_dt2801_init_module(void)
103 return comedi_driver_register(&driver_dt2801);
106 static void __exit driver_dt2801_cleanup_module(void)
108 comedi_driver_unregister(&driver_dt2801);
111 module_init(driver_dt2801_init_module);
112 module_exit(driver_dt2801_cleanup_module);
114 static const struct comedi_lrange range_dt2801_ai_pgl_bipolar = { 4, {
115 RANGE(-10,
116 10),
117 RANGE(-1,
119 RANGE
120 (-0.1,
121 0.1),
122 RANGE
123 (-0.02,
124 0.02),
128 static const struct comedi_lrange range_dt2801_ai_pgl_unipolar = { 4, {
129 RANGE(0,
130 10),
131 RANGE(0,
133 RANGE(0,
134 0.1),
135 RANGE(0,
136 0.02),
140 struct dt2801_board {
142 const char *name;
143 int boardcode;
144 int ad_diff;
145 int ad_chan;
146 int adbits;
147 int adrangetype;
148 int dabits;
151 /* Typeid's for the different boards of the DT2801-series
152 (taken from the test-software, that comes with the board)
154 static const struct dt2801_board boardtypes[] = {
156 .name = "dt2801",
157 .boardcode = 0x09,
158 .ad_diff = 2,
159 .ad_chan = 16,
160 .adbits = 12,
161 .adrangetype = 0,
162 .dabits = 12},
164 .name = "dt2801-a",
165 .boardcode = 0x52,
166 .ad_diff = 2,
167 .ad_chan = 16,
168 .adbits = 12,
169 .adrangetype = 0,
170 .dabits = 12},
172 .name = "dt2801/5716a",
173 .boardcode = 0x82,
174 .ad_diff = 1,
175 .ad_chan = 16,
176 .adbits = 16,
177 .adrangetype = 1,
178 .dabits = 12},
180 .name = "dt2805",
181 .boardcode = 0x12,
182 .ad_diff = 1,
183 .ad_chan = 16,
184 .adbits = 12,
185 .adrangetype = 0,
186 .dabits = 12},
188 .name = "dt2805/5716a",
189 .boardcode = 0x92,
190 .ad_diff = 1,
191 .ad_chan = 16,
192 .adbits = 16,
193 .adrangetype = 1,
194 .dabits = 12},
196 .name = "dt2808",
197 .boardcode = 0x20,
198 .ad_diff = 0,
199 .ad_chan = 16,
200 .adbits = 12,
201 .adrangetype = 2,
202 .dabits = 8},
204 .name = "dt2818",
205 .boardcode = 0xa2,
206 .ad_diff = 0,
207 .ad_chan = 4,
208 .adbits = 12,
209 .adrangetype = 0,
210 .dabits = 12},
212 .name = "dt2809",
213 .boardcode = 0xb0,
214 .ad_diff = 0,
215 .ad_chan = 8,
216 .adbits = 12,
217 .adrangetype = 1,
218 .dabits = 12},
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,
232 struct comedi_subdevice *s,
233 struct comedi_insn *insn, unsigned int *data);
234 static int dt2801_ao_insn_read(struct comedi_device *dev,
235 struct comedi_subdevice *s,
236 struct comedi_insn *insn, unsigned int *data);
237 static int dt2801_ao_insn_write(struct comedi_device *dev,
238 struct comedi_subdevice *s,
239 struct comedi_insn *insn, unsigned int *data);
240 static int dt2801_dio_insn_bits(struct comedi_device *dev,
241 struct comedi_subdevice *s,
242 struct comedi_insn *insn, unsigned int *data);
243 static int dt2801_dio_insn_config(struct comedi_device *dev,
244 struct comedi_subdevice *s,
245 struct comedi_insn *insn, unsigned int *data);
247 /* These are the low-level routines:
248 writecommand: write a command to the board
249 writedata: write data byte
250 readdata: read data byte
253 /* Only checks DataOutReady-flag, not the Ready-flag as it is done
254 in the examples of the manual. I don't see why this should be
255 necessary. */
256 static int dt2801_readdata(struct comedi_device *dev, int *data)
258 int stat = 0;
259 int timeout = DT2801_TIMEOUT;
261 do {
262 stat = inb_p(dev->iobase + DT2801_STATUS);
263 if (stat & (DT_S_COMPOSITE_ERROR | DT_S_READY))
264 return stat;
265 if (stat & DT_S_DATA_OUT_READY) {
266 *data = inb_p(dev->iobase + DT2801_DATA);
267 return 0;
269 } while (--timeout > 0);
271 return -ETIME;
274 static int dt2801_readdata2(struct comedi_device *dev, int *data)
276 int lb, hb;
277 int ret;
279 ret = dt2801_readdata(dev, &lb);
280 if (ret)
281 return ret;
282 ret = dt2801_readdata(dev, &hb);
283 if (ret)
284 return ret;
286 *data = (hb << 8) + lb;
287 return 0;
290 static int dt2801_writedata(struct comedi_device *dev, unsigned int data)
292 int stat = 0;
293 int timeout = DT2801_TIMEOUT;
295 do {
296 stat = inb_p(dev->iobase + DT2801_STATUS);
298 if (stat & DT_S_COMPOSITE_ERROR)
299 return stat;
300 if (!(stat & DT_S_DATA_IN_FULL)) {
301 outb_p(data & 0xff, dev->iobase + DT2801_DATA);
302 return 0;
304 } while (--timeout > 0);
306 return -ETIME;
309 static int dt2801_writedata2(struct comedi_device *dev, unsigned int data)
311 int ret;
313 ret = dt2801_writedata(dev, data & 0xff);
314 if (ret < 0)
315 return ret;
316 ret = dt2801_writedata(dev, (data >> 8));
317 if (ret < 0)
318 return ret;
320 return 0;
323 static int dt2801_wait_for_ready(struct comedi_device *dev)
325 int timeout = DT2801_TIMEOUT;
326 int stat;
328 stat = inb_p(dev->iobase + DT2801_STATUS);
329 if (stat & DT_S_READY)
330 return 0;
331 do {
332 stat = inb_p(dev->iobase + DT2801_STATUS);
334 if (stat & DT_S_COMPOSITE_ERROR)
335 return stat;
336 if (stat & DT_S_READY)
337 return 0;
338 } while (--timeout > 0);
340 return -ETIME;
343 static int dt2801_writecmd(struct comedi_device *dev, int command)
345 int stat;
347 dt2801_wait_for_ready(dev);
349 stat = inb_p(dev->iobase + DT2801_STATUS);
350 if (stat & DT_S_COMPOSITE_ERROR) {
351 printk
352 ("dt2801: composite-error in dt2801_writecmd(), ignoring\n");
354 if (!(stat & DT_S_READY))
355 printk("dt2801: !ready in dt2801_writecmd(), ignoring\n");
356 outb_p(command, dev->iobase + DT2801_CMD);
358 return 0;
361 static int dt2801_reset(struct comedi_device *dev)
363 int board_code = 0;
364 unsigned int stat;
365 int timeout;
367 DPRINTK("dt2801: resetting board...\n");
368 DPRINTK("fingerprint: 0x%02x 0x%02x\n", inb_p(dev->iobase),
369 inb_p(dev->iobase + 1));
371 /* pull random data from data port */
372 inb_p(dev->iobase + DT2801_DATA);
373 inb_p(dev->iobase + DT2801_DATA);
374 inb_p(dev->iobase + DT2801_DATA);
375 inb_p(dev->iobase + DT2801_DATA);
377 DPRINTK("dt2801: stop\n");
378 /* dt2801_writecmd(dev,DT_C_STOP); */
379 outb_p(DT_C_STOP, dev->iobase + DT2801_CMD);
381 /* dt2801_wait_for_ready(dev); */
382 udelay(100);
383 timeout = 10000;
384 do {
385 stat = inb_p(dev->iobase + DT2801_STATUS);
386 if (stat & DT_S_READY)
387 break;
388 } while (timeout--);
389 if (!timeout)
390 printk("dt2801: timeout 1 status=0x%02x\n", stat);
392 /* printk("dt2801: reading dummy\n"); */
393 /* dt2801_readdata(dev,&board_code); */
395 DPRINTK("dt2801: reset\n");
396 outb_p(DT_C_RESET, dev->iobase + DT2801_CMD);
397 /* dt2801_writecmd(dev,DT_C_RESET); */
399 udelay(100);
400 timeout = 10000;
401 do {
402 stat = inb_p(dev->iobase + DT2801_STATUS);
403 if (stat & DT_S_READY)
404 break;
405 } while (timeout--);
406 if (!timeout)
407 printk("dt2801: timeout 2 status=0x%02x\n", stat);
409 DPRINTK("dt2801: reading code\n");
410 dt2801_readdata(dev, &board_code);
412 DPRINTK("dt2801: ok. code=0x%02x\n", board_code);
414 return board_code;
417 static int probe_number_of_ai_chans(struct comedi_device *dev)
419 int n_chans;
420 int stat;
421 int data;
423 for (n_chans = 0; n_chans < 16; n_chans++) {
424 stat = dt2801_writecmd(dev, DT_C_READ_ADIM);
425 dt2801_writedata(dev, 0);
426 dt2801_writedata(dev, n_chans);
427 stat = dt2801_readdata2(dev, &data);
429 if (stat)
430 break;
433 dt2801_reset(dev);
434 dt2801_reset(dev);
436 return n_chans;
439 static const struct comedi_lrange *dac_range_table[] = {
440 &range_bipolar10,
441 &range_bipolar5,
442 &range_bipolar2_5,
443 &range_unipolar10,
444 &range_unipolar5
447 static const struct comedi_lrange *dac_range_lkup(int opt)
449 if (opt < 0 || opt >= 5)
450 return &range_unknown;
451 return dac_range_table[opt];
454 static const struct comedi_lrange *ai_range_lkup(int type, int opt)
456 switch (type) {
457 case 0:
458 return (opt) ?
459 &range_dt2801_ai_pgl_unipolar :
460 &range_dt2801_ai_pgl_bipolar;
461 case 1:
462 return (opt) ? &range_unipolar10 : &range_bipolar10;
463 case 2:
464 return &range_unipolar5;
466 return &range_unknown;
470 options:
471 [0] - i/o base
472 [1] - unused
473 [2] - a/d 0=differential, 1=single-ended
474 [3] - a/d range 0=[-10,10], 1=[0,10]
475 [4] - dac0 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
476 [5] - dac1 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
478 static int dt2801_attach(struct comedi_device *dev, struct comedi_devconfig *it)
480 struct comedi_subdevice *s;
481 unsigned long iobase;
482 int board_code, type;
483 int ret = 0;
484 int n_ai_chans;
486 iobase = it->options[0];
487 if (!request_region(iobase, DT2801_IOSIZE, "dt2801")) {
488 comedi_error(dev, "I/O port conflict");
489 return -EIO;
491 dev->iobase = iobase;
493 /* do some checking */
495 board_code = dt2801_reset(dev);
497 /* heh. if it didn't work, try it again. */
498 if (!board_code)
499 board_code = dt2801_reset(dev);
501 for (type = 0; type < ARRAY_SIZE(boardtypes); type++) {
502 if (boardtypes[type].boardcode == board_code)
503 goto havetype;
505 printk("dt2801: unrecognized board code=0x%02x, contact author\n",
506 board_code);
507 type = 0;
509 havetype:
510 dev->board_ptr = boardtypes + type;
511 printk("dt2801: %s at port 0x%lx", boardtype.name, iobase);
513 n_ai_chans = probe_number_of_ai_chans(dev);
514 printk(" (ai channels = %d)", n_ai_chans);
516 ret = alloc_subdevices(dev, 4);
517 if (ret < 0)
518 goto out;
520 ret = alloc_private(dev, sizeof(struct dt2801_private));
521 if (ret < 0)
522 goto out;
524 dev->board_name = boardtype.name;
526 s = dev->subdevices + 0;
527 /* ai subdevice */
528 s->type = COMEDI_SUBD_AI;
529 s->subdev_flags = SDF_READABLE | SDF_GROUND;
530 s->n_chan = n_ai_chans;
531 s->maxdata = (1 << boardtype.adbits) - 1;
532 s->range_table = ai_range_lkup(boardtype.adrangetype, it->options[3]);
533 s->insn_read = dt2801_ai_insn_read;
535 s++;
536 /* ao subdevice */
537 s->type = COMEDI_SUBD_AO;
538 s->subdev_flags = SDF_WRITABLE;
539 s->n_chan = 2;
540 s->maxdata = (1 << boardtype.dabits) - 1;
541 s->range_table_list = devpriv->dac_range_types;
542 devpriv->dac_range_types[0] = dac_range_lkup(it->options[4]);
543 devpriv->dac_range_types[1] = dac_range_lkup(it->options[5]);
544 s->insn_read = dt2801_ao_insn_read;
545 s->insn_write = dt2801_ao_insn_write;
547 s++;
548 /* 1st digital subdevice */
549 s->type = COMEDI_SUBD_DIO;
550 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
551 s->n_chan = 8;
552 s->maxdata = 1;
553 s->range_table = &range_digital;
554 s->insn_bits = dt2801_dio_insn_bits;
555 s->insn_config = dt2801_dio_insn_config;
557 s++;
558 /* 2nd digital subdevice */
559 s->type = COMEDI_SUBD_DIO;
560 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
561 s->n_chan = 8;
562 s->maxdata = 1;
563 s->range_table = &range_digital;
564 s->insn_bits = dt2801_dio_insn_bits;
565 s->insn_config = dt2801_dio_insn_config;
567 ret = 0;
568 out:
569 printk("\n");
571 return ret;
574 static int dt2801_detach(struct comedi_device *dev)
576 if (dev->iobase)
577 release_region(dev->iobase, DT2801_IOSIZE);
579 return 0;
582 static int dt2801_error(struct comedi_device *dev, int stat)
584 if (stat < 0) {
585 if (stat == -ETIME)
586 printk("dt2801: timeout\n");
587 else
588 printk("dt2801: error %d\n", stat);
589 return stat;
591 printk("dt2801: error status 0x%02x, resetting...\n", stat);
593 dt2801_reset(dev);
594 dt2801_reset(dev);
596 return -EIO;
599 static int dt2801_ai_insn_read(struct comedi_device *dev,
600 struct comedi_subdevice *s,
601 struct comedi_insn *insn, unsigned int *data)
603 int d;
604 int stat;
605 int i;
607 for (i = 0; i < insn->n; i++) {
608 stat = dt2801_writecmd(dev, DT_C_READ_ADIM);
609 dt2801_writedata(dev, CR_RANGE(insn->chanspec));
610 dt2801_writedata(dev, CR_CHAN(insn->chanspec));
611 stat = dt2801_readdata2(dev, &d);
613 if (stat != 0)
614 return dt2801_error(dev, stat);
616 data[i] = d;
619 return i;
622 static int dt2801_ao_insn_read(struct comedi_device *dev,
623 struct comedi_subdevice *s,
624 struct comedi_insn *insn, unsigned int *data)
626 data[0] = devpriv->ao_readback[CR_CHAN(insn->chanspec)];
628 return 1;
631 static int dt2801_ao_insn_write(struct comedi_device *dev,
632 struct comedi_subdevice *s,
633 struct comedi_insn *insn, unsigned int *data)
635 dt2801_writecmd(dev, DT_C_WRITE_DAIM);
636 dt2801_writedata(dev, CR_CHAN(insn->chanspec));
637 dt2801_writedata2(dev, data[0]);
639 devpriv->ao_readback[CR_CHAN(insn->chanspec)] = data[0];
641 return 1;
644 static int dt2801_dio_insn_bits(struct comedi_device *dev,
645 struct comedi_subdevice *s,
646 struct comedi_insn *insn, unsigned int *data)
648 int which = 0;
650 if (s == dev->subdevices + 4)
651 which = 1;
653 if (insn->n != 2)
654 return -EINVAL;
655 if (data[0]) {
656 s->state &= ~data[0];
657 s->state |= (data[0] & data[1]);
658 dt2801_writecmd(dev, DT_C_WRITE_DIG);
659 dt2801_writedata(dev, which);
660 dt2801_writedata(dev, s->state);
662 dt2801_writecmd(dev, DT_C_READ_DIG);
663 dt2801_writedata(dev, which);
664 dt2801_readdata(dev, data + 1);
666 return 2;
669 static int dt2801_dio_insn_config(struct comedi_device *dev,
670 struct comedi_subdevice *s,
671 struct comedi_insn *insn, unsigned int *data)
673 int which = 0;
675 if (s == dev->subdevices + 4)
676 which = 1;
678 /* configure */
679 if (data[0]) {
680 s->io_bits = 0xff;
681 dt2801_writecmd(dev, DT_C_SET_DIGOUT);
682 } else {
683 s->io_bits = 0;
684 dt2801_writecmd(dev, DT_C_SET_DIGIN);
686 dt2801_writedata(dev, which);
688 return 1;
691 MODULE_AUTHOR("Comedi http://www.comedi.org");
692 MODULE_DESCRIPTION("Comedi low-level driver");
693 MODULE_LICENSE("GPL");