Linux IIO development
 help / color / mirror / Atom feed
* [PATCH] iio: light: tsl2772: fix ALS calibscale readback
@ 2026-07-24 23:46 Yuanshen Cao
  2026-07-25 21:27 ` David Lechner
  2026-07-27  2:14 ` Jonathan Cameron
  0 siblings, 2 replies; 4+ messages in thread
From: Yuanshen Cao @ 2026-07-24 23:46 UTC (permalink / raw)
  To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko
  Cc: linux-iio, linux-kernel, Yuanshen Cao

The read_raw() implementation uses IIO_LIGHT to distinguish between the
ambient light and proximity channels when handling
IIO_CHAN_INFO_CALIBSCALE.

However, the ALS channel is registered as IIO_INTENSITY, while
write_raw() correctly writes to IIO_INTENSITY. As a result, reading
in_intensity0_calibscale incorrectly returns the proximity gain instead
of the ALS gain.

This causes the following user-visible behavior:
- Writing in_intensity0_calibscale appears to have no effect because the
  readback reports the proximity gain.
- Writing in_proximity0_calibscale causes both in_proximity0_calibscale
  and in_intensity0_calibscale to report the same value.

Fix this by checking for IIO_INTENSITY in read_raw(), matching the
channel definition and the existing write_raw() implementation.

Signed-off-by: Yuanshen Cao <alex.caoys@gmail.com>
---
 drivers/iio/light/tsl2772.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/light/tsl2772.c b/drivers/iio/light/tsl2772.c
index 244f44379c36..2287585711c6 100644
--- a/drivers/iio/light/tsl2772.c
+++ b/drivers/iio/light/tsl2772.c
@@ -1274,7 +1274,7 @@ static int tsl2772_read_raw(struct iio_dev *indio_dev,
 		}
 		break;
 	case IIO_CHAN_INFO_CALIBSCALE:
-		if (chan->type == IIO_LIGHT)
+		if (chan->type == IIO_INTENSITY)
 			*val = tsl2772_als_gain[chip->settings.als_gain];
 		else
 			*val = tsl2772_prox_gain[chip->settings.prox_gain];

---
base-commit: 1590cf0329716306e948a8fc29f1d3ee87d3989f
change-id: 20260724-tsl2772-calibscale-fix-0cc42518d5fc

Best regards,
--  
Yuanshen Cao <alex.caoys@gmail.com>


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] iio: light: tsl2772: fix ALS calibscale readback
  2026-07-24 23:46 [PATCH] iio: light: tsl2772: fix ALS calibscale readback Yuanshen Cao
@ 2026-07-25 21:27 ` David Lechner
  2026-07-27  2:14 ` Jonathan Cameron
  1 sibling, 0 replies; 4+ messages in thread
From: David Lechner @ 2026-07-25 21:27 UTC (permalink / raw)
  To: Yuanshen Cao, Jonathan Cameron, Nuno Sá, Andy Shevchenko
  Cc: linux-iio, linux-kernel

On 7/24/26 6:46 PM, Yuanshen Cao wrote:
> The read_raw() implementation uses IIO_LIGHT to distinguish between the
> ambient light and proximity channels when handling
> IIO_CHAN_INFO_CALIBSCALE.
> 
> However, the ALS channel is registered as IIO_INTENSITY, while
> write_raw() correctly writes to IIO_INTENSITY. As a result, reading
> in_intensity0_calibscale incorrectly returns the proximity gain instead
> of the ALS gain.
> 
> This causes the following user-visible behavior:
> - Writing in_intensity0_calibscale appears to have no effect because the
>   readback reports the proximity gain.
> - Writing in_proximity0_calibscale causes both in_proximity0_calibscale
>   and in_intensity0_calibscale to report the same value.
> 
> Fix this by checking for IIO_INTENSITY in read_raw(), matching the
> channel definition and the existing write_raw() implementation.
> 
> Signed-off-by: Yuanshen Cao <alex.caoys@gmail.com>
> ---
>  drivers/iio/light/tsl2772.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/light/tsl2772.c b/drivers/iio/light/tsl2772.c
> index 244f44379c36..2287585711c6 100644
> --- a/drivers/iio/light/tsl2772.c
> +++ b/drivers/iio/light/tsl2772.c
> @@ -1274,7 +1274,7 @@ static int tsl2772_read_raw(struct iio_dev *indio_dev,
>  		}
>  		break;
>  	case IIO_CHAN_INFO_CALIBSCALE:
> -		if (chan->type == IIO_LIGHT)
> +		if (chan->type == IIO_INTENSITY)
>  			*val = tsl2772_als_gain[chip->settings.als_gain];
>  		else
>  			*val = tsl2772_prox_gain[chip->settings.prox_gain];
> 
> ---
> base-commit: 1590cf0329716306e948a8fc29f1d3ee87d3989f
> change-id: 20260724-tsl2772-calibscale-fix-0cc42518d5fc
> 
> Best regards,
> --  
> Yuanshen Cao <alex.caoys@gmail.com>
> 

