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.
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
26 The Micro/sys MPC-624 board is based on the LTC2440 24-bit sigma-delta
29 Subdevices supported by the driver:
30 - Analog In: supported
31 - Digital I/O: not supported
33 - EEPROM: not supported
35 Configuration Options:
36 [0] - I/O base address
38 Conversion rate RMS noise Effective Number Of Bits
54 #include <linux/module.h>
55 #include "../comedidev.h"
57 #include <linux/delay.h>
59 /* Offsets of different ports */
60 #define MPC624_MASTER_CONTROL 0 /* not used */
61 #define MPC624_GNMUXCH 1 /* Gain, Mux, Channel of ADC */
62 #define MPC624_ADC 2 /* read/write to/from ADC */
63 #define MPC624_EE 3 /* read/write to/from serial EEPROM via I2C */
64 #define MPC624_LEDS 4 /* write to LEDs */
65 #define MPC624_DIO 5 /* read/write to/from digital I/O ports */
66 #define MPC624_IRQ_MASK 6 /* IRQ masking enable/disable */
68 /* Register bits' names */
69 #define MPC624_ADBUSY (1<<5)
70 #define MPC624_ADSDO (1<<4)
71 #define MPC624_ADFO (1<<3)
72 #define MPC624_ADCS (1<<2)
73 #define MPC624_ADSCK (1<<1)
74 #define MPC624_ADSDI (1<<0)
76 /* SDI Speed/Resolution Programming bits */
77 #define MPC624_OSR4 (1<<31)
78 #define MPC624_OSR3 (1<<30)
79 #define MPC624_OSR2 (1<<29)
80 #define MPC624_OSR1 (1<<28)
81 #define MPC624_OSR0 (1<<27)
83 /* 32-bit output value bits' names */
84 #define MPC624_EOC_BIT (1<<31)
85 #define MPC624_DMY_BIT (1<<30)
86 #define MPC624_SGN_BIT (1<<29)
88 /* Conversion speeds */
89 /* OSR4 OSR3 OSR2 OSR1 OSR0 Conversion rate RMS noise ENOB^
90 * X 0 0 0 1 3.52kHz 23uV 17
91 * X 0 0 1 0 1.76kHz 3.5uV 20
92 * X 0 0 1 1 880Hz 2uV 21.3
93 * X 0 1 0 0 440Hz 1.4uV 21.8
94 * X 0 1 0 1 220Hz 1uV 22.4
95 * X 0 1 1 0 110Hz 750uV 22.9
96 * X 0 1 1 1 55Hz 510nV 23.4
97 * X 1 0 0 0 27.5Hz 375nV 24
98 * X 1 0 0 1 13.75Hz 250nV 24.4
99 * X 1 1 1 1 6.875Hz 200nV 24.6
101 * ^ - Effective Number Of Bits
104 #define MPC624_SPEED_3_52_kHz (MPC624_OSR4 | MPC624_OSR0)
105 #define MPC624_SPEED_1_76_kHz (MPC624_OSR4 | MPC624_OSR1)
106 #define MPC624_SPEED_880_Hz (MPC624_OSR4 | MPC624_OSR1 | MPC624_OSR0)
107 #define MPC624_SPEED_440_Hz (MPC624_OSR4 | MPC624_OSR2)
108 #define MPC624_SPEED_220_Hz (MPC624_OSR4 | MPC624_OSR2 | MPC624_OSR0)
109 #define MPC624_SPEED_110_Hz (MPC624_OSR4 | MPC624_OSR2 | MPC624_OSR1)
110 #define MPC624_SPEED_55_Hz \
111 (MPC624_OSR4 | MPC624_OSR2 | MPC624_OSR1 | MPC624_OSR0)
112 #define MPC624_SPEED_27_5_Hz (MPC624_OSR4 | MPC624_OSR3)
113 #define MPC624_SPEED_13_75_Hz (MPC624_OSR4 | MPC624_OSR3 | MPC624_OSR0)
114 #define MPC624_SPEED_6_875_Hz \
115 (MPC624_OSR4 | MPC624_OSR3 | MPC624_OSR2 | MPC624_OSR1 | MPC624_OSR0)
116 /* -------------------------------------------------------------------------- */
117 struct mpc624_private {
118 /* set by mpc624_attach() from driver's parameters */
119 unsigned long int ulConvertionRate;
122 /* -------------------------------------------------------------------------- */
123 static const struct comedi_lrange range_mpc624_bipolar1 = {
126 /* BIP_RANGE(1.01) this is correct, */
127 /* but my MPC-624 actually seems to have a range of 2.02 */
132 static const struct comedi_lrange range_mpc624_bipolar10 = {
135 /* BIP_RANGE(10.1) this is correct, */
136 /* but my MPC-624 actually seems to have a range of 20.2 */
141 static int mpc624_ai_eoc(struct comedi_device *dev,
142 struct comedi_subdevice *s,
143 struct comedi_insn *insn,
144 unsigned long context)
146 unsigned char status;
148 status = inb(dev->iobase + MPC624_ADC);
149 if ((status & MPC624_ADBUSY) == 0)
154 static int mpc624_ai_rinsn(struct comedi_device *dev,
155 struct comedi_subdevice *s, struct comedi_insn *insn,
158 struct mpc624_private *devpriv = dev->private;
160 unsigned long int data_in, data_out;
165 * We always write 0 to GNSWA bit, so the channel range is +-/10.1Vdc
167 outb(insn->chanspec, dev->iobase + MPC624_GNMUXCH);
169 for (n = 0; n < insn->n; n++) {
170 /* Trigger the conversion */
171 outb(MPC624_ADSCK, dev->iobase + MPC624_ADC);
173 outb(MPC624_ADCS | MPC624_ADSCK, dev->iobase + MPC624_ADC);
175 outb(0, dev->iobase + MPC624_ADC);
178 /* Wait for the conversion to end */
179 ret = comedi_timeout(dev, s, insn, mpc624_ai_eoc, 0);
183 /* Start reading data */
185 data_out = devpriv->ulConvertionRate;
187 for (i = 0; i < 32; i++) {
188 /* Set the clock low */
189 outb(0, dev->iobase + MPC624_ADC);
192 if (data_out & (1 << 31)) { /* the next bit is a 1 */
193 /* Set the ADSDI line (send to MPC624) */
194 outb(MPC624_ADSDI, dev->iobase + MPC624_ADC);
196 /* Set the clock high */
197 outb(MPC624_ADSCK | MPC624_ADSDI,
198 dev->iobase + MPC624_ADC);
199 } else { /* the next bit is a 0 */
201 /* Set the ADSDI line (send to MPC624) */
202 outb(0, dev->iobase + MPC624_ADC);
204 /* Set the clock high */
205 outb(MPC624_ADSCK, dev->iobase + MPC624_ADC);
207 /* Read ADSDO on high clock (receive from MPC624) */
211 (inb(dev->iobase + MPC624_ADC) & MPC624_ADSDO) >> 4;
218 * Received 32-bit long value consist of:
220 * (End Of Transmission) bit - should be 0
222 * (Dummy) bit - should be 0
224 * (Sign) bit- 1 if the voltage is positive,
227 * (Most Significant Bit) - the first bit of
228 * the conversion result
231 * (Least Significant Bit)- the last bit of the
234 * - sub-LSBs are basically noise, but when
235 * averaged properly, they can increase conversion
236 * precision up to 29 bits; they can be discarded
237 * without loss of resolution.
240 if (data_in & MPC624_EOC_BIT)
241 dev_dbg(dev->class_dev,
242 "EOC bit is set (data_in=%lu)!", data_in);
243 if (data_in & MPC624_DMY_BIT)
244 dev_dbg(dev->class_dev,
245 "DMY bit is set (data_in=%lu)!", data_in);
246 if (data_in & MPC624_SGN_BIT) { /* Volatge is positive */
248 * comedi operates on unsigned numbers, so mask off EOC
249 * and DMY and don't clear the SGN bit
251 data_in &= 0x3FFFFFFF;
253 } else { /* The voltage is negative */
255 * data_in contains a number in 30-bit two's complement
256 * code and we must deal with it
258 data_in |= MPC624_SGN_BIT;
261 data_in &= ~(MPC624_EOC_BIT | MPC624_DMY_BIT);
262 /* clear EOC and DMY bits */
263 data_in = 0x20000000 - data_in;
268 /* Return the number of samples read/written */
272 static int mpc624_attach(struct comedi_device *dev, struct comedi_devconfig *it)
274 struct mpc624_private *devpriv;
275 struct comedi_subdevice *s;
278 ret = comedi_request_region(dev, it->options[0], 0x10);
282 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
286 switch (it->options[1]) {
288 devpriv->ulConvertionRate = MPC624_SPEED_3_52_kHz;
291 devpriv->ulConvertionRate = MPC624_SPEED_1_76_kHz;
294 devpriv->ulConvertionRate = MPC624_SPEED_880_Hz;
297 devpriv->ulConvertionRate = MPC624_SPEED_440_Hz;
300 devpriv->ulConvertionRate = MPC624_SPEED_220_Hz;
303 devpriv->ulConvertionRate = MPC624_SPEED_110_Hz;
306 devpriv->ulConvertionRate = MPC624_SPEED_55_Hz;
309 devpriv->ulConvertionRate = MPC624_SPEED_27_5_Hz;
312 devpriv->ulConvertionRate = MPC624_SPEED_13_75_Hz;
315 devpriv->ulConvertionRate = MPC624_SPEED_6_875_Hz;
318 devpriv->ulConvertionRate = MPC624_SPEED_3_52_kHz;
321 ret = comedi_alloc_subdevices(dev, 1);
325 s = &dev->subdevices[0];
326 s->type = COMEDI_SUBD_AI;
327 s->subdev_flags = SDF_READABLE | SDF_DIFF;
329 switch (it->options[1]) {
331 s->maxdata = 0x3FFFFFFF;
334 switch (it->options[1]) {
336 s->range_table = &range_mpc624_bipolar1;
339 s->range_table = &range_mpc624_bipolar10;
342 s->insn_read = mpc624_ai_rinsn;
347 static struct comedi_driver mpc624_driver = {
348 .driver_name = "mpc624",
349 .module = THIS_MODULE,
350 .attach = mpc624_attach,
351 .detach = comedi_legacy_detach,
353 module_comedi_driver(mpc624_driver);
355 MODULE_AUTHOR("Comedi http://www.comedi.org");
356 MODULE_DESCRIPTION("Comedi low-level driver");
357 MODULE_LICENSE("GPL");