Skip to contentSkip to conversation

iio: proximity: sx9360: fix sampling frequency write and available range

PATCH Superseded Arnav Kapoor 2 participants 2 messages Oct 24 – Oct 25 IIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: proximity: sx9360: fix sampling frequency write and available range

GNRL_CTRL1/2 hold the scan period in units of 8192 / FOSC, but sx9360_set_samp_freq() computes freq * 8192 / FOSC and mis-scales val2, which is in micro units. Any integer frequency then programs a period of 0, and fractional parts above ~0.26 Hz overflow the int, writing 0x10 to GNRL_CTRL1 outside the SCANPERIOD field.

Compute FOSC / (freq * 8192), rounded to the closest value, and reject frequencies outside the 1..2047 period range.

The available range doesn't match the limits in the comments next to it
either: 4 MHz / 8192 is 488.28125 Hz, not 448.28125 Hz,
4 MHz / (8192 * 2047) is 0.238535 Hz, and (max - min) / 2047 is 0.238419 Hz. Use those values.

Fixes 1cdb4c47f7f5 ("iio:proximity:sx9360: Add sx9360 support")

---
Compile-tested only (W=1); I don't have SX9360 hardware. I round-tripped the new conversion through the existing read formula in userspace, e.g. 9.765625 Hz -> period 50 (the 102 ms default) -> 9.765625 Hz.

 drivers/iio/proximity/sx9360.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
drivers/iio/proximity/sx9360.c +22 -6
index ed83d809ecf5..d076de19b9e7 100644
@@ -14,10 +14,12 @@
 #include <linux/interrupt.h>
 #include <linux/kernel.h>
 #include <linux/log2.h>
+#include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/pm.h>
 #include <linux/property.h>
 #include <linux/regmap.h>
+#include <linux/units.h>
 
 #include <linux/iio/iio.h>
 
@@ -43,6 +45,7 @@
 #define SX9360_REG_GNRL_CTRL1_SCANPERIOD_MASK GENMASK(2, 0)
 #define SX9360_REG_GNRL_CTRL2		0x12
 #define SX9360_REG_GNRL_CTRL2_PERIOD_102MS	0x32
+#define SX9360_REG_GNRL_SCANPERIOD_MAX		GENMASK(10, 0)
 #define SX9360_REG_GNRL_REG_2_PERIOD_MS(_r)	\
 	(((_r) * 8192) / (SX9360_FOSC_HZ / 1000))
 #define SX9360_REG_GNRL_FREQ_2_REG(_f)  (((_f) * 8192) / SX9360_FOSC_HZ)
