omap-cpufreq.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 <linux/regulator/consumer.h>
  29. #include <asm/smp_plat.h>
  30. #include <asm/cpu.h>
  31. /* OPP tolerance in percentage */
  32. #define OPP_TOLERANCE 4
  33. static struct cpufreq_frequency_table *freq_table;
  34. static atomic_t freq_table_users = ATOMIC_INIT(0);
  35. static struct clk *mpu_clk;
  36. static struct device *mpu_dev;
  37. static struct regulator *mpu_reg;
  38. static int omap_verify_speed(struct cpufreq_policy *policy)
  39. {
  40. if (!freq_table)
  41. return -EINVAL;
  42. return cpufreq_frequency_table_verify(policy, freq_table);
  43. }
  44. static unsigned int omap_getspeed(unsigned int cpu)
  45. {
  46. unsigned long rate;
  47. if (cpu >= NR_CPUS)
  48. return 0;
  49. rate = clk_get_rate(mpu_clk) / 1000;
  50. return rate;
  51. }
  52. static int omap_target(struct cpufreq_policy *policy,
  53. unsigned int target_freq,
  54. unsigned int relation)
  55. {
  56. unsigned int i;
  57. int r, ret = 0;
  58. struct cpufreq_freqs freqs;
  59. struct opp *opp;
  60. unsigned long freq, volt = 0, volt_old = 0, tol = 0;
  61. if (!freq_table) {
  62. dev_err(mpu_dev, "%s: cpu%d: no freq table!\n", __func__,
  63. policy->cpu);
  64. return -EINVAL;
  65. }
  66. ret = cpufreq_frequency_table_target(policy, freq_table, target_freq,
  67. relation, &i);
  68. if (ret) {
  69. dev_dbg(mpu_dev, "%s: cpu%d: no freq match for %d(ret=%d)\n",
  70. __func__, policy->cpu, target_freq, ret);
  71. return ret;
  72. }
  73. freqs.new = freq_table[i].frequency;
  74. if (!freqs.new) {
  75. dev_err(mpu_dev, "%s: cpu%d: no match for freq %d\n", __func__,
  76. policy->cpu, target_freq);
  77. return -EINVAL;
  78. }
  79. freqs.old = omap_getspeed(policy->cpu);
  80. freqs.cpu = policy->cpu;
  81. if (freqs.old == freqs.new && policy->cur == freqs.new)
  82. return ret;
  83. /* notifiers */
  84. for_each_cpu(i, policy->cpus) {
  85. freqs.cpu = i;
  86. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  87. }
  88. freq = freqs.new * 1000;
  89. ret = clk_round_rate(mpu_clk, freq);
  90. if (IS_ERR_VALUE(ret)) {
  91. dev_warn(mpu_dev,
  92. "CPUfreq: Cannot find matching frequency for %lu\n",
  93. freq);
  94. return ret;
  95. }
  96. freq = ret;
  97. if (mpu_reg) {
  98. rcu_read_lock();
  99. opp = opp_find_freq_ceil(mpu_dev, &freq);
  100. if (IS_ERR(opp)) {
  101. rcu_read_unlock();
  102. dev_err(mpu_dev, "%s: unable to find MPU OPP for %d\n",
  103. __func__, freqs.new);
  104. return -EINVAL;
  105. }
  106. volt = opp_get_voltage(opp);
  107. rcu_read_unlock();
  108. tol = volt * OPP_TOLERANCE / 100;
  109. volt_old = regulator_get_voltage(mpu_reg);
  110. }
  111. dev_dbg(mpu_dev, "cpufreq-omap: %u MHz, %ld mV --> %u MHz, %ld mV\n",
  112. freqs.old / 1000, volt_old ? volt_old / 1000 : -1,
  113. freqs.new / 1000, volt ? volt / 1000 : -1);
  114. /* scaling up? scale voltage before frequency */
  115. if (mpu_reg && (freqs.new > freqs.old)) {
  116. r = regulator_set_voltage(mpu_reg, volt - tol, volt + tol);
  117. if (r < 0) {
  118. dev_warn(mpu_dev, "%s: unable to scale voltage up.\n",
  119. __func__);
  120. freqs.new = freqs.old;
  121. goto done;
  122. }
  123. }
  124. ret = clk_set_rate(mpu_clk, freqs.new * 1000);
  125. /* scaling down? scale voltage after frequency */
  126. if (mpu_reg && (freqs.new < freqs.old)) {
  127. r = regulator_set_voltage(mpu_reg, volt - tol, volt + tol);
  128. if (r < 0) {
  129. dev_warn(mpu_dev, "%s: unable to scale voltage down.\n",
  130. __func__);
  131. ret = clk_set_rate(mpu_clk, freqs.old * 1000);
  132. freqs.new = freqs.old;
  133. goto done;
  134. }
  135. }
  136. freqs.new = omap_getspeed(policy->cpu);
  137. done:
  138. /* notifiers */
  139. for_each_cpu(i, policy->cpus) {
  140. freqs.cpu = i;
  141. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  142. }
  143. return ret;
  144. }
  145. static inline void freq_table_free(void)
  146. {
  147. if (atomic_dec_and_test(&freq_table_users))
  148. opp_free_cpufreq_table(mpu_dev, &freq_table);
  149. }
  150. static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
  151. {
  152. int result = 0;
  153. mpu_clk = clk_get(NULL, "cpufreq_ck");
  154. if (IS_ERR(mpu_clk))
  155. return PTR_ERR(mpu_clk);
  156. if (policy->cpu >= NR_CPUS) {
  157. result = -EINVAL;
  158. goto fail_ck;
  159. }
  160. policy->cur = policy->min = policy->max = omap_getspeed(policy->cpu);
  161. if (!freq_table)
  162. result = opp_init_cpufreq_table(mpu_dev, &freq_table);
  163. if (result) {
  164. dev_err(mpu_dev, "%s: cpu%d: failed creating freq table[%d]\n",
  165. __func__, policy->cpu, result);
  166. goto fail_ck;
  167. }
  168. atomic_inc_return(&freq_table_users);
  169. result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
  170. if (result)
  171. goto fail_table;
  172. cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
  173. policy->min = policy->cpuinfo.min_freq;
  174. policy->max = policy->cpuinfo.max_freq;
  175. policy->cur = omap_getspeed(policy->cpu);
  176. /*
  177. * On OMAP SMP configuartion, both processors share the voltage
  178. * and clock. So both CPUs needs to be scaled together and hence
  179. * needs software co-ordination. Use cpufreq affected_cpus
  180. * interface to handle this scenario. Additional is_smp() check
  181. * is to keep SMP_ON_UP build working.
  182. */
  183. if (is_smp())
  184. cpumask_setall(policy->cpus);
  185. /* FIXME: what's the actual transition time? */
  186. policy->cpuinfo.transition_latency = 300 * 1000;
  187. return 0;
  188. fail_table:
  189. freq_table_free();
  190. fail_ck:
  191. clk_put(mpu_clk);
  192. return result;
  193. }
  194. static int omap_cpu_exit(struct cpufreq_policy *policy)
  195. {
  196. freq_table_free();
  197. clk_put(mpu_clk);
  198. return 0;
  199. }
  200. static struct freq_attr *omap_cpufreq_attr[] = {
  201. &cpufreq_freq_attr_scaling_available_freqs,
  202. NULL,
  203. };
  204. static struct cpufreq_driver omap_driver = {
  205. .flags = CPUFREQ_STICKY,
  206. .verify = omap_verify_speed,
  207. .target = omap_target,
  208. .get = omap_getspeed,
  209. .init = omap_cpu_init,
  210. .exit = omap_cpu_exit,
  211. .name = "omap",
  212. .attr = omap_cpufreq_attr,
  213. };
  214. static int __init omap_cpufreq_init(void)
  215. {
  216. mpu_dev = get_cpu_device(0);
  217. if (!mpu_dev) {
  218. pr_warning("%s: unable to get the mpu device\n", __func__);
  219. return -EINVAL;
  220. }
  221. mpu_reg = regulator_get(mpu_dev, "vcc");
  222. if (IS_ERR(mpu_reg)) {
  223. pr_warning("%s: unable to get MPU regulator\n", __func__);
  224. mpu_reg = NULL;
  225. } else {
  226. /*
  227. * Ensure physical regulator is present.
  228. * (e.g. could be dummy regulator.)
  229. */
  230. if (regulator_get_voltage(mpu_reg) < 0) {
  231. pr_warn("%s: physical regulator not present for MPU\n",
  232. __func__);
  233. regulator_put(mpu_reg);
  234. mpu_reg = NULL;
  235. }
  236. }
  237. return cpufreq_register_driver(&omap_driver);
  238. }
  239. static void __exit omap_cpufreq_exit(void)
  240. {
  241. cpufreq_unregister_driver(&omap_driver);
  242. }
  243. MODULE_DESCRIPTION("cpufreq driver for OMAP SoCs");
  244. MODULE_LICENSE("GPL");
  245. module_init(omap_cpufreq_init);
  246. module_exit(omap_cpufreq_exit);