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