omap-cpufreq.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * CPU frequency scaling for OMAP
  3. *
  4. * Copyright (C) 2005 Nokia Corporation
  5. * Written by Tony Lindgren <tony@atomide.com>
  6. *
  7. * Based on cpu-sa1110.c, Copyright (C) 2001 Russell King
  8. *
  9. * Copyright (C) 2007-2011 Texas Instruments, Inc.
  10. * - OMAP3/4 support by Rajendra Nayak, Santosh Shilimkar
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/cpufreq.h>
  20. #include <linux/delay.h>
  21. #include <linux/init.h>
  22. #include <linux/err.h>
  23. #include <linux/clk.h>
  24. #include <linux/io.h>
  25. #include <linux/opp.h>
  26. #include <linux/cpu.h>
  27. #include <asm/system.h>
  28. #include <asm/smp_plat.h>
  29. #include <asm/cpu.h>
  30. #include <plat/clock.h>
  31. #include <plat/omap-pm.h>
  32. #include <plat/common.h>
  33. #include <mach/hardware.h>
  34. #define VERY_HI_RATE 900000000
  35. #ifdef CONFIG_SMP
  36. struct lpj_info {
  37. unsigned long ref;
  38. unsigned int freq;
  39. };
  40. static DEFINE_PER_CPU(struct lpj_info, lpj_ref);
  41. static struct lpj_info global_lpj_ref;
  42. #endif
  43. static struct cpufreq_frequency_table *freq_table;
  44. static struct clk *mpu_clk;
  45. static int omap_verify_speed(struct cpufreq_policy *policy)
  46. {
  47. if (freq_table)
  48. return cpufreq_frequency_table_verify(policy, freq_table);
  49. if (policy->cpu)
  50. return -EINVAL;
  51. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  52. policy->cpuinfo.max_freq);
  53. policy->min = clk_round_rate(mpu_clk, policy->min * 1000) / 1000;
  54. policy->max = clk_round_rate(mpu_clk, policy->max * 1000) / 1000;
  55. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  56. policy->cpuinfo.max_freq);
  57. return 0;
  58. }
  59. static unsigned int omap_getspeed(unsigned int cpu)
  60. {
  61. unsigned long rate;
  62. if (cpu >= NR_CPUS)
  63. return 0;
  64. rate = clk_get_rate(mpu_clk) / 1000;
  65. return rate;
  66. }
  67. static int omap_target(struct cpufreq_policy *policy,
  68. unsigned int target_freq,
  69. unsigned int relation)
  70. {
  71. int i, ret = 0;
  72. struct cpufreq_freqs freqs;
  73. /* Ensure desired rate is within allowed range. Some govenors
  74. * (ondemand) will just pass target_freq=0 to get the minimum. */
  75. if (target_freq < policy->min)
  76. target_freq = policy->min;
  77. if (target_freq > policy->max)
  78. target_freq = policy->max;
  79. freqs.old = omap_getspeed(policy->cpu);
  80. freqs.new = clk_round_rate(mpu_clk, target_freq * 1000) / 1000;
  81. freqs.cpu = policy->cpu;
  82. if (freqs.old == freqs.new && policy->cur == freqs.new)
  83. return ret;
  84. /* notifiers */
  85. for_each_cpu(i, policy->cpus) {
  86. freqs.cpu = i;
  87. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  88. }
  89. #ifdef CONFIG_CPU_FREQ_DEBUG
  90. pr_info("cpufreq-omap: transition: %u --> %u\n", freqs.old, freqs.new);
  91. #endif
  92. ret = clk_set_rate(mpu_clk, freqs.new * 1000);
  93. freqs.new = omap_getspeed(policy->cpu);
  94. #ifdef CONFIG_SMP
  95. /*
  96. * Note that loops_per_jiffy is not updated on SMP systems in
  97. * cpufreq driver. So, update the per-CPU loops_per_jiffy value
  98. * on frequency transition. We need to update all dependent CPUs.
  99. */
  100. for_each_cpu(i, policy->cpus) {
  101. struct lpj_info *lpj = &per_cpu(lpj_ref, i);
  102. if (!lpj->freq) {
  103. lpj->ref = per_cpu(cpu_data, i).loops_per_jiffy;
  104. lpj->freq = freqs.old;
  105. }
  106. per_cpu(cpu_data, i).loops_per_jiffy =
  107. cpufreq_scale(lpj->ref, lpj->freq, freqs.new);
  108. }
  109. /* And don't forget to adjust the global one */
  110. if (!global_lpj_ref.freq) {
  111. global_lpj_ref.ref = loops_per_jiffy;
  112. global_lpj_ref.freq = freqs.old;
  113. }
  114. loops_per_jiffy = cpufreq_scale(global_lpj_ref.ref, global_lpj_ref.freq,
  115. freqs.new);
  116. #endif
  117. /* notifiers */
  118. for_each_cpu(i, policy->cpus) {
  119. freqs.cpu = i;
  120. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  121. }
  122. return ret;
  123. }
  124. static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
  125. {
  126. int result = 0;
  127. struct device *mpu_dev;
  128. if (cpu_is_omap24xx())
  129. mpu_clk = clk_get(NULL, "virt_prcm_set");
  130. else if (cpu_is_omap34xx())
  131. mpu_clk = clk_get(NULL, "dpll1_ck");
  132. else if (cpu_is_omap44xx())
  133. mpu_clk = clk_get(NULL, "dpll_mpu_ck");
  134. if (IS_ERR(mpu_clk))
  135. return PTR_ERR(mpu_clk);
  136. if (policy->cpu >= NR_CPUS)
  137. return -EINVAL;
  138. policy->cur = policy->min = policy->max = omap_getspeed(policy->cpu);
  139. mpu_dev = omap2_get_mpuss_device();
  140. if (!mpu_dev) {
  141. pr_warning("%s: unable to get the mpu device\n", __func__);
  142. return -EINVAL;
  143. }
  144. opp_init_cpufreq_table(mpu_dev, &freq_table);
  145. if (freq_table) {
  146. result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
  147. if (!result)
  148. cpufreq_frequency_table_get_attr(freq_table,
  149. policy->cpu);
  150. } else {
  151. policy->cpuinfo.min_freq = clk_round_rate(mpu_clk, 0) / 1000;
  152. policy->cpuinfo.max_freq = clk_round_rate(mpu_clk,
  153. VERY_HI_RATE) / 1000;
  154. }
  155. policy->min = policy->cpuinfo.min_freq;
  156. policy->max = policy->cpuinfo.max_freq;
  157. policy->cur = omap_getspeed(policy->cpu);
  158. /*
  159. * On OMAP SMP configuartion, both processors share the voltage
  160. * and clock. So both CPUs needs to be scaled together and hence
  161. * needs software co-ordination. Use cpufreq affected_cpus
  162. * interface to handle this scenario. Additional is_smp() check
  163. * is to keep SMP_ON_UP build working.
  164. */
  165. if (is_smp()) {
  166. policy->shared_type = CPUFREQ_SHARED_TYPE_ANY;
  167. cpumask_setall(policy->cpus);
  168. }
  169. /* FIXME: what's the actual transition time? */
  170. policy->cpuinfo.transition_latency = 300 * 1000;
  171. return 0;
  172. }
  173. static int omap_cpu_exit(struct cpufreq_policy *policy)
  174. {
  175. clk_exit_cpufreq_table(&freq_table);
  176. clk_put(mpu_clk);
  177. return 0;
  178. }
  179. static struct freq_attr *omap_cpufreq_attr[] = {
  180. &cpufreq_freq_attr_scaling_available_freqs,
  181. NULL,
  182. };
  183. static struct cpufreq_driver omap_driver = {
  184. .flags = CPUFREQ_STICKY,
  185. .verify = omap_verify_speed,
  186. .target = omap_target,
  187. .get = omap_getspeed,
  188. .init = omap_cpu_init,
  189. .exit = omap_cpu_exit,
  190. .name = "omap",
  191. .attr = omap_cpufreq_attr,
  192. };
  193. static int __init omap_cpufreq_init(void)
  194. {
  195. return cpufreq_register_driver(&omap_driver);
  196. }
  197. static void __exit omap_cpufreq_exit(void)
  198. {
  199. cpufreq_unregister_driver(&omap_driver);
  200. }
  201. MODULE_DESCRIPTION("cpufreq driver for OMAP SoCs");
  202. MODULE_LICENSE("GPL");
  203. module_init(omap_cpufreq_init);
  204. module_exit(omap_cpufreq_exit);