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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 Description: National Instruments 670x
26 Author: Bart Joris <bjoris@advalvas.be>
27 Updated: Wed, 11 Dec 2002 18:25:35 -0800
28 Devices: [National Instruments] PCI-6703 (ni_670x), PCI-6704
31 Commands are not supported.
35 Bart Joris <bjoris@advalvas.be> Last updated on 20/08/2001
39 322110a.pdf PCI/PXI-6704 User Manual
40 322110b.pdf PCI/PXI-6703/6704 User Manual
44 #include <linux/interrupt.h>
45 #include <linux/slab.h>
46 #include "../comedidev.h"
50 #define AO_VALUE_OFFSET 0x00
51 #define AO_CHAN_OFFSET 0x0c
52 #define AO_STATUS_OFFSET 0x10
53 #define AO_CONTROL_OFFSET 0x10
54 #define DIO_PORT0_DIR_OFFSET 0x20
55 #define DIO_PORT0_DATA_OFFSET 0x24
56 #define DIO_PORT1_DIR_OFFSET 0x28
57 #define DIO_PORT1_DATA_OFFSET 0x2c
58 #define MISC_STATUS_OFFSET 0x14
59 #define MISC_CONTROL_OFFSET 0x14
61 /* Board description*/
63 struct ni_670x_board {
65 unsigned short dev_id;
66 unsigned short ao_chans;
69 static const struct ni_670x_board ni_670x_boards[] = {
85 struct ni_670x_private {
87 struct mite_struct *mite;
90 unsigned int ao_readback[32];
93 static struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} };
95 static int ni_670x_ao_winsn(struct comedi_device *dev,
96 struct comedi_subdevice *s,
97 struct comedi_insn *insn, unsigned int *data)
99 struct ni_670x_private *devpriv = dev->private;
101 int chan = CR_CHAN(insn->chanspec);
103 /* Channel number mapping :
105 NI 6703/ NI 6704 | NI 6704 Only
106 ----------------------------------------------------
107 vch(0) : 0 | ich(16) : 1
108 vch(1) : 2 | ich(17) : 3
112 vch(15) : 30 | ich(31) : 31 */
114 for (i = 0; i < insn->n; i++) {
115 /* First write in channel register which channel to use */
116 writel(((chan & 15) << 1) | ((chan & 16) >> 4),
117 devpriv->mite->daq_io_addr + AO_CHAN_OFFSET);
118 /* write channel value */
119 writel(data[i], devpriv->mite->daq_io_addr + AO_VALUE_OFFSET);
120 devpriv->ao_readback[chan] = data[i];
126 static int ni_670x_ao_rinsn(struct comedi_device *dev,
127 struct comedi_subdevice *s,
128 struct comedi_insn *insn, unsigned int *data)
130 struct ni_670x_private *devpriv = dev->private;
132 int chan = CR_CHAN(insn->chanspec);
134 for (i = 0; i < insn->n; i++)
135 data[i] = devpriv->ao_readback[chan];
140 static int ni_670x_dio_insn_bits(struct comedi_device *dev,
141 struct comedi_subdevice *s,
142 struct comedi_insn *insn, unsigned int *data)
144 struct ni_670x_private *devpriv = dev->private;
145 void __iomem *io_addr = devpriv->mite->daq_io_addr +
146 DIO_PORT0_DATA_OFFSET;
147 unsigned int mask = data[0];
148 unsigned int bits = data[1];
152 s->state |= (bits & mask);
154 writel(s->state, io_addr);
157 data[1] = readl(io_addr);
162 static int ni_670x_dio_insn_config(struct comedi_device *dev,
163 struct comedi_subdevice *s,
164 struct comedi_insn *insn, unsigned int *data)
166 struct ni_670x_private *devpriv = dev->private;
167 int chan = CR_CHAN(insn->chanspec);
170 case INSN_CONFIG_DIO_OUTPUT:
171 s->io_bits |= 1 << chan;
173 case INSN_CONFIG_DIO_INPUT:
174 s->io_bits &= ~(1 << chan);
176 case INSN_CONFIG_DIO_QUERY:
178 (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
185 writel(s->io_bits, devpriv->mite->daq_io_addr + DIO_PORT0_DIR_OFFSET);
190 static int ni_670x_find_device(struct comedi_device *dev, int bus, int slot)
192 struct ni_670x_private *devpriv = dev->private;
193 struct mite_struct *mite;
196 for (mite = mite_devices; mite; mite = mite->next) {
200 if (bus != mite->pcidev->bus->number
201 || slot != PCI_SLOT(mite->pcidev->devfn))
205 for (i = 0; i < ARRAY_SIZE(ni_670x_boards); i++) {
206 if (mite_device_id(mite) == ni_670x_boards[i].dev_id) {
207 dev->board_ptr = ni_670x_boards + i;
208 devpriv->mite = mite;
214 dev_warn(dev->class_dev, "no device found\n");
219 static int ni_670x_attach(struct comedi_device *dev,
220 struct comedi_devconfig *it)
222 const struct ni_670x_board *thisboard;
223 struct ni_670x_private *devpriv;
224 struct comedi_subdevice *s;
228 ret = alloc_private(dev, sizeof(*devpriv));
231 devpriv = dev->private;
233 ret = ni_670x_find_device(dev, it->options[0], it->options[1]);
236 thisboard = comedi_board(dev);
238 ret = mite_setup(devpriv->mite);
240 dev_warn(dev->class_dev, "error setting up mite\n");
243 dev->board_name = thisboard->name;
244 dev->irq = mite_irq(devpriv->mite);
246 ret = comedi_alloc_subdevices(dev, 2);
250 s = dev->subdevices + 0;
251 /* analog output subdevice */
252 s->type = COMEDI_SUBD_AO;
253 s->subdev_flags = SDF_WRITABLE;
254 s->n_chan = thisboard->ao_chans;
256 if (s->n_chan == 32) {
257 const struct comedi_lrange **range_table_list;
259 range_table_list = kmalloc(sizeof(struct comedi_lrange *) * 32,
261 if (!range_table_list)
263 s->range_table_list = range_table_list;
264 for (i = 0; i < 16; i++) {
265 range_table_list[i] = &range_bipolar10;
266 range_table_list[16 + i] = &range_0_20mA;
269 s->range_table = &range_bipolar10;
271 s->insn_write = &ni_670x_ao_winsn;
272 s->insn_read = &ni_670x_ao_rinsn;
274 s = dev->subdevices + 1;
275 /* digital i/o subdevice */
276 s->type = COMEDI_SUBD_DIO;
277 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
280 s->range_table = &range_digital;
281 s->insn_bits = ni_670x_dio_insn_bits;
282 s->insn_config = ni_670x_dio_insn_config;
284 /* Config of misc registers */
285 writel(0x10, devpriv->mite->daq_io_addr + MISC_CONTROL_OFFSET);
286 /* Config of ao registers */
287 writel(0x00, devpriv->mite->daq_io_addr + AO_CONTROL_OFFSET);
289 dev_info(dev->class_dev, "%s: %s attached\n",
290 dev->driver->driver_name, dev->board_name);
295 static void ni_670x_detach(struct comedi_device *dev)
297 struct ni_670x_private *devpriv = dev->private;
298 struct comedi_subdevice *s;
300 if (dev->n_subdevices) {
301 s = dev->subdevices + 0;
303 kfree(s->range_table_list);
305 if (devpriv && devpriv->mite)
306 mite_unsetup(devpriv->mite);
308 free_irq(dev->irq, dev);
311 static struct comedi_driver ni_670x_driver = {
312 .driver_name = "ni_670x",
313 .module = THIS_MODULE,
314 .attach = ni_670x_attach,
315 .detach = ni_670x_detach,
318 static int __devinit ni_670x_pci_probe(struct pci_dev *dev,
319 const struct pci_device_id *ent)
321 return comedi_pci_auto_config(dev, &ni_670x_driver);
324 static void __devexit ni_670x_pci_remove(struct pci_dev *dev)
326 comedi_pci_auto_unconfig(dev);
329 static DEFINE_PCI_DEVICE_TABLE(ni_670x_pci_table) = {
330 { PCI_DEVICE(PCI_VENDOR_ID_NI, 0x2c90) },
331 { PCI_DEVICE(PCI_VENDOR_ID_NI, 0x1920) },
334 MODULE_DEVICE_TABLE(pci, ni_670x_pci_table);
336 static struct pci_driver ni_670x_pci_driver = {
338 .id_table = ni_670x_pci_table,
339 .probe = ni_670x_pci_probe,
340 .remove = __devexit_p(ni_670x_pci_remove),
342 module_comedi_pci_driver(ni_670x_driver, ni_670x_pci_driver);
344 MODULE_AUTHOR("Comedi http://www.comedi.org");
345 MODULE_DESCRIPTION("Comedi low-level driver");
346 MODULE_LICENSE("GPL");