[PATCH v2 4/6] iio: pressure: Simplify and make more clear temperature readings

From: Vasileios Amoiridis
Date: Wed Mar 13 2024 - 14:46:12 EST


The read_press/read_humid functions need the updated t_fine value
in order to calculate the current pressure/humidity. Temperature
reads should be removed from the read_press/read_humid functions
and should be placed in the oneshot captures before the pressure
and humidity reads. This makes the code more intuitive.

Signed-off-by: Vasileios Amoiridis <vassilisamir@xxxxxxxxx>
---
drivers/iio/pressure/bmp280-core.c | 38 ++++++++++++++----------------
1 file changed, 18 insertions(+), 20 deletions(-)

diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c
index 6d7734f867bc..377e90d9e5a2 100644
--- a/drivers/iio/pressure/bmp280-core.c
+++ b/drivers/iio/pressure/bmp280-core.c
@@ -404,11 +404,6 @@ static u32 bmp280_read_press(struct bmp280_data *data)
s32 adc_press;
int ret;

- /* Read and compensate temperature so we get a reading of t_fine. */
- ret = bmp280_read_temp(data);
- if (ret < 0)
- return ret;
-
ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
data->buf, sizeof(data->buf));
if (ret < 0) {
@@ -433,11 +428,6 @@ static u32 bmp280_read_humid(struct bmp280_data *data)
s32 adc_humidity;
int ret;

- /* Read and compensate temperature so we get a reading of t_fine. */
- ret = bmp280_read_temp(data);
- if (ret < 0)
- return ret;
-
ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
&data->be16, sizeof(data->be16));
if (ret < 0) {
@@ -470,12 +460,21 @@ static int bmp280_read_raw(struct iio_dev *indio_dev,
case IIO_CHAN_INFO_PROCESSED:
switch (chan->type) {
case IIO_HUMIDITYRELATIVE:
+ /* Read temperature to update the t_fine value */
+ data->chip_info->read_temp(data);
ret = data->chip_info->read_humid(data);
*val = data->chip_info->humid_coeffs[0] * ret;
*val2 = data->chip_info->humid_coeffs[1];
ret = IIO_VAL_FRACTIONAL;
break;
case IIO_PRESSURE:
+ /*
+ * Read temperature to update the t_fine value.
+ * BMP5xx devices do this in hardware, so skip it.
+ */
+ if (strcmp(indio_dev->name, "bmp580"))
+ data->chip_info->read_temp(data);
+
ret = data->chip_info->read_press(data);
*val = data->chip_info->press_coeffs[0] * ret;
*val2 = data->chip_info->press_coeffs[1];
@@ -500,10 +499,19 @@ static int bmp280_read_raw(struct iio_dev *indio_dev,
case IIO_CHAN_INFO_RAW:
switch (chan->type) {
case IIO_HUMIDITYRELATIVE:
+ /* Read temperature to update the t_fine value */
+ data->chip_info->read_temp(data);
*val = data->chip_info->read_humid(data);
ret = IIO_VAL_INT;
break;
case IIO_PRESSURE:
+ /*
+ * Read temperature to update the t_fine value.
+ * BMP5xx devices do this in hardware, so skip it.
+ */
+ if (strcmp(indio_dev->name, "bmp580"))
+ data->chip_info->read_temp(data);
+
*val = data->chip_info->read_press(data);
ret = IIO_VAL_INT;
break;
@@ -1092,11 +1100,6 @@ static u32 bmp380_read_press(struct bmp280_data *data)
s32 adc_press;
int ret;

- /* Read and compensate for temperature so we get a reading of t_fine */
- ret = bmp380_read_temp(data);
- if (ret)
- return ret;
-
ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB,
data->buf, sizeof(data->buf));
if (ret) {
@@ -2009,11 +2012,6 @@ static u32 bmp180_read_press(struct bmp280_data *data)
s32 adc_press;
int ret;

- /* Read and compensate temperature so we get a reading of t_fine. */
- ret = bmp180_read_temp(data);
- if (ret)
- return ret;
-
ret = bmp180_read_adc_press(data, &adc_press);
if (ret)
return ret;
--
2.25.1