iio: light: tsl2591: fix lux calculation and precision, add I2C ID table, report saturation
Subsystems (2)
v4 cover iio: light: tsl2591: fix lux calculation and precision, add I2C ID table, report saturation
This series fixes several issues found while using the TSL2591 in company products (Raspberry Pi CM5) and when testing (x86_64 laptops with MCP2221 USB-I2C bridge).
The original field report found that the illumination value has always been 0.0 (caused by division by zero on ARM).
Patch 1 fixes divisions by zero in the lux calculation, which occur with LOW gain and short integration times, or in darkness when CH0 = 0.
Patch 2 adds an i2c_device_id table so the driver can be matched by name (e.g. via sysfs new_device) on kernels without CONFIG_OF.
Patch 3 makes the driver return an error instead of a bogus value when the sensor is saturated.
Patch 4 improves the formula for calculating lux values to make the returned value more accurate.
Patches 3 and 4 change existing behavior, I am OK with splitting the series into two, if that makes things better.
AI assistance: Claude Code (Claude Opus 5.5) was used for research on sensor behavior, datasheets and Adafruit library, to compare results produced by old and new formula, to draft some code for patches 1 and 4. I reviewed all of it and tested it on x86_64.
---
Changes in v4:
- no changes in the code
- drop "Assisted-by: LLM" tags (Andy)
- Patch 2: make commit message more to the point (Andy)
- Link to v3: https://patch.msgid.link/[email protected]
Changes in v3:
- Patch 2: Fix commit message, OF-only drivers can still be enumerated via DT or ACPI (PRP0001), only matching by name needs the I2C ID table (Andy)
- Patch 2: Drop Andy's Reviewed-by
- Rebase on current iio/testing
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2 (thanks to Andy Shevchenko for review):
- Patch 2: Use C99 initializers
- Patch 4: Keep variables in reversed xmas tree order
- Link to v1: https://lore.kernel.org/r/[email protected]/
To: Jonathan Cameron <[email protected]>
To: David Lechner <[email protected]>
To: Nuno Sá <[email protected]>
To: Andy Shevchenko <[email protected]>
To: Joe Sandom <[email protected]>
---
Jan Remes (4):
iio: light: tsl2591: don't divide by zero
iio: light: tsl2591: add I2C device ID table
iio: light: tsl2591: report saturation
iio: light: tsl2591: improve lux calculation precision
drivers/iio/light/tsl2591.c | 58 ++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 16 deletions(-)
---
base-commit: 87e28db23d0384f3995c43ecc5ae2118b371edb5
change-id: 20261014-first-patch-23099c6002da
Best regards,
Signature · 1 line
Jan Remes <[email protected]>
PATCH v4 1/4 iio: light: tsl2591: don't divide by zero
From: Jan Remes <[email protected]>
The TSL2591 has two raw light channels - CH0 and CH1. The algorithm for calculating illumination from the raw channels requires division by CH0 (which can be zero) and by "counts per lux" (CPL) value which is also zero for (gain = LOW, integration time <= 400 ms).
Furthermore, the algorithm assumes CH0 > CH1, which can be violated in almost complete darkness.
As a result, reading from 'in_illuminance_input' results in division-by-zero whenever CH0 is 0 (complete darkness) or whenever the parameters result in CPL being zero. This results in kernel oops on x86_64 and in reading the value as 0 on arm64.
Fix that by:
- reporting CH0 <= CH1 as zero (catches also CH0 = 0)
- updating the lux algorithm to only do the division at the end
--- drivers/iio/light/tsl2591.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-)
drivers/iio/light/tsl2591.c +22 -10
@@ -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>
@@ -362,6 +363,10 @@ static int tsl2591_wait_adc_complete(struct tsl2591_chip *chip)* Scale values to get more representative value of lux i.e. * lux = ((C0DATA - C1DATA) * (1000 - ((C1DATA * 1000) / C0DATA))) / CPL * +* CPL is not computed on its own: at 1x gain it is below 1 for integration +* times under 500 ms, so the division by LUX DF is folded into the final +* division instead. +* * Channel 0 = IR + Visible * Channel 1 = IR only */
@@ -373,8 +378,10 @@ static int tsl2591_read_channel_data(struct iio_dev *indio_dev,struct tsl2591_als_settings *settings = &chip->als_settings;struct i2c_client *client = chip->client;u8 als_data[TSL2591_NUM_DATA_REGISTERS ]; -int counts_per_lux, int_time_fval, gain_multi, lux; +int int_time_fval, gain_multi;u16 als_ch0, als_ch1; +u64 lux; +u32 rem;int ret; ret =tsl2591_wait_adc_complete (chip);
@@ -410,23 +417,28 @@ static int tsl2591_read_channel_data(struct iio_dev *indio_dev,return gain_multi; } - int_time_fval =TSL2591_FVAL_TO_MSEC (settings->als_int_time); -/* Calculate counts per lux value */ - counts_per_lux = (int_time_fval * gain_multi) /TSL2591_LUX_COEFFICIENT ; +/* No visible light, this also catches C0DATA == 0 */ +if (als_ch0 <= als_ch1) { + *val =0 ; + *val2 =0 ; +break ; + } -dev_dbg (&client->dev,"Counts Per Lux: %d\n" , counts_per_lux); + int_time_fval =TSL2591_FVAL_TO_MSEC (settings->als_int_time);/* Calculate lux value */ - lux = ((als_ch0 - als_ch1) * - (1000 - ((als_ch1 *1000 ) / als_ch0))) / counts_per_lux; + lux = (u64)(als_ch0 - als_ch1) * + (1000 - ((als_ch1 *1000 ) / als_ch0)) * +TSL2591_LUX_COEFFICIENT ; + lux =div_u64 (lux, int_time_fval * gain_multi); -dev_dbg (&client->dev,"Raw lux calculation: %d\n" , lux); +dev_dbg (&client->dev,"Raw lux calculation: %llu\n" , lux);/* Divide by 1000 to get real lux value before scaling */ - *val = lux /1000 ; + *val =div_u64_rem (lux,1000 , &rem);/* Get the decimal part of lux reading */ - *val2 = (lux - (*val *1000 )) *1000 ; + *val2 = rem *1000 ;break ;default :
Signature · 1 line
2.53.0
Re: [PATCH v4 1/4] iio: light: tsl2591: don't divide by zero
Quoted 19 lines · On Thu, 22 Oct 2026 20:56:40 +0200 Jan Remes <[email protected]> wrote:
From: Jan Remes <[email protected]>
The TSL2591 has two raw light channels - CH0 and CH1. The algorithm for calculating illumination from the raw channels requires division by CH0 (which can be zero) and by "counts per lux" (CPL) value which is also zero for (gain = LOW, integration time <= 400 ms).
Furthermore, the algorithm assumes CH0 > CH1, which can be violated in almost complete darkness.
As a result, reading from '
in_illuminance_input' results in division-by-zero whenever CH0 is 0 (complete darkness) or whenever the parameters result in CPL being zero. This results in kernel oops onx86_64and in reading the value as 0 on arm64.Fix that by:
- reporting CH0 <= CH1 as zero (catches also CH0 = 0)
- updating the lux algorithm to only do the division at the end
Quoted 5 lines
Fixes 2335f0d7c790 ("iio: light: Added AMS tsl2591 driver implementation")Reported-by Martin Triska <[email protected]>Reviewed-by Andy Shevchenko <[email protected]>Signed-off-by Jan Remes <[email protected]>
Applied to the fixes-togreg branch of iio.git
Thanks,
Jonathan
PATCH v4 2/4 iio: light: tsl2591: add I2C device ID table
In reply to Jan RemesFrom: Jan Remes <[email protected]>
Add an I2C ID table so that driver can match devices assigned via sysfs or through board info.
--- drivers/iio/light/tsl2591.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/iio/light/tsl2591.c +7 -0
@@ -1149,7 +1149,14 @@ static const struct of_device_id tsl2591_of_match[] = { };MODULE_DEVICE_TABLE (of, tsl2591_of_match); +static const struct i2c_device_id tsl2591_id[] = { + { .name ="tsl2591" }, + { } +}; +MODULE_DEVICE_TABLE (i2c, tsl2591_id); +static struct i2c_driver tsl2591_driver = { + .id_table = tsl2591_id, .driver = { .name ="tsl2591" , .pm =pm_ptr (&tsl2591_pm_ops),
Signature · 1 line
2.53.0
Jonathan Cameron
maintainerRe: [PATCH v4 2/4] iio: light: tsl2591: add I2C device ID table
Quoted 6 lines · On Thu, 22 Oct 2026 20:56:41 +0200 Jan Remes <[email protected]> wrote:
From: Jan Remes <[email protected]>
Add an I2C ID table so that driver can match devices assigned via sysfs or through board info.
Signed-off-by Jan Remes <[email protected]>
This one is independent enough to queue now so applied to the testing branch of iio.git. I think the other two need to wait for the fix to work it's way to upstream.
J
Quoted 24 lines
--- drivers/iio/light/tsl2591.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/iio/light/tsl2591.c +7
-0@@ -1149,7 +1149,14 @@ static const struct of_device_id tsl2591_of_match[] = { };MODULE_DEVICE_TABLE (of, tsl2591_of_match); +static const struct i2c_device_id tsl2591_id[] = { + { .name ="tsl2591" }, + { } +}; +MODULE_DEVICE_TABLE (i2c, tsl2591_id); +static struct i2c_driver tsl2591_driver = { + .id_table = tsl2591_id, .driver = { .name ="tsl2591" , .pm =pm_ptr (&tsl2591_pm_ops),
PATCH v4 3/4 iio: light: tsl2591: report saturation
In reply to Jan RemesFrom: Jan Remes <[email protected]>
When the TSL2591 sensor is saturated, it reports its maximum value (specific one for 100ms integration time, UINT16_MAX otherwise).
When either raw channel becomes saturated, return -EOVERFLOW (like as73211 and si1133 do) instead of calculating an invalid number when reading 'in_illuminance_input'. Keep existing behavior on raw channels.
Note: Datasheet versions differ in the saturation number for 100ms. Old 2013 datasheet lists it as 37888, newer versions say 36863. Use the new value although the hardware can report up to 37888.
--- drivers/iio/light/tsl2591.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/iio/light/tsl2591.c +11 -1
@@ -139,7 +139,9 @@/* TSL2591 maximum values */ #define TSL2591_MAX_ALS_INT_TIME_MS 600 +/* Maximal values per datasheet [v3-00] 2023-Feb-08 */ #define TSL2591_ALS_MAX_VALUE (BIT(16) - 1) +#define TSL2591_ALS_MAX_VALUE_100MS 36863/* * LUX calculations;
@@ -379,7 +381,7 @@ static int tsl2591_read_channel_data(struct iio_dev *indio_dev,struct i2c_client *client = chip->client;u8 als_data[TSL2591_NUM_DATA_REGISTERS ];int int_time_fval, gain_multi; -u16 als_ch0, als_ch1; +u16 als_ch0, als_ch1, als_max;u64 lux;u32 rem;int ret;
@@ -417,6 +419,14 @@ static int tsl2591_read_channel_data(struct iio_dev *indio_dev,return gain_multi; } +/* Check for overflow; special case for 100ms integration time */ + als_max =TSL2591_ALS_MAX_VALUE ; +if (settings->als_int_time ==TSL2591_CTRL_ALS_INTEGRATION_100MS ) + als_max =TSL2591_ALS_MAX_VALUE_100MS ; + +if (als_ch0 >= als_max || als_ch1 >= als_max) +return -EOVERFLOW ; +/* No visible light, this also catches C0DATA == 0 */ if (als_ch0 <= als_ch1) { *val =0 ;
Signature · 1 line
2.53.0
PATCH v4 4/4 iio: light: tsl2591: improve lux calculation precision
In reply to Jan RemesFrom: Jan Remes <[email protected]>
The lux calculation computes the ratio CH1 / CH0 as a truncated integer (CH1 * 1000 / CH0) before using it. When CH1 is close to CH0, the truncation makes the result off by up to ~35%, e.g. CH0 = 56097, CH1 = 55973 at LOW gain and 100ms gives 1.517 lux instead of 1.118 lux.
The formula (CH0 - CH1) * (1 - CH1 / CH0) / CPL is equal to
(CH0 - CH1)^2 * DF / (CH0 * ATIME_ms * AGAIN)
which needs no intermediate ratio. Compute it like that, with a single division at the end.
Also report the result in micro-lux instead of milli-lux. At MAX gain and 600ms one count is well below 1 mlx, so the milli-lux resolution throws away information. The numerator still fits into u64.
--- drivers/iio/light/tsl2591.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-)
drivers/iio/light/tsl2591.c +12 -15
@@ -21,6 +21,7 @@#include <linux/mutex.h> #include <linux/pm_runtime.h> #include <linux/sysfs.h> +#include <linux/units.h> #include <linux/unaligned.h>
@@ -362,12 +363,12 @@ static int tsl2591_wait_adc_complete(struct tsl2591_chip *chip)* Counts Per Lux (CPL) = (ATIME_ms * AGAIN) / LUX DF * lux = ((C0DATA - C1DATA) * (1 - (C1DATA / C0DATA))) / CPL * -* Scale values to get more representative value of lux i.e. -* lux = ((C0DATA - C1DATA) * (1000 - ((C1DATA * 1000) / C0DATA))) / CPL +* Since (C0DATA - C1DATA) * (1 - (C1DATA / C0DATA)) equals +* (C0DATA - C1DATA)^2 / C0DATA, this is computed in micro-lux as +* lux = ((C0DATA - C1DATA)^2 * LUX DF * 10^6) / (C0DATA * ATIME_ms * AGAIN) * * CPL is not computed on its own: at 1x gain it is below 1 for integration -* times under 500 ms, so the division by LUX DF is folded into the final -* division instead. +* times under 500 ms, so all divisions are done at the end. * * Channel 0 = IR + Visible * Channel 1 = IR only
@@ -382,8 +383,8 @@ static int tsl2591_read_channel_data(struct iio_dev *indio_dev,u8 als_data[TSL2591_NUM_DATA_REGISTERS ];int int_time_fval, gain_multi;u16 als_ch0, als_ch1, als_max; +u32 diff, rem;u64 lux; -u32 rem;int ret; ret =tsl2591_wait_adc_complete (chip);
@@ -436,19 +437,15 @@ static int tsl2591_read_channel_data(struct iio_dev *indio_dev, int_time_fval =TSL2591_FVAL_TO_MSEC (settings->als_int_time); -/* Calculate lux value */ - lux = (u64)(als_ch0 - als_ch1) * - (1000 - ((als_ch1 *1000 ) / als_ch0)) * -TSL2591_LUX_COEFFICIENT ; - lux =div_u64 (lux, int_time_fval * gain_multi); +/* Calculate lux value in micro-lux */ + diff = als_ch0 - als_ch1; + lux = (u64 )diff * diff *TSL2591_LUX_COEFFICIENT *MICRO ; + lux =div64_u64 (lux, (u64 )als_ch0 * int_time_fval * gain_multi);dev_dbg (&client->dev,"Raw lux calculation: %llu\n" , lux); -/* Divide by 1000 to get real lux value before scaling */ - *val =div_u64_rem (lux,1000 , &rem); - -/* Get the decimal part of lux reading */ - *val2 = rem *1000 ; + *val =div_u64_rem (lux,MICRO , &rem); + *val2 = rem;break ;default :
Signature · 1 line
2.53.0