omap-cpufreq.c 4.9 KB

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