Skip to contentSkip to conversation

iio: proximity: aw96103: fix diff sign, missing config, async config lifetime and event values

PATCH Superseded Arnav Kapoor 3 participants 7 messages Nov 26 – Nov 28 IIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover iio: proximity: aw96103: fix diff sign, missing config, async config lifetime and event values

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

Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/4 iio: proximity: aw96103: report negative proximity diff values

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")
---
 drivers/iio/proximity/aw96103.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/proximity/aw96103.c +1 -1
index e3ab55170b6c..1fd0ceaf2a05 100644
@@ -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;
 }
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/4] iio: proximity: aw96103: report negative proximity diff values

Quoted 7 lines · 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.

...

drivers/iio/proximity/aw96103.c static int aw96103_get_diff_raw(struct aw96103 *aw96103, unsigned int chan, · line 271

Quoted 4 lines
drivers/iio/proximity/aw96103.c +1 -1
 	if (ret)
 		return ret;
-	*buf = (int)(data / AW_DATA_PROCESS_FACTOR);
+	*buf = (s32)data / AW_DATA_PROCESS_FACTOR;

The type of data is u32, however regmap expects unsigned int. I would change the type and leave (int) casting (yes, the change is required still, I noticed it).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

PATCH 2/4 iio: proximity: aw96103: use the default configuration without firmware

In reply to 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")
---
 drivers/iio/proximity/aw96103.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/iio/proximity/aw96103.c +6 -5
index 1fd0ceaf2a05..504e92ab9b07 100644
@@ -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
Signature · 1 line
2.53.0
#4 lore raw

PATCH 3/4 iio: proximity: aw96103: wait for the configuration load before freeing

In reply to 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")
---
 drivers/iio/proximity/aw96103.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
drivers/iio/proximity/aw96103.c +21 -1
index 504e92ab9b07..b4aa31a65369 100644
@@ -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;
Signature · 1 line
2.53.0
#5 lore raw

PATCH 4/4 iio: proximity: aw96103: reject out-of-range debounce and hysteresis values

In reply to 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")
---
 drivers/iio/proximity/aw96103.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/iio/proximity/aw96103.c +6 -0
index b4aa31a65369..24557a8f764d 100644
@@ -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,
Signature · 1 line
2.53.0
maintainer
#6 lore raw

Re: [PATCH 4/4] iio: proximity: aw96103: reject out-of-range debounce and hysteresis values

Quoted 20 lines · On Thu, 26 Nov 2026 11:00:28 +0530 Arnav Kapoor <[email protected]> wrote:

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")
---
 drivers/iio/proximity/aw96103.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/iio/proximity/aw96103.c +0 -0
index b4aa31a65369..24557a8f764d 100644

drivers/iio/proximity/aw96103.c static int aw96103_write_event_val(struct iio_dev *indio_dev, · line 404

Quoted 4 lines
drivers/iio/proximity/aw96103.c +1 -0
@@ -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))

Maybe just use FIELD_FIT()? I think that should cover all eventualities?

Jonathan

Quoted 15 lines
drivers/iio/proximity/aw96103.c +5 -0
+			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,