staging: comedi: pcl726: convert boardinfo declaration to C99 format
[firefly-linux-kernel-4.4.55.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 /*
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)
31
32 Interrupts are not supported.
33
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)
39
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
45
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
51
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
58 */
59
60 /*
61     Thanks to Circuit Specialists for having programming info (!) on
62     their web page.  (http://www.cir.com/)
63 */
64
65 #include <linux/module.h>
66 #include "../comedidev.h"
67
68 #undef ACL6126_IRQ              /* no interrupt support (yet) */
69
70 #define PCL726_SIZE 16
71 #define PCL727_SIZE 32
72 #define PCL728_SIZE 8
73
74 #define PCL726_DAC0_HI 0
75 #define PCL726_DAC0_LO 1
76
77 #define PCL726_DO_HI 12
78 #define PCL726_DO_LO 13
79 #define PCL726_DI_HI 14
80 #define PCL726_DI_LO 15
81
82 #define PCL727_DO_HI 24
83 #define PCL727_DO_LO 25
84 #define PCL727_DI_HI  0
85 #define PCL727_DI_LO  1
86
87 static const struct comedi_lrange *const rangelist_726[] = {
88         &range_unipolar5, &range_unipolar10,
89         &range_bipolar5, &range_bipolar10,
90         &range_4_20mA, &range_unknown
91 };
92
93 static const struct comedi_lrange *const rangelist_727[] = {
94         &range_unipolar5, &range_unipolar10,
95         &range_bipolar5,
96         &range_4_20mA
97 };
98
99 static const struct comedi_lrange *const rangelist_728[] = {
100         &range_unipolar5, &range_unipolar10,
101         &range_bipolar5, &range_bipolar10,
102         &range_4_20mA, &range_0_20mA
103 };
104
105 struct pcl726_board {
106
107         const char *name;       /*  driver name */
108         int n_aochan;           /*  num of D/A chans */
109         int num_of_ranges;      /*  num of ranges */
110         unsigned int IRQbits;   /*  allowed interrupts */
111         unsigned int io_range;  /*  len of IO space */
112         char have_dio;          /*  1=card have DI/DO ports */
113         int di_hi;              /*  ports for DI/DO operations */
114         int di_lo;
115         int do_hi;
116         int do_lo;
117         const struct comedi_lrange *const *range_type_list;
118         /*  list of supported ranges */
119 };
120
121 static const struct pcl726_board boardtypes[] = {
122         {
123                 .name           = "pcl726",
124                 .n_aochan       = 6,
125                 .num_of_ranges  = 6,
126                 .IRQbits        = 0x0000,
127                 .io_range       = PCL726_SIZE,
128                 .have_dio       = 1,
129                 .di_hi          = PCL726_DI_HI,
130                 .di_lo          = PCL726_DI_LO,
131                 .do_hi          = PCL726_DO_HI,
132                 .do_lo          = PCL726_DO_LO,
133                 .range_type_list = &rangelist_726[0],
134         }, {
135                 .name           = "pcl727",
136                 .n_aochan       = 12,
137                 .num_of_ranges  = 4,
138                 .IRQbits        = 0x0000,
139                 .io_range       = PCL727_SIZE,
140                 .have_dio       = 1,
141                 .di_hi          = PCL727_DI_HI,
142                 .di_lo          = PCL727_DI_LO,
143                 .do_hi          = PCL727_DO_HI,
144                 .do_lo          = PCL727_DO_LO,
145                 .range_type_list = &rangelist_727[0],
146         }, {
147                 .name           = "pcl728",
148                 .n_aochan       = 2,
149                 .num_of_ranges  = 6,
150                 .IRQbits        = 0x0000,
151                 .io_range       = PCL728_SIZE,
152                 .have_dio       = 0,
153                 .di_hi          = 0,
154                 .di_lo          = 0,
155                 .do_hi          = 0,
156                 .do_lo          = 0,
157                 .range_type_list = &rangelist_728[0],
158         }, {
159                 .name           = "acl6126",
160                 .n_aochan       = 6,
161                 .num_of_ranges  = 5,
162                 .IRQbits        = 0x96e8,
163                 .io_range       = PCL726_SIZE,
164                 .have_dio       = 1,
165                 .di_hi          = PCL726_DI_HI,
166                 .di_lo          = PCL726_DI_LO,
167                 .do_hi          = PCL726_DO_HI,
168                 .do_lo          = PCL726_DO_LO,
169                 .range_type_list = &rangelist_726[0],
170         }, {
171                 .name           = "acl6128",
172                 .n_aochan       = 2,
173                 .num_of_ranges  = 6,
174                 .IRQbits        = 0x0000,
175                 .io_range       = PCL728_SIZE,
176                 .have_dio       = 0,
177                 .di_hi          = 0,
178                 .di_lo          = 0,
179                 .do_hi          = 0,
180                 .do_lo          = 0,
181                 .range_type_list = &rangelist_728[0],
182         },
183 };
184
185 struct pcl726_private {
186
187         int bipolar[12];
188         const struct comedi_lrange *rangelist[12];
189         unsigned int ao_readback[12];
190 };
191
192 static int pcl726_ao_insn(struct comedi_device *dev, struct comedi_subdevice *s,
193                           struct comedi_insn *insn, unsigned int *data)
194 {
195         struct pcl726_private *devpriv = dev->private;
196         int hi, lo;
197         int n;
198         int chan = CR_CHAN(insn->chanspec);
199
200         for (n = 0; n < insn->n; n++) {
201                 lo = data[n] & 0xff;
202                 hi = (data[n] >> 8) & 0xf;
203                 if (devpriv->bipolar[chan])
204                         hi ^= 0x8;
205                 /*
206                  * the programming info did not say which order
207                  * to write bytes.  switch the order of the next
208                  * two lines if you get glitches.
209                  */
210                 outb(hi, dev->iobase + PCL726_DAC0_HI + 2 * chan);
211                 outb(lo, dev->iobase + PCL726_DAC0_LO + 2 * chan);
212                 devpriv->ao_readback[chan] = data[n];
213         }
214
215         return n;
216 }
217
218 static int pcl726_ao_insn_read(struct comedi_device *dev,
219                                struct comedi_subdevice *s,
220                                struct comedi_insn *insn, unsigned int *data)
221 {
222         struct pcl726_private *devpriv = dev->private;
223         int chan = CR_CHAN(insn->chanspec);
224         int n;
225
226         for (n = 0; n < insn->n; n++)
227                 data[n] = devpriv->ao_readback[chan];
228         return n;
229 }
230
231 static int pcl726_di_insn_bits(struct comedi_device *dev,
232                                struct comedi_subdevice *s,
233                                struct comedi_insn *insn, unsigned int *data)
234 {
235         const struct pcl726_board *board = comedi_board(dev);
236
237         data[1] = inb(dev->iobase + board->di_lo) |
238             (inb(dev->iobase + board->di_hi) << 8);
239
240         return insn->n;
241 }
242
243 static int pcl726_do_insn_bits(struct comedi_device *dev,
244                                struct comedi_subdevice *s,
245                                struct comedi_insn *insn,
246                                unsigned int *data)
247 {
248         const struct pcl726_board *board = comedi_board(dev);
249         unsigned int mask;
250
251         mask = comedi_dio_update_state(s, data);
252         if (mask) {
253                 if (mask & 0x00ff)
254                         outb(s->state & 0xff, dev->iobase + board->do_lo);
255                 if (mask & 0xff00)
256                         outb((s->state >> 8), dev->iobase + board->do_hi);
257         }
258
259         data[1] = s->state;
260
261         return insn->n;
262 }
263
264 static int pcl726_attach(struct comedi_device *dev, struct comedi_devconfig *it)
265 {
266         const struct pcl726_board *board = comedi_board(dev);
267         struct pcl726_private *devpriv;
268         struct comedi_subdevice *s;
269         int ret, i;
270 #ifdef ACL6126_IRQ
271         unsigned int irq;
272 #endif
273
274         ret = comedi_request_region(dev, it->options[0], board->io_range);
275         if (ret)
276                 return ret;
277
278         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
279         if (!devpriv)
280                 return -ENOMEM;
281
282         for (i = 0; i < 12; i++) {
283                 devpriv->bipolar[i] = 0;
284                 devpriv->rangelist[i] = &range_unknown;
285         }
286
287 #ifdef ACL6126_IRQ
288         irq = 0;
289         if (boardtypes[board].IRQbits != 0) {   /* board support IRQ */
290                 irq = it->options[1];
291                 devpriv->first_chan = 2;
292                 if (irq) {      /* we want to use IRQ */
293                         if (((1 << irq) & boardtypes[board].IRQbits) == 0) {
294                                 printk(KERN_WARNING
295                                         ", IRQ %d is out of allowed range,"
296                                         " DISABLING IT", irq);
297                                 irq = 0;        /* Bad IRQ */
298                         } else {
299                                 if (request_irq(irq, interrupt_pcl818, 0,
300                                                 dev->board_name, dev)) {
301                                         printk(KERN_WARNING
302                                                 ", unable to allocate IRQ %d,"
303                                                 " DISABLING IT", irq);
304                                         irq = 0;        /* Can't use IRQ */
305                                 } else {
306                                         printk(", irq=%d", irq);
307                                 }
308                         }
309                 }
310         }
311
312         dev->irq = irq;
313 #endif
314
315         printk("\n");
316
317         ret = comedi_alloc_subdevices(dev, 3);
318         if (ret)
319                 return ret;
320
321         s = &dev->subdevices[0];
322         /* ao */
323         s->type = COMEDI_SUBD_AO;
324         s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
325         s->n_chan = board->n_aochan;
326         s->maxdata = 0xfff;
327         s->len_chanlist = 1;
328         s->insn_write = pcl726_ao_insn;
329         s->insn_read = pcl726_ao_insn_read;
330         s->range_table_list = devpriv->rangelist;
331         for (i = 0; i < board->n_aochan; i++) {
332                 int j;
333
334                 j = it->options[2 + 1];
335                 if ((j < 0) || (j >= board->num_of_ranges)) {
336                         printk
337                             ("Invalid range for channel %d! Must be 0<=%d<%d\n",
338                              i, j, board->num_of_ranges - 1);
339                         j = 0;
340                 }
341                 devpriv->rangelist[i] = board->range_type_list[j];
342                 if (devpriv->rangelist[i]->range[0].min ==
343                     -devpriv->rangelist[i]->range[0].max)
344                         devpriv->bipolar[i] = 1;        /* bipolar range */
345         }
346
347         s = &dev->subdevices[1];
348         /* di */
349         if (!board->have_dio) {
350                 s->type = COMEDI_SUBD_UNUSED;
351         } else {
352                 s->type = COMEDI_SUBD_DI;
353                 s->subdev_flags = SDF_READABLE | SDF_GROUND;
354                 s->n_chan = 16;
355                 s->maxdata = 1;
356                 s->len_chanlist = 1;
357                 s->insn_bits = pcl726_di_insn_bits;
358                 s->range_table = &range_digital;
359         }
360
361         s = &dev->subdevices[2];
362         /* do */
363         if (!board->have_dio) {
364                 s->type = COMEDI_SUBD_UNUSED;
365         } else {
366                 s->type = COMEDI_SUBD_DO;
367                 s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
368                 s->n_chan = 16;
369                 s->maxdata = 1;
370                 s->len_chanlist = 1;
371                 s->insn_bits = pcl726_do_insn_bits;
372                 s->range_table = &range_digital;
373         }
374
375         return 0;
376 }
377
378 static struct comedi_driver pcl726_driver = {
379         .driver_name    = "pcl726",
380         .module         = THIS_MODULE,
381         .attach         = pcl726_attach,
382         .detach         = comedi_legacy_detach,
383         .board_name     = &boardtypes[0].name,
384         .num_names      = ARRAY_SIZE(boardtypes),
385         .offset         = sizeof(struct pcl726_board),
386 };
387 module_comedi_driver(pcl726_driver);
388
389 MODULE_AUTHOR("Comedi http://www.comedi.org");
390 MODULE_DESCRIPTION("Comedi low-level driver");
391 MODULE_LICENSE("GPL");