diff mbox series

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

Message ID 724e92e64e6d91d48d762e804b430c716679bccb.1672062380.git.ang.iglesiasg@gmail.com (mailing list archive)
State Changes Requested
Headers show
Series Add support for pressure sensor Bosch BMP580 | expand

Commit Message

Angel Iglesias Dec. 26, 2022, 2:29 p.m. UTC
Adds preinit callback to execute operations on probe before applying
initial configuration.

Signed-off-by: Angel Iglesias <ang.iglesiasg@gmail.com>

Comments

Andy Shevchenko Dec. 27, 2022, 9:41 p.m. UTC | #1
On Mon, Dec 26, 2022 at 03:29:21PM +0100, Angel Iglesias wrote:
> Adds preinit callback to execute operations on probe before applying
> initial configuration.

...

> @@ -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,
>  };
>  
>  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,
>  };

Useless changes.

...

> @@ -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,
>  };

Ditto.

...

> +	/*
> +	 * Some chips like the BMP3xx have preinit tasks to run
> +	 * before applying the initial configuration.
> +	 */
> +	if (data->chip_info->preinit) {
> +		ret = data->chip_info->preinit(data);

> +		dev_err(dev, "error running preinit tasks");

Huh?! I guess you wanted

>  		if (ret < 0)
>  			return ret;

	if (ret < 0)
		return dev_err_probe(...);

>  	}
Jonathan Cameron Dec. 30, 2022, 6:18 p.m. UTC | #2
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");

>  	}
Angel Iglesias Jan. 1, 2023, 11:06 a.m. UTC | #3
On Tue, 2022-12-27 at 23:41 +0200, Andy Shevchenko wrote:
> On Mon, Dec 26, 2022 at 03:29:21PM +0100, Angel Iglesias wrote:
> > Adds preinit callback to execute operations on probe before applying
> > initial configuration.
> 
> ...
> 
> > @@ -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,
> >  };
> >  
> >  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,
> >  };
> 
> Useless changes.
> 
> ...
> 
> > @@ -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,
> >  };
> 
> Ditto.
> 
> ...
> 
> > +       /*
> > +        * Some chips like the BMP3xx have preinit tasks to run
> > +        * before applying the initial configuration.
> > +        */
> > +       if (data->chip_info->preinit) {
> > +               ret = data->chip_info->preinit(data);
> 
> > +               dev_err(dev, "error running preinit tasks");
> 
> Huh?! I guess you wanted

The dangers of copying paste and rushing things etc. Sorry for this brain fart!

Thanks for your time,
Angel

> >                 if (ret < 0)
> >                         return ret;
> 
>         if (ret < 0)
>                 return dev_err_probe(...);
> 
> >         }
>
Angel Iglesias Jan. 1, 2023, 11:09 a.m. UTC | #4
On Fri, 2022-12-30 at 18:18 +0000, Jonathan Cameron wrote:
> 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.

OK! Note to self: compiler knows best!

> >  };
> >  
> >  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...

Yup, sorry about that...

> 
> >                 if (ret < 0)
> >                         return ret;
> 
>                         return dev_err_probe(dev, ret, "error running preinit
> tasks");
> 
> >         }
> 
Thanks for your time!
Angel
diff mbox series

Patch

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,
 };
 
 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.
+	 */
+	if (data->chip_info->preinit) {
+		ret = data->chip_info->preinit(data);
+		dev_err(dev, "error running preinit tasks");
 		if (ret < 0)
 			return ret;
 	}