exynos-cpufreq.c 7.8 KB

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