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
16 #include <linux/module.h>
17 #include <linux/comedi/comedi_pci.h>
19 #include "addi_watchdog.h"
22 * PCI bar 1 I/O Register map - Digital input/output
24 #define APCI1516_DI_REG 0x00
25 #define APCI1516_DO_REG 0x04
28 * PCI bar 2 I/O Register map - Watchdog (APCI-1516 and APCI-2016)
30 #define APCI1516_WDOG_REG 0x00
32 enum apci1516_boardid {
38 struct apci1516_boardinfo {
45 static const struct apci1516_boardinfo apci1516_boardtypes[] = {
63 struct apci1516_private {
64 unsigned long wdog_iobase;
67 static int apci1516_di_insn_bits(struct comedi_device *dev,
68 struct comedi_subdevice *s,
69 struct comedi_insn *insn,
72 data[1] = inw(dev->iobase + APCI1516_DI_REG);
77 static int apci1516_do_insn_bits(struct comedi_device *dev,
78 struct comedi_subdevice *s,
79 struct comedi_insn *insn,
82 s->state = inw(dev->iobase + APCI1516_DO_REG);
84 if (comedi_dio_update_state(s, data))
85 outw(s->state, dev->iobase + APCI1516_DO_REG);
92 static int apci1516_reset(struct comedi_device *dev)
94 const struct apci1516_boardinfo *board = dev->board_ptr;
95 struct apci1516_private *devpriv = dev->private;
100 outw(0x0, dev->iobase + APCI1516_DO_REG);
102 addi_watchdog_reset(devpriv->wdog_iobase);
107 static int apci1516_auto_attach(struct comedi_device *dev,
108 unsigned long context)
110 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
111 const struct apci1516_boardinfo *board = NULL;
112 struct apci1516_private *devpriv;
113 struct comedi_subdevice *s;
116 if (context < ARRAY_SIZE(apci1516_boardtypes))
117 board = &apci1516_boardtypes[context];
120 dev->board_ptr = board;
121 dev->board_name = board->name;
123 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
127 ret = comedi_pci_enable(dev);
131 dev->iobase = pci_resource_start(pcidev, 1);
132 devpriv->wdog_iobase = pci_resource_start(pcidev, 2);
134 ret = comedi_alloc_subdevices(dev, 3);
138 /* Initialize the digital input subdevice */
139 s = &dev->subdevices[0];
140 if (board->di_nchan) {
141 s->type = COMEDI_SUBD_DI;
142 s->subdev_flags = SDF_READABLE;
143 s->n_chan = board->di_nchan;
145 s->range_table = &range_digital;
146 s->insn_bits = apci1516_di_insn_bits;
148 s->type = COMEDI_SUBD_UNUSED;
151 /* Initialize the digital output subdevice */
152 s = &dev->subdevices[1];
153 if (board->do_nchan) {
154 s->type = COMEDI_SUBD_DO;
155 s->subdev_flags = SDF_WRITABLE;
156 s->n_chan = board->do_nchan;
158 s->range_table = &range_digital;
159 s->insn_bits = apci1516_do_insn_bits;
161 s->type = COMEDI_SUBD_UNUSED;
164 /* Initialize the watchdog subdevice */
165 s = &dev->subdevices[2];
166 if (board->has_wdog) {
167 ret = addi_watchdog_init(s, devpriv->wdog_iobase);
171 s->type = COMEDI_SUBD_UNUSED;
178 static void apci1516_detach(struct comedi_device *dev)
182 comedi_pci_detach(dev);
185 static struct comedi_driver apci1516_driver = {
186 .driver_name = "addi_apci_1516",
187 .module = THIS_MODULE,
188 .auto_attach = apci1516_auto_attach,
189 .detach = apci1516_detach,
192 static int apci1516_pci_probe(struct pci_dev *dev,
193 const struct pci_device_id *id)
195 return comedi_pci_auto_config(dev, &apci1516_driver, id->driver_data);
198 static const struct pci_device_id apci1516_pci_table[] = {
199 { PCI_VDEVICE(ADDIDATA, 0x1000), BOARD_APCI1016 },
200 { PCI_VDEVICE(ADDIDATA, 0x1001), BOARD_APCI1516 },
201 { PCI_VDEVICE(ADDIDATA, 0x1002), BOARD_APCI2016 },
204 MODULE_DEVICE_TABLE(pci, apci1516_pci_table);
206 static struct pci_driver apci1516_pci_driver = {
207 .name = "addi_apci_1516",
208 .id_table = apci1516_pci_table,
209 .probe = apci1516_pci_probe,
210 .remove = comedi_pci_auto_unconfig,
212 module_comedi_pci_driver(apci1516_driver, apci1516_pci_driver);
214 MODULE_DESCRIPTION("ADDI-DATA APCI-1016/1516/2016, 16 channel DIO boards");
215 MODULE_AUTHOR("Comedi https://www.comedi.org");
216 MODULE_LICENSE("GPL");