[go: nahoru, domu]

adl_pci8164.c revision 83dcad479d8b71c9046abfd38d83fcaa55e14f5c
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: not applicable, uses PCI auto config
31*/
32
33#include <linux/kernel.h>
34#include <linux/pci.h>
35#include <linux/delay.h>
36
37#include "../comedidev.h"
38#include "comedi_fc.h"
39#include "8253.h"
40
41#define PCI8164_AXIS(x)		((x) * 0x08)
42#define PCI8164_CMD_MSTS_REG	0x00
43#define PCI8164_OTP_SSTS_REG	0x02
44#define PCI8164_BUF0_REG	0x04
45#define PCI8164_BUF1_REG	0x06
46
47static int adl_pci8164_insn_read(struct comedi_device *dev,
48				 struct comedi_subdevice *s,
49				 struct comedi_insn *insn,
50				 unsigned int *data)
51{
52	unsigned long offset = (unsigned long)s->private;
53	unsigned int chan = CR_CHAN(insn->chanspec);
54	int i;
55
56	for (i = 0; i < insn->n; i++)
57		data[i] = inw(dev->iobase + PCI8164_AXIS(chan) + offset);
58
59	return insn->n;
60}
61
62static int adl_pci8164_insn_write(struct comedi_device *dev,
63				  struct comedi_subdevice *s,
64				  struct comedi_insn *insn,
65				  unsigned int *data)
66{
67	unsigned long offset = (unsigned long)s->private;
68	unsigned int chan = CR_CHAN(insn->chanspec);
69	int i;
70
71	for (i = 0; i < insn->n; i++)
72		outw(data[i], dev->iobase + PCI8164_AXIS(chan) + offset);
73
74	return insn->n;
75}
76
77static int adl_pci8164_auto_attach(struct comedi_device *dev,
78					     unsigned long context_unused)
79{
80	struct pci_dev *pcidev = comedi_to_pci_dev(dev);
81	struct comedi_subdevice *s;
82	int ret;
83
84	dev->board_name = dev->driver->driver_name;
85
86	ret = comedi_pci_enable(pcidev, dev->board_name);
87	if (ret)
88		return ret;
89	dev->iobase = pci_resource_start(pcidev, 2);
90
91	ret = comedi_alloc_subdevices(dev, 4);
92	if (ret)
93		return ret;
94
95	/* read MSTS register / write CMD register for each axis (channel) */
96	s = &dev->subdevices[0];
97	s->type		= COMEDI_SUBD_PROC;
98	s->subdev_flags	= SDF_READABLE | SDF_WRITABLE;
99	s->n_chan	= 4;
100	s->maxdata	= 0xffff;
101	s->len_chanlist	= 4;
102	s->insn_read	= adl_pci8164_insn_read;
103	s->insn_write	= adl_pci8164_insn_write;
104	s->private	= (void *)PCI8164_CMD_MSTS_REG;
105
106	/* read SSTS register / write OTP register for each axis (channel) */
107	s = &dev->subdevices[1];
108	s->type		= COMEDI_SUBD_PROC;
109	s->subdev_flags	= SDF_READABLE | SDF_WRITABLE;
110	s->n_chan	= 4;
111	s->maxdata	= 0xffff;
112	s->len_chanlist	= 4;
113	s->insn_read	= adl_pci8164_insn_read;
114	s->insn_write	= adl_pci8164_insn_write;
115	s->private	= (void *)PCI8164_OTP_SSTS_REG;
116
117	/* read/write BUF0 register for each axis (channel) */
118	s = &dev->subdevices[2];
119	s->type		= COMEDI_SUBD_PROC;
120	s->subdev_flags	= SDF_READABLE | SDF_WRITABLE;
121	s->n_chan	= 4;
122	s->maxdata	= 0xffff;
123	s->len_chanlist	= 4;
124	s->insn_read	= adl_pci8164_insn_read;
125	s->insn_write	= adl_pci8164_insn_write;
126	s->private	= (void *)PCI8164_BUF0_REG;
127
128	/* read/write BUF1 register for each axis (channel) */
129	s = &dev->subdevices[3];
130	s->type		= COMEDI_SUBD_PROC;
131	s->subdev_flags	= SDF_READABLE | SDF_WRITABLE;
132	s->n_chan	= 4;
133	s->maxdata	= 0xffff;
134	s->len_chanlist	= 4;
135	s->insn_read	= adl_pci8164_insn_read;
136	s->insn_write	= adl_pci8164_insn_write;
137	s->private	= (void *)PCI8164_BUF1_REG;
138
139	return 0;
140}
141
142static void adl_pci8164_detach(struct comedi_device *dev)
143{
144	struct pci_dev *pcidev = comedi_to_pci_dev(dev);
145
146	if (pcidev) {
147		if (dev->iobase)
148			comedi_pci_disable(pcidev);
149	}
150}
151
152static struct comedi_driver adl_pci8164_driver = {
153	.driver_name	= "adl_pci8164",
154	.module		= THIS_MODULE,
155	.auto_attach	= adl_pci8164_auto_attach,
156	.detach		= adl_pci8164_detach,
157};
158
159static int adl_pci8164_pci_probe(struct pci_dev *dev,
160				 const struct pci_device_id *id)
161{
162	return comedi_pci_auto_config(dev, &adl_pci8164_driver,
163				      id->driver_data);
164}
165
166static DEFINE_PCI_DEVICE_TABLE(adl_pci8164_pci_table) = {
167	{ PCI_DEVICE(PCI_VENDOR_ID_ADLINK, 0x8164) },
168	{ 0 }
169};
170MODULE_DEVICE_TABLE(pci, adl_pci8164_pci_table);
171
172static struct pci_driver adl_pci8164_pci_driver = {
173	.name		= "adl_pci8164",
174	.id_table	= adl_pci8164_pci_table,
175	.probe		= adl_pci8164_pci_probe,
176	.remove		= comedi_pci_auto_unconfig,
177};
178module_comedi_pci_driver(adl_pci8164_driver, adl_pci8164_pci_driver);
179
180MODULE_AUTHOR("Comedi http://www.comedi.org");
181MODULE_DESCRIPTION("Comedi low-level driver");
182MODULE_LICENSE("GPL");
183