Skip to contentSkip to conversation

iio: accel: adxl345: add FIFO operating with IRQ triggered watermark events

PATCH In review Lothar Rubusch 3 participants 13 messages Dec 28, 2024 – Jan 12, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v9 cover iio: accel: adxl345: add FIFO operating with IRQ triggered watermark events

The adxl345 sensor offers several features. Most of them are based on using the hardware FIFO and reacting on events coming in on an interrupt line. Add access to configure and read out the FIFO, handling of interrupts and configuration and application of the watermark feature on that FIFO.

---
v8 -> v9:
- move FIELD_PREP() usage to a local usage in adxl345_core.c
- remove ADXL345_{SD}_TAP_MSK macros
v7 -> v8:
- remove dt-binding quotation marks
- add DMA alignment
- fix indention, formatting and text alignment
v6 -> v7:
- reorder dt-binding patches
- extracted FIFO specific from constants list
- reorder constants list in header patch to the end
- verify watermark input is within valid range
v5 -> v6:
- dropped justify patch, since unnecessary change to format mask
- added separate dt-bindings patch to remove required interrupts property
- merged FIFO watermark patches
- reworked bitfield handling
- group irq setup in probe()
- several type fixes by smatch and tools
v4 -> v5:
- fix dt-binding for enum array of INT1 and INT2
v3 -> v4:
- fix dt-binding indention
v2 -> v3:
- reorganize commits, merge the watermark handling
- INT lines are defined by binding
- kfifo is prepared by devm_iio_kfifo_buffer_setup()
- event handler is registered w/ devm_request_threaded_irq() v1 -> v2:
Fix comments according to Documentation/doc-guide/kernel-doc.rst and missing static declaration of function.
---
Lothar Rubusch (4):
iio: accel: adxl345: introduce interrupt handling
iio: accel: adxl345: initialize FIFO delay value for SPI
iio: accel: adxl345: add FIFO with watermark events
iio: accel: adxl345: complete the list of defines

 drivers/iio/accel/adxl345.h      |  77 +++++--
 drivers/iio/accel/adxl345_core.c | 338 ++++++++++++++++++++++++++++++-
 drivers/iio/accel/adxl345_i2c.c  |   2 +-
 drivers/iio/accel/adxl345_spi.c  |   7 +-
 4 files changed, 404 insertions(+), 20 deletions(-)
Signature · 1 line
2.39.5
#1 lore raw

PATCH v9 1/4 iio: accel: adxl345: introduce interrupt handling

Add the possibility to claim an interrupt. Init the state structure with an interrupt line obtained from the DT. The adxl345 can use two different interrupt lines for event handling. Only one is used.

---
 drivers/iio/accel/adxl345_core.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
drivers/iio/accel/adxl345_core.c +16 -0
index 27d70a1f0..134e72540 100644
@@ -7,6 +7,7 @@
  * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf
  */
 
+#include <linux/interrupt.h>
 #include <linux/module.h>
 #include <linux/property.h>
 #include <linux/regmap.h>
@@ -17,9 +18,15 @@
 
 #include "adxl345.h"
 
+#define ADXL345_INT_NONE		0xff
+#define ADXL345_INT1			0
+#define ADXL345_INT2			1
+
 struct adxl345_state {
 	const struct adxl345_chip_info *info;
 	struct regmap *regmap;
+	int irq;
+	u8 intio;
 };
 
 #define ADXL345_CHANNEL(index, axis) {					\
@@ -262,6 +269,15 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap,
 	if (ret < 0)
 		return ret;
 
+	st->intio = ADXL345_INT1;
+	st->irq = fwnode_irq_get_byname(dev_fwnode(dev), "INT1");
+	if (st->irq < 0) {
+		st->intio = ADXL345_INT2;
+		st->irq = fwnode_irq_get_byname(dev_fwnode(dev), "INT2");
+		if (st->irq < 0)
+			st->intio = ADXL345_INT_NONE;
+	}
+
 	return devm_iio_device_register(dev, indio_dev);
 }
 EXPORT_SYMBOL_NS_GPL(adxl345_core_probe, IIO_ADXL345);
Signature · 1 line
2.39.5
#2 lore raw

PATCH v9 2/4 iio: accel: adxl345: initialize FIFO delay value for SPI

In reply to Lothar Rubusch

Add the possibility to delay FIFO access when SPI is used. According to the datasheet this is needed for the adxl345. When initialization happens over SPI the need for delay is to be signalized, and the delay will be used.

---
 drivers/iio/accel/adxl345.h      |  1 +
 drivers/iio/accel/adxl345_core.c | 11 +++++++++++
 drivers/iio/accel/adxl345_i2c.c  |  2 +-
 drivers/iio/accel/adxl345_spi.c  |  7 +++++--
 4 files changed, 18 insertions(+), 3 deletions(-)
drivers/iio/accel/adxl345.h +1 -0
index 3d5c8719d..6f39f16d3 100644
@@ -62,6 +62,7 @@ struct adxl345_chip_info {
 };
 
 int adxl345_core_probe(struct device *dev, struct regmap *regmap,
+		       bool fifo_delay_default,
 		       int (*setup)(struct device*, struct regmap*));
 
 #endif /* _ADXL345_H_ */
