]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - drivers/regulator/core.c
regulator: core: Mutually resolve regulators coupling
[linux.git] / drivers / regulator / core.c
index 2c66b528aedec02f4afdda7e8c04faf60711a70d..925df9e6f1e37383de8e98040da73baffa87aa23 100644 (file)
@@ -105,6 +105,11 @@ static int _notifier_call_chain(struct regulator_dev *rdev,
                                  unsigned long event, void *data);
 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
                                     int min_uV, int max_uV);
+static int regulator_balance_voltage(struct regulator_dev *rdev,
+                                    suspend_state_t state);
+static int regulator_set_voltage_rdev(struct regulator_dev *rdev,
+                                     int min_uV, int max_uV,
+                                     suspend_state_t state);
 static struct regulator *create_regulator(struct regulator_dev *rdev,
                                          struct device *dev,
                                          const char *supply_name);
@@ -196,37 +201,66 @@ static void regulator_unlock(struct regulator_dev *rdev)
        }
 }
 
-/**
- * regulator_lock_supply - lock a regulator and its supplies
- * @rdev:         regulator source
- */
-static void regulator_lock_supply(struct regulator_dev *rdev)
+static int regulator_lock_recursive(struct regulator_dev *rdev,
+                                   unsigned int subclass)
 {
+       struct regulator_dev *c_rdev;
        int i;
 
-       for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
-               regulator_lock_nested(rdev, i);
+       for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
+               c_rdev = rdev->coupling_desc.coupled_rdevs[i];
+
+               if (!c_rdev)
+                       continue;
+
+               regulator_lock_nested(c_rdev, subclass++);
+
+               if (c_rdev->supply)
+                       subclass =
+                               regulator_lock_recursive(c_rdev->supply->rdev,
+                                                        subclass);
+       }
+
+       return subclass;
 }
 
 /**
- * regulator_unlock_supply - unlock a regulator and its supplies
- * @rdev:         regulator source
+ * regulator_unlock_dependent - unlock regulator's suppliers and coupled
+ *                             regulators
+ * @rdev:                      regulator source
+ *
+ * Unlock all regulators related with rdev by coupling or suppling.
  */
-static void regulator_unlock_supply(struct regulator_dev *rdev)
+static void regulator_unlock_dependent(struct regulator_dev *rdev)
 {
-       struct regulator *supply;
+       struct regulator_dev *c_rdev;
+       int i;
 
-       while (1) {
-               regulator_unlock(rdev);
-               supply = rdev->supply;
+       for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
+               c_rdev = rdev->coupling_desc.coupled_rdevs[i];
 
-               if (!rdev->supply)
-                       return;
+               if (!c_rdev)
+                       continue;
+
+               regulator_unlock(c_rdev);
 
-               rdev = supply->rdev;
+               if (c_rdev->supply)
+                       regulator_unlock_dependent(c_rdev->supply->rdev);
        }
 }
 
+/**
+ * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
+ * @rdev:                      regulator source
+ *
+ * This function as a wrapper on regulator_lock_recursive(), which locks
+ * all regulators related with rdev by coupling or suppling.
+ */
+static inline void regulator_lock_dependent(struct regulator_dev *rdev)
+{
+       regulator_lock_recursive(rdev, 0);
+}
+
 /**
  * of_get_regulator - get a regulator device node based on supply name
  * @dev: Device pointer for the consumer (of regulator) device
@@ -2287,9 +2321,16 @@ int regulator_enable(struct regulator *regulator)
                        return ret;
        }
 
-       mutex_lock(&rdev->mutex);
+       regulator_lock_dependent(rdev);
+       /* balance only if there are regulators coupled */
+       if (rdev->coupling_desc.n_coupled > 1) {
+               ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
+               if (ret != 0)
+                       goto unlock;
+       }
        ret = _regulator_enable(rdev);
-       mutex_unlock(&rdev->mutex);
+unlock:
+       regulator_unlock_dependent(rdev);
 
        if (ret != 0 && rdev->supply)
                regulator_disable(rdev->supply);
@@ -2395,9 +2436,11 @@ int regulator_disable(struct regulator *regulator)
        if (regulator->always_on)
                return 0;
 
