Skip to contentSkip to conversation

iio: accel: bmc150: DMA-safe buffers and use iio_push_to_buffers_with_ts()

PATCH Superseded Yash Suthar 4 participants 12 messages Aug 15 – Aug 24 IIO

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover iio: accel: bmc150: DMA-safe buffers and use iio_push_to_buffers_with_ts()

The trigger path used a plain s16 buffer while the FIFO path already had a properly typed scan member (__le16 + aligned timestamp). Those two buffers did the same job, and s16 does not represent little-endian data. Drop the duplicate, keep scan at the end of struct bmc150_accel_data with IIO_DMA_MINALIGN, and use it for both the trigger and FIFO paths.

The FIFO and read_raw buffers are passed to regmap bulk/raw reads, which is not DMA-safe. Move them into struct bmc150_accel_data after scan and aligned to IIO_DMA_MINALIGN.

Finally, replace the deprecated iio_push_to_buffers_with_timestamp() with iio_push_to_buffers_with_ts() so the push sites pass an explicit buffer size.

This supersedes the earlier standalone patches:

Link to v1: https://lore.kernel.org/all/[email protected]/

Changes in v2:
- patch 1: restore early error check flow
- patch 2: keep fifo_buff declaration in one line

Yash Suthar (3):
iio: accel: bmc150: use aligned scan buffer for both trigger and fifo
iio: accel: bmc150: use DMA-safe buffers for regmap bulk reads
iio: accel: bmc150: use iio_push_to_buffers_with_ts()

 drivers/iio/accel/bmc150-accel-core.c | 24 +++++++++++-------------
 drivers/iio/accel/bmc150-accel.h      | 19 ++++++++++---------
 2 files changed, 21 insertions(+), 22 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/3 iio: accel: bmc150: use aligned scan buffer for both trigger and fifo

Drop buffer as duplicate and s16 is not correct we needed __le16 as little endian, keep scan at the end of the bmc150_accel_data struct with IIO_DMA_MINALIGN, and use it for both paths.

Fixes bd7fe5b71918 ("iio: accel: BMC150 accel support")
---
 drivers/iio/accel/bmc150-accel-core.c |  4 ++--
 drivers/iio/accel/bmc150-accel.h      | 14 +++++---------
 2 files changed, 7 insertions(+), 11 deletions(-)
drivers/iio/accel/bmc150-accel-core.c +2 -2
index dc8a6285cf3d..89a475ef9a9b 100644
@@ -1191,12 +1191,12 @@ static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p)
 
 	mutex_lock(&data->mutex);
 	ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_REG_XOUT_L,
-			       data->buffer, AXIS_MAX * 2);
+			       data->scan.channels, AXIS_MAX * 2);
 	mutex_unlock(&data->mutex);
 	if (ret < 0)
 		goto err_read;
 
-	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
+	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
 					   pf->timestamp);
 err_read:
 	iio_trigger_notify_done(indio_dev->trig);
drivers/iio/accel/bmc150-accel.h +5 -9
index e8f26198359f..9deff256aed5 100644
@@ -64,15 +64,6 @@ struct bmc150_accel_data {
 	struct bmc150_accel_trigger triggers[BMC150_ACCEL_TRIGGERS];
 	struct mutex mutex;
 	u8 fifo_mode, watermark;
-	s16 buffer[8];
-	/*
-	 * Ensure there is sufficient space and correct alignment for
-	 * the timestamp if enabled
-	 */
-	struct {
-		__le16 channels[3];
-		aligned_s64 ts;
-	} scan;
 	u8 bw_bits;
 	u32 slope_dur;
 	u32 slope_thres;
@@ -85,6 +76,11 @@ struct bmc150_accel_data {
 	void (*resume_callback)(struct device *dev);
 	struct delayed_work resume_work;
 	struct iio_mount_matrix orientation;
+	/* Ensure correct alignment of timestamp and DMA safety */
+	struct {
+		__le16 channels[3];
+		aligned_s64 ts;
+	} scan __aligned(IIO_DMA_MINALIGN);
 };
 
 int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
Signature · 1 line
2.43.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] iio: accel: bmc150: use aligned scan buffer for both trigger and fifo

Quoted 7 lines · Yash Suthar

