adl_pci6208.c revision 98bcf9119cbcab67768a548bcb6dfc2b9464cb96
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 = "pci6208v",
78	   .dev_id = 0x6208,      // not sure
79	   .ao_chans = 8
80	   },
81	   {
82	   .name = "pci6216v",
83	   .dev_id = 0x6208,      // not sure
84	   .ao_chans = 16
85	   }, */
86	{
87	 .name = "pci6208a",
88	 .dev_id = 0x6208,
89	 .ao_chans = 8
90	 }
91};
92
93struct pci6208_private {
94	int data;
95	struct pci_dev *pci_dev;	/* for a PCI device */
96	unsigned int ao_readback[2];	/* Used for AO readback */
97};
98
99static int pci6208_ao_winsn(struct comedi_device *dev,
100			    struct comedi_subdevice *s,
101			    struct comedi_insn *insn, unsigned int *data)
102{
103	struct pci6208_private *devpriv = dev->private;
104	int i = 0, Data_Read;
105	unsigned short chan = CR_CHAN(insn->chanspec);
106	unsigned long invert = 1 << (16 - 1);
107	unsigned long out_value;
108	/* Writing a list of values to an AO channel is probably not
109	 * very useful, but that's how the interface is defined. */
110	for (i = 0; i < insn->n; i++) {
111		out_value = data[i] ^ invert;
112		/* a typical programming sequence */
113		do {
114			Data_Read = (inw(dev->iobase) & 1);
115		} while (Data_Read);
116		outw(out_value, dev->iobase + (0x02 * chan));
117		devpriv->ao_readback[chan] = out_value;
118	}
119
120	/* return the number of samples read/written */
121	return i;
122}
123
124/* AO subdevices should have a read insn as well as a write insn.
125 * Usually this means copying a value stored in devpriv. */
126static int pci6208_ao_rinsn(struct comedi_device *dev,
127			    struct comedi_subdevice *s,
128			    struct comedi_insn *insn, unsigned int *data)
129{
130	struct pci6208_private *devpriv = dev->private;
131	int i;
132	int chan = CR_CHAN(insn->chanspec);
133
134	for (i = 0; i < insn->n; i++)
135		data[i] = devpriv->ao_readback[chan];
136
137	return i;
138}
139
140static int pci6208_dio_insn_bits(struct comedi_device *dev,
141				 struct comedi_subdevice *s,
142				 struct comedi_insn *insn,
143				 unsigned int *data)
144{
145	unsigned int mask = data[0] & PCI6208_DIO_DO_MASK;
146	unsigned int bits = data[1];
147
148	if (mask) {
149		s->state &= ~mask;
150		s->state |= bits & mask;
151
152		outw(s->state, dev->iobase + PCI6208_DIO);
153	}
154
155	s->state = inw(dev->iobase + PCI6208_DIO);
156	data[1] = s->state;
157
158	return insn->n;
159}
160
161static int pci6208_dio_insn_config(struct comedi_device *dev,
162				   struct comedi_subdevice *s,
163				   struct comedi_insn *insn,
164				   unsigned int *data)
165{
166	int chan = CR_CHAN(insn->chanspec);
167	unsigned int mask = 1 << chan;
168
169	switch (data[0]) {
170	case INSN_CONFIG_DIO_QUERY:
171		data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
172		break;
173	default:
174		return -EINVAL;
175	}
176
177	return insn->n;
178}
179
180static struct pci_dev *pci6208_find_device(struct comedi_device *dev,
181					   struct comedi_devconfig *it)
182{
183	const struct pci6208_board *thisboard;
184	struct pci_dev *pci_dev = NULL;
185	int bus = it->options[0];
186	int slot = it->options[1];
187	int i;
188
189	for_each_pci_dev(pci_dev) {
190		if (pci_dev->vendor != PCI_VENDOR_ID_ADLINK)
191			continue;
192		for (i = 0; i < ARRAY_SIZE(pci6208_boards); i++) {
193			thisboard = &pci6208_boards[i];
194			if (thisboard->dev_id != pci_dev->device)
195				continue;
196			/* was a particular bus/slot requested? */
197			if (bus || slot) {
198				/* are we on the wrong bus/slot? */
199				if (pci_dev->bus->number != bus ||
200				    PCI_SLOT(pci_dev->devfn) != slot)
201					continue;
202			}
203			dev_dbg(dev->class_dev,
204				"Found %s on bus %d, slot, %d, irq=%d\n",
205				thisboard->name,
206				pci_dev->bus->number,
207				PCI_SLOT(pci_dev->devfn),
208				pci_dev->irq);
209			dev->board_ptr = thisboard;
210			return pci_dev;
211		}
212	}
213	dev_err(dev->class_dev,
214		"No supported board found! (req. bus %d, slot %d)\n",
215		bus, slot);
216	return NULL;
217}
218
219static int pci6208_attach(struct comedi_device *dev,
220			  struct comedi_devconfig *it)
221{
222	const struct pci6208_board *thisboard;
223	struct pci6208_private *devpriv;
224	struct comedi_subdevice *s;
225	int ret;
226
227	ret = alloc_private(dev, sizeof(*devpriv));
228	if (ret < 0)
229		return ret;
230	devpriv = dev->private;
231
232	devpriv->pci_dev = pci6208_find_device(dev, it);
233	if (!devpriv->pci_dev)
234		return -EIO;
235	thisboard = comedi_board(dev);
236
237	ret = comedi_pci_enable(devpriv->pci_dev, "adl_pci6208");
238	if (ret) {
239		dev_err(dev->class_dev,
240			"Failed to enable PCI device and request regions\n");
241		return ret;
242	}
243
244	dev->iobase = pci_resource_start(devpriv->pci_dev, 2);
245
246	dev->board_name = thisboard->name;
247
248	ret = comedi_alloc_subdevices(dev, 2);
249	if (ret)
250		return ret;
251
252	s = dev->subdevices + 0;
253	/* analog output subdevice */
254	s->type		= COMEDI_SUBD_AO;
255	s->subdev_flags	= SDF_WRITABLE;
256	s->n_chan	= thisboard->ao_chans;
257	s->maxdata	= 0xffff;
258	s->range_table	= &range_bipolar10;
259	s->insn_write	= pci6208_ao_winsn;
260	s->insn_read	= pci6208_ao_rinsn;
261
262	s = dev->subdevices + 1;
263	/* digital i/o subdevice */
264	s->type		= COMEDI_SUBD_DIO;
265	s->subdev_flags	= SDF_READABLE | SDF_WRITABLE;
266	s->n_chan	= 8;
267	s->maxdata	= 1;
268	s->range_table	= &range_digital;
269	s->insn_bits	= pci6208_dio_insn_bits;
270	s->insn_config	= pci6208_dio_insn_config;
271
272	s->io_bits	= 0x0f;
273	s->state	= inw(dev->iobase + PCI6208_DIO);
274
275	dev_info(dev->class_dev, "%s: %s, I/O base=0x%04lx\n",
276		dev->driver->driver_name, dev->board_name, dev->iobase);
277
278	return 0;
279}
280
281static void pci6208_detach(struct comedi_device *dev)
282{
283	struct pci6208_private *devpriv = dev->private;
284
285	if (devpriv && devpriv->pci_dev) {
286		if (dev->iobase)
287			comedi_pci_disable(devpriv->pci_dev);
288		pci_dev_put(devpriv->pci_dev);
289	}
290}
291
292static struct comedi_driver adl_pci6208_driver = {
293	.driver_name	= "adl_pci6208",
294	.module		= THIS_MODULE,
295	.attach		= pci6208_attach,
296	.detach		= pci6208_detach,
297};
298
299static int __devinit adl_pci6208_pci_probe(struct pci_dev *dev,
300					   const struct pci_device_id *ent)
301{
302	return comedi_pci_auto_config(dev, &adl_pci6208_driver);
303}
304
305static void __devexit adl_pci6208_pci_remove(struct pci_dev *dev)
306{
307	comedi_pci_auto_unconfig(dev);
308}
309
310/* This is used by modprobe to translate PCI IDs to drivers.  Should
311 * only be used for PCI and ISA-PnP devices */
312static DEFINE_PCI_DEVICE_TABLE(adl_pci6208_pci_table) = {
313	/* { PCI_VENDOR_ID_ADLINK, 0x6208, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, */
314	/* { PCI_VENDOR_ID_ADLINK, 0x6208, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, */
315	{ PCI_DEVICE(PCI_VENDOR_ID_ADLINK, 0x6208) },
316	{ 0 }
317};
318MODULE_DEVICE_TABLE(pci, adl_pci6208_pci_table);
319
320static struct pci_driver adl_pci6208_pci_driver = {
321	.name		= "adl_pci6208",
322	.id_table	= adl_pci6208_pci_table,
323	.probe		= adl_pci6208_pci_probe,
324	.remove		= __devexit_p(adl_pci6208_pci_remove),
325};
326module_comedi_pci_driver(adl_pci6208_driver, adl_pci6208_pci_driver);
327
328MODULE_AUTHOR("Comedi http://www.comedi.org");
329MODULE_DESCRIPTION("Comedi low-level driver");
330MODULE_LICENSE("GPL");
331