omap-cpufreq.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/sched.h>
  20. #include <linux/cpufreq.h>
  21. #include <linux/delay.h>
  22. #include <linux/init.h>
  23. #include <linux/err.h>
  24. #include <linux/clk.h>
  25. #include <linux/io.h>
  26. #include <linux/pm_opp.h>
  27. #include <linux/cpu.h>
  28. #include <linux/module.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/regulator/consumer.h>
  31. #include <asm/smp_plat.h>
  32. #include <asm/cpu.h>
  33. /* OPP tolerance in percentage */
  34. #define OPP_TOLERANCE 4
  35. static struct cpufreq_frequency_table *freq_table;
  36. static atomic_t freq_table_users = ATOMIC_INIT(0);
  37. static struct device *mpu_dev;
  38. static struct regulator *mpu_reg;
  39. static int omap_target(struct cpufreq_policy *policy, unsigned int index)
  40. {
  41. int r, ret;
  42. struct dev_pm_opp *opp;
  43. unsigned long freq, volt = 0, volt_old = 0, tol = 0;
  44. unsigned int old_freq, new_freq;
  45. old_freq = policy->cur;
  46. new_freq = freq_table[index].frequency;
  47. freq = new_freq * 1000;
  48. ret = clk_round_rate(policy->clk, freq);
  49. if (ret < 0) {
  50. dev_warn(mpu_dev,
  51. "CPUfreq: Cannot find matching frequency for %lu\n",
  52. freq);
  53. return ret;
  54. }
  55. freq = ret;
  56. if (mpu_reg) {
  57. rcu_read_lock();
  58. opp = dev_pm_opp_find_freq_ceil(mpu_dev, &freq);
  59. if (IS_ERR(opp)) {
  60. rcu_read_unlock();
  61. dev_err(mpu_dev, "%s: unable to find MPU OPP for %d\n",
  62. __func__, new_freq);
  63. return -EINVAL;
  64. }
  65. volt = dev_pm_opp_get_voltage(opp);
  66. rcu_read_unlock();
  67. tol = volt * OPP_TOLERANCE / 100;
  68. volt_old = regulator_get_voltage(mpu_reg);
  69. }
  70. dev_dbg(mpu_dev, "cpufreq-omap: %u MHz, %ld mV --> %u MHz, %ld mV\n",
  71. old_freq / 1000, volt_old ? volt_old / 1000 : -1,
  72. new_freq / 1000, volt ? volt / 1000 : -1);
  73. /* scaling up? scale voltage before frequency */
  74. if (mpu_reg && (new_freq > old_freq)) {
  75. r = regulator_set_voltage(mpu_reg, volt - tol, volt + tol);
  76. if (r < 0) {
  77. dev_warn(mpu_dev, "%s: unable to scale voltage up.\n",
  78. __func__);
  79. return r;
  80. }
  81. }
  82. ret = clk_set_rate(policy->clk, new_freq * 1000);
  83. /* scaling down? scale voltage after frequency */
  84. if (mpu_reg && (new_freq < old_freq)) {
  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 down.\n",
  88. __func__);
  89. clk_set_rate(policy->clk, old_freq * 1000);
  90. return r;
  91. }
  92. }
  93. return ret;
  94. }
  95. static inline void freq_table_free(void)
  96. {
  97. if (atomic_dec_and_test(&freq_table_users))
  98. dev_pm_opp_free_cpufreq_table(mpu_dev, &freq_table);
  99. }
  100. static int omap_cpu_init(struct cpufreq_policy *policy)
  101. {
  102. int result;
  103. policy->clk = clk_get(NULL, "cpufreq_ck");
  104. if (IS_ERR(policy->clk))
  105. return PTR_ERR(policy->clk);
  106. if (!freq_table) {
  107. result = dev_pm_opp_init_cpufreq_table(mpu_dev, &freq_table);
  108. if (result) {
  109. dev_err(mpu_dev,
  110. "%s: cpu%d: failed creating freq table[%d]\n",
  111. __func__, policy->cpu, result);
  112. goto fail;
  113. }
  114. }
  115. atomic_inc_return(&freq_table_users);
  116. /* FIXME: what's the actual transition time? */
  117. result = cpufreq_generic_init(policy, freq_table, 300 * 1000);
  118. if (!result)
  119. return 0;
  120. freq_table_free();
  121. fail:
  122. clk_put(policy->clk);
  123. return result;
  124. }
  125. static int omap_cpu_exit(struct cpufreq_policy *policy)
  126. {
  127. freq_table_free();
  128. clk_put(policy->clk);
  129. return 0;
  130. }
  131. static struct cpufreq_driver omap_driver = {
  132. .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  133. .verify = cpufreq_generic_frequency_table_verify,
  134. .target_index = omap_target,
  135. .get = cpufreq_generic_get,
  136. .init = omap_cpu_init,
  137. .exit = omap_cpu_exit,
  138. .name = "omap",
  139. .attr = cpufreq_generic_attr,
  140. };
  141. static int omap_cpufreq_probe(struct platform_device *pdev)
  142. {
  143. mpu_dev = get_cpu_device(0);
  144. if (!mpu_dev) {
  145. pr_warn("%s: unable to get the MPU device\n", __func__);
  146. return -EINVAL;
  147. }
  148. mpu_reg = regulator_get(mpu_dev, "vcc");
  149. if (IS_ERR(mpu_reg)) {
  150. pr_warn("%s: unable to get MPU regulator\n", __func__);
  151. mpu_reg = NULL;
  152. } else {
  153. /*
  154. * Ensure physical regulator is present.
  155. * (e.g. could be dummy regulator.)
  156. */
  157. if (regulator_get_voltage(mpu_reg) < 0) {
  158. pr_warn("%s: physical regulator not present for MPU\n",
  159. __func__);
  160. regulator_put(mpu_reg);
  161. mpu_reg = NULL;
  162. }
  163. }
  164. return cpufreq_register_driver(&omap_driver);
  165. }
  166. static int omap_cpufreq_remove(struct platform_device *pdev)
  167. {
  168. return cpufreq_unregister_driver(&omap_driver);
  169. }
  170. static struct platform_driver omap_cpufreq_platdrv = {
  171. .driver = {
  172. .name = "omap-cpufreq",
  173. },
  174. .probe = omap_cpufreq_probe,
  175. .remove = omap_cpufreq_remove,
  176. };
  177. module_platform_driver(omap_cpufreq_platdrv);
  178. MODULE_DESCRIPTION("cpufreq driver for OMAP SoCs");
  179. MODULE_LICENSE("GPL");