Drop buffer as duplicate and s16 is not correct we needed __le16 as little endian, keep scan at the end of the bmc150_accel_data struct with IIO_DMA_MINALIGN, and use it for both paths.

Fixes bd7fe5b71918 ("iio: accel: BMC150 accel support")

Yours,
Linus Walleij

maintainer
#3 lore raw

Re: [PATCH v2 1/3] iio: accel: bmc150: use aligned scan buffer for both trigger and fifo

In reply to Yash Suthar
Quoted 17 lines · On Sat, 15 Aug 2026 23:27:26 +0530 Yash Suthar <[email protected]> wrote:

Drop buffer as duplicate and s16 is not correct we needed __le16 as little endian, keep scan at the end of the bmc150_accel_data struct with IIO_DMA_MINALIGN, and use it for both paths.

Fixes bd7fe5b71918 ("iio: accel: BMC150 accel support")
---
 drivers/iio/accel/bmc150-accel-core.c |  4 ++--
 drivers/iio/accel/bmc150-accel.h      | 14 +++++---------
 2 files changed, 7 insertions(+), 11 deletions(-)
drivers/iio/accel/bmc150-accel-core.c +0 -0
index dc8a6285cf3d..89a475ef9a9b 100644

drivers/iio/accel/bmc150-accel-core.c static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p) · line 1194

Quoted 5 lines
drivers/iio/accel/bmc150-accel-core.c +1 -1
@@ -1191,12 +1191,12 @@ static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p)
 
 	mutex_lock(&data->mutex);
 	ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_REG_XOUT_L,
-			       data->buffer, AXIS_MAX * 2);
+			       data->scan.channels, AXIS_MAX * 2);

Even nicer if you replace AXIS_MAX * 2 with sizeof(data->scan.channels) and also drop the AXIS_MAX definition as no longer used.

Quoted 41 lines
drivers/iio/accel/bmc150-accel-core.c +1 -1
 	mutex_unlock(&data->mutex);
 	if (ret < 0)
 		goto err_read;
 
-	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
+	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
 					   pf->timestamp);
 err_read:
 	iio_trigger_notify_done(indio_dev->trig);
drivers/iio/accel/bmc150-accel.h +5 -9
index e8f26198359f..9deff256aed5 100644
@@ -64,15 +64,6 @@ struct bmc150_accel_data {
 	struct bmc150_accel_trigger triggers[BMC150_ACCEL_TRIGGERS];
 	struct mutex mutex;
 	u8 fifo_mode, watermark;
-	s16 buffer[8];
-	/*
-	 * Ensure there is sufficient space and correct alignment for
-	 * the timestamp if enabled
-	 */
-	struct {
-		__le16 channels[3];
-		aligned_s64 ts;
-	} scan;
 	u8 bw_bits;
 	u32 slope_dur;
 	u32 slope_thres;
@@ -85,6 +76,11 @@ struct bmc150_accel_data {
 	void (*resume_callback)(struct device *dev);
 	struct delayed_work resume_work;
 	struct iio_mount_matrix orientation;
+	/* Ensure correct alignment of timestamp and DMA safety */
+	struct {
+		__le16 channels[3];
+		aligned_s64 ts;
+	} scan __aligned(IIO_DMA_MINALIGN);
 };
 
 int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
maintainer
#4 lore raw

Re: [PATCH v2 1/3] iio: accel: bmc150: use aligned scan buffer for both trigger and fifo

In reply to Yash Suthar
Quoted 17 lines · On Sat, 15 Aug 2026 23:27:26 +0530 Yash Suthar <[email protected]> wrote:

Drop buffer as duplicate and s16 is not correct we needed __le16 as little endian, keep scan at the end of the bmc150_accel_data struct with IIO_DMA_MINALIGN, and use it for both paths.

Fixes bd7fe5b71918 ("iio: accel: BMC150 accel support")
---
 drivers/iio/accel/bmc150-accel-core.c |  4 ++--
 drivers/iio/accel/bmc150-accel.h      | 14 +++++---------
 2 files changed, 7 insertions(+), 11 deletions(-)
drivers/iio/accel/bmc150-accel-core.c +0 -0
index dc8a6285cf3d..89a475ef9a9b 100644

drivers/iio/accel/bmc150-accel-core.c static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p) · line 1192

