Skip to contentSkip to conversation

iio: imu: inv_mpu6050: runtime pm fixes

PATCH In review Andrey Skvortsov 2 participants 5 messages Jun 5 – Jun 14

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover iio: imu: inv_mpu6050: runtime pm fixes

This patchset fixes problem with inv_mpu6050 driver,
when it fails after device is suspended by runtime pm during a probe. To simplify runtime pm handling in the driver more devm-helpers are used.

Andrey Skvortsov (3):
iio: imu: inv_mpu6050: fix unbalanced regulator_disable() calls, when
probe fails
iio: imu: inv_mpu6050: use devres-enabled version of
pm_runtime_enable()
iio: imu: inv_mpu6050: control vdd supply using devm-helpers

 drivers/iio/imu/inv_mpu6050/inv_mpu_core.c | 49 +++++++---------------
 drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h  |  1 -
 2 files changed, 15 insertions(+), 35 deletions(-)

Changes in v4:
- update commit messages to address Andy's comment
about functions, callbacks and runtime PM names
- move pm_runtime_set_active just before vddio regulator is enabled as it was proposed previously in v2.

Changes in v3:
- make patch "fix unbalanced regulator_disable calls, when probe fails" with Fixes to be the first patch of the patchset for easier backporting
- don't move pm_runtime_set_active, move devm_add_action_or_reset for inv_mpu_core_disable_regulator_action instead
- update commit message in "fix unbalanced regulator_disable calls, when probe fails" to fix Andy's comment
- remove blank lines in "use devres-enabled version of pm_runtime_enable"
- no dead code in inv_mpu_core_disable_regulator_action anymore since st variable is used to get 'struct device' now

Changes in v2:
- minimize call trace in commit message
- use runtime pm framework to track vddio regulator state
- include specific driver name in patch subject
- add more patches to use device managed functions

Signature · 1 line
2.53.0
#1 lore raw

PATCH v4 1/3 iio: imu: inv_mpu6050: fix unbalanced regulator_disable() calls, when probe fails

During a probe function 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 vddio regulator is disabled by runtime PM, 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

vddio state is handled in two places: runtime PM and inv_mpu_core_disable_regulator_vddio_action() devm action. inv_mpu_core_disable_regulator_vddio_action() has to check, whether regulator is disabled by runtime PM already. But this information is available only after runtime PM state is initialized by pm_runtime_set_active() during a probe. So inv_mpu_core_disable_regulator_vddio_action() has to be called only after runtime PM state is initialized. To handle this correctly runtime PM active status is set just before vddio regulator setup.

Fixes 07c12b1c007c ("iio: imu: mpu6050: add support for regulator framework")

---

Changes in v4:
- update commit message to address Andy's comment
about functions, callbacks and runtime PM names
- move pm_runtime_set_active just before vddio regulator is enabled as it was proposed previously in v2.

Changes in v3:
- make patch to be the first patch of the patchset for easier backporting
- update commit message to fix Andy's comment
- don't move pm_runtime_set_active from pm_runtime initialization, move activation of inv_mpu_core_disable_regulator_action instead

Changes in v2:
- minimize call trace in commit message
- include specific driver name in patch subject
- move pm_runtime_set_active just before vddio regulator is enabled

 drivers/iio/imu/inv_mpu6050/inv_mpu_core.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/iio/imu/inv_mpu6050/inv_mpu_core.c +8 -4
index 5796896d54cd8..8c8b4f9a8ff63 100644
@@ -1862,6 +1862,7 @@ static int inv_mpu_core_disable_regulator_vddio(struct inv_mpu6050_state *st)
 static void inv_mpu_core_disable_regulator_action(void *_data)
 {
 	struct inv_mpu6050_state *st = _data;
+	struct device *dev = regmap_get_device(st->map);
 	int result;
 
 	result = regulator_disable(st->vdd_supply);
@@ -1869,7 +1870,8 @@ static void inv_mpu_core_disable_regulator_action(void *_data)
 		dev_err(regmap_get_device(st->map),
 			"Failed to disable vdd regulator: %d\n", result);
 
-	inv_mpu_core_disable_regulator_vddio(st);
+	if (!pm_runtime_status_suspended(dev))
+		inv_mpu_core_disable_regulator_vddio(st);
 }
 
 static void inv_mpu_pm_disable(void *data)
