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