]> Pileus Git - ~andy/linux/blob - drivers/staging/comedi/drivers/adq12b.c
staging: comedi: export alloc_subdevices as comedi_alloc_subdevices
[~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     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
45          address            jumper JADR
46           0x300                 1 (factory default)
47           0x320                 2
48           0x340                 3
49           0x360                 4
50           0x380                 5
51           0x3A0                 6
52
53   option 2: unipolar/bipolar ADC selection: 0 -> bipolar, 1 -> unipolar
54
55         selection         comedi_config option            JUB
56          bipolar                0                         2-3 (factory default)
57          unipolar               1                         1-2
58
59   option 3: single-ended/differential AI selection: 0 -> SE, 1 -> differential
60
61         selection         comedi_config option     JCHA    JCHB
62        single-ended             0                  1-2     1-2 (factory default)
63        differential             1                  2-3     2-3
64
65    written by jeremy theler <thelerg@ib.cnea.gov.ar>
66
67    instituto balseiro
68    commission nacional de energia atomica
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
83 /* address scheme (page 2.17 of the manual) */
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
96 /* mask of the bit at STINR to check end of conversion */
97 #define ADQ12B_EOC     0x20
98
99 #define TIMEOUT        20
100
101 /* available ranges through the PGA gains */
102 static const struct comedi_lrange range_adq12b_ai_bipolar = { 4, {
103                                                                   BIP_RANGE(5),
104                                                                   BIP_RANGE(2),
105                                                                   BIP_RANGE(1),
106                                                                   BIP_RANGE(0.5)
107                                                                   }
108 };
109
110 static const struct comedi_lrange range_adq12b_ai_unipolar = { 4, {
111                                                                    UNI_RANGE(5),
112                                                                    UNI_RANGE(2),
113                                                                    UNI_RANGE(1),
114                                                                    UNI_RANGE
115                                                                    (0.5)
116                                                                    }
117 };
118
119 struct adq12b_board {
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;
126 };
127
128 struct adq12b_private {
129         int unipolar;           /* option 2 of comedi_config (1 is iobase) */
130         int differential;       /* option 3 of comedi_config */
131         int last_channel;
132         int last_range;
133         unsigned int digital_state;
134 };
135
136 #define devpriv ((struct adq12b_private *)dev->private)
137
138 /*
139  * "instructions" read/write data in "one-shot" or "software-triggered"
140  * mode.
141  */
142
143 static int adq12b_ai_rinsn(struct comedi_device *dev,
144                            struct comedi_subdevice *s, struct comedi_insn *insn,
145                            unsigned int *data)
146 {
147         int n, i;
148         int range, channel;
149         unsigned char hi, lo, status;
150
151         /* change channel and range only if it is different from the previous */
152         range = CR_RANGE(insn->chanspec);
153         channel = CR_CHAN(insn->chanspec);
154         if (channel != devpriv->last_channel || range != devpriv->last_range) {
155                 outb((range << 4) | channel, dev->iobase + ADQ12B_CTREG);
156                 udelay(50);     /* wait for the mux to settle */
157         }
158
159         /* trigger conversion */
160         status = inb(dev->iobase + ADQ12B_ADLOW);
161
162         /* convert n samples */
163         for (n = 0; n < insn->n; n++) {
164
165                 /* wait for end of conversion */
166                 i = 0;
167                 do {
168                         /* udelay(1); */
169                         status = inb(dev->iobase + ADQ12B_STINR);
170                         status = status & ADQ12B_EOC;
171                 } while (status == 0 && ++i < TIMEOUT);
172                 /* } while (++i < 10); */
173
174                 /* read data */
175                 hi = inb(dev->iobase + ADQ12B_ADHIG);
176                 lo = inb(dev->iobase + ADQ12B_ADLOW);
177
178                 /* printk("debug: chan=%d range=%d status=%d hi=%d lo=%d\n",
179                        channel, range, status,  hi, lo); */
180                 data[n] = (hi << 8) | lo;
181
182         }
183
184         /* return the number of samples read/written */
185         return n;
186 }
187
188 static int adq12b_di_insn_bits(struct comedi_device *dev,
189                                struct comedi_subdevice *s,
190                                struct comedi_insn *insn, unsigned int *data)
191 {
192
193         /* only bits 0-4 have information about digital inputs */
194         data[1] = (inb(dev->iobase + ADQ12B_STINR) & (0x1f));
195
196         return 2;
197 }
198
199 static int adq12b_do_insn_bits(struct comedi_device *dev,
200                                struct comedi_subdevice *s,
201                                struct comedi_insn *insn, unsigned int *data)
202 {
203         int channel;
204
205         for (channel = 0; channel < 8; channel++)
206                 if (((data[0] >> channel) & 0x01) != 0)
207                         outb((((data[1] >> channel) & 0x01) << 3) | channel,
208                              dev->iobase + ADQ12B_OUTBR);
209
210         /* store information to retrieve when asked for reading */
211         if (data[0]) {
212                 devpriv->digital_state &= ~data[0];
213                 devpriv->digital_state |= (data[0] & data[1]);
214         }
215
216         data[1] = devpriv->digital_state;
217
218         return 2;
219 }
220
221 static int adq12b_attach(struct comedi_device *dev, struct comedi_devconfig *it)
222 {
223         const struct adq12b_board *board = comedi_board(dev);
224         struct comedi_subdevice *s;
225         unsigned long iobase;
226         int unipolar, differential;
227
228         iobase = it->options[0];
229         unipolar = it->options[1];
230         differential = it->options[2];
231
232         printk(KERN_INFO "comedi%d: adq12b called with options base=0x%03lx, "
233                "%s and %s\n", dev->minor, iobase,
234                (unipolar == 1) ? "unipolar" : "bipolar",
235                (differential == 1) ? "differential" : "single-ended");
236
237         /* if no address was specified, try the default 0x300 */
238         if (iobase == 0) {
239                 printk(KERN_WARNING "comedi%d: adq12b warning: I/O base "
240                        "address not specified. Trying the default 0x300.\n",
241                        dev->minor);
242                 iobase = 0x300;
243         }
244
245         printk("comedi%d: adq12b: 0x%04lx ", dev->minor, iobase);
246         if (!request_region(iobase, ADQ12B_SIZE, "adq12b")) {
247                 printk("I/O port conflict\n");
248                 return -EIO;
249         }
250         dev->iobase = iobase;
251
252         dev->board_name = board->name;
253
254 /*
255  * Allocate the private structure area.  alloc_private() is a
256  * convenient macro defined in comedidev.h.
257  */
258         if (alloc_private(dev, sizeof(struct adq12b_private)) < 0)
259                 return -ENOMEM;
260
261 /* fill in devpriv structure */
262         devpriv->unipolar = unipolar;
263         devpriv->differential = differential;
264         devpriv->digital_state = 0;
265 /* initialize channel and range to -1 so we make sure we always write
266    at least once to the CTREG in the instruction */
267         devpriv->last_channel = -1;
268         devpriv->last_range = -1;
269
270 /*
271  * Allocate the subdevice structures.  alloc_subdevice() is a
272  * convenient macro defined in comedidev.h.
273  */
274         if (comedi_alloc_subdevices(dev, 3) < 0)
275                 return -ENOMEM;
276
277         s = dev->subdevices + 0;
278         /* analog input subdevice */
279         s->type = COMEDI_SUBD_AI;
280         if (differential) {
281                 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
282                 s->n_chan = board->ai_diff_chans;
283         } else {
284                 s->subdev_flags = SDF_READABLE | SDF_GROUND;
285                 s->n_chan = board->ai_se_chans;
286         }
287
288         if (unipolar)
289                 s->range_table = &range_adq12b_ai_unipolar;
290         else
291                 s->range_table = &range_adq12b_ai_bipolar;
292
293         s->maxdata = (1 << board->ai_bits) - 1;
294
295         s->len_chanlist = 4;    /* This is the maximum chanlist length that
296                                    the board can handle */
297         s->insn_read = adq12b_ai_rinsn;
298
299         s = dev->subdevices + 1;
300         /* digital input subdevice */
301         s->type = COMEDI_SUBD_DI;
302         s->subdev_flags = SDF_READABLE;
303         s->n_chan = board->di_chans;
304         s->maxdata = 1;
305         s->range_table = &range_digital;
306         s->insn_bits = adq12b_di_insn_bits;
307
308         s = dev->subdevices + 2;
309         /* digital output subdevice */
310         s->type = COMEDI_SUBD_DO;
311         s->subdev_flags = SDF_WRITABLE;
312         s->n_chan = board->do_chans;
313         s->maxdata = 1;
314         s->range_table = &range_digital;
315         s->insn_bits = adq12b_do_insn_bits;
316
317         printk(KERN_INFO "attached\n");
318
319         return 0;
320 }
321
322 static void adq12b_detach(struct comedi_device *dev)
323 {
324         if (dev->iobase)
325                 release_region(dev->iobase, ADQ12B_SIZE);
326         kfree(devpriv);
327 }
328
329 static const struct adq12b_board adq12b_boards[] = {
330         {
331                 .name           = "adq12b",
332                 .ai_se_chans    = 16,
333                 .ai_diff_chans  = 8,
334                 .ai_bits        = 12,
335                 .di_chans       = 5,
336                 .do_chans       = 8,
337         },
338 };
339
340 static struct comedi_driver adq12b_driver = {
341         .driver_name    = "adq12b",
342         .module         = THIS_MODULE,
343         .attach         = adq12b_attach,
344         .detach         = adq12b_detach,
345         .board_name     = &adq12b_boards[0].name,
346         .offset         = sizeof(struct adq12b_board),
347         .num_names      = ARRAY_SIZE(adq12b_boards),
348 };
349 module_comedi_driver(adq12b_driver);
350
351 MODULE_AUTHOR("Comedi http://www.comedi.org");
352 MODULE_DESCRIPTION("Comedi low-level driver");
353 MODULE_LICENSE("GPL");