Merge branch 'for-3.11' of git://linux-nfs.org/~bfields/linux
[linux-2.6.git] / drivers / staging / comedi / drivers / pcl726.c
blob893f012a1b7a5fc6b468a330b367a27528821238
1 /*
2 comedi/drivers/pcl726.c
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
11 COMEDI - Linux Control and Measurement Device Interface
12 Copyright (C) 1998 David A. Schleef <ds@schleef.org>
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.
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.
25 Driver: pcl726
26 Description: Advantech PCL-726 & compatibles
27 Author: ds
28 Status: untested
29 Devices: [Advantech] PCL-726 (pcl726), PCL-727 (pcl727), PCL-728 (pcl728),
30 [ADLink] ACL-6126 (acl6126), ACL-6128 (acl6128)
32 Interrupts are not supported.
34 Options for PCL-726:
35 [0] - IO Base
36 [2]...[7] - D/A output range for channel 1-6:
37 0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V,
38 4: 4-20mA, 5: unknown (external reference)
40 Options for PCL-727:
41 [0] - IO Base
42 [2]...[13] - D/A output range for channel 1-12:
43 0: 0-5V, 1: 0-10V, 2: +/-5V,
44 3: 4-20mA
46 Options for PCL-728 and ACL-6128:
47 [0] - IO Base
48 [2], [3] - D/A output range for channel 1 and 2:
49 0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V,
50 4: 4-20mA, 5: 0-20mA
52 Options for ACL-6126:
53 [0] - IO Base
54 [1] - IRQ (0=disable, 3, 5, 6, 7, 9, 10, 11, 12, 15) (currently ignored)
55 [2]...[7] - D/A output range for channel 1-6:
56 0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V,
57 4: 4-20mA
61 Thanks to Circuit Specialists for having programming info (!) on
62 their web page. (http://www.cir.com/)
65 #include "../comedidev.h"
67 #include <linux/ioport.h>
69 #undef ACL6126_IRQ /* no interrupt support (yet) */
71 #define PCL726_SIZE 16
72 #define PCL727_SIZE 32
73 #define PCL728_SIZE 8
75 #define PCL726_DAC0_HI 0
76 #define PCL726_DAC0_LO 1
78 #define PCL726_DO_HI 12
79 #define PCL726_DO_LO 13
80 #define PCL726_DI_HI 14
81 #define PCL726_DI_LO 15
83 #define PCL727_DO_HI 24
84 #define PCL727_DO_LO 25
85 #define PCL727_DI_HI 0
86 #define PCL727_DI_LO 1
88 static const struct comedi_lrange *const rangelist_726[] = {
89 &range_unipolar5, &range_unipolar10,
90 &range_bipolar5, &range_bipolar10,
91 &range_4_20mA, &range_unknown
94 static const struct comedi_lrange *const rangelist_727[] = {
95 &range_unipolar5, &range_unipolar10,
96 &range_bipolar5,
97 &range_4_20mA
100 static const struct comedi_lrange *const rangelist_728[] = {
101 &range_unipolar5, &range_unipolar10,
102 &range_bipolar5, &range_bipolar10,
103 &range_4_20mA, &range_0_20mA
106 struct pcl726_board {
108 const char *name; /* driver name */
109 int n_aochan; /* num of D/A chans */
110 int num_of_ranges; /* num of ranges */
111 unsigned int IRQbits; /* allowed interrupts */
112 unsigned int io_range; /* len of IO space */
113 char have_dio; /* 1=card have DI/DO ports */
114 int di_hi; /* ports for DI/DO operations */
115 int di_lo;
116 int do_hi;
117 int do_lo;
118 const struct comedi_lrange *const *range_type_list;
119 /* list of supported ranges */
122 static const struct pcl726_board boardtypes[] = {
123 {"pcl726", 6, 6, 0x0000, PCL726_SIZE, 1,
124 PCL726_DI_HI, PCL726_DI_LO, PCL726_DO_HI, PCL726_DO_LO,
125 &rangelist_726[0],},
126 {"pcl727", 12, 4, 0x0000, PCL727_SIZE, 1,
127 PCL727_DI_HI, PCL727_DI_LO, PCL727_DO_HI, PCL727_DO_LO,
128 &rangelist_727[0],},
129 {"pcl728", 2, 6, 0x0000, PCL728_SIZE, 0,
130 0, 0, 0, 0,
131 &rangelist_728[0],},
132 {"acl6126", 6, 5, 0x96e8, PCL726_SIZE, 1,
133 PCL726_DI_HI, PCL726_DI_LO, PCL726_DO_HI, PCL726_DO_LO,
134 &rangelist_726[0],},
135 {"acl6128", 2, 6, 0x0000, PCL728_SIZE, 0,
136 0, 0, 0, 0,
137 &rangelist_728[0],},
140 struct pcl726_private {
142 int bipolar[12];
143 const struct comedi_lrange *rangelist[12];
144 unsigned int ao_readback[12];
147 static int pcl726_ao_insn(struct comedi_device *dev, struct comedi_subdevice *s,
148 struct comedi_insn *insn, unsigned int *data)
150 struct pcl726_private *devpriv = dev->private;
151 int hi, lo;
152 int n;
153 int chan = CR_CHAN(insn->chanspec);
155 for (n = 0; n < insn->n; n++) {
156 lo = data[n] & 0xff;
157 hi = (data[n] >> 8) & 0xf;
158 if (devpriv->bipolar[chan])
159 hi ^= 0x8;
161 * the programming info did not say which order
162 * to write bytes. switch the order of the next
163 * two lines if you get glitches.
165 outb(hi, dev->iobase + PCL726_DAC0_HI + 2 * chan);
166 outb(lo, dev->iobase + PCL726_DAC0_LO + 2 * chan);
167 devpriv->ao_readback[chan] = data[n];
170 return n;
173 static int pcl726_ao_insn_read(struct comedi_device *dev,
174 struct comedi_subdevice *s,
175 struct comedi_insn *insn, unsigned int *data)
177 struct pcl726_private *devpriv = dev->private;
178 int chan = CR_CHAN(insn->chanspec);
179 int n;
181 for (n = 0; n < insn->n; n++)
182 data[n] = devpriv->ao_readback[chan];
183 return n;
186 static int pcl726_di_insn_bits(struct comedi_device *dev,
187 struct comedi_subdevice *s,
188 struct comedi_insn *insn, unsigned int *data)
190 const struct pcl726_board *board = comedi_board(dev);
192 data[1] = inb(dev->iobase + board->di_lo) |
193 (inb(dev->iobase + board->di_hi) << 8);
195 return insn->n;
198 static int pcl726_do_insn_bits(struct comedi_device *dev,
199 struct comedi_subdevice *s,
200 struct comedi_insn *insn, unsigned int *data)
202 const struct pcl726_board *board = comedi_board(dev);
204 if (data[0]) {
205 s->state &= ~data[0];
206 s->state |= data[0] & data[1];
208 if (data[1] & 0x00ff)
209 outb(s->state & 0xff, dev->iobase + board->do_lo);
210 if (data[1] & 0xff00)
211 outb((s->state >> 8), dev->iobase + board->do_hi);
213 data[1] = s->state;
215 return insn->n;
218 static int pcl726_attach(struct comedi_device *dev, struct comedi_devconfig *it)
220 const struct pcl726_board *board = comedi_board(dev);
221 struct pcl726_private *devpriv;
222 struct comedi_subdevice *s;
223 int ret, i;
224 #ifdef ACL6126_IRQ
225 unsigned int irq;
226 #endif
228 ret = comedi_request_region(dev, it->options[0], board->io_range);
229 if (ret)
230 return ret;
232 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
233 if (!devpriv)
234 return -ENOMEM;
235 dev->private = devpriv;
237 for (i = 0; i < 12; i++) {
238 devpriv->bipolar[i] = 0;
239 devpriv->rangelist[i] = &range_unknown;
242 #ifdef ACL6126_IRQ
243 irq = 0;
244 if (boardtypes[board].IRQbits != 0) { /* board support IRQ */
245 irq = it->options[1];
246 devpriv->first_chan = 2;
247 if (irq) { /* we want to use IRQ */
248 if (((1 << irq) & boardtypes[board].IRQbits) == 0) {
249 printk(KERN_WARNING
250 ", IRQ %d is out of allowed range,"
251 " DISABLING IT", irq);
252 irq = 0; /* Bad IRQ */
253 } else {
254 if (request_irq(irq, interrupt_pcl818, 0,
255 dev->board_name, dev)) {
256 printk(KERN_WARNING
257 ", unable to allocate IRQ %d,"
258 " DISABLING IT", irq);
259 irq = 0; /* Can't use IRQ */
260 } else {
261 printk(", irq=%d", irq);
267 dev->irq = irq;
268 #endif
270 printk("\n");
272 ret = comedi_alloc_subdevices(dev, 3);
273 if (ret)
274 return ret;
276 s = &dev->subdevices[0];
277 /* ao */
278 s->type = COMEDI_SUBD_AO;
279 s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
280 s->n_chan = board->n_aochan;
281 s->maxdata = 0xfff;
282 s->len_chanlist = 1;
283 s->insn_write = pcl726_ao_insn;
284 s->insn_read = pcl726_ao_insn_read;
285 s->range_table_list = devpriv->rangelist;
286 for (i = 0; i < board->n_aochan; i++) {
287 int j;
289 j = it->options[2 + 1];
290 if ((j < 0) || (j >= board->num_of_ranges)) {
291 printk
292 ("Invalid range for channel %d! Must be 0<=%d<%d\n",
293 i, j, board->num_of_ranges - 1);
294 j = 0;
296 devpriv->rangelist[i] = board->range_type_list[j];
297 if (devpriv->rangelist[i]->range[0].min ==
298 -devpriv->rangelist[i]->range[0].max)
299 devpriv->bipolar[i] = 1; /* bipolar range */
302 s = &dev->subdevices[1];
303 /* di */
304 if (!board->have_dio) {
305 s->type = COMEDI_SUBD_UNUSED;
306 } else {
307 s->type = COMEDI_SUBD_DI;
308 s->subdev_flags = SDF_READABLE | SDF_GROUND;
309 s->n_chan = 16;
310 s->maxdata = 1;
311 s->len_chanlist = 1;
312 s->insn_bits = pcl726_di_insn_bits;
313 s->range_table = &range_digital;
316 s = &dev->subdevices[2];
317 /* do */
318 if (!board->have_dio) {
319 s->type = COMEDI_SUBD_UNUSED;
320 } else {
321 s->type = COMEDI_SUBD_DO;
322 s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
323 s->n_chan = 16;
324 s->maxdata = 1;
325 s->len_chanlist = 1;
326 s->insn_bits = pcl726_do_insn_bits;
327 s->range_table = &range_digital;
330 return 0;
333 static struct comedi_driver pcl726_driver = {
334 .driver_name = "pcl726",
335 .module = THIS_MODULE,
336 .attach = pcl726_attach,
337 .detach = comedi_legacy_detach,
338 .board_name = &boardtypes[0].name,
339 .num_names = ARRAY_SIZE(boardtypes),
340 .offset = sizeof(struct pcl726_board),
342 module_comedi_driver(pcl726_driver);
344 MODULE_AUTHOR("Comedi http://www.comedi.org");
345 MODULE_DESCRIPTION("Comedi low-level driver");
346 MODULE_LICENSE("GPL");