1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/module.h>
25#include <linux/comedi/comedi_pci.h>
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48#define PCI1760_OMB_REG(x) (0x0c + (x))
49#define PCI1760_IMB_REG(x) (0x1c + (x))
50#define PCI1760_INTCSR_REG(x) (0x38 + (x))
51#define PCI1760_INTCSR1_IRQ_ENA BIT(5)
52#define PCI1760_INTCSR2_OMB_IRQ BIT(0)
53#define PCI1760_INTCSR2_IMB_IRQ BIT(1)
54#define PCI1760_INTCSR2_IRQ_STATUS BIT(6)
55#define PCI1760_INTCSR2_IRQ_ASSERTED BIT(7)
56
57
58#define PCI1760_CMD_CLR_IMB2 0x00
59#define PCI1760_CMD_SET_DO 0x01
60#define PCI1760_CMD_GET_DO 0x02
61#define PCI1760_CMD_GET_STATUS 0x03
62#define PCI1760_CMD_GET_FW_VER 0x0e
63#define PCI1760_CMD_GET_HW_VER 0x0f
64#define PCI1760_CMD_SET_PWM_HI(x) (0x10 + (x) * 2)
65#define PCI1760_CMD_SET_PWM_LO(x) (0x11 + (x) * 2)
66#define PCI1760_CMD_SET_PWM_CNT(x) (0x14 + (x))
67#define PCI1760_CMD_ENA_PWM 0x1f
68#define PCI1760_CMD_ENA_FILT 0x20
69#define PCI1760_CMD_ENA_PAT_MATCH 0x21
70#define PCI1760_CMD_SET_PAT_MATCH 0x22
71#define PCI1760_CMD_ENA_RISE_EDGE 0x23
72#define PCI1760_CMD_ENA_FALL_EDGE 0x24
73#define PCI1760_CMD_ENA_CNT 0x28
74#define PCI1760_CMD_RST_CNT 0x29
75#define PCI1760_CMD_ENA_CNT_OFLOW 0x2a
76#define PCI1760_CMD_ENA_CNT_MATCH 0x2b
77#define PCI1760_CMD_SET_CNT_EDGE 0x2c
78#define PCI1760_CMD_GET_CNT 0x2f
79#define PCI1760_CMD_SET_HI_SAMP(x) (0x30 + (x))
80#define PCI1760_CMD_SET_LO_SAMP(x) (0x38 + (x))
81#define PCI1760_CMD_SET_CNT(x) (0x40 + (x))
82#define PCI1760_CMD_SET_CNT_MATCH(x) (0x48 + (x))
83#define PCI1760_CMD_GET_INT_FLAGS 0x60
84#define PCI1760_CMD_GET_INT_FLAGS_MATCH BIT(0)
85#define PCI1760_CMD_GET_INT_FLAGS_COS BIT(1)
86#define PCI1760_CMD_GET_INT_FLAGS_OFLOW BIT(2)
87#define PCI1760_CMD_GET_OS 0x61
88#define PCI1760_CMD_GET_CNT_STATUS 0x62
89
90#define PCI1760_CMD_TIMEOUT 250
91#define PCI1760_CMD_RETRIES 3
92
93#define PCI1760_PWM_TIMEBASE 100000
94
95static int pci1760_send_cmd(struct comedi_device *dev,
96 unsigned char cmd, unsigned short val)
97{
98 unsigned long timeout;
99
100
101 outb(val & 0xff, dev->iobase + PCI1760_OMB_REG(0));
102 outb((val >> 8) & 0xff, dev->iobase + PCI1760_OMB_REG(1));
103 outb(cmd, dev->iobase + PCI1760_OMB_REG(2));
104 outb(0, dev->iobase + PCI1760_OMB_REG(3));
105
106
107 timeout = jiffies + usecs_to_jiffies(PCI1760_CMD_TIMEOUT);
108 do {
109 if (inb(dev->iobase + PCI1760_IMB_REG(2)) == cmd) {
110
111 return inb(dev->iobase + PCI1760_IMB_REG(0)) |
112 (inb(dev->iobase + PCI1760_IMB_REG(1)) << 8);
113 }
114 cpu_relax();
115 } while (time_before(jiffies, timeout));
116
117 return -EBUSY;
118}
119
120static int pci1760_cmd(struct comedi_device *dev,
121 unsigned char cmd, unsigned short val)
122{
123 int repeats;
124 int ret;
125
126
127 if (inb(dev->iobase + PCI1760_IMB_REG(2)) == cmd) {
128 ret = pci1760_send_cmd(dev, PCI1760_CMD_CLR_IMB2, 0);
129 if (ret < 0) {
130
131 ret = pci1760_send_cmd(dev, PCI1760_CMD_CLR_IMB2, 0);
132 if (ret < 0)
133 return -ETIMEDOUT;
134 }
135 }
136
137
138 for (repeats = 0; repeats < PCI1760_CMD_RETRIES; repeats++) {
139 ret = pci1760_send_cmd(dev, cmd, val);
140 if (ret >= 0)
141 return ret;
142 }
143
144
145 return -ETIMEDOUT;
146}
147
148static int pci1760_di_insn_bits(struct comedi_device *dev,
149 struct comedi_subdevice *s,
150 struct comedi_insn *insn,
151 unsigned int *data)
152{
153 data[1] = inb(dev->iobase + PCI1760_IMB_REG(3));
154
155 return insn->n;
156}
157
158static int pci1760_do_insn_bits(struct comedi_device *dev,
159 struct comedi_subdevice *s,
160 struct comedi_insn *insn,
161 unsigned int *data)
162{
163 int ret;
164
165 if (comedi_dio_update_state(s, data)) {
166 ret = pci1760_cmd(dev, PCI1760_CMD_SET_DO, s->state);
167 if (ret < 0)
168 return ret;
169 }
170
171 data[1] = s->state;
172
173 return insn->n;
174}
175
176static int pci1760_pwm_ns_to_div(unsigned int flags, unsigned int ns)
177{
178 unsigned int divisor;
179
180 switch (flags) {
181 case CMDF_ROUND_NEAREST:
182 divisor = DIV_ROUND_CLOSEST(ns, PCI1760_PWM_TIMEBASE);
183 break;
184 case CMDF_ROUND_UP:
185 divisor = DIV_ROUND_UP(ns, PCI1760_PWM_TIMEBASE);
186 break;
187 case CMDF_ROUND_DOWN:
188 divisor = ns / PCI1760_PWM_TIMEBASE;
189 break;
190 default:
191 return -EINVAL;
192 }
193
194 if (divisor < 1)
195 divisor = 1;
196 if (divisor > 0xffff)
197 divisor = 0xffff;
198
199 return divisor;
200}
201
202static int pci1760_pwm_enable(struct comedi_device *dev,
203 unsigned int chan, bool enable)
204{
205 int ret;
206
207 ret = pci1760_cmd(dev, PCI1760_CMD_GET_STATUS, PCI1760_CMD_ENA_PWM);
208 if (ret < 0)
209 return ret;
210
211 if (enable)
212 ret |= BIT(chan);
213 else
214 ret &= ~BIT(chan);
215
216 return pci1760_cmd(dev, PCI1760_CMD_ENA_PWM, ret);
217}
218
219static int pci1760_pwm_insn_config(struct comedi_device *dev,
220 struct comedi_subdevice *s,
221 struct comedi_insn *insn,
222 unsigned int *data)
223{
224 unsigned int chan = CR_CHAN(insn->chanspec);
225 int hi_div;
226 int lo_div;
227 int ret;
228
229 switch (data[0]) {
230 case INSN_CONFIG_ARM:
231 ret = pci1760_pwm_enable(dev, chan, false);
232 if (ret < 0)
233 return ret;
234
235 if (data[1] > 0xffff)
236 return -EINVAL;
237 ret = pci1760_cmd(dev, PCI1760_CMD_SET_PWM_CNT(chan), data[1]);
238 if (ret < 0)
239 return ret;
240
241 ret = pci1760_pwm_enable(dev, chan, true);
242 if (ret < 0)
243 return ret;
244 break;
245 case INSN_CONFIG_DISARM:
246 ret = pci1760_pwm_enable(dev, chan, false);
247 if (ret < 0)
248 return ret;
249 break;
250 case INSN_CONFIG_PWM_OUTPUT:
251 ret = pci1760_pwm_enable(dev, chan, false);
252 if (ret < 0)
253 return ret;
254
255 hi_div = pci1760_pwm_ns_to_div(data[1], data[2]);
256 lo_div = pci1760_pwm_ns_to_div(data[3], data[4]);
257 if (hi_div < 0 || lo_div < 0)
258 return -EINVAL;
259 if ((hi_div * PCI1760_PWM_TIMEBASE) != data[2] ||
260 (lo_div * PCI1760_PWM_TIMEBASE) != data[4]) {
261 data[2] = hi_div * PCI1760_PWM_TIMEBASE;
262 data[4] = lo_div * PCI1760_PWM_TIMEBASE;
263 return -EAGAIN;
264 }
265 ret = pci1760_cmd(dev, PCI1760_CMD_SET_PWM_HI(chan), hi_div);
266 if (ret < 0)
267 return ret;
268 ret = pci1760_cmd(dev, PCI1760_CMD_SET_PWM_LO(chan), lo_div);
269 if (ret < 0)
270 return ret;
271 break;
272 case INSN_CONFIG_GET_PWM_OUTPUT:
273 hi_div = pci1760_cmd(dev, PCI1760_CMD_GET_STATUS,
274 PCI1760_CMD_SET_PWM_HI(chan));
275 lo_div = pci1760_cmd(dev, PCI1760_CMD_GET_STATUS,
276 PCI1760_CMD_SET_PWM_LO(chan));
277 if (hi_div < 0 || lo_div < 0)
278 return -ETIMEDOUT;
279
280 data[1] = hi_div * PCI1760_PWM_TIMEBASE;
281 data[2] = lo_div * PCI1760_PWM_TIMEBASE;
282 break;
283 case INSN_CONFIG_GET_PWM_STATUS:
284 ret = pci1760_cmd(dev, PCI1760_CMD_GET_STATUS,
285 PCI1760_CMD_ENA_PWM);
286 if (ret < 0)
287 return ret;
288
289 data[1] = (ret & BIT(chan)) ? 1 : 0;
290 break;
291 default:
292 return -EINVAL;
293 }
294
295 return insn->n;
296}
297
298static void pci1760_reset(struct comedi_device *dev)
299{
300 int i;
301
302
303 outb(0, dev->iobase + PCI1760_INTCSR_REG(0));
304 outb(0, dev->iobase + PCI1760_INTCSR_REG(1));
305 outb(0, dev->iobase + PCI1760_INTCSR_REG(3));
306
307
308 pci1760_cmd(dev, PCI1760_CMD_ENA_CNT, 0);
309
310
311 pci1760_cmd(dev, PCI1760_CMD_ENA_CNT_OFLOW, 0);
312
313
314 pci1760_cmd(dev, PCI1760_CMD_ENA_CNT_MATCH, 0);
315
316
317 for (i = 0; i < 8; i++) {
318 pci1760_cmd(dev, PCI1760_CMD_SET_CNT_MATCH(i), 0x8000);
319 pci1760_cmd(dev, PCI1760_CMD_SET_CNT(i), 0x0000);
320 }
321
322
323 pci1760_cmd(dev, PCI1760_CMD_RST_CNT, 0xff);
324
325
326 pci1760_cmd(dev, PCI1760_CMD_SET_CNT_EDGE, 0);
327
328
329 pci1760_cmd(dev, PCI1760_CMD_ENA_FILT, 0);
330
331
332 pci1760_cmd(dev, PCI1760_CMD_ENA_PAT_MATCH, 0);
333
334
335 pci1760_cmd(dev, PCI1760_CMD_SET_PAT_MATCH, 0);
336}
337
338static int pci1760_auto_attach(struct comedi_device *dev,
339 unsigned long context)
340{
341 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
342 struct comedi_subdevice *s;
343 int ret;
344
345 ret = comedi_pci_enable(dev);
346 if (ret)
347 return ret;
348 dev->iobase = pci_resource_start(pcidev, 0);
349
350 pci1760_reset(dev);
351
352 ret = comedi_alloc_subdevices(dev, 4);
353 if (ret)
354 return ret;
355
356
357 s = &dev->subdevices[0];
358 s->type = COMEDI_SUBD_DI;
359 s->subdev_flags = SDF_READABLE;
360 s->n_chan = 8;
361 s->maxdata = 1;
362 s->range_table = &range_digital;
363 s->insn_bits = pci1760_di_insn_bits;
364
365
366 s = &dev->subdevices[1];
367 s->type = COMEDI_SUBD_DO;
368 s->subdev_flags = SDF_WRITABLE;
369 s->n_chan = 8;
370 s->maxdata = 1;
371 s->range_table = &range_digital;
372 s->insn_bits = pci1760_do_insn_bits;
373
374
375 ret = pci1760_cmd(dev, PCI1760_CMD_GET_DO, 0);
376 if (ret < 0)
377 return ret;
378 s->state = ret;
379
380
381 s = &dev->subdevices[2];
382 s->type = COMEDI_SUBD_PWM;
383 s->subdev_flags = SDF_PWM_COUNTER;
384 s->n_chan = 2;
385 s->insn_config = pci1760_pwm_insn_config;
386
387
388 s = &dev->subdevices[3];
389 s->type = COMEDI_SUBD_UNUSED;
390
391 return 0;
392}
393
394static struct comedi_driver pci1760_driver = {
395 .driver_name = "adv_pci1760",
396 .module = THIS_MODULE,
397 .auto_attach = pci1760_auto_attach,
398 .detach = comedi_pci_detach,
399};
400
401static int pci1760_pci_probe(struct pci_dev *dev,
402 const struct pci_device_id *id)
403{
404 return comedi_pci_auto_config(dev, &pci1760_driver, id->driver_data);
405}
406
407static const struct pci_device_id pci1760_pci_table[] = {
408 { PCI_DEVICE(PCI_VENDOR_ID_ADVANTECH, 0x1760) },
409 { 0 }
410};
411MODULE_DEVICE_TABLE(pci, pci1760_pci_table);
412
413static struct pci_driver pci1760_pci_driver = {
414 .name = "adv_pci1760",
415 .id_table = pci1760_pci_table,
416 .probe = pci1760_pci_probe,
417 .remove = comedi_pci_auto_unconfig,
418};
419module_comedi_pci_driver(pci1760_driver, pci1760_pci_driver);
420
421MODULE_AUTHOR("Comedi https://www.comedi.org");
422MODULE_DESCRIPTION("Comedi driver for Advantech PCI-1760");
423MODULE_LICENSE("GPL");
424