drivers/iio/accel/adxl345_core.c +11 -0
index 134e72540..987a0fe03 100644
@@ -25,6 +25,7 @@
 struct adxl345_state {
 	const struct adxl345_chip_info *info;
 	struct regmap *regmap;
+	bool fifo_delay; /* delay: delay is needed for SPI */
 	int irq;
 	u8 intio;
 };
@@ -196,12 +197,21 @@ static const struct iio_info adxl345_info = {
  * adxl345_core_probe() - Probe and setup for the accelerometer.
  * @dev:	Driver model representation of the device
  * @regmap:	Regmap instance for the device
+ * @fifo_delay_default: Using FIFO with SPI needs delay
  * @setup:	Setup routine to be executed right before the standard device
  *		setup
  *
+ * For SPI operation greater than 1.6 MHz, it is necessary to deassert the CS
+ * pin to ensure a total delay of 5 us; otherwise, the delay is not sufficient.
+ * The total delay necessary for 5 MHz operation is at most 3.4 us. This is not
+ * a concern when using I2C mode because the communication rate is low enough
+ * to ensure a sufficient delay between FIFO reads.
+ * Ref: "Retrieving Data from FIFO", p. 21 of 36, Data Sheet ADXL345 Rev. G
+ *
  * Return: 0 on success, negative errno on error
  */
 int adxl345_core_probe(struct device *dev, struct regmap *regmap,
+		       bool fifo_delay_default,
 		       int (*setup)(struct device*, struct regmap*))
 {
 	struct adxl345_state *st;
@@ -222,6 +232,7 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap,
 	st->info = device_get_match_data(dev);
 	if (!st->info)
 		return -ENODEV;
+	st->fifo_delay = fifo_delay_default;
 
 	indio_dev->name = st->info->name;
 	indio_dev->info = &adxl345_info;
drivers/iio/accel/adxl345_i2c.c +1 -1
index e550bc078..eb3e0aadf 100644
@@ -27,7 +27,7 @@ static int adxl345_i2c_probe(struct i2c_client *client)
 	if (IS_ERR(regmap))
 		return dev_err_probe(&client->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
-	return adxl345_core_probe(&client->dev, regmap, NULL);
+	return adxl345_core_probe(&client->dev, regmap, false, NULL);
 }
 
 static const struct adxl345_chip_info adxl345_i2c_info = {
drivers/iio/accel/adxl345_spi.c +5 -2
index 61fd9a6f5..e03915ece 100644
@@ -12,6 +12,7 @@
 #include "adxl345.h"
 
 #define ADXL345_MAX_SPI_FREQ_HZ		5000000
+#define ADXL345_MAX_FREQ_NO_FIFO_DELAY	1500000
 
 static const struct regmap_config adxl345_spi_regmap_config = {
 	.reg_bits = 8,
@@ -28,6 +29,7 @@ static int adxl345_spi_setup(struct device *dev, struct regmap *regmap)
 static int adxl345_spi_probe(struct spi_device *spi)
 {
 	struct regmap *regmap;
+	bool needs_delay;
 
 	/* Bail out if max_speed_hz exceeds 5 MHz */
 	if (spi->max_speed_hz > ADXL345_MAX_SPI_FREQ_HZ)
@@ -38,10 +40,11 @@ static int adxl345_spi_probe(struct spi_device *spi)
 	if (IS_ERR(regmap))
 		return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
+	needs_delay = spi->max_speed_hz > ADXL345_MAX_FREQ_NO_FIFO_DELAY;
 	if (spi->mode & SPI_3WIRE)
-		return adxl345_core_probe(&spi->dev, regmap, adxl345_spi_setup);
+		return adxl345_core_probe(&spi->dev, regmap, needs_delay, adxl345_spi_setup);
 	else
-		return adxl345_core_probe(&spi->dev, regmap, NULL);
+		return adxl345_core_probe(&spi->dev, regmap, needs_delay, NULL);
 }
 
 static const struct adxl345_chip_info adxl345_spi_info = {
Signature · 1 line
2.39.5
#3 lore raw

Re: [PATCH v9 2/4] iio: accel: adxl345: initialize FIFO delay value for SPI

Sat, Dec 28, 2024 at 11:29:47PM +0000, Lothar Rubusch kirjoitti:

Quoted 4 lines

Add the possibility to delay FIFO access when SPI is used. According to the datasheet this is needed for the adxl345. When initialization happens over SPI the need for delay is to be signalized, and the delay will be used.

...

drivers/iio/accel/adxl345.h struct adxl345_chip_info { · line 66

Quoted 3 lines

int adxl345_core_probe(struct device *dev, struct regmap *regmap,

drivers/iio/accel/adxl345.h +1 -0
+		       bool fifo_delay_default,
 		       int (*setup)(struct device*, struct regmap*));

Missing spaces, but it seems the original issue.

...

drivers/iio/accel/adxl345_core.c static const struct iio_info adxl345_info = { · line 215

Quoted 3 lines

int adxl345_core_probe(struct device *dev, struct regmap *regmap,

drivers/iio/accel/adxl345_core.c +1 -0
+		       bool fifo_delay_default,
 		       int (*setup)(struct device*, struct regmap*))

Ditto.

...

Quoted 2 lines

#define ADXL345_MAX_SPI_FREQ_HZ 5000000
+#define ADXL345_MAX_FREQ_NO_FIFO_DELAY 1500000

In which units? Usually we add a unit suffix, e.g., _US (for microseconds).

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

PATCH v9 3/4 iio: accel: adxl345: add FIFO with watermark events

In reply to Lothar Rubusch

Add a basic setup for FIFO with configurable watermark. Add a handler for watermark interrupt events and extend the channel for the scan_index needed for the iio channel. The sensor is configurable to use a FIFO_BYPASSED mode or a FIFO_STREAM mode. For the FIFO_STREAM mode now a watermark can be configured, or disabled by setting 0. Further features require a working FIFO setup.

---
 drivers/iio/accel/adxl345.h      |  22 ++-
 drivers/iio/accel/adxl345_core.c | 311 ++++++++++++++++++++++++++++++-
 2 files changed, 322 insertions(+), 11 deletions(-)
drivers/iio/accel/adxl345.h +16 -6
index 6f39f16d3..b78b4973a 100644
@@ -15,18 +15,27 @@
 #define ADXL345_REG_OFS_AXIS(index)	(ADXL345_REG_OFSX + (index))
 #define ADXL345_REG_BW_RATE		0x2C
 #define ADXL345_REG_POWER_CTL		0x2D
+#define ADXL345_REG_INT_ENABLE		0x2E
+#define ADXL345_REG_INT_MAP		0x2F
+#define ADXL345_REG_INT_SOURCE		0x30
+#define ADXL345_REG_INT_SOURCE_MSK	0xFF
 #define ADXL345_REG_DATA_FORMAT		0x31
-#define ADXL345_REG_DATAX0		0x32
-#define ADXL345_REG_DATAY0		0x34
-#define ADXL345_REG_DATAZ0		0x36
-#define ADXL345_REG_DATA_AXIS(index)	\
-	(ADXL345_REG_DATAX0 + (index) * sizeof(__le16))
+#define ADXL345_REG_XYZ_BASE		0x32
+#define ADXL345_REG_DATA_AXIS(index)				\
+	(ADXL345_REG_XYZ_BASE + (index) * sizeof(__le16))
 
+#define ADXL345_REG_FIFO_CTL		0x38
+#define ADXL345_REG_FIFO_STATUS	0x39
+#define ADXL345_REG_FIFO_STATUS_MSK	0x3F
+
+#define ADXL345_INT_OVERRUN		BIT(0)
+#define ADXL345_INT_WATERMARK		BIT(1)
+#define ADXL345_INT_DATA_READY		BIT(7)
 #define ADXL345_BW_RATE			GENMASK(3, 0)
 #define ADXL345_BASE_RATE_NANO_HZ	97656250LL
 
-#define ADXL345_POWER_CTL_MEASURE	BIT(3)
 #define ADXL345_POWER_CTL_STANDBY	0x00
+#define ADXL345_POWER_CTL_MEASURE	BIT(3)
 
 #define ADXL345_DATA_FORMAT_RANGE	GENMASK(1, 0)	/* Set the g range */
 #define ADXL345_DATA_FORMAT_JUSTIFY	BIT(2)	/* Left-justified (MSB) mode */
@@ -40,6 +49,7 @@
 #define ADXL345_DATA_FORMAT_16G		3
 
 #define ADXL345_DEVID			0xE5
+#define ADXL345_FIFO_SIZE		32
 
 /*
  * In full-resolution mode, scale factor is maintained at ~4 mg/LSB
drivers/iio/accel/adxl345_core.c +306 -5

385 lines not on this page · raw message

Signature · 1 line
2.39.5
maintainer
#5 lore raw

Re: [PATCH v9 3/4] iio: accel: adxl345: add FIFO with watermark events

Quoted 8 lines · On Sat, 28 Dec 2024 23:29:48 +0000 Lothar Rubusch <[email protected]> wrote:

Add a basic setup for FIFO with configurable watermark. Add a handler for watermark interrupt events and extend the channel for the scan_index needed for the iio channel. The sensor is configurable to use a FIFO_BYPASSED mode or a FIFO_STREAM mode. For the FIFO_STREAM mode now a watermark can be configured, or disabled by setting 0. Further features require a working FIFO setup.

Hi Lothar,

Applied with a tweak. See below and please check my testing branch to see if I messed this mechanical change up (wouldn't be the first time!)

Thanks,

Jonathan

Review

Quoted 5 lines
(quoted diff) +4 -0
+#define ADXL345_FIFO_CTL_SAMPLES(x)	FIELD_PREP(GENMASK(4, 0), x)
+/* 0: INT1, 1: INT2 */
+#define ADXL345_FIFO_CTL_TRIGGER(x)	FIELD_PREP(BIT(5), x)
+#define ADXL345_FIFO_CTL_MODE(x)	FIELD_PREP(GENMASK(7, 6), x)

Ah. I now realize I subtly misread your reply to v8.

What I want to see the masks defined with the rest of the fields and the FIELD_PREP used with those masks inline.

Rather than go around again, I've applied the following tweak.

drivers/iio/accel/adxl345.h +4 -0
index b78b4973a4d4..9fcf6756768e 100644
@@ -25,6 +25,10 @@
 	(ADXL345_REG_XYZ_BASE + (index) * sizeof(__le16))
 
 #define ADXL345_REG_FIFO_CTL		0x38
+#define ADXL345_FIFO_CTL_SAMPLES_MSK	GENMASK(4, 0)
+/* 0: INT1, 1: INT2 */
+#define ADXL345_FIFO_CTL_TRIGGER_MSK	BIT(5)
+#define ADXL345_FIFO_CTL_MODE_MSK	GENMASK(7, 6)
 #define ADXL345_REG_FIFO_STATUS	0x39
 #define ADXL345_REG_FIFO_STATUS_MSK	0x3F
 
drivers/iio/accel/adxl345_core.c +9 -12
index d33e3c6528a9..d1b2d3985a40 100644
@@ -31,11 +31,6 @@
 #define ADXL345_INT1			0
 #define ADXL345_INT2			1
 
-#define ADXL345_FIFO_CTL_SAMPLES(x)	FIELD_PREP(GENMASK(4, 0), x)
-/* 0: INT1, 1: INT2 */
-#define ADXL345_FIFO_CTL_TRIGGER(x)	FIELD_PREP(BIT(5), x)
-#define ADXL345_FIFO_CTL_MODE(x)	FIELD_PREP(GENMASK(7, 6), x)
-
 struct adxl345_state {
 	const struct adxl345_chip_info *info;
 	struct regmap *regmap;
@@ -269,9 +264,12 @@ static int adxl345_set_fifo(struct adxl345_state *st)
 		return ret;
 
 	ret = regmap_write(st->regmap, ADXL345_REG_FIFO_CTL,
-			   ADXL345_FIFO_CTL_SAMPLES(st->watermark) |
-			   ADXL345_FIFO_CTL_TRIGGER(st->intio) |
-			   ADXL345_FIFO_CTL_MODE(st->fifo_mode));
+			   FIELD_PREP(ADXL345_FIFO_CTL_SAMPLES_MSK,
+				      st->watermark) |
+			   FIELD_PREP(ADXL345_FIFO_CTL_TRIGGER_MSK,
+				      st->intio) |
+			   FIELD_PREP(ADXL345_FIFO_CTL_MODE_MSK,
+				      st->fifo_mode));
 	if (ret < 0)
 		return ret;
 
@@ -500,7 +498,6 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap,
 					 ADXL345_DATA_FORMAT_JUSTIFY |
 					 ADXL345_DATA_FORMAT_FULL_RES |
 					 ADXL345_DATA_FORMAT_SELF_TEST);
-	u8 fifo_ctl;
 	int ret;
 
 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
@@ -583,9 +580,9 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap,
 		if (ret)
 			return ret;
 	} else {
-		/* FIFO_BYPASS mode */
-		fifo_ctl = ADXL345_FIFO_CTL_MODE(ADXL345_FIFO_BYPASS);
-		ret = regmap_write(st->regmap, ADXL345_REG_FIFO_CTL, fifo_ctl);
+		ret = regmap_write(st->regmap, ADXL345_REG_FIFO_CTL,
+				   FIELD_PREP(ADXL345_FIFO_CTL_MODE_MSK,
+					      ADXL345_FIFO_BYPASS));
 		if (ret < 0)
 			return ret;
 	}
#6 lore raw

Re: [PATCH v9 3/4] iio: accel: adxl345: add FIFO with watermark events

In reply to Lothar Rubusch

Sat, Dec 28, 2024 at 11:29:48PM +0000, Lothar Rubusch kirjoitti:

Quoted 6 lines

Add a basic setup for FIFO with configurable watermark. Add a handler for watermark interrupt events and extend the channel for the scan_index needed for the iio channel. The sensor is configurable to use a FIFO_BYPASSED mode or a FIFO_STREAM mode. For the FIFO_STREAM mode now a watermark can be configured, or disabled by setting 0. Further features require a working FIFO setup.

...

drivers/iio/accel/adxl345_core.c line 20

Quoted 4 lines

#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>

drivers/iio/accel/adxl345_core.c +2 -0
+#include <linux/iio/buffer.h>
+#include <linux/iio/kfifo_buf.h>

Why not keep it ordered?

...

drivers/iio/accel/adxl345_core.c static int adxl345_write_raw(struct iio_dev *indio_dev, · line 193

Quoted 4 lines
drivers/iio/accel/adxl345_core.c +4 -0
+static int adxl345_set_watermark(struct iio_dev *indio_dev, unsigned int value)
+{
+	struct adxl345_state *st = iio_priv(indio_dev);
+	unsigned int fifo_mask = 0x1F;

GENMASK() ?
(BIT(5) - 1) ?

Quoted 13 lines
drivers/iio/accel/adxl345_core.c +13 -0
+	int ret;
+
+	value = min(value, ADXL345_FIFO_SIZE - 1);
+
+	ret = regmap_update_bits(st->regmap, ADXL345_REG_FIFO_CTL, fifo_mask, value);
+	if (ret)
+		return ret;
+
+	st->watermark = value;
+	st->int_map |= ADXL345_INT_WATERMARK;
+
+	return 0;
+}

...

drivers/iio/accel/adxl345_core.c static const struct attribute_group adxl345_attrs_group = { · line 316

Quoted 1 line
drivers/iio/accel/adxl345_core.c +1 -0
+	int i, ret = 0;

Why is i signed?

Quoted 14 lines
drivers/iio/accel/adxl345_core.c +14 -0
+
+	/* count is the 3x the fifo_buf element size, hence 6B */
+	count = sizeof(st->fifo_buf[0]) * ADXL345_DIRS;
+	for (i = 0; i < samples; i++) {
+		/* read 3x 2 byte elements from base address into next fifo_buf position */
+		ret = regmap_bulk_read(st->regmap, ADXL345_REG_XYZ_BASE,
+				       st->fifo_buf + (i * count / 2), count);
+		if (ret < 0)
+			return ret;
+
+		/*
+		 * To ensure that the FIFO has completely popped, there must be at least 5
+		 * us between the end of reading the data registers, signified by the
+		 * transition to register 0x38 from 0x37 or the CS pin going high, and the

drivers/iio/accel/adxl345_core.c static const struct attribute_group adxl345_attrs_group = { · line 340

Quoted 10 lines
drivers/iio/accel/adxl345_core.c +10 -0
+		 * start of new reads of the FIFO or reading the FIFO_STATUS register. For
+		 * SPI operation at 1.5 MHz or lower, the register addressing portion of the
+		 * transmission is sufficient delay to ensure the FIFO has completely
+		 * popped. It is necessary for SPI operation greater than 1.5 MHz to
+		 * de-assert the CS pin to ensure a total of 5 us, which is at most 3.4 us
+		 * at 5 MHz operation.
+		 */
+		if (st->fifo_delay && samples > 1)
+			udelay(3);
+	}

MIssed blank line.

Quoted 1 line
(quoted diff) +1 -0
+	return ret;

...

drivers/iio/accel/adxl345_core.c static const struct attribute_group adxl345_attrs_group = { · line 412

Quoted 2 lines
drivers/iio/accel/adxl345_core.c +2 -0
+static int adxl345_fifo_push(struct iio_dev *indio_dev,
+			     int samples)

This can be effectively a single line.

drivers/iio/accel/adxl345_core.c static const struct attribute_group adxl345_attrs_group = { · line 418

Quoted 6 lines
drivers/iio/accel/adxl345_core.c +5 -0
+{
+	struct adxl345_state *st = iio_priv(indio_dev);
+	int i, ret;
 
+	if (samples <= 0)
+		return -EINVAL;

This is strange. Why the heck somebody could translate the input parameter to an error? Can't the function simply take the positive input only? Otherwise, what is the meaning of the samples < 0 ?

drivers/iio/accel/adxl345_core.c static const struct attribute_group adxl345_attrs_group = {

Quoted 5 lines
drivers/iio/accel/adxl345_core.c +4 -0
+	ret = adxl345_fifo_transfer(st, samples);
+	if (ret)
+		return ret;
 
+	for (i = 0; i < ADXL345_DIRS * samples; i += ADXL345_DIRS)

Why doing mulpiplication here and step != 1 there instead of just one multiplication...

drivers/iio/accel/adxl345_core.c static const struct attribute_group adxl345_attrs_group = { · line 425

Quoted 1 line
drivers/iio/accel/adxl345_core.c +1 -0
+		iio_push_to_buffers(indio_dev, &st->fifo_buf[i]);

...in the above line?

Quoted 2 lines
(quoted diff) +2 -0
+	return 0;
+}

...

Quoted 15 lines
drivers/iio/accel/adxl345_core.c +15 -0
+/**
+ * adxl345_irq_handler() - Handle irqs of the ADXL345.
+ * @irq: The irq being handled.
+ * @p: The struct iio_device pointer for the device.
+ *
+ * Return: The interrupt was handled.
+ */
+static irqreturn_t adxl345_irq_handler(int irq, void *p)
+{
+	struct iio_dev *indio_dev = p;
+	struct adxl345_state *st = iio_priv(indio_dev);
+	int int_stat;
+	int samples;
+
+	int_stat = adxl345_get_status(st);

drivers/iio/accel/adxl345_core.c static const struct attribute_group adxl345_attrs_group = { · line 454

Quoted 10 lines
drivers/iio/accel/adxl345_core.c +10 -0
+	if (int_stat <= 0)
+		return IRQ_NONE;
+
+	if (int_stat & ADXL345_INT_OVERRUN)
+		goto err;
+
+	if (int_stat & ADXL345_INT_WATERMARK) {
+		samples = adxl345_get_samples(st);
+		if (samples < 0)
+			goto err;

You already seem to guarantee no negative samples, and TBH this has to be

		ret = ...
		...

		samples = ret;

which will make more sense and better types.

Quoted 10 lines
drivers/iio/accel/adxl345_core.c +10 -0
+		if (adxl345_fifo_push(indio_dev, samples) < 0)
+			goto err;
+	}
+	return IRQ_HANDLED;
+
+err:
+	adxl345_fifo_reset(st);
+
+	return IRQ_HANDLED;
+}

...

drivers/iio/accel/adxl345_core.c int adxl345_core_probe(struct device *dev, struct regmap *regmap, · line 573

Quoted 1 line
drivers/iio/accel/adxl345_core.c +1 -0
+	if (st->intio != ADXL345_INT_NONE) {

What's wrong with positive conditional?
Negative is slightly harder to read and parse at a glance.

Quoted 18 lines
drivers/iio/accel/adxl345_core.c +18 -0
+		/* FIFO_STREAM mode is going to be activated later */
+		ret = devm_iio_kfifo_buffer_setup(dev, indio_dev, &adxl345_buffer_ops);
+		if (ret)
+			return ret;
+
+		ret = devm_request_threaded_irq(dev, st->irq, NULL,
+						&adxl345_irq_handler,
+						IRQF_SHARED | IRQF_ONESHOT,
+						indio_dev->name, indio_dev);
+		if (ret)
+			return ret;
+	} else {
+		/* FIFO_BYPASS mode */
+		fifo_ctl = ADXL345_FIFO_CTL_MODE(ADXL345_FIFO_BYPASS);
+		ret = regmap_write(st->regmap, ADXL345_REG_FIFO_CTL, fifo_ctl);
+		if (ret < 0)
+			return ret;
+	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

PATCH v9 4/4 iio: accel: adxl345: complete the list of defines

In reply to Lothar Rubusch

Having interrupts events and FIFO available allows to evaluate the sensor events. Cover the list of interrupt based sensor events. Keep them in the header file for readability.

---
 drivers/iio/accel/adxl345.h | 54 ++++++++++++++++++++++++++++++++-----
 1 file changed, 48 insertions(+), 6 deletions(-)
drivers/iio/accel/adxl345.h +48 -6
index b78b4973a..bf7cf15a8 100644
@@ -9,10 +9,35 @@
 #define _ADXL345_H_
 
 #define ADXL345_REG_DEVID		0x00
+#define ADXL345_REG_THRESH_TAP		0x1D
 #define ADXL345_REG_OFSX		0x1E
 #define ADXL345_REG_OFSY		0x1F
 #define ADXL345_REG_OFSZ		0x20
 #define ADXL345_REG_OFS_AXIS(index)	(ADXL345_REG_OFSX + (index))
+
+/* Tap duration */
+#define ADXL345_REG_DUR		0x21
+/* Tap latency */
+#define ADXL345_REG_LATENT		0x22
+/* Tap window */
+#define ADXL345_REG_WINDOW		0x23
+/* Activity threshold */
+#define ADXL345_REG_THRESH_ACT		0x24
+/* Inactivity threshold */
+#define ADXL345_REG_THRESH_INACT	0x25
+/* Inactivity time */
+#define ADXL345_REG_TIME_INACT		0x26
+/* Axis enable control for activity and inactivity detection */
+#define ADXL345_REG_ACT_INACT_CTRL	0x27
+/* Free-fall threshold */
+#define ADXL345_REG_THRESH_FF		0x28
+/* Free-fall time */
+#define ADXL345_REG_TIME_FF		0x29
+/* Axis control for single tap or double tap */
+#define ADXL345_REG_TAP_AXIS		0x2A
+/* Source of single tap or double tap */
+#define ADXL345_REG_ACT_TAP_STATUS	0x2B
+/* Data rate and power mode control */
 #define ADXL345_REG_BW_RATE		0x2C
 #define ADXL345_REG_POWER_CTL		0x2D
 #define ADXL345_REG_INT_ENABLE		0x2E
@@ -30,19 +55,36 @@
 
 #define ADXL345_INT_OVERRUN		BIT(0)
 #define ADXL345_INT_WATERMARK		BIT(1)
+#define ADXL345_INT_FREE_FALL		BIT(2)
+#define ADXL345_INT_INACTIVITY		BIT(3)
+#define ADXL345_INT_ACTIVITY		BIT(4)
+#define ADXL345_INT_DOUBLE_TAP		BIT(5)
+#define ADXL345_INT_SINGLE_TAP		BIT(6)
 #define ADXL345_INT_DATA_READY		BIT(7)
+
+/*
+ * BW_RATE bits - Bandwidth and output data rate. The default value is
+ * 0x0A, which translates to a 100 Hz output data rate
+ */
 #define ADXL345_BW_RATE			GENMASK(3, 0)
+#define ADXL345_BW_LOW_POWER		BIT(4)
 #define ADXL345_BASE_RATE_NANO_HZ	97656250LL
 
 #define ADXL345_POWER_CTL_STANDBY	0x00
+#define ADXL345_POWER_CTL_WAKEUP	GENMASK(1, 0)
+#define ADXL345_POWER_CTL_SLEEP	BIT(2)
 #define ADXL345_POWER_CTL_MEASURE	BIT(3)
+#define ADXL345_POWER_CTL_AUTO_SLEEP	BIT(4)
+#define ADXL345_POWER_CTL_LINK		BIT(5)
 
-#define ADXL345_DATA_FORMAT_RANGE	GENMASK(1, 0)	/* Set the g range */
-#define ADXL345_DATA_FORMAT_JUSTIFY	BIT(2)	/* Left-justified (MSB) mode */
-#define ADXL345_DATA_FORMAT_FULL_RES	BIT(3)	/* Up to 13-bits resolution */
-#define ADXL345_DATA_FORMAT_SPI_3WIRE	BIT(6)	/* 3-wire SPI mode */
-#define ADXL345_DATA_FORMAT_SELF_TEST	BIT(7)	/* Enable a self test */
-
+/* Set the g range */
+#define ADXL345_DATA_FORMAT_RANGE	GENMASK(1, 0)
+/* Data is left justified */
+#define ADXL345_DATA_FORMAT_JUSTIFY	BIT(2)
+/* Up to 13-bits resolution */
+#define ADXL345_DATA_FORMAT_FULL_RES	BIT(3)
+#define ADXL345_DATA_FORMAT_SPI_3WIRE	BIT(6)
+#define ADXL345_DATA_FORMAT_SELF_TEST	BIT(7)
 #define ADXL345_DATA_FORMAT_2G		0
 #define ADXL345_DATA_FORMAT_4G		1
 #define ADXL345_DATA_FORMAT_8G		2
Signature · 1 line
2.39.5
maintainer
#8 lore raw
In reply to Lothar Rubusch
Quoted 6 lines · On Sat, 28 Dec 2024 23:29:45 +0000 Lothar Rubusch <[email protected]> wrote:

The adxl345 sensor offers several features. Most of them are based on using the hardware FIFO and reacting on events coming in on an interrupt line. Add access to configure and read out the FIFO, handling of interrupts and configuration and application of the watermark feature on that FIFO.

Series applied but with a tweak on patch 3. Please take a look at the testing branch where this will sit for a few days,

Jonathan

Quoted 45 lines

---
v8 -> v9:
- move FIELD_PREP() usage to a local usage in adxl345_core.c
- remove ADXL345_{SD}_TAP_MSK macros
v7 -> v8:
- remove dt-binding quotation marks
- add DMA alignment
- fix indention, formatting and text alignment
v6 -> v7:
- reorder dt-binding patches
- extracted FIFO specific from constants list
- reorder constants list in header patch to the end
- verify watermark input is within valid range
v5 -> v6:
- dropped justify patch, since unnecessary change to format mask
- added separate dt-bindings patch to remove required interrupts property
- merged FIFO watermark patches
- reworked bitfield handling
- group irq setup in probe()
- several type fixes by smatch and tools
v4 -> v5:
- fix dt-binding for enum array of INT1 and INT2
v3 -> v4:
- fix dt-binding indention
v2 -> v3:
- reorganize commits, merge the watermark handling
- INT lines are defined by binding
- kfifo is prepared by devm_iio_kfifo_buffer_setup()
- event handler is registered w/ devm_request_threaded_irq() v1 -> v2:
Fix comments according to Documentation/doc-guide/kernel-doc.rst and missing static declaration of function.
---
Lothar Rubusch (4):
iio: accel: adxl345: introduce interrupt handling
iio: accel: adxl345: initialize FIFO delay value for SPI
iio: accel: adxl345: add FIFO with watermark events
iio: accel: adxl345: complete the list of defines

 drivers/iio/accel/adxl345.h      |  77 +++++--
 drivers/iio/accel/adxl345_core.c | 338 ++++++++++++++++++++++++++++++-
 drivers/iio/accel/adxl345_i2c.c  |   2 +-
 drivers/iio/accel/adxl345_spi.c  |   7 +-
 4 files changed, 404 insertions(+), 20 deletions(-)
#9 lore raw

Hi Jonathan, find my answer inlined down below.

Quoted 4 lines · Jonathan Cameron

On Sat, 28 Dec 2024 23:29:45 +0000
Lothar Rubusch <[email protected]> wrote:

Quoted 9 lines
Quoted 6 lines

The adxl345 sensor offers several features. Most of them are based on using the hardware FIFO and reacting on events coming in on an interrupt line. Add access to configure and read out the FIFO, handling of interrupts and configuration and application of the watermark feature on that FIFO.

Series applied but with a tweak on patch 3. Please take a look at the testing branch where this will sit for a few days,

The patches w/ adjustments are looking fine at the sensor.

[Do I need to say ACK here, or the like? I guess not.]

Thank you so much for applying. I verified several watermark settings and could observe /proc/interrupts increasing with at a higher and lower pace, respectively, as expected. I could turn the feature on, off and back on. I saw the measurements on my sensor as they used to show up before.

I'll continue with the implementation of the sensor features, and I will have a more thorough look at it anyway. I really appreciate the adjustments. Again cleaner!

Best,
L

Quoted 48 lines

Jonathan

Quoted 45 lines

---
v8 -> v9:
- move FIELD_PREP() usage to a local usage in adxl345_core.c
- remove ADXL345_{SD}_TAP_MSK macros
v7 -> v8:
- remove dt-binding quotation marks
- add DMA alignment
- fix indention, formatting and text alignment
v6 -> v7:
- reorder dt-binding patches
- extracted FIFO specific from constants list
- reorder constants list in header patch to the end
- verify watermark input is within valid range
v5 -> v6:
- dropped justify patch, since unnecessary change to format mask
- added separate dt-bindings patch to remove required interrupts property
- merged FIFO watermark patches
- reworked bitfield handling
- group irq setup in probe()
- several type fixes by smatch and tools
v4 -> v5:
- fix dt-binding for enum array of INT1 and INT2
v3 -> v4:
- fix dt-binding indention
v2 -> v3:
- reorganize commits, merge the watermark handling
- INT lines are defined by binding
- kfifo is prepared by devm_iio_kfifo_buffer_setup()
- event handler is registered w/ devm_request_threaded_irq() v1 -> v2:
Fix comments according to Documentation/doc-guide/kernel-doc.rst and missing static declaration of function.
---
Lothar Rubusch (4):
iio: accel: adxl345: introduce interrupt handling
iio: accel: adxl345: initialize FIFO delay value for SPI
iio: accel: adxl345: add FIFO with watermark events
iio: accel: adxl345: complete the list of defines

 drivers/iio/accel/adxl345.h      |  77 +++++--
 drivers/iio/accel/adxl345_core.c | 338 ++++++++++++++++++++++++++++++-
 drivers/iio/accel/adxl345_i2c.c  |   2 +-
 drivers/iio/accel/adxl345_spi.c  |   7 +-
 4 files changed, 404 insertions(+), 20 deletions(-)
#10 lore raw
In reply to Jonathan Cameron

Sat, Jan 04, 2025 at 01:09:16PM +0000, Jonathan Cameron kirjoitti:

Quoted 11 lines
Quoted 6 lines · On Sat, 28 Dec 2024 23:29:45 +0000 Lothar Rubusch <[email protected]> wrote:

The adxl345 sensor offers several features. Most of them are based on using the hardware FIFO and reacting on events coming in on an interrupt line. Add access to configure and read out the FIFO, handling of interrupts and configuration and application of the watermark feature on that FIFO.

Series applied but with a tweak on patch 3. Please take a look at the testing branch where this will sit for a few days,

I would expect the comments I gave to be addressed as well before going to upstream, but sorry for having a bit prolonged vacation.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#11 lore raw
Quoted 9 lines · On Sun, 12 Jan 2025 18:06:59 +0200 Andy Shevchenko <[email protected]>…

Sat, Jan 04, 2025 at 01:09:16PM +0000, Jonathan Cameron kirjoitti:

Quoted 8 lines
Quoted 5 lines · On Sat, 28 Dec 2024 23:29:45 +0000 Lothar Rubusch <[email protected]> wrote:

The adxl345 sensor offers several features. Most of them are based on using the hardware FIFO and reacting on events coming in on an interrupt line. Add access to configure and read out the FIFO, handling of interrupts and configuration and application of the watermark feature on that FIFO.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Series applied but with a tweak on patch 3. Please take a look at the testing branch where this will sit for a few days,

I would expect the comments I gave to be addressed as well before going to upstream, but sorry for having a bit prolonged vacation.

I missed outstanding bits. Suggested improvements will need to be handled as cleanup patches on top.

Hope you had a good vacation.

Jonathan

#12 lore raw
Quoted 17 lines · Jonathan Cameron
Quoted 13 lines · On Sun, 12 Jan 2025 18:06:59 +0200 Andy Shevchenko <[email protected]>…

Sat, Jan 04, 2025 at 01:09:16PM +0000, Jonathan Cameron kirjoitti:

Quoted 11 lines
Quoted 6 lines · On Sat, 28 Dec 2024 23:29:45 +0000 Lothar Rubusch <[email protected]> wrote:

The adxl345 sensor offers several features. Most of them are based on using the hardware FIFO and reacting on events coming in on an interrupt line. Add access to configure and read out the FIFO, handling of interrupts and configuration and application of the watermark feature on that FIFO.

Series applied but with a tweak on patch 3. Please take a look at the testing branch where this will sit for a few days,

Quoted 7 lines
Quoted 3 lines

I would expect the comments I gave to be addressed as well before going to upstream, but sorry for having a bit prolonged vacation.

I missed outstanding bits.

Suggested improvements will need to be handled
as cleanup patches on top.

Yes, that's what I meant.

Quoted 1 line

Hope you had a good vacation.

I have a good enough one, thanks!

Signature · 2 lines
With Best Regards,
Andy Shevchenko