diff mbox series

[v3] iio:imu:bmi160: disable regulator in error path

Message ID 20220327154005.806049-1-ztong0001@gmail.com (mailing list archive)
State Accepted
Headers show
Series [v3] iio:imu:bmi160: disable regulator in error path | expand

Commit Message

Tong Zhang March 27, 2022, 3:40 p.m. UTC
Regulator should be disabled in error path as mentioned in _regulator_put().
Also disable accel if gyro cannot be enabled.

[   16.233604] WARNING: CPU: 0 PID: 2177 at drivers/regulator/core.c:2257 _regulator_put
[   16.240453] Call Trace:
[   16.240572]  <TASK>
[   16.240676]  regulator_put+0x26/0x40
[   16.240853]  regulator_bulk_free+0x26/0x50
[   16.241050]  release_nodes+0x3f/0x70
[   16.241225]  devres_release_group+0x147/0x1c0
[   16.241441]  ? bmi160_core_probe+0x175/0x3a0 [bmi160_core]

Fixes: 5dea3fb066f0 ("iio: imu: bmi160: added regulator support")
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Tong Zhang <ztong0001@gmail.com>
---
v2: also disable accel when gyro fail to enable
v3: add tag
 drivers/iio/imu/bmi160/bmi160_core.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

Comments

Jonathan Cameron March 27, 2022, 3:53 p.m. UTC | #1
On Sun, 27 Mar 2022 08:40:05 -0700
Tong Zhang <ztong0001@gmail.com> wrote:

> Regulator should be disabled in error path as mentioned in _regulator_put().
> Also disable accel if gyro cannot be enabled.
> 
> [   16.233604] WARNING: CPU: 0 PID: 2177 at drivers/regulator/core.c:2257 _regulator_put
> [   16.240453] Call Trace:
> [   16.240572]  <TASK>
> [   16.240676]  regulator_put+0x26/0x40
> [   16.240853]  regulator_bulk_free+0x26/0x50
> [   16.241050]  release_nodes+0x3f/0x70
> [   16.241225]  devres_release_group+0x147/0x1c0
> [   16.241441]  ? bmi160_core_probe+0x175/0x3a0 [bmi160_core]
> 
> Fixes: 5dea3fb066f0 ("iio: imu: bmi160: added regulator support")
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Signed-off-by: Tong Zhang <ztong0001@gmail.com>
oops. Raced with you ;)

Anyhow, I added the tag, so no problem.

Jonathan

