mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/4] iio: proximity: aw96103: fix diff sign, missing config, async config lifetime and event values
@ 2026-11-26  5:30 Arnav Kapoor
  2026-11-26  5:30 ` [PATCH 1/4] iio: proximity: aw96103: report negative proximity diff values Arnav Kapoor
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Arnav Kapoor @ 2026-11-26  5:30 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá,
	Andy Shevchenko, Andy Shevchenko, Wang Shuaijie, linux-iio,
	linux-kernel, Arnav Kapoor

Four fixes for the AW96103/AW96105 capacitive proximity driver.

1: the DIFF registers are signed S21.10 (as documented in Awinic's
   aw_sar driver), but are divided as u32, so negative diffs read back
   as about 4 million.
2: without aw96103_0.bin the driver never loads its built-in defaults,
   so the scan and the interrupts are never enabled.
3: nothing waits for the request_firmware_nowait() callback, which
   uses the devm-allocated driver data; a probe failure after the
   request (e.g. no interrupt) or an early unbind frees it first.
4: out-of-range debounce and hysteresis values are masked into their
   2-bit fields instead of being rejected.

Patch 3 makes unbind wait for the callback, which can take as long as
the firmware loader's fallback timeout if a user-mode helper fallback
is configured.

Applies to both fixes-togreg and testing.
Compile-tested only (W=1 for every patch, checkpatch --strict clean); I
don't have AW96103 hardware.

Arnav Kapoor (4):
  iio: proximity: aw96103: report negative proximity diff values
  iio: proximity: aw96103: use the default configuration without
    firmware
  iio: proximity: aw96103: wait for the configuration load before
    freeing
  iio: proximity: aw96103: reject out-of-range debounce and hysteresis
    values

 drivers/iio/proximity/aw96103.c | 41 +++++++++++++++++++++++++++------
 1 file changed, 34 insertions(+), 7 deletions(-)


base-commit: 793d80b04b6c4f7f896c273567f94eccd0fac54d
-- 
2.53.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] iio: proximity: aw96103: report negative proximity diff values
  2026-11-26  5:30 [PATCH 0/4] iio: proximity: aw96103: fix diff sign, missing config, async config lifetime and event values Arnav Kapoor
@ 2026-11-26  5:30 ` Arnav Kapoor
  2026-11-26  5:30 ` [PATCH 2/4] iio: proximity: aw96103: use the default configuration without firmware Arnav Kapoor
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Arnav Kapoor @ 2026-11-26  5:30 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá,
	Andy Shevchenko, Andy Shevchenko, Wang Shuaijie, linux-iio,
	linux-kernel, Arnav Kapoor

The DIFF registers hold a signed S21.10 fixed-point value (Awinic's own
aw_sar driver documents the diff data format as S21.10), which goes
negative when the measured capacitance drops below the baseline.
aw96103_get_diff_raw() divides it as a u32, so a diff of -1 (0xfffffc00)
is reported as 4194303 instead of -1.

Do the division on the value as an s32.

Fixes: 07b241262dca ("iio: proximity: aw96103: Add support for aw96103/aw96105 proximity sensor")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
 drivers/iio/proximity/aw96103.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/proximity/aw96103.c b/drivers/iio/proximity/aw96103.c
index e3ab55170b6c..1fd0ceaf2a05 100644
--- a/drivers/iio/proximity/aw96103.c
+++ b/drivers/iio/proximity/aw96103.c
@@ -268,7 +268,7 @@ static int aw96103_get_diff_raw(struct aw96103 *aw96103, unsigned int chan,
 			  AW96103_REG_DIFF_CH0 + chan * 4, &data);
 	if (ret)
 		return ret;
-	*buf = (int)(data / AW_DATA_PROCESS_FACTOR);
+	*buf = (s32)data / AW_DATA_PROCESS_FACTOR;
 
 	return 0;
 }
-- 
2.53.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/4] iio: proximity: aw96103: use the default configuration without firmware
  2026-11-26  5:30 [PATCH 0/4] iio: proximity: aw96103: fix diff sign, missing config, async config lifetime and event values Arnav Kapoor
  2026-11-26  5:30 ` [PATCH 1/4] iio: proximity: aw96103: report negative proximity diff values Arnav Kapoor
