2024-06-06 21:30:34

by Vasileios Amoiridis

[permalink] [raw]
Subject: [PATCH v2 00/19] iio: chemical: bme680: Driver fixes and cleanup

Changes in v2:

Patch 4/19:
- Combined the bme680_conversion_time_us() and bme680_wait_for_eoc()
into one function.
- Added better comment for the calculation.
- Added checks in the bme680_wait_for_eoc() function.

Patch 5/19:
- Fixed typo in commit message.

Patch 6/19:
- Added a fixes tag since without the mutexes, read operations can be
broken.

Patch 10/19:
- Converted shifting operation to FIELD_GET()

Patch 11/19:
- Changed convention from &data->bufer[0] to data->buffer.
- Removed IIO_DMA_MINALIGN as it is not needed anymore.

Patch 13/19:
- Removed IIO_DMA_MINALIGN

Patch 14/19:
- Splitted from Patch v1 14/19

Patch 15/19:
- Splitted from Patch v1 14/19

Patch 16/19: **NEW**
- Use dev_err_probe() where applicable.

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

This started as a series to add support for buffers and the new
BME688 but it ended up being just a cleaning series. These might
be quite some patches for such a thing but I feel that they are
are well split, in order to allow for better review.

The patches are mostly small changes but essential for the correct use
of the driver. The first patches looked like fixes that should be
marked for the stable. Patches [11,17/17] might be a bit bigger but 11/17
is quite straightforward and 17/17 is basically a duplication of a
very similar commit coming from the BMP280 driver [1].

In general, the datasheet [2] of the driver is not very descriptive,
and it redirects the user to the BME68x Sensor API [3]. All the things
that were identified from the BME68x Sensor API have been marked with
links to the original locations of the GitHub code. If this is too much
and we don't want this type of information on the commit message, please
let me know and I will fix it.

[1]: https://lore.kernel.org/linux-iio/[email protected]/T/#mc6f814e9a4f8c2b39015909d174c7013b3648b9b
[2]: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme680-ds001.pdf
[3]: https://github.com/boschsensortec/BME68x_SensorAPI/tree/master

Vasileios Amoiridis (19):
iio: chemical: bme680: Fix pressure value output
iio: chemical: bme680: Fix calibration data variable
iio: chemical: bme680: Fix overflows in compensate() functions
iio: chemical: bme680: Fix sensor data read operation
iio: chemical: bme680: Fix typo in define
iio: chemical: bme680: Fix read/write ops to device by adding mutexes
iio: chemical: bme680: Drop unnecessary casts and correct adc data
types
iio: chemical: bme680: Remove remaining ACPI-only stuff
iio: chemical: bme680: Sort headers alphabetically
iio: chemical: bme680: Remove duplicate register read
iio: chemical: bme680: Use bulk reads for calibration data
iio: chemical: bme680: Allocate IIO device before chip initialization
iio: chemical: bme680: Add read buffers in DMA safe region
iio: chemical: bme680: Make error checks consistent
iio: chemical: bme680: Modify startup procedure
iio: chemical: bme680: Move probe errors to dev_err_probe()
iio: chemical: bme680: Remove redundant gas configuration
iio: chemical: bme680: Move forced mode setup in ->read_raw()
iio: chemical: bme680: Refactorize reading functions

drivers/iio/chemical/bme680.h | 43 +-
drivers/iio/chemical/bme680_core.c | 669 ++++++++++++++---------------
2 files changed, 337 insertions(+), 375 deletions(-)


base-commit: b3019fcdeb286b2cfe45e44bccb44dbcd8ff66dd
--
2.25.1



2024-06-06 21:30:59

by Vasileios Amoiridis

[permalink] [raw]
Subject: [PATCH v2 02/19] iio: chemical: bme680: Fix calibration data variable

According to the BME68x Sensor API [1], the h6 calibration
data variable should be an unsigned integer of size 8.

