mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/6] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling
@ 2026-11-26 21:36 Arnav Kapoor
  2026-11-26 21:36 ` [PATCH v2 1/6] iio: proximity: hx9023s: fix division by zero in set_samp_freq Arnav Kapoor
                   ` (5 more replies)
  0 siblings, 6 replies; 9+ messages in thread
From: Arnav Kapoor @ 2026-11-26 21:36 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá,
	Andy Shevchenko, Andy Shevchenko, Yasin Lee, linux-iio,
	linux-kernel, Arnav Kapoor

Five fixes for the HX9023S capacitive proximity driver, and one cleanup.

1: writing 67108864 (or any value for which val * 10^6 + val2 is a
   multiple of 2^32) to sampling_frequency divides by zero: div_u64()
   takes a u32 divisor. The check added in a318cfc08537 only catches 0.
2: reading sampling_frequency indexes the 32-entry period table with
   the PRF_CFG register value unchecked; the configuration file loaded
   by hx9023s_bin_load() can put any 8-bit value there.
3: resume re-enables the chip interrupts only when the trigger is
   enabled, so threshold events stop after the first suspend.
4: out-of-range threshold values are masked into the 10-bit field
   instead of being rejected (-32 becomes the largest threshold).
5: same for the 4-bit debounce fields (16 becomes 0).
6: cleanup, no functional change: get_samp_freq() doesn't need a
   64-bit division either.

Changes in v2:
- 1: use val == 0 && val2 == 0 for consistency (Andy)
- 1: reject 1 kHz and above (a period below the smallest table entry)
  and use a plain 32-bit division instead of div64_u64() (Andy)
- 4: check val >= BIT(15), keep the threshold with & ~GENMASK(4, 0)
  and add a comment on why the low bits are dropped (Andy)
- 6: new, drop the div_u64() in get_samp_freq() as well (Andy, on v1
  2/5); last in the series as it isn't a fix
- Rebase on the current fixes-togreg

v1: https://lore.kernel.org/all/20261123165255.239490-1-kapoorarnav43@gmail.com/

Patch 2 assumes the whole PRF_CFG register is the period index, as the
driver already does; I don't have the datasheet to check whether it has
other fields.

Applies to both fixes-togreg and testing.
Compile-tested only (W=1 for every patch, checkpatch --strict clean); I
don't have HX9023S hardware. The checks in 1 and 4 were tested with a
userspace copy on 32-bit and 64-bit: all 32 table periods are still
accepted, and the threshold check matches the v1 one for every value.

Arnav Kapoor (6):
  iio: proximity: hx9023s: fix division by zero in set_samp_freq
  iio: proximity: hx9023s: check the sampling frequency index
  iio: proximity: hx9023s: re-enable interrupts on resume for events
  iio: proximity: hx9023s: reject out-of-range threshold values
  iio: proximity: hx9023s: reject out-of-range debounce values
  iio: proximity: hx9023s: use plain division in get_samp_freq

 drivers/iio/proximity/hx9023s.c | 47 +++++++++++++++++++++++++--------
 1 file changed, 36 insertions(+), 11 deletions(-)


base-commit: 69bf7489f01ffa3c3269d09eb8a0668b1bdfa053
-- 
2.53.0


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

* [PATCH v2 1/6] iio: proximity: hx9023s: fix division by zero in set_samp_freq
  2026-11-26 21:36 [PATCH v2 0/6] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
