omap-cpufreq.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * CPU frequency scaling for OMAP using OPP information
  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. #ifdef CONFIG_SMP
  35. struct lpj_info {
  36. unsigned long ref;
  37. unsigned int freq;
  38. };
  39. static DEFINE_PER_CPU(struct lpj_info, lpj_ref);
  40. static struct lpj_info global_lpj_ref;
  41. #endif
  42. static struct cpufreq_frequency_table *freq_table;
  43. static atomic_t freq_table_users = ATOMIC_INIT(0);
  44. static struct clk *mpu_clk;
  45. static char *mpu_clk_name;
  46. static struct device *mpu_dev;
  47. static int omap_verify_speed(struct cpufreq_policy *policy)
  48. {
  49. if (!freq_table)
  50. return -EINVAL;
  51. return cpufreq_frequency_table_verify(policy, freq_table);
  52. }
  53. static unsigned int omap_getspeed(unsigned int cpu)
  54. {
  55. unsigned long rate;
  56. if (cpu >= NR_CPUS)
  57. return 0;
  58. rate = clk_get_rate(mpu_clk) / 1000;
  59. return rate;
  60. }
  61. static int omap_target(struct cpufreq_policy *policy,
  62. unsigned int target_freq,
  63. unsigned int relation)
  64. {
  65. unsigned int i;
  66. int ret = 0;
  67. struct cpufreq_freqs freqs;
  68. if (!freq_table) {
  69. dev_err(mpu_dev, "%s: cpu%d: no freq table!\n", __func__,
  70. policy->cpu);
  71. return -EINVAL;
  72. }
  73. ret = cpufreq_frequency_table_target(policy, freq_table, target_freq,
  74. relation, &i);
  75. if (ret) {
  76. dev_dbg(mpu_dev, "%s: cpu%d: no freq match for %d(ret=%d)\n",
  77. __func__, policy->cpu, target_freq, ret);
  78. return ret;
  79. }
  80. freqs.new = freq_table[i].frequency;
  81. if (!freqs.new) {
  82. dev_err(mpu_dev, "%s: cpu%d: no match for freq %d\n", __func__,
  83. policy->cpu, target_freq);
  84. return -EINVAL;
  85. }
  86. freqs.old = omap_getspeed(policy->cpu);
  87. freqs.cpu = policy->cpu;
  88. if (freqs.old == freqs.new && policy->cur == freqs.new)
  89. return ret;
  90. /* notifiers */
  91. for_each_cpu(i, policy->cpus) {
  92. freqs.cpu = i;
  93. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  94. }
  95. #ifdef CONFIG_CPU_FREQ_DEBUG
  96. pr_info("cpufreq-omap: transition: %u --> %u\n", freqs.old, freqs.new);
  97. #endif
  98. ret = clk_set_rate(mpu_clk, freqs.new * 1000);
  99. freqs.new = omap_getspeed(policy->cpu);
  100. #ifdef CONFIG_SMP
  101. /*
  102. * Note that loops_per_jiffy is not updated on SMP systems in
  103. * cpufreq driver. So, update the per-CPU loops_per_jiffy value
  104. * on frequency transition. We need to update all dependent CPUs.
  105. */
  106. for_each_cpu(i, policy->cpus) {
  107. struct lpj_info *lpj = &per_cpu(lpj_ref, i);
  108. if (!lpj->freq) {
  109. lpj->ref = per_cpu(cpu_data, i).loops_per_jiffy;
  110. lpj->freq = freqs.old;
  111. }
  112. per_cpu(cpu_data, i).loops_per_jiffy =
  113. cpufreq_scale(lpj->ref, lpj->freq, freqs.new);
  114. }
  115. /* And don't forget to adjust the global one */
  116. if (!global_lpj_ref.freq) {
  117. global_lpj_ref.ref = loops_per_jiffy;
  118. global_lpj_ref.freq = freqs.old;
  119. }
  120. loops_per_jiffy = cpufreq_scale(global_lpj_ref.ref, global_lpj_ref.freq,
  121. freqs.new);
  122. #endif
  123. /* notifiers */
  124. for_each_cpu(i, policy->cpus) {
  125. freqs.cpu = i;
  126. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  127. }
  128. return ret;
  129. }
  130. static inline void freq_table_free(void)
  131. {
  132. if (atomic_dec_and_test(&freq_table_users))
  133. opp_free_cpufreq_table(mpu_dev, &freq_table);
  134. }
  135. static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
  136. {
  137. int result = 0;
  138. mpu_clk = clk_get(NULL, mpu_clk_name);
  139. if (IS_ERR(mpu_clk))
  140. return PTR_ERR(mpu_clk);
  141. if (policy->cpu >= NR_CPUS) {
  142. result = -EINVAL;
  143. goto fail_ck;
  144. }
  145. policy->cur = policy->min = policy->max = omap_getspeed(policy->cpu);
  146. if (atomic_inc_return(&freq_table_users) == 1)
  147. result = opp_init_cpufreq_table(mpu_dev, &freq_table);
  148. if (result) {
  149. dev_err(mpu_dev, "%s: cpu%d: failed creating freq table[%d]\n",
  150. __func__, policy->cpu, result);
  151. goto fail_ck;
  152. }
  153. result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
  154. if (result)
  155. goto fail_table;
  156. cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
  157. policy->min = policy->cpuinfo.min_freq;
  158. policy->max = policy->cpuinfo.max_freq;
  159. policy->cur = omap_getspeed(policy->cpu);
  160. /*
  161. * On OMAP SMP configuartion, both processors share the voltage
  162. * and clock. So both CPUs needs to be scaled together and hence
  163. * needs software co-ordination. Use cpufreq affected_cpus
  164. * interface to handle this scenario. Additional is_smp() check
  165. * is to keep SMP_ON_UP build working.
  166. */
  167. if (is_smp()) {
  168. policy->shared_type = CPUFREQ_SHARED_TYPE_ANY;
  169. cpumask_setall(policy->cpus);
  170. }
  171. /* FIXME: what's the actual transition time? */
  172. policy->cpuinfo.transition_latency = 300 * 1000;
  173. return 0;
  174. fail_table:
  175. freq_table_free();
  176. fail_ck:
  177. clk_put(mpu_clk);
  178. return result;
  179. }
  180. static int omap_cpu_exit(struct cpufreq_policy *policy)
  181. {
  182. freq_table_free();
  183. clk_put(mpu_clk);
  184. return 0;
  185. }
  186. static struct freq_attr *omap_cpufreq_attr[] = {
  187. &cpufreq_freq_attr_scaling_available_freqs,
  188. NULL,
  189. };
  190. static struct cpufreq_driver omap_driver = {
  191. .flags = CPUFREQ_STICKY,
  192. .verify = omap_verify_speed,
  193. .target = omap_target,
  194. .get = omap_getspeed,
  195. .init = omap_cpu_init,
  196. .exit = omap_cpu_exit,
  197. .name = "omap",
  198. .attr = omap_cpufreq_attr,
  199. };
  200. static int __init omap_cpufreq_init(void)
  201. {
  202. if (cpu_is_omap24xx())
  203. mpu_clk_name = "virt_prcm_set";
  204. else if (cpu_is_omap34xx())
  205. mpu_clk_name = "dpll1_ck";
  206. else if (cpu_is_omap44xx())
  207. mpu_clk_name = "dpll_mpu_ck";
  208. if (!mpu_clk_name) {
  209. pr_err("%s: unsupported Silicon?\n", __func__);
  210. return -EINVAL;
  211. }
  212. mpu_dev = omap2_get_mpuss_device();
  213. if (!mpu_dev) {
  214. pr_warning("%s: unable to get the mpu device\n", __func__);
  215. return -EINVAL;
  216. }
  217. return cpufreq_register_driver(&omap_driver);
  218. }
  219. static void __exit omap_cpufreq_exit(void)
  220. {
  221. cpufreq_unregister_driver(&omap_driver);
  222. }
  223. MODULE_DESCRIPTION("cpufreq driver for OMAP SoCs");
  224. MODULE_LICENSE("GPL");
  225. module_init(omap_cpufreq_init);
  226. module_exit(omap_cpufreq_exit);