regulator: core: introduce function to lock regulators and its supplies
[linux-drm-fsl-dcu.git] / drivers / regulator / core.c
index 7a85ac9e32c5da9168c1e79d3ae82d230d9dd628..1f9ff55b362021d78bd782a6d322e0fe0a14a91b 100644 (file)
@@ -131,6 +131,45 @@ static bool have_full_constraints(void)
        return has_full_constraints || of_have_populated_dt();
 }
 
+/**
+ * regulator_lock_supply - lock a regulator and its supplies
+ * @rdev:         regulator source
+ */
+static void regulator_lock_supply(struct regulator_dev *rdev)
+{
+       struct regulator *supply;
+       int i = 0;
+
+       while (1) {
+               mutex_lock_nested(&rdev->mutex, i++);
+               supply = rdev->supply;
+
+               if (!rdev->supply)
+                       return;
+
+               rdev = supply->rdev;
+       }
+}
+
+/**
+ * regulator_unlock_supply - unlock a regulator and its supplies
+ * @rdev:         regulator source
+ */
+static void regulator_unlock_supply(struct regulator_dev *rdev)
+{
+       struct regulator *supply;
+
+       while (1) {
+               mutex_unlock(&rdev->mutex);
+               supply = rdev->supply;
+
+               if (!rdev->supply)
+                       return;
+
+               rdev = supply->rdev;
+       }
+}
+
 /**
  * of_get_regulator - get a regulator device node based on supply name
  * @dev: Device pointer for the consumer (of regulator) device