1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49#include <linux/module.h>
50
51#include "../comedi_pci.h"
52
53
54
55
56#define PCI1724_DAC_CTRL_REG 0x00
57#define PCI1724_DAC_CTRL_GX(x) (1 << (20 + ((x) / 8)))
58#define PCI1724_DAC_CTRL_CX(x) (((x) % 8) << 16)
59#define PCI1724_DAC_CTRL_MODE_GAIN (1 << 14)
60#define PCI1724_DAC_CTRL_MODE_OFFSET (2 << 14)
61#define PCI1724_DAC_CTRL_MODE_NORMAL (3 << 14)
62#define PCI1724_DAC_CTRL_MODE_MASK (3 << 14)
63#define PCI1724_DAC_CTRL_DATA(x) (((x) & 0x3fff) << 0)
64#define PCI1724_SYNC_CTRL_REG 0x04
65#define PCI1724_SYNC_CTRL_DACSTAT (1 << 1)
66#define PCI1724_SYNC_CTRL_SYN (1 << 0)
67#define PCI1724_EEPROM_CTRL_REG 0x08
68#define PCI1724_SYNC_TRIG_REG 0x0c
69#define PCI1724_BOARD_ID_REG 0x10
70#define PCI1724_BOARD_ID_MASK (0xf << 0)
71
72static const struct comedi_lrange adv_pci1724_ao_ranges = {
73 4, {
74 BIP_RANGE(10),
75 RANGE_mA(0, 20),
76 RANGE_mA(4, 20),
77 RANGE_unitless(0, 1)
78 }
79};
80
81static int adv_pci1724_dac_idle(struct comedi_device *dev,
82 struct comedi_subdevice *s,
83 struct comedi_insn *insn,
84 unsigned long context)
85{
86 unsigned int status;
87
88 status = inl(dev->iobase + PCI1724_SYNC_CTRL_REG);
89 if ((status & PCI1724_SYNC_CTRL_DACSTAT) == 0)
90 return 0;
91 return -EBUSY;
92}
93
94static int adv_pci1724_insn_write(struct comedi_device *dev,
95 struct comedi_subdevice *s,
96 struct comedi_insn *insn,
97 unsigned int *data)
98{
99 unsigned long mode = (unsigned long)s->private;
100 unsigned int chan = CR_CHAN(insn->chanspec);
101 unsigned int ctrl;
102 int ret;
103 int i;
104
105 ctrl = PCI1724_DAC_CTRL_GX(chan) | PCI1724_DAC_CTRL_CX(chan) | mode;
106
107
108 outl(0, dev->iobase + PCI1724_SYNC_CTRL_REG);
109
110 for (i = 0; i < insn->n; ++i) {
111 unsigned int val = data[i];
112
113 ret = comedi_timeout(dev, s, insn, adv_pci1724_dac_idle, 0);
114 if (ret)
115 return ret;
116
117 outl(ctrl | PCI1724_DAC_CTRL_DATA(val),
118 dev->iobase + PCI1724_DAC_CTRL_REG);
119
120 s->readback[chan] = val;
121 }
122
123 return insn->n;
124}
125
126static int adv_pci1724_auto_attach(struct comedi_device *dev,
127 unsigned long context_unused)
128{
129 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
130 struct comedi_subdevice *s;
131 unsigned int board_id;
132 int ret;
133
134 ret = comedi_pci_enable(dev);
135 if (ret)
136 return ret;
137
138 dev->iobase = pci_resource_start(pcidev, 2);
139 board_id = inl(dev->iobase + PCI1724_BOARD_ID_REG);
140 dev_info(dev->class_dev, "board id: %d\n",
141 board_id & PCI1724_BOARD_ID_MASK);
142
143 ret = comedi_alloc_subdevices(dev, 3);
144 if (ret)
145 return ret;
146
147
148 s = &dev->subdevices[0];
149 s->type = COMEDI_SUBD_AO;
150 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_GROUND;
151 s->n_chan = 32;
152 s->maxdata = 0x3fff;
153 s->range_table = &adv_pci1724_ao_ranges;
154 s->insn_write = adv_pci1724_insn_write;
155 s->private = (void *)PCI1724_DAC_CTRL_MODE_NORMAL;
156
157 ret = comedi_alloc_subdev_readback(s);
158 if (ret)
159 return ret;
160
161
162 s = &dev->subdevices[1];
163 s->type = COMEDI_SUBD_CALIB;
164 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
165 s->n_chan = 32;
166 s->maxdata = 0x3fff;
167 s->insn_write = adv_pci1724_insn_write;
168 s->private = (void *)PCI1724_DAC_CTRL_MODE_OFFSET;
169
170 ret = comedi_alloc_subdev_readback(s);
171 if (ret)
172 return ret;
173
174
175 s = &dev->subdevices[2];
176 s->type = COMEDI_SUBD_CALIB;
177 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
178 s->n_chan = 32;
179 s->maxdata = 0x3fff;
180 s->insn_write = adv_pci1724_insn_write;
181 s->private = (void *)PCI1724_DAC_CTRL_MODE_GAIN;
182
183 ret = comedi_alloc_subdev_readback(s);
184 if (ret)
185 return ret;
186
187 return 0;
188}
189
190static struct comedi_driver adv_pci1724_driver = {
191 .driver_name = "adv_pci1724",
192 .module = THIS_MODULE,
193 .auto_attach = adv_pci1724_auto_attach,
194 .detach = comedi_pci_detach,
195};
196
197static int adv_pci1724_pci_probe(struct pci_dev *dev,
198 const struct pci_device_id *id)
199{
200 return comedi_pci_auto_config(dev, &adv_pci1724_driver,
201 id->driver_data);
202}
203
204static const struct pci_device_id adv_pci1724_pci_table[] = {
205 { PCI_DEVICE(PCI_VENDOR_ID_ADVANTECH, 0x1724) },
206 { 0 }
207};
208MODULE_DEVICE_TABLE(pci, adv_pci1724_pci_table);
209
210static struct pci_driver adv_pci1724_pci_driver = {
211 .name = "adv_pci1724",
212 .id_table = adv_pci1724_pci_table,
213 .probe = adv_pci1724_pci_probe,
214 .remove = comedi_pci_auto_unconfig,
215};
216module_comedi_pci_driver(adv_pci1724_driver, adv_pci1724_pci_driver);
217
218MODULE_AUTHOR("Frank Mori Hess <fmh6jj@gmail.com>");
219MODULE_DESCRIPTION("Advantech PCI-1724U Comedi driver");
220MODULE_LICENSE("GPL");
221