Re: [PATCH v2 2/5] iio: pressure: bmp280: Add preinit callback

Jonathan Cameron
2022-12-30T18:05:27Z

On Mon, 26 Dec 2022 15:29:21 +0100
Angel Iglesias <ang.iglesiasg@gmail.com> wrote:

> Adds preinit callback to execute operations on probe before applying
> initial configuration.
>
> Signed-off-by: Angel Iglesias <ang.iglesiasg@gmail.com>
>
> diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c
> index 46959a91408f..c37cf2caec68 100644
> --- a/drivers/iio/pressure/bmp280-core.c
> +++ b/drivers/iio/pressure/bmp280-core.c
> @@ -217,6 +217,7 @@ struct bmp280_chip_info {
> int (*read_press)(struct bmp280_data *, int *, int *);
> int (*read_humid)(struct bmp280_data *, int *, int *);
> int (*read_calib)(struct bmp280_data *);
> + int (*preinit)(struct bmp280_data *);
> };
>
> /*
> @@ -935,6 +936,7 @@ static const struct bmp280_chip_info bmp280_chip_info = {
> .read_temp = bmp280_read_temp,
> .read_press = bmp280_read_press,
> .read_calib = bmp280_read_calib,
> + .preinit = NULL,
C standard guarantees those are set to NULL anyway + the default is obvious.
Hence don't set them to NULL, just leave the automatic initialization of
unspecified structure elements to handle it for you.

> };
>
> static int bme280_chip_config(struct bmp280_data *data)
> @@ -979,6 +981,7 @@ static const struct bmp280_chip_info bme280_chip_info = {
> .read_press = bmp280_read_press,
> .read_humid = bmp280_read_humid,
> .read_calib = bme280_read_calib,
> + .preinit = NULL,
> };
>
> /*
> @@ -1220,6 +1223,12 @@ static const int bmp380_odr_table[][2] = {
> [BMP380_ODR_0_0015HZ] = {0, 1526},
> };
>
> +static int bmp380_preinit(struct bmp280_data *data)
> +{
> + /* BMP3xx requires soft-reset as part of initialization */
> + return bmp380_cmd(data, BMP380_CMD_SOFT_RESET);
> +}
> +
> static int bmp380_chip_config(struct bmp280_data *data)
> {
> bool change = false, aux;
> @@ -1349,6 +1358,7 @@ static const struct bmp280_chip_info bmp380_chip_info = {
> .read_temp = bmp380_read_temp,
> .read_press = bmp380_read_press,
> .read_calib = bmp380_read_calib,
> + .preinit = bmp380_preinit,
> };
>
> static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
> @@ -1604,6 +1614,7 @@ static const struct bmp280_chip_info bmp180_chip_info = {
> .read_temp = bmp180_read_temp,
> .read_press = bmp180_read_press,
> .read_calib = bmp180_read_calib,
> + .preinit = NULL,
> };
>
> static irqreturn_t bmp085_eoc_irq(int irq, void *d)
> @@ -1762,9 +1773,13 @@ int bmp280_common_probe(struct device *dev,
> return -EINVAL;
> }
>
> - /* BMP3xx requires soft-reset as part of initialization */
> - if (chip_id == BMP380_CHIP_ID) {
> - ret = bmp380_cmd(data, BMP380_CMD_SOFT_RESET);
> + /*
> + * Some chips like the BMP3xx have preinit tasks to run
> + * before applying the initial configuration.
> + */
I would drop this comment. It's kind of obvious that some devices need you
to call something here - otherwise why have the clearly optional callback?
The specific BMP3xx requirements are well commented in your new callback above
so don't want to be here as well.

> + if (data->chip_info->preinit) {
> + ret = data->chip_info->preinit(data);
> + dev_err(dev, "error running preinit tasks");

Error message printed on success...

> if (ret < 0)
> return ret;

return dev_err_probe(dev, ret, "error running preinit tasks");

> }