2 comedi/drivers/pci1723.c
4 COMEDI - Linux Control and Measurement Device Interface
5 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
19 Description: Advantech PCI-1723
20 Author: yonggang <rsmgnu@gmail.com>, Ian Abbott <abbotti@mev.co.uk>
21 Devices: [Advantech] PCI-1723 (adv_pci1723)
22 Updated: Mon, 14 Apr 2008 15:12:56 +0100
25 Configuration Options:
26 [0] - PCI bus of device (optional)
27 [1] - PCI slot of device (optional)
29 If bus/slot is not specified, the first supported
30 PCI device found will be used.
32 Subdevice 0 is 8-channel AO, 16-bit, range +/- 10 V.
34 Subdevice 1 is 16-channel DIO. The channels are configurable as input or
35 output in 2 groups (0 to 7, 8 to 15). Configuring any channel implicitly
36 configures all channels in the same group.
40 1. Add the two milliamp ranges to the AO subdevice (0 to 20 mA, 4 to 20 mA).
41 2. Read the initial ranges and values of the AO subdevice at start-up instead
42 of reinitializing them.
43 3. Implement calibration.
46 #include <linux/module.h>
47 #include <linux/pci.h>
49 #include "../comedidev.h"
52 * PCI Bar 2 I/O Register map (dev->iobase)
54 #define PCI1723_AO_REG(x) (0x00 + ((x) * 2))
55 #define PCI1723_BOARD_ID_REG 0x10
56 #define PCI1723_BOARD_ID_MASK (0xf << 0)
57 #define PCI1723_SYNC_CTRL_REG 0x12
58 #define PCI1723_SYNC_CTRL_ASYNC (0 << 0)
59 #define PCI1723_SYNC_CTRL_SYNC (1 << 0)
60 #define PCI1723_CTRL_REG 0x14
61 #define PCI1723_CTRL_BUSY (1 << 15)
62 #define PCI1723_CTRL_INIT (1 << 14)
63 #define PCI1723_CTRL_SELF (1 << 8)
64 #define PCI1723_CTRL_IDX(x) (((x) & 0x3) << 6)
65 #define PCI1723_CTRL_RANGE(x) (((x) & 0x3) << 4)
66 #define PCI1723_CTRL_GAIN (0 << 3)
67 #define PCI1723_CTRL_OFFSET (1 << 3)
68 #define PCI1723_CTRL_CHAN(x) (((x) & 0x7) << 0)
69 #define PCI1723_CALIB_CTRL_REG 0x16
70 #define PCI1723_CALIB_CTRL_CS (1 << 2)
71 #define PCI1723_CALIB_CTRL_DAT (1 << 1)
72 #define PCI1723_CALIB_CTRL_CLK (1 << 0)
73 #define PCI1723_CALIB_STROBE_REG 0x18
74 #define PCI1723_DIO_CTRL_REG 0x1a
75 #define PCI1723_DIO_CTRL_HDIO (1 << 1)
76 #define PCI1723_DIO_CTRL_LDIO (1 << 0)
77 #define PCI1723_DIO_DATA_REG 0x1c
78 #define PCI1723_CALIB_DATA_REG 0x1e
79 #define PCI1723_SYNC_STROBE_REG 0x20
80 #define PCI1723_RESET_AO_STROBE_REG 0x22
81 #define PCI1723_RESET_CALIB_STROBE_REG 0x24
82 #define PCI1723_RANGE_STROBE_REG 0x26
83 #define PCI1723_VREF_REG 0x28
84 #define PCI1723_VREF_NEG10V (0 << 0)
85 #define PCI1723_VREF_0V (1 << 0)
86 #define PCI1723_VREF_POS10V (3 << 0)
88 struct pci1723_private {
89 unsigned char da_range[8]; /* D/A output range for each channel */
90 unsigned short ao_data[8]; /* data output buffer */
94 * The pci1723 card reset;
96 static int pci1723_reset(struct comedi_device *dev)
98 struct pci1723_private *devpriv = dev->private;
101 outw(PCI1723_SYNC_CTRL_SYNC, dev->iobase + PCI1723_SYNC_CTRL_REG);
103 for (i = 0; i < 8; i++) {
104 /* set all outputs to 0V */
105 devpriv->ao_data[i] = 0x8000;
106 outw(devpriv->ao_data[i], dev->iobase + PCI1723_AO_REG(i));
107 /* set all ranges to +/- 10V */
108 devpriv->da_range[i] = 0;
109 outw(((devpriv->da_range[i] << 4) | i),
113 outw(0, dev->iobase + PCI1723_RANGE_STROBE_REG);
114 outw(0, dev->iobase + PCI1723_SYNC_STROBE_REG);
116 outw(PCI1723_SYNC_CTRL_ASYNC, dev->iobase + PCI1723_SYNC_CTRL_REG);
121 static int pci1723_insn_read_ao(struct comedi_device *dev,
122 struct comedi_subdevice *s,
123 struct comedi_insn *insn, unsigned int *data)
125 struct pci1723_private *devpriv = dev->private;
128 chan = CR_CHAN(insn->chanspec);
129 for (n = 0; n < insn->n; n++)
130 data[n] = devpriv->ao_data[chan];
138 static int pci1723_ao_write_winsn(struct comedi_device *dev,
139 struct comedi_subdevice *s,
140 struct comedi_insn *insn, unsigned int *data)
142 struct pci1723_private *devpriv = dev->private;
143 unsigned int chan = CR_CHAN(insn->chanspec);
146 for (n = 0; n < insn->n; n++) {
147 devpriv->ao_data[chan] = data[n];
148 outw(data[n], dev->iobase + PCI1723_AO_REG(chan));
155 digital i/o config/query
157 static int pci1723_dio_insn_config(struct comedi_device *dev,
158 struct comedi_subdevice *s,
159 struct comedi_insn *insn, unsigned int *data)
161 unsigned int chan = CR_CHAN(insn->chanspec);
171 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
175 /* update hardware DIO mode */
176 mode = 0x0000; /* assume output */
177 if (!(s->io_bits & 0x00ff))
178 mode |= 0x0001; /* low byte input */
179 if (!(s->io_bits & 0xff00))
180 mode |= 0x0002; /* high byte input */
181 outw(mode, dev->iobase + PCI1723_DIO_CTRL_REG);
186 static int pci1723_dio_insn_bits(struct comedi_device *dev,
187 struct comedi_subdevice *s,
188 struct comedi_insn *insn,
191 if (comedi_dio_update_state(s, data))
192 outw(s->state, dev->iobase + PCI1723_DIO_DATA_REG);
194 data[1] = inw(dev->iobase + PCI1723_DIO_DATA_REG);
199 static int pci1723_auto_attach(struct comedi_device *dev,
200 unsigned long context_unused)
202 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
203 struct pci1723_private *devpriv;
204 struct comedi_subdevice *s;
207 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
211 ret = comedi_pci_enable(dev);
214 dev->iobase = pci_resource_start(pcidev, 2);
216 ret = comedi_alloc_subdevices(dev, 2);
220 s = &dev->subdevices[0];
221 dev->write_subdev = s;
222 s->type = COMEDI_SUBD_AO;
223 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
227 s->range_table = &range_bipolar10;
228 s->insn_write = pci1723_ao_write_winsn;
229 s->insn_read = pci1723_insn_read_ao;
231 s = &dev->subdevices[1];
232 s->type = COMEDI_SUBD_DIO;
233 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
236 s->len_chanlist = 16;
237 s->range_table = &range_digital;
238 s->insn_config = pci1723_dio_insn_config;
239 s->insn_bits = pci1723_dio_insn_bits;
241 /* read DIO config */
242 switch (inw(dev->iobase + PCI1723_DIO_CTRL_REG) & 0x03) {
243 case 0x00: /* low byte output, high byte output */
246 case 0x01: /* low byte input, high byte output */
249 case 0x02: /* low byte output, high byte input */
252 case 0x03: /* low byte input, high byte input */
256 /* read DIO port state */
257 s->state = inw(dev->iobase + PCI1723_DIO_DATA_REG);
264 static void pci1723_detach(struct comedi_device *dev)
268 comedi_pci_detach(dev);
271 static struct comedi_driver adv_pci1723_driver = {
272 .driver_name = "adv_pci1723",
273 .module = THIS_MODULE,
274 .auto_attach = pci1723_auto_attach,
275 .detach = pci1723_detach,
278 static int adv_pci1723_pci_probe(struct pci_dev *dev,
279 const struct pci_device_id *id)
281 return comedi_pci_auto_config(dev, &adv_pci1723_driver,
285 static const struct pci_device_id adv_pci1723_pci_table[] = {
286 { PCI_DEVICE(PCI_VENDOR_ID_ADVANTECH, 0x1723) },
289 MODULE_DEVICE_TABLE(pci, adv_pci1723_pci_table);
291 static struct pci_driver adv_pci1723_pci_driver = {
292 .name = "adv_pci1723",
293 .id_table = adv_pci1723_pci_table,
294 .probe = adv_pci1723_pci_probe,
295 .remove = comedi_pci_auto_unconfig,
297 module_comedi_pci_driver(adv_pci1723_driver, adv_pci1723_pci_driver);
299 MODULE_AUTHOR("Comedi http://www.comedi.org");
300 MODULE_DESCRIPTION("Comedi low-level driver");
301 MODULE_LICENSE("GPL");