xref: /linux/drivers/hwmon/tmp513.c (revision 72bea132f3680ee51e7ed2cee62892b6f5121909)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for Texas Instruments TMP512, TMP513 power monitor chips
4  *
5  * TMP513:
6  * Thermal/Power Management with Triple Remote and
7  * Local Temperature Sensor and Current Shunt Monitor
8  * Datasheet: https://www.ti.com/lit/gpn/tmp513
9  *
10  * TMP512:
11  * Thermal/Power Management with Dual Remote
12  *	and Local Temperature Sensor and Current Shunt Monitor
13  * Datasheet: https://www.ti.com/lit/gpn/tmp512
14  *
15  * Copyright (C) 2019 Eric Tremblay <etremblay@distech-controls.com>
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; version 2 of the License.
20  */
21 
22 #include <linux/bitops.h>
23 #include <linux/bug.h>
24 #include <linux/device.h>
25 #include <linux/err.h>
26 #include <linux/hwmon.h>
27 #include <linux/i2c.h>
28 #include <linux/init.h>
29 #include <linux/math.h>
30 #include <linux/module.h>
31 #include <linux/property.h>
32 #include <linux/regmap.h>
33 #include <linux/slab.h>
34 #include <linux/types.h>
35 #include <linux/units.h>
36 
37 // Common register definition
38 #define TMP51X_SHUNT_CONFIG		0x00
39 #define TMP51X_TEMP_CONFIG		0x01
40 #define TMP51X_STATUS			0x02
41 #define TMP51X_SMBUS_ALERT		0x03
42 #define TMP51X_SHUNT_CURRENT_RESULT	0x04
43 #define TMP51X_BUS_VOLTAGE_RESULT	0x05
44 #define TMP51X_POWER_RESULT		0x06
45 #define TMP51X_BUS_CURRENT_RESULT	0x07
46 #define TMP51X_LOCAL_TEMP_RESULT	0x08
47 #define TMP51X_REMOTE_TEMP_RESULT_1	0x09
48 #define TMP51X_REMOTE_TEMP_RESULT_2	0x0A
49 #define TMP51X_SHUNT_CURRENT_H_LIMIT	0x0C
50 #define TMP51X_SHUNT_CURRENT_L_LIMIT	0x0D
51 #define TMP51X_BUS_VOLTAGE_H_LIMIT	0x0E
52 #define TMP51X_BUS_VOLTAGE_L_LIMIT	0x0F
53 #define TMP51X_POWER_LIMIT		0x10
54 #define TMP51X_LOCAL_TEMP_LIMIT	0x11
55 #define TMP51X_REMOTE_TEMP_LIMIT_1	0x12
56 #define TMP51X_REMOTE_TEMP_LIMIT_2	0x13
57 #define TMP51X_SHUNT_CALIBRATION	0x15
58 #define TMP51X_N_FACTOR_AND_HYST_1	0x16
59 #define TMP51X_N_FACTOR_2		0x17
60 #define TMP51X_MAN_ID_REG		0xFE
61 #define TMP51X_DEVICE_ID_REG		0xFF
62 
63 // TMP513 specific register definition
64 #define TMP513_REMOTE_TEMP_RESULT_3	0x0B
65 #define TMP513_REMOTE_TEMP_LIMIT_3	0x14
66 #define TMP513_N_FACTOR_3		0x18
67 
68 // Common attrs, and NULL
69 #define TMP51X_MANUFACTURER_ID		0x55FF
70 
71 #define TMP512_DEVICE_ID		0x22FF
72 #define TMP513_DEVICE_ID		0x23FF
73 
74 // Default config
75 #define TMP51X_SHUNT_CONFIG_DEFAULT	0x399F
76 #define TMP51X_SHUNT_VALUE_DEFAULT	1000
77 #define TMP51X_VBUS_RANGE_DEFAULT	TMP51X_VBUS_RANGE_32V
78 #define TMP51X_PGA_DEFAULT		8
79 #define TMP51X_MAX_REGISTER_ADDR	0xFF
80 
81 // Mask and shift
82 #define CURRENT_SENSE_VOLTAGE_320_MASK	0x1800
83 #define CURRENT_SENSE_VOLTAGE_160_MASK	0x1000
84 #define CURRENT_SENSE_VOLTAGE_80_MASK	0x0800
85 #define CURRENT_SENSE_VOLTAGE_40_MASK	0
86 
87 #define TMP51X_BUS_VOLTAGE_MASK		0x2000
88 #define TMP51X_NFACTOR_MASK		0xFF00
89 #define TMP51X_HYST_MASK		0x00FF
90 
91 #define TMP51X_BUS_VOLTAGE_SHIFT	3
92 #define TMP51X_TEMP_SHIFT		3
93 
94 // Alarms
95 #define TMP51X_SHUNT_CURRENT_H_LIMIT_POS	15
96 #define TMP51X_SHUNT_CURRENT_L_LIMIT_POS	14
97 #define TMP51X_BUS_VOLTAGE_H_LIMIT_POS		13
98 #define TMP51X_BUS_VOLTAGE_L_LIMIT_POS		12
99 #define TMP51X_POWER_LIMIT_POS			11
100 #define TMP51X_LOCAL_TEMP_LIMIT_POS		10
101 #define TMP51X_REMOTE_TEMP_LIMIT_1_POS		9
102 #define TMP51X_REMOTE_TEMP_LIMIT_2_POS		8
103 #define TMP513_REMOTE_TEMP_LIMIT_3_POS		7
104 
105 #define TMP51X_VBUS_RANGE_32V		(32 * MICRO)
106 #define TMP51X_VBUS_RANGE_16V		(16 * MICRO)
107 
108 // Max and Min value
109 #define MAX_BUS_VOLTAGE_32_LIMIT	32764
110 #define MAX_BUS_VOLTAGE_16_LIMIT	16382
111 
112 // Max possible value is -256 to +256 but datasheet indicated -40 to 125.
113 #define MAX_TEMP_LIMIT			125000
114 #define MIN_TEMP_LIMIT			-40000
115 
116 #define MAX_TEMP_HYST			127500
117 
118 #define TMP512_MAX_CHANNELS		3
119 #define TMP513_MAX_CHANNELS		4
120 
121 #define TMP51X_TEMP_CONFIG_CONV_RATE	GENMASK(9, 7)
122 #define TMP51X_TEMP_CONFIG_RC		BIT(10)
123 #define TMP51X_TEMP_CHANNEL_MASK(n)	(GENMASK((n) - 1, 0) << 11)
124 #define TMP51X_TEMP_CONFIG_CONT		BIT(15)
125 #define TMP51X_TEMP_CONFIG_DEFAULT(n)					\
126 	(TMP51X_TEMP_CHANNEL_MASK(n) | TMP51X_TEMP_CONFIG_CONT |	\
127 	 TMP51X_TEMP_CONFIG_CONV_RATE | TMP51X_TEMP_CONFIG_RC)
128 
129 static const u8 TMP51X_TEMP_INPUT[4] = {
130 	TMP51X_LOCAL_TEMP_RESULT,
131 	TMP51X_REMOTE_TEMP_RESULT_1,
132 	TMP51X_REMOTE_TEMP_RESULT_2,
133 	TMP513_REMOTE_TEMP_RESULT_3
134 };
135 
136 static const u8 TMP51X_TEMP_CRIT[4] = {
137 	TMP51X_LOCAL_TEMP_LIMIT,
138 	TMP51X_REMOTE_TEMP_LIMIT_1,
139 	TMP51X_REMOTE_TEMP_LIMIT_2,
140 	TMP513_REMOTE_TEMP_LIMIT_3
141 };
142 
143 static const u8 TMP51X_TEMP_CRIT_ALARM[4] = {
144 	TMP51X_LOCAL_TEMP_LIMIT_POS,
145 	TMP51X_REMOTE_TEMP_LIMIT_1_POS,
146 	TMP51X_REMOTE_TEMP_LIMIT_2_POS,
147 	TMP513_REMOTE_TEMP_LIMIT_3_POS
148 };
149 
150 static const u8 TMP51X_TEMP_CRIT_HYST[4] = {
151 	TMP51X_N_FACTOR_AND_HYST_1,
152 	TMP51X_N_FACTOR_AND_HYST_1,
153 	TMP51X_N_FACTOR_AND_HYST_1,
154 	TMP51X_N_FACTOR_AND_HYST_1
155 };
156 
157 static const u8 TMP51X_CURR_INPUT[2] = {
158 	TMP51X_SHUNT_CURRENT_RESULT,
159 	TMP51X_BUS_CURRENT_RESULT
160 };
161 
162 static struct regmap_config tmp51x_regmap_config = {
163 	.reg_bits = 8,
164 	.val_bits = 16,
165 	.max_register = TMP51X_MAX_REGISTER_ADDR,
166 };
167 
168 struct tmp51x_data {
169 	u16 shunt_config;
170 	u16 pga_gain;
171 	u32 vbus_range_uvolt;
172 
173 	u16 temp_config;
174 	u32 nfactor[3];
175 
176 	u32 shunt_uohms;
177 
178 	u32 curr_lsb_ua;
179 	u32 pwr_lsb_uw;
180 
181 	u8 max_channels;
182 	struct regmap *regmap;
183 };
184 
185 // Set the shift based on the gain 8=4, 4=3, 2=2, 1=1
186 static inline u8 tmp51x_get_pga_shift(struct tmp51x_data *data)
187 {
188 	return 5 - ffs(data->pga_gain);
189 }
190 
191 static int tmp51x_get_value(struct tmp51x_data *data, u8 reg, u8 pos,
192 			    unsigned int regval, long *val)
193 {
194 	switch (reg) {
195 	case TMP51X_STATUS:
196 		*val = (regval >> pos) & 1;
197 		break;
198 	case TMP51X_SHUNT_CURRENT_RESULT:
199 	case TMP51X_SHUNT_CURRENT_H_LIMIT:
200 	case TMP51X_SHUNT_CURRENT_L_LIMIT:
201 		/*
202 		 * The valus is read in voltage in the chip but reported as
203 		 * current to the user.
204 		 * 2's complement number shifted by one to four depending
205 		 * on the pga gain setting. 1lsb = 10uV
206 		 */
207 		*val = sign_extend32(regval, 17 - tmp51x_get_pga_shift(data));
208 		*val = DIV_ROUND_CLOSEST(*val * 10 * MILLI, data->shunt_uohms);
209 		break;
210 	case TMP51X_BUS_VOLTAGE_RESULT:
211 	case TMP51X_BUS_VOLTAGE_H_LIMIT:
212 	case TMP51X_BUS_VOLTAGE_L_LIMIT:
213 		// 1lsb = 4mV
214 		*val = (regval >> TMP51X_BUS_VOLTAGE_SHIFT) * 4;
215 		break;
216 	case TMP51X_POWER_RESULT:
217 	case TMP51X_POWER_LIMIT:
218 		// Power = (current * BusVoltage) / 5000
219 		*val = regval * data->pwr_lsb_uw;
220 		break;
221 	case TMP51X_BUS_CURRENT_RESULT:
222 		// Current = (ShuntVoltage * CalibrationRegister) / 4096
223 		*val = sign_extend32(regval, 16) * data->curr_lsb_ua;
224 		*val = DIV_ROUND_CLOSEST(*val, MILLI);
225 		break;
226 	case TMP51X_LOCAL_TEMP_RESULT:
227 	case TMP51X_REMOTE_TEMP_RESULT_1:
228 	case TMP51X_REMOTE_TEMP_RESULT_2:
229 	case TMP513_REMOTE_TEMP_RESULT_3:
230 	case TMP51X_LOCAL_TEMP_LIMIT:
231 	case TMP51X_REMOTE_TEMP_LIMIT_1:
232 	case TMP51X_REMOTE_TEMP_LIMIT_2:
233 	case TMP513_REMOTE_TEMP_LIMIT_3:
234 		// 1lsb = 0.0625 degrees centigrade
235 		*val = sign_extend32(regval, 16) >> TMP51X_TEMP_SHIFT;
236 		*val = DIV_ROUND_CLOSEST(*val * 625, 10);
237 		break;
238 	case TMP51X_N_FACTOR_AND_HYST_1:
239 		// 1lsb = 0.5 degrees centigrade
240 		*val = (regval & TMP51X_HYST_MASK) * 500;
241 		break;
242 	default:
243 		// Programmer goofed
244 		WARN_ON_ONCE(1);
245 		*val = 0;
246 		return -EOPNOTSUPP;
247 	}
248 
249 	return 0;
250 }
251 
252 static int tmp51x_set_value(struct tmp51x_data *data, u8 reg, long val)
253 {
254 	int regval, max_val;
255 	u32 mask = 0;
256 
257 	switch (reg) {
258 	case TMP51X_SHUNT_CURRENT_H_LIMIT:
259 	case TMP51X_SHUNT_CURRENT_L_LIMIT:
260 		/*
261 		 * The user enter current value and we convert it to
262 		 * voltage. 1lsb = 10uV
263 		 */
264 		val = DIV_ROUND_CLOSEST(val * data->shunt_uohms, 10 * MILLI);
265 		max_val = U16_MAX >> tmp51x_get_pga_shift(data);
266 		regval = clamp_val(val, -max_val, max_val);
267 		break;
268 	case TMP51X_BUS_VOLTAGE_H_LIMIT:
269 	case TMP51X_BUS_VOLTAGE_L_LIMIT:
270 		// 1lsb = 4mV
271 		max_val = (data->vbus_range_uvolt == TMP51X_VBUS_RANGE_32V) ?
272 			MAX_BUS_VOLTAGE_32_LIMIT : MAX_BUS_VOLTAGE_16_LIMIT;
273 
274 		val = clamp_val(DIV_ROUND_CLOSEST(val, 4), 0, max_val);
275 		regval = val << TMP51X_BUS_VOLTAGE_SHIFT;
276 		break;
277 	case TMP51X_POWER_LIMIT:
278 		regval = clamp_val(DIV_ROUND_CLOSEST(val, data->pwr_lsb_uw), 0,
279 				   U16_MAX);
280 		break;
281 	case TMP51X_LOCAL_TEMP_LIMIT:
282 	case TMP51X_REMOTE_TEMP_LIMIT_1:
283 	case TMP51X_REMOTE_TEMP_LIMIT_2:
284 	case TMP513_REMOTE_TEMP_LIMIT_3:
285 		// 1lsb = 0.0625 degrees centigrade
286 		val = clamp_val(val, MIN_TEMP_LIMIT, MAX_TEMP_LIMIT);
287 		regval = DIV_ROUND_CLOSEST(val * 10, 625) << TMP51X_TEMP_SHIFT;
288 		break;
289 	case TMP51X_N_FACTOR_AND_HYST_1:
290 		// 1lsb = 0.5 degrees centigrade
291 		val = clamp_val(val, 0, MAX_TEMP_HYST);
292 		regval = DIV_ROUND_CLOSEST(val, 500);
293 		mask = TMP51X_HYST_MASK;
294 		break;
295 	default:
296 		// Programmer goofed
297 		WARN_ON_ONCE(1);
298 		return -EOPNOTSUPP;
299 	}
300 
301 	if (mask == 0)
302 		return regmap_write(data->regmap, reg, regval);
303 	else
304 		return regmap_update_bits(data->regmap, reg, mask, regval);
305 }
306 
307 static u8 tmp51x_get_reg(enum hwmon_sensor_types type, u32 attr, int channel)
308 {
309 	switch (type) {
310 	case hwmon_temp:
311 		switch (attr) {
312 		case hwmon_temp_input:
313 			return TMP51X_TEMP_INPUT[channel];
314 		case hwmon_temp_crit_alarm:
315 			return TMP51X_STATUS;
316 		case hwmon_temp_crit:
317 			return TMP51X_TEMP_CRIT[channel];
318 		case hwmon_temp_crit_hyst:
319 			return TMP51X_TEMP_CRIT_HYST[channel];
320 		}
321 		break;
322 	case hwmon_in:
323 		switch (attr) {
324 		case hwmon_in_input:
325 			return TMP51X_BUS_VOLTAGE_RESULT;
326 		case hwmon_in_lcrit_alarm:
327 		case hwmon_in_crit_alarm:
328 			return TMP51X_STATUS;
329 		case hwmon_in_lcrit:
330 			return TMP51X_BUS_VOLTAGE_L_LIMIT;
331 		case hwmon_in_crit:
332 			return TMP51X_BUS_VOLTAGE_H_LIMIT;
333 		}
334 		break;
335 	case hwmon_curr:
336 		switch (attr) {
337 		case hwmon_curr_input:
338 			return TMP51X_CURR_INPUT[channel];
339 		case hwmon_curr_lcrit_alarm:
340 		case hwmon_curr_crit_alarm:
341 			return TMP51X_STATUS;
342 		case hwmon_curr_lcrit:
343 			return TMP51X_SHUNT_CURRENT_L_LIMIT;
344 		case hwmon_curr_crit:
345 			return TMP51X_SHUNT_CURRENT_H_LIMIT;
346 		}
347 		break;
348 	case hwmon_power:
349 		switch (attr) {
350 		case hwmon_power_input:
351 			return TMP51X_POWER_RESULT;
352 		case hwmon_power_crit_alarm:
353 			return TMP51X_STATUS;
354 		case hwmon_power_crit:
355 			return TMP51X_POWER_LIMIT;
356 		}
357 		break;
358 	default:
359 		break;
360 	}
361 
362 	return 0;
363 }
364 
365 static u8 tmp51x_get_status_pos(enum hwmon_sensor_types type, u32 attr,
366 				int channel)
367 {
368 	switch (type) {
369 	case hwmon_temp:
370 		switch (attr) {
371 		case hwmon_temp_crit_alarm:
372 			return TMP51X_TEMP_CRIT_ALARM[channel];
373 		}
374 		break;
375 	case hwmon_in:
376 		switch (attr) {
377 		case hwmon_in_lcrit_alarm:
378 			return TMP51X_BUS_VOLTAGE_L_LIMIT_POS;
379 		case hwmon_in_crit_alarm:
380 			return TMP51X_BUS_VOLTAGE_H_LIMIT_POS;
381 		}
382 		break;
383 	case hwmon_curr:
384 		switch (attr) {
385 		case hwmon_curr_lcrit_alarm:
386 			return TMP51X_SHUNT_CURRENT_L_LIMIT_POS;
387 		case hwmon_curr_crit_alarm:
388 			return TMP51X_SHUNT_CURRENT_H_LIMIT_POS;
389 		}
390 		break;
391 	case hwmon_power:
392 		switch (attr) {
393 		case hwmon_power_crit_alarm:
394 			return TMP51X_POWER_LIMIT_POS;
395 		}
396 		break;
397 	default:
398 		break;
399 	}
400 
401 	return 0;
402 }
403 
404 static int tmp51x_read(struct device *dev, enum hwmon_sensor_types type,
405 		       u32 attr, int channel, long *val)
406 {
407 	struct tmp51x_data *data = dev_get_drvdata(dev);
408 	int ret;
409 	u32 regval;
410 	u8 pos = 0, reg = 0;
411 
412 	reg = tmp51x_get_reg(type, attr, channel);
413 	if (reg == 0)
414 		return -EOPNOTSUPP;
415 
416 	if (reg == TMP51X_STATUS)
417 		pos = tmp51x_get_status_pos(type, attr, channel);
418 
419 	ret = regmap_read(data->regmap, reg, &regval);
420 	if (ret < 0)
421 		return ret;
422 
423 	return tmp51x_get_value(data, reg, pos, regval, val);
424 }
425 
426 static int tmp51x_write(struct device *dev, enum hwmon_sensor_types type,
427 			u32 attr, int channel, long val)
428 {
429 	u8 reg = 0;
430 
431 	reg = tmp51x_get_reg(type, attr, channel);
432 	if (reg == 0)
433 		return -EOPNOTSUPP;
434 
435 	return tmp51x_set_value(dev_get_drvdata(dev), reg, val);
436 }
437 
438 static umode_t tmp51x_is_visible(const void *_data,
439 				 enum hwmon_sensor_types type, u32 attr,
440 				 int channel)
441 {
442 	const struct tmp51x_data *data = _data;
443 
444 	switch (type) {
445 	case hwmon_temp:
446 		if (channel >= data->max_channels)
447 			return 0;
448 		switch (attr) {
449 		case hwmon_temp_input:
450 		case hwmon_temp_crit_alarm:
451 			return 0444;
452 		case hwmon_temp_crit:
453 			return 0644;
454 		case hwmon_temp_crit_hyst:
455 			if (channel == 0)
456 				return 0644;
457 			return 0444;
458 		}
459 		break;
460 	case hwmon_in:
461 		switch (attr) {
462 		case hwmon_in_input:
463 		case hwmon_in_lcrit_alarm:
464 		case hwmon_in_crit_alarm:
465 			return 0444;
466 		case hwmon_in_lcrit:
467 		case hwmon_in_crit:
468 			return 0644;
469 		}
470 		break;
471 	case hwmon_curr:
472 		if (!data->shunt_uohms)
473 			return 0;
474 
475 		switch (attr) {
476 		case hwmon_curr_input:
477 		case hwmon_curr_lcrit_alarm:
478 		case hwmon_curr_crit_alarm:
479 			return 0444;
480 		case hwmon_curr_lcrit:
481 		case hwmon_curr_crit:
482 			return 0644;
483 		}
484 		break;
485 	case hwmon_power:
486 		if (!data->shunt_uohms)
487 			return 0;
488 
489 		switch (attr) {
490 		case hwmon_power_input:
491 		case hwmon_power_crit_alarm:
492 			return 0444;
493 		case hwmon_power_crit:
494 			return 0644;
495 		}
496 		break;
497 	default:
498 		break;
499 	}
500 	return 0;
501 }
502 
503 static const struct hwmon_channel_info * const tmp51x_info[] = {
504 	HWMON_CHANNEL_INFO(temp,
505 			   HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
506 			   HWMON_T_CRIT_HYST,
507 			   HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
508 			   HWMON_T_CRIT_HYST,
509 			   HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
510 			   HWMON_T_CRIT_HYST,
511 			   HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
512 			   HWMON_T_CRIT_HYST),
513 	HWMON_CHANNEL_INFO(in,
514 			   HWMON_I_INPUT | HWMON_I_LCRIT | HWMON_I_LCRIT_ALARM |
515 			   HWMON_I_CRIT | HWMON_I_CRIT_ALARM),
516 	HWMON_CHANNEL_INFO(curr,
517 			   HWMON_C_INPUT | HWMON_C_LCRIT | HWMON_C_LCRIT_ALARM |
518 			   HWMON_C_CRIT | HWMON_C_CRIT_ALARM,
519 			   HWMON_C_INPUT),
520 	HWMON_CHANNEL_INFO(power,
521 			   HWMON_P_INPUT | HWMON_P_CRIT | HWMON_P_CRIT_ALARM),
522 	NULL
523 };
524 
525 static const struct hwmon_ops tmp51x_hwmon_ops = {
526 	.is_visible = tmp51x_is_visible,
527 	.read = tmp51x_read,
528 	.write = tmp51x_write,
529 };
530 
531 static const struct hwmon_chip_info tmp51x_chip_info = {
532 	.ops = &tmp51x_hwmon_ops,
533 	.info = tmp51x_info,
534 };
535 
536 /*
537  * Calibrate the tmp51x following the datasheet method
538  */
539 static int tmp51x_calibrate(struct tmp51x_data *data)
540 {
541 	int vshunt_max = data->pga_gain * 40;
542 	u64 max_curr_ma;
543 	u32 div;
544 
545 	/*
546 	 * If shunt_uohms is equal to 0, the calibration should be set to 0.
547 	 * The consequence will be that the current and power measurement engine
548 	 * of the sensor will not work. Temperature and voltage sensing will
549 	 * continue to work.
550 	 */
551 	if (data->shunt_uohms == 0)
552 		return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION, 0);
553 
554 	max_curr_ma = DIV_ROUND_CLOSEST_ULL(vshunt_max * MICRO, data->shunt_uohms);
555 
556 	/*
557 	 * Calculate the minimal bit resolution for the current and the power.
558 	 * Those values will be used during register interpretation.
559 	 */
560 	data->curr_lsb_ua = DIV_ROUND_CLOSEST_ULL(max_curr_ma * MILLI, 32767);
561 	data->pwr_lsb_uw = 20 * data->curr_lsb_ua;
562 
563 	div = DIV_ROUND_CLOSEST_ULL(data->curr_lsb_ua * data->shunt_uohms, MICRO);
564 
565 	return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION,
566 			    DIV_ROUND_CLOSEST(40960, div));
567 }
568 
569 /*
570  * Initialize the configuration and calibration registers.
571  */
572 static int tmp51x_init(struct tmp51x_data *data)
573 {
574 	unsigned int regval;
575 	int ret = regmap_write(data->regmap, TMP51X_SHUNT_CONFIG,
576 			       data->shunt_config);
577 	if (ret < 0)
578 		return ret;
579 
580 	ret = regmap_write(data->regmap, TMP51X_TEMP_CONFIG, data->temp_config);
581 	if (ret < 0)
582 		return ret;
583 
584 	// nFactor configuration
585 	ret = regmap_update_bits(data->regmap, TMP51X_N_FACTOR_AND_HYST_1,
586 				 TMP51X_NFACTOR_MASK, data->nfactor[0] << 8);
587 	if (ret < 0)
588 		return ret;
589 
590 	ret = regmap_write(data->regmap, TMP51X_N_FACTOR_2,
591 			   data->nfactor[1] << 8);
592 	if (ret < 0)
593 		return ret;
594 
595 	if (data->max_channels == TMP513_MAX_CHANNELS) {
596 		ret = regmap_write(data->regmap, TMP513_N_FACTOR_3,
597 				   data->nfactor[2] << 8);
598 		if (ret < 0)
599 			return ret;
600 	}
601 
602 	ret = tmp51x_calibrate(data);
603 	if (ret < 0)
604 		return ret;
605 
606 	// Read the status register before using as the datasheet propose
607 	return regmap_read(data->regmap, TMP51X_STATUS, &regval);
608 }
609 
610 static const struct i2c_device_id tmp51x_id[] = {
611 	{ "tmp512", TMP512_MAX_CHANNELS },
612 	{ "tmp513", TMP513_MAX_CHANNELS },
613 	{ }
614 };
615 MODULE_DEVICE_TABLE(i2c, tmp51x_id);
616 
617 static const struct of_device_id tmp51x_of_match[] = {
618 	{ .compatible = "ti,tmp512", .data = (void *)TMP512_MAX_CHANNELS },
619 	{ .compatible = "ti,tmp513", .data = (void *)TMP513_MAX_CHANNELS },
620 	{ }
621 };
622 MODULE_DEVICE_TABLE(of, tmp51x_of_match);
623 
624 static int tmp51x_vbus_range_to_reg(struct device *dev,
625 				    struct tmp51x_data *data)
626 {
627 	if (data->vbus_range_uvolt == TMP51X_VBUS_RANGE_32V) {
628 		data->shunt_config |= TMP51X_BUS_VOLTAGE_MASK;
629 	} else if (data->vbus_range_uvolt == TMP51X_VBUS_RANGE_16V) {
630 		data->shunt_config &= ~TMP51X_BUS_VOLTAGE_MASK;
631 	} else {
632 		return dev_err_probe(dev, -EINVAL,
633 				     "ti,bus-range-microvolt is invalid: %u\n",
634 				     data->vbus_range_uvolt);
635 	}
636 	return 0;
637 }
638 
639 static int tmp51x_pga_gain_to_reg(struct device *dev, struct tmp51x_data *data)
640 {
641 	if (data->pga_gain == 8) {
642 		data->shunt_config |= CURRENT_SENSE_VOLTAGE_320_MASK;
643 	} else if (data->pga_gain == 4) {
644 		data->shunt_config |= CURRENT_SENSE_VOLTAGE_160_MASK;
645 	} else if (data->pga_gain == 2) {
646 		data->shunt_config |= CURRENT_SENSE_VOLTAGE_80_MASK;
647 	} else if (data->pga_gain == 1) {
648 		data->shunt_config |= CURRENT_SENSE_VOLTAGE_40_MASK;
649 	} else {
650 		return dev_err_probe(dev, -EINVAL,
651 				     "ti,pga-gain is invalid: %u\n", data->pga_gain);
652 	}
653 	return 0;
654 }
655 
656 static int tmp51x_read_properties(struct device *dev, struct tmp51x_data *data)
657 {
658 	int ret;
659 	u32 val;
660 
661 	ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms", &val);
662 	data->shunt_uohms = (ret >= 0) ? val : TMP51X_SHUNT_VALUE_DEFAULT;
663 
664 	ret = device_property_read_u32(dev, "ti,bus-range-microvolt", &val);
665 	data->vbus_range_uvolt = (ret >= 0) ? val : TMP51X_VBUS_RANGE_DEFAULT;
666 	ret = tmp51x_vbus_range_to_reg(dev, data);
667 	if (ret < 0)
668 		return ret;
669 
670 	ret = device_property_read_u32(dev, "ti,pga-gain", &val);
671 	data->pga_gain = (ret >= 0) ? val : TMP51X_PGA_DEFAULT;
672 	ret = tmp51x_pga_gain_to_reg(dev, data);
673 	if (ret < 0)
674 		return ret;
675 
676 	device_property_read_u32_array(dev, "ti,nfactor", data->nfactor,
677 				       data->max_channels - 1);
678 
679 	// Check if shunt value is compatible with pga-gain
680 	if (data->shunt_uohms > data->pga_gain * 40 * MICRO) {
681 		return dev_err_probe(dev, -EINVAL,
682 				     "shunt-resistor: %u too big for pga_gain: %u\n",
683 				     data->shunt_uohms, data->pga_gain);
684 	}
685 
686 	return 0;
687 }
688 
689 static void tmp51x_use_default(struct tmp51x_data *data)
690 {
691 	data->vbus_range_uvolt = TMP51X_VBUS_RANGE_DEFAULT;
692 	data->pga_gain = TMP51X_PGA_DEFAULT;
693 	data->shunt_uohms = TMP51X_SHUNT_VALUE_DEFAULT;
694 }
695 
696 static int tmp51x_configure(struct device *dev, struct tmp51x_data *data)
697 {
698 	data->shunt_config = TMP51X_SHUNT_CONFIG_DEFAULT;
699 	data->temp_config = TMP51X_TEMP_CONFIG_DEFAULT(data->max_channels);
700 
701 	if (dev->of_node)
702 		return tmp51x_read_properties(dev, data);
703 
704 	tmp51x_use_default(data);
705 
706 	return 0;
707 }
708 
709 static int tmp51x_probe(struct i2c_client *client)
710 {
711 	struct device *dev = &client->dev;
712 	struct tmp51x_data *data;
713 	struct device *hwmon_dev;
714 	int ret;
715 
716 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
717 	if (!data)
718 		return -ENOMEM;
719 
720 	data->max_channels = (uintptr_t)i2c_get_match_data(client);
721 
722 	ret = tmp51x_configure(dev, data);
723 	if (ret < 0)
724 		return dev_err_probe(dev, ret, "error configuring the device\n");
725 
726 	data->regmap = devm_regmap_init_i2c(client, &tmp51x_regmap_config);
727 	if (IS_ERR(data->regmap))
728 		return dev_err_probe(dev, PTR_ERR(data->regmap),
729 				     "failed to allocate register map\n");
730 
731 	ret = tmp51x_init(data);
732 	if (ret < 0)
733 		return dev_err_probe(dev, ret, "error configuring the device\n");
734 
735 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
736 							 data,
737 							 &tmp51x_chip_info,
738 							 NULL);
739 	if (IS_ERR(hwmon_dev))
740 		return PTR_ERR(hwmon_dev);
741 
742 	dev_dbg(dev, "power monitor %s\n", client->name);
743 
744 	return 0;
745 }
746 
747 static struct i2c_driver tmp51x_driver = {
748 	.driver = {
749 		.name	= "tmp51x",
750 		.of_match_table = tmp51x_of_match,
751 	},
752 	.probe		= tmp51x_probe,
753 	.id_table	= tmp51x_id,
754 };
755 
756 module_i2c_driver(tmp51x_driver);
757 
758 MODULE_AUTHOR("Eric Tremblay <etremblay@distechcontrols.com>");
759 MODULE_DESCRIPTION("tmp51x driver");
760 MODULE_LICENSE("GPL");
761