adl_pci8164.c revision 0a85b6f0ab0d2edb0d41b32697111ce0e4f43496
1/*
2    comedi/drivers/adl_pci8164.c
3
4    Hardware comedi driver fot PCI-8164 Adlink card
5    Copyright (C) 2004 Michel Lachine <mike@mikelachaine.ca>
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21*/
22/*
23Driver: adl_pci8164
24Description: Driver for the Adlink PCI-8164 4 Axes Motion Control board
25Devices: [ADLink] PCI-8164 (adl_pci8164)
26Author: Michel Lachaine <mike@mikelachaine.ca>
27Status: experimental
28Updated: Mon, 14 Apr 2008 15:10:32 +0100
29
30Configuration Options:
31  [0] - PCI bus of device (optional)
32  [1] - PCI slot of device (optional)
33  If bus/slot is not specified, the first supported
34  PCI device found will be used.
35*/
36
37#include "../comedidev.h"
38#include <linux/delay.h>
39#include "comedi_fc.h"
40#include "comedi_pci.h"
41#include "8253.h"
42
43#define PCI8164_AXIS_X  0x00
44#define PCI8164_AXIS_Y  0x08
45#define PCI8164_AXIS_Z  0x10
46#define PCI8164_AXIS_U  0x18
47
48#define PCI8164_MSTS	0x00
49#define PCI8164_SSTS    0x02
50#define PCI8164_BUF0    0x04
51#define PCI8164_BUF1    0x06
52
53#define PCI8164_CMD     0x00
54#define PCI8164_OTP     0x02
55
56#define PCI_DEVICE_ID_PCI8164 0x8164
57
58static DEFINE_PCI_DEVICE_TABLE(adl_pci8164_pci_table) = {
59	{
60	PCI_VENDOR_ID_ADLINK, PCI_DEVICE_ID_PCI8164, PCI_ANY_ID,
61		    PCI_ANY_ID, 0, 0, 0}, {
62	0}
63};
64
65MODULE_DEVICE_TABLE(pci, adl_pci8164_pci_table);
66
67struct adl_pci8164_private {
68	int data;
69	struct pci_dev *pci_dev;
70};
71
72#define devpriv ((struct adl_pci8164_private *)dev->private)
73
74static int adl_pci8164_attach(struct comedi_device *dev,
75			      struct comedi_devconfig *it);
76static int adl_pci8164_detach(struct comedi_device *dev);
77static struct comedi_driver driver_adl_pci8164 = {
78	.driver_name = "adl_pci8164",
79	.module = THIS_MODULE,
80	.attach = adl_pci8164_attach,
81	.detach = adl_pci8164_detach,
82};
83
84static int adl_pci8164_insn_read_msts(struct comedi_device *dev,
85				      struct comedi_subdevice *s,
86				      struct comedi_insn *insn,
87				      unsigned int *data);
88
89static int adl_pci8164_insn_read_ssts(struct comedi_device *dev,
90				      struct comedi_subdevice *s,
91				      struct comedi_insn *insn,
92				      unsigned int *data);
93
94static int adl_pci8164_insn_read_buf0(struct comedi_device *dev,
95				      struct comedi_subdevice *s,
96				      struct comedi_insn *insn,
97				      unsigned int *data);
98
99static int adl_pci8164_insn_read_buf1(struct comedi_device *dev,
100				      struct comedi_subdevice *s,
101				      struct comedi_insn *insn,
102				      unsigned int *data);
103
104static int adl_pci8164_insn_write_cmd(struct comedi_device *dev,
105				      struct comedi_subdevice *s,
106				      struct comedi_insn *insn,
107				      unsigned int *data);
108
109static int adl_pci8164_insn_write_otp(struct comedi_device *dev,
110				      struct comedi_subdevice *s,
111				      struct comedi_insn *insn,
112				      unsigned int *data);
113
114static int adl_pci8164_insn_write_buf0(struct comedi_device *dev,
115				       struct comedi_subdevice *s,
116				       struct comedi_insn *insn,
117				       unsigned int *data);
118
119static int adl_pci8164_insn_write_buf1(struct comedi_device *dev,
120				       struct comedi_subdevice *s,
121				       struct comedi_insn *insn,
122				       unsigned int *data);
123
124static int adl_pci8164_attach(struct comedi_device *dev,
125			      struct comedi_devconfig *it)
126{
127	struct pci_dev *pcidev;
128	struct comedi_subdevice *s;
129	int bus, slot;
130
131	printk("comedi: attempt to attach...\n");
132	printk("comedi%d: adl_pci8164\n", dev->minor);
133
134	dev->board_name = "pci8164";
135	bus = it->options[0];
136	slot = it->options[1];
137
138	if (alloc_private(dev, sizeof(struct adl_pci8164_private)) < 0)
139		return -ENOMEM;
140
141	if (alloc_subdevices(dev, 4) < 0)
142		return -ENOMEM;
143
144	for (pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
145	     pcidev != NULL;
146	     pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pcidev)) {
147
148		if (pcidev->vendor == PCI_VENDOR_ID_ADLINK &&
149		    pcidev->device == PCI_DEVICE_ID_PCI8164) {
150			if (bus || slot) {
151				/* requested particular bus/slot */
152				if (pcidev->bus->number != bus
153				    || PCI_SLOT(pcidev->devfn) != slot) {
154					continue;
155				}
156			}
157			devpriv->pci_dev = pcidev;
158			if (comedi_pci_enable(pcidev, "adl_pci8164") < 0) {
159				printk
160				    ("comedi%d: Failed to enable PCI device and request regions\n",
161				     dev->minor);
162				return -EIO;
163			}
164			dev->iobase = pci_resource_start(pcidev, 2);
165			printk("comedi: base addr %4lx\n", dev->iobase);
166
167			s = dev->subdevices + 0;
168			s->type = COMEDI_SUBD_PROC;
169			s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
170			s->n_chan = 4;
171			s->maxdata = 0xffff;
172			s->len_chanlist = 4;
173			/* s->range_table = &range_axis; */
174			s->insn_read = adl_pci8164_insn_read_msts;
175			s->insn_write = adl_pci8164_insn_write_cmd;
176
177			s = dev->subdevices + 1;
178			s->type = COMEDI_SUBD_PROC;
179			s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
180			s->n_chan = 4;
181			s->maxdata = 0xffff;
182			s->len_chanlist = 4;
183			/* s->range_table = &range_axis; */
184			s->insn_read = adl_pci8164_insn_read_ssts;
185			s->insn_write = adl_pci8164_insn_write_otp;
186
187			s = dev->subdevices + 2;
188			s->type = COMEDI_SUBD_PROC;
189			s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
190			s->n_chan = 4;
191			s->maxdata = 0xffff;
192			s->len_chanlist = 4;
193			/* s->range_table = &range_axis; */
194			s->insn_read = adl_pci8164_insn_read_buf0;
195			s->insn_write = adl_pci8164_insn_write_buf0;
196
197			s = dev->subdevices + 3;
198			s->type = COMEDI_SUBD_PROC;
199			s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
200			s->n_chan = 4;
201			s->maxdata = 0xffff;
202			s->len_chanlist = 4;
203			/* s->range_table = &range_axis; */
204			s->insn_read = adl_pci8164_insn_read_buf1;
205			s->insn_write = adl_pci8164_insn_write_buf1;
206
207			printk("comedi: attached\n");
208
209			return 1;
210		}
211	}
212
213	printk("comedi%d: no supported board found! (req. bus/slot : %d/%d)\n",
214	       dev->minor, bus, slot);
215	return -EIO;
216}
217
218static int adl_pci8164_detach(struct comedi_device *dev)
219{
220	printk("comedi%d: pci8164: remove\n", dev->minor);
221
222	if (devpriv && devpriv->pci_dev) {
223		if (dev->iobase) {
224			comedi_pci_disable(devpriv->pci_dev);
225		}
226		pci_dev_put(devpriv->pci_dev);
227	}
228
229	return 0;
230}
231
232/*
233 all the read commands are the same except for the addition a constant
234 * const to the data for inw()
235 */
236static void adl_pci8164_insn_read(struct comedi_device *dev,
237				  struct comedi_subdevice *s,
238				  struct comedi_insn *insn,
239				  unsigned int *data,
240				  char *action, unsigned short offset)
241{
242	int axis, axis_reg;
243	char *axisname;
244
245	axis = CR_CHAN(insn->chanspec);
246
247	switch (axis) {
248	case 0:
249		axis_reg = PCI8164_AXIS_X;
250		axisname = "X";
251		break;
252	case 1:
253		axis_reg = PCI8164_AXIS_Y;
254		axisname = "Y";
255		break;
256	case 2:
257		axis_reg = PCI8164_AXIS_Z;
258		axisname = "Z";
259		break;
260	case 3:
261		axis_reg = PCI8164_AXIS_U;
262		axisname = "U";
263		break;
264	default:
265		axis_reg = PCI8164_AXIS_X;
266		axisname = "X";
267	}
268
269	data[0] = inw(dev->iobase + axis_reg + offset);
270	printk("comedi: pci8164 %s read -> %04X:%04X on axis %s\n", action,
271	       data[0], data[1], axisname);
272}
273
274static int adl_pci8164_insn_read_msts(struct comedi_device *dev,
275				      struct comedi_subdevice *s,
276				      struct comedi_insn *insn,
277				      unsigned int *data)
278{
279	adl_pci8164_insn_read(dev, s, insn, data, "MSTS", PCI8164_MSTS);
280	return 2;
281}
282
283static int adl_pci8164_insn_read_ssts(struct comedi_device *dev,
284				      struct comedi_subdevice *s,
285				      struct comedi_insn *insn,
286				      unsigned int *data)
287{
288	adl_pci8164_insn_read(dev, s, insn, data, "SSTS", PCI8164_SSTS);
289	return 2;
290}
291
292static int adl_pci8164_insn_read_buf0(struct comedi_device *dev,
293				      struct comedi_subdevice *s,
294				      struct comedi_insn *insn,
295				      unsigned int *data)
296{
297	adl_pci8164_insn_read(dev, s, insn, data, "BUF0", PCI8164_BUF0);
298	return 2;
299}
300
301static int adl_pci8164_insn_read_buf1(struct comedi_device *dev,
302				      struct comedi_subdevice *s,
303				      struct comedi_insn *insn,
304				      unsigned int *data)
305{
306	adl_pci8164_insn_read(dev, s, insn, data, "BUF1", PCI8164_BUF1);
307	return 2;
308}
309
310/*
311 all the write commands are the same except for the addition a constant
312 * const to the data for outw()
313 */
314static void adl_pci8164_insn_out(struct comedi_device *dev,
315				 struct comedi_subdevice *s,
316				 struct comedi_insn *insn,
317				 unsigned int *data,
318				 char *action, unsigned short offset)
319{
320	unsigned int axis, axis_reg;
321
322	char *axisname;
323
324	axis = CR_CHAN(insn->chanspec);
325
326	switch (axis) {
327	case 0:
328		axis_reg = PCI8164_AXIS_X;
329		axisname = "X";
330		break;
331	case 1:
332		axis_reg = PCI8164_AXIS_Y;
333		axisname = "Y";
334		break;
335	case 2:
336		axis_reg = PCI8164_AXIS_Z;
337		axisname = "Z";
338		break;
339	case 3:
340		axis_reg = PCI8164_AXIS_U;
341		axisname = "U";
342		break;
343	default:
344		axis_reg = PCI8164_AXIS_X;
345		axisname = "X";
346	}
347
348	outw(data[0], dev->iobase + axis_reg + offset);
349
350	printk("comedi: pci8164 %s write -> %04X:%04X on axis %s\n", action,
351	       data[0], data[1], axisname);
352
353}
354
355static int adl_pci8164_insn_write_cmd(struct comedi_device *dev,
356				      struct comedi_subdevice *s,
357				      struct comedi_insn *insn,
358				      unsigned int *data)
359{
360	adl_pci8164_insn_out(dev, s, insn, data, "CMD", PCI8164_CMD);
361	return 2;
362}
363
364static int adl_pci8164_insn_write_otp(struct comedi_device *dev,
365				      struct comedi_subdevice *s,
366				      struct comedi_insn *insn,
367				      unsigned int *data)
368{
369	adl_pci8164_insn_out(dev, s, insn, data, "OTP", PCI8164_OTP);
370	return 2;
371}
372
373static int adl_pci8164_insn_write_buf0(struct comedi_device *dev,
374				       struct comedi_subdevice *s,
375				       struct comedi_insn *insn,
376				       unsigned int *data)
377{
378	adl_pci8164_insn_out(dev, s, insn, data, "BUF0", PCI8164_BUF0);
379	return 2;
380}
381
382static int adl_pci8164_insn_write_buf1(struct comedi_device *dev,
383				       struct comedi_subdevice *s,
384				       struct comedi_insn *insn,
385				       unsigned int *data)
386{
387	adl_pci8164_insn_out(dev, s, insn, data, "BUF1", PCI8164_BUF1);
388	return 2;
389}
390
391COMEDI_PCI_INITCLEANUP(driver_adl_pci8164, adl_pci8164_pci_table);
392