Re: [PATCH 10/15] iio: light: tsl2591: don't divide by zero with low gain

From: Jonathan Cameron

Date: Tue Oct 27 2026 - 22:13:23 EST


On Tue, 27 Oct 2026 17:19:14 +0100
Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> wrote:

> From: Josef Bacik <josef@xxxxxxxxxxxxxx>
>
> The number of counts per lux is the integration time in milliseconds times
> the gain, divided by 408. With the low gain, which is 1, and an
> integration time of 100 to 400 ms, that is 0 in integer arithmetic, and
> each read of the illuminance divides by zero.
>
> In that case, multiply by the coefficient and divide by the product
> instead. The result of the other settings does not change.
>
https://lore.kernel.org/all/20261022-first-patch-v4-0-90b2fc6e8e28@xxxxxxxxxxxxxxx/

I have first few of that series queued up already.

Jonathan


> Fixes: 2335f0d7c790 ("iio: light: Added AMS tsl2591 driver implementation")
> Cc: stable <stable@xxxxxxxxxx>
> Cc: David Lechner <dlechner@xxxxxxxxxxxx>
> Cc: "Nuno Sá" <nuno.sa@xxxxxxxxxx>
> Cc: Andy Shevchenko <andy@xxxxxxxxxx>
> Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
> Cc: Lucas Rabaquim <lucas.rabaquim@xxxxxx>
> Cc: Matheus Silveira <matheus.feitosa@xxxxxx>
> Cc: Josef Bacik <josef@xxxxxxxxxxxxxx>
> Cc: Stepan Ionichev <sozdayvek@xxxxxxxxx>
> Cc: Joe Sandom <joe.g.sandom@xxxxxxxxx>
> Signed-off-by: Josef Bacik <josef@xxxxxxxxxxxxxx>
> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
> ---
> drivers/iio/light/tsl2591.c | 10 ++++++++--
> 1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iio/light/tsl2591.c b/drivers/iio/light/tsl2591.c
> index 286dc09e170f..773b50c17e71 100644
> --- a/drivers/iio/light/tsl2591.c
> +++ b/drivers/iio/light/tsl2591.c
> @@ -16,6 +16,7 @@
> #include <linux/interrupt.h>
> #include <linux/iopoll.h>
> #include <linux/kernel.h>
> +#include <linux/math64.h>
> #include <linux/module.h>
> #include <linux/mutex.h>
> #include <linux/pm_runtime.h>
> @@ -424,8 +425,13 @@ static int tsl2591_read_channel_data(struct iio_dev *indio_dev,
> }
>
> /* Calculate lux value */
> - lux = ((als_ch0 - als_ch1) *
> - (1000 - ((als_ch1 * 1000) / als_ch0))) / counts_per_lux;
> + lux = (als_ch0 - als_ch1) *
> + (1000 - ((als_ch1 * 1000) / als_ch0));
> + if (counts_per_lux)
> + lux /= counts_per_lux;
> + else /* Less than one count per lux */
> + lux = div_s64((s64)lux * TSL2591_LUX_COEFFICIENT,
> + int_time_fval * gain_multi);
>
> dev_dbg(&client->dev, "Raw lux calculation: %d\n", lux);
>
>