Reviewed-by: David Lechner <dlechner@baylibre.com>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] iio: light: tsl2772: fix ALS calibscale readback
  2026-07-24 23:46 [PATCH] iio: light: tsl2772: fix ALS calibscale readback Yuanshen Cao
  2026-07-25 21:27 ` David Lechner
@ 2026-07-27  2:14 ` Jonathan Cameron
  2026-07-27  4:40   ` Yuanshen Cao
  1 sibling, 1 reply; 4+ messages in thread
From: Jonathan Cameron @ 2026-07-27  2:14 UTC (permalink / raw)
  To: Yuanshen Cao
  Cc: David Lechner, Nuno Sá, Andy Shevchenko, linux-iio,
	linux-kernel

On Fri, 24 Jul 2026 23:46:01 +0000
Yuanshen Cao <alex.caoys@gmail.com> wrote:

> The read_raw() implementation uses IIO_LIGHT to distinguish between the
> ambient light and proximity channels when handling
> IIO_CHAN_INFO_CALIBSCALE.
> 
> However, the ALS channel is registered as IIO_INTENSITY, while
> write_raw() correctly writes to IIO_INTENSITY. As a result, reading
> in_intensity0_calibscale incorrectly returns the proximity gain instead
> of the ALS gain.
> 
> This causes the following user-visible behavior:
> - Writing in_intensity0_calibscale appears to have no effect because the
>   readback reports the proximity gain.
> - Writing in_proximity0_calibscale causes both in_proximity0_calibscale
>   and in_intensity0_calibscale to report the same value.
> 
> Fix this by checking for IIO_INTENSITY in read_raw(), matching the
> channel definition and the existing write_raw() implementation.
> 
> Signed-off-by: Yuanshen Cao <alex.caoys@gmail.com>
Hi,

Fix looks good and the explanation correct.  Was this an AI found
one, or did you either spot this whilst reading the code or best
of all saw it in practice and went looking for what is wrong?

Give it is fix, what is missing is a suitable Fixes tag.
Please see if you can figure out what that should be.
No need to send a v2 unless anything else comes up in review.
Just reply to this thread with the Fixes tag so I'll get it
when I use b4 to pick up the patch

Thanks,

Jonathan

> ---
>  drivers/iio/light/tsl2772.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/light/tsl2772.c b/drivers/iio/light/tsl2772.c
> index 244f44379c36..2287585711c6 100644
> --- a/drivers/iio/light/tsl2772.c
> +++ b/drivers/iio/light/tsl2772.c
> @@ -1274,7 +1274,7 @@ static int tsl2772_read_raw(struct iio_dev *indio_dev,
>  		}
>  		break;
>  	case IIO_CHAN_INFO_CALIBSCALE:
> -		if (chan->type == IIO_LIGHT)
> +		if (chan->type == IIO_INTENSITY)
>  			*val = tsl2772_als_gain[chip->settings.als_gain];
>  		else
>  			*val = tsl2772_prox_gain[chip->settings.prox_gain];
> 
> ---
> base-commit: 1590cf0329716306e948a8fc29f1d3ee87d3989f
> change-id: 20260724-tsl2772-calibscale-fix-0cc42518d5fc
> 
> Best regards,
> --  
> Yuanshen Cao <alex.caoys@gmail.com>
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] iio: light: tsl2772: fix ALS calibscale readback
  2026-07-27  2:14 ` Jonathan Cameron