[1]: https://github.com/boschsensortec/BME68x_SensorAPI/blob/v4.4.8/bme68x_defs.h#L789
Fixes: 1b3bd8592780 ("iio: chemical: Add support for Bosch BME680 sensor")
Signed-off-by: Vasileios Amoiridis <[email protected]>
---
drivers/iio/chemical/bme680_core.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/chemical/bme680_core.c b/drivers/iio/chemical/bme680_core.c
index 2c40c13fe97a..812829841733 100644
--- a/drivers/iio/chemical/bme680_core.c
+++ b/drivers/iio/chemical/bme680_core.c
@@ -38,7 +38,7 @@ struct bme680_calib {
s8 par_h3;
s8 par_h4;
s8 par_h5;
- s8 par_h6;
+ u8 par_h6;
s8 par_h7;
s8 par_gh1;
s16 par_gh2;
--
2.25.1


2024-06-06 21:36:01

by Vasileios Amoiridis

[permalink] [raw]
Subject: [PATCH v2 11/19] iio: chemical: bme680: Use bulk reads for calibration data

Calibration data are located in contiguous-ish registers
inside the chip. For that reason we can use bulk reads as is
done as well in the BME68x Sensor API [1].

The arrays that are used for reading the data out of the sensor
are located inside DMA safe buffer.

[1]: https://github.com/boschsensortec/BME68x_SensorAPI/blob/v4.4.8/bme68x.c#L1769
Signed-off-by: Vasileios Amoiridis <[email protected]>
---
drivers/iio/chemical/bme680.h | 28 +--
drivers/iio/chemical/bme680_core.c | 279 ++++++++++-------------------
2 files changed, 97 insertions(+), 210 deletions(-)

diff --git a/drivers/iio/chemical/bme680.h b/drivers/iio/chemical/bme680.h
index b5f16ca81e70..8d0f53c05d7d 100644
--- a/drivers/iio/chemical/bme680.h
+++ b/drivers/iio/chemical/bme680.h
@@ -39,10 +39,8 @@
#define BME680_HUM_REG_SHIFT_VAL 4
#define BME680_BIT_H1_DATA_MASK GENMASK(3, 0)

-#define BME680_REG_RES_HEAT_RANGE 0x02
#define BME680_RHRANGE_MASK GENMASK(5, 4)
#define BME680_REG_RES_HEAT_VAL 0x00
-#define BME680_REG_RANGE_SW_ERR 0x04
#define BME680_RSERROR_MASK GENMASK(7, 4)
#define BME680_REG_RES_HEAT_0 0x5A
#define BME680_REG_GAS_WAIT_0 0x64
@@ -60,31 +58,13 @@

/* Calibration Parameters */
#define BME680_T2_LSB_REG 0x8A
-#define BME680_T3_REG 0x8C
-#define BME680_P1_LSB_REG 0x8E
-#define BME680_P2_LSB_REG 0x90
-#define BME680_P3_REG 0x92
-#define BME680_P4_LSB_REG 0x94
-#define BME680_P5_LSB_REG 0x96
-#define BME680_P7_REG 0x98
-#define BME680_P6_REG 0x99
-#define BME680_P8_LSB_REG 0x9C
-#define BME680_P9_LSB_REG 0x9E
-#define BME680_P10_REG 0xA0
-#define BME680_H2_LSB_REG 0xE2
#define BME680_H2_MSB_REG 0xE1
-#define BME680_H1_MSB_REG 0xE3
-#define BME680_H1_LSB_REG 0xE2
-#define BME680_H3_REG 0xE4
-#define BME680_H4_REG 0xE5
-#define BME680_H5_REG 0xE6
-#define BME680_H6_REG 0xE7
-#define BME680_H7_REG 0xE8
-#define BME680_T1_LSB_REG 0xE9
-#define BME680_GH2_LSB_REG 0xEB
-#define BME680_GH1_REG 0xED
#define BME680_GH3_REG 0xEE

+#define BME680_CALIB_RANGE_1_LEN 23
+#define BME680_CALIB_RANGE_2_LEN 14
+#define BME680_CALIB_RANGE_3_LEN 5
+
extern const struct regmap_config bme680_regmap_config;

int bme680_core_probe(struct device *dev, struct regmap *regmap,
diff --git a/drivers/iio/chemical/bme680_core.c b/drivers/iio/chemical/bme680_core.c
index c1e191ed4955..f96cd4157c95 100644
--- a/drivers/iio/chemical/bme680_core.c
+++ b/drivers/iio/chemical/bme680_core.c
@@ -19,8 +19,53 @@
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>

+#include <asm/unaligned.h>
+
#include "bme680.h"

+/* 1st set of calibration data */
+enum {
+ /* Temperature calib indexes */
+ T2_LSB = 0,
+ T3 = 2,
+ /* Pressure calib indexes */
+ P1_LSB = 4,
+ P2_LSB = 6,
+ P3 = 8,
+ P4_LSB = 10,
+ P5_LSB = 12,
+ P7 = 14,
+ P6 = 15,
+ P8_LSB = 18,
+ P9_LSB = 20,
+ P10 = 22,
+};
+
+/* 2nd set of calibration data */
+enum {
+ /* Humidity calib indexes */
+ H2_MSB = 0,
+ H1_LSB = 1,
+ H3 = 3,
+ H4 = 4,
+ H5 = 5,
+ H6 = 6,
+ H7 = 7,
+ /* Stray T1 calib index */
+ T1_LSB = 8,
+ /* Gas heater calib indexes */
+ GH2_LSB = 10,
+ GH1 = 12,
+ GH3 = 13,
+};
+
+/* 3rd set of calibration data */
+enum {
+ RES_HEAT_VAL = 0,
+ RES_HEAT_RANGE = 2,
+ RANGE_SW_ERR = 4,
+};
+
struct bme680_calib {
u16 par_t1;
s16 par_t2;
@@ -64,6 +109,12 @@ struct bme680_data {
* and humidity compensation calculations.
*/
s32 t_fine;
+
+ union {
+ u8 bme680_cal_buf_1[BME680_CALIB_RANGE_1_LEN];
+ u8 bme680_cal_buf_2[BME680_CALIB_RANGE_2_LEN];
+ u8 bme680_cal_buf_3[BME680_CALIB_RANGE_3_LEN];
+ };
};

static const struct regmap_range bme680_volatile_ranges[] = {
@@ -112,217 +163,73 @@ static int bme680_read_calib(struct bme680_data *data,
struct bme680_calib *calib)
{
struct device *dev = regmap_get_device(data->regmap);
- unsigned int tmp, tmp_msb, tmp_lsb;
+ unsigned int tmp_msb, tmp_lsb;
int ret;
- __le16 buf;
-
- /* Temperature related coefficients */
- ret = regmap_bulk_read(data->regmap, BME680_T1_LSB_REG,
- &buf, sizeof(buf));
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_T1_LSB_REG\n");
- return ret;
- }
- calib->par_t1 = le16_to_cpu(buf);

ret = regmap_bulk_read(data->regmap, BME680_T2_LSB_REG,
- &buf, sizeof(buf));
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_T2_LSB_REG\n");
- return ret;
- }
- calib->par_t2 = le16_to_cpu(buf);
-
- ret = regmap_read(data->regmap, BME680_T3_REG, &tmp);
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_T3_REG\n");
- return ret;
- }
- calib->par_t3 = tmp;
-
- /* Pressure related coefficients */
- ret = regmap_bulk_read(data->regmap, BME680_P1_LSB_REG,
- &buf, sizeof(buf));
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_P1_LSB_REG\n");
- return ret;
- }
- calib->par_p1 = le16_to_cpu(buf);
-
- ret = regmap_bulk_read(data->regmap, BME680_P2_LSB_REG,
- &buf, sizeof(buf));
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_P2_LSB_REG\n");
- return ret;
- }
- calib->par_p2 = le16_to_cpu(buf);
-
- ret = regmap_read(data->regmap, BME680_P3_REG, &tmp);
+ data->bme680_cal_buf_1,
+ sizeof(data->bme680_cal_buf_1));
if (ret < 0) {
- dev_err(dev, "failed to read BME680_P3_REG\n");
+ dev_err(dev, "failed to read 1st set of calib data;\n");
return ret;
}
- calib->par_p3 = tmp;

- ret = regmap_bulk_read(data->regmap, BME680_P4_LSB_REG,
- &buf, sizeof(buf));
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_P4_LSB_REG\n");
- return ret;
- }
- calib->par_p4 = le16_to_cpu(buf);
+ calib->par_t2 = get_unaligned_le16(&data->bme680_cal_buf_1[T2_LSB]);
+ calib->par_t3 = data->bme680_cal_buf_1[T3];
+ calib->par_p1 = get_unaligned_le16(&data->bme680_cal_buf_1[P1_LSB]);
+ calib->par_p2 = get_unaligned_le16(&data->bme680_cal_buf_1[P2_LSB]);
+ calib->par_p3 = data->bme680_cal_buf_1[P3];
+ calib->par_p4 = get_unaligned_le16(&data->bme680_cal_buf_1[P4_LSB]);
+ calib->par_p5 = get_unaligned_le16(&data->bme680_cal_buf_1[P5_LSB]);
+ calib->par_p7 = data->bme680_cal_buf_1[P7];
+ calib->par_p6 = data->bme680_cal_buf_1[P6];
+ calib->par_p8 = get_unaligned_le16(&data->bme680_cal_buf_1[P8_LSB]);
+ calib->par_p9 = get_unaligned_le16(&data->bme680_cal_buf_1[P9_LSB]);
+ calib->par_p10 = data->bme680_cal_buf_1[P10];