> ---
> v2: also disable accel when gyro fail to enable
> v3: add tag
>  drivers/iio/imu/bmi160/bmi160_core.c | 20 ++++++++++++++------
>  1 file changed, 14 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/iio/imu/bmi160/bmi160_core.c b/drivers/iio/imu/bmi160/bmi160_core.c
> index 824b5124a5f5..01336105792e 100644
> --- a/drivers/iio/imu/bmi160/bmi160_core.c
> +++ b/drivers/iio/imu/bmi160/bmi160_core.c
> @@ -730,7 +730,7 @@ static int bmi160_chip_init(struct bmi160_data *data, bool use_spi)
>  
>  	ret = regmap_write(data->regmap, BMI160_REG_CMD, BMI160_CMD_SOFTRESET);
>  	if (ret)
> -		return ret;
> +		goto disable_regulator;
>  
>  	usleep_range(BMI160_SOFTRESET_USLEEP, BMI160_SOFTRESET_USLEEP + 1);
>  
> @@ -741,29 +741,37 @@ static int bmi160_chip_init(struct bmi160_data *data, bool use_spi)
>  	if (use_spi) {
>  		ret = regmap_read(data->regmap, BMI160_REG_DUMMY, &val);
>  		if (ret)
> -			return ret;
> +			goto disable_regulator;
>  	}
>  
>  	ret = regmap_read(data->regmap, BMI160_REG_CHIP_ID, &val);
>  	if (ret) {
>  		dev_err(dev, "Error reading chip id\n");
> -		return ret;
> +		goto disable_regulator;
>  	}
>  	if (val != BMI160_CHIP_ID_VAL) {
>  		dev_err(dev, "Wrong chip id, got %x expected %x\n",
>  			val, BMI160_CHIP_ID_VAL);
> -		return -ENODEV;
> +		ret = -ENODEV;
> +		goto disable_regulator;
>  	}
>  
>  	ret = bmi160_set_mode(data, BMI160_ACCEL, true);
>  	if (ret)
> -		return ret;
> +		goto disable_regulator;
>  
>  	ret = bmi160_set_mode(data, BMI160_GYRO, true);
>  	if (ret)
> -		return ret;
> +		goto disable_accel;
>  
>  	return 0;
> +
> +disable_accel:
> +	bmi160_set_mode(data, BMI160_ACCEL, false);
> +
> +disable_regulator:
> +	regulator_bulk_disable(ARRAY_SIZE(data->supplies), data->supplies);
> +	return ret;
>  }
>  
>  static int bmi160_data_rdy_trigger_set_state(struct iio_trigger *trig,
Tong Zhang March 27, 2022, 3:58 p.m. UTC | #2
On Sun, Mar 27, 2022 at 8:46 AM Jonathan Cameron <jic23@kernel.org> wrote:
>
> On Sun, 27 Mar 2022 08:40:05 -0700
> Tong Zhang <ztong0001@gmail.com> wrote:
>
> > Regulator should be disabled in error path as mentioned in _regulator_put().
> > Also disable accel if gyro cannot be enabled.
> >
> > [   16.233604] WARNING: CPU: 0 PID: 2177 at drivers/regulator/core.c:2257 _regulator_put
> > [   16.240453] Call Trace:
> > [   16.240572]  <TASK>
> > [   16.240676]  regulator_put+0x26/0x40
> > [   16.240853]  regulator_bulk_free+0x26/0x50
> > [   16.241050]  release_nodes+0x3f/0x70
> > [   16.241225]  devres_release_group+0x147/0x1c0
> > [   16.241441]  ? bmi160_core_probe+0x175/0x3a0 [bmi160_core]
> >
> > Fixes: 5dea3fb066f0 ("iio: imu: bmi160: added regulator support")
> > Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> > Signed-off-by: Tong Zhang <ztong0001@gmail.com>
> oops. Raced with you ;)
>
> Anyhow, I added the tag, so no problem.
>
> Jonathan

No problem. Have a nice weekend!:-)
- Tong

