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