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