exynos-cpufreq.c 7.1 KB

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