tegra124-cpufreq.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Tegra 124 cpufreq driver
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/clk.h>
  15. #include <linux/cpufreq-dt.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_opp.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/types.h>
  26. struct tegra124_cpufreq_priv {
  27. struct regulator *vdd_cpu_reg;
  28. struct clk *cpu_clk;
  29. struct clk *pllp_clk;
  30. struct clk *pllx_clk;
  31. struct clk *dfll_clk;
  32. struct platform_device *cpufreq_dt_pdev;
  33. };
  34. static int tegra124_cpu_switch_to_dfll(struct tegra124_cpufreq_priv *priv)
  35. {
  36. struct clk *orig_parent;
  37. int ret;
  38. ret = clk_set_rate(priv->dfll_clk, clk_get_rate(priv->cpu_clk));
  39. if (ret)
  40. return ret;
  41. orig_parent = clk_get_parent(priv->cpu_clk);
  42. clk_set_parent(priv->cpu_clk, priv->pllp_clk);
  43. ret = clk_prepare_enable(priv->dfll_clk);
  44. if (ret)
  45. goto out;
  46. clk_set_parent(priv->cpu_clk, priv->dfll_clk);
  47. return 0;
  48. out:
  49. clk_set_parent(priv->cpu_clk, orig_parent);
  50. return ret;
  51. }
  52. static void tegra124_cpu_switch_to_pllx(struct tegra124_cpufreq_priv *priv)
  53. {
  54. clk_set_parent(priv->cpu_clk, priv->pllp_clk);
  55. clk_disable_unprepare(priv->dfll_clk);
  56. regulator_sync_voltage(priv->vdd_cpu_reg);
  57. clk_set_parent(priv->cpu_clk, priv->pllx_clk);
  58. }
  59. static struct cpufreq_dt_platform_data cpufreq_dt_pd = {
  60. .independent_clocks = false,
  61. };
  62. static int tegra124_cpufreq_probe(struct platform_device *pdev)
  63. {
  64. struct tegra124_cpufreq_priv *priv;
  65. struct device_node *np;
  66. struct device *cpu_dev;
  67. struct platform_device_info cpufreq_dt_devinfo = {};
  68. int ret;
  69. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  70. if (!priv)
  71. return -ENOMEM;
  72. cpu_dev = get_cpu_device(0);
  73. if (!cpu_dev)
  74. return -ENODEV;
  75. np = of_cpu_device_node_get(0);
  76. if (!np)
  77. return -ENODEV;
  78. priv->vdd_cpu_reg = regulator_get(cpu_dev, "vdd-cpu");
  79. if (IS_ERR(priv->vdd_cpu_reg)) {
  80. ret = PTR_ERR(priv->vdd_cpu_reg);
  81. goto out_put_np;
  82. }
  83. priv->cpu_clk = of_clk_get_by_name(np, "cpu_g");
  84. if (IS_ERR(priv->cpu_clk)) {
  85. ret = PTR_ERR(priv->cpu_clk);
  86. goto out_put_vdd_cpu_reg;
  87. }
  88. priv->dfll_clk = of_clk_get_by_name(np, "dfll");
  89. if (IS_ERR(priv->dfll_clk)) {
  90. ret = PTR_ERR(priv->dfll_clk);
  91. goto out_put_cpu_clk;
  92. }
  93. priv->pllx_clk = of_clk_get_by_name(np, "pll_x");
  94. if (IS_ERR(priv->pllx_clk)) {
  95. ret = PTR_ERR(priv->pllx_clk);
  96. goto out_put_dfll_clk;
  97. }
  98. priv->pllp_clk = of_clk_get_by_name(np, "pll_p");
  99. if (IS_ERR(priv->pllp_clk)) {
  100. ret = PTR_ERR(priv->pllp_clk);
  101. goto out_put_pllx_clk;
  102. }
  103. ret = tegra124_cpu_switch_to_dfll(priv);
  104. if (ret)
  105. goto out_put_pllp_clk;
  106. cpufreq_dt_devinfo.name = "cpufreq-dt";
  107. cpufreq_dt_devinfo.parent = &pdev->dev;
  108. cpufreq_dt_devinfo.data = &cpufreq_dt_pd;
  109. cpufreq_dt_devinfo.size_data = sizeof(cpufreq_dt_pd);
  110. priv->cpufreq_dt_pdev =
  111. platform_device_register_full(&cpufreq_dt_devinfo);
  112. if (IS_ERR(priv->cpufreq_dt_pdev)) {
  113. ret = PTR_ERR(priv->cpufreq_dt_pdev);
  114. goto out_switch_to_pllx;
  115. }
  116. platform_set_drvdata(pdev, priv);
  117. of_node_put(np);
  118. return 0;
  119. out_switch_to_pllx:
  120. tegra124_cpu_switch_to_pllx(priv);
  121. out_put_pllp_clk:
  122. clk_put(priv->pllp_clk);
  123. out_put_pllx_clk:
  124. clk_put(priv->pllx_clk);
  125. out_put_dfll_clk:
  126. clk_put(priv->dfll_clk);
  127. out_put_cpu_clk:
  128. clk_put(priv->cpu_clk);
  129. out_put_vdd_cpu_reg:
  130. regulator_put(priv->vdd_cpu_reg);
  131. out_put_np:
  132. of_node_put(np);
  133. return ret;
  134. }
  135. static int tegra124_cpufreq_remove(struct platform_device *pdev)
  136. {
  137. struct tegra124_cpufreq_priv *priv = platform_get_drvdata(pdev);
  138. platform_device_unregister(priv->cpufreq_dt_pdev);
  139. tegra124_cpu_switch_to_pllx(priv);
  140. clk_put(priv->pllp_clk);
  141. clk_put(priv->pllx_clk);
  142. clk_put(priv->dfll_clk);
  143. clk_put(priv->cpu_clk);
  144. regulator_put(priv->vdd_cpu_reg);
  145. return 0;
  146. }
  147. static struct platform_driver tegra124_cpufreq_platdrv = {
  148. .driver.name = "cpufreq-tegra124",
  149. .probe = tegra124_cpufreq_probe,
  150. .remove = tegra124_cpufreq_remove,
  151. };
  152. static int __init tegra_cpufreq_init(void)
  153. {
  154. int ret;
  155. struct platform_device *pdev;
  156. if (!of_machine_is_compatible("nvidia,tegra124"))
  157. return -ENODEV;
  158. /*
  159. * Platform driver+device required for handling EPROBE_DEFER with
  160. * the regulator and the DFLL clock
  161. */
  162. ret = platform_driver_register(&tegra124_cpufreq_platdrv);
  163. if (ret)
  164. return ret;
  165. pdev = platform_device_register_simple("cpufreq-tegra124", -1, NULL, 0);
  166. if (IS_ERR(pdev)) {
  167. platform_driver_unregister(&tegra124_cpufreq_platdrv);
  168. return PTR_ERR(pdev);
  169. }
  170. return 0;
  171. }
  172. module_init(tegra_cpufreq_init);
  173. MODULE_AUTHOR("Tuomas Tynkkynen <ttynkkynen@nvidia.com>");
  174. MODULE_DESCRIPTION("cpufreq driver for NVIDIA Tegra124");
  175. MODULE_LICENSE("GPL v2");