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: S. Weber
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>
22 #define APCI16XX_IN_REG(x) (((x) * 4) + 0x08)
23 #define APCI16XX_OUT_REG(x) (((x) * 4) + 0x14)
24 #define APCI16XX_DIR_REG(x) (((x) * 4) + 0x20)
26 enum apci16xx_boardid {
31 struct apci16xx_boardinfo {
36 static const struct apci16xx_boardinfo apci16xx_boardtypes[] = {
39 .n_chan = 48, /* 2 subdevices */
43 .n_chan = 96, /* 3 subdevices */
47 static int apci16xx_insn_config(struct comedi_device *dev,
48 struct comedi_subdevice *s,
49 struct comedi_insn *insn,
52 unsigned int chan = CR_CHAN(insn->chanspec);
65 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
69 outl(s->io_bits, dev->iobase + APCI16XX_DIR_REG(s->index));
74 static int apci16xx_dio_insn_bits(struct comedi_device *dev,
75 struct comedi_subdevice *s,
76 struct comedi_insn *insn,
79 if (comedi_dio_update_state(s, data))
80 outl(s->state, dev->iobase + APCI16XX_OUT_REG(s->index));
82 data[1] = inl(dev->iobase + APCI16XX_IN_REG(s->index));
87 static int apci16xx_auto_attach(struct comedi_device *dev,
88 unsigned long context)
90 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
91 const struct apci16xx_boardinfo *board = NULL;
92 struct comedi_subdevice *s;
93 unsigned int n_subdevs;
98 if (context < ARRAY_SIZE(apci16xx_boardtypes))
99 board = &apci16xx_boardtypes[context];
102 dev->board_ptr = board;
103 dev->board_name = board->name;
105 ret = comedi_pci_enable(dev);
109 dev->iobase = pci_resource_start(pcidev, 0);
112 * Work out the number of subdevices needed to support all the
113 * digital i/o channels on the board. Each subdevice supports
116 n_subdevs = board->n_chan / 32;
117 if ((n_subdevs * 32) < board->n_chan) {
118 last = board->n_chan - (n_subdevs * 32);
124 ret = comedi_alloc_subdevices(dev, n_subdevs);
128 /* Initialize the TTL digital i/o subdevices */
129 for (i = 0; i < n_subdevs; i++) {
130 s = &dev->subdevices[i];
131 s->type = COMEDI_SUBD_DIO;
132 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
133 s->n_chan = ((i * 32) < board->n_chan) ? 32 : last;
135 s->range_table = &range_digital;
136 s->insn_config = apci16xx_insn_config;
137 s->insn_bits = apci16xx_dio_insn_bits;
139 /* Default all channels to inputs */
141 outl(s->io_bits, dev->iobase + APCI16XX_DIR_REG(i));
147 static struct comedi_driver apci16xx_driver = {
148 .driver_name = "addi_apci_16xx",
149 .module = THIS_MODULE,
150 .auto_attach = apci16xx_auto_attach,
151 .detach = comedi_pci_detach,
154 static int apci16xx_pci_probe(struct pci_dev *dev,
155 const struct pci_device_id *id)
157 return comedi_pci_auto_config(dev, &apci16xx_driver, id->driver_data);
160 static const struct pci_device_id apci16xx_pci_table[] = {
161 { PCI_VDEVICE(ADDIDATA, 0x1009), BOARD_APCI1648 },
162 { PCI_VDEVICE(ADDIDATA, 0x100a), BOARD_APCI1696 },
165 MODULE_DEVICE_TABLE(pci, apci16xx_pci_table);
167 static struct pci_driver apci16xx_pci_driver = {
168 .name = "addi_apci_16xx",
169 .id_table = apci16xx_pci_table,
170 .probe = apci16xx_pci_probe,
171 .remove = comedi_pci_auto_unconfig,
173 module_comedi_pci_driver(apci16xx_driver, apci16xx_pci_driver);
175 MODULE_DESCRIPTION("ADDI-DATA APCI-1648/1696, TTL I/O boards");
176 MODULE_AUTHOR("Comedi https://www.comedi.org");
177 MODULE_LICENSE("GPL");