omap-cpufreq.c 5.2 KB

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