blob: f3c22874da753c07dd5f12d58f4b85a6438a2de3 [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;
Jaecheol Leea125a172012-01-07 20:18:35 +090028
29static unsigned int locking_frequency;
30static bool frequency_locked;
31static DEFINE_MUTEX(cpufreq_lock);
32
Tushar Behera55427212012-11-22 00:19:25 +010033static unsigned int exynos_getspeed(unsigned int cpu)
Jaecheol Leea125a172012-01-07 20:18:35 +090034{
35 return clk_get_rate(exynos_info->cpu_clk) / 1000;
36}
37
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080038static int exynos_cpufreq_get_index(unsigned int freq)
Jaecheol Leea125a172012-01-07 20:18:35 +090039{
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080040 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
41 int index;
42
43 for (index = 0;
44 freq_table[index].frequency != CPUFREQ_TABLE_END; index++)
45 if (freq_table[index].frequency == freq)
46 break;
47
48 if (freq_table[index].frequency == CPUFREQ_TABLE_END)
49 return -EINVAL;
50
51 return index;
52}
53
54static int exynos_cpufreq_scale(unsigned int target_freq)
55{
Jaecheol Leea125a172012-01-07 20:18:35 +090056 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
57 unsigned int *volt_table = exynos_info->volt_table;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080058 struct cpufreq_policy *policy = cpufreq_cpu_get(0);
59 unsigned int arm_volt, safe_arm_volt = 0;
Jaecheol Leea125a172012-01-07 20:18:35 +090060 unsigned int mpll_freq_khz = exynos_info->mpll_freq_khz;
Viresh Kumard4019f02013-08-14 19:38:24 +053061 unsigned int old_freq;
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
Viresh Kumard4019f02013-08-14 19:38:24 +053065 old_freq = policy->cur;
Jaecheol Leea125a172012-01-07 20:18:35 +090066
Jonghwa Lee53df1ad2012-07-20 02:54:02 +000067 /*
68 * The policy max have been changed so that we cannot get proper
69 * old_index with cpufreq_frequency_table_target(). Thus, ignore
LABBE Corentin05851232013-09-26 16:50:21 +020070 * policy and get the index from the raw frequency table.
Jonghwa Lee53df1ad2012-07-20 02:54:02 +000071 */
Viresh Kumard4019f02013-08-14 19:38:24 +053072 old_index = exynos_cpufreq_get_index(old_freq);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080073 if (old_index < 0) {
74 ret = old_index;
Jaecheol Leea125a172012-01-07 20:18:35 +090075 goto out;
76 }
77
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080078 index = exynos_cpufreq_get_index(target_freq);
79 if (index < 0) {
80 ret = index;
Jaecheol Leea125a172012-01-07 20:18:35 +090081 goto out;
82 }
83
Jaecheol Leea125a172012-01-07 20:18:35 +090084 /*
85 * ARM clock source will be changed APLL to MPLL temporary
86 * To support this level, need to control regulator for
87 * required voltage level
88 */
89 if (exynos_info->need_apll_change != NULL) {
90 if (exynos_info->need_apll_change(old_index, index) &&
91 (freq_table[index].frequency < mpll_freq_khz) &&
92 (freq_table[old_index].frequency < mpll_freq_khz))
93 safe_arm_volt = volt_table[exynos_info->pll_safe_idx];
94 }
95 arm_volt = volt_table[index];
96
Jaecheol Leea125a172012-01-07 20:18:35 +090097 /* When the new frequency is higher than current frequency */
Viresh Kumard4019f02013-08-14 19:38:24 +053098 if ((target_freq > old_freq) && !safe_arm_volt) {
Jaecheol Leea125a172012-01-07 20:18:35 +090099 /* Firstly, voltage up to increase frequency */
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800100 ret = regulator_set_voltage(arm_regulator, arm_volt, arm_volt);
101 if (ret) {
102 pr_err("%s: failed to set cpu voltage to %d\n",
103 __func__, arm_volt);
Viresh Kumard4019f02013-08-14 19:38:24 +0530104 return ret;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800105 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900106 }
107
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800108 if (safe_arm_volt) {
109 ret = regulator_set_voltage(arm_regulator, safe_arm_volt,
Jaecheol Leea125a172012-01-07 20:18:35 +0900110 safe_arm_volt);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800111 if (ret) {
112 pr_err("%s: failed to set cpu voltage to %d\n",
113 __func__, safe_arm_volt);
Viresh Kumard4019f02013-08-14 19:38:24 +0530114 return ret;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800115 }
116 }
Jonghwan Choi857d90f2012-12-23 15:57:39 -0800117
118 exynos_info->set_freq(old_index, index);
Jaecheol Leea125a172012-01-07 20:18:35 +0900119
Jaecheol Leea125a172012-01-07 20:18:35 +0900120 /* When the new frequency is lower than current frequency */
Viresh Kumard4019f02013-08-14 19:38:24 +0530121 if ((target_freq < old_freq) ||
122 ((target_freq > old_freq) && safe_arm_volt)) {
Jaecheol Leea125a172012-01-07 20:18:35 +0900123 /* down the voltage after frequency change */
Manish Badarkhe006454a2013-10-09 20:43:37 +0530124 ret = regulator_set_voltage(arm_regulator, arm_volt,
Jaecheol Leea125a172012-01-07 20:18:35 +0900125 arm_volt);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800126 if (ret) {
127 pr_err("%s: failed to set cpu voltage to %d\n",
128 __func__, arm_volt);
129 goto out;
130 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900131 }
132
133out:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800134 cpufreq_cpu_put(policy);
135
136 return ret;
137}
138
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530139static int exynos_target(struct cpufreq_policy *policy, unsigned int index)
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800140{
141 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
Sachin Kamat229b21e2013-01-31 17:13:39 -0800142 int ret = 0;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800143
144 mutex_lock(&cpufreq_lock);
145
146 if (frequency_locked)
147 goto out;
148
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530149 ret = exynos_cpufreq_scale(freq_table[index].frequency);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800150
151out:
Jaecheol Leea125a172012-01-07 20:18:35 +0900152 mutex_unlock(&cpufreq_lock);
153
154 return ret;
155}
156
157#ifdef CONFIG_PM
158static int exynos_cpufreq_suspend(struct cpufreq_policy *policy)
159{
160 return 0;
161}
162
163static int exynos_cpufreq_resume(struct cpufreq_policy *policy)
164{
165 return 0;
166}
167#endif
168
169/**
170 * exynos_cpufreq_pm_notifier - block CPUFREQ's activities in suspend-resume
171 * context
172 * @notifier
173 * @pm_event
174 * @v
175 *
176 * While frequency_locked == true, target() ignores every frequency but
177 * locking_frequency. The locking_frequency value is the initial frequency,
178 * which is set by the bootloader. In order to eliminate possible
179 * inconsistency in clock values, we save and restore frequencies during
180 * suspend and resume and block CPUFREQ activities. Note that the standard
181 * suspend/resume cannot be used as they are too deep (syscore_ops) for
182 * regulator actions.
183 */
184static int exynos_cpufreq_pm_notifier(struct notifier_block *notifier,
185 unsigned long pm_event, void *v)
186{
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800187 int ret;
Jaecheol Leea125a172012-01-07 20:18:35 +0900188
Jaecheol Leea125a172012-01-07 20:18:35 +0900189 switch (pm_event) {
190 case PM_SUSPEND_PREPARE:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800191 mutex_lock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900192 frequency_locked = true;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800193 mutex_unlock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900194
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800195 ret = exynos_cpufreq_scale(locking_frequency);
196 if (ret < 0)
197 return NOTIFY_BAD;
Jaecheol Leea125a172012-01-07 20:18:35 +0900198
Jaecheol Leea125a172012-01-07 20:18:35 +0900199 break;
200
201 case PM_POST_SUSPEND:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800202 mutex_lock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900203 frequency_locked = false;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800204 mutex_unlock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900205 break;
206 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900207
208 return NOTIFY_OK;
209}
210
211static struct notifier_block exynos_cpufreq_nb = {
212 .notifier_call = exynos_cpufreq_pm_notifier,
213};
214
215static int exynos_cpufreq_cpu_init(struct cpufreq_policy *policy)
216{
Viresh Kumarb249aba2013-10-03 20:29:13 +0530217 return cpufreq_generic_init(policy, exynos_info->freq_table, 100000);
Jaecheol Leea125a172012-01-07 20:18:35 +0900218}
219
220static struct cpufreq_driver exynos_driver = {
221 .flags = CPUFREQ_STICKY,
Viresh Kumareea61812013-10-03 20:28:06 +0530222 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530223 .target_index = exynos_target,
Jaecheol Leea125a172012-01-07 20:18:35 +0900224 .get = exynos_getspeed,
225 .init = exynos_cpufreq_cpu_init,
Viresh Kumareea61812013-10-03 20:28:06 +0530226 .exit = cpufreq_generic_exit,
Jaecheol Leea125a172012-01-07 20:18:35 +0900227 .name = "exynos_cpufreq",
Viresh Kumareea61812013-10-03 20:28:06 +0530228 .attr = cpufreq_generic_attr,
Jaecheol Leea125a172012-01-07 20:18:35 +0900229#ifdef CONFIG_PM
230 .suspend = exynos_cpufreq_suspend,
231 .resume = exynos_cpufreq_resume,
232#endif
233};
234
235static int __init exynos_cpufreq_init(void)
236{
237 int ret = -EINVAL;
238
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530239 exynos_info = kzalloc(sizeof(*exynos_info), GFP_KERNEL);
Jaecheol Leea125a172012-01-07 20:18:35 +0900240 if (!exynos_info)
241 return -ENOMEM;
242
243 if (soc_is_exynos4210())
244 ret = exynos4210_cpufreq_init(exynos_info);
Jaecheol Leea35c5052012-03-10 02:59:22 -0800245 else if (soc_is_exynos4212() || soc_is_exynos4412())
246 ret = exynos4x12_cpufreq_init(exynos_info);
Jaecheol Lee562a6cb2012-03-10 03:00:02 -0800247 else if (soc_is_exynos5250())
248 ret = exynos5250_cpufreq_init(exynos_info);
Jaecheol Leea125a172012-01-07 20:18:35 +0900249 else
Amit Daniel Kachhapc1585202013-04-08 08:17:36 +0000250 return 0;
Jaecheol Leea125a172012-01-07 20:18:35 +0900251
252 if (ret)
253 goto err_vdd_arm;
254
255 if (exynos_info->set_freq == NULL) {
256 pr_err("%s: No set_freq function (ERR)\n", __func__);
257 goto err_vdd_arm;
258 }
259
260 arm_regulator = regulator_get(NULL, "vdd_arm");
261 if (IS_ERR(arm_regulator)) {
262 pr_err("%s: failed to get resource vdd_arm\n", __func__);
263 goto err_vdd_arm;
264 }
265
Jonghwan Choi6e45eb12013-01-18 11:09:01 -0800266 locking_frequency = exynos_getspeed(0);
267
Jaecheol Leea125a172012-01-07 20:18:35 +0900268 register_pm_notifier(&exynos_cpufreq_nb);
269
270 if (cpufreq_register_driver(&exynos_driver)) {
271 pr_err("%s: failed to register cpufreq driver\n", __func__);
272 goto err_cpufreq;
273 }
274
275 return 0;
276err_cpufreq:
277 unregister_pm_notifier(&exynos_cpufreq_nb);
278
Jonghwan Choi184cddd2012-12-23 15:51:40 -0800279 regulator_put(arm_regulator);
Jaecheol Leea125a172012-01-07 20:18:35 +0900280err_vdd_arm:
281 kfree(exynos_info);
Jaecheol Leea125a172012-01-07 20:18:35 +0900282 return -EINVAL;
283}
284late_initcall(exynos_cpufreq_init);