[PATCH v2 3/3] iio: imu: inv_mpu6050: fix unbalanced regulator_disable calls, when probe fails

Andrey Skvortsov posted 3 patches 13 hours ago
[PATCH v2 3/3] iio: imu: inv_mpu6050: fix unbalanced regulator_disable calls, when probe fails
Posted by Andrey Skvortsov 13 hours ago
During a probe functions after all regulators are enabled, runtime pm
is enabled. Before probe function finishes, runtime pm triggers and
disables vddio regulator. When probe function fails after that,
inv_mpu_core_disable_regulator_action tries to disable already
disabled by runtime pm vddio regulator causing following backtrace:

  inv-mpu6050-i2c 1-0068: trigger probe fail -19
  WARNING: drivers/regulator/core.c:3244 at _regulator_disable+0x2ac/0x600
  Call trace:
   _regulator_disable+0x2ac/0x600 (P)
   regulator_disable+0xac/0x148
   inv_mpu_core_disable_regulator_vddio_action+0x3c/0xb0 [inv_mpu6050]
   devm_action_release+0x4c/0x88
   release_nodes+0xd8/0x178
   devres_release_group+0x214/0x3c8
   i2c_device_probe+0x6fc/0x9b0
  ...
  inv-mpu6050-i2c 1-0068: Failed to disable vddio regulator: -5

Fix the issue by checking runtime suspend status, when vddio regulator
is disabled. To handle this correctly pm_runtime active status has to
be set early before vddio regulator setup.

Signed-off-by: Andrey Skvortsov <andrej.skvortzov@gmail.com>
Fixes: 07c12b1c007c ("iio: imu: mpu6050: add support for regulator framework")
---
 drivers/iio/imu/inv_mpu6050/inv_mpu_core.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
index 3a57a3f440526..86e4d42c4d3d1 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
@@ -1862,8 +1862,10 @@ static int inv_mpu_core_disable_regulator_vddio(struct inv_mpu6050_state *st)
 static void inv_mpu_core_disable_regulator_vddio_action(void *_data)
 {
 	struct inv_mpu6050_state *st = _data;
+	struct device *dev = regmap_get_device(st->map);
 
-	inv_mpu_core_disable_regulator_vddio(st);
+	if (!pm_runtime_status_suspended(dev))
+		inv_mpu_core_disable_regulator_vddio(st);
 }
 
 
@@ -1953,6 +1955,11 @@ int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
 
 	msleep(INV_MPU6050_POWER_UP_TIME);
 
+	/* set pm_runtime active early for disable vddio resource cleanup */
+	result = pm_runtime_set_active(dev);
+	if (result)
+		return result;
+
 	result = inv_mpu_core_enable_regulator_vddio(st);
 	if (result)
 		return result;
@@ -1996,9 +2003,6 @@ int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
 	}
 
 	/* chip init is done, turning on runtime power management */
-	result = pm_runtime_set_active(dev);
-	if (result)
-		goto error_power_off;
 	pm_runtime_get_noresume(dev);
 	result = devm_pm_runtime_enable(dev);
 	if (result)
-- 
2.51.0