@@ -182,9 +185,9 @@ static const struct {
 	int val;
 	int val2;
 } sx9360_samp_freq_interval[] = {
-	{ 0, 281250 },  /* 4MHz / (8192 * 2047) */
-	{ 0, 281250 },
-	{ 448, 281250 },  /* 4MHz / 8192 */
+	{ 0, 238535 },  /* 4MHz / (8192 * 2047) */
+	{ 0, 238419 },
+	{ 488, 281250 },  /* 4MHz / 8192 */
 };
 
 static const struct regmap_range sx9360_writable_reg_ranges[] = {
@@ -383,11 +386,24 @@ static int sx9360_read_avail(struct iio_dev *indio_dev,
 static int sx9360_set_samp_freq(struct sx_common_data *data,
 				int val, int val2)
 {
-	int reg;
+	unsigned int period;
+	u64 freq_uhz;
 	__be16 buf;
 
-	reg = val * 8192 / SX9360_FOSC_HZ + val2 * 8192 / (SX9360_FOSC_MHZ);
-	buf = cpu_to_be16(reg);
+	if (val < 0 || val2 < 0)
+		return -EINVAL;
+
+	freq_uhz = (u64)val * MICRO + val2;
+	if (!freq_uhz)
+		return -EINVAL;
+
+	/* The register holds the scan period, in units of 8192 / FOSC. */
+	period = DIV64_U64_ROUND_CLOSEST((u64)SX9360_FOSC_HZ * MICRO,
+					 freq_uhz * 8192);
+	if (!period || period > SX9360_REG_GNRL_SCANPERIOD_MAX)
+		return -EINVAL;
+
+	buf = cpu_to_be16(period);
 	guard(mutex)(&data->mutex);
 
 	return regmap_bulk_write(data->regmap, SX9360_REG_GNRL_CTRL1, &buf,

base-commit: 2ef6f5c4ee56e90c237c5f7687f09e635f7d30a9

Signature · 1 line
2.53.0
#1 lore raw
Quoted 13 lines · Arnav Kapoor

GNRL_CTRL1/2 hold the scan period in units of 8192 / FOSC, but sx9360_set_samp_freq() computes freq * 8192 / FOSC and mis-scales val2, which is in micro units. Any integer frequency then programs a period of 0, and fractional parts above ~0.26 Hz overflow the int, writing 0x10 to GNRL_CTRL1 outside the SCANPERIOD field.

Compute FOSC / (freq * 8192), rounded to the closest value, and reject frequencies outside the 1..2047 period range.

The available range doesn't match the limits in the comments next to it
either: 4 MHz / 8192 is 488.28125 Hz, not 448.28125 Hz,
4 MHz / (8192 * 2047) is 0.238535 Hz, and (max - min) / 2047 is 0.238419 Hz. Use those values.

...

drivers/iio/proximity/sx9360.c static const struct { · line 191

Quoted 11 lines

static const struct {

drivers/iio/proximity/sx9360.c +3 -3
 	int val;
 	int val2;
 } sx9360_samp_freq_interval[] = {
-	{ 0, 281250 },  /* 4MHz / (8192 * 2047) */
-	{ 0, 281250 },
-	{ 448, 281250 },  /* 4MHz / 8192 */
+	{ 0, 238535 },  /* 4MHz / (8192 * 2047) */
+	{ 0, 238419 },
+	{ 488, 281250 },  /* 4MHz / 8192 */
 };

At some point this should be converted to double array of int:s.

...

drivers/iio/proximity/sx9360.c static int sx9360_read_avail(struct iio_dev *indio_dev, · line 389

Quoted 5 lines

static int sx9360_set_samp_freq(struct sx_common_data *data,

drivers/iio/proximity/sx9360.c +1 -1
 				int val, int val2)
 {
-	int reg;
+	unsigned int period;

What's the units for period? Seconds?

drivers/iio/proximity/sx9360.c static int sx9360_read_avail(struct iio_dev *indio_dev, · line 390

Quoted 1 line
drivers/iio/proximity/sx9360.c +1 -0
+	u64 freq_uhz;

_uHz

drivers/iio/proximity/sx9360.c static int sx9360_read_avail(struct iio_dev *indio_dev, · line 396

Quoted 8 lines
drivers/iio/proximity/sx9360.c +4 -2
 	__be16 buf;
 
-	reg = val * 8192 / SX9360_FOSC_HZ + val2 * 8192 / (SX9360_FOSC_MHZ);
-	buf = cpu_to_be16(reg);
+	if (val < 0 || val2 < 0)
+		return -EINVAL;
+
+	freq_uhz = (u64)val * MICRO + val2;

Read what units.h has for units of Hz.

drivers/iio/proximity/sx9360.c static int sx9360_read_avail(struct iio_dev *indio_dev, · line 402

Quoted 6 lines
drivers/iio/proximity/sx9360.c +6 -0
+	if (!freq_uhz)
+		return -EINVAL;
+
+	/* The register holds the scan period, in units of 8192 / FOSC. */
+	period = DIV64_U64_ROUND_CLOSEST((u64)SX9360_FOSC_HZ * MICRO,
+					 freq_uhz * 8192);

Ditto.

drivers/iio/proximity/sx9360.c static int sx9360_read_avail(struct iio_dev *indio_dev, · line 406

Quoted 4 lines
drivers/iio/proximity/sx9360.c +4 -0
+	if (!period || period > SX9360_REG_GNRL_SCANPERIOD_MAX)
+		return -EINVAL;
+
+	buf = cpu_to_be16(period);

Missing blank line. Always make guard()() to be standalone in the code.

Quoted 3 lines
drivers/iio/proximity/sx9360.c +0 -0
 	guard(mutex)(&data->mutex);
 
 	return regmap_bulk_write(data->regmap, SX9360_REG_GNRL_CTRL1, &buf,
Signature · 2 lines
With Best Regards,
Andy Shevchenko