Merge branch 'next/drivers' into HEAD
[deliverable/linux.git] / drivers / staging / comedi / drivers / rti800.c
CommitLineData
fc59905e
DS
1/*
2 comedi/drivers/rti800.c
3 Hardware driver for Analog Devices RTI-800/815 board
4
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1998 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/*
24Driver: rti800
25Description: Analog Devices RTI-800/815
26Author: ds
27Status: unknown
28Updated: Fri, 05 Sep 2008 14:50:44 +0100
29Devices: [Analog Devices] RTI-800 (rti800), RTI-815 (rti815)
30
31Configuration options:
32 [0] - I/O port base address
33 [1] - IRQ
34 [2] - A/D reference
79a22d5c
BA
35 0 = differential
36 1 = pseudodifferential (common)
37 2 = single-ended
fc59905e 38 [3] - A/D range
79a22d5c
BA
39 0 = [-10,10]
40 1 = [-5,5]
41 2 = [0,10]
fc59905e 42 [4] - A/D encoding
79a22d5c
BA
43 0 = two's complement
44 1 = straight binary
fc59905e 45 [5] - DAC 0 range
79a22d5c
BA
46 0 = [-10,10]
47 1 = [0,10]
fc59905e 48 [6] - DAC 0 encoding
79a22d5c
BA
49 0 = two's complement
50 1 = straight binary
fc59905e
DS
51 [7] - DAC 1 range (same as DAC 0)
52 [8] - DAC 1 encoding (same as DAC 0)
53*/
54
25436dc9 55#include <linux/interrupt.h>
fc59905e
DS
56#include "../comedidev.h"
57
58#include <linux/ioport.h>
59
60#define RTI800_SIZE 16
61
62#define RTI800_CSR 0
63#define RTI800_MUXGAIN 1
64#define RTI800_CONVERT 2
65#define RTI800_ADCLO 3
66#define RTI800_ADCHI 4
67#define RTI800_DAC0LO 5
68#define RTI800_DAC0HI 6
69#define RTI800_DAC1LO 7
70#define RTI800_DAC1HI 8
71#define RTI800_CLRFLAGS 9
72#define RTI800_DI 10
73#define RTI800_DO 11
74#define RTI800_9513A_DATA 12
75#define RTI800_9513A_CNTRL 13
76#define RTI800_9513A_STATUS 13
77
78/*
79 * flags for CSR register
80 */
81
82#define RTI800_BUSY 0x80
83#define RTI800_DONE 0x40
84#define RTI800_OVERRUN 0x20
85#define RTI800_TCR 0x10
86#define RTI800_DMA_ENAB 0x08
87#define RTI800_INTR_TC 0x04
88#define RTI800_INTR_EC 0x02
89#define RTI800_INTR_OVRN 0x01
90
91#define Am9513_8BITBUS
92
f7cbd7aa
BP
93#define Am9513_output_control(a) outb(a, dev->iobase+RTI800_9513A_CNTRL)
94#define Am9513_output_data(a) outb(a, dev->iobase+RTI800_9513A_DATA)
fc59905e
DS
95#define Am9513_input_data() inb(dev->iobase+RTI800_9513A_DATA)
96#define Am9513_input_status() inb(dev->iobase+RTI800_9513A_STATUS)
97
98#include "am9513.h"
99
9ced1de6 100static const struct comedi_lrange range_rti800_ai_10_bipolar = { 4, {
0a85b6f0
MT
101 BIP_RANGE
102 (10),
103 BIP_RANGE
104 (1),
105 BIP_RANGE
106 (0.1),
107 BIP_RANGE
108 (0.02)
109 }
fc59905e 110};
0a85b6f0 111
9ced1de6 112static const struct comedi_lrange range_rti800_ai_5_bipolar = { 4, {
0a85b6f0
MT
113 BIP_RANGE
114 (5),
115 BIP_RANGE
116 (0.5),
117 BIP_RANGE
118 (0.05),
119 BIP_RANGE
120 (0.01)
121 }
fc59905e 122};
0a85b6f0 123
9ced1de6 124static const struct comedi_lrange range_rti800_ai_unipolar = { 4, {
0a85b6f0
MT
125 UNI_RANGE
126 (10),
127 UNI_RANGE(1),
128 UNI_RANGE
129 (0.1),
130 UNI_RANGE
131 (0.02)
132 }
fc59905e
DS
133};
134
1de6a6fd
BP
135struct rti800_board {
136
fc59905e
DS
137 const char *name;
138 int has_ao;
1de6a6fd
BP
139};
140
70265d24 141static irqreturn_t rti800_interrupt(int irq, void *dev);
fc59905e 142
9335f261 143struct rti800_private {
fc59905e
DS
144 enum {
145 adc_diff, adc_pseudodiff, adc_singleended
146 } adc_mux;
147 enum {
148 adc_bipolar10, adc_bipolar5, adc_unipolar10
149 } adc_range;
150 enum {
151 adc_2comp, adc_straight
152 } adc_coding;
153 enum {
154 dac_bipolar10, dac_unipolar10
155 } dac0_range, dac1_range;
156 enum {
157 dac_2comp, dac_straight
158 } dac0_coding, dac1_coding;
9ced1de6 159 const struct comedi_lrange *ao_range_type_list[2];
790c5541 160 unsigned int ao_readback[2];
fc59905e 161 int muxgain_bits;
9335f261 162};
fc59905e 163
9335f261 164#define devpriv ((struct rti800_private *)dev->private)
fc59905e
DS
165
166#define RTI800_TIMEOUT 100
167
70265d24 168static irqreturn_t rti800_interrupt(int irq, void *dev)
fc59905e
DS
169{
170 return IRQ_HANDLED;
171}
172
2696fb57 173/* settling delay times in usec for different gains */
fc59905e
DS
174static const int gaindelay[] = { 10, 20, 40, 80 };
175
0a85b6f0
MT
176static int rti800_ai_insn_read(struct comedi_device *dev,
177 struct comedi_subdevice *s,
178 struct comedi_insn *insn, unsigned int *data)
fc59905e
DS
179{
180 int i, t;
181 int status;
182 int chan = CR_CHAN(insn->chanspec);
183 unsigned gain = CR_RANGE(insn->chanspec);
184 unsigned muxgain_bits;
185
186 inb(dev->iobase + RTI800_ADCHI);
187 outb(0, dev->iobase + RTI800_CLRFLAGS);
188
189 muxgain_bits = chan | (gain << 5);
190 if (muxgain_bits != devpriv->muxgain_bits) {
191 devpriv->muxgain_bits = muxgain_bits;
192 outb(devpriv->muxgain_bits, dev->iobase + RTI800_MUXGAIN);
193 /* without a delay here, the RTI_OVERRUN bit
194 * gets set, and you will have an error. */
195 if (insn->n > 0) {
8629efa4 196 BUG_ON(gain >= ARRAY_SIZE(gaindelay));
5f74ea14 197 udelay(gaindelay[gain]);
fc59905e
DS
198 }
199 }
200
201 for (i = 0; i < insn->n; i++) {
202 outb(0, dev->iobase + RTI800_CONVERT);
203 for (t = RTI800_TIMEOUT; t; t--) {
204 status = inb(dev->iobase + RTI800_CSR);
205 if (status & RTI800_OVERRUN) {
326bdc65 206 printk(KERN_WARNING "rti800: a/d overrun\n");
fc59905e
DS
207 outb(0, dev->iobase + RTI800_CLRFLAGS);
208 return -EIO;
209 }
210 if (status & RTI800_DONE)
211 break;
5f74ea14 212 udelay(1);
fc59905e
DS
213 }
214 if (t == 0) {
326bdc65 215 printk(KERN_WARNING "rti800: timeout\n");
fc59905e
DS
216 return -ETIME;
217 }
218 data[i] = inb(dev->iobase + RTI800_ADCLO);
219 data[i] |= (0xf & inb(dev->iobase + RTI800_ADCHI)) << 8;
220
79a22d5c 221 if (devpriv->adc_coding == adc_2comp)
fc59905e 222 data[i] ^= 0x800;
fc59905e
DS
223 }
224
225 return i;
226}
227
0a85b6f0
MT
228static int rti800_ao_insn_read(struct comedi_device *dev,
229 struct comedi_subdevice *s,
230 struct comedi_insn *insn, unsigned int *data)
fc59905e
DS
231{
232 int i;
233 int chan = CR_CHAN(insn->chanspec);
234
235 for (i = 0; i < insn->n; i++)
236 data[i] = devpriv->ao_readback[chan];
237
238 return i;
239}
240
0a85b6f0
MT
241static int rti800_ao_insn_write(struct comedi_device *dev,
242 struct comedi_subdevice *s,
243 struct comedi_insn *insn, unsigned int *data)
fc59905e
DS
244{
245 int chan = CR_CHAN(insn->chanspec);
246 int d;
247 int i;
248
249 for (i = 0; i < insn->n; i++) {
250 devpriv->ao_readback[chan] = d = data[i];
79a22d5c 251 if (devpriv->dac0_coding == dac_2comp)
fc59905e 252 d ^= 0x800;
79a22d5c 253
fc59905e 254 outb(d & 0xff,
0a85b6f0 255 dev->iobase + (chan ? RTI800_DAC1LO : RTI800_DAC0LO));
fc59905e 256 outb(d >> 8,
0a85b6f0 257 dev->iobase + (chan ? RTI800_DAC1HI : RTI800_DAC0HI));
fc59905e
DS
258 }
259 return i;
260}
261
0a85b6f0
MT
262static int rti800_di_insn_bits(struct comedi_device *dev,
263 struct comedi_subdevice *s,
264 struct comedi_insn *insn, unsigned int *data)
fc59905e 265{
fc59905e 266 data[1] = inb(dev->iobase + RTI800_DI);
a2714e3e 267 return insn->n;
fc59905e
DS
268}
269
0a85b6f0
MT
270static int rti800_do_insn_bits(struct comedi_device *dev,
271 struct comedi_subdevice *s,
272 struct comedi_insn *insn, unsigned int *data)
fc59905e 273{
fc59905e
DS
274 if (data[0]) {
275 s->state &= ~data[0];
276 s->state |= data[0] & data[1];
277 /* Outputs are inverted... */
278 outb(s->state ^ 0xff, dev->iobase + RTI800_DO);
279 }
280
281 data[1] = s->state;
282
a2714e3e 283 return insn->n;
fc59905e
DS
284}
285
286/*
287 options[0] - I/O port
288 options[1] - irq
289 options[2] - a/d mux
79a22d5c 290 0=differential, 1=pseudodiff, 2=single
fc59905e 291 options[3] - a/d range
79a22d5c 292 0=bipolar10, 1=bipolar5, 2=unipolar10
fc59905e 293 options[4] - a/d coding
79a22d5c 294 0=2's comp, 1=straight binary
fc59905e 295 options[5] - dac0 range
79a22d5c 296 0=bipolar10, 1=unipolar10
fc59905e 297 options[6] - dac0 coding
79a22d5c 298 0=2's comp, 1=straight binary
fc59905e
DS
299 options[7] - dac1 range
300 options[8] - dac1 coding
301 */
302
da91b269 303static int rti800_attach(struct comedi_device *dev, struct comedi_devconfig *it)
fc59905e 304{
354c00a6 305 const struct rti800_board *board = comedi_board(dev);
fc59905e
DS
306 unsigned int irq;
307 unsigned long iobase;
308 int ret;
34c43922 309 struct comedi_subdevice *s;
fc59905e
DS
310
311 iobase = it->options[0];
326bdc65 312 printk(KERN_INFO "comedi%d: rti800: 0x%04lx\n", dev->minor, iobase);
fc59905e 313 if (!request_region(iobase, RTI800_SIZE, "rti800")) {
326bdc65 314 printk(KERN_WARNING "I/O port conflict\n");
fc59905e
DS
315 return -EIO;
316 }
317 dev->iobase = iobase;
318
319#ifdef DEBUG
326bdc65 320 printk(KERN_DEBUG "fingerprint=%x,%x,%x,%x,%x ",
0a85b6f0
MT
321 inb(dev->iobase + 0),
322 inb(dev->iobase + 1),
323 inb(dev->iobase + 2),
324 inb(dev->iobase + 3), inb(dev->iobase + 4));
fc59905e
DS
325#endif
326
327 outb(0, dev->iobase + RTI800_CSR);
328 inb(dev->iobase + RTI800_ADCHI);
329 outb(0, dev->iobase + RTI800_CLRFLAGS);
330
331 irq = it->options[1];
332 if (irq) {
326bdc65 333 printk(KERN_INFO "( irq = %u )\n", irq);
5f74ea14 334 ret = request_irq(irq, rti800_interrupt, 0, "rti800", dev);
c3744138 335 if (ret < 0) {
326bdc65 336 printk(KERN_WARNING " Failed to allocate IRQ\n");
fc59905e
DS
337 return ret;
338 }
339 dev->irq = irq;
340 } else {
326bdc65 341 printk(KERN_INFO "( no irq )\n");
fc59905e
DS
342 }
343
354c00a6 344 dev->board_name = board->name;
fc59905e 345
2f0b9d08 346 ret = comedi_alloc_subdevices(dev, 4);
8b6c5694 347 if (ret)
fc59905e 348 return ret;
c3744138
BP
349
350 ret = alloc_private(dev, sizeof(struct rti800_private));
351 if (ret < 0)
fc59905e
DS
352 return ret;
353
354 devpriv->adc_mux = it->options[2];
355 devpriv->adc_range = it->options[3];
356 devpriv->adc_coding = it->options[4];
357 devpriv->dac0_range = it->options[5];
358 devpriv->dac0_coding = it->options[6];
359 devpriv->dac1_range = it->options[7];
360 devpriv->dac1_coding = it->options[8];
361 devpriv->muxgain_bits = -1;
362
13bee03e 363 s = &dev->subdevices[0];
fc59905e
DS
364 /* ai subdevice */
365 s->type = COMEDI_SUBD_AI;
366 s->subdev_flags = SDF_READABLE | SDF_GROUND;
367 s->n_chan = (devpriv->adc_mux ? 16 : 8);
368 s->insn_read = rti800_ai_insn_read;
369 s->maxdata = 0xfff;
370 switch (devpriv->adc_range) {
371 case adc_bipolar10:
372 s->range_table = &range_rti800_ai_10_bipolar;
373 break;
374 case adc_bipolar5:
375 s->range_table = &range_rti800_ai_5_bipolar;
376 break;
377 case adc_unipolar10:
378 s->range_table = &range_rti800_ai_unipolar;
379 break;
380 }
381
13bee03e 382 s = &dev->subdevices[1];
354c00a6 383 if (board->has_ao) {
fc59905e
DS
384 /* ao subdevice (only on rti815) */
385 s->type = COMEDI_SUBD_AO;
386 s->subdev_flags = SDF_WRITABLE;
387 s->n_chan = 2;
388 s->insn_read = rti800_ao_insn_read;
389 s->insn_write = rti800_ao_insn_write;
390 s->maxdata = 0xfff;
391 s->range_table_list = devpriv->ao_range_type_list;
392 switch (devpriv->dac0_range) {
393 case dac_bipolar10:
394 devpriv->ao_range_type_list[0] = &range_bipolar10;
395 break;
396 case dac_unipolar10:
397 devpriv->ao_range_type_list[0] = &range_unipolar10;
398 break;
399 }
400 switch (devpriv->dac1_range) {
401 case dac_bipolar10:
402 devpriv->ao_range_type_list[1] = &range_bipolar10;
403 break;
404 case dac_unipolar10:
405 devpriv->ao_range_type_list[1] = &range_unipolar10;
406 break;
407 }
408 } else {
409 s->type = COMEDI_SUBD_UNUSED;
410 }
411
13bee03e 412 s = &dev->subdevices[2];
fc59905e
DS
413 /* di */
414 s->type = COMEDI_SUBD_DI;
415 s->subdev_flags = SDF_READABLE;
416 s->n_chan = 8;
417 s->insn_bits = rti800_di_insn_bits;
418 s->maxdata = 1;
419 s->range_table = &range_digital;
420
13bee03e 421 s = &dev->subdevices[3];
fc59905e
DS
422 /* do */
423 s->type = COMEDI_SUBD_DO;
424 s->subdev_flags = SDF_WRITABLE;
425 s->n_chan = 8;
426 s->insn_bits = rti800_do_insn_bits;
427 s->maxdata = 1;
428 s->range_table = &range_digital;
429
430/* don't yet know how to deal with counter/timers */
431#if 0
13bee03e 432 s = &dev->subdevices[4];
fc59905e
DS
433 /* do */
434 s->type = COMEDI_SUBD_TIMER;
435#endif
436
fc59905e
DS
437 return 0;
438}
439
484ecc95 440static void rti800_detach(struct comedi_device *dev)
fc59905e 441{
fc59905e
DS
442 if (dev->iobase)
443 release_region(dev->iobase, RTI800_SIZE);
fc59905e 444 if (dev->irq)
5f74ea14 445 free_irq(dev->irq, dev);
fc59905e 446}
90f703d3 447
69d7c49e
HS
448static const struct rti800_board boardtypes[] = {
449 { "rti800", 0 },
450 { "rti815", 1 },
451};
452
294f930d 453static struct comedi_driver rti800_driver = {
69d7c49e
HS
454 .driver_name = "rti800",
455 .module = THIS_MODULE,
456 .attach = rti800_attach,
457 .detach = rti800_detach,
458 .num_names = ARRAY_SIZE(boardtypes),
459 .board_name = &boardtypes[0].name,
460 .offset = sizeof(struct rti800_board),
461};
294f930d 462module_comedi_driver(rti800_driver);
69d7c49e 463
90f703d3
AT
464MODULE_AUTHOR("Comedi http://www.comedi.org");
465MODULE_DESCRIPTION("Comedi low-level driver");
466MODULE_LICENSE("GPL");
This page took 0.377055 seconds and 5 git commands to generate.