omap-cpufreq.c 5.0 KB

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