omap-cpufreq.c 5.0 KB

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