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

* Re: [PATCH] hwmon: (tmp513) fix division by zero when calibrating a large shunt
  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
  1 sibling, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-05  1:37 UTC (permalink / raw)
  To: Shirong Zhao; +Cc: linux-hwmon

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] hwmon: (tmp513) divide-by-zero in tmp51x_set_value() when div
  == 0
- [Medium] hwmon: (tmp513) tmp51x_calibrate() disables sensing for valid
  large shunts
- [Low] hwmon: (tmp513) inaccurate shunt bound units and direction in
  commit message
--

commit a7739395a7938b1d11a4a6e3d2b569de06b0cc02
Author: Shirong Zhao <shxzhaosr@163.com>

hwmon: (tmp513) fix division by zero when calibrating a large shunt

This commit adds a check for div == 0 in tmp51x_calibrate() and writes 0 to
TMP51X_SHUNT_CALIBRATION to disable the current and power measurement engine
instead of dividing by zero.

> 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.

[Severity: Low]
In tmp51x_read_properties(), the rejected threshold is
pga_gain * 40 * MICRO uOhm (pga_gain * 40 Ohm), not pga_gain * 40 uOhm.
Also, since curr_lsb_ua decreases as shunt_uohms grows, keeping curr_lsb_ua
nonzero would require a tighter upper bound on shunt_uohms (or a lower bound
on max_curr_ma), not a lower bound on shunt_uohms.

> diff --git a/drivers/hwmon/tmp513.c b/drivers/hwmon/tmp513.c
> index a3a4772a37e93..b7eb6143faff3 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);

[Severity: High]
Can writing to power1_crit still trigger a divide-by-zero when div == 0?

When div == 0, data->curr_lsb_ua and data->pwr_lsb_uw are 0 while
data->shunt_uohms stays non-zero.  tmp51x_is_visible() only checks
!data->shunt_uohms for hwmon_curr and hwmon_power, so power1_crit and the
other disabled current/power attributes remain exposed in sysfs.

drivers/hwmon/tmp513.c:tmp51x_set_value() {
    ...
	case TMP51X_POWER_LIMIT:
		regval = clamp_val(DIV_ROUND_CLOSEST(val, data->pwr_lsb_uw), 0,
				   U16_MAX);
		break;
    ...
}

Any write to power1_crit calls tmp51x_write() -> tmp51x_set_value() and
divides by data->pwr_lsb_uw == 0, crashing the kernel.

[Severity: Medium]
Should tmp51x_calibrate() round curr_lsb_ua up instead of disabling sensing
for valid large shunts?

Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml specifies that only
0 disables calibration and values up to pga-gain * 40 * 1000 * 1000 uOhm
are valid.  Disabling the engine on div == 0 breaks current and power
sensing from roughly 19.4 Ohm to 320 Ohm at the default pga_gain = 8.

curr_lsb_ua is a minimum lower bound so the signed current register does
not overflow, and only rounds to 0 because of DIV_ROUND_CLOSEST_ULL().
Using DIV_ROUND_UP_ULL() keeps curr_lsb_ua >= 1 uA and div in [1, 320]
across the entire valid range so large shunts calibrate properly.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205012428.120-1-shxzhaosr@163.com?part=1

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

* [PATCH v2] hwmon: (tmp513) avoid divide by zero from rounded-down current LSB
  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 ` Shirong Zhao
  2026-12-05  1:57   ` sashiko-bot
                     ` (2 more replies)
  1 sibling, 3 replies; 8+ messages in thread
From: Shirong Zhao @ 2026-12-05  1:45 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Eric Tremblay, linux-hwmon, stable, Shirong Zhao

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>
---
 drivers/hwmon/tmp513.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/hwmon/tmp513.c b/drivers/hwmon/tmp513.c
index a3a4772..6059c78 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);
-- 
2.53.0


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

* Re: [PATCH v2] hwmon: (tmp513) avoid divide by zero from rounded-down current LSB
  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
  2 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-05  1:57 UTC (permalink / raw)
  To: Shirong Zhao; +Cc: linux-hwmon

> 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>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205014552.39-1-shxzhaosr@163.com?part=1

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

* Re: [PATCH v2] hwmon: (tmp513) avoid divide by zero from rounded-down current LSB
  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
  2 siblings, 0 replies; 8+ messages in thread
From: Guenter Roeck @ 2026-12-05  4:29 UTC (permalink / raw)
  To: Shirong Zhao; +Cc: Eric Tremblay, linux-hwmon, stable

On 12/4/26 17:45, Shirong Zhao wrote:
> 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>
> ---

Change log goes here.

Guenter

>   drivers/hwmon/tmp513.c | 9 ++++++++-
>   1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/hwmon/tmp513.c b/drivers/hwmon/tmp513.c
> index a3a4772..6059c78 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);


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

* [PATCH v3] hwmon: (tmp513) avoid divide by zero from rounded-down current LSB
  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   ` Shirong Zhao
  2026-12-05  4:48     ` sashiko-bot
  2026-12-05 16:28     ` Guenter Roeck
  2 siblings, 2 replies; 8+ messages in thread
From: Shirong Zhao @ 2026-12-05  4:37 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Eric Tremblay, linux-hwmon, stable, Shirong Zhao

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>
---
v3:
- Add this changelog as requested.

v2:
- Round curr_lsb_ua up (DIV_ROUND_UP_ULL) instead of to the nearest, which
  also avoids the power1_crit division by a zero pwr_lsb_uw (reported by
  sashiko AI review); rebased on the current tree with a rewritten commit
  message.
---
 drivers/hwmon/tmp513.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/hwmon/tmp513.c b/drivers/hwmon/tmp513.c
index a3a4772..6059c78 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);
-- 
2.53.0


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

* Re: [PATCH v3] hwmon: (tmp513) avoid divide by zero from rounded-down current LSB
  2026-12-05  4:37   ` [PATCH v3] " Shirong Zhao
@ 2026-12-05  4:48     ` sashiko-bot
  2026-12-05 16:28     ` Guenter Roeck
  1 sibling, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-05  4:48 UTC (permalink / raw)
  To: Shirong Zhao; +Cc: linux-hwmon

> 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>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205043727.18-1-shxzhaosr@163.com?part=1

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

* Re: [PATCH v3] hwmon: (tmp513) avoid divide by zero from rounded-down current LSB
  2026-12-05  4:37   ` [PATCH v3] " Shirong Zhao
  2026-12-05  4:48     ` sashiko-bot
@ 2026-12-05 16:28     ` Guenter Roeck
  1 sibling, 0 replies; 8+ messages in thread
From: Guenter Roeck @ 2026-12-05 16:28 UTC (permalink / raw)
  To: Shirong Zhao; +Cc: Eric Tremblay, linux-hwmon, stable

On Sat, Dec 05, 2026 at 12:37:27PM +0800, Shirong Zhao wrote:
> 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>

Applied.

Thanks,
Guenter

^ permalink raw reply	[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