2 comedi/drivers/multiq3.c
3 Hardware driver for Quanser Consulting MultiQ-3 board
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1999 Anders Blomdell <anders.blomdell@control.lth.se>
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.
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.
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.
25 Description: Quanser Consulting MultiQ-3
26 Author: Anders Blomdell <anders.blomdell@control.lth.se>
28 Devices: [Quanser Consulting] MultiQ-3 (multiq3)
32 #include <linux/interrupt.h>
33 #include "../comedidev.h"
35 #include <linux/ioport.h>
37 #define MULTIQ3_SIZE 16
40 * MULTIQ-3 port offsets
42 #define MULTIQ3_DIGIN_PORT 0
43 #define MULTIQ3_DIGOUT_PORT 0
44 #define MULTIQ3_DAC_DATA 2
45 #define MULTIQ3_AD_DATA 4
46 #define MULTIQ3_AD_CS 4
47 #define MULTIQ3_STATUS 6
48 #define MULTIQ3_CONTROL 6
49 #define MULTIQ3_CLK_DATA 8
50 #define MULTIQ3_ENC_DATA 12
51 #define MULTIQ3_ENC_CONTROL 14
54 * flags for CONTROL register
56 #define MULTIQ3_AD_MUX_EN 0x0040
57 #define MULTIQ3_AD_AUTOZ 0x0080
58 #define MULTIQ3_AD_AUTOCAL 0x0100
59 #define MULTIQ3_AD_SH 0x0200
60 #define MULTIQ3_AD_CLOCK_4M 0x0400
61 #define MULTIQ3_DA_LOAD 0x1800
63 #define MULTIQ3_CONTROL_MUST 0x0600
66 * flags for STATUS register
68 #define MULTIQ3_STATUS_EOC 0x008
69 #define MULTIQ3_STATUS_EOC_I 0x010
72 * flags for encoder control
74 #define MULTIQ3_CLOCK_DATA 0x00
75 #define MULTIQ3_CLOCK_SETUP 0x18
76 #define MULTIQ3_INPUT_SETUP 0x41
77 #define MULTIQ3_QUAD_X4 0x38
78 #define MULTIQ3_BP_RESET 0x01
79 #define MULTIQ3_CNTR_RESET 0x02
80 #define MULTIQ3_TRSFRPR_CTR 0x08
81 #define MULTIQ3_TRSFRCNTR_OL 0x10
82 #define MULTIQ3_EFLAG_RESET 0x06
84 #define MULTIQ3_TIMEOUT 30
86 struct multiq3_private {
87 unsigned int ao_readback[2];
89 #define devpriv ((struct multiq3_private *)dev->private)
91 static int multiq3_ai_insn_read(struct comedi_device *dev,
92 struct comedi_subdevice *s,
93 struct comedi_insn *insn, unsigned int *data)
99 chan = CR_CHAN(insn->chanspec);
100 outw(MULTIQ3_CONTROL_MUST | MULTIQ3_AD_MUX_EN | (chan << 3),
101 dev->iobase + MULTIQ3_CONTROL);
103 for (i = 0; i < MULTIQ3_TIMEOUT; i++) {
104 if (inw(dev->iobase + MULTIQ3_STATUS) & MULTIQ3_STATUS_EOC)
107 if (i == MULTIQ3_TIMEOUT)
110 for (n = 0; n < insn->n; n++) {
111 outw(0, dev->iobase + MULTIQ3_AD_CS);
112 for (i = 0; i < MULTIQ3_TIMEOUT; i++) {
113 if (inw(dev->iobase +
114 MULTIQ3_STATUS) & MULTIQ3_STATUS_EOC_I)
117 if (i == MULTIQ3_TIMEOUT)
120 hi = inb(dev->iobase + MULTIQ3_AD_CS);
121 lo = inb(dev->iobase + MULTIQ3_AD_CS);
122 data[n] = (((hi << 8) | lo) + 0x1000) & 0x1fff;
128 static int multiq3_ao_insn_read(struct comedi_device *dev,
129 struct comedi_subdevice *s,
130 struct comedi_insn *insn, unsigned int *data)
133 int chan = CR_CHAN(insn->chanspec);
135 for (i = 0; i < insn->n; i++)
136 data[i] = devpriv->ao_readback[chan];
141 static int multiq3_ao_insn_write(struct comedi_device *dev,
142 struct comedi_subdevice *s,
143 struct comedi_insn *insn, unsigned int *data)
146 int chan = CR_CHAN(insn->chanspec);
148 for (i = 0; i < insn->n; i++) {
149 outw(MULTIQ3_CONTROL_MUST | MULTIQ3_DA_LOAD | chan,
150 dev->iobase + MULTIQ3_CONTROL);
151 outw(data[i], dev->iobase + MULTIQ3_DAC_DATA);
152 outw(MULTIQ3_CONTROL_MUST, dev->iobase + MULTIQ3_CONTROL);
154 devpriv->ao_readback[chan] = data[i];
160 static int multiq3_di_insn_bits(struct comedi_device *dev,
161 struct comedi_subdevice *s,
162 struct comedi_insn *insn, unsigned int *data)
164 data[1] = inw(dev->iobase + MULTIQ3_DIGIN_PORT);
169 static int multiq3_do_insn_bits(struct comedi_device *dev,
170 struct comedi_subdevice *s,
171 struct comedi_insn *insn, unsigned int *data)
173 s->state &= ~data[0];
174 s->state |= (data[0] & data[1]);
175 outw(s->state, dev->iobase + MULTIQ3_DIGOUT_PORT);
182 static int multiq3_encoder_insn_read(struct comedi_device *dev,
183 struct comedi_subdevice *s,
184 struct comedi_insn *insn,
188 int chan = CR_CHAN(insn->chanspec);
189 int control = MULTIQ3_CONTROL_MUST | MULTIQ3_AD_MUX_EN | (chan << 3);
191 for (n = 0; n < insn->n; n++) {
193 outw(control, dev->iobase + MULTIQ3_CONTROL);
194 outb(MULTIQ3_BP_RESET, dev->iobase + MULTIQ3_ENC_CONTROL);
195 outb(MULTIQ3_TRSFRCNTR_OL, dev->iobase + MULTIQ3_ENC_CONTROL);
196 value = inb(dev->iobase + MULTIQ3_ENC_DATA);
197 value |= (inb(dev->iobase + MULTIQ3_ENC_DATA) << 8);
198 value |= (inb(dev->iobase + MULTIQ3_ENC_DATA) << 16);
199 data[n] = (value + 0x800000) & 0xffffff;
205 static void encoder_reset(struct comedi_device *dev)
207 struct comedi_subdevice *s = &dev->subdevices[4];
210 for (chan = 0; chan < s->n_chan; chan++) {
212 MULTIQ3_CONTROL_MUST | MULTIQ3_AD_MUX_EN | (chan << 3);
213 outw(control, dev->iobase + MULTIQ3_CONTROL);
214 outb(MULTIQ3_EFLAG_RESET, dev->iobase + MULTIQ3_ENC_CONTROL);
215 outb(MULTIQ3_BP_RESET, dev->iobase + MULTIQ3_ENC_CONTROL);
216 outb(MULTIQ3_CLOCK_DATA, dev->iobase + MULTIQ3_ENC_DATA);
217 outb(MULTIQ3_CLOCK_SETUP, dev->iobase + MULTIQ3_ENC_CONTROL);
218 outb(MULTIQ3_INPUT_SETUP, dev->iobase + MULTIQ3_ENC_CONTROL);
219 outb(MULTIQ3_QUAD_X4, dev->iobase + MULTIQ3_ENC_CONTROL);
220 outb(MULTIQ3_CNTR_RESET, dev->iobase + MULTIQ3_ENC_CONTROL);
225 options[0] - I/O port
227 options[2] - number of encoder chips installed
230 static int multiq3_attach(struct comedi_device *dev,
231 struct comedi_devconfig *it)
234 unsigned long iobase;
236 struct comedi_subdevice *s;
238 iobase = it->options[0];
239 printk(KERN_INFO "comedi%d: multiq3: 0x%04lx ", dev->minor, iobase);
240 if (!request_region(iobase, MULTIQ3_SIZE, "multiq3")) {
241 printk(KERN_ERR "comedi%d: I/O port conflict\n", dev->minor);
245 dev->iobase = iobase;
247 irq = it->options[1];
249 printk(KERN_WARNING "comedi%d: irq = %u ignored\n",
252 printk(KERN_WARNING "comedi%d: no irq\n", dev->minor);
253 dev->board_name = "multiq3";
255 result = comedi_alloc_subdevices(dev, 5);
259 result = alloc_private(dev, sizeof(struct multiq3_private));
263 s = &dev->subdevices[0];
265 s->type = COMEDI_SUBD_AI;
266 s->subdev_flags = SDF_READABLE | SDF_GROUND;
268 s->insn_read = multiq3_ai_insn_read;
270 s->range_table = &range_bipolar5;
272 s = &dev->subdevices[1];
274 s->type = COMEDI_SUBD_AO;
275 s->subdev_flags = SDF_WRITABLE;
277 s->insn_read = multiq3_ao_insn_read;
278 s->insn_write = multiq3_ao_insn_write;
280 s->range_table = &range_bipolar5;
282 s = &dev->subdevices[2];
284 s->type = COMEDI_SUBD_DI;
285 s->subdev_flags = SDF_READABLE;
287 s->insn_bits = multiq3_di_insn_bits;
289 s->range_table = &range_digital;
291 s = &dev->subdevices[3];
293 s->type = COMEDI_SUBD_DO;
294 s->subdev_flags = SDF_WRITABLE;
296 s->insn_bits = multiq3_do_insn_bits;
298 s->range_table = &range_digital;
301 s = &dev->subdevices[4];
302 /* encoder (counter) subdevice */
303 s->type = COMEDI_SUBD_COUNTER;
304 s->subdev_flags = SDF_READABLE | SDF_LSAMPL;
305 s->n_chan = it->options[2] * 2;
306 s->insn_read = multiq3_encoder_insn_read;
307 s->maxdata = 0xffffff;
308 s->range_table = &range_unknown;
315 static void multiq3_detach(struct comedi_device *dev)
318 release_region(dev->iobase, MULTIQ3_SIZE);
320 free_irq(dev->irq, dev);
323 static struct comedi_driver multiq3_driver = {
324 .driver_name = "multiq3",
325 .module = THIS_MODULE,
326 .attach = multiq3_attach,
327 .detach = multiq3_detach,
329 module_comedi_driver(multiq3_driver);
331 MODULE_AUTHOR("Comedi http://www.comedi.org");
332 MODULE_DESCRIPTION("Comedi low-level driver");
333 MODULE_LICENSE("GPL");