@ 2026-11-26 21:36 ` Arnav Kapoor
  2026-11-27 11:17   ` Andy Shevchenko
  2026-11-26 21:36 ` [PATCH v2 2/6] iio: proximity: hx9023s: check the sampling frequency index Arnav Kapoor
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 9+ messages in thread
From: Arnav Kapoor @ 2026-11-26 21:36 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá,
	Andy Shevchenko, Andy Shevchenko, Yasin Lee, linux-iio,
	linux-kernel, Arnav Kapoor

hx9023s_set_samp_freq() passes val * MEGA + val2 to div_u64(), whose
divisor is a u32, so the sum is truncated to 32 bits. MEGA is an
unsigned long, so on 64-bit the product itself does not overflow, but
the truncation does, and on 32-bit the product already wraps. Either
way, any value for which val * MEGA + val2 is a multiple of 2^32 gives a
zero divisor, e.g.

  echo 67108864 > sampling_frequency

(67108864 * 10^6 = 15625 * 2^32) triggers a divide error, on both 32-bit
and 64-bit architectures. Negative values wrap the same way (-67108864
also gives 0), which the check added by commit a318cfc08537 ("iio:
proximity: hx9023s: Protect against division by zero in set_samp_freq")
does not catch, as it only rejects val == 0 && val2 == 0.

Reject negative values, and reject frequencies of 1 kHz and above: they
give a period of at most 1 ms, below the smallest entry (2 ms) of
hx9023s_samp_freq_table[], so they never matched anyway. With them gone
the divisor stays below NANO and fits in 32 bits, so a plain division is
enough.

Fixes: 60df548277b7 ("iio: proximity: Add driver support for TYHX's HX9023S capacitive proximity sensor")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
 drivers/iio/proximity/hx9023s.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index 2a6da557f22e..f5cd31a308c3 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -718,10 +718,17 @@ static int hx9023s_set_samp_freq(struct hx9023s_data *data, int val, int val2)
 	struct device *dev = regmap_get_device(data->regmap);
 	unsigned int i, period_ms;
 
-	if (!val && !val2)
+	if (val < 0 || val2 < 0 || (val == 0 && val2 == 0))
 		return -EINVAL;
 
-	period_ms = div_u64(NANO, (val * MEGA + val2));
+	/*
+	 * 1 kHz or more gives a period below the smallest table entry. Rejecting
+	 * it here also keeps the divisor below NANO, so it fits in 32 bits.
+	 */
+	if (val >= KILO)
+		return -EINVAL;
+
+	period_ms = NANO / (val * MEGA + val2);
 
 	for (i = 0; i < ARRAY_SIZE(hx9023s_samp_freq_table); i++) {
 		if (period_ms == hx9023s_samp_freq_table[i])
-- 
2.53.0


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

* [PATCH v2 2/6] iio: proximity: hx9023s: check the sampling frequency index
  2026-11-26 21:36 [PATCH v2 0/6] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
  2026-11-26 21:36 ` [PATCH v2 1/6] iio: proximity: hx9023s: fix division by zero in set_samp_freq Arnav Kapoor
@ 2026-11-26 21:36 ` Arnav Kapoor
  2026-11-26 21:36 ` [PATCH v2 3/6] iio: proximity: hx9023s: re-enable interrupts on resume for events Arnav Kapoor
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Arnav Kapoor @ 2026-11-26 21:36 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá,
	Andy Shevchenko, Andy Shevchenko, Yasin Lee, linux-iio,
	linux-kernel, Arnav Kapoor

hx9023s_get_samp_freq() uses the PRF_CFG register value as an index
into hx9023s_samp_freq_table[], which has 32 entries, without checking
it. The driver only writes valid indexes itself, but the configuration
file loaded by hx9023s_bin_load() writes arbitrary register/value pairs,
so PRF_CFG can hold any 8-bit value. Reading sampling_frequency then
reads past the end of the table and divides KILO by whatever it finds
there, which may be zero.

Return -EINVAL if the index is outside the table.

Fixes: e9ed97be4fcc ("iio: proximity: hx9023s: Added firmware file parsing functionality")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
 drivers/iio/proximity/hx9023s.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index f5cd31a308c3..030a6265f896 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -681,6 +681,9 @@ static int hx9023s_get_samp_freq(struct hx9023s_data *data, int *val, int *val2)
 	if (ret)
 		return ret;
 
+	if (index >= ARRAY_SIZE(hx9023s_samp_freq_table))
+		return -EINVAL;
+
 	odr = hx9023s_samp_freq_table[index];
 	*val = KILO / odr;
 	*val2 = div_u64((KILO % odr) * MICRO, odr);
-- 
2.53.0


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

* [PATCH v2 3/6] iio: proximity: hx9023s: re-enable interrupts on resume for events
  2026-11-26 21:36 [PATCH v2 0/6] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
  2026-11-26 21:36 ` [PATCH v2 1/6] iio: proximity: hx9023s: fix division by zero in set_samp_freq Arnav Kapoor
  2026-11-26 21:36 ` [PATCH v2 2/6] iio: proximity: hx9023s: check the sampling frequency index Arnav Kapoor
