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#include <linux/module.h>
47#include <linux/comedi/comedidev.h>
48#include <linux/delay.h>
49
50
51#define MPC624_MASTER_CONTROL 0
52#define MPC624_GNMUXCH 1
53#define MPC624_ADC 2
54#define MPC624_EE 3
55#define MPC624_LEDS 4
56#define MPC624_DIO 5
57#define MPC624_IRQ_MASK 6
58
59
60#define MPC624_ADBUSY BIT(5)
61#define MPC624_ADSDO BIT(4)
62#define MPC624_ADFO BIT(3)
63#define MPC624_ADCS BIT(2)
64#define MPC624_ADSCK BIT(1)
65#define MPC624_ADSDI BIT(0)
66
67
68#define MPC624_EOC_BIT BIT(31)
69#define MPC624_DMY_BIT BIT(30)
70#define MPC624_SGN_BIT BIT(29)
71
72
73#define MPC624_OSR(x) (((x) & 0x1f) << 27)
74#define MPC624_SPEED_3_52_KHZ MPC624_OSR(0x11)
75#define MPC624_SPEED_1_76_KHZ MPC624_OSR(0x12)
76#define MPC624_SPEED_880_HZ MPC624_OSR(0x13)
77#define MPC624_SPEED_440_HZ MPC624_OSR(0x14)
78#define MPC624_SPEED_220_HZ MPC624_OSR(0x15)
79#define MPC624_SPEED_110_HZ MPC624_OSR(0x16)
80#define MPC624_SPEED_55_HZ MPC624_OSR(0x17)
81#define MPC624_SPEED_27_5_HZ MPC624_OSR(0x18)
82#define MPC624_SPEED_13_75_HZ MPC624_OSR(0x19)
83#define MPC624_SPEED_6_875_HZ MPC624_OSR(0x1f)
84
85struct mpc624_private {
86 unsigned int ai_speed;
87};
88
89
90static const struct comedi_lrange range_mpc624_bipolar1 = {
91 1,
92 {
93
94
95 BIP_RANGE(2.02)
96 }
97};
98
99static const struct comedi_lrange range_mpc624_bipolar10 = {
100 1,
101 {
102
103
104 BIP_RANGE(20.2)
105 }
106};
107
108static unsigned int mpc624_ai_get_sample(struct comedi_device *dev,
109 struct comedi_subdevice *s)
110{
111 struct mpc624_private *devpriv = dev->private;
112 unsigned int data_out = devpriv->ai_speed;
113 unsigned int data_in = 0;
114 unsigned int bit;
115 int i;
116
117
118 udelay(1);
119 for (i = 0; i < 32; i++) {
120
121 outb(0, dev->iobase + MPC624_ADC);
122 udelay(1);
123
124
125 bit = (data_out & BIT(31)) ? MPC624_ADSDI : 0;
126 outb(bit, dev->iobase + MPC624_ADC);
127 udelay(1);
128
129
130 outb(MPC624_ADSCK | bit, dev->iobase + MPC624_ADC);
131 udelay(1);
132
133
134 data_in <<= 1;
135 data_in |= (inb(dev->iobase + MPC624_ADC) & MPC624_ADSDO) >> 4;
136 udelay(1);
137
138 data_out <<= 1;
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 if (data_in & MPC624_EOC_BIT)
158 dev_dbg(dev->class_dev, "EOC bit is set!");
159 if (data_in & MPC624_DMY_BIT)
160 dev_dbg(dev->class_dev, "DMY bit is set!");
161
162 if (data_in & MPC624_SGN_BIT) {
163
164
165
166
167
168
169 data_in &= 0x3fffffff;
170 } else {
171
172
173
174
175
176
177 data_in |= MPC624_SGN_BIT;
178 data_in = ~data_in;
179 data_in += 1;
180
181 data_in &= ~(MPC624_EOC_BIT | MPC624_DMY_BIT);
182 data_in = 0x20000000 - data_in;
183 }
184 return data_in;
185}
186
187static int mpc624_ai_eoc(struct comedi_device *dev,
188 struct comedi_subdevice *s,
189 struct comedi_insn *insn,
190 unsigned long context)
191{
192 unsigned char status;
193
194 status = inb(dev->iobase + MPC624_ADC);
195 if ((status & MPC624_ADBUSY) == 0)
196 return 0;
197 return -EBUSY;
198}
199
200static int mpc624_ai_insn_read(struct comedi_device *dev,
201 struct comedi_subdevice *s,
202 struct comedi_insn *insn,
203 unsigned int *data)
204{
205 int ret;
206 int i;
207
208
209
210
211
212 outb(insn->chanspec, dev->iobase + MPC624_GNMUXCH);
213
214 for (i = 0; i < insn->n; i++) {
215
216 outb(MPC624_ADSCK, dev->iobase + MPC624_ADC);
217 udelay(1);
218 outb(MPC624_ADCS | MPC624_ADSCK, dev->iobase + MPC624_ADC);
219 udelay(1);
220 outb(0, dev->iobase + MPC624_ADC);
221 udelay(1);
222
223
224 ret = comedi_timeout(dev, s, insn, mpc624_ai_eoc, 0);
225 if (ret)
226 return ret;
227
228 data[i] = mpc624_ai_get_sample(dev, s);
229 }
230
231 return insn->n;
232}
233
234static int mpc624_attach(struct comedi_device *dev, struct comedi_devconfig *it)
235{
236 struct mpc624_private *devpriv;
237 struct comedi_subdevice *s;
238 int ret;
239
240 ret = comedi_request_region(dev, it->options[0], 0x10);
241 if (ret)
242 return ret;
243
244 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
245 if (!devpriv)
246 return -ENOMEM;
247
248 switch (it->options[1]) {
249 case 0:
250 devpriv->ai_speed = MPC624_SPEED_3_52_KHZ;
251 break;
252 case 1:
253 devpriv->ai_speed = MPC624_SPEED_1_76_KHZ;
254 break;
255 case 2:
256 devpriv->ai_speed = MPC624_SPEED_880_HZ;
257 break;
258 case 3:
259 devpriv->ai_speed = MPC624_SPEED_440_HZ;
260 break;
261 case 4:
262 devpriv->ai_speed = MPC624_SPEED_220_HZ;
263 break;
264 case 5:
265 devpriv->ai_speed = MPC624_SPEED_110_HZ;
266 break;
267 case 6:
268 devpriv->ai_speed = MPC624_SPEED_55_HZ;
269 break;
270 case 7:
271 devpriv->ai_speed = MPC624_SPEED_27_5_HZ;
272 break;
273 case 8:
274 devpriv->ai_speed = MPC624_SPEED_13_75_HZ;
275 break;
276 case 9:
277 devpriv->ai_speed = MPC624_SPEED_6_875_HZ;
278 break;
279 default:
280 devpriv->ai_speed = MPC624_SPEED_3_52_KHZ;
281 }
282
283 ret = comedi_alloc_subdevices(dev, 1);
284 if (ret)
285 return ret;
286
287
288 s = &dev->subdevices[0];
289 s->type = COMEDI_SUBD_AI;
290 s->subdev_flags = SDF_READABLE | SDF_DIFF;
291 s->n_chan = 4;
292 s->maxdata = 0x3fffffff;
293 s->range_table = (it->options[1] == 0) ? &range_mpc624_bipolar1
294 : &range_mpc624_bipolar10;
295 s->insn_read = mpc624_ai_insn_read;
296
297 return 0;
298}
299
300static struct comedi_driver mpc624_driver = {
301 .driver_name = "mpc624",
302 .module = THIS_MODULE,
303 .attach = mpc624_attach,
304 .detach = comedi_legacy_detach,
305};
306module_comedi_driver(mpc624_driver);
307
308MODULE_AUTHOR("Comedi https://www.comedi.org");
309MODULE_DESCRIPTION("Comedi driver for Micro/sys MPC-624 PC/104 board");
310MODULE_LICENSE("GPL");
311