2 * comedi/drivers/rti800.c
3 * Hardware driver for Analog Devices RTI-800/815 board
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
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.
21 * Description: Analog Devices RTI-800/815
22 * Devices: (Analog Devices) RTI-800 [rti800]
23 * (Analog Devices) RTI-815 [rti815]
24 * Author: David A. Schleef <ds@schleef.org>
26 * Updated: Fri, 05 Sep 2008 14:50:44 +0100
28 * Configuration options:
29 * [0] - I/O port base address
30 * [1] - IRQ (not supported / unused)
31 * [2] - A/D mux/reference (number of channels)
33 * 1 = pseudodifferential (common)
40 * 0 = two's complement
45 * [6] - DAC 0 encoding
46 * 0 = two's complement
48 * [7] - DAC 1 range (same as DAC 0)
49 * [8] - DAC 1 encoding (same as DAC 0)
52 #include <linux/module.h>
53 #include <linux/delay.h>
54 #include <linux/interrupt.h>
55 #include "../comedidev.h"
60 #define RTI800_CSR 0x00
61 #define RTI800_CSR_BUSY (1 << 7)
62 #define RTI800_CSR_DONE (1 << 6)
63 #define RTI800_CSR_OVERRUN (1 << 5)
64 #define RTI800_CSR_TCR (1 << 4)
65 #define RTI800_CSR_DMA_ENAB (1 << 3)
66 #define RTI800_CSR_INTR_TC (1 << 2)
67 #define RTI800_CSR_INTR_EC (1 << 1)
68 #define RTI800_CSR_INTR_OVRN (1 << 0)
69 #define RTI800_MUXGAIN 0x01
70 #define RTI800_CONVERT 0x02
71 #define RTI800_ADCLO 0x03
72 #define RTI800_ADCHI 0x04
73 #define RTI800_DAC0LO 0x05
74 #define RTI800_DAC0HI 0x06
75 #define RTI800_DAC1LO 0x07
76 #define RTI800_DAC1HI 0x08
77 #define RTI800_CLRFLAGS 0x09
78 #define RTI800_DI 0x0a
79 #define RTI800_DO 0x0b
80 #define RTI800_9513A_DATA 0x0c
81 #define RTI800_9513A_CNTRL 0x0d
82 #define RTI800_9513A_STATUS 0x0d
84 #define RTI800_IOSIZE 0x10
86 static const struct comedi_lrange range_rti800_ai_10_bipolar = {
95 static const struct comedi_lrange range_rti800_ai_5_bipolar = {
104 static const struct comedi_lrange range_rti800_ai_unipolar = {
113 static const struct comedi_lrange *const rti800_ai_ranges[] = {
114 &range_rti800_ai_10_bipolar,
115 &range_rti800_ai_5_bipolar,
116 &range_rti800_ai_unipolar,
119 static const struct comedi_lrange *const rti800_ao_ranges[] = {
124 struct rti800_board {
129 static const struct rti800_board rti800_boardtypes[] = {
138 struct rti800_private {
141 const struct comedi_lrange *ao_range_type_list[2];
142 unsigned int ao_readback[2];
143 unsigned char muxgain_bits;
146 static int rti800_ai_eoc(struct comedi_device *dev,
147 struct comedi_subdevice *s,
148 struct comedi_insn *insn,
149 unsigned long context)
151 unsigned char status;
153 status = inb(dev->iobase + RTI800_CSR);
154 if (status & RTI800_CSR_OVERRUN) {
155 outb(0, dev->iobase + RTI800_CLRFLAGS);
158 if (status & RTI800_CSR_DONE)
163 static int rti800_ai_insn_read(struct comedi_device *dev,
164 struct comedi_subdevice *s,
165 struct comedi_insn *insn,
168 struct rti800_private *devpriv = dev->private;
169 unsigned int chan = CR_CHAN(insn->chanspec);
170 unsigned int gain = CR_RANGE(insn->chanspec);
171 unsigned char muxgain_bits;
175 inb(dev->iobase + RTI800_ADCHI);
176 outb(0, dev->iobase + RTI800_CLRFLAGS);
178 muxgain_bits = chan | (gain << 5);
179 if (muxgain_bits != devpriv->muxgain_bits) {
180 devpriv->muxgain_bits = muxgain_bits;
181 outb(devpriv->muxgain_bits, dev->iobase + RTI800_MUXGAIN);
183 * Without a delay here, the RTI_CSR_OVERRUN bit
184 * gets set, and you will have an error.
187 int delay = (gain == 0) ? 10 :
189 (gain == 2) ? 40 : 80;
195 for (i = 0; i < insn->n; i++) {
196 outb(0, dev->iobase + RTI800_CONVERT);
198 ret = comedi_timeout(dev, s, insn, rti800_ai_eoc, 0);
202 data[i] = inb(dev->iobase + RTI800_ADCLO);
203 data[i] |= (inb(dev->iobase + RTI800_ADCHI) & 0xf) << 8;
205 if (devpriv->adc_2comp)
212 static int rti800_ao_insn_read(struct comedi_device *dev,
213 struct comedi_subdevice *s,
214 struct comedi_insn *insn,
217 struct rti800_private *devpriv = dev->private;
218 unsigned int chan = CR_CHAN(insn->chanspec);
221 for (i = 0; i < insn->n; i++)
222 data[i] = devpriv->ao_readback[chan];
227 static int rti800_ao_insn_write(struct comedi_device *dev,
228 struct comedi_subdevice *s,
229 struct comedi_insn *insn,
232 struct rti800_private *devpriv = dev->private;
233 unsigned int chan = CR_CHAN(insn->chanspec);
234 int reg_lo = chan ? RTI800_DAC1LO : RTI800_DAC0LO;
235 int reg_hi = chan ? RTI800_DAC1HI : RTI800_DAC0HI;
236 int val = devpriv->ao_readback[chan];
239 for (i = 0; i < insn->n; i++) {
241 if (devpriv->dac_2comp[chan])
244 outb(val & 0xff, dev->iobase + reg_lo);
245 outb((val >> 8) & 0xff, dev->iobase + reg_hi);
248 devpriv->ao_readback[chan] = val;
253 static int rti800_di_insn_bits(struct comedi_device *dev,
254 struct comedi_subdevice *s,
255 struct comedi_insn *insn,
258 data[1] = inb(dev->iobase + RTI800_DI);
262 static int rti800_do_insn_bits(struct comedi_device *dev,
263 struct comedi_subdevice *s,
264 struct comedi_insn *insn,
267 if (comedi_dio_update_state(s, data)) {
268 /* Outputs are inverted... */
269 outb(s->state ^ 0xff, dev->iobase + RTI800_DO);
277 static int rti800_attach(struct comedi_device *dev, struct comedi_devconfig *it)
279 const struct rti800_board *board = comedi_board(dev);
280 struct rti800_private *devpriv;
281 struct comedi_subdevice *s;
284 ret = comedi_request_region(dev, it->options[0], RTI800_IOSIZE);
288 outb(0, dev->iobase + RTI800_CSR);
289 inb(dev->iobase + RTI800_ADCHI);
290 outb(0, dev->iobase + RTI800_CLRFLAGS);
292 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
296 devpriv->adc_2comp = (it->options[4] == 0);
297 devpriv->dac_2comp[0] = (it->options[6] == 0);
298 devpriv->dac_2comp[1] = (it->options[8] == 0);
299 /* invalid, forces the MUXGAIN register to be set when first used */
300 devpriv->muxgain_bits = 0xff;
302 ret = comedi_alloc_subdevices(dev, 4);
306 s = &dev->subdevices[0];
308 s->type = COMEDI_SUBD_AI;
309 s->subdev_flags = SDF_READABLE | SDF_GROUND;
310 s->n_chan = (it->options[2] ? 16 : 8);
311 s->insn_read = rti800_ai_insn_read;
313 s->range_table = (it->options[3] < ARRAY_SIZE(rti800_ai_ranges))
314 ? rti800_ai_ranges[it->options[3]]
317 s = &dev->subdevices[1];
319 /* ao subdevice (only on rti815) */
320 s->type = COMEDI_SUBD_AO;
321 s->subdev_flags = SDF_WRITABLE;
323 s->insn_read = rti800_ao_insn_read;
324 s->insn_write = rti800_ao_insn_write;
326 s->range_table_list = devpriv->ao_range_type_list;
327 devpriv->ao_range_type_list[0] =
328 (it->options[5] < ARRAY_SIZE(rti800_ao_ranges))
329 ? rti800_ao_ranges[it->options[5]]
331 devpriv->ao_range_type_list[1] =
332 (it->options[7] < ARRAY_SIZE(rti800_ao_ranges))
333 ? rti800_ao_ranges[it->options[7]]
336 s->type = COMEDI_SUBD_UNUSED;
339 s = &dev->subdevices[2];
341 s->type = COMEDI_SUBD_DI;
342 s->subdev_flags = SDF_READABLE;
344 s->insn_bits = rti800_di_insn_bits;
346 s->range_table = &range_digital;
348 s = &dev->subdevices[3];
350 s->type = COMEDI_SUBD_DO;
351 s->subdev_flags = SDF_WRITABLE;
353 s->insn_bits = rti800_do_insn_bits;
355 s->range_table = &range_digital;
358 * There is also an Am9513 timer on these boards. This subdevice
359 * is not currently supported.
365 static struct comedi_driver rti800_driver = {
366 .driver_name = "rti800",
367 .module = THIS_MODULE,
368 .attach = rti800_attach,
369 .detach = comedi_legacy_detach,
370 .num_names = ARRAY_SIZE(rti800_boardtypes),
371 .board_name = &rti800_boardtypes[0].name,
372 .offset = sizeof(struct rti800_board),
374 module_comedi_driver(rti800_driver);
376 MODULE_DESCRIPTION("Comedi: RTI-800 Multifunction Analog/Digital board");
377 MODULE_AUTHOR("Comedi http://www.comedi.org");
378 MODULE_LICENSE("GPL");