adl_pci6208.c revision dcff1681cb49b2b2be3a3cddee620a81fc81e8e6
1/*
2    comedi/drivers/adl_pci6208.c
3
4    Hardware driver for ADLink 6208 series cards:
5	card	     | voltage output    | current output
6	-------------+-------------------+---------------
7	PCI-6208V    |  8 channels       | -
8	PCI-6216V    | 16 channels       | -
9	PCI-6208A    |  8 channels       | 8 channels
10
11    COMEDI - Linux Control and Measurement Device Interface
12    Copyright (C) 2000 David A. Schleef <ds@schleef.org>
13
14    This program is free software; you can redistribute it and/or modify
15    it under the terms of the GNU General Public License as published by
16    the Free Software Foundation; either version 2 of the License, or
17    (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License for more details.
23
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27*/
28/*
29Driver: adl_pci6208
30Description: ADLink PCI-6208A
31Devices: [ADLink] PCI-6208A (adl_pci6208)
32Author: nsyeow <nsyeow@pd.jaring.my>
33Updated: Fri, 30 Jan 2004 14:44:27 +0800
34Status: untested
35
36Configuration Options:
37  none
38
39References:
40	- ni_660x.c
41	- adl_pci9111.c		copied the entire pci setup section
42	- adl_pci9118.c
43*/
44/*
45 * These headers should be followed by a blank line, and any comments
46 * you wish to say about the driver.  The comment area is the place
47 * to put any known bugs, limitations, unsupported features, supported
48 * command triggers, whether or not commands are supported on particular
49 * subdevices, etc.
50 *
51 * Somewhere in the comment should be information about configuration
52 * options that are used with comedi_config.
53 */
54#include "../comedidev.h"
55
56/*
57 * PCI-6208/6216-GL register map
58 */
59#define PCI6208_AO_CONTROL(x)		(0x00 + (2 * (x)))
60#define PCI6208_AO_STATUS		0x00
61#define PCI6208_AO_STATUS_DATA_SEND	(1 << 0)
62#define PCI6208_DIO			0x40
63#define PCI6208_DIO_DO_MASK		(0x0f)
64#define PCI6208_DIO_DO_SHIFT		(0)
65#define PCI6208_DIO_DI_MASK		(0xf0)
66#define PCI6208_DIO_DI_SHIFT		(4)
67
68/* Board descriptions */
69struct pci6208_board {
70	const char *name;
71	unsigned short dev_id;	/* `lspci` will show you this */
72	int ao_chans;
73};
74
75static const struct pci6208_board pci6208_boards[] = {
76	{
77		.name		= "pci6208a",
78		.dev_id		= 0x6208,
79		.ao_chans	= 8,
80	},
81};
82
83struct pci6208_private {
84	int data;
85	struct pci_dev *pci_dev;	/* for a PCI device */
86	unsigned int ao_readback[2];	/* Used for AO readback */
87};
88
89static int pci6208_ao_winsn(struct comedi_device *dev,
90			    struct comedi_subdevice *s,
91			    struct comedi_insn *insn, unsigned int *data)
92{
93	struct pci6208_private *devpriv = dev->private;
94	int i = 0, Data_Read;
95	unsigned short chan = CR_CHAN(insn->chanspec);
96	unsigned long invert = 1 << (16 - 1);
97	unsigned long out_value;
98	/* Writing a list of values to an AO channel is probably not
99	 * very useful, but that's how the interface is defined. */
100	for (i = 0; i < insn->n; i++) {
101		out_value = data[i] ^ invert;
102		/* a typical programming sequence */
103		do {
104			Data_Read = (inw(dev->iobase) & 1);
105		} while (Data_Read);
106		outw(out_value, dev->iobase + (0x02 * chan));
107		devpriv->ao_readback[chan] = out_value;
108	}
109
110	/* return the number of samples read/written */
111	return i;
112}
113
114/* AO subdevices should have a read insn as well as a write insn.
115 * Usually this means copying a value stored in devpriv. */
116static int pci6208_ao_rinsn(struct comedi_device *dev,
117			    struct comedi_subdevice *s,
118			    struct comedi_insn *insn, unsigned int *data)
119{
120	struct pci6208_private *devpriv = dev->private;
121	int i;
122	int chan = CR_CHAN(insn->chanspec);
123
124	for (i = 0; i < insn->n; i++)
125		data[i] = devpriv->ao_readback[chan];
126
127	return i;
128}
129
130static int pci6208_dio_insn_bits(struct comedi_device *dev,
131				 struct comedi_subdevice *s,
132				 struct comedi_insn *insn,
133				 unsigned int *data)
134{
135	unsigned int mask = data[0] & PCI6208_DIO_DO_MASK;
136	unsigned int bits = data[1];
137
138	if (mask) {
139		s->state &= ~mask;
140		s->state |= bits & mask;
141
142		outw(s->state, dev->iobase + PCI6208_DIO);
143	}
144
145	s->state = inw(dev->iobase + PCI6208_DIO);
146	data[1] = s->state;
147
148	return insn->n;
149}
150
151static int pci6208_dio_insn_config(struct comedi_device *dev,
152				   struct comedi_subdevice *s,
153				   struct comedi_insn *insn,
154				   unsigned int *data)
155{
156	int chan = CR_CHAN(insn->chanspec);
157	unsigned int mask = 1 << chan;
158
159	switch (data[0]) {
160	case INSN_CONFIG_DIO_QUERY:
161		data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
162		break;
163	default:
164		return -EINVAL;
165	}
166
167	return insn->n;
168}
169
170static struct pci_dev *pci6208_find_device(struct comedi_device *dev,
171					   struct comedi_devconfig *it)
172{
173	const struct pci6208_board *thisboard;
174	struct pci_dev *pci_dev = NULL;
175	int bus = it->options[0];
176	int slot = it->options[1];
177	int i;
178
179	for_each_pci_dev(pci_dev) {
180		if (pci_dev->vendor != PCI_VENDOR_ID_ADLINK)
181			continue;
182		for (i = 0; i < ARRAY_SIZE(pci6208_boards); i++) {
183			thisboard = &pci6208_boards[i];
184			if (thisboard->dev_id != pci_dev->device)
185				continue;
186			/* was a particular bus/slot requested? */
187			if (bus || slot) {
188				/* are we on the wrong bus/slot? */
189				if (pci_dev->bus->number != bus ||
190				    PCI_SLOT(pci_dev->devfn) != slot)
191					continue;
192			}
193			dev_dbg(dev->class_dev,
194				"Found %s on bus %d, slot, %d, irq=%d\n",
195				thisboard->name,
196				pci_dev->bus->number,
197				PCI_SLOT(pci_dev->devfn),
198				pci_dev->irq);
199			dev->board_ptr = thisboard;
200			return pci_dev;
201		}
202	}
203	dev_err(dev->class_dev,
204		"No supported board found! (req. bus %d, slot %d)\n",
205		bus, slot);
206	return NULL;
207}
208
209static int pci6208_attach(struct comedi_device *dev,
210			  struct comedi_devconfig *it)
211{
212	const struct pci6208_board *thisboard;
213	struct pci6208_private *devpriv;
214	struct comedi_subdevice *s;
215	int ret;
216
217	ret = alloc_private(dev, sizeof(*devpriv));
218	if (ret < 0)
219		return ret;
220	devpriv = dev->private;
221
222	devpriv->pci_dev = pci6208_find_device(dev, it);
223	if (!devpriv->pci_dev)
224		return -EIO;
225	thisboard = comedi_board(dev);
226
227	ret = comedi_pci_enable(devpriv->pci_dev, "adl_pci6208");
228	if (ret) {
229		dev_err(dev->class_dev,
230			"Failed to enable PCI device and request regions\n");
231		return ret;
232	}
233
234	dev->iobase = pci_resource_start(devpriv->pci_dev, 2);
235
236	dev->board_name = thisboard->name;
237
238	ret = comedi_alloc_subdevices(dev, 2);
239	if (ret)
240		return ret;
241
242	s = dev->subdevices + 0;
243	/* analog output subdevice */
244	s->type		= COMEDI_SUBD_AO;
245	s->subdev_flags	= SDF_WRITABLE;
246	s->n_chan	= thisboard->ao_chans;
247	s->maxdata	= 0xffff;
248	s->range_table	= &range_bipolar10;
249	s->insn_write	= pci6208_ao_winsn;
250	s->insn_read	= pci6208_ao_rinsn;
251
252	s = dev->subdevices + 1;
253	/* digital i/o subdevice */
254	s->type		= COMEDI_SUBD_DIO;
255	s->subdev_flags	= SDF_READABLE | SDF_WRITABLE;
256	s->n_chan	= 8;
257	s->maxdata	= 1;
258	s->range_table	= &range_digital;
259	s->insn_bits	= pci6208_dio_insn_bits;
260	s->insn_config	= pci6208_dio_insn_config;
261
262	s->io_bits	= 0x0f;
263	s->state	= inw(dev->iobase + PCI6208_DIO);
264
265	dev_info(dev->class_dev, "%s: %s, I/O base=0x%04lx\n",
266		dev->driver->driver_name, dev->board_name, dev->iobase);
267
268	return 0;
269}
270
271static void pci6208_detach(struct comedi_device *dev)
272{
273	struct pci6208_private *devpriv = dev->private;
274
275	if (devpriv && devpriv->pci_dev) {
276		if (dev->iobase)
277			comedi_pci_disable(devpriv->pci_dev);
278		pci_dev_put(devpriv->pci_dev);
279	}
280}
281
282static struct comedi_driver adl_pci6208_driver = {
283	.driver_name	= "adl_pci6208",
284	.module		= THIS_MODULE,
285	.attach		= pci6208_attach,
286	.detach		= pci6208_detach,
287};
288
289static int __devinit adl_pci6208_pci_probe(struct pci_dev *dev,
290					   const struct pci_device_id *ent)
291{
292	return comedi_pci_auto_config(dev, &adl_pci6208_driver);
293}
294
295static void __devexit adl_pci6208_pci_remove(struct pci_dev *dev)
296{
297	comedi_pci_auto_unconfig(dev);
298}
299
300static DEFINE_PCI_DEVICE_TABLE(adl_pci6208_pci_table) = {
301	{ PCI_DEVICE(PCI_VENDOR_ID_ADLINK, 0x6208) },
302	{ 0 }
303};
304MODULE_DEVICE_TABLE(pci, adl_pci6208_pci_table);
305
306static struct pci_driver adl_pci6208_pci_driver = {
307	.name		= "adl_pci6208",
308	.id_table	= adl_pci6208_pci_table,
309	.probe		= adl_pci6208_pci_probe,
310	.remove		= __devexit_p(adl_pci6208_pci_remove),
311};
312module_comedi_pci_driver(adl_pci6208_driver, adl_pci6208_pci_driver);
313
314MODULE_AUTHOR("Comedi http://www.comedi.org");
315MODULE_DESCRIPTION("Comedi low-level driver");
316MODULE_LICENSE("GPL");
317