1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Voltage regulators coupler for NVIDIA Tegra20 4 * Copyright (C) 2019 GRATE-DRIVER project 5 * 6 * Voltage constraints borrowed from downstream kernel sources 7 * Copyright (C) 2010-2011 NVIDIA Corporation 8 */ 9 10 #define pr_fmt(fmt) "tegra voltage-coupler: " fmt 11 12 #include <linux/init.h> 13 #include <linux/kernel.h> 14 #include <linux/of.h> 15 #include <linux/reboot.h> 16 #include <linux/regulator/coupler.h> 17 #include <linux/regulator/driver.h> 18 #include <linux/regulator/machine.h> 19 20 struct tegra_regulator_coupler { 21 struct regulator_coupler coupler; 22 struct regulator_dev *core_rdev; 23 struct regulator_dev *cpu_rdev; 24 struct regulator_dev *rtc_rdev; 25 struct notifier_block reboot_notifier; 26 int core_min_uV, cpu_min_uV; 27 bool sys_reboot_mode_req; 28 bool sys_reboot_mode; 29 }; 30 31 static inline struct tegra_regulator_coupler * 32 to_tegra_coupler(struct regulator_coupler *coupler) 33 { 34 return container_of(coupler, struct tegra_regulator_coupler, coupler); 35 } 36 37 static int tegra20_core_limit(struct tegra_regulator_coupler *tegra, 38 struct regulator_dev *core_rdev) 39 { 40 int core_min_uV = 0; 41 int core_max_uV; 42 int core_cur_uV; 43 int err; 44 45 if (tegra->core_min_uV > 0) 46 return tegra->core_min_uV; 47 48 core_cur_uV = regulator_get_voltage_rdev(core_rdev); 49 if (core_cur_uV < 0) 50 return core_cur_uV; 51 52 core_max_uV = max(core_cur_uV, 1200000); 53 54 err = regulator_check_voltage(core_rdev, &core_min_uV, &core_max_uV); 55 if (err) 56 return err; 57 58 /* 59 * Limit minimum CORE voltage to a value left from bootloader or, 60 * if it's unreasonably low value, to the most common 1.2v or to 61 * whatever maximum value defined via board's device-tree. 62 */ 63 tegra->core_min_uV = core_max_uV; 64 65 pr_info("core minimum voltage limited to %duV\n", tegra->core_min_uV); 66 67 return tegra->core_min_uV; 68 } 69 70 static int tegra20_core_rtc_max_spread(struct regulator_dev *core_rdev, 71 struct regulator_dev *rtc_rdev) 72 { 73 struct coupling_desc *c_desc = &core_rdev->coupling_desc; 74 struct regulator_dev *rdev; 75 int max_spread; 76 unsigned int i; 77 78 for (i = 1; i < c_desc->n_coupled; i++) { 79 max_spread = core_rdev->constraints->max_spread[i - 1]; 80 rdev = c_desc->coupled_rdevs[i]; 81 82 if (rdev == rtc_rdev && max_spread) 83 return max_spread; 84 } 85 86 pr_err_once("rtc-core max-spread is undefined in device-tree\n"); 87 88 return 150000; 89 } 90 91 static int tegra20_core_rtc_update(struct tegra_regulator_coupler *tegra, 92 struct regulator_dev *core_rdev, 93 struct regulator_dev *rtc_rdev, 94 int cpu_uV, int cpu_min_uV) 95 { 96 int core_min_uV, core_max_uV = INT_MAX; 97 int rtc_min_uV, rtc_max_uV = INT_MAX; 98 int core_target_uV; 99 int rtc_target_uV; 100 int max_spread; 101 int core_uV; 102 int rtc_uV; 103 int err; 104 105 /* 106 * RTC and CORE voltages should be no more than 170mV from each other, 107 * CPU should be below RTC and CORE by at least 120mV. This applies 108 * to all Tegra20 SoC's. 109 */ 110 max_spread = tegra20_core_rtc_max_spread(core_rdev, rtc_rdev); 111 112 /* 113 * The core voltage scaling is currently not hooked up in drivers, 114 * hence we will limit the minimum core voltage to a reasonable value. 115 * This should be good enough for the time being. 116 */ 117 core_min_uV = tegra20_core_limit(tegra, core_rdev); 118 if (core_min_uV < 0) 119 return core_min_uV; 120 121 err = regulator_check_voltage(core_rdev, &core_min_uV, &core_max_uV); 122 if (err) 123 return err; 124 125 err = regulator_check_consumers(core_rdev, &core_min_uV, &core_max_uV, 126 PM_SUSPEND_ON); 127 if (err) 128 return err; 129 130 core_uV = regulator_get_voltage_rdev(core_rdev); 131 if (core_uV < 0) 132 return core_uV; 133 134 core_min_uV = max(cpu_min_uV + 125000, core_min_uV); 135 if (core_min_uV > core_max_uV) 136 return -EINVAL; 137 138 if (cpu_uV + 120000 > core_uV) 139 pr_err("core-cpu voltage constraint violated: %d %d\n", 140 core_uV, cpu_uV + 120000); 141 142 rtc_uV = regulator_get_voltage_rdev(rtc_rdev); 143 if (rtc_uV < 0) 144 return rtc_uV; 145 146 if (cpu_uV + 120000 > rtc_uV) 147 pr_err("rtc-cpu voltage constraint violated: %d %d\n", 148 rtc_uV, cpu_uV + 120000); 149 150 if (abs(core_uV - rtc_uV) > 170000) 151 pr_err("core-rtc voltage constraint violated: %d %d\n", 152 core_uV, rtc_uV); 153 154 rtc_min_uV = max(cpu_min_uV + 125000, core_min_uV - max_spread); 155 156 err = regulator_check_voltage(rtc_rdev, &rtc_min_uV, &rtc_max_uV); 157 if (err) 158 return err; 159 160 while (core_uV != core_min_uV || rtc_uV != rtc_min_uV) { 161 if (core_uV < core_min_uV) { 162 core_target_uV = min(core_uV + max_spread, core_min_uV); 163 core_target_uV = min(rtc_uV + max_spread, core_target_uV); 164 } else { 165 core_target_uV = max(core_uV - max_spread, core_min_uV); 166 core_target_uV = max(rtc_uV - max_spread, core_target_uV); 167 } 168 169 if (core_uV == core_target_uV) 170 goto update_rtc; 171 172 err = regulator_set_voltage_rdev(core_rdev, 173 core_target_uV, 174 core_max_uV, 175 PM_SUSPEND_ON); 176 if (err) 177 return err; 178 179 core_uV = core_target_uV; 180 update_rtc: 181 if (rtc_uV < rtc_min_uV) { 182 rtc_target_uV = min(rtc_uV + max_spread, rtc_min_uV); 183 rtc_target_uV = min(core_uV + max_spread, rtc_target_uV); 184 } else { 185 rtc_target_uV = max(rtc_uV - max_spread, rtc_min_uV); 186 rtc_target_uV = max(core_uV - max_spread, rtc_target_uV); 187 } 188 189 if (rtc_uV == rtc_target_uV) 190 continue; 191 192 err = regulator_set_voltage_rdev(rtc_rdev, 193 rtc_target_uV, 194 rtc_max_uV, 195 PM_SUSPEND_ON); 196 if (err) 197 return err; 198 199 rtc_uV = rtc_target_uV; 200 } 201 202 return 0; 203 } 204 205 static int tegra20_core_voltage_update(struct tegra_regulator_coupler *tegra, 206 struct regulator_dev *cpu_rdev, 207 struct regulator_dev *core_rdev, 208 struct regulator_dev *rtc_rdev) 209 { 210 int cpu_uV; 211 212 cpu_uV = regulator_get_voltage_rdev(cpu_rdev); 213 if (cpu_uV < 0) 214 return cpu_uV; 215 216 return tegra20_core_rtc_update(tegra, core_rdev, rtc_rdev, 217 cpu_uV, cpu_uV); 218 } 219 220 static int tegra20_cpu_voltage_update(struct tegra_regulator_coupler *tegra, 221 struct regulator_dev *cpu_rdev, 222 struct regulator_dev *core_rdev, 223 struct regulator_dev *rtc_rdev) 224 { 225 int cpu_min_uV_consumers = 0; 226 int cpu_max_uV = INT_MAX; 227 int cpu_min_uV = 0; 228 int cpu_uV; 229 int err; 230 231 err = regulator_check_voltage(cpu_rdev, &cpu_min_uV, &cpu_max_uV); 232 if (err) 233 return err; 234 235 err = regulator_check_consumers(cpu_rdev, &cpu_min_uV, &cpu_max_uV, 236 PM_SUSPEND_ON); 237 if (err) 238 return err; 239 240 err = regulator_check_consumers(cpu_rdev, &cpu_min_uV_consumers, 241 &cpu_max_uV, PM_SUSPEND_ON); 242 if (err) 243 return err; 244 245 cpu_uV = regulator_get_voltage_rdev(cpu_rdev); 246 if (cpu_uV < 0) 247 return cpu_uV; 248 249 /* store boot voltage level */ 250 if (!tegra->cpu_min_uV) 251 tegra->cpu_min_uV = cpu_uV; 252 253 /* 254 * CPU's regulator may not have any consumers, hence the voltage 255 * must not be changed in that case because CPU simply won't 256 * survive the voltage drop if it's running on a higher frequency. 257 */ 258 if (!cpu_min_uV_consumers) 259 cpu_min_uV = cpu_uV; 260 261 /* restore boot voltage level */ 262 if (tegra->sys_reboot_mode) 263 cpu_min_uV = max(cpu_min_uV, tegra->cpu_min_uV); 264 265 if (cpu_min_uV > cpu_uV) { 266 err = tegra20_core_rtc_update(tegra, core_rdev, rtc_rdev, 267 cpu_uV, cpu_min_uV); 268 if (err) 269 return err; 270 271 err = regulator_set_voltage_rdev(cpu_rdev, cpu_min_uV, 272 cpu_max_uV, PM_SUSPEND_ON); 273 if (err) 274 return err; 275 } else if (cpu_min_uV < cpu_uV) { 276 err = regulator_set_voltage_rdev(cpu_rdev, cpu_min_uV, 277 cpu_max_uV, PM_SUSPEND_ON); 278 if (err) 279 return err; 280 281 err = tegra20_core_rtc_update(tegra, core_rdev, rtc_rdev, 282 cpu_uV, cpu_min_uV); 283 if (err) 284 return err; 285 } 286 287 return 0; 288 } 289 290 static int tegra20_regulator_balance_voltage(struct regulator_coupler *coupler, 291 struct regulator_dev *rdev, 292 suspend_state_t state) 293 { 294 struct tegra_regulator_coupler *tegra = to_tegra_coupler(coupler); 295 struct regulator_dev *core_rdev = tegra->core_rdev; 296 struct regulator_dev *cpu_rdev = tegra->cpu_rdev; 297 struct regulator_dev *rtc_rdev = tegra->rtc_rdev; 298 299 if ((core_rdev != rdev && cpu_rdev != rdev && rtc_rdev != rdev) || 300 state != PM_SUSPEND_ON) { 301 pr_err("regulators are not coupled properly\n"); 302 return -EINVAL; 303 } 304 305 tegra->sys_reboot_mode = READ_ONCE(tegra->sys_reboot_mode_req); 306 307 if (rdev == cpu_rdev) 308 return tegra20_cpu_voltage_update(tegra, cpu_rdev, 309 core_rdev, rtc_rdev); 310 311 if (rdev == core_rdev) 312 return tegra20_core_voltage_update(tegra, cpu_rdev, 313 core_rdev, rtc_rdev); 314 315 pr_err("changing %s voltage not permitted\n", rdev_get_name(rtc_rdev)); 316 317 return -EPERM; 318 } 319 320 static int tegra20_regulator_prepare_reboot(struct tegra_regulator_coupler *tegra, 321 bool sys_reboot_mode) 322 { 323 int err; 324 325 if (!tegra->core_rdev || !tegra->rtc_rdev || !tegra->cpu_rdev) 326 return 0; 327 328 WRITE_ONCE(tegra->sys_reboot_mode_req, true); 329 330 /* 331 * Some devices use CPU soft-reboot method and in this case we 332 * should ensure that voltages are sane for the reboot by restoring 333 * the minimum boot levels. 334 */ 335 err = regulator_sync_voltage_rdev(tegra->cpu_rdev); 336 if (err) 337 return err; 338 339 err = regulator_sync_voltage_rdev(tegra->core_rdev); 340 if (err) 341 return err; 342 343 WRITE_ONCE(tegra->sys_reboot_mode_req, sys_reboot_mode); 344 345 return 0; 346 } 347 348 static int tegra20_regulator_reboot(struct notifier_block *notifier, 349 unsigned long event, void *cmd) 350 { 351 struct tegra_regulator_coupler *tegra; 352 int ret; 353 354 if (event != SYS_RESTART) 355 return NOTIFY_DONE; 356 357 tegra = container_of(notifier, struct tegra_regulator_coupler, 358 reboot_notifier); 359 360 ret = tegra20_regulator_prepare_reboot(tegra, true); 361 362 return notifier_from_errno(ret); 363 } 364 365 static int tegra20_regulator_attach(struct regulator_coupler *coupler, 366 struct regulator_dev *rdev) 367 { 368 struct tegra_regulator_coupler *tegra = to_tegra_coupler(coupler); 369 struct device_node *np = rdev->dev.of_node; 370 371 if (of_property_read_bool(np, "nvidia,tegra-core-regulator") && 372 !tegra->core_rdev) { 373 tegra->core_rdev = rdev; 374 return 0; 375 } 376 377 if (of_property_read_bool(np, "nvidia,tegra-rtc-regulator") && 378 !tegra->rtc_rdev) { 379 tegra->rtc_rdev = rdev; 380 return 0; 381 } 382 383 if (of_property_read_bool(np, "nvidia,tegra-cpu-regulator") && 384 !tegra->cpu_rdev) { 385 tegra->cpu_rdev = rdev; 386 return 0; 387 } 388 389 return -EINVAL; 390 } 391 392 static int tegra20_regulator_detach(struct regulator_coupler *coupler, 393 struct regulator_dev *rdev) 394 { 395 struct tegra_regulator_coupler *tegra = to_tegra_coupler(coupler); 396 397 /* 398 * We don't expect regulators to be decoupled during reboot, 399 * this may race with the reboot handler and shouldn't ever 400 * happen in practice. 401 */ 402 if (WARN_ON_ONCE(system_state > SYSTEM_RUNNING)) 403 return -EPERM; 404 405 if (tegra->core_rdev == rdev) { 406 tegra->core_rdev = NULL; 407 return 0; 408 } 409 410 if (tegra->rtc_rdev == rdev) { 411 tegra->rtc_rdev = NULL; 412 return 0; 413 } 414 415 if (tegra->cpu_rdev == rdev) { 416 tegra->cpu_rdev = NULL; 417 return 0; 418 } 419 420 return -EINVAL; 421 } 422 423 static struct tegra_regulator_coupler tegra20_coupler = { 424 .coupler = { 425 .attach_regulator = tegra20_regulator_attach, 426 .detach_regulator = tegra20_regulator_detach, 427 .balance_voltage = tegra20_regulator_balance_voltage, 428 }, 429 .reboot_notifier.notifier_call = tegra20_regulator_reboot, 430 }; 431 432 static int __init tegra_regulator_coupler_init(void) 433 { 434 int err; 435 436 if (!of_machine_is_compatible("nvidia,tegra20")) 437 return 0; 438 439 err = register_reboot_notifier(&tegra20_coupler.reboot_notifier); 440 WARN_ON(err); 441 442 return regulator_coupler_register(&tegra20_coupler.coupler); 443 } 444 arch_initcall(tegra_regulator_coupler_init); 445