Linux IIO development
 help / color / mirror / Atom feed
* [PATCH] iio: imu: bmi160: Fix the error handling at bmi160_chip_init()
@ 2022-05-13 14:35 Zheyu Ma
  2022-05-14 14:36 ` Jonathan Cameron
  0 siblings, 1 reply; 3+ messages in thread
From: Zheyu Ma @ 2022-05-13 14:35 UTC (permalink / raw)
  To: jic23, lars; +Cc: andriy.shevchenko, sean, linux-iio, linux-kernel, Zheyu Ma

When the driver fails in bmi160_chip_init(), it should disable the
regulator.

The following log shows it:

[   18.709662] WARNING: CPU: 0 PID: 294 at drivers/regulator/core.c:2257 _regulator_put+0x3ec/0x4e0
[   18.712069] RIP: 0010:_regulator_put+0x3ec/0x4e0
[   18.716395] Call Trace:
[   18.716522]  <TASK>
[   18.716636]  regulator_bulk_free+0x82/0xe0
[   18.717077]  i2c_device_probe+0x6f0/0x800

Fixes: 5dea3fb066f0 ("iio: imu: bmi160: added regulator support")
Signed-off-by: Zheyu Ma <zheyuma97@gmail.com>
---
 drivers/iio/imu/bmi160/bmi160_core.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/drivers/iio/imu/bmi160/bmi160_core.c b/drivers/iio/imu/bmi160/bmi160_core.c
index 824b5124a5f5..33abfc2bbd7c 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 err_regulator_disable;
 
 	usleep_range(BMI160_SOFTRESET_USLEEP, BMI160_SOFTRESET_USLEEP + 1);
 
@@ -741,29 +741,34 @@ 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 err_regulator_disable;
 	}
 
 	ret = regmap_read(data->regmap, BMI160_REG_CHIP_ID, &val);
 	if (ret) {
 		dev_err(dev, "Error reading chip id\n");
-		return ret;
+		goto err_regulator_disable;
 	}
 	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 err_regulator_disable;
 	}
 
 	ret = bmi160_set_mode(data, BMI160_ACCEL, true);
 	if (ret)
-		return ret;
+		goto err_regulator_disable;
 
 	ret = bmi160_set_mode(data, BMI160_GYRO, true);
 	if (ret)
-		return ret;
+		goto err_regulator_disable;
 
 	return 0;
+
+err_regulator_disable:
+	regulator_bulk_disable(ARRAY_SIZE(data->supplies), data->supplies);
+	return ret;
 }
 
 static int bmi160_data_rdy_trigger_set_state(struct iio_trigger *trig,
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] iio: imu: bmi160: Fix the error handling at bmi160_chip_init()
  2022-05-13 14:35 [PATCH] iio: imu: bmi160: Fix the error handling at bmi160_chip_init() Zheyu Ma
@ 2022-05-14 14:36 ` Jonathan Cameron
  2022-05-17 12:23   ` Zheyu Ma
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Cameron @ 2022-05-14 14:36 UTC (permalink / raw)
  To: Zheyu Ma; +Cc: lars, andriy.shevchenko, sean, linux-iio, linux-kernel

On Fri, 13 May 2022 22:35:13 +0800
Zheyu Ma <zheyuma97@gmail.com> wrote:

> When the driver fails in bmi160_chip_init(), it should disable the
> regulator.
> 
> The following log shows it:
> 
> [   18.709662] WARNING: CPU: 0 PID: 294 at drivers/regulator/core.c:2257 _regulator_put+0x3ec/0x4e0
> [   18.712069] RIP: 0010:_regulator_put+0x3ec/0x4e0
> [   18.716395] Call Trace:
> [   18.716522]  <TASK>
> [   18.716636]  regulator_bulk_free+0x82/0xe0
> [   18.717077]  i2c_device_probe+0x6f0/0x800
> 
> Fixes: 5dea3fb066f0 ("iio: imu: bmi160: added regulator support")
> Signed-off-by: Zheyu Ma <zheyuma97@gmail.com>

Hi,

Good find, but Tong Zhang got their first and the resulting patch
fixes an additional issue missed in this fix. See below.

Jonathan

> ---
>  drivers/iio/imu/bmi160/bmi160_core.c | 17 +++++++++++------
>  1 file changed, 11 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/iio/imu/bmi160/bmi160_core.c b/drivers/iio/imu/bmi160/bmi160_core.c
> index 824b5124a5f5..33abfc2bbd7c 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 err_regulator_disable;
>  
>  	usleep_range(BMI160_SOFTRESET_USLEEP, BMI160_SOFTRESET_USLEEP + 1);
>  
> @@ -741,29 +741,34 @@ 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 err_regulator_disable;
>  	}
>  
>  	ret = regmap_read(data->regmap, BMI160_REG_CHIP_ID, &val);
>  	if (ret) {
>  		dev_err(dev, "Error reading chip id\n");
> -		return ret;
> +		goto err_regulator_disable;
>  	}
>  	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 err_regulator_disable;
>  	}
>  
>  	ret = bmi160_set_mode(data, BMI160_ACCEL, true);
>  	if (ret)
> -		return ret;
> +		goto err_regulator_disable;
>  
>  	ret = bmi160_set_mode(data, BMI160_GYRO, true);
>  	if (ret)

A failure here does not disable the accelerometer (which is done
in the uninit() function which we we never end
up running.


> -		return ret;
> +		goto err_regulator_disable;
>  
>  	return 0;
> +
> +err_regulator_disable:
> +	regulator_bulk_disable(ARRAY_SIZE(data->supplies), data->supplies);
> +	return ret;
>  }
>  
>  static int bmi160_data_rdy_trigger_set_state(struct iio_trigger *trig,


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] iio: imu: bmi160: Fix the error handling at bmi160_chip_init()
  2022-05-14 14:36 ` Jonathan Cameron
@ 2022-05-17 12:23   ` Zheyu Ma
  0 siblings, 0 replies; 3+ messages in thread
From: Zheyu Ma @ 2022-05-17 12:23 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: lars, andriy.shevchenko, sean, linux-iio,
	Linux Kernel Mailing List

On Sat, May 14, 2022 at 10:27 PM Jonathan Cameron <jic23@kernel.org> wrote:
>
> On Fri, 13 May 2022 22:35:13 +0800
> Zheyu Ma <zheyuma97@gmail.com> wrote:
>
> > When the driver fails in bmi160_chip_init(), it should disable the
> > regulator.
> >
> > The following log shows it:
> >
> > [   18.709662] WARNING: CPU: 0 PID: 294 at drivers/regulator/core.c:2257 _regulator_put+0x3ec/0x4e0
> > [   18.712069] RIP: 0010:_regulator_put+0x3ec/0x4e0
> > [   18.716395] Call Trace:
> > [   18.716522]  <TASK>
> > [   18.716636]  regulator_bulk_free+0x82/0xe0
> > [   18.717077]  i2c_device_probe+0x6f0/0x800
> >
> > Fixes: 5dea3fb066f0 ("iio: imu: bmi160: added regulator support")
> > Signed-off-by: Zheyu Ma <zheyuma97@gmail.com>
>
> Hi,
>
> Good find, but Tong Zhang got their first and the resulting patch
> fixes an additional issue missed in this fix. See below.

Thanks for your reply! I'll check the linux-next branch next time.

Zheyu Ma

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-05-17 12:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-05-13 14:35 [PATCH] iio: imu: bmi160: Fix the error handling at bmi160_chip_init() Zheyu Ma
2022-05-14 14:36 ` Jonathan Cameron
2022-05-17 12:23   ` Zheyu Ma

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox