Merge branch 'serge-next-1' of git://git.kernel.org/pub/scm/linux/kernel/git/sergeh...
[cascardo/linux.git] / drivers / staging / comedi / drivers / adq12b.c
1 /*
2     comedi/drivers/adq12b.c
3     driver for MicroAxial ADQ12-B data acquisition and control card
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 */
18 /*
19 Driver: adq12b
20 Description: driver for MicroAxial ADQ12-B data acquisition and control card
21 Devices: [MicroAxial] ADQ12-B (adq12b)
22 Author: jeremy theler <thelerg@ib.cnea.gov.ar>
23 Updated: Thu, 21 Feb 2008 02:56:27 -0300
24 Status: works
25
26 Driver for the acquisition card ADQ12-B (without any add-on).
27
28  - Analog input is subdevice 0 (16 channels single-ended or 8 differential)
29  - Digital input is subdevice 1 (5 channels)
30  - Digital output is subdevice 1 (8 channels)
31  - The PACER is not supported in this version
32
33 If you do not specify any options, they will default to
34
35   # comedi_config /dev/comedi0 adq12b 0x300,0,0
36
37   option 1: I/O base address. The following table is provided as a help
38    of the hardware jumpers.
39
40          address            jumper JADR
41           0x300                 1 (factory default)
42           0x320                 2
43           0x340                 3
44           0x360                 4
45           0x380                 5
46           0x3A0                 6
47
48   option 2: unipolar/bipolar ADC selection: 0 -> bipolar, 1 -> unipolar
49
50         selection         comedi_config option            JUB
51          bipolar                0                         2-3 (factory default)
52          unipolar               1                         1-2
53
54   option 3: single-ended/differential AI selection: 0 -> SE, 1 -> differential
55
56         selection         comedi_config option     JCHA    JCHB
57        single-ended             0                  1-2     1-2 (factory default)
58        differential             1                  2-3     2-3
59
60    written by jeremy theler <thelerg@ib.cnea.gov.ar>
61
62    instituto balseiro
63    commission nacional de energia atomica
64    universidad nacional de cuyo
65    argentina
66
67    21-feb-2008
68      + changed supported devices string (missused the [] and ())
69
70    13-oct-2007
71      + first try
72
73
74 */
75
76 #include <linux/module.h>
77 #include <linux/delay.h>
78
79 #include "../comedidev.h"
80
81 /* address scheme (page 2.17 of the manual) */
82 #define ADQ12B_SIZE     16
83
84 #define ADQ12B_CTREG    0x00
85 #define ADQ12B_STINR    0x00
86 #define ADQ12B_OUTBR    0x04
87 #define ADQ12B_ADLOW    0x08
88 #define ADQ12B_ADHIG    0x09
89 #define ADQ12B_CONT0    0x0c
90 #define ADQ12B_CONT1    0x0d
91 #define ADQ12B_CONT2    0x0e
92 #define ADQ12B_COWORD   0x0f
93
94 /* mask of the bit at STINR to check end of conversion */
95 #define ADQ12B_EOC     0x20
96
97 /* available ranges through the PGA gains */
98 static const struct comedi_lrange range_adq12b_ai_bipolar = {
99         4, {
100                 BIP_RANGE(5),
101                 BIP_RANGE(2),
102                 BIP_RANGE(1),
103                 BIP_RANGE(0.5)
104         }
105 };
106
107 static const struct comedi_lrange range_adq12b_ai_unipolar = {
108         4, {
109                 UNI_RANGE(5),
110                 UNI_RANGE(2),
111                 UNI_RANGE(1),
112                 UNI_RANGE(0.5)
113         }
114 };
115
116 struct adq12b_private {
117         int unipolar;           /* option 2 of comedi_config (1 is iobase) */
118         int differential;       /* option 3 of comedi_config */
119         int last_channel;
120         int last_range;
121 };
122
123 static int adq12b_ai_eoc(struct comedi_device *dev,
124                          struct comedi_subdevice *s,
125                          struct comedi_insn *insn,
126                          unsigned long context)
127 {
128         unsigned char status;
129
130         status = inb(dev->iobase + ADQ12B_STINR);
131         if (status & ADQ12B_EOC)
132                 return 0;
133         return -EBUSY;
134 }
135
136 static int adq12b_ai_rinsn(struct comedi_device *dev,
137                            struct comedi_subdevice *s, struct comedi_insn *insn,
138                            unsigned int *data)
139 {
140         struct adq12b_private *devpriv = dev->private;
141         int n;
142         int range, channel;
143         unsigned char hi, lo, status;
144         int ret;
145
146         /* change channel and range only if it is different from the previous */
147         range = CR_RANGE(insn->chanspec);
148         channel = CR_CHAN(insn->chanspec);
149         if (channel != devpriv->last_channel || range != devpriv->last_range) {
150                 outb((range << 4) | channel, dev->iobase + ADQ12B_CTREG);
151                 udelay(50);     /* wait for the mux to settle */
152         }
153
154         /* trigger conversion */
155         status = inb(dev->iobase + ADQ12B_ADLOW);
156
157         /* convert n samples */
158         for (n = 0; n < insn->n; n++) {
159
160                 /* wait for end of conversion */
161                 ret = comedi_timeout(dev, s, insn, adq12b_ai_eoc, 0);
162                 if (ret)
163                         return ret;
164
165                 /* read data */
166                 hi = inb(dev->iobase + ADQ12B_ADHIG);
167                 lo = inb(dev->iobase + ADQ12B_ADLOW);
168
169                 data[n] = (hi << 8) | lo;
170
171         }
172
173         /* return the number of samples read/written */
174         return n;
175 }
176
177 static int adq12b_di_insn_bits(struct comedi_device *dev,
178                                struct comedi_subdevice *s,
179                                struct comedi_insn *insn, unsigned int *data)
180 {
181
182         /* only bits 0-4 have information about digital inputs */
183         data[1] = (inb(dev->iobase + ADQ12B_STINR) & (0x1f));
184
185         return insn->n;
186 }
187
188 static int adq12b_do_insn_bits(struct comedi_device *dev,
189                                struct comedi_subdevice *s,
190                                struct comedi_insn *insn,
191                                unsigned int *data)
192 {
193         unsigned int mask;
194         unsigned int chan;
195         unsigned int val;
196
197         mask = comedi_dio_update_state(s, data);
198         if (mask) {
199                 for (chan = 0; chan < 8; chan++) {
200                         if ((mask >> chan) & 0x01) {
201                                 val = (s->state >> chan) & 0x01;
202                                 outb((val << 3) | chan,
203                                      dev->iobase + ADQ12B_OUTBR);
204                         }
205                 }
206         }
207
208         data[1] = s->state;
209
210         return insn->n;
211 }
212
213 static int adq12b_attach(struct comedi_device *dev, struct comedi_devconfig *it)
214 {
215         struct adq12b_private *devpriv;
216         struct comedi_subdevice *s;
217         int ret;
218
219         ret = comedi_request_region(dev, it->options[0], ADQ12B_SIZE);
220         if (ret)
221                 return ret;
222
223         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
224         if (!devpriv)
225                 return -ENOMEM;
226
227         devpriv->unipolar = it->options[1];
228         devpriv->differential = it->options[2];
229         /*
230          * initialize channel and range to -1 so we make sure we
231          * always write at least once to the CTREG in the instruction
232          */
233         devpriv->last_channel = -1;
234         devpriv->last_range = -1;
235
236         ret = comedi_alloc_subdevices(dev, 3);
237         if (ret)
238                 return ret;
239
240         s = &dev->subdevices[0];
241         /* analog input subdevice */
242         s->type = COMEDI_SUBD_AI;
243         if (devpriv->differential) {
244                 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
245                 s->n_chan = 8;
246         } else {
247                 s->subdev_flags = SDF_READABLE | SDF_GROUND;
248                 s->n_chan = 16;
249         }
250
251         if (devpriv->unipolar)
252                 s->range_table = &range_adq12b_ai_unipolar;
253         else
254                 s->range_table = &range_adq12b_ai_bipolar;
255
256         s->maxdata = 0xfff;
257
258         s->len_chanlist = 4;    /* This is the maximum chanlist length that
259                                    the board can handle */
260         s->insn_read = adq12b_ai_rinsn;
261
262         s = &dev->subdevices[1];
263         /* digital input subdevice */
264         s->type = COMEDI_SUBD_DI;
265         s->subdev_flags = SDF_READABLE;
266         s->n_chan = 5;
267         s->maxdata = 1;
268         s->range_table = &range_digital;
269         s->insn_bits = adq12b_di_insn_bits;
270
271         s = &dev->subdevices[2];
272         /* digital output subdevice */
273         s->type = COMEDI_SUBD_DO;
274         s->subdev_flags = SDF_WRITABLE;
275         s->n_chan = 8;
276         s->maxdata = 1;
277         s->range_table = &range_digital;
278         s->insn_bits = adq12b_do_insn_bits;
279
280         return 0;
281 }
282
283 static struct comedi_driver adq12b_driver = {
284         .driver_name    = "adq12b",
285         .module         = THIS_MODULE,
286         .attach         = adq12b_attach,
287         .detach         = comedi_legacy_detach,
288 };
289 module_comedi_driver(adq12b_driver);
290
291 MODULE_AUTHOR("Comedi http://www.comedi.org");
292 MODULE_DESCRIPTION("Comedi low-level driver");
293 MODULE_LICENSE("GPL");