>
> > ---
> > v2: also disable accel when gyro fail to enable
> > v3: add tag
> >  drivers/iio/imu/bmi160/bmi160_core.c | 20 ++++++++++++++------
> >  1 file changed, 14 insertions(+), 6 deletions(-)
> >
> > diff --git a/drivers/iio/imu/bmi160/bmi160_core.c b/drivers/iio/imu/bmi160/bmi160_core.c
> > index 824b5124a5f5..01336105792e 100644
> > --- a/drivers/iio/imu/bmi160/bmi160_core.c
> > +++ b/drivers/iio/imu/bmi160/bmi160_core.c
> > @@ -730,7 +730,7 @@ static int bmi160_chip_init(struct bmi160_data *data, bool use_spi)
> >
> >       ret = regmap_write(data->regmap, BMI160_REG_CMD, BMI160_CMD_SOFTRESET);
> >       if (ret)
> > -             return ret;
> > +             goto disable_regulator;
> >
> >       usleep_range(BMI160_SOFTRESET_USLEEP, BMI160_SOFTRESET_USLEEP + 1);
> >
> > @@ -741,29 +741,37 @@ static int bmi160_chip_init(struct bmi160_data *data, bool use_spi)
> >       if (use_spi) {
> >               ret = regmap_read(data->regmap, BMI160_REG_DUMMY, &val);
> >               if (ret)
> > -                     return ret;
> > +                     goto disable_regulator;
> >       }
> >
> >       ret = regmap_read(data->regmap, BMI160_REG_CHIP_ID, &val);
> >       if (ret) {
> >               dev_err(dev, "Error reading chip id\n");
> > -             return ret;
> > +             goto disable_regulator;
> >       }
> >       if (val != BMI160_CHIP_ID_VAL) {
> >               dev_err(dev, "Wrong chip id, got %x expected %x\n",
> >                       val, BMI160_CHIP_ID_VAL);
> > -             return -ENODEV;
> > +             ret = -ENODEV;
> > +             goto disable_regulator;
> >       }
> >
> >       ret = bmi160_set_mode(data, BMI160_ACCEL, true);
> >       if (ret)
> > -             return ret;
> > +             goto disable_regulator;
> >
> >       ret = bmi160_set_mode(data, BMI160_GYRO, true);
> >       if (ret)
> > -             return ret;
> > +             goto disable_accel;
> >
> >       return 0;
> > +
> > +disable_accel:
> > +     bmi160_set_mode(data, BMI160_ACCEL, false);
> > +
> > +disable_regulator:
> > +     regulator_bulk_disable(ARRAY_SIZE(data->supplies), data->supplies);
> > +     return ret;
> >  }
> >
> >  static int bmi160_data_rdy_trigger_set_state(struct iio_trigger *trig,
>
Jonathan Cameron March 27, 2022, 4:02 p.m. UTC | #3
On Sun, 27 Mar 2022 16:53:36 +0100
Jonathan Cameron <jic23@kernel.org> wrote:

> On Sun, 27 Mar 2022 08:40:05 -0700
> Tong Zhang <ztong0001@gmail.com> wrote:
> 
> > Regulator should be disabled in error path as mentioned in _regulator_put().
> > Also disable accel if gyro cannot be enabled.
> > 
> > [   16.233604] WARNING: CPU: 0 PID: 2177 at drivers/regulator/core.c:2257 _regulator_put
> > [   16.240453] Call Trace:
> > [   16.240572]  <TASK>
> > [   16.240676]  regulator_put+0x26/0x40
> > [   16.240853]  regulator_bulk_free+0x26/0x50
> > [   16.241050]  release_nodes+0x3f/0x70
> > [   16.241225]  devres_release_group+0x147/0x1c0
> > [   16.241441]  ? bmi160_core_probe+0x175/0x3a0 [bmi160_core]
> > 
> > Fixes: 5dea3fb066f0 ("iio: imu: bmi160: added regulator support")
> > Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> > Signed-off-by: Tong Zhang <ztong0001@gmail.com>  
> oops. Raced with you ;)
> 
> Anyhow, I added the tag, so no problem.

on closer inspection b4 was clever and picked v3 up anyway.

