mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] iio: proximity: sx9324: reject reserved gain values on read
@ 2026-11-04 17:59 Arnav Kapoor
  0 siblings, 0 replies; only message in thread
From: Arnav Kapoor @ 2026-11-04 17:59 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_gain() is meant to return -EINVAL for the reserved
PROX_CTRL0 gain codes, 0 and 5 to 7. But the range check sits in the
else branch of "if (regval)", where regval is always 0, so codes 5 to 7
are never caught and are reported as gains of 16, 32 and 64, which the
sensor doesn't support and which write_raw() rejects.

Check both reserved ranges before converting the code to a gain.

Fixes: 108e4d4de2b5 ("iio:proximity:sx9324: Fix hardware gain read/write")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
Compile-tested only (W=1); I don't have SX9324 hardware.

 drivers/iio/proximity/sx9324.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/proximity/sx9324.c b/drivers/iio/proximity/sx9324.c
index 1c8ba28c030a..95e7315314f8 100644
--- a/drivers/iio/proximity/sx9324.c
+++ b/drivers/iio/proximity/sx9324.c
@@ -404,13 +404,11 @@ static int sx9324_read_gain(struct sx_common_data *data,
 		return ret;
 
 	regval = FIELD_GET(SX9324_REG_PROX_CTRL0_GAIN_MASK, regval);
-	if (regval)
-		regval--;
-	else if (regval == SX9324_REG_PROX_CTRL0_GAIN_RSVD ||
-		 regval > SX9324_REG_PROX_CTRL0_GAIN_8)
+	if (regval == SX9324_REG_PROX_CTRL0_GAIN_RSVD ||
+	    regval > SX9324_REG_PROX_CTRL0_GAIN_8)
 		return -EINVAL;
 
-	*val = 1 << regval;
+	*val = 1 << (regval - 1);
 
 	return IIO_VAL_INT;
 }

base-commit: 340d44bdf9db0f095f99845b5ad8628781d79b6f
-- 
2.53.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-04 17:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-04 17:59 [PATCH] iio: proximity: sx9324: reject reserved gain values on read Arnav Kapoor

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

all inboxes | Powered by JetHome®