-       mutex_lock(&rdev->mutex);
+       regulator_lock_dependent(rdev);
        ret = _regulator_disable(rdev);
-       mutex_unlock(&rdev->mutex);
+       if (rdev->coupling_desc.n_coupled > 1)
+               regulator_balance_voltage(rdev, PM_SUSPEND_ON);
+       regulator_unlock_dependent(rdev);
 
        if (ret == 0 && rdev->supply)
                regulator_disable(rdev->supply);
@@ -2446,10 +2489,12 @@ int regulator_force_disable(struct regulator *regulator)
        struct regulator_dev *rdev = regulator->rdev;
        int ret;
 
-       mutex_lock(&rdev->mutex);
+       regulator_lock_dependent(rdev);
        regulator->uA_load = 0;
        ret = _regulator_force_disable(regulator->rdev);
-       mutex_unlock(&rdev->mutex);
+       if (rdev->coupling_desc.n_coupled > 1)
+               regulator_balance_voltage(rdev, PM_SUSPEND_ON);
+       regulator_unlock_dependent(rdev);
 
        if (rdev->supply)
                while (rdev->open_count--)
@@ -2597,9 +2642,9 @@ int regulator_is_enabled(struct regulator *regulator)
        if (regulator->always_on)
                return 1;
 
-       mutex_lock(&regulator->rdev->mutex);
+       regulator_lock_dependent(regulator->rdev);
        ret = _regulator_is_enabled(regulator->rdev);
-       mutex_unlock(&regulator->rdev->mutex);
+       regulator_unlock_dependent(regulator->rdev);
 
        return ret;
 }
