2 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
5 * EXYNOS - CPU frequency scaling support for EXYNOS series
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.
12 #include <linux/kernel.h>
13 #include <linux/err.h>
14 #include <linux/clk.h>
16 #include <linux/slab.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/cpufreq.h>
19 #include <linux/suspend.h>
23 #include "exynos-cpufreq.h"
25 static struct exynos_dvfs_info *exynos_info;
27 static struct regulator *arm_regulator;
28 static struct cpufreq_freqs freqs;
30 static unsigned int locking_frequency;
31 static bool frequency_locked;
32 static DEFINE_MUTEX(cpufreq_lock);
34 static unsigned int exynos_getspeed(unsigned int cpu)
36 return clk_get_rate(exynos_info->cpu_clk) / 1000;
39 static int exynos_cpufreq_get_index(unsigned int freq)
41 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
45 freq_table[index].frequency != CPUFREQ_TABLE_END; index++)
46 if (freq_table[index].frequency == freq)
49 if (freq_table[index].frequency == CPUFREQ_TABLE_END)
55 static int exynos_cpufreq_scale(unsigned int target_freq)
57 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
58 unsigned int *volt_table = exynos_info->volt_table;
59 struct cpufreq_policy *policy = cpufreq_cpu_get(0);
60 unsigned int arm_volt, safe_arm_volt = 0;
61 unsigned int mpll_freq_khz = exynos_info->mpll_freq_khz;
65 freqs.old = policy->cur;
66 freqs.new = target_freq;
68 if (freqs.new == freqs.old)
72 * The policy max have been changed so that we cannot get proper
73 * old_index with cpufreq_frequency_table_target(). Thus, ignore
74 * policy and get the index from the raw freqeuncy table.
76 old_index = exynos_cpufreq_get_index(freqs.old);
82 index = exynos_cpufreq_get_index(target_freq);
89 * ARM clock source will be changed APLL to MPLL temporary
90 * To support this level, need to control regulator for
91 * required voltage level
93 if (exynos_info->need_apll_change != NULL) {
94 if (exynos_info->need_apll_change(old_index, index) &&
95 (freq_table[index].frequency < mpll_freq_khz) &&
96 (freq_table[old_index].frequency < mpll_freq_khz))
97 safe_arm_volt = volt_table[exynos_info->pll_safe_idx];
99 arm_volt = volt_table[index];
101 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
103 /* When the new frequency is higher than current frequency */
104 if ((freqs.new > freqs.old) && !safe_arm_volt) {
105 /* Firstly, voltage up to increase frequency */
106 ret = regulator_set_voltage(arm_regulator, arm_volt, arm_volt);
108 pr_err("%s: failed to set cpu voltage to %d\n",
110 freqs.new = freqs.old;
116 ret = regulator_set_voltage(arm_regulator, safe_arm_volt,
119 pr_err("%s: failed to set cpu voltage to %d\n",
120 __func__, safe_arm_volt);
121 freqs.new = freqs.old;
126 exynos_info->set_freq(old_index, index);
129 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
134 /* When the new frequency is lower than current frequency */
135 if ((freqs.new < freqs.old) ||
136 ((freqs.new > freqs.old) && safe_arm_volt)) {
137 /* down the voltage after frequency change */
138 ret = regulator_set_voltage(arm_regulator, arm_volt,
141 pr_err("%s: failed to set cpu voltage to %d\n",
149 cpufreq_cpu_put(policy);
154 static int exynos_target(struct cpufreq_policy *policy,
155 unsigned int target_freq,
156 unsigned int relation)
158 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
160 unsigned int new_freq;
163 mutex_lock(&cpufreq_lock);
165 if (frequency_locked)
168 if (cpufreq_frequency_table_target(policy, freq_table,
169 target_freq, relation, &index)) {
174 new_freq = freq_table[index].frequency;
176 ret = exynos_cpufreq_scale(new_freq);
179 mutex_unlock(&cpufreq_lock);
185 static int exynos_cpufreq_suspend(struct cpufreq_policy *policy)
190 static int exynos_cpufreq_resume(struct cpufreq_policy *policy)
197 * exynos_cpufreq_pm_notifier - block CPUFREQ's activities in suspend-resume
203 * While frequency_locked == true, target() ignores every frequency but
204 * locking_frequency. The locking_frequency value is the initial frequency,
205 * which is set by the bootloader. In order to eliminate possible
206 * inconsistency in clock values, we save and restore frequencies during
207 * suspend and resume and block CPUFREQ activities. Note that the standard
208 * suspend/resume cannot be used as they are too deep (syscore_ops) for
211 static int exynos_cpufreq_pm_notifier(struct notifier_block *notifier,
212 unsigned long pm_event, void *v)
217 case PM_SUSPEND_PREPARE:
218 mutex_lock(&cpufreq_lock);
219 frequency_locked = true;
220 mutex_unlock(&cpufreq_lock);
222 ret = exynos_cpufreq_scale(locking_frequency);
228 case PM_POST_SUSPEND:
229 mutex_lock(&cpufreq_lock);
230 frequency_locked = false;
231 mutex_unlock(&cpufreq_lock);
238 static struct notifier_block exynos_cpufreq_nb = {
239 .notifier_call = exynos_cpufreq_pm_notifier,
242 static int exynos_cpufreq_cpu_init(struct cpufreq_policy *policy)
244 return cpufreq_generic_init(policy, exynos_info->freq_table, 100000);
247 static struct cpufreq_driver exynos_driver = {
248 .flags = CPUFREQ_STICKY,
249 .verify = cpufreq_generic_frequency_table_verify,
250 .target = exynos_target,
251 .get = exynos_getspeed,
252 .init = exynos_cpufreq_cpu_init,
253 .exit = cpufreq_generic_exit,
254 .name = "exynos_cpufreq",
255 .attr = cpufreq_generic_attr,
257 .suspend = exynos_cpufreq_suspend,
258 .resume = exynos_cpufreq_resume,
262 static int __init exynos_cpufreq_init(void)
266 exynos_info = kzalloc(sizeof(*exynos_info), GFP_KERNEL);
270 if (soc_is_exynos4210())
271 ret = exynos4210_cpufreq_init(exynos_info);
272 else if (soc_is_exynos4212() || soc_is_exynos4412())
273 ret = exynos4x12_cpufreq_init(exynos_info);
274 else if (soc_is_exynos5250())
275 ret = exynos5250_cpufreq_init(exynos_info);
282 if (exynos_info->set_freq == NULL) {
283 pr_err("%s: No set_freq function (ERR)\n", __func__);
287 arm_regulator = regulator_get(NULL, "vdd_arm");
288 if (IS_ERR(arm_regulator)) {
289 pr_err("%s: failed to get resource vdd_arm\n", __func__);
293 locking_frequency = exynos_getspeed(0);
295 register_pm_notifier(&exynos_cpufreq_nb);
297 if (cpufreq_register_driver(&exynos_driver)) {
298 pr_err("%s: failed to register cpufreq driver\n", __func__);
304 unregister_pm_notifier(&exynos_cpufreq_nb);
306 regulator_put(arm_regulator);
311 late_initcall(exynos_cpufreq_init);