exynos-cpufreq.c 8.1 KB

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