@@ -3013,8 +3058,6 @@ static int regulator_set_voltage_unlocked(struct regulator *regulator,
        int ret = 0;
        int old_min_uV, old_max_uV;
        int current_uV;
-       int best_supply_uV = 0;
-       int supply_change_uV = 0;
 
        /* If we're setting the same range as last time the change
         * should be a noop (some cpufreq implementations use the same
@@ -3054,10 +3097,27 @@ static int regulator_set_voltage_unlocked(struct regulator *regulator,
        voltage->min_uV = min_uV;
        voltage->max_uV = max_uV;
 
-       ret = regulator_check_consumers(rdev, &min_uV, &max_uV, state);
+       /* for not coupled regulators this will just set the voltage */
+       ret = regulator_balance_voltage(rdev, state);
        if (ret < 0)
                goto out2;
 
+out:
+       return 0;
+out2:
+       voltage->min_uV = old_min_uV;
+       voltage->max_uV = old_max_uV;
+
+       return ret;
+}
+
+static int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
+                                     int max_uV, suspend_state_t state)
+{
+       int best_supply_uV = 0;
+       int supply_change_uV = 0;
+       int ret;
+
        if (rdev->supply &&
            regulator_ops_is_valid(rdev->supply->rdev,
                                   REGULATOR_CHANGE_VOLTAGE) &&
@@ -3069,13 +3129,13 @@ static int regulator_set_voltage_unlocked(struct regulator *regulator,
                selector = regulator_map_voltage(rdev, min_uV, max_uV);
                if (selector < 0) {
                        ret = selector;
-                       goto out2;
+                       goto out;
                }
 
                best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
                if (best_supply_uV < 0) {
                        ret = best_supply_uV;
-                       goto out2;
+                       goto out;
                }
 
                best_supply_uV += rdev->desc->min_dropout_uV;
@@ -3083,7 +3143,7 @@ static int regulator_set_voltage_unlocked(struct regulator *regulator,
                current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
                if (current_supply_uV < 0) {
                        ret = current_supply_uV;
-                       goto out2;
+                       goto out;
                }
 
                supply_change_uV = best_supply_uV - current_supply_uV;
@@ -3095,7 +3155,7 @@ static int regulator_set_voltage_unlocked(struct regulator *regulator,
                if (ret) {
                        dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
                                        ret);
-                       goto out2;
+                       goto out;
                }
        }
 
@@ -3105,7 +3165,7 @@ static int regulator_set_voltage_unlocked(struct regulator *regulator,
                ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
                                                        max_uV, state);
        if (ret < 0)
-               goto out2;
+               goto out;
 
        if (supply_change_uV < 0) {
                ret = regulator_set_voltage_unlocked(rdev->supply,
@@ -3119,10 +3179,232 @@ static int regulator_set_voltage_unlocked(struct regulator *regulator,
 
 out:
        return ret;
-out2:
-       voltage->min_uV = old_min_uV;
-       voltage->max_uV = old_max_uV;
+}
+
+static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
+                                        int *current_uV,
+                                        int *min_uV, int *max_uV,
+                                        suspend_state_t state,
+                                        int n_coupled)
+{
+       struct coupling_desc *c_desc = &rdev->coupling_desc;
+       struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
+       struct regulation_constraints *constraints = rdev->constraints;
+       int max_spread = constraints->max_spread;
+       int desired_min_uV = 0, desired_max_uV = INT_MAX;
+       int max_current_uV = 0, min_current_uV = INT_MAX;
+       int highest_min_uV = 0, target_uV, possible_uV;
+       int i, ret;
+       bool done;
+
+       *current_uV = -1;
+
+       /*
+        * If there are no coupled regulators, simply set the voltage
+        * demanded by consumers.
+        */
+       if (n_coupled == 1) {
+               /*
+                * If consumers don't provide any demands, set voltage
+                * to min_uV
+                */
+               desired_min_uV = constraints->min_uV;
+               desired_max_uV = constraints->max_uV;
+
+               ret = regulator_check_consumers(rdev,
+                                               &desired_min_uV,
+                                               &desired_max_uV, state);
+               if (ret < 0)
+                       return ret;
+
+               possible_uV = desired_min_uV;
+               done = true;
+
+               goto finish;
+       }
+
+       /* Find highest min desired voltage */
+       for (i = 0; i < n_coupled; i++) {
+               int tmp_min = 0;
+               int tmp_max = INT_MAX;
+
+               lockdep_assert_held_once(&c_rdevs[i]->mutex);
+
+               ret = regulator_check_consumers(c_rdevs[i],
+                                               &tmp_min,
+                                               &tmp_max, state);
+               if (ret < 0)
+                       return ret;
+
+               ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
+               if (ret < 0)
+                       return ret;
+
+               highest_min_uV = max(highest_min_uV, tmp_min);
+
+               if (i == 0) {
+                       desired_min_uV = tmp_min;
+                       desired_max_uV = tmp_max;
+               }
+       }
+
+       /*
+        * Let target_uV be equal to the desired one if possible.
+        * If not, set it to minimum voltage, allowed by other coupled
+        * regulators.
+        */
+       target_uV = max(desired_min_uV, highest_min_uV - max_spread);
+
+       /*
+        * Find min and max voltages, which currently aren't violating
+        * max_spread.
+        */
+       for (i = 1; i < n_coupled; i++) {
+               int tmp_act;
+
+               if (!_regulator_is_enabled(c_rdevs[i]))
+                       continue;
+
+               tmp_act = _regulator_get_voltage(c_rdevs[i]);
+               if (tmp_act < 0)
+                       return tmp_act;
+
+               min_current_uV = min(tmp_act, min_current_uV);
+               max_current_uV = max(tmp_act, max_current_uV);
+       }
+
+       /* There aren't any other regulators enabled */
+       if (max_current_uV == 0) {
+               possible_uV = target_uV;
+       } else {
+               /*
+                * Correct target voltage, so as it currently isn't
+                * violating max_spread
+                */
+               possible_uV = max(target_uV, max_current_uV - max_spread);
+               possible_uV = min(possible_uV, min_current_uV + max_spread);
+       }
+
+       if (possible_uV > desired_max_uV)
+               return -EINVAL;
+
+       done = (possible_uV == target_uV);
+       desired_min_uV = possible_uV;
+
+finish:
+       /* Set current_uV if wasn't done earlier in the code and if necessary */
+       if (n_coupled > 1 && *current_uV == -1) {
+
+               if (_regulator_is_enabled(rdev)) {
+                       ret = _regulator_get_voltage(rdev);
+                       if (ret < 0)
+                               return ret;
+
+                       *current_uV = ret;
+               } else {
+                       *current_uV = desired_min_uV;
+               }
+       }
+
+       *min_uV = desired_min_uV;
+       *max_uV = desired_max_uV;
+
+       return done;
+}
+
+static int regulator_balance_voltage(struct regulator_dev *rdev,
+                                    suspend_state_t state)
+{
+       struct regulator_dev **c_rdevs;
+       struct regulator_dev *best_rdev;
+       struct coupling_desc *c_desc = &rdev->coupling_desc;
+       int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
+       bool best_c_rdev_done, c_rdev_done[MAX_COUPLED];
+       unsigned int delta, best_delta;
+
+       c_rdevs = c_desc->coupled_rdevs;
+       n_coupled = c_desc->n_coupled;
+
+       /*
+        * If system is in a state other than PM_SUSPEND_ON, don't check
+        * other coupled regulators.
+        */
+       if (state != PM_SUSPEND_ON)
+               n_coupled = 1;
+
+       if (c_desc->n_resolved < n_coupled) {
+               rdev_err(rdev, "Not all coupled regulators registered\n");
+               return -EPERM;
+       }
+
+       for (i = 0; i < n_coupled; i++)
+               c_rdev_done[i] = false;
+
+       /*
+        * Find the best possible voltage change on each loop. Leave the loop
+        * if there isn't any possible change.
+        */
+       do {
+               best_c_rdev_done = false;
+               best_delta = 0;
+               best_min_uV = 0;
+               best_max_uV = 0;
+               best_c_rdev = 0;
+               best_rdev = NULL;
+
+               /*
+                * Find highest difference between optimal voltage
+                * and current voltage.
+                */
+               for (i = 0; i < n_coupled; i++) {
+                       /*
+                        * optimal_uV is the best voltage that can be set for
+                        * i-th regulator at the moment without violating
+                        * max_spread constraint in order to balance
+                        * the coupled voltages.
+                        */
+                       int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
+
+                       if (c_rdev_done[i])
+                               continue;
+
+                       ret = regulator_get_optimal_voltage(c_rdevs[i],
+                                                           &current_uV,
+                                                           &optimal_uV,
+                                                           &optimal_max_uV,
+                                                           state, n_coupled);
+                       if (ret < 0)
+                               goto out;
+
+                       delta = abs(optimal_uV - current_uV);
+
+                       if (delta && best_delta <= delta) {
+                               best_c_rdev_done = ret;
+                               best_delta = delta;
+                               best_rdev = c_rdevs[i];
+                               best_min_uV = optimal_uV;
+                               best_max_uV = optimal_max_uV;
+                               best_c_rdev = i;
+                       }
+               }
+
+               /* Nothing to change, return successfully */
+               if (!best_rdev) {
+                       ret = 0;
+                       goto out;
+               }
 
+               ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
+                                                best_max_uV, state);
+
+               if (ret < 0)
+                       goto out;
+
+               c_rdev_done[best_c_rdev] = best_c_rdev_done;
+
+       } while (n_coupled > 1);
+
+out:
        return ret;
 }
 
@@ -3148,12 +3430,12 @@ int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
 {
        int ret = 0;
 
-       regulator_lock_supply(regulator->rdev);
+       regulator_lock_dependent(regulator->rdev);
 
        ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
                                             PM_SUSPEND_ON);
 
-       regulator_unlock_supply(regulator->rdev);
+       regulator_unlock_dependent(regulator->rdev);
 
        return ret;
 }
@@ -3231,12 +3513,12 @@ int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
        if (regulator_check_states(state) || state == PM_SUSPEND_ON)
                return -EINVAL;
 
-       regulator_lock_supply(regulator->rdev);
+       regulator_lock_dependent(regulator->rdev);
 
        ret = _regulator_set_suspend_voltage(regulator, min_uV,
                                             max_uV, state);
 
-       regulator_unlock_supply(regulator->rdev);
+       regulator_unlock_dependent(regulator->rdev);
 
        return ret;
 }
@@ -3428,11 +3710,11 @@ int regulator_get_voltage(struct regulator *regulator)
 {
        int ret;
 
-       regulator_lock_supply(regulator->rdev);
+       regulator_lock_dependent(regulator->rdev);
 
        ret = _regulator_get_voltage(regulator->rdev);
 
-       regulator_unlock_supply(regulator->rdev);
+       regulator_unlock_dependent(regulator->rdev);
 
        return ret;
 }
@@ -4157,7 +4439,7 @@ static int regulator_register_resolve_supply(struct device *dev, void *data)
        return 0;
 }
 
