blob: 366475161c76aa86e320b881fd9d775d638b0065 [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 int exynos_verify_speed(struct cpufreq_policy *policy)
Jaecheol Leea125a172012-01-07 20:18:35 +090035{
36 return cpufreq_frequency_table_verify(policy,
37 exynos_info->freq_table);
38}
39
Tushar Behera55427212012-11-22 00:19:25 +010040static unsigned int exynos_getspeed(unsigned int cpu)
Jaecheol Leea125a172012-01-07 20:18:35 +090041{
42 return clk_get_rate(exynos_info->cpu_clk) / 1000;
43}
44
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080045static int exynos_cpufreq_get_index(unsigned int freq)
Jaecheol Leea125a172012-01-07 20:18:35 +090046{
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080047 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
48 int index;
49
50 for (index = 0;
51 freq_table[index].frequency != CPUFREQ_TABLE_END; index++)
52 if (freq_table[index].frequency == freq)
53 break;
54
55 if (freq_table[index].frequency == CPUFREQ_TABLE_END)
56 return -EINVAL;
57
58 return index;
59}
60
61static int exynos_cpufreq_scale(unsigned int target_freq)
62{
Jaecheol Leea125a172012-01-07 20:18:35 +090063 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
64 unsigned int *volt_table = exynos_info->volt_table;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080065 struct cpufreq_policy *policy = cpufreq_cpu_get(0);
66 unsigned int arm_volt, safe_arm_volt = 0;
Jaecheol Leea125a172012-01-07 20:18:35 +090067 unsigned int mpll_freq_khz = exynos_info->mpll_freq_khz;
Sachin Kamatd271d072013-01-25 10:18:09 -080068 int index, old_index;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080069 int ret = 0;
Jaecheol Leea125a172012-01-07 20:18:35 +090070
71 freqs.old = policy->cur;
Jonghwan Choic098ea72013-02-04 21:01:22 -080072 freqs.new = target_freq;
Jaecheol Leea125a172012-01-07 20:18:35 +090073
Jonghwan Choic098ea72013-02-04 21:01:22 -080074 if (freqs.new == freqs.old)
Jaecheol Leea125a172012-01-07 20:18:35 +090075 goto out;
Jaecheol Leea125a172012-01-07 20:18:35 +090076
Jonghwa Lee53df1ad2012-07-20 02:54:02 +000077 /*
78 * The policy max have been changed so that we cannot get proper
79 * old_index with cpufreq_frequency_table_target(). Thus, ignore
80 * policy and get the index from the raw freqeuncy table.
81 */
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080082 old_index = exynos_cpufreq_get_index(freqs.old);
83 if (old_index < 0) {
84 ret = old_index;
Jaecheol Leea125a172012-01-07 20:18:35 +090085 goto out;
86 }
87
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080088 index = exynos_cpufreq_get_index(target_freq);
89 if (index < 0) {
90 ret = index;
Jaecheol Leea125a172012-01-07 20:18:35 +090091 goto out;
92 }
93
Jaecheol Leea125a172012-01-07 20:18:35 +090094 /*
95 * ARM clock source will be changed APLL to MPLL temporary
96 * To support this level, need to control regulator for
97 * required voltage level
98 */
99 if (exynos_info->need_apll_change != NULL) {
100 if (exynos_info->need_apll_change(old_index, index) &&
101 (freq_table[index].frequency < mpll_freq_khz) &&
102 (freq_table[old_index].frequency < mpll_freq_khz))
103 safe_arm_volt = volt_table[exynos_info->pll_safe_idx];
104 }
105 arm_volt = volt_table[index];
106
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530107 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Jaecheol Leea125a172012-01-07 20:18:35 +0900108
109 /* When the new frequency is higher than current frequency */
110 if ((freqs.new > freqs.old) && !safe_arm_volt) {
111 /* Firstly, voltage up to increase frequency */
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800112 ret = regulator_set_voltage(arm_regulator, arm_volt, arm_volt);
113 if (ret) {
114 pr_err("%s: failed to set cpu voltage to %d\n",
115 __func__, arm_volt);
Viresh Kumarc3aca6b2013-06-19 11:18:20 +0530116 freqs.new = freqs.old;
117 goto post_notify;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800118 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900119 }
120
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800121 if (safe_arm_volt) {
122 ret = regulator_set_voltage(arm_regulator, safe_arm_volt,
Jaecheol Leea125a172012-01-07 20:18:35 +0900123 safe_arm_volt);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800124 if (ret) {
125 pr_err("%s: failed to set cpu voltage to %d\n",
126 __func__, safe_arm_volt);
Viresh Kumarc3aca6b2013-06-19 11:18:20 +0530127 freqs.new = freqs.old;
128 goto post_notify;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800129 }
130 }
Jonghwan Choi857d90f2012-12-23 15:57:39 -0800131
132 exynos_info->set_freq(old_index, index);
Jaecheol Leea125a172012-01-07 20:18:35 +0900133
Viresh Kumarc3aca6b2013-06-19 11:18:20 +0530134post_notify:
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530135 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Jaecheol Leea125a172012-01-07 20:18:35 +0900136
Viresh Kumarc3aca6b2013-06-19 11:18:20 +0530137 if (ret)
138 goto out;
139
Jaecheol Leea125a172012-01-07 20:18:35 +0900140 /* When the new frequency is lower than current frequency */
141 if ((freqs.new < freqs.old) ||
142 ((freqs.new > freqs.old) && safe_arm_volt)) {
143 /* down the voltage after frequency change */
144 regulator_set_voltage(arm_regulator, arm_volt,
145 arm_volt);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800146 if (ret) {
147 pr_err("%s: failed to set cpu voltage to %d\n",
148 __func__, arm_volt);
149 goto out;
150 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900151 }
152
153out:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800154
155 cpufreq_cpu_put(policy);
156
157 return ret;
158}
159
160static int exynos_target(struct cpufreq_policy *policy,
161 unsigned int target_freq,
162 unsigned int relation)
163{
164 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
165 unsigned int index;
Jonghwan Choic098ea72013-02-04 21:01:22 -0800166 unsigned int new_freq;
Sachin Kamat229b21e2013-01-31 17:13:39 -0800167 int ret = 0;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800168
169 mutex_lock(&cpufreq_lock);
170
171 if (frequency_locked)
172 goto out;
173
174 if (cpufreq_frequency_table_target(policy, freq_table,
175 target_freq, relation, &index)) {
176 ret = -EINVAL;
177 goto out;
178 }
179
Jonghwan Choic098ea72013-02-04 21:01:22 -0800180 new_freq = freq_table[index].frequency;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800181
Jonghwan Choic098ea72013-02-04 21:01:22 -0800182 ret = exynos_cpufreq_scale(new_freq);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800183
184out:
Jaecheol Leea125a172012-01-07 20:18:35 +0900185 mutex_unlock(&cpufreq_lock);
186
187 return ret;
188}
189
190#ifdef CONFIG_PM
191static int exynos_cpufreq_suspend(struct cpufreq_policy *policy)
192{
193 return 0;
194}
195
196static int exynos_cpufreq_resume(struct cpufreq_policy *policy)
197{
198 return 0;
199}
200#endif
201
202/**
203 * exynos_cpufreq_pm_notifier - block CPUFREQ's activities in suspend-resume
204 * context
205 * @notifier
206 * @pm_event
207 * @v
208 *
209 * While frequency_locked == true, target() ignores every frequency but
210 * locking_frequency. The locking_frequency value is the initial frequency,
211 * which is set by the bootloader. In order to eliminate possible
212 * inconsistency in clock values, we save and restore frequencies during
213 * suspend and resume and block CPUFREQ activities. Note that the standard
214 * suspend/resume cannot be used as they are too deep (syscore_ops) for
215 * regulator actions.
216 */
217static int exynos_cpufreq_pm_notifier(struct notifier_block *notifier,
218 unsigned long pm_event, void *v)
219{
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800220 int ret;
Jaecheol Leea125a172012-01-07 20:18:35 +0900221
Jaecheol Leea125a172012-01-07 20:18:35 +0900222 switch (pm_event) {
223 case PM_SUSPEND_PREPARE:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800224 mutex_lock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900225 frequency_locked = true;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800226 mutex_unlock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900227
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800228 ret = exynos_cpufreq_scale(locking_frequency);
229 if (ret < 0)
230 return NOTIFY_BAD;
Jaecheol Leea125a172012-01-07 20:18:35 +0900231
Jaecheol Leea125a172012-01-07 20:18:35 +0900232 break;
233
234 case PM_POST_SUSPEND:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800235 mutex_lock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900236 frequency_locked = false;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800237 mutex_unlock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900238 break;
239 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900240
241 return NOTIFY_OK;
242}
243
244static struct notifier_block exynos_cpufreq_nb = {
245 .notifier_call = exynos_cpufreq_pm_notifier,
246};
247
248static int exynos_cpufreq_cpu_init(struct cpufreq_policy *policy)
249{
250 policy->cur = policy->min = policy->max = exynos_getspeed(policy->cpu);
251
252 cpufreq_frequency_table_get_attr(exynos_info->freq_table, policy->cpu);
253
Jaecheol Leea125a172012-01-07 20:18:35 +0900254 /* set the transition latency value */
255 policy->cpuinfo.transition_latency = 100000;
256
Viresh Kumar6ca939b2013-01-31 07:56:04 +0530257 cpumask_setall(policy->cpus);
Jaecheol Leea125a172012-01-07 20:18:35 +0900258
259 return cpufreq_frequency_table_cpuinfo(policy, exynos_info->freq_table);
260}
261
Inderpal Singh12982712013-01-18 11:07:33 -0800262static int exynos_cpufreq_cpu_exit(struct cpufreq_policy *policy)
263{
264 cpufreq_frequency_table_put_attr(policy->cpu);
265 return 0;
266}
267
268static struct freq_attr *exynos_cpufreq_attr[] = {
269 &cpufreq_freq_attr_scaling_available_freqs,
270 NULL,
271};
272
Jaecheol Leea125a172012-01-07 20:18:35 +0900273static struct cpufreq_driver exynos_driver = {
274 .flags = CPUFREQ_STICKY,
275 .verify = exynos_verify_speed,
276 .target = exynos_target,
277 .get = exynos_getspeed,
278 .init = exynos_cpufreq_cpu_init,
Inderpal Singh12982712013-01-18 11:07:33 -0800279 .exit = exynos_cpufreq_cpu_exit,
Jaecheol Leea125a172012-01-07 20:18:35 +0900280 .name = "exynos_cpufreq",
Inderpal Singh12982712013-01-18 11:07:33 -0800281 .attr = exynos_cpufreq_attr,
Jaecheol Leea125a172012-01-07 20:18:35 +0900282#ifdef CONFIG_PM
283 .suspend = exynos_cpufreq_suspend,
284 .resume = exynos_cpufreq_resume,
285#endif
286};
287
288static int __init exynos_cpufreq_init(void)
289{
290 int ret = -EINVAL;
291
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530292 exynos_info = kzalloc(sizeof(*exynos_info), GFP_KERNEL);
Jaecheol Leea125a172012-01-07 20:18:35 +0900293 if (!exynos_info)
294 return -ENOMEM;
295
296 if (soc_is_exynos4210())
297 ret = exynos4210_cpufreq_init(exynos_info);
Jaecheol Leea35c5052012-03-10 02:59:22 -0800298 else if (soc_is_exynos4212() || soc_is_exynos4412())
299 ret = exynos4x12_cpufreq_init(exynos_info);
Jaecheol Lee562a6cb2012-03-10 03:00:02 -0800300 else if (soc_is_exynos5250())
301 ret = exynos5250_cpufreq_init(exynos_info);
Jaecheol Leea125a172012-01-07 20:18:35 +0900302 else
Amit Daniel Kachhapc1585202013-04-08 08:17:36 +0000303 return 0;
Jaecheol Leea125a172012-01-07 20:18:35 +0900304
305 if (ret)
306 goto err_vdd_arm;
307
308 if (exynos_info->set_freq == NULL) {
309 pr_err("%s: No set_freq function (ERR)\n", __func__);
310 goto err_vdd_arm;
311 }
312
313 arm_regulator = regulator_get(NULL, "vdd_arm");
314 if (IS_ERR(arm_regulator)) {
315 pr_err("%s: failed to get resource vdd_arm\n", __func__);
316 goto err_vdd_arm;
317 }
318
Jonghwan Choi6e45eb12013-01-18 11:09:01 -0800319 locking_frequency = exynos_getspeed(0);
320
Jaecheol Leea125a172012-01-07 20:18:35 +0900321 register_pm_notifier(&exynos_cpufreq_nb);
322
323 if (cpufreq_register_driver(&exynos_driver)) {
324 pr_err("%s: failed to register cpufreq driver\n", __func__);
325 goto err_cpufreq;
326 }
327
328 return 0;
329err_cpufreq:
330 unregister_pm_notifier(&exynos_cpufreq_nb);
331
Jonghwan Choi184cddd2012-12-23 15:51:40 -0800332 regulator_put(arm_regulator);
Jaecheol Leea125a172012-01-07 20:18:35 +0900333err_vdd_arm:
334 kfree(exynos_info);
335 pr_debug("%s: failed initialization\n", __func__);
336 return -EINVAL;
337}
338late_initcall(exynos_cpufreq_init);