exynos-cpufreq.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * EXYNOS - CPU frequency scaling support for EXYNOS series
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/err.h>
  13. #include <linux/clk.h>
  14. #include <linux/io.h>
  15. #include <linux/slab.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/cpufreq.h>
  18. #include <linux/suspend.h>
  19. #include <plat/cpu.h>
  20. #include "exynos-cpufreq.h"
  21. static struct exynos_dvfs_info *exynos_info;
  22. static struct regulator *arm_regulator;
  23. static unsigned int locking_frequency;
  24. static bool frequency_locked;
  25. static DEFINE_MUTEX(cpufreq_lock);
  26. static unsigned int exynos_getspeed(unsigned int cpu)
  27. {
  28. return clk_get_rate(exynos_info->cpu_clk) / 1000;
  29. }
  30. static int exynos_cpufreq_get_index(unsigned int freq)
  31. {
  32. struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
  33. int index;
  34. for (index = 0;
  35. freq_table[index].frequency != CPUFREQ_TABLE_END; index++)
  36. if (freq_table[index].frequency == freq)
  37. break;
  38. if (freq_table[index].frequency == CPUFREQ_TABLE_END)
  39. return -EINVAL;
  40. return index;
  41. }
  42. static int exynos_cpufreq_scale(unsigned int target_freq)
  43. {
  44. struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
  45. unsigned int *volt_table = exynos_info->volt_table;
  46. struct cpufreq_policy *policy = cpufreq_cpu_get(0);
  47. unsigned int arm_volt, safe_arm_volt = 0;
  48. unsigned int mpll_freq_khz = exynos_info->mpll_freq_khz;
  49. unsigned int old_freq;
  50. int index, old_index;
  51. int ret = 0;
  52. old_freq = policy->cur;
  53. /*
  54. * The policy max have been changed so that we cannot get proper
  55. * old_index with cpufreq_frequency_table_target(). Thus, ignore
  56. * policy and get the index from the raw freqeuncy table.
  57. */
  58. old_index = exynos_cpufreq_get_index(old_freq);
  59. if (old_index < 0) {
  60. ret = old_index;
  61. goto out;
  62. }
  63. index = exynos_cpufreq_get_index(target_freq);
  64. if (index < 0) {
  65. ret = index;
  66. goto out;
  67. }
  68. /*
  69. * ARM clock source will be changed APLL to MPLL temporary
  70. * To support this level, need to control regulator for
  71. * required voltage level
  72. */
  73. if (exynos_info->need_apll_change != NULL) {
  74. if (exynos_info->need_apll_change(old_index, index) &&
  75. (freq_table[index].frequency < mpll_freq_khz) &&
  76. (freq_table[old_index].frequency < mpll_freq_khz))
  77. safe_arm_volt = volt_table[exynos_info->pll_safe_idx];
  78. }
  79. arm_volt = volt_table[index];
  80. /* When the new frequency is higher than current frequency */
  81. if ((target_freq > old_freq) && !safe_arm_volt) {
  82. /* Firstly, voltage up to increase frequency */
  83. ret = regulator_set_voltage(arm_regulator, arm_volt, arm_volt);
  84. if (ret) {
  85. pr_err("%s: failed to set cpu voltage to %d\n",
  86. __func__, arm_volt);
  87. return ret;
  88. }
  89. }
  90. if (safe_arm_volt) {
  91. ret = regulator_set_voltage(arm_regulator, safe_arm_volt,
  92. safe_arm_volt);
  93. if (ret) {
  94. pr_err("%s: failed to set cpu voltage to %d\n",
  95. __func__, safe_arm_volt);
  96. return ret;
  97. }
  98. }
  99. exynos_info->set_freq(old_index, index);
  100. /* When the new frequency is lower than current frequency */
  101. if ((target_freq < old_freq) ||
  102. ((target_freq > old_freq) && safe_arm_volt)) {
  103. /* down the voltage after frequency change */
  104. ret = regulator_set_voltage(arm_regulator, arm_volt,
  105. arm_volt);
  106. if (ret) {
  107. pr_err("%s: failed to set cpu voltage to %d\n",
  108. __func__, arm_volt);
  109. goto out;
  110. }
  111. }
  112. out:
  113. cpufreq_cpu_put(policy);
  114. return ret;
  115. }
  116. static int exynos_target(struct cpufreq_policy *policy, unsigned int index)
  117. {
  118. struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
  119. int ret = 0;
  120. mutex_lock(&cpufreq_lock);
  121. if (frequency_locked)
  122. goto out;
  123. ret = exynos_cpufreq_scale(freq_table[index].frequency);
  124. out:
  125. mutex_unlock(&cpufreq_lock);
  126. return ret;
  127. }
  128. #ifdef CONFIG_PM
  129. static int exynos_cpufreq_suspend(struct cpufreq_policy *policy)
  130. {
  131. return 0;
  132. }
  133. static int exynos_cpufreq_resume(struct cpufreq_policy *policy)
  134. {
  135. return 0;
  136. }
  137. #endif
  138. /**
  139. * exynos_cpufreq_pm_notifier - block CPUFREQ's activities in suspend-resume
  140. * context
  141. * @notifier
  142. * @pm_event
  143. * @v
  144. *
  145. * While frequency_locked == true, target() ignores every frequency but
  146. * locking_frequency. The locking_frequency value is the initial frequency,
  147. * which is set by the bootloader. In order to eliminate possible
  148. * inconsistency in clock values, we save and restore frequencies during
  149. * suspend and resume and block CPUFREQ activities. Note that the standard
  150. * suspend/resume cannot be used as they are too deep (syscore_ops) for
  151. * regulator actions.
  152. */
  153. static int exynos_cpufreq_pm_notifier(struct notifier_block *notifier,
  154. unsigned long pm_event, void *v)
  155. {
  156. int ret;
  157. switch (pm_event) {
  158. case PM_SUSPEND_PREPARE:
  159. mutex_lock(&cpufreq_lock);
  160. frequency_locked = true;
  161. mutex_unlock(&cpufreq_lock);
  162. ret = exynos_cpufreq_scale(locking_frequency);
  163. if (ret < 0)
  164. return NOTIFY_BAD;
  165. break;
  166. case PM_POST_SUSPEND:
  167. mutex_lock(&cpufreq_lock);
  168. frequency_locked = false;
  169. mutex_unlock(&cpufreq_lock);
  170. break;
  171. }
  172. return NOTIFY_OK;
  173. }
  174. static struct notifier_block exynos_cpufreq_nb = {
  175. .notifier_call = exynos_cpufreq_pm_notifier,
  176. };
  177. static int exynos_cpufreq_cpu_init(struct cpufreq_policy *policy)
  178. {
  179. return cpufreq_generic_init(policy, exynos_info->freq_table, 100000);
  180. }
  181. static struct cpufreq_driver exynos_driver = {
  182. .flags = CPUFREQ_STICKY,
  183. .verify = cpufreq_generic_frequency_table_verify,
  184. .target_index = exynos_target,
  185. .get = exynos_getspeed,
  186. .init = exynos_cpufreq_cpu_init,
  187. .exit = cpufreq_generic_exit,
  188. .name = "exynos_cpufreq",
  189. .attr = cpufreq_generic_attr,
  190. #ifdef CONFIG_PM
  191. .suspend = exynos_cpufreq_suspend,
  192. .resume = exynos_cpufreq_resume,
  193. #endif
  194. };
  195. static int __init exynos_cpufreq_init(void)
  196. {
  197. int ret = -EINVAL;
  198. exynos_info = kzalloc(sizeof(*exynos_info), GFP_KERNEL);
  199. if (!exynos_info)
  200. return -ENOMEM;
  201. if (soc_is_exynos4210())
  202. ret = exynos4210_cpufreq_init(exynos_info);
  203. else if (soc_is_exynos4212() || soc_is_exynos4412())
  204. ret = exynos4x12_cpufreq_init(exynos_info);
  205. else if (soc_is_exynos5250())
  206. ret = exynos5250_cpufreq_init(exynos_info);
  207. else
  208. return 0;
  209. if (ret)
  210. goto err_vdd_arm;
  211. if (exynos_info->set_freq == NULL) {
  212. pr_err("%s: No set_freq function (ERR)\n", __func__);
  213. goto err_vdd_arm;
  214. }
  215. arm_regulator = regulator_get(NULL, "vdd_arm");
  216. if (IS_ERR(arm_regulator)) {
  217. pr_err("%s: failed to get resource vdd_arm\n", __func__);
  218. goto err_vdd_arm;
  219. }
  220. locking_frequency = exynos_getspeed(0);
  221. register_pm_notifier(&exynos_cpufreq_nb);
  222. if (cpufreq_register_driver(&exynos_driver)) {
  223. pr_err("%s: failed to register cpufreq driver\n", __func__);
  224. goto err_cpufreq;
  225. }
  226. return 0;
  227. err_cpufreq:
  228. unregister_pm_notifier(&exynos_cpufreq_nb);
  229. regulator_put(arm_regulator);
  230. err_vdd_arm:
  231. kfree(exynos_info);
  232. return -EINVAL;
  233. }
  234. late_initcall(exynos_cpufreq_init);