exynos-cpufreq.c 8.4 KB

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