3 * Comedi driver for the Advantech PCI-1724U card.
5 * Author: Frank Mori Hess <fmh6jj@gmail.com>
6 * Copyright (C) 2013 GnuBIO Inc
8 * COMEDI - Linux Control and Measurement Device Interface
9 * Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
24 * Description: Advantech PCI-1724U
25 * Devices: [Advantech] PCI-1724U (adv_pci1724)
26 * Author: Frank Mori Hess <fmh6jj@gmail.com>
30 * Configuration Options: not applicable, uses comedi PCI auto config
32 * Subdevice 0 is the analog output.
33 * Subdevice 1 is the offset calibration for the analog output.
34 * Subdevice 2 is the gain calibration for the analog output.
36 * The calibration offset and gains have quite a large effect on the
37 * analog output, so it is possible to adjust the analog output to
38 * have an output range significantly different from the board's
39 * nominal output ranges. For a calibrated +/-10V range, the analog
40 * output's offset will be set somewhere near mid-range (0x2000) and
41 * its gain will be near maximum (0x3fff).
43 * There is really no difference between the board's documented 0-20mA
44 * versus 4-20mA output ranges. To pick one or the other is simply a
45 * matter of adjusting the offset and gain calibration until the board
46 * outputs in the desired range.
49 #include <linux/module.h>
51 #include "../comedi_pci.h"
54 * PCI bar 2 Register I/O map (dev->iobase)
56 #define PCI1724_DAC_CTRL_REG 0x00
57 #define PCI1724_DAC_CTRL_GX(x) (1 << (20 + ((x) / 8)))
58 #define PCI1724_DAC_CTRL_CX(x) (((x) % 8) << 16)
59 #define PCI1724_DAC_CTRL_MODE_GAIN (1 << 14)
60 #define PCI1724_DAC_CTRL_MODE_OFFSET (2 << 14)
61 #define PCI1724_DAC_CTRL_MODE_NORMAL (3 << 14)
62 #define PCI1724_DAC_CTRL_MODE_MASK (3 << 14)
63 #define PCI1724_DAC_CTRL_DATA(x) (((x) & 0x3fff) << 0)
64 #define PCI1724_SYNC_CTRL_REG 0x04
65 #define PCI1724_SYNC_CTRL_DACSTAT (1 << 1)
66 #define PCI1724_SYNC_CTRL_SYN (1 << 0)
67 #define PCI1724_EEPROM_CTRL_REG 0x08
68 #define PCI1724_SYNC_TRIG_REG 0x0c /* any value works */
69 #define PCI1724_BOARD_ID_REG 0x10
70 #define PCI1724_BOARD_ID_MASK (0xf << 0)
72 static const struct comedi_lrange adv_pci1724_ao_ranges = {
81 static int adv_pci1724_dac_idle(struct comedi_device *dev,
82 struct comedi_subdevice *s,
83 struct comedi_insn *insn,
84 unsigned long context)
88 status = inl(dev->iobase + PCI1724_SYNC_CTRL_REG);
89 if ((status & PCI1724_SYNC_CTRL_DACSTAT) == 0)
94 static int adv_pci1724_insn_write(struct comedi_device *dev,
95 struct comedi_subdevice *s,
96 struct comedi_insn *insn,
99 unsigned long mode = (unsigned long)s->private;
100 unsigned int chan = CR_CHAN(insn->chanspec);
105 ctrl = PCI1724_DAC_CTRL_GX(chan) | PCI1724_DAC_CTRL_CX(chan) | mode;
107 /* turn off synchronous mode */
108 outl(0, dev->iobase + PCI1724_SYNC_CTRL_REG);
110 for (i = 0; i < insn->n; ++i) {
111 unsigned int val = data[i];
113 ret = comedi_timeout(dev, s, insn, adv_pci1724_dac_idle, 0);
117 outl(ctrl | PCI1724_DAC_CTRL_DATA(val),
118 dev->iobase + PCI1724_DAC_CTRL_REG);
120 s->readback[chan] = val;
126 static int adv_pci1724_auto_attach(struct comedi_device *dev,
127 unsigned long context_unused)
129 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
130 struct comedi_subdevice *s;
131 unsigned int board_id;
134 ret = comedi_pci_enable(dev);
138 dev->iobase = pci_resource_start(pcidev, 2);
139 board_id = inl(dev->iobase + PCI1724_BOARD_ID_REG);
140 dev_info(dev->class_dev, "board id: %d\n",
141 board_id & PCI1724_BOARD_ID_MASK);
143 ret = comedi_alloc_subdevices(dev, 3);
147 /* Analog Output subdevice */
148 s = &dev->subdevices[0];
149 s->type = COMEDI_SUBD_AO;
150 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_GROUND;
153 s->range_table = &adv_pci1724_ao_ranges;
154 s->insn_write = adv_pci1724_insn_write;
155 s->private = (void *)PCI1724_DAC_CTRL_MODE_NORMAL;
157 ret = comedi_alloc_subdev_readback(s);
161 /* Offset Calibration subdevice */
162 s = &dev->subdevices[1];
163 s->type = COMEDI_SUBD_CALIB;
164 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
167 s->insn_write = adv_pci1724_insn_write;
168 s->private = (void *)PCI1724_DAC_CTRL_MODE_OFFSET;
170 ret = comedi_alloc_subdev_readback(s);
174 /* Gain Calibration subdevice */
175 s = &dev->subdevices[2];
176 s->type = COMEDI_SUBD_CALIB;
177 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
180 s->insn_write = adv_pci1724_insn_write;
181 s->private = (void *)PCI1724_DAC_CTRL_MODE_GAIN;
183 ret = comedi_alloc_subdev_readback(s);
190 static struct comedi_driver adv_pci1724_driver = {
191 .driver_name = "adv_pci1724",
192 .module = THIS_MODULE,
193 .auto_attach = adv_pci1724_auto_attach,
194 .detach = comedi_pci_detach,
197 static int adv_pci1724_pci_probe(struct pci_dev *dev,
198 const struct pci_device_id *id)
200 return comedi_pci_auto_config(dev, &adv_pci1724_driver,
204 static const struct pci_device_id adv_pci1724_pci_table[] = {
205 { PCI_DEVICE(PCI_VENDOR_ID_ADVANTECH, 0x1724) },
208 MODULE_DEVICE_TABLE(pci, adv_pci1724_pci_table);
210 static struct pci_driver adv_pci1724_pci_driver = {
211 .name = "adv_pci1724",
212 .id_table = adv_pci1724_pci_table,
213 .probe = adv_pci1724_pci_probe,
214 .remove = comedi_pci_auto_unconfig,
216 module_comedi_pci_driver(adv_pci1724_driver, adv_pci1724_pci_driver);
218 MODULE_AUTHOR("Frank Mori Hess <fmh6jj@gmail.com>");
219 MODULE_DESCRIPTION("Advantech PCI-1724U Comedi driver");
220 MODULE_LICENSE("GPL");