1 // SPDX-License-Identifier: GPL-2.0+
4 * Hardware driver for a Micro/sys inc. MPC-624 PC/104 board
6 * COMEDI - Linux Control and Measurement Device Interface
7 * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
12 * Description: Micro/sys MPC-624 PC/104 board
13 * Devices: [Micro/sys] MPC-624 (mpc624)
14 * Author: Stanislaw Raczynski <sraczynski@op.pl>
15 * Updated: Thu, 15 Sep 2005 12:01:18 +0200
18 * The Micro/sys MPC-624 board is based on the LTC2440 24-bit sigma-delta
21 * Subdevices supported by the driver:
22 * - Analog In: supported
23 * - Digital I/O: not supported
24 * - LEDs: not supported
25 * - EEPROM: not supported
27 * Configuration Options:
28 * [0] - I/O base address
29 * [1] - conversion rate
30 * Conversion rate RMS noise Effective Number Of Bits
39 * 8 13.75Hz 250nV 24.4
40 * 9 6.875Hz 200nV 24.6
46 #include <linux/module.h>
47 #include <linux/comedi/comedidev.h>
48 #include <linux/delay.h>
50 /* Offsets of different ports */
51 #define MPC624_MASTER_CONTROL 0 /* not used */
52 #define MPC624_GNMUXCH 1 /* Gain, Mux, Channel of ADC */
53 #define MPC624_ADC 2 /* read/write to/from ADC */
54 #define MPC624_EE 3 /* read/write to/from serial EEPROM via I2C */
55 #define MPC624_LEDS 4 /* write to LEDs */
56 #define MPC624_DIO 5 /* read/write to/from digital I/O ports */
57 #define MPC624_IRQ_MASK 6 /* IRQ masking enable/disable */
59 /* Register bits' names */
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)
67 /* 32-bit output value bits' names */
68 #define MPC624_EOC_BIT BIT(31)
69 #define MPC624_DMY_BIT BIT(30)
70 #define MPC624_SGN_BIT BIT(29)
72 /* SDI Speed/Resolution Programming bits */
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)
85 struct mpc624_private {
86 unsigned int ai_speed;
89 /* -------------------------------------------------------------------------- */
90 static const struct comedi_lrange range_mpc624_bipolar1 = {
93 /* BIP_RANGE(1.01) this is correct, */
94 /* but my MPC-624 actually seems to have a range of 2.02 */
99 static const struct comedi_lrange range_mpc624_bipolar10 = {
102 /* BIP_RANGE(10.1) this is correct, */
103 /* but my MPC-624 actually seems to have a range of 20.2 */
108 static unsigned int mpc624_ai_get_sample(struct comedi_device *dev,
109 struct comedi_subdevice *s)
111 struct mpc624_private *devpriv = dev->private;
112 unsigned int data_out = devpriv->ai_speed;
113 unsigned int data_in = 0;
117 /* Start reading data */
119 for (i = 0; i < 32; i++) {
120 /* Set the clock low */
121 outb(0, dev->iobase + MPC624_ADC);
124 /* Set the ADSDI line for the next bit (send to MPC624) */
125 bit = (data_out & BIT(31)) ? MPC624_ADSDI : 0;
126 outb(bit, dev->iobase + MPC624_ADC);
129 /* Set the clock high */
130 outb(MPC624_ADSCK | bit, dev->iobase + MPC624_ADC);
133 /* Read ADSDO on high clock (receive from MPC624) */
135 data_in |= (inb(dev->iobase + MPC624_ADC) & MPC624_ADSDO) >> 4;
142 * Received 32-bit long value consist of:
143 * 31: EOC - (End Of Transmission) bit - should be 0
144 * 30: DMY - (Dummy) bit - should be 0
145 * 29: SIG - (Sign) bit - 1 if positive, 0 if negative
146 * 28: MSB - (Most Significant Bit) - the first bit of the
149 * 05: LSB - (Least Significant Bit)- the last bit of the
151 * 04-00: sub-LSB - sub-LSBs are basically noise, but when
152 * averaged properly, they can increase
153 * conversion precision up to 29 bits;
154 * they can be discarded without loss of
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!");
162 if (data_in & MPC624_SGN_BIT) {
164 * Voltage is positive
166 * comedi operates on unsigned numbers, so mask off EOC
167 * and DMY and don't clear the SGN bit
169 data_in &= 0x3fffffff;
172 * The voltage is negative
174 * data_in contains a number in 30-bit two's complement
175 * code and we must deal with it
177 data_in |= MPC624_SGN_BIT;
180 /* clear EOC and DMY bits */
181 data_in &= ~(MPC624_EOC_BIT | MPC624_DMY_BIT);
182 data_in = 0x20000000 - data_in;
187 static int mpc624_ai_eoc(struct comedi_device *dev,
188 struct comedi_subdevice *s,
189 struct comedi_insn *insn,
190 unsigned long context)
192 unsigned char status;
194 status = inb(dev->iobase + MPC624_ADC);
195 if ((status & MPC624_ADBUSY) == 0)
200 static int mpc624_ai_insn_read(struct comedi_device *dev,
201 struct comedi_subdevice *s,
202 struct comedi_insn *insn,
210 * We always write 0 to GNSWA bit, so the channel range is +-/10.1Vdc
212 outb(insn->chanspec, dev->iobase + MPC624_GNMUXCH);
214 for (i = 0; i < insn->n; i++) {
215 /* Trigger the conversion */
216 outb(MPC624_ADSCK, dev->iobase + MPC624_ADC);
218 outb(MPC624_ADCS | MPC624_ADSCK, dev->iobase + MPC624_ADC);
220 outb(0, dev->iobase + MPC624_ADC);
223 /* Wait for the conversion to end */
224 ret = comedi_timeout(dev, s, insn, mpc624_ai_eoc, 0);
228 data[i] = mpc624_ai_get_sample(dev, s);
234 static int mpc624_attach(struct comedi_device *dev, struct comedi_devconfig *it)
236 struct mpc624_private *devpriv;
237 struct comedi_subdevice *s;
240 ret = comedi_request_region(dev, it->options[0], 0x10);
244 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
248 switch (it->options[1]) {
250 devpriv->ai_speed = MPC624_SPEED_3_52_KHZ;
253 devpriv->ai_speed = MPC624_SPEED_1_76_KHZ;
256 devpriv->ai_speed = MPC624_SPEED_880_HZ;
259 devpriv->ai_speed = MPC624_SPEED_440_HZ;
262 devpriv->ai_speed = MPC624_SPEED_220_HZ;
265 devpriv->ai_speed = MPC624_SPEED_110_HZ;
268 devpriv->ai_speed = MPC624_SPEED_55_HZ;
271 devpriv->ai_speed = MPC624_SPEED_27_5_HZ;
274 devpriv->ai_speed = MPC624_SPEED_13_75_HZ;
277 devpriv->ai_speed = MPC624_SPEED_6_875_HZ;
280 devpriv->ai_speed = MPC624_SPEED_3_52_KHZ;
283 ret = comedi_alloc_subdevices(dev, 1);
287 /* Analog Input subdevice */
288 s = &dev->subdevices[0];
289 s->type = COMEDI_SUBD_AI;
290 s->subdev_flags = SDF_READABLE | SDF_DIFF;
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;
300 static struct comedi_driver mpc624_driver = {
301 .driver_name = "mpc624",
302 .module = THIS_MODULE,
303 .attach = mpc624_attach,
304 .detach = comedi_legacy_detach,
306 module_comedi_driver(mpc624_driver);
308 MODULE_AUTHOR("Comedi https://www.comedi.org");
309 MODULE_DESCRIPTION("Comedi driver for Micro/sys MPC-624 PC/104 board");
310 MODULE_LICENSE("GPL");