dt2801.c revision d438a1795754030b9b389d7a64981907a1869dc7
1/*
2 * comedi/drivers/dt2801.c
3 * Device Driver for DataTranslation DT2801
4 *
5 */
6/*
7Driver: dt2801
8Description: Data Translation DT2801 series and DT01-EZ
9Author: ds
10Status: works
11Devices: [Data Translation] DT2801 (dt2801), DT2801-A, DT2801/5716A,
12  DT2805, DT2805/5716A, DT2808, DT2818, DT2809, DT01-EZ
13
14This driver can autoprobe the type of board.
15
16Configuration 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)
30*/
31
32#include "../comedidev.h"
33#include <linux/delay.h>
34#include <linux/ioport.h>
35
36#define DT2801_TIMEOUT 1000
37
38/* Hardware Configuration */
39/* ====================== */
40
41#define DT2801_MAX_DMA_SIZE (64 * 1024)
42
43/* Ports */
44#define DT2801_IOSIZE 2
45
46/* define's & typedef's */
47/* ====================== */
48
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
54
55#define DT_C_TEST        0xb
56#define DT_C_STOP        0xf
57
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
62
63#define DT_C_WRITE_DAIM  0x8
64#define DT_C_SET_DA      0x9
65#define DT_C_WRITE_DA    0xa
66
67#define DT_C_READ_ADIM   0xc
68#define DT_C_SET_AD      0xd
69#define DT_C_READ_AD     0xe
70
71/* Command modifiers (only used with read/write), EXTTRIG can be
72   used with some other commands.
73*/
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)
78
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)
85
86/* registers */
87#define DT2801_DATA		0
88#define DT2801_STATUS		1
89#define DT2801_CMD		1
90
91static int dt2801_attach(struct comedi_device * dev, struct comedi_devconfig * it);
92static int dt2801_detach(struct comedi_device * dev);
93static struct comedi_driver driver_dt2801 = {
94      driver_name:"dt2801",
95      module:THIS_MODULE,
96      attach:dt2801_attach,
97      detach:dt2801_detach,
98};
99
100COMEDI_INITCLEANUP(driver_dt2801);
101
102#if 0
103// ignore 'defined but not used' warning
104static const struct comedi_lrange range_dt2801_ai_pgh_bipolar = { 4, {
105			RANGE(-10, 10),
106			RANGE(-5, 5),
107			RANGE(-2.5, 2.5),
108			RANGE(-1.25, 1.25),
109	}
110};
111#endif
112static const struct comedi_lrange range_dt2801_ai_pgl_bipolar = { 4, {
113			RANGE(-10, 10),
114			RANGE(-1, 1),
115			RANGE(-0.1, 0.1),
116			RANGE(-0.02, 0.02),
117	}
118};
119
120#if 0
121// ignore 'defined but not used' warning
122static const struct comedi_lrange range_dt2801_ai_pgh_unipolar = { 4, {
123			RANGE(0, 10),
124			RANGE(0, 5),
125			RANGE(0, 2.5),
126			RANGE(0, 1.25),
127	}
128};
129#endif
130static const struct comedi_lrange range_dt2801_ai_pgl_unipolar = { 4, {
131			RANGE(0, 10),
132			RANGE(0, 1),
133			RANGE(0, 0.1),
134			RANGE(0, 0.02),
135	}
136};
137
138struct dt2801_board {
139
140	const char *name;
141	int boardcode;
142	int ad_diff;
143	int ad_chan;
144	int adbits;
145	int adrangetype;
146	int dabits;
147};
148
149
150/* Typeid's for the different boards of the DT2801-series
151   (taken from the test-software, that comes with the board)
152   */
153static const struct dt2801_board boardtypes[] = {
154	{
155	      name:	"dt2801",
156	      boardcode:0x09,
157	      ad_diff:	2,
158	      ad_chan:	16,
159	      adbits:	12,
160	      adrangetype:0,
161      dabits:	12},
162	{
163	      name:	"dt2801-a",
164	      boardcode:0x52,
165	      ad_diff:	2,
166	      ad_chan:	16,
167	      adbits:	12,
168	      adrangetype:0,
169      dabits:	12},
170	{
171	      name:	"dt2801/5716a",
172	      boardcode:0x82,
173	      ad_diff:	1,
174	      ad_chan:	16,
175	      adbits:	16,
176	      adrangetype:1,
177      dabits:	12},
178	{
179	      name:	"dt2805",
180	      boardcode:0x12,
181	      ad_diff:	1,
182	      ad_chan:	16,
183	      adbits:	12,
184	      adrangetype:0,
185      dabits:	12},
186	{
187	      name:	"dt2805/5716a",
188	      boardcode:0x92,
189	      ad_diff:	1,
190	      ad_chan:	16,
191	      adbits:	16,
192	      adrangetype:1,
193      dabits:	12},
194	{
195	      name:	"dt2808",
196	      boardcode:0x20,
197	      ad_diff:	0,
198	      ad_chan:	16,
199	      adbits:	12,
200	      adrangetype:2,
201      dabits:	8},
202	{
203	      name:	"dt2818",
204	      boardcode:0xa2,
205	      ad_diff:	0,
206	      ad_chan:	4,
207	      adbits:	12,
208	      adrangetype:0,
209      dabits:	12},
210	{
211	      name:	"dt2809",
212	      boardcode:0xb0,
213	      ad_diff:	0,
214	      ad_chan:	8,
215	      adbits:	12,
216	      adrangetype:1,
217      dabits:	12},
218};
219
220#define n_boardtypes ((sizeof(boardtypes))/(sizeof(boardtypes[0])))
221#define boardtype (*(const struct dt2801_board *)dev->board_ptr)
222
223typedef struct {
224	const struct comedi_lrange *dac_range_types[2];
225	unsigned int ao_readback[2];
226} dt2801_private;
227#define devpriv ((dt2801_private *)dev->private)
228
229static int dt2801_ai_insn_read(struct comedi_device * dev, struct comedi_subdevice * s,
230	struct comedi_insn * insn, unsigned int * data);
231static int dt2801_ao_insn_read(struct comedi_device * dev, struct comedi_subdevice * s,
232	struct comedi_insn * insn, unsigned int * data);
233static int dt2801_ao_insn_write(struct comedi_device * dev, struct comedi_subdevice * s,
234	struct comedi_insn * insn, unsigned int * data);
235static int dt2801_dio_insn_bits(struct comedi_device * dev, struct comedi_subdevice * s,
236	struct comedi_insn * insn, unsigned int * data);
237static int dt2801_dio_insn_config(struct comedi_device * dev, struct comedi_subdevice * s,
238	struct comedi_insn * insn, unsigned int * data);
239
240/* These are the low-level routines:
241   writecommand: write a command to the board
242   writedata: write data byte
243   readdata: read data byte
244 */
245
246/* Only checks DataOutReady-flag, not the Ready-flag as it is done
247   in the examples of the manual. I don't see why this should be
248   necessary. */
249static int dt2801_readdata(struct comedi_device * dev, int *data)
250{
251	int stat = 0;
252	int timeout = DT2801_TIMEOUT;
253
254	do {
255		stat = inb_p(dev->iobase + DT2801_STATUS);
256		if (stat & (DT_S_COMPOSITE_ERROR | DT_S_READY)) {
257			return stat;
258		}
259		if (stat & DT_S_DATA_OUT_READY) {
260			*data = inb_p(dev->iobase + DT2801_DATA);
261			return 0;
262		}
263	} while (--timeout > 0);
264
265	return -ETIME;
266}
267
268static int dt2801_readdata2(struct comedi_device * dev, int *data)
269{
270	int lb, hb;
271	int ret;
272
273	ret = dt2801_readdata(dev, &lb);
274	if (ret)
275		return ret;
276	ret = dt2801_readdata(dev, &hb);
277	if (ret)
278		return ret;
279
280	*data = (hb << 8) + lb;
281	return 0;
282}
283
284static int dt2801_writedata(struct comedi_device * dev, unsigned int data)
285{
286	int stat = 0;
287	int timeout = DT2801_TIMEOUT;
288
289	do {
290		stat = inb_p(dev->iobase + DT2801_STATUS);
291
292		if (stat & DT_S_COMPOSITE_ERROR) {
293			return stat;
294		}
295		if (!(stat & DT_S_DATA_IN_FULL)) {
296			outb_p(data & 0xff, dev->iobase + DT2801_DATA);
297			return 0;
298		}
299#if 0
300		if (stat & DT_S_READY) {
301			printk("dt2801: ready flag set (bad!) in dt2801_writedata()\n");
302			return -EIO;
303		}
304#endif
305	} while (--timeout > 0);
306
307	return -ETIME;
308}
309
310static int dt2801_writedata2(struct comedi_device * dev, unsigned int data)
311{
312	int ret;
313
314	ret = dt2801_writedata(dev, data & 0xff);
315	if (ret < 0)
316		return ret;
317	ret = dt2801_writedata(dev, (data >> 8));
318	if (ret < 0)
319		return ret;
320
321	return 0;
322}
323
324static int dt2801_wait_for_ready(struct comedi_device * dev)
325{
326	int timeout = DT2801_TIMEOUT;
327	int stat;
328
329	stat = inb_p(dev->iobase + DT2801_STATUS);
330	if (stat & DT_S_READY) {
331		return 0;
332	}
333	do {
334		stat = inb_p(dev->iobase + DT2801_STATUS);
335
336		if (stat & DT_S_COMPOSITE_ERROR) {
337			return stat;
338		}
339		if (stat & DT_S_READY) {
340			return 0;
341		}
342	} while (--timeout > 0);
343
344	return -ETIME;
345}
346
347static int dt2801_writecmd(struct comedi_device * dev, int command)
348{
349	int stat;
350
351	dt2801_wait_for_ready(dev);
352
353	stat = inb_p(dev->iobase + DT2801_STATUS);
354	if (stat & DT_S_COMPOSITE_ERROR) {
355		printk("dt2801: composite-error in dt2801_writecmd(), ignoring\n");
356	}
357	if (!(stat & DT_S_READY)) {
358		printk("dt2801: !ready in dt2801_writecmd(), ignoring\n");
359	}
360	outb_p(command, dev->iobase + DT2801_CMD);
361
362	return 0;
363}
364
365static int dt2801_reset(struct comedi_device * dev)
366{
367	int board_code = 0;
368	unsigned int stat;
369	int timeout;
370
371	DPRINTK("dt2801: resetting board...\n");
372	DPRINTK("fingerprint: 0x%02x 0x%02x\n", inb_p(dev->iobase),
373		inb_p(dev->iobase + 1));
374
375	/* pull random data from data port */
376	inb_p(dev->iobase + DT2801_DATA);
377	inb_p(dev->iobase + DT2801_DATA);
378	inb_p(dev->iobase + DT2801_DATA);
379	inb_p(dev->iobase + DT2801_DATA);
380
381	DPRINTK("dt2801: stop\n");
382	//dt2801_writecmd(dev,DT_C_STOP);
383	outb_p(DT_C_STOP, dev->iobase + DT2801_CMD);
384
385	//dt2801_wait_for_ready(dev);
386	comedi_udelay(100);
387	timeout = 10000;
388	do {
389		stat = inb_p(dev->iobase + DT2801_STATUS);
390		if (stat & DT_S_READY)
391			break;
392	} while (timeout--);
393	if (!timeout) {
394		printk("dt2801: timeout 1 status=0x%02x\n", stat);
395	}
396	//printk("dt2801: reading dummy\n");
397	//dt2801_readdata(dev,&board_code);
398
399	DPRINTK("dt2801: reset\n");
400	outb_p(DT_C_RESET, dev->iobase + DT2801_CMD);
401	//dt2801_writecmd(dev,DT_C_RESET);
402
403	comedi_udelay(100);
404	timeout = 10000;
405	do {
406		stat = inb_p(dev->iobase + DT2801_STATUS);
407		if (stat & DT_S_READY)
408			break;
409	} while (timeout--);
410	if (!timeout) {
411		printk("dt2801: timeout 2 status=0x%02x\n", stat);
412	}
413
414	DPRINTK("dt2801: reading code\n");
415	dt2801_readdata(dev, &board_code);
416
417	DPRINTK("dt2801: ok.  code=0x%02x\n", board_code);
418
419	return board_code;
420}
421
422static int probe_number_of_ai_chans(struct comedi_device * dev)
423{
424	int n_chans;
425	int stat;
426	int data;
427
428	for (n_chans = 0; n_chans < 16; n_chans++) {
429		stat = dt2801_writecmd(dev, DT_C_READ_ADIM);
430		dt2801_writedata(dev, 0);
431		dt2801_writedata(dev, n_chans);
432		stat = dt2801_readdata2(dev, &data);
433
434		if (stat)
435			break;
436	}
437
438	dt2801_reset(dev);
439	dt2801_reset(dev);
440
441	return n_chans;
442}
443
444static const struct comedi_lrange *dac_range_table[] = {
445	&range_bipolar10,
446	&range_bipolar5,
447	&range_bipolar2_5,
448	&range_unipolar10,
449	&range_unipolar5
450};
451
452static const struct comedi_lrange *dac_range_lkup(int opt)
453{
454	if (opt < 0 || opt > 5)
455		return &range_unknown;
456	return dac_range_table[opt];
457}
458
459static const struct comedi_lrange *ai_range_lkup(int type, int opt)
460{
461	switch (type) {
462	case 0:
463		return (opt) ?
464			&range_dt2801_ai_pgl_unipolar :
465			&range_dt2801_ai_pgl_bipolar;
466	case 1:
467		return (opt) ? &range_unipolar10 : &range_bipolar10;
468	case 2:
469		return &range_unipolar5;
470	}
471	return &range_unknown;
472}
473
474/*
475   options:
476	[0] - i/o base
477	[1] - unused
478	[2] - a/d 0=differential, 1=single-ended
479	[3] - a/d range 0=[-10,10], 1=[0,10]
480	[4] - dac0 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
481	[5] - dac1 range 0=[-10,10], 1=[-5,5], 2=[-2.5,2.5] 3=[0,10], 4=[0,5]
482*/
483static int dt2801_attach(struct comedi_device * dev, struct comedi_devconfig * it)
484{
485	struct comedi_subdevice *s;
486	unsigned long iobase;
487	int board_code, type;
488	int ret = 0;
489	int n_ai_chans;
490
491	iobase = it->options[0];
492	if (!request_region(iobase, DT2801_IOSIZE, "dt2801")) {
493		comedi_error(dev, "I/O port conflict");
494		return -EIO;
495	}
496	dev->iobase = iobase;
497
498	/* do some checking */
499
500	board_code = dt2801_reset(dev);
501
502	/* heh.  if it didn't work, try it again. */
503	if (!board_code)
504		board_code = dt2801_reset(dev);
505
506	for (type = 0; type < n_boardtypes; type++) {
507		if (boardtypes[type].boardcode == board_code)
508			goto havetype;
509	}
510	printk("dt2801: unrecognized board code=0x%02x, contact author\n",
511		board_code);
512	type = 0;
513
514      havetype:
515	dev->board_ptr = boardtypes + type;
516	printk("dt2801: %s at port 0x%lx", boardtype.name, iobase);
517
518	n_ai_chans = probe_number_of_ai_chans(dev);
519	printk(" (ai channels = %d)", n_ai_chans);
520
521	if ((ret = alloc_subdevices(dev, 4)) < 0)
522		goto out;
523
524	if ((ret = alloc_private(dev, sizeof(dt2801_private))) < 0)
525		goto out;
526
527	dev->board_name = boardtype.name;
528
529	s = dev->subdevices + 0;
530	/* ai subdevice */
531	s->type = COMEDI_SUBD_AI;
532	s->subdev_flags = SDF_READABLE | SDF_GROUND;
533#if 1
534	s->n_chan = n_ai_chans;
535#else
536	if (it->options[2])
537		s->n_chan = boardtype.ad_chan;
538	else
539		s->n_chan = boardtype.ad_chan / 2;
540#endif
541	s->maxdata = (1 << boardtype.adbits) - 1;
542	s->range_table = ai_range_lkup(boardtype.adrangetype, it->options[3]);
543	s->insn_read = dt2801_ai_insn_read;
544
545	s++;
546	/* ao subdevice */
547	s->type = COMEDI_SUBD_AO;
548	s->subdev_flags = SDF_WRITABLE;
549	s->n_chan = 2;
550	s->maxdata = (1 << boardtype.dabits) - 1;
551	s->range_table_list = devpriv->dac_range_types;
552	devpriv->dac_range_types[0] = dac_range_lkup(it->options[4]);
553	devpriv->dac_range_types[1] = dac_range_lkup(it->options[5]);
554	s->insn_read = dt2801_ao_insn_read;
555	s->insn_write = dt2801_ao_insn_write;
556
557	s++;
558	/* 1st 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;
566
567	s++;
568	/* 2nd digital subdevice */
569	s->type = COMEDI_SUBD_DIO;
570	s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
571	s->n_chan = 8;
572	s->maxdata = 1;
573	s->range_table = &range_digital;
574	s->insn_bits = dt2801_dio_insn_bits;
575	s->insn_config = dt2801_dio_insn_config;
576
577	ret = 0;
578      out:
579	printk("\n");
580
581	return ret;
582}
583
584static int dt2801_detach(struct comedi_device * dev)
585{
586	if (dev->iobase)
587		release_region(dev->iobase, DT2801_IOSIZE);
588
589	return 0;
590}
591
592static int dt2801_error(struct comedi_device * dev, int stat)
593{
594	if (stat < 0) {
595		if (stat == -ETIME) {
596			printk("dt2801: timeout\n");
597		} else {
598			printk("dt2801: error %d\n", stat);
599		}
600		return stat;
601	}
602	printk("dt2801: error status 0x%02x, resetting...\n", stat);
603
604	dt2801_reset(dev);
605	dt2801_reset(dev);
606
607	return -EIO;
608}
609
610static int dt2801_ai_insn_read(struct comedi_device * dev, struct comedi_subdevice * s,
611	struct comedi_insn * insn, unsigned int * data)
612{
613	int d;
614	int stat;
615	int i;
616
617	for (i = 0; i < insn->n; i++) {
618		stat = dt2801_writecmd(dev, DT_C_READ_ADIM);
619		dt2801_writedata(dev, CR_RANGE(insn->chanspec));
620		dt2801_writedata(dev, CR_CHAN(insn->chanspec));
621		stat = dt2801_readdata2(dev, &d);
622
623		if (stat != 0)
624			return dt2801_error(dev, stat);
625
626		data[i] = d;
627	}
628
629	return i;
630}
631
632static int dt2801_ao_insn_read(struct comedi_device * dev, struct comedi_subdevice * s,
633	struct comedi_insn * insn, unsigned int * data)
634{
635	data[0] = devpriv->ao_readback[CR_CHAN(insn->chanspec)];
636
637	return 1;
638}
639
640static int dt2801_ao_insn_write(struct comedi_device * dev, struct comedi_subdevice * s,
641	struct comedi_insn * insn, unsigned int * data)
642{
643	dt2801_writecmd(dev, DT_C_WRITE_DAIM);
644	dt2801_writedata(dev, CR_CHAN(insn->chanspec));
645	dt2801_writedata2(dev, data[0]);
646
647	devpriv->ao_readback[CR_CHAN(insn->chanspec)] = data[0];
648
649	return 1;
650}
651
652static int dt2801_dio_insn_bits(struct comedi_device * dev, struct comedi_subdevice * s,
653	struct comedi_insn * insn, unsigned int * data)
654{
655	int which = 0;
656
657	if (s == dev->subdevices + 4)
658		which = 1;
659
660	if (insn->n != 2)
661		return -EINVAL;
662	if (data[0]) {
663		s->state &= ~data[0];
664		s->state |= (data[0] & data[1]);
665		dt2801_writecmd(dev, DT_C_WRITE_DIG);
666		dt2801_writedata(dev, which);
667		dt2801_writedata(dev, s->state);
668	}
669	dt2801_writecmd(dev, DT_C_READ_DIG);
670	dt2801_writedata(dev, which);
671	dt2801_readdata(dev, data + 1);
672
673	return 2;
674}
675
676static int dt2801_dio_insn_config(struct comedi_device * dev, struct comedi_subdevice * s,
677	struct comedi_insn * insn, unsigned int * data)
678{
679	int which = 0;
680
681	if (s == dev->subdevices + 4)
682		which = 1;
683
684	/* configure */
685	if (data[0]) {
686		s->io_bits = 0xff;
687		dt2801_writecmd(dev, DT_C_SET_DIGOUT);
688	} else {
689		s->io_bits = 0;
690		dt2801_writecmd(dev, DT_C_SET_DIGIN);
691	}
692	dt2801_writedata(dev, which);
693
694	return 1;
695}
696