xref: /openbmc/linux/drivers/iio/adc/max9611.c (revision dfc53baa)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * iio/adc/max9611.c
4  *
5  * Maxim max9611/max9612 high side current sense amplifier with
6  * 12-bit ADC interface.
7  *
8  * Copyright (C) 2017 Jacopo Mondi
9  */
10 
11 /*
12  * This driver supports input common-mode voltage, current-sense
13  * amplifier with programmable gains and die temperature reading from
14  * Maxim max9611/max9612.
15  *
16  * Op-amp, analog comparator, and watchdog functionalities are not
17  * supported by this driver.
18  */
19 
20 #include <linux/delay.h>
21 #include <linux/i2c.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/module.h>
25 #include <linux/of_device.h>
26 
27 #define DRIVER_NAME			"max9611"
28 
29 /* max9611 register addresses */
30 #define MAX9611_REG_CSA_DATA		0x00
31 #define MAX9611_REG_RS_DATA		0x02
32 #define MAX9611_REG_TEMP_DATA		0x08
33 #define MAX9611_REG_CTRL1		0x0a
34 #define MAX9611_REG_CTRL2		0x0b
35 
36 /* max9611 REG1 mux configuration options */
37 #define MAX9611_MUX_MASK		GENMASK(3, 0)
38 #define MAX9611_MUX_SENSE_1x		0x00
39 #define MAX9611_MUX_SENSE_4x		0x01
40 #define MAX9611_MUX_SENSE_8x		0x02
41 #define MAX9611_INPUT_VOLT		0x03
42 #define MAX9611_MUX_TEMP		0x06
43 
44 /* max9611 voltage (both csa and input) helper macros */
45 #define MAX9611_VOLTAGE_SHIFT		0x04
46 #define MAX9611_VOLTAGE_RAW(_r)		((_r) >> MAX9611_VOLTAGE_SHIFT)
47 
48 /*
49  * max9611 current sense amplifier voltage output:
50  * LSB and offset values depends on selected gain (1x, 4x, 8x)
51  *
52  * GAIN		LSB (nV)	OFFSET (LSB steps)
53  * 1x		107500		1
54  * 4x		26880		1
55  * 8x		13440		3
56  *
57  * The complete formula to calculate current sense voltage is:
58  *     (((adc_read >> 4) - offset) / ((1 / LSB) * 10^-3)
59  */
60 #define MAX9611_CSA_1X_LSB_nV		107500
61 #define MAX9611_CSA_4X_LSB_nV		26880
62 #define MAX9611_CSA_8X_LSB_nV		13440
63 
64 #define MAX9611_CSA_1X_OFFS_RAW		1
65 #define MAX9611_CSA_4X_OFFS_RAW		1
66 #define MAX9611_CSA_8X_OFFS_RAW		3
67 
68 /*
69  * max9611 common input mode (CIM): LSB is 14mV, with 14mV offset at 25 C
70  *
71  * The complete formula to calculate input common voltage is:
72  *     (((adc_read >> 4) * 1000) - offset) / (1 / 14 * 1000)
73  */
74 #define MAX9611_CIM_LSB_mV		14
75 #define MAX9611_CIM_OFFSET_RAW		1
76 
77 /*
78  * max9611 temperature reading: LSB is 480 milli degrees Celsius
79  *
80  * The complete formula to calculate temperature is:
81  *     ((adc_read >> 7) * 1000) / (1 / 480 * 1000)
82  */
83 #define MAX9611_TEMP_MAX_POS		0x7f80
84 #define MAX9611_TEMP_MAX_NEG		0xff80
85 #define MAX9611_TEMP_MIN_NEG		0xd980
86 #define MAX9611_TEMP_MASK		GENMASK(15, 7)
87 #define MAX9611_TEMP_SHIFT		0x07
88 #define MAX9611_TEMP_RAW(_r)		((_r) >> MAX9611_TEMP_SHIFT)
89 #define MAX9611_TEMP_SCALE_NUM		1000000
90 #define MAX9611_TEMP_SCALE_DIV		2083
91 
92 /*
93  * Conversion time is 2 ms (typically) at Ta=25 degreeC
94  * No maximum value is known, so play it safe.
95  */
96 #define MAX9611_CONV_TIME_US_RANGE	3000, 3300
97 
98 struct max9611_dev {
99 	struct device *dev;
100 	struct i2c_client *i2c_client;
101 	struct mutex lock;
102 	unsigned int shunt_resistor_uohm;
103 };
104 
105 enum max9611_conf_ids {
106 	CONF_SENSE_1x,
107 	CONF_SENSE_4x,
108 	CONF_SENSE_8x,
109 	CONF_IN_VOLT,
110 	CONF_TEMP,
111 };
112 
113 /*
114  * max9611_mux_conf - associate ADC mux configuration with register address
115  *		      where data shall be read from
116  */
117 static const unsigned int max9611_mux_conf[][2] = {
118 	[CONF_SENSE_1x]	= { MAX9611_MUX_SENSE_1x, MAX9611_REG_CSA_DATA },
119 	[CONF_SENSE_4x]	= { MAX9611_MUX_SENSE_4x, MAX9611_REG_CSA_DATA },
120 	[CONF_SENSE_8x]	= { MAX9611_MUX_SENSE_8x, MAX9611_REG_CSA_DATA },
121 	[CONF_IN_VOLT]	= { MAX9611_INPUT_VOLT, MAX9611_REG_RS_DATA },
122 	[CONF_TEMP]	= { MAX9611_MUX_TEMP, MAX9611_REG_TEMP_DATA },
123 };
124 
125 enum max9611_csa_gain {
126 	CSA_GAIN_1x = CONF_SENSE_1x,
127 	CSA_GAIN_4x = CONF_SENSE_4x,
128 	CSA_GAIN_8x = CONF_SENSE_8x,
129 };
130 
131 enum max9611_csa_gain_params {
132 	CSA_GAIN_LSB_nV,
133 	CSA_GAIN_OFFS_RAW,
134 };
135 
136 /*
137  * max9611_csa_gain_conf - associate gain multiplier with LSB and
138  *			   offset values.
139  *
140  * Group together parameters associated with configurable gain
141  * on current sense amplifier path to ADC interface.
142  * Current sense read routine adjusts gain until it gets a meaningful
143  * value; use this structure to retrieve the correct LSB and offset values.
144  */
145 static const unsigned int max9611_gain_conf[][2] = {
146 	[CSA_GAIN_1x] = { MAX9611_CSA_1X_LSB_nV, MAX9611_CSA_1X_OFFS_RAW, },
147 	[CSA_GAIN_4x] = { MAX9611_CSA_4X_LSB_nV, MAX9611_CSA_4X_OFFS_RAW, },
148 	[CSA_GAIN_8x] = { MAX9611_CSA_8X_LSB_nV, MAX9611_CSA_8X_OFFS_RAW, },
149 };
150 
151 enum max9611_chan_addrs {
152 	MAX9611_CHAN_VOLTAGE_INPUT,
153 	MAX9611_CHAN_VOLTAGE_SENSE,
154 	MAX9611_CHAN_TEMPERATURE,
155 	MAX9611_CHAN_CURRENT_LOAD,
156 	MAX9611_CHAN_POWER_LOAD,
157 };
158 
159 static const struct iio_chan_spec max9611_channels[] = {
160 	{
161 	  .type			= IIO_TEMP,
162 	  .info_mask_separate	= BIT(IIO_CHAN_INFO_RAW) |
163 				  BIT(IIO_CHAN_INFO_SCALE),
164 	  .address		= MAX9611_CHAN_TEMPERATURE,
165 	},
166 	{
167 	  .type			= IIO_VOLTAGE,
168 	  .info_mask_separate	= BIT(IIO_CHAN_INFO_PROCESSED),
169 	  .address		= MAX9611_CHAN_VOLTAGE_SENSE,
170 	  .indexed		= 1,
171 	  .channel		= 0,
172 	},
173 	{
174 	  .type			= IIO_VOLTAGE,
175 	  .info_mask_separate	= BIT(IIO_CHAN_INFO_RAW)   |
176 				  BIT(IIO_CHAN_INFO_SCALE) |
177 				  BIT(IIO_CHAN_INFO_OFFSET),
178 	  .address		= MAX9611_CHAN_VOLTAGE_INPUT,
179 	  .indexed		= 1,
180 	  .channel		= 1,
181 	},
182 	{
183 	  .type			= IIO_CURRENT,
184 	  .info_mask_separate	= BIT(IIO_CHAN_INFO_PROCESSED),
185 	  .address		= MAX9611_CHAN_CURRENT_LOAD,
186 	},
187 	{
188 	  .type			= IIO_POWER,
189 	  .info_mask_separate	= BIT(IIO_CHAN_INFO_PROCESSED),
190 	  .address		= MAX9611_CHAN_POWER_LOAD
191 	},
192 };
193 
194 /**
195  * max9611_read_single() - read a single value from ADC interface
196  *
197  * Data registers are 16 bit long, spread between two 8 bit registers
198  * with consecutive addresses.
199  * Configure ADC mux first, then read register at address "reg_addr".
200  * The smbus_read_word routine asks for 16 bits and the ADC is kind enough
201  * to return values from "reg_addr" and "reg_addr + 1" consecutively.
202  * Data are transmitted with big-endian ordering: MSB arrives first.
203  *
204  * @max9611: max9611 device
205  * @selector: index for mux and register configuration
206  * @raw_val: the value returned from ADC
207  */
208 static int max9611_read_single(struct max9611_dev *max9611,
209 			       enum max9611_conf_ids selector,
210 			       u16 *raw_val)
211 {
212 	int ret;
213 
214 	u8 mux_conf = max9611_mux_conf[selector][0] & MAX9611_MUX_MASK;
215 	u8 reg_addr = max9611_mux_conf[selector][1];
216 
217 	/*
218 	 * Keep mutex lock held during read-write to avoid mux register
219 	 * (CTRL1) re-configuration.
220 	 */
221 	mutex_lock(&max9611->lock);
222 	ret = i2c_smbus_write_byte_data(max9611->i2c_client,
223 					MAX9611_REG_CTRL1, mux_conf);
224 	if (ret) {
225 		dev_err(max9611->dev, "i2c write byte failed: 0x%2x - 0x%2x\n",
226 			MAX9611_REG_CTRL1, mux_conf);
227 		mutex_unlock(&max9611->lock);
228 		return ret;
229 	}
230 
231 	/* need a delay here to make register configuration stabilize. */
232 
233 	usleep_range(MAX9611_CONV_TIME_US_RANGE);
234 
235 	ret = i2c_smbus_read_word_swapped(max9611->i2c_client, reg_addr);
236 	if (ret < 0) {
237 		dev_err(max9611->dev, "i2c read word from 0x%2x failed\n",
238 			reg_addr);
239 		mutex_unlock(&max9611->lock);
240 		return ret;
241 	}
242 
243 	*raw_val = ret;
244 	mutex_unlock(&max9611->lock);
245 
246 	return 0;
247 }
248 
249 /**
250  * max9611_read_csa_voltage() - read current sense amplifier output voltage
251  *
252  * Current sense amplifier output voltage is read through a configurable
253  * 1x, 4x or 8x gain.
254  * Start with plain 1x gain, and adjust gain control properly until a
255  * meaningful value is read from ADC output.
256  *
257  * @max9611: max9611 device
258  * @adc_raw: raw value read from ADC output
259  * @csa_gain: gain configuration option selector
260  */
261 static int max9611_read_csa_voltage(struct max9611_dev *max9611,
262 				    u16 *adc_raw,
263 				    enum max9611_csa_gain *csa_gain)
264 {
265 	enum max9611_conf_ids gain_selectors[] = {
266 		CONF_SENSE_1x,
267 		CONF_SENSE_4x,
268 		CONF_SENSE_8x
269 	};
270 	unsigned int i;
271 	int ret;
272 
273 	for (i = 0; i < ARRAY_SIZE(gain_selectors); ++i) {
274 		ret = max9611_read_single(max9611, gain_selectors[i], adc_raw);
275 		if (ret)
276 			return ret;
277 
278 		if (*adc_raw > 0) {
279 			*csa_gain = (enum max9611_csa_gain)gain_selectors[i];
280 			return 0;
281 		}
282 	}
283 
284 	return -EIO;
285 }
286 
287 static int max9611_read_raw(struct iio_dev *indio_dev,
288 			    struct iio_chan_spec const *chan,
289 			    int *val, int *val2, long mask)
290 {
291 	struct max9611_dev *dev = iio_priv(indio_dev);
292 	enum max9611_csa_gain gain_selector;
293 	const unsigned int *csa_gain;
294 	u16 adc_data;
295 	int ret;
296 
297 	switch (mask) {
298 	case IIO_CHAN_INFO_RAW:
299 
300 		switch (chan->address) {
301 		case MAX9611_CHAN_TEMPERATURE:
302 			ret = max9611_read_single(dev, CONF_TEMP,
303 						  &adc_data);
304 			if (ret)
305 				return -EINVAL;
306 
307 			*val = MAX9611_TEMP_RAW(adc_data);
308 			return IIO_VAL_INT;
309 
310 		case MAX9611_CHAN_VOLTAGE_INPUT:
311 			ret = max9611_read_single(dev, CONF_IN_VOLT,
312 						  &adc_data);
313 			if (ret)
314 				return -EINVAL;
315 
316 			*val = MAX9611_VOLTAGE_RAW(adc_data);
317 			return IIO_VAL_INT;
318 		}
319 
320 		break;
321 
322 	case IIO_CHAN_INFO_OFFSET:
323 		/* MAX9611_CHAN_VOLTAGE_INPUT */
324 		*val = MAX9611_CIM_OFFSET_RAW;
325 
326 		return IIO_VAL_INT;
327 
328 	case IIO_CHAN_INFO_SCALE:
329 
330 		switch (chan->address) {
331 		case MAX9611_CHAN_TEMPERATURE:
332 			*val = MAX9611_TEMP_SCALE_NUM;
333 			*val2 = MAX9611_TEMP_SCALE_DIV;
334 
335 			return IIO_VAL_FRACTIONAL;
336 
337 		case MAX9611_CHAN_VOLTAGE_INPUT:
338 			*val = MAX9611_CIM_LSB_mV;
339 
340 			return IIO_VAL_INT;
341 		}
342 
343 		break;
344 
345 	case IIO_CHAN_INFO_PROCESSED:
346 
347 		switch (chan->address) {
348 		case MAX9611_CHAN_VOLTAGE_SENSE:
349 			/*
350 			 * processed (mV): (raw - offset) * LSB (nV) / 10^6
351 			 *
352 			 * Even if max9611 can output raw csa voltage readings,
353 			 * use a produced value as scale depends on gain.
354 			 */
355 			ret = max9611_read_csa_voltage(dev, &adc_data,
356 						       &gain_selector);
357 			if (ret)
358 				return -EINVAL;
359 
360 			csa_gain = max9611_gain_conf[gain_selector];
361 
362 			adc_data -= csa_gain[CSA_GAIN_OFFS_RAW];
363 			*val = MAX9611_VOLTAGE_RAW(adc_data) *
364 			       csa_gain[CSA_GAIN_LSB_nV];
365 			*val2 = 1000000;
366 
367 			return IIO_VAL_FRACTIONAL;
368 
369 		case MAX9611_CHAN_CURRENT_LOAD:
370 			/* processed (mA): Vcsa (nV) / Rshunt (uOhm)  */
371 			ret = max9611_read_csa_voltage(dev, &adc_data,
372 						       &gain_selector);
373 			if (ret)
374 				return -EINVAL;
375 
376 			csa_gain = max9611_gain_conf[gain_selector];
377 
378 			adc_data -= csa_gain[CSA_GAIN_OFFS_RAW];
379 			*val = MAX9611_VOLTAGE_RAW(adc_data) *
380 			       csa_gain[CSA_GAIN_LSB_nV];
381 			*val2 = dev->shunt_resistor_uohm;
382 
383 			return IIO_VAL_FRACTIONAL;
384 
385 		case MAX9611_CHAN_POWER_LOAD:
386 			/*
387 			 * processed (mW): Vin (mV) * Vcsa (uV) /
388 			 *		   Rshunt (uOhm)
389 			 */
390 			ret = max9611_read_single(dev, CONF_IN_VOLT,
391 						  &adc_data);
392 			if (ret)
393 				return -EINVAL;
394 
395 			adc_data -= MAX9611_CIM_OFFSET_RAW;
396 			*val = MAX9611_VOLTAGE_RAW(adc_data) *
397 			       MAX9611_CIM_LSB_mV;
398 
399 			ret = max9611_read_csa_voltage(dev, &adc_data,
400 						       &gain_selector);
401 			if (ret)
402 				return -EINVAL;
403 
404 			csa_gain = max9611_gain_conf[gain_selector];
405 
406 			/* divide by 10^3 here to avoid 32bit overflow */
407 			adc_data -= csa_gain[CSA_GAIN_OFFS_RAW];
408 			*val *= MAX9611_VOLTAGE_RAW(adc_data) *
409 				csa_gain[CSA_GAIN_LSB_nV] / 1000;
410 			*val2 = dev->shunt_resistor_uohm;
411 
412 			return IIO_VAL_FRACTIONAL;
413 		}
414 
415 		break;
416 	}
417 
418 	return -EINVAL;
419 }
420 
421 static ssize_t max9611_shunt_resistor_show(struct device *dev,
422 					   struct device_attribute *attr,
423 					   char *buf)
424 {
425 	struct max9611_dev *max9611 = iio_priv(dev_to_iio_dev(dev));
426 	unsigned int i, r;
427 
428 	i = max9611->shunt_resistor_uohm / 1000000;
429 	r = max9611->shunt_resistor_uohm % 1000000;
430 
431 	return sprintf(buf, "%u.%06u\n", i, r);
432 }
433 
434 static IIO_DEVICE_ATTR(in_power_shunt_resistor, 0444,
435 		       max9611_shunt_resistor_show, NULL, 0);
436 static IIO_DEVICE_ATTR(in_current_shunt_resistor, 0444,
437 		       max9611_shunt_resistor_show, NULL, 0);
438 
439 static struct attribute *max9611_attributes[] = {
440 	&iio_dev_attr_in_power_shunt_resistor.dev_attr.attr,
441 	&iio_dev_attr_in_current_shunt_resistor.dev_attr.attr,
442 	NULL,
443 };
444 
445 static const struct attribute_group max9611_attribute_group = {
446 	.attrs = max9611_attributes,
447 };
448 
449 static const struct iio_info indio_info = {
450 	.read_raw	= max9611_read_raw,
451 	.attrs		= &max9611_attribute_group,
452 };
453 
454 static int max9611_init(struct max9611_dev *max9611)
455 {
456 	struct i2c_client *client = max9611->i2c_client;
457 	u16 regval;
458 	int ret;
459 
460 	if (!i2c_check_functionality(client->adapter,
461 				     I2C_FUNC_SMBUS_WRITE_BYTE	|
462 				     I2C_FUNC_SMBUS_READ_WORD_DATA)) {
463 		dev_err(max9611->dev,
464 			"I2c adapter does not support smbus write_byte or read_word functionalities: aborting probe.\n");
465 		return -EINVAL;
466 	}
467 
468 	/* Make sure die temperature is in range to test communications. */
469 	ret = max9611_read_single(max9611, CONF_TEMP, &regval);
470 	if (ret)
471 		return ret;
472 
473 	regval &= MAX9611_TEMP_MASK;
474 
475 	if ((regval > MAX9611_TEMP_MAX_POS &&
476 	     regval < MAX9611_TEMP_MIN_NEG) ||
477 	     regval > MAX9611_TEMP_MAX_NEG) {
478 		dev_err(max9611->dev,
479 			"Invalid value received from ADC 0x%4x: aborting\n",
480 			regval);
481 		return -EIO;
482 	}
483 
484 	/* Mux shall be zeroed back before applying other configurations */
485 	ret = i2c_smbus_write_byte_data(max9611->i2c_client,
486 					MAX9611_REG_CTRL1, 0);
487 	if (ret) {
488 		dev_err(max9611->dev, "i2c write byte failed: 0x%2x - 0x%2x\n",
489 			MAX9611_REG_CTRL1, 0);
490 		return ret;
491 	}
492 
493 	ret = i2c_smbus_write_byte_data(max9611->i2c_client,
494 					MAX9611_REG_CTRL2, 0);
495 	if (ret) {
496 		dev_err(max9611->dev, "i2c write byte failed: 0x%2x - 0x%2x\n",
497 			MAX9611_REG_CTRL2, 0);
498 		return ret;
499 	}
500 	usleep_range(MAX9611_CONV_TIME_US_RANGE);
501 
502 	return 0;
503 }
504 
505 static const struct of_device_id max9611_of_table[] = {
506 	{.compatible = "maxim,max9611", .data = "max9611"},
507 	{.compatible = "maxim,max9612", .data = "max9612"},
508 	{ },
509 };
510 
511 MODULE_DEVICE_TABLE(of, max9611_of_table);
512 static int max9611_probe(struct i2c_client *client,
513 			 const struct i2c_device_id *id)
514 {
515 	const char * const shunt_res_prop = "shunt-resistor-micro-ohms";
516 	const struct device_node *of_node = client->dev.of_node;
517 	const struct of_device_id *of_id =
518 		of_match_device(max9611_of_table, &client->dev);
519 	struct max9611_dev *max9611;
520 	struct iio_dev *indio_dev;
521 	unsigned int of_shunt;
522 	int ret;
523 
524 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*max9611));
525 	if (!indio_dev)
526 		return -ENOMEM;
527 
528 	i2c_set_clientdata(client, indio_dev);
529 
530 	max9611			= iio_priv(indio_dev);
531 	max9611->dev		= &client->dev;
532 	max9611->i2c_client	= client;
533 	mutex_init(&max9611->lock);
534 
535 	ret = of_property_read_u32(of_node, shunt_res_prop, &of_shunt);
536 	if (ret) {
537 		dev_err(&client->dev,
538 			"Missing %s property for %pOF node\n",
539 			shunt_res_prop, of_node);
540 		return ret;
541 	}
542 	max9611->shunt_resistor_uohm = of_shunt;
543 
544 	ret = max9611_init(max9611);
545 	if (ret)
546 		return ret;
547 
548 	indio_dev->name		= of_id->data;
549 	indio_dev->modes	= INDIO_DIRECT_MODE;
550 	indio_dev->info		= &indio_info;
551 	indio_dev->channels	= max9611_channels;
552 	indio_dev->num_channels	= ARRAY_SIZE(max9611_channels);
553 
554 	return devm_iio_device_register(&client->dev, indio_dev);
555 }
556 
557 static struct i2c_driver max9611_driver = {
558 	.driver = {
559 		   .name = DRIVER_NAME,
560 		   .of_match_table = max9611_of_table,
561 	},
562 	.probe = max9611_probe,
563 };
564 module_i2c_driver(max9611_driver);
565 
566 MODULE_AUTHOR("Jacopo Mondi <jacopo+renesas@jmondi.org>");
567 MODULE_DESCRIPTION("Maxim max9611/12 current sense amplifier with 12bit ADC");
568 MODULE_LICENSE("GPL v2");
569