2022-07-23 17:50:27

by Angel Iglesias

[permalink] [raw]
Subject: [PATCH v4 0/5] Add support for pressure sensor Bosch BMP380

This patchset adds BMP380 variant to the already existing drivers for
the Bosch BMP180/280 pressure sensors.

Patch 1 adds references and sensor id to the devicetree bindings docs.
Patch 2 is a minor refactor simplifying driver initialization logic
to facilitate the integration of the new sensor variant extending the
information stored in the "chip_info" struct.
Patch 3 fixes DMA unsafe regmap_bulk_* calls reported by Jonathan Cameron
<[email protected]>.
Patch 4 adds the basic logic to initialize and read measurements from
the sensor.
Patch 5 adds advanced configurable features such as sampling frequency
and IIR filter through the IIO sysfs ABI.

Changes in v4:
- Patch 4 and 2: Merged v3 patch 2 (Kconfig refs update) into this patch.
- Patch 3: Added patch fixing unsafe DMA regmap_bulk_* calls reported by
Jonathan Cameron <[email protected]>.
- Patch 4: Fixed DMA unsafe buffers used on regmap_bulk_* calls reported
by Jonathan Cameron <[email protected]>.

Changes in v3:
- Patch 2: Fixed incorrect abbreviation.
- Patch 3: use dev_err_probe helper to handle error initializing sensor.
- Patch 4: Fixed kernel test robot warning provoked by missing include.
- Patch 4: Fixed bug reported by Dan Carpenter <[email protected]>.
- Patch 5: Fixed formatting and typos on multiple comments.
- Patch 5: Fixed missing boolean initialization reported by
Andy Shevchenko <[email protected]>.
- Patch 5: Replaced duplicated comments with a single comment containing
a brief explantation in a shared location.
- Patch 5: Dropped incorrect use of unlikely macro.

Changes in v2:
- Added patch 2 updating Kconfig with references to new sensor.
- Patch 3 adds changes proposed by Jonathan Cameron <[email protected]>
to declutter and unify configuration logic for the different sensors
extending "chip_info" struct with default configuration parameters.
- Patch 4: store temperature and pressure adc values on 3 byte array
instead of using the type __le32. Uses function get_unaligned_le24
to convert the little-endian encoded 3 byte value to an integer.
- Patch 4: drops custom macro le16_from_bytes and use get_unaligned_le16.
- Patch 4: generate masks using GENMASK macro.
- Patch 4: use FIELD_PREP to generate bitfields for registries.
- Patch 4: dropped stray formatting change.
- Patch 5: adds sanity checks in bmp280_read_raw for channel properties
only available in the BMP380.
- Patch 5: on bmp280_write_* checks if a problem ocurred committing new
configuration and tries to restore previous working configuration
to keep the sensor in a previous working state.
- Patch 5: refactored bmp380_chip_config to only check for configuration
errors when a configuration change is detected.
- Patch 5: improved invalid configuration detection on BMP380 restarting
measurement loop to force a new measurement after the configuration is
updated.

Angel Iglesias (5):
dt-bindings: iio: pressure: bmp085: Add BMP380 compatible string
iio: pressure: bmp280: simplify driver initialization logic
iio: pressure: bmp280: Fix alignment for DMA safety
iio: pressure: bmp280: Add support for BMP380 sensor family
iio: pressure: bmp280: Add more tunable config parameters for BMP380

.../bindings/iio/pressure/bmp085.yaml | 4 +-
drivers/iio/pressure/Kconfig | 6 +-
drivers/iio/pressure/bmp280-core.c | 884 ++++++++++++++++--
drivers/iio/pressure/bmp280-i2c.c | 5 +
drivers/iio/pressure/bmp280-regmap.c | 55 ++
drivers/iio/pressure/bmp280-spi.c | 5 +
drivers/iio/pressure/bmp280.h | 83 ++
7 files changed, 937 insertions(+), 105 deletions(-)


base-commit: 180c6cb6b9b79c55b79e8414f4c0208f2463af7d
--
2.37.1


2022-07-23 18:00:26

by Angel Iglesias

[permalink] [raw]
Subject: [PATCH v4 2/5] iio: pressure: bmp280: simplify driver initialization logic

Simplified common initialization logic of different sensor types
unifying calibration and initial configuration recovery.

Default config param values of each sensor type are stored inside
chip_info structure and used to initialize sensor data struct instance.

The auxiliar functions for read each sensor type calibration are converted
to a callback available on the chip_info struct.

