2 comedi/drivers/ni_6514.c
3 driver for National Instruments PCI-6514
5 Copyright (C) 2006 Jon Grierson <jd@renko.co.uk>
6 Copyright (C) 2006 Frank Mori Hess <fmhess@users.sourceforge.net>
8 COMEDI - Linux Control and Measurement Device Interface
9 Copyright (C) 1999,2002,2003 David A. Schleef <ds@schleef.org>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 Description: National Instruments 65xx static dio boards
29 Author: Jon Grierson <jd@renko.co.uk>,
30 Frank Mori Hess <fmhess@users.sourceforge.net>
32 Devices: [National Instruments] PCI-6509 (ni_65xx), PXI-6509, PCI-6510,
33 PCI-6511, PXI-6511, PCI-6512, PXI-6512, PCI-6513, PXI-6513, PCI-6514,
34 PXI-6514, PCI-6515, PXI-6515, PCI-6516, PCI-6517, PCI-6518, PCI-6519,
35 PCI-6520, PCI-6521, PXI-6521, PCI-6528, PXI-6528
36 Updated: Wed Oct 18 08:59:11 EDT 2006
38 Based on the PCI-6527 driver by ds.
39 The interrupt subdevice (subdevice 3) is probably broken for all boards
40 except maybe the 6514.
45 Manuals (available from ftp://ftp.natinst.com/support/manuals)
47 370106b.pdf 6514 Register Level Programmer Manual
54 #include <linux/interrupt.h>
55 #include <linux/slab.h>
56 #include "../comedidev.h"
58 #include "comedi_fc.h"
61 #define NI6514_DIO_SIZE 4096
62 #define NI6514_MITE_SIZE 4096
64 #define NI_65XX_MAX_NUM_PORTS 12
65 static const unsigned ni_65xx_channels_per_port = 8;
66 static const unsigned ni_65xx_port_offset = 0x10;
68 static inline unsigned Port_Data(unsigned port)
70 return 0x40 + port * ni_65xx_port_offset;
73 static inline unsigned Port_Select(unsigned port)
75 return 0x41 + port * ni_65xx_port_offset;
78 static inline unsigned Rising_Edge_Detection_Enable(unsigned port)
80 return 0x42 + port * ni_65xx_port_offset;
83 static inline unsigned Falling_Edge_Detection_Enable(unsigned port)
85 return 0x43 + port * ni_65xx_port_offset;
88 static inline unsigned Filter_Enable(unsigned port)
90 return 0x44 + port * ni_65xx_port_offset;
93 #define ID_Register 0x00
95 #define Clear_Register 0x01
97 #define ClrOverflow 0x04
99 #define Filter_Interval 0x08
101 #define Change_Status 0x02
102 #define MasterInterruptStatus 0x04
103 #define Overflow 0x02
104 #define EdgeStatus 0x01
106 #define Master_Interrupt_Control 0x03
107 #define FallingEdgeIntEnable 0x10
108 #define RisingEdgeIntEnable 0x08
109 #define MasterInterruptEnable 0x04
110 #define OverflowIntEnable 0x02
111 #define EdgeIntEnable 0x01
113 struct ni_65xx_board {
116 unsigned num_dio_ports;
117 unsigned num_di_ports;
118 unsigned num_do_ports;
119 unsigned invert_outputs:1;
122 static const struct ni_65xx_board ni_65xx_boards[] = {
127 .invert_outputs = 0},
132 .invert_outputs = 0},
157 .invert_outputs = 1},
162 .invert_outputs = 1},
168 .invert_outputs = 1},
174 .invert_outputs = 1},
180 .invert_outputs = 1},
186 .invert_outputs = 1},
191 .invert_outputs = 1},
196 .invert_outputs = 1},
202 .invert_outputs = 1},
208 .invert_outputs = 1},
241 #define n_ni_65xx_boards ARRAY_SIZE(ni_65xx_boards)
242 static inline const struct ni_65xx_board *board(struct comedi_device *dev)
244 return dev->board_ptr;
247 static inline unsigned ni_65xx_port_by_channel(unsigned channel)
249 return channel / ni_65xx_channels_per_port;
252 static inline unsigned ni_65xx_total_num_ports(const struct ni_65xx_board
255 return board->num_dio_ports + board->num_di_ports + board->num_do_ports;
258 static DEFINE_PCI_DEVICE_TABLE(ni_65xx_pci_table) = {
259 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x1710)},
260 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x7085)},
261 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x7086)},
262 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x7087)},
263 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x7088)},
264 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x70a9)},
265 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x70c3)},
266 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x70c8)},
267 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x70c9)},
268 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x70cc)},
269 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x70CD)},
270 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x70d1)},
271 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x70d2)},
272 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x70d3)},
273 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x7124)},
274 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x7125)},
275 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x7126)},
276 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x7127)},
277 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x7128)},
278 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x718b)},
279 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x718c)},
280 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x71c5)},
284 MODULE_DEVICE_TABLE(pci, ni_65xx_pci_table);
286 struct ni_65xx_private {
287 struct mite_struct *mite;
288 unsigned int filter_interval;
289 unsigned short filter_enable[NI_65XX_MAX_NUM_PORTS];
290 unsigned short output_bits[NI_65XX_MAX_NUM_PORTS];
291 unsigned short dio_direction[NI_65XX_MAX_NUM_PORTS];
294 struct ni_65xx_subdevice_private {
298 static inline struct ni_65xx_subdevice_private *sprivate(struct comedi_subdevice
301 return subdev->private;
304 static struct ni_65xx_subdevice_private *ni_65xx_alloc_subdevice_private(void)
306 struct ni_65xx_subdevice_private *subdev_private =
307 kzalloc(sizeof(struct ni_65xx_subdevice_private), GFP_KERNEL);
308 if (subdev_private == NULL)
310 return subdev_private;
313 static int ni_65xx_config_filter(struct comedi_device *dev,
314 struct comedi_subdevice *s,
315 struct comedi_insn *insn, unsigned int *data)
317 struct ni_65xx_private *devpriv = dev->private;
318 const unsigned chan = CR_CHAN(insn->chanspec);
319 const unsigned port =
320 sprivate(s)->base_port + ni_65xx_port_by_channel(chan);
322 if (data[0] != INSN_CONFIG_FILTER)
325 static const unsigned filter_resolution_ns = 200;
326 static const unsigned max_filter_interval = 0xfffff;
329 (filter_resolution_ns / 2)) / filter_resolution_ns;
330 if (interval > max_filter_interval)
331 interval = max_filter_interval;
332 data[1] = interval * filter_resolution_ns;
334 if (interval != devpriv->filter_interval) {
336 devpriv->mite->daq_io_addr +
338 devpriv->filter_interval = interval;
341 devpriv->filter_enable[port] |=
342 1 << (chan % ni_65xx_channels_per_port);
344 devpriv->filter_enable[port] &=
345 ~(1 << (chan % ni_65xx_channels_per_port));
348 writeb(devpriv->filter_enable[port],
349 devpriv->mite->daq_io_addr + Filter_Enable(port));
354 static int ni_65xx_dio_insn_config(struct comedi_device *dev,
355 struct comedi_subdevice *s,
356 struct comedi_insn *insn, unsigned int *data)
358 struct ni_65xx_private *devpriv = dev->private;
363 port = sprivate(s)->base_port +
364 ni_65xx_port_by_channel(CR_CHAN(insn->chanspec));
366 case INSN_CONFIG_FILTER:
367 return ni_65xx_config_filter(dev, s, insn, data);
369 case INSN_CONFIG_DIO_OUTPUT:
370 if (s->type != COMEDI_SUBD_DIO)
372 devpriv->dio_direction[port] = COMEDI_OUTPUT;
373 writeb(0, devpriv->mite->daq_io_addr + Port_Select(port));
376 case INSN_CONFIG_DIO_INPUT:
377 if (s->type != COMEDI_SUBD_DIO)
379 devpriv->dio_direction[port] = COMEDI_INPUT;
380 writeb(1, devpriv->mite->daq_io_addr + Port_Select(port));
383 case INSN_CONFIG_DIO_QUERY:
384 if (s->type != COMEDI_SUBD_DIO)
386 data[1] = devpriv->dio_direction[port];
395 static int ni_65xx_dio_insn_bits(struct comedi_device *dev,
396 struct comedi_subdevice *s,
397 struct comedi_insn *insn, unsigned int *data)
399 struct ni_65xx_private *devpriv = dev->private;
400 unsigned base_bitfield_channel;
401 const unsigned max_ports_per_bitfield = 5;
402 unsigned read_bits = 0;
405 base_bitfield_channel = CR_CHAN(insn->chanspec);
406 for (j = 0; j < max_ports_per_bitfield; ++j) {
407 const unsigned port_offset =
408 ni_65xx_port_by_channel(base_bitfield_channel) + j;
409 const unsigned port =
410 sprivate(s)->base_port + port_offset;
411 unsigned base_port_channel;
412 unsigned port_mask, port_data, port_read_bits;
414 if (port >= ni_65xx_total_num_ports(board(dev)))
416 base_port_channel = port_offset * ni_65xx_channels_per_port;
419 bitshift = base_port_channel - base_bitfield_channel;
420 if (bitshift >= 32 || bitshift <= -32)
423 port_mask >>= bitshift;
424 port_data >>= bitshift;
426 port_mask <<= -bitshift;
427 port_data <<= -bitshift;
433 devpriv->output_bits[port] &= ~port_mask;
434 devpriv->output_bits[port] |=
435 port_data & port_mask;
436 bits = devpriv->output_bits[port];
437 if (board(dev)->invert_outputs)
440 devpriv->mite->daq_io_addr +
444 readb(devpriv->mite->daq_io_addr + Port_Data(port));
445 if (s->type == COMEDI_SUBD_DO && board(dev)->invert_outputs) {
446 /* Outputs inverted, so invert value read back from
447 * DO subdevice. (Does not apply to boards with DIO
449 port_read_bits ^= 0xFF;
452 port_read_bits <<= bitshift;
454 port_read_bits >>= -bitshift;
456 read_bits |= port_read_bits;
462 static irqreturn_t ni_65xx_interrupt(int irq, void *d)
464 struct comedi_device *dev = d;
465 struct ni_65xx_private *devpriv = dev->private;
466 struct comedi_subdevice *s = &dev->subdevices[2];
469 status = readb(devpriv->mite->daq_io_addr + Change_Status);
470 if ((status & MasterInterruptStatus) == 0)
472 if ((status & EdgeStatus) == 0)
475 writeb(ClrEdge | ClrOverflow,
476 devpriv->mite->daq_io_addr + Clear_Register);
478 comedi_buf_put(s->async, 0);
479 s->async->events |= COMEDI_CB_EOS;
480 comedi_event(dev, s);
484 static int ni_65xx_intr_cmdtest(struct comedi_device *dev,
485 struct comedi_subdevice *s,
486 struct comedi_cmd *cmd)
490 /* Step 1 : check if triggers are trivially valid */
492 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
493 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_OTHER);
494 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
495 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
496 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT);
501 /* Step 2a : make sure trigger sources are unique */
502 /* Step 2b : and mutually compatible */
507 /* step 3: make sure arguments are trivially compatible */
509 if (cmd->start_arg != 0) {
513 if (cmd->scan_begin_arg != 0) {
514 cmd->scan_begin_arg = 0;
517 if (cmd->convert_arg != 0) {
518 cmd->convert_arg = 0;
522 if (cmd->scan_end_arg != 1) {
523 cmd->scan_end_arg = 1;
526 if (cmd->stop_arg != 0) {
534 /* step 4: fix up any arguments */
542 static int ni_65xx_intr_cmd(struct comedi_device *dev,
543 struct comedi_subdevice *s)
545 struct ni_65xx_private *devpriv = dev->private;
546 /* struct comedi_cmd *cmd = &s->async->cmd; */
548 writeb(ClrEdge | ClrOverflow,
549 devpriv->mite->daq_io_addr + Clear_Register);
550 writeb(FallingEdgeIntEnable | RisingEdgeIntEnable |
551 MasterInterruptEnable | EdgeIntEnable,
552 devpriv->mite->daq_io_addr + Master_Interrupt_Control);
557 static int ni_65xx_intr_cancel(struct comedi_device *dev,
558 struct comedi_subdevice *s)
560 struct ni_65xx_private *devpriv = dev->private;
562 writeb(0x00, devpriv->mite->daq_io_addr + Master_Interrupt_Control);
567 static int ni_65xx_intr_insn_bits(struct comedi_device *dev,
568 struct comedi_subdevice *s,
569 struct comedi_insn *insn, unsigned int *data)
575 static int ni_65xx_intr_insn_config(struct comedi_device *dev,
576 struct comedi_subdevice *s,
577 struct comedi_insn *insn,
580 struct ni_65xx_private *devpriv = dev->private;
584 if (data[0] != INSN_CONFIG_CHANGE_NOTIFY)
588 devpriv->mite->daq_io_addr +
589 Rising_Edge_Detection_Enable(0));
591 devpriv->mite->daq_io_addr +
592 Rising_Edge_Detection_Enable(0x10));
593 writeb(data[1] >> 16,
594 devpriv->mite->daq_io_addr +
595 Rising_Edge_Detection_Enable(0x20));
596 writeb(data[1] >> 24,
597 devpriv->mite->daq_io_addr +
598 Rising_Edge_Detection_Enable(0x30));
601 devpriv->mite->daq_io_addr +
602 Falling_Edge_Detection_Enable(0));
604 devpriv->mite->daq_io_addr +
605 Falling_Edge_Detection_Enable(0x10));
606 writeb(data[2] >> 16,
607 devpriv->mite->daq_io_addr +
608 Falling_Edge_Detection_Enable(0x20));
609 writeb(data[2] >> 24,
610 devpriv->mite->daq_io_addr +
611 Falling_Edge_Detection_Enable(0x30));
616 static const struct ni_65xx_board *
617 ni_65xx_find_boardinfo(struct pci_dev *pcidev)
619 unsigned int dev_id = pcidev->device;
622 for (n = 0; n < ARRAY_SIZE(ni_65xx_boards); n++) {
623 const struct ni_65xx_board *board = &ni_65xx_boards[n];
624 if (board->dev_id == dev_id)
630 static int __devinit ni_65xx_attach_pci(struct comedi_device *dev,
631 struct pci_dev *pcidev)
633 struct ni_65xx_private *devpriv;
634 struct comedi_subdevice *s;
638 ret = alloc_private(dev, sizeof(*devpriv));
641 devpriv = dev->private;
643 dev->board_ptr = ni_65xx_find_boardinfo(pcidev);
647 devpriv->mite = mite_alloc(pcidev);
651 ret = mite_setup(devpriv->mite);
653 dev_warn(dev->class_dev, "error setting up mite\n");
657 dev->board_name = board(dev)->name;
658 dev->irq = mite_irq(devpriv->mite);
659 dev_info(dev->class_dev, "board: %s, ID=0x%02x", dev->board_name,
660 readb(devpriv->mite->daq_io_addr + ID_Register));
662 ret = comedi_alloc_subdevices(dev, 4);
666 s = &dev->subdevices[0];
667 if (board(dev)->num_di_ports) {
668 s->type = COMEDI_SUBD_DI;
669 s->subdev_flags = SDF_READABLE;
671 board(dev)->num_di_ports * ni_65xx_channels_per_port;
672 s->range_table = &range_digital;
674 s->insn_config = ni_65xx_dio_insn_config;
675 s->insn_bits = ni_65xx_dio_insn_bits;
676 s->private = ni_65xx_alloc_subdevice_private();
677 if (s->private == NULL)
679 sprivate(s)->base_port = 0;
681 s->type = COMEDI_SUBD_UNUSED;
684 s = &dev->subdevices[1];
685 if (board(dev)->num_do_ports) {
686 s->type = COMEDI_SUBD_DO;
687 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
689 board(dev)->num_do_ports * ni_65xx_channels_per_port;
690 s->range_table = &range_digital;
692 s->insn_bits = ni_65xx_dio_insn_bits;
693 s->private = ni_65xx_alloc_subdevice_private();
694 if (s->private == NULL)
696 sprivate(s)->base_port = board(dev)->num_di_ports;
698 s->type = COMEDI_SUBD_UNUSED;
701 s = &dev->subdevices[2];
702 if (board(dev)->num_dio_ports) {
703 s->type = COMEDI_SUBD_DIO;
704 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
706 board(dev)->num_dio_ports * ni_65xx_channels_per_port;
707 s->range_table = &range_digital;
709 s->insn_config = ni_65xx_dio_insn_config;
710 s->insn_bits = ni_65xx_dio_insn_bits;
711 s->private = ni_65xx_alloc_subdevice_private();
712 if (s->private == NULL)
714 sprivate(s)->base_port = 0;
715 for (i = 0; i < board(dev)->num_dio_ports; ++i) {
716 /* configure all ports for input */
718 devpriv->mite->daq_io_addr +
722 s->type = COMEDI_SUBD_UNUSED;
725 s = &dev->subdevices[3];
726 dev->read_subdev = s;
727 s->type = COMEDI_SUBD_DI;
728 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
730 s->range_table = &range_unknown;
732 s->do_cmdtest = ni_65xx_intr_cmdtest;
733 s->do_cmd = ni_65xx_intr_cmd;
734 s->cancel = ni_65xx_intr_cancel;
735 s->insn_bits = ni_65xx_intr_insn_bits;
736 s->insn_config = ni_65xx_intr_insn_config;
738 for (i = 0; i < ni_65xx_total_num_ports(board(dev)); ++i) {
740 devpriv->mite->daq_io_addr + Filter_Enable(i));
741 if (board(dev)->invert_outputs)
743 devpriv->mite->daq_io_addr + Port_Data(i));
746 devpriv->mite->daq_io_addr + Port_Data(i));
748 writeb(ClrEdge | ClrOverflow,
749 devpriv->mite->daq_io_addr + Clear_Register);
751 devpriv->mite->daq_io_addr + Master_Interrupt_Control);
753 /* Set filter interval to 0 (32bit reg) */
754 writeb(0x00000000, devpriv->mite->daq_io_addr + Filter_Interval);
756 ret = request_irq(dev->irq, ni_65xx_interrupt, IRQF_SHARED,
760 dev_warn(dev->class_dev, "irq not available\n");
766 static void ni_65xx_detach(struct comedi_device *dev)
768 struct ni_65xx_private *devpriv = dev->private;
770 if (devpriv && devpriv->mite && devpriv->mite->daq_io_addr) {
772 devpriv->mite->daq_io_addr +
773 Master_Interrupt_Control);
776 free_irq(dev->irq, dev);
778 struct comedi_subdevice *s;
781 for (i = 0; i < dev->n_subdevices; ++i) {
782 s = &dev->subdevices[i];
787 mite_unsetup(devpriv->mite);
788 mite_free(devpriv->mite);
793 static struct comedi_driver ni_65xx_driver = {
794 .driver_name = "ni_65xx",
795 .module = THIS_MODULE,
796 .attach_pci = ni_65xx_attach_pci,
797 .detach = ni_65xx_detach,
800 static int __devinit ni_65xx_pci_probe(struct pci_dev *dev,
801 const struct pci_device_id *ent)
803 return comedi_pci_auto_config(dev, &ni_65xx_driver);
806 static void __devexit ni_65xx_pci_remove(struct pci_dev *dev)
808 comedi_pci_auto_unconfig(dev);
811 static struct pci_driver ni_65xx_pci_driver = {
813 .id_table = ni_65xx_pci_table,
814 .probe = ni_65xx_pci_probe,
815 .remove = __devexit_p(ni_65xx_pci_remove)
817 module_comedi_pci_driver(ni_65xx_driver, ni_65xx_pci_driver);
819 MODULE_AUTHOR("Comedi http://www.comedi.org");
820 MODULE_DESCRIPTION("Comedi low-level driver");
821 MODULE_LICENSE("GPL");