1 // SPDX-License-Identifier: GPL-2.0+
4 * Comedi driver for the Advantech PCI-1724U card.
6 * Author: Frank Mori Hess <fmh6jj@gmail.com>
7 * Copyright (C) 2013 GnuBIO Inc
9 * COMEDI - Linux Control and Measurement Device Interface
10 * Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
15 * Description: Advantech PCI-1724U
16 * Devices: [Advantech] PCI-1724U (adv_pci1724)
17 * Author: Frank Mori Hess <fmh6jj@gmail.com>
21 * Configuration Options: not applicable, uses comedi PCI auto config
23 * Subdevice 0 is the analog output.
24 * Subdevice 1 is the offset calibration for the analog output.
25 * Subdevice 2 is the gain calibration for the analog output.
27 * The calibration offset and gains have quite a large effect on the
28 * analog output, so it is possible to adjust the analog output to
29 * have an output range significantly different from the board's
30 * nominal output ranges. For a calibrated +/-10V range, the analog
31 * output's offset will be set somewhere near mid-range (0x2000) and
32 * its gain will be near maximum (0x3fff).
34 * There is really no difference between the board's documented 0-20mA
35 * versus 4-20mA output ranges. To pick one or the other is simply a
36 * matter of adjusting the offset and gain calibration until the board
37 * outputs in the desired range.
40 #include <linux/module.h>
41 #include <linux/comedi/comedi_pci.h>
44 * PCI bar 2 Register I/O map (dev->iobase)
46 #define PCI1724_DAC_CTRL_REG 0x00
47 #define PCI1724_DAC_CTRL_GX(x) BIT(20 + ((x) / 8))
48 #define PCI1724_DAC_CTRL_CX(x) (((x) % 8) << 16)
49 #define PCI1724_DAC_CTRL_MODE(x) (((x) & 0x3) << 14)
50 #define PCI1724_DAC_CTRL_MODE_GAIN PCI1724_DAC_CTRL_MODE(1)
51 #define PCI1724_DAC_CTRL_MODE_OFFSET PCI1724_DAC_CTRL_MODE(2)
52 #define PCI1724_DAC_CTRL_MODE_NORMAL PCI1724_DAC_CTRL_MODE(3)
53 #define PCI1724_DAC_CTRL_MODE_MASK PCI1724_DAC_CTRL_MODE(3)
54 #define PCI1724_DAC_CTRL_DATA(x) (((x) & 0x3fff) << 0)
55 #define PCI1724_SYNC_CTRL_REG 0x04
56 #define PCI1724_SYNC_CTRL_DACSTAT BIT(1)
57 #define PCI1724_SYNC_CTRL_SYN BIT(0)
58 #define PCI1724_EEPROM_CTRL_REG 0x08
59 #define PCI1724_SYNC_TRIG_REG 0x0c /* any value works */
60 #define PCI1724_BOARD_ID_REG 0x10
61 #define PCI1724_BOARD_ID_MASK (0xf << 0)
63 static const struct comedi_lrange adv_pci1724_ao_ranges = {
72 static int adv_pci1724_dac_idle(struct comedi_device *dev,
73 struct comedi_subdevice *s,
74 struct comedi_insn *insn,
75 unsigned long context)
79 status = inl(dev->iobase + PCI1724_SYNC_CTRL_REG);
80 if ((status & PCI1724_SYNC_CTRL_DACSTAT) == 0)
85 static int adv_pci1724_insn_write(struct comedi_device *dev,
86 struct comedi_subdevice *s,
87 struct comedi_insn *insn,
90 unsigned long mode = (unsigned long)s->private;
91 unsigned int chan = CR_CHAN(insn->chanspec);
96 ctrl = PCI1724_DAC_CTRL_GX(chan) | PCI1724_DAC_CTRL_CX(chan) | mode;
98 /* turn off synchronous mode */
99 outl(0, dev->iobase + PCI1724_SYNC_CTRL_REG);
101 for (i = 0; i < insn->n; ++i) {
102 unsigned int val = data[i];
104 ret = comedi_timeout(dev, s, insn, adv_pci1724_dac_idle, 0);
108 outl(ctrl | PCI1724_DAC_CTRL_DATA(val),
109 dev->iobase + PCI1724_DAC_CTRL_REG);
111 s->readback[chan] = val;
117 static int adv_pci1724_auto_attach(struct comedi_device *dev,
118 unsigned long context_unused)
120 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
121 struct comedi_subdevice *s;
122 unsigned int board_id;
125 ret = comedi_pci_enable(dev);
129 dev->iobase = pci_resource_start(pcidev, 2);
130 board_id = inl(dev->iobase + PCI1724_BOARD_ID_REG);
131 dev_info(dev->class_dev, "board id: %d\n",
132 board_id & PCI1724_BOARD_ID_MASK);
134 ret = comedi_alloc_subdevices(dev, 3);
138 /* Analog Output subdevice */
139 s = &dev->subdevices[0];
140 s->type = COMEDI_SUBD_AO;
141 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_GROUND;
144 s->range_table = &adv_pci1724_ao_ranges;
145 s->insn_write = adv_pci1724_insn_write;
146 s->private = (void *)PCI1724_DAC_CTRL_MODE_NORMAL;
148 ret = comedi_alloc_subdev_readback(s);
152 /* Offset Calibration subdevice */
153 s = &dev->subdevices[1];
154 s->type = COMEDI_SUBD_CALIB;
155 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
158 s->insn_write = adv_pci1724_insn_write;
159 s->private = (void *)PCI1724_DAC_CTRL_MODE_OFFSET;
161 ret = comedi_alloc_subdev_readback(s);
165 /* Gain Calibration subdevice */
166 s = &dev->subdevices[2];
167 s->type = COMEDI_SUBD_CALIB;
168 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
171 s->insn_write = adv_pci1724_insn_write;
172 s->private = (void *)PCI1724_DAC_CTRL_MODE_GAIN;
174 return comedi_alloc_subdev_readback(s);
177 static struct comedi_driver adv_pci1724_driver = {
178 .driver_name = "adv_pci1724",
179 .module = THIS_MODULE,
180 .auto_attach = adv_pci1724_auto_attach,
181 .detach = comedi_pci_detach,
184 static int adv_pci1724_pci_probe(struct pci_dev *dev,
185 const struct pci_device_id *id)
187 return comedi_pci_auto_config(dev, &adv_pci1724_driver,
191 static const struct pci_device_id adv_pci1724_pci_table[] = {
192 { PCI_DEVICE(PCI_VENDOR_ID_ADVANTECH, 0x1724) },
195 MODULE_DEVICE_TABLE(pci, adv_pci1724_pci_table);
197 static struct pci_driver adv_pci1724_pci_driver = {
198 .name = "adv_pci1724",
199 .id_table = adv_pci1724_pci_table,
200 .probe = adv_pci1724_pci_probe,
201 .remove = comedi_pci_auto_unconfig,
203 module_comedi_pci_driver(adv_pci1724_driver, adv_pci1724_pci_driver);
205 MODULE_AUTHOR("Frank Mori Hess <fmh6jj@gmail.com>");
206 MODULE_DESCRIPTION("Advantech PCI-1724U Comedi driver");
207 MODULE_LICENSE("GPL");