Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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@kernel.org>
Link: https://lore.kernel.org/linux-iio/20250421135540.1a667221@jic23-huawei/
Signed-off-by: David Lechner <dlechner@baylibre.com>
Reviewed-by: Andy Shevchenko <andy@kernel.org>
Link: https://patch.msgid.link/20250422-iio-pressure-bmp280-rework-push-to-buffers-v1-1-ee722f29aeca@baylibre.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

David Lechner and committed by
Jonathan Cameron
872c8014 162129a2

+52 -58
+52 -50
drivers/iio/pressure/bmp280-core.c
··· 46 46 #include <linux/random.h> 47 47 #include <linux/regmap.h> 48 48 #include <linux/regulator/consumer.h> 49 + #include <linux/types.h> 49 50 50 51 #include <linux/iio/buffer.h> 51 52 #include <linux/iio/iio.h> ··· 1106 1105 struct iio_poll_func *pf = p; 1107 1106 struct iio_dev *indio_dev = pf->indio_dev; 1108 1107 struct bmp280_data *data = iio_priv(indio_dev); 1109 - u32 adc_temp, adc_press, comp_press; 1110 - s32 t_fine, comp_temp; 1111 - s32 *chans = (s32 *)data->sensor_data; 1108 + u32 adc_temp, adc_press; 1109 + s32 t_fine; 1110 + struct { 1111 + u32 comp_press; 1112 + s32 comp_temp; 1113 + aligned_s64 timestamp; 1114 + } buffer; 1112 1115 int ret; 1113 1116 1114 1117 guard(mutex)(&data->lock); ··· 1132 1127 goto out; 1133 1128 } 1134 1129 1135 - comp_temp = bmp280_compensate_temp(data, adc_temp); 1130 + buffer.comp_temp = bmp280_compensate_temp(data, adc_temp); 1136 1131 1137 1132 /* Pressure calculations */ 1138 1133 adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[0])); ··· 1142 1137 } 1143 1138 1144 1139 t_fine = bmp280_calc_t_fine(data, adc_temp); 1145 - comp_press = bmp280_compensate_press(data, adc_press, t_fine); 1140 + buffer.comp_press = bmp280_compensate_press(data, adc_press, t_fine); 1146 1141 1147 - chans[0] = comp_press; 1148 - chans[1] = comp_temp; 1149 - 1150 - iio_push_to_buffers_with_ts(indio_dev, data->sensor_data, 1151 - sizeof(data->sensor_data), 1142 + iio_push_to_buffers_with_ts(indio_dev, &buffer, sizeof(buffer), 1152 1143 iio_get_time_ns(indio_dev)); 1153 1144 1154 1145 out: ··· 1227 1226 struct iio_poll_func *pf = p; 1228 1227 struct iio_dev *indio_dev = pf->indio_dev; 1229 1228 struct bmp280_data *data = iio_priv(indio_dev); 1230 - u32 adc_temp, adc_press, adc_humidity, comp_press, comp_humidity; 1231 - s32 t_fine, comp_temp; 1232 - s32 *chans = (s32 *)data->sensor_data; 1229 + u32 adc_temp, adc_press, adc_humidity; 1230 + s32 t_fine; 1231 + struct { 1232 + u32 comp_press; 1233 + s32 comp_temp; 1234 + u32 comp_humidity; 1235 + aligned_s64 timestamp; 1236 + } buffer; 1233 1237 int ret; 1234 1238 1235 1239 guard(mutex)(&data->lock); ··· 1254 1248 goto out; 1255 1249 } 1256 1250 1257 - comp_temp = bmp280_compensate_temp(data, adc_temp); 1251 + buffer.comp_temp = bmp280_compensate_temp(data, adc_temp); 1258 1252 1259 1253 /* Pressure calculations */ 1260 1254 adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[0])); ··· 1264 1258 } 1265 1259 1266 1260 t_fine = bmp280_calc_t_fine(data, adc_temp); 1267 - comp_press = bmp280_compensate_press(data, adc_press, t_fine); 1261 + buffer.comp_press = bmp280_compensate_press(data, adc_press, t_fine); 1268 1262 1269 1263 /* Humidity calculations */ 1270 1264 adc_humidity = get_unaligned_be16(&data->buf[6]); ··· 1274 1268 goto out; 1275 1269 } 1276 1270 1277 - comp_humidity = bme280_compensate_humidity(data, adc_humidity, t_fine); 1271 + buffer.comp_humidity = bme280_compensate_humidity(data, adc_humidity, 1272 + t_fine); 1278 1273 1279 - chans[0] = comp_press; 1280 - chans[1] = comp_temp; 1281 - chans[2] = comp_humidity; 1282 - 1283 - iio_push_to_buffers_with_ts(indio_dev, data->sensor_data, 1284 - sizeof(data->sensor_data), 1274 + iio_push_to_buffers_with_ts(indio_dev, &buffer, sizeof(buffer), 1285 1275 iio_get_time_ns(indio_dev)); 1286 1276 1287 1277 out: ··· 1903 1901 struct iio_poll_func *pf = p; 1904 1902 struct iio_dev *indio_dev = pf->indio_dev; 1905 1903 struct bmp280_data *data = iio_priv(indio_dev); 1906 - u32 adc_temp, adc_press, comp_press; 1907 - s32 t_fine, comp_temp; 1908 - s32 *chans = (s32 *)data->sensor_data; 1904 + u32 adc_temp, adc_press; 1905 + s32 t_fine; 1906 + struct { 1907 + u32 comp_press; 1908 + s32 comp_temp; 1909 + aligned_s64 timestamp; 1910 + } buffer; 1909 1911 int ret; 1910 1912 1911 1913 guard(mutex)(&data->lock); ··· 1929 1923 goto out; 1930 1924 } 1931 1925 1932 - comp_temp = bmp380_compensate_temp(data, adc_temp); 1926 + buffer.comp_temp = bmp380_compensate_temp(data, adc_temp); 1933 1927 1934 1928 /* Pressure calculations */ 1935 1929 adc_press = get_unaligned_le24(&data->buf[0]); ··· 1939 1933 } 1940 1934 1941 1935 t_fine = bmp380_calc_t_fine(data, adc_temp); 1942 - comp_press = bmp380_compensate_press(data, adc_press, t_fine); 1936 + buffer.comp_press = bmp380_compensate_press(data, adc_press, t_fine); 1943 1937 1944 - chans[0] = comp_press; 1945 - chans[1] = comp_temp; 1946 - 1947 - iio_push_to_buffers_with_ts(indio_dev, data->sensor_data, 1948 - sizeof(data->sensor_data), 1938 + iio_push_to_buffers_with_ts(indio_dev, &buffer, sizeof(buffer), 1949 1939 iio_get_time_ns(indio_dev)); 1950 1940 1951 1941 out: ··· 2613 2611 struct iio_poll_func *pf = p; 2614 2612 struct iio_dev *indio_dev = pf->indio_dev; 2615 2613 struct bmp280_data *data = iio_priv(indio_dev); 2616 - int ret, offset; 2614 + struct { 2615 + __le32 comp_temp; 2616 + __le32 comp_press; 2617 + aligned_s64 timestamp; 2618 + } buffer; 2619 + int ret; 2617 2620 2618 2621 guard(mutex)(&data->lock); 2619 2622 ··· 2630 2623 goto out; 2631 2624 } 2632 2625 2633 - offset = 0; 2634 - 2635 2626 /* Pressure calculations */ 2636 - memcpy(&data->sensor_data[offset], &data->buf[3], 3); 2637 - 2638 - offset += sizeof(s32); 2627 + memcpy(&buffer.comp_press, &data->buf[3], 3); 2639 2628 2640 2629 /* Temperature calculations */ 2641 - memcpy(&data->sensor_data[offset], &data->buf[0], 3); 2630 + memcpy(&buffer.comp_temp, &data->buf[0], 3); 2642 2631 2643 - iio_push_to_buffers_with_ts(indio_dev, data->sensor_data, 2644 - sizeof(data->sensor_data), 2632 + iio_push_to_buffers_with_ts(indio_dev, &buffer, sizeof(buffer), 2645 2633 iio_get_time_ns(indio_dev)); 2646 2634 2647 2635 out: ··· 2958 2956 struct iio_poll_func *pf = p; 2959 2957 struct iio_dev *indio_dev = pf->indio_dev; 2960 2958 struct bmp280_data *data = iio_priv(indio_dev); 2961 - int ret, comp_temp, comp_press; 2962 - s32 *chans = (s32 *)data->sensor_data; 2959 + struct { 2960 + u32 comp_press; 2961 + s32 comp_temp; 2962 + aligned_s64 timestamp; 2963 + } buffer; 2964 + int ret; 2963 2965 2964 2966 guard(mutex)(&data->lock); 2965 2967 2966 - ret = bmp180_read_temp(data, &comp_temp); 2968 + ret = bmp180_read_temp(data, &buffer.comp_temp); 2967 2969 if (ret) 2968 2970 goto out; 2969 2971 2970 2972 2971 - ret = bmp180_read_press(data, &comp_press); 2973 + ret = bmp180_read_press(data, &buffer.comp_press); 2972 2974 if (ret) 2973 2975 goto out; 2974 2976 2975 - chans[0] = comp_press; 2976 - chans[1] = comp_temp; 2977 - 2978 - iio_push_to_buffers_with_ts(indio_dev, data->sensor_data, 2979 - sizeof(data->sensor_data), 2977 + iio_push_to_buffers_with_ts(indio_dev, &buffer, sizeof(buffer), 2980 2978 iio_get_time_ns(indio_dev)); 2981 2979 2982 2980 out:
-8
drivers/iio/pressure/bmp280.h
··· 349 349 BMP280_NUM_TEMP_BYTES + \ 350 350 BME280_NUM_HUMIDITY_BYTES) 351 351 352 - #define BME280_NUM_MAX_CHANNELS 3 353 352 /* Core exported structs */ 354 353 355 354 static const char *const bmp280_supply_names[] = { ··· 450 451 * section 3.6.3 of the datasheet. 451 452 */ 452 453 int sampling_freq; 453 - 454 - /* 455 - * Data to push to userspace triggered buffer. Up to 3 channels and 456 - * s64 timestamp, aligned. 457 - */ 458 - u8 sensor_data[ALIGN(sizeof(s32) * BME280_NUM_MAX_CHANNELS, sizeof(s64)) 459 - + sizeof(s64)] __aligned(sizeof(s64)); 460 454 461 455 /* Value to hold the current operation mode of the device */ 462 456 enum bmp280_op_mode op_mode;