@@ -1970,6 +1972,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) {
 		regulator_disable(st->vdd_supply);
@@ -2015,9 +2022,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);
 	pm_runtime_enable(dev);
 	pm_runtime_set_autosuspend_delay(dev, INV_MPU6050_SUSPEND_DELAY_MS);
Signature · 1 line
2.53.0
#2 lore raw

PATCH v4 2/3 iio: imu: inv_mpu6050: use devres-enabled version of pm_runtime_enable()

In reply to Andrey Skvortsov

Usage of devm_pm_runtime_enable() allows to remove driver-specific action, that does the same as default action.

---

Changes in v4:
- update commit message to address Andy's comment about functions, callbacks and runtime PM names

Changes in v3:
- remove extra blank lines in "use devres-enabled version of pm_runtime_enable"

 drivers/iio/imu/inv_mpu6050/inv_mpu_core.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
drivers/iio/imu/inv_mpu6050/inv_mpu_core.c +3 -11
index 8c8b4f9a8ff63..6fd35ea5daabb 100644
@@ -1874,13 +1874,6 @@ static void inv_mpu_core_disable_regulator_action(void *_data)
 		inv_mpu_core_disable_regulator_vddio(st);
 }
 
-static void inv_mpu_pm_disable(void *data)
-{
-	struct device *dev = data;
-
-	pm_runtime_disable(dev);
-}
-
 int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
 		int (*inv_mpu_bus_setup)(struct iio_dev *), int chip_type)
 {
@@ -2023,13 +2016,12 @@ int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
 
 	/* chip init is done, turning on runtime power management */
 	pm_runtime_get_noresume(dev);
-	pm_runtime_enable(dev);
+	result = devm_pm_runtime_enable(dev);
+	if (result)
+		return result;
 	pm_runtime_set_autosuspend_delay(dev, INV_MPU6050_SUSPEND_DELAY_MS);
 	pm_runtime_use_autosuspend(dev);
 	pm_runtime_put(dev);
-	result = devm_add_action_or_reset(dev, inv_mpu_pm_disable, dev);
-	if (result)
-		return result;
 
 	switch (chip_type) {
 	case INV_MPU6000:
Signature · 1 line
2.53.0
#3 lore raw

PATCH v4 3/3 iio: imu: inv_mpu6050: control vdd supply using devm-helpers

In reply to Andrey Skvortsov

vdd supply will be automatically disabled on error condition during a probe and on driver cleanup.

---

Changes in v4:
- no actual changes

Changes in v3:
- no dead code in inv_mpu_core_disable_regulator_action anymore since st variable is used to get 'struct device' now

 drivers/iio/imu/inv_mpu6050/inv_mpu_core.c | 23 ++++------------------
 drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h  |  1 -
 2 files changed, 4 insertions(+), 20 deletions(-)
drivers/iio/imu/inv_mpu6050/inv_mpu_core.c +4 -19
index 6fd35ea5daabb..c170efce7d31c 100644
@@ -1863,12 +1863,6 @@ static void inv_mpu_core_disable_regulator_action(void *_data)
 {
 	struct inv_mpu6050_state *st = _data;
 	struct device *dev = regmap_get_device(st->map);
-	int result;
-
-	result = regulator_disable(st->vdd_supply);
-	if (result)
-		dev_err(regmap_get_device(st->map),
-			"Failed to disable vdd regulator: %d\n", result);
 
 	if (!pm_runtime_status_suspended(dev))
 		inv_mpu_core_disable_regulator_vddio(st);
@@ -1948,21 +1942,14 @@ int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
 
 	device_set_wakeup_capable(dev, true);
 
-	st->vdd_supply = devm_regulator_get(dev, "vdd");
-	if (IS_ERR(st->vdd_supply))
-		return dev_err_probe(dev, PTR_ERR(st->vdd_supply),
-				     "Failed to get vdd regulator\n");
-
 	st->vddio_supply = devm_regulator_get(dev, "vddio");
 	if (IS_ERR(st->vddio_supply))
 		return dev_err_probe(dev, PTR_ERR(st->vddio_supply),
 				     "Failed to get vddio regulator\n");
 
-	result = regulator_enable(st->vdd_supply);
-	if (result) {
-		dev_err(dev, "Failed to enable vdd regulator: %d\n", result);
-		return result;
-	}
+	result = devm_regulator_get_enable(dev, "vdd");
+	if (result)
+		return dev_err_probe(dev, result, "Failed to enable vdd regulator\n");
 	msleep(INV_MPU6050_POWER_UP_TIME);
 
 	/* set pm_runtime active early for disable vddio resource cleanup */
@@ -1971,10 +1958,8 @@ int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
 		return result;
 
 	result = inv_mpu_core_enable_regulator_vddio(st);
-	if (result) {
-		regulator_disable(st->vdd_supply);
+	if (result)
 		return result;
-	}
 
 	result = devm_add_action_or_reset(dev, inv_mpu_core_disable_regulator_action,
 				 st);
drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h +0 -1
index 6239b1a803f77..2c0cf53c9a627 100644
@@ -206,7 +206,6 @@ struct inv_mpu6050_state {
 	u8 irq_mask;
 	unsigned skip_samples;
 	struct inv_sensors_timestamp timestamp;
-	struct regulator *vdd_supply;
 	struct regulator *vddio_supply;
 	bool magn_disabled;
 	s32 magn_raw_to_gauss[3];
Signature · 1 line
2.53.0
maintainer
#4 lore raw
In reply to Andrey Skvortsov
Quoted 4 lines · On Fri, 5 Jun 2026 12:09:37 +0300 Andrey Skvortsov…

This patchset fixes problem with inv_mpu6050 driver,
when it fails after device is suspended by runtime pm during a probe. To simplify runtime pm handling in the driver more devm-helpers are used.

Hi Andrey,

https://sashiko.dev/#/patchset/20260605090940.1103750-1-andrej.skvortzov%40gmail.com
Has some feedback. Please take a look.

You may need to do some experimentation to confirm but I believe you can just remove the pm_runtime_get_noresume(dev) and associated
put(). Drivers automatically try to enter autosuspend due to calls in
drivers/base/dd.c when probe is complete if their reference counter is 0.

This dance is therefore not needed. However, please confirm this for yourself.

The other bit of feedback about not powering down if devm_pm_runtime_enable(dev); fails looks plausible but maybe I'm missing something.

Thanks,

Jonathan

Quoted 34 lines

Andrey Skvortsov (3):
iio: imu: inv_mpu6050: fix unbalanced regulator_disable() calls, when
probe fails
iio: imu: inv_mpu6050: use devres-enabled version of
pm_runtime_enable()
iio: imu: inv_mpu6050: control vdd supply using devm-helpers

 drivers/iio/imu/inv_mpu6050/inv_mpu_core.c | 49 +++++++---------------
 drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h  |  1 -
 2 files changed, 15 insertions(+), 35 deletions(-)

Changes in v4:
- update commit messages to address Andy's comment
about functions, callbacks and runtime PM names
- move pm_runtime_set_active just before vddio regulator is enabled as it was proposed previously in v2.

Changes in v3:
- make patch "fix unbalanced regulator_disable calls, when probe fails" with Fixes to be the first patch of the patchset for easier backporting
- don't move pm_runtime_set_active, move devm_add_action_or_reset for inv_mpu_core_disable_regulator_action instead
- update commit message in "fix unbalanced regulator_disable calls, when probe fails" to fix Andy's comment
- remove blank lines in "use devres-enabled version of pm_runtime_enable"
- no dead code in inv_mpu_core_disable_regulator_action anymore since st variable is used to get 'struct device' now

Changes in v2:
- minimize call trace in commit message
- use runtime pm framework to track vddio regulator state
- include specific driver name in patch subject
- add more patches to use device managed functions