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