blob: b96fcc847636dba107eec92dd9860af6239d408f [file] [log] [blame]
Jaecheol Leea125a172012-01-07 20:18:35 +09001/*
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
Jaecheol Leea125a172012-01-07 20:18:35 +090012#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>
Jaecheol Leea125a172012-01-07 20:18:35 +090020
Jaecheol Lee6c523c62012-01-07 20:18:39 +090021#include <plat/cpu.h>
Jaecheol Leea125a172012-01-07 20:18:35 +090022
Kukjin Kimc4aaa292012-12-28 16:29:10 -080023#include "exynos-cpufreq.h"
24
Jaecheol Leea125a172012-01-07 20:18:35 +090025static struct exynos_dvfs_info *exynos_info;
26
27static struct regulator *arm_regulator;
28static struct cpufreq_freqs freqs;
29
30static unsigned int locking_frequency;
31static bool frequency_locked;
32static DEFINE_MUTEX(cpufreq_lock);
33
Tushar Behera55427212012-11-22 00:19:25 +010034static unsigned int exynos_getspeed(unsigned int cpu)
Jaecheol Leea125a172012-01-07 20:18:35 +090035{
36 return clk_get_rate(exynos_info->cpu_clk) / 1000;
37}
38
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080039static int exynos_cpufreq_get_index(unsigned int freq)
Jaecheol Leea125a172012-01-07 20:18:35 +090040{
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080041 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
42 int index;
43
44 for (index = 0;
45 freq_table[index].frequency != CPUFREQ_TABLE_END; index++)
46 if (freq_table[index].frequency == freq)
47 break;
48
49 if (freq_table[index].frequency == CPUFREQ_TABLE_END)
50 return -EINVAL;
51
52 return index;
53}
54
55static int exynos_cpufreq_scale(unsigned int target_freq)
56{
Jaecheol Leea125a172012-01-07 20:18:35 +090057 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
58 unsigned int *volt_table = exynos_info->volt_table;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080059 struct cpufreq_policy *policy = cpufreq_cpu_get(0);
60 unsigned int arm_volt, safe_arm_volt = 0;
Jaecheol Leea125a172012-01-07 20:18:35 +090061 unsigned int mpll_freq_khz = exynos_info->mpll_freq_khz;
Sachin Kamatd271d072013-01-25 10:18:09 -080062 int index, old_index;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080063 int ret = 0;
Jaecheol Leea125a172012-01-07 20:18:35 +090064
65 freqs.old = policy->cur;
Jonghwan Choic098ea72013-02-04 21:01:22 -080066 freqs.new = target_freq;
Jaecheol Leea125a172012-01-07 20:18:35 +090067
Jonghwan Choic098ea72013-02-04 21:01:22 -080068 if (freqs.new == freqs.old)
Jaecheol Leea125a172012-01-07 20:18:35 +090069 goto out;
Jaecheol Leea125a172012-01-07 20:18:35 +090070
Jonghwa Lee53df1ad2012-07-20 02:54:02 +000071 /*
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.
75 */
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080076 old_index = exynos_cpufreq_get_index(freqs.old);
77 if (old_index < 0) {
78 ret = old_index;
Jaecheol Leea125a172012-01-07 20:18:35 +090079 goto out;
80 }
81
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080082 index = exynos_cpufreq_get_index(target_freq);
83 if (index < 0) {
84 ret = index;
Jaecheol Leea125a172012-01-07 20:18:35 +090085 goto out;
86 }
87
Jaecheol Leea125a172012-01-07 20:18:35 +090088 /*
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
92 */
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];
98 }
99 arm_volt = volt_table[index];
100
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530101 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Jaecheol Leea125a172012-01-07 20:18:35 +0900102
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 */
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800106 ret = regulator_set_voltage(arm_regulator, arm_volt, arm_volt);
107 if (ret) {
108 pr_err("%s: failed to set cpu voltage to %d\n",
109 __func__, arm_volt);
Viresh Kumarc3aca6b2013-06-19 11:18:20 +0530110 freqs.new = freqs.old;
111 goto post_notify;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800112 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900113 }
114
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800115 if (safe_arm_volt) {
116 ret = regulator_set_voltage(arm_regulator, safe_arm_volt,
Jaecheol Leea125a172012-01-07 20:18:35 +0900117 safe_arm_volt);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800118 if (ret) {
119 pr_err("%s: failed to set cpu voltage to %d\n",
120 __func__, safe_arm_volt);
Viresh Kumarc3aca6b2013-06-19 11:18:20 +0530121 freqs.new = freqs.old;
122 goto post_notify;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800123 }
124 }
Jonghwan Choi857d90f2012-12-23 15:57:39 -0800125
126 exynos_info->set_freq(old_index, index);
Jaecheol Leea125a172012-01-07 20:18:35 +0900127
Viresh Kumarc3aca6b2013-06-19 11:18:20 +0530128post_notify:
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530129 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Jaecheol Leea125a172012-01-07 20:18:35 +0900130
Viresh Kumarc3aca6b2013-06-19 11:18:20 +0530131 if (ret)
132 goto out;
133
Jaecheol Leea125a172012-01-07 20:18:35 +0900134 /* 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 regulator_set_voltage(arm_regulator, arm_volt,
139 arm_volt);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800140 if (ret) {
141 pr_err("%s: failed to set cpu voltage to %d\n",
142 __func__, arm_volt);
143 goto out;
144 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900145 }
146
147out:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800148
149 cpufreq_cpu_put(policy);
150
151 return ret;
152}
153
154static int exynos_target(struct cpufreq_policy *policy,
155 unsigned int target_freq,
156 unsigned int relation)
157{
158 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
159 unsigned int index;
Jonghwan Choic098ea72013-02-04 21:01:22 -0800160 unsigned int new_freq;
Sachin Kamat229b21e2013-01-31 17:13:39 -0800161 int ret = 0;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800162
163 mutex_lock(&cpufreq_lock);
164
165 if (frequency_locked)
166 goto out;
167
168 if (cpufreq_frequency_table_target(policy, freq_table,
169 target_freq, relation, &index)) {
170 ret = -EINVAL;
171 goto out;
172 }
173
Jonghwan Choic098ea72013-02-04 21:01:22 -0800174 new_freq = freq_table[index].frequency;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800175
Jonghwan Choic098ea72013-02-04 21:01:22 -0800176 ret = exynos_cpufreq_scale(new_freq);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800177
178out:
Jaecheol Leea125a172012-01-07 20:18:35 +0900179 mutex_unlock(&cpufreq_lock);
180
181 return ret;
182}
183
184#ifdef CONFIG_PM
185static int exynos_cpufreq_suspend(struct cpufreq_policy *policy)
186{
187 return 0;
188}
189
190static int exynos_cpufreq_resume(struct cpufreq_policy *policy)
191{
192 return 0;
193}
194#endif
195
196/**
197 * exynos_cpufreq_pm_notifier - block CPUFREQ's activities in suspend-resume
198 * context
199 * @notifier
200 * @pm_event
201 * @v
202 *
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
209 * regulator actions.
210 */
211static int exynos_cpufreq_pm_notifier(struct notifier_block *notifier,
212 unsigned long pm_event, void *v)
213{
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800214 int ret;
Jaecheol Leea125a172012-01-07 20:18:35 +0900215
Jaecheol Leea125a172012-01-07 20:18:35 +0900216 switch (pm_event) {
217 case PM_SUSPEND_PREPARE:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800218 mutex_lock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900219 frequency_locked = true;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800220 mutex_unlock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900221
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800222 ret = exynos_cpufreq_scale(locking_frequency);
223 if (ret < 0)
224 return NOTIFY_BAD;
Jaecheol Leea125a172012-01-07 20:18:35 +0900225
Jaecheol Leea125a172012-01-07 20:18:35 +0900226 break;
227
228 case PM_POST_SUSPEND:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800229 mutex_lock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900230 frequency_locked = false;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800231 mutex_unlock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900232 break;
233 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900234
235 return NOTIFY_OK;
236}
237
238static struct notifier_block exynos_cpufreq_nb = {
239 .notifier_call = exynos_cpufreq_pm_notifier,
240};
241
242static int exynos_cpufreq_cpu_init(struct cpufreq_policy *policy)
243{
Viresh Kumarb249aba2013-10-03 20:29:13 +0530244 return cpufreq_generic_init(policy, exynos_info->freq_table, 100000);
Jaecheol Leea125a172012-01-07 20:18:35 +0900245}
246
247static struct cpufreq_driver exynos_driver = {
248 .flags = CPUFREQ_STICKY,
Viresh Kumareea61812013-10-03 20:28:06 +0530249 .verify = cpufreq_generic_frequency_table_verify,
Jaecheol Leea125a172012-01-07 20:18:35 +0900250 .target = exynos_target,
251 .get = exynos_getspeed,
252 .init = exynos_cpufreq_cpu_init,
Viresh Kumareea61812013-10-03 20:28:06 +0530253 .exit = cpufreq_generic_exit,
Jaecheol Leea125a172012-01-07 20:18:35 +0900254 .name = "exynos_cpufreq",
Viresh Kumareea61812013-10-03 20:28:06 +0530255 .attr = cpufreq_generic_attr,
Jaecheol Leea125a172012-01-07 20:18:35 +0900256#ifdef CONFIG_PM
257 .suspend = exynos_cpufreq_suspend,
258 .resume = exynos_cpufreq_resume,
259#endif
260};
261
262static int __init exynos_cpufreq_init(void)
263{
264 int ret = -EINVAL;
265
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530266 exynos_info = kzalloc(sizeof(*exynos_info), GFP_KERNEL);
Jaecheol Leea125a172012-01-07 20:18:35 +0900267 if (!exynos_info)
268 return -ENOMEM;
269
270 if (soc_is_exynos4210())
271 ret = exynos4210_cpufreq_init(exynos_info);
Jaecheol Leea35c5052012-03-10 02:59:22 -0800272 else if (soc_is_exynos4212() || soc_is_exynos4412())
273 ret = exynos4x12_cpufreq_init(exynos_info);
Jaecheol Lee562a6cb2012-03-10 03:00:02 -0800274 else if (soc_is_exynos5250())
275 ret = exynos5250_cpufreq_init(exynos_info);
Jaecheol Leea125a172012-01-07 20:18:35 +0900276 else
Amit Daniel Kachhapc1585202013-04-08 08:17:36 +0000277 return 0;
Jaecheol Leea125a172012-01-07 20:18:35 +0900278
279 if (ret)
280 goto err_vdd_arm;
281
282 if (exynos_info->set_freq == NULL) {
283 pr_err("%s: No set_freq function (ERR)\n", __func__);
284 goto err_vdd_arm;
285 }
286
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__);
290 goto err_vdd_arm;
291 }
292
Jonghwan Choi6e45eb12013-01-18 11:09:01 -0800293 locking_frequency = exynos_getspeed(0);
294
Jaecheol Leea125a172012-01-07 20:18:35 +0900295 register_pm_notifier(&exynos_cpufreq_nb);
296
297 if (cpufreq_register_driver(&exynos_driver)) {
298 pr_err("%s: failed to register cpufreq driver\n", __func__);
299 goto err_cpufreq;
300 }
301
302 return 0;
303err_cpufreq:
304 unregister_pm_notifier(&exynos_cpufreq_nb);
305
Jonghwan Choi184cddd2012-12-23 15:51:40 -0800306 regulator_put(arm_regulator);
Jaecheol Leea125a172012-01-07 20:18:35 +0900307err_vdd_arm:
308 kfree(exynos_info);
Jaecheol Leea125a172012-01-07 20:18:35 +0900309 return -EINVAL;
310}
311late_initcall(exynos_cpufreq_init);