blob: f7c322c7d7ed391afded77681e8f74316e6d0ffa [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>
Lukasz Majewskid568b6f2013-11-28 13:42:42 +010020#include <linux/platform_device.h>
Jaecheol Leea125a172012-01-07 20:18:35 +090021
Jaecheol Lee6c523c62012-01-07 20:18:39 +090022#include <plat/cpu.h>
Jaecheol Leea125a172012-01-07 20:18:35 +090023
Kukjin Kimc4aaa292012-12-28 16:29:10 -080024#include "exynos-cpufreq.h"
25
Jaecheol Leea125a172012-01-07 20:18:35 +090026static struct exynos_dvfs_info *exynos_info;
27
28static struct regulator *arm_regulator;
Jaecheol Leea125a172012-01-07 20:18:35 +090029
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;
Viresh Kumard4019f02013-08-14 19:38:24 +053062 unsigned int old_freq;
Sachin Kamatd271d072013-01-25 10:18:09 -080063 int index, old_index;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080064 int ret = 0;
Jaecheol Leea125a172012-01-07 20:18:35 +090065
Viresh Kumard4019f02013-08-14 19:38:24 +053066 old_freq = policy->cur;
Jaecheol Leea125a172012-01-07 20:18:35 +090067
Jonghwa Lee53df1ad2012-07-20 02:54:02 +000068 /*
69 * The policy max have been changed so that we cannot get proper
70 * old_index with cpufreq_frequency_table_target(). Thus, ignore
LABBE Corentin05851232013-09-26 16:50:21 +020071 * policy and get the index from the raw frequency table.
Jonghwa Lee53df1ad2012-07-20 02:54:02 +000072 */
Viresh Kumard4019f02013-08-14 19:38:24 +053073 old_index = exynos_cpufreq_get_index(old_freq);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080074 if (old_index < 0) {
75 ret = old_index;
Jaecheol Leea125a172012-01-07 20:18:35 +090076 goto out;
77 }
78
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080079 index = exynos_cpufreq_get_index(target_freq);
80 if (index < 0) {
81 ret = index;
Jaecheol Leea125a172012-01-07 20:18:35 +090082 goto out;
83 }
84
Jaecheol Leea125a172012-01-07 20:18:35 +090085 /*
86 * ARM clock source will be changed APLL to MPLL temporary
87 * To support this level, need to control regulator for
88 * required voltage level
89 */
90 if (exynos_info->need_apll_change != NULL) {
91 if (exynos_info->need_apll_change(old_index, index) &&
92 (freq_table[index].frequency < mpll_freq_khz) &&
93 (freq_table[old_index].frequency < mpll_freq_khz))
94 safe_arm_volt = volt_table[exynos_info->pll_safe_idx];
95 }
96 arm_volt = volt_table[index];
97
Jaecheol Leea125a172012-01-07 20:18:35 +090098 /* When the new frequency is higher than current frequency */
Viresh Kumard4019f02013-08-14 19:38:24 +053099 if ((target_freq > old_freq) && !safe_arm_volt) {
Jaecheol Leea125a172012-01-07 20:18:35 +0900100 /* Firstly, voltage up to increase frequency */
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800101 ret = regulator_set_voltage(arm_regulator, arm_volt, arm_volt);
102 if (ret) {
103 pr_err("%s: failed to set cpu voltage to %d\n",
104 __func__, arm_volt);
Viresh Kumard4019f02013-08-14 19:38:24 +0530105 return ret;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800106 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900107 }
108
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800109 if (safe_arm_volt) {
110 ret = regulator_set_voltage(arm_regulator, safe_arm_volt,
Jaecheol Leea125a172012-01-07 20:18:35 +0900111 safe_arm_volt);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800112 if (ret) {
113 pr_err("%s: failed to set cpu voltage to %d\n",
114 __func__, safe_arm_volt);
Viresh Kumard4019f02013-08-14 19:38:24 +0530115 return ret;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800116 }
117 }
Jonghwan Choi857d90f2012-12-23 15:57:39 -0800118
119 exynos_info->set_freq(old_index, index);
Jaecheol Leea125a172012-01-07 20:18:35 +0900120
Jaecheol Leea125a172012-01-07 20:18:35 +0900121 /* When the new frequency is lower than current frequency */
Viresh Kumard4019f02013-08-14 19:38:24 +0530122 if ((target_freq < old_freq) ||
123 ((target_freq > old_freq) && safe_arm_volt)) {
Jaecheol Leea125a172012-01-07 20:18:35 +0900124 /* down the voltage after frequency change */
Manish Badarkhe006454a2013-10-09 20:43:37 +0530125 ret = regulator_set_voltage(arm_regulator, arm_volt,
Jaecheol Leea125a172012-01-07 20:18:35 +0900126 arm_volt);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800127 if (ret) {
128 pr_err("%s: failed to set cpu voltage to %d\n",
129 __func__, arm_volt);
130 goto out;
131 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900132 }
133
134out:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800135 cpufreq_cpu_put(policy);
136
137 return ret;
138}
139
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530140static int exynos_target(struct cpufreq_policy *policy, unsigned int index)
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800141{
142 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
Sachin Kamat229b21e2013-01-31 17:13:39 -0800143 int ret = 0;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800144
145 mutex_lock(&cpufreq_lock);
146
147 if (frequency_locked)
148 goto out;
149
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530150 ret = exynos_cpufreq_scale(freq_table[index].frequency);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800151
152out:
Jaecheol Leea125a172012-01-07 20:18:35 +0900153 mutex_unlock(&cpufreq_lock);
154
155 return ret;
156}
157
158#ifdef CONFIG_PM
159static int exynos_cpufreq_suspend(struct cpufreq_policy *policy)
160{
161 return 0;
162}
163
164static int exynos_cpufreq_resume(struct cpufreq_policy *policy)
165{
166 return 0;
167}
168#endif
169
170/**
171 * exynos_cpufreq_pm_notifier - block CPUFREQ's activities in suspend-resume
172 * context
173 * @notifier
174 * @pm_event
175 * @v
176 *
177 * While frequency_locked == true, target() ignores every frequency but
178 * locking_frequency. The locking_frequency value is the initial frequency,
179 * which is set by the bootloader. In order to eliminate possible
180 * inconsistency in clock values, we save and restore frequencies during
181 * suspend and resume and block CPUFREQ activities. Note that the standard
182 * suspend/resume cannot be used as they are too deep (syscore_ops) for
183 * regulator actions.
184 */
185static int exynos_cpufreq_pm_notifier(struct notifier_block *notifier,
186 unsigned long pm_event, void *v)
187{
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800188 int ret;
Jaecheol Leea125a172012-01-07 20:18:35 +0900189
Jaecheol Leea125a172012-01-07 20:18:35 +0900190 switch (pm_event) {
191 case PM_SUSPEND_PREPARE:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800192 mutex_lock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900193 frequency_locked = true;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800194 mutex_unlock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900195
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800196 ret = exynos_cpufreq_scale(locking_frequency);
197 if (ret < 0)
198 return NOTIFY_BAD;
Jaecheol Leea125a172012-01-07 20:18:35 +0900199
Jaecheol Leea125a172012-01-07 20:18:35 +0900200 break;
201
202 case PM_POST_SUSPEND:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800203 mutex_lock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900204 frequency_locked = false;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800205 mutex_unlock(&cpufreq_lock);
Jaecheol Leea125a172012-01-07 20:18:35 +0900206 break;
207 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900208
209 return NOTIFY_OK;
210}
211
212static struct notifier_block exynos_cpufreq_nb = {
213 .notifier_call = exynos_cpufreq_pm_notifier,
214};
215
216static int exynos_cpufreq_cpu_init(struct cpufreq_policy *policy)
217{
Viresh Kumarb249aba2013-10-03 20:29:13 +0530218 return cpufreq_generic_init(policy, exynos_info->freq_table, 100000);
Jaecheol Leea125a172012-01-07 20:18:35 +0900219}
220
221static struct cpufreq_driver exynos_driver = {
Viresh Kumarae6b4272013-12-03 11:20:45 +0530222 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
Viresh Kumareea61812013-10-03 20:28:06 +0530223 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530224 .target_index = exynos_target,
Jaecheol Leea125a172012-01-07 20:18:35 +0900225 .get = exynos_getspeed,
226 .init = exynos_cpufreq_cpu_init,
Viresh Kumareea61812013-10-03 20:28:06 +0530227 .exit = cpufreq_generic_exit,
Jaecheol Leea125a172012-01-07 20:18:35 +0900228 .name = "exynos_cpufreq",
Viresh Kumareea61812013-10-03 20:28:06 +0530229 .attr = cpufreq_generic_attr,
Jaecheol Leea125a172012-01-07 20:18:35 +0900230#ifdef CONFIG_PM
231 .suspend = exynos_cpufreq_suspend,
232 .resume = exynos_cpufreq_resume,
233#endif
234};
235
Lukasz Majewskid568b6f2013-11-28 13:42:42 +0100236static int exynos_cpufreq_probe(struct platform_device *pdev)
Jaecheol Leea125a172012-01-07 20:18:35 +0900237{
238 int ret = -EINVAL;
239
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530240 exynos_info = kzalloc(sizeof(*exynos_info), GFP_KERNEL);
Jaecheol Leea125a172012-01-07 20:18:35 +0900241 if (!exynos_info)
242 return -ENOMEM;
243
244 if (soc_is_exynos4210())
245 ret = exynos4210_cpufreq_init(exynos_info);
Jaecheol Leea35c50512012-03-10 02:59:22 -0800246 else if (soc_is_exynos4212() || soc_is_exynos4412())
247 ret = exynos4x12_cpufreq_init(exynos_info);
Jaecheol Lee562a6cb2012-03-10 03:00:02 -0800248 else if (soc_is_exynos5250())
249 ret = exynos5250_cpufreq_init(exynos_info);
Jaecheol Leea125a172012-01-07 20:18:35 +0900250 else
Amit Daniel Kachhapc1585202013-04-08 08:17:36 +0000251 return 0;
Jaecheol Leea125a172012-01-07 20:18:35 +0900252
253 if (ret)
254 goto err_vdd_arm;
255
256 if (exynos_info->set_freq == NULL) {
257 pr_err("%s: No set_freq function (ERR)\n", __func__);
258 goto err_vdd_arm;
259 }
260
261 arm_regulator = regulator_get(NULL, "vdd_arm");
262 if (IS_ERR(arm_regulator)) {
263 pr_err("%s: failed to get resource vdd_arm\n", __func__);
264 goto err_vdd_arm;
265 }
266
Jonghwan Choi6e45eb12013-01-18 11:09:01 -0800267 locking_frequency = exynos_getspeed(0);
268
Jaecheol Leea125a172012-01-07 20:18:35 +0900269 register_pm_notifier(&exynos_cpufreq_nb);
270
271 if (cpufreq_register_driver(&exynos_driver)) {
272 pr_err("%s: failed to register cpufreq driver\n", __func__);
273 goto err_cpufreq;
274 }
275
276 return 0;
277err_cpufreq:
278 unregister_pm_notifier(&exynos_cpufreq_nb);
279
Jonghwan Choi184cddd2012-12-23 15:51:40 -0800280 regulator_put(arm_regulator);
Jaecheol Leea125a172012-01-07 20:18:35 +0900281err_vdd_arm:
282 kfree(exynos_info);
Jaecheol Leea125a172012-01-07 20:18:35 +0900283 return -EINVAL;
284}
Lukasz Majewskid568b6f2013-11-28 13:42:42 +0100285
286static struct platform_driver exynos_cpufreq_platdrv = {
287 .driver = {
288 .name = "exynos-cpufreq",
289 .owner = THIS_MODULE,
290 },
291 .probe = exynos_cpufreq_probe,
292};
293module_platform_driver(exynos_cpufreq_platdrv);