Staging: comedi: Remove C99 comments
[safe/jmp/linux-2.6] / drivers / staging / comedi / drivers / multiq3.c
1 /*
2    comedi/drivers/multiq3.c
3    Hardware driver for Quanser Consulting MultiQ-3 board
4
5    COMEDI - Linux Control and Measurement Device Interface
6    Copyright (C) 1999 Anders Blomdell <anders.blomdell@control.lth.se>
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: multiq3
25 Description: Quanser Consulting MultiQ-3
26 Author: Anders Blomdell <anders.blomdell@control.lth.se>
27 Status: works
28 Devices: [Quanser Consulting] MultiQ-3 (multiq3)
29
30 */
31
32 #include "../comedidev.h"
33
34 #include <linux/ioport.h>
35
36 #define MULTIQ3_SIZE 16
37
38 /*
39  * MULTIQ-3 port offsets
40  */
41 #define MULTIQ3_DIGIN_PORT 0
42 #define MULTIQ3_DIGOUT_PORT 0
43 #define MULTIQ3_DAC_DATA 2
44 #define MULTIQ3_AD_DATA 4
45 #define MULTIQ3_AD_CS 4
46 #define MULTIQ3_STATUS 6
47 #define MULTIQ3_CONTROL 6
48 #define MULTIQ3_CLK_DATA 8
49 #define MULTIQ3_ENC_DATA 12
50 #define MULTIQ3_ENC_CONTROL 14
51
52 /*
53  * flags for CONTROL register
54  */
55 #define MULTIQ3_AD_MUX_EN      0x0040
56 #define MULTIQ3_AD_AUTOZ       0x0080
57 #define MULTIQ3_AD_AUTOCAL     0x0100
58 #define MULTIQ3_AD_SH          0x0200
59 #define MULTIQ3_AD_CLOCK_4M    0x0400
60 #define MULTIQ3_DA_LOAD                0x1800
61
62 #define MULTIQ3_CONTROL_MUST    0x0600
63
64 /*
65  * flags for STATUS register
66  */
67 #define MULTIQ3_STATUS_EOC      0x008
68 #define MULTIQ3_STATUS_EOC_I    0x010
69
70 /*
71  * flags for encoder control
72  */
73 #define MULTIQ3_CLOCK_DATA      0x00
74 #define MULTIQ3_CLOCK_SETUP     0x18
75 #define MULTIQ3_INPUT_SETUP     0x41
76 #define MULTIQ3_QUAD_X4         0x38
77 #define MULTIQ3_BP_RESET        0x01
78 #define MULTIQ3_CNTR_RESET      0x02
79 #define MULTIQ3_TRSFRPR_CTR     0x08
80 #define MULTIQ3_TRSFRCNTR_OL    0x10
81 #define MULTIQ3_EFLAG_RESET     0x06
82
83 #define MULTIQ3_TIMEOUT 30
84
85 static int multiq3_attach(struct comedi_device * dev, struct comedi_devconfig * it);
86 static int multiq3_detach(struct comedi_device * dev);
87 static struct comedi_driver driver_multiq3 = {
88       driver_name:"multiq3",
89       module:THIS_MODULE,
90       attach:multiq3_attach,
91       detach:multiq3_detach,
92 };
93
94 COMEDI_INITCLEANUP(driver_multiq3);
95
96 struct multiq3_private {
97         unsigned int ao_readback[2];
98 };
99 #define devpriv ((struct multiq3_private *)dev->private)
100
101 static int multiq3_ai_insn_read(struct comedi_device * dev, struct comedi_subdevice * s,
102         struct comedi_insn * insn, unsigned int * data)
103 {
104         int i, n;
105         int chan;
106         unsigned int hi, lo;
107
108         chan = CR_CHAN(insn->chanspec);
109         outw(MULTIQ3_CONTROL_MUST | MULTIQ3_AD_MUX_EN | (chan << 3),
110                 dev->iobase + MULTIQ3_CONTROL);
111
112         for (i = 0; i < MULTIQ3_TIMEOUT; i++) {
113                 if (inw(dev->iobase + MULTIQ3_STATUS) & MULTIQ3_STATUS_EOC)
114                         break;
115         }
116         if (i == MULTIQ3_TIMEOUT)
117                 return -ETIMEDOUT;
118
119         for (n = 0; n < insn->n; n++) {
120                 outw(0, dev->iobase + MULTIQ3_AD_CS);
121                 for (i = 0; i < MULTIQ3_TIMEOUT; i++) {
122                         if (inw(dev->iobase +
123                                         MULTIQ3_STATUS) & MULTIQ3_STATUS_EOC_I)
124                                 break;
125                 }
126                 if (i == MULTIQ3_TIMEOUT)
127                         return -ETIMEDOUT;
128
129                 hi = inb(dev->iobase + MULTIQ3_AD_CS);
130                 lo = inb(dev->iobase + MULTIQ3_AD_CS);
131                 data[n] = (((hi << 8) | lo) + 0x1000) & 0x1fff;
132         }
133
134         return n;
135 }
136
137 static int multiq3_ao_insn_read(struct comedi_device * dev, struct comedi_subdevice * s,
138         struct comedi_insn * insn, unsigned int * data)
139 {
140         int i;
141         int chan = CR_CHAN(insn->chanspec);
142
143         for (i = 0; i < insn->n; i++) {
144                 data[i] = devpriv->ao_readback[chan];
145         }
146
147         return i;
148 }
149
150 static int multiq3_ao_insn_write(struct comedi_device * dev, struct comedi_subdevice * s,
151         struct comedi_insn * insn, unsigned int * data)
152 {
153         int i;
154         int chan = CR_CHAN(insn->chanspec);
155
156         for (i = 0; i < insn->n; i++) {
157                 outw(MULTIQ3_CONTROL_MUST | MULTIQ3_DA_LOAD | chan,
158                         dev->iobase + MULTIQ3_CONTROL);
159                 outw(data[i], dev->iobase + MULTIQ3_DAC_DATA);
160                 outw(MULTIQ3_CONTROL_MUST, dev->iobase + MULTIQ3_CONTROL);
161
162                 devpriv->ao_readback[chan] = data[i];
163         }
164
165         return i;
166 }
167
168 static int multiq3_di_insn_bits(struct comedi_device * dev, struct comedi_subdevice * s,
169         struct comedi_insn * insn, unsigned int * data)
170 {
171         if (insn->n != 2)
172                 return -EINVAL;
173
174         data[1] = inw(dev->iobase + MULTIQ3_DIGIN_PORT);
175
176         return 2;
177 }
178
179 static int multiq3_do_insn_bits(struct comedi_device * dev, struct comedi_subdevice * s,
180         struct comedi_insn * insn, unsigned int * data)
181 {
182         if (insn->n != 2)
183                 return -EINVAL;
184
185         s->state &= ~data[0];
186         s->state |= (data[0] & data[1]);
187         outw(s->state, dev->iobase + MULTIQ3_DIGOUT_PORT);
188
189         data[1] = s->state;
190
191         return 2;
192 }
193
194 static int multiq3_encoder_insn_read(struct comedi_device * dev, struct comedi_subdevice * s,
195         struct comedi_insn * insn, unsigned int * data)
196 {
197         int n;
198         int chan = CR_CHAN(insn->chanspec);
199         int control = MULTIQ3_CONTROL_MUST | MULTIQ3_AD_MUX_EN | (chan << 3);
200
201         for (n = 0; n < insn->n; n++) {
202                 int value;
203                 outw(control, dev->iobase + MULTIQ3_CONTROL);
204                 outb(MULTIQ3_BP_RESET, dev->iobase + MULTIQ3_ENC_CONTROL);
205                 outb(MULTIQ3_TRSFRCNTR_OL, dev->iobase + MULTIQ3_ENC_CONTROL);
206                 value = inb(dev->iobase + MULTIQ3_ENC_DATA);
207                 value |= (inb(dev->iobase + MULTIQ3_ENC_DATA) << 8);
208                 value |= (inb(dev->iobase + MULTIQ3_ENC_DATA) << 16);
209                 data[n] = (value + 0x800000) & 0xffffff;
210         }
211
212         return n;
213 }
214
215 static void encoder_reset(struct comedi_device * dev)
216 {
217         int chan;
218         for (chan = 0; chan < dev->subdevices[4].n_chan; chan++) {
219                 int control =
220                         MULTIQ3_CONTROL_MUST | MULTIQ3_AD_MUX_EN | (chan << 3);
221                 outw(control, dev->iobase + MULTIQ3_CONTROL);
222                 outb(MULTIQ3_EFLAG_RESET, dev->iobase + MULTIQ3_ENC_CONTROL);
223                 outb(MULTIQ3_BP_RESET, dev->iobase + MULTIQ3_ENC_CONTROL);
224                 outb(MULTIQ3_CLOCK_DATA, dev->iobase + MULTIQ3_ENC_DATA);
225                 outb(MULTIQ3_CLOCK_SETUP, dev->iobase + MULTIQ3_ENC_CONTROL);
226                 outb(MULTIQ3_INPUT_SETUP, dev->iobase + MULTIQ3_ENC_CONTROL);
227                 outb(MULTIQ3_QUAD_X4, dev->iobase + MULTIQ3_ENC_CONTROL);
228                 outb(MULTIQ3_CNTR_RESET, dev->iobase + MULTIQ3_ENC_CONTROL);
229         }
230 }
231
232 /*
233    options[0] - I/O port
234    options[1] - irq
235    options[2] - number of encoder chips installed
236  */
237
238 static int multiq3_attach(struct comedi_device * dev, struct comedi_devconfig * it)
239 {
240         int result = 0;
241         unsigned long iobase;
242         unsigned int irq;
243         struct comedi_subdevice *s;
244
245         iobase = it->options[0];
246         printk("comedi%d: multiq3: 0x%04lx ", dev->minor, iobase);
247         if (!request_region(iobase, MULTIQ3_SIZE, "multiq3")) {
248                 printk("comedi%d: I/O port conflict\n", dev->minor);
249                 return -EIO;
250         }
251
252         dev->iobase = iobase;
253
254         irq = it->options[1];
255         if (irq) {
256                 printk("comedi%d: irq = %u ignored\n", dev->minor, irq);
257         } else {
258                 printk("comedi%d: no irq\n", dev->minor);
259         }
260         dev->board_name = "multiq3";
261         result = alloc_subdevices(dev, 5);
262         if (result < 0)
263                 return result;
264
265         result = alloc_private(dev, sizeof(struct multiq3_private));
266         if (result < 0)
267                 return result;
268
269         s = dev->subdevices + 0;
270         /* ai subdevice */
271         s->type = COMEDI_SUBD_AI;
272         s->subdev_flags = SDF_READABLE | SDF_GROUND;
273         s->n_chan = 8;
274         s->insn_read = multiq3_ai_insn_read;
275         s->maxdata = 0x1fff;
276         s->range_table = &range_bipolar5;
277
278         s = dev->subdevices + 1;
279         /* ao subdevice */
280         s->type = COMEDI_SUBD_AO;
281         s->subdev_flags = SDF_WRITABLE;
282         s->n_chan = 8;
283         s->insn_read = multiq3_ao_insn_read;
284         s->insn_write = multiq3_ao_insn_write;
285         s->maxdata = 0xfff;
286         s->range_table = &range_bipolar5;
287
288         s = dev->subdevices + 2;
289         /* di subdevice */
290         s->type = COMEDI_SUBD_DI;
291         s->subdev_flags = SDF_READABLE;
292         s->n_chan = 16;
293         s->insn_bits = multiq3_di_insn_bits;
294         s->maxdata = 1;
295         s->range_table = &range_digital;
296
297         s = dev->subdevices + 3;
298         /* do subdevice */
299         s->type = COMEDI_SUBD_DO;
300         s->subdev_flags = SDF_WRITABLE;
301         s->n_chan = 16;
302         s->insn_bits = multiq3_do_insn_bits;
303         s->maxdata = 1;
304         s->range_table = &range_digital;
305         s->state = 0;
306
307         s = dev->subdevices + 4;
308         /* encoder (counter) subdevice */
309         s->type = COMEDI_SUBD_COUNTER;
310         s->subdev_flags = SDF_READABLE | SDF_LSAMPL;
311         s->n_chan = it->options[2] * 2;
312         s->insn_read = multiq3_encoder_insn_read;
313         s->maxdata = 0xffffff;
314         s->range_table = &range_unknown;
315
316         encoder_reset(dev);
317
318         return 0;
319 }
320
321 static int multiq3_detach(struct comedi_device * dev)
322 {
323         printk("comedi%d: multiq3: remove\n", dev->minor);
324
325         if (dev->iobase) {
326                 release_region(dev->iobase, MULTIQ3_SIZE);
327         }
328         if (dev->irq) {
329                 free_irq(dev->irq, dev);
330         }
331
332         return 0;
333 }