Suggested-by: Jonathan Cameron <[email protected]>
Signed-off-by: Angel Iglesias <[email protected]>
---
drivers/iio/pressure/bmp280-core.c | 91 ++++++++++++++++++------------
1 file changed, 54 insertions(+), 37 deletions(-)

diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c
index fe7aa81e7cc9..60fba199c7a0 100644
--- a/drivers/iio/pressure/bmp280-core.c
+++ b/drivers/iio/pressure/bmp280-core.c
@@ -107,19 +107,28 @@ struct bmp280_data {
};

struct bmp280_chip_info {
+ unsigned int id_reg;
+
+ int num_channels;
+ unsigned int start_up_time;
+
const int *oversampling_temp_avail;
int num_oversampling_temp_avail;
+ int oversampling_temp_default;

const int *oversampling_press_avail;
int num_oversampling_press_avail;
+ int oversampling_press_default;

const int *oversampling_humid_avail;
int num_oversampling_humid_avail;
+ int oversampling_humid_default;

int (*chip_config)(struct bmp280_data *);
int (*read_temp)(struct bmp280_data *, int *);
int (*read_press)(struct bmp280_data *, int *, int *);
int (*read_humid)(struct bmp280_data *, int *, int *);
+ int (*read_calib)(struct bmp280_data *, unsigned int);
};

/*
@@ -147,15 +156,14 @@ static const struct iio_chan_spec bmp280_channels[] = {
},
};

-static int bmp280_read_calib(struct bmp280_data *data,
- struct bmp280_calib *calib,
- unsigned int chip)
+static int bmp280_read_calib(struct bmp280_data *data, unsigned int chip)
{
int ret;
unsigned int tmp;
__le16 l16;
__be16 b16;
struct device *dev = data->dev;
+ struct bmp280_calib *calib = &data->calib.bmp280;
__le16 t_buf[BMP280_COMP_TEMP_REG_COUNT / 2];
__le16 p_buf[BMP280_COMP_PRESS_REG_COUNT / 2];

@@ -640,15 +648,22 @@ static int bmp280_chip_config(struct bmp280_data *data)
static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };

static const struct bmp280_chip_info bmp280_chip_info = {
+ .id_reg = BMP280_REG_ID,
+ .start_up_time = 2000,
+ .num_channels = 2,
+
.oversampling_temp_avail = bmp280_oversampling_avail,
.num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
+ .oversampling_temp_default = ilog2(2),

.oversampling_press_avail = bmp280_oversampling_avail,
.num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
+ .oversampling_press_default = ilog2(16),

.chip_config = bmp280_chip_config,
.read_temp = bmp280_read_temp,
.read_press = bmp280_read_press,
+ .read_calib = bmp280_read_calib,
};

static int bme280_chip_config(struct bmp280_data *data)
@@ -670,19 +685,27 @@ static int bme280_chip_config(struct bmp280_data *data)
}

static const struct bmp280_chip_info bme280_chip_info = {
+ .id_reg = BMP280_REG_ID,
+ .start_up_time = 2000,
+ .num_channels = 3,
+
.oversampling_temp_avail = bmp280_oversampling_avail,
.num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
+ .oversampling_temp_default = ilog2(2),

.oversampling_press_avail = bmp280_oversampling_avail,
.num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
+ .oversampling_press_default = ilog2(16),

.oversampling_humid_avail = bmp280_oversampling_avail,
.num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
+ .oversampling_humid_default = ilog2(16),

.chip_config = bme280_chip_config,
.read_temp = bmp280_read_temp,
.read_press = bmp280_read_press,
.read_humid = bmp280_read_humid,
+ .read_calib = bmp280_read_calib,
};

static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
@@ -748,11 +771,11 @@ static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
return 0;
}

-static int bmp180_read_calib(struct bmp280_data *data,
- struct bmp180_calib *calib)
+static int bmp180_read_calib(struct bmp280_data *data, unsigned int chip)
{
int ret;
int i;
+ struct bmp180_calib *calib = &data->calib.bmp180;
__be16 buf[BMP180_REG_CALIB_COUNT / 2];

ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
@@ -913,17 +936,24 @@ static const int bmp180_oversampling_temp_avail[] = { 1 };
static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };

static const struct bmp280_chip_info bmp180_chip_info = {
+ .id_reg = BMP280_REG_ID,
+ .start_up_time = 2000,
+ .num_channels = 2,
+
.oversampling_temp_avail = bmp180_oversampling_temp_avail,
.num_oversampling_temp_avail =
ARRAY_SIZE(bmp180_oversampling_temp_avail),
+ .oversampling_temp_default = ilog2(1),

.oversampling_press_avail = bmp180_oversampling_press_avail,
.num_oversampling_press_avail =
ARRAY_SIZE(bmp180_oversampling_press_avail),
+ .oversampling_press_default = ilog2(8),

.chip_config = bmp180_chip_config,
.read_temp = bmp180_read_temp,
.read_press = bmp180_read_press,
+ .read_calib = bmp180_read_calib,
};

static irqreturn_t bmp085_eoc_irq(int irq, void *d)
@@ -993,6 +1023,7 @@ int bmp280_common_probe(struct device *dev,
int ret;
struct iio_dev *indio_dev;
struct bmp280_data *data;
+ const struct bmp280_chip_info *chip_info;
unsigned int chip_id;
struct gpio_desc *gpiod;

@@ -1011,30 +1042,25 @@ int bmp280_common_probe(struct device *dev,

switch (chip) {
case BMP180_CHIP_ID:
- indio_dev->num_channels = 2;
- data->chip_info = &bmp180_chip_info;
- data->oversampling_press = ilog2(8);
- data->oversampling_temp = ilog2(1);
- data->start_up_time = 10000;
+ chip_info = &bmp180_chip_info;
break;
case BMP280_CHIP_ID:
- indio_dev->num_channels = 2;
- data->chip_info = &bmp280_chip_info;
- data->oversampling_press = ilog2(16);
- data->oversampling_temp = ilog2(2);
- data->start_up_time = 2000;
+ chip_info = &bmp280_chip_info;
break;
case BME280_CHIP_ID:
- indio_dev->num_channels = 3;
- data->chip_info = &bme280_chip_info;
- data->oversampling_press = ilog2(16);
- data->oversampling_humid = ilog2(16);
- data->oversampling_temp = ilog2(2);
- data->start_up_time = 2000;
+ chip_info = &bme280_chip_info;
break;
default:
return -EINVAL;
}
+ data->chip_info = chip_info;
+
+ /* apply initial values from chip info structure */
+ indio_dev->num_channels = chip_info->num_channels;
+ data->oversampling_press = chip_info->oversampling_press_default;
+ data->oversampling_humid = chip_info->oversampling_humid_default;
+ data->oversampling_temp = chip_info->oversampling_temp_default;
+ data->start_up_time = chip_info->start_up_time;

