regulator: core: Fix nested locking of supplies
[linux-drm-fsl-dcu.git] / drivers / regulator / core.c
index f71db02fcb7130091e86c455ee70567e187f5cac..732ac71b82cdd5981454dec04da6ad2cad6c4cba 100644 (file)
@@ -132,6 +132,14 @@ static bool have_full_constraints(void)
        return has_full_constraints || of_have_populated_dt();
 }
 
+static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
+{
+       if (rdev && rdev->supply)
+               return rdev->supply->rdev;
+
+       return NULL;
+}
+
 /**
  * regulator_lock_supply - lock a regulator and its supplies
  * @rdev:         regulator source
@@ -140,8 +148,7 @@ static void regulator_lock_supply(struct regulator_dev *rdev)
 {
        int i;
 
-       mutex_lock(&rdev->mutex);
-       for (i = 1; rdev; rdev = rdev->supply->rdev, i++)
+       for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
                mutex_lock_nested(&rdev->mutex, i);
 }