Re: [PATCH 5/6] iio: proximity: sx9360: fix proximity threshold writes

From: Andy Shevchenko

Date: Tue Nov 17 2026 - 14:11:27 EST


On Tue, Nov 17, 2026 at 02:23:13AM +0530, Arnav Kapoor wrote:
> sx9360_read_thresh() reports a PROX_THRESH code r as r for r <= 1 and as
> r * r / 2 otherwise, but sx9360_write_thresh() converts back with
> int_sqrt(2 * val). For odd r the read value is (r * r - 1) / 2, so
> writing back the value that was just read programs r - 1: setting the
> threshold to the value it already has lowers it, for 127 of the 256
> codes (3, 5, 7, ...).
>
> The int to unsigned int conversion also lets some negative values
> through: 2 * val wraps, so e.g. INT_MIN is written as 0 and values just
> above INT_MIN as small thresholds instead of being rejected.
>
> Reject negative values and use int_sqrt(2 * val + 1), which picks the
> largest code whose reported threshold does not exceed the requested
> value and makes read_thresh() and write_thresh() inverses of each
> other for all 256 codes.

All the same issues with this patch as the other one with sqrt() operation.

--
With Best Regards,
Andy Shevchenko