/* Bring up regulators */
regulator_bulk_set_supply_names(data->supplies,
@@ -1071,7 +1097,8 @@ int bmp280_common_probe(struct device *dev,
}

data->regmap = regmap;
- ret = regmap_read(regmap, BMP280_REG_ID, &chip_id);
+
+ ret = regmap_read(regmap, data->chip_info->id_reg, &chip_id);
if (ret < 0)
return ret;
if (chip_id != chip) {
@@ -1091,21 +1118,11 @@ int bmp280_common_probe(struct device *dev,
* non-volatile memory during production". Let's read them out at probe
* time once. They will not change.
*/
- if (chip_id == BMP180_CHIP_ID) {
- ret = bmp180_read_calib(data, &data->calib.bmp180);
- if (ret < 0) {
- dev_err(data->dev,
- "failed to read calibration coefficients\n");
- return ret;
- }
- } else if (chip_id == BMP280_CHIP_ID || chip_id == BME280_CHIP_ID) {
- ret = bmp280_read_calib(data, &data->calib.bmp280, chip_id);
- if (ret < 0) {
- dev_err(data->dev,
- "failed to read calibration coefficients\n");
- return ret;
- }
- }
+
+ ret = data->chip_info->read_calib(data, chip_id);
+ if (ret < 0)
+ return dev_err_probe(data->dev, ret,
+ "failed to read calibration coefficients\n");

/*
* Attempt to grab an optional EOC IRQ - only the BMP085 has this
--
2.37.1

2022-07-25 21:56:49

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v4 2/5] iio: pressure: bmp280: simplify driver initialization logic

On Sat, Jul 23, 2022 at 7:40 PM Angel Iglesias <[email protected]> wrote:
>
> Simplified common initialization logic of different sensor types
> unifying calibration and initial configuration recovery.
>
> Default config param values of each sensor type are stored inside
> chip_info structure and used to initialize sensor data struct instance.
>
> The auxiliar functions for read each sensor type calibration are converted
> to a callback available on the chip_info struct.

...

> + .oversampling_temp_default = ilog2(2),
> + .oversampling_press_default = ilog2(16),
> + .oversampling_humid_default = ilog2(16),
> + .oversampling_temp_default = ilog2(1),
> + .oversampling_press_default = ilog2(8),

It's a very interesting way of writing BIT(x)...

--
With Best Regards,
Andy Shevchenko