2 comedi/drivers/ni_670x.c
3 Hardware driver for NI 670x devices
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2001 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.
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.
30 Bart Joris <bjoris@advalvas.be> Last updated on 20/08/2001
34 322110a.pdf PCI/PXI-6704 User Manual
35 322110b.pdf PCI/PXI-6703/6704 User Manual
39 #include <linux/module.h>
40 #include <linux/pci.h>
41 #include <linux/interrupt.h>
43 #include "../comedidev.h"
45 #define AO_VALUE_OFFSET 0x00
46 #define AO_CHAN_OFFSET 0x0c
47 #define AO_STATUS_OFFSET 0x10
48 #define AO_CONTROL_OFFSET 0x10
49 #define DIO_PORT0_DIR_OFFSET 0x20
50 #define DIO_PORT0_DATA_OFFSET 0x24
51 #define DIO_PORT1_DIR_OFFSET 0x28
52 #define DIO_PORT1_DATA_OFFSET 0x2c
53 #define MISC_STATUS_OFFSET 0x14
54 #define MISC_CONTROL_OFFSET 0x14
56 enum ni_670x_boardid {
62 struct ni_670x_board {
64 unsigned short ao_chans;
67 static const struct ni_670x_board ni_670x_boards[] = {
82 struct ni_670x_private {
86 unsigned int ao_readback[32];
89 static int ni_670x_ao_winsn(struct comedi_device *dev,
90 struct comedi_subdevice *s,
91 struct comedi_insn *insn, unsigned int *data)
93 struct ni_670x_private *devpriv = dev->private;
95 int chan = CR_CHAN(insn->chanspec);
97 /* Channel number mapping :
99 NI 6703/ NI 6704 | NI 6704 Only
100 ----------------------------------------------------
101 vch(0) : 0 | ich(16) : 1
102 vch(1) : 2 | ich(17) : 3
106 vch(15) : 30 | ich(31) : 31 */
108 for (i = 0; i < insn->n; i++) {
109 /* First write in channel register which channel to use */
110 writel(((chan & 15) << 1) | ((chan & 16) >> 4),
111 devpriv->mmio + AO_CHAN_OFFSET);
112 /* write channel value */
113 writel(data[i], devpriv->mmio + AO_VALUE_OFFSET);
114 devpriv->ao_readback[chan] = data[i];
120 static int ni_670x_ao_rinsn(struct comedi_device *dev,
121 struct comedi_subdevice *s,
122 struct comedi_insn *insn, unsigned int *data)
124 struct ni_670x_private *devpriv = dev->private;
126 int chan = CR_CHAN(insn->chanspec);
128 for (i = 0; i < insn->n; i++)
129 data[i] = devpriv->ao_readback[chan];
134 static int ni_670x_dio_insn_bits(struct comedi_device *dev,
135 struct comedi_subdevice *s,
136 struct comedi_insn *insn,
139 struct ni_670x_private *devpriv = dev->private;
140 void __iomem *io_addr = devpriv->mmio + DIO_PORT0_DATA_OFFSET;
142 if (comedi_dio_update_state(s, data))
143 writel(s->state, io_addr);
145 data[1] = readl(io_addr);
150 static int ni_670x_dio_insn_config(struct comedi_device *dev,
151 struct comedi_subdevice *s,
152 struct comedi_insn *insn,
155 struct ni_670x_private *devpriv = dev->private;
158 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
162 writel(s->io_bits, devpriv->mmio + DIO_PORT0_DIR_OFFSET);
167 /* ripped from mite.h and mite_setup2() to avoid mite dependancy */
168 #define MITE_IODWBSR 0xc0 /* IO Device Window Base Size Register */
169 #define WENAB (1 << 7) /* window enable */
171 static int ni_670x_mite_init(struct pci_dev *pcidev)
173 void __iomem *mite_base;
176 /* ioremap the MITE registers (BAR 0) temporarily */
177 mite_base = pci_ioremap_bar(pcidev, 0);
181 /* set data window to main registers (BAR 1) */
182 main_phys_addr = pci_resource_start(pcidev, 1);
183 writel(main_phys_addr | WENAB, mite_base + MITE_IODWBSR);
185 /* finished with MITE registers */
190 static int ni_670x_auto_attach(struct comedi_device *dev,
191 unsigned long context)
193 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
194 const struct ni_670x_board *thisboard = NULL;
195 struct ni_670x_private *devpriv;
196 struct comedi_subdevice *s;
200 if (context < ARRAY_SIZE(ni_670x_boards))
201 thisboard = &ni_670x_boards[context];
204 dev->board_ptr = thisboard;
205 dev->board_name = thisboard->name;
207 ret = comedi_pci_enable(dev);
211 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
215 ret = ni_670x_mite_init(pcidev);
219 devpriv->mmio = pci_ioremap_bar(pcidev, 1);
223 ret = comedi_alloc_subdevices(dev, 2);
227 s = &dev->subdevices[0];
228 /* analog output subdevice */
229 s->type = COMEDI_SUBD_AO;
230 s->subdev_flags = SDF_WRITABLE;
231 s->n_chan = thisboard->ao_chans;
233 if (s->n_chan == 32) {
234 const struct comedi_lrange **range_table_list;
236 range_table_list = kmalloc(sizeof(struct comedi_lrange *) * 32,
238 if (!range_table_list)
240 s->range_table_list = range_table_list;
241 for (i = 0; i < 16; i++) {
242 range_table_list[i] = &range_bipolar10;
243 range_table_list[16 + i] = &range_0_20mA;
246 s->range_table = &range_bipolar10;
248 s->insn_write = &ni_670x_ao_winsn;
249 s->insn_read = &ni_670x_ao_rinsn;
251 s = &dev->subdevices[1];
252 /* digital i/o subdevice */
253 s->type = COMEDI_SUBD_DIO;
254 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
257 s->range_table = &range_digital;
258 s->insn_bits = ni_670x_dio_insn_bits;
259 s->insn_config = ni_670x_dio_insn_config;
261 /* Config of misc registers */
262 writel(0x10, devpriv->mmio + MISC_CONTROL_OFFSET);
263 /* Config of ao registers */
264 writel(0x00, devpriv->mmio + AO_CONTROL_OFFSET);
269 static void ni_670x_detach(struct comedi_device *dev)
271 struct ni_670x_private *devpriv = dev->private;
272 struct comedi_subdevice *s;
274 if (dev->n_subdevices) {
275 s = &dev->subdevices[0];
277 kfree(s->range_table_list);
279 if (devpriv && devpriv->mmio)
280 iounmap(devpriv->mmio);
281 comedi_pci_disable(dev);
284 static struct comedi_driver ni_670x_driver = {
285 .driver_name = "ni_670x",
286 .module = THIS_MODULE,
287 .auto_attach = ni_670x_auto_attach,
288 .detach = ni_670x_detach,
291 static int ni_670x_pci_probe(struct pci_dev *dev,
292 const struct pci_device_id *id)
294 return comedi_pci_auto_config(dev, &ni_670x_driver, id->driver_data);
297 static const struct pci_device_id ni_670x_pci_table[] = {
298 { PCI_VDEVICE(NI, 0x1290), BOARD_PCI6704 },
299 { PCI_VDEVICE(NI, 0x1920), BOARD_PXI6704 },
300 { PCI_VDEVICE(NI, 0x2c90), BOARD_PCI6703 },
303 MODULE_DEVICE_TABLE(pci, ni_670x_pci_table);
305 static struct pci_driver ni_670x_pci_driver = {
307 .id_table = ni_670x_pci_table,
308 .probe = ni_670x_pci_probe,
309 .remove = comedi_pci_auto_unconfig,
311 module_comedi_pci_driver(ni_670x_driver, ni_670x_pci_driver);
313 MODULE_AUTHOR("Comedi http://www.comedi.org");
314 MODULE_DESCRIPTION("Comedi low-level driver");
315 MODULE_LICENSE("GPL");