Re: [PATCH] iio: sx9310: Fix semtech,avg-pos-strength setting when > 16

From: Doug Anderson
Date: Fri Nov 20 2020 - 13:17:05 EST


Hi,

On Thu, Nov 19, 2020 at 11:38 PM Stephen Boyd <swboyd@xxxxxxxxxxxx> wrote:
>
> This DT property can be 0, 16, and then 64, but not 32. The math here
> doesn't recognize this slight bump in the power of 2 numbers and
> translates a DT property of 64 into the register value '3' when it
> really should be '2'. Fix it by subtracting one more if the number being
> translated is larger than 16.
>
> Cc: Daniel Campello <campello@xxxxxxxxxxxx>
> Cc: Lars-Peter Clausen <lars@xxxxxxxxxx>
> Cc: Peter Meerwald-Stadler <pmeerw@xxxxxxxxxx>
> Cc: Douglas Anderson <dianders@xxxxxxxxxxxx>
> Cc: Gwendal Grignou <gwendal@xxxxxxxxxxxx>
> Cc: Evan Green <evgreen@xxxxxxxxxxxx>
> Signed-off-by: Stephen Boyd <swboyd@xxxxxxxxxxxx>
> ---
>
> This fixes commit 5b19ca2c78a0 ("iio: sx9310: Set various settings from
> DT") in the togreg branch.
>
> drivers/iio/proximity/sx9310.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index a2f820997afc..5d8387ed9301 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -1305,7 +1305,7 @@ sx9310_get_default_reg(struct sx9310_data *data, int i,
> if (ret)
> break;
>
> - pos = min(max(ilog2(pos), 3), 10) - 3;
> + pos = min(max(ilog2(pos), 3), 11) - (pos > 16 ? 4 : 3);

Checking the math for the documented possible values of pos. What we want:

0 => 0
16 => 1
64 => 2
128 => 3
256 => 4
512 => 5
1024 => 6
4294967295 => 7

So looks OK. Do we care about anything other than the documented
numbers? If my understanding of ilog2 is correct, then you'll get the
wrong answer for 17. I think you could fix it with:

pos = min(max(ilog2(pos), 3), 11) - (pos >= 32 ? 4 : 3);

-Doug