1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com/
5 * Author: Marek Szyprowski <m.szyprowski@samsung.com>
7 * Simplified generic voltage coupler from regulator core.c
8 * The main difference is that it keeps current regulator voltage
9 * if consumers didn't apply their constraints yet.
12 #include <linux/init.h>
13 #include <linux/kernel.h>
15 #include <linux/regulator/coupler.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/machine.h>
19 static int regulator_get_optimal_voltage(struct regulator_dev
*rdev
,
21 int *min_uV
, int *max_uV
,
22 suspend_state_t state
)
24 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
25 struct regulator_dev
**c_rdevs
= c_desc
->coupled_rdevs
;
26 struct regulation_constraints
*constraints
= rdev
->constraints
;
27 int desired_min_uV
= 0, desired_max_uV
= INT_MAX
;
28 int max_current_uV
= 0, min_current_uV
= INT_MAX
;
29 int highest_min_uV
= 0, target_uV
, possible_uV
;
30 int i
, ret
, max_spread
, n_coupled
= c_desc
->n_coupled
;
35 /* Find highest min desired voltage */
36 for (i
= 0; i
< n_coupled
; i
++) {
38 int tmp_max
= INT_MAX
;
40 lockdep_assert_held_once(&c_rdevs
[i
]->mutex
.base
);
42 ret
= regulator_check_consumers(c_rdevs
[i
],
49 ret
= regulator_get_voltage_rdev(c_rdevs
[i
]);
55 /* apply constraints */
56 ret
= regulator_check_voltage(c_rdevs
[i
], &tmp_min
, &tmp_max
);
60 highest_min_uV
= max(highest_min_uV
, tmp_min
);
63 desired_min_uV
= tmp_min
;
64 desired_max_uV
= tmp_max
;
68 max_spread
= constraints
->max_spread
[0];
71 * Let target_uV be equal to the desired one if possible.
72 * If not, set it to minimum voltage, allowed by other coupled
75 target_uV
= max(desired_min_uV
, highest_min_uV
- max_spread
);
78 * Find min and max voltages, which currently aren't violating
81 for (i
= 1; i
< n_coupled
; i
++) {
84 tmp_act
= regulator_get_voltage_rdev(c_rdevs
[i
]);
88 min_current_uV
= min(tmp_act
, min_current_uV
);
89 max_current_uV
= max(tmp_act
, max_current_uV
);
93 * Correct target voltage, so as it currently isn't
94 * violating max_spread
96 possible_uV
= max(target_uV
, max_current_uV
- max_spread
);
97 possible_uV
= min(possible_uV
, min_current_uV
+ max_spread
);
99 if (possible_uV
> desired_max_uV
)
102 done
= (possible_uV
== target_uV
);
103 desired_min_uV
= possible_uV
;
105 /* Set current_uV if wasn't done earlier in the code and if necessary */
106 if (*current_uV
== -1) {
107 ret
= regulator_get_voltage_rdev(rdev
);
113 *min_uV
= desired_min_uV
;
114 *max_uV
= desired_max_uV
;
119 static int exynos_coupler_balance_voltage(struct regulator_coupler
*coupler
,
120 struct regulator_dev
*rdev
,
121 suspend_state_t state
)
123 struct regulator_dev
**c_rdevs
;
124 struct regulator_dev
*best_rdev
;
125 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
126 int i
, ret
, n_coupled
, best_min_uV
, best_max_uV
, best_c_rdev
;
127 unsigned int delta
, best_delta
;
128 unsigned long c_rdev_done
= 0;
129 bool best_c_rdev_done
;
131 c_rdevs
= c_desc
->coupled_rdevs
;
132 n_coupled
= c_desc
->n_coupled
;
135 * Find the best possible voltage change on each loop. Leave the loop
136 * if there isn't any possible change.
139 best_c_rdev_done
= false;
147 * Find highest difference between optimal voltage
148 * and current voltage.
150 for (i
= 0; i
< n_coupled
; i
++) {
152 * optimal_uV is the best voltage that can be set for
153 * i-th regulator at the moment without violating
154 * max_spread constraint in order to balance
155 * the coupled voltages.
157 int optimal_uV
= 0, optimal_max_uV
= 0, current_uV
= 0;
159 if (test_bit(i
, &c_rdev_done
))
162 ret
= regulator_get_optimal_voltage(c_rdevs
[i
],
170 delta
= abs(optimal_uV
- current_uV
);
172 if (delta
&& best_delta
<= delta
) {
173 best_c_rdev_done
= ret
;
175 best_rdev
= c_rdevs
[i
];
176 best_min_uV
= optimal_uV
;
177 best_max_uV
= optimal_max_uV
;
182 /* Nothing to change, return successfully */
188 ret
= regulator_set_voltage_rdev(best_rdev
, best_min_uV
,
194 if (best_c_rdev_done
)
195 set_bit(best_c_rdev
, &c_rdev_done
);
197 } while (n_coupled
> 1);
203 static int exynos_coupler_attach(struct regulator_coupler
*coupler
,
204 struct regulator_dev
*rdev
)
209 static struct regulator_coupler exynos_coupler
= {
210 .attach_regulator
= exynos_coupler_attach
,
211 .balance_voltage
= exynos_coupler_balance_voltage
,
214 static int __init
exynos_coupler_init(void)
216 if (!of_machine_is_compatible("samsung,exynos5800"))
219 return regulator_coupler_register(&exynos_coupler
);
221 arch_initcall(exynos_coupler_init
);