Skip to contentSkip to conversation

iio: accel: kionix-kx022a: unify staging buffer and convert to iio_push_to_buffers_with_ts()

PATCH In review Gabriel Rondon 2 participants 4 messages Aug 19 – Aug 23

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

v3 cover iio: accel: kionix-kx022a: unify staging buffer and convert to iio_push_to_buffers_with_ts()

This is the kx022a staging-buffer unification plus the deprecated-API conversion. Patch 1 drops the redundant buffer and routes the one-shot read and the triggered handler through scan; patch 2 does the iio_push_to_buffers_with_ts() conversion.

Changes in v3:
- get_axis(): use a local __le16 *buf and le16_to_cpup() (Andy)
- Add Suggested-by: Jonathan Cameron to patch 1
- Trim patch 2 changelog

v2: https://lore.kernel.org/linux-iio/[email protected]/

Gabriel Rondon (2):
iio: accel: kionix-kx022a: use scan struct for one-shot and trigger
reads
iio: accel: kionix-kx022a: use iio_push_to_buffers_with_ts()

 drivers/iio/accel/kionix-kx022a.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

PATCH v3 1/2 iio: accel: kionix-kx022a: use scan struct for one-shot and trigger reads

The driver kept two separate staging areas that hold the same thing:
buffer[8], a DMA-aligned area used by the one-shot read in kx022a_get_axis() and by the triggered handler, and the scan struct, used by the FIFO flush path. Both are three __le16 channels plus room for the timestamp.

Drop buffer and route the one-shot read and the triggered handler through scan.channels, so the driver has a single staging area. Move the IIO_DMA_MINALIGN alignment onto scan, since it now backs the regmap bulk reads that buffer used to.

No functional change. get_axis() only runs via read_raw() under iio_device_claim_direct(), so it cannot run while the triggered buffer is active, and the triggered handler only runs while it is; the two never touch scan concurrently, exactly as they previously shared buffer.

---
 drivers/iio/accel/kionix-kx022a.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/iio/accel/kionix-kx022a.c +7 -8
index 02dd1db7a646..93104fdc74b5 100644
@@ -301,11 +301,10 @@ struct kx022a_data {
 	__le16 *fifo_buffer;
 
 	/* 3 x 16bit accel data + timestamp */
-	__le16 buffer[8] __aligned(IIO_DMA_MINALIGN);
 	struct {
 		__le16 channels[3];
 		aligned_s64 ts;
-	} scan;
+	} scan __aligned(IIO_DMA_MINALIGN);
 };
 
 static const struct iio_mount_matrix *
@@ -611,14 +610,14 @@ static int kx022a_get_axis(struct kx022a_data *data,
 			   struct iio_chan_spec const *chan,
 			   int *val)
 {
+	__le16 *buf = &data->scan.channels[0];
 	int ret;
 
-	ret = regmap_bulk_read(data->regmap, chan->address, &data->buffer[0],
-			       sizeof(__le16));
+	ret = regmap_bulk_read(data->regmap, chan->address, buf, sizeof(*buf));
 	if (ret)
 		return ret;
 
-	*val = (s16)le16_to_cpu(data->buffer[0]);
+	*val = (s16)le16_to_cpup(buf);
 
 	return IIO_VAL_INT;
 }
@@ -1029,12 +1028,12 @@ static irqreturn_t kx022a_trigger_handler(int irq, void *p)
 	struct kx022a_data *data = iio_priv(idev);
 	int ret;
 
-	ret = regmap_bulk_read(data->regmap, data->chip_info->xout_l, data->buffer,
-			       KX022A_FIFO_SAMPLES_SIZE_BYTES);
+	ret = regmap_bulk_read(data->regmap, data->chip_info->xout_l,
+			       data->scan.channels, KX022A_FIFO_SAMPLES_SIZE_BYTES);
 	if (ret < 0)
 		goto err_read;
 
-	iio_push_to_buffers_with_timestamp(idev, data->buffer, data->timestamp);
+	iio_push_to_buffers_with_timestamp(idev, &data->scan, data->timestamp);
 err_read:
 	iio_trigger_notify_done(idev->trig);
 
Signature · 1 line
2.50.1 (Apple Git-155)
#2 lore raw

PATCH v3 2/2 iio: accel: kionix-kx022a: use iio_push_to_buffers_with_ts()

In reply to Gabriel Rondon

Replace the deprecated iio_push_to_buffers_with_timestamp() with iio_push_to_buffers_with_ts(), which takes and checks the buffer size. Both sites push data->scan, so pass its size.

---
 drivers/iio/accel/kionix-kx022a.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/iio/accel/kionix-kx022a.c +4 -2
index 93104fdc74b5..06ae7e540ee6 100644
@@ -863,7 +863,8 @@ static int __kx022a_fifo_flush(struct iio_dev *idev, unsigned int samples,
 		for_each_set_bit(bit, idev->active_scan_mask, AXIS_MAX)
 			chs[bit] = sam[bit];
 
-		iio_push_to_buffers_with_timestamp(idev, &data->scan, tstamp);
+		iio_push_to_buffers_with_ts(idev, &data->scan,
+					    sizeof(data->scan), tstamp);
 
 		tstamp += sample_period;
 	}
@@ -1033,7 +1034,8 @@ static irqreturn_t kx022a_trigger_handler(int irq, void *p)
 	if (ret < 0)
 		goto err_read;
 
-	iio_push_to_buffers_with_timestamp(idev, &data->scan, data->timestamp);
+	iio_push_to_buffers_with_ts(idev, &data->scan, sizeof(data->scan),
+				    data->timestamp);
 err_read:
 	iio_trigger_notify_done(idev->trig);
 
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#3 lore raw
In reply to Gabriel Rondon
Quoted 5 lines · On Wed, 19 Aug 2026 11:37:44 +0100 Gabriel Rondon <[email protected]> wrote:

This is the kx022a staging-buffer unification plus the deprecated-API conversion. Patch 1 drops the redundant buffer and routes the one-shot read and the triggered handler through scan; patch 2 does the iio_push_to_buffers_with_ts() conversion.

Ah. I didn't see you'd posted a new version with the changes requested post my having picked this up (to fast as it turned out!)

If you are going to do this - which is fine - then reply to original thread to call that out. I think we ended up pretty much with the same thing upstream though as I went ahead and tweaked it in my tree. Mostly once a patch has been accepted, it's fixes on top. Things are a little more flexible when I haven't yet pushed it out as a non rebasing (ish) branch like iio.git/togreg

Give me a shout if I messed anything up when tweaking it.

Jonathan

Quoted 15 lines

Changes in v3:
- get_axis(): use a local __le16 *buf and le16_to_cpup() (Andy)
- Add Suggested-by: Jonathan Cameron to patch 1
- Trim patch 2 changelog

v2: https://lore.kernel.org/linux-iio/[email protected]/

Gabriel Rondon (2):
iio: accel: kionix-kx022a: use scan struct for one-shot and trigger
reads
iio: accel: kionix-kx022a: use iio_push_to_buffers_with_ts()

 drivers/iio/accel/kionix-kx022a.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)