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 short ao_data[8]; /* data output buffer */
93 * The pci1723 card reset;
95 static int pci1723_reset(struct comedi_device *dev)
97 struct pci1723_private *devpriv = dev->private;
100 outw(PCI1723_SYNC_CTRL_SYNC, dev->iobase + PCI1723_SYNC_CTRL_REG);
102 for (i = 0; i < 8; i++) {
103 /* set all outputs to 0V */
104 devpriv->ao_data[i] = 0x8000;
105 outw(devpriv->ao_data[i], dev->iobase + PCI1723_AO_REG(i));
106 /* set all ranges to +/- 10V */
107 outw(PCI1723_CTRL_RANGE(0) | PCI1723_CTRL_CHAN(i),
111 outw(0, dev->iobase + PCI1723_RANGE_STROBE_REG);
112 outw(0, dev->iobase + PCI1723_SYNC_STROBE_REG);
114 outw(PCI1723_SYNC_CTRL_ASYNC, dev->iobase + PCI1723_SYNC_CTRL_REG);
119 static int pci1723_insn_read_ao(struct comedi_device *dev,
120 struct comedi_subdevice *s,
121 struct comedi_insn *insn, unsigned int *data)
123 struct pci1723_private *devpriv = dev->private;
126 chan = CR_CHAN(insn->chanspec);
127 for (n = 0; n < insn->n; n++)
128 data[n] = devpriv->ao_data[chan];
136 static int pci1723_ao_write_winsn(struct comedi_device *dev,
137 struct comedi_subdevice *s,
138 struct comedi_insn *insn, unsigned int *data)
140 struct pci1723_private *devpriv = dev->private;
141 unsigned int chan = CR_CHAN(insn->chanspec);
144 for (n = 0; n < insn->n; n++) {
145 devpriv->ao_data[chan] = data[n];
146 outw(data[n], dev->iobase + PCI1723_AO_REG(chan));
153 digital i/o config/query
155 static int pci1723_dio_insn_config(struct comedi_device *dev,
156 struct comedi_subdevice *s,
157 struct comedi_insn *insn, unsigned int *data)
159 unsigned int chan = CR_CHAN(insn->chanspec);
169 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
173 /* update hardware DIO mode */
174 mode = 0x0000; /* assume output */
175 if (!(s->io_bits & 0x00ff))
176 mode |= 0x0001; /* low byte input */
177 if (!(s->io_bits & 0xff00))
178 mode |= 0x0002; /* high byte input */
179 outw(mode, dev->iobase + PCI1723_DIO_CTRL_REG);
184 static int pci1723_dio_insn_bits(struct comedi_device *dev,
185 struct comedi_subdevice *s,
186 struct comedi_insn *insn,
189 if (comedi_dio_update_state(s, data))
190 outw(s->state, dev->iobase + PCI1723_DIO_DATA_REG);
192 data[1] = inw(dev->iobase + PCI1723_DIO_DATA_REG);
197 static int pci1723_auto_attach(struct comedi_device *dev,
198 unsigned long context_unused)
200 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
201 struct pci1723_private *devpriv;
202 struct comedi_subdevice *s;
205 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
209 ret = comedi_pci_enable(dev);
212 dev->iobase = pci_resource_start(pcidev, 2);
214 ret = comedi_alloc_subdevices(dev, 2);
218 s = &dev->subdevices[0];
219 dev->write_subdev = s;
220 s->type = COMEDI_SUBD_AO;
221 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
225 s->range_table = &range_bipolar10;
226 s->insn_write = pci1723_ao_write_winsn;
227 s->insn_read = pci1723_insn_read_ao;
229 s = &dev->subdevices[1];
230 s->type = COMEDI_SUBD_DIO;
231 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
234 s->len_chanlist = 16;
235 s->range_table = &range_digital;
236 s->insn_config = pci1723_dio_insn_config;
237 s->insn_bits = pci1723_dio_insn_bits;
239 /* read DIO config */
240 switch (inw(dev->iobase + PCI1723_DIO_CTRL_REG) & 0x03) {
241 case 0x00: /* low byte output, high byte output */
244 case 0x01: /* low byte input, high byte output */
247 case 0x02: /* low byte output, high byte input */
250 case 0x03: /* low byte input, high byte input */
254 /* read DIO port state */
255 s->state = inw(dev->iobase + PCI1723_DIO_DATA_REG);
262 static void pci1723_detach(struct comedi_device *dev)
266 comedi_pci_detach(dev);
269 static struct comedi_driver adv_pci1723_driver = {
270 .driver_name = "adv_pci1723",
271 .module = THIS_MODULE,
272 .auto_attach = pci1723_auto_attach,
273 .detach = pci1723_detach,
276 static int adv_pci1723_pci_probe(struct pci_dev *dev,
277 const struct pci_device_id *id)
279 return comedi_pci_auto_config(dev, &adv_pci1723_driver,
283 static const struct pci_device_id adv_pci1723_pci_table[] = {
284 { PCI_DEVICE(PCI_VENDOR_ID_ADVANTECH, 0x1723) },
287 MODULE_DEVICE_TABLE(pci, adv_pci1723_pci_table);
289 static struct pci_driver adv_pci1723_pci_driver = {
290 .name = "adv_pci1723",
291 .id_table = adv_pci1723_pci_table,
292 .probe = adv_pci1723_pci_probe,
293 .remove = comedi_pci_auto_unconfig,
295 module_comedi_pci_driver(adv_pci1723_driver, adv_pci1723_pci_driver);
297 MODULE_AUTHOR("Comedi http://www.comedi.org");
298 MODULE_DESCRIPTION("Comedi low-level driver");
299 MODULE_LICENSE("GPL");