Linux IIO development
 help / color / mirror / Atom feed
From: Md Shofiqul Islam <shofiqtest@gmail.com>
To: linux-iio@vger.kernel.org
Cc: jic23@kernel.org, lars@metafoo.de, shofiqtest@gmail.com
Subject: [PATCH v3] iio: accel: adxl372: Add timestamp to FIFO data
Date: Sun, 17 May 2026 12:17:35 +0300	[thread overview]
Message-ID: <20260517091735.135610-1-shofiqtest@gmail.com> (raw)
In-Reply-To: <20260516211935.36773-1-shofiqtest@gmail.com>

Add a software timestamp to each sample set pushed to the IIO buffer
during FIFO readout. IIO_CHAN_SOFT_TIMESTAMP(3) is added to the channel
spec so the IIO core includes the timestamp slot in scan_bytes.

The timestamp is captured once per FIFO watermark interrupt via
iio_get_time_ns() and reused for every sample set drained from the FIFO
in that invocation. This is an approximation: the single timestamp
reflects when the interrupt was handled, which is closest in time to the
last sample in the burst. Earlier samples in the same burst carry an
increasingly stale timestamp. This is acceptable for coarse event
sequencing and consistent with watermark-based FIFO readout in other
IIO accelerometer drivers.

Use iio_push_to_buffers_with_ts_unaligned() rather than
iio_push_to_buffers_with_ts(): the latter writes the s64 timestamp
directly into the source buffer at offset ALIGN(data_sz - 8, 8), which
in a multi-sample FIFO drain loop overwrites FIFO entries not yet
consumed by the next iteration. The _unaligned() variant copies each
sample into an internal bounce buffer and leaves the callers data
intact.

Signed-off-by: Md Shofiqul Islam <shofiqtest@gmail.com>
---
Changes since v2:
- Use iio_push_to_buffers_with_ts_unaligned() instead of
  iio_push_to_buffers_with_ts() to avoid writing the timestamp into the
  source buffer during the FIFO drain loop (Stepan Ionichev)
- Update commit message to explain the single-timestamp-per-IRQ
  approximation and why _unaligned() is needed (David Lechner)
 drivers/iio/accel/adxl372.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/accel/adxl372.c b/drivers/iio/accel/adxl372.c
index e375d068a..b108b214b 100644
--- a/drivers/iio/accel/adxl372.c
+++ b/drivers/iio/accel/adxl372.c
@@ -344,6 +344,7 @@ static const struct iio_chan_spec adxl372_channels[] = {
 	ADXL372_ACCEL_CHANNEL(0, ADXL372_X_DATA_H, X),
 	ADXL372_ACCEL_CHANNEL(1, ADXL372_Y_DATA_H, Y),
 	ADXL372_ACCEL_CHANNEL(2, ADXL372_Z_DATA_H, Z),
+	IIO_CHAN_SOFT_TIMESTAMP(3),
 };
 
 struct adxl372_state {
@@ -365,7 +366,7 @@ struct adxl372_state {
 	u8				fifo_set_size;
 	unsigned long			int1_bitmask;
 	u16				watermark;
-	__be16				fifo_buf[ADXL372_FIFO_SIZE];
+	IIO_DECLARE_BUFFER_WITH_TS(__be16, fifo_buf, ADXL372_FIFO_SIZE);
 	bool				peak_fifo_mode_en;
 	struct mutex			threshold_m; /* lock for threshold */
 };
@@ -703,13 +704,15 @@ static irqreturn_t adxl372_trigger_handler(int irq, void  *p)
 	struct adxl372_state *st = iio_priv(indio_dev);
 	u8 status1, status2;
 	u16 fifo_entries;
+	s64 ts;
 	int i, ret;
 
 	ret = adxl372_get_status(st, &status1, &status2, &fifo_entries);
 	if (ret < 0)
 		goto err;
 
-	adxl372_push_event(indio_dev, iio_get_time_ns(indio_dev), status2);
+	ts = iio_get_time_ns(indio_dev);
+	adxl372_push_event(indio_dev, ts, status2);
 
 	if (st->fifo_mode != ADXL372_FIFO_BYPASSED &&
 	    ADXL372_STATUS_1_FIFO_FULL(status1)) {
@@ -733,7 +736,9 @@ static irqreturn_t adxl372_trigger_handler(int irq, void  *p)
 			/* filter peak detection data */
 			if (st->peak_fifo_mode_en)
 				adxl372_arrange_axis_data(st, &st->fifo_buf[i]);
-			iio_push_to_buffers(indio_dev, &st->fifo_buf[i]);
+			iio_push_to_buffers_with_ts_unaligned(indio_dev, &st->fifo_buf[i],
+						    st->fifo_set_size * sizeof(__be16),
+						    ts);
 		}
 	}
 err:
-- 
2.51.1


  parent reply	other threads:[~2026-05-17  9:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-16 21:19 [PATCH v2] iio: accel: adxl372: Add timestamp to FIFO data Md Shofiqul Islam
2026-05-16 23:35 ` David Lechner
2026-05-16 23:36 ` David Lechner
2026-05-17  7:14 ` Stepan Ionichev
2026-05-17  9:17 ` Md Shofiqul Islam [this message]
2026-05-17  9:19 ` Md Shofiqul Islam
2026-05-17 12:57   ` Jonathan Cameron
2026-05-17  9:19 ` Md Shofiqul Islam

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260517091735.135610-1-shofiqtest@gmail.com \
    --to=shofiqtest@gmail.com \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox