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