Commit 5fd7b4348fcc for kernel
commit 5fd7b4348fcc9a3ccdd08fc426a272c1e4837c95
Author: Shirong Zhao <shxzhaosr@163.com>
Date: Sat Dec 5 12:37:27 2026 +0800
hwmon: (tmp513) avoid divide by zero from rounded-down current LSB
tmp51x_calibrate() computes the current LSB as
curr_lsb_ua = DIV_ROUND_CLOSEST_ULL(max_curr_ma * MILLI, 32767);
and then derives pwr_lsb_uw = 20 * curr_lsb_ua and
div = DIV_ROUND_CLOSEST_ULL(curr_lsb_ua * shunt_uohms, MICRO);
so the calibration register write is DIV_ROUND_CLOSEST(40960, div).
curr_lsb_ua decreasing with a larger shunt means that for large shunt
resistors DIV_ROUND_CLOSEST_ULL() rounds it to 0. Both div and
pwr_lsb_uw then become 0: the calibration division traps, and a later
write to power1_crit (tmp51x_set_value(), DIV_ROUND_CLOSEST(val,
pwr_lsb_uw)) traps as well, because tmp51x_is_visible() only hides the
attributes when shunt_uohms is exactly 0.
curr_lsb_ua is a minimum resolution that only needs to be >= 1 uA; round
it up with DIV_ROUND_UP_ULL() instead of to the nearest. This keeps
curr_lsb_ua, pwr_lsb_uw and div >= 1 for the entire valid shunt range and
lets large (but valid) shunts calibrate normally instead of being
disabled.
Fixes: 59dfa75e5d82 ("hwmon: Add driver for Texas Instruments TMP512/513 sensor chips.")
Cc: stable@vger.kernel.org
Signed-off-by: Shirong Zhao <shxzhaosr@163.com>
Link: https://patch.msgid.link/20261205043727.18-1-shxzhaosr@163.com
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
diff --git a/drivers/hwmon/tmp513.c b/drivers/hwmon/tmp513.c
index a3a4772a37e9..6059c780f192 100644
--- a/drivers/hwmon/tmp513.c
+++ b/drivers/hwmon/tmp513.c
@@ -560,7 +560,14 @@ static int tmp51x_calibrate(struct tmp51x_data *data)
* Calculate the minimal bit resolution for the current and the power.
* Those values will be used during register interpretation.
*/
- data->curr_lsb_ua = DIV_ROUND_CLOSEST_ULL(max_curr_ma * MILLI, 32767);
+ /*
+ * Round up so curr_lsb_ua (and thus pwr_lsb_uw and div) never become 0
+ * for any valid shunt. Rounding to nearest can round this down to 0
+ * for large shunt resistors (large max_curr_ma denominator), which would
+ * make the calibration division below and the pwr_lsb_uw division in
+ * tmp51x_set_value() divide by zero.
+ */
+ data->curr_lsb_ua = DIV_ROUND_UP_ULL(max_curr_ma * MILLI, 32767);
data->pwr_lsb_uw = 20 * data->curr_lsb_ua;
div = DIV_ROUND_CLOSEST_ULL(data->curr_lsb_ua * data->shunt_uohms, MICRO);