Quoted 2 lines
drivers/iio/accel/bmc150-accel-core.c +0 -0
@@ -1191,12 +1191,12 @@ static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p)
 
 	mutex_lock(&data->mutex);

Relevant to the next patch - what is this lock actually protecting? At first glance I'd assume the buffer, but then we drop the lock before we are done with that.

drivers/iio/accel/bmc150-accel-core.c static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p) · line 1199

Quoted 9 lines
drivers/iio/accel/bmc150-accel-core.c +2 -2
 	ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_REG_XOUT_L,
-			       data->buffer, AXIS_MAX * 2);
+			       data->scan.channels, AXIS_MAX * 2);
 	mutex_unlock(&data->mutex);
 	if (ret < 0)
 		goto err_read;
 
-	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
+	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,

This is still using scan, but the lock has been released.

Quoted 35 lines
drivers/iio/accel/bmc150-accel-core.c +0 -0
 					   pf->timestamp);
 err_read:
 	iio_trigger_notify_done(indio_dev->trig);
drivers/iio/accel/bmc150-accel.h +5 -9
index e8f26198359f..9deff256aed5 100644
@@ -64,15 +64,6 @@ struct bmc150_accel_data {
 	struct bmc150_accel_trigger triggers[BMC150_ACCEL_TRIGGERS];
 	struct mutex mutex;
 	u8 fifo_mode, watermark;
-	s16 buffer[8];
-	/*
-	 * Ensure there is sufficient space and correct alignment for
-	 * the timestamp if enabled
-	 */
-	struct {
-		__le16 channels[3];
-		aligned_s64 ts;
-	} scan;
 	u8 bw_bits;
 	u32 slope_dur;
 	u32 slope_thres;
@@ -85,6 +76,11 @@ struct bmc150_accel_data {
 	void (*resume_callback)(struct device *dev);
 	struct delayed_work resume_work;
 	struct iio_mount_matrix orientation;
+	/* Ensure correct alignment of timestamp and DMA safety */
+	struct {
+		__le16 channels[3];
+		aligned_s64 ts;
+	} scan __aligned(IIO_DMA_MINALIGN);
 };
 
 int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
#5 lore raw

PATCH v2 2/3 iio: accel: bmc150: use DMA-safe buffers for regmap bulk reads

In reply to Yash Suthar

The FIFO and read_raw buffers are passed to regmap bulk/raw reads ,which is not DMA-safe.Moved them into struct bmc150_accel_data after scan, each aligned to IIO_DMA_MINALIGN.

---
 drivers/iio/accel/bmc150-accel-core.c | 14 ++++++--------
 drivers/iio/accel/bmc150-accel.h      |  5 +++++
 2 files changed, 11 insertions(+), 8 deletions(-)
drivers/iio/accel/bmc150-accel-core.c +6 -8
index 89a475ef9a9b..e8d27fd1be3f 100644
@@ -125,7 +125,6 @@
 #define BMC150_ACCEL_REG_FIFO_CONFIG0		0x30
 #define BMC150_ACCEL_REG_FIFO_CONFIG1		0x3E
 #define BMC150_ACCEL_REG_FIFO_DATA		0x3F
-#define BMC150_ACCEL_FIFO_LENGTH		32
 
 enum bmc150_accel_axis {
 	AXIS_X,
@@ -622,7 +621,6 @@ static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
 	struct device *dev = regmap_get_device(data->regmap);
 	int ret;
 	int axis = chan->scan_index;
-	__le16 raw_val;
 
 	mutex_lock(&data->mutex);
 	ret = bmc150_accel_set_power_state(data, true);
@@ -632,14 +630,14 @@ static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
 	}
 
 	ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis),
-			       &raw_val, sizeof(raw_val));
+			       &data->regval, sizeof(data->regval));
 	if (ret < 0) {
 		dev_err(dev, "Error reading axis %d\n", axis);
 		bmc150_accel_set_power_state(data, false);
 		mutex_unlock(&data->mutex);
 		return ret;
 	}
-	*val = sign_extend32(le16_to_cpu(raw_val) >> chan->scan_type.shift,
+	*val = sign_extend32(le16_to_cpu(data->regval) >> chan->scan_type.shift,
 			     chan->scan_type.realbits - 1);
 	ret = bmc150_accel_set_power_state(data, false);
 	mutex_unlock(&data->mutex);
@@ -918,7 +916,7 @@ static int bmc150_accel_set_watermark(struct iio_dev *indio_dev, unsigned val)
  * frame data is discarded.
  */
 static int bmc150_accel_fifo_transfer(struct bmc150_accel_data *data,
-				      char *buffer, int samples)
+				      void *buffer, int samples)
 {
 	struct device *dev = regmap_get_device(data->regmap);
 	int sample_length = 3 * 2;
@@ -941,7 +939,6 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
 	struct device *dev = regmap_get_device(data->regmap);
 	int ret, i;
 	u8 count;
-	u16 buffer[BMC150_ACCEL_FIFO_LENGTH * 3];
 	int64_t tstamp;
 	uint64_t sample_period;
 	unsigned int val;
@@ -993,7 +990,7 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
 
 	count = min_t(u8, count, BMC150_ACCEL_FIFO_LENGTH);
 
-	ret = bmc150_accel_fifo_transfer(data, (u8 *)buffer, count);
+	ret = bmc150_accel_fifo_transfer(data, data->fifo_buff, count);
 	if (ret)
 		return ret;
 
@@ -1008,7 +1005,8 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
 
 		j = 0;
 		iio_for_each_active_channel(indio_dev, bit)
-			memcpy(&data->scan.channels[j++], &buffer[i * 3 + bit],
+			memcpy(&data->scan.channels[j++],
+			       &data->fifo_buff[i * 3 + bit],
 			       sizeof(data->scan.channels[0]));
 
 		iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
drivers/iio/accel/bmc150-accel.h +5 -0
index 9deff256aed5..8f64b24339e4 100644
@@ -56,6 +56,8 @@ enum bmc150_accel_trigger_id {
 	BMC150_ACCEL_TRIGGERS,
 };
 
+#define BMC150_ACCEL_FIFO_LENGTH		32
+
 struct bmc150_accel_data {
 	struct regmap *regmap;
 	int irq;
@@ -81,6 +83,9 @@ struct bmc150_accel_data {
 		__le16 channels[3];
 		aligned_s64 ts;
 	} scan __aligned(IIO_DMA_MINALIGN);
+	/* DMA-safe buffers for bulk/raw reads */
+	__le16 fifo_buff[BMC150_ACCEL_FIFO_LENGTH * 3] __aligned(IIO_DMA_MINALIGN);
+	__le16 regval __aligned(IIO_DMA_MINALIGN);
 };
 
 int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
Signature · 1 line
2.43.0
Reviewed-by on v2 2/3
#6 lore raw

Re: [PATCH v2 2/3] iio: accel: bmc150: use DMA-safe buffers for regmap bulk reads

Quoted 6 lines · Yash Suthar

The FIFO and read_raw buffers are passed to regmap bulk/raw reads ,which is not DMA-safe.Moved them into struct bmc150_accel_data after scan, each aligned to IIO_DMA_MINALIGN.

Yours,
Linus Walleij

maintainer
#7 lore raw

Re: [PATCH v2 2/3] iio: accel: bmc150: use DMA-safe buffers for regmap bulk reads

In reply to Yash Suthar
Quoted 2 lines · On Sat, 15 Aug 2026 23:27:27 +0530 Yash Suthar <[email protected]> wrote:

The FIFO and read_raw buffers are passed to regmap bulk/raw reads ,which is not DMA-safe.Moved them into struct bmc150_accel_data

Move the comma up a line.

Quoted 4 lines

after scan, each aligned to IIO_DMA_MINALIGN.

My main question here is around locking and whether we need to space all 3 buffers out in a cache line each. That is painful on some architectures. If we ensure that DMA to one of these never overlaps with the CPU accessing a different one, then we can just mark the first one as IIO_DMA_MINALIGN.

Jonathan

Quoted 92 lines
---
 drivers/iio/accel/bmc150-accel-core.c | 14 ++++++--------
 drivers/iio/accel/bmc150-accel.h      |  5 +++++
 2 files changed, 11 insertions(+), 8 deletions(-)
drivers/iio/accel/bmc150-accel-core.c +6 -8
index 89a475ef9a9b..e8d27fd1be3f 100644
@@ -125,7 +125,6 @@
 #define BMC150_ACCEL_REG_FIFO_CONFIG0		0x30
 #define BMC150_ACCEL_REG_FIFO_CONFIG1		0x3E
 #define BMC150_ACCEL_REG_FIFO_DATA		0x3F
-#define BMC150_ACCEL_FIFO_LENGTH		32
 
 enum bmc150_accel_axis {
 	AXIS_X,
@@ -622,7 +621,6 @@ static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
 	struct device *dev = regmap_get_device(data->regmap);
 	int ret;
 	int axis = chan->scan_index;
-	__le16 raw_val;
 
 	mutex_lock(&data->mutex);
 	ret = bmc150_accel_set_power_state(data, true);
@@ -632,14 +630,14 @@ static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
 	}
 
 	ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis),
-			       &raw_val, sizeof(raw_val));
+			       &data->regval, sizeof(data->regval));
 	if (ret < 0) {
 		dev_err(dev, "Error reading axis %d\n", axis);
 		bmc150_accel_set_power_state(data, false);
 		mutex_unlock(&data->mutex);
 		return ret;
 	}
