exynos-cpufreq.c 7.4 KB

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