Linux Hardware Monitor development
 help / color / mirror / Atom feed
* [PATCH] hwmon: (tmp513) fix division by zero when calibrating a large shunt
@ 2026-12-05  1:24 Shirong Zhao
  2026-12-05  1:37 ` sashiko-bot
  2026-12-05  1:45 ` [PATCH v2] hwmon: (tmp513) avoid divide by zero from rounded-down current LSB Shirong Zhao
  0 siblings, 2 replies; 8+ messages in thread
From: Shirong Zhao @ 2026-12-05  1:24 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Eric Tremblay, linux-hwmon, stable, Shirong Zhao

tmp51x_calibrate() computes the calibration divisor as

	div = DIV_ROUND_CLOSEST_ULL(data->curr_lsb_ua * data->shunt_uohms,
				    MICRO);

and then does DIV_ROUND_CLOSEST(40960, div).  curr_lsb_ua is
DIV_ROUND_CLOSEST_ULL(max_curr_ma * MILLI, 32767), so when the shunt
resistor is large enough that max_curr_ma is below ~16 mA, curr_lsb_ua
rounds to 0 and div becomes 0, making the final division a divide by
zero during probe.

The shunt value comes from the "shunt-resistor-micro-ohms" device-tree /
firmware property (tmp51x_read_properties()), so this is a device
configuration issue, not a runtime userspace input.  It is limited only
from above (shunt_uohms > pga_gain * 40 uOhm is rejected); there is no
lower bound that keeps curr_lsb_ua nonzero.  For the default pga_gain of 8
the trap triggers for shunt resistors from about 19.4 Ohm up to the 320
Ohm upper bound, i.e. for perfectly ordinary shunt values.

Handle div == 0 the same way as the existing shunt_uohms == 0 case:
program the calibration register to 0 so the current and power engine is
disabled instead of crashing.

Fixes: 59dfa75e5d82 ("hwmon: Add driver for Texas Instruments TMP512/513 sensor chips.")
Signed-off-by: Shirong Zhao <shxzhaosr@163.com>
---
 drivers/hwmon/tmp513.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/hwmon/tmp513.c b/drivers/hwmon/tmp513.c
index a3a4772..b7eb614 100644
--- a/drivers/hwmon/tmp513.c
+++ b/drivers/hwmon/tmp513.c
@@ -565,6 +565,14 @@ static int tmp51x_calibrate(struct tmp51x_data *data)
 
 	div = DIV_ROUND_CLOSEST_ULL(data->curr_lsb_ua * data->shunt_uohms, MICRO);
 
+	/*
+	 * A large shunt resistor can round curr_lsb_ua down to 0, which makes
+	 * div 0 as well.  Just like the shunt_uohms == 0 case above, disable the
+	 * current/power engine rather than dividing by zero.
+	 */
+	if (div == 0)
+		return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION, 0);
+
 	return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION,
 			    DIV_ROUND_CLOSEST(40960, div));
 }
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-12-05 16:28 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  1:24 [PATCH] hwmon: (tmp513) fix division by zero when calibrating a large shunt Shirong Zhao
2026-12-05  1:37 ` sashiko-bot
2026-12-05  1:45 ` [PATCH v2] hwmon: (tmp513) avoid divide by zero from rounded-down current LSB Shirong Zhao
2026-12-05  1:57   ` sashiko-bot
2026-12-05  4:29   ` Guenter Roeck
2026-12-05  4:37   ` [PATCH v3] " Shirong Zhao
2026-12-05  4:48     ` sashiko-bot
2026-12-05 16:28     ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox