[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250422-iio-pressure-bmp280-rework-push-to-buffers-v1-1-ee722f29aeca@baylibre.com>
Date: Tue, 22 Apr 2025 14:28:36 -0500
From: David Lechner <dlechner@...libre.com>
To: Jonathan Cameron <jic23@...nel.org>,
David Lechner <dlechner@...libre.com>,
Nuno Sá <nuno.sa@...log.com>,
Andy Shevchenko <andy@...nel.org>
Cc: linux-iio@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: [PATCH] iio: pressure: bmp280: drop sensor_data array
Drop the sensor_data array from struct bmp280_data and replace it using
local structs in each interrupt handler.
The sensor_data array in struct bmp280_data is not used to share data
between functions and isn't used for DMA, so there isn't really a need
to have it in the struct. Instead, we can use the struct pattern for
scan data in each interrupt handler. This has the advantage of allowing
us to see the actual layout of each scan buffer for each different type
of supported sensor. It also avoid juggling values between local
variables and the array which makes the code a bit simpler by avoiding
some extra assignments.
We can also drop the BME280_NUM_MAX_CHANNELS macro as it is no longer
used.
Suggested-by: Jonathan Cameron <jic23@...nel.org>
Link: https://lore.kernel.org/linux-iio/20250421135540.1a667221@jic23-huawei/
Signed-off-by: David Lechner <dlechner@...libre.com>
---
This is an alternative to [1] that takes a different approach to avoid
the messy one-size-fits-all buffer used with iio_push_to_buffers_with_ts().
[1]: https://lore.kernel.org/linux-iio/20250418-iio-introduce-iio_declare_buffer_with_ts-v1-4-ee0c62a33a0f@baylibre.com/
---
drivers/iio/pressure/bmp280-core.c | 102 +++++++++++++++++++------------------
drivers/iio/pressure/bmp280.h | 8 ---
2 files changed, 52 insertions(+), 58 deletions(-)
diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c
index c20cc4a98c9c494a9c8843518ba2f17b41be18a9..5728cc18cced223284a2c41dc6dec6f47169c797 100644
--- a/drivers/iio/pressure/bmp280-core.c
+++ b/drivers/iio/pressure/bmp280-core.c
@@ -46,6 +46,7 @@
#include <linux/random.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
+#include <linux/types.h>
#include <linux/iio/buffer.h>
#include <linux/iio/iio.h>
@@ -1105,9 +1106,13 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p)
struct iio_poll_func *pf = p;
struct iio_dev *indio_dev = pf->indio_dev;
struct bmp280_data *data = iio_priv(indio_dev);
- u32 adc_temp, adc_press, comp_press;
- s32 t_fine, comp_temp;
- s32 *chans = (s32 *)data->sensor_data;
+ u32 adc_temp, adc_press;
+ s32 t_fine;
+ struct {
+ u32 comp_press;
+ s32 comp_temp;
+ aligned_s64 timestamp;
+ } buffer;
int ret;
guard(mutex)(&data->lock);
@@ -1127,7 +1132,7 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p)
goto out;
}
- comp_temp = bmp280_compensate_temp(data, adc_temp);
+ buffer.comp_temp = bmp280_compensate_temp(data, adc_temp);
/* Pressure calculations */
adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[0]));
@@ -1137,13 +1142,9 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p)
}
t_fine = bmp280_calc_t_fine(data, adc_temp);
- comp_press = bmp280_compensate_press(data, adc_press, t_fine);
-
- chans[0] = comp_press;
- chans[1] = comp_temp;
+ buffer.comp_press = bmp280_compensate_press(data, adc_press, t_fine);
- iio_push_to_buffers_with_ts(indio_dev, data->sensor_data,
- sizeof(data->sensor_data),
+ iio_push_to_buffers_with_ts(indio_dev, &buffer, sizeof(buffer),
iio_get_time_ns(indio_dev));
out:
@@ -1226,9 +1227,14 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p)
struct iio_poll_func *pf = p;
struct iio_dev *indio_dev = pf->indio_dev;
struct bmp280_data *data = iio_priv(indio_dev);
- u32 adc_temp, adc_press, adc_humidity, comp_press, comp_humidity;
- s32 t_fine, comp_temp;
- s32 *chans = (s32 *)data->sensor_data;
+ u32 adc_temp, adc_press, adc_humidity;
+ s32 t_fine;
+ struct {
+ u32 comp_press;
+ s32 comp_temp;
+ u32 comp_humidity;
+ aligned_s64 timestamp;
+ } buffer;
int ret;
guard(mutex)(&data->lock);
@@ -1248,7 +1254,7 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p)
goto out;
}
- comp_temp = bmp280_compensate_temp(data, adc_temp);
+ buffer.comp_temp = bmp280_compensate_temp(data, adc_temp);
/* Pressure calculations */
adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[0]));
@@ -1258,7 +1264,7 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p)
}
t_fine = bmp280_calc_t_fine(data, adc_temp);
- comp_press = bmp280_compensate_press(data, adc_press, t_fine);
+ buffer.comp_press = bmp280_compensate_press(data, adc_press, t_fine);
/* Humidity calculations */
adc_humidity = get_unaligned_be16(&data->buf[6]);
@@ -1268,14 +1274,10 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p)
goto out;
}
- comp_humidity = bme280_compensate_humidity(data, adc_humidity, t_fine);
+ buffer.comp_humidity = bme280_compensate_humidity(data, adc_humidity,
+ t_fine);
- chans[0] = comp_press;
- chans[1] = comp_temp;
- chans[2] = comp_humidity;
-
- iio_push_to_buffers_with_ts(indio_dev, data->sensor_data,
- sizeof(data->sensor_data),
+ iio_push_to_buffers_with_ts(indio_dev, &buffer, sizeof(buffer),
iio_get_time_ns(indio_dev));
out:
@@ -1901,9 +1903,13 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p)
struct iio_poll_func *pf = p;
struct iio_dev *indio_dev = pf->indio_dev;
struct bmp280_data *data = iio_priv(indio_dev);
- u32 adc_temp, adc_press, comp_press;
- s32 t_fine, comp_temp;
- s32 *chans = (s32 *)data->sensor_data;
+ u32 adc_temp, adc_press;
+ s32 t_fine;
+ struct {
+ u32 comp_press;
+ s32 comp_temp;
+ aligned_s64 timestamp;
+ } buffer;
int ret;
guard(mutex)(&data->lock);
@@ -1923,7 +1929,7 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p)
goto out;
}
- comp_temp = bmp380_compensate_temp(data, adc_temp);
+ buffer.comp_temp = bmp380_compensate_temp(data, adc_temp);
/* Pressure calculations */
adc_press = get_unaligned_le24(&data->buf[0]);
@@ -1933,13 +1939,9 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p)
}
t_fine = bmp380_calc_t_fine(data, adc_temp);
- comp_press = bmp380_compensate_press(data, adc_press, t_fine);
-
- chans[0] = comp_press;
- chans[1] = comp_temp;
+ buffer.comp_press = bmp380_compensate_press(data, adc_press, t_fine);
- iio_push_to_buffers_with_ts(indio_dev, data->sensor_data,
- sizeof(data->sensor_data),
+ iio_push_to_buffers_with_ts(indio_dev, &buffer, sizeof(buffer),
iio_get_time_ns(indio_dev));
out:
@@ -2611,7 +2613,12 @@ static irqreturn_t bmp580_trigger_handler(int irq, void *p)
struct iio_poll_func *pf = p;
struct iio_dev *indio_dev = pf->indio_dev;
struct bmp280_data *data = iio_priv(indio_dev);
- int ret, offset;
+ struct {
+ __le32 comp_temp;
+ __le32 comp_press;
+ aligned_s64 timestamp;
+ } buffer;
+ int ret;
guard(mutex)(&data->lock);
@@ -2623,18 +2630,13 @@ static irqreturn_t bmp580_trigger_handler(int irq, void *p)
goto out;
}
- offset = 0;
-
/* Pressure calculations */
- memcpy(&data->sensor_data[offset], &data->buf[3], 3);
-
- offset += sizeof(s32);
+ memcpy(&buffer.comp_press, &data->buf[3], 3);
/* Temperature calculations */
- memcpy(&data->sensor_data[offset], &data->buf[0], 3);
+ memcpy(&buffer.comp_temp, &data->buf[0], 3);
- iio_push_to_buffers_with_ts(indio_dev, data->sensor_data,
- sizeof(data->sensor_data),
+ iio_push_to_buffers_with_ts(indio_dev, &buffer, sizeof(buffer),
iio_get_time_ns(indio_dev));
out:
@@ -2956,25 +2958,25 @@ static irqreturn_t bmp180_trigger_handler(int irq, void *p)
struct iio_poll_func *pf = p;
struct iio_dev *indio_dev = pf->indio_dev;
struct bmp280_data *data = iio_priv(indio_dev);
- int ret, comp_temp, comp_press;
- s32 *chans = (s32 *)data->sensor_data;
+ struct {
+ u32 comp_press;
+ s32 comp_temp;
+ aligned_s64 timestamp;
+ } buffer;
+ int ret;
guard(mutex)(&data->lock);
- ret = bmp180_read_temp(data, &comp_temp);
+ ret = bmp180_read_temp(data, &buffer.comp_temp);
if (ret)
goto out;
- ret = bmp180_read_press(data, &comp_press);
+ ret = bmp180_read_press(data, &buffer.comp_press);
if (ret)
goto out;
- chans[0] = comp_press;
- chans[1] = comp_temp;
-
- iio_push_to_buffers_with_ts(indio_dev, data->sensor_data,
- sizeof(data->sensor_data),
+ iio_push_to_buffers_with_ts(indio_dev, &buffer, sizeof(buffer),
iio_get_time_ns(indio_dev));
out:
diff --git a/drivers/iio/pressure/bmp280.h b/drivers/iio/pressure/bmp280.h
index 5b2ee1d0ee464797d1d9993a014d8f84c37d5596..25bb9c743a05a19c5d2396969f0f4e0fef6ddcd0 100644
--- a/drivers/iio/pressure/bmp280.h
+++ b/drivers/iio/pressure/bmp280.h
@@ -349,7 +349,6 @@
BMP280_NUM_TEMP_BYTES + \
BME280_NUM_HUMIDITY_BYTES)
-#define BME280_NUM_MAX_CHANNELS 3
/* Core exported structs */
static const char *const bmp280_supply_names[] = {
@@ -452,13 +451,6 @@ struct bmp280_data {
*/
int sampling_freq;
- /*
- * Data to push to userspace triggered buffer. Up to 3 channels and
- * s64 timestamp, aligned.
- */
- u8 sensor_data[ALIGN(sizeof(s32) * BME280_NUM_MAX_CHANNELS, sizeof(s64))
- + sizeof(s64)] __aligned(sizeof(s64));
-
/* Value to hold the current operation mode of the device */
enum bmp280_op_mode op_mode;
---
base-commit: b475195fecc79a1a6e7fb0846aaaab0a1a4cb2e6
change-id: 20250422-iio-pressure-bmp280-rework-push-to-buffers-078da0e9973e
Best regards,
--
David Lechner <dlechner@...libre.com>
Powered by blists - more mailing lists