1 // SPDX-License-Identifier: GPL-2.0+
4 * Driver for Access I/O Products PC-104 AIO12-8 Analog I/O Board
5 * Copyright (C) 2006 C&C Technologies, Inc.
10 * Description: Access I/O Products PC-104 AIO12-8 Analog I/O Board
11 * Author: Pablo Mejia <pablo.mejia@cctechnol.com>
12 * Devices: [Access I/O] PC-104 AIO12-8 (aio_aio12_8),
13 * [Access I/O] PC-104 AI12-8 (aio_ai12_8),
14 * [Access I/O] PC-104 AO12-4 (aio_ao12_4)
15 * Status: experimental
17 * Configuration Options:
18 * [0] - I/O port base address
21 * Only synchronous operations are supported.
24 #include <linux/module.h>
25 #include "../comedidev.h"
27 #include "comedi_8254.h"
33 #define AIO12_8_STATUS_REG 0x00
34 #define AIO12_8_STATUS_ADC_EOC BIT(7)
35 #define AIO12_8_STATUS_PORT_C_COS BIT(6)
36 #define AIO12_8_STATUS_IRQ_ENA BIT(2)
37 #define AIO12_8_INTERRUPT_REG 0x01
38 #define AIO12_8_INTERRUPT_ADC BIT(7)
39 #define AIO12_8_INTERRUPT_COS BIT(6)
40 #define AIO12_8_INTERRUPT_COUNTER1 BIT(5)
41 #define AIO12_8_INTERRUPT_PORT_C3 BIT(4)
42 #define AIO12_8_INTERRUPT_PORT_C0 BIT(3)
43 #define AIO12_8_INTERRUPT_ENA BIT(2)
44 #define AIO12_8_ADC_REG 0x02
45 #define AIO12_8_ADC_MODE(x) (((x) & 0x3) << 6)
46 #define AIO12_8_ADC_MODE_NORMAL AIO12_8_ADC_MODE(0)
47 #define AIO12_8_ADC_MODE_INT_CLK AIO12_8_ADC_MODE(1)
48 #define AIO12_8_ADC_MODE_STANDBY AIO12_8_ADC_MODE(2)
49 #define AIO12_8_ADC_MODE_POWERDOWN AIO12_8_ADC_MODE(3)
50 #define AIO12_8_ADC_ACQ(x) (((x) & 0x1) << 5)
51 #define AIO12_8_ADC_ACQ_3USEC AIO12_8_ADC_ACQ(0)
52 #define AIO12_8_ADC_ACQ_PROGRAM AIO12_8_ADC_ACQ(1)
53 #define AIO12_8_ADC_RANGE(x) ((x) << 3)
54 #define AIO12_8_ADC_CHAN(x) ((x) << 0)
55 #define AIO12_8_DAC_REG(x) (0x04 + (x) * 2)
56 #define AIO12_8_8254_BASE_REG 0x0c
57 #define AIO12_8_8255_BASE_REG 0x10
58 #define AIO12_8_DIO_CONTROL_REG 0x14
59 #define AIO12_8_DIO_CONTROL_TST BIT(0)
60 #define AIO12_8_ADC_TRIGGER_REG 0x15
61 #define AIO12_8_ADC_TRIGGER_RANGE(x) ((x) << 3)
62 #define AIO12_8_ADC_TRIGGER_CHAN(x) ((x) << 0)
63 #define AIO12_8_TRIGGER_REG 0x16
64 #define AIO12_8_TRIGGER_ADTRIG BIT(1)
65 #define AIO12_8_TRIGGER_DACTRIG BIT(0)
66 #define AIO12_8_COS_REG 0x17
67 #define AIO12_8_DAC_ENABLE_REG 0x18
68 #define AIO12_8_DAC_ENABLE_REF_ENA BIT(0)
70 static const struct comedi_lrange aio_aio12_8_range = {
79 struct aio12_8_boardtype {
81 unsigned int has_ai:1;
82 unsigned int has_ao:1;
85 static const struct aio12_8_boardtype board_types[] = {
87 .name = "aio_aio12_8",
99 static int aio_aio12_8_ai_eoc(struct comedi_device *dev,
100 struct comedi_subdevice *s,
101 struct comedi_insn *insn,
102 unsigned long context)
106 status = inb(dev->iobase + AIO12_8_STATUS_REG);
107 if (status & AIO12_8_STATUS_ADC_EOC)
112 static int aio_aio12_8_ai_read(struct comedi_device *dev,
113 struct comedi_subdevice *s,
114 struct comedi_insn *insn,
117 unsigned int chan = CR_CHAN(insn->chanspec);
118 unsigned int range = CR_RANGE(insn->chanspec);
120 unsigned char control;
125 * Setup the control byte for internal 2MHz clock, 3uS conversion,
126 * at the desired range of the requested channel.
128 control = AIO12_8_ADC_MODE_NORMAL | AIO12_8_ADC_ACQ_3USEC |
129 AIO12_8_ADC_RANGE(range) | AIO12_8_ADC_CHAN(chan);
131 /* Read status to clear EOC latch */
132 inb(dev->iobase + AIO12_8_STATUS_REG);
134 for (i = 0; i < insn->n; i++) {
135 /* Setup and start conversion */
136 outb(control, dev->iobase + AIO12_8_ADC_REG);
138 /* Wait for conversion to complete */
139 ret = comedi_timeout(dev, s, insn, aio_aio12_8_ai_eoc, 0);
143 val = inw(dev->iobase + AIO12_8_ADC_REG) & s->maxdata;
145 /* munge bipolar 2's complement data to offset binary */
146 if (comedi_range_is_bipolar(s, range))
147 val = comedi_offset_munge(s, val);
155 static int aio_aio12_8_ao_insn_write(struct comedi_device *dev,
156 struct comedi_subdevice *s,
157 struct comedi_insn *insn,
160 unsigned int chan = CR_CHAN(insn->chanspec);
161 unsigned int val = s->readback[chan];
165 outb(AIO12_8_DAC_ENABLE_REF_ENA, dev->iobase + AIO12_8_DAC_ENABLE_REG);
167 for (i = 0; i < insn->n; i++) {
169 outw(val, dev->iobase + AIO12_8_DAC_REG(chan));
171 s->readback[chan] = val;
176 static int aio_aio12_8_counter_insn_config(struct comedi_device *dev,
177 struct comedi_subdevice *s,
178 struct comedi_insn *insn,
181 unsigned int chan = CR_CHAN(insn->chanspec);
184 case INSN_CONFIG_GET_CLOCK_SRC:
186 * Channels 0 and 2 have external clock sources.
187 * Channel 1 has a fixed 1 MHz clock source.
190 data[1] = (chan == 1) ? I8254_OSC_BASE_1MHZ : 0;
199 static int aio_aio12_8_attach(struct comedi_device *dev,
200 struct comedi_devconfig *it)
202 const struct aio12_8_boardtype *board = dev->board_ptr;
203 struct comedi_subdevice *s;
206 ret = comedi_request_region(dev, it->options[0], 32);
210 dev->pacer = comedi_8254_init(dev->iobase + AIO12_8_8254_BASE_REG,
215 ret = comedi_alloc_subdevices(dev, 4);
219 /* Analog Input subdevice */
220 s = &dev->subdevices[0];
222 s->type = COMEDI_SUBD_AI;
223 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
226 s->range_table = &aio_aio12_8_range;
227 s->insn_read = aio_aio12_8_ai_read;
229 s->type = COMEDI_SUBD_UNUSED;
232 /* Analog Output subdevice */
233 s = &dev->subdevices[1];
235 s->type = COMEDI_SUBD_AO;
236 s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
239 s->range_table = &aio_aio12_8_range;
240 s->insn_write = aio_aio12_8_ao_insn_write;
242 ret = comedi_alloc_subdev_readback(s);
246 s->type = COMEDI_SUBD_UNUSED;
249 /* Digital I/O subdevice (8255) */
250 s = &dev->subdevices[2];
251 ret = subdev_8255_init(dev, s, NULL, AIO12_8_8255_BASE_REG);
255 /* Counter subdevice (8254) */
256 s = &dev->subdevices[3];
257 comedi_8254_subdevice_init(s, dev->pacer);
259 dev->pacer->insn_config = aio_aio12_8_counter_insn_config;
264 static struct comedi_driver aio_aio12_8_driver = {
265 .driver_name = "aio_aio12_8",
266 .module = THIS_MODULE,
267 .attach = aio_aio12_8_attach,
268 .detach = comedi_legacy_detach,
269 .board_name = &board_types[0].name,
270 .num_names = ARRAY_SIZE(board_types),
271 .offset = sizeof(struct aio12_8_boardtype),
273 module_comedi_driver(aio_aio12_8_driver);
275 MODULE_AUTHOR("Comedi https://www.comedi.org");
276 MODULE_DESCRIPTION("Comedi driver for Access I/O AIO12-8 Analog I/O Board");
277 MODULE_LICENSE("GPL");