Re: [PATCH v3] iio: temperature: mcp9600: Bound thresholds before scaling

From: Andy Shevchenko

Date: Thu Nov 12 2026 - 12:05:43 EST


On Thu, Nov 12, 2026 at 09:57:38AM +0800, Zhu Jun wrote:
> mcp9600_write_thresh() multiplies the integer part by 1000000 before
> clamping to the supported temperature range. The multiplication can
> overflow int, so a request such as 3000 degrees can select the lower
> threshold instead of the upper threshold.
>
> Clamp the integer part to the hot-junction range before scaling. Keep
> the existing final clamp for fractional values and the narrower cold-
> junction range.

Reviewed-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxx>

...

> + val = clamp(val, MCP9600_MIN_TEMP_HOT_JUNCTION_MICRO / (int)MICRO,
> + MCP9600_MAX_TEMP_HOT_JUNCTION_MICRO / (int)MICRO);

In such a case I would wrap on a logical boundaries:

val = clamp(val,
MCP9600_MIN_TEMP_HOT_JUNCTION_MICRO / (int)MICRO,
MCP9600_MAX_TEMP_HOT_JUNCTION_MICRO / (int)MICRO);

(but no need to resend, let Jonathan to decide).

--
With Best Regards,
Andy Shevchenko