]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/pcl726.c
staging: comedi: pcl726: tidy up pcl726_attach()
[karo-tx-linux.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_SIZE 16
71 #define PCL727_SIZE 32
72 #define PCL728_SIZE 8
73
74 #define PCL726_AO_MSB_REG(x)    (0x00 + ((x) * 2))
75 #define PCL726_AO_LSB_REG(x)    (0x01 + ((x) * 2))
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                 .io_range       = PCL726_SIZE,
127                 .have_dio       = 1,
128                 .di_hi          = PCL726_DI_HI,
129                 .di_lo          = PCL726_DI_LO,
130                 .do_hi          = PCL726_DO_HI,
131                 .do_lo          = PCL726_DO_LO,
132                 .range_type_list = &rangelist_726[0],
133         }, {
134                 .name           = "pcl727",
135                 .n_aochan       = 12,
136                 .num_of_ranges  = 4,
137                 .io_range       = PCL727_SIZE,
138                 .have_dio       = 1,
139                 .di_hi          = PCL727_DI_HI,
140                 .di_lo          = PCL727_DI_LO,
141                 .do_hi          = PCL727_DO_HI,
142                 .do_lo          = PCL727_DO_LO,
143                 .range_type_list = &rangelist_727[0],
144         }, {
145                 .name           = "pcl728",
146                 .n_aochan       = 2,
147                 .num_of_ranges  = 6,
148                 .io_range       = PCL728_SIZE,
149                 .range_type_list = &rangelist_728[0],
150         }, {
151                 .name           = "acl6126",
152                 .n_aochan       = 6,
153                 .num_of_ranges  = 5,
154                 .IRQbits        = 0x96e8,
155                 .io_range       = PCL726_SIZE,
156                 .have_dio       = 1,
157                 .di_hi          = PCL726_DI_HI,
158                 .di_lo          = PCL726_DI_LO,
159                 .do_hi          = PCL726_DO_HI,
160                 .do_lo          = PCL726_DO_LO,
161                 .range_type_list = &rangelist_726[0],
162         }, {
163                 .name           = "acl6128",
164                 .n_aochan       = 2,
165                 .num_of_ranges  = 6,
166                 .io_range       = PCL728_SIZE,
167                 .range_type_list = &rangelist_728[0],
168         },
169 };
170
171 struct pcl726_private {
172         const struct comedi_lrange *rangelist[12];
173         unsigned int ao_readback[12];
174 };
175
176 static irqreturn_t pcl818_interrupt(int irq, void *d)
177 {
178         return IRQ_HANDLED;
179 }
180
181 static int pcl726_ao_insn_write(struct comedi_device *dev,
182                                 struct comedi_subdevice *s,
183                                 struct comedi_insn *insn,
184                                 unsigned int *data)
185 {
186         struct pcl726_private *devpriv = dev->private;
187         unsigned int chan = CR_CHAN(insn->chanspec);
188         unsigned int range = CR_RANGE(insn->chanspec);
189         unsigned int val;
190         int i;
191
192         for (i = 0; i < insn->n; i++) {
193                 val = data[i];
194                 devpriv->ao_readback[chan] = val;
195
196                 /* bipolar data to the DAC is two's complement */
197                 if (comedi_chan_range_is_bipolar(s, chan, range))
198                         val = comedi_offset_munge(s, val);
199
200                 /* order is important, MSB then LSB */
201                 outb((val >> 8) & 0xff, dev->iobase + PCL726_AO_MSB_REG(chan));
202                 outb(val & 0xff, dev->iobase + PCL726_AO_LSB_REG(chan));
203         }
204
205         return insn->n;
206 }
207
208 static int pcl726_ao_insn_read(struct comedi_device *dev,
209                                struct comedi_subdevice *s,
210                                struct comedi_insn *insn,
211                                unsigned int *data)
212 {
213         struct pcl726_private *devpriv = dev->private;
214         unsigned int chan = CR_CHAN(insn->chanspec);
215         int i;
216
217         for (i = 0; i < insn->n; i++)
218                 data[i] = devpriv->ao_readback[chan];
219
220         return insn->n;
221 }
222
223 static int pcl726_di_insn_bits(struct comedi_device *dev,
224                                struct comedi_subdevice *s,
225                                struct comedi_insn *insn, unsigned int *data)
226 {
227         const struct pcl726_board *board = comedi_board(dev);
228
229         data[1] = inb(dev->iobase + board->di_lo) |
230             (inb(dev->iobase + board->di_hi) << 8);
231
232         return insn->n;
233 }
234
235 static int pcl726_do_insn_bits(struct comedi_device *dev,
236                                struct comedi_subdevice *s,
237                                struct comedi_insn *insn,
238                                unsigned int *data)
239 {
240         const struct pcl726_board *board = comedi_board(dev);
241         unsigned int mask;
242
243         mask = comedi_dio_update_state(s, data);
244         if (mask) {
245                 if (mask & 0x00ff)
246                         outb(s->state & 0xff, dev->iobase + board->do_lo);
247                 if (mask & 0xff00)
248                         outb((s->state >> 8), dev->iobase + board->do_hi);
249         }
250
251         data[1] = s->state;
252
253         return insn->n;
254 }
255
256 static int pcl726_attach(struct comedi_device *dev,
257                          struct comedi_devconfig *it)
258 {
259         const struct pcl726_board *board = comedi_board(dev);
260         struct pcl726_private *devpriv;
261         struct comedi_subdevice *s;
262         int ret;
263         int i;
264
265         ret = comedi_request_region(dev, it->options[0], board->io_range);
266         if (ret)
267                 return ret;
268
269         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
270         if (!devpriv)
271                 return -ENOMEM;
272
273         /*
274          * Hook up the external trigger source interrupt only if the
275          * user config option is valid and the board supports interrupts.
276          */
277         if (it->options[1] && (board->IRQbits & (1 << it->options[1]))) {
278                 ret = request_irq(it->options[1], pcl818_interrupt, 0,
279                                   dev->board_name, dev);
280                 if (ret == 0) {
281                         /* External trigger source is from Pin-17 of CN3 */
282                         dev->irq = it->options[1];
283                 }
284         }
285
286         /* setup the per-channel analog output range_table_list */
287         for (i = 0; i < 12; i++) {
288                 unsigned int opt = it->options[2 + i];
289
290                 if (opt < board->num_of_ranges && i < board->n_aochan)
291                         devpriv->rangelist[i] = board->range_type_list[opt];
292                 else
293                         devpriv->rangelist[i] = &range_unknown;
294         }
295
296         ret = comedi_alloc_subdevices(dev, board->have_dio ? 3 : 1);
297         if (ret)
298                 return ret;
299
300         /* Analog Output subdevice */
301         s = &dev->subdevices[0];
302         s->type         = COMEDI_SUBD_AO;
303         s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
304         s->n_chan       = board->n_aochan;
305         s->maxdata      = 0x0fff;
306         s->range_table_list = devpriv->rangelist;
307         s->insn_write   = pcl726_ao_insn_write;
308         s->insn_read    = pcl726_ao_insn_read;
309
310         if (board->have_dio) {
311                 /* Digital Input subdevice */
312                 s = &dev->subdevices[1];
313                 s->type         = COMEDI_SUBD_DI;
314                 s->subdev_flags = SDF_READABLE;
315                 s->n_chan       = 16;
316                 s->maxdata      = 1;
317                 s->insn_bits    = pcl726_di_insn_bits;
318                 s->range_table  = &range_digital;
319
320                 /* Digital Output subdevice */
321                 s = &dev->subdevices[2];
322                 s->type         = COMEDI_SUBD_DO;
323                 s->subdev_flags = SDF_WRITABLE;
324                 s->n_chan       = 16;
325                 s->maxdata      = 1;
326                 s->insn_bits    = pcl726_do_insn_bits;
327                 s->range_table  = &range_digital;
328         }
329
330         return 0;
331 }
332
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),
341 };
342 module_comedi_driver(pcl726_driver);
343
344 MODULE_AUTHOR("Comedi http://www.comedi.org");
345 MODULE_DESCRIPTION("Comedi low-level driver");
346 MODULE_LICENSE("GPL");