1 // SPDX-License-Identifier: GPL-2.0+
4 * Comedi driver for National Instruments PCI-6527
6 * COMEDI - Linux Control and Measurement Device Interface
7 * Copyright (C) 1999,2002,2003 David A. Schleef <ds@schleef.org>
12 * Description: National Instruments 6527
13 * Devices: [National Instruments] PCI-6527 (pci-6527), PXI-6527 (pxi-6527)
14 * Author: David A. Schleef <ds@schleef.org>
15 * Updated: Sat, 25 Jan 2003 13:24:40 -0800
18 * Configuration Options: not applicable, uses PCI auto config
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
24 #include "../comedi_pci.h"
27 * PCI BAR1 - Register memory map
29 * Manuals (available from ftp://ftp.natinst.com/support/manuals)
30 * 370106b.pdf 6527 Register Level Programmer Manual
32 #define NI6527_DI_REG(x) (0x00 + (x))
33 #define NI6527_DO_REG(x) (0x03 + (x))
34 #define NI6527_ID_REG 0x06
35 #define NI6527_CLR_REG 0x07
36 #define NI6527_CLR_EDGE BIT(3)
37 #define NI6527_CLR_OVERFLOW BIT(2)
38 #define NI6527_CLR_FILT BIT(1)
39 #define NI6527_CLR_INTERVAL BIT(0)
40 #define NI6527_CLR_IRQS (NI6527_CLR_EDGE | NI6527_CLR_OVERFLOW)
41 #define NI6527_CLR_RESET_FILT (NI6527_CLR_FILT | NI6527_CLR_INTERVAL)
42 #define NI6527_FILT_INTERVAL_REG(x) (0x08 + (x))
43 #define NI6527_FILT_ENA_REG(x) (0x0c + (x))
44 #define NI6527_STATUS_REG 0x14
45 #define NI6527_STATUS_IRQ BIT(2)
46 #define NI6527_STATUS_OVERFLOW BIT(1)
47 #define NI6527_STATUS_EDGE BIT(0)
48 #define NI6527_CTRL_REG 0x15
49 #define NI6527_CTRL_FALLING BIT(4)
50 #define NI6527_CTRL_RISING BIT(3)
51 #define NI6527_CTRL_IRQ BIT(2)
52 #define NI6527_CTRL_OVERFLOW BIT(1)
53 #define NI6527_CTRL_EDGE BIT(0)
54 #define NI6527_CTRL_DISABLE_IRQS 0
55 #define NI6527_CTRL_ENABLE_IRQS (NI6527_CTRL_FALLING | \
56 NI6527_CTRL_RISING | \
57 NI6527_CTRL_IRQ | NI6527_CTRL_EDGE)
58 #define NI6527_RISING_EDGE_REG(x) (0x18 + (x))
59 #define NI6527_FALLING_EDGE_REG(x) (0x20 + (x))
70 static const struct ni6527_board ni6527_boards[] = {
79 struct ni6527_private {
80 unsigned int filter_interval;
81 unsigned int filter_enable;
84 static void ni6527_set_filter_interval(struct comedi_device *dev,
87 struct ni6527_private *devpriv = dev->private;
89 if (val != devpriv->filter_interval) {
90 writeb(val & 0xff, dev->mmio + NI6527_FILT_INTERVAL_REG(0));
91 writeb((val >> 8) & 0xff,
92 dev->mmio + NI6527_FILT_INTERVAL_REG(1));
93 writeb((val >> 16) & 0x0f,
94 dev->mmio + NI6527_FILT_INTERVAL_REG(2));
96 writeb(NI6527_CLR_INTERVAL, dev->mmio + NI6527_CLR_REG);
98 devpriv->filter_interval = val;
102 static void ni6527_set_filter_enable(struct comedi_device *dev,
105 writeb(val & 0xff, dev->mmio + NI6527_FILT_ENA_REG(0));
106 writeb((val >> 8) & 0xff, dev->mmio + NI6527_FILT_ENA_REG(1));
107 writeb((val >> 16) & 0xff, dev->mmio + NI6527_FILT_ENA_REG(2));
110 static int ni6527_di_insn_config(struct comedi_device *dev,
111 struct comedi_subdevice *s,
112 struct comedi_insn *insn,
115 struct ni6527_private *devpriv = dev->private;
116 unsigned int chan = CR_CHAN(insn->chanspec);
117 unsigned int interval;
120 case INSN_CONFIG_FILTER:
122 * The deglitch filter interval is specified in nanoseconds.
123 * The hardware supports intervals in 200ns increments. Round
124 * the user values up and return the actual interval.
126 interval = (data[1] + 100) / 200;
127 data[1] = interval * 200;
130 ni6527_set_filter_interval(dev, interval);
131 devpriv->filter_enable |= 1 << chan;
133 devpriv->filter_enable &= ~(1 << chan);
135 ni6527_set_filter_enable(dev, devpriv->filter_enable);
144 static int ni6527_di_insn_bits(struct comedi_device *dev,
145 struct comedi_subdevice *s,
146 struct comedi_insn *insn,
151 val = readb(dev->mmio + NI6527_DI_REG(0));
152 val |= (readb(dev->mmio + NI6527_DI_REG(1)) << 8);
153 val |= (readb(dev->mmio + NI6527_DI_REG(2)) << 16);
160 static int ni6527_do_insn_bits(struct comedi_device *dev,
161 struct comedi_subdevice *s,
162 struct comedi_insn *insn,
167 mask = comedi_dio_update_state(s, data);
169 /* Outputs are inverted */
170 unsigned int val = s->state ^ 0xffffff;
173 writeb(val & 0xff, dev->mmio + NI6527_DO_REG(0));
175 writeb((val >> 8) & 0xff,
176 dev->mmio + NI6527_DO_REG(1));
178 writeb((val >> 16) & 0xff,
179 dev->mmio + NI6527_DO_REG(2));
187 static irqreturn_t ni6527_interrupt(int irq, void *d)
189 struct comedi_device *dev = d;
190 struct comedi_subdevice *s = dev->read_subdev;
193 status = readb(dev->mmio + NI6527_STATUS_REG);
194 if (!(status & NI6527_STATUS_IRQ))
197 if (status & NI6527_STATUS_EDGE) {
198 comedi_buf_write_samples(s, &s->state, 1);
199 comedi_handle_events(dev, s);
202 writeb(NI6527_CLR_IRQS, dev->mmio + NI6527_CLR_REG);
207 static int ni6527_intr_cmdtest(struct comedi_device *dev,
208 struct comedi_subdevice *s,
209 struct comedi_cmd *cmd)
213 /* Step 1 : check if triggers are trivially valid */
215 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
216 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_OTHER);
217 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
218 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
219 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT);
224 /* Step 2a : make sure trigger sources are unique */
225 /* Step 2b : and mutually compatible */
227 /* Step 3: check if arguments are trivially valid */
229 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
230 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
231 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
232 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
234 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
239 /* Step 4: fix up any arguments */
241 /* Step 5: check channel list if it exists */
246 static int ni6527_intr_cmd(struct comedi_device *dev,
247 struct comedi_subdevice *s)
249 writeb(NI6527_CLR_IRQS, dev->mmio + NI6527_CLR_REG);
250 writeb(NI6527_CTRL_ENABLE_IRQS, dev->mmio + NI6527_CTRL_REG);
255 static int ni6527_intr_cancel(struct comedi_device *dev,
256 struct comedi_subdevice *s)
258 writeb(NI6527_CTRL_DISABLE_IRQS, dev->mmio + NI6527_CTRL_REG);
263 static int ni6527_intr_insn_bits(struct comedi_device *dev,
264 struct comedi_subdevice *s,
265 struct comedi_insn *insn, unsigned int *data)
271 static void ni6527_set_edge_detection(struct comedi_device *dev,
274 unsigned int falling)
280 for (i = 0; i < 2; i++) {
283 /* preserve rising-edge detection channels */
284 rising |= readb(dev->mmio +
285 NI6527_RISING_EDGE_REG(i)) &
287 /* preserve falling-edge detection channels */
288 falling |= readb(dev->mmio +
289 NI6527_FALLING_EDGE_REG(i)) &
292 /* update rising-edge detection channels */
293 writeb(rising & 0xff,
294 dev->mmio + NI6527_RISING_EDGE_REG(i));
295 /* update falling-edge detection channels */
296 writeb(falling & 0xff,
297 dev->mmio + NI6527_FALLING_EDGE_REG(i));
305 static int ni6527_intr_insn_config(struct comedi_device *dev,
306 struct comedi_subdevice *s,
307 struct comedi_insn *insn,
310 unsigned int mask = 0xffffffff;
311 unsigned int rising, falling, shift;
314 case INSN_CONFIG_CHANGE_NOTIFY:
315 /* check_insn_config_length() does not check this instruction */
320 ni6527_set_edge_detection(dev, mask, rising, falling);
322 case INSN_CONFIG_DIGITAL_TRIG:
323 /* check trigger number */
326 /* check digital trigger operation */
328 case COMEDI_DIGITAL_TRIG_DISABLE:
332 case COMEDI_DIGITAL_TRIG_ENABLE_EDGES:
333 /* check shift amount */
341 rising = data[4] << shift;
342 falling = data[5] << shift;
348 ni6527_set_edge_detection(dev, mask, rising, falling);
357 static void ni6527_reset(struct comedi_device *dev)
359 /* disable deglitch filters on all channels */
360 ni6527_set_filter_enable(dev, 0);
362 /* disable edge detection */
363 ni6527_set_edge_detection(dev, 0xffffffff, 0, 0);
365 writeb(NI6527_CLR_IRQS | NI6527_CLR_RESET_FILT,
366 dev->mmio + NI6527_CLR_REG);
367 writeb(NI6527_CTRL_DISABLE_IRQS, dev->mmio + NI6527_CTRL_REG);
370 static int ni6527_auto_attach(struct comedi_device *dev,
371 unsigned long context)
373 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
374 const struct ni6527_board *board = NULL;
375 struct ni6527_private *devpriv;
376 struct comedi_subdevice *s;
379 if (context < ARRAY_SIZE(ni6527_boards))
380 board = &ni6527_boards[context];
383 dev->board_ptr = board;
384 dev->board_name = board->name;
386 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
390 ret = comedi_pci_enable(dev);
394 dev->mmio = pci_ioremap_bar(pcidev, 1);
398 /* make sure this is actually a 6527 device */
399 if (readb(dev->mmio + NI6527_ID_REG) != 0x27)
404 ret = request_irq(pcidev->irq, ni6527_interrupt, IRQF_SHARED,
405 dev->board_name, dev);
407 dev->irq = pcidev->irq;
409 ret = comedi_alloc_subdevices(dev, 3);
413 /* Digital Input subdevice */
414 s = &dev->subdevices[0];
415 s->type = COMEDI_SUBD_DI;
416 s->subdev_flags = SDF_READABLE;
419 s->range_table = &range_digital;
420 s->insn_config = ni6527_di_insn_config;
421 s->insn_bits = ni6527_di_insn_bits;
423 /* Digital Output subdevice */
424 s = &dev->subdevices[1];
425 s->type = COMEDI_SUBD_DO;
426 s->subdev_flags = SDF_WRITABLE;
429 s->range_table = &range_digital;
430 s->insn_bits = ni6527_do_insn_bits;
432 /* Edge detection interrupt subdevice */
433 s = &dev->subdevices[2];
435 dev->read_subdev = s;
436 s->type = COMEDI_SUBD_DI;
437 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
440 s->range_table = &range_digital;
441 s->insn_config = ni6527_intr_insn_config;
442 s->insn_bits = ni6527_intr_insn_bits;
444 s->do_cmdtest = ni6527_intr_cmdtest;
445 s->do_cmd = ni6527_intr_cmd;
446 s->cancel = ni6527_intr_cancel;
448 s->type = COMEDI_SUBD_UNUSED;
454 static void ni6527_detach(struct comedi_device *dev)
458 comedi_pci_detach(dev);
461 static struct comedi_driver ni6527_driver = {
462 .driver_name = "ni_6527",
463 .module = THIS_MODULE,
464 .auto_attach = ni6527_auto_attach,
465 .detach = ni6527_detach,
468 static int ni6527_pci_probe(struct pci_dev *dev,
469 const struct pci_device_id *id)
471 return comedi_pci_auto_config(dev, &ni6527_driver, id->driver_data);
474 static const struct pci_device_id ni6527_pci_table[] = {
475 { PCI_VDEVICE(NI, 0x2b10), BOARD_PXI6527 },
476 { PCI_VDEVICE(NI, 0x2b20), BOARD_PCI6527 },
479 MODULE_DEVICE_TABLE(pci, ni6527_pci_table);
481 static struct pci_driver ni6527_pci_driver = {
483 .id_table = ni6527_pci_table,
484 .probe = ni6527_pci_probe,
485 .remove = comedi_pci_auto_unconfig,
487 module_comedi_pci_driver(ni6527_driver, ni6527_pci_driver);
489 MODULE_AUTHOR("Comedi http://www.comedi.org");
490 MODULE_DESCRIPTION("Comedi driver for National Instruments PCI-6527");
491 MODULE_LICENSE("GPL");