xref: /openbmc/linux/drivers/comedi/drivers/mpc624.c (revision df0e68c1)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * mpc624.c
4  * Hardware driver for a Micro/sys inc. MPC-624 PC/104 board
5  *
6  * COMEDI - Linux Control and Measurement Device Interface
7  * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8  */
9 
10 /*
11  * Driver: mpc624
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
16  * Status: working
17  *
18  * The Micro/sys MPC-624 board is based on the LTC2440 24-bit sigma-delta
19  * ADC chip.
20  *
21  * Subdevices supported by the driver:
22  * - Analog In:   supported
23  * - Digital I/O: not supported
24  * - LEDs:        not supported
25  * - EEPROM:      not supported
26  *
27  * Configuration Options:
28  *   [0] - I/O base address
29  *   [1] - conversion rate
30  *	   Conversion rate   RMS noise	Effective Number Of Bits
31  *	   0	3.52kHz		23uV		17
32  *	   1	1.76kHz		3.5uV		20
33  *	   2	880Hz		2uV		21.3
34  *	   3	440Hz		1.4uV		21.8
35  *	   4	220Hz		1uV		22.4
36  *	   5	110Hz		750uV		22.9
37  *	   6	55Hz		510nV		23.4
38  *	   7	27.5Hz		375nV		24
39  *	   8	13.75Hz		250nV		24.4
40  *	   9	6.875Hz		200nV		24.6
41  *   [2] - voltage range
42  *	   0	-1.01V .. +1.01V
43  *	   1	-10.1V .. +10.1V
44  */
45 
46 #include <linux/module.h>
47 #include <linux/comedi/comedidev.h>
48 #include <linux/delay.h>
49 
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 */
58 
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)
66 
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)
71 
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)
84 
85 struct mpc624_private {
86 	unsigned int ai_speed;
87 };
88 
89 /* -------------------------------------------------------------------------- */
90 static const struct comedi_lrange range_mpc624_bipolar1 = {
91 	1,
92 	{
93 /* BIP_RANGE(1.01)  this is correct, */
94 	 /*  but my MPC-624 actually seems to have a range of 2.02 */
95 	 BIP_RANGE(2.02)
96 	}
97 };
98 
99 static const struct comedi_lrange range_mpc624_bipolar10 = {
100 	1,
101 	{
102 /* BIP_RANGE(10.1)   this is correct, */
103 	 /*  but my MPC-624 actually seems to have a range of 20.2 */
104 	 BIP_RANGE(20.2)
105 	}
106 };
107 
mpc624_ai_get_sample(struct comedi_device * dev,struct comedi_subdevice * s)108 static 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 	/* Start reading data */
118 	udelay(1);
119 	for (i = 0; i < 32; i++) {
120 		/* Set the clock low */
121 		outb(0, dev->iobase + MPC624_ADC);
122 		udelay(1);
123 
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);
127 		udelay(1);
128 
129 		/* Set the clock high */
130 		outb(MPC624_ADSCK | bit, dev->iobase + MPC624_ADC);
131 		udelay(1);
132 
133 		/* Read ADSDO on high clock (receive from MPC624) */
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 	 * 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
147 	 *					   conversion result
148 	 *	....
149 	 *	05: LSB - (Least Significant Bit)- the last bit of the
150 	 *					   conversion result
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
155 	 *			 resolution.
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 		 * Voltage is positive
165 		 *
166 		 * comedi operates on unsigned numbers, so mask off EOC
167 		 * and DMY and don't clear the SGN bit
168 		 */
169 		data_in &= 0x3fffffff;
170 	} else {
171 		/*
172 		 * The voltage is negative
173 		 *
174 		 * data_in contains a number in 30-bit two's complement
175 		 * code and we must deal with it
176 		 */
177 		data_in |= MPC624_SGN_BIT;
178 		data_in = ~data_in;
179 		data_in += 1;
180 		/* clear EOC and DMY bits */
181 		data_in &= ~(MPC624_EOC_BIT | MPC624_DMY_BIT);
182 		data_in = 0x20000000 - data_in;
183 	}
184 	return data_in;
185 }
186 
mpc624_ai_eoc(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned long context)187 static 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 
mpc624_ai_insn_read(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)200 static 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 	 *  WARNING:
210 	 *  We always write 0 to GNSWA bit, so the channel range is +-/10.1Vdc
211 	 */
212 	outb(insn->chanspec, dev->iobase + MPC624_GNMUXCH);
213 
214 	for (i = 0; i < insn->n; i++) {
215 		/*  Trigger the conversion */
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 		/*  Wait for the conversion to end */
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 
mpc624_attach(struct comedi_device * dev,struct comedi_devconfig * it)234 static 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 	/* Analog Input subdevice */
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 
300 static struct comedi_driver mpc624_driver = {
301 	.driver_name	= "mpc624",
302 	.module		= THIS_MODULE,
303 	.attach		= mpc624_attach,
304 	.detach		= comedi_legacy_detach,
305 };
306 module_comedi_driver(mpc624_driver);
307 
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");
311