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