]> Pileus Git - ~andy/linux/blob - drivers/staging/comedi/drivers/adq12b.c
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[~andy/linux] / 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 #define TIMEOUT        20
98
99 /* available ranges through the PGA gains */
100 static const struct comedi_lrange range_adq12b_ai_bipolar = { 4, {
101                                                                   BIP_RANGE(5),
102                                                                   BIP_RANGE(2),
103                                                                   BIP_RANGE(1),
104                                                                   BIP_RANGE(0.5)
105                                                                   }
106 };
107
108 static const struct comedi_lrange range_adq12b_ai_unipolar = { 4, {
109                                                                    UNI_RANGE(5),
110                                                                    UNI_RANGE(2),
111                                                                    UNI_RANGE(1),
112                                                                    UNI_RANGE
113                                                                    (0.5)
114                                                                    }
115 };
116
117 struct adq12b_private {
118         int unipolar;           /* option 2 of comedi_config (1 is iobase) */
119         int differential;       /* option 3 of comedi_config */
120         int last_channel;
121         int last_range;
122         unsigned int digital_state;
123 };
124
125 /*
126  * "instructions" read/write data in "one-shot" or "software-triggered"
127  * mode.
128  */
129
130 static int adq12b_ai_rinsn(struct comedi_device *dev,
131                            struct comedi_subdevice *s, struct comedi_insn *insn,
132                            unsigned int *data)
133 {
134         struct adq12b_private *devpriv = dev->private;
135         int n, i;
136         int range, channel;
137         unsigned char hi, lo, status;
138
139         /* change channel and range only if it is different from the previous */
140         range = CR_RANGE(insn->chanspec);
141         channel = CR_CHAN(insn->chanspec);
142         if (channel != devpriv->last_channel || range != devpriv->last_range) {
143                 outb((range << 4) | channel, dev->iobase + ADQ12B_CTREG);
144                 udelay(50);     /* wait for the mux to settle */
145         }
146
147         /* trigger conversion */
148         status = inb(dev->iobase + ADQ12B_ADLOW);
149
150         /* convert n samples */
151         for (n = 0; n < insn->n; n++) {
152
153                 /* wait for end of conversion */
154                 i = 0;
155                 do {
156                         /* udelay(1); */
157                         status = inb(dev->iobase + ADQ12B_STINR);
158                         status = status & ADQ12B_EOC;
159                 } while (status == 0 && ++i < TIMEOUT);
160                 /* } while (++i < 10); */
161
162                 /* read data */
163                 hi = inb(dev->iobase + ADQ12B_ADHIG);
164                 lo = inb(dev->iobase + ADQ12B_ADLOW);
165
166                 /* printk("debug: chan=%d range=%d status=%d hi=%d lo=%d\n",
167                        channel, range, status,  hi, lo); */
168                 data[n] = (hi << 8) | lo;
169
170         }
171
172         /* return the number of samples read/written */
173         return n;
174 }
175
176 static int adq12b_di_insn_bits(struct comedi_device *dev,
177                                struct comedi_subdevice *s,
178                                struct comedi_insn *insn, unsigned int *data)
179 {
180
181         /* only bits 0-4 have information about digital inputs */
182         data[1] = (inb(dev->iobase + ADQ12B_STINR) & (0x1f));
183
184         return insn->n;
185 }
186
187 static int adq12b_do_insn_bits(struct comedi_device *dev,
188                                struct comedi_subdevice *s,
189                                struct comedi_insn *insn, unsigned int *data)
190 {
191         struct adq12b_private *devpriv = dev->private;
192         int channel;
193
194         for (channel = 0; channel < 8; channel++)
195                 if (((data[0] >> channel) & 0x01) != 0)
196                         outb((((data[1] >> channel) & 0x01) << 3) | channel,
197                              dev->iobase + ADQ12B_OUTBR);
198
199         /* store information to retrieve when asked for reading */
200         if (data[0]) {
201                 devpriv->digital_state &= ~data[0];
202                 devpriv->digital_state |= (data[0] & data[1]);
203         }
204
205         data[1] = devpriv->digital_state;
206
207         return insn->n;
208 }
209
210 static int adq12b_attach(struct comedi_device *dev, struct comedi_devconfig *it)
211 {
212         struct adq12b_private *devpriv;
213         struct comedi_subdevice *s;
214         int ret;
215
216         ret = comedi_request_region(dev, it->options[0], ADQ12B_SIZE);
217         if (ret)
218                 return ret;
219
220         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
221         if (!devpriv)
222                 return -ENOMEM;
223
224         devpriv->unipolar = it->options[1];
225         devpriv->differential = it->options[2];
226         devpriv->digital_state = 0;
227         /*
228          * initialize channel and range to -1 so we make sure we
229          * always write at least once to the CTREG in the instruction
230          */
231         devpriv->last_channel = -1;
232         devpriv->last_range = -1;
233
234         ret = comedi_alloc_subdevices(dev, 3);
235         if (ret)
236                 return ret;
237
238         s = &dev->subdevices[0];
239         /* analog input subdevice */
240         s->type = COMEDI_SUBD_AI;
241         if (devpriv->differential) {
242                 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
243                 s->n_chan = 8;
244         } else {
245                 s->subdev_flags = SDF_READABLE | SDF_GROUND;
246                 s->n_chan = 16;
247         }
248
249         if (devpriv->unipolar)
250                 s->range_table = &range_adq12b_ai_unipolar;
251         else
252                 s->range_table = &range_adq12b_ai_bipolar;
253
254         s->maxdata = 0xfff;
255
256         s->len_chanlist = 4;    /* This is the maximum chanlist length that
257                                    the board can handle */
258         s->insn_read = adq12b_ai_rinsn;
259
260         s = &dev->subdevices[1];
261         /* digital input subdevice */
262         s->type = COMEDI_SUBD_DI;
263         s->subdev_flags = SDF_READABLE;
264         s->n_chan = 5;
265         s->maxdata = 1;
266         s->range_table = &range_digital;
267         s->insn_bits = adq12b_di_insn_bits;
268
269         s = &dev->subdevices[2];
270         /* digital output subdevice */
271         s->type = COMEDI_SUBD_DO;
272         s->subdev_flags = SDF_WRITABLE;
273         s->n_chan = 8;
274         s->maxdata = 1;
275         s->range_table = &range_digital;
276         s->insn_bits = adq12b_do_insn_bits;
277
278         return 0;
279 }
280
281 static struct comedi_driver adq12b_driver = {
282         .driver_name    = "adq12b",
283         .module         = THIS_MODULE,
284         .attach         = adq12b_attach,
285         .detach         = comedi_legacy_detach,
286 };
287 module_comedi_driver(adq12b_driver);
288
289 MODULE_AUTHOR("Comedi http://www.comedi.org");
290 MODULE_DESCRIPTION("Comedi low-level driver");
291 MODULE_LICENSE("GPL");