@ 2026-07-27  4:40   ` Yuanshen Cao
  0 siblings, 0 replies; 4+ messages in thread
From: Yuanshen Cao @ 2026-07-27  4:40 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá, Andy Shevchenko, linux-iio,
	linux-kernel

On Mon, Jul 27, 2026 at 03:14:07AM +0100, Jonathan Cameron wrote:
> On Fri, 24 Jul 2026 23:46:01 +0000
> Yuanshen Cao <alex.caoys@gmail.com> wrote:
> 
> > The read_raw() implementation uses IIO_LIGHT to distinguish between the
> > ambient light and proximity channels when handling
> > IIO_CHAN_INFO_CALIBSCALE.
> > 
> > However, the ALS channel is registered as IIO_INTENSITY, while
> > write_raw() correctly writes to IIO_INTENSITY. As a result, reading
> > in_intensity0_calibscale incorrectly returns the proximity gain instead
> > of the ALS gain.
> > 
> > This causes the following user-visible behavior:
> > - Writing in_intensity0_calibscale appears to have no effect because the
> >   readback reports the proximity gain.
> > - Writing in_proximity0_calibscale causes both in_proximity0_calibscale
> >   and in_intensity0_calibscale to report the same value.
> > 
> > Fix this by checking for IIO_INTENSITY in read_raw(), matching the
> > channel definition and the existing write_raw() implementation.
> > 
> > Signed-off-by: Yuanshen Cao <alex.caoys@gmail.com>
> Hi,
> 
> Fix looks good and the explanation correct.  Was this an AI found
> one, or did you either spot this whilst reading the code or best
> of all saw it in practice and went looking for what is wrong?

Hi Jonathan,

It's actually a bit of both. I first noticed the strange calibscale
behavior while testing a device with this sensor. Since most part of
this driver has been around for 7+ years, I wasn't expecting to find a
bug there. I asked ChatGPT for some ideas about what might explain the
behavior, and it pointed out the mismatch. I then went back and verified
it, and sure enough, it turned out to be a 14-year-old bug. After that,
I checked the rest of the file for similar issues, but this appears to
be the only instance. Also this was tested on the device and it works
as intended now.

> Give it is fix, what is missing is a suitable Fixes tag.
> Please see if you can figure out what that should be.
> No need to send a v2 unless anything else comes up in review.
> Just reply to this thread with the Fixes tag so I'll get it
> when I use b4 to pick up the patch

Thanks for pointing this out.
The missing tag is:

Fixes: 3c97c08b5735 ("iio: light: add TAOS tsl2x7x driver")

Best,

Yuanshen (Alex) Cao

> Thanks,
> 
> Jonathan
> 
> > ---
> >  drivers/iio/light/tsl2772.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/iio/light/tsl2772.c b/drivers/iio/light/tsl2772.c
> > index 244f44379c36..2287585711c6 100644
> > --- a/drivers/iio/light/tsl2772.c
> > +++ b/drivers/iio/light/tsl2772.c
> > @@ -1274,7 +1274,7 @@ static int tsl2772_read_raw(struct iio_dev *indio_dev,
> >  		}
> >  		break;
> >  	case IIO_CHAN_INFO_CALIBSCALE:
> > -		if (chan->type == IIO_LIGHT)
> > +		if (chan->type == IIO_INTENSITY)
> >  			*val = tsl2772_als_gain[chip->settings.als_gain];
> >  		else
> >  			*val = tsl2772_prox_gain[chip->settings.prox_gain];
> > 
> > ---
> > base-commit: 1590cf0329716306e948a8fc29f1d3ee87d3989f
> > change-id: 20260724-tsl2772-calibscale-fix-0cc42518d5fc
> > 
> > Best regards,
> > --  
> > Yuanshen Cao <alex.caoys@gmail.com>
> > 
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-07-27  4:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-24 23:46 [PATCH] iio: light: tsl2772: fix ALS calibscale readback Yuanshen Cao
2026-07-25 21:27 ` David Lechner
2026-07-27  2:14 ` Jonathan Cameron
2026-07-27  4:40   ` Yuanshen Cao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox