2 comedi/drivers/ni_6527.c
3 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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 Description: National Instruments 6527
28 Devices: [National Instruments] PCI-6527 (ni6527), PXI-6527
29 Updated: Sat, 25 Jan 2003 13:24:40 -0800
35 Manuals (available from ftp://ftp.natinst.com/support/manuals)
37 370106b.pdf 6527 Register Level Programmer Manual
44 #include <linux/interrupt.h>
45 #include "../comedidev.h"
47 #include "comedi_fc.h"
50 #define DRIVER_NAME "ni_6527"
52 #define NI6527_DIO_SIZE 4096
53 #define NI6527_MITE_SIZE 4096
55 #define Port_Register(x) (0x00+(x))
56 #define ID_Register 0x06
58 #define Clear_Register 0x07
60 #define ClrOverflow 0x04
61 #define ClrFilter 0x02
62 #define ClrInterval 0x01
64 #define Filter_Interval(x) (0x08+(x))
65 #define Filter_Enable(x) (0x0c+(x))
67 #define Change_Status 0x14
68 #define MasterInterruptStatus 0x04
70 #define EdgeStatus 0x01
72 #define Master_Interrupt_Control 0x15
73 #define FallingEdgeIntEnable 0x10
74 #define RisingEdgeIntEnable 0x08
75 #define MasterInterruptEnable 0x04
76 #define OverflowIntEnable 0x02
77 #define EdgeIntEnable 0x01
79 #define Rising_Edge_Detection_Enable(x) (0x018+(x))
80 #define Falling_Edge_Detection_Enable(x) (0x020+(x))
88 static const struct ni6527_board ni6527_boards[] = {
99 #define this_board ((const struct ni6527_board *)dev->board_ptr)
101 static DEFINE_PCI_DEVICE_TABLE(ni6527_pci_table) = {
102 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x2b10)},
103 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x2b20)},
107 MODULE_DEVICE_TABLE(pci, ni6527_pci_table);
109 struct ni6527_private {
110 struct mite_struct *mite;
111 unsigned int filter_interval;
112 unsigned int filter_enable;
115 static int ni6527_di_insn_config(struct comedi_device *dev,
116 struct comedi_subdevice *s,
117 struct comedi_insn *insn, unsigned int *data)
119 struct ni6527_private *devpriv = dev->private;
120 int chan = CR_CHAN(insn->chanspec);
121 unsigned int interval;
126 if (data[0] != INSN_CONFIG_FILTER)
130 interval = (data[1] + 100) / 200;
131 data[1] = interval * 200;
133 if (interval != devpriv->filter_interval) {
134 writeb(interval & 0xff,
135 devpriv->mite->daq_io_addr + Filter_Interval(0));
136 writeb((interval >> 8) & 0xff,
137 devpriv->mite->daq_io_addr + Filter_Interval(1));
138 writeb((interval >> 16) & 0x0f,
139 devpriv->mite->daq_io_addr + Filter_Interval(2));
142 devpriv->mite->daq_io_addr + Clear_Register);
144 devpriv->filter_interval = interval;
147 devpriv->filter_enable |= 1 << chan;
149 devpriv->filter_enable &= ~(1 << chan);
152 writeb(devpriv->filter_enable,
153 devpriv->mite->daq_io_addr + Filter_Enable(0));
154 writeb(devpriv->filter_enable >> 8,
155 devpriv->mite->daq_io_addr + Filter_Enable(1));
156 writeb(devpriv->filter_enable >> 16,
157 devpriv->mite->daq_io_addr + Filter_Enable(2));
162 static int ni6527_di_insn_bits(struct comedi_device *dev,
163 struct comedi_subdevice *s,
164 struct comedi_insn *insn, unsigned int *data)
166 struct ni6527_private *devpriv = dev->private;
168 data[1] = readb(devpriv->mite->daq_io_addr + Port_Register(0));
169 data[1] |= readb(devpriv->mite->daq_io_addr + Port_Register(1)) << 8;
170 data[1] |= readb(devpriv->mite->daq_io_addr + Port_Register(2)) << 16;
175 static int ni6527_do_insn_bits(struct comedi_device *dev,
176 struct comedi_subdevice *s,
177 struct comedi_insn *insn, unsigned int *data)
179 struct ni6527_private *devpriv = dev->private;
182 s->state &= ~data[0];
183 s->state |= (data[0] & data[1]);
185 /* The open relay state on the board cooresponds to 1,
186 * but in Comedi, it is represented by 0. */
187 if (data[0] & 0x0000ff) {
188 writeb((s->state ^ 0xff),
189 devpriv->mite->daq_io_addr + Port_Register(3));
191 if (data[0] & 0x00ff00) {
192 writeb((s->state >> 8) ^ 0xff,
193 devpriv->mite->daq_io_addr + Port_Register(4));
195 if (data[0] & 0xff0000) {
196 writeb((s->state >> 16) ^ 0xff,
197 devpriv->mite->daq_io_addr + Port_Register(5));
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->subdevices[2];
212 status = readb(devpriv->mite->daq_io_addr + Change_Status);
213 if ((status & MasterInterruptStatus) == 0)
215 if ((status & EdgeStatus) == 0)
218 writeb(ClrEdge | ClrOverflow,
219 devpriv->mite->daq_io_addr + Clear_Register);
221 comedi_buf_put(s->async, 0);
222 s->async->events |= COMEDI_CB_EOS;
223 comedi_event(dev, s);
227 static int ni6527_intr_cmdtest(struct comedi_device *dev,
228 struct comedi_subdevice *s,
229 struct comedi_cmd *cmd)
233 /* Step 1 : check if triggers are trivially valid */
235 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
236 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_OTHER);
237 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
238 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
239 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT);
244 /* Step 2a : make sure trigger sources are unique */
245 /* Step 2b : and mutually compatible */
250 /* step 3: make sure arguments are trivially compatible */
252 if (cmd->start_arg != 0) {
256 if (cmd->scan_begin_arg != 0) {
257 cmd->scan_begin_arg = 0;
260 if (cmd->convert_arg != 0) {
261 cmd->convert_arg = 0;
265 if (cmd->scan_end_arg != 1) {
266 cmd->scan_end_arg = 1;
269 if (cmd->stop_arg != 0) {
277 /* step 4: fix up any arguments */
285 static int ni6527_intr_cmd(struct comedi_device *dev,
286 struct comedi_subdevice *s)
288 struct ni6527_private *devpriv = dev->private;
289 /* struct comedi_cmd *cmd = &s->async->cmd; */
291 writeb(ClrEdge | ClrOverflow,
292 devpriv->mite->daq_io_addr + Clear_Register);
293 writeb(FallingEdgeIntEnable | RisingEdgeIntEnable |
294 MasterInterruptEnable | EdgeIntEnable,
295 devpriv->mite->daq_io_addr + Master_Interrupt_Control);
300 static int ni6527_intr_cancel(struct comedi_device *dev,
301 struct comedi_subdevice *s)
303 struct ni6527_private *devpriv = dev->private;
305 writeb(0x00, devpriv->mite->daq_io_addr + Master_Interrupt_Control);
310 static int ni6527_intr_insn_bits(struct comedi_device *dev,
311 struct comedi_subdevice *s,
312 struct comedi_insn *insn, unsigned int *data)
318 static int ni6527_intr_insn_config(struct comedi_device *dev,
319 struct comedi_subdevice *s,
320 struct comedi_insn *insn, unsigned int *data)
322 struct ni6527_private *devpriv = dev->private;
326 if (data[0] != INSN_CONFIG_CHANGE_NOTIFY)
330 devpriv->mite->daq_io_addr + Rising_Edge_Detection_Enable(0));
332 devpriv->mite->daq_io_addr + Rising_Edge_Detection_Enable(1));
333 writeb(data[1] >> 16,
334 devpriv->mite->daq_io_addr + Rising_Edge_Detection_Enable(2));
337 devpriv->mite->daq_io_addr + Falling_Edge_Detection_Enable(0));
339 devpriv->mite->daq_io_addr + Falling_Edge_Detection_Enable(1));
340 writeb(data[2] >> 16,
341 devpriv->mite->daq_io_addr + Falling_Edge_Detection_Enable(2));
346 static const struct ni6527_board *
347 ni6527_find_boardinfo(struct pci_dev *pcidev)
349 unsigned int dev_id = pcidev->device;
352 for (n = 0; n < ARRAY_SIZE(ni6527_boards); n++) {
353 const struct ni6527_board *board = &ni6527_boards[n];
354 if (board->dev_id == dev_id)
360 static int __devinit ni6527_attach_pci(struct comedi_device *dev,
361 struct pci_dev *pcidev)
363 struct ni6527_private *devpriv;
364 struct comedi_subdevice *s;
367 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
370 dev->private = devpriv;
372 dev->board_ptr = ni6527_find_boardinfo(pcidev);
376 devpriv->mite = mite_alloc(pcidev);
380 ret = mite_setup(devpriv->mite);
382 dev_err(dev->class_dev, "error setting up mite\n");
386 dev->board_name = this_board->name;
387 dev_info(dev->class_dev, "board: %s, ID=0x%02x\n", dev->board_name,
388 readb(devpriv->mite->daq_io_addr + ID_Register));
390 ret = comedi_alloc_subdevices(dev, 3);
394 s = &dev->subdevices[0];
395 s->type = COMEDI_SUBD_DI;
396 s->subdev_flags = SDF_READABLE;
398 s->range_table = &range_digital;
400 s->insn_config = ni6527_di_insn_config;
401 s->insn_bits = ni6527_di_insn_bits;
403 s = &dev->subdevices[1];
404 s->type = COMEDI_SUBD_DO;
405 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
407 s->range_table = &range_unknown; /* FIXME: actually conductance */
409 s->insn_bits = ni6527_do_insn_bits;
411 s = &dev->subdevices[2];
412 dev->read_subdev = s;
413 s->type = COMEDI_SUBD_DI;
414 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
416 s->range_table = &range_unknown;
418 s->do_cmdtest = ni6527_intr_cmdtest;
419 s->do_cmd = ni6527_intr_cmd;
420 s->cancel = ni6527_intr_cancel;
421 s->insn_bits = ni6527_intr_insn_bits;
422 s->insn_config = ni6527_intr_insn_config;
424 writeb(0x00, devpriv->mite->daq_io_addr + Filter_Enable(0));
425 writeb(0x00, devpriv->mite->daq_io_addr + Filter_Enable(1));
426 writeb(0x00, devpriv->mite->daq_io_addr + Filter_Enable(2));
428 writeb(ClrEdge | ClrOverflow | ClrFilter | ClrInterval,
429 devpriv->mite->daq_io_addr + Clear_Register);
430 writeb(0x00, devpriv->mite->daq_io_addr + Master_Interrupt_Control);
432 ret = request_irq(mite_irq(devpriv->mite), ni6527_interrupt,
433 IRQF_SHARED, DRIVER_NAME, dev);
435 dev_warn(dev->class_dev, "irq not available\n");
437 dev->irq = mite_irq(devpriv->mite);
442 static void ni6527_detach(struct comedi_device *dev)
444 struct ni6527_private *devpriv = dev->private;
446 if (devpriv && devpriv->mite && devpriv->mite->daq_io_addr)
448 devpriv->mite->daq_io_addr + Master_Interrupt_Control);
450 free_irq(dev->irq, dev);
451 if (devpriv && devpriv->mite) {
452 mite_unsetup(devpriv->mite);
453 mite_free(devpriv->mite);
457 static struct comedi_driver ni6527_driver = {
458 .driver_name = DRIVER_NAME,
459 .module = THIS_MODULE,
460 .attach_pci = ni6527_attach_pci,
461 .detach = ni6527_detach,
464 static int __devinit ni6527_pci_probe(struct pci_dev *dev,
465 const struct pci_device_id *ent)
467 return comedi_pci_auto_config(dev, &ni6527_driver);
470 static void __devexit ni6527_pci_remove(struct pci_dev *dev)
472 comedi_pci_auto_unconfig(dev);
475 static struct pci_driver ni6527_pci_driver = {
477 .id_table = ni6527_pci_table,
478 .probe = ni6527_pci_probe,
479 .remove = __devexit_p(ni6527_pci_remove)
481 module_comedi_pci_driver(ni6527_driver, ni6527_pci_driver);
483 MODULE_AUTHOR("Comedi http://www.comedi.org");
484 MODULE_DESCRIPTION("Comedi low-level driver");
485 MODULE_LICENSE("GPL");