[PATCH 4/6] iio: proximity: sx9360: fix hardware gain encoding

From: Arnav Kapoor

Date: Mon Nov 16 2026 - 15:54:43 EST


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@xxxxxxxxx>
---
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