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