regulators-tegra20.c 14 KB

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