@ 2026-11-26 21:36 ` Arnav Kapoor
  2026-11-26 21:36 ` [PATCH v2 4/6] iio: proximity: hx9023s: reject out-of-range threshold values Arnav Kapoor
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Arnav Kapoor @ 2026-11-26 21:36 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá,
	Andy Shevchenko, Andy Shevchenko, Yasin Lee, linux-iio,
	linux-kernel, Arnav Kapoor

The chip interrupts are enabled at probe (INTERRUPT_CFG = 0xff in the
register defaults) and stay enabled while only threshold events are in
use. hx9023s_suspend() disables them, but hx9023s_resume() re-enables
them only if the data-ready trigger is enabled. With events enabled and
no buffer running, the interrupts therefore stay off after the first
suspend/resume cycle and no proximity event is reported any more, until
a buffer is started.

Re-enable the interrupts on resume if any event is enabled as well.

Fixes: 60df548277b7 ("iio: proximity: Add driver support for TYHX's HX9023S capacitive proximity sensor")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
 drivers/iio/proximity/hx9023s.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index 030a6265f896..d509de6e265b 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -1192,7 +1192,7 @@ static int hx9023s_resume(struct device *dev)
 	struct hx9023s_data *data = iio_priv(dev_get_drvdata(dev));
 
 	guard(mutex)(&data->mutex);
-	if (data->trigger_enabled)
+	if (data->trigger_enabled || data->chan_event)
 		hx9023s_interrupt_enable(data);
 
 	return 0;
-- 
2.53.0


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

* [PATCH v2 4/6] iio: proximity: hx9023s: reject out-of-range threshold values
  2026-11-26 21:36 [PATCH v2 0/6] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
                   ` (2 preceding siblings ...)
  2026-11-26 21:36 ` [PATCH v2 3/6] iio: proximity: hx9023s: re-enable interrupts on resume for events Arnav Kapoor
@ 2026-11-26 21:36 ` Arnav Kapoor
  2026-11-27 11:21   ` Andy Shevchenko
  2026-11-26 21:36 ` [PATCH v2 5/6] iio: proximity: hx9023s: reject out-of-range debounce values Arnav Kapoor
  2026-11-26 21:36 ` [PATCH v2 6/6] iio: proximity: hx9023s: use plain division in get_samp_freq Arnav Kapoor
  5 siblings, 1 reply; 9+ messages in thread
From: Arnav Kapoor @ 2026-11-26 21:36 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá,
	Andy Shevchenko, Andy Shevchenko, Yasin Lee, linux-iio,
	linux-kernel, Arnav Kapoor

The proximity thresholds are 10-bit register fields in units of 32.
hx9023s_set_thres_near() and hx9023s_set_thres_far() mask val / 32 with
GENMASK(9, 0) instead of checking it, so a value out of range is
silently written as a different threshold: 32768 becomes 0, the most
sensitive threshold, and -32 becomes 1023 * 32 = 32736, the least
sensitive one.

Return -EINVAL for negative values and values above 32767, the largest
value that still maps to the field. val >= BIT(15) catches both, as
BIT() is an unsigned long. The low 5 bits are still dropped, since the
field is in units of 32; add a comment saying so.

