adl_pci6208.c revision 0a1e6c1fdbdcdbbf9457bc812e145062d59a68c2
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-6208/6216 Series Multi-channel Analog Output Cards
31Devices: (ADLink) PCI-6208 [adl_pci6208]
32	 (ADLink) PCI-6216 [adl_pci6216]
33Author: nsyeow <nsyeow@pd.jaring.my>
34Updated: Fri, 30 Jan 2004 14:44:27 +0800
35Status: untested
36
37Configuration Options: not applicable, uses PCI auto config
38
39References:
40	- ni_660x.c
41	- adl_pci9111.c		copied the entire pci setup section
42	- adl_pci9118.c
43*/
44
45#include "../comedidev.h"
46
47/*
48 * ADLINK PCI Device ID's supported by this driver
49 */
50#define PCI_DEVICE_ID_PCI6208		0x6208
51#define PCI_DEVICE_ID_PCI6216		0x6216
52
53/*
54 * PCI-6208/6216-GL register map
55 */
56#define PCI6208_AO_CONTROL(x)		(0x00 + (2 * (x)))
57#define PCI6208_AO_STATUS		0x00
58#define PCI6208_AO_STATUS_DATA_SEND	(1 << 0)
59#define PCI6208_DIO			0x40
60#define PCI6208_DIO_DO_MASK		(0x0f)
61#define PCI6208_DIO_DO_SHIFT		(0)
62#define PCI6208_DIO_DI_MASK		(0xf0)
63#define PCI6208_DIO_DI_SHIFT		(4)
64
65#define PCI6208_MAX_AO_CHANNELS		16
66
67struct pci6208_board {
68	const char *name;
69	unsigned short dev_id;
70	int ao_chans;
71};
72
73static const struct pci6208_board pci6208_boards[] = {
74	{
75		.name		= "adl_pci6208",
76		.dev_id		= PCI_DEVICE_ID_PCI6208,
77		.ao_chans	= 8,
78	}, {
79		.name		= "adl_pci6216",
80		.dev_id		= PCI_DEVICE_ID_PCI6216,
81		.ao_chans	= 16,
82	},
83};
84
85struct pci6208_private {
86	unsigned int ao_readback[PCI6208_MAX_AO_CHANNELS];
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 chan = CR_CHAN(insn->chanspec);
95	unsigned long invert = 1 << (16 - 1);
96	unsigned long value = 0;
97	unsigned short status;
98	int i;
99
100	for (i = 0; i < insn->n; i++) {
101		value = data[i] ^ invert;
102
103		do {
104			status = inw(dev->iobase + PCI6208_AO_STATUS);
105		} while (status & PCI6208_AO_STATUS_DATA_SEND);
106
107		outw(value, dev->iobase + PCI6208_AO_CONTROL(chan));
108	}
109	devpriv->ao_readback[chan] = value;
110
111	return insn->n;
112}
113
114static int pci6208_ao_rinsn(struct comedi_device *dev,
115			    struct comedi_subdevice *s,
116			    struct comedi_insn *insn, unsigned int *data)
117{
118	struct pci6208_private *devpriv = dev->private;
119	int chan = CR_CHAN(insn->chanspec);
120	int i;
121
122	for (i = 0; i < insn->n; i++)
123		data[i] = devpriv->ao_readback[chan];
124
125	return insn->n;
126}
127
128static int pci6208_dio_insn_bits(struct comedi_device *dev,
129				 struct comedi_subdevice *s,
130				 struct comedi_insn *insn,
131				 unsigned int *data)
132{
133	unsigned int mask = data[0] & PCI6208_DIO_DO_MASK;
134	unsigned int bits = data[1];
135
136	if (mask) {
137		s->state &= ~mask;
138		s->state |= bits & mask;
139
140		outw(s->state, dev->iobase + PCI6208_DIO);
141	}
142
143	s->state = inw(dev->iobase + PCI6208_DIO);
144	data[1] = s->state;
145
146	return insn->n;
147}
148
149static int pci6208_dio_insn_config(struct comedi_device *dev,
150				   struct comedi_subdevice *s,
151				   struct comedi_insn *insn,
152				   unsigned int *data)
153{
154	int chan = CR_CHAN(insn->chanspec);
155	unsigned int mask = 1 << chan;
156
157	switch (data[0]) {
158	case INSN_CONFIG_DIO_QUERY:
159		data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
160		break;
161	default:
162		return -EINVAL;
163	}
164
165	return insn->n;
166}
167
168static const void *pci6208_find_boardinfo(struct comedi_device *dev,
169					  struct pci_dev *pcidev)
170{
171	const struct pci6208_board *boardinfo;
172	int i;
173
174	for (i = 0; i < ARRAY_SIZE(pci6208_boards); i++) {
175		boardinfo = &pci6208_boards[i];
176		if (boardinfo->dev_id == pcidev->device)
177			return boardinfo;
178	}
179	return NULL;
180}
181
182static int pci6208_attach_pci(struct comedi_device *dev,
183			      struct pci_dev *pcidev)
184{
185	const struct pci6208_board *boardinfo;
186	struct pci6208_private *devpriv;
187	struct comedi_subdevice *s;
188	int ret;
189
190	comedi_set_hw_dev(dev, &pcidev->dev);
191
192	boardinfo = pci6208_find_boardinfo(dev, pcidev);
193	if (!boardinfo)
194		return -ENODEV;
195	dev->board_ptr = boardinfo;
196	dev->board_name = boardinfo->name;
197
198	ret = alloc_private(dev, sizeof(*devpriv));
199	if (ret < 0)
200		return ret;
201	devpriv = dev->private;
202
203	ret = comedi_pci_enable(pcidev, dev->driver->driver_name);
204	if (ret) {
205		dev_err(dev->class_dev,
206			"Failed to enable PCI device and request regions\n");
207		return ret;
208	}
209	dev->iobase = pci_resource_start(pcidev, 2);
210
211	ret = comedi_alloc_subdevices(dev, 2);
212	if (ret)
213		return ret;
214
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	= boardinfo->ao_chans;
220	s->maxdata	= 0xffff;
221	s->range_table	= &range_bipolar10;
222	s->insn_write	= pci6208_ao_winsn;
223	s->insn_read	= pci6208_ao_rinsn;
224
225	s = dev->subdevices + 1;
226	/* digital i/o subdevice */
227	s->type		= COMEDI_SUBD_DIO;
228	s->subdev_flags	= SDF_READABLE | SDF_WRITABLE;
229	s->n_chan	= 8;
230	s->maxdata	= 1;
231	s->range_table	= &range_digital;
232	s->insn_bits	= pci6208_dio_insn_bits;
233	s->insn_config	= pci6208_dio_insn_config;
234
235	s->io_bits	= 0x0f;
236	s->state	= inw(dev->iobase + PCI6208_DIO);
237
238	dev_info(dev->class_dev, "%s: %s, I/O base=0x%04lx\n",
239		dev->driver->driver_name, dev->board_name, dev->iobase);
240
241	return 0;
242}
243
244static int pci6208_attach(struct comedi_device *dev,
245			  struct comedi_devconfig *it)
246{
247	dev_warn(dev->class_dev,
248		"This driver does not support attach using comedi_config\n");
249
250	return -ENOSYS;
251}
252
253static void pci6208_detach(struct comedi_device *dev)
254{
255	struct pci_dev *pcidev = comedi_to_pci_dev(dev);
256
257	if (pcidev) {
258		if (dev->iobase)
259			comedi_pci_disable(pcidev);
260	}
261}
262
263static struct comedi_driver adl_pci6208_driver = {
264	.driver_name	= "adl_pci6208",
265	.module		= THIS_MODULE,
266	.attach		= pci6208_attach,
267	.attach_pci	= pci6208_attach_pci,
268	.detach		= pci6208_detach,
269};
270
271static int __devinit adl_pci6208_pci_probe(struct pci_dev *dev,
272					   const struct pci_device_id *ent)
273{
274	return comedi_pci_auto_config(dev, &adl_pci6208_driver);
275}
276
277static void __devexit adl_pci6208_pci_remove(struct pci_dev *dev)
278{
279	comedi_pci_auto_unconfig(dev);
280}
281
282static DEFINE_PCI_DEVICE_TABLE(adl_pci6208_pci_table) = {
283	{ PCI_DEVICE(PCI_VENDOR_ID_ADLINK, PCI_DEVICE_ID_PCI6208) },
284	{ PCI_DEVICE(PCI_VENDOR_ID_ADLINK, PCI_DEVICE_ID_PCI6216) },
285	{ 0 }
286};
287MODULE_DEVICE_TABLE(pci, adl_pci6208_pci_table);
288
289static struct pci_driver adl_pci6208_pci_driver = {
290	.name		= "adl_pci6208",
291	.id_table	= adl_pci6208_pci_table,
292	.probe		= adl_pci6208_pci_probe,
293	.remove		= __devexit_p(adl_pci6208_pci_remove),
294};
295module_comedi_pci_driver(adl_pci6208_driver, adl_pci6208_pci_driver);
296
297MODULE_AUTHOR("Comedi http://www.comedi.org");
298MODULE_DESCRIPTION("Comedi low-level driver");
299MODULE_LICENSE("GPL");
300