3 * Copyright (C) 2004,2005 ADDI-DATA GmbH for the source code of this module.
4 * Project manager: Eric Stolz
9 * Tel: +19(0)7223/9493-0
10 * Fax: +49(0)7223/9493-92
11 * http://www.addi-data.com
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
25 #include <linux/module.h>
26 #include <linux/pci.h>
27 #include <linux/interrupt.h>
29 #include "../comedidev.h"
30 #include "comedi_fc.h"
31 #include "amcc_s5933.h"
36 #define APCI1032_DI_REG 0x00
37 #define APCI1032_MODE1_REG 0x04
38 #define APCI1032_MODE2_REG 0x08
39 #define APCI1032_STATUS_REG 0x0c
40 #define APCI1032_CTRL_REG 0x10
41 #define APCI1032_CTRL_INT_OR (0 << 1)
42 #define APCI1032_CTRL_INT_AND (1 << 1)
43 #define APCI1032_CTRL_INT_ENA (1 << 2)
45 struct apci1032_private {
46 unsigned long amcc_iobase; /* base of AMCC I/O registers */
47 unsigned int mode1; /* rising-edge/high level channels */
48 unsigned int mode2; /* falling-edge/low level channels */
49 unsigned int ctrl; /* interrupt mode OR (edge) . AND (level) */
52 static int apci1032_reset(struct comedi_device *dev)
54 /* disable the interrupts */
55 outl(0x0, dev->iobase + APCI1032_CTRL_REG);
56 /* Reset the interrupt status register */
57 inl(dev->iobase + APCI1032_STATUS_REG);
58 /* Disable the and/or interrupt */
59 outl(0x0, dev->iobase + APCI1032_MODE1_REG);
60 outl(0x0, dev->iobase + APCI1032_MODE2_REG);
66 * Change-Of-State (COS) interrupt configuration
68 * Channels 0 to 15 are interruptible. These channels can be configured
69 * to generate interrupts based on AND/OR logic for the desired channels.
72 * - reacts to rising or falling edges
73 * - interrupt is generated when any enabled channel
74 * meet the desired interrupt condition
77 * - reacts to changes in level of the selected inputs
78 * - interrupt is generated when all enabled channels
79 * meet the desired interrupt condition
80 * - after an interrupt, a change in level must occur on
81 * the selected inputs to release the IRQ logic
83 * The COS interrupt must be configured before it can be enabled.
85 * data[0] : INSN_CONFIG_DIGITAL_TRIG
86 * data[1] : trigger number (= 0)
87 * data[2] : configuration operation:
88 * COMEDI_DIGITAL_TRIG_DISABLE = no interrupts
89 * COMEDI_DIGITAL_TRIG_ENABLE_EDGES = OR (edge) interrupts
90 * COMEDI_DIGITAL_TRIG_ENABLE_LEVELS = AND (level) interrupts
91 * data[3] : left-shift for data[4] and data[5]
92 * data[4] : rising-edge/high level channels
93 * data[5] : falling-edge/low level channels
95 static int apci1032_cos_insn_config(struct comedi_device *dev,
96 struct comedi_subdevice *s,
97 struct comedi_insn *insn,
100 struct apci1032_private *devpriv = dev->private;
101 unsigned int shift, oldmask;
104 case INSN_CONFIG_DIGITAL_TRIG:
108 oldmask = (1U << shift) - 1;
110 case COMEDI_DIGITAL_TRIG_DISABLE:
116 case COMEDI_DIGITAL_TRIG_ENABLE_EDGES:
117 if (devpriv->ctrl != (APCI1032_CTRL_INT_ENA |
118 APCI1032_CTRL_INT_OR)) {
119 /* switching to 'OR' mode */
120 devpriv->ctrl = APCI1032_CTRL_INT_ENA |
121 APCI1032_CTRL_INT_OR;
122 /* wipe old channels */
126 /* preserve unspecified channels */
127 devpriv->mode1 &= oldmask;
128 devpriv->mode2 &= oldmask;
130 /* configure specified channels */
131 devpriv->mode1 |= data[4] << shift;
132 devpriv->mode2 |= data[5] << shift;
134 case COMEDI_DIGITAL_TRIG_ENABLE_LEVELS:
135 if (devpriv->ctrl != (APCI1032_CTRL_INT_ENA |
136 APCI1032_CTRL_INT_AND)) {
137 /* switching to 'AND' mode */
138 devpriv->ctrl = APCI1032_CTRL_INT_ENA |
139 APCI1032_CTRL_INT_AND;
140 /* wipe old channels */
144 /* preserve unspecified channels */
145 devpriv->mode1 &= oldmask;
146 devpriv->mode2 &= oldmask;
148 /* configure specified channels */
149 devpriv->mode1 |= data[4] << shift;
150 devpriv->mode2 |= data[5] << shift;
163 static int apci1032_cos_insn_bits(struct comedi_device *dev,
164 struct comedi_subdevice *s,
165 struct comedi_insn *insn,
173 static int apci1032_cos_cmdtest(struct comedi_device *dev,
174 struct comedi_subdevice *s,
175 struct comedi_cmd *cmd)
179 /* Step 1 : check if triggers are trivially valid */
181 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
182 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
183 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
184 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
185 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_NONE);
190 /* Step 2a : make sure trigger sources are unique */
191 /* Step 2b : and mutually compatible */
193 /* Step 3: check if arguments are trivially valid */
195 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
196 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
197 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
198 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
199 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
204 /* Step 4: fix up any arguments */
206 /* Step 5: check channel list if it exists */
212 * Change-Of-State (COS) 'do_cmd' operation
214 * Enable the COS interrupt as configured by apci1032_cos_insn_config().
216 static int apci1032_cos_cmd(struct comedi_device *dev,
217 struct comedi_subdevice *s)
219 struct apci1032_private *devpriv = dev->private;
221 if (!devpriv->ctrl) {
222 dev_warn(dev->class_dev,
223 "Interrupts disabled due to mode configuration!\n");
227 outl(devpriv->mode1, dev->iobase + APCI1032_MODE1_REG);
228 outl(devpriv->mode2, dev->iobase + APCI1032_MODE2_REG);
229 outl(devpriv->ctrl, dev->iobase + APCI1032_CTRL_REG);
234 static int apci1032_cos_cancel(struct comedi_device *dev,
235 struct comedi_subdevice *s)
237 return apci1032_reset(dev);
240 static irqreturn_t apci1032_interrupt(int irq, void *d)
242 struct comedi_device *dev = d;
243 struct apci1032_private *devpriv = dev->private;
244 struct comedi_subdevice *s = dev->read_subdev;
247 /* check interrupt is from this device */
248 if ((inl(devpriv->amcc_iobase + AMCC_OP_REG_INTCSR) &
249 INTCSR_INTR_ASSERTED) == 0)
252 /* check interrupt is enabled */
253 ctrl = inl(dev->iobase + APCI1032_CTRL_REG);
254 if ((ctrl & APCI1032_CTRL_INT_ENA) == 0)
257 /* disable the interrupt */
258 outl(ctrl & ~APCI1032_CTRL_INT_ENA, dev->iobase + APCI1032_CTRL_REG);
260 s->state = inl(dev->iobase + APCI1032_STATUS_REG) & 0xffff;
261 comedi_buf_write_samples(s, &s->state, 1);
262 comedi_handle_events(dev, s);
264 /* enable the interrupt */
265 outl(ctrl, dev->iobase + APCI1032_CTRL_REG);
270 static int apci1032_di_insn_bits(struct comedi_device *dev,
271 struct comedi_subdevice *s,
272 struct comedi_insn *insn,
275 data[1] = inl(dev->iobase + APCI1032_DI_REG);
280 static int apci1032_auto_attach(struct comedi_device *dev,
281 unsigned long context_unused)
283 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
284 struct apci1032_private *devpriv;
285 struct comedi_subdevice *s;
288 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
292 ret = comedi_pci_enable(dev);
296 devpriv->amcc_iobase = pci_resource_start(pcidev, 0);
297 dev->iobase = pci_resource_start(pcidev, 1);
299 if (pcidev->irq > 0) {
300 ret = request_irq(pcidev->irq, apci1032_interrupt, IRQF_SHARED,
301 dev->board_name, dev);
303 dev->irq = pcidev->irq;
306 ret = comedi_alloc_subdevices(dev, 2);
310 /* Allocate and Initialise DI Subdevice Structures */
311 s = &dev->subdevices[0];
312 s->type = COMEDI_SUBD_DI;
313 s->subdev_flags = SDF_READABLE;
316 s->range_table = &range_digital;
317 s->insn_bits = apci1032_di_insn_bits;
319 /* Change-Of-State (COS) interrupt subdevice */
320 s = &dev->subdevices[1];
322 dev->read_subdev = s;
323 s->type = COMEDI_SUBD_DI;
324 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
327 s->range_table = &range_digital;
328 s->insn_config = apci1032_cos_insn_config;
329 s->insn_bits = apci1032_cos_insn_bits;
331 s->do_cmdtest = apci1032_cos_cmdtest;
332 s->do_cmd = apci1032_cos_cmd;
333 s->cancel = apci1032_cos_cancel;
335 s->type = COMEDI_SUBD_UNUSED;
341 static void apci1032_detach(struct comedi_device *dev)
345 comedi_pci_detach(dev);
348 static struct comedi_driver apci1032_driver = {
349 .driver_name = "addi_apci_1032",
350 .module = THIS_MODULE,
351 .auto_attach = apci1032_auto_attach,
352 .detach = apci1032_detach,
355 static int apci1032_pci_probe(struct pci_dev *dev,
356 const struct pci_device_id *id)
358 return comedi_pci_auto_config(dev, &apci1032_driver, id->driver_data);
361 static const struct pci_device_id apci1032_pci_table[] = {
362 { PCI_DEVICE(PCI_VENDOR_ID_ADDIDATA, 0x1003) },
365 MODULE_DEVICE_TABLE(pci, apci1032_pci_table);
367 static struct pci_driver apci1032_pci_driver = {
368 .name = "addi_apci_1032",
369 .id_table = apci1032_pci_table,
370 .probe = apci1032_pci_probe,
371 .remove = comedi_pci_auto_unconfig,
373 module_comedi_pci_driver(apci1032_driver, apci1032_pci_driver);
375 MODULE_AUTHOR("Comedi http://www.comedi.org");
376 MODULE_DESCRIPTION("ADDI-DATA APCI-1032, 32 channel DI boards");
377 MODULE_LICENSE("GPL");