omap-cpufreq.c 6.4 KB

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