@ 2026-11-26  5:30 ` Arnav Kapoor
  2026-11-26  5:30 ` [PATCH 3/4] iio: proximity: aw96103: wait for the configuration load before freeing Arnav Kapoor
  2026-11-26  5:30 ` [PATCH 4/4] iio: proximity: aw96103: reject out-of-range debounce and hysteresis values Arnav Kapoor
  3 siblings, 0 replies; 5+ messages in thread
From: Arnav Kapoor @ 2026-11-26  5:30 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá,
	Andy Shevchenko, Andy Shevchenko, Wang Shuaijie, linux-iio,
	linux-kernel, Arnav Kapoor

aw96103_cfg_update() falls back to the built-in register defaults when
the aw96103_0.bin configuration file cannot be used, but not when it is
missing: then it only logs "No firmware." and returns. In that case the
chip is never configured, the channel scan and the interrupts are never
enabled (aw96103_channel_scan_start()), and no channel is marked as
used, so the device registers but never reports an event.

Load the defaults when the file is missing as well, as the comment in
aw96103_cfg_update() describes.

Fixes: 07b241262dca ("iio: proximity: aw96103: Add support for aw96103/aw96105 proximity sensor")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
 drivers/iio/proximity/aw96103.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/proximity/aw96103.c b/drivers/iio/proximity/aw96103.c
index 1fd0ceaf2a05..504e92ab9b07 100644
--- a/drivers/iio/proximity/aw96103.c
+++ b/drivers/iio/proximity/aw96103.c
@@ -589,12 +589,13 @@ static void aw96103_cfg_update(const struct firmware *fw, void *data)
 	struct aw96103 *aw96103 = data;
 	int ret, i;
 
-	if (!fw || !fw->data) {
-		dev_err(aw96103->dev, "No firmware.\n");
-		return;
+	if (fw && fw->data) {
+		ret = aw96103_cfg_all_loaded(fw, aw96103);
+	} else {
+		release_firmware(fw);
+		dev_warn(aw96103->dev, "No firmware, using the default configuration\n");
+		ret = -ENOENT;
 	}
-
-	ret = aw96103_cfg_all_loaded(fw, aw96103);
 	/*
 	 * If loading the register configuration file fails,
 	 * load the default register configuration in the driver to
-- 
2.53.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/4] iio: proximity: aw96103: wait for the configuration load before freeing
  2026-11-26  5:30 [PATCH 0/4] iio: proximity: aw96103: fix diff sign, missing config, async config lifetime and event values Arnav Kapoor
  2026-11-26  5:30 ` [PATCH 1/4] iio: proximity: aw96103: report negative proximity diff values Arnav Kapoor
  2026-11-26  5:30 ` [PATCH 2/4] iio: proximity: aw96103: use the default configuration without firmware Arnav Kapoor
@ 2026-11-26  5:30 ` Arnav Kapoor
  2026-11-26  5:30 ` [PATCH 4/4] iio: proximity: aw96103: reject out-of-range debounce and hysteresis values Arnav Kapoor
  3 siblings, 0 replies; 5+ messages in thread
From: Arnav Kapoor @ 2026-11-26  5:30 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá,
	Andy Shevchenko, Andy Shevchenko, Wang Shuaijie, linux-iio,
	linux-kernel, Arnav Kapoor

aw96103_i2c_probe() loads the register configuration with
request_firmware_nowait(), and aw96103_cfg_update() runs later and
writes to the driver data and the chip. Nothing waits for it: if probe
fails after the request (e.g. aw96103_interrupt_init() fails because the
device has no interrupt), or the driver is unbound before the callback
has run, the devm-allocated iio_dev that holds that data is freed first,
and the callback then uses freed memory. The regmap and the vcc supply
it uses are devm-managed as well.

Signal a completion at the end of aw96103_cfg_update() and wait for it
in a devm action added right after the request, so it runs before any
of those resources are released.

Fixes: 07b241262dca ("iio: proximity: aw96103: Add support for aw96103/aw96105 proximity sensor")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
 drivers/iio/proximity/aw96103.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/proximity/aw96103.c b/drivers/iio/proximity/aw96103.c
index 504e92ab9b07..b4aa31a65369 100644
--- a/drivers/iio/proximity/aw96103.c
+++ b/drivers/iio/proximity/aw96103.c
@@ -8,6 +8,7 @@
  */
 #include <linux/bits.h>
 #include <linux/bitfield.h>
+#include <linux/completion.h>
 #include <linux/delay.h>
 #include <linux/firmware.h>
 #include <linux/i2c.h>
@@ -118,6 +119,8 @@ struct aw96103 {
 	struct aw_channels_info channels_arr[6];
 	unsigned int max_channels;
 	unsigned int chan_en;
+	/* Signalled when the asynchronous configuration load has finished */
+	struct completion cfg_done;
 };
 
 static const unsigned int aw96103_reg_default[] = {
@@ -605,7 +608,7 @@ static void aw96103_cfg_update(const struct firmware *fw, void *data)
 		ret = aw96103_para_loaded(aw96103);
 		if (ret) {
 			dev_err(aw96103->dev, "load param error.\n");
-			return;
+			goto out;
 		}
 	}
 
@@ -615,6 +618,16 @@ static void aw96103_cfg_update(const struct firmware *fw, void *data)
 		else
 			aw96103->channels_arr[i].used = false;
 	}
+
+out:
+	complete(&aw96103->cfg_done);
+}
+
+static void aw96103_wait_cfg_update(void *data)
+{
+	struct aw96103 *aw96103 = data;
+
+	wait_for_completion(&aw96103->cfg_done);
 }
 
 static int aw96103_sw_reset(struct aw96103 *aw96103)
@@ -811,12 +824,19 @@ static int aw96103_i2c_probe(struct i2c_client *i2c)
 	if (ret)
 		return ret;
 
+	init_completion(&aw96103->cfg_done);
 	ret = request_firmware_nowait(THIS_MODULE, true, "aw96103_0.bin",
 				      aw96103->dev, GFP_KERNEL, aw96103,
 				      aw96103_cfg_update);
 	if (ret)
 		return ret;
 
+	/* The callback uses aw96103, so it must finish before that is freed */
+	ret = devm_add_action_or_reset(aw96103->dev, aw96103_wait_cfg_update,
+				       aw96103);
+	if (ret)
+		return ret;
+
 	ret = aw96103_interrupt_init(indio_dev, i2c);
 	if (ret)
 		return ret;
-- 
2.53.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 4/4] iio: proximity: aw96103: reject out-of-range debounce and hysteresis values
  2026-11-26  5:30 [PATCH 0/4] iio: proximity: aw96103: fix diff sign, missing config, async config lifetime and event values Arnav Kapoor
                   ` (2 preceding siblings ...)
  2026-11-26  5:30 ` [PATCH 3/4] iio: proximity: aw96103: wait for the configuration load before freeing Arnav Kapoor
@ 2026-11-26  5:30 ` Arnav Kapoor
  3 siblings, 0 replies; 5+ messages in thread