-	*val = sign_extend32(le16_to_cpu(raw_val) >> chan->scan_type.shift,
+	*val = sign_extend32(le16_to_cpu(data->regval) >> chan->scan_type.shift,
 			     chan->scan_type.realbits - 1);
 	ret = bmc150_accel_set_power_state(data, false);
 	mutex_unlock(&data->mutex);
@@ -918,7 +916,7 @@ static int bmc150_accel_set_watermark(struct iio_dev *indio_dev, unsigned val)
  * frame data is discarded.
  */
 static int bmc150_accel_fifo_transfer(struct bmc150_accel_data *data,
-				      char *buffer, int samples)
+				      void *buffer, int samples)
 {
 	struct device *dev = regmap_get_device(data->regmap);
 	int sample_length = 3 * 2;
@@ -941,7 +939,6 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
 	struct device *dev = regmap_get_device(data->regmap);
 	int ret, i;
 	u8 count;
-	u16 buffer[BMC150_ACCEL_FIFO_LENGTH * 3];
 	int64_t tstamp;
 	uint64_t sample_period;
 	unsigned int val;
@@ -993,7 +990,7 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
 
 	count = min_t(u8, count, BMC150_ACCEL_FIFO_LENGTH);
 
-	ret = bmc150_accel_fifo_transfer(data, (u8 *)buffer, count);
+	ret = bmc150_accel_fifo_transfer(data, data->fifo_buff, count);
 	if (ret)
 		return ret;
 
@@ -1008,7 +1005,8 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
 
 		j = 0;
 		iio_for_each_active_channel(indio_dev, bit)
-			memcpy(&data->scan.channels[j++], &buffer[i * 3 + bit],
+			memcpy(&data->scan.channels[j++],
+			       &data->fifo_buff[i * 3 + bit],
 			       sizeof(data->scan.channels[0]));
 
 		iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
drivers/iio/accel/bmc150-accel.h +2 -0
index 9deff256aed5..8f64b24339e4 100644
@@ -56,6 +56,8 @@ enum bmc150_accel_trigger_id {
 	BMC150_ACCEL_TRIGGERS,
 };
 
+#define BMC150_ACCEL_FIFO_LENGTH		32
+
 struct bmc150_accel_data {
 	struct regmap *regmap;
 	int irq;

drivers/iio/accel/bmc150-accel.h struct bmc150_accel_data { · line 88

Quoted 6 lines
drivers/iio/accel/bmc150-accel.h +3 -0
@@ -81,6 +83,9 @@ struct bmc150_accel_data {
 		__le16 channels[3];
 		aligned_s64 ts;
 	} scan __aligned(IIO_DMA_MINALIGN);
+	/* DMA-safe buffers for bulk/raw reads */
+	__le16 fifo_buff[BMC150_ACCEL_FIFO_LENGTH * 3] __aligned(IIO_DMA_MINALIGN);
+	__le16 regval __aligned(IIO_DMA_MINALIGN);

This is potentially a lot of padding that might not be necessary. What locking protects these? I'd kind of expect all 3 to be used under a single lock. If that's the case and locks are held for all such usage, then we can just force alignment of the first one.

Quoted 3 lines
drivers/iio/accel/bmc150-accel.h +0 -0
 };
 
 int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
#8 lore raw

Re: [PATCH v2 2/3] iio: accel: bmc150: use DMA-safe buffers for regmap bulk reads

Hi Jonathan
thanks for review
i agreed with you, earlier i did same as you mentioned see:
https://lore.kernel.org/all/[email protected]/
but after review from Andy Shevchenko:
https://lore.kernel.org/all/[email protected]/#t
I agreed and revert the flow for v2, but in doing so I moved mutex_unlock() before
push.

In v3 will hold mutex for both read and push, single IIO_DMA_MINALIGN on scan is sufficient, so fifo_buff and regval no longer need separate __aligned(IIO_DMA_MINALIGN)
Will also replace AXIS_MAX*2 with sizeof(data->scan.channels) and drop the unused AXIS_MAX.

Sincerely,
Yash Suthar

Quoted 131 lines · Jonathan Cameron
Quoted 2 lines · On Sat, 15 Aug 2026 23:27:27 +0530 Yash Suthar <[email protected]> wrote:

The FIFO and read_raw buffers are passed to regmap bulk/raw reads ,which is not DMA-safe.Moved them into struct bmc150_accel_data

Move the comma up a line.

Quoted 4 lines

after scan, each aligned to IIO_DMA_MINALIGN.

My main question here is around locking and whether we need to space all 3 buffers out in a cache line each. That is painful on some architectures. If we ensure that DMA to one of these never overlaps with the CPU accessing a different one, then we can just mark the first one as IIO_DMA_MINALIGN.

Jonathan

Quoted 92 lines
---
 drivers/iio/accel/bmc150-accel-core.c | 14 ++++++--------
 drivers/iio/accel/bmc150-accel.h      |  5 +++++
 2 files changed, 11 insertions(+), 8 deletions(-)
drivers/iio/accel/bmc150-accel-core.c +6 -8
index 89a475ef9a9b..e8d27fd1be3f 100644
@@ -125,7 +125,6 @@
 #define BMC150_ACCEL_REG_FIFO_CONFIG0                0x30
 #define BMC150_ACCEL_REG_FIFO_CONFIG1                0x3E
 #define BMC150_ACCEL_REG_FIFO_DATA           0x3F
-#define BMC150_ACCEL_FIFO_LENGTH             32
 
 enum bmc150_accel_axis {
      AXIS_X,
@@ -622,7 +621,6 @@ static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
      struct device *dev = regmap_get_device(data->regmap);
      int ret;
      int axis = chan->scan_index;
-     __le16 raw_val;
 
      mutex_lock(&data->mutex);
      ret = bmc150_accel_set_power_state(data, true);
@@ -632,14 +630,14 @@ static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
      }
 
      ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis),
-                            &raw_val, sizeof(raw_val));
+                            &data->regval, sizeof(data->regval));
      if (ret < 0) {
              dev_err(dev, "Error reading axis %d\n", axis);
              bmc150_accel_set_power_state(data, false);
              mutex_unlock(&data->mutex);
              return ret;
      }
