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.
21 * Description: Micro/sys MPC-624 PC/104 board
22 * Devices: [Micro/sys] MPC-624 (mpc624)
23 * Author: Stanislaw Raczynski <sraczynski@op.pl>
24 * Updated: Thu, 15 Sep 2005 12:01:18 +0200
27 * The Micro/sys MPC-624 board is based on the LTC2440 24-bit sigma-delta
30 * Subdevices supported by the driver:
31 * - Analog In: supported
32 * - Digital I/O: not supported
33 * - LEDs: not supported
34 * - EEPROM: not supported
36 * Configuration Options:
37 * [0] - I/O base address
38 * [1] - conversion rate
39 * Conversion rate RMS noise Effective Number Of Bits
48 * 8 13.75Hz 250nV 24.4
49 * 9 6.875Hz 200nV 24.6
55 #include <linux/module.h>
56 #include "../comedidev.h"
58 #include <linux/delay.h>
60 /* Offsets of different ports */
61 #define MPC624_MASTER_CONTROL 0 /* not used */
62 #define MPC624_GNMUXCH 1 /* Gain, Mux, Channel of ADC */
63 #define MPC624_ADC 2 /* read/write to/from ADC */
64 #define MPC624_EE 3 /* read/write to/from serial EEPROM via I2C */
65 #define MPC624_LEDS 4 /* write to LEDs */
66 #define MPC624_DIO 5 /* read/write to/from digital I/O ports */
67 #define MPC624_IRQ_MASK 6 /* IRQ masking enable/disable */
69 /* Register bits' names */
70 #define MPC624_ADBUSY BIT(5)
71 #define MPC624_ADSDO BIT(4)
72 #define MPC624_ADFO BIT(3)
73 #define MPC624_ADCS BIT(2)
74 #define MPC624_ADSCK BIT(1)
75 #define MPC624_ADSDI BIT(0)
77 /* 32-bit output value bits' names */
78 #define MPC624_EOC_BIT BIT(31)
79 #define MPC624_DMY_BIT BIT(30)
80 #define MPC624_SGN_BIT BIT(29)
82 /* SDI Speed/Resolution Programming bits */
83 #define MPC624_OSR(x) (((x) & 0x1f) << 27)
84 #define MPC624_SPEED_3_52_KHZ MPC624_OSR(0x11)
85 #define MPC624_SPEED_1_76_KHZ MPC624_OSR(0x12)
86 #define MPC624_SPEED_880_HZ MPC624_OSR(0x13)
87 #define MPC624_SPEED_440_HZ MPC624_OSR(0x14)
88 #define MPC624_SPEED_220_HZ MPC624_OSR(0x15)
89 #define MPC624_SPEED_110_HZ MPC624_OSR(0x16)
90 #define MPC624_SPEED_55_HZ MPC624_OSR(0x17)
91 #define MPC624_SPEED_27_5_HZ MPC624_OSR(0x18)
92 #define MPC624_SPEED_13_75_HZ MPC624_OSR(0x19)
93 #define MPC624_SPEED_6_875_HZ MPC624_OSR(0x1f)
95 struct mpc624_private {
96 unsigned int ai_speed;
99 /* -------------------------------------------------------------------------- */
100 static const struct comedi_lrange range_mpc624_bipolar1 = {
103 /* BIP_RANGE(1.01) this is correct, */
104 /* but my MPC-624 actually seems to have a range of 2.02 */
109 static const struct comedi_lrange range_mpc624_bipolar10 = {
112 /* BIP_RANGE(10.1) this is correct, */
113 /* but my MPC-624 actually seems to have a range of 20.2 */
118 static unsigned int mpc624_ai_get_sample(struct comedi_device *dev,
119 struct comedi_subdevice *s)
121 struct mpc624_private *devpriv = dev->private;
122 unsigned int data_out = devpriv->ai_speed;
123 unsigned int data_in = 0;
127 /* Start reading data */
129 for (i = 0; i < 32; i++) {
130 /* Set the clock low */
131 outb(0, dev->iobase + MPC624_ADC);
134 /* Set the ADSDI line for the next bit (send to MPC624) */
135 bit = (data_out & BIT(31)) ? MPC624_ADSDI : 0;
136 outb(bit, dev->iobase + MPC624_ADC);
139 /* Set the clock high */
140 outb(MPC624_ADSCK | bit, dev->iobase + MPC624_ADC);
143 /* Read ADSDO on high clock (receive from MPC624) */
145 data_in |= (inb(dev->iobase + MPC624_ADC) & MPC624_ADSDO) >> 4;
152 * Received 32-bit long value consist of:
153 * 31: EOC - (End Of Transmission) bit - should be 0
154 * 30: DMY - (Dummy) bit - should be 0
155 * 29: SIG - (Sign) bit - 1 if positive, 0 if negative
156 * 28: MSB - (Most Significant Bit) - the first bit of the
159 * 05: LSB - (Least Significant Bit)- the last bit of the
161 * 04-00: sub-LSB - sub-LSBs are basically noise, but when
162 * averaged properly, they can increase
163 * conversion precision up to 29 bits;
164 * they can be discarded without loss of
167 if (data_in & MPC624_EOC_BIT)
168 dev_dbg(dev->class_dev, "EOC bit is set!");
169 if (data_in & MPC624_DMY_BIT)
170 dev_dbg(dev->class_dev, "DMY bit is set!");
172 if (data_in & MPC624_SGN_BIT) {
174 * Voltage is positive
176 * comedi operates on unsigned numbers, so mask off EOC
177 * and DMY and don't clear the SGN bit
179 data_in &= 0x3fffffff;
182 * The voltage is negative
184 * data_in contains a number in 30-bit two's complement
185 * code and we must deal with it
187 data_in |= MPC624_SGN_BIT;
190 /* clear EOC and DMY bits */
191 data_in &= ~(MPC624_EOC_BIT | MPC624_DMY_BIT);
192 data_in = 0x20000000 - data_in;
197 static int mpc624_ai_eoc(struct comedi_device *dev,
198 struct comedi_subdevice *s,
199 struct comedi_insn *insn,
200 unsigned long context)
202 unsigned char status;
204 status = inb(dev->iobase + MPC624_ADC);
205 if ((status & MPC624_ADBUSY) == 0)
210 static int mpc624_ai_insn_read(struct comedi_device *dev,
211 struct comedi_subdevice *s,
212 struct comedi_insn *insn,
220 * We always write 0 to GNSWA bit, so the channel range is +-/10.1Vdc
222 outb(insn->chanspec, dev->iobase + MPC624_GNMUXCH);
224 for (i = 0; i < insn->n; i++) {
225 /* Trigger the conversion */
226 outb(MPC624_ADSCK, dev->iobase + MPC624_ADC);
228 outb(MPC624_ADCS | MPC624_ADSCK, dev->iobase + MPC624_ADC);
230 outb(0, dev->iobase + MPC624_ADC);
233 /* Wait for the conversion to end */
234 ret = comedi_timeout(dev, s, insn, mpc624_ai_eoc, 0);
238 data[i] = mpc624_ai_get_sample(dev, s);
244 static int mpc624_attach(struct comedi_device *dev, struct comedi_devconfig *it)
246 struct mpc624_private *devpriv;
247 struct comedi_subdevice *s;
250 ret = comedi_request_region(dev, it->options[0], 0x10);
254 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
258 switch (it->options[1]) {
260 devpriv->ai_speed = MPC624_SPEED_3_52_KHZ;
263 devpriv->ai_speed = MPC624_SPEED_1_76_KHZ;
266 devpriv->ai_speed = MPC624_SPEED_880_HZ;
269 devpriv->ai_speed = MPC624_SPEED_440_HZ;
272 devpriv->ai_speed = MPC624_SPEED_220_HZ;
275 devpriv->ai_speed = MPC624_SPEED_110_HZ;
278 devpriv->ai_speed = MPC624_SPEED_55_HZ;
281 devpriv->ai_speed = MPC624_SPEED_27_5_HZ;
284 devpriv->ai_speed = MPC624_SPEED_13_75_HZ;
287 devpriv->ai_speed = MPC624_SPEED_6_875_HZ;
290 devpriv->ai_speed = MPC624_SPEED_3_52_KHZ;
293 ret = comedi_alloc_subdevices(dev, 1);
297 /* Analog Input subdevice */
298 s = &dev->subdevices[0];
299 s->type = COMEDI_SUBD_AI;
300 s->subdev_flags = SDF_READABLE | SDF_DIFF;
302 s->maxdata = 0x3fffffff;
303 s->range_table = (it->options[1] == 0) ? &range_mpc624_bipolar1
304 : &range_mpc624_bipolar10;
305 s->insn_read = mpc624_ai_insn_read;
310 static struct comedi_driver mpc624_driver = {
311 .driver_name = "mpc624",
312 .module = THIS_MODULE,
313 .attach = mpc624_attach,
314 .detach = comedi_legacy_detach,
316 module_comedi_driver(mpc624_driver);
318 MODULE_AUTHOR("Comedi http://www.comedi.org");
319 MODULE_DESCRIPTION("Comedi driver for Micro/sys MPC-624 PC/104 board");
320 MODULE_LICENSE("GPL");