omap-cpufreq.c 5.5 KB

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