1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * comedi/drivers/adl_pci8164.c
4  *
5  * Hardware comedi driver for PCI-8164 Adlink card
6  * Copyright (C) 2004 Michel Lachine <mike@mikelachaine.ca>
7  */
8 
9 /*
10  * Driver: adl_pci8164
11  * Description: Driver for the Adlink PCI-8164 4 Axes Motion Control board
12  * Devices: [ADLink] PCI-8164 (adl_pci8164)
13  * Author: Michel Lachaine <mike@mikelachaine.ca>
14  * Status: experimental
15  * Updated: Mon, 14 Apr 2008 15:10:32 +0100
16  *
17  * Configuration Options: not applicable, uses PCI auto config
18  */
19 
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/comedi/comedi_pci.h>
23 
24 #define PCI8164_AXIS(x)		((x) * 0x08)
25 #define PCI8164_CMD_MSTS_REG	0x00
26 #define PCI8164_OTP_SSTS_REG	0x02
27 #define PCI8164_BUF0_REG	0x04
28 #define PCI8164_BUF1_REG	0x06
29 
adl_pci8164_insn_read(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)30 static int adl_pci8164_insn_read(struct comedi_device *dev,
31 				 struct comedi_subdevice *s,
32 				 struct comedi_insn *insn,
33 				 unsigned int *data)
34 {
35 	unsigned long offset = (unsigned long)s->private;
36 	unsigned int chan = CR_CHAN(insn->chanspec);
37 	int i;
38 
39 	for (i = 0; i < insn->n; i++)
40 		data[i] = inw(dev->iobase + PCI8164_AXIS(chan) + offset);
41 
42 	return insn->n;
43 }
44 
adl_pci8164_insn_write(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)45 static int adl_pci8164_insn_write(struct comedi_device *dev,
46 				  struct comedi_subdevice *s,
47 				  struct comedi_insn *insn,
48 				  unsigned int *data)
49 {
50 	unsigned long offset = (unsigned long)s->private;
51 	unsigned int chan = CR_CHAN(insn->chanspec);
52 	int i;
53 
54 	for (i = 0; i < insn->n; i++)
55 		outw(data[i], dev->iobase + PCI8164_AXIS(chan) + offset);
56 
57 	return insn->n;
58 }
59 
adl_pci8164_auto_attach(struct comedi_device * dev,unsigned long context_unused)60 static int adl_pci8164_auto_attach(struct comedi_device *dev,
61 				   unsigned long context_unused)
62 {
63 	struct pci_dev *pcidev = comedi_to_pci_dev(dev);
64 	struct comedi_subdevice *s;
65 	int ret;
66 
67 	ret = comedi_pci_enable(dev);
68 	if (ret)
69 		return ret;
70 	dev->iobase = pci_resource_start(pcidev, 2);
71 
72 	ret = comedi_alloc_subdevices(dev, 4);
73 	if (ret)
74 		return ret;
75 
76 	/* read MSTS register / write CMD register for each axis (channel) */
77 	s = &dev->subdevices[0];
78 	s->type		= COMEDI_SUBD_PROC;
79 	s->subdev_flags	= SDF_READABLE | SDF_WRITABLE;
80 	s->n_chan	= 4;
81 	s->maxdata	= 0xffff;
82 	s->len_chanlist	= 4;
83 	s->insn_read	= adl_pci8164_insn_read;
84 	s->insn_write	= adl_pci8164_insn_write;
85 	s->private	= (void *)PCI8164_CMD_MSTS_REG;
86 
87 	/* read SSTS register / write OTP register for each axis (channel) */
88 	s = &dev->subdevices[1];
89 	s->type		= COMEDI_SUBD_PROC;
90 	s->subdev_flags	= SDF_READABLE | SDF_WRITABLE;
91 	s->n_chan	= 4;
92 	s->maxdata	= 0xffff;
93 	s->len_chanlist	= 4;
94 	s->insn_read	= adl_pci8164_insn_read;
95 	s->insn_write	= adl_pci8164_insn_write;
96 	s->private	= (void *)PCI8164_OTP_SSTS_REG;
97 
98 	/* read/write BUF0 register for each axis (channel) */
99 	s = &dev->subdevices[2];
100 	s->type		= COMEDI_SUBD_PROC;
101 	s->subdev_flags	= SDF_READABLE | SDF_WRITABLE;
102 	s->n_chan	= 4;
103 	s->maxdata	= 0xffff;
104 	s->len_chanlist	= 4;
105 	s->insn_read	= adl_pci8164_insn_read;
106 	s->insn_write	= adl_pci8164_insn_write;
107 	s->private	= (void *)PCI8164_BUF0_REG;
108 
109 	/* read/write BUF1 register for each axis (channel) */
110 	s = &dev->subdevices[3];
111 	s->type		= COMEDI_SUBD_PROC;
112 	s->subdev_flags	= SDF_READABLE | SDF_WRITABLE;
113 	s->n_chan	= 4;
114 	s->maxdata	= 0xffff;
115 	s->len_chanlist	= 4;
116 	s->insn_read	= adl_pci8164_insn_read;
117 	s->insn_write	= adl_pci8164_insn_write;
118 	s->private	= (void *)PCI8164_BUF1_REG;
119 
120 	return 0;
121 }
122 
123 static struct comedi_driver adl_pci8164_driver = {
124 	.driver_name	= "adl_pci8164",
125 	.module		= THIS_MODULE,
126 	.auto_attach	= adl_pci8164_auto_attach,
127 	.detach		= comedi_pci_detach,
128 };
129 
adl_pci8164_pci_probe(struct pci_dev * dev,const struct pci_device_id * id)130 static int adl_pci8164_pci_probe(struct pci_dev *dev,
131 				 const struct pci_device_id *id)
132 {
133 	return comedi_pci_auto_config(dev, &adl_pci8164_driver,
134 				      id->driver_data);
135 }
136 
137 static const struct pci_device_id adl_pci8164_pci_table[] = {
138 	{ PCI_DEVICE(PCI_VENDOR_ID_ADLINK, 0x8164) },
139 	{ 0 }
140 };
141 MODULE_DEVICE_TABLE(pci, adl_pci8164_pci_table);
142 
143 static struct pci_driver adl_pci8164_pci_driver = {
144 	.name		= "adl_pci8164",
145 	.id_table	= adl_pci8164_pci_table,
146 	.probe		= adl_pci8164_pci_probe,
147 	.remove		= comedi_pci_auto_unconfig,
148 };
149 module_comedi_pci_driver(adl_pci8164_driver, adl_pci8164_pci_driver);
150 
151 MODULE_AUTHOR("Comedi https://www.comedi.org");
152 MODULE_DESCRIPTION("Comedi low-level driver");
153 MODULE_LICENSE("GPL");
154