aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/regulator/core.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/regulator/core.c')
-rw-r--r--drivers/regulator/core.c278
1 files changed, 216 insertions, 62 deletions
diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index c894cf0d8a28..86ae1825cec1 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -1,12 +1,11 @@
1// SPDX-License-Identifier: GPL-2.0-or-later 1// SPDX-License-Identifier: GPL-2.0-or-later
2/* 2//
3 * core.c -- Voltage/Current Regulator framework. 3// core.c -- Voltage/Current Regulator framework.
4 * 4//
5 * Copyright 2007, 2008 Wolfson Microelectronics PLC. 5// Copyright 2007, 2008 Wolfson Microelectronics PLC.
6 * Copyright 2008 SlimLogic Ltd. 6// Copyright 2008 SlimLogic Ltd.
7 * 7//
8 * Author: Liam Girdwood <lrg@slimlogic.co.uk> 8// Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 */
10 9
11#include <linux/kernel.h> 10#include <linux/kernel.h>
12#include <linux/init.h> 11#include <linux/init.h>
@@ -23,6 +22,7 @@
23#include <linux/regmap.h> 22#include <linux/regmap.h>
24#include <linux/regulator/of_regulator.h> 23#include <linux/regulator/of_regulator.h>
25#include <linux/regulator/consumer.h> 24#include <linux/regulator/consumer.h>
25#include <linux/regulator/coupler.h>
26#include <linux/regulator/driver.h> 26#include <linux/regulator/driver.h>
27#include <linux/regulator/machine.h> 27#include <linux/regulator/machine.h>
28#include <linux/module.h> 28#include <linux/module.h>
@@ -50,6 +50,7 @@ static DEFINE_MUTEX(regulator_list_mutex);
50static LIST_HEAD(regulator_map_list); 50static LIST_HEAD(regulator_map_list);
51static LIST_HEAD(regulator_ena_gpio_list); 51static LIST_HEAD(regulator_ena_gpio_list);
52static LIST_HEAD(regulator_supply_alias_list); 52static LIST_HEAD(regulator_supply_alias_list);
53static LIST_HEAD(regulator_coupler_list);
53static bool has_full_constraints; 54static bool has_full_constraints;
54 55
55static struct dentry *debugfs_root; 56static struct dentry *debugfs_root;
@@ -93,7 +94,6 @@ struct regulator_supply_alias {
93 94
94static int _regulator_is_enabled(struct regulator_dev *rdev); 95static int _regulator_is_enabled(struct regulator_dev *rdev);
95static int _regulator_disable(struct regulator *regulator); 96static int _regulator_disable(struct regulator *regulator);
96static int _regulator_get_voltage(struct regulator_dev *rdev);
97static int _regulator_get_current_limit(struct regulator_dev *rdev); 97static int _regulator_get_current_limit(struct regulator_dev *rdev);
98static unsigned int _regulator_get_mode(struct regulator_dev *rdev); 98static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
99static int _notifier_call_chain(struct regulator_dev *rdev, 99static int _notifier_call_chain(struct regulator_dev *rdev,
@@ -102,15 +102,12 @@ static int _regulator_do_set_voltage(struct regulator_dev *rdev,
102 int min_uV, int max_uV); 102 int min_uV, int max_uV);
103static int regulator_balance_voltage(struct regulator_dev *rdev, 103static int regulator_balance_voltage(struct regulator_dev *rdev,
104 suspend_state_t state); 104 suspend_state_t state);
105static int regulator_set_voltage_rdev(struct regulator_dev *rdev,
106 int min_uV, int max_uV,
107 suspend_state_t state);
108static struct regulator *create_regulator(struct regulator_dev *rdev, 105static struct regulator *create_regulator(struct regulator_dev *rdev,
109 struct device *dev, 106 struct device *dev,
110 const char *supply_name); 107 const char *supply_name);
111static void _regulator_put(struct regulator *regulator); 108static void _regulator_put(struct regulator *regulator);
112 109
113static const char *rdev_get_name(struct regulator_dev *rdev) 110const char *rdev_get_name(struct regulator_dev *rdev)
114{ 111{
115 if (rdev->constraints && rdev->constraints->name) 112 if (rdev->constraints && rdev->constraints->name)
116 return rdev->constraints->name; 113 return rdev->constraints->name;
@@ -424,8 +421,8 @@ static struct device_node *of_get_regulator(struct device *dev, const char *supp
424} 421}
425 422
426/* Platform voltage constraint check */ 423/* Platform voltage constraint check */
427static int regulator_check_voltage(struct regulator_dev *rdev, 424int regulator_check_voltage(struct regulator_dev *rdev,
428 int *min_uV, int *max_uV) 425 int *min_uV, int *max_uV)
429{ 426{
430 BUG_ON(*min_uV > *max_uV); 427 BUG_ON(*min_uV > *max_uV);
431 428
@@ -457,9 +454,9 @@ static int regulator_check_states(suspend_state_t state)
457/* Make sure we select a voltage that suits the needs of all 454/* Make sure we select a voltage that suits the needs of all
458 * regulator consumers 455 * regulator consumers
459 */ 456 */
460static int regulator_check_consumers(struct regulator_dev *rdev, 457int regulator_check_consumers(struct regulator_dev *rdev,
461 int *min_uV, int *max_uV, 458 int *min_uV, int *max_uV,
462 suspend_state_t state) 459 suspend_state_t state)
463{ 460{
464 struct regulator *regulator; 461 struct regulator *regulator;
465 struct regulator_voltage *voltage; 462 struct regulator_voltage *voltage;
@@ -570,7 +567,7 @@ static ssize_t regulator_uV_show(struct device *dev,
570 ssize_t ret; 567 ssize_t ret;
571 568
572 regulator_lock(rdev); 569 regulator_lock(rdev);
573 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev)); 570 ret = sprintf(buf, "%d\n", regulator_get_voltage_rdev(rdev));
574 regulator_unlock(rdev); 571 regulator_unlock(rdev);
575 572
576 return ret; 573 return ret;
@@ -941,7 +938,7 @@ static int drms_uA_update(struct regulator_dev *rdev)
941 rdev_err(rdev, "failed to set load %d\n", current_uA); 938 rdev_err(rdev, "failed to set load %d\n", current_uA);
942 } else { 939 } else {
943 /* get output voltage */ 940 /* get output voltage */
944 output_uV = _regulator_get_voltage(rdev); 941 output_uV = regulator_get_voltage_rdev(rdev);
945 if (output_uV <= 0) { 942 if (output_uV <= 0) {
946 rdev_err(rdev, "invalid output voltage found\n"); 943 rdev_err(rdev, "invalid output voltage found\n");
947 return -EINVAL; 944 return -EINVAL;
@@ -1054,7 +1051,7 @@ static void print_constraints(struct regulator_dev *rdev)
1054 1051
1055 if (!constraints->min_uV || 1052 if (!constraints->min_uV ||
1056 constraints->min_uV != constraints->max_uV) { 1053 constraints->min_uV != constraints->max_uV) {
1057 ret = _regulator_get_voltage(rdev); 1054 ret = regulator_get_voltage_rdev(rdev);
1058 if (ret > 0) 1055 if (ret > 0)
1059 count += scnprintf(buf + count, len - count, 1056 count += scnprintf(buf + count, len - count,
1060 "at %d mV ", ret / 1000); 1057 "at %d mV ", ret / 1000);
@@ -1113,7 +1110,7 @@ static int machine_constraints_voltage(struct regulator_dev *rdev,
1113 if (rdev->constraints->apply_uV && 1110 if (rdev->constraints->apply_uV &&
1114 rdev->constraints->min_uV && rdev->constraints->max_uV) { 1111 rdev->constraints->min_uV && rdev->constraints->max_uV) {
1115 int target_min, target_max; 1112 int target_min, target_max;
1116 int current_uV = _regulator_get_voltage(rdev); 1113 int current_uV = regulator_get_voltage_rdev(rdev);
1117 1114
1118 if (current_uV == -ENOTRECOVERABLE) { 1115 if (current_uV == -ENOTRECOVERABLE) {
1119 /* This regulator can't be read and must be initialized */ 1116 /* This regulator can't be read and must be initialized */
@@ -1123,7 +1120,7 @@ static int machine_constraints_voltage(struct regulator_dev *rdev,
1123 _regulator_do_set_voltage(rdev, 1120 _regulator_do_set_voltage(rdev,
1124 rdev->constraints->min_uV, 1121 rdev->constraints->min_uV,
1125 rdev->constraints->max_uV); 1122 rdev->constraints->max_uV);
1126 current_uV = _regulator_get_voltage(rdev); 1123 current_uV = regulator_get_voltage_rdev(rdev);
1127 } 1124 }
1128 1125
1129 if (current_uV < 0) { 1126 if (current_uV < 0) {
@@ -1645,9 +1642,9 @@ static int _regulator_get_enable_time(struct regulator_dev *rdev)
1645{ 1642{
1646 if (rdev->constraints && rdev->constraints->enable_time) 1643 if (rdev->constraints && rdev->constraints->enable_time)
1647 return rdev->constraints->enable_time; 1644 return rdev->constraints->enable_time;
1648 if (!rdev->desc->ops->enable_time) 1645 if (rdev->desc->ops->enable_time)
1649 return rdev->desc->enable_time; 1646 return rdev->desc->ops->enable_time(rdev);
1650 return rdev->desc->ops->enable_time(rdev); 1647 return rdev->desc->enable_time;
1651} 1648}
1652 1649
1653static struct regulator_supply_alias *regulator_find_supply_alias( 1650static struct regulator_supply_alias *regulator_find_supply_alias(
@@ -3065,7 +3062,7 @@ static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3065 struct pre_voltage_change_data data; 3062 struct pre_voltage_change_data data;
3066 int ret; 3063 int ret;
3067 3064
3068 data.old_uV = _regulator_get_voltage(rdev); 3065 data.old_uV = regulator_get_voltage_rdev(rdev);
3069 data.min_uV = min_uV; 3066 data.min_uV = min_uV;
3070 data.max_uV = max_uV; 3067 data.max_uV = max_uV;
3071 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, 3068 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
@@ -3089,7 +3086,7 @@ static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3089 struct pre_voltage_change_data data; 3086 struct pre_voltage_change_data data;
3090 int ret; 3087 int ret;
3091 3088
3092 data.old_uV = _regulator_get_voltage(rdev); 3089 data.old_uV = regulator_get_voltage_rdev(rdev);
3093 data.min_uV = uV; 3090 data.min_uV = uV;
3094 data.max_uV = uV; 3091 data.max_uV = uV;
3095 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, 3092 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
@@ -3107,6 +3104,66 @@ static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3107 return ret; 3104 return ret;
3108} 3105}
3109 3106
3107static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3108 int uV, int new_selector)
3109{
3110 const struct regulator_ops *ops = rdev->desc->ops;
3111 int diff, old_sel, curr_sel, ret;
3112
3113 /* Stepping is only needed if the regulator is enabled. */
3114 if (!_regulator_is_enabled(rdev))
3115 goto final_set;
3116
3117 if (!ops->get_voltage_sel)
3118 return -EINVAL;
3119
3120 old_sel = ops->get_voltage_sel(rdev);
3121 if (old_sel < 0)
3122 return old_sel;
3123
3124 diff = new_selector - old_sel;
3125 if (diff == 0)
3126 return 0; /* No change needed. */
3127
3128 if (diff > 0) {
3129 /* Stepping up. */
3130 for (curr_sel = old_sel + rdev->desc->vsel_step;
3131 curr_sel < new_selector;
3132 curr_sel += rdev->desc->vsel_step) {
3133 /*
3134 * Call the callback directly instead of using
3135 * _regulator_call_set_voltage_sel() as we don't
3136 * want to notify anyone yet. Same in the branch
3137 * below.
3138 */
3139 ret = ops->set_voltage_sel(rdev, curr_sel);
3140 if (ret)
3141 goto try_revert;
3142 }
3143 } else {
3144 /* Stepping down. */
3145 for (curr_sel = old_sel - rdev->desc->vsel_step;
3146 curr_sel > new_selector;
3147 curr_sel -= rdev->desc->vsel_step) {
3148 ret = ops->set_voltage_sel(rdev, curr_sel);
3149 if (ret)
3150 goto try_revert;
3151 }
3152 }
3153
3154final_set:
3155 /* The final selector will trigger the notifiers. */
3156 return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3157
3158try_revert:
3159 /*
3160 * At least try to return to the previous voltage if setting a new
3161 * one failed.
3162 */
3163 (void)ops->set_voltage_sel(rdev, old_sel);
3164 return ret;
3165}
3166
3110static int _regulator_set_voltage_time(struct regulator_dev *rdev, 3167static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3111 int old_uV, int new_uV) 3168 int old_uV, int new_uV)
3112{ 3169{
@@ -3142,7 +3199,7 @@ static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3142 unsigned int selector; 3199 unsigned int selector;
3143 int old_selector = -1; 3200 int old_selector = -1;
3144 const struct regulator_ops *ops = rdev->desc->ops; 3201 const struct regulator_ops *ops = rdev->desc->ops;
3145 int old_uV = _regulator_get_voltage(rdev); 3202 int old_uV = regulator_get_voltage_rdev(rdev);
3146 3203
3147 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV); 3204 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3148 3205
@@ -3169,7 +3226,7 @@ static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3169 best_val = ops->list_voltage(rdev, 3226 best_val = ops->list_voltage(rdev,
3170 selector); 3227 selector);
3171 else 3228 else
3172 best_val = _regulator_get_voltage(rdev); 3229 best_val = regulator_get_voltage_rdev(rdev);
3173 } 3230 }
3174 3231
3175 } else if (ops->set_voltage_sel) { 3232 } else if (ops->set_voltage_sel) {
@@ -3180,6 +3237,9 @@ static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3180 selector = ret; 3237 selector = ret;
3181 if (old_selector == selector) 3238 if (old_selector == selector)
3182 ret = 0; 3239 ret = 0;
3240 else if (rdev->desc->vsel_step)
3241 ret = _regulator_set_voltage_sel_step(
3242 rdev, best_val, selector);
3183 else 3243 else
3184 ret = _regulator_call_set_voltage_sel( 3244 ret = _regulator_call_set_voltage_sel(
3185 rdev, best_val, selector); 3245 rdev, best_val, selector);
@@ -3288,7 +3348,7 @@ static int regulator_set_voltage_unlocked(struct regulator *regulator,
3288 * changing the voltage. 3348 * changing the voltage.
3289 */ 3349 */
3290 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { 3350 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3291 current_uV = _regulator_get_voltage(rdev); 3351 current_uV = regulator_get_voltage_rdev(rdev);
3292 if (min_uV <= current_uV && current_uV <= max_uV) { 3352 if (min_uV <= current_uV && current_uV <= max_uV) {
3293 voltage->min_uV = min_uV; 3353 voltage->min_uV = min_uV;
3294 voltage->max_uV = max_uV; 3354 voltage->max_uV = max_uV;
@@ -3325,8 +3385,8 @@ out:
3325 return ret; 3385 return ret;
3326} 3386}
3327 3387
3328static int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV, 3388int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3329 int max_uV, suspend_state_t state) 3389 int max_uV, suspend_state_t state)
3330{ 3390{
3331 int best_supply_uV = 0; 3391 int best_supply_uV = 0;
3332 int supply_change_uV = 0; 3392 int supply_change_uV = 0;
@@ -3354,7 +3414,7 @@ static int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3354 3414
3355 best_supply_uV += rdev->desc->min_dropout_uV; 3415 best_supply_uV += rdev->desc->min_dropout_uV;
3356 3416
3357 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev); 3417 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3358 if (current_supply_uV < 0) { 3418 if (current_supply_uV < 0) {
3359 ret = current_supply_uV; 3419 ret = current_supply_uV;
3360 goto out; 3420 goto out;
@@ -3405,7 +3465,7 @@ static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3405 return 1; 3465 return 1;
3406 3466
3407 if (*current_uV < 0) { 3467 if (*current_uV < 0) {
3408 *current_uV = _regulator_get_voltage(rdev); 3468 *current_uV = regulator_get_voltage_rdev(rdev);
3409 3469
3410 if (*current_uV < 0) 3470 if (*current_uV < 0)
3411 return *current_uV; 3471 return *current_uV;
@@ -3434,11 +3494,10 @@ static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3434 struct coupling_desc *c_desc = &rdev->coupling_desc; 3494 struct coupling_desc *c_desc = &rdev->coupling_desc;
3435 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs; 3495 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3436 struct regulation_constraints *constraints = rdev->constraints; 3496 struct regulation_constraints *constraints = rdev->constraints;
3437 int max_spread = constraints->max_spread;
3438 int desired_min_uV = 0, desired_max_uV = INT_MAX; 3497 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3439 int max_current_uV = 0, min_current_uV = INT_MAX; 3498 int max_current_uV = 0, min_current_uV = INT_MAX;
3440 int highest_min_uV = 0, target_uV, possible_uV; 3499 int highest_min_uV = 0, target_uV, possible_uV;
3441 int i, ret; 3500 int i, ret, max_spread;
3442 bool done; 3501 bool done;
3443 3502
3444 *current_uV = -1; 3503 *current_uV = -1;
@@ -3492,6 +3551,8 @@ static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3492 } 3551 }
3493 } 3552 }
3494 3553
3554 max_spread = constraints->max_spread[0];
3555
3495 /* 3556 /*
3496 * Let target_uV be equal to the desired one if possible. 3557 * Let target_uV be equal to the desired one if possible.
3497 * If not, set it to minimum voltage, allowed by other coupled 3558 * If not, set it to minimum voltage, allowed by other coupled
@@ -3509,7 +3570,7 @@ static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3509 if (!_regulator_is_enabled(c_rdevs[i])) 3570 if (!_regulator_is_enabled(c_rdevs[i]))
3510 continue; 3571 continue;
3511 3572
3512 tmp_act = _regulator_get_voltage(c_rdevs[i]); 3573 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3513 if (tmp_act < 0) 3574 if (tmp_act < 0)
3514 return tmp_act; 3575 return tmp_act;
3515 3576
@@ -3551,7 +3612,7 @@ finish:
3551 if (n_coupled > 1 && *current_uV == -1) { 3612 if (n_coupled > 1 && *current_uV == -1) {
3552 3613
3553 if (_regulator_is_enabled(rdev)) { 3614 if (_regulator_is_enabled(rdev)) {
3554 ret = _regulator_get_voltage(rdev); 3615 ret = regulator_get_voltage_rdev(rdev);
3555 if (ret < 0) 3616 if (ret < 0)
3556 return ret; 3617 return ret;
3557 3618
@@ -3573,9 +3634,11 @@ static int regulator_balance_voltage(struct regulator_dev *rdev,
3573 struct regulator_dev **c_rdevs; 3634 struct regulator_dev **c_rdevs;
3574 struct regulator_dev *best_rdev; 3635 struct regulator_dev *best_rdev;
3575 struct coupling_desc *c_desc = &rdev->coupling_desc; 3636 struct coupling_desc *c_desc = &rdev->coupling_desc;
3637 struct regulator_coupler *coupler = c_desc->coupler;
3576 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev; 3638 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3577 bool best_c_rdev_done, c_rdev_done[MAX_COUPLED];
3578 unsigned int delta, best_delta; 3639 unsigned int delta, best_delta;
3640 unsigned long c_rdev_done = 0;
3641 bool best_c_rdev_done;
3579 3642
3580 c_rdevs = c_desc->coupled_rdevs; 3643 c_rdevs = c_desc->coupled_rdevs;
3581 n_coupled = c_desc->n_coupled; 3644 n_coupled = c_desc->n_coupled;
@@ -3592,8 +3655,9 @@ static int regulator_balance_voltage(struct regulator_dev *rdev,
3592 return -EPERM; 3655 return -EPERM;
3593 } 3656 }
3594 3657
3595 for (i = 0; i < n_coupled; i++) 3658 /* Invoke custom balancer for customized couplers */
3596 c_rdev_done[i] = false; 3659 if (coupler && coupler->balance_voltage)
3660 return coupler->balance_voltage(coupler, rdev, state);
3597 3661
3598 /* 3662 /*
3599 * Find the best possible voltage change on each loop. Leave the loop 3663 * Find the best possible voltage change on each loop. Leave the loop
@@ -3620,7 +3684,7 @@ static int regulator_balance_voltage(struct regulator_dev *rdev,
3620 */ 3684 */
3621 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0; 3685 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3622 3686
3623 if (c_rdev_done[i]) 3687 if (test_bit(i, &c_rdev_done))
3624 continue; 3688 continue;
3625 3689
3626 ret = regulator_get_optimal_voltage(c_rdevs[i], 3690 ret = regulator_get_optimal_voltage(c_rdevs[i],
@@ -3655,7 +3719,8 @@ static int regulator_balance_voltage(struct regulator_dev *rdev,
3655 if (ret < 0) 3719 if (ret < 0)
3656 goto out; 3720 goto out;
3657 3721
3658 c_rdev_done[best_c_rdev] = best_c_rdev_done; 3722 if (best_c_rdev_done)
3723 set_bit(best_c_rdev, &c_rdev_done);
3659 3724
3660 } while (n_coupled > 1); 3725 } while (n_coupled > 1);
3661 3726
@@ -3911,7 +3976,7 @@ out:
3911} 3976}
3912EXPORT_SYMBOL_GPL(regulator_sync_voltage); 3977EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3913 3978
3914static int _regulator_get_voltage(struct regulator_dev *rdev) 3979int regulator_get_voltage_rdev(struct regulator_dev *rdev)
3915{ 3980{
3916 int sel, ret; 3981 int sel, ret;
3917 bool bypassed; 3982 bool bypassed;
@@ -3928,7 +3993,7 @@ static int _regulator_get_voltage(struct regulator_dev *rdev)
3928 return -EPROBE_DEFER; 3993 return -EPROBE_DEFER;
3929 } 3994 }
3930 3995
3931 return _regulator_get_voltage(rdev->supply->rdev); 3996 return regulator_get_voltage_rdev(rdev->supply->rdev);
3932 } 3997 }
3933 } 3998 }
3934 3999
@@ -3944,7 +4009,7 @@ static int _regulator_get_voltage(struct regulator_dev *rdev)
3944 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) { 4009 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3945 ret = rdev->desc->fixed_uV; 4010 ret = rdev->desc->fixed_uV;
3946 } else if (rdev->supply) { 4011 } else if (rdev->supply) {
3947 ret = _regulator_get_voltage(rdev->supply->rdev); 4012 ret = regulator_get_voltage_rdev(rdev->supply->rdev);
3948 } else { 4013 } else {
3949 return -EINVAL; 4014 return -EINVAL;
3950 } 4015 }
@@ -3969,7 +4034,7 @@ int regulator_get_voltage(struct regulator *regulator)
3969 int ret; 4034 int ret;
3970 4035
3971 regulator_lock_dependent(regulator->rdev, &ww_ctx); 4036 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3972 ret = _regulator_get_voltage(regulator->rdev); 4037 ret = regulator_get_voltage_rdev(regulator->rdev);
3973 regulator_unlock_dependent(regulator->rdev, &ww_ctx); 4038 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3974 4039
3975 return ret; 4040 return ret;
@@ -4707,8 +4772,60 @@ static int regulator_register_resolve_supply(struct device *dev, void *data)
4707 return 0; 4772 return 0;
4708} 4773}
4709 4774
4775int regulator_coupler_register(struct regulator_coupler *coupler)
4776{
4777 mutex_lock(&regulator_list_mutex);
4778 list_add_tail(&coupler->list, &regulator_coupler_list);
4779 mutex_unlock(&regulator_list_mutex);
4780
4781 return 0;
4782}
4783
4784static struct regulator_coupler *
4785regulator_find_coupler(struct regulator_dev *rdev)
4786{
4787 struct regulator_coupler *coupler;
4788 int err;
4789
4790 /*
4791 * Note that regulators are appended to the list and the generic
4792 * coupler is registered first, hence it will be attached at last
4793 * if nobody cared.
4794 */
4795 list_for_each_entry_reverse(coupler, &regulator_coupler_list, list) {
4796 err = coupler->attach_regulator(coupler, rdev);
4797 if (!err) {
4798 if (!coupler->balance_voltage &&
4799 rdev->coupling_desc.n_coupled > 2)
4800 goto err_unsupported;
4801
4802 return coupler;
4803 }
4804
4805 if (err < 0)
4806 return ERR_PTR(err);
4807
4808 if (err == 1)
4809 continue;
4810
4811 break;
4812 }
4813
4814 return ERR_PTR(-EINVAL);
4815
4816err_unsupported:
4817 if (coupler->detach_regulator)
4818 coupler->detach_regulator(coupler, rdev);
4819
4820 rdev_err(rdev,
4821 "Voltage balancing for multiple regulator couples is unimplemented\n");
4822
4823 return ERR_PTR(-EPERM);
4824}
4825
4710static void regulator_resolve_coupling(struct regulator_dev *rdev) 4826static void regulator_resolve_coupling(struct regulator_dev *rdev)
4711{ 4827{
4828 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4712 struct coupling_desc *c_desc = &rdev->coupling_desc; 4829 struct coupling_desc *c_desc = &rdev->coupling_desc;
4713 int n_coupled = c_desc->n_coupled; 4830 int n_coupled = c_desc->n_coupled;
4714 struct regulator_dev *c_rdev; 4831 struct regulator_dev *c_rdev;
@@ -4724,6 +4841,12 @@ static void regulator_resolve_coupling(struct regulator_dev *rdev)
4724 if (!c_rdev) 4841 if (!c_rdev)
4725 continue; 4842 continue;
4726 4843
4844 if (c_rdev->coupling_desc.coupler != coupler) {
4845 rdev_err(rdev, "coupler mismatch with %s\n",
4846 rdev_get_name(c_rdev));
4847 return;
4848 }
4849
4727 regulator_lock(c_rdev); 4850 regulator_lock(c_rdev);
4728 4851
4729 c_desc->coupled_rdevs[i] = c_rdev; 4852 c_desc->coupled_rdevs[i] = c_rdev;
@@ -4737,10 +4860,12 @@ static void regulator_resolve_coupling(struct regulator_dev *rdev)
4737 4860
4738static void regulator_remove_coupling(struct regulator_dev *rdev) 4861static void regulator_remove_coupling(struct regulator_dev *rdev)
4739{ 4862{
4863 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4740 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc; 4864 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
4741 struct regulator_dev *__c_rdev, *c_rdev; 4865 struct regulator_dev *__c_rdev, *c_rdev;
4742 unsigned int __n_coupled, n_coupled; 4866 unsigned int __n_coupled, n_coupled;
4743 int i, k; 4867 int i, k;
4868 int err;
4744 4869
4745 n_coupled = c_desc->n_coupled; 4870 n_coupled = c_desc->n_coupled;
4746 4871
@@ -4770,21 +4895,33 @@ static void regulator_remove_coupling(struct regulator_dev *rdev)
4770 c_desc->coupled_rdevs[i] = NULL; 4895 c_desc->coupled_rdevs[i] = NULL;
4771 c_desc->n_resolved--; 4896 c_desc->n_resolved--;
4772 } 4897 }
4898
4899 if (coupler && coupler->detach_regulator) {
4900 err = coupler->detach_regulator(coupler, rdev);
4901 if (err)
4902 rdev_err(rdev, "failed to detach from coupler: %d\n",
4903 err);
4904 }
4905
4906 kfree(rdev->coupling_desc.coupled_rdevs);
4907 rdev->coupling_desc.coupled_rdevs = NULL;
4773} 4908}
4774 4909
4775static int regulator_init_coupling(struct regulator_dev *rdev) 4910static int regulator_init_coupling(struct regulator_dev *rdev)
4776{ 4911{
4777 int n_phandles; 4912 int err, n_phandles;
4913 size_t alloc_size;
4778 4914
4779 if (!IS_ENABLED(CONFIG_OF)) 4915 if (!IS_ENABLED(CONFIG_OF))
4780 n_phandles = 0; 4916 n_phandles = 0;
4781 else 4917 else
4782 n_phandles = of_get_n_coupled(rdev); 4918 n_phandles = of_get_n_coupled(rdev);
4783 4919
4784 if (n_phandles + 1 > MAX_COUPLED) { 4920 alloc_size = sizeof(*rdev) * (n_phandles + 1);
4785 rdev_err(rdev, "too many regulators coupled\n"); 4921
4786 return -EPERM; 4922 rdev->coupling_desc.coupled_rdevs = kzalloc(alloc_size, GFP_KERNEL);
4787 } 4923 if (!rdev->coupling_desc.coupled_rdevs)
4924 return -ENOMEM;
4788 4925
4789 /* 4926 /*
4790 * Every regulator should always have coupling descriptor filled with 4927 * Every regulator should always have coupling descriptor filled with
@@ -4798,23 +4935,35 @@ static int regulator_init_coupling(struct regulator_dev *rdev)
4798 if (n_phandles == 0) 4935 if (n_phandles == 0)
4799 return 0; 4936 return 0;
4800 4937
4801 /* regulator, which can't change its voltage, can't be coupled */ 4938 if (!of_check_coupling_data(rdev))
4802 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
4803 rdev_err(rdev, "voltage operation not allowed\n");
4804 return -EPERM; 4939 return -EPERM;
4805 }
4806 4940
4807 if (rdev->constraints->max_spread <= 0) { 4941 rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
4808 rdev_err(rdev, "wrong max_spread value\n"); 4942 if (IS_ERR(rdev->coupling_desc.coupler)) {
4809 return -EPERM; 4943 err = PTR_ERR(rdev->coupling_desc.coupler);
4944 rdev_err(rdev, "failed to get coupler: %d\n", err);
4945 return err;
4810 } 4946 }
4811 4947
4812 if (!of_check_coupling_data(rdev)) 4948 return 0;
4949}
4950
4951static int generic_coupler_attach(struct regulator_coupler *coupler,
4952 struct regulator_dev *rdev)
4953{
4954 if (rdev->coupling_desc.n_coupled > 2) {
4955 rdev_err(rdev,
4956 "Voltage balancing for multiple regulator couples is unimplemented\n");
4813 return -EPERM; 4957 return -EPERM;
4958 }
4814 4959
4815 return 0; 4960 return 0;
4816} 4961}
4817 4962
4963static struct regulator_coupler generic_regulator_coupler = {
4964 .attach_regulator = generic_coupler_attach,
4965};
4966
4818/** 4967/**
4819 * regulator_register - register regulator 4968 * regulator_register - register regulator
4820 * @regulator_desc: regulator to register 4969 * @regulator_desc: regulator to register
@@ -4976,7 +5125,9 @@ regulator_register(const struct regulator_desc *regulator_desc,
4976 if (ret < 0) 5125 if (ret < 0)
4977 goto wash; 5126 goto wash;
4978 5127
5128 mutex_lock(&regulator_list_mutex);
4979 ret = regulator_init_coupling(rdev); 5129 ret = regulator_init_coupling(rdev);
5130 mutex_unlock(&regulator_list_mutex);
4980 if (ret < 0) 5131 if (ret < 0)
4981 goto wash; 5132 goto wash;
4982 5133
@@ -5025,6 +5176,7 @@ regulator_register(const struct regulator_desc *regulator_desc,
5025unset_supplies: 5176unset_supplies:
5026 mutex_lock(&regulator_list_mutex); 5177 mutex_lock(&regulator_list_mutex);
5027 unset_regulator_supplies(rdev); 5178 unset_regulator_supplies(rdev);
5179 regulator_remove_coupling(rdev);
5028 mutex_unlock(&regulator_list_mutex); 5180 mutex_unlock(&regulator_list_mutex);
5029wash: 5181wash:
5030 kfree(rdev->constraints); 5182 kfree(rdev->constraints);
@@ -5278,7 +5430,7 @@ static void regulator_summary_show_subtree(struct seq_file *s,
5278 rdev->use_count, rdev->open_count, rdev->bypass_count, 5430 rdev->use_count, rdev->open_count, rdev->bypass_count,
5279 regulator_opmode_to_str(opmode)); 5431 regulator_opmode_to_str(opmode));
5280 5432
5281 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000); 5433 seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
5282 seq_printf(s, "%5dmA ", 5434 seq_printf(s, "%5dmA ",
5283 _regulator_get_current_limit_unlocked(rdev) / 1000); 5435 _regulator_get_current_limit_unlocked(rdev) / 1000);
5284 5436
@@ -5480,6 +5632,8 @@ static int __init regulator_init(void)
5480#endif 5632#endif
5481 regulator_dummy_init(); 5633 regulator_dummy_init();
5482 5634
5635 regulator_coupler_register(&generic_regulator_coupler);
5636
5483 return ret; 5637 return ret;
5484} 5638}
5485 5639