omap-cpufreq.c 5.2 KB

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