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