regulators-tegra30.c 13 KB

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