cpu-tegra.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 <asm/system.h>
  31. #include <mach/hardware.h>
  32. #include <mach/clk.h>
  33. /* Frequency table index must be sequential starting at 0 */
  34. static struct cpufreq_frequency_table freq_table[] = {
  35. { 0, 312000 },
  36. { 1, 456000 },
  37. { 2, 608000 },
  38. { 3, 760000 },
  39. { 4, 816000 },
  40. { 5, 912000 },
  41. { 6, 1000000 },
  42. { 7, CPUFREQ_TABLE_END },
  43. };
  44. #define NUM_CPUS 2
  45. static struct clk *cpu_clk;
  46. static unsigned long target_cpu_speed[NUM_CPUS];
  47. int tegra_verify_speed(struct cpufreq_policy *policy)
  48. {
  49. return cpufreq_frequency_table_verify(policy, freq_table);
  50. }
  51. unsigned int tegra_getspeed(unsigned int cpu)
  52. {
  53. unsigned long rate;
  54. if (cpu >= NUM_CPUS)
  55. return 0;
  56. rate = clk_get_rate(cpu_clk) / 1000;
  57. return rate;
  58. }
  59. static int tegra_update_cpu_speed(void)
  60. {
  61. int i;
  62. unsigned long rate = 0;
  63. int ret = 0;
  64. struct cpufreq_freqs freqs;
  65. for_each_online_cpu(i)
  66. rate = max(rate, target_cpu_speed[i]);
  67. freqs.old = tegra_getspeed(0);
  68. freqs.new = rate;
  69. if (freqs.old == freqs.new)
  70. return ret;
  71. for_each_online_cpu(freqs.cpu)
  72. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  73. #ifdef CONFIG_CPU_FREQ_DEBUG
  74. printk(KERN_DEBUG "cpufreq-tegra: transition: %u --> %u\n",
  75. freqs.old, freqs.new);
  76. #endif
  77. ret = clk_set_rate_cansleep(cpu_clk, freqs.new * 1000);
  78. if (ret) {
  79. pr_err("cpu-tegra: Failed to set cpu frequency to %d kHz\n",
  80. freqs.new);
  81. return ret;
  82. }
  83. for_each_online_cpu(freqs.cpu)
  84. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  85. return 0;
  86. }
  87. static int tegra_target(struct cpufreq_policy *policy,
  88. unsigned int target_freq,
  89. unsigned int relation)
  90. {
  91. int idx;
  92. unsigned int freq;
  93. cpufreq_frequency_table_target(policy, freq_table, target_freq,
  94. relation, &idx);
  95. freq = freq_table[idx].frequency;
  96. target_cpu_speed[policy->cpu] = freq;
  97. return tegra_update_cpu_speed();
  98. }
  99. static int tegra_cpu_init(struct cpufreq_policy *policy)
  100. {
  101. if (policy->cpu >= NUM_CPUS)
  102. return -EINVAL;
  103. cpu_clk = clk_get_sys(NULL, "cpu");
  104. if (IS_ERR(cpu_clk))
  105. return PTR_ERR(cpu_clk);
  106. cpufreq_frequency_table_cpuinfo(policy, freq_table);
  107. cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
  108. policy->cur = tegra_getspeed(policy->cpu);
  109. target_cpu_speed[policy->cpu] = policy->cur;
  110. /* FIXME: what's the actual transition time? */
  111. policy->cpuinfo.transition_latency = 300 * 1000;
  112. policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  113. cpumask_copy(policy->related_cpus, cpu_possible_mask);
  114. return 0;
  115. }
  116. static int tegra_cpu_exit(struct cpufreq_policy *policy)
  117. {
  118. cpufreq_frequency_table_cpuinfo(policy, freq_table);
  119. clk_put(cpu_clk);
  120. return 0;
  121. }
  122. static struct freq_attr *tegra_cpufreq_attr[] = {
  123. &cpufreq_freq_attr_scaling_available_freqs,
  124. NULL,
  125. };
  126. static struct cpufreq_driver tegra_cpufreq_driver = {
  127. .verify = tegra_verify_speed,
  128. .target = tegra_target,
  129. .get = tegra_getspeed,
  130. .init = tegra_cpu_init,
  131. .exit = tegra_cpu_exit,
  132. .name = "tegra",
  133. .attr = tegra_cpufreq_attr,
  134. };
  135. static int __init tegra_cpufreq_init(void)
  136. {
  137. return cpufreq_register_driver(&tegra_cpufreq_driver);
  138. }
  139. static void __exit tegra_cpufreq_exit(void)
  140. {
  141. cpufreq_unregister_driver(&tegra_cpufreq_driver);
  142. }
  143. MODULE_AUTHOR("Colin Cross <ccross@android.com>");
  144. MODULE_DESCRIPTION("cpufreq driver for Nvidia Tegra2");
  145. MODULE_LICENSE("GPL");
  146. module_init(tegra_cpufreq_init);
  147. module_exit(tegra_cpufreq_exit);