Skip to contentSkip to conversation

iio:imu:bmi160: disable regulator in error path

PATCH Superseded Tong Zhang 4 participants 13 messages Mar 18, 2022 – Mar 27, 2022 IIO

lore
Share & tools
lore

Tong Zhang

author
#0 lore raw

PATCH 1/1 iio:imu:bmi160: disable regulator in error path

regulator should be disabled in error path as mentioned in _regulator_put()

[   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")
---
 drivers/iio/imu/bmi160/bmi160_core.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/iio/imu/bmi160/bmi160_core.c +11 -6
index 824b5124a5f5..f12446edb5ce 100644
@@ -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,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 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_regulator;
 
 	return 0;
+
+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,
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Tong Zhang

regulator should be disabled in error path as mentioned in _regulator_put()

[   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]

Seems legit. Currently we call it only when something else is failed afterwards.

Quoted 62 lines
Fixes 5dea3fb066f0 ("iio: imu: bmi160: added regulator support")
---
 drivers/iio/imu/bmi160/bmi160_core.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/iio/imu/bmi160/bmi160_core.c +11 -6
index 824b5124a5f5..f12446edb5ce 100644
@@ -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,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 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_regulator;
 
 	return 0;
+
+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,
Signature · 1 line
2.25.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#2 lore raw
Quoted 12 lines · On Fri, 18 Mar 2022 16:11:55 +0200 Andy Shevchenko…
Quoted 10 lines · Tong Zhang

regulator should be disabled in error path as mentioned in _regulator_put()

[   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]  
Quoted 2 lines

Seems legit. Currently we call it only when something else is failed afterwards.

Indeed this is fixing a real issue, but only one of two similar issues. (I'd never have looked closely at this code without you spotting the first one :)

If I were writing this driver from scratch I would register multiple devm_add_action_or_reset() callbacks and I note that even though we might have turned the power off we haven't handled the other state set in this init function.

1) Regulator disable.
2) bmi160_set_mode(bmi_data, BMI160_GYRO, false);
3) bmi160_set_mode(bmi_data, BMI160_ACCEL, false);

An alternative is to add handling for the first set_mode() in here being unwound if the second fails. See below.

Quoted 42 lines
Quoted 42 lines
Fixes 5dea3fb066f0 ("iio: imu: bmi160: added regulator support")
---
 drivers/iio/imu/bmi160/bmi160_core.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/iio/imu/bmi160/bmi160_core.c +5 -4
index 824b5124a5f5..f12446edb5ce 100644
@@ -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,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 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;
 	}
 
Quoted 7 lines

drivers/iio/imu/bmi160/bmi160_core.c static int bmi160_chip_init(struct bmi160_data *data, bool use_spi) · line 764

Quoted 7 lines
drivers/iio/imu/bmi160/bmi160_core.c +1 -1
 	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)

If this fails, we should also undo the previous call as well. For readability use goto disable_accel; then fix it under that new label.

Quoted 15 lines
Quoted 14 lines
drivers/iio/imu/bmi160/bmi160_core.c +5 -1
-		return ret;
+		goto disable_regulator;
 
 	return 0;
+
+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,
Signature · 1 line
2.25.1
author
#3 lore raw

Thank you, Jonathan and Andy. I will send v2 adding suggested fixes.

author
#4 lore raw

PATCH v2 1/1 iio:imu:bmi160: disable regulator in error path

In reply to Jonathan Cameron

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")

---
v2: also disable accel when gyro fail to enable

 drivers/iio/imu/bmi160/bmi160_core.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
drivers/iio/imu/bmi160/bmi160_core.c +14 -6
index 824b5124a5f5..01336105792e 100644
@@ -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,
Signature · 1 line
2.25.1
#5 lore raw
Quoted 4 lines · Tong Zhang

Regulator should be disabled in error path as mentioned in _regulator_put(). Also disable accel if gyro cannot be enabled.

Quoted 8 lines
[   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]

Haven't I given you a tag?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#6 lore raw
Quoted 15 lines · Andy Shevchenko
Quoted 4 lines · Tong Zhang

Regulator should be disabled in error path as mentioned in _regulator_put(). Also disable accel if gyro cannot be enabled.