J
> 
> Jonathan
> 
> > ---
> > v2: also disable accel when gyro fail to enable
> > v3: add tag
> >  drivers/iio/imu/bmi160/bmi160_core.c | 20 ++++++++++++++------
> >  1 file changed, 14 insertions(+), 6 deletions(-)
> > 
> > diff --git a/drivers/iio/imu/bmi160/bmi160_core.c b/drivers/iio/imu/bmi160/bmi160_core.c
> > index 824b5124a5f5..01336105792e 100644
> > --- a/drivers/iio/imu/bmi160/bmi160_core.c
> > +++ b/drivers/iio/imu/bmi160/bmi160_core.c
> > @@ -730,7 +730,7 @@ static int bmi160_chip_init(struct bmi160_data *data, bool use_spi)
> >  
> >  	ret = regmap_write(data->regmap, BMI160_REG_CMD, BMI160_CMD_SOFTRESET);
> >  	if (ret)
> > -		return ret;
> > +		goto disable_regulator;
> >  
> >  	usleep_range(BMI160_SOFTRESET_USLEEP, BMI160_SOFTRESET_USLEEP + 1);
> >  
> > @@ -741,29 +741,37 @@ static int bmi160_chip_init(struct bmi160_data *data, bool use_spi)
> >  	if (use_spi) {
> >  		ret = regmap_read(data->regmap, BMI160_REG_DUMMY, &val);
> >  		if (ret)
> > -			return ret;
> > +			goto disable_regulator;
> >  	}
> >  
> >  	ret = regmap_read(data->regmap, BMI160_REG_CHIP_ID, &val);
> >  	if (ret) {
> >  		dev_err(dev, "Error reading chip id\n");
> > -		return ret;
> > +		goto disable_regulator;
> >  	}
> >  	if (val != BMI160_CHIP_ID_VAL) {
> >  		dev_err(dev, "Wrong chip id, got %x expected %x\n",
> >  			val, BMI160_CHIP_ID_VAL);
> > -		return -ENODEV;
> > +		ret = -ENODEV;
> > +		goto disable_regulator;
> >  	}
> >  
> >  	ret = bmi160_set_mode(data, BMI160_ACCEL, true);
> >  	if (ret)
> > -		return ret;
> > +		goto disable_regulator;
> >  
> >  	ret = bmi160_set_mode(data, BMI160_GYRO, true);
> >  	if (ret)
> > -		return ret;
> > +		goto disable_accel;
> >  
> >  	return 0;
> > +
> > +disable_accel:
> > +	bmi160_set_mode(data, BMI160_ACCEL, false);
> > +
> > +disable_regulator:
> > +	regulator_bulk_disable(ARRAY_SIZE(data->supplies), data->supplies);
> > +	return ret;
> >  }
> >  
> >  static int bmi160_data_rdy_trigger_set_state(struct iio_trigger *trig,  
>
diff mbox series

Patch

diff --git a/drivers/iio/imu/bmi160/bmi160_core.c b/drivers/iio/imu/bmi160/bmi160_core.c
index 824b5124a5f5..01336105792e 100644
--- a/drivers/iio/imu/bmi160/bmi160_core.c
+++ b/drivers/iio/imu/bmi160/bmi160_core.c
@@ -730,7 +730,7 @@  static int bmi160_chip_init(struct bmi160_data *data, bool use_spi)
 
 	ret = regmap_write(data->regmap, BMI160_REG_CMD, BMI160_CMD_SOFTRESET);
 	if (ret)
-		return ret;
+		goto disable_regulator;
 
 	usleep_range(BMI160_SOFTRESET_USLEEP, BMI160_SOFTRESET_USLEEP + 1);
 
@@ -741,29 +741,37 @@  static int bmi160_chip_init(struct bmi160_data *data, bool use_spi)
 	if (use_spi) {
 		ret = regmap_read(data->regmap, BMI160_REG_DUMMY, &val);
 		if (ret)
-			return ret;
+			goto disable_regulator;
 	}
 
 	ret = regmap_read(data->regmap, BMI160_REG_CHIP_ID, &val);
 	if (ret) {
 		dev_err(dev, "Error reading chip id\n");
-		return ret;
+		goto disable_regulator;
 	}
 	if (val != BMI160_CHIP_ID_VAL) {
 		dev_err(dev, "Wrong chip id, got %x expected %x\n",
 			val, BMI160_CHIP_ID_VAL);
-		return -ENODEV;
+		ret = -ENODEV;
+		goto disable_regulator;
 	}
 
 	ret = bmi160_set_mode(data, BMI160_ACCEL, true);
 	if (ret)
-		return ret;
+		goto disable_regulator;
 
 	ret = bmi160_set_mode(data, BMI160_GYRO, true);
 	if (ret)
-		return ret;
+		goto disable_accel;
 
 	return 0;
+
+disable_accel:
+	bmi160_set_mode(data, BMI160_ACCEL, false);
+
+disable_regulator:
+	regulator_bulk_disable(ARRAY_SIZE(data->supplies), data->supplies);
+	return ret;
 }
 
 static int bmi160_data_rdy_trigger_set_state(struct iio_trigger *trig,