ni_670x.c revision 0707bb04be89b18ee83b5a997e36cc585f0b988d
1/*
2    comedi/drivers/ni_670x.c
3    Hardware driver for NI 670x devices
4
5    COMEDI - Linux Control and Measurement Device Interface
6    Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
7
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.
12
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.
17
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.
21
22*/
23/*
24Driver: ni_670x
25Description: National Instruments 670x
26Author: Bart Joris <bjoris@advalvas.be>
27Updated: Wed, 11 Dec 2002 18:25:35 -0800
28Devices: [National Instruments] PCI-6703 (ni_670x), PCI-6704
29Status: unknown
30
31Commands are not supported.
32*/
33
34/*
35	Bart Joris <bjoris@advalvas.be> Last updated on 20/08/2001
36
37	Manuals:
38
39	322110a.pdf	PCI/PXI-6704 User Manual
40	322110b.pdf	PCI/PXI-6703/6704 User Manual
41
42*/
43
44#include "../comedidev.h"
45
46#include "mite.h"
47
48#define PCI_VENDOR_ID_NATINST	0x1093
49
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
60
61/* Board description*/
62
63typedef struct ni_670x_board_struct {
64	unsigned short dev_id;
65	const char *name;
66	unsigned short ao_chans;
67	unsigned short ao_bits;
68} ni_670x_board;
69static const ni_670x_board ni_670x_boards[] = {
70	{
71	      dev_id:	0x2c90,
72	      name:	"PCI-6703",
73	      ao_chans:16,
74	      ao_bits:	16,
75		},
76	{
77	      dev_id:	0x1920,
78	      name:	"PXI-6704",
79	      ao_chans:32,
80	      ao_bits:	16,
81		},
82	{
83	      dev_id:	0x1290,
84	      name:	"PCI-6704",
85	      ao_chans:32,
86	      ao_bits:	16,
87		},
88};
89
90static DEFINE_PCI_DEVICE_TABLE(ni_670x_pci_table) = {
91	{PCI_VENDOR_ID_NATINST, 0x2c90, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
92	{PCI_VENDOR_ID_NATINST, 0x1920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
93	//{ PCI_VENDOR_ID_NATINST, 0x0000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
94	{0}
95};
96
97MODULE_DEVICE_TABLE(pci, ni_670x_pci_table);
98
99#define thisboard ((ni_670x_board *)dev->board_ptr)
100
101typedef struct {
102	struct mite_struct *mite;
103	int boardtype;
104	int dio;
105	unsigned int ao_readback[32];
106} ni_670x_private;
107
108#define devpriv ((ni_670x_private *)dev->private)
109#define n_ni_670x_boards (sizeof(ni_670x_boards)/sizeof(ni_670x_boards[0]))
110
111static int ni_670x_attach(struct comedi_device * dev, struct comedi_devconfig * it);
112static int ni_670x_detach(struct comedi_device * dev);
113
114static struct comedi_driver driver_ni_670x = {
115      driver_name:"ni_670x",
116      module:THIS_MODULE,
117      attach:ni_670x_attach,
118      detach:ni_670x_detach,
119};
120
121COMEDI_PCI_INITCLEANUP(driver_ni_670x, ni_670x_pci_table);
122
123static struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} };
124
125static int ni_670x_find_device(struct comedi_device * dev, int bus, int slot);
126
127static int ni_670x_ao_winsn(struct comedi_device * dev, struct comedi_subdevice * s,
128	struct comedi_insn * insn, unsigned int * data);
129static int ni_670x_ao_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
130	struct comedi_insn * insn, unsigned int * data);
131static int ni_670x_dio_insn_bits(struct comedi_device * dev, struct comedi_subdevice * s,
132	struct comedi_insn * insn, unsigned int * data);
133static int ni_670x_dio_insn_config(struct comedi_device * dev, struct comedi_subdevice * s,
134	struct comedi_insn * insn, unsigned int * data);
135
136static int ni_670x_attach(struct comedi_device * dev, struct comedi_devconfig * it)
137{
138	struct comedi_subdevice *s;
139	int ret;
140	int i;
141
142	printk("comedi%d: ni_670x: ", dev->minor);
143
144	if ((ret = alloc_private(dev, sizeof(ni_670x_private))) < 0)
145		return ret;
146
147	ret = ni_670x_find_device(dev, it->options[0], it->options[1]);
148	if (ret < 0)
149		return ret;
150
151	ret = mite_setup(devpriv->mite);
152	if (ret < 0) {
153		printk("error setting up mite\n");
154		return ret;
155	}
156	dev->board_name = thisboard->name;
157	dev->irq = mite_irq(devpriv->mite);
158	printk(" %s", dev->board_name);
159
160	if (alloc_subdevices(dev, 2) < 0)
161		return -ENOMEM;
162
163	s = dev->subdevices + 0;
164	/* analog output subdevice */
165	s->type = COMEDI_SUBD_AO;
166	s->subdev_flags = SDF_WRITABLE;
167	s->n_chan = thisboard->ao_chans;
168	s->maxdata = 0xffff;
169	if (s->n_chan == 32) {
170		const struct comedi_lrange **range_table_list;
171
172		range_table_list = kmalloc(sizeof(struct comedi_lrange *) * 32,
173			GFP_KERNEL);
174		if (!range_table_list)
175			return -ENOMEM;
176		s->range_table_list = range_table_list;
177		for (i = 0; i < 16; i++) {
178			range_table_list[i] = &range_bipolar10;
179			range_table_list[16 + i] = &range_0_20mA;
180		}
181	} else {
182		s->range_table = &range_bipolar10;
183	}
184	s->insn_write = &ni_670x_ao_winsn;
185	s->insn_read = &ni_670x_ao_rinsn;
186
187	s = dev->subdevices + 1;
188	/* digital i/o subdevice */
189	s->type = COMEDI_SUBD_DIO;
190	s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
191	s->n_chan = 8;
192	s->maxdata = 1;
193	s->range_table = &range_digital;
194	s->insn_bits = ni_670x_dio_insn_bits;
195	s->insn_config = ni_670x_dio_insn_config;
196
197	writel(0x10, devpriv->mite->daq_io_addr + MISC_CONTROL_OFFSET);	/* Config of misc registers */
198	writel(0x00, devpriv->mite->daq_io_addr + AO_CONTROL_OFFSET);	/* Config of ao registers */
199
200	printk("attached\n");
201
202	return 1;
203}
204
205static int ni_670x_detach(struct comedi_device * dev)
206{
207	printk("comedi%d: ni_670x: remove\n", dev->minor);
208
209	if (dev->subdevices[0].range_table_list) {
210		kfree(dev->subdevices[0].range_table_list);
211	}
212	if (dev->private && devpriv->mite)
213		mite_unsetup(devpriv->mite);
214
215	if (dev->irq)
216		comedi_free_irq(dev->irq, dev);
217
218	return 0;
219}
220
221static int ni_670x_ao_winsn(struct comedi_device * dev, struct comedi_subdevice * s,
222	struct comedi_insn * insn, unsigned int * data)
223{
224	int i;
225	int chan = CR_CHAN(insn->chanspec);
226
227	/* Channel number mapping :
228
229	   NI 6703/ NI 6704     | NI 6704 Only
230	   ----------------------------------------------------
231	   vch(0)       :       0       | ich(16)       :       1
232	   vch(1)       :       2       | ich(17)       :       3
233	   .    :       .       |   .                   .
234	   .    :       .       |   .                   .
235	   .    :       .       |   .                   .
236	   vch(15)      :       30      | ich(31)       :       31      */
237
238	for (i = 0; i < insn->n; i++) {
239		writel(((chan & 15) << 1) | ((chan & 16) >> 4), devpriv->mite->daq_io_addr + AO_CHAN_OFFSET);	/* First write in channel register which channel to use */
240		writel(data[i], devpriv->mite->daq_io_addr + AO_VALUE_OFFSET);	/* write channel value */
241		devpriv->ao_readback[chan] = data[i];
242	}
243
244	return i;
245}
246
247static int ni_670x_ao_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
248	struct comedi_insn * insn, unsigned int * data)
249{
250	int i;
251	int chan = CR_CHAN(insn->chanspec);
252
253	for (i = 0; i < insn->n; i++)
254		data[i] = devpriv->ao_readback[chan];
255
256	return i;
257}
258
259static int ni_670x_dio_insn_bits(struct comedi_device * dev, struct comedi_subdevice * s,
260	struct comedi_insn * insn, unsigned int * data)
261{
262	if (insn->n != 2)
263		return -EINVAL;
264
265	/* The insn data is a mask in data[0] and the new data
266	 * in data[1], each channel cooresponding to a bit. */
267	if (data[0]) {
268		s->state &= ~data[0];
269		s->state |= data[0] & data[1];
270		writel(s->state,
271			devpriv->mite->daq_io_addr + DIO_PORT0_DATA_OFFSET);
272	}
273
274	/* on return, data[1] contains the value of the digital
275	 * input lines. */
276	data[1] = readl(devpriv->mite->daq_io_addr + DIO_PORT0_DATA_OFFSET);
277
278	return 2;
279}
280
281static int ni_670x_dio_insn_config(struct comedi_device * dev, struct comedi_subdevice * s,
282	struct comedi_insn * insn, unsigned int * data)
283{
284	int chan = CR_CHAN(insn->chanspec);
285
286	switch (data[0]) {
287	case INSN_CONFIG_DIO_OUTPUT:
288		s->io_bits |= 1 << chan;
289		break;
290	case INSN_CONFIG_DIO_INPUT:
291		s->io_bits &= ~(1 << chan);
292		break;
293	case INSN_CONFIG_DIO_QUERY:
294		data[1] =
295			(s->
296			io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
297		return insn->n;
298		break;
299	default:
300		return -EINVAL;
301		break;
302	}
303	writel(s->io_bits, devpriv->mite->daq_io_addr + DIO_PORT0_DIR_OFFSET);
304
305	return insn->n;
306}
307
308static int ni_670x_find_device(struct comedi_device * dev, int bus, int slot)
309{
310	struct mite_struct *mite;
311	int i;
312
313	for (mite = mite_devices; mite; mite = mite->next) {
314		if (mite->used)
315			continue;
316		if (bus || slot) {
317			if (bus != mite->pcidev->bus->number
318				|| slot != PCI_SLOT(mite->pcidev->devfn))
319				continue;
320		}
321
322		for (i = 0; i < n_ni_670x_boards; i++) {
323			if (mite_device_id(mite) == ni_670x_boards[i].dev_id) {
324				dev->board_ptr = ni_670x_boards + i;
325				devpriv->mite = mite;
326
327				return 0;
328			}
329		}
330	}
331	printk("no device found\n");
332	mite_list_devices();
333	return -EIO;
334}
335