blob: c8c7653a6033f3ffd30949e7367c77bd1213633b [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 Leea125a172012-01-07 20:18:35 +090021#include <mach/cpufreq.h>
22
Jaecheol Lee6c523c62012-01-07 20:18:39 +090023#include <plat/cpu.h>
Jaecheol Leea125a172012-01-07 20:18:35 +090024
25static 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 Choi0e0e4252012-12-23 15:57:48 -080072 freqs.cpu = policy->cpu;
Jaecheol Leea125a172012-01-07 20:18:35 +090073
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080074 if (target_freq == 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
Tomasz Figafd06a202012-11-22 00:09:27 +0100107 for_each_cpu(freqs.cpu, policy->cpus)
108 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
Jaecheol Leea125a172012-01-07 20:18:35 +0900109
110 /* When the new frequency is higher than current frequency */
111 if ((freqs.new > freqs.old) && !safe_arm_volt) {
112 /* Firstly, voltage up to increase frequency */
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800113 ret = regulator_set_voltage(arm_regulator, arm_volt, arm_volt);
114 if (ret) {
115 pr_err("%s: failed to set cpu voltage to %d\n",
116 __func__, arm_volt);
117 goto out;
118 }
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);
127 goto out;
128 }
129 }
Jonghwan Choi857d90f2012-12-23 15:57:39 -0800130
131 exynos_info->set_freq(old_index, index);
Jaecheol Leea125a172012-01-07 20:18:35 +0900132
Tomasz Figafd06a202012-11-22 00:09:27 +0100133 for_each_cpu(freqs.cpu, policy->cpus)
134 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
Jaecheol Leea125a172012-01-07 20:18:35 +0900135
136 /* When the new frequency is lower than current frequency */
137 if ((freqs.new < freqs.old) ||
138 ((freqs.new > freqs.old) && safe_arm_volt)) {
139 /* down the voltage after frequency change */
140 regulator_set_voltage(arm_regulator, arm_volt,
141 arm_volt);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800142 if (ret) {
143 pr_err("%s: failed to set cpu voltage to %d\n",
144 __func__, arm_volt);
145 goto out;
146 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900147 }
148
149out:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800150
151 cpufreq_cpu_put(policy);
152
153 return ret;
154}
155
156static int exynos_target(struct cpufreq_policy *policy,
157 unsigned int target_freq,
158 unsigned int relation)
159{
160 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
161 unsigned int index;
162 int ret;
163
164 mutex_lock(&cpufreq_lock);
165
166 if (frequency_locked)
167 goto out;
168
169 if (cpufreq_frequency_table_target(policy, freq_table,
170 target_freq, relation, &index)) {
171 ret = -EINVAL;
172 goto out;
173 }
174
175 freqs.new = freq_table[index].frequency;
176
177 ret = exynos_cpufreq_scale(freqs.new);
178
179out:
Jaecheol Leea125a172012-01-07 20:18:35 +0900180 mutex_unlock(&cpufreq_lock);
181
182 return ret;
183}
184
185#ifdef CONFIG_PM
186static int exynos_cpufreq_suspend(struct cpufreq_policy *policy)
187{
188 return 0;
189}
190
191static int exynos_cpufreq_resume(struct cpufreq_policy *policy)
192{
193 return 0;
194}
195#endif
196
197/**
198 * exynos_cpufreq_pm_notifier - block CPUFREQ's activities in suspend-resume
199 * context
200 * @notifier
201 * @pm_event
202 * @v
203 *
204 * While frequency_locked == true, target() ignores every frequency but
205 * locking_frequency. The locking_frequency value is the initial frequency,
206 * which is set by the bootloader. In order to eliminate possible
207 * inconsistency in clock values, we save and restore frequencies during
208 * suspend and resume and block CPUFREQ activities. Note that the standard
209 * suspend/resume cannot be used as they are too deep (syscore_ops) for
210 * regulator actions.
211 */
212static int exynos_cpufreq_pm_notifier(struct notifier_block *notifier,
213 unsigned long pm_event, void *v)
214{
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800215 int ret;
Jaecheol Leea125a172012-01-07 20:18:35 +0900216
Jaecheol Leea125a172012-01-07 20:18:35 +0900217 switch (pm_event) {
218 case PM_SUSPEND_PREPARE:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800219 mutex_lock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900220 frequency_locked = true;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800221 mutex_unlock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900222
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800223 ret = exynos_cpufreq_scale(locking_frequency);
224 if (ret < 0)
225 return NOTIFY_BAD;
Jaecheol Leea125a172012-01-07 20:18:35 +0900226
Jaecheol Leea125a172012-01-07 20:18:35 +0900227 break;
228
229 case PM_POST_SUSPEND:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800230 mutex_lock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900231 frequency_locked = false;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800232 mutex_unlock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900233 break;
234 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900235
236 return NOTIFY_OK;
237}
238
239static struct notifier_block exynos_cpufreq_nb = {
240 .notifier_call = exynos_cpufreq_pm_notifier,
241};
242
243static int exynos_cpufreq_cpu_init(struct cpufreq_policy *policy)
244{
245 policy->cur = policy->min = policy->max = exynos_getspeed(policy->cpu);
246
247 cpufreq_frequency_table_get_attr(exynos_info->freq_table, policy->cpu);
248
Jaecheol Leea125a172012-01-07 20:18:35 +0900249 /* set the transition latency value */
250 policy->cpuinfo.transition_latency = 100000;
251
252 /*
253 * EXYNOS4 multi-core processors has 2 cores
254 * that the frequency cannot be set independently.
255 * Each cpu is bound to the same speed.
256 * So the affected cpu is all of the cpus.
257 */
258 if (num_online_cpus() == 1) {
259 cpumask_copy(policy->related_cpus, cpu_possible_mask);
260 cpumask_copy(policy->cpus, cpu_online_mask);
261 } else {
Tomasz Figafd06a202012-11-22 00:09:27 +0100262 policy->shared_type = CPUFREQ_SHARED_TYPE_ANY;
Jaecheol Leea125a172012-01-07 20:18:35 +0900263 cpumask_setall(policy->cpus);
264 }
265
266 return cpufreq_frequency_table_cpuinfo(policy, exynos_info->freq_table);
267}
268
Inderpal Singh12982712013-01-18 11:07:33 -0800269static int exynos_cpufreq_cpu_exit(struct cpufreq_policy *policy)
270{
271 cpufreq_frequency_table_put_attr(policy->cpu);
272 return 0;
273}
274
275static struct freq_attr *exynos_cpufreq_attr[] = {
276 &cpufreq_freq_attr_scaling_available_freqs,
277 NULL,
278};
279
Jaecheol Leea125a172012-01-07 20:18:35 +0900280static struct cpufreq_driver exynos_driver = {
281 .flags = CPUFREQ_STICKY,
282 .verify = exynos_verify_speed,
283 .target = exynos_target,
284 .get = exynos_getspeed,
285 .init = exynos_cpufreq_cpu_init,
Inderpal Singh12982712013-01-18 11:07:33 -0800286 .exit = exynos_cpufreq_cpu_exit,
Jaecheol Leea125a172012-01-07 20:18:35 +0900287 .name = "exynos_cpufreq",
Inderpal Singh12982712013-01-18 11:07:33 -0800288 .attr = exynos_cpufreq_attr,
Jaecheol Leea125a172012-01-07 20:18:35 +0900289#ifdef CONFIG_PM
290 .suspend = exynos_cpufreq_suspend,
291 .resume = exynos_cpufreq_resume,
292#endif
293};
294
295static int __init exynos_cpufreq_init(void)
296{
297 int ret = -EINVAL;
298
299 exynos_info = kzalloc(sizeof(struct exynos_dvfs_info), GFP_KERNEL);
300 if (!exynos_info)
301 return -ENOMEM;
302
303 if (soc_is_exynos4210())
304 ret = exynos4210_cpufreq_init(exynos_info);
Jaecheol Leea35c5052012-03-10 02:59:22 -0800305 else if (soc_is_exynos4212() || soc_is_exynos4412())
306 ret = exynos4x12_cpufreq_init(exynos_info);
Jaecheol Lee562a6cb2012-03-10 03:00:02 -0800307 else if (soc_is_exynos5250())
308 ret = exynos5250_cpufreq_init(exynos_info);
Jaecheol Leea125a172012-01-07 20:18:35 +0900309 else
310 pr_err("%s: CPU type not found\n", __func__);
311
312 if (ret)
313 goto err_vdd_arm;
314
315 if (exynos_info->set_freq == NULL) {
316 pr_err("%s: No set_freq function (ERR)\n", __func__);
317 goto err_vdd_arm;
318 }
319
320 arm_regulator = regulator_get(NULL, "vdd_arm");
321 if (IS_ERR(arm_regulator)) {
322 pr_err("%s: failed to get resource vdd_arm\n", __func__);
323 goto err_vdd_arm;
324 }
325
Jonghwan Choi6e45eb12013-01-18 11:09:01 -0800326 locking_frequency = exynos_getspeed(0);
327
Jaecheol Leea125a172012-01-07 20:18:35 +0900328 register_pm_notifier(&exynos_cpufreq_nb);
329
330 if (cpufreq_register_driver(&exynos_driver)) {
331 pr_err("%s: failed to register cpufreq driver\n", __func__);
332 goto err_cpufreq;
333 }
334
335 return 0;
336err_cpufreq:
337 unregister_pm_notifier(&exynos_cpufreq_nb);
338
Jonghwan Choi184cddd2012-12-23 15:51:40 -0800339 regulator_put(arm_regulator);
Jaecheol Leea125a172012-01-07 20:18:35 +0900340err_vdd_arm:
341 kfree(exynos_info);
342 pr_debug("%s: failed initialization\n", __func__);
343 return -EINVAL;
344}
345late_initcall(exynos_cpufreq_init);