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>
27 #include <linux/comedi/comedi_pci.h>
29 #define AO_VALUE_OFFSET 0x00
30 #define AO_CHAN_OFFSET 0x0c
31 #define AO_STATUS_OFFSET 0x10
32 #define AO_CONTROL_OFFSET 0x10
33 #define DIO_PORT0_DIR_OFFSET 0x20
34 #define DIO_PORT0_DATA_OFFSET 0x24
35 #define DIO_PORT1_DIR_OFFSET 0x28
36 #define DIO_PORT1_DATA_OFFSET 0x2c
37 #define MISC_STATUS_OFFSET 0x14
38 #define MISC_CONTROL_OFFSET 0x14
40 enum ni_670x_boardid {
46 struct ni_670x_board {
48 unsigned short ao_chans;
51 static const struct ni_670x_board ni_670x_boards[] = {
66 struct ni_670x_private {
71 static int ni_670x_ao_insn_write(struct comedi_device *dev,
72 struct comedi_subdevice *s,
73 struct comedi_insn *insn,
76 unsigned int chan = CR_CHAN(insn->chanspec);
77 unsigned int val = s->readback[chan];
81 * Channel number mapping:
83 * NI 6703/ NI 6704 | NI 6704 Only
84 * -------------------------------
85 * vch(0) : 0 | ich(16) : 1
86 * vch(1) : 2 | ich(17) : 3
88 * vch(15) : 30 | ich(31) : 31
90 for (i = 0; i < insn->n; i++) {
92 /* First write in channel register which channel to use */
93 writel(((chan & 15) << 1) | ((chan & 16) >> 4),
94 dev->mmio + AO_CHAN_OFFSET);
95 /* write channel value */
96 writel(val, dev->mmio + AO_VALUE_OFFSET);
98 s->readback[chan] = val;
103 static int ni_670x_dio_insn_bits(struct comedi_device *dev,
104 struct comedi_subdevice *s,
105 struct comedi_insn *insn,
108 if (comedi_dio_update_state(s, data))
109 writel(s->state, dev->mmio + DIO_PORT0_DATA_OFFSET);
111 data[1] = readl(dev->mmio + DIO_PORT0_DATA_OFFSET);
116 static int ni_670x_dio_insn_config(struct comedi_device *dev,
117 struct comedi_subdevice *s,
118 struct comedi_insn *insn,
123 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
127 writel(s->io_bits, dev->mmio + DIO_PORT0_DIR_OFFSET);
132 /* ripped from mite.h and mite_setup2() to avoid mite dependency */
133 #define MITE_IODWBSR 0xc0 /* IO Device Window Base Size Register */
134 #define WENAB BIT(7) /* window enable */
136 static int ni_670x_mite_init(struct pci_dev *pcidev)
138 void __iomem *mite_base;
141 /* ioremap the MITE registers (BAR 0) temporarily */
142 mite_base = pci_ioremap_bar(pcidev, 0);
146 /* set data window to main registers (BAR 1) */
147 main_phys_addr = pci_resource_start(pcidev, 1);
148 writel(main_phys_addr | WENAB, mite_base + MITE_IODWBSR);
150 /* finished with MITE registers */
155 static int ni_670x_auto_attach(struct comedi_device *dev,
156 unsigned long context)
158 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
159 const struct ni_670x_board *board = NULL;
160 struct ni_670x_private *devpriv;
161 struct comedi_subdevice *s;
165 if (context < ARRAY_SIZE(ni_670x_boards))
166 board = &ni_670x_boards[context];
169 dev->board_ptr = board;
170 dev->board_name = board->name;
172 ret = comedi_pci_enable(dev);
176 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
180 ret = ni_670x_mite_init(pcidev);
184 dev->mmio = pci_ioremap_bar(pcidev, 1);
188 ret = comedi_alloc_subdevices(dev, 2);
192 s = &dev->subdevices[0];
193 /* analog output subdevice */
194 s->type = COMEDI_SUBD_AO;
195 s->subdev_flags = SDF_WRITABLE;
196 s->n_chan = board->ao_chans;
198 if (s->n_chan == 32) {
199 const struct comedi_lrange **range_table_list;
201 range_table_list = kmalloc_array(32,
202 sizeof(struct comedi_lrange *),
204 if (!range_table_list)
206 s->range_table_list = range_table_list;
207 for (i = 0; i < 16; i++) {
208 range_table_list[i] = &range_bipolar10;
209 range_table_list[16 + i] = &range_0_20mA;
212 s->range_table = &range_bipolar10;
214 s->insn_write = ni_670x_ao_insn_write;
216 ret = comedi_alloc_subdev_readback(s);
220 s = &dev->subdevices[1];
221 /* digital i/o subdevice */
222 s->type = COMEDI_SUBD_DIO;
223 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
226 s->range_table = &range_digital;
227 s->insn_bits = ni_670x_dio_insn_bits;
228 s->insn_config = ni_670x_dio_insn_config;
230 /* Config of misc registers */
231 writel(0x10, dev->mmio + MISC_CONTROL_OFFSET);
232 /* Config of ao registers */
233 writel(0x00, dev->mmio + AO_CONTROL_OFFSET);
238 static void ni_670x_detach(struct comedi_device *dev)
240 struct comedi_subdevice *s;
242 comedi_pci_detach(dev);
243 if (dev->n_subdevices) {
244 s = &dev->subdevices[0];
246 kfree(s->range_table_list);
250 static struct comedi_driver ni_670x_driver = {
251 .driver_name = "ni_670x",
252 .module = THIS_MODULE,
253 .auto_attach = ni_670x_auto_attach,
254 .detach = ni_670x_detach,
257 static int ni_670x_pci_probe(struct pci_dev *dev,
258 const struct pci_device_id *id)
260 return comedi_pci_auto_config(dev, &ni_670x_driver, id->driver_data);
263 static const struct pci_device_id ni_670x_pci_table[] = {
264 { PCI_VDEVICE(NI, 0x1290), BOARD_PCI6704 },
265 { PCI_VDEVICE(NI, 0x1920), BOARD_PXI6704 },
266 { PCI_VDEVICE(NI, 0x2c90), BOARD_PCI6703 },
269 MODULE_DEVICE_TABLE(pci, ni_670x_pci_table);
271 static struct pci_driver ni_670x_pci_driver = {
273 .id_table = ni_670x_pci_table,
274 .probe = ni_670x_pci_probe,
275 .remove = comedi_pci_auto_unconfig,
277 module_comedi_pci_driver(ni_670x_driver, ni_670x_pci_driver);
279 MODULE_AUTHOR("Comedi https://www.comedi.org");
280 MODULE_DESCRIPTION("Comedi low-level driver");
281 MODULE_LICENSE("GPL");