1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <linux/module.h>
35
36#include "../comedi_pci.h"
37
38
39
40
41#define PCI1723_AO_REG(x) (0x00 + ((x) * 2))
42#define PCI1723_BOARD_ID_REG 0x10
43#define PCI1723_BOARD_ID_MASK (0xf << 0)
44#define PCI1723_SYNC_CTRL_REG 0x12
45#define PCI1723_SYNC_CTRL(x) (((x) & 0x1) << 0)
46#define PCI1723_SYNC_CTRL_ASYNC PCI1723_SYNC_CTRL(0)
47#define PCI1723_SYNC_CTRL_SYNC PCI1723_SYNC_CTRL(1)
48#define PCI1723_CTRL_REG 0x14
49#define PCI1723_CTRL_BUSY BIT(15)
50#define PCI1723_CTRL_INIT BIT(14)
51#define PCI1723_CTRL_SELF BIT(8)
52#define PCI1723_CTRL_IDX(x) (((x) & 0x3) << 6)
53#define PCI1723_CTRL_RANGE(x) (((x) & 0x3) << 4)
54#define PCI1723_CTRL_SEL(x) (((x) & 0x1) << 3)
55#define PCI1723_CTRL_GAIN PCI1723_CTRL_SEL(0)
56#define PCI1723_CTRL_OFFSET PCI1723_CTRL_SEL(1)
57#define PCI1723_CTRL_CHAN(x) (((x) & 0x7) << 0)
58#define PCI1723_CALIB_CTRL_REG 0x16
59#define PCI1723_CALIB_CTRL_CS BIT(2)
60#define PCI1723_CALIB_CTRL_DAT BIT(1)
61#define PCI1723_CALIB_CTRL_CLK BIT(0)
62#define PCI1723_CALIB_STROBE_REG 0x18
63#define PCI1723_DIO_CTRL_REG 0x1a
64#define PCI1723_DIO_CTRL_HDIO BIT(1)
65#define PCI1723_DIO_CTRL_LDIO BIT(0)
66#define PCI1723_DIO_DATA_REG 0x1c
67#define PCI1723_CALIB_DATA_REG 0x1e
68#define PCI1723_SYNC_STROBE_REG 0x20
69#define PCI1723_RESET_AO_STROBE_REG 0x22
70#define PCI1723_RESET_CALIB_STROBE_REG 0x24
71#define PCI1723_RANGE_STROBE_REG 0x26
72#define PCI1723_VREF_REG 0x28
73#define PCI1723_VREF(x) (((x) & 0x3) << 0)
74#define PCI1723_VREF_NEG10V PCI1723_VREF(0)
75#define PCI1723_VREF_0V PCI1723_VREF(1)
76#define PCI1723_VREF_POS10V PCI1723_VREF(3)
77
78static int pci1723_ao_insn_write(struct comedi_device *dev,
79 struct comedi_subdevice *s,
80 struct comedi_insn *insn,
81 unsigned int *data)
82{
83 unsigned int chan = CR_CHAN(insn->chanspec);
84 int i;
85
86 for (i = 0; i < insn->n; i++) {
87 unsigned int val = data[i];
88
89 outw(val, dev->iobase + PCI1723_AO_REG(chan));
90 s->readback[chan] = val;
91 }
92
93 return insn->n;
94}
95
96static int pci1723_dio_insn_config(struct comedi_device *dev,
97 struct comedi_subdevice *s,
98 struct comedi_insn *insn,
99 unsigned int *data)
100{
101 unsigned int chan = CR_CHAN(insn->chanspec);
102 unsigned int mask = (chan < 8) ? 0x00ff : 0xff00;
103 unsigned short mode = 0x0000;
104 int ret;
105
106 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
107 if (ret)
108 return ret;
109
110 if (!(s->io_bits & 0x00ff))
111 mode |= PCI1723_DIO_CTRL_LDIO;
112 if (!(s->io_bits & 0xff00))
113 mode |= PCI1723_DIO_CTRL_HDIO;
114 outw(mode, dev->iobase + PCI1723_DIO_CTRL_REG);
115
116 return insn->n;
117}
118
119static int pci1723_dio_insn_bits(struct comedi_device *dev,
120 struct comedi_subdevice *s,
121 struct comedi_insn *insn,
122 unsigned int *data)
123{
124 if (comedi_dio_update_state(s, data))
125 outw(s->state, dev->iobase + PCI1723_DIO_DATA_REG);
126
127 data[1] = inw(dev->iobase + PCI1723_DIO_DATA_REG);
128
129 return insn->n;
130}
131
132static int pci1723_auto_attach(struct comedi_device *dev,
133 unsigned long context_unused)
134{
135 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
136 struct comedi_subdevice *s;
137 unsigned int val;
138 int ret;
139 int i;
140
141 ret = comedi_pci_enable(dev);
142 if (ret)
143 return ret;
144 dev->iobase = pci_resource_start(pcidev, 2);
145
146 ret = comedi_alloc_subdevices(dev, 2);
147 if (ret)
148 return ret;
149
150 s = &dev->subdevices[0];
151 s->type = COMEDI_SUBD_AO;
152 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
153 s->n_chan = 8;
154 s->maxdata = 0xffff;
155 s->range_table = &range_bipolar10;
156 s->insn_write = pci1723_ao_insn_write;
157
158 ret = comedi_alloc_subdev_readback(s);
159 if (ret)
160 return ret;
161
162
163 outw(PCI1723_SYNC_CTRL_SYNC, dev->iobase + PCI1723_SYNC_CTRL_REG);
164 for (i = 0; i < s->n_chan; i++) {
165 outw(PCI1723_CTRL_RANGE(0) | PCI1723_CTRL_CHAN(i),
166 PCI1723_CTRL_REG);
167 outw(0, dev->iobase + PCI1723_RANGE_STROBE_REG);
168
169 outw(0x8000, dev->iobase + PCI1723_AO_REG(i));
170 s->readback[i] = 0x8000;
171 }
172 outw(0, dev->iobase + PCI1723_SYNC_STROBE_REG);
173
174
175 outw(PCI1723_SYNC_CTRL_ASYNC, dev->iobase + PCI1723_SYNC_CTRL_REG);
176
177 s = &dev->subdevices[1];
178 s->type = COMEDI_SUBD_DIO;
179 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
180 s->n_chan = 16;
181 s->maxdata = 1;
182 s->range_table = &range_digital;
183 s->insn_config = pci1723_dio_insn_config;
184 s->insn_bits = pci1723_dio_insn_bits;
185
186
187 val = inw(dev->iobase + PCI1723_DIO_CTRL_REG);
188 if (!(val & PCI1723_DIO_CTRL_LDIO))
189 s->io_bits |= 0x00ff;
190 if (!(val & PCI1723_DIO_CTRL_HDIO))
191 s->io_bits |= 0xff00;
192 s->state = inw(dev->iobase + PCI1723_DIO_DATA_REG);
193
194 return 0;
195}
196
197static struct comedi_driver adv_pci1723_driver = {
198 .driver_name = "adv_pci1723",
199 .module = THIS_MODULE,
200 .auto_attach = pci1723_auto_attach,
201 .detach = comedi_pci_detach,
202};
203
204static int adv_pci1723_pci_probe(struct pci_dev *dev,
205 const struct pci_device_id *id)
206{
207 return comedi_pci_auto_config(dev, &adv_pci1723_driver,
208 id->driver_data);
209}
210
211static const struct pci_device_id adv_pci1723_pci_table[] = {
212 { PCI_DEVICE(PCI_VENDOR_ID_ADVANTECH, 0x1723) },
213 { 0 }
214};
215MODULE_DEVICE_TABLE(pci, adv_pci1723_pci_table);
216
217static struct pci_driver adv_pci1723_pci_driver = {
218 .name = "adv_pci1723",
219 .id_table = adv_pci1723_pci_table,
220 .probe = adv_pci1723_pci_probe,
221 .remove = comedi_pci_auto_unconfig,
222};
223module_comedi_pci_driver(adv_pci1723_driver, adv_pci1723_pci_driver);
224
225MODULE_AUTHOR("Comedi http://www.comedi.org");
226MODULE_DESCRIPTION("Advantech PCI-1723 Comedi driver");
227MODULE_LICENSE("GPL");
228