* [PATCH 1/6] iio: proximity: sx_common: fix raw reads while the trigger is in use
2026-11-16 20:53 [PATCH 0/6] iio: proximity: sx93xx: fix raw reads, thresholds, hysteresis and gain Arnav Kapoor
@ 2026-11-16 20:53 ` Arnav Kapoor
2026-11-16 20:53 ` [PATCH 2/6] iio: proximity: sx9324: fix proximity threshold writes Arnav Kapoor
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Arnav Kapoor @ 2026-11-16 20:53 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] 7+ messages in thread* [PATCH 2/6] iio: proximity: sx9324: fix proximity threshold writes
2026-11-16 20:53 [PATCH 0/6] iio: proximity: sx93xx: fix raw reads, thresholds, hysteresis and gain Arnav Kapoor
2026-11-16 20:53 ` [PATCH 1/6] iio: proximity: sx_common: fix raw reads while the trigger is in use Arnav Kapoor
@ 2026-11-16 20:53 ` Arnav Kapoor
2026-11-16 20:53 ` [PATCH 3/6] iio: proximity: sx9324: reject negative hysteresis values Arnav Kapoor
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Arnav Kapoor @ 2026-11-16 20:53 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 code r as r for r <= 1 and as
r * r / 2 otherwise, but sx9324_write_thresh() converts back with
int_sqrt(2 * val). For odd r the read value is (r * r - 1) / 2, so
writing back the value that was just read programs r - 1: setting the
threshold to the value it already has lowers it, for 127 of the 256
codes (3, 5, 7, ...).
The int to unsigned int conversion also lets some negative values
through: 2 * val wraps, so e.g. INT_MIN is written as 0 and values just
above INT_MIN as small thresholds instead of being rejected.
Reject negative values and use int_sqrt(2 * val + 1), which picks the
largest code whose reported threshold does not exceed the requested
value and makes read_thresh() and write_thresh() inverses of each
other for all 256 codes.
Fixes: 4c18a890dff8 ("iio:proximity:sx9324: Add SX9324 support")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/sx9324.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/drivers/iio/proximity/sx9324.c b/drivers/iio/proximity/sx9324.c
index 93f9577bbda7..39760c92552b 100644
--- a/drivers/iio/proximity/sx9324.c
+++ b/drivers/iio/proximity/sx9324.c
@@ -617,8 +617,12 @@ static int sx9324_write_thresh(struct sx_common_data *data,
reg = SX9324_REG_PROX_CTRL6 + chan->channel / 2;
+ if (_val < 0)
+ return -EINVAL;
+
+ /* Inverse of read_thresh(), so that writing back a read value is a no-op */
if (val >= 1)
- val = int_sqrt(2 * val);
+ val = int_sqrt(2 * val + 1);
if (val > 0xff)
return -EINVAL;
--
2.53.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH 3/6] iio: proximity: sx9324: reject negative hysteresis values
2026-11-16 20:53 [PATCH 0/6] iio: proximity: sx93xx: fix raw reads, thresholds, hysteresis and gain Arnav Kapoor
2026-11-16 20:53 ` [PATCH 1/6] iio: proximity: sx_common: fix raw reads while the trigger is in use Arnav Kapoor
2026-11-16 20:53 ` [PATCH 2/6] iio: proximity: sx9324: fix proximity threshold writes Arnav Kapoor
@ 2026-11-16 20:53 ` Arnav Kapoor
2026-11-16 20:53 ` [PATCH 4/6] iio: proximity: sx9360: fix hardware gain encoding Arnav Kapoor
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Arnav Kapoor @ 2026-11-16 20:53 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. Return -EINVAL for negative
values.
Fixes: 4c18a890dff8 ("iio:proximity:sx9324: Add SX9324 support")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/sx9324.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/iio/proximity/sx9324.c b/drivers/iio/proximity/sx9324.c
index 39760c92552b..9b7ac1d56e53 100644
--- a/drivers/iio/proximity/sx9324.c
+++ b/drivers/iio/proximity/sx9324.c
@@ -638,6 +638,9 @@ static int sx9324_write_hysteresis(struct sx_common_data *data,
unsigned int hyst, val = _val;
int ret, pthresh;
+ if (_val < 0)
+ return -EINVAL;
+
ret = sx9324_read_thresh(data, chan, &pthresh);
if (ret < 0)
return ret;
--
2.53.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH 4/6] iio: proximity: sx9360: fix hardware gain encoding
2026-11-16 20:53 [PATCH 0/6] iio: proximity: sx93xx: fix raw reads, thresholds, hysteresis and gain Arnav Kapoor
` (2 preceding siblings ...)
2026-11-16 20:53 ` [PATCH 3/6] iio: proximity: sx9324: reject negative hysteresis values Arnav Kapoor
@ 2026-11-16 20:53 ` Arnav Kapoor
2026-11-16 20:53 ` [PATCH 5/6] iio: proximity: sx9360: fix proximity threshold writes Arnav Kapoor
2026-11-16 20:53 ` [PATCH 6/6] iio: proximity: sx9360: reject negative hysteresis values Arnav Kapoor
5 siblings, 0 replies; 7+ messages in thread
From: Arnav Kapoor @ 2026-11-16 20:53 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] 7+ messages in thread* [PATCH 5/6] iio: proximity: sx9360: fix proximity threshold writes
2026-11-16 20:53 [PATCH 0/6] iio: proximity: sx93xx: fix raw reads, thresholds, hysteresis and gain Arnav Kapoor
` (3 preceding siblings ...)
2026-11-16 20:53 ` [PATCH 4/6] iio: proximity: sx9360: fix hardware gain encoding Arnav Kapoor
@ 2026-11-16 20:53 ` Arnav Kapoor
2026-11-16 20:53 ` [PATCH 6/6] iio: proximity: sx9360: reject negative hysteresis values Arnav Kapoor
5 siblings, 0 replies; 7+ messages in thread
From: Arnav Kapoor @ 2026-11-16 20:53 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 code r as r for r <= 1 and as
r * r / 2 otherwise, but sx9360_write_thresh() converts back with
int_sqrt(2 * val). For odd r the read value is (r * r - 1) / 2, so
writing back the value that was just read programs r - 1: setting the
threshold to the value it already has lowers it, for 127 of the 256
codes (3, 5, 7, ...).
The int to unsigned int conversion also lets some negative values
through: 2 * val wraps, so e.g. INT_MIN is written as 0 and values just
above INT_MIN as small thresholds instead of being rejected.
Reject negative values and use int_sqrt(2 * val + 1), which picks the
largest code whose reported threshold does not exceed the requested
value and makes read_thresh() and write_thresh() inverses of each
other for all 256 codes.
Fixes: 1cdb4c47f7f5 ("iio:proximity:sx9360: Add sx9360 support")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/sx9360.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/drivers/iio/proximity/sx9360.c b/drivers/iio/proximity/sx9360.c
index 9ba967a33c98..26190f9c6c1f 100644
--- a/drivers/iio/proximity/sx9360.c
+++ b/drivers/iio/proximity/sx9360.c
@@ -510,8 +510,12 @@ static int sx9360_write_thresh(struct sx_common_data *data, int _val)
{
unsigned int val = _val;
+ if (_val < 0)
+ return -EINVAL;
+
+ /* Inverse of read_thresh(), so that writing back a read value is a no-op */
if (val >= 1)
- val = int_sqrt(2 * val);
+ val = int_sqrt(2 * val + 1);
if (val > 0xff)
return -EINVAL;
--
2.53.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH 6/6] iio: proximity: sx9360: reject negative hysteresis values
2026-11-16 20:53 [PATCH 0/6] iio: proximity: sx93xx: fix raw reads, thresholds, hysteresis and gain Arnav Kapoor
` (4 preceding siblings ...)
2026-11-16 20:53 ` [PATCH 5/6] iio: proximity: sx9360: fix proximity threshold writes Arnav Kapoor
@ 2026-11-16 20:53 ` Arnav Kapoor
5 siblings, 0 replies; 7+ messages in thread
From: Arnav Kapoor @ 2026-11-16 20:53 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. Return -EINVAL for negative
values.
Fixes: 1cdb4c47f7f5 ("iio:proximity:sx9360: Add sx9360 support")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/sx9360.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/iio/proximity/sx9360.c b/drivers/iio/proximity/sx9360.c
index 26190f9c6c1f..9989b0dbeae0 100644
--- a/drivers/iio/proximity/sx9360.c
+++ b/drivers/iio/proximity/sx9360.c
@@ -529,6 +529,9 @@ static int sx9360_write_hysteresis(struct sx_common_data *data, int _val)
unsigned int hyst, val = _val;
int ret, pthresh;
+ if (_val < 0)
+ return -EINVAL;
+
ret = sx9360_read_thresh(data, &pthresh);
if (ret < 0)
return ret;
--
2.53.0
^ permalink raw reply [flat|nested] 7+ messages in thread