adl_pci8164.c revision ba7834b3f335f6bdef95e416d72245f6687cc660
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	{PCI_VENDOR_ID_ADLINK, PCI_DEVICE_ID_PCI8164, PCI_ANY_ID, PCI_ANY_ID, 0,
60		0, 0},
61	{0}
62};
63
64MODULE_DEVICE_TABLE(pci, adl_pci8164_pci_table);
65
66struct adl_pci8164_private {
67	int data;
68	struct pci_dev *pci_dev;
69};
70
71#define devpriv ((struct adl_pci8164_private *)dev->private)
72
73static int adl_pci8164_attach(struct comedi_device *dev, struct comedi_devconfig *it);
74static int adl_pci8164_detach(struct comedi_device *dev);
75static struct comedi_driver driver_adl_pci8164 = {
76	.driver_name = "adl_pci8164",
77	.module = THIS_MODULE,
78	.attach = adl_pci8164_attach,
79	.detach = adl_pci8164_detach,
80};
81
82static int adl_pci8164_insn_read_msts(struct comedi_device *dev, struct comedi_subdevice *s,
83	struct comedi_insn *insn, unsigned int *data);
84
85static int adl_pci8164_insn_read_ssts(struct comedi_device *dev, struct comedi_subdevice *s,
86	struct comedi_insn *insn, unsigned int *data);
87
88static int adl_pci8164_insn_read_buf0(struct comedi_device *dev, struct comedi_subdevice *s,
89	struct comedi_insn *insn, unsigned int *data);
90
91static int adl_pci8164_insn_read_buf1(struct comedi_device *dev, struct comedi_subdevice *s,
92	struct comedi_insn *insn, unsigned int *data);
93
94static int adl_pci8164_insn_write_cmd(struct comedi_device *dev, struct comedi_subdevice *s,
95	struct comedi_insn *insn, unsigned int *data);
96
97static int adl_pci8164_insn_write_otp(struct comedi_device *dev, struct comedi_subdevice *s,
98	struct comedi_insn *insn, unsigned int *data);
99
100static int adl_pci8164_insn_write_buf0(struct comedi_device *dev,
101	struct comedi_subdevice *s, struct comedi_insn *insn, unsigned int *data);
102
103static int adl_pci8164_insn_write_buf1(struct comedi_device *dev,
104	struct comedi_subdevice *s, struct comedi_insn *insn, unsigned int *data);
105
106static int adl_pci8164_attach(struct comedi_device *dev, struct comedi_devconfig *it)
107{
108	struct pci_dev *pcidev;
109	struct comedi_subdevice *s;
110	int bus, slot;
111
112	printk("comedi: attempt to attach...\n");
113	printk("comedi%d: adl_pci8164\n", dev->minor);
114
115	dev->board_name = "pci8164";
116	bus = it->options[0];
117	slot = it->options[1];
118
119	if (alloc_private(dev, sizeof(struct adl_pci8164_private)) < 0)
120		return -ENOMEM;
121
122	if (alloc_subdevices(dev, 4) < 0)
123		return -ENOMEM;
124
125	for (pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
126		pcidev != NULL;
127		pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pcidev)) {
128
129		if (pcidev->vendor == PCI_VENDOR_ID_ADLINK &&
130			pcidev->device == PCI_DEVICE_ID_PCI8164) {
131			if (bus || slot) {
132				/* requested particular bus/slot */
133				if (pcidev->bus->number != bus
134					|| PCI_SLOT(pcidev->devfn) != slot) {
135					continue;
136				}
137			}
138			devpriv->pci_dev = pcidev;
139			if (comedi_pci_enable(pcidev, "adl_pci8164") < 0) {
140				printk("comedi%d: Failed to enable PCI device and request regions\n", dev->minor);
141				return -EIO;
142			}
143			dev->iobase = pci_resource_start(pcidev, 2);
144			printk("comedi: base addr %4lx\n", dev->iobase);
145
146			s = dev->subdevices + 0;
147			s->type = COMEDI_SUBD_PROC;
148			s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
149			s->n_chan = 4;
150			s->maxdata = 0xffff;
151			s->len_chanlist = 4;
152			/* s->range_table = &range_axis; */
153			s->insn_read = adl_pci8164_insn_read_msts;
154			s->insn_write = adl_pci8164_insn_write_cmd;
155
156			s = dev->subdevices + 1;
157			s->type = COMEDI_SUBD_PROC;
158			s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
159			s->n_chan = 4;
160			s->maxdata = 0xffff;
161			s->len_chanlist = 4;
162			/* s->range_table = &range_axis; */
163			s->insn_read = adl_pci8164_insn_read_ssts;
164			s->insn_write = adl_pci8164_insn_write_otp;
165
166			s = dev->subdevices + 2;
167			s->type = COMEDI_SUBD_PROC;
168			s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
169			s->n_chan = 4;
170			s->maxdata = 0xffff;
171			s->len_chanlist = 4;
172			/* s->range_table = &range_axis; */
173			s->insn_read = adl_pci8164_insn_read_buf0;
174			s->insn_write = adl_pci8164_insn_write_buf0;
175
176			s = dev->subdevices + 3;
177			s->type = COMEDI_SUBD_PROC;
178			s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
179			s->n_chan = 4;
180			s->maxdata = 0xffff;
181			s->len_chanlist = 4;
182			/* s->range_table = &range_axis; */
183			s->insn_read = adl_pci8164_insn_read_buf1;
184			s->insn_write = adl_pci8164_insn_write_buf1;
185
186			printk("comedi: attached\n");
187
188			return 1;
189		}
190	}
191
192	printk("comedi%d: no supported board found! (req. bus/slot : %d/%d)\n",
193		dev->minor, bus, slot);
194	return -EIO;
195}
196
197static int adl_pci8164_detach(struct comedi_device *dev)
198{
199	printk("comedi%d: pci8164: remove\n", dev->minor);
200
201	if (devpriv && devpriv->pci_dev) {
202		if (dev->iobase) {
203			comedi_pci_disable(devpriv->pci_dev);
204		}
205		pci_dev_put(devpriv->pci_dev);
206	}
207
208	return 0;
209}
210
211/*
212 all the read commands are the same except for the addition a constant
213 * const to the data for inw()
214 */
215static void adl_pci8164_insn_read(struct comedi_device *dev,
216				  struct comedi_subdevice *s,
217				  struct comedi_insn *insn,
218				  unsigned int *data,
219				  char *action,
220				  unsigned short offset)
221{
222	int axis, axis_reg;
223	char *axisname;
224
225	axis = CR_CHAN(insn->chanspec);
226
227	switch (axis) {
228	case 0:
229		axis_reg = PCI8164_AXIS_X;
230		axisname = "X";
231		break;
232	case 1:
233		axis_reg = PCI8164_AXIS_Y;
234		axisname = "Y";
235		break;
236	case 2:
237		axis_reg = PCI8164_AXIS_Z;
238		axisname = "Z";
239		break;
240	case 3:
241		axis_reg = PCI8164_AXIS_U;
242		axisname = "U";
243		break;
244	default:
245		axis_reg = PCI8164_AXIS_X;
246		axisname = "X";
247	}
248
249	data[0] = inw(dev->iobase + axis_reg + offset);
250	printk("comedi: pci8164 %s read -> %04X:%04X on axis %s\n", action, data[0],
251		data[1], axisname);
252}
253
254static int adl_pci8164_insn_read_msts(struct comedi_device *dev,
255				      struct comedi_subdevice *s,
256				      struct comedi_insn *insn,
257				      unsigned int *data)
258{
259	adl_pci8164_insn_read(dev, s, insn, data, "MSTS", PCI8164_MSTS);
260	return 2;
261}
262
263static int adl_pci8164_insn_read_ssts(struct comedi_device *dev, struct comedi_subdevice *s,
264	struct comedi_insn *insn, unsigned int *data)
265{
266	adl_pci8164_insn_read(dev, s, insn, data, "SSTS", PCI8164_SSTS);
267	return 2;
268}
269
270static int adl_pci8164_insn_read_buf0(struct comedi_device *dev, struct comedi_subdevice *s,
271	struct comedi_insn *insn, unsigned int *data)
272{
273	adl_pci8164_insn_read(dev, s, insn, data, "BUF0", PCI8164_BUF0);
274	return 2;
275}
276
277static int adl_pci8164_insn_read_buf1(struct comedi_device *dev, struct comedi_subdevice *s,
278	struct comedi_insn *insn, unsigned int *data)
279{
280	adl_pci8164_insn_read(dev, s, insn, data, "BUF1", PCI8164_BUF1);
281	return 2;
282}
283
284/*
285 all the write commands are the same except for the addition a constant
286 * const to the data for outw()
287 */
288static void adl_pci8164_insn_out(struct comedi_device *dev,
289					struct comedi_subdevice *s,
290					struct comedi_insn *insn,
291					unsigned int *data,
292					char *action,
293					unsigned short offset)
294{
295	unsigned int axis, axis_reg;
296
297	char *axisname;
298
299	axis = CR_CHAN(insn->chanspec);
300
301	switch (axis) {
302	case 0:
303		axis_reg = PCI8164_AXIS_X;
304		axisname = "X";
305		break;
306	case 1:
307		axis_reg = PCI8164_AXIS_Y;
308		axisname = "Y";
309		break;
310	case 2:
311		axis_reg = PCI8164_AXIS_Z;
312		axisname = "Z";
313		break;
314	case 3:
315		axis_reg = PCI8164_AXIS_U;
316		axisname = "U";
317		break;
318	default:
319		axis_reg = PCI8164_AXIS_X;
320		axisname = "X";
321	}
322
323	outw(data[0], dev->iobase + axis_reg + offset);
324
325	printk("comedi: pci8164 %s write -> %04X:%04X on axis %s\n", action,
326	       data[0], data[1], axisname);
327
328}
329
330
331static int adl_pci8164_insn_write_cmd(struct comedi_device *dev, struct comedi_subdevice *s,
332	struct comedi_insn *insn, unsigned int *data)
333{
334	adl_pci8164_insn_out(dev, s, insn, data, "CMD", PCI8164_CMD);
335	return 2;
336}
337
338static int adl_pci8164_insn_write_otp(struct comedi_device *dev, struct comedi_subdevice *s,
339	struct comedi_insn *insn, unsigned int *data)
340{
341	adl_pci8164_insn_out(dev, s, insn, data, "OTP", PCI8164_OTP);
342	return 2;
343}
344
345static int adl_pci8164_insn_write_buf0(struct comedi_device *dev,
346	struct comedi_subdevice *s, struct comedi_insn *insn, unsigned int *data)
347{
348	adl_pci8164_insn_out(dev, s, insn, data, "BUF0", PCI8164_BUF0);
349	return 2;
350}
351
352static int adl_pci8164_insn_write_buf1(struct comedi_device *dev,
353	struct comedi_subdevice *s, struct comedi_insn *insn, unsigned int *data)
354{
355	adl_pci8164_insn_out(dev, s, insn, data, "BUF1", PCI8164_BUF1);
356	return 2;
357}
358
359COMEDI_PCI_INITCLEANUP(driver_adl_pci8164, adl_pci8164_pci_table);
360