Linux-libre 3.10.72-gnu
[librecmc/linux-libre.git] / drivers / staging / comedi / drivers / pcl726.c
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 /*
30 Driver: pcl726
31 Description: Advantech PCL-726 & compatibles
32 Author: ds
33 Status: untested
34 Devices: [Advantech] PCL-726 (pcl726), PCL-727 (pcl727), PCL-728 (pcl728),
35   [ADLink] ACL-6126 (acl6126), ACL-6128 (acl6128)
36
37 Interrupts 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
93 static const struct comedi_lrange *const rangelist_726[] = {
94         &range_unipolar5, &range_unipolar10,
95         &range_bipolar5, &range_bipolar10,
96         &range_4_20mA, &range_unknown
97 };
98
99 static const struct comedi_lrange *const rangelist_727[] = {
100         &range_unipolar5, &range_unipolar10,
101         &range_bipolar5,
102         &range_4_20mA
103 };
104
105 static const struct comedi_lrange *const rangelist_728[] = {
106         &range_unipolar5, &range_unipolar10,
107         &range_bipolar5, &range_bipolar10,
108         &range_4_20mA, &range_0_20mA
109 };
110
111 struct pcl726_board {
112
113         const char *name;       /*  driver name */
114         int n_aochan;           /*  num of D/A chans */
115         int num_of_ranges;      /*  num of ranges */
116         unsigned int IRQbits;   /*  allowed interrupts */
117         unsigned int io_range;  /*  len of IO space */
118         char have_dio;          /*  1=card have DI/DO ports */
119         int di_hi;              /*  ports for DI/DO operations */
120         int di_lo;
121         int do_hi;
122         int do_lo;
123         const struct comedi_lrange *const *range_type_list;
124         /*  list of supported ranges */
125 };
126
127 static const struct pcl726_board boardtypes[] = {
128         {"pcl726", 6, 6, 0x0000, PCL726_SIZE, 1,
129          PCL726_DI_HI, PCL726_DI_LO, PCL726_DO_HI, PCL726_DO_LO,
130          &rangelist_726[0],},
131         {"pcl727", 12, 4, 0x0000, PCL727_SIZE, 1,
132          PCL727_DI_HI, PCL727_DI_LO, PCL727_DO_HI, PCL727_DO_LO,
133          &rangelist_727[0],},
134         {"pcl728", 2, 6, 0x0000, PCL728_SIZE, 0,
135          0, 0, 0, 0,
136          &rangelist_728[0],},
137         {"acl6126", 6, 5, 0x96e8, PCL726_SIZE, 1,
138          PCL726_DI_HI, PCL726_DI_LO, PCL726_DO_HI, PCL726_DO_LO,
139          &rangelist_726[0],},
140         {"acl6128", 2, 6, 0x0000, PCL728_SIZE, 0,
141          0, 0, 0, 0,
142          &rangelist_728[0],},
143 };
144
145 struct pcl726_private {
146
147         int bipolar[12];
148         const struct comedi_lrange *rangelist[12];
149         unsigned int ao_readback[12];
150 };
151
152 static int pcl726_ao_insn(struct comedi_device *dev, struct comedi_subdevice *s,
153                           struct comedi_insn *insn, unsigned int *data)
154 {
155         struct pcl726_private *devpriv = dev->private;
156         int hi, lo;
157         int n;
158         int chan = CR_CHAN(insn->chanspec);
159
160         for (n = 0; n < insn->n; n++) {
161                 lo = data[n] & 0xff;
162                 hi = (data[n] >> 8) & 0xf;
163                 if (devpriv->bipolar[chan])
164                         hi ^= 0x8;
165                 /*
166                  * the programming info did not say which order
167                  * to write bytes.  switch the order of the next
168                  * two lines if you get glitches.
169                  */
170                 outb(hi, dev->iobase + PCL726_DAC0_HI + 2 * chan);
171                 outb(lo, dev->iobase + PCL726_DAC0_LO + 2 * chan);
172                 devpriv->ao_readback[chan] = data[n];
173         }
174
175         return n;
176 }
177
178 static int pcl726_ao_insn_read(struct comedi_device *dev,
179                                struct comedi_subdevice *s,
180                                struct comedi_insn *insn, unsigned int *data)
181 {
182         struct pcl726_private *devpriv = dev->private;
183         int chan = CR_CHAN(insn->chanspec);
184         int n;
185
186         for (n = 0; n < insn->n; n++)
187                 data[n] = devpriv->ao_readback[chan];
188         return n;
189 }
190
191 static int pcl726_di_insn_bits(struct comedi_device *dev,
192                                struct comedi_subdevice *s,
193                                struct comedi_insn *insn, unsigned int *data)
194 {
195         const struct pcl726_board *board = comedi_board(dev);
196
197         data[1] = inb(dev->iobase + board->di_lo) |
198             (inb(dev->iobase + board->di_hi) << 8);
199
200         return insn->n;
201 }
202
203 static int pcl726_do_insn_bits(struct comedi_device *dev,
204                                struct comedi_subdevice *s,
205                                struct comedi_insn *insn, unsigned int *data)
206 {
207         const struct pcl726_board *board = comedi_board(dev);
208
209         if (data[0]) {
210                 s->state &= ~data[0];
211                 s->state |= data[0] & data[1];
212         }
213         if (data[1] & 0x00ff)
214                 outb(s->state & 0xff, dev->iobase + board->do_lo);
215         if (data[1] & 0xff00)
216                 outb((s->state >> 8), dev->iobase + board->do_hi);
217
218         data[1] = s->state;
219
220         return insn->n;
221 }
222
223 static int pcl726_attach(struct comedi_device *dev, struct comedi_devconfig *it)
224 {
225         const struct pcl726_board *board = comedi_board(dev);
226         struct pcl726_private *devpriv;
227         struct comedi_subdevice *s;
228         int ret, i;
229 #ifdef ACL6126_IRQ
230         unsigned int irq;
231 #endif
232
233         ret = comedi_request_region(dev, it->options[0], board->io_range);
234         if (ret)
235                 return ret;
236
237         devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
238         if (!devpriv)
239                 return -ENOMEM;
240         dev->private = devpriv;
241
242         for (i = 0; i < 12; i++) {
243                 devpriv->bipolar[i] = 0;
244                 devpriv->rangelist[i] = &range_unknown;
245         }
246
247 #ifdef ACL6126_IRQ
248         irq = 0;
249         if (boardtypes[board].IRQbits != 0) {   /* board support IRQ */
250                 irq = it->options[1];
251                 devpriv->first_chan = 2;
252                 if (irq) {      /* we want to use IRQ */
253                         if (((1 << irq) & boardtypes[board].IRQbits) == 0) {
254                                 printk(KERN_WARNING
255                                         ", IRQ %d is out of allowed range,"
256                                         " DISABLING IT", irq);
257                                 irq = 0;        /* Bad IRQ */
258                         } else {
259                                 if (request_irq(irq, interrupt_pcl818, 0,
260                                                 dev->board_name, dev)) {
261                                         printk(KERN_WARNING
262                                                 ", unable to allocate IRQ %d,"
263                                                 " DISABLING IT", irq);
264                                         irq = 0;        /* Can't use IRQ */
265                                 } else {
266                                         printk(", irq=%d", irq);
267                                 }
268                         }
269                 }
270         }
271
272         dev->irq = irq;
273 #endif
274
275         printk("\n");
276
277         ret = comedi_alloc_subdevices(dev, 3);
278         if (ret)
279                 return ret;
280
281         s = &dev->subdevices[0];
282         /* ao */
283         s->type = COMEDI_SUBD_AO;
284         s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
285         s->n_chan = board->n_aochan;
286         s->maxdata = 0xfff;
287         s->len_chanlist = 1;
288         s->insn_write = pcl726_ao_insn;
289         s->insn_read = pcl726_ao_insn_read;
290         s->range_table_list = devpriv->rangelist;
291         for (i = 0; i < board->n_aochan; i++) {
292                 int j;
293
294                 j = it->options[2 + 1];
295                 if ((j < 0) || (j >= board->num_of_ranges)) {
296                         printk
297                             ("Invalid range for channel %d! Must be 0<=%d<%d\n",
298                              i, j, board->num_of_ranges - 1);
299                         j = 0;
300                 }
301                 devpriv->rangelist[i] = board->range_type_list[j];
302                 if (devpriv->rangelist[i]->range[0].min ==
303                     -devpriv->rangelist[i]->range[0].max)
304                         devpriv->bipolar[i] = 1;        /* bipolar range */
305         }
306
307         s = &dev->subdevices[1];
308         /* di */
309         if (!board->have_dio) {
310                 s->type = COMEDI_SUBD_UNUSED;
311         } else {
312                 s->type = COMEDI_SUBD_DI;
313                 s->subdev_flags = SDF_READABLE | SDF_GROUND;
314                 s->n_chan = 16;
315                 s->maxdata = 1;
316                 s->len_chanlist = 1;
317                 s->insn_bits = pcl726_di_insn_bits;
318                 s->range_table = &range_digital;
319         }
320
321         s = &dev->subdevices[2];
322         /* do */
323         if (!board->have_dio) {
324                 s->type = COMEDI_SUBD_UNUSED;
325         } else {
326                 s->type = COMEDI_SUBD_DO;
327                 s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
328                 s->n_chan = 16;
329                 s->maxdata = 1;
330                 s->len_chanlist = 1;
331                 s->insn_bits = pcl726_do_insn_bits;
332                 s->range_table = &range_digital;
333         }
334
335         return 0;
336 }
337
338 static struct comedi_driver pcl726_driver = {
339         .driver_name    = "pcl726",
340         .module         = THIS_MODULE,
341         .attach         = pcl726_attach,
342         .detach         = comedi_legacy_detach,
343         .board_name     = &boardtypes[0].name,
344         .num_names      = ARRAY_SIZE(boardtypes),
345         .offset         = sizeof(struct pcl726_board),
346 };
347 module_comedi_driver(pcl726_driver);
348
349 MODULE_AUTHOR("Comedi http://www.comedi.org");
350 MODULE_DESCRIPTION("Comedi low-level driver");
351 MODULE_LICENSE("GPL");