cpu-tegra.c 5.7 KB

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