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"
47 #define AO_VALUE_OFFSET 0x00
48 #define AO_CHAN_OFFSET 0x0c
49 #define AO_STATUS_OFFSET 0x10
50 #define AO_CONTROL_OFFSET 0x10
51 #define DIO_PORT0_DIR_OFFSET 0x20
52 #define DIO_PORT0_DATA_OFFSET 0x24
53 #define DIO_PORT1_DIR_OFFSET 0x28
54 #define DIO_PORT1_DATA_OFFSET 0x2c
55 #define MISC_STATUS_OFFSET 0x14
56 #define MISC_CONTROL_OFFSET 0x14
58 enum ni_670x_boardid {
64 struct ni_670x_board {
66 unsigned short ao_chans;
69 static const struct ni_670x_board ni_670x_boards[] = {
84 struct ni_670x_private {
86 struct mite_struct *mite;
89 unsigned int ao_readback[32];
92 static int ni_670x_ao_winsn(struct comedi_device *dev,
93 struct comedi_subdevice *s,
94 struct comedi_insn *insn, unsigned int *data)
96 struct ni_670x_private *devpriv = dev->private;
98 int chan = CR_CHAN(insn->chanspec);
100 /* Channel number mapping :
102 NI 6703/ NI 6704 | NI 6704 Only
103 ----------------------------------------------------
104 vch(0) : 0 | ich(16) : 1
105 vch(1) : 2 | ich(17) : 3
109 vch(15) : 30 | ich(31) : 31 */
111 for (i = 0; i < insn->n; i++) {
112 /* First write in channel register which channel to use */
113 writel(((chan & 15) << 1) | ((chan & 16) >> 4),
114 devpriv->mite->daq_io_addr + AO_CHAN_OFFSET);
115 /* write channel value */
116 writel(data[i], devpriv->mite->daq_io_addr + AO_VALUE_OFFSET);
117 devpriv->ao_readback[chan] = data[i];
123 static int ni_670x_ao_rinsn(struct comedi_device *dev,
124 struct comedi_subdevice *s,
125 struct comedi_insn *insn, unsigned int *data)
127 struct ni_670x_private *devpriv = dev->private;
129 int chan = CR_CHAN(insn->chanspec);
131 for (i = 0; i < insn->n; i++)
132 data[i] = devpriv->ao_readback[chan];
137 static int ni_670x_dio_insn_bits(struct comedi_device *dev,
138 struct comedi_subdevice *s,
139 struct comedi_insn *insn, unsigned int *data)
141 struct ni_670x_private *devpriv = dev->private;
142 void __iomem *io_addr = devpriv->mite->daq_io_addr +
143 DIO_PORT0_DATA_OFFSET;
144 unsigned int mask = data[0];
145 unsigned int bits = data[1];
149 s->state |= (bits & mask);
151 writel(s->state, io_addr);
154 data[1] = readl(io_addr);
159 static int ni_670x_dio_insn_config(struct comedi_device *dev,
160 struct comedi_subdevice *s,
161 struct comedi_insn *insn,
164 struct ni_670x_private *devpriv = dev->private;
167 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
171 writel(s->io_bits, devpriv->mite->daq_io_addr + DIO_PORT0_DIR_OFFSET);
176 static int ni_670x_auto_attach(struct comedi_device *dev,
177 unsigned long context)
179 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
180 const struct ni_670x_board *thisboard = NULL;
181 struct ni_670x_private *devpriv;
182 struct comedi_subdevice *s;
186 if (context < ARRAY_SIZE(ni_670x_boards))
187 thisboard = &ni_670x_boards[context];
190 dev->board_ptr = thisboard;
191 dev->board_name = thisboard->name;
193 ret = comedi_pci_enable(dev);
197 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
201 devpriv->mite = mite_alloc(pcidev);
205 ret = mite_setup(devpriv->mite);
207 dev_warn(dev->class_dev, "error setting up mite\n");
211 ret = comedi_alloc_subdevices(dev, 2);
215 s = &dev->subdevices[0];
216 /* analog output subdevice */
217 s->type = COMEDI_SUBD_AO;
218 s->subdev_flags = SDF_WRITABLE;
219 s->n_chan = thisboard->ao_chans;
221 if (s->n_chan == 32) {
222 const struct comedi_lrange **range_table_list;
224 range_table_list = kmalloc(sizeof(struct comedi_lrange *) * 32,
226 if (!range_table_list)
228 s->range_table_list = range_table_list;
229 for (i = 0; i < 16; i++) {
230 range_table_list[i] = &range_bipolar10;
231 range_table_list[16 + i] = &range_0_20mA;
234 s->range_table = &range_bipolar10;
236 s->insn_write = &ni_670x_ao_winsn;
237 s->insn_read = &ni_670x_ao_rinsn;
239 s = &dev->subdevices[1];
240 /* digital i/o subdevice */
241 s->type = COMEDI_SUBD_DIO;
242 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
245 s->range_table = &range_digital;
246 s->insn_bits = ni_670x_dio_insn_bits;
247 s->insn_config = ni_670x_dio_insn_config;
249 /* Config of misc registers */
250 writel(0x10, devpriv->mite->daq_io_addr + MISC_CONTROL_OFFSET);
251 /* Config of ao registers */
252 writel(0x00, devpriv->mite->daq_io_addr + AO_CONTROL_OFFSET);
254 dev_info(dev->class_dev, "%s: %s attached\n",
255 dev->driver->driver_name, dev->board_name);
260 static void ni_670x_detach(struct comedi_device *dev)
262 struct ni_670x_private *devpriv = dev->private;
263 struct comedi_subdevice *s;
265 if (dev->n_subdevices) {
266 s = &dev->subdevices[0];
268 kfree(s->range_table_list);
270 if (devpriv && devpriv->mite) {
271 mite_unsetup(devpriv->mite);
272 mite_free(devpriv->mite);
274 comedi_pci_disable(dev);
277 static struct comedi_driver ni_670x_driver = {
278 .driver_name = "ni_670x",
279 .module = THIS_MODULE,
280 .auto_attach = ni_670x_auto_attach,
281 .detach = ni_670x_detach,
284 static int ni_670x_pci_probe(struct pci_dev *dev,
285 const struct pci_device_id *id)
287 return comedi_pci_auto_config(dev, &ni_670x_driver, id->driver_data);
290 static DEFINE_PCI_DEVICE_TABLE(ni_670x_pci_table) = {
291 { PCI_VDEVICE(NI, 0x1290), BOARD_PCI6704 },
292 { PCI_VDEVICE(NI, 0x1920), BOARD_PXI6704 },
293 { PCI_VDEVICE(NI, 0x2c90), BOARD_PCI6703 },
296 MODULE_DEVICE_TABLE(pci, ni_670x_pci_table);
298 static struct pci_driver ni_670x_pci_driver = {
300 .id_table = ni_670x_pci_table,
301 .probe = ni_670x_pci_probe,
302 .remove = comedi_pci_auto_unconfig,
304 module_comedi_pci_driver(ni_670x_driver, ni_670x_pci_driver);
306 MODULE_AUTHOR("Comedi http://www.comedi.org");
307 MODULE_DESCRIPTION("Comedi low-level driver");
308 MODULE_LICENSE("GPL");