tegra-cpufreq.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2010 Google, Inc.
  3. *
  4. * Author:
  5. * Colin Cross <ccross@google.com>
  6. * Based on arch/arm/plat-omap/cpu-omap.c, (C) 2005 Nokia Corporation
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/sched.h>
  22. #include <linux/cpufreq.h>
  23. #include <linux/delay.h>
  24. #include <linux/init.h>
  25. #include <linux/err.h>
  26. #include <linux/clk.h>
  27. #include <linux/io.h>
  28. #include <linux/suspend.h>
  29. static struct cpufreq_frequency_table freq_table[] = {
  30. { .frequency = 216000 },
  31. { .frequency = 312000 },
  32. { .frequency = 456000 },
  33. { .frequency = 608000 },
  34. { .frequency = 760000 },
  35. { .frequency = 816000 },
  36. { .frequency = 912000 },
  37. { .frequency = 1000000 },
  38. { .frequency = CPUFREQ_TABLE_END },
  39. };
  40. #define NUM_CPUS 2
  41. static struct clk *cpu_clk;
  42. static struct clk *pll_x_clk;
  43. static struct clk *pll_p_clk;
  44. static struct clk *emc_clk;
  45. static DEFINE_MUTEX(tegra_cpu_lock);
  46. static bool is_suspended;
  47. static int tegra_cpu_clk_set_rate(unsigned long rate)
  48. {
  49. int ret;
  50. /*
  51. * Take an extra reference to the main pll so it doesn't turn
  52. * off when we move the cpu off of it
  53. */
  54. clk_prepare_enable(pll_x_clk);
  55. ret = clk_set_parent(cpu_clk, pll_p_clk);
  56. if (ret) {
  57. pr_err("Failed to switch cpu to clock pll_p\n");
  58. goto out;
  59. }
  60. if (rate == clk_get_rate(pll_p_clk))
  61. goto out;
  62. ret = clk_set_rate(pll_x_clk, rate);
  63. if (ret) {
  64. pr_err("Failed to change pll_x to %lu\n", rate);
  65. goto out;
  66. }
  67. ret = clk_set_parent(cpu_clk, pll_x_clk);
  68. if (ret) {
  69. pr_err("Failed to switch cpu to clock pll_x\n");
  70. goto out;
  71. }
  72. out:
  73. clk_disable_unprepare(pll_x_clk);
  74. return ret;
  75. }
  76. static int tegra_update_cpu_speed(struct cpufreq_policy *policy,
  77. unsigned long rate)
  78. {
  79. int ret = 0;
  80. /*
  81. * Vote on memory bus frequency based on cpu frequency
  82. * This sets the minimum frequency, display or avp may request higher
  83. */
  84. if (rate >= 816000)
  85. clk_set_rate(emc_clk, 600000000); /* cpu 816 MHz, emc max */
  86. else if (rate >= 456000)
  87. clk_set_rate(emc_clk, 300000000); /* cpu 456 MHz, emc 150Mhz */
  88. else
  89. clk_set_rate(emc_clk, 100000000); /* emc 50Mhz */
  90. ret = tegra_cpu_clk_set_rate(rate * 1000);
  91. if (ret)
  92. pr_err("cpu-tegra: Failed to set cpu frequency to %lu kHz\n",
  93. rate);
  94. return ret;
  95. }
  96. static int tegra_target(struct cpufreq_policy *policy, unsigned int index)
  97. {
  98. int ret = -EBUSY;
  99. mutex_lock(&tegra_cpu_lock);
  100. if (!is_suspended)
  101. ret = tegra_update_cpu_speed(policy,
  102. freq_table[index].frequency);
  103. mutex_unlock(&tegra_cpu_lock);
  104. return ret;
  105. }
  106. static int tegra_pm_notify(struct notifier_block *nb, unsigned long event,
  107. void *dummy)
  108. {
  109. mutex_lock(&tegra_cpu_lock);
  110. if (event == PM_SUSPEND_PREPARE) {
  111. struct cpufreq_policy *policy = cpufreq_cpu_get(0);
  112. is_suspended = true;
  113. pr_info("Tegra cpufreq suspend: setting frequency to %d kHz\n",
  114. freq_table[0].frequency);
  115. if (clk_get_rate(cpu_clk) / 1000 != freq_table[0].frequency)
  116. tegra_update_cpu_speed(policy, freq_table[0].frequency);
  117. cpufreq_cpu_put(policy);
  118. } else if (event == PM_POST_SUSPEND) {
  119. is_suspended = false;
  120. }
  121. mutex_unlock(&tegra_cpu_lock);
  122. return NOTIFY_OK;
  123. }
  124. static struct notifier_block tegra_cpu_pm_notifier = {
  125. .notifier_call = tegra_pm_notify,
  126. };
  127. static int tegra_cpu_init(struct cpufreq_policy *policy)
  128. {
  129. int ret;
  130. if (policy->cpu >= NUM_CPUS)
  131. return -EINVAL;
  132. clk_prepare_enable(emc_clk);
  133. clk_prepare_enable(cpu_clk);
  134. /* FIXME: what's the actual transition time? */
  135. ret = cpufreq_generic_init(policy, freq_table, 300 * 1000);
  136. if (ret) {
  137. clk_disable_unprepare(cpu_clk);
  138. clk_disable_unprepare(emc_clk);
  139. return ret;
  140. }
  141. if (policy->cpu == 0)
  142. register_pm_notifier(&tegra_cpu_pm_notifier);
  143. policy->clk = cpu_clk;
  144. return 0;
  145. }
  146. static int tegra_cpu_exit(struct cpufreq_policy *policy)
  147. {
  148. cpufreq_frequency_table_put_attr(policy->cpu);
  149. clk_disable_unprepare(cpu_clk);
  150. clk_disable_unprepare(emc_clk);
  151. return 0;
  152. }
  153. static struct cpufreq_driver tegra_cpufreq_driver = {
  154. .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  155. .verify = cpufreq_generic_frequency_table_verify,
  156. .target_index = tegra_target,
  157. .get = cpufreq_generic_get,
  158. .init = tegra_cpu_init,
  159. .exit = tegra_cpu_exit,
  160. .name = "tegra",
  161. .attr = cpufreq_generic_attr,
  162. };
  163. static int __init tegra_cpufreq_init(void)
  164. {
  165. cpu_clk = clk_get_sys(NULL, "cclk");
  166. if (IS_ERR(cpu_clk))
  167. return PTR_ERR(cpu_clk);
  168. pll_x_clk = clk_get_sys(NULL, "pll_x");
  169. if (IS_ERR(pll_x_clk))
  170. return PTR_ERR(pll_x_clk);
  171. pll_p_clk = clk_get_sys(NULL, "pll_p");
  172. if (IS_ERR(pll_p_clk))
  173. return PTR_ERR(pll_p_clk);
  174. emc_clk = clk_get_sys("cpu", "emc");
  175. if (IS_ERR(emc_clk)) {
  176. clk_put(cpu_clk);
  177. return PTR_ERR(emc_clk);
  178. }
  179. return cpufreq_register_driver(&tegra_cpufreq_driver);
  180. }
  181. static void __exit tegra_cpufreq_exit(void)
  182. {
  183. cpufreq_unregister_driver(&tegra_cpufreq_driver);
  184. clk_put(emc_clk);
  185. clk_put(cpu_clk);
  186. }
  187. MODULE_AUTHOR("Colin Cross <ccross@android.com>");
  188. MODULE_DESCRIPTION("cpufreq driver for Nvidia Tegra2");
  189. MODULE_LICENSE("GPL");
  190. module_init(tegra_cpufreq_init);
  191. module_exit(tegra_cpufreq_exit);