- ret = regmap_bulk_read(data->regmap, BME680_P5_LSB_REG,
- &buf, sizeof(buf));
+ ret = regmap_bulk_read(data->regmap, BME680_H2_MSB_REG,
+ data->bme680_cal_buf_2,
+ sizeof(data->bme680_cal_buf_2));
if (ret < 0) {
- dev_err(dev, "failed to read BME680_P5_LSB_REG\n");
+ dev_err(dev, "failed to read 2nd set of calib data;\n");
return ret;
}
- calib->par_p5 = le16_to_cpu(buf);

- ret = regmap_read(data->regmap, BME680_P6_REG, &tmp);
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_P6_REG\n");
- return ret;
- }
- calib->par_p6 = tmp;
-
- ret = regmap_read(data->regmap, BME680_P7_REG, &tmp);
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_P7_REG\n");
- return ret;
- }
- calib->par_p7 = tmp;
-
- ret = regmap_bulk_read(data->regmap, BME680_P8_LSB_REG,
- &buf, sizeof(buf));
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_P8_LSB_REG\n");
- return ret;
- }
- calib->par_p8 = le16_to_cpu(buf);
-
- ret = regmap_bulk_read(data->regmap, BME680_P9_LSB_REG,
- &buf, sizeof(buf));
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_P9_LSB_REG\n");
- return ret;
- }
- calib->par_p9 = le16_to_cpu(buf);
-
- ret = regmap_read(data->regmap, BME680_P10_REG, &tmp);
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_P10_REG\n");
- return ret;
- }
- calib->par_p10 = tmp;
-
- /* Humidity related coefficients */
- ret = regmap_read(data->regmap, BME680_H1_MSB_REG, &tmp_msb);
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_H1_MSB_REG\n");
- return ret;
- }
- ret = regmap_read(data->regmap, BME680_H1_LSB_REG, &tmp_lsb);
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_H1_LSB_REG\n");
- return ret;
- }
+ tmp_lsb = data->bme680_cal_buf_2[H1_LSB];
+ tmp_msb = data->bme680_cal_buf_2[H1_LSB + 1];
calib->par_h1 = (tmp_msb << BME680_HUM_REG_SHIFT_VAL) |
(tmp_lsb & BME680_BIT_H1_DATA_MASK);

- ret = regmap_read(data->regmap, BME680_H2_MSB_REG, &tmp_msb);
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_H2_MSB_REG\n");
- return ret;
- }
- ret = regmap_read(data->regmap, BME680_H2_LSB_REG, &tmp_lsb);
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_H2_LSB_REG\n");
- return ret;
- }
+ tmp_msb = data->bme680_cal_buf_2[H2_MSB];
+ tmp_lsb = data->bme680_cal_buf_2[H2_MSB + 1];
calib->par_h2 = (tmp_msb << BME680_HUM_REG_SHIFT_VAL) |
(tmp_lsb >> BME680_HUM_REG_SHIFT_VAL);

- ret = regmap_read(data->regmap, BME680_H3_REG, &tmp);
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_H3_REG\n");
- return ret;
- }
- calib->par_h3 = tmp;
+ calib->par_h3 = data->bme680_cal_buf_2[H3];
+ calib->par_h4 = data->bme680_cal_buf_2[H4];
+ calib->par_h5 = data->bme680_cal_buf_2[H5];
+ calib->par_h6 = data->bme680_cal_buf_2[H6];
+ calib->par_h7 = data->bme680_cal_buf_2[H7];
+ calib->par_t1 = get_unaligned_le16(&data->bme680_cal_buf_2[T1_LSB]);
+ calib->par_gh2 = get_unaligned_le16(&data->bme680_cal_buf_2[GH2_LSB]);
+ calib->par_gh1 = data->bme680_cal_buf_2[GH1];
+ calib->par_gh3 = data->bme680_cal_buf_2[GH3];

- ret = regmap_read(data->regmap, BME680_H4_REG, &tmp);
+ ret = regmap_bulk_read(data->regmap, BME680_REG_RES_HEAT_VAL,
+ data->bme680_cal_buf_3,
+ sizeof(data->bme680_cal_buf_3));
if (ret < 0) {
- dev_err(dev, "failed to read BME680_H4_REG\n");
+ dev_err(dev, "failed to read 3rd set of calib data;\n");
return ret;
}
- calib->par_h4 = tmp;

- ret = regmap_read(data->regmap, BME680_H5_REG, &tmp);
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_H5_REG\n");
- return ret;
- }
- calib->par_h5 = tmp;
+ calib->res_heat_val = data->bme680_cal_buf_3[RES_HEAT_VAL];

- ret = regmap_read(data->regmap, BME680_H6_REG, &tmp);
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_H6_REG\n");
- return ret;
- }
- calib->par_h6 = tmp;
+ calib->res_heat_range = FIELD_GET(BME680_RHRANGE_MASK,
+ data->bme680_cal_buf_3[RES_HEAT_RANGE]);

