3 * Comedi driver for National Instruments PCI-6527
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 1999,2002,2003 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: National Instruments 6527
22 * Devices: (National Instruments) PCI-6527 [pci-6527]
23 * (National Instruments) PXI-6527 [pxi-6527]
24 * Author: David A. Schleef <ds@schleef.org>
25 * Updated: Sat, 25 Jan 2003 13:24:40 -0800
28 * Configuration Options: not applicable, uses PCI auto config
31 #include <linux/module.h>
32 #include <linux/pci.h>
33 #include <linux/interrupt.h>
35 #include "../comedidev.h"
37 #include "comedi_fc.h"
40 * PCI BAR1 - Register memory map
42 * Manuals (available from ftp://ftp.natinst.com/support/manuals)
43 * 370106b.pdf 6527 Register Level Programmer Manual
45 #define NI6527_DI_REG(x) (0x00 + (x))
46 #define NI6527_DO_REG(x) (0x03 + (x))
47 #define NI6527_ID_REG 0x06
48 #define NI6527_CLR_REG 0x07
49 #define NI6527_CLR_EDGE (1 << 3)
50 #define NI6527_CLR_OVERFLOW (1 << 2)
51 #define NI6527_CLR_FILT (1 << 1)
52 #define NI6527_CLR_INTERVAL (1 << 0)
53 #define NI6527_CLR_IRQS (NI6527_CLR_EDGE | NI6527_CLR_OVERFLOW)
54 #define NI6527_CLR_RESET_FILT (NI6527_CLR_FILT | NI6527_CLR_INTERVAL)
55 #define NI6527_FILT_INTERVAL_REG(x) (0x08 + (x))
56 #define NI6527_FILT_ENA_REG(x) (0x0c + (x))
57 #define NI6527_STATUS_REG 0x14
58 #define NI6527_STATUS_IRQ (1 << 2)
59 #define NI6527_STATUS_OVERFLOW (1 << 1)
60 #define NI6527_STATUS_EDGE (1 << 0)
61 #define NI6527_CTRL_REG 0x15
62 #define NI6527_CTRL_FALLING (1 << 4)
63 #define NI6527_CTRL_RISING (1 << 3)
64 #define NI6527_CTRL_IRQ (1 << 2)
65 #define NI6527_CTRL_OVERFLOW (1 << 1)
66 #define NI6527_CTRL_EDGE (1 << 0)
67 #define NI6527_CTRL_DISABLE_IRQS 0
68 #define NI6527_CTRL_ENABLE_IRQS (NI6527_CTRL_FALLING | \
69 NI6527_CTRL_RISING | \
70 NI6527_CTRL_IRQ | NI6527_CTRL_EDGE)
71 #define NI6527_RISING_EDGE_REG(x) (0x18 + (x))
72 #define NI6527_FALLING_EDGE_REG(x) (0x20 + (x))
83 static const struct ni6527_board ni6527_boards[] = {
92 struct ni6527_private {
93 void __iomem *mmio_base;
94 unsigned int filter_interval;
95 unsigned int filter_enable;
98 static void ni6527_set_filter_interval(struct comedi_device *dev,
101 struct ni6527_private *devpriv = dev->private;
102 void __iomem *mmio = devpriv->mmio_base;
104 if (val != devpriv->filter_interval) {
105 writeb(val & 0xff, mmio + NI6527_FILT_INTERVAL_REG(0));
106 writeb((val >> 8) & 0xff, mmio + NI6527_FILT_INTERVAL_REG(1));
107 writeb((val >> 16) & 0x0f, mmio + NI6527_FILT_INTERVAL_REG(2));
109 writeb(NI6527_CLR_INTERVAL, mmio + NI6527_CLR_REG);
111 devpriv->filter_interval = val;
115 static void ni6527_set_filter_enable(struct comedi_device *dev,
118 struct ni6527_private *devpriv = dev->private;
119 void __iomem *mmio = devpriv->mmio_base;
121 writeb(val & 0xff, mmio + NI6527_FILT_ENA_REG(0));
122 writeb((val >> 8) & 0xff, mmio + NI6527_FILT_ENA_REG(1));
123 writeb((val >> 16) & 0xff, mmio + NI6527_FILT_ENA_REG(2));
126 static int ni6527_di_insn_config(struct comedi_device *dev,
127 struct comedi_subdevice *s,
128 struct comedi_insn *insn,
131 struct ni6527_private *devpriv = dev->private;
132 unsigned int chan = CR_CHAN(insn->chanspec);
133 unsigned int interval;
136 case INSN_CONFIG_FILTER:
138 * The deglitch filter interval is specified in nanoseconds.
139 * The hardware supports intervals in 200ns increments. Round
140 * the user values up and return the actual interval.
142 interval = (data[1] + 100) / 200;
143 data[1] = interval * 200;
146 ni6527_set_filter_interval(dev, interval);
147 devpriv->filter_enable |= 1 << chan;
149 devpriv->filter_enable &= ~(1 << chan);
151 ni6527_set_filter_enable(dev, devpriv->filter_enable);
160 static int ni6527_di_insn_bits(struct comedi_device *dev,
161 struct comedi_subdevice *s,
162 struct comedi_insn *insn,
165 struct ni6527_private *devpriv = dev->private;
166 void __iomem *mmio = devpriv->mmio_base;
169 val = readb(mmio + NI6527_DI_REG(0));
170 val |= (readb(mmio + NI6527_DI_REG(1)) << 8);
171 val |= (readb(mmio + NI6527_DI_REG(2)) << 16);
178 static int ni6527_do_insn_bits(struct comedi_device *dev,
179 struct comedi_subdevice *s,
180 struct comedi_insn *insn,
183 struct ni6527_private *devpriv = dev->private;
184 void __iomem *mmio = devpriv->mmio_base;
187 mask = comedi_dio_update_state(s, data);
189 /* Outputs are inverted */
190 unsigned int val = s->state ^ 0xffffff;
193 writeb(val & 0xff, mmio + NI6527_DO_REG(0));
195 writeb((val >> 8) & 0xff, mmio + NI6527_DO_REG(1));
197 writeb((val >> 16) & 0xff, mmio + NI6527_DO_REG(2));
205 static irqreturn_t ni6527_interrupt(int irq, void *d)
207 struct comedi_device *dev = d;
208 struct ni6527_private *devpriv = dev->private;
209 struct comedi_subdevice *s = dev->read_subdev;
210 void __iomem *mmio = devpriv->mmio_base;
213 status = readb(mmio + NI6527_STATUS_REG);
214 if (!(status & NI6527_STATUS_IRQ))
217 if (status & NI6527_STATUS_EDGE) {
218 comedi_buf_put(s->async, 0);
219 s->async->events |= COMEDI_CB_EOS;
220 comedi_event(dev, s);
223 writeb(NI6527_CLR_IRQS, mmio + NI6527_CLR_REG);
228 static int ni6527_intr_cmdtest(struct comedi_device *dev,
229 struct comedi_subdevice *s,
230 struct comedi_cmd *cmd)
234 /* Step 1 : check if triggers are trivially valid */
236 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
237 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_OTHER);
238 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
239 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
240 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT);
245 /* Step 2a : make sure trigger sources are unique */
246 /* Step 2b : and mutually compatible */
251 /* Step 3: check if arguments are trivially valid */
253 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
254 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
255 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
256 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, 1);
257 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
262 /* step 4: fix up any arguments */
270 static int ni6527_intr_cmd(struct comedi_device *dev,
271 struct comedi_subdevice *s)
273 struct ni6527_private *devpriv = dev->private;
274 void __iomem *mmio = devpriv->mmio_base;
276 writeb(NI6527_CLR_IRQS, mmio + NI6527_CLR_REG);
277 writeb(NI6527_CTRL_ENABLE_IRQS, mmio + NI6527_CTRL_REG);
282 static int ni6527_intr_cancel(struct comedi_device *dev,
283 struct comedi_subdevice *s)
285 struct ni6527_private *devpriv = dev->private;
286 void __iomem *mmio = devpriv->mmio_base;
288 writeb(NI6527_CTRL_DISABLE_IRQS, mmio + NI6527_CTRL_REG);
293 static int ni6527_intr_insn_bits(struct comedi_device *dev,
294 struct comedi_subdevice *s,
295 struct comedi_insn *insn, unsigned int *data)
301 static void ni6527_set_edge_detection(struct comedi_device *dev,
303 unsigned int falling)
305 struct ni6527_private *devpriv = dev->private;
306 void __iomem *mmio = devpriv->mmio_base;
308 /* enable rising-edge detection channels */
309 writeb(rising & 0xff, mmio + NI6527_RISING_EDGE_REG(0));
310 writeb((rising >> 8) & 0xff, mmio + NI6527_RISING_EDGE_REG(1));
311 writeb((rising >> 16) & 0xff, mmio + NI6527_RISING_EDGE_REG(2));
313 /* enable falling-edge detection channels */
314 writeb(falling & 0xff, mmio + NI6527_FALLING_EDGE_REG(0));
315 writeb((falling >> 8) & 0xff, mmio + NI6527_FALLING_EDGE_REG(1));
316 writeb((falling >> 16) & 0xff, mmio + NI6527_FALLING_EDGE_REG(2));
319 static int ni6527_intr_insn_config(struct comedi_device *dev,
320 struct comedi_subdevice *s,
321 struct comedi_insn *insn,
325 case INSN_CONFIG_CHANGE_NOTIFY:
326 /* check_insn_config_length() does not check this instruction */
329 ni6527_set_edge_detection(dev, data[1], data[2]);
338 static void ni6527_reset(struct comedi_device *dev)
340 struct ni6527_private *devpriv = dev->private;
341 void __iomem *mmio = devpriv->mmio_base;
343 /* disable deglitch filters on all channels */
344 ni6527_set_filter_enable(dev, 0);
346 writeb(NI6527_CLR_IRQS | NI6527_CLR_RESET_FILT,
347 mmio + NI6527_CLR_REG);
348 writeb(NI6527_CTRL_DISABLE_IRQS, mmio + NI6527_CTRL_REG);
351 static int ni6527_auto_attach(struct comedi_device *dev,
352 unsigned long context)
354 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
355 const struct ni6527_board *board = NULL;
356 struct ni6527_private *devpriv;
357 struct comedi_subdevice *s;
360 if (context < ARRAY_SIZE(ni6527_boards))
361 board = &ni6527_boards[context];
364 dev->board_ptr = board;
365 dev->board_name = board->name;
367 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
371 ret = comedi_pci_enable(dev);
375 devpriv->mmio_base = pci_ioremap_bar(pcidev, 1);
376 if (!devpriv->mmio_base)
379 /* make sure this is actually a 6527 device */
380 if (readb(devpriv->mmio_base + NI6527_ID_REG) != 0x27)
385 ret = request_irq(pcidev->irq, ni6527_interrupt, IRQF_SHARED,
386 dev->board_name, dev);
388 dev->irq = pcidev->irq;
390 ret = comedi_alloc_subdevices(dev, 3);
394 /* Digital Input subdevice */
395 s = &dev->subdevices[0];
396 s->type = COMEDI_SUBD_DI;
397 s->subdev_flags = SDF_READABLE;
400 s->range_table = &range_digital;
401 s->insn_config = ni6527_di_insn_config;
402 s->insn_bits = ni6527_di_insn_bits;
404 /* Digital Output subdevice */
405 s = &dev->subdevices[1];
406 s->type = COMEDI_SUBD_DO;
407 s->subdev_flags = SDF_WRITABLE;
410 s->range_table = &range_digital;
411 s->insn_bits = ni6527_do_insn_bits;
413 /* Edge detection interrupt subdevice */
414 s = &dev->subdevices[2];
416 dev->read_subdev = s;
417 s->type = COMEDI_SUBD_DI;
418 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
421 s->range_table = &range_digital;
422 s->insn_config = ni6527_intr_insn_config;
423 s->insn_bits = ni6527_intr_insn_bits;
424 s->do_cmdtest = ni6527_intr_cmdtest;
425 s->do_cmd = ni6527_intr_cmd;
426 s->cancel = ni6527_intr_cancel;
428 s->type = COMEDI_SUBD_UNUSED;
434 static void ni6527_detach(struct comedi_device *dev)
436 struct ni6527_private *devpriv = dev->private;
438 if (devpriv && devpriv->mmio_base)
441 free_irq(dev->irq, dev);
442 comedi_pci_disable(dev);
445 static struct comedi_driver ni6527_driver = {
446 .driver_name = "ni_6527",
447 .module = THIS_MODULE,
448 .auto_attach = ni6527_auto_attach,
449 .detach = ni6527_detach,
452 static int ni6527_pci_probe(struct pci_dev *dev,
453 const struct pci_device_id *id)
455 return comedi_pci_auto_config(dev, &ni6527_driver, id->driver_data);
458 static DEFINE_PCI_DEVICE_TABLE(ni6527_pci_table) = {
459 { PCI_VDEVICE(NI, 0x2b10), BOARD_PXI6527 },
460 { PCI_VDEVICE(NI, 0x2b20), BOARD_PCI6527 },
463 MODULE_DEVICE_TABLE(pci, ni6527_pci_table);
465 static struct pci_driver ni6527_pci_driver = {
467 .id_table = ni6527_pci_table,
468 .probe = ni6527_pci_probe,
469 .remove = comedi_pci_auto_unconfig,
471 module_comedi_pci_driver(ni6527_driver, ni6527_pci_driver);
473 MODULE_AUTHOR("Comedi http://www.comedi.org");
474 MODULE_DESCRIPTION("Comedi driver for National Instruments PCI-6527");
475 MODULE_LICENSE("GPL");