Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / 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 #include <linux/module.h>
75 #include <linux/delay.h>
76
77 #include "../comedidev.h"
78
79 /* address scheme (page 2.17 of the manual) */
80 #define ADQ12B_CTREG            0x00
81 #define ADQ12B_CTREG_MSKP       (1 << 7)        /* enable pacer interrupt */
82 #define ADQ12B_CTREG_GTP        (1 << 6)        /* enable pacer */
83 #define ADQ12B_CTREG_RANGE(x)   ((x) << 4)
84 #define ADQ12B_CTREG_CHAN(x)    ((x) << 0)
85 #define ADQ12B_STINR            0x00
86 #define ADQ12B_STINR_OUT2       (1 << 7)        /* timer 2 output state */
87 #define ADQ12B_STINR_OUTP       (1 << 6)        /* pacer output state */
88 #define ADQ12B_STINR_EOC        (1 << 5)        /* A/D end-of-conversion */
89 #define ADQ12B_STINR_IN_MASK    (0x1f << 0)
90 #define ADQ12B_OUTBR            0x04
91 #define ADQ12B_ADLOW            0x08
92 #define ADQ12B_ADHIG            0x09
93 #define ADQ12B_TIMER_BASE       0x0c
94
95 /* available ranges through the PGA gains */
96 static const struct comedi_lrange range_adq12b_ai_bipolar = {
97         4, {
98                 BIP_RANGE(5),
99                 BIP_RANGE(2),
100                 BIP_RANGE(1),
101                 BIP_RANGE(0.5)
102         }
103 };
104
105 static const struct comedi_lrange range_adq12b_ai_unipolar = {
106         4, {
107                 UNI_RANGE(5),
108                 UNI_RANGE(2),
109                 UNI_RANGE(1),
110                 UNI_RANGE(0.5)
111         }
112 };
113
114 struct adq12b_private {
115         unsigned int last_ctreg;
116 };
117
118 static int adq12b_ai_eoc(struct comedi_device *dev,
119                          struct comedi_subdevice *s,
120                          struct comedi_insn *insn,
121                          unsigned long context)
122 {
123         unsigned char status;
124
125         status = inb(dev->iobase + ADQ12B_STINR);
126         if (status & ADQ12B_STINR_EOC)
127                 return 0;
128         return -EBUSY;
129 }
130
131 static int adq12b_ai_insn_read(struct comedi_device *dev,
132                                struct comedi_subdevice *s,
133                                struct comedi_insn *insn,
134                                unsigned int *data)
135 {
136         struct adq12b_private *devpriv = dev->private;
137         unsigned int chan = CR_CHAN(insn->chanspec);
138         unsigned int range = CR_RANGE(insn->chanspec);
139         unsigned int val;
140         int ret;
141         int i;
142
143         /* change channel and range only if it is different from the previous */
144         val = ADQ12B_CTREG_RANGE(range) | ADQ12B_CTREG_CHAN(chan);
145         if (val != devpriv->last_ctreg) {
146                 outb(val, dev->iobase + ADQ12B_CTREG);
147                 devpriv->last_ctreg = val;
148                 udelay(50);     /* wait for the mux to settle */
149         }
150
151         val = inb(dev->iobase + ADQ12B_ADLOW);  /* trigger A/D */
152
153         for (i = 0; i < insn->n; i++) {
154                 ret = comedi_timeout(dev, s, insn, adq12b_ai_eoc, 0);
155                 if (ret)
156                         return ret;
157
158                 val = inb(dev->iobase + ADQ12B_ADHIG) << 8;
159                 val |= inb(dev->iobase + ADQ12B_ADLOW); /* retriggers A/D */
160
161                 data[i] = val;
162         }
163
164         return insn->n;
165 }
166
167 static int adq12b_di_insn_bits(struct comedi_device *dev,
168                                struct comedi_subdevice *s,
169                                struct comedi_insn *insn, unsigned int *data)
170 {
171         /* only bits 0-4 have information about digital inputs */
172         data[1] = (inb(dev->iobase + ADQ12B_STINR) & ADQ12B_STINR_IN_MASK);
173
174         return insn->n;
175 }
176
177 static int adq12b_do_insn_bits(struct comedi_device *dev,
178                                struct comedi_subdevice *s,
179                                struct comedi_insn *insn,
180                                unsigned int *data)
181 {
182         unsigned int mask;
183         unsigned int chan;
184         unsigned int val;
185
186         mask = comedi_dio_update_state(s, data);
187         if (mask) {
188                 for (chan = 0; chan < 8; chan++) {
189                         if ((mask >> chan) & 0x01) {
190                                 val = (s->state >> chan) & 0x01;
191                                 outb((val << 3) | chan,
192                                      dev->iobase + ADQ12B_OUTBR);
193                         }
194                 }
195         }
196
197         data[1] = s->state;
198
199         return insn->n;
200 }
201
202 static int adq12b_attach(struct comedi_device *dev, struct comedi_devconfig *it)
203 {
204         struct adq12b_private *devpriv;
205         struct comedi_subdevice *s;
206         int ret;
207
208         ret = comedi_request_region(dev, it->options[0], 0x10);
209         if (ret)
210                 return ret;
211
212         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
213         if (!devpriv)
214                 return -ENOMEM;
215
216         devpriv->last_ctreg = -1;       /* force ctreg update */
217
218         ret = comedi_alloc_subdevices(dev, 3);
219         if (ret)
220                 return ret;
221
222         /* Analog Input subdevice */
223         s = &dev->subdevices[0];
224         s->type         = COMEDI_SUBD_AI;
225         if (it->options[2]) {
226                 s->subdev_flags = SDF_READABLE | SDF_DIFF;
227                 s->n_chan       = 8;
228         } else {
229                 s->subdev_flags = SDF_READABLE | SDF_GROUND;
230                 s->n_chan       = 16;
231         }
232         s->maxdata      = 0xfff;
233         s->range_table  = it->options[1] ? &range_adq12b_ai_unipolar
234                                          : &range_adq12b_ai_bipolar;
235         s->insn_read    = adq12b_ai_insn_read;
236
237         /* Digital Input subdevice */
238         s = &dev->subdevices[1];
239         s->type         = COMEDI_SUBD_DI;
240         s->subdev_flags = SDF_READABLE;
241         s->n_chan       = 5;
242         s->maxdata      = 1;
243         s->range_table  = &range_digital;
244         s->insn_bits    = adq12b_di_insn_bits;
245
246         /* Digital Output subdevice */
247         s = &dev->subdevices[2];
248         s->type         = COMEDI_SUBD_DO;
249         s->subdev_flags = SDF_WRITABLE;
250         s->n_chan       = 8;
251         s->maxdata      = 1;
252         s->range_table  = &range_digital;
253         s->insn_bits    = adq12b_do_insn_bits;
254
255         return 0;
256 }
257
258 static struct comedi_driver adq12b_driver = {
259         .driver_name    = "adq12b",
260         .module         = THIS_MODULE,
261         .attach         = adq12b_attach,
262         .detach         = comedi_legacy_detach,
263 };
264 module_comedi_driver(adq12b_driver);
265
266 MODULE_AUTHOR("Comedi http://www.comedi.org");
267 MODULE_DESCRIPTION("Comedi low-level driver");
268 MODULE_LICENSE("GPL");