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
35
36
37
38
39
40
41
42
43
44
45
46#include <linux/module.h>
47#include <linux/delay.h>
48
49#include "../comedi_pci.h"
50
51#define ICP_MULTI_ADC_CSR 0x00
52#define ICP_MULTI_ADC_CSR_ST BIT(0)
53#define ICP_MULTI_ADC_CSR_BSY BIT(0)
54#define ICP_MULTI_ADC_CSR_BI BIT(4)
55#define ICP_MULTI_ADC_CSR_RA BIT(5)
56#define ICP_MULTI_ADC_CSR_DI BIT(6)
57#define ICP_MULTI_ADC_CSR_DI_CHAN(x) (((x) & 0x7) << 9)
58#define ICP_MULTI_ADC_CSR_SE_CHAN(x) (((x) & 0xf) << 8)
59#define ICP_MULTI_AI 2
60#define ICP_MULTI_DAC_CSR 0x04
61#define ICP_MULTI_DAC_CSR_ST BIT(0)
62#define ICP_MULTI_DAC_CSR_BSY BIT(0)
63#define ICP_MULTI_DAC_CSR_BI BIT(4)
64#define ICP_MULTI_DAC_CSR_RA BIT(5)
65#define ICP_MULTI_DAC_CSR_CHAN(x) (((x) & 0x3) << 8)
66#define ICP_MULTI_AO 6
67#define ICP_MULTI_DI 8
68#define ICP_MULTI_DO 0x0A
69#define ICP_MULTI_INT_EN 0x0c
70#define ICP_MULTI_INT_STAT 0x0e
71#define ICP_MULTI_INT_ADC_RDY BIT(0)
72#define ICP_MULTI_INT_DAC_RDY BIT(1)
73#define ICP_MULTI_INT_DOUT_ERR BIT(2)
74#define ICP_MULTI_INT_DIN_STAT BIT(3)
75#define ICP_MULTI_INT_CIE0 BIT(4)
76#define ICP_MULTI_INT_CIE1 BIT(5)
77#define ICP_MULTI_INT_CIE2 BIT(6)
78#define ICP_MULTI_INT_CIE3 BIT(7)
79#define ICP_MULTI_INT_MASK 0xff
80#define ICP_MULTI_CNTR0 0x10
81#define ICP_MULTI_CNTR1 0x12
82#define ICP_MULTI_CNTR2 0x14
83#define ICP_MULTI_CNTR3 0x16
84
85
86static const struct comedi_lrange icp_multi_ranges = {
87 4, {
88 UNI_RANGE(5),
89 UNI_RANGE(10),
90 BIP_RANGE(5),
91 BIP_RANGE(10)
92 }
93};
94
95static const char range_codes_analog[] = { 0x00, 0x20, 0x10, 0x30 };
96
97static int icp_multi_ai_eoc(struct comedi_device *dev,
98 struct comedi_subdevice *s,
99 struct comedi_insn *insn,
100 unsigned long context)
101{
102 unsigned int status;
103
104 status = readw(dev->mmio + ICP_MULTI_ADC_CSR);
105 if ((status & ICP_MULTI_ADC_CSR_BSY) == 0)
106 return 0;
107 return -EBUSY;
108}
109
110static int icp_multi_ai_insn_read(struct comedi_device *dev,
111 struct comedi_subdevice *s,
112 struct comedi_insn *insn,
113 unsigned int *data)
114{
115 unsigned int chan = CR_CHAN(insn->chanspec);
116 unsigned int range = CR_RANGE(insn->chanspec);
117 unsigned int aref = CR_AREF(insn->chanspec);
118 unsigned int adc_csr;
119 int ret = 0;
120 int n;
121
122
123 if (aref == AREF_DIFF) {
124 adc_csr = ICP_MULTI_ADC_CSR_DI_CHAN(chan) |
125 ICP_MULTI_ADC_CSR_DI;
126 } else {
127 adc_csr = ICP_MULTI_ADC_CSR_SE_CHAN(chan);
128 }
129 adc_csr |= range_codes_analog[range];
130 writew(adc_csr, dev->mmio + ICP_MULTI_ADC_CSR);
131
132 for (n = 0; n < insn->n; n++) {
133
134 writew(adc_csr | ICP_MULTI_ADC_CSR_ST,
135 dev->mmio + ICP_MULTI_ADC_CSR);
136
137 udelay(1);
138
139
140 ret = comedi_timeout(dev, s, insn, icp_multi_ai_eoc, 0);
141 if (ret)
142 break;
143
144 data[n] = (readw(dev->mmio + ICP_MULTI_AI) >> 4) & 0x0fff;
145 }
146
147 return ret ? ret : n;
148}
149
150static int icp_multi_ao_ready(struct comedi_device *dev,
151 struct comedi_subdevice *s,
152 struct comedi_insn *insn,
153 unsigned long context)
154{
155 unsigned int status;
156
157 status = readw(dev->mmio + ICP_MULTI_DAC_CSR);
158 if ((status & ICP_MULTI_DAC_CSR_BSY) == 0)
159 return 0;
160 return -EBUSY;
161}
162
163static int icp_multi_ao_insn_write(struct comedi_device *dev,
164 struct comedi_subdevice *s,
165 struct comedi_insn *insn,
166 unsigned int *data)
167{
168 unsigned int chan = CR_CHAN(insn->chanspec);
169 unsigned int range = CR_RANGE(insn->chanspec);
170 unsigned int dac_csr;
171 int i;
172
173
174 dac_csr = ICP_MULTI_DAC_CSR_CHAN(chan);
175 dac_csr |= range_codes_analog[range];
176 writew(dac_csr, dev->mmio + ICP_MULTI_DAC_CSR);
177
178 for (i = 0; i < insn->n; i++) {
179 unsigned int val = data[i];
180 int ret;
181
182
183 ret = comedi_timeout(dev, s, insn, icp_multi_ao_ready, 0);
184 if (ret)
185 return ret;
186
187 writew(val, dev->mmio + ICP_MULTI_AO);
188
189
190 writew(dac_csr | ICP_MULTI_DAC_CSR_ST,
191 dev->mmio + ICP_MULTI_DAC_CSR);
192
193 s->readback[chan] = val;
194 }
195
196 return insn->n;
197}
198
199static int icp_multi_di_insn_bits(struct comedi_device *dev,
200 struct comedi_subdevice *s,
201 struct comedi_insn *insn,
202 unsigned int *data)
203{
204 data[1] = readw(dev->mmio + ICP_MULTI_DI);
205
206 return insn->n;
207}
208
209static int icp_multi_do_insn_bits(struct comedi_device *dev,
210 struct comedi_subdevice *s,
211 struct comedi_insn *insn,
212 unsigned int *data)
213{
214 if (comedi_dio_update_state(s, data))
215 writew(s->state, dev->mmio + ICP_MULTI_DO);
216
217 data[1] = s->state;
218
219 return insn->n;
220}
221
222static int icp_multi_reset(struct comedi_device *dev)
223{
224 int i;
225
226
227 writew(0, dev->mmio + ICP_MULTI_INT_EN);
228 writew(ICP_MULTI_INT_MASK, dev->mmio + ICP_MULTI_INT_STAT);
229
230
231 for (i = 0; i < 4; i++) {
232 unsigned int dac_csr = ICP_MULTI_DAC_CSR_CHAN(i);
233
234
235 writew(dac_csr, dev->mmio + ICP_MULTI_DAC_CSR);
236
237
238 writew(0, dev->mmio + ICP_MULTI_AO);
239
240
241 writew(dac_csr | ICP_MULTI_DAC_CSR_ST,
242 dev->mmio + ICP_MULTI_DAC_CSR);
243 udelay(1);
244 }
245
246
247 writew(0, dev->mmio + ICP_MULTI_DO);
248
249 return 0;
250}
251
252static int icp_multi_auto_attach(struct comedi_device *dev,
253 unsigned long context_unused)
254{
255 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
256 struct comedi_subdevice *s;
257 int ret;
258
259 ret = comedi_pci_enable(dev);
260 if (ret)
261 return ret;
262
263 dev->mmio = pci_ioremap_bar(pcidev, 2);
264 if (!dev->mmio)
265 return -ENOMEM;
266
267 ret = comedi_alloc_subdevices(dev, 4);
268 if (ret)
269 return ret;
270
271 icp_multi_reset(dev);
272
273
274 s = &dev->subdevices[0];
275 s->type = COMEDI_SUBD_AI;
276 s->subdev_flags = SDF_READABLE | SDF_COMMON | SDF_GROUND | SDF_DIFF;
277 s->n_chan = 16;
278 s->maxdata = 0x0fff;
279 s->range_table = &icp_multi_ranges;
280 s->insn_read = icp_multi_ai_insn_read;
281
282
283 s = &dev->subdevices[1];
284 s->type = COMEDI_SUBD_AO;
285 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
286 s->n_chan = 4;
287 s->maxdata = 0x0fff;
288 s->range_table = &icp_multi_ranges;
289 s->insn_write = icp_multi_ao_insn_write;
290
291 ret = comedi_alloc_subdev_readback(s);
292 if (ret)
293 return ret;
294
295
296 s = &dev->subdevices[2];
297 s->type = COMEDI_SUBD_DI;
298 s->subdev_flags = SDF_READABLE;
299 s->n_chan = 16;
300 s->maxdata = 1;
301 s->range_table = &range_digital;
302 s->insn_bits = icp_multi_di_insn_bits;
303
304
305 s = &dev->subdevices[3];
306 s->type = COMEDI_SUBD_DO;
307 s->subdev_flags = SDF_WRITABLE;
308 s->n_chan = 8;
309 s->maxdata = 1;
310 s->range_table = &range_digital;
311 s->insn_bits = icp_multi_do_insn_bits;
312
313 return 0;
314}
315
316static struct comedi_driver icp_multi_driver = {
317 .driver_name = "icp_multi",
318 .module = THIS_MODULE,
319 .auto_attach = icp_multi_auto_attach,
320 .detach = comedi_pci_detach,
321};
322
323static int icp_multi_pci_probe(struct pci_dev *dev,
324 const struct pci_device_id *id)
325{
326 return comedi_pci_auto_config(dev, &icp_multi_driver, id->driver_data);
327}
328
329static const struct pci_device_id icp_multi_pci_table[] = {
330 { PCI_DEVICE(PCI_VENDOR_ID_ICP, 0x8000) },
331 { 0 }
332};
333MODULE_DEVICE_TABLE(pci, icp_multi_pci_table);
334
335static struct pci_driver icp_multi_pci_driver = {
336 .name = "icp_multi",
337 .id_table = icp_multi_pci_table,
338 .probe = icp_multi_pci_probe,
339 .remove = comedi_pci_auto_unconfig,
340};
341module_comedi_pci_driver(icp_multi_driver, icp_multi_pci_driver);
342
343MODULE_AUTHOR("Comedi http://www.comedi.org");
344MODULE_DESCRIPTION("Comedi driver for Inova ICP_MULTI board");
345MODULE_LICENSE("GPL");
346