xref: /linux/drivers/hwmon/ina238.c (revision 805185b7c7a1069e407b6f7b3bc98e44d415f484)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for Texas Instruments INA238 power monitor chip
4  * Datasheet: https://www.ti.com/product/ina238
5  *
6  * Copyright (C) 2021 Nathan Rossi <nathan.rossi@digi.com>
7  */
8 
9 #include <linux/bitops.h>
10 #include <linux/err.h>
11 #include <linux/hwmon.h>
12 #include <linux/i2c.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/regmap.h>
18 #include <linux/util_macros.h>
19 
20 /* INA238 register definitions */
21 #define INA238_CONFIG			0x0
22 #define INA238_ADC_CONFIG		0x1
23 #define INA238_SHUNT_CALIBRATION	0x2
24 #define SQ52206_SHUNT_TEMPCO		0x3
25 #define INA238_SHUNT_VOLTAGE		0x4
26 #define INA238_BUS_VOLTAGE		0x5
27 #define INA238_DIE_TEMP			0x6
28 #define INA238_CURRENT			0x7
29 #define INA238_POWER			0x8
30 #define SQ52206_ENERGY			0x9
31 #define SQ52206_CHARGE			0xa
32 #define INA238_DIAG_ALERT		0xb
33 #define INA238_SHUNT_OVER_VOLTAGE	0xc
34 #define INA238_SHUNT_UNDER_VOLTAGE	0xd
35 #define INA238_BUS_OVER_VOLTAGE		0xe
36 #define INA238_BUS_UNDER_VOLTAGE	0xf
37 #define INA238_TEMP_LIMIT		0x10
38 #define INA238_POWER_LIMIT		0x11
39 #define SQ52206_POWER_PEAK		0x20
40 #define INA238_DEVICE_ID		0x3f /* not available on INA237 */
41 
42 #define INA238_CONFIG_ADCRANGE		BIT(4)
43 #define SQ52206_CONFIG_ADCRANGE_HIGH	BIT(4)
44 #define SQ52206_CONFIG_ADCRANGE_LOW	BIT(3)
45 
46 #define INA238_DIAG_ALERT_TMPOL		BIT(7)
47 #define INA238_DIAG_ALERT_SHNTOL	BIT(6)
48 #define INA238_DIAG_ALERT_SHNTUL	BIT(5)
49 #define INA238_DIAG_ALERT_BUSOL		BIT(4)
50 #define INA238_DIAG_ALERT_BUSUL		BIT(3)
51 #define INA238_DIAG_ALERT_POL		BIT(2)
52 
53 /* INA238_ADC_CONFIG register field masks and shifts */
54 #define INA238_ADC_CONFIG_MODE_MASK	GENMASK(15, 12)
55 #define INA238_ADC_CONFIG_VBUSCT_SHIFT	9
56 #define INA238_ADC_CONFIG_VBUSCT_MASK	GENMASK(11, 9)
57 #define INA238_ADC_CONFIG_VSHCT_SHIFT	6
58 #define INA238_ADC_CONFIG_VSHCT_MASK	GENMASK(8, 6)
59 #define INA238_ADC_CONFIG_VTCT_SHIFT	3
60 #define INA238_ADC_CONFIG_VTCT_MASK	GENMASK(5, 3)
61 #define INA238_ADC_CONFIG_AVG_SHIFT	0
62 #define INA238_ADC_CONFIG_AVG_MASK	GENMASK(2, 0)
63 
64 #define INA238_REGISTERS		0x20
65 
66 #define INA238_RSHUNT_DEFAULT		2500	/* uOhm */
67 
68 /* Default configuration of device on reset. */
69 #define INA238_CONFIG_DEFAULT		0
70 #define SQ52206_CONFIG_DEFAULT		0x0005
71 /* 16 sample averaging, 1052us conversion time, continuous mode */
72 #define INA238_ADC_CONFIG_DEFAULT	0xfb6a
73 /* Configure alerts to be based on averaged value (SLOWALERT) */
74 #define INA238_DIAG_ALERT_DEFAULT	0x2000
75 #define INA238_DIAG_ALERT_APOL		BIT(12)
76 /*
77  * This driver uses a fixed calibration value in order to scale current/power
78  * based on a fixed shunt resistor value. This allows for conversion within the
79  * device to avoid integer limits whilst current/power accuracy is scaled
80  * relative to the shunt resistor value within the driver. This is similar to
81  * how the ina2xx driver handles current/power scaling.
82  *
83  * To achieve the best possible dynamic range, the value of the shunt voltage
84  * register should match the value of the current register. With that, the shunt
85  * voltage of 0x7fff = 32,767 uV = 163,785 uV matches the maximum current,
86  * and no accuracy is lost. Experiments with a real chip show that this is
87  * achieved by setting the SHUNT_CAL register to a value of 0x1000 = 4,096.
88  * Per datasheet,
89  *  SHUNT_CAL = 819.2 x 10^6 x CURRENT_LSB x Rshunt
90  *            = 819,200,000 x CURRENT_LSB x Rshunt
91  * With SHUNT_CAL set to 4,096, we get
92  *  CURRENT_LSB = 4,096 / (819,200,000 x Rshunt)
93  * Assuming an Rshunt value of 5 mOhm, we get
94  *  CURRENT_LSB = 4,096 / (819,200,000 x 0.005) = 1mA
95  * and thus a dynamic range of 1mA ... 32,767mA, which is sufficient for most
96  * applications. The actual dynamic range is of course determined by the actual
97  * shunt resistor value.
98  *
99  * Power and energy values are scaled accordingly.
100  */
101 #define INA238_CALIBRATION_VALUE	4096
102 #define INA238_FIXED_SHUNT		5000
103 
104 #define INA238_SHUNT_VOLTAGE_LSB	5000	/* 5 uV/lsb, in nV */
105 #define INA238_BUS_VOLTAGE_LSB		3125000	/* 3.125 mV/lsb, in nV */
106 #define SQ52206_BUS_VOLTAGE_LSB		3750000	/* 3.75 mV/lsb, in nV */
107 
108 #define NUNIT_PER_MUNIT		1000000	/* n[AV] -> m[AV] */
109 
110 static const struct regmap_config ina238_regmap_config = {
111 	.max_register = INA238_REGISTERS,
112 	.reg_bits = 8,
113 	.val_bits = 16,
114 };
115 
116 /* Lookup table for conversion times in usec for INA238 family */
117 static const u16 ina238_conv_time[] = {
118 	50, 84, 150, 280, 540, 1052, 2074, 4120,
119 };
120 
121 /* Lookup table for conversion times in usec for SQ52206 */
122 static const u16 sq52206_conv_time[] = {
123 	66, 118, 310, 566, 1070, 2090, 4140, 8230,
124 };
125 
126 /* Lookup table for number of samples used in averaging mode */
127 static const int ina238_avg_samples[] = {
128 	1, 4, 16, 64, 128, 256, 512, 1024,
129 };
130 
131 enum ina238_ids { ina228, ina237, ina238, ina700, ina780, sq52206 };
132 
133 struct ina238_config {
134 	bool has_20bit_voltage_current; /* vshunt, vbus and current are 20-bit fields */
135 	bool has_power_highest;		/* chip detection power peak */
136 	bool has_energy;		/* chip detection energy */
137 	u8 temp_resolution;		/* temperature register resolution in bit */
138 	u16 config_default;		/* Power-on default state */
139 	u32 power_calculate_factor;	/* fixed parameter for power calculation, from datasheet */
140 	u32 bus_voltage_lsb;		/* bus voltage LSB, in nV */
141 	int current_lsb;		/* current LSB, in uA */
142 	const u16 *conv_time;		/* conversion time lookup table */
143 };
144 
145 struct ina238_data {
146 	const struct ina238_config *config;
147 	struct i2c_client *client;
148 	struct regmap *regmap;
149 	u32 rshunt;
150 	int gain;
151 	u32 voltage_lsb[2];		/* shunt, bus voltage LSB, in nV */
152 	int current_lsb;		/* current LSB, in uA */
153 	int power_lsb;			/* power LSB, in uW */
154 	int energy_lsb;			/* energy LSB, in uJ */
155 	u16 adc_config;			/* cached ADC_CONFIG register value */
156 };
157 
158 static const struct ina238_config ina238_config[] = {
159 	[ina228] = {
160 		.has_20bit_voltage_current = true,
161 		.has_energy = true,
162 		.has_power_highest = false,
163 		.power_calculate_factor = 20,
164 		.config_default = INA238_CONFIG_DEFAULT,
165 		.bus_voltage_lsb = INA238_BUS_VOLTAGE_LSB,
166 		.temp_resolution = 16,
167 		.conv_time = ina238_conv_time,
168 	},
169 	[ina237] = {
170 		.has_20bit_voltage_current = false,
171 		.has_energy = false,
172 		.has_power_highest = false,
173 		.power_calculate_factor = 20,
174 		.config_default = INA238_CONFIG_DEFAULT,
175 		.bus_voltage_lsb = INA238_BUS_VOLTAGE_LSB,
176 		.temp_resolution = 12,
177 		.conv_time = ina238_conv_time,
178 	},
179 	[ina238] = {
180 		.has_20bit_voltage_current = false,
181 		.has_energy = false,
182 		.has_power_highest = false,
183 		.power_calculate_factor = 20,
184 		.config_default = INA238_CONFIG_DEFAULT,
185 		.bus_voltage_lsb = INA238_BUS_VOLTAGE_LSB,
186 		.temp_resolution = 12,
187 		.conv_time = ina238_conv_time,
188 	},
189 	[ina700] = {
190 		.has_20bit_voltage_current = false,
191 		.has_energy = true,
192 		.has_power_highest = false,
193 		.power_calculate_factor = 20,
194 		.config_default = INA238_CONFIG_DEFAULT,
195 		.bus_voltage_lsb = INA238_BUS_VOLTAGE_LSB,
196 		.temp_resolution = 12,
197 		.current_lsb = 480,
198 		.conv_time = ina238_conv_time,
199 	},
200 	[ina780] = {
201 		.has_20bit_voltage_current = false,
202 		.has_energy = true,
203 		.has_power_highest = false,
204 		.power_calculate_factor = 20,
205 		.config_default = INA238_CONFIG_DEFAULT,
206 		.bus_voltage_lsb = INA238_BUS_VOLTAGE_LSB,
207 		.temp_resolution = 12,
208 		.current_lsb = 2400,
209 		.conv_time = ina238_conv_time,
210 	},
211 	[sq52206] = {
212 		.has_20bit_voltage_current = false,
213 		.has_energy = true,
214 		.has_power_highest = true,
215 		.power_calculate_factor = 24,
216 		.config_default = SQ52206_CONFIG_DEFAULT,
217 		.bus_voltage_lsb = SQ52206_BUS_VOLTAGE_LSB,
218 		.temp_resolution = 16,
219 		.conv_time = sq52206_conv_time,
220 	},
221 };
222 
223 static int ina238_read_reg24(const struct i2c_client *client, u8 reg, u32 *val)
224 {
225 	u8 data[3];
226 	int err;
227 
228 	/* 24-bit register read */
229 	err = i2c_smbus_read_i2c_block_data(client, reg, 3, data);
230 	if (err < 0)
231 		return err;
232 	if (err != 3)
233 		return -EIO;
234 	*val = (data[0] << 16) | (data[1] << 8) | data[2];
235 
236 	return 0;
237 }
238 
239 static int ina238_read_reg40(const struct i2c_client *client, u8 reg, u64 *val)
240 {
241 	u8 data[5];
242 	u32 low;
243 	int err;
244 
245 	/* 40-bit register read */
246 	err = i2c_smbus_read_i2c_block_data(client, reg, 5, data);
247 	if (err < 0)
248 		return err;
249 	if (err != 5)
250 		return -EIO;
251 	low = (data[1] << 24) | (data[2] << 16) | (data[3] << 8) | data[4];
252 	*val = ((long long)data[0] << 32) | low;
253 
254 	return 0;
255 }
256 
257 static int ina238_read_field_s20(const struct i2c_client *client, u8 reg, s32 *val)
258 {
259 	u32 regval;
260 	int err;
261 
262 	err = ina238_read_reg24(client, reg, &regval);
263 	if (err)
264 		return err;
265 
266 	/* bits 3-0 Reserved, always zero */
267 	regval >>= 4;
268 
269 	*val = sign_extend32(regval, 19);
270 
271 	return 0;
272 }
273 
274 static int ina228_read_voltage(struct ina238_data *data, int channel, long *val)
275 {
276 	int reg = channel ? INA238_BUS_VOLTAGE : INA238_CURRENT;
277 	u32 lsb = data->voltage_lsb[channel];
278 	u32 factor = NUNIT_PER_MUNIT;
279 	int err, regval;
280 
281 	if (data->config->has_20bit_voltage_current) {
282 		err = ina238_read_field_s20(data->client, reg, &regval);
283 		if (err)
284 			return err;
285 		/* Adjust accuracy: LSB in units of 500 pV */
286 		lsb /= 8;
287 		factor *= 2;
288 	} else {
289 		err = regmap_read(data->regmap, reg, &regval);
290 		if (err)
291 			return err;
292 		regval = (s16)regval;
293 	}
294 
295 	*val = DIV_S64_ROUND_CLOSEST((s64)regval * lsb, factor);
296 	return 0;
297 }
298 
299 /* Converting ADC_CONFIG register value to update_interval in usec */
300 static inline u32 ina238_reg_to_interval_us(struct ina238_data *data)
301 {
302 	const u16 *ct = data->config->conv_time;
303 	u32 vbusct = ct[(data->adc_config & INA238_ADC_CONFIG_VBUSCT_MASK) >>
304 			INA238_ADC_CONFIG_VBUSCT_SHIFT];
305 	u32 vshct  = ct[(data->adc_config & INA238_ADC_CONFIG_VSHCT_MASK) >>
306 			INA238_ADC_CONFIG_VSHCT_SHIFT];
307 	u32 vtct   = ct[(data->adc_config & INA238_ADC_CONFIG_VTCT_MASK) >>
308 			INA238_ADC_CONFIG_VTCT_SHIFT];
309 
310 	return vbusct + vshct + vtct;
311 }
312 
313 static inline u32 ina238_samples(struct ina238_data *data)
314 {
315 	return ina238_avg_samples[(data->adc_config & INA238_ADC_CONFIG_AVG_MASK) >>
316 				  INA238_ADC_CONFIG_AVG_SHIFT];
317 }
318 
319 /* Converting update_interval(_us) to a per-field conversion time in usec.
320  * interval_us is the total ADC cycle time including averaging in microseconds.
321  * All three conversion fields (VBUSCT, VSHCT, VTCT) are set equal, so the
322  * per-field time is interval_us / (samples * 3).
323  */
324 static inline u32 ina238_interval_us_to_conv_time(u32 interval_us, u32 samples)
325 {
326 	return DIV_ROUND_CLOSEST_ULL(interval_us, samples * 3);
327 }
328 
329 /* Write a per-field conversion time (in usec) to the ADC_CONFIG register */
330 static int ina238_write_conv_time(struct ina238_data *data, u32 conv_time_us)
331 {
332 	u16 adc_config;
333 	int idx, ret;
334 
335 	idx = find_closest(conv_time_us, data->config->conv_time,
336 			   ARRAY_SIZE(ina238_conv_time));
337 	adc_config = (data->adc_config &
338 		      ~(INA238_ADC_CONFIG_VBUSCT_MASK |
339 			INA238_ADC_CONFIG_VSHCT_MASK |
340 			INA238_ADC_CONFIG_VTCT_MASK)) |
341 		     ((u16)idx << INA238_ADC_CONFIG_VBUSCT_SHIFT) |
342 		     ((u16)idx << INA238_ADC_CONFIG_VSHCT_SHIFT) |
343 		     ((u16)idx << INA238_ADC_CONFIG_VTCT_SHIFT);
344 	ret = regmap_write(data->regmap, INA238_ADC_CONFIG, adc_config);
345 	if (ret)
346 		return ret;
347 	data->adc_config = adc_config;
348 	return 0;
349 }
350 
351 static int ina238_read_chip(struct device *dev, u32 attr, long *val)
352 {
353 	struct ina238_data *data = dev_get_drvdata(dev);
354 
355 	switch (attr) {
356 	case hwmon_chip_samples:
357 		*val = ina238_samples(data);
358 		return 0;
359 	case hwmon_chip_update_interval:
360 		/* Return in msec */
361 		*val = DIV_ROUND_CLOSEST(ina238_reg_to_interval_us(data) *
362 					ina238_samples(data), 1000);
363 		return 0;
364 	case hwmon_chip_update_interval_us:
365 		/* Return in usec */
366 		*val = ina238_reg_to_interval_us(data) * ina238_samples(data);
367 		return 0;
368 	default:
369 		return -EOPNOTSUPP;
370 	}
371 }
372 
373 static int ina238_write_chip(struct device *dev, u32 attr, long val)
374 {
375 	struct ina238_data *data = dev_get_drvdata(dev);
376 	u16 adc_config;
377 	int idx, ret;
378 
379 	switch (attr) {
380 	case hwmon_chip_samples:
381 		idx = find_closest(val, ina238_avg_samples,
382 				   ARRAY_SIZE(ina238_avg_samples));
383 		adc_config = (data->adc_config & ~INA238_ADC_CONFIG_AVG_MASK) |
384 			     (idx << INA238_ADC_CONFIG_AVG_SHIFT);
385 		ret = regmap_write(data->regmap, INA238_ADC_CONFIG, adc_config);
386 		if (ret)
387 			return ret;
388 		data->adc_config = adc_config;
389 		return 0;
390 	case hwmon_chip_update_interval:
391 		/* Convert ms to us before passing to the shared helper */
392 		val = clamp_val(val, 0, INT_MAX / 1000) * 1000;
393 		return ina238_write_conv_time(data,
394 			ina238_interval_us_to_conv_time((u32)val, ina238_samples(data)));
395 	case hwmon_chip_update_interval_us:
396 		val = clamp_val(val, 0, INT_MAX);
397 		return ina238_write_conv_time(data,
398 			ina238_interval_us_to_conv_time((u32)val, ina238_samples(data)));
399 	default:
400 		return -EOPNOTSUPP;
401 	}
402 }
403 
404 static int ina238_read_in(struct device *dev, u32 attr, int channel,
405 			  long *val)
406 {
407 	struct ina238_data *data = dev_get_drvdata(dev);
408 	int reg, mask = 0;
409 	int regval;
410 	int err;
411 
412 	if (attr == hwmon_in_input)
413 		return ina228_read_voltage(data, channel, val);
414 
415 	switch (channel) {
416 	case 0:
417 		switch (attr) {
418 		case hwmon_in_max:
419 			reg = INA238_SHUNT_OVER_VOLTAGE;
420 			break;
421 		case hwmon_in_min:
422 			reg = INA238_SHUNT_UNDER_VOLTAGE;
423 			break;
424 		case hwmon_in_max_alarm:
425 			reg = INA238_DIAG_ALERT;
426 			mask = INA238_DIAG_ALERT_SHNTOL;
427 			break;
428 		case hwmon_in_min_alarm:
429 			reg = INA238_DIAG_ALERT;
430 			mask = INA238_DIAG_ALERT_SHNTUL;
431 			break;
432 		default:
433 			return -EOPNOTSUPP;
434 		}
435 		break;
436 	case 1:
437 		switch (attr) {
438 		case hwmon_in_max:
439 			reg = INA238_BUS_OVER_VOLTAGE;
440 			break;
441 		case hwmon_in_min:
442 			reg = INA238_BUS_UNDER_VOLTAGE;
443 			break;
444 		case hwmon_in_max_alarm:
445 			reg = INA238_DIAG_ALERT;
446 			mask = INA238_DIAG_ALERT_BUSOL;
447 			break;
448 		case hwmon_in_min_alarm:
449 			reg = INA238_DIAG_ALERT;
450 			mask = INA238_DIAG_ALERT_BUSUL;
451 			break;
452 		default:
453 			return -EOPNOTSUPP;
454 		}
455 		break;
456 	default:
457 		return -EOPNOTSUPP;
458 	}
459 
460 	err = regmap_read(data->regmap, reg, &regval);
461 	if (err < 0)
462 		return err;
463 
464 	if (mask)
465 		*val = !!(regval & mask);
466 	else
467 		*val = DIV_S64_ROUND_CLOSEST((s64)(s16)regval * data->voltage_lsb[channel],
468 					     NUNIT_PER_MUNIT);
469 
470 	return 0;
471 }
472 
473 static int ina238_write_in(struct device *dev, u32 attr, int channel, long val)
474 {
475 	struct ina238_data *data = dev_get_drvdata(dev);
476 	static const int low_limits[2] = {-164, 0};
477 	static const int high_limits[2] = {164, 150000};
478 	static const u8 low_regs[2] = {INA238_SHUNT_UNDER_VOLTAGE, INA238_BUS_UNDER_VOLTAGE};
479 	static const u8 high_regs[2] = {INA238_SHUNT_OVER_VOLTAGE, INA238_BUS_OVER_VOLTAGE};
480 	int regval;
481 
482 	/* Initial clamp to avoid overflows */
483 	val = clamp_val(val, low_limits[channel], high_limits[channel]);
484 	val = DIV_S64_ROUND_CLOSEST((s64)val * NUNIT_PER_MUNIT, data->voltage_lsb[channel]);
485 	/* Final clamp to register limits */
486 	regval = clamp_val(val, S16_MIN, S16_MAX) & 0xffff;
487 
488 	switch (attr) {
489 	case hwmon_in_min:
490 		return regmap_write(data->regmap, low_regs[channel], regval);
491 	case hwmon_in_max:
492 		return regmap_write(data->regmap, high_regs[channel], regval);
493 	default:
494 		return -EOPNOTSUPP;
495 	}
496 }
497 
498 static int __ina238_read_curr(struct ina238_data *data, long *val)
499 {
500 	u32 lsb = data->current_lsb;
501 	int err, regval;
502 
503 	if (data->config->has_20bit_voltage_current) {
504 		err = ina238_read_field_s20(data->client, INA238_CURRENT, &regval);
505 		if (err)
506 			return err;
507 		lsb /= 16;	/* Adjust accuracy */
508 	} else {
509 		err = regmap_read(data->regmap, INA238_CURRENT, &regval);
510 		if (err)
511 			return err;
512 		regval = (s16)regval;
513 	}
514 
515 	*val = DIV_S64_ROUND_CLOSEST((s64)regval * lsb, 1000);
516 	return 0;
517 }
518 
519 static int ina238_read_curr(struct device *dev, u32 attr, long *val)
520 {
521 	struct ina238_data *data = dev_get_drvdata(dev);
522 	int reg, mask = 0;
523 	int regval;
524 	int err;
525 
526 	if (attr == hwmon_curr_input)
527 		return __ina238_read_curr(data, val);
528 
529 	switch (attr) {
530 	case hwmon_curr_min:
531 		reg = INA238_SHUNT_UNDER_VOLTAGE;
532 		break;
533 	case hwmon_curr_min_alarm:
534 		reg = INA238_DIAG_ALERT;
535 		mask = INA238_DIAG_ALERT_SHNTUL;
536 		break;
537 	case hwmon_curr_max:
538 		reg = INA238_SHUNT_OVER_VOLTAGE;
539 		break;
540 	case hwmon_curr_max_alarm:
541 		reg = INA238_DIAG_ALERT;
542 		mask = INA238_DIAG_ALERT_SHNTOL;
543 		break;
544 	default:
545 		return -EOPNOTSUPP;
546 	}
547 
548 	err = regmap_read(data->regmap, reg, &regval);
549 	if (err < 0)
550 		return err;
551 
552 	if (mask)
553 		*val = !!(regval & mask);
554 	else
555 		*val = DIV_S64_ROUND_CLOSEST((s64)(s16)regval * data->current_lsb, 1000);
556 
557 	return 0;
558 }
559 
560 static int ina238_write_curr(struct device *dev, u32 attr, long val)
561 {
562 	struct ina238_data *data = dev_get_drvdata(dev);
563 	int regval;
564 
565 	/* Set baseline range to avoid over/underflows */
566 	val = clamp_val(val, -1000000, 1000000);
567 	/* Scale */
568 	val = DIV_ROUND_CLOSEST(val * 1000, data->current_lsb);
569 	/* Clamp to register size */
570 	regval = clamp_val(val, S16_MIN, S16_MAX) & 0xffff;
571 
572 	switch (attr) {
573 	case hwmon_curr_min:
574 		return regmap_write(data->regmap, INA238_SHUNT_UNDER_VOLTAGE,
575 				    regval);
576 	case hwmon_curr_max:
577 		return regmap_write(data->regmap, INA238_SHUNT_OVER_VOLTAGE,
578 				    regval);
579 	default:
580 		return -EOPNOTSUPP;
581 	}
582 }
583 
584 static int ina238_read_power(struct device *dev, u32 attr, long *val)
585 {
586 	struct ina238_data *data = dev_get_drvdata(dev);
587 	long long power;
588 	int regval;
589 	int err;
590 
591 	switch (attr) {
592 	case hwmon_power_input:
593 		err = ina238_read_reg24(data->client, INA238_POWER, &regval);
594 		if (err)
595 			return err;
596 
597 		power = (long long)regval * data->power_lsb;
598 		/* Clamp value to maximum value of long */
599 		*val = clamp_val(power, 0, LONG_MAX);
600 		break;
601 	case hwmon_power_input_highest:
602 		err = ina238_read_reg24(data->client, SQ52206_POWER_PEAK, &regval);
603 		if (err)
604 			return err;
605 
606 		power = (long long)regval * data->power_lsb;
607 		/* Clamp value to maximum value of long */
608 		*val = clamp_val(power, 0, LONG_MAX);
609 		break;
610 	case hwmon_power_max:
611 		err = regmap_read(data->regmap, INA238_POWER_LIMIT, &regval);
612 		if (err)
613 			return err;
614 
615 		/*
616 		 * Truncated 24-bit compare register, lower 8-bits are
617 		 * truncated. Same conversion to/from uW as POWER register.
618 		 */
619 		power = ((long long)regval << 8) * data->power_lsb;
620 		/* Clamp value to maximum value of long */
621 		*val = clamp_val(power, 0, LONG_MAX);
622 		break;
623 	case hwmon_power_max_alarm:
624 		err = regmap_read(data->regmap, INA238_DIAG_ALERT, &regval);
625 		if (err)
626 			return err;
627 
628 		*val = !!(regval & INA238_DIAG_ALERT_POL);
629 		break;
630 	default:
631 		return -EOPNOTSUPP;
632 	}
633 
634 	return 0;
635 }
636 
637 static int ina238_write_power_max(struct device *dev, long val)
638 {
639 	struct ina238_data *data = dev_get_drvdata(dev);
640 
641 	/*
642 	 * Unsigned postive values. Compared against the 24-bit power register,
643 	 * lower 8-bits are truncated. Same conversion to/from uW as POWER
644 	 * register.
645 	 * The first clamp_val() is to establish a baseline to avoid overflows.
646 	 */
647 	val = clamp_val(val, 0, LONG_MAX / 2);
648 	val = DIV_ROUND_CLOSEST(val, data->power_lsb);
649 	val = clamp_val(val >> 8, 0, U16_MAX);
650 
651 	return regmap_write(data->regmap, INA238_POWER_LIMIT, val);
652 }
653 
654 static int ina238_temp_from_reg(s16 regval, u8 resolution)
655 {
656 	return ((regval >> (16 - resolution)) * 1000) >> (resolution - 9);
657 }
658 
659 static int ina238_read_temp(struct device *dev, u32 attr, long *val)
660 {
661 	struct ina238_data *data = dev_get_drvdata(dev);
662 	int regval;
663 	int err;
664 
665 	switch (attr) {
666 	case hwmon_temp_input:
667 		err = regmap_read(data->regmap, INA238_DIE_TEMP, &regval);
668 		if (err)
669 			return err;
670 		*val = ina238_temp_from_reg(regval, data->config->temp_resolution);
671 		break;
672 	case hwmon_temp_max:
673 		err = regmap_read(data->regmap, INA238_TEMP_LIMIT, &regval);
674 		if (err)
675 			return err;
676 		/* Signed, result in mC */
677 		*val = ina238_temp_from_reg(regval, data->config->temp_resolution);
678 		break;
679 	case hwmon_temp_max_alarm:
680 		err = regmap_read(data->regmap, INA238_DIAG_ALERT, &regval);
681 		if (err)
682 			return err;
683 
684 		*val = !!(regval & INA238_DIAG_ALERT_TMPOL);
685 		break;
686 	default:
687 		return -EOPNOTSUPP;
688 	}
689 
690 	return 0;
691 }
692 
693 static u16 ina238_temp_to_reg(long val, u8 resolution)
694 {
695 	int fraction = 1000 - DIV_ROUND_CLOSEST(1000, BIT(resolution - 9));
696 
697 	val = clamp_val(val, -255000 - fraction, 255000 + fraction);
698 
699 	return (DIV_ROUND_CLOSEST(val << (resolution - 9), 1000) << (16 - resolution)) & 0xffff;
700 }
701 
702 static int ina238_write_temp_max(struct device *dev, long val)
703 {
704 	struct ina238_data *data = dev_get_drvdata(dev);
705 	int regval;
706 
707 	regval = ina238_temp_to_reg(val, data->config->temp_resolution);
708 	return regmap_write(data->regmap, INA238_TEMP_LIMIT, regval);
709 }
710 
711 static int ina238_read_energy(struct device *dev, s64 *energy)
712 {
713 	struct ina238_data *data = dev_get_drvdata(dev);
714 	u64 regval;
715 	int ret;
716 
717 	ret = ina238_read_reg40(data->client, SQ52206_ENERGY, &regval);
718 	if (ret)
719 		return ret;
720 
721 	/* result in uJ */
722 	*energy = regval * data->energy_lsb;
723 	return 0;
724 }
725 
726 static int ina238_read(struct device *dev, enum hwmon_sensor_types type,
727 		       u32 attr, int channel, long *val)
728 {
729 	switch (type) {
730 	case hwmon_chip:
731 		return ina238_read_chip(dev, attr, val);
732 	case hwmon_in:
733 		return ina238_read_in(dev, attr, channel, val);
734 	case hwmon_curr:
735 		return ina238_read_curr(dev, attr, val);
736 	case hwmon_power:
737 		return ina238_read_power(dev, attr, val);
738 	case hwmon_energy64:
739 		return ina238_read_energy(dev, (s64 *)val);
740 	case hwmon_temp:
741 		return ina238_read_temp(dev, attr, val);
742 	default:
743 		return -EOPNOTSUPP;
744 	}
745 	return 0;
746 }
747 
748 static int ina238_write(struct device *dev, enum hwmon_sensor_types type,
749 			u32 attr, int channel, long val)
750 {
751 	switch (type) {
752 	case hwmon_chip:
753 		return ina238_write_chip(dev, attr, val);
754 	case hwmon_in:
755 		return ina238_write_in(dev, attr, channel, val);
756 	case hwmon_curr:
757 		return ina238_write_curr(dev, attr, val);
758 	case hwmon_power:
759 		return ina238_write_power_max(dev, val);
760 	case hwmon_temp:
761 		return ina238_write_temp_max(dev, val);
762 	default:
763 		return -EOPNOTSUPP;
764 	}
765 }
766 
767 static umode_t ina238_is_visible(const void *drvdata,
768 				 enum hwmon_sensor_types type,
769 				 u32 attr, int channel)
770 {
771 	const struct ina238_data *data = drvdata;
772 	bool has_power_highest = data->config->has_power_highest;
773 	bool has_energy = data->config->has_energy;
774 
775 	switch (type) {
776 	case hwmon_chip:
777 		switch (attr) {
778 		case hwmon_chip_samples:
779 		case hwmon_chip_update_interval:
780 		case hwmon_chip_update_interval_us:
781 			return 0644;
782 		default:
783 			return 0;
784 		}
785 	case hwmon_in:
786 		switch (attr) {
787 		case hwmon_in_input:
788 		case hwmon_in_max_alarm:
789 		case hwmon_in_min_alarm:
790 			return 0444;
791 		case hwmon_in_max:
792 		case hwmon_in_min:
793 			return 0644;
794 		default:
795 			return 0;
796 		}
797 	case hwmon_curr:
798 		switch (attr) {
799 		case hwmon_curr_input:
800 		case hwmon_curr_max_alarm:
801 		case hwmon_curr_min_alarm:
802 			return 0444;
803 		case hwmon_curr_max:
804 		case hwmon_curr_min:
805 			return 0644;
806 		default:
807 			return 0;
808 		}
809 	case hwmon_power:
810 		switch (attr) {
811 		case hwmon_power_input:
812 		case hwmon_power_max_alarm:
813 			return 0444;
814 		case hwmon_power_max:
815 			return 0644;
816 		case hwmon_power_input_highest:
817 			if (has_power_highest)
818 				return 0444;
819 			return 0;
820 		default:
821 			return 0;
822 		}
823 	case hwmon_energy64:
824 		/* hwmon_energy_input */
825 		if (has_energy)
826 			return 0444;
827 		return 0;
828 	case hwmon_temp:
829 		switch (attr) {
830 		case hwmon_temp_input:
831 		case hwmon_temp_max_alarm:
832 			return 0444;
833 		case hwmon_temp_max:
834 			return 0644;
835 		default:
836 			return 0;
837 		}
838 	default:
839 		return 0;
840 	}
841 }
842 
843 #define INA238_HWMON_IN_CONFIG (HWMON_I_INPUT | \
844 				HWMON_I_MAX | HWMON_I_MAX_ALARM | \
845 				HWMON_I_MIN | HWMON_I_MIN_ALARM)
846 
847 static const struct hwmon_channel_info * const ina238_info[] = {
848 	HWMON_CHANNEL_INFO(chip,
849 			   HWMON_C_SAMPLES | HWMON_C_UPDATE_INTERVAL |
850 			   HWMON_C_UPDATE_INTERVAL_US),
851 	HWMON_CHANNEL_INFO(in,
852 			   /* 0: shunt voltage */
853 			   INA238_HWMON_IN_CONFIG,
854 			   /* 1: bus voltage */
855 			   INA238_HWMON_IN_CONFIG),
856 	HWMON_CHANNEL_INFO(curr,
857 			   /* 0: current through shunt */
858 			   HWMON_C_INPUT | HWMON_C_MIN | HWMON_C_MIN_ALARM |
859 			   HWMON_C_MAX | HWMON_C_MAX_ALARM),
860 	HWMON_CHANNEL_INFO(power,
861 			   /* 0: power */
862 			   HWMON_P_INPUT | HWMON_P_MAX |
863 			   HWMON_P_MAX_ALARM | HWMON_P_INPUT_HIGHEST),
864 	HWMON_CHANNEL_INFO(energy64,
865 			   HWMON_E_INPUT),
866 	HWMON_CHANNEL_INFO(temp,
867 			   /* 0: die temperature */
868 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_ALARM),
869 	NULL
870 };
871 
872 static const struct hwmon_ops ina238_hwmon_ops = {
873 	.is_visible = ina238_is_visible,
874 	.read = ina238_read,
875 	.write = ina238_write,
876 };
877 
878 static const struct hwmon_chip_info ina238_chip_info = {
879 	.ops = &ina238_hwmon_ops,
880 	.info = ina238_info,
881 };
882 
883 static int ina238_probe(struct i2c_client *client)
884 {
885 	struct device *dev = &client->dev;
886 	struct device *hwmon_dev;
887 	struct ina238_data *data;
888 	enum ina238_ids chip;
889 	int config;
890 	int ret;
891 
892 	chip = (uintptr_t)i2c_get_match_data(client);
893 
894 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
895 	if (!data)
896 		return -ENOMEM;
897 
898 	data->client = client;
899 	/* set the device type */
900 	data->config = &ina238_config[chip];
901 
902 	data->regmap = devm_regmap_init_i2c(client, &ina238_regmap_config);
903 	if (IS_ERR(data->regmap)) {
904 		dev_err(dev, "failed to allocate register map\n");
905 		return PTR_ERR(data->regmap);
906 	}
907 
908 	/* Setup CONFIG register */
909 	config = data->config->config_default;
910 	if (data->config->current_lsb) {
911 		data->voltage_lsb[0] = INA238_SHUNT_VOLTAGE_LSB;
912 		data->current_lsb = data->config->current_lsb;
913 	} else {
914 		/* load shunt value */
915 		if (device_property_read_u32(dev, "shunt-resistor", &data->rshunt) < 0)
916 			data->rshunt = INA238_RSHUNT_DEFAULT;
917 		if (data->rshunt == 0) {
918 			dev_err(dev, "invalid shunt resister value %u\n", data->rshunt);
919 			return -EINVAL;
920 		}
921 
922 		/* load shunt gain value */
923 		if (device_property_read_u32(dev, "ti,shunt-gain", &data->gain) < 0)
924 			data->gain = 4;	/* Default of ADCRANGE = 0 */
925 		if (data->gain != 1 && data->gain != 2 && data->gain != 4) {
926 			dev_err(dev, "invalid shunt gain value %u\n", data->gain);
927 			return -EINVAL;
928 		}
929 
930 		/* Setup SHUNT_CALIBRATION register with fixed value */
931 		ret = regmap_write(data->regmap, INA238_SHUNT_CALIBRATION,
932 				   INA238_CALIBRATION_VALUE);
933 		if (ret < 0) {
934 			dev_err(dev, "error configuring the device: %d\n", ret);
935 			return -ENODEV;
936 		}
937 		if (chip == sq52206) {
938 			if (data->gain == 1)		/* ADCRANGE = 10/11 is /1 */
939 				config |= SQ52206_CONFIG_ADCRANGE_HIGH;
940 			else if (data->gain == 2)	/* ADCRANGE = 01 is /2 */
941 				config |= SQ52206_CONFIG_ADCRANGE_LOW;
942 		} else if (data->gain == 1) {		/* ADCRANGE = 1 is /1 */
943 			config |= INA238_CONFIG_ADCRANGE;
944 		}
945 		data->voltage_lsb[0] = INA238_SHUNT_VOLTAGE_LSB * data->gain / 4;
946 		data->current_lsb = DIV_U64_ROUND_CLOSEST(250ULL * INA238_FIXED_SHUNT * data->gain,
947 							  data->rshunt);
948 	}
949 
950 	ret = regmap_write(data->regmap, INA238_CONFIG, config);
951 	if (ret < 0) {
952 		dev_err(dev, "error configuring the device: %d\n", ret);
953 		return -ENODEV;
954 	}
955 
956 	/* Setup ADC_CONFIG register */
957 	data->adc_config = INA238_ADC_CONFIG_DEFAULT;
958 	ret = regmap_write(data->regmap, INA238_ADC_CONFIG, data->adc_config);
959 	if (ret < 0) {
960 		dev_err(dev, "error configuring the device: %d\n", ret);
961 		return -ENODEV;
962 	}
963 
964 	/* Setup alert/alarm configuration */
965 	config = INA238_DIAG_ALERT_DEFAULT;
966 	if (device_property_read_bool(dev, "ti,alert-polarity-active-high"))
967 		config |= INA238_DIAG_ALERT_APOL;
968 
969 	ret = regmap_write(data->regmap, INA238_DIAG_ALERT, config);
970 	if (ret < 0) {
971 		dev_err(dev, "error configuring the device: %d\n", ret);
972 		return -ENODEV;
973 	}
974 
975 	data->voltage_lsb[1] = data->config->bus_voltage_lsb;
976 
977 	data->power_lsb = DIV_ROUND_CLOSEST(data->current_lsb *
978 					    data->config->power_calculate_factor,
979 					    100);
980 
981 	data->energy_lsb = data->power_lsb * 16;
982 
983 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data,
984 							 &ina238_chip_info, NULL);
985 	if (IS_ERR(hwmon_dev))
986 		return PTR_ERR(hwmon_dev);
987 
988 	if (data->rshunt)
989 		dev_info(dev, "power monitor %s (Rshunt = %u uOhm, gain = %u)\n",
990 			 client->name, data->rshunt, data->gain);
991 
992 	return 0;
993 }
994 
995 static const struct i2c_device_id ina238_id[] = {
996 	{ .name = "ina228", .driver_data = ina228 },
997 	{ .name = "ina237", .driver_data = ina237 },
998 	{ .name = "ina238", .driver_data = ina238 },
999 	{ .name = "ina700", .driver_data = ina700 },
1000 	{ .name = "ina780", .driver_data = ina780 },
1001 	{ .name = "sq52206", .driver_data = sq52206 },
1002 	{ }
1003 };
1004 MODULE_DEVICE_TABLE(i2c, ina238_id);
1005 
1006 static const struct of_device_id __maybe_unused ina238_of_match[] = {
1007 	{
1008 		.compatible = "ti,ina228",
1009 		.data = (void *)ina228
1010 	},
1011 	{
1012 		.compatible = "ti,ina237",
1013 		.data = (void *)ina237
1014 	},
1015 	{
1016 		.compatible = "ti,ina238",
1017 		.data = (void *)ina238
1018 	},
1019 	{
1020 		.compatible = "ti,ina700",
1021 		.data = (void *)ina700
1022 	},
1023 	{
1024 		.compatible = "ti,ina780",
1025 		.data = (void *)ina780
1026 	},
1027 	{
1028 		.compatible = "silergy,sq52206",
1029 		.data = (void *)sq52206
1030 	},
1031 	{ }
1032 };
1033 MODULE_DEVICE_TABLE(of, ina238_of_match);
1034 
1035 static struct i2c_driver ina238_driver = {
1036 	.driver = {
1037 		.name	= "ina238",
1038 		.of_match_table = of_match_ptr(ina238_of_match),
1039 	},
1040 	.probe		= ina238_probe,
1041 	.id_table	= ina238_id,
1042 };
1043 
1044 module_i2c_driver(ina238_driver);
1045 
1046 MODULE_AUTHOR("Nathan Rossi <nathan.rossi@digi.com>");
1047 MODULE_DESCRIPTION("ina238 driver");
1048 MODULE_LICENSE("GPL");
1049