pcl726.c revision a69fba9e379b67c7fb54012547200f4517447bbd
1/*
2    comedi/drivers/pcl726.c
3
4    hardware driver for Advantech cards:
5     card:   PCL-726, PCL-727, PCL-728
6     driver: pcl726,  pcl727,  pcl728
7    and for ADLink cards:
8     card:   ACL-6126, ACL-6128
9     driver: acl6126,  acl6128
10
11    COMEDI - Linux Control and Measurement Device Interface
12    Copyright (C) 1998 David A. Schleef <ds@schleef.org>
13
14    This program is free software; you can redistribute it and/or modify
15    it under the terms of the GNU General Public License as published by
16    the Free Software Foundation; either version 2 of the License, or
17    (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License for more details.
23
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27
28*/
29/*
30Driver: pcl726
31Description: Advantech PCL-726 & compatibles
32Author: ds
33Status: untested
34Devices: [Advantech] PCL-726 (pcl726), PCL-727 (pcl727), PCL-728 (pcl728),
35  [ADLink] ACL-6126 (acl6126), ACL-6128 (acl6128)
36
37Interrupts are not supported.
38
39    Options for PCL-726:
40     [0] - IO Base
41     [2]...[7] - D/A output range for channel 1-6:
42		0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V,
43		4: 4-20mA, 5: unknown (external reference)
44
45    Options for PCL-727:
46     [0] - IO Base
47     [2]...[13] - D/A output range for channel 1-12:
48		0: 0-5V, 1: 0-10V, 2: +/-5V,
49		3: 4-20mA
50
51    Options for PCL-728 and ACL-6128:
52     [0] - IO Base
53     [2], [3] - D/A output range for channel 1 and 2:
54		0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V,
55		4: 4-20mA, 5: 0-20mA
56
57    Options for ACL-6126:
58     [0] - IO Base
59     [1] - IRQ (0=disable, 3, 5, 6, 7, 9, 10, 11, 12, 15) (currently ignored)
60     [2]...[7] - D/A output range for channel 1-6:
61		0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V,
62		4: 4-20mA
63*/
64
65/*
66    Thanks to Circuit Specialists for having programming info (!) on
67    their web page.  (http://www.cir.com/)
68*/
69
70#include "../comedidev.h"
71
72#include <linux/ioport.h>
73
74#undef ACL6126_IRQ		/* no interrupt support (yet) */
75
76#define PCL726_SIZE 16
77#define PCL727_SIZE 32
78#define PCL728_SIZE 8
79
80#define PCL726_DAC0_HI 0
81#define PCL726_DAC0_LO 1
82
83#define PCL726_DO_HI 12
84#define PCL726_DO_LO 13
85#define PCL726_DI_HI 14
86#define PCL726_DI_LO 15
87
88#define PCL727_DO_HI 24
89#define PCL727_DO_LO 25
90#define PCL727_DI_HI  0
91#define PCL727_DI_LO  1
92
93static const struct comedi_lrange range_4_20mA = { 1, {RANGE_mA(4, 20)} };
94static const struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} };
95
96static const struct comedi_lrange *const rangelist_726[] = {
97	&range_unipolar5, &range_unipolar10,
98	&range_bipolar5, &range_bipolar10,
99	&range_4_20mA, &range_unknown
100};
101
102static const struct comedi_lrange *const rangelist_727[] = {
103	&range_unipolar5, &range_unipolar10,
104	&range_bipolar5,
105	&range_4_20mA
106};
107
108static const struct comedi_lrange *const rangelist_728[] = {
109	&range_unipolar5, &range_unipolar10,
110	&range_bipolar5, &range_bipolar10,
111	&range_4_20mA, &range_0_20mA
112};
113
114static int pcl726_attach(struct comedi_device *dev,
115			 struct comedi_devconfig *it);
116static int pcl726_detach(struct comedi_device *dev);
117
118struct pcl726_board {
119
120	const char *name;	/*  driver name */
121	int n_aochan;		/*  num of D/A chans */
122	int num_of_ranges;	/*  num of ranges */
123	unsigned int IRQbits;	/*  allowed interrupts */
124	unsigned int io_range;	/*  len of IO space */
125	char have_dio;		/*  1=card have DI/DO ports */
126	int di_hi;		/*  ports for DI/DO operations */
127	int di_lo;
128	int do_hi;
129	int do_lo;
130	const struct comedi_lrange *const *range_type_list;
131	/*  list of supported ranges */
132};
133
134static const struct pcl726_board boardtypes[] = {
135	{"pcl726", 6, 6, 0x0000, PCL726_SIZE, 1,
136	 PCL726_DI_HI, PCL726_DI_LO, PCL726_DO_HI, PCL726_DO_LO,
137	 &rangelist_726[0],},
138	{"pcl727", 12, 4, 0x0000, PCL727_SIZE, 1,
139	 PCL727_DI_HI, PCL727_DI_LO, PCL727_DO_HI, PCL727_DO_LO,
140	 &rangelist_727[0],},
141	{"pcl728", 2, 6, 0x0000, PCL728_SIZE, 0,
142	 0, 0, 0, 0,
143	 &rangelist_728[0],},
144	{"acl6126", 6, 5, 0x96e8, PCL726_SIZE, 1,
145	 PCL726_DI_HI, PCL726_DI_LO, PCL726_DO_HI, PCL726_DO_LO,
146	 &rangelist_726[0],},
147	{"acl6128", 2, 6, 0x0000, PCL728_SIZE, 0,
148	 0, 0, 0, 0,
149	 &rangelist_728[0],},
150};
151
152#define n_boardtypes (sizeof(boardtypes)/sizeof(struct pcl726_board))
153#define this_board ((const struct pcl726_board *)dev->board_ptr)
154
155static struct comedi_driver driver_pcl726 = {
156	.driver_name = "pcl726",
157	.module = THIS_MODULE,
158	.attach = pcl726_attach,
159	.detach = pcl726_detach,
160	.board_name = &boardtypes[0].name,
161	.num_names = n_boardtypes,
162	.offset = sizeof(struct pcl726_board),
163};
164
165COMEDI_INITCLEANUP(driver_pcl726);
166
167struct pcl726_private {
168
169	int bipolar[12];
170	const struct comedi_lrange *rangelist[12];
171	unsigned int ao_readback[12];
172};
173
174#define devpriv ((struct pcl726_private *)dev->private)
175
176static int pcl726_ao_insn(struct comedi_device *dev, struct comedi_subdevice *s,
177			  struct comedi_insn *insn, unsigned int *data)
178{
179	int hi, lo;
180	int n;
181	int chan = CR_CHAN(insn->chanspec);
182
183	for (n = 0; n < insn->n; n++) {
184		lo = data[n] & 0xff;
185		hi = (data[n] >> 8) & 0xf;
186		if (devpriv->bipolar[chan])
187			hi ^= 0x8;
188		/*
189		 * the programming info did not say which order
190		 * to write bytes.  switch the order of the next
191		 * two lines if you get glitches.
192		 */
193		outb(hi, dev->iobase + PCL726_DAC0_HI + 2 * chan);
194		outb(lo, dev->iobase + PCL726_DAC0_LO + 2 * chan);
195		devpriv->ao_readback[chan] = data[n];
196	}
197
198	return n;
199}
200
201static int pcl726_ao_insn_read(struct comedi_device *dev,
202			       struct comedi_subdevice *s,
203			       struct comedi_insn *insn, unsigned int *data)
204{
205	int chan = CR_CHAN(insn->chanspec);
206	int n;
207
208	for (n = 0; n < insn->n; n++)
209		data[n] = devpriv->ao_readback[chan];
210	return n;
211}
212
213static int pcl726_di_insn_bits(struct comedi_device *dev,
214			       struct comedi_subdevice *s,
215			       struct comedi_insn *insn, unsigned int *data)
216{
217	if (insn->n != 2)
218		return -EINVAL;
219
220	data[1] = inb(dev->iobase + this_board->di_lo) |
221	    (inb(dev->iobase + this_board->di_hi) << 8);
222
223	return 2;
224}
225
226static int pcl726_do_insn_bits(struct comedi_device *dev,
227			       struct comedi_subdevice *s,
228			       struct comedi_insn *insn, unsigned int *data)
229{
230	if (insn->n != 2)
231		return -EINVAL;
232
233	if (data[0]) {
234		s->state &= ~data[0];
235		s->state |= data[0] & data[1];
236	}
237	if (data[1] & 0x00ff)
238		outb(s->state & 0xff, dev->iobase + this_board->do_lo);
239	if (data[1] & 0xff00)
240		outb((s->state >> 8), dev->iobase + this_board->do_hi);
241
242	data[1] = s->state;
243
244	return 2;
245}
246
247static int pcl726_attach(struct comedi_device *dev, struct comedi_devconfig *it)
248{
249	struct comedi_subdevice *s;
250	unsigned long iobase;
251	unsigned int iorange;
252	int ret, i;
253#ifdef ACL6126_IRQ
254	unsigned int irq;
255#endif
256
257	iobase = it->options[0];
258	iorange = this_board->io_range;
259	printk(KERN_WARNING "comedi%d: pcl726: board=%s, 0x%03lx ", dev->minor,
260	       this_board->name, iobase);
261	if (!request_region(iobase, iorange, "pcl726")) {
262		printk(KERN_WARNING "I/O port conflict\n");
263		return -EIO;
264	}
265
266	dev->iobase = iobase;
267
268	dev->board_name = this_board->name;
269
270	ret = alloc_private(dev, sizeof(struct pcl726_private));
271	if (ret < 0)
272		return -ENOMEM;
273
274	for (i = 0; i < 12; i++) {
275		devpriv->bipolar[i] = 0;
276		devpriv->rangelist[i] = &range_unknown;
277	}
278
279#ifdef ACL6126_IRQ
280	irq = 0;
281	if (boardtypes[board].IRQbits != 0) {	/* board support IRQ */
282		irq = it->options[1];
283		devpriv->first_chan = 2;
284		if (irq) {	/* we want to use IRQ */
285			if (((1 << irq) & boardtypes[board].IRQbits) == 0) {
286				printk(KERN_WARNING
287					", IRQ %d is out of allowed range,"
288					" DISABLING IT", irq);
289				irq = 0;	/* Bad IRQ */
290			} else {
291				if (request_irq(irq, interrupt_pcl818, 0,
292						"pcl726", dev)) {
293					printk(KERN_WARNING
294						", unable to allocate IRQ %d,"
295						" DISABLING IT", irq);
296					irq = 0;	/* Can't use IRQ */
297				} else {
298					printk(", irq=%d", irq);
299				}
300			}
301		}
302	}
303
304	dev->irq = irq;
305#endif
306
307	printk("\n");
308
309	ret = alloc_subdevices(dev, 3);
310	if (ret < 0)
311		return ret;
312
313	s = dev->subdevices + 0;
314	/* ao */
315	s->type = COMEDI_SUBD_AO;
316	s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
317	s->n_chan = this_board->n_aochan;
318	s->maxdata = 0xfff;
319	s->len_chanlist = 1;
320	s->insn_write = pcl726_ao_insn;
321	s->insn_read = pcl726_ao_insn_read;
322	s->range_table_list = devpriv->rangelist;
323	for (i = 0; i < this_board->n_aochan; i++) {
324		int j;
325
326		j = it->options[2 + 1];
327		if ((j < 0) || (j >= this_board->num_of_ranges)) {
328			printk
329			    ("Invalid range for channel %d! Must be 0<=%d<%d\n",
330			     i, j, this_board->num_of_ranges - 1);
331			j = 0;
332		}
333		devpriv->rangelist[i] = this_board->range_type_list[j];
334		if (devpriv->rangelist[i]->range[0].min ==
335		    -devpriv->rangelist[i]->range[0].max)
336			devpriv->bipolar[i] = 1;	/* bipolar range */
337	}
338
339	s = dev->subdevices + 1;
340	/* di */
341	if (!this_board->have_dio) {
342		s->type = COMEDI_SUBD_UNUSED;
343	} else {
344		s->type = COMEDI_SUBD_DI;
345		s->subdev_flags = SDF_READABLE | SDF_GROUND;
346		s->n_chan = 16;
347		s->maxdata = 1;
348		s->len_chanlist = 1;
349		s->insn_bits = pcl726_di_insn_bits;
350		s->range_table = &range_digital;
351	}
352
353	s = dev->subdevices + 2;
354	/* do */
355	if (!this_board->have_dio) {
356		s->type = COMEDI_SUBD_UNUSED;
357	} else {
358		s->type = COMEDI_SUBD_DO;
359		s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
360		s->n_chan = 16;
361		s->maxdata = 1;
362		s->len_chanlist = 1;
363		s->insn_bits = pcl726_do_insn_bits;
364		s->range_table = &range_digital;
365	}
366
367	return 0;
368}
369
370static int pcl726_detach(struct comedi_device *dev)
371{
372/* printk("comedi%d: pcl726: remove\n",dev->minor); */
373
374#ifdef ACL6126_IRQ
375	if (dev->irq)
376		free_irq(dev->irq, dev);
377#endif
378
379	if (dev->iobase)
380		release_region(dev->iobase, this_board->io_range);
381
382	return 0;
383}
384