-static int regulator_fill_coupling_array(struct regulator_dev *rdev)
+static void regulator_resolve_coupling(struct regulator_dev *rdev)
 {
        struct coupling_desc *c_desc = &rdev->coupling_desc;
        int n_coupled = c_desc->n_coupled;
@@ -4171,33 +4453,21 @@ static int regulator_fill_coupling_array(struct regulator_dev *rdev)
 
                c_rdev = of_parse_coupled_regulator(rdev, i - 1);
 
-               if (c_rdev) {
-                       c_desc->coupled_rdevs[i] = c_rdev;
-                       c_desc->n_resolved++;
-               }
-       }
-
-       if (rdev->coupling_desc.n_resolved < n_coupled)
-               return -1;
-       else
-               return 0;
-}
+               if (!c_rdev)
+                       continue;
 
-static int regulator_register_fill_coupling_array(struct device *dev,
-                                                 void *data)
-{
-       struct regulator_dev *rdev = dev_to_rdev(dev);
+               regulator_lock(c_rdev);
 
-       if (!IS_ENABLED(CONFIG_OF))
-               return 0;
+               c_desc->coupled_rdevs[i] = c_rdev;
+               c_desc->n_resolved++;
 
-       if (regulator_fill_coupling_array(rdev))
-               rdev_dbg(rdev, "unable to resolve coupling\n");
+               regulator_unlock(c_rdev);
 
-       return 0;
+               regulator_resolve_coupling(c_rdev);
+       }
 }
 
