1 // SPDX-License-Identifier: GPL-2.0+
4 * Copyright (C) 2004,2005 ADDI-DATA GmbH for the source code of this module.
5 * Project manager: Eric Stolz
10 * Tel: +19(0)7223/9493-0
11 * Fax: +49(0)7223/9493-92
12 * http://www.addi-data.com
17 * Driver: addi_apci_1032
18 * Description: ADDI-DATA APCI-1032 Digital Input Board
19 * Author: ADDI-DATA GmbH <info@addi-data.com>,
20 * H Hartley Sweeten <hsweeten@visionengravers.com>
22 * Devices: [ADDI-DATA] APCI-1032 (addi_apci_1032)
24 * Configuration options:
25 * None; devices are configured automatically.
27 * This driver models the APCI-1032 as a 32-channel, digital input subdevice
28 * plus an additional digital input subdevice to handle change-of-state (COS)
29 * interrupts (if an interrupt handler can be set up successfully).
31 * The COS subdevice supports comedi asynchronous read commands.
33 * Change-Of-State (COS) interrupt configuration:
35 * Channels 0 to 15 are interruptible. These channels can be configured
36 * to generate interrupts based on AND/OR logic for the desired channels.
39 * - reacts to rising or falling edges
40 * - interrupt is generated when any enabled channel meets the desired
44 * - reacts to changes in level of the selected inputs
45 * - interrupt is generated when all enabled channels meet the desired
47 * - after an interrupt, a change in level must occur on the selected
48 * inputs to release the IRQ logic
50 * The COS subdevice must be configured before setting up a comedi
51 * asynchronous command:
53 * data[0] : INSN_CONFIG_DIGITAL_TRIG
54 * data[1] : trigger number (= 0)
55 * data[2] : configuration operation:
56 * - COMEDI_DIGITAL_TRIG_DISABLE = no interrupts
57 * - COMEDI_DIGITAL_TRIG_ENABLE_EDGES = OR (edge) interrupts
58 * - COMEDI_DIGITAL_TRIG_ENABLE_LEVELS = AND (level) interrupts
59 * data[3] : left-shift for data[4] and data[5]
60 * data[4] : rising-edge/high level channels
61 * data[5] : falling-edge/low level channels
64 #include <linux/module.h>
65 #include <linux/interrupt.h>
67 #include "../comedi_pci.h"
68 #include "amcc_s5933.h"
73 #define APCI1032_DI_REG 0x00
74 #define APCI1032_MODE1_REG 0x04
75 #define APCI1032_MODE2_REG 0x08
76 #define APCI1032_STATUS_REG 0x0c
77 #define APCI1032_CTRL_REG 0x10
78 #define APCI1032_CTRL_INT_MODE(x) (((x) & 0x1) << 1)
79 #define APCI1032_CTRL_INT_OR APCI1032_CTRL_INT_MODE(0)
80 #define APCI1032_CTRL_INT_AND APCI1032_CTRL_INT_MODE(1)
81 #define APCI1032_CTRL_INT_ENA BIT(2)
83 struct apci1032_private {
84 unsigned long amcc_iobase; /* base of AMCC I/O registers */
85 unsigned int mode1; /* rising-edge/high level channels */
86 unsigned int mode2; /* falling-edge/low level channels */
87 unsigned int ctrl; /* interrupt mode OR (edge) . AND (level) */
90 static int apci1032_reset(struct comedi_device *dev)
92 /* disable the interrupts */
93 outl(0x0, dev->iobase + APCI1032_CTRL_REG);
94 /* Reset the interrupt status register */
95 inl(dev->iobase + APCI1032_STATUS_REG);
96 /* Disable the and/or interrupt */
97 outl(0x0, dev->iobase + APCI1032_MODE1_REG);
98 outl(0x0, dev->iobase + APCI1032_MODE2_REG);
103 static int apci1032_cos_insn_config(struct comedi_device *dev,
104 struct comedi_subdevice *s,
105 struct comedi_insn *insn,
108 struct apci1032_private *devpriv = dev->private;
109 unsigned int shift, oldmask;
112 case INSN_CONFIG_DIGITAL_TRIG:
116 oldmask = (1U << shift) - 1;
118 case COMEDI_DIGITAL_TRIG_DISABLE:
124 case COMEDI_DIGITAL_TRIG_ENABLE_EDGES:
125 if (devpriv->ctrl != (APCI1032_CTRL_INT_ENA |
126 APCI1032_CTRL_INT_OR)) {
127 /* switching to 'OR' mode */
128 devpriv->ctrl = APCI1032_CTRL_INT_ENA |
129 APCI1032_CTRL_INT_OR;
130 /* wipe old channels */
134 /* preserve unspecified channels */
135 devpriv->mode1 &= oldmask;
136 devpriv->mode2 &= oldmask;
138 /* configure specified channels */
139 devpriv->mode1 |= data[4] << shift;
140 devpriv->mode2 |= data[5] << shift;
142 case COMEDI_DIGITAL_TRIG_ENABLE_LEVELS:
143 if (devpriv->ctrl != (APCI1032_CTRL_INT_ENA |
144 APCI1032_CTRL_INT_AND)) {
145 /* switching to 'AND' mode */
146 devpriv->ctrl = APCI1032_CTRL_INT_ENA |
147 APCI1032_CTRL_INT_AND;
148 /* wipe old channels */
152 /* preserve unspecified channels */
153 devpriv->mode1 &= oldmask;
154 devpriv->mode2 &= oldmask;
156 /* configure specified channels */
157 devpriv->mode1 |= data[4] << shift;
158 devpriv->mode2 |= data[5] << shift;
171 static int apci1032_cos_insn_bits(struct comedi_device *dev,
172 struct comedi_subdevice *s,
173 struct comedi_insn *insn,
181 static int apci1032_cos_cmdtest(struct comedi_device *dev,
182 struct comedi_subdevice *s,
183 struct comedi_cmd *cmd)
187 /* Step 1 : check if triggers are trivially valid */
189 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
190 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
191 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
192 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
193 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_NONE);
198 /* Step 2a : make sure trigger sources are unique */
199 /* Step 2b : and mutually compatible */
201 /* Step 3: check if arguments are trivially valid */
203 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
204 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
205 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
206 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
208 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
213 /* Step 4: fix up any arguments */
215 /* Step 5: check channel list if it exists */
221 * Change-Of-State (COS) 'do_cmd' operation
223 * Enable the COS interrupt as configured by apci1032_cos_insn_config().
225 static int apci1032_cos_cmd(struct comedi_device *dev,
226 struct comedi_subdevice *s)
228 struct apci1032_private *devpriv = dev->private;
230 if (!devpriv->ctrl) {
231 dev_warn(dev->class_dev,
232 "Interrupts disabled due to mode configuration!\n");
236 outl(devpriv->mode1, dev->iobase + APCI1032_MODE1_REG);
237 outl(devpriv->mode2, dev->iobase + APCI1032_MODE2_REG);
238 outl(devpriv->ctrl, dev->iobase + APCI1032_CTRL_REG);
243 static int apci1032_cos_cancel(struct comedi_device *dev,
244 struct comedi_subdevice *s)
246 return apci1032_reset(dev);
249 static irqreturn_t apci1032_interrupt(int irq, void *d)
251 struct comedi_device *dev = d;
252 struct apci1032_private *devpriv = dev->private;
253 struct comedi_subdevice *s = dev->read_subdev;
256 /* check interrupt is from this device */
257 if ((inl(devpriv->amcc_iobase + AMCC_OP_REG_INTCSR) &
258 INTCSR_INTR_ASSERTED) == 0)
261 /* check interrupt is enabled */
262 ctrl = inl(dev->iobase + APCI1032_CTRL_REG);
263 if ((ctrl & APCI1032_CTRL_INT_ENA) == 0)
266 /* disable the interrupt */
267 outl(ctrl & ~APCI1032_CTRL_INT_ENA, dev->iobase + APCI1032_CTRL_REG);
269 s->state = inl(dev->iobase + APCI1032_STATUS_REG) & 0xffff;
270 comedi_buf_write_samples(s, &s->state, 1);
271 comedi_handle_events(dev, s);
273 /* enable the interrupt */
274 outl(ctrl, dev->iobase + APCI1032_CTRL_REG);
279 static int apci1032_di_insn_bits(struct comedi_device *dev,
280 struct comedi_subdevice *s,
281 struct comedi_insn *insn,
284 data[1] = inl(dev->iobase + APCI1032_DI_REG);
289 static int apci1032_auto_attach(struct comedi_device *dev,
290 unsigned long context_unused)
292 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
293 struct apci1032_private *devpriv;
294 struct comedi_subdevice *s;
297 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
301 ret = comedi_pci_enable(dev);
305 devpriv->amcc_iobase = pci_resource_start(pcidev, 0);
306 dev->iobase = pci_resource_start(pcidev, 1);
308 if (pcidev->irq > 0) {
309 ret = request_irq(pcidev->irq, apci1032_interrupt, IRQF_SHARED,
310 dev->board_name, dev);
312 dev->irq = pcidev->irq;
315 ret = comedi_alloc_subdevices(dev, 2);
319 /* Allocate and Initialise DI Subdevice Structures */
320 s = &dev->subdevices[0];
321 s->type = COMEDI_SUBD_DI;
322 s->subdev_flags = SDF_READABLE;
325 s->range_table = &range_digital;
326 s->insn_bits = apci1032_di_insn_bits;
328 /* Change-Of-State (COS) interrupt subdevice */
329 s = &dev->subdevices[1];
331 dev->read_subdev = s;
332 s->type = COMEDI_SUBD_DI;
333 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
336 s->range_table = &range_digital;
337 s->insn_config = apci1032_cos_insn_config;
338 s->insn_bits = apci1032_cos_insn_bits;
340 s->do_cmdtest = apci1032_cos_cmdtest;
341 s->do_cmd = apci1032_cos_cmd;
342 s->cancel = apci1032_cos_cancel;
344 s->type = COMEDI_SUBD_UNUSED;
350 static void apci1032_detach(struct comedi_device *dev)
354 comedi_pci_detach(dev);
357 static struct comedi_driver apci1032_driver = {
358 .driver_name = "addi_apci_1032",
359 .module = THIS_MODULE,
360 .auto_attach = apci1032_auto_attach,
361 .detach = apci1032_detach,
364 static int apci1032_pci_probe(struct pci_dev *dev,
365 const struct pci_device_id *id)
367 return comedi_pci_auto_config(dev, &apci1032_driver, id->driver_data);
370 static const struct pci_device_id apci1032_pci_table[] = {
371 { PCI_DEVICE(PCI_VENDOR_ID_ADDIDATA, 0x1003) },
374 MODULE_DEVICE_TABLE(pci, apci1032_pci_table);
376 static struct pci_driver apci1032_pci_driver = {
377 .name = "addi_apci_1032",
378 .id_table = apci1032_pci_table,
379 .probe = apci1032_pci_probe,
380 .remove = comedi_pci_auto_unconfig,
382 module_comedi_pci_driver(apci1032_driver, apci1032_pci_driver);
384 MODULE_AUTHOR("Comedi http://www.comedi.org");
385 MODULE_DESCRIPTION("ADDI-DATA APCI-1032, 32 channel DI boards");
386 MODULE_LICENSE("GPL");