omap-cpufreq.c 6.4 KB

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