Fixes: 60df548277b7 ("iio: proximity: Add driver support for TYHX's HX9023S capacitive proximity sensor")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
 drivers/iio/proximity/hx9023s.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index d509de6e265b..2d2774398259 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -441,10 +441,15 @@ static int hx9023s_get_thres_far(struct hx9023s_data *data, u8 ch, int *val)
 
 static int hx9023s_set_thres_near(struct hx9023s_data *data, u8 ch, int val)
 {
-	__le16 val_le16 = cpu_to_le16((val / 32) & GENMASK(9, 0));
+	__le16 val_le16;
 	unsigned int reg;
 
-	data->ch_data[ch].thres.near = ((val / 32) & GENMASK(9, 0)) * 32;
+	if (val >= BIT(15))
+		return -EINVAL;
+
+	/* The field is in units of 32, so the low 5 bits are dropped */
+	val_le16 = cpu_to_le16(val / 32);
+	data->ch_data[ch].thres.near = val & ~GENMASK(4, 0);
 	reg = (ch == 4) ? HX9023S_PROX_HIGH_DIFF_CFG_CH4_0 :
 		HX9023S_PROX_HIGH_DIFF_CFG_CH0_0 + (ch * 2);
 
@@ -453,10 +458,15 @@ static int hx9023s_set_thres_near(struct hx9023s_data *data, u8 ch, int val)
 
 static int hx9023s_set_thres_far(struct hx9023s_data *data, u8 ch, int val)
 {
-	__le16 val_le16 = cpu_to_le16((val / 32) & GENMASK(9, 0));
+	__le16 val_le16;
 	unsigned int reg;
 
-	data->ch_data[ch].thres.far = ((val / 32) & GENMASK(9, 0)) * 32;
+	if (val >= BIT(15))
+		return -EINVAL;
+
+	/* The field is in units of 32, so the low 5 bits are dropped */
+	val_le16 = cpu_to_le16(val / 32);
+	data->ch_data[ch].thres.far = val & ~GENMASK(4, 0);
 	reg = (ch == 4) ? HX9023S_PROX_LOW_DIFF_CFG_CH4_0 :
 		HX9023S_PROX_LOW_DIFF_CFG_CH0_0 + (ch * 2);
 
-- 
2.53.0


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

* [PATCH v2 5/6] iio: proximity: hx9023s: reject out-of-range debounce values
  2026-11-26 21:36 [PATCH v2 0/6] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
                   ` (3 preceding siblings ...)
  2026-11-26 21:36 ` [PATCH v2 4/6] iio: proximity: hx9023s: reject out-of-range threshold values Arnav Kapoor
@ 2026-11-26 21:36 ` Arnav Kapoor
  2026-11-26 21:36 ` [PATCH v2 6/6] iio: proximity: hx9023s: use plain division in get_samp_freq Arnav Kapoor
  5 siblings, 0 replies; 9+ messages in thread
From: Arnav Kapoor @ 2026-11-26 21:36 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá,
	Andy Shevchenko, Andy Shevchenko, Yasin Lee, linux-iio,
	linux-kernel, Arnav Kapoor

The near and far debounce counts are 4-bit fields.
hx9023s_write_far_debounce() and hx9023s_write_near_debounce() pass the
value through FIELD_GET(), which only masks it here because the field
starts at bit 0, so an out-of-range value is silently written as a
different one: 16 becomes 0 (no debounce) and -1 becomes 15.

Return -EINVAL for values outside 0..15, and use FIELD_PREP(), which is
the right helper for building a register value.

Fixes: 60df548277b7 ("iio: proximity: Add driver support for TYHX's HX9023S capacitive proximity sensor")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
 drivers/iio/proximity/hx9023s.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index 2d2774398259..c2718723db27 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -359,18 +359,24 @@ static int hx9023s_ch_cfg(struct hx9023s_data *data)
 
 static int hx9023s_write_far_debounce(struct hx9023s_data *data, int val)
 {
+	if (val < 0 || val > FIELD_MAX(HX9023S_PROX_DEBOUNCE_MASK))
+		return -EINVAL;
+
 	guard(mutex)(&data->mutex);
 	return regmap_update_bits(data->regmap, HX9023S_PROX_INT_LOW_CFG,
 				HX9023S_PROX_DEBOUNCE_MASK,
-				FIELD_GET(HX9023S_PROX_DEBOUNCE_MASK, val));
+				FIELD_PREP(HX9023S_PROX_DEBOUNCE_MASK, val));
 }
 
 static int hx9023s_write_near_debounce(struct hx9023s_data *data, int val)
 {
+	if (val < 0 || val > FIELD_MAX(HX9023S_PROX_DEBOUNCE_MASK))
+		return -EINVAL;
+
 	guard(mutex)(&data->mutex);
 	return regmap_update_bits(data->regmap, HX9023S_PROX_INT_HIGH_CFG,
 				HX9023S_PROX_DEBOUNCE_MASK,
-				FIELD_GET(HX9023S_PROX_DEBOUNCE_MASK, val));
+				FIELD_PREP(HX9023S_PROX_DEBOUNCE_MASK, val));
 }
 
 static int hx9023s_read_far_debounce(struct hx9023s_data *data, int *val)
