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>
28 #include <linux/slab.h>
30 #include "../comedidev.h"
31 #include "addi_watchdog.h"
32 #include "comedi_fc.h"
35 * PCI bar 1 I/O Register map
37 #define APCI2032_DO_REG 0x00
38 #define APCI2032_INT_CTRL_REG 0x04
39 #define APCI2032_INT_CTRL_VCC_ENA (1 << 0)
40 #define APCI2032_INT_CTRL_CC_ENA (1 << 1)
41 #define APCI2032_INT_STATUS_REG 0x08
42 #define APCI2032_INT_STATUS_VCC (1 << 0)
43 #define APCI2032_INT_STATUS_CC (1 << 1)
44 #define APCI2032_STATUS_REG 0x0c
45 #define APCI2032_STATUS_IRQ (1 << 0)
46 #define APCI2032_WDOG_REG 0x10
48 struct apci2032_int_private {
50 unsigned int stop_count;
52 unsigned char enabled_isns;
55 static int apci2032_do_insn_bits(struct comedi_device *dev,
56 struct comedi_subdevice *s,
57 struct comedi_insn *insn,
60 unsigned int mask = data[0];
61 unsigned int bits = data[1];
63 s->state = inl(dev->iobase + APCI2032_DO_REG);
66 s->state |= (bits & mask);
68 outl(s->state, dev->iobase + APCI2032_DO_REG);
76 static int apci2032_int_insn_bits(struct comedi_device *dev,
77 struct comedi_subdevice *s,
78 struct comedi_insn *insn,
81 data[1] = inl(dev->iobase + APCI2032_INT_STATUS_REG) & 3;
85 static void apci2032_int_stop(struct comedi_device *dev,
86 struct comedi_subdevice *s)
88 struct apci2032_int_private *subpriv = s->private;
90 subpriv->active = false;
91 subpriv->enabled_isns = 0;
92 outl(0x0, dev->iobase + APCI2032_INT_CTRL_REG);
95 static bool apci2032_int_start(struct comedi_device *dev,
96 struct comedi_subdevice *s,
97 unsigned char enabled_isns)
99 struct apci2032_int_private *subpriv = s->private;
100 struct comedi_cmd *cmd = &s->async->cmd;
103 subpriv->enabled_isns = enabled_isns;
104 subpriv->stop_count = cmd->stop_arg;
105 if (cmd->stop_src == TRIG_COUNT && subpriv->stop_count == 0) {
106 /* An empty acquisition! */
107 s->async->events |= COMEDI_CB_EOA;
108 subpriv->active = false;
111 subpriv->active = true;
112 outl(enabled_isns, dev->iobase + APCI2032_INT_CTRL_REG);
119 static int apci2032_int_cmdtest(struct comedi_device *dev,
120 struct comedi_subdevice *s,
121 struct comedi_cmd *cmd)
125 /* Step 1 : check if triggers are trivially valid */
127 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
128 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
129 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
130 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
131 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
136 /* Step 2a : make sure trigger sources are unique */
137 err |= cfc_check_trigger_is_unique(cmd->stop_src);
139 /* Step 2b : and mutually compatible */
144 /* Step 3: check if arguments are trivially valid */
146 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
147 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
148 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
149 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
150 if (cmd->stop_src == TRIG_NONE)
151 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
156 /* step 4: ignored */
164 static int apci2032_int_cmd(struct comedi_device *dev,
165 struct comedi_subdevice *s)
167 struct comedi_cmd *cmd = &s->async->cmd;
168 struct apci2032_int_private *subpriv = s->private;
169 unsigned char enabled_isns;
175 for (n = 0; n < cmd->chanlist_len; n++)
176 enabled_isns |= 1 << CR_CHAN(cmd->chanlist[n]);
178 spin_lock_irqsave(&subpriv->spinlock, flags);
179 do_event = apci2032_int_start(dev, s, enabled_isns);
180 spin_unlock_irqrestore(&subpriv->spinlock, flags);
183 comedi_event(dev, s);
188 static int apci2032_int_cancel(struct comedi_device *dev,
189 struct comedi_subdevice *s)
191 struct apci2032_int_private *subpriv = s->private;
194 spin_lock_irqsave(&subpriv->spinlock, flags);
196 apci2032_int_stop(dev, s);
197 spin_unlock_irqrestore(&subpriv->spinlock, flags);
202 static irqreturn_t apci2032_interrupt(int irq, void *d)
204 struct comedi_device *dev = d;
205 struct comedi_subdevice *s = dev->read_subdev;
206 struct apci2032_int_private *subpriv;
208 bool do_event = false;
213 /* Check if VCC OR CC interrupt has occurred */
214 val = inl(dev->iobase + APCI2032_STATUS_REG) & APCI2032_STATUS_IRQ;
218 subpriv = s->private;
219 spin_lock(&subpriv->spinlock);
221 val = inl(dev->iobase + APCI2032_INT_STATUS_REG) & 3;
222 /* Disable triggered interrupt sources. */
223 outl(~val & 3, dev->iobase + APCI2032_INT_CTRL_REG);
225 * Note: We don't reenable the triggered interrupt sources because they
226 * are level-sensitive, hardware error status interrupt sources and
227 * they'd keep triggering interrupts repeatedly.
230 if (subpriv->active && (val & subpriv->enabled_isns) != 0) {
233 unsigned int *chanlist;
235 /* Bits in scan data correspond to indices in channel list. */
237 len = s->async->cmd.chanlist_len;
238 chanlist = &s->async->cmd.chanlist[0];
239 for (n = 0; n < len; n++)
240 if ((val & (1U << CR_CHAN(chanlist[n]))) != 0)
243 if (comedi_buf_put(s->async, bits)) {
244 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
245 if (s->async->cmd.stop_src == TRIG_COUNT &&
246 subpriv->stop_count > 0) {
247 subpriv->stop_count--;
248 if (subpriv->stop_count == 0) {
249 /* end of acquisition */
250 s->async->events |= COMEDI_CB_EOA;
251 apci2032_int_stop(dev, s);
255 apci2032_int_stop(dev, s);
256 s->async->events |= COMEDI_CB_OVERFLOW;
261 spin_unlock(&subpriv->spinlock);
263 comedi_event(dev, s);
268 static int apci2032_reset(struct comedi_device *dev)
270 outl(0x0, dev->iobase + APCI2032_DO_REG);
271 outl(0x0, dev->iobase + APCI2032_INT_CTRL_REG);
273 addi_watchdog_reset(dev->iobase + APCI2032_WDOG_REG);
278 static int apci2032_auto_attach(struct comedi_device *dev,
279 unsigned long context_unused)
281 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
282 struct comedi_subdevice *s;
285 ret = comedi_pci_enable(dev);
288 dev->iobase = pci_resource_start(pcidev, 1);
291 if (pcidev->irq > 0) {
292 ret = request_irq(pcidev->irq, apci2032_interrupt,
293 IRQF_SHARED, dev->board_name, dev);
295 dev->irq = pcidev->irq;
298 ret = comedi_alloc_subdevices(dev, 3);
302 /* Initialize the digital output subdevice */
303 s = &dev->subdevices[0];
304 s->type = COMEDI_SUBD_DO;
305 s->subdev_flags = SDF_WRITEABLE;
308 s->range_table = &range_digital;
309 s->insn_bits = apci2032_do_insn_bits;
311 /* Initialize the watchdog subdevice */
312 s = &dev->subdevices[1];
313 ret = addi_watchdog_init(s, dev->iobase + APCI2032_WDOG_REG);
317 /* Initialize the interrupt subdevice */
318 s = &dev->subdevices[2];
319 s->type = COMEDI_SUBD_DI;
320 s->subdev_flags = SDF_READABLE;
323 s->range_table = &range_digital;
324 s->insn_bits = apci2032_int_insn_bits;
326 struct apci2032_int_private *subpriv;
328 dev->read_subdev = s;
329 subpriv = kzalloc(sizeof(*subpriv), GFP_KERNEL);
332 spin_lock_init(&subpriv->spinlock);
333 s->private = subpriv;
334 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
336 s->do_cmdtest = apci2032_int_cmdtest;
337 s->do_cmd = apci2032_int_cmd;
338 s->cancel = apci2032_int_cancel;
344 static void apci2032_detach(struct comedi_device *dev)
349 free_irq(dev->irq, dev);
350 if (dev->read_subdev)
351 kfree(dev->read_subdev->private);
352 comedi_pci_disable(dev);
355 static struct comedi_driver apci2032_driver = {
356 .driver_name = "addi_apci_2032",
357 .module = THIS_MODULE,
358 .auto_attach = apci2032_auto_attach,
359 .detach = apci2032_detach,
362 static int apci2032_pci_probe(struct pci_dev *dev,
363 const struct pci_device_id *id)
365 return comedi_pci_auto_config(dev, &apci2032_driver, id->driver_data);
368 static DEFINE_PCI_DEVICE_TABLE(apci2032_pci_table) = {
369 { PCI_DEVICE(PCI_VENDOR_ID_ADDIDATA, 0x1004) },
372 MODULE_DEVICE_TABLE(pci, apci2032_pci_table);
374 static struct pci_driver apci2032_pci_driver = {
375 .name = "addi_apci_2032",
376 .id_table = apci2032_pci_table,
377 .probe = apci2032_pci_probe,
378 .remove = comedi_pci_auto_unconfig,
380 module_comedi_pci_driver(apci2032_driver, apci2032_pci_driver);
382 MODULE_AUTHOR("Comedi http://www.comedi.org");
383 MODULE_DESCRIPTION("Comedi low-level driver");
384 MODULE_LICENSE("GPL");