-     *val = sign_extend32(le16_to_cpu(raw_val) >> chan->scan_type.shift,
+     *val = sign_extend32(le16_to_cpu(data->regval) >> chan->scan_type.shift,
                           chan->scan_type.realbits - 1);
      ret = bmc150_accel_set_power_state(data, false);
      mutex_unlock(&data->mutex);
@@ -918,7 +916,7 @@ static int bmc150_accel_set_watermark(struct iio_dev *indio_dev, unsigned val)
  * frame data is discarded.
  */
 static int bmc150_accel_fifo_transfer(struct bmc150_accel_data *data,
-                                   char *buffer, int samples)
+                                   void *buffer, int samples)
 {
      struct device *dev = regmap_get_device(data->regmap);
      int sample_length = 3 * 2;
@@ -941,7 +939,6 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
      struct device *dev = regmap_get_device(data->regmap);
      int ret, i;
      u8 count;
-     u16 buffer[BMC150_ACCEL_FIFO_LENGTH * 3];
      int64_t tstamp;
      uint64_t sample_period;
      unsigned int val;
@@ -993,7 +990,7 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
 
      count = min_t(u8, count, BMC150_ACCEL_FIFO_LENGTH);
 
-     ret = bmc150_accel_fifo_transfer(data, (u8 *)buffer, count);
+     ret = bmc150_accel_fifo_transfer(data, data->fifo_buff, count);
      if (ret)
              return ret;
 
@@ -1008,7 +1005,8 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
 
              j = 0;
              iio_for_each_active_channel(indio_dev, bit)
-                     memcpy(&data->scan.channels[j++], &buffer[i * 3 + bit],
+                     memcpy(&data->scan.channels[j++],
+                            &data->fifo_buff[i * 3 + bit],
                             sizeof(data->scan.channels[0]));
 
              iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
drivers/iio/accel/bmc150-accel.h +2 -0
index 9deff256aed5..8f64b24339e4 100644
@@ -56,6 +56,8 @@ enum bmc150_accel_trigger_id {
      BMC150_ACCEL_TRIGGERS,
 };
 
+#define BMC150_ACCEL_FIFO_LENGTH             32
+
 struct bmc150_accel_data {
      struct regmap *regmap;
      int irq;

drivers/iio/accel/bmc150-accel.h struct bmc150_accel_data { · line 88

Quoted 6 lines
drivers/iio/accel/bmc150-accel.h +3 -0
@@ -81,6 +83,9 @@ struct bmc150_accel_data {
              __le16 channels[3];
              aligned_s64 ts;
      } scan __aligned(IIO_DMA_MINALIGN);
+     /* DMA-safe buffers for bulk/raw reads */
+     __le16 fifo_buff[BMC150_ACCEL_FIFO_LENGTH * 3] __aligned(IIO_DMA_MINALIGN);
+     __le16 regval __aligned(IIO_DMA_MINALIGN);

This is potentially a lot of padding that might not be necessary. What locking protects these? I'd kind of expect all 3 to be used under a single lock. If that's the case and locks are held for all such usage, then we can just force alignment of the first one.

Quoted 3 lines
drivers/iio/accel/bmc150-accel.h +0 -0
 };
 
 int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
#9 lore raw

PATCH v2 3/3 iio: accel: bmc150: use iio_push_to_buffers_with_ts()

In reply to Yash Suthar

Replace deprecated iio_push_to_buffers_with_timestamp() with iio_push_to_buffers_with_ts() to allow source size runtime checks.

---
 drivers/iio/accel/bmc150-accel-core.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/accel/bmc150-accel-core.c +4 -4
index e8d27fd1be3f..b6d67965b984 100644
@@ -1009,8 +1009,8 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
 			       &data->fifo_buff[i * 3 + bit],
 			       sizeof(data->scan.channels[0]));
 
-		iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
-						   tstamp);
+		iio_push_to_buffers_with_ts(indio_dev, &data->scan,
+					    sizeof(data->scan), tstamp);
 
 		tstamp += sample_period;
 	}
@@ -1194,8 +1194,8 @@ static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p)
 	if (ret < 0)
 		goto err_read;
 
-	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
-					   pf->timestamp);
+	iio_push_to_buffers_with_ts(indio_dev, &data->scan,
+				    sizeof(data->scan), pf->timestamp);
 err_read:
 	iio_trigger_notify_done(indio_dev->trig);
 
Signature · 1 line
2.43.0
#11 lore raw
In reply to Yash Suthar
Quoted 3 lines · Yash Suthar

The trigger path used a plain s16 buffer while the FIFO path already had a properly typed scan member (__le16 + aligned timestamp). Those two buffers did the same job, and s16 does not represent little-endian data.

Built the series on top of current iio/togreg with gcc 13.3, x86_64 allmodconfig and W=1: the three bmc150 objects (core, i2c, spi) compile cleanly with no new warnings.

I also used it as the base for v4 of my bmc150 series (event-enable race fix + guard(mutex) conversion), which is rebased on top of these patches:
https://lore.kernel.org/linux-iio/[email protected]/

Gabriel