From: Arnav Kapoor @ 2026-11-26  5:30 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá,
	Andy Shevchenko, Andy Shevchenko, Wang Shuaijie, linux-iio,
	linux-kernel, Arnav Kapoor

The debounce (OUTDEB, INDEB) and hysteresis (THHYST) settings are 2-bit
fields of PROXCTRL. aw96103_write_event_val() passes the value through
FIELD_PREP() without checking it, which masks it to the field, so an
out-of-range value is silently written as a different one: 4 becomes 0
and -1 becomes 3.

Return -EINVAL for values outside 0..3. Both debounce fields have the
same width, so one check covers the two directions.

Fixes: 07b241262dca ("iio: proximity: aw96103: Add support for aw96103/aw96105 proximity sensor")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
 drivers/iio/proximity/aw96103.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/iio/proximity/aw96103.c b/drivers/iio/proximity/aw96103.c
index b4aa31a65369..24557a8f764d 100644
--- a/drivers/iio/proximity/aw96103.c
+++ b/drivers/iio/proximity/aw96103.c
@@ -401,6 +401,9 @@ static int aw96103_write_event_val(struct iio_dev *indio_dev,
 		return regmap_write(aw96103->regmap,
 				    AW96103_REG_PROXTH0_CH(chan->channel), val);
 	case IIO_EV_INFO_PERIOD:
+		if (val < 0 || val > FIELD_MAX(AW96103_OUTDEB_MASK))
+			return -EINVAL;
+
 		switch (dir) {
 		case IIO_EV_DIR_RISING:
 			return regmap_update_bits(aw96103->regmap,
@@ -417,6 +420,9 @@ static int aw96103_write_event_val(struct iio_dev *indio_dev,
 			return -EINVAL;
 		}
 	case IIO_EV_INFO_HYSTERESIS:
+		if (val < 0 || val > FIELD_MAX(AW96103_THHYST_MASK))
+			return -EINVAL;
+
 		return regmap_update_bits(aw96103->regmap,
 					AW96103_REG_PROXCTRL_CH(chan->channel),
 					AW96103_THHYST_MASK,
-- 
2.53.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-26  5:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26  5:30 [PATCH 0/4] iio: proximity: aw96103: fix diff sign, missing config, async config lifetime and event values Arnav Kapoor
2026-11-26  5:30 ` [PATCH 1/4] iio: proximity: aw96103: report negative proximity diff values Arnav Kapoor
2026-11-26  5:30 ` [PATCH 2/4] iio: proximity: aw96103: use the default configuration without firmware Arnav Kapoor
2026-11-26  5:30 ` [PATCH 3/4] iio: proximity: aw96103: wait for the configuration load before freeing Arnav Kapoor
2026-11-26  5:30 ` [PATCH 4/4] iio: proximity: aw96103: reject out-of-range debounce and hysteresis values 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®