Quoted 8 lines
[   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]
Quoted 5 lines

Haven't I given you a tag?

--
With Best Regards,
Andy Shevchenko

Hi Any, Thank you for reviewing the patch. I appreciate it. I thought I would need another tag since this patch is a v2. Sorry for this back and forth. Have a great one. Thanks,
- Tong

#7 lore raw
Quoted 3 lines · Tong Zhang
Quoted 1 line · Andy Shevchenko

On Sun, Mar 20, 2022 at 8:44 AM Tong Zhang <[email protected]> wrote:

...

Quoted 4 lines
Quoted 1 line

Haven't I given you a tag?

Hi Any, Thank you for reviewing the patch. I appreciate it. I thought I would need another tag since this patch is a v2.

It depends on the nature of the changes you made. As far as I read the code the changes you made are in addition to what I have tagged and I see nothing that prevents you from keeping the tag.

Quoted 1 line

Sorry for this back and forth. Have a great one.

NP.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer Applied
#8 lore raw
Quoted 12 lines · On Mon, 21 Mar 2022 18:22:10 +0200 Andy Shevchenko <[email protected]>…
Quoted 3 lines · Tong Zhang
Quoted 1 line · Andy Shevchenko

On Sun, Mar 20, 2022 at 8:44 AM Tong Zhang <[email protected]> wrote:

...

Quoted 4 lines
Quoted 1 line

Haven't I given you a tag?

Hi Any, Thank you for reviewing the patch. I appreciate it. I thought I would need another tag since this patch is a v2.

Quoted 8 lines

It depends on the nature of the changes you made. As far as I read the code the changes you made are in addition to what I have tagged and I see nothing that prevents you from keeping the tag.

Quoted 1 line

Sorry for this back and forth. Have a great one.

NP.

Applied to the fixes-togreg branch of iio.git but I won't push it out until I've rebased that on rc1 once available.

Thanks,

Jonathan

author
#9 lore raw

PATCH v3 1/1 iio:imu:bmi160: disable regulator in error path

In reply to Tong Zhang

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")

---
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(-)
drivers/iio/imu/bmi160/bmi160_core.c +14 -6
index 824b5124a5f5..01336105792e 100644
@@ -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,
Signature · 1 line
2.25.1
maintainer
#10 lore raw
Quoted 12 lines · On Sun, 27 Mar 2022 08:40:05 -0700 Tong Zhang <[email protected]> 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]
Quoted 3 lines
Fixes 5dea3fb066f0 ("iio: imu: bmi160: added regulator support")

oops. Raced with you ;)

Anyhow, I added the tag, so no problem.

Jonathan

Quoted 62 lines

---
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(-)
drivers/iio/imu/bmi160/bmi160_core.c +14 -6
index 824b5124a5f5..01336105792e 100644
@@ -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,
maintainer
#11 lore raw
Quoted 15 lines · On Sun, 27 Mar 2022 16:53:36 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 12 lines · On Sun, 27 Mar 2022 08:40:05 -0700 Tong Zhang <[email protected]> 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]
Quoted 6 lines
Quoted 3 lines
Fixes 5dea3fb066f0 ("iio: imu: bmi160: added regulator support")

oops. Raced with you ;)

Anyhow, I added the tag, so no problem.

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

J

Quoted 66 lines

Jonathan

Quoted 62 lines

---
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(-)
drivers/iio/imu/bmi160/bmi160_core.c +14 -6
index 824b5124a5f5..01336105792e 100644
@@ -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,  
author
#12 lore raw
In reply to Jonathan Cameron
Quoted 16 lines · Jonathan Cameron
Quoted 12 lines · On Sun, 27 Mar 2022 08:40:05 -0700 Tong Zhang <[email protected]> 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]
Quoted 8 lines
Quoted 3 lines
Fixes 5dea3fb066f0 ("iio: imu: bmi160: added regulator support")

oops. Raced with you ;)

Anyhow, I added the tag, so no problem.

Jonathan

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

Quoted 64 lines
Quoted 63 lines

---
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(-)
drivers/iio/imu/bmi160/bmi160_core.c +14 -6
index 824b5124a5f5..01336105792e 100644
@@ -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,