[PATCH 09/15] iio: light: tsl2591: don't divide by zero in the dark

From: Greg Kroah-Hartman

Date: Tue Oct 27 2026 - 12:25:01 EST


From: Josef Bacik <josef@xxxxxxxxxxxxxx>

The computation of the illuminance divides by the count of channel 0.
That is the channel which sees the whole spectrum: in complete darkness it
reads zero, and reading in_illuminance_input divides by zero. Depending
on the architecture this is an oops, a warning with a backtrace, or goes
unnoticed.

Report 0 lux in that case.

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: Josef Bacik <josef@xxxxxxxxxxxxxx>
Cc: Matheus Silveira <matheus.feitosa@xxxxxx>
Cc: Lucas Rabaquim <lucas.rabaquim@xxxxxx>
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 | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/drivers/iio/light/tsl2591.c b/drivers/iio/light/tsl2591.c
index ef3ed9635a1e..286dc09e170f 100644
--- a/drivers/iio/light/tsl2591.c
+++ b/drivers/iio/light/tsl2591.c
@@ -416,6 +416,13 @@ static int tsl2591_read_channel_data(struct iio_dev *indio_dev,

dev_dbg(&client->dev, "Counts Per Lux: %d\n", counts_per_lux);

+ /* No light at all: the formula below divides by als_ch0 */
+ if (!als_ch0) {
+ *val = 0;
+ *val2 = 0;
+ break;
+ }
+
/* Calculate lux value */
lux = ((als_ch0 - als_ch1) *
(1000 - ((als_ch1 * 1000) / als_ch0))) / counts_per_lux;

--
2.56.0