-static int regulator_resolve_coupling(struct regulator_dev *rdev)
+static int regulator_init_coupling(struct regulator_dev *rdev)
 {
        int n_phandles;
 
@@ -4237,13 +4507,6 @@ static int regulator_resolve_coupling(struct regulator_dev *rdev)
        if (!of_check_coupling_data(rdev))
                return -EPERM;
 
-       /*
-        * After everything has been checked, try to fill rdevs array
-        * with pointers to regulators parsed from device tree. If some
-        * regulators are not registered yet, retry in late init call
-        */
-       regulator_fill_coupling_array(rdev);
-
        return 0;
 }
 
@@ -4380,11 +4643,8 @@ regulator_register(const struct regulator_desc *regulator_desc,
        if (ret < 0)
                goto wash;
 
-       mutex_lock(&regulator_list_mutex);
-       ret = regulator_resolve_coupling(rdev);
-       mutex_unlock(&regulator_list_mutex);
-
-       if (ret != 0)
+       ret = regulator_init_coupling(rdev);
+       if (ret < 0)
                goto wash;
 
        /* add consumers devices */
@@ -4418,6 +4678,11 @@ regulator_register(const struct regulator_desc *regulator_desc,
 
        rdev_init_debugfs(rdev);
 
+       /* try to resolve regulators coupling since a new one was registered */
+       mutex_lock(&regulator_list_mutex);
+       regulator_resolve_coupling(rdev);
+       mutex_unlock(&regulator_list_mutex);
+
        /* try to resolve regulators supply since a new one was registered */
        class_for_each_device(&regulator_class, NULL, NULL,
                              regulator_register_resolve_supply);
@@ -4873,9 +5138,6 @@ static int __init regulator_init_complete(void)
        class_for_each_device(&regulator_class, NULL, NULL,
                              regulator_late_cleanup);
 
-       class_for_each_device(&regulator_class, NULL, NULL,
-                             regulator_register_fill_coupling_array);
-
        return 0;
 }
 late_initcall_sync(regulator_init_complete);