- ret = regmap_read(data->regmap, BME680_H7_REG, &tmp);
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_H7_REG\n");
- return ret;
- }
- calib->par_h7 = tmp;
-
- /* Gas heater related coefficients */
- ret = regmap_read(data->regmap, BME680_GH1_REG, &tmp);
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_GH1_REG\n");
- return ret;
- }
- calib->par_gh1 = tmp;
-
- ret = regmap_bulk_read(data->regmap, BME680_GH2_LSB_REG,
- &buf, sizeof(buf));
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_GH2_LSB_REG\n");
- return ret;
- }
- calib->par_gh2 = le16_to_cpu(buf);
-
- ret = regmap_read(data->regmap, BME680_GH3_REG, &tmp);
- if (ret < 0) {
- dev_err(dev, "failed to read BME680_GH3_REG\n");
- return ret;
- }
- calib->par_gh3 = tmp;
-
- /* Other coefficients */
- ret = regmap_read(data->regmap, BME680_REG_RES_HEAT_RANGE, &tmp);
- if (ret < 0) {
- dev_err(dev, "failed to read resistance heat range\n");
- return ret;
- }
- calib->res_heat_range = FIELD_GET(BME680_RHRANGE_MASK, tmp);
-
- ret = regmap_read(data->regmap, BME680_REG_RES_HEAT_VAL, &tmp);
- if (ret < 0) {
- dev_err(dev, "failed to read resistance heat value\n");
- return ret;
- }
- calib->res_heat_val = tmp;
-
- ret = regmap_read(data->regmap, BME680_REG_RANGE_SW_ERR, &tmp);
- if (ret < 0) {
- dev_err(dev, "failed to read range software error\n");
- return ret;
- }
- calib->range_sw_err = FIELD_GET(BME680_RSERROR_MASK, tmp);
+ calib->range_sw_err = FIELD_GET(BME680_RSERROR_MASK,
+ data->bme680_cal_buf_3[RANGE_SW_ERR]);

return 0;
}
--
2.25.1


2024-06-06 21:36:34

by Vasileios Amoiridis

[permalink] [raw]
Subject: [PATCH v2 13/19] iio: chemical: bme680: Add read buffers in DMA safe region

Move the buffers that are used in order to read data from the
device in a DMA-safe region. Also create defines for the number
of bytes that are being read from the device and don't use
magic numbers.

Signed-off-by: Vasileios Amoiridis <[email protected]>
---
drivers/iio/chemical/bme680.h | 7 +++++
drivers/iio/chemical/bme680_core.c | 45 +++++++++++++++---------------
2 files changed, 29 insertions(+), 23 deletions(-)

diff --git a/drivers/iio/chemical/bme680.h b/drivers/iio/chemical/bme680.h
index 8d0f53c05d7d..7d0ff294725a 100644
--- a/drivers/iio/chemical/bme680.h
+++ b/drivers/iio/chemical/bme680.h
@@ -56,6 +56,13 @@
#define BME680_GAS_MEAS_BIT BIT(6)
#define BME680_MEAS_BIT BIT(5)

