Commit | Line | Data |
---|---|---|
27b3f921 JT |
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 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | ||
22 | */ | |
23 | /* | |
24 | Driver: adq12b | |
25 | Description: driver for MicroAxial ADQ12-B data acquisition and control card | |
26 | Devices: [MicroAxial] ADQ12-B (adq12b) | |
27 | Author: jeremy theler <thelerg@ib.cnea.gov.ar> | |
28 | Updated: Thu, 21 Feb 2008 02:56:27 -0300 | |
29 | Status: works | |
30 | ||
31 | Driver for the acquisition card ADQ12-B (without any add-on). | |
32 | ||
33 | - Analog input is subdevice 0 (16 channels single-ended or 8 differential) | |
34 | - Digital input is subdevice 1 (5 channels) | |
35 | - Digital output is subdevice 1 (8 channels) | |
36 | - The PACER is not supported in this version | |
37 | ||
38 | If you do not specify any options, they will default to | |
39 | ||
40 | # comedi_config /dev/comedi0 adq12b 0x300,0,0 | |
41 | ||
42 | option 1: I/O base address. The following table is provided as a help | |
43 | of the hardware jumpers. | |
44 | ||
d2e01434 BA |
45 | address jumper JADR |
46 | 0x300 1 (factory default) | |
47 | 0x320 2 | |
48 | 0x340 3 | |
49 | 0x360 4 | |
50 | 0x380 5 | |
51 | 0x3A0 6 | |
27b3f921 JT |
52 | |
53 | option 2: unipolar/bipolar ADC selection: 0 -> bipolar, 1 -> unipolar | |
54 | ||
d2e01434 BA |
55 | selection comedi_config option JUB |
56 | bipolar 0 2-3 (factory default) | |
57 | unipolar 1 1-2 | |
27b3f921 JT |
58 | |
59 | option 3: single-ended/differential AI selection: 0 -> SE, 1 -> differential | |
60 | ||
d2e01434 | 61 | selection comedi_config option JCHA JCHB |
27b3f921 JT |
62 | single-ended 0 1-2 1-2 (factory default) |
63 | differential 1 2-3 2-3 | |
64 | ||
27b3f921 JT |
65 | written by jeremy theler <thelerg@ib.cnea.gov.ar> |
66 | ||
67 | instituto balseiro | |
25985edc | 68 | commission nacional de energia atomica |
27b3f921 JT |
69 | universidad nacional de cuyo |
70 | argentina | |
71 | ||
72 | 21-feb-2008 | |
73 | + changed supported devices string (missused the [] and ()) | |
74 | ||
75 | 13-oct-2007 | |
76 | + first try | |
77 | ||
78 | ||
79 | */ | |
80 | ||
81 | #include "../comedidev.h" | |
82 | ||
56e9e166 | 83 | /* address scheme (page 2.17 of the manual) */ |
27b3f921 JT |
84 | #define ADQ12B_SIZE 16 |
85 | ||
86 | #define ADQ12B_CTREG 0x00 | |
87 | #define ADQ12B_STINR 0x00 | |
88 | #define ADQ12B_OUTBR 0x04 | |
89 | #define ADQ12B_ADLOW 0x08 | |
90 | #define ADQ12B_ADHIG 0x09 | |
91 | #define ADQ12B_CONT0 0x0c | |
92 | #define ADQ12B_CONT1 0x0d | |
93 | #define ADQ12B_CONT2 0x0e | |
94 | #define ADQ12B_COWORD 0x0f | |
95 | ||
56e9e166 | 96 | /* mask of the bit at STINR to check end of conversion */ |
27b3f921 JT |
97 | #define ADQ12B_EOC 0x20 |
98 | ||
99 | #define TIMEOUT 20 | |
100 | ||
56e9e166 | 101 | /* available ranges through the PGA gains */ |
9ced1de6 | 102 | static const struct comedi_lrange range_adq12b_ai_bipolar = { 4, { |
0a85b6f0 MT |
103 | BIP_RANGE(5), |
104 | BIP_RANGE(2), | |
105 | BIP_RANGE(1), | |
106 | BIP_RANGE(0.5) | |
107 | } | |
108 | }; | |
27b3f921 | 109 | |
9ced1de6 | 110 | static const struct comedi_lrange range_adq12b_ai_unipolar = { 4, { |
0a85b6f0 MT |
111 | UNI_RANGE(5), |
112 | UNI_RANGE(2), | |
113 | UNI_RANGE(1), | |
114 | UNI_RANGE | |
115 | (0.5) | |
116 | } | |
117 | }; | |
27b3f921 | 118 | |
1ade3157 | 119 | struct adq12b_private { |
0a85b6f0 MT |
120 | int unipolar; /* option 2 of comedi_config (1 is iobase) */ |
121 | int differential; /* option 3 of comedi_config */ | |
122 | int last_channel; | |
123 | int last_range; | |
124 | unsigned int digital_state; | |
1ade3157 | 125 | }; |
27b3f921 | 126 | |
27b3f921 | 127 | /* |
632d391b HS |
128 | * "instructions" read/write data in "one-shot" or "software-triggered" |
129 | * mode. | |
27b3f921 | 130 | */ |
27b3f921 | 131 | |
0a85b6f0 MT |
132 | static int adq12b_ai_rinsn(struct comedi_device *dev, |
133 | struct comedi_subdevice *s, struct comedi_insn *insn, | |
632d391b HS |
134 | unsigned int *data) |
135 | { | |
5c1eb385 | 136 | struct adq12b_private *devpriv = dev->private; |
632d391b HS |
137 | int n, i; |
138 | int range, channel; | |
139 | unsigned char hi, lo, status; | |
140 | ||
141 | /* change channel and range only if it is different from the previous */ | |
142 | range = CR_RANGE(insn->chanspec); | |
143 | channel = CR_CHAN(insn->chanspec); | |
144 | if (channel != devpriv->last_channel || range != devpriv->last_range) { | |
145 | outb((range << 4) | channel, dev->iobase + ADQ12B_CTREG); | |
146 | udelay(50); /* wait for the mux to settle */ | |
147 | } | |
148 | ||
149 | /* trigger conversion */ | |
150 | status = inb(dev->iobase + ADQ12B_ADLOW); | |
151 | ||
152 | /* convert n samples */ | |
153 | for (n = 0; n < insn->n; n++) { | |
154 | ||
155 | /* wait for end of conversion */ | |
156 | i = 0; | |
157 | do { | |
158 | /* udelay(1); */ | |
159 | status = inb(dev->iobase + ADQ12B_STINR); | |
160 | status = status & ADQ12B_EOC; | |
161 | } while (status == 0 && ++i < TIMEOUT); | |
162 | /* } while (++i < 10); */ | |
163 | ||
164 | /* read data */ | |
165 | hi = inb(dev->iobase + ADQ12B_ADHIG); | |
166 | lo = inb(dev->iobase + ADQ12B_ADLOW); | |
167 | ||
168 | /* printk("debug: chan=%d range=%d status=%d hi=%d lo=%d\n", | |
169 | channel, range, status, hi, lo); */ | |
170 | data[n] = (hi << 8) | lo; | |
171 | ||
172 | } | |
173 | ||
174 | /* return the number of samples read/written */ | |
175 | return n; | |
176 | } | |
177 | ||
0a85b6f0 MT |
178 | static int adq12b_di_insn_bits(struct comedi_device *dev, |
179 | struct comedi_subdevice *s, | |
632d391b HS |
180 | struct comedi_insn *insn, unsigned int *data) |
181 | { | |
182 | ||
183 | /* only bits 0-4 have information about digital inputs */ | |
184 | data[1] = (inb(dev->iobase + ADQ12B_STINR) & (0x1f)); | |
185 | ||
a2714e3e | 186 | return insn->n; |
632d391b HS |
187 | } |
188 | ||
0a85b6f0 MT |
189 | static int adq12b_do_insn_bits(struct comedi_device *dev, |
190 | struct comedi_subdevice *s, | |
632d391b HS |
191 | struct comedi_insn *insn, unsigned int *data) |
192 | { | |
5c1eb385 | 193 | struct adq12b_private *devpriv = dev->private; |
632d391b HS |
194 | int channel; |
195 | ||
196 | for (channel = 0; channel < 8; channel++) | |
197 | if (((data[0] >> channel) & 0x01) != 0) | |
198 | outb((((data[1] >> channel) & 0x01) << 3) | channel, | |
199 | dev->iobase + ADQ12B_OUTBR); | |
200 | ||
201 | /* store information to retrieve when asked for reading */ | |
202 | if (data[0]) { | |
203 | devpriv->digital_state &= ~data[0]; | |
204 | devpriv->digital_state |= (data[0] & data[1]); | |
205 | } | |
206 | ||
207 | data[1] = devpriv->digital_state; | |
208 | ||
a2714e3e | 209 | return insn->n; |
632d391b | 210 | } |
27b3f921 | 211 | |
f7cbd7aa | 212 | static int adq12b_attach(struct comedi_device *dev, struct comedi_devconfig *it) |
27b3f921 | 213 | { |
5c1eb385 | 214 | struct adq12b_private *devpriv; |
0a85b6f0 MT |
215 | struct comedi_subdevice *s; |
216 | unsigned long iobase; | |
217 | int unipolar, differential; | |
8b6c5694 | 218 | int ret; |
0a85b6f0 | 219 | |
7195ea04 HS |
220 | dev->board_name = dev->driver->driver_name; |
221 | ||
0a85b6f0 MT |
222 | iobase = it->options[0]; |
223 | unipolar = it->options[1]; | |
224 | differential = it->options[2]; | |
225 | ||
7df1735b BA |
226 | printk(KERN_INFO "comedi%d: adq12b called with options base=0x%03lx, " |
227 | "%s and %s\n", dev->minor, iobase, | |
228 | (unipolar == 1) ? "unipolar" : "bipolar", | |
0a85b6f0 MT |
229 | (differential == 1) ? "differential" : "single-ended"); |
230 | ||
231 | /* if no address was specified, try the default 0x300 */ | |
232 | if (iobase == 0) { | |
7df1735b BA |
233 | printk(KERN_WARNING "comedi%d: adq12b warning: I/O base " |
234 | "address not specified. Trying the default 0x300.\n", | |
235 | dev->minor); | |
0a85b6f0 MT |
236 | iobase = 0x300; |
237 | } | |
238 | ||
239 | printk("comedi%d: adq12b: 0x%04lx ", dev->minor, iobase); | |
240 | if (!request_region(iobase, ADQ12B_SIZE, "adq12b")) { | |
241 | printk("I/O port conflict\n"); | |
242 | return -EIO; | |
243 | } | |
244 | dev->iobase = iobase; | |
27b3f921 | 245 | |
c34fa261 HS |
246 | devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL); |
247 | if (!devpriv) | |
248 | return -ENOMEM; | |
249 | dev->private = devpriv; | |
27b3f921 | 250 | |
0a85b6f0 MT |
251 | devpriv->unipolar = unipolar; |
252 | devpriv->differential = differential; | |
27b3f921 | 253 | devpriv->digital_state = 0; |
5c1eb385 HS |
254 | /* |
255 | * initialize channel and range to -1 so we make sure we | |
256 | * always write at least once to the CTREG in the instruction | |
257 | */ | |
0a85b6f0 MT |
258 | devpriv->last_channel = -1; |
259 | devpriv->last_range = -1; | |
27b3f921 | 260 | |
8b6c5694 HS |
261 | ret = comedi_alloc_subdevices(dev, 3); |
262 | if (ret) | |
263 | return ret; | |
0a85b6f0 | 264 | |
4fe92e16 | 265 | s = &dev->subdevices[0]; |
0a85b6f0 MT |
266 | /* analog input subdevice */ |
267 | s->type = COMEDI_SUBD_AI; | |
268 | if (differential) { | |
269 | s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF; | |
7195ea04 | 270 | s->n_chan = 8; |
0a85b6f0 MT |
271 | } else { |
272 | s->subdev_flags = SDF_READABLE | SDF_GROUND; | |
7195ea04 | 273 | s->n_chan = 16; |
0a85b6f0 MT |
274 | } |
275 | ||
d2e01434 | 276 | if (unipolar) |
0a85b6f0 | 277 | s->range_table = &range_adq12b_ai_unipolar; |
d2e01434 | 278 | else |
0a85b6f0 | 279 | s->range_table = &range_adq12b_ai_bipolar; |
0a85b6f0 | 280 | |
7195ea04 | 281 | s->maxdata = 0xfff; |
0a85b6f0 MT |
282 | |
283 | s->len_chanlist = 4; /* This is the maximum chanlist length that | |
284 | the board can handle */ | |
285 | s->insn_read = adq12b_ai_rinsn; | |
286 | ||
4fe92e16 | 287 | s = &dev->subdevices[1]; |
0a85b6f0 MT |
288 | /* digital input subdevice */ |
289 | s->type = COMEDI_SUBD_DI; | |
290 | s->subdev_flags = SDF_READABLE; | |
7195ea04 | 291 | s->n_chan = 5; |
0a85b6f0 MT |
292 | s->maxdata = 1; |
293 | s->range_table = &range_digital; | |
294 | s->insn_bits = adq12b_di_insn_bits; | |
295 | ||
4fe92e16 | 296 | s = &dev->subdevices[2]; |
0a85b6f0 MT |
297 | /* digital output subdevice */ |
298 | s->type = COMEDI_SUBD_DO; | |
299 | s->subdev_flags = SDF_WRITABLE; | |
7195ea04 | 300 | s->n_chan = 8; |
0a85b6f0 MT |
301 | s->maxdata = 1; |
302 | s->range_table = &range_digital; | |
303 | s->insn_bits = adq12b_do_insn_bits; | |
304 | ||
7df1735b | 305 | printk(KERN_INFO "attached\n"); |
0a85b6f0 MT |
306 | |
307 | return 0; | |
27b3f921 JT |
308 | } |
309 | ||
484ecc95 | 310 | static void adq12b_detach(struct comedi_device *dev) |
27b3f921 | 311 | { |
0a85b6f0 MT |
312 | if (dev->iobase) |
313 | release_region(dev->iobase, ADQ12B_SIZE); | |
27b3f921 JT |
314 | } |
315 | ||
294f930d | 316 | static struct comedi_driver adq12b_driver = { |
632d391b HS |
317 | .driver_name = "adq12b", |
318 | .module = THIS_MODULE, | |
319 | .attach = adq12b_attach, | |
320 | .detach = adq12b_detach, | |
632d391b | 321 | }; |
294f930d | 322 | module_comedi_driver(adq12b_driver); |
90f703d3 AT |
323 | |
324 | MODULE_AUTHOR("Comedi http://www.comedi.org"); | |
325 | MODULE_DESCRIPTION("Comedi low-level driver"); | |
326 | MODULE_LICENSE("GPL"); |