2 * Comedi driver for NI 670x devices
4 * COMEDI - Linux Control and Measurement Device Interface
5 * Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
20 * Description: National Instruments 670x
21 * Author: Bart Joris <bjoris@advalvas.be>
22 * Updated: Wed, 11 Dec 2002 18:25:35 -0800
23 * Devices: [National Instruments] PCI-6703 (ni_670x), PCI-6704
26 * Commands are not supported.
29 * 322110a.pdf PCI/PXI-6704 User Manual
30 * 322110b.pdf PCI/PXI-6703/6704 User Manual
33 #include <linux/module.h>
34 #include <linux/interrupt.h>
35 #include <linux/slab.h>
37 #include "../comedi_pci.h"
39 #define AO_VALUE_OFFSET 0x00
40 #define AO_CHAN_OFFSET 0x0c
41 #define AO_STATUS_OFFSET 0x10
42 #define AO_CONTROL_OFFSET 0x10
43 #define DIO_PORT0_DIR_OFFSET 0x20
44 #define DIO_PORT0_DATA_OFFSET 0x24
45 #define DIO_PORT1_DIR_OFFSET 0x28
46 #define DIO_PORT1_DATA_OFFSET 0x2c
47 #define MISC_STATUS_OFFSET 0x14
48 #define MISC_CONTROL_OFFSET 0x14
50 enum ni_670x_boardid {
56 struct ni_670x_board {
58 unsigned short ao_chans;
61 static const struct ni_670x_board ni_670x_boards[] = {
76 struct ni_670x_private {
81 static int ni_670x_ao_insn_write(struct comedi_device *dev,
82 struct comedi_subdevice *s,
83 struct comedi_insn *insn,
86 unsigned int chan = CR_CHAN(insn->chanspec);
87 unsigned int val = s->readback[chan];
91 * Channel number mapping:
93 * NI 6703/ NI 6704 | NI 6704 Only
94 * -------------------------------
95 * vch(0) : 0 | ich(16) : 1
96 * vch(1) : 2 | ich(17) : 3
98 * vch(15) : 30 | ich(31) : 31
100 for (i = 0; i < insn->n; i++) {
102 /* First write in channel register which channel to use */
103 writel(((chan & 15) << 1) | ((chan & 16) >> 4),
104 dev->mmio + AO_CHAN_OFFSET);
105 /* write channel value */
106 writel(val, dev->mmio + AO_VALUE_OFFSET);
108 s->readback[chan] = val;
113 static int ni_670x_dio_insn_bits(struct comedi_device *dev,
114 struct comedi_subdevice *s,
115 struct comedi_insn *insn,
118 if (comedi_dio_update_state(s, data))
119 writel(s->state, dev->mmio + DIO_PORT0_DATA_OFFSET);
121 data[1] = readl(dev->mmio + DIO_PORT0_DATA_OFFSET);
126 static int ni_670x_dio_insn_config(struct comedi_device *dev,
127 struct comedi_subdevice *s,
128 struct comedi_insn *insn,
133 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
137 writel(s->io_bits, dev->mmio + DIO_PORT0_DIR_OFFSET);
142 /* ripped from mite.h and mite_setup2() to avoid mite dependency */
143 #define MITE_IODWBSR 0xc0 /* IO Device Window Base Size Register */
144 #define WENAB (1 << 7) /* window enable */
146 static int ni_670x_mite_init(struct pci_dev *pcidev)
148 void __iomem *mite_base;
151 /* ioremap the MITE registers (BAR 0) temporarily */
152 mite_base = pci_ioremap_bar(pcidev, 0);
156 /* set data window to main registers (BAR 1) */
157 main_phys_addr = pci_resource_start(pcidev, 1);
158 writel(main_phys_addr | WENAB, mite_base + MITE_IODWBSR);
160 /* finished with MITE registers */
165 static int ni_670x_auto_attach(struct comedi_device *dev,
166 unsigned long context)
168 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
169 const struct ni_670x_board *board = NULL;
170 struct ni_670x_private *devpriv;
171 struct comedi_subdevice *s;
175 if (context < ARRAY_SIZE(ni_670x_boards))
176 board = &ni_670x_boards[context];
179 dev->board_ptr = board;
180 dev->board_name = board->name;
182 ret = comedi_pci_enable(dev);
186 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
190 ret = ni_670x_mite_init(pcidev);
194 dev->mmio = pci_ioremap_bar(pcidev, 1);
198 ret = comedi_alloc_subdevices(dev, 2);
202 s = &dev->subdevices[0];
203 /* analog output subdevice */
204 s->type = COMEDI_SUBD_AO;
205 s->subdev_flags = SDF_WRITABLE;
206 s->n_chan = board->ao_chans;
208 if (s->n_chan == 32) {
209 const struct comedi_lrange **range_table_list;
211 range_table_list = kmalloc_array(32,
212 sizeof(struct comedi_lrange *),
214 if (!range_table_list)
216 s->range_table_list = range_table_list;
217 for (i = 0; i < 16; i++) {
218 range_table_list[i] = &range_bipolar10;
219 range_table_list[16 + i] = &range_0_20mA;
222 s->range_table = &range_bipolar10;
224 s->insn_write = ni_670x_ao_insn_write;
226 ret = comedi_alloc_subdev_readback(s);
230 s = &dev->subdevices[1];
231 /* digital i/o subdevice */
232 s->type = COMEDI_SUBD_DIO;
233 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
236 s->range_table = &range_digital;
237 s->insn_bits = ni_670x_dio_insn_bits;
238 s->insn_config = ni_670x_dio_insn_config;
240 /* Config of misc registers */
241 writel(0x10, dev->mmio + MISC_CONTROL_OFFSET);
242 /* Config of ao registers */
243 writel(0x00, dev->mmio + AO_CONTROL_OFFSET);
248 static void ni_670x_detach(struct comedi_device *dev)
250 struct comedi_subdevice *s;
252 comedi_pci_detach(dev);
253 if (dev->n_subdevices) {
254 s = &dev->subdevices[0];
256 kfree(s->range_table_list);
260 static struct comedi_driver ni_670x_driver = {
261 .driver_name = "ni_670x",
262 .module = THIS_MODULE,
263 .auto_attach = ni_670x_auto_attach,
264 .detach = ni_670x_detach,
267 static int ni_670x_pci_probe(struct pci_dev *dev,
268 const struct pci_device_id *id)
270 return comedi_pci_auto_config(dev, &ni_670x_driver, id->driver_data);
273 static const struct pci_device_id ni_670x_pci_table[] = {
274 { PCI_VDEVICE(NI, 0x1290), BOARD_PCI6704 },
275 { PCI_VDEVICE(NI, 0x1920), BOARD_PXI6704 },
276 { PCI_VDEVICE(NI, 0x2c90), BOARD_PCI6703 },
279 MODULE_DEVICE_TABLE(pci, ni_670x_pci_table);
281 static struct pci_driver ni_670x_pci_driver = {
283 .id_table = ni_670x_pci_table,
284 .probe = ni_670x_pci_probe,
285 .remove = comedi_pci_auto_unconfig,
287 module_comedi_pci_driver(ni_670x_driver, ni_670x_pci_driver);
289 MODULE_AUTHOR("Comedi http://www.comedi.org");
290 MODULE_DESCRIPTION("Comedi low-level driver");
291 MODULE_LICENSE("GPL");