3 * Comedi driver for the Advantech PCI-1723 card.
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
21 * Description: Advantech PCI-1723
22 * Author: yonggang <rsmgnu@gmail.com>, Ian Abbott <abbotti@mev.co.uk>
23 * Devices: [Advantech] PCI-1723 (adv_pci1723)
24 * Updated: Mon, 14 Apr 2008 15:12:56 +0100
27 * Configuration Options: not applicable, uses comedi PCI auto config
29 * Subdevice 0 is 8-channel AO, 16-bit, range +/- 10 V.
31 * Subdevice 1 is 16-channel DIO. The channels are configurable as
32 * input or output in 2 groups (0 to 7, 8 to 15). Configuring any
33 * channel implicitly configures all channels in the same group.
36 * 1. Add the two milliamp ranges to the AO subdevice (0 to 20 mA,
38 * 2. Read the initial ranges and values of the AO subdevice at
39 * start-up instead of reinitializing them.
40 * 3. Implement calibration.
43 #include <linux/module.h>
45 #include "../comedi_pci.h"
48 * PCI Bar 2 I/O Register map (dev->iobase)
50 #define PCI1723_AO_REG(x) (0x00 + ((x) * 2))
51 #define PCI1723_BOARD_ID_REG 0x10
52 #define PCI1723_BOARD_ID_MASK (0xf << 0)
53 #define PCI1723_SYNC_CTRL_REG 0x12
54 #define PCI1723_SYNC_CTRL(x) (((x) & 0x1) << 0)
55 #define PCI1723_SYNC_CTRL_ASYNC PCI1723_SYNC_CTRL(0)
56 #define PCI1723_SYNC_CTRL_SYNC PCI1723_SYNC_CTRL(1)
57 #define PCI1723_CTRL_REG 0x14
58 #define PCI1723_CTRL_BUSY BIT(15)
59 #define PCI1723_CTRL_INIT BIT(14)
60 #define PCI1723_CTRL_SELF BIT(8)
61 #define PCI1723_CTRL_IDX(x) (((x) & 0x3) << 6)
62 #define PCI1723_CTRL_RANGE(x) (((x) & 0x3) << 4)
63 #define PCI1723_CTRL_SEL(x) (((x) & 0x1) << 3)
64 #define PCI1723_CTRL_GAIN PCI1723_CTRL_SEL(0)
65 #define PCI1723_CTRL_OFFSET PCI1723_CTRL_SEL(1)
66 #define PCI1723_CTRL_CHAN(x) (((x) & 0x7) << 0)
67 #define PCI1723_CALIB_CTRL_REG 0x16
68 #define PCI1723_CALIB_CTRL_CS BIT(2)
69 #define PCI1723_CALIB_CTRL_DAT BIT(1)
70 #define PCI1723_CALIB_CTRL_CLK BIT(0)
71 #define PCI1723_CALIB_STROBE_REG 0x18
72 #define PCI1723_DIO_CTRL_REG 0x1a
73 #define PCI1723_DIO_CTRL_HDIO BIT(1)
74 #define PCI1723_DIO_CTRL_LDIO BIT(0)
75 #define PCI1723_DIO_DATA_REG 0x1c
76 #define PCI1723_CALIB_DATA_REG 0x1e
77 #define PCI1723_SYNC_STROBE_REG 0x20
78 #define PCI1723_RESET_AO_STROBE_REG 0x22
79 #define PCI1723_RESET_CALIB_STROBE_REG 0x24
80 #define PCI1723_RANGE_STROBE_REG 0x26
81 #define PCI1723_VREF_REG 0x28
82 #define PCI1723_VREF(x) (((x) & 0x3) << 0)
83 #define PCI1723_VREF_NEG10V PCI1723_VREF(0)
84 #define PCI1723_VREF_0V PCI1723_VREF(1)
85 #define PCI1723_VREF_POS10V PCI1723_VREF(3)
87 static int pci1723_ao_insn_write(struct comedi_device *dev,
88 struct comedi_subdevice *s,
89 struct comedi_insn *insn,
92 unsigned int chan = CR_CHAN(insn->chanspec);
95 for (i = 0; i < insn->n; i++) {
96 unsigned int val = data[i];
98 outw(val, dev->iobase + PCI1723_AO_REG(chan));
99 s->readback[chan] = val;
105 static int pci1723_dio_insn_config(struct comedi_device *dev,
106 struct comedi_subdevice *s,
107 struct comedi_insn *insn,
110 unsigned int chan = CR_CHAN(insn->chanspec);
111 unsigned int mask = (chan < 8) ? 0x00ff : 0xff00;
112 unsigned short mode = 0x0000; /* assume output */
115 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
119 if (!(s->io_bits & 0x00ff))
120 mode |= PCI1723_DIO_CTRL_LDIO; /* low byte input */
121 if (!(s->io_bits & 0xff00))
122 mode |= PCI1723_DIO_CTRL_HDIO; /* high byte input */
123 outw(mode, dev->iobase + PCI1723_DIO_CTRL_REG);
128 static int pci1723_dio_insn_bits(struct comedi_device *dev,
129 struct comedi_subdevice *s,
130 struct comedi_insn *insn,
133 if (comedi_dio_update_state(s, data))
134 outw(s->state, dev->iobase + PCI1723_DIO_DATA_REG);
136 data[1] = inw(dev->iobase + PCI1723_DIO_DATA_REG);
141 static int pci1723_auto_attach(struct comedi_device *dev,
142 unsigned long context_unused)
144 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
145 struct comedi_subdevice *s;
150 ret = comedi_pci_enable(dev);
153 dev->iobase = pci_resource_start(pcidev, 2);
155 ret = comedi_alloc_subdevices(dev, 2);
159 s = &dev->subdevices[0];
160 s->type = COMEDI_SUBD_AO;
161 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
164 s->range_table = &range_bipolar10;
165 s->insn_write = pci1723_ao_insn_write;
167 ret = comedi_alloc_subdev_readback(s);
171 /* synchronously reset all analog outputs to 0V, +/-10V range */
172 outw(PCI1723_SYNC_CTRL_SYNC, dev->iobase + PCI1723_SYNC_CTRL_REG);
173 for (i = 0; i < s->n_chan; i++) {
174 outw(PCI1723_CTRL_RANGE(0) | PCI1723_CTRL_CHAN(i),
176 outw(0, dev->iobase + PCI1723_RANGE_STROBE_REG);
178 outw(0x8000, dev->iobase + PCI1723_AO_REG(i));
179 s->readback[i] = 0x8000;
181 outw(0, dev->iobase + PCI1723_SYNC_STROBE_REG);
183 /* disable syncronous control */
184 outw(PCI1723_SYNC_CTRL_ASYNC, dev->iobase + PCI1723_SYNC_CTRL_REG);
186 s = &dev->subdevices[1];
187 s->type = COMEDI_SUBD_DIO;
188 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
191 s->range_table = &range_digital;
192 s->insn_config = pci1723_dio_insn_config;
193 s->insn_bits = pci1723_dio_insn_bits;
195 /* get initial DIO direction and state */
196 val = inw(dev->iobase + PCI1723_DIO_CTRL_REG);
197 if (!(val & PCI1723_DIO_CTRL_LDIO))
198 s->io_bits |= 0x00ff; /* low byte output */
199 if (!(val & PCI1723_DIO_CTRL_HDIO))
200 s->io_bits |= 0xff00; /* high byte output */
201 s->state = inw(dev->iobase + PCI1723_DIO_DATA_REG);
206 static struct comedi_driver adv_pci1723_driver = {
207 .driver_name = "adv_pci1723",
208 .module = THIS_MODULE,
209 .auto_attach = pci1723_auto_attach,
210 .detach = comedi_pci_detach,
213 static int adv_pci1723_pci_probe(struct pci_dev *dev,
214 const struct pci_device_id *id)
216 return comedi_pci_auto_config(dev, &adv_pci1723_driver,
220 static const struct pci_device_id adv_pci1723_pci_table[] = {
221 { PCI_DEVICE(PCI_VENDOR_ID_ADVANTECH, 0x1723) },
224 MODULE_DEVICE_TABLE(pci, adv_pci1723_pci_table);
226 static struct pci_driver adv_pci1723_pci_driver = {
227 .name = "adv_pci1723",
228 .id_table = adv_pci1723_pci_table,
229 .probe = adv_pci1723_pci_probe,
230 .remove = comedi_pci_auto_unconfig,
232 module_comedi_pci_driver(adv_pci1723_driver, adv_pci1723_pci_driver);
234 MODULE_AUTHOR("Comedi http://www.comedi.org");
235 MODULE_DESCRIPTION("Advantech PCI-1723 Comedi driver");
236 MODULE_LICENSE("GPL");