+#define BME680_TEMP_NUM_BYTES 3
+#define BME680_PRESS_NUM_BYTES 3
+#define BME680_HUMID_NUM_BYTES 2
+#define BME680_GAS_NUM_BYTES 2
+
+#define BME680_MEAS_TRIM_MASK GENMASK(24, 4)
+
/* Calibration Parameters */
#define BME680_T2_LSB_REG 0x8A
#define BME680_H2_MSB_REG 0xE1
diff --git a/drivers/iio/chemical/bme680_core.c b/drivers/iio/chemical/bme680_core.c
index 538696ac4205..99259d0cf13d 100644
--- a/drivers/iio/chemical/bme680_core.c
+++ b/drivers/iio/chemical/bme680_core.c
@@ -115,6 +115,9 @@ struct bme680_data {
* transfer buffers to live in their own cache lines.
*/
union {
+ u8 buf[3];
+ unsigned int check;
+ __be16 be16;
u8 bme680_cal_buf_1[BME680_CALIB_RANGE_1_LEN];
u8 bme680_cal_buf_2[BME680_CALIB_RANGE_2_LEN];
u8 bme680_cal_buf_3[BME680_CALIB_RANGE_3_LEN];
@@ -453,7 +456,6 @@ static u8 bme680_oversampling_to_reg(u8 val)
static int bme680_wait_for_eoc(struct bme680_data *data)
{
struct device *dev = regmap_get_device(data->regmap);
- unsigned int check;
int ret;
/*
* (Sum of oversampling ratios * time per oversampling) +
@@ -466,16 +468,16 @@ static int bme680_wait_for_eoc(struct bme680_data *data)

usleep_range(wait_eoc_us, wait_eoc_us + 100);

- ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &check);
+ ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &data->check);
if (ret) {
dev_err(dev, "failed to read measurement status register.\n");
return ret;
}
- if (check & BME680_MEAS_BIT) {
+ if (data->check & BME680_MEAS_BIT) {
dev_err(dev, "Device measurement cycle incomplete.\n");
return -EBUSY;
}
- if (!(check & BME680_NEW_DATA_BIT)) {
+ if (!(data->check & BME680_NEW_DATA_BIT)) {
dev_err(dev, "No new data available from the device.\n");
return -ENODATA;
}
@@ -564,7 +566,6 @@ static int bme680_read_temp(struct bme680_data *data, int *val)
{
struct device *dev = regmap_get_device(data->regmap);
int ret;
- __be32 tmp = 0;
u32 adc_temp;
s16 comp_temp;

@@ -578,13 +579,14 @@ static int bme680_read_temp(struct bme680_data *data, int *val)
return ret;

ret = regmap_bulk_read(data->regmap, BME680_REG_TEMP_MSB,
- &tmp, 3);
+ data->buf, BME680_TEMP_NUM_BYTES);
if (ret < 0) {
dev_err(dev, "failed to read temperature\n");
return ret;
}

- adc_temp = be32_to_cpu(tmp) >> 12;
+ adc_temp = FIELD_GET(BME680_MEAS_TRIM_MASK,
+ get_unaligned_be24(data->buf));
if (adc_temp == BME680_MEAS_SKIPPED) {
/* reading was skipped */
dev_err(dev, "reading temperature skipped\n");
@@ -610,7 +612,6 @@ static int bme680_read_press(struct bme680_data *data,
{
struct device *dev = regmap_get_device(data->regmap);
int ret;
- __be32 tmp = 0;
u32 adc_press;

/* Read and compensate temperature to get a reading of t_fine */
@@ -619,13 +620,14 @@ static int bme680_read_press(struct bme680_data *data,
return ret;

ret = regmap_bulk_read(data->regmap, BME680_REG_PRESS_MSB,
- &tmp, 3);
+ data->buf, BME680_PRESS_NUM_BYTES);
if (ret < 0) {
dev_err(dev, "failed to read pressure\n");
return ret;
}

- adc_press = be32_to_cpu(tmp) >> 12;
+ adc_press = FIELD_GET(BME680_MEAS_TRIM_MASK,
+ get_unaligned_be24(data->buf));
if (adc_press == BME680_MEAS_SKIPPED) {
/* reading was skipped */
dev_err(dev, "reading pressure skipped\n");
@@ -642,7 +644,6 @@ static int bme680_read_humid(struct bme680_data *data,
{
struct device *dev = regmap_get_device(data->regmap);
int ret;
- __be16 tmp = 0;
u16 adc_humidity;
u32 comp_humidity;

@@ -652,13 +653,13 @@ static int bme680_read_humid(struct bme680_data *data,
return ret;

ret = regmap_bulk_read(data->regmap, BME680_REG_HUMIDITY_MSB,
- &tmp, sizeof(tmp));
+ &data->be16, BME680_HUMID_NUM_BYTES);
if (ret < 0) {
dev_err(dev, "failed to read humidity\n");
return ret;
}

- adc_humidity = be16_to_cpu(tmp);
+ adc_humidity = be16_to_cpu(data->be16);
if (adc_humidity == BME680_MEAS_SKIPPED) {
/* reading was skipped */
dev_err(dev, "reading humidity skipped\n");
@@ -676,8 +677,6 @@ static int bme680_read_gas(struct bme680_data *data,
{
struct device *dev = regmap_get_device(data->regmap);
int ret;
- __be16 tmp = 0;
- unsigned int check;
u16 adc_gas_res, gas_regs_val;
u8 gas_range;

@@ -697,19 +696,20 @@ static int bme680_read_gas(struct bme680_data *data,
if (ret)
return ret;

- ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &check);
- if (check & BME680_GAS_MEAS_BIT) {
+ ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &data->check);
+ if (data->check & BME680_GAS_MEAS_BIT) {
dev_err(dev, "gas measurement incomplete\n");
return -EBUSY;
}

ret = regmap_bulk_read(data->regmap, BME680_REG_GAS_MSB,
- &tmp, sizeof(tmp));
+ &data->be16, BME680_GAS_NUM_BYTES);
if (ret < 0) {
dev_err(dev, "failed to read gas resistance\n");
return ret;
}
- gas_regs_val = be16_to_cpu(tmp);
+
+ gas_regs_val = be16_to_cpu(data->be16);
adc_gas_res = FIELD_GET(BME680_ADC_GAS_RES, gas_regs_val);

/*
@@ -838,7 +838,6 @@ int bme680_core_probe(struct device *dev, struct regmap *regmap,
{
struct iio_dev *indio_dev;
struct bme680_data *data;
- unsigned int val;
int ret;

indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
@@ -869,15 +868,15 @@ int bme680_core_probe(struct device *dev, struct regmap *regmap,
return ret;
}

- ret = regmap_read(regmap, BME680_REG_CHIP_ID, &val);
+ ret = regmap_read(regmap, BME680_REG_CHIP_ID, &data->check);
if (ret < 0) {
dev_err(dev, "Error reading chip ID\n");
return ret;
}

- if (val != BME680_CHIP_ID_VAL) {
+ if (data->check != BME680_CHIP_ID_VAL) {
dev_err(dev, "Wrong chip ID, got %x expected %x\n",
- val, BME680_CHIP_ID_VAL);
+ data->check, BME680_CHIP_ID_VAL);
return -ENODEV;
}

--
2.25.1


2024-06-06 21:36:45

by Vasileios Amoiridis

[permalink] [raw]
Subject: [PATCH v2 13/19] iio: chemical: bme680: Add read buffers in read/write buffer union

Move the buffers that are used in order to read data from the
device in the union which handles all the device read/write
buffers. Also create defines for the number of bytes that are
being read from the device and don't use magic numbers.

Signed-off-by: Vasileios Amoiridis <[email protected]>
---
drivers/iio/chemical/bme680.h | 7 +++++
drivers/iio/chemical/bme680_core.c | 45 +++++++++++++++---------------
2 files changed, 29 insertions(+), 23 deletions(-)

diff --git a/drivers/iio/chemical/bme680.h b/drivers/iio/chemical/bme680.h
index 8d0f53c05d7d..7d0ff294725a 100644
--- a/drivers/iio/chemical/bme680.h
+++ b/drivers/iio/chemical/bme680.h
@@ -56,6 +56,13 @@
#define BME680_GAS_MEAS_BIT BIT(6)
#define BME680_MEAS_BIT BIT(5)

+#define BME680_TEMP_NUM_BYTES 3
+#define BME680_PRESS_NUM_BYTES 3
+#define BME680_HUMID_NUM_BYTES 2
+#define BME680_GAS_NUM_BYTES 2
+
+#define BME680_MEAS_TRIM_MASK GENMASK(24, 4)
+
/* Calibration Parameters */
#define BME680_T2_LSB_REG 0x8A
#define BME680_H2_MSB_REG 0xE1
diff --git a/drivers/iio/chemical/bme680_core.c b/drivers/iio/chemical/bme680_core.c
index 9d33952e5d01..3c33c21b5d6a 100644
--- a/drivers/iio/chemical/bme680_core.c
+++ b/drivers/iio/chemical/bme680_core.c
@@ -111,6 +111,9 @@ struct bme680_data {
s32 t_fine;

union {
+ u8 buf[3];
+ unsigned int check;
+ __be16 be16;
u8 bme680_cal_buf_1[BME680_CALIB_RANGE_1_LEN];
u8 bme680_cal_buf_2[BME680_CALIB_RANGE_2_LEN];
u8 bme680_cal_buf_3[BME680_CALIB_RANGE_3_LEN];
@@ -449,7 +452,6 @@ static u8 bme680_oversampling_to_reg(u8 val)
static int bme680_wait_for_eoc(struct bme680_data *data)
{
struct device *dev = regmap_get_device(data->regmap);
- unsigned int check;
int ret;
/*
* (Sum of oversampling ratios * time per oversampling) +
@@ -462,16 +464,16 @@ static int bme680_wait_for_eoc(struct bme680_data *data)

usleep_range(wait_eoc_us, wait_eoc_us + 100);

- ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &check);
+ ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &data->check);
if (ret) {
dev_err(dev, "failed to read measurement status register.\n");
return ret;
}
- if (check & BME680_MEAS_BIT) {
+ if (data->check & BME680_MEAS_BIT) {
dev_err(dev, "Device measurement cycle incomplete.\n");
return -EBUSY;
}
- if (!(check & BME680_NEW_DATA_BIT)) {
+ if (!(data->check & BME680_NEW_DATA_BIT)) {
dev_err(dev, "No new data available from the device.\n");
return -ENODATA;
}
@@ -560,7 +562,6 @@ static int bme680_read_temp(struct bme680_data *data, int *val)
{
struct device *dev = regmap_get_device(data->regmap);
int ret;
- __be32 tmp = 0;
u32 adc_temp;
s16 comp_temp;

@@ -574,13 +575,14 @@ static int bme680_read_temp(struct bme680_data *data, int *val)
return ret;

ret = regmap_bulk_read(data->regmap, BME680_REG_TEMP_MSB,
- &tmp, 3);
+ data->buf, BME680_TEMP_NUM_BYTES);
if (ret < 0) {
dev_err(dev, "failed to read temperature\n");
return ret;
}

- adc_temp = be32_to_cpu(tmp) >> 12;
+ adc_temp = FIELD_GET(BME680_MEAS_TRIM_MASK,
+ get_unaligned_be24(data->buf));
if (adc_temp == BME680_MEAS_SKIPPED) {
/* reading was skipped */
dev_err(dev, "reading temperature skipped\n");
@@ -606,7 +608,6 @@ static int bme680_read_press(struct bme680_data *data,
{
struct device *dev = regmap_get_device(data->regmap);
int ret;
- __be32 tmp = 0;
u32 adc_press;

/* Read and compensate temperature to get a reading of t_fine */
@@ -615,13 +616,14 @@ static int bme680_read_press(struct bme680_data *data,
return ret;

ret = regmap_bulk_read(data->regmap, BME680_REG_PRESS_MSB,
- &tmp, 3);
+ data->buf, BME680_PRESS_NUM_BYTES);
if (ret < 0) {
dev_err(dev, "failed to read pressure\n");
return ret;
}

- adc_press = be32_to_cpu(tmp) >> 12;
+ adc_press = FIELD_GET(BME680_MEAS_TRIM_MASK,
+ get_unaligned_be24(data->buf));
if (adc_press == BME680_MEAS_SKIPPED) {
/* reading was skipped */
dev_err(dev, "reading pressure skipped\n");
@@ -638,7 +640,6 @@ static int bme680_read_humid(struct bme680_data *data,
{
struct device *dev = regmap_get_device(data->regmap);
int ret;
- __be16 tmp = 0;
u16 adc_humidity;
u32 comp_humidity;

@@ -648,13 +649,13 @@ static int bme680_read_humid(struct bme680_data *data,
return ret;

ret = regmap_bulk_read(data->regmap, BME680_REG_HUMIDITY_MSB,
- &tmp, sizeof(tmp));
+ &data->be16, BME680_HUMID_NUM_BYTES);
if (ret < 0) {
dev_err(dev, "failed to read humidity\n");
return ret;
}

- adc_humidity = be16_to_cpu(tmp);
+ adc_humidity = be16_to_cpu(data->be16);
if (adc_humidity == BME680_MEAS_SKIPPED) {
/* reading was skipped */
dev_err(dev, "reading humidity skipped\n");
@@ -672,8 +673,6 @@ static int bme680_read_gas(struct bme680_data *data,
{
struct device *dev = regmap_get_device(data->regmap);
int ret;
- __be16 tmp = 0;
- unsigned int check;
u16 adc_gas_res, gas_regs_val;
u8 gas_range;

@@ -693,19 +692,20 @@ static int bme680_read_gas(struct bme680_data *data,
if (ret)
return ret;

- ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &check);
- if (check & BME680_GAS_MEAS_BIT) {
+ ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &data->check);
+ if (data->check & BME680_GAS_MEAS_BIT) {
dev_err(dev, "gas measurement incomplete\n");
return -EBUSY;
}

ret = regmap_bulk_read(data->regmap, BME680_REG_GAS_MSB,
- &tmp, sizeof(tmp));
+ &data->be16, BME680_GAS_NUM_BYTES);
if (ret < 0) {
dev_err(dev, "failed to read gas resistance\n");
return ret;
}
- gas_regs_val = be16_to_cpu(tmp);
+
+ gas_regs_val = be16_to_cpu(data->be16);
adc_gas_res = FIELD_GET(BME680_ADC_GAS_RES, gas_regs_val);

/*
@@ -834,7 +834,6 @@ int bme680_core_probe(struct device *dev, struct regmap *regmap,
{
struct iio_dev *indio_dev;
struct bme680_data *data;
- unsigned int val;
int ret;

indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
@@ -865,15 +864,15 @@ int bme680_core_probe(struct device *dev, struct regmap *regmap,
return ret;
}

- ret = regmap_read(regmap, BME680_REG_CHIP_ID, &val);
+ ret = regmap_read(regmap, BME680_REG_CHIP_ID, &data->check);
if (ret < 0) {
dev_err(dev, "Error reading chip ID\n");
return ret;
}

- if (val != BME680_CHIP_ID_VAL) {
+ if (data->check != BME680_CHIP_ID_VAL) {
dev_err(dev, "Wrong chip ID, got %x expected %x\n",
- val, BME680_CHIP_ID_VAL);
+ data->check, BME680_CHIP_ID_VAL);
return -ENODEV;
}

--
2.25.1


2024-06-06 21:37:11

by Vasileios Amoiridis

[permalink] [raw]
Subject: [PATCH v2 14/19] iio: chemical: bme680: Make error checks consistent

In the majority of the error checks after a regmap_{read,write}()
operation the following coding style is used:

ret = regmap_{read,write}(data->regmap, ...);
if (ret < 0) {
dev_err(dev, ...);
return ret;
}

In this particular case it was different so change it for consistency.

Signed-off-by: Vasileios Amoiridis <[email protected]>
---
drivers/iio/chemical/bme680_core.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/chemical/bme680_core.c b/drivers/iio/chemical/bme680_core.c
index 3c33c21b5d6a..25d128e1ddcf 100644
--- a/drivers/iio/chemical/bme680_core.c
+++ b/drivers/iio/chemical/bme680_core.c
@@ -517,10 +517,12 @@ static int bme680_chip_config(struct bme680_data *data)
ret = regmap_write_bits(data->regmap, BME680_REG_CTRL_MEAS,
BME680_OSRS_TEMP_MASK | BME680_OSRS_PRESS_MASK,
osrs);
- if (ret < 0)
+ if (ret < 0) {
dev_err(dev, "failed to write ctrl_meas register\n");
+ return ret;
+ }

- return ret;
+ return 0;
}

static int bme680_gas_config(struct bme680_data *data)
--
2.25.1


2024-06-06 21:38:03

by Vasileios Amoiridis

[permalink] [raw]
Subject: [PATCH v2 16/19] iio: chemical: bme680: Move probe errors to dev_err_probe()

There are multiple cases in the probe function that dev_err_probe()
fits the needs, so use it.

Signed-off-by: Vasileios Amoiridis <[email protected]>
---
drivers/iio/chemical/bme680_core.c | 29 +++++++++++------------------
1 file changed, 11 insertions(+), 18 deletions(-)

diff --git a/drivers/iio/chemical/bme680_core.c b/drivers/iio/chemical/bme680_core.c
index e354eaa34d59..1cf375904b8d 100644
--- a/drivers/iio/chemical/bme680_core.c
+++ b/drivers/iio/chemical/bme680_core.c
@@ -866,18 +866,14 @@ int bme680_core_probe(struct device *dev, struct regmap *regmap,

ret = regmap_write(regmap, BME680_REG_SOFT_RESET,
BME680_CMD_SOFTRESET);
- if (ret < 0) {
- dev_err(dev, "Failed to reset chip\n");
- return ret;
- }
+ if (ret < 0)
+ return dev_err_probe(dev, ret, "Failed to reset chip\n");

usleep_range(BME680_STARTUP_TIME_US, BME680_STARTUP_TIME_US + 1000);

ret = regmap_read(regmap, BME680_REG_CHIP_ID, &data->check);
- if (ret < 0) {
- dev_err(dev, "Error reading chip ID\n");
- return ret;
- }
+ if (ret < 0)
+ return dev_err_probe(dev, ret, "Error reading chip ID\n");

if (data->check != BME680_CHIP_ID_VAL) {
dev_err(dev, "Wrong chip ID, got %x expected %x\n",
@@ -887,22 +883,19 @@ int bme680_core_probe(struct device *dev, struct regmap *regmap,

ret = bme680_read_calib(data, &data->bme680);
if (ret < 0) {
- dev_err(dev,
+ return dev_err_probe(dev, ret,
"failed to read calibration coefficients at probe\n");
- return ret;
}

ret = bme680_chip_config(data);
- if (ret < 0) {
- dev_err(dev, "failed to set chip_config data\n");
- return ret;
- }
+ if (ret < 0)
+ return dev_err_probe(dev, ret,
+ "failed to set chip_config data\n");

ret = bme680_gas_config(data);
- if (ret < 0) {
- dev_err(dev, "failed to set gas config data\n");
- return ret;
- }
+ if (ret < 0)
+ return dev_err_probe(dev, ret,
+ "failed to set gas config data\n");

return devm_iio_device_register(dev, indio_dev);
}
--
2.25.1


2024-06-07 16:11:16

by Vasileios Amoiridis

[permalink] [raw]
Subject: Re: [PATCH v2 13/19] iio: chemical: bme680: Add read buffers in DMA safe region

On Thu, Jun 06, 2024 at 11:23:05PM +0200, Vasileios Amoiridis wrote:
> Move the buffers that are used in order to read data from the
> device in a DMA-safe region. Also create defines for the number
> of bytes that are being read from the device and don't use
> magic numbers.
>

Well this commit shouldn't have been here, I changed the title and
forgot to delete it from the folder...

Vasilis

> Signed-off-by: Vasileios Amoiridis <[email protected]>
> ---
> drivers/iio/chemical/bme680.h | 7 +++++
> drivers/iio/chemical/bme680_core.c | 45 +++++++++++++++---------------
> 2 files changed, 29 insertions(+), 23 deletions(-)
>
> diff --git a/drivers/iio/chemical/bme680.h b/drivers/iio/chemical/bme680.h
> index 8d0f53c05d7d..7d0ff294725a 100644
> --- a/drivers/iio/chemical/bme680.h
> +++ b/drivers/iio/chemical/bme680.h
> @@ -56,6 +56,13 @@
> #define BME680_GAS_MEAS_BIT BIT(6)
> #define BME680_MEAS_BIT BIT(5)
>
> +#define BME680_TEMP_NUM_BYTES 3
> +#define BME680_PRESS_NUM_BYTES 3
> +#define BME680_HUMID_NUM_BYTES 2
> +#define BME680_GAS_NUM_BYTES 2
> +
> +#define BME680_MEAS_TRIM_MASK GENMASK(24, 4)
> +
> /* Calibration Parameters */
> #define BME680_T2_LSB_REG 0x8A
> #define BME680_H2_MSB_REG 0xE1
> diff --git a/drivers/iio/chemical/bme680_core.c b/drivers/iio/chemical/bme680_core.c
> index 538696ac4205..99259d0cf13d 100644
> --- a/drivers/iio/chemical/bme680_core.c
> +++ b/drivers/iio/chemical/bme680_core.c
> @@ -115,6 +115,9 @@ struct bme680_data {
> * transfer buffers to live in their own cache lines.
> */
> union {
> + u8 buf[3];
> + unsigned int check;
> + __be16 be16;
> u8 bme680_cal_buf_1[BME680_CALIB_RANGE_1_LEN];
> u8 bme680_cal_buf_2[BME680_CALIB_RANGE_2_LEN];
> u8 bme680_cal_buf_3[BME680_CALIB_RANGE_3_LEN];
> @@ -453,7 +456,6 @@ static u8 bme680_oversampling_to_reg(u8 val)
> static int bme680_wait_for_eoc(struct bme680_data *data)
> {
> struct device *dev = regmap_get_device(data->regmap);
> - unsigned int check;
> int ret;
> /*
> * (Sum of oversampling ratios * time per oversampling) +
> @@ -466,16 +468,16 @@ static int bme680_wait_for_eoc(struct bme680_data *data)
>
> usleep_range(wait_eoc_us, wait_eoc_us + 100);
>
> - ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &check);
> + ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &data->check);
> if (ret) {
> dev_err(dev, "failed to read measurement status register.\n");
> return ret;
> }
> - if (check & BME680_MEAS_BIT) {
> + if (data->check & BME680_MEAS_BIT) {
> dev_err(dev, "Device measurement cycle incomplete.\n");
> return -EBUSY;
> }
> - if (!(check & BME680_NEW_DATA_BIT)) {
> + if (!(data->check & BME680_NEW_DATA_BIT)) {
> dev_err(dev, "No new data available from the device.\n");
> return -ENODATA;
> }
> @@ -564,7 +566,6 @@ static int bme680_read_temp(struct bme680_data *data, int *val)
> {
> struct device *dev = regmap_get_device(data->regmap);
> int ret;
> - __be32 tmp = 0;
> u32 adc_temp;
> s16 comp_temp;
>
> @@ -578,13 +579,14 @@ static int bme680_read_temp(struct bme680_data *data, int *val)
> return ret;
>
> ret = regmap_bulk_read(data->regmap, BME680_REG_TEMP_MSB,
> - &tmp, 3);
> + data->buf, BME680_TEMP_NUM_BYTES);
> if (ret < 0) {
> dev_err(dev, "failed to read temperature\n");
> return ret;
> }
>
> - adc_temp = be32_to_cpu(tmp) >> 12;
> + adc_temp = FIELD_GET(BME680_MEAS_TRIM_MASK,
> + get_unaligned_be24(data->buf));
> if (adc_temp == BME680_MEAS_SKIPPED) {
> /* reading was skipped */
> dev_err(dev, "reading temperature skipped\n");
> @@ -610,7 +612,6 @@ static int bme680_read_press(struct bme680_data *data,
> {
> struct device *dev = regmap_get_device(data->regmap);
> int ret;
> - __be32 tmp = 0;
> u32 adc_press;
>
> /* Read and compensate temperature to get a reading of t_fine */
> @@ -619,13 +620,14 @@ static int bme680_read_press(struct bme680_data *data,
> return ret;
>
> ret = regmap_bulk_read(data->regmap, BME680_REG_PRESS_MSB,
> - &tmp, 3);
> + data->buf, BME680_PRESS_NUM_BYTES);
> if (ret < 0) {
> dev_err(dev, "failed to read pressure\n");
> return ret;
> }
>
> - adc_press = be32_to_cpu(tmp) >> 12;
> + adc_press = FIELD_GET(BME680_MEAS_TRIM_MASK,
> + get_unaligned_be24(data->buf));
> if (adc_press == BME680_MEAS_SKIPPED) {
> /* reading was skipped */
> dev_err(dev, "reading pressure skipped\n");
> @@ -642,7 +644,6 @@ static int bme680_read_humid(struct bme680_data *data,
> {
> struct device *dev = regmap_get_device(data->regmap);
> int ret;
> - __be16 tmp = 0;
> u16 adc_humidity;
> u32 comp_humidity;
>
> @@ -652,13 +653,13 @@ static int bme680_read_humid(struct bme680_data *data,
> return ret;
>
> ret = regmap_bulk_read(data->regmap, BME680_REG_HUMIDITY_MSB,
> - &tmp, sizeof(tmp));
> + &data->be16, BME680_HUMID_NUM_BYTES);
> if (ret < 0) {
> dev_err(dev, "failed to read humidity\n");
> return ret;
> }
>
> - adc_humidity = be16_to_cpu(tmp);
> + adc_humidity = be16_to_cpu(data->be16);
> if (adc_humidity == BME680_MEAS_SKIPPED) {
> /* reading was skipped */
> dev_err(dev, "reading humidity skipped\n");
> @@ -676,8 +677,6 @@ static int bme680_read_gas(struct bme680_data *data,
> {
> struct device *dev = regmap_get_device(data->regmap);
> int ret;
> - __be16 tmp = 0;
> - unsigned int check;
> u16 adc_gas_res, gas_regs_val;
> u8 gas_range;
>
> @@ -697,19 +696,20 @@ static int bme680_read_gas(struct bme680_data *data,
> if (ret)
> return ret;
>
> - ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &check);
> - if (check & BME680_GAS_MEAS_BIT) {
> + ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &data->check);
> + if (data->check & BME680_GAS_MEAS_BIT) {
> dev_err(dev, "gas measurement incomplete\n");
> return -EBUSY;
> }
>
> ret = regmap_bulk_read(data->regmap, BME680_REG_GAS_MSB,
> - &tmp, sizeof(tmp));
> + &data->be16, BME680_GAS_NUM_BYTES);
> if (ret < 0) {
> dev_err(dev, "failed to read gas resistance\n");
> return ret;
> }
> - gas_regs_val = be16_to_cpu(tmp);
> +
> + gas_regs_val = be16_to_cpu(data->be16);
> adc_gas_res = FIELD_GET(BME680_ADC_GAS_RES, gas_regs_val);
>
> /*
> @@ -838,7 +838,6 @@ int bme680_core_probe(struct device *dev, struct regmap *regmap,
> {
> struct iio_dev *indio_dev;
> struct bme680_data *data;
> - unsigned int val;
> int ret;
>
> indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
> @@ -869,15 +868,15 @@ int bme680_core_probe(struct device *dev, struct regmap *regmap,
> return ret;
> }
>
> - ret = regmap_read(regmap, BME680_REG_CHIP_ID, &val);
> + ret = regmap_read(regmap, BME680_REG_CHIP_ID, &data->check);
> if (ret < 0) {
> dev_err(dev, "Error reading chip ID\n");
> return ret;
> }
>
> - if (val != BME680_CHIP_ID_VAL) {
> + if (data->check != BME680_CHIP_ID_VAL) {
> dev_err(dev, "Wrong chip ID, got %x expected %x\n",
> - val, BME680_CHIP_ID_VAL);
> + data->check, BME680_CHIP_ID_VAL);
> return -ENODEV;
> }
>
> --
> 2.25.1
>

2024-06-09 11:01:35

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v2 02/19] iio: chemical: bme680: Fix calibration data variable

On Thu, 6 Jun 2024 23:22:54 +0200
Vasileios Amoiridis <[email protected]> wrote:

> According to the BME68x Sensor API [1], the h6 calibration
> data variable should be an unsigned integer of size 8.
>
> [1]: https://github.com/boschsensortec/BME68x_SensorAPI/blob/v4.4.8/bme68x_defs.h#L789
> Fixes: 1b3bd8592780 ("iio: chemical: Add support for Bosch BME680 sensor")
> Signed-off-by: Vasileios Amoiridis <[email protected]>
Applied to the fixes-togreg branch of iio.git and marked for stable.
> ---
> drivers/iio/chemical/bme680_core.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/iio/chemical/bme680_core.c b/drivers/iio/chemical/bme680_core.c
> index 2c40c13fe97a..812829841733 100644
> --- a/drivers/iio/chemical/bme680_core.c
> +++ b/drivers/iio/chemical/bme680_core.c
> @@ -38,7 +38,7 @@ struct bme680_calib {
> s8 par_h3;
> s8 par_h4;
> s8 par_h5;
> - s8 par_h6;
> + u8 par_h6;
> s8 par_h7;
> s8 par_gh1;
> s16 par_gh2;