omap-cpufreq.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/pm_opp.h>
  26. #include <linux/cpu.h>
  27. #include <linux/module.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/regulator/consumer.h>
  30. #include <asm/smp_plat.h>
  31. #include <asm/cpu.h>
  32. /* OPP tolerance in percentage */
  33. #define OPP_TOLERANCE 4
  34. static struct cpufreq_frequency_table *freq_table;
  35. static atomic_t freq_table_users = ATOMIC_INIT(0);
  36. static struct clk *mpu_clk;
  37. static struct device *mpu_dev;
  38. static struct regulator *mpu_reg;
  39. static unsigned int omap_getspeed(unsigned int cpu)
  40. {
  41. unsigned long rate;
  42. if (cpu >= NR_CPUS)
  43. return 0;
  44. rate = clk_get_rate(mpu_clk) / 1000;
  45. return rate;
  46. }
  47. static int omap_target(struct cpufreq_policy *policy,
  48. unsigned int target_freq,
  49. unsigned int relation)
  50. {
  51. unsigned int i;
  52. int r, ret = 0;
  53. struct cpufreq_freqs freqs;
  54. struct dev_pm_opp *opp;
  55. unsigned long freq, volt = 0, volt_old = 0, tol = 0;
  56. if (!freq_table) {
  57. dev_err(mpu_dev, "%s: cpu%d: no freq table!\n", __func__,
  58. policy->cpu);
  59. return -EINVAL;
  60. }
  61. ret = cpufreq_frequency_table_target(policy, freq_table, target_freq,
  62. relation, &i);
  63. if (ret) {
  64. dev_dbg(mpu_dev, "%s: cpu%d: no freq match for %d(ret=%d)\n",
  65. __func__, policy->cpu, target_freq, ret);
  66. return ret;
  67. }
  68. freqs.new = freq_table[i].frequency;
  69. if (!freqs.new) {
  70. dev_err(mpu_dev, "%s: cpu%d: no match for freq %d\n", __func__,
  71. policy->cpu, target_freq);
  72. return -EINVAL;
  73. }
  74. freqs.old = omap_getspeed(policy->cpu);
  75. if (freqs.old == freqs.new && policy->cur == freqs.new)
  76. return ret;
  77. freq = freqs.new * 1000;
  78. ret = clk_round_rate(mpu_clk, freq);
  79. if (IS_ERR_VALUE(ret)) {
  80. dev_warn(mpu_dev,
  81. "CPUfreq: Cannot find matching frequency for %lu\n",
  82. freq);
  83. return ret;
  84. }
  85. freq = ret;
  86. if (mpu_reg) {
  87. rcu_read_lock();
  88. opp = dev_pm_opp_find_freq_ceil(mpu_dev, &freq);
  89. if (IS_ERR(opp)) {
  90. rcu_read_unlock();
  91. dev_err(mpu_dev, "%s: unable to find MPU OPP for %d\n",
  92. __func__, freqs.new);
  93. return -EINVAL;
  94. }
  95. volt = dev_pm_opp_get_voltage(opp);
  96. rcu_read_unlock();
  97. tol = volt * OPP_TOLERANCE / 100;
  98. volt_old = regulator_get_voltage(mpu_reg);
  99. }
  100. dev_dbg(mpu_dev, "cpufreq-omap: %u MHz, %ld mV --> %u MHz, %ld mV\n",
  101. freqs.old / 1000, volt_old ? volt_old / 1000 : -1,
  102. freqs.new / 1000, volt ? volt / 1000 : -1);
  103. /* notifiers */
  104. cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
  105. /* scaling up? scale voltage before frequency */
  106. if (mpu_reg && (freqs.new > freqs.old)) {
  107. r = regulator_set_voltage(mpu_reg, volt - tol, volt + tol);
  108. if (r < 0) {
  109. dev_warn(mpu_dev, "%s: unable to scale voltage up.\n",
  110. __func__);
  111. freqs.new = freqs.old;
  112. goto done;
  113. }
  114. }
  115. ret = clk_set_rate(mpu_clk, freqs.new * 1000);
  116. /* scaling down? scale voltage after frequency */
  117. if (mpu_reg && (freqs.new < freqs.old)) {
  118. r = regulator_set_voltage(mpu_reg, volt - tol, volt + tol);
  119. if (r < 0) {
  120. dev_warn(mpu_dev, "%s: unable to scale voltage down.\n",
  121. __func__);
  122. ret = clk_set_rate(mpu_clk, freqs.old * 1000);
  123. freqs.new = freqs.old;
  124. goto done;
  125. }
  126. }
  127. freqs.new = omap_getspeed(policy->cpu);
  128. done:
  129. /* notifiers */
  130. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  131. return ret;
  132. }
  133. static inline void freq_table_free(void)
  134. {
  135. if (atomic_dec_and_test(&freq_table_users))
  136. dev_pm_opp_free_cpufreq_table(mpu_dev, &freq_table);
  137. }
  138. static int omap_cpu_init(struct cpufreq_policy *policy)
  139. {
  140. int result;
  141. mpu_clk = clk_get(NULL, "cpufreq_ck");
  142. if (IS_ERR(mpu_clk))
  143. return PTR_ERR(mpu_clk);
  144. if (!freq_table) {
  145. result = dev_pm_opp_init_cpufreq_table(mpu_dev, &freq_table);
  146. if (result) {
  147. dev_err(mpu_dev,
  148. "%s: cpu%d: failed creating freq table[%d]\n",
  149. __func__, policy->cpu, result);
  150. goto fail;
  151. }
  152. }
  153. atomic_inc_return(&freq_table_users);
  154. /* FIXME: what's the actual transition time? */
  155. result = cpufreq_generic_init(policy, freq_table, 300 * 1000);
  156. if (!result)
  157. return 0;
  158. freq_table_free();
  159. fail:
  160. clk_put(mpu_clk);
  161. return result;
  162. }
  163. static int omap_cpu_exit(struct cpufreq_policy *policy)
  164. {
  165. cpufreq_frequency_table_put_attr(policy->cpu);
  166. freq_table_free();
  167. clk_put(mpu_clk);
  168. return 0;
  169. }
  170. static struct cpufreq_driver omap_driver = {
  171. .flags = CPUFREQ_STICKY,
  172. .verify = cpufreq_generic_frequency_table_verify,
  173. .target = omap_target,
  174. .get = omap_getspeed,
  175. .init = omap_cpu_init,
  176. .exit = omap_cpu_exit,
  177. .name = "omap",
  178. .attr = cpufreq_generic_attr,
  179. };
  180. static int omap_cpufreq_probe(struct platform_device *pdev)
  181. {
  182. mpu_dev = get_cpu_device(0);
  183. if (!mpu_dev) {
  184. pr_warning("%s: unable to get the mpu device\n", __func__);
  185. return -EINVAL;
  186. }
  187. mpu_reg = regulator_get(mpu_dev, "vcc");
  188. if (IS_ERR(mpu_reg)) {
  189. pr_warning("%s: unable to get MPU regulator\n", __func__);
  190. mpu_reg = NULL;
  191. } else {
  192. /*
  193. * Ensure physical regulator is present.
  194. * (e.g. could be dummy regulator.)
  195. */
  196. if (regulator_get_voltage(mpu_reg) < 0) {
  197. pr_warn("%s: physical regulator not present for MPU\n",
  198. __func__);
  199. regulator_put(mpu_reg);
  200. mpu_reg = NULL;
  201. }
  202. }
  203. return cpufreq_register_driver(&omap_driver);
  204. }
  205. static int omap_cpufreq_remove(struct platform_device *pdev)
  206. {
  207. return cpufreq_unregister_driver(&omap_driver);
  208. }
  209. static struct platform_driver omap_cpufreq_platdrv = {
  210. .driver = {
  211. .name = "omap-cpufreq",
  212. .owner = THIS_MODULE,
  213. },
  214. .probe = omap_cpufreq_probe,
  215. .remove = omap_cpufreq_remove,
  216. };
  217. module_platform_driver(omap_cpufreq_platdrv);
  218. MODULE_DESCRIPTION("cpufreq driver for OMAP SoCs");
  219. MODULE_LICENSE("GPL");