]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
iio: buffer: cache timestamp offset in scan buffer
authorDavid Lechner <dlechner@baylibre.com>
Sun, 8 Mar 2026 01:44:11 +0000 (19:44 -0600)
committerJonathan Cameron <jic23@kernel.org>
Mon, 27 Apr 2026 08:58:15 +0000 (09:58 +0100)
Cache the offset (in bytes) for the timestamp element in a scan buffer.
This will be used later to ensure proper alignment of the timestamp
element in the scan buffer.

The new field could not be placed in struct iio_dev_opaque because we
will need to access it in a static inline function later, so we make it
__private instead. It is only intended to be used by core IIO code.

Signed-off-by: David Lechner <dlechner@baylibre.com>
Reviewed-by: Nuno Sá <nuno.sa@analog.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/industrialio-buffer.c
include/linux/iio/iio.h

index 71dfc81cb9e57c3bab543e526faa5050dbc5adeb..d59ab57dc994292ee91b0dcaadcaaff4dc9e5065 100644 (file)
@@ -765,7 +765,8 @@ static int iio_storage_bytes_for_timestamp(struct iio_dev *indio_dev)
 
 static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
                                  const unsigned long *mask, bool timestamp,
-                                 unsigned int *scan_bytes)
+                                 unsigned int *scan_bytes,
+                                 unsigned int *timestamp_offset)
 {
        unsigned int bytes = 0;
        int length, i, largest = 0;
@@ -787,6 +788,10 @@ static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
                        return length;
 
                bytes = ALIGN(bytes, length);
+
+               if (timestamp_offset)
+                       *timestamp_offset = bytes;
+
                bytes += length;
                largest = max(largest, length);
        }
@@ -848,7 +853,7 @@ static int iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
                return 0;
 
        ret = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
-                                    buffer->scan_timestamp, &bytes);
+                                    buffer->scan_timestamp, &bytes, NULL);
        if (ret)
                return ret;
 
@@ -892,6 +897,7 @@ struct iio_device_config {
        unsigned int watermark;
        const unsigned long *scan_mask;
        unsigned int scan_bytes;
+       unsigned int scan_timestamp_offset;
        bool scan_timestamp;
 };
 
@@ -997,7 +1003,8 @@ static int iio_verify_update(struct iio_dev *indio_dev,
        }
 
        ret = iio_compute_scan_bytes(indio_dev, scan_mask, scan_timestamp,
-                                    &config->scan_bytes);
+                                    &config->scan_bytes,
+                                    &config->scan_timestamp_offset);
        if (ret)
                return ret;
 
@@ -1155,6 +1162,7 @@ static int iio_enable_buffers(struct iio_dev *indio_dev,
        indio_dev->active_scan_mask = config->scan_mask;
        ACCESS_PRIVATE(indio_dev, scan_timestamp) = config->scan_timestamp;
        indio_dev->scan_bytes = config->scan_bytes;
+       ACCESS_PRIVATE(indio_dev, scan_timestamp_offset) = config->scan_timestamp_offset;
        iio_dev_opaque->currentmode = config->mode;
 
        iio_update_demux(indio_dev);
index 2c91b7659ce931dc73f201c87adb38a501535cad..ecbaeecbe0ac604118a9f128c3f3c61ff6487e8f 100644 (file)
@@ -584,6 +584,8 @@ struct iio_buffer_setup_ops {
  *                     and owner
  * @buffer:            [DRIVER] any buffer present
  * @scan_bytes:                [INTERN] num bytes captured to be fed to buffer demux
+ * @scan_timestamp_offset: [INTERN] cache of the offset (in bytes) for the
+ *                        timestamp in the scan buffer
  * @available_scan_masks: [DRIVER] optional array of allowed bitmasks. Sort the
  *                        array in order of preference, the most preferred
  *                        masks first.
@@ -610,6 +612,7 @@ struct iio_dev {
 
        struct iio_buffer               *buffer;
        int                             scan_bytes;
+       unsigned int                    __private scan_timestamp_offset;
 
        const unsigned long             *available_scan_masks;
        unsigned int                    __private masklength;