Re: [PATCH v4 4/4] iio: light: ltr501: Add support for the LTR-659PS-01 proximity sensor

From: Arnav Kapoor

Date: Mon Nov 16 2026 - 14:22:12 EST


On Sun, Nov 15, 2026 at 10:37:41AM +0200, Svyatoslav Ryhel wrote:
> - if (test_bit(0, indio_dev->active_scan_mask) ||
> - test_bit(1, indio_dev->active_scan_mask))
> - mask |= LTR501_STATUS_ALS_RDY;
> - if (test_bit(2, indio_dev->active_scan_mask))
> - mask |= LTR501_STATUS_PS_RDY;
> + iio_for_each_active_channel(indio_dev, chan_idx) {
> + const struct iio_chan_spec *chan = &indio_dev->channels[chan_idx];

iio_for_each_active_channel() walks the bits of active_scan_mask, so
chan_idx is a scan index, not an index into indio_dev->channels[].
The two only match when every channel before it has scan_index ==
array position. That holds for ltr659_channels[], but not for
ltr501_channels[] (also used by ltr559) or ltr301_channels[]: entry 0
there is LTR501_LIGHT_CHANNEL() with .scan_index = -1, so scan bit N
maps to channels[N + 1].

On ltr501/ltr559 that gives:

scan bit 0 (intensity_both) -> channels[0], IIO_LIGHT: no RDY bit set
scan bit 1 (intensity_ir) -> channels[1], IIO_INTENSITY: ALS_RDY (ok)
scan bit 2 (proximity) -> channels[2], IIO_INTENSITY: ALS_RDY
scan bit 3 (timestamp) -> channels[3], IIO_PROXIMITY: PS_RDY

So a proximity-only buffer waits for ALS_RDY and never reads PS (the
"if (mask & LTR501_STATUS_PS_RDY)" block is skipped), and pushes
zeros; intensity_both alone gives mask == 0 and also pushes zeros;
and enabling the timestamp makes an ALS-only capture wait for PS_RDY
as well. On ltr301 bit 0 is likewise lost.

> + if (chan->type == IIO_INTENSITY)
> + mask |= LTR501_STATUS_ALS_RDY;
> +
> + if (chan->type == IIO_PROXIMITY)
> + mask |= LTR501_STATUS_PS_RDY;
> + }

This is what iio_find_channel_from_si() is for, as Jonathan suggested
on v3. With the export patch you sent separately, something like

iio_for_each_active_channel(indio_dev, scan_idx) {
const struct iio_chan_spec *chan =
iio_find_channel_from_si(indio_dev, scan_idx);
...
}

(it also returns the timestamp channel, which matches neither type)
would keep the type-based check working for all four chips. Since
this patch is the export's only user, it is probably best to fold the
export into this series.

Thanks,
Arnav