staging: comedi: pcl726: remove the *_SIZE defines
[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 <linux/interrupt.h>
67
68 #include "../comedidev.h"
69
70 #define PCL726_AO_MSB_REG(x)    (0x00 + ((x) * 2))
71 #define PCL726_AO_LSB_REG(x)    (0x01 + ((x) * 2))
72 #define PCL726_DO_MSB_REG       0x0c
73 #define PCL726_DO_LSB_REG       0x0d
74 #define PCL726_DI_MSB_REG       0x0e
75 #define PCL726_DI_LSB_REG       0x0f
76
77 #define PCL727_DI_MSB_REG       0x00
78 #define PCL727_DI_LSB_REG       0x01
79 #define PCL727_DO_MSB_REG       0x18
80 #define PCL727_DO_LSB_REG       0x19
81
82 static const struct comedi_lrange *const rangelist_726[] = {
83         &range_unipolar5,
84         &range_unipolar10,
85         &range_bipolar5,
86         &range_bipolar10,
87         &range_4_20mA,
88         &range_unknown
89 };
90
91 static const struct comedi_lrange *const rangelist_727[] = {
92         &range_unipolar5,
93         &range_unipolar10,
94         &range_bipolar5,
95         &range_4_20mA
96 };
97
98 static const struct comedi_lrange *const rangelist_728[] = {
99         &range_unipolar5,
100         &range_unipolar10,
101         &range_bipolar5,
102         &range_bipolar10,
103         &range_4_20mA,
104         &range_0_20mA
105 };
106
107 struct pcl726_board {
108         const char *name;
109         unsigned long io_len;
110         unsigned int irq_mask;
111         int n_aochan;
112         unsigned int have_dio:1;
113         unsigned int is_pcl727:1;
114         const struct comedi_lrange *const *ao_ranges;
115         int ao_num_ranges;
116 };
117
118 static const struct pcl726_board boardtypes[] = {
119         {
120                 .name           = "pcl726",
121                 .io_len         = 0x10,
122                 .n_aochan       = 6,
123                 .have_dio       = 1,
124                 .ao_ranges      = &rangelist_726[0],
125                 .ao_num_ranges  = ARRAY_SIZE(rangelist_726),
126         }, {
127                 .name           = "pcl727",
128                 .io_len         = 0x20,
129                 .n_aochan       = 12,
130                 .have_dio       = 1,
131                 .is_pcl727      = 1,
132                 .ao_ranges      = &rangelist_727[0],
133                 .ao_num_ranges  = ARRAY_SIZE(rangelist_727),
134         }, {
135                 .name           = "pcl728",
136                 .io_len         = 0x08,
137                 .n_aochan       = 2,
138                 .ao_num_ranges  = ARRAY_SIZE(rangelist_728),
139                 .ao_ranges      = &rangelist_728[0],
140         }, {
141                 .name           = "acl6126",
142                 .io_len         = 0x10,
143                 .irq_mask       = 0x96e8,
144                 .n_aochan       = 6,
145                 .have_dio       = 1,
146                 .ao_num_ranges  = ARRAY_SIZE(rangelist_726),
147                 .ao_ranges      = &rangelist_726[0],
148         }, {
149                 .name           = "acl6128",
150                 .io_len         = 0x08,
151                 .n_aochan       = 2,
152                 .ao_num_ranges  = ARRAY_SIZE(rangelist_728),
153                 .ao_ranges      = &rangelist_728[0],
154         },
155 };
156
157 struct pcl726_private {
158         const struct comedi_lrange *rangelist[12];
159         unsigned int ao_readback[12];
160 };
161
162 static irqreturn_t pcl818_interrupt(int irq, void *d)
163 {
164         return IRQ_HANDLED;
165 }
166
167 static int pcl726_ao_insn_write(struct comedi_device *dev,
168                                 struct comedi_subdevice *s,
169                                 struct comedi_insn *insn,
170                                 unsigned int *data)
171 {
172         struct pcl726_private *devpriv = dev->private;
173         unsigned int chan = CR_CHAN(insn->chanspec);
174         unsigned int range = CR_RANGE(insn->chanspec);
175         unsigned int val;
176         int i;
177
178         for (i = 0; i < insn->n; i++) {
179                 val = data[i];
180                 devpriv->ao_readback[chan] = val;
181
182                 /* bipolar data to the DAC is two's complement */
183                 if (comedi_chan_range_is_bipolar(s, chan, range))
184                         val = comedi_offset_munge(s, val);
185
186                 /* order is important, MSB then LSB */
187                 outb((val >> 8) & 0xff, dev->iobase + PCL726_AO_MSB_REG(chan));
188                 outb(val & 0xff, dev->iobase + PCL726_AO_LSB_REG(chan));
189         }
190
191         return insn->n;
192 }
193
194 static int pcl726_ao_insn_read(struct comedi_device *dev,
195                                struct comedi_subdevice *s,
196                                struct comedi_insn *insn,
197                                unsigned int *data)
198 {
199         struct pcl726_private *devpriv = dev->private;
200         unsigned int chan = CR_CHAN(insn->chanspec);
201         int i;
202
203         for (i = 0; i < insn->n; i++)
204                 data[i] = devpriv->ao_readback[chan];
205
206         return insn->n;
207 }
208
209 static int pcl726_di_insn_bits(struct comedi_device *dev,
210                                struct comedi_subdevice *s,
211                                struct comedi_insn *insn,
212                                unsigned int *data)
213 {
214         const struct pcl726_board *board = comedi_board(dev);
215         unsigned int val;
216
217         if (board->is_pcl727) {
218                 val = inb(dev->iobase + PCL727_DI_LSB_REG);
219                 val |= (inb(dev->iobase + PCL727_DI_MSB_REG) << 8);
220         } else {
221                 val = inb(dev->iobase + PCL726_DI_LSB_REG);
222                 val |= (inb(dev->iobase + PCL726_DI_MSB_REG) << 8);
223         }
224
225         data[1] = val;
226
227         return insn->n;
228 }
229
230 static int pcl726_do_insn_bits(struct comedi_device *dev,
231                                struct comedi_subdevice *s,
232                                struct comedi_insn *insn,
233                                unsigned int *data)
234 {
235         const struct pcl726_board *board = comedi_board(dev);
236         unsigned long io = dev->iobase;
237         unsigned int mask;
238
239         mask = comedi_dio_update_state(s, data);
240         if (mask) {
241                 if (board->is_pcl727) {
242                         if (mask & 0x00ff)
243                                 outb(s->state & 0xff, io + PCL727_DO_LSB_REG);
244                         if (mask & 0xff00)
245                                 outb((s->state >> 8), io + PCL727_DO_MSB_REG);
246                 } else {
247                         if (mask & 0x00ff)
248                                 outb(s->state & 0xff, io + PCL726_DO_LSB_REG);
249                         if (mask & 0xff00)
250                                 outb((s->state >> 8), io + PCL726_DO_MSB_REG);
251                 }
252         }
253
254         data[1] = s->state;
255
256         return insn->n;
257 }
258
259 static int pcl726_attach(struct comedi_device *dev,
260                          struct comedi_devconfig *it)
261 {
262         const struct pcl726_board *board = comedi_board(dev);
263         struct pcl726_private *devpriv;
264         struct comedi_subdevice *s;
265         int ret;
266         int i;
267
268         ret = comedi_request_region(dev, it->options[0], board->io_len);
269         if (ret)
270                 return ret;
271
272         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
273         if (!devpriv)
274                 return -ENOMEM;
275
276         /*
277          * Hook up the external trigger source interrupt only if the
278          * user config option is valid and the board supports interrupts.
279          */
280         if (it->options[1] && (board->irq_mask & (1 << it->options[1]))) {
281                 ret = request_irq(it->options[1], pcl818_interrupt, 0,
282                                   dev->board_name, dev);
283                 if (ret == 0) {
284                         /* External trigger source is from Pin-17 of CN3 */
285                         dev->irq = it->options[1];
286                 }
287         }
288
289         /* setup the per-channel analog output range_table_list */
290         for (i = 0; i < 12; i++) {
291                 unsigned int opt = it->options[2 + i];
292
293                 if (opt < board->ao_num_ranges && i < board->n_aochan)
294                         devpriv->rangelist[i] = board->ao_ranges[opt];
295                 else
296                         devpriv->rangelist[i] = &range_unknown;
297         }
298
299         ret = comedi_alloc_subdevices(dev, board->have_dio ? 3 : 1);
300         if (ret)
301                 return ret;
302
303         /* Analog Output subdevice */
304         s = &dev->subdevices[0];
305         s->type         = COMEDI_SUBD_AO;
306         s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
307         s->n_chan       = board->n_aochan;
308         s->maxdata      = 0x0fff;
309         s->range_table_list = devpriv->rangelist;
310         s->insn_write   = pcl726_ao_insn_write;
311         s->insn_read    = pcl726_ao_insn_read;
312
313         if (board->have_dio) {
314                 /* Digital Input subdevice */
315                 s = &dev->subdevices[1];
316                 s->type         = COMEDI_SUBD_DI;
317                 s->subdev_flags = SDF_READABLE;
318                 s->n_chan       = 16;
319                 s->maxdata      = 1;
320                 s->insn_bits    = pcl726_di_insn_bits;
321                 s->range_table  = &range_digital;
322
323                 /* Digital Output subdevice */
324                 s = &dev->subdevices[2];
325                 s->type         = COMEDI_SUBD_DO;
326                 s->subdev_flags = SDF_WRITABLE;
327                 s->n_chan       = 16;
328                 s->maxdata      = 1;
329                 s->insn_bits    = pcl726_do_insn_bits;
330                 s->range_table  = &range_digital;
331         }
332
333         return 0;
334 }
335
336 static struct comedi_driver pcl726_driver = {
337         .driver_name    = "pcl726",
338         .module         = THIS_MODULE,
339         .attach         = pcl726_attach,
340         .detach         = comedi_legacy_detach,
341         .board_name     = &boardtypes[0].name,
342         .num_names      = ARRAY_SIZE(boardtypes),
343         .offset         = sizeof(struct pcl726_board),
344 };
345 module_comedi_driver(pcl726_driver);
346
347 MODULE_AUTHOR("Comedi http://www.comedi.org");
348 MODULE_DESCRIPTION("Comedi low-level driver");
349 MODULE_LICENSE("GPL");