-- 
2.53.0


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

* [PATCH v2 6/6] iio: proximity: hx9023s: use plain division in get_samp_freq
  2026-11-26 21:36 [PATCH v2 0/6] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
                   ` (4 preceding siblings ...)
  2026-11-26 21:36 ` [PATCH v2 5/6] iio: proximity: hx9023s: reject out-of-range debounce values Arnav Kapoor
@ 2026-11-26 21:36 ` Arnav Kapoor
  5 siblings, 0 replies; 9+ messages in thread
From: Arnav Kapoor @ 2026-11-26 21:36 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá,
	Andy Shevchenko, Andy Shevchenko, Yasin Lee, linux-iio,
	linux-kernel, Arnav Kapoor

hx9023s_get_samp_freq() computes the fractional part of the sampling
frequency with div_u64(), but nothing here needs 64 bits: KILO % odr
is at most 1000, so the dividend is at most 10^9 and fits in an
unsigned long on 32-bit as well. Use a plain division, and drop the
linux/math64.h include, which has no other user left after the
previous patches.

No functional change.

Suggested-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
 drivers/iio/proximity/hx9023s.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index c2718723db27..98b65a3e69e1 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -18,7 +18,6 @@
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
 #include <linux/irqreturn.h>
-#include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/pm.h>
@@ -702,7 +701,7 @@ static int hx9023s_get_samp_freq(struct hx9023s_data *data, int *val, int *val2)
 
 	odr = hx9023s_samp_freq_table[index];
 	*val = KILO / odr;
-	*val2 = div_u64((KILO % odr) * MICRO, odr);
+	*val2 = (KILO % odr) * MICRO / odr;
 
 	return IIO_VAL_INT_PLUS_MICRO;
 }
-- 
2.53.0


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

* Re: [PATCH v2 1/6] iio: proximity: hx9023s: fix division by zero in set_samp_freq
  2026-11-26 21:36 ` [PATCH v2 1/6] iio: proximity: hx9023s: fix division by zero in set_samp_freq Arnav Kapoor
@ 2026-11-27 11:17   ` Andy Shevchenko
  0 siblings, 0 replies; 9+ messages in thread
From: Andy Shevchenko @ 2026-11-27 11:17 UTC (permalink / raw)
  To: Arnav Kapoor
  Cc: Jonathan Cameron, David Lechner, Nuno Sá,
	Andy Shevchenko, Yasin Lee, linux-iio, linux-kernel

On Fri, Nov 27, 2026 at 03:06:46AM +0530, Arnav Kapoor wrote:
> hx9023s_set_samp_freq() passes val * MEGA + val2 to div_u64(), whose
> divisor is a u32, so the sum is truncated to 32 bits. MEGA is an
> unsigned long, so on 64-bit the product itself does not overflow, but
> the truncation does, and on 32-bit the product already wraps. Either
> way, any value for which val * MEGA + val2 is a multiple of 2^32 gives a
> zero divisor, e.g.
> 
>   echo 67108864 > sampling_frequency
> 
> (67108864 * 10^6 = 15625 * 2^32) triggers a divide error, on both 32-bit
> and 64-bit architectures. Negative values wrap the same way (-67108864
> also gives 0), which the check added by commit a318cfc08537 ("iio:
> proximity: hx9023s: Protect against division by zero in set_samp_freq")
> does not catch, as it only rejects val == 0 && val2 == 0.
> 
> Reject negative values, and reject frequencies of 1 kHz and above: they
> give a period of at most 1 ms, below the smallest entry (2 ms) of
> hx9023s_samp_freq_table[], so they never matched anyway. With them gone
> the divisor stays below NANO and fits in 32 bits, so a plain division is
> enough.

...

> -	period_ms = div_u64(NANO, (val * MEGA + val2));
> +	/*
> +	 * 1 kHz or more gives a period below the smallest table entry. Rejecting
> +	 * it here also keeps the divisor below NANO, so it fits in 32 bits.
> +	 */
> +	if (val >= KILO)

	if (val >= (1 * HZ_PER_KHZ))

> +		return -EINVAL;
> +
> +	period_ms = NANO / (val * MEGA + val2);

Can you find dedicated multipliers like

	period_ms = (MSEC_PER_SEC * KILO) / (val * HZ_PER_KHZ * KILO + val2);

? And seems this KILO multiplier has to be explained, as I don't get
the precision used here. Or maybe something even better in your mind?

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH v2 4/6] iio: proximity: hx9023s: reject out-of-range threshold values
  2026-11-26 21:36 ` [PATCH v2 4/6] iio: proximity: hx9023s: reject out-of-range threshold values Arnav Kapoor
@ 2026-11-27 11:21   ` Andy Shevchenko
  0 siblings, 0 replies; 9+ messages in thread
From: Andy Shevchenko @ 2026-11-27 11:21 UTC (permalink / raw)
  To: Arnav Kapoor
  Cc: Jonathan Cameron, David Lechner, Nuno Sá,
	Andy Shevchenko, Yasin Lee, linux-iio, linux-kernel

On Fri, Nov 27, 2026 at 03:06:49AM +0530, Arnav Kapoor wrote:
> The proximity thresholds are 10-bit register fields in units of 32.
> hx9023s_set_thres_near() and hx9023s_set_thres_far() mask val / 32 with
> GENMASK(9, 0) instead of checking it, so a value out of range is
> silently written as a different threshold: 32768 becomes 0, the most
> sensitive threshold, and -32 becomes 1023 * 32 = 32736, the least
> sensitive one.
> 
> Return -EINVAL for negative values and values above 32767, the largest
> value that still maps to the field. val >= BIT(15) catches both, as
> BIT() is an unsigned long. The low 5 bits are still dropped, since the
> field is in units of 32; add a comment saying so.

...

>  static int hx9023s_set_thres_near(struct hx9023s_data *data, u8 ch, int val)
>  {
> -	__le16 val_le16 = cpu_to_le16((val / 32) & GENMASK(9, 0));
> +	__le16 val_le16;
>  	unsigned int reg;

Keep reversed xmas tree order.

> -	data->ch_data[ch].thres.near = ((val / 32) & GENMASK(9, 0)) * 32;
> +	if (val >= BIT(15))
> +		return -EINVAL;
> +
> +	/* The field is in units of 32, so the low 5 bits are dropped */
> +	val_le16 = cpu_to_le16(val / 32);
> +	data->ch_data[ch].thres.near = val & ~GENMASK(4, 0);

This comment seem detached from the actual expression. I don't see anything
wrong in having the

	if (val >= BIT(15))
		return -EINVAL;

	val_le16 = cpu_to_le16(val / 32);

	/* The field is in units of 32, so the low 5 bits are dropped */
	data->ch_data[ch].thres.near = val & ~GENMASK(4, 0);

...

In the similar way in the second case.

-- 
With Best Regards,
Andy Shevchenko



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

end of thread, other threads:[~2026-11-27 11:21 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 21:36 [PATCH v2 0/6] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
2026-11-26 21:36 ` [PATCH v2 1/6] iio: proximity: hx9023s: fix division by zero in set_samp_freq Arnav Kapoor
2026-11-27 11:17   ` Andy Shevchenko
2026-11-26 21:36 ` [PATCH v2 2/6] iio: proximity: hx9023s: check the sampling frequency index Arnav Kapoor
2026-11-26 21:36 ` [PATCH v2 3/6] iio: proximity: hx9023s: re-enable interrupts on resume for events Arnav Kapoor
2026-11-26 21:36 ` [PATCH v2 4/6] iio: proximity: hx9023s: reject out-of-range threshold values Arnav Kapoor
2026-11-27 11:21   ` Andy Shevchenko
2026-11-26 21:36 ` [PATCH v2 5/6] iio: proximity: hx9023s: reject out-of-range debounce values Arnav Kapoor
2026-11-26 21:36 ` [PATCH v2 6/6] iio: proximity: hx9023s: use plain division in get_samp_freq 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®