tegra124-cpufreq.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Tegra 124 cpufreq driver
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/clk.h>
  7. #include <linux/cpufreq.h>
  8. #include <linux/err.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_opp.h>
  15. #include <linux/types.h>
  16. #include "cpufreq-dt.h"
  17. static struct platform_device *tegra124_cpufreq_pdev;
  18. struct tegra124_cpufreq_priv {
  19. struct clk *cpu_clk;
  20. struct clk *pllp_clk;
  21. struct clk *pllx_clk;
  22. struct clk *dfll_clk;
  23. struct platform_device *cpufreq_dt_pdev;
  24. };
  25. static int tegra124_cpu_switch_to_dfll(struct tegra124_cpufreq_priv *priv)
  26. {
  27. struct clk *orig_parent;
  28. int ret;
  29. ret = clk_set_rate(priv->dfll_clk, clk_get_rate(priv->cpu_clk));
  30. if (ret)
  31. return ret;
  32. orig_parent = clk_get_parent(priv->cpu_clk);
  33. clk_set_parent(priv->cpu_clk, priv->pllp_clk);
  34. ret = clk_prepare_enable(priv->dfll_clk);
  35. if (ret)
  36. goto out;
  37. clk_set_parent(priv->cpu_clk, priv->dfll_clk);
  38. return 0;
  39. out:
  40. clk_set_parent(priv->cpu_clk, orig_parent);
  41. return ret;
  42. }
  43. static int tegra124_cpufreq_probe(struct platform_device *pdev)
  44. {
  45. struct device_node *np __free(device_node) = of_cpu_device_node_get(0);
  46. struct tegra124_cpufreq_priv *priv;
  47. struct device *cpu_dev;
  48. int ret;
  49. if (!np)
  50. return -ENODEV;
  51. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  52. if (!priv)
  53. return -ENOMEM;
  54. cpu_dev = get_cpu_device(0);
  55. if (!cpu_dev)
  56. return -ENODEV;
  57. priv->cpu_clk = of_clk_get_by_name(np, "cpu_g");
  58. if (IS_ERR(priv->cpu_clk))
  59. return PTR_ERR(priv->cpu_clk);
  60. priv->dfll_clk = of_clk_get_by_name(np, "dfll");
  61. if (IS_ERR(priv->dfll_clk)) {
  62. ret = PTR_ERR(priv->dfll_clk);
  63. goto out_put_cpu_clk;
  64. }
  65. priv->pllx_clk = of_clk_get_by_name(np, "pll_x");
  66. if (IS_ERR(priv->pllx_clk)) {
  67. ret = PTR_ERR(priv->pllx_clk);
  68. goto out_put_dfll_clk;
  69. }
  70. priv->pllp_clk = of_clk_get_by_name(np, "pll_p");
  71. if (IS_ERR(priv->pllp_clk)) {
  72. ret = PTR_ERR(priv->pllp_clk);
  73. goto out_put_pllx_clk;
  74. }
  75. ret = tegra124_cpu_switch_to_dfll(priv);
  76. if (ret)
  77. goto out_put_pllp_clk;
  78. priv->cpufreq_dt_pdev = cpufreq_dt_pdev_register(&pdev->dev);
  79. if (IS_ERR(priv->cpufreq_dt_pdev)) {
  80. ret = PTR_ERR(priv->cpufreq_dt_pdev);
  81. goto out_put_pllp_clk;
  82. }
  83. platform_set_drvdata(pdev, priv);
  84. return 0;
  85. out_put_pllp_clk:
  86. clk_put(priv->pllp_clk);
  87. out_put_pllx_clk:
  88. clk_put(priv->pllx_clk);
  89. out_put_dfll_clk:
  90. clk_put(priv->dfll_clk);
  91. out_put_cpu_clk:
  92. clk_put(priv->cpu_clk);
  93. return ret;
  94. }
  95. static int __maybe_unused tegra124_cpufreq_suspend(struct device *dev)
  96. {
  97. struct tegra124_cpufreq_priv *priv = dev_get_drvdata(dev);
  98. int err;
  99. /*
  100. * PLLP rate 408Mhz is below the CPU Fmax at Vmin and is safe to
  101. * use during suspend and resume. So, switch the CPU clock source
  102. * to PLLP and disable DFLL.
  103. */
  104. err = clk_set_parent(priv->cpu_clk, priv->pllp_clk);
  105. if (err < 0) {
  106. dev_err(dev, "failed to reparent to PLLP: %d\n", err);
  107. return err;
  108. }
  109. clk_disable_unprepare(priv->dfll_clk);
  110. return 0;
  111. }
  112. static int __maybe_unused tegra124_cpufreq_resume(struct device *dev)
  113. {
  114. struct tegra124_cpufreq_priv *priv = dev_get_drvdata(dev);
  115. int err;
  116. /*
  117. * Warmboot code powers up the CPU with PLLP clock source.
  118. * Enable DFLL clock and switch CPU clock source back to DFLL.
  119. */
  120. err = clk_prepare_enable(priv->dfll_clk);
  121. if (err < 0) {
  122. dev_err(dev, "failed to enable DFLL clock for CPU: %d\n", err);
  123. goto disable_cpufreq;
  124. }
  125. err = clk_set_parent(priv->cpu_clk, priv->dfll_clk);
  126. if (err < 0) {
  127. dev_err(dev, "failed to reparent to DFLL clock: %d\n", err);
  128. goto disable_dfll;
  129. }
  130. return 0;
  131. disable_dfll:
  132. clk_disable_unprepare(priv->dfll_clk);
  133. disable_cpufreq:
  134. disable_cpufreq();
  135. return err;
  136. }
  137. static void tegra124_cpufreq_remove(struct platform_device *pdev)
  138. {
  139. struct tegra124_cpufreq_priv *priv = dev_get_drvdata(&pdev->dev);
  140. if (!IS_ERR(priv->cpufreq_dt_pdev)) {
  141. platform_device_unregister(priv->cpufreq_dt_pdev);
  142. priv->cpufreq_dt_pdev = ERR_PTR(-ENODEV);
  143. }
  144. clk_put(priv->pllp_clk);
  145. clk_put(priv->pllx_clk);
  146. clk_put(priv->dfll_clk);
  147. clk_put(priv->cpu_clk);
  148. }
  149. static const struct dev_pm_ops tegra124_cpufreq_pm_ops = {
  150. SET_SYSTEM_SLEEP_PM_OPS(tegra124_cpufreq_suspend,
  151. tegra124_cpufreq_resume)
  152. };
  153. static struct platform_driver tegra124_cpufreq_platdrv = {
  154. .driver.name = "cpufreq-tegra124",
  155. .driver.pm = &tegra124_cpufreq_pm_ops,
  156. .probe = tegra124_cpufreq_probe,
  157. .remove = tegra124_cpufreq_remove,
  158. };
  159. static int __init tegra_cpufreq_init(void)
  160. {
  161. int ret;
  162. if (!(of_machine_is_compatible("nvidia,tegra114") ||
  163. of_machine_is_compatible("nvidia,tegra124") ||
  164. of_machine_is_compatible("nvidia,tegra210")))
  165. return -ENODEV;
  166. /*
  167. * Platform driver+device required for handling EPROBE_DEFER with
  168. * the regulator and the DFLL clock
  169. */
  170. ret = platform_driver_register(&tegra124_cpufreq_platdrv);
  171. if (ret)
  172. return ret;
  173. tegra124_cpufreq_pdev = platform_device_register_simple("cpufreq-tegra124", -1, NULL, 0);
  174. if (IS_ERR(tegra124_cpufreq_pdev)) {
  175. platform_driver_unregister(&tegra124_cpufreq_platdrv);
  176. return PTR_ERR(tegra124_cpufreq_pdev);
  177. }
  178. return 0;
  179. }
  180. module_init(tegra_cpufreq_init);
  181. static void __exit tegra_cpufreq_module_exit(void)
  182. {
  183. if (!IS_ERR_OR_NULL(tegra124_cpufreq_pdev))
  184. platform_device_unregister(tegra124_cpufreq_pdev);
  185. platform_driver_unregister(&tegra124_cpufreq_platdrv);
  186. }
  187. module_exit(tegra_cpufreq_module_exit);
  188. MODULE_AUTHOR("Tuomas Tynkkynen <ttynkkynen@nvidia.com>");
  189. MODULE_DESCRIPTION("cpufreq driver for NVIDIA Tegra124");
  190. MODULE_LICENSE("GPL");