treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / soc / tegra / regulators-tegra30.c
blob8e623ff18e70b1d0f47d957cb0717dc9d2f10f5f
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 err = regulator_set_voltage_rdev(cpu_rdev,
213 cpu_target_uV,
214 cpu_max_uV,
215 PM_SUSPEND_ON);
216 if (err)
217 return err;
219 cpu_uV = cpu_target_uV;
220 update_core:
221 core_min_limited_uV = tegra30_core_cpu_limit(cpu_uV);
222 if (core_min_limited_uV < 0)
223 return core_min_limited_uV;
225 core_target_uV = max(core_min_limited_uV, core_min_uV);
227 if (core_uV < core_target_uV) {
228 core_target_uV = min(core_target_uV, core_uV + core_max_step);
229 core_target_uV = min(core_target_uV, cpu_uV + max_spread);
230 } else {
231 core_target_uV = max(core_target_uV, core_uV - core_max_step);
234 err = regulator_set_voltage_rdev(core_rdev,
235 core_target_uV,
236 core_max_uV,
237 PM_SUSPEND_ON);
238 if (err)
239 return err;
241 core_uV = core_target_uV;
244 return 0;
247 static int tegra30_regulator_balance_voltage(struct regulator_coupler *coupler,
248 struct regulator_dev *rdev,
249 suspend_state_t state)
251 struct tegra_regulator_coupler *tegra = to_tegra_coupler(coupler);
252 struct regulator_dev *core_rdev = tegra->core_rdev;
253 struct regulator_dev *cpu_rdev = tegra->cpu_rdev;
255 if ((core_rdev != rdev && cpu_rdev != rdev) || state != PM_SUSPEND_ON) {
256 pr_err("regulators are not coupled properly\n");
257 return -EINVAL;
260 return tegra30_voltage_update(tegra, cpu_rdev, core_rdev);
263 static int tegra30_regulator_attach(struct regulator_coupler *coupler,
264 struct regulator_dev *rdev)
266 struct tegra_regulator_coupler *tegra = to_tegra_coupler(coupler);
267 struct device_node *np = rdev->dev.of_node;
269 if (of_property_read_bool(np, "nvidia,tegra-core-regulator") &&
270 !tegra->core_rdev) {
271 tegra->core_rdev = rdev;
272 return 0;
275 if (of_property_read_bool(np, "nvidia,tegra-cpu-regulator") &&
276 !tegra->cpu_rdev) {
277 tegra->cpu_rdev = rdev;
278 return 0;
281 return -EINVAL;
284 static int tegra30_regulator_detach(struct regulator_coupler *coupler,
285 struct regulator_dev *rdev)
287 struct tegra_regulator_coupler *tegra = to_tegra_coupler(coupler);
289 if (tegra->core_rdev == rdev) {
290 tegra->core_rdev = NULL;
291 return 0;
294 if (tegra->cpu_rdev == rdev) {
295 tegra->cpu_rdev = NULL;
296 return 0;
299 return -EINVAL;
302 static struct tegra_regulator_coupler tegra30_coupler = {
303 .coupler = {
304 .attach_regulator = tegra30_regulator_attach,
305 .detach_regulator = tegra30_regulator_detach,
306 .balance_voltage = tegra30_regulator_balance_voltage,
310 static int __init tegra_regulator_coupler_init(void)
312 if (!of_machine_is_compatible("nvidia,tegra30"))
313 return 0;
315 return regulator_coupler_register(&tegra30_coupler.coupler);
317 arch_initcall(tegra_regulator_coupler_init);