dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / soc / tegra / regulators-tegra30.c
blob7f21f31de09d6822b82569461264c7de342c749d
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Voltage regulators coupler for NVIDIA Tegra30
4 * Copyright (C) 2019 GRATE-DRIVER project
6 * Voltage constraints borrowed from downstream kernel sources
7 * Copyright (C) 2010-2011 NVIDIA Corporation
8 */
10 #define pr_fmt(fmt) "tegra voltage-coupler: " fmt
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/of.h>
15 #include <linux/regulator/coupler.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/machine.h>
19 #include <soc/tegra/fuse.h>
21 struct tegra_regulator_coupler {
22 struct regulator_coupler coupler;
23 struct regulator_dev *core_rdev;
24 struct regulator_dev *cpu_rdev;
25 int core_min_uV;
28 static inline struct tegra_regulator_coupler *
29 to_tegra_coupler(struct regulator_coupler *coupler)
31 return container_of(coupler, struct tegra_regulator_coupler, coupler);
34 static int tegra30_core_limit(struct tegra_regulator_coupler *tegra,
35 struct regulator_dev *core_rdev)
37 int core_min_uV = 0;
38 int core_max_uV;
39 int core_cur_uV;
40 int err;
42 if (tegra->core_min_uV > 0)
43 return tegra->core_min_uV;
45 core_cur_uV = regulator_get_voltage_rdev(core_rdev);
46 if (core_cur_uV < 0)
47 return core_cur_uV;
49 core_max_uV = max(core_cur_uV, 1200000);
51 err = regulator_check_voltage(core_rdev, &core_min_uV, &core_max_uV);
52 if (err)
53 return err;
56 * Limit minimum CORE voltage to a value left from bootloader or,
57 * if it's unreasonably low value, to the most common 1.2v or to
58 * whatever maximum value defined via board's device-tree.
60 tegra->core_min_uV = core_max_uV;
62 pr_info("core minimum voltage limited to %duV\n", tegra->core_min_uV);
64 return tegra->core_min_uV;
67 static int tegra30_core_cpu_limit(int cpu_uV)
69 if (cpu_uV < 800000)
70 return 950000;
72 if (cpu_uV < 900000)
73 return 1000000;
75 if (cpu_uV < 1000000)
76 return 1100000;
78 if (cpu_uV < 1100000)
79 return 1200000;
81 if (cpu_uV < 1250000) {
82 switch (tegra_sku_info.cpu_speedo_id) {
83 case 0 ... 1:
84 case 4:
85 case 7 ... 8:
86 return 1200000;
88 default:
89 return 1300000;
93 return -EINVAL;
96 static int tegra30_voltage_update(struct tegra_regulator_coupler *tegra,
97 struct regulator_dev *cpu_rdev,
98 struct regulator_dev *core_rdev)
100 int core_min_uV, core_max_uV = INT_MAX;
101 int cpu_min_uV, cpu_max_uV = INT_MAX;
102 int cpu_min_uV_consumers = 0;
103 int core_min_limited_uV;
104 int core_target_uV;
105 int cpu_target_uV;
106 int core_max_step;
107 int cpu_max_step;
108 int max_spread;
109 int core_uV;
110 int cpu_uV;
111 int err;
114 * CPU voltage should not got lower than 300mV from the CORE.
115 * CPU voltage should stay below the CORE by 100mV+, depending
116 * by the CORE voltage. This applies to all Tegra30 SoC's.
118 max_spread = cpu_rdev->constraints->max_spread[0];
119 cpu_max_step = cpu_rdev->constraints->max_uV_step;
120 core_max_step = core_rdev->constraints->max_uV_step;
122 if (!max_spread) {
123 pr_err_once("cpu-core max-spread is undefined in device-tree\n");
124 max_spread = 300000;
127 if (!cpu_max_step) {
128 pr_err_once("cpu max-step is undefined in device-tree\n");
129 cpu_max_step = 150000;
132 if (!core_max_step) {
133 pr_err_once("core max-step is undefined in device-tree\n");
134 core_max_step = 150000;
138 * The CORE voltage scaling is currently not hooked up in drivers,
139 * hence we will limit the minimum CORE voltage to a reasonable value.
140 * This should be good enough for the time being.
142 core_min_uV = tegra30_core_limit(tegra, core_rdev);
143 if (core_min_uV < 0)
144 return core_min_uV;
146 err = regulator_check_consumers(core_rdev, &core_min_uV, &core_max_uV,
147 PM_SUSPEND_ON);
148 if (err)
149 return err;
151 core_uV = regulator_get_voltage_rdev(core_rdev);
152 if (core_uV < 0)
153 return core_uV;
155 cpu_min_uV = core_min_uV - max_spread;
157 err = regulator_check_consumers(cpu_rdev, &cpu_min_uV, &cpu_max_uV,
158 PM_SUSPEND_ON);
159 if (err)
160 return err;
162 err = regulator_check_consumers(cpu_rdev, &cpu_min_uV_consumers,
163 &cpu_max_uV, PM_SUSPEND_ON);
164 if (err)
165 return err;
167 err = regulator_check_voltage(cpu_rdev, &cpu_min_uV, &cpu_max_uV);
168 if (err)
169 return err;
171 cpu_uV = regulator_get_voltage_rdev(cpu_rdev);
172 if (cpu_uV < 0)
173 return cpu_uV;
176 * CPU's regulator may not have any consumers, hence the voltage
177 * must not be changed in that case because CPU simply won't
178 * survive the voltage drop if it's running on a higher frequency.
180 if (!cpu_min_uV_consumers)
181 cpu_min_uV = cpu_uV;
184 * Bootloader shall set up voltages correctly, but if it
185 * happens that there is a violation, then try to fix it
186 * at first.
188 core_min_limited_uV = tegra30_core_cpu_limit(cpu_uV);
189 if (core_min_limited_uV < 0)
190 return core_min_limited_uV;
192 core_min_uV = max(core_min_uV, tegra30_core_cpu_limit(cpu_min_uV));
194 err = regulator_check_voltage(core_rdev, &core_min_uV, &core_max_uV);
195 if (err)
196 return err;
198 if (core_min_limited_uV > core_uV) {
199 pr_err("core voltage constraint violated: %d %d %d\n",
200 core_uV, core_min_limited_uV, cpu_uV);
201 goto update_core;
204 while (cpu_uV != cpu_min_uV || core_uV != core_min_uV) {
205 if (cpu_uV < cpu_min_uV) {
206 cpu_target_uV = min(cpu_uV + cpu_max_step, cpu_min_uV);
207 } else {
208 cpu_target_uV = max(cpu_uV - cpu_max_step, cpu_min_uV);
209 cpu_target_uV = max(core_uV - max_spread, cpu_target_uV);
212 if (cpu_uV == cpu_target_uV)
213 goto update_core;
215 err = regulator_set_voltage_rdev(cpu_rdev,
216 cpu_target_uV,
217 cpu_max_uV,
218 PM_SUSPEND_ON);
219 if (err)
220 return err;
222 cpu_uV = cpu_target_uV;
223 update_core:
224 core_min_limited_uV = tegra30_core_cpu_limit(cpu_uV);
225 if (core_min_limited_uV < 0)
226 return core_min_limited_uV;
228 core_target_uV = max(core_min_limited_uV, core_min_uV);
230 if (core_uV < core_target_uV) {
231 core_target_uV = min(core_target_uV, core_uV + core_max_step);
232 core_target_uV = min(core_target_uV, cpu_uV + max_spread);
233 } else {
234 core_target_uV = max(core_target_uV, core_uV - core_max_step);
237 if (core_uV == core_target_uV)
238 continue;
240 err = regulator_set_voltage_rdev(core_rdev,
241 core_target_uV,
242 core_max_uV,
243 PM_SUSPEND_ON);
244 if (err)
245 return err;
247 core_uV = core_target_uV;
250 return 0;
253 static int tegra30_regulator_balance_voltage(struct regulator_coupler *coupler,
254 struct regulator_dev *rdev,
255 suspend_state_t state)
257 struct tegra_regulator_coupler *tegra = to_tegra_coupler(coupler);
258 struct regulator_dev *core_rdev = tegra->core_rdev;
259 struct regulator_dev *cpu_rdev = tegra->cpu_rdev;
261 if ((core_rdev != rdev && cpu_rdev != rdev) || state != PM_SUSPEND_ON) {
262 pr_err("regulators are not coupled properly\n");
263 return -EINVAL;
266 return tegra30_voltage_update(tegra, cpu_rdev, core_rdev);
269 static int tegra30_regulator_attach(struct regulator_coupler *coupler,
270 struct regulator_dev *rdev)
272 struct tegra_regulator_coupler *tegra = to_tegra_coupler(coupler);
273 struct device_node *np = rdev->dev.of_node;
275 if (of_property_read_bool(np, "nvidia,tegra-core-regulator") &&
276 !tegra->core_rdev) {
277 tegra->core_rdev = rdev;
278 return 0;
281 if (of_property_read_bool(np, "nvidia,tegra-cpu-regulator") &&
282 !tegra->cpu_rdev) {
283 tegra->cpu_rdev = rdev;
284 return 0;
287 return -EINVAL;
290 static int tegra30_regulator_detach(struct regulator_coupler *coupler,
291 struct regulator_dev *rdev)
293 struct tegra_regulator_coupler *tegra = to_tegra_coupler(coupler);
295 if (tegra->core_rdev == rdev) {
296 tegra->core_rdev = NULL;
297 return 0;
300 if (tegra->cpu_rdev == rdev) {
301 tegra->cpu_rdev = NULL;
302 return 0;
305 return -EINVAL;
308 static struct tegra_regulator_coupler tegra30_coupler = {
309 .coupler = {
310 .attach_regulator = tegra30_regulator_attach,
311 .detach_regulator = tegra30_regulator_detach,
312 .balance_voltage = tegra30_regulator_balance_voltage,
316 static int __init tegra_regulator_coupler_init(void)
318 if (!of_machine_is_compatible("nvidia,tegra30"))
319 return 0;
321 return regulator_coupler_register(&tegra30_coupler.coupler);
323 arch_initcall(tegra_regulator_coupler_init);