Merge branch 'for-3.11' of git://linux-nfs.org/~bfields/linux
[linux-2.6.git] / drivers / staging / comedi / drivers / mpc624.c
blob713842ad6ff67a9fc842e3c4f31d3a6e85eb37ed
1 /*
2 comedi/drivers/mpc624.c
3 Hardware driver for a Micro/sys inc. MPC-624 PC/104 board
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
19 Driver: mpc624
20 Description: Micro/sys MPC-624 PC/104 board
21 Devices: [Micro/sys] MPC-624 (mpc624)
22 Author: Stanislaw Raczynski <sraczynski@op.pl>
23 Updated: Thu, 15 Sep 2005 12:01:18 +0200
24 Status: working
26 The Micro/sys MPC-624 board is based on the LTC2440 24-bit sigma-delta
27 ADC chip.
29 Subdevices supported by the driver:
30 - Analog In: supported
31 - Digital I/O: not supported
32 - LEDs: not supported
33 - EEPROM: not supported
35 Configuration Options:
36 [0] - I/O base address
37 [1] - conversion rate
38 Conversion rate RMS noise Effective Number Of Bits
39 0 3.52kHz 23uV 17
40 1 1.76kHz 3.5uV 20
41 2 880Hz 2uV 21.3
42 3 440Hz 1.4uV 21.8
43 4 220Hz 1uV 22.4
44 5 110Hz 750uV 22.9
45 6 55Hz 510nV 23.4
46 7 27.5Hz 375nV 24
47 8 13.75Hz 250nV 24.4
48 9 6.875Hz 200nV 24.6
49 [2] - voltage range
50 0 -1.01V .. +1.01V
51 1 -10.1V .. +10.1V
54 #include "../comedidev.h"
56 #include <linux/ioport.h>
57 #include <linux/delay.h>
59 /* Consecutive I/O port addresses */
60 #define MPC624_SIZE 16
62 /* Offsets of different ports */
63 #define MPC624_MASTER_CONTROL 0 /* not used */
64 #define MPC624_GNMUXCH 1 /* Gain, Mux, Channel of ADC */
65 #define MPC624_ADC 2 /* read/write to/from ADC */
66 #define MPC624_EE 3 /* read/write to/from serial EEPROM via I2C */
67 #define MPC624_LEDS 4 /* write to LEDs */
68 #define MPC624_DIO 5 /* read/write to/from digital I/O ports */
69 #define MPC624_IRQ_MASK 6 /* IRQ masking enable/disable */
71 /* Register bits' names */
72 #define MPC624_ADBUSY (1<<5)
73 #define MPC624_ADSDO (1<<4)
74 #define MPC624_ADFO (1<<3)
75 #define MPC624_ADCS (1<<2)
76 #define MPC624_ADSCK (1<<1)
77 #define MPC624_ADSDI (1<<0)
79 /* SDI Speed/Resolution Programming bits */
80 #define MPC624_OSR4 (1<<31)
81 #define MPC624_OSR3 (1<<30)
82 #define MPC624_OSR2 (1<<29)
83 #define MPC624_OSR1 (1<<28)
84 #define MPC624_OSR0 (1<<27)
86 /* 32-bit output value bits' names */
87 #define MPC624_EOC_BIT (1<<31)
88 #define MPC624_DMY_BIT (1<<30)
89 #define MPC624_SGN_BIT (1<<29)
91 /* Conversion speeds */
92 /* OSR4 OSR3 OSR2 OSR1 OSR0 Conversion rate RMS noise ENOB^
93 * X 0 0 0 1 3.52kHz 23uV 17
94 * X 0 0 1 0 1.76kHz 3.5uV 20
95 * X 0 0 1 1 880Hz 2uV 21.3
96 * X 0 1 0 0 440Hz 1.4uV 21.8
97 * X 0 1 0 1 220Hz 1uV 22.4
98 * X 0 1 1 0 110Hz 750uV 22.9
99 * X 0 1 1 1 55Hz 510nV 23.4
100 * X 1 0 0 0 27.5Hz 375nV 24
101 * X 1 0 0 1 13.75Hz 250nV 24.4
102 * X 1 1 1 1 6.875Hz 200nV 24.6
104 * ^ - Effective Number Of Bits
107 #define MPC624_SPEED_3_52_kHz (MPC624_OSR4 | MPC624_OSR0)
108 #define MPC624_SPEED_1_76_kHz (MPC624_OSR4 | MPC624_OSR1)
109 #define MPC624_SPEED_880_Hz (MPC624_OSR4 | MPC624_OSR1 | MPC624_OSR0)
110 #define MPC624_SPEED_440_Hz (MPC624_OSR4 | MPC624_OSR2)
111 #define MPC624_SPEED_220_Hz (MPC624_OSR4 | MPC624_OSR2 | MPC624_OSR0)
112 #define MPC624_SPEED_110_Hz (MPC624_OSR4 | MPC624_OSR2 | MPC624_OSR1)
113 #define MPC624_SPEED_55_Hz \
114 (MPC624_OSR4 | MPC624_OSR2 | MPC624_OSR1 | MPC624_OSR0)
115 #define MPC624_SPEED_27_5_Hz (MPC624_OSR4 | MPC624_OSR3)
116 #define MPC624_SPEED_13_75_Hz (MPC624_OSR4 | MPC624_OSR3 | MPC624_OSR0)
117 #define MPC624_SPEED_6_875_Hz \
118 (MPC624_OSR4 | MPC624_OSR3 | MPC624_OSR2 | MPC624_OSR1 | MPC624_OSR0)
119 /* -------------------------------------------------------------------------- */
120 struct mpc624_private {
122 /* set by mpc624_attach() from driver's parameters */
123 unsigned long int ulConvertionRate;
126 /* -------------------------------------------------------------------------- */
127 static const struct comedi_lrange range_mpc624_bipolar1 = {
130 /* BIP_RANGE(1.01) this is correct, */
131 /* but my MPC-624 actually seems to have a range of 2.02 */
132 BIP_RANGE(2.02)
136 static const struct comedi_lrange range_mpc624_bipolar10 = {
139 /* BIP_RANGE(10.1) this is correct, */
140 /* but my MPC-624 actually seems to have a range of 20.2 */
141 BIP_RANGE(20.2)
145 /* Timeout 200ms */
146 #define TIMEOUT 200
148 static int mpc624_ai_rinsn(struct comedi_device *dev,
149 struct comedi_subdevice *s, struct comedi_insn *insn,
150 unsigned int *data)
152 struct mpc624_private *devpriv = dev->private;
153 int n, i;
154 unsigned long int data_in, data_out;
155 unsigned char ucPort;
158 * WARNING:
159 * We always write 0 to GNSWA bit, so the channel range is +-/10.1Vdc
161 outb(insn->chanspec, dev->iobase + MPC624_GNMUXCH);
162 /* printk("Channel %d:\n", insn->chanspec); */
163 if (!insn->n) {
164 printk(KERN_INFO "MPC624: Warning, no data to acquire\n");
165 return 0;
168 for (n = 0; n < insn->n; n++) {
169 /* Trigger the conversion */
170 outb(MPC624_ADSCK, dev->iobase + MPC624_ADC);
171 udelay(1);
172 outb(MPC624_ADCS | MPC624_ADSCK, dev->iobase + MPC624_ADC);
173 udelay(1);
174 outb(0, dev->iobase + MPC624_ADC);
175 udelay(1);
177 /* Wait for the conversion to end */
178 for (i = 0; i < TIMEOUT; i++) {
179 ucPort = inb(dev->iobase + MPC624_ADC);
180 if (ucPort & MPC624_ADBUSY)
181 udelay(1000);
182 else
183 break;
185 if (i == TIMEOUT) {
186 printk(KERN_ERR "MPC624: timeout (%dms)\n", TIMEOUT);
187 data[n] = 0;
188 return -ETIMEDOUT;
190 /* Start reading data */
191 data_in = 0;
192 data_out = devpriv->ulConvertionRate;
193 udelay(1);
194 for (i = 0; i < 32; i++) {
195 /* Set the clock low */
196 outb(0, dev->iobase + MPC624_ADC);
197 udelay(1);
199 if (data_out & (1 << 31)) { /* the next bit is a 1 */
200 /* Set the ADSDI line (send to MPC624) */
201 outb(MPC624_ADSDI, dev->iobase + MPC624_ADC);
202 udelay(1);
203 /* Set the clock high */
204 outb(MPC624_ADSCK | MPC624_ADSDI,
205 dev->iobase + MPC624_ADC);
206 } else { /* the next bit is a 0 */
208 /* Set the ADSDI line (send to MPC624) */
209 outb(0, dev->iobase + MPC624_ADC);
210 udelay(1);
211 /* Set the clock high */
212 outb(MPC624_ADSCK, dev->iobase + MPC624_ADC);
214 /* Read ADSDO on high clock (receive from MPC624) */
215 udelay(1);
216 data_in <<= 1;
217 data_in |=
218 (inb(dev->iobase + MPC624_ADC) & MPC624_ADSDO) >> 4;
219 udelay(1);
221 data_out <<= 1;
225 * Received 32-bit long value consist of:
226 * 31: EOC -
227 * (End Of Transmission) bit - should be 0
228 * 30: DMY
229 * (Dummy) bit - should be 0
230 * 29: SIG
231 * (Sign) bit- 1 if the voltage is positive,
232 * 0 if negative
233 * 28: MSB
234 * (Most Significant Bit) - the first bit of
235 * the conversion result
236 * ....
237 * 05: LSB
238 * (Least Significant Bit)- the last bit of the
239 * conversion result
240 * 04-00: sub-LSB
241 * - sub-LSBs are basically noise, but when
242 * averaged properly, they can increase conversion
243 * precision up to 29 bits; they can be discarded
244 * without loss of resolution.
247 if (data_in & MPC624_EOC_BIT)
248 printk(KERN_INFO "MPC624:EOC bit is set (data_in=%lu)!",
249 data_in);
250 if (data_in & MPC624_DMY_BIT)
251 printk(KERN_INFO "MPC624:DMY bit is set (data_in=%lu)!",
252 data_in);
253 if (data_in & MPC624_SGN_BIT) { /* Volatge is positive */
255 * comedi operates on unsigned numbers, so mask off EOC
256 * and DMY and don't clear the SGN bit
258 data_in &= 0x3FFFFFFF;
259 data[n] = data_in;
260 } else { /* The voltage is negative */
262 * data_in contains a number in 30-bit two's complement
263 * code and we must deal with it
265 data_in |= MPC624_SGN_BIT;
266 data_in = ~data_in;
267 data_in += 1;
268 data_in &= ~(MPC624_EOC_BIT | MPC624_DMY_BIT);
269 /* clear EOC and DMY bits */
270 data_in = 0x20000000 - data_in;
271 data[n] = data_in;
275 /* Return the number of samples read/written */
276 return n;
279 static int mpc624_attach(struct comedi_device *dev, struct comedi_devconfig *it)
281 struct mpc624_private *devpriv;
282 struct comedi_subdevice *s;
283 int ret;
285 ret = comedi_request_region(dev, it->options[0], MPC624_SIZE);
286 if (ret)
287 return ret;
289 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
290 if (!devpriv)
291 return -ENOMEM;
292 dev->private = devpriv;
294 switch (it->options[1]) {
295 case 0:
296 devpriv->ulConvertionRate = MPC624_SPEED_3_52_kHz;
297 break;
298 case 1:
299 devpriv->ulConvertionRate = MPC624_SPEED_1_76_kHz;
300 break;
301 case 2:
302 devpriv->ulConvertionRate = MPC624_SPEED_880_Hz;
303 break;
304 case 3:
305 devpriv->ulConvertionRate = MPC624_SPEED_440_Hz;
306 break;
307 case 4:
308 devpriv->ulConvertionRate = MPC624_SPEED_220_Hz;
309 break;
310 case 5:
311 devpriv->ulConvertionRate = MPC624_SPEED_110_Hz;
312 break;
313 case 6:
314 devpriv->ulConvertionRate = MPC624_SPEED_55_Hz;
315 break;
316 case 7:
317 devpriv->ulConvertionRate = MPC624_SPEED_27_5_Hz;
318 break;
319 case 8:
320 devpriv->ulConvertionRate = MPC624_SPEED_13_75_Hz;
321 break;
322 case 9:
323 devpriv->ulConvertionRate = MPC624_SPEED_6_875_Hz;
324 break;
325 default:
326 devpriv->ulConvertionRate = MPC624_SPEED_3_52_kHz;
329 ret = comedi_alloc_subdevices(dev, 1);
330 if (ret)
331 return ret;
333 s = &dev->subdevices[0];
334 s->type = COMEDI_SUBD_AI;
335 s->subdev_flags = SDF_READABLE | SDF_DIFF;
336 s->n_chan = 8;
337 switch (it->options[1]) {
338 default:
339 s->maxdata = 0x3FFFFFFF;
342 switch (it->options[1]) {
343 case 0:
344 s->range_table = &range_mpc624_bipolar1;
345 break;
346 default:
347 s->range_table = &range_mpc624_bipolar10;
349 s->len_chanlist = 1;
350 s->insn_read = mpc624_ai_rinsn;
352 return 1;
355 static struct comedi_driver mpc624_driver = {
356 .driver_name = "mpc624",
357 .module = THIS_MODULE,
358 .attach = mpc624_attach,
359 .detach = comedi_legacy_detach,
361 module_comedi_driver(mpc624_driver);
363 MODULE_AUTHOR("Comedi http://www.comedi.org");
364 MODULE_DESCRIPTION("Comedi low-level driver");
365 MODULE_LICENSE("GPL");