1 // SPDX-License-Identifier: GPL-2.0+
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
10 #define pr_fmt(fmt) "tegra voltage-coupler: " fmt
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 #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;
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)
42 if (tegra->core_min_uV > 0)
43 return tegra->core_min_uV;
45 core_cur_uV = regulator_get_voltage_rdev(core_rdev);
49 core_max_uV = max(core_cur_uV, 1200000);
51 err = regulator_check_voltage(core_rdev, &core_min_uV, &core_max_uV);
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)
81 if (cpu_uV < 1250000) {
82 switch (tegra_sku_info.cpu_speedo_id) {
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;
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;
123 pr_err_once("cpu-core max-spread is undefined in device-tree\n");
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);
146 err = regulator_check_consumers(core_rdev, &core_min_uV, &core_max_uV,
151 core_uV = regulator_get_voltage_rdev(core_rdev);
155 cpu_min_uV = core_min_uV - max_spread;
157 err = regulator_check_consumers(cpu_rdev, &cpu_min_uV, &cpu_max_uV,
162 err = regulator_check_consumers(cpu_rdev, &cpu_min_uV_consumers,
163 &cpu_max_uV, PM_SUSPEND_ON);
167 err = regulator_check_voltage(cpu_rdev, &cpu_min_uV, &cpu_max_uV);
171 cpu_uV = regulator_get_voltage_rdev(cpu_rdev);
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 = max(cpu_uV, cpu_min_uV);
184 * Bootloader shall set up voltages correctly, but if it
185 * happens that there is a violation, then try to fix it
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);
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);
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);
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)
215 err = regulator_set_voltage_rdev(cpu_rdev,
222 cpu_uV = cpu_target_uV;
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);
234 core_target_uV = max(core_target_uV, core_uV - core_max_step);
237 if (core_uV == core_target_uV)
240 err = regulator_set_voltage_rdev(core_rdev,
247 core_uV = core_target_uV;
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");
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") &&
277 tegra->core_rdev = rdev;
281 if (of_property_read_bool(np, "nvidia,tegra-cpu-regulator") &&
283 tegra->cpu_rdev = rdev;
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;
300 if (tegra->cpu_rdev == rdev) {
301 tegra->cpu_rdev = NULL;
308 static struct tegra_regulator_coupler tegra30_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"))
321 return regulator_coupler_register(&tegra30_coupler.coupler);
323 arch_initcall(tegra_regulator_coupler_init);