1 // SPDX-License-Identifier: GPL-2.0+
3 * Comedi driver for NI 670x devices
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
11 * Description: National Instruments 670x
12 * Author: Bart Joris <bjoris@advalvas.be>
13 * Updated: Wed, 11 Dec 2002 18:25:35 -0800
14 * Devices: [National Instruments] PCI-6703 (ni_670x), PCI-6704
17 * Commands are not supported.
20 * 322110a.pdf PCI/PXI-6704 User Manual
21 * 322110b.pdf PCI/PXI-6703/6704 User Manual
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/slab.h>
28 #include "../comedi_pci.h"
30 #define AO_VALUE_OFFSET 0x00
31 #define AO_CHAN_OFFSET 0x0c
32 #define AO_STATUS_OFFSET 0x10
33 #define AO_CONTROL_OFFSET 0x10
34 #define DIO_PORT0_DIR_OFFSET 0x20
35 #define DIO_PORT0_DATA_OFFSET 0x24
36 #define DIO_PORT1_DIR_OFFSET 0x28
37 #define DIO_PORT1_DATA_OFFSET 0x2c
38 #define MISC_STATUS_OFFSET 0x14
39 #define MISC_CONTROL_OFFSET 0x14
41 enum ni_670x_boardid {
47 struct ni_670x_board {
49 unsigned short ao_chans;
52 static const struct ni_670x_board ni_670x_boards[] = {
67 struct ni_670x_private {
72 static int ni_670x_ao_insn_write(struct comedi_device *dev,
73 struct comedi_subdevice *s,
74 struct comedi_insn *insn,
77 unsigned int chan = CR_CHAN(insn->chanspec);
78 unsigned int val = s->readback[chan];
82 * Channel number mapping:
84 * NI 6703/ NI 6704 | NI 6704 Only
85 * -------------------------------
86 * vch(0) : 0 | ich(16) : 1
87 * vch(1) : 2 | ich(17) : 3
89 * vch(15) : 30 | ich(31) : 31
91 for (i = 0; i < insn->n; i++) {
93 /* First write in channel register which channel to use */
94 writel(((chan & 15) << 1) | ((chan & 16) >> 4),
95 dev->mmio + AO_CHAN_OFFSET);
96 /* write channel value */
97 writel(val, dev->mmio + AO_VALUE_OFFSET);
99 s->readback[chan] = val;
104 static int ni_670x_dio_insn_bits(struct comedi_device *dev,
105 struct comedi_subdevice *s,
106 struct comedi_insn *insn,
109 if (comedi_dio_update_state(s, data))
110 writel(s->state, dev->mmio + DIO_PORT0_DATA_OFFSET);
112 data[1] = readl(dev->mmio + DIO_PORT0_DATA_OFFSET);
117 static int ni_670x_dio_insn_config(struct comedi_device *dev,
118 struct comedi_subdevice *s,
119 struct comedi_insn *insn,
124 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
128 writel(s->io_bits, dev->mmio + DIO_PORT0_DIR_OFFSET);
133 /* ripped from mite.h and mite_setup2() to avoid mite dependency */
134 #define MITE_IODWBSR 0xc0 /* IO Device Window Base Size Register */
135 #define WENAB BIT(7) /* window enable */
137 static int ni_670x_mite_init(struct pci_dev *pcidev)
139 void __iomem *mite_base;
142 /* ioremap the MITE registers (BAR 0) temporarily */
143 mite_base = pci_ioremap_bar(pcidev, 0);
147 /* set data window to main registers (BAR 1) */
148 main_phys_addr = pci_resource_start(pcidev, 1);
149 writel(main_phys_addr | WENAB, mite_base + MITE_IODWBSR);
151 /* finished with MITE registers */
156 static int ni_670x_auto_attach(struct comedi_device *dev,
157 unsigned long context)
159 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
160 const struct ni_670x_board *board = NULL;
161 struct ni_670x_private *devpriv;
162 struct comedi_subdevice *s;
166 if (context < ARRAY_SIZE(ni_670x_boards))
167 board = &ni_670x_boards[context];
170 dev->board_ptr = board;
171 dev->board_name = board->name;
173 ret = comedi_pci_enable(dev);
177 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
181 ret = ni_670x_mite_init(pcidev);
185 dev->mmio = pci_ioremap_bar(pcidev, 1);
189 ret = comedi_alloc_subdevices(dev, 2);
193 s = &dev->subdevices[0];
194 /* analog output subdevice */
195 s->type = COMEDI_SUBD_AO;
196 s->subdev_flags = SDF_WRITABLE;
197 s->n_chan = board->ao_chans;
199 if (s->n_chan == 32) {
200 const struct comedi_lrange **range_table_list;
202 range_table_list = kmalloc_array(32,
203 sizeof(struct comedi_lrange *),
205 if (!range_table_list)
207 s->range_table_list = range_table_list;
208 for (i = 0; i < 16; i++) {
209 range_table_list[i] = &range_bipolar10;
210 range_table_list[16 + i] = &range_0_20mA;
213 s->range_table = &range_bipolar10;
215 s->insn_write = ni_670x_ao_insn_write;
217 ret = comedi_alloc_subdev_readback(s);
221 s = &dev->subdevices[1];
222 /* digital i/o subdevice */
223 s->type = COMEDI_SUBD_DIO;
224 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
227 s->range_table = &range_digital;
228 s->insn_bits = ni_670x_dio_insn_bits;
229 s->insn_config = ni_670x_dio_insn_config;
231 /* Config of misc registers */
232 writel(0x10, dev->mmio + MISC_CONTROL_OFFSET);
233 /* Config of ao registers */
234 writel(0x00, dev->mmio + AO_CONTROL_OFFSET);
239 static void ni_670x_detach(struct comedi_device *dev)
241 struct comedi_subdevice *s;
243 comedi_pci_detach(dev);
244 if (dev->n_subdevices) {
245 s = &dev->subdevices[0];
247 kfree(s->range_table_list);
251 static struct comedi_driver ni_670x_driver = {
252 .driver_name = "ni_670x",
253 .module = THIS_MODULE,
254 .auto_attach = ni_670x_auto_attach,
255 .detach = ni_670x_detach,
258 static int ni_670x_pci_probe(struct pci_dev *dev,
259 const struct pci_device_id *id)
261 return comedi_pci_auto_config(dev, &ni_670x_driver, id->driver_data);
264 static const struct pci_device_id ni_670x_pci_table[] = {
265 { PCI_VDEVICE(NI, 0x1290), BOARD_PCI6704 },
266 { PCI_VDEVICE(NI, 0x1920), BOARD_PXI6704 },
267 { PCI_VDEVICE(NI, 0x2c90), BOARD_PCI6703 },
270 MODULE_DEVICE_TABLE(pci, ni_670x_pci_table);
272 static struct pci_driver ni_670x_pci_driver = {
274 .id_table = ni_670x_pci_table,
275 .probe = ni_670x_pci_probe,
276 .remove = comedi_pci_auto_unconfig,
278 module_comedi_pci_driver(ni_670x_driver, ni_670x_pci_driver);
280 MODULE_AUTHOR("Comedi https://www.comedi.org");
281 MODULE_DESCRIPTION("Comedi low-level driver");
282 MODULE_LICENSE("GPL");