Fix common misspellings
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / comedi / drivers / adq12b.c
CommitLineData
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/*
24Driver: adq12b
25Description: driver for MicroAxial ADQ12-B data acquisition and control card
26Devices: [MicroAxial] ADQ12-B (adq12b)
27Author: jeremy theler <thelerg@ib.cnea.gov.ar>
28Updated: Thu, 21 Feb 2008 02:56:27 -0300
29Status: works
30
31Driver 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
38If 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 102static 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 110static 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
47c6e958 119struct adq12b_board {
0a85b6f0
MT
120 const char *name;
121 int ai_se_chans;
122 int ai_diff_chans;
123 int ai_bits;
124 int di_chans;
125 int do_chans;
47c6e958 126};
27b3f921 127
47c6e958 128static const struct adq12b_board adq12b_boards[] = {
0a85b6f0
MT
129 {
130 .name = "adq12b",
131 .ai_se_chans = 16,
132 .ai_diff_chans = 8,
133 .ai_bits = 12,
134 .di_chans = 5,
135 .do_chans = 8}
56e9e166 136/* potentially, more adq-based deviced will be added */
27b3f921 137/*,
68c3dbff
BP
138 .name = "adq12b",
139 .ai_chans = 16, // this is just for reference, hardcoded again later
140 .ai_bits = 12,
141 .di_chans = 8,
142 .do_chans = 5
d2e01434 143 }*/
27b3f921
JT
144};
145
47c6e958 146#define thisboard ((const struct adq12b_board *)dev->board_ptr)
27b3f921 147
1ade3157 148struct adq12b_private {
0a85b6f0
MT
149 int unipolar; /* option 2 of comedi_config (1 is iobase) */
150 int differential; /* option 3 of comedi_config */
151 int last_channel;
152 int last_range;
153 unsigned int digital_state;
1ade3157 154};
27b3f921 155
1ade3157 156#define devpriv ((struct adq12b_private *)dev->private)
27b3f921
JT
157
158/*
139dfbdf 159 * The struct comedi_driver structure tells the Comedi core module
27b3f921
JT
160 * which functions to call to configure/deconfigure (attach/detach)
161 * the board, and also about the kernel module that contains
162 * the device code.
163 */
0a85b6f0
MT
164static int adq12b_attach(struct comedi_device *dev,
165 struct comedi_devconfig *it);
71b5f4f1 166static int adq12b_detach(struct comedi_device *dev);
d2e01434 167
0a85b6f0 168static struct comedi_driver driver_adq12b = {
d2e01434
BA
169 .driver_name = "adq12b",
170 .module = THIS_MODULE,
171 .attach = adq12b_attach,
172 .detach = adq12b_detach,
173 .board_name = &adq12b_boards[0].name,
174 .offset = sizeof(struct adq12b_board),
175 .num_names = ARRAY_SIZE(adq12b_boards),
27b3f921
JT
176};
177
0a85b6f0
MT
178static int adq12b_ai_rinsn(struct comedi_device *dev,
179 struct comedi_subdevice *s, struct comedi_insn *insn,
180 unsigned int *data);
181static int adq12b_di_insn_bits(struct comedi_device *dev,
182 struct comedi_subdevice *s,
183 struct comedi_insn *insn, unsigned int *data);
184static int adq12b_do_insn_bits(struct comedi_device *dev,
185 struct comedi_subdevice *s,
186 struct comedi_insn *insn, unsigned int *data);
27b3f921
JT
187
188/*
189 * Attach is called by the Comedi core to configure the driver
190 * for a particular board. If you specified a board_name array
191 * in the driver structure, dev->board_ptr contains that
192 * address.
193 */
f7cbd7aa 194static int adq12b_attach(struct comedi_device *dev, struct comedi_devconfig *it)
27b3f921 195{
0a85b6f0
MT
196 struct comedi_subdevice *s;
197 unsigned long iobase;
198 int unipolar, differential;
199
200 iobase = it->options[0];
201 unipolar = it->options[1];
202 differential = it->options[2];
203
7df1735b
BA
204 printk(KERN_INFO "comedi%d: adq12b called with options base=0x%03lx, "
205 "%s and %s\n", dev->minor, iobase,
206 (unipolar == 1) ? "unipolar" : "bipolar",
0a85b6f0
MT
207 (differential == 1) ? "differential" : "single-ended");
208
209 /* if no address was specified, try the default 0x300 */
210 if (iobase == 0) {
7df1735b
BA
211 printk(KERN_WARNING "comedi%d: adq12b warning: I/O base "
212 "address not specified. Trying the default 0x300.\n",
213 dev->minor);
0a85b6f0
MT
214 iobase = 0x300;
215 }
216
217 printk("comedi%d: adq12b: 0x%04lx ", dev->minor, iobase);
218 if (!request_region(iobase, ADQ12B_SIZE, "adq12b")) {
219 printk("I/O port conflict\n");
220 return -EIO;
221 }
222 dev->iobase = iobase;
27b3f921
JT
223
224/*
225 * Initialize dev->board_name. Note that we can use the "thisboard"
226 * macro now, since we just initialized it in the last line.
227 */
0a85b6f0 228 dev->board_name = thisboard->name;
27b3f921
JT
229
230/*
231 * Allocate the private structure area. alloc_private() is a
232 * convenient macro defined in comedidev.h.
233 */
0a85b6f0
MT
234 if (alloc_private(dev, sizeof(struct adq12b_private)) < 0)
235 return -ENOMEM;
27b3f921
JT
236
237/* fill in devpriv structure */
0a85b6f0
MT
238 devpriv->unipolar = unipolar;
239 devpriv->differential = differential;
27b3f921
JT
240 devpriv->digital_state = 0;
241/* initialize channel and range to -1 so we make sure we always write
242 at least once to the CTREG in the instruction */
0a85b6f0
MT
243 devpriv->last_channel = -1;
244 devpriv->last_range = -1;
27b3f921
JT
245
246/*
247 * Allocate the subdevice structures. alloc_subdevice() is a
248 * convenient macro defined in comedidev.h.
249 */
0a85b6f0
MT
250 if (alloc_subdevices(dev, 3) < 0)
251 return -ENOMEM;
252
253 s = dev->subdevices + 0;
254 /* analog input subdevice */
255 s->type = COMEDI_SUBD_AI;
256 if (differential) {
257 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
258 s->n_chan = thisboard->ai_diff_chans;
259 } else {
260 s->subdev_flags = SDF_READABLE | SDF_GROUND;
261 s->n_chan = thisboard->ai_se_chans;
262 }
263
d2e01434 264 if (unipolar)
0a85b6f0 265 s->range_table = &range_adq12b_ai_unipolar;
d2e01434 266 else
0a85b6f0 267 s->range_table = &range_adq12b_ai_bipolar;
0a85b6f0
MT
268
269 s->maxdata = (1 << thisboard->ai_bits) - 1;
270
271 s->len_chanlist = 4; /* This is the maximum chanlist length that
272 the board can handle */
273 s->insn_read = adq12b_ai_rinsn;
274
275 s = dev->subdevices + 1;
276 /* digital input subdevice */
277 s->type = COMEDI_SUBD_DI;
278 s->subdev_flags = SDF_READABLE;
279 s->n_chan = thisboard->di_chans;
280 s->maxdata = 1;
281 s->range_table = &range_digital;
282 s->insn_bits = adq12b_di_insn_bits;
283
284 s = dev->subdevices + 2;
285 /* digital output subdevice */
286 s->type = COMEDI_SUBD_DO;
287 s->subdev_flags = SDF_WRITABLE;
288 s->n_chan = thisboard->do_chans;
289 s->maxdata = 1;
290 s->range_table = &range_digital;
291 s->insn_bits = adq12b_do_insn_bits;
292
7df1735b 293 printk(KERN_INFO "attached\n");
0a85b6f0
MT
294
295 return 0;
27b3f921
JT
296}
297
27b3f921
JT
298/*
299 * _detach is called to deconfigure a device. It should deallocate
300 * resources.
301 * This function is also called when _attach() fails, so it should be
302 * careful not to release resources that were not necessarily
303 * allocated by _attach(). dev->private and dev->subdevices are
304 * deallocated automatically by the core.
305 */
71b5f4f1 306static int adq12b_detach(struct comedi_device *dev)
27b3f921 307{
0a85b6f0
MT
308 if (dev->iobase)
309 release_region(dev->iobase, ADQ12B_SIZE);
27b3f921 310
0a85b6f0 311 kfree(devpriv);
27b3f921 312
7df1735b 313 printk(KERN_INFO "comedi%d: adq12b: removed\n", dev->minor);
27b3f921 314
0a85b6f0 315 return 0;
27b3f921
JT
316}
317
318/*
319 * "instructions" read/write data in "one-shot" or "software-triggered"
320 * mode.
321 */
322
0a85b6f0
MT
323static int adq12b_ai_rinsn(struct comedi_device *dev,
324 struct comedi_subdevice *s, struct comedi_insn *insn,
325 unsigned int *data)
27b3f921 326{
0a85b6f0
MT
327 int n, i;
328 int range, channel;
329 unsigned char hi, lo, status;
330
331 /* change channel and range only if it is different from the previous */
332 range = CR_RANGE(insn->chanspec);
333 channel = CR_CHAN(insn->chanspec);
334 if (channel != devpriv->last_channel || range != devpriv->last_range) {
335 outb((range << 4) | channel, dev->iobase + ADQ12B_CTREG);
336 udelay(50); /* wait for the mux to settle */
337 }
338
339 /* trigger conversion */
340 status = inb(dev->iobase + ADQ12B_ADLOW);
341
342 /* convert n samples */
343 for (n = 0; n < insn->n; n++) {
344
25985edc 345 /* wait for end of conversion */
0a85b6f0
MT
346 i = 0;
347 do {
d2e01434 348 /* udelay(1); */
0a85b6f0
MT
349 status = inb(dev->iobase + ADQ12B_STINR);
350 status = status & ADQ12B_EOC;
351 } while (status == 0 && ++i < TIMEOUT);
d2e01434 352 /* } while (++i < 10); */
27b3f921 353
0a85b6f0
MT
354 /* read data */
355 hi = inb(dev->iobase + ADQ12B_ADHIG);
356 lo = inb(dev->iobase + ADQ12B_ADLOW);
27b3f921 357
7df1735b
BA
358 /* printk("debug: chan=%d range=%d status=%d hi=%d lo=%d\n",
359 channel, range, status, hi, lo); */
0a85b6f0 360 data[n] = (hi << 8) | lo;
27b3f921 361
0a85b6f0 362 }
27b3f921 363
0a85b6f0
MT
364 /* return the number of samples read/written */
365 return n;
27b3f921
JT
366}
367
0a85b6f0
MT
368static int adq12b_di_insn_bits(struct comedi_device *dev,
369 struct comedi_subdevice *s,
370 struct comedi_insn *insn, unsigned int *data)
27b3f921
JT
371{
372
0a85b6f0
MT
373 /* only bits 0-4 have information about digital inputs */
374 data[1] = (inb(dev->iobase + ADQ12B_STINR) & (0x1f));
27b3f921 375
0a85b6f0 376 return 2;
27b3f921
JT
377}
378
0a85b6f0
MT
379static int adq12b_do_insn_bits(struct comedi_device *dev,
380 struct comedi_subdevice *s,
381 struct comedi_insn *insn, unsigned int *data)
27b3f921 382{
0a85b6f0 383 int channel;
27b3f921
JT
384
385 for (channel = 0; channel < 8; channel++)
0a85b6f0
MT
386 if (((data[0] >> channel) & 0x01) != 0)
387 outb((((data[1] >> channel) & 0x01) << 3) | channel,
388 dev->iobase + ADQ12B_OUTBR);
27b3f921 389
0a85b6f0
MT
390 /* store information to retrieve when asked for reading */
391 if (data[0]) {
392 devpriv->digital_state &= ~data[0];
393 devpriv->digital_state |= (data[0] & data[1]);
394 }
27b3f921 395
0a85b6f0 396 data[1] = devpriv->digital_state;
27b3f921 397
0a85b6f0 398 return 2;
27b3f921
JT
399}
400
27b3f921
JT
401/*
402 * A convenient macro that defines init_module() and cleanup_module(),
403 * as necessary.
404 */
7114a280
AT
405static int __init driver_adq12b_init_module(void)
406{
407 return comedi_driver_register(&driver_adq12b);
408}
409
410static void __exit driver_adq12b_cleanup_module(void)
411{
412 comedi_driver_unregister(&driver_adq12b);
413}
414
415module_init(driver_adq12b_init_module);
416module_exit(driver_adq12b_cleanup_module);
90f703d3
AT
417
418MODULE_AUTHOR("Comedi http://www.comedi.org");
419MODULE_DESCRIPTION("Comedi low-level driver");
420MODULE_LICENSE("GPL");