* [PATCH v2 1/6] iio: proximity: sx_common: fix raw reads while the trigger is in use
2026-11-22 7:46 [PATCH v2 0/6] iio: proximity: sx93xx: fix raw reads, thresholds, hysteresis and gain Arnav Kapoor
@ 2026-11-22 7:46 ` Arnav Kapoor
2026-11-22 7:46 ` [PATCH v2 2/6] iio: proximity: sx9324: fix proximity threshold writes Arnav Kapoor
` (4 subsequent siblings)
5 siblings, 0 replies; 13+ messages in thread
From: Arnav Kapoor @ 2026-11-22 7:46 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Andy Shevchenko, Gwendal Grignou, Stephen Boyd,
linux-iio, linux-kernel, Arnav Kapoor
sx_common_read_proximity() only reinitialises the completion after
waiting on it, so a complete() that arrives when no read is waiting is
consumed by the next read, which then returns old data without waiting
for a conversion that includes the channel it just enabled. This
happens in two ways:
- The device trigger has no validate_device(), so another IIO device
can use it. While it does, set_trigger_state() keeps the CONVDONE
interrupt enabled and the IRQ thread calls complete() for every
conversion.
- A read interrupted by a signal reinitialises the completion and
returns, but a CONVDONE interrupt already raised can still complete
it once the mutex is dropped.
Also, when it is done, the read disables the CONVDONE interrupt
unconditionally, which stops the trigger for the other device.
Reinitialise the completion before enabling the interrupt, and leave
the interrupt enabled when the trigger is, as set_trigger_state()
already does for pending raw reads.
The interrupted-wait case was reported by the Sashiko AI reviewer on an
unrelated sx9324 patch.
Fixes: 72ad02b15d63 ("iio: Add SEMTECH SX9310/9311 sensor driver")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/sx_common.c | 25 +++++++++++++++++++------
1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/drivers/iio/proximity/sx_common.c b/drivers/iio/proximity/sx_common.c
index 4d4937395d9a..66d06435da05 100644
--- a/drivers/iio/proximity/sx_common.c
+++ b/drivers/iio/proximity/sx_common.c
@@ -167,6 +167,15 @@ static int sx_common_put_event_channel(struct sx_common_data *data, int channel)
data->chan_event & ~BIT(channel));
}
+/* The trigger needs the CONVDONE interrupt too, leave it on while in use. */
+static int sx_common_put_convdone_irq(struct sx_common_data *data)
+{
+ if (data->trigger_enabled)
+ return 0;
+
+ return sx_common_disable_irq(data, SX_COMMON_CONVDONE_IRQ);
+}
+
/**
* sx_common_read_proximity() - Read raw proximity value.
* @data: Internal data
@@ -188,18 +197,22 @@ int sx_common_read_proximity(struct sx_common_data *data,
if (ret)
goto out;
+ /*
+ * Forget conversions that completed before this read, e.g. while the
+ * trigger was in use, so that we wait for a fresh one.
+ */
+ reinit_completion(&data->completion);
+
ret = sx_common_enable_irq(data, SX_COMMON_CONVDONE_IRQ);
if (ret)
goto out_put_channel;
mutex_unlock(&data->mutex);
- if (data->client->irq) {
+ if (data->client->irq)
ret = wait_for_completion_interruptible(&data->completion);
- reinit_completion(&data->completion);
- } else {
+ else
ret = data->chip_info->ops.wait_for_sample(data);
- }
mutex_lock(&data->mutex);
@@ -212,7 +225,7 @@ int sx_common_read_proximity(struct sx_common_data *data,
*val = sign_extend32(be16_to_cpu(rawval), chan->scan_type.realbits - 1);
- ret = sx_common_disable_irq(data, SX_COMMON_CONVDONE_IRQ);
+ ret = sx_common_put_convdone_irq(data);
if (ret)
goto out_put_channel;
@@ -225,7 +238,7 @@ int sx_common_read_proximity(struct sx_common_data *data,
return IIO_VAL_INT;
out_disable_irq:
- sx_common_disable_irq(data, SX_COMMON_CONVDONE_IRQ);
+ sx_common_put_convdone_irq(data);
out_put_channel:
sx_common_put_read_channel(data, chan->channel);
out:
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread* [PATCH v2 2/6] iio: proximity: sx9324: fix proximity threshold writes
2026-11-22 7:46 [PATCH v2 0/6] iio: proximity: sx93xx: fix raw reads, thresholds, hysteresis and gain Arnav Kapoor
2026-11-22 7:46 ` [PATCH v2 1/6] iio: proximity: sx_common: fix raw reads while the trigger is in use Arnav Kapoor
@ 2026-11-22 7:46 ` Arnav Kapoor
2026-11-23 11:11 ` Andy Shevchenko
2026-11-22 7:46 ` [PATCH v2 3/6] iio: proximity: sx9324: reject negative hysteresis values Arnav Kapoor
` (3 subsequent siblings)
5 siblings, 1 reply; 13+ messages in thread
From: Arnav Kapoor @ 2026-11-22 7:46 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Andy Shevchenko, Gwendal Grignou, Stephen Boyd,
linux-iio, linux-kernel, Arnav Kapoor
sx9324_read_thresh() reports a PROX_THRESH register value r as r for
r <= 1 and as r * r / 2 otherwise, and the division rounds down for
odd r. sx9324_write_thresh() inverts that with int_sqrt(2 * val), which
is one register value too low when 2 * val == r * r - 1, so writing
back the threshold read from an odd register value lowers it by one:
setting the threshold to the value it already has changes it, for 127
of the 256 register values (3, 5, 7, ...).
Use int_sqrt(2 * val + 1), which gives the largest register value whose
threshold does not exceed val: for r > 1, in integer arithmetic,
r * r / 2 <= val is the same as r * r <= 2 * val + 1. The result only
differs from int_sqrt(2 * val) on those edges. Checked for every val
from 0 to 65535.
Also reject negative values. They are converted to unsigned int, and
2 * val wraps at 32 bits on both 32-bit and 64-bit architectures, so
e.g. INT_MIN is written as 0 and values just above INT_MIN as small
thresholds instead of being rejected.
Fixes: 4c18a890dff8 ("iio:proximity:sx9324: Add SX9324 support")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/sx9324.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/drivers/iio/proximity/sx9324.c b/drivers/iio/proximity/sx9324.c
index 93f9577bbda7..57fe78089167 100644
--- a/drivers/iio/proximity/sx9324.c
+++ b/drivers/iio/proximity/sx9324.c
@@ -613,12 +613,17 @@ static int sx9324_read_event_val(struct iio_dev *indio_dev,
static int sx9324_write_thresh(struct sx_common_data *data,
const struct iio_chan_spec *chan, int _val)
{
- unsigned int reg, val = _val;
+ unsigned int reg, val;
+
+ if (_val < 0)
+ return -EINVAL;
reg = SX9324_REG_PROX_CTRL6 + chan->channel / 2;
+ val = _val;
- if (val >= 1)
- val = int_sqrt(2 * val);
+ /* Inverse of read_thresh(), so that writing back a read value is a no-op */
+ if (val > 1)
+ val = int_sqrt(2 * val + 1);
if (val > 0xff)
return -EINVAL;
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH v2 2/6] iio: proximity: sx9324: fix proximity threshold writes
2026-11-22 7:46 ` [PATCH v2 2/6] iio: proximity: sx9324: fix proximity threshold writes Arnav Kapoor
@ 2026-11-23 11:11 ` Andy Shevchenko
2026-11-23 11:16 ` Andy Shevchenko
2026-11-23 13:17 ` Arnav Kapoor
0 siblings, 2 replies; 13+ messages in thread
From: Andy Shevchenko @ 2026-11-23 11:11 UTC (permalink / raw)
To: Arnav Kapoor
Cc: Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, Gwendal Grignou, Stephen Boyd, linux-iio,
linux-kernel
On Sun, Nov 22, 2026 at 01:16:42PM +0530, Arnav Kapoor wrote:
> sx9324_read_thresh() reports a PROX_THRESH register value r as r for
> r <= 1 and as r * r / 2 otherwise, and the division rounds down for
> odd r. sx9324_write_thresh() inverts that with int_sqrt(2 * val), which
> is one register value too low when 2 * val == r * r - 1, so writing
> back the threshold read from an odd register value lowers it by one:
> setting the threshold to the value it already has changes it, for 127
> of the 256 register values (3, 5, 7, ...).
>
> Use int_sqrt(2 * val + 1), which gives the largest register value whose
> threshold does not exceed val: for r > 1, in integer arithmetic,
> r * r / 2 <= val is the same as r * r <= 2 * val + 1. The result only
> differs from int_sqrt(2 * val) on those edges. Checked for every val
> from 0 to 65535.
>
> Also reject negative values. They are converted to unsigned int, and
> 2 * val wraps at 32 bits on both 32-bit and 64-bit architectures, so
> e.g. INT_MIN is written as 0 and values just above INT_MIN as small
> thresholds instead of being rejected.
...
> {
> - unsigned int reg, val = _val;
> + unsigned int reg, val;
> +
> + if (_val < 0)
> + return -EINVAL;
>
> reg = SX9324_REG_PROX_CTRL6 + chan->channel / 2;
> + val = _val;
Why not simply
val = _val;
if (val < 0)
return -EINVAL;
...
reg = SX9324_REG_PROX_CTRL6 + chan->channel / 2;
Note, reg might still be not used, we have more validations and with your
approach the reg assignment is in the middle, confusing...
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH v2 2/6] iio: proximity: sx9324: fix proximity threshold writes
2026-11-23 11:11 ` Andy Shevchenko
@ 2026-11-23 11:16 ` Andy Shevchenko
2026-11-23 13:17 ` Arnav Kapoor
1 sibling, 0 replies; 13+ messages in thread
From: Andy Shevchenko @ 2026-11-23 11:16 UTC (permalink / raw)
To: Arnav Kapoor
Cc: Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, Gwendal Grignou, Stephen Boyd, linux-iio,
linux-kernel
On Mon, Nov 23, 2026 at 01:11:11PM +0200, Andy Shevchenko wrote:
> On Sun, Nov 22, 2026 at 01:16:42PM +0530, Arnav Kapoor wrote:
...
> > {
> > - unsigned int reg, val = _val;
> > + unsigned int reg, val;
> > +
> > + if (_val < 0)
> > + return -EINVAL;
> >
> > reg = SX9324_REG_PROX_CTRL6 + chan->channel / 2;
> > + val = _val;
>
>
> Why not simply
>
> val = _val;
> if (val < 0)
> return -EINVAL;
Ah, see the comment I gave in the other patch.
> ...
> reg = SX9324_REG_PROX_CTRL6 + chan->channel / 2;
>
> Note, reg might still be not used, we have more validations and with your
> approach the reg assignment is in the middle, confusing...
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH v2 2/6] iio: proximity: sx9324: fix proximity threshold writes
2026-11-23 11:11 ` Andy Shevchenko
2026-11-23 11:16 ` Andy Shevchenko
@ 2026-11-23 13:17 ` Arnav Kapoor
1 sibling, 0 replies; 13+ messages in thread
From: Arnav Kapoor @ 2026-11-23 13:17 UTC (permalink / raw)
To: Andy Shevchenko
Cc: jic23, dlechner, nuno.sa, andy, gwendal, swboyd, linux-iio, linux-kernel
On Mon, Nov 23, 2026 at 01:11:11PM +0200, Andy Shevchenko wrote:
> On Sun, Nov 22, 2026 at 01:16:42PM +0530, Arnav Kapoor wrote:
...
> Why not simply
>
> val = _val;
> if (val < 0)
> return -EINVAL;
val is unsigned int, so I think that check would never be true; it has
to test _val, or be the val > INT_MAX form from your comment on 3/6.
Whichever form Jonathan prefers there, I'll use the same one in all four.
> ...
> reg = SX9324_REG_PROX_CTRL6 + chan->channel / 2;
>
> Note, reg might still be not used, we have more validations and with your
> approach the reg assignment is in the middle, confusing...
Agreed, reg belongs after the val > 0xff check, right before the
guard(). I'll fix it in v3 as soon as I get the val verdict.
Thanks,
Arnav
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 3/6] iio: proximity: sx9324: reject negative hysteresis values
2026-11-22 7:46 [PATCH v2 0/6] iio: proximity: sx93xx: fix raw reads, thresholds, hysteresis and gain Arnav Kapoor
2026-11-22 7:46 ` [PATCH v2 1/6] iio: proximity: sx_common: fix raw reads while the trigger is in use Arnav Kapoor
2026-11-22 7:46 ` [PATCH v2 2/6] iio: proximity: sx9324: fix proximity threshold writes Arnav Kapoor
@ 2026-11-22 7:46 ` Arnav Kapoor
2026-11-23 11:15 ` Andy Shevchenko
2026-11-22 7:46 ` [PATCH v2 4/6] iio: proximity: sx9360: fix hardware gain encoding Arnav Kapoor
` (2 subsequent siblings)
5 siblings, 1 reply; 13+ messages in thread
From: Arnav Kapoor @ 2026-11-22 7:46 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Andy Shevchenko, Gwendal Grignou, Stephen Boyd,
linux-iio, linux-kernel, Arnav Kapoor
sx9324_write_hysteresis() copies the value into an unsigned int before
comparing it with the threshold, so a negative value becomes a large
one, passes the "val >= pthresh >> 2" check and sets the largest
hysteresis instead of being rejected. The comparison is done in
unsigned int, so this happens on both 32-bit and 64-bit architectures.
Return -EINVAL for negative values, before converting them.
Fixes: 4c18a890dff8 ("iio:proximity:sx9324: Add SX9324 support")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/sx9324.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/drivers/iio/proximity/sx9324.c b/drivers/iio/proximity/sx9324.c
index 57fe78089167..35f0cca5ede3 100644
--- a/drivers/iio/proximity/sx9324.c
+++ b/drivers/iio/proximity/sx9324.c
@@ -636,9 +636,14 @@ static int sx9324_write_thresh(struct sx_common_data *data,
static int sx9324_write_hysteresis(struct sx_common_data *data,
const struct iio_chan_spec *chan, int _val)
{
- unsigned int hyst, val = _val;
+ unsigned int hyst, val;
int ret, pthresh;
+ if (_val < 0)
+ return -EINVAL;
+
+ val = _val;
+
ret = sx9324_read_thresh(data, chan, &pthresh);
if (ret < 0)
return ret;
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH v2 3/6] iio: proximity: sx9324: reject negative hysteresis values
2026-11-22 7:46 ` [PATCH v2 3/6] iio: proximity: sx9324: reject negative hysteresis values Arnav Kapoor
@ 2026-11-23 11:15 ` Andy Shevchenko
2026-11-23 11:18 ` Andy Shevchenko
0 siblings, 1 reply; 13+ messages in thread
From: Andy Shevchenko @ 2026-11-23 11:15 UTC (permalink / raw)
To: Arnav Kapoor
Cc: Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, Gwendal Grignou, Stephen Boyd, linux-iio,
linux-kernel
On Sun, Nov 22, 2026 at 01:16:43PM +0530, Arnav Kapoor wrote:
> sx9324_write_hysteresis() copies the value into an unsigned int before
> comparing it with the threshold, so a negative value becomes a large
> one, passes the "val >= pthresh >> 2" check and sets the largest
> hysteresis instead of being rejected. The comparison is done in
> unsigned int, so this happens on both 32-bit and 64-bit architectures.
>
> Return -EINVAL for negative values, before converting them.
...
> static int sx9324_write_hysteresis(struct sx_common_data *data,
> const struct iio_chan_spec *chan, int _val)
> {
> - unsigned int hyst, val = _val;
> + unsigned int hyst, val;
> int ret, pthresh;
>
> + if (_val < 0)
> + return -EINVAL;
> +
> + val = _val;
You can validate assigned value instead as there are no side-effects.
But while I wrote it, I have noticed that there is actually one, we
modify the sign of the value. With that in mind maybe better to have
/*
* Assume only positive input, negative, after conversion to unsigned
* is unsupported. // or something like this
*/
val = _val;
if (val > INT_MAX)
return -EINVAL;
> ret = sx9324_read_thresh(data, chan, &pthresh);
> if (ret < 0)
> return ret;
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH v2 3/6] iio: proximity: sx9324: reject negative hysteresis values
2026-11-23 11:15 ` Andy Shevchenko
@ 2026-11-23 11:18 ` Andy Shevchenko
2026-11-23 13:17 ` Arnav Kapoor
0 siblings, 1 reply; 13+ messages in thread
From: Andy Shevchenko @ 2026-11-23 11:18 UTC (permalink / raw)
To: Arnav Kapoor
Cc: Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, Gwendal Grignou, Stephen Boyd, linux-iio,
linux-kernel
On Mon, Nov 23, 2026 at 01:15:42PM +0200, Andy Shevchenko wrote:
> On Sun, Nov 22, 2026 at 01:16:43PM +0530, Arnav Kapoor wrote:
...
> > static int sx9324_write_hysteresis(struct sx_common_data *data,
> > const struct iio_chan_spec *chan, int _val)
> > {
> > - unsigned int hyst, val = _val;
> > + unsigned int hyst, val;
> > int ret, pthresh;
> >
> > + if (_val < 0)
> > + return -EINVAL;
> > +
> > + val = _val;
>
> You can validate assigned value instead as there are no side-effects.
> But while I wrote it, I have noticed that there is actually one, we
> modify the sign of the value. With that in mind maybe better to have
>
> /*
> * Assume only positive input, negative, after conversion to unsigned
> * is unsupported. // or something like this
> */
> val = _val;
> if (val > INT_MAX)
> return -EINVAL;
On the second thought, the original < 0 check might be cleaner... Dunno.
Jonathan, what do you think?
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH v2 3/6] iio: proximity: sx9324: reject negative hysteresis values
2026-11-23 11:18 ` Andy Shevchenko
@ 2026-11-23 13:17 ` Arnav Kapoor
0 siblings, 0 replies; 13+ messages in thread
From: Arnav Kapoor @ 2026-11-23 13:17 UTC (permalink / raw)
To: Andy Shevchenko
Cc: jic23, dlechner, nuno.sa, andy, gwendal, swboyd, linux-iio, linux-kernel
On Mon, Nov 23, 2026 at 01:18:01PM +0200, Andy Shevchenko wrote:
> On Mon, Nov 23, 2026 at 01:15:42PM +0200, Andy Shevchenko wrote:
...
> > val = _val;
> > if (val > INT_MAX)
> > return -EINVAL;
>
> On the second thought, the original < 0 check might be cleaner... Dunno.
> Jonathan, what do you think?
I'd also keep the _val < 0 check. It tests the value as userspace
wrote it, before the conversion, so it needs no comment explaining why
INT_MAX is the limit. But I'm happy to switch to val > INT_MAX in all
four patches if Jonathan prefers that.
Thanks,
Arnav
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 4/6] iio: proximity: sx9360: fix hardware gain encoding
2026-11-22 7:46 [PATCH v2 0/6] iio: proximity: sx93xx: fix raw reads, thresholds, hysteresis and gain Arnav Kapoor
` (2 preceding siblings ...)
2026-11-22 7:46 ` [PATCH v2 3/6] iio: proximity: sx9324: reject negative hysteresis values Arnav Kapoor
@ 2026-11-22 7:46 ` Arnav Kapoor
2026-11-22 7:46 ` [PATCH v2 5/6] iio: proximity: sx9360: fix proximity threshold writes Arnav Kapoor
2026-11-22 7:46 ` [PATCH v2 6/6] iio: proximity: sx9360: reject negative hysteresis values Arnav Kapoor
5 siblings, 0 replies; 13+ messages in thread
From: Arnav Kapoor @ 2026-11-22 7:46 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Andy Shevchenko, Gwendal Grignou, Stephen Boyd,
linux-iio, linux-kernel, Arnav Kapoor
The sx9360 PROX_CTRL0 gain handling was copied from the original sx9324
driver and has the same bug that commit 108e4d4de2b5
("iio:proximity:sx9324: Fix hardware gain read/write") fixed there:
- The gain field holds ilog2(<gain>) + 1, with 0 reserved, so a gain
of 1 is 0x1 and a gain of 8 is 0x4. read_gain() and write_gain()
use ilog2(<gain>) instead, so every gain is off by one: writing 1
programs the reserved code 0 and writing 8 programs a gain of 4.
- SX9360_REG_PROX_CTRL0_GAIN_1 is 0x80, which sets the reserved bit 7
and leaves the gain field at the reserved code 0, instead of
0x1 << 3.
Semtech's own SX9360 driver (shipped in Samsung Galaxy kernels) decodes
the field the same way, codes 1..4 as x1..x8 and 0, 5..7 as reserved,
and programs 0x09 (gain x1, bit 7 clear) into both PROX_CTRL0
registers.
Use the same encoding as sx9324: add one when writing, subtract one
when reading, return -EINVAL for the reserved codes on read, and build
the default with FIELD_PREP_CONST(). SX9360_REG_PROX_CTRL0_GAIN_8 used
by the range check in write_gain() becomes 0x4 accordingly.
Fixes: 1cdb4c47f7f5 ("iio:proximity:sx9360: Add sx9360 support")
Link: https://github.com/TeamWin/android_kernel_samsung_d2q/blob/master/drivers/sensors/sx9360.c
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/sx9360.c | 20 ++++++++++++++------
1 file changed, 14 insertions(+), 6 deletions(-)
diff --git a/drivers/iio/proximity/sx9360.c b/drivers/iio/proximity/sx9360.c
index d102e20d4084..9ba967a33c98 100644
--- a/drivers/iio/proximity/sx9360.c
+++ b/drivers/iio/proximity/sx9360.c
@@ -64,8 +64,9 @@
#define SX9360_REG_PROX_CTRL0_PHR 0x40
#define SX9360_REG_PROX_CTRL0_PHM 0x41
#define SX9360_REG_PROX_CTRL0_GAIN_MASK GENMASK(5, 3)
-#define SX9360_REG_PROX_CTRL0_GAIN_1 0x80
-#define SX9360_REG_PROX_CTRL0_GAIN_8 0x3
+#define SX9360_REG_PROX_CTRL0_GAIN_RSVD 0x0
+#define SX9360_REG_PROX_CTRL0_GAIN_1 0x1
+#define SX9360_REG_PROX_CTRL0_GAIN_8 0x4
#define SX9360_REG_PROX_CTRL0_RAWFILT_MASK GENMASK(2, 0)
#define SX9360_REG_PROX_CTRL0_RAWFILT_1P50 0x01
#define SX9360_REG_PROX_CTRL1 0x42
@@ -289,7 +290,12 @@ static int sx9360_read_gain(struct sx_common_data *data,
if (ret)
return ret;
- *val = 1 << FIELD_GET(SX9360_REG_PROX_CTRL0_GAIN_MASK, regval);
+ regval = FIELD_GET(SX9360_REG_PROX_CTRL0_GAIN_MASK, regval);
+ if (regval == SX9360_REG_PROX_CTRL0_GAIN_RSVD ||
+ regval > SX9360_REG_PROX_CTRL0_GAIN_8)
+ return -EINVAL;
+
+ *val = BIT(regval - 1);
return IIO_VAL_INT;
}
@@ -612,7 +618,7 @@ static int sx9360_write_gain(struct sx_common_data *data,
if (val <= 0)
return -ERANGE;
- gain = ilog2(val);
+ gain = ilog2(val) + 1;
if (gain > SX9360_REG_PROX_CTRL0_GAIN_8)
return -ERANGE;
@@ -663,9 +669,11 @@ static const struct sx_common_reg_default sx9360_default_regs[] = {
{ SX9360_REG_AFE_PARAM1_PHM, SX9360_REG_AFE_PARAM1_AGAIN_PHM_6PF |
SX9360_REG_AFE_PARAM1_FREQ_83_33HZ, "afe_param1_phm" },
- { SX9360_REG_PROX_CTRL0_PHR, SX9360_REG_PROX_CTRL0_GAIN_1 |
+ { SX9360_REG_PROX_CTRL0_PHR,
+ FIELD_PREP_CONST(SX9360_REG_PROX_CTRL0_GAIN_MASK, SX9360_REG_PROX_CTRL0_GAIN_1) |
SX9360_REG_PROX_CTRL0_RAWFILT_1P50, "prox_ctrl0_phr" },
- { SX9360_REG_PROX_CTRL0_PHM, SX9360_REG_PROX_CTRL0_GAIN_1 |
+ { SX9360_REG_PROX_CTRL0_PHM,
+ FIELD_PREP_CONST(SX9360_REG_PROX_CTRL0_GAIN_MASK, SX9360_REG_PROX_CTRL0_GAIN_1) |
SX9360_REG_PROX_CTRL0_RAWFILT_1P50, "prox_ctrl0_phm" },
{ SX9360_REG_PROX_CTRL1, SX9360_REG_PROX_CTRL1_AVGNEG_THRESH_16K, "prox_ctrl1" },
{ SX9360_REG_PROX_CTRL2, SX9360_REG_PROX_CTRL2_AVGDEB_2SAMPLES |
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread* [PATCH v2 5/6] iio: proximity: sx9360: fix proximity threshold writes
2026-11-22 7:46 [PATCH v2 0/6] iio: proximity: sx93xx: fix raw reads, thresholds, hysteresis and gain Arnav Kapoor
` (3 preceding siblings ...)
2026-11-22 7:46 ` [PATCH v2 4/6] iio: proximity: sx9360: fix hardware gain encoding Arnav Kapoor
@ 2026-11-22 7:46 ` Arnav Kapoor
2026-11-22 7:46 ` [PATCH v2 6/6] iio: proximity: sx9360: reject negative hysteresis values Arnav Kapoor
5 siblings, 0 replies; 13+ messages in thread
From: Arnav Kapoor @ 2026-11-22 7:46 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Andy Shevchenko, Gwendal Grignou, Stephen Boyd,
linux-iio, linux-kernel, Arnav Kapoor
sx9360_read_thresh() reports a PROX_THRESH register value r as r for
r <= 1 and as r * r / 2 otherwise, and the division rounds down for
odd r. sx9360_write_thresh() inverts that with int_sqrt(2 * val), which
is one register value too low when 2 * val == r * r - 1, so writing
back the threshold read from an odd register value lowers it by one:
setting the threshold to the value it already has changes it, for 127
of the 256 register values (3, 5, 7, ...).
Use int_sqrt(2 * val + 1), which gives the largest register value whose
threshold does not exceed val: for r > 1, in integer arithmetic,
r * r / 2 <= val is the same as r * r <= 2 * val + 1. The result only
differs from int_sqrt(2 * val) on those edges. Checked for every val
from 0 to 65535.
Also reject negative values. They are converted to unsigned int, and
2 * val wraps at 32 bits on both 32-bit and 64-bit architectures, so
e.g. INT_MIN is written as 0 and values just above INT_MIN as small
thresholds instead of being rejected.
Fixes: 1cdb4c47f7f5 ("iio:proximity:sx9360: Add sx9360 support")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/sx9360.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/drivers/iio/proximity/sx9360.c b/drivers/iio/proximity/sx9360.c
index 9ba967a33c98..9a2796f67949 100644
--- a/drivers/iio/proximity/sx9360.c
+++ b/drivers/iio/proximity/sx9360.c
@@ -508,10 +508,16 @@ static int sx9360_read_event_val(struct iio_dev *indio_dev,
static int sx9360_write_thresh(struct sx_common_data *data, int _val)
{
- unsigned int val = _val;
+ unsigned int val;
+
+ if (_val < 0)
+ return -EINVAL;
+
+ val = _val;
- if (val >= 1)
- val = int_sqrt(2 * val);
+ /* Inverse of read_thresh(), so that writing back a read value is a no-op */
+ if (val > 1)
+ val = int_sqrt(2 * val + 1);
if (val > 0xff)
return -EINVAL;
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread* [PATCH v2 6/6] iio: proximity: sx9360: reject negative hysteresis values
2026-11-22 7:46 [PATCH v2 0/6] iio: proximity: sx93xx: fix raw reads, thresholds, hysteresis and gain Arnav Kapoor
` (4 preceding siblings ...)
2026-11-22 7:46 ` [PATCH v2 5/6] iio: proximity: sx9360: fix proximity threshold writes Arnav Kapoor
@ 2026-11-22 7:46 ` Arnav Kapoor
5 siblings, 0 replies; 13+ messages in thread
From: Arnav Kapoor @ 2026-11-22 7:46 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Andy Shevchenko, Gwendal Grignou, Stephen Boyd,
linux-iio, linux-kernel, Arnav Kapoor
sx9360_write_hysteresis() copies the value into an unsigned int before
comparing it with the threshold, so a negative value becomes a large
one, passes the "val >= pthresh >> 2" check and sets the largest
hysteresis instead of being rejected. The comparison is done in
unsigned int, so this happens on both 32-bit and 64-bit architectures.
Return -EINVAL for negative values, before converting them.
Fixes: 1cdb4c47f7f5 ("iio:proximity:sx9360: Add sx9360 support")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/sx9360.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/drivers/iio/proximity/sx9360.c b/drivers/iio/proximity/sx9360.c
index 9a2796f67949..21c1801cc2ff 100644
--- a/drivers/iio/proximity/sx9360.c
+++ b/drivers/iio/proximity/sx9360.c
@@ -528,9 +528,14 @@ static int sx9360_write_thresh(struct sx_common_data *data, int _val)
static int sx9360_write_hysteresis(struct sx_common_data *data, int _val)
{
- unsigned int hyst, val = _val;
+ unsigned int hyst, val;
int ret, pthresh;
+ if (_val < 0)
+ return -EINVAL;
+
+ val = _val;
+
ret = sx9360_read_thresh(data, &pthresh);
if (ret < 0)
return ret;
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread