tegra124-cpufreq.c 4.8 KB

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