Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 1 | /* |
| 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 Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 12 | #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 Majewski | d568b6f | 2013-11-28 13:42:42 +0100 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 21 | |
Jaecheol Lee | 6c523c6 | 2012-01-07 20:18:39 +0900 | [diff] [blame] | 22 | #include <plat/cpu.h> |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 23 | |
Kukjin Kim | c4aaa29 | 2012-12-28 16:29:10 -0800 | [diff] [blame] | 24 | #include "exynos-cpufreq.h" |
| 25 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 26 | static struct exynos_dvfs_info *exynos_info; |
| 27 | |
| 28 | static struct regulator *arm_regulator; |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 29 | |
| 30 | static unsigned int locking_frequency; |
| 31 | static bool frequency_locked; |
| 32 | static DEFINE_MUTEX(cpufreq_lock); |
| 33 | |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 34 | static int exynos_cpufreq_get_index(unsigned int freq) |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 35 | { |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 36 | struct cpufreq_frequency_table *freq_table = exynos_info->freq_table; |
| 37 | int index; |
| 38 | |
| 39 | for (index = 0; |
| 40 | freq_table[index].frequency != CPUFREQ_TABLE_END; index++) |
| 41 | if (freq_table[index].frequency == freq) |
| 42 | break; |
| 43 | |
| 44 | if (freq_table[index].frequency == CPUFREQ_TABLE_END) |
| 45 | return -EINVAL; |
| 46 | |
| 47 | return index; |
| 48 | } |
| 49 | |
| 50 | static int exynos_cpufreq_scale(unsigned int target_freq) |
| 51 | { |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 52 | struct cpufreq_frequency_table *freq_table = exynos_info->freq_table; |
| 53 | unsigned int *volt_table = exynos_info->volt_table; |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 54 | struct cpufreq_policy *policy = cpufreq_cpu_get(0); |
| 55 | unsigned int arm_volt, safe_arm_volt = 0; |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 56 | unsigned int mpll_freq_khz = exynos_info->mpll_freq_khz; |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 57 | unsigned int old_freq; |
Sachin Kamat | d271d07 | 2013-01-25 10:18:09 -0800 | [diff] [blame] | 58 | int index, old_index; |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 59 | int ret = 0; |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 60 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 61 | old_freq = policy->cur; |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 62 | |
Jonghwa Lee | 53df1ad | 2012-07-20 02:54:02 +0000 | [diff] [blame] | 63 | /* |
| 64 | * The policy max have been changed so that we cannot get proper |
| 65 | * old_index with cpufreq_frequency_table_target(). Thus, ignore |
LABBE Corentin | 0585123 | 2013-09-26 16:50:21 +0200 | [diff] [blame] | 66 | * policy and get the index from the raw frequency table. |
Jonghwa Lee | 53df1ad | 2012-07-20 02:54:02 +0000 | [diff] [blame] | 67 | */ |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 68 | old_index = exynos_cpufreq_get_index(old_freq); |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 69 | if (old_index < 0) { |
| 70 | ret = old_index; |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 71 | goto out; |
| 72 | } |
| 73 | |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 74 | index = exynos_cpufreq_get_index(target_freq); |
| 75 | if (index < 0) { |
| 76 | ret = index; |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 77 | goto out; |
| 78 | } |
| 79 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 80 | /* |
| 81 | * ARM clock source will be changed APLL to MPLL temporary |
| 82 | * To support this level, need to control regulator for |
| 83 | * required voltage level |
| 84 | */ |
| 85 | if (exynos_info->need_apll_change != NULL) { |
| 86 | if (exynos_info->need_apll_change(old_index, index) && |
| 87 | (freq_table[index].frequency < mpll_freq_khz) && |
| 88 | (freq_table[old_index].frequency < mpll_freq_khz)) |
| 89 | safe_arm_volt = volt_table[exynos_info->pll_safe_idx]; |
| 90 | } |
| 91 | arm_volt = volt_table[index]; |
| 92 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 93 | /* When the new frequency is higher than current frequency */ |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 94 | if ((target_freq > old_freq) && !safe_arm_volt) { |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 95 | /* Firstly, voltage up to increase frequency */ |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 96 | ret = regulator_set_voltage(arm_regulator, arm_volt, arm_volt); |
| 97 | if (ret) { |
| 98 | pr_err("%s: failed to set cpu voltage to %d\n", |
| 99 | __func__, arm_volt); |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 100 | return ret; |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 101 | } |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 102 | } |
| 103 | |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 104 | if (safe_arm_volt) { |
| 105 | ret = regulator_set_voltage(arm_regulator, safe_arm_volt, |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 106 | safe_arm_volt); |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 107 | if (ret) { |
| 108 | pr_err("%s: failed to set cpu voltage to %d\n", |
| 109 | __func__, safe_arm_volt); |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 110 | return ret; |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 111 | } |
| 112 | } |
Jonghwan Choi | 857d90f | 2012-12-23 15:57:39 -0800 | [diff] [blame] | 113 | |
| 114 | exynos_info->set_freq(old_index, index); |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 115 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 116 | /* When the new frequency is lower than current frequency */ |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 117 | if ((target_freq < old_freq) || |
| 118 | ((target_freq > old_freq) && safe_arm_volt)) { |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 119 | /* down the voltage after frequency change */ |
Manish Badarkhe | 006454a | 2013-10-09 20:43:37 +0530 | [diff] [blame] | 120 | ret = regulator_set_voltage(arm_regulator, arm_volt, |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 121 | arm_volt); |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 122 | if (ret) { |
| 123 | pr_err("%s: failed to set cpu voltage to %d\n", |
| 124 | __func__, arm_volt); |
| 125 | goto out; |
| 126 | } |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | out: |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 130 | cpufreq_cpu_put(policy); |
| 131 | |
| 132 | return ret; |
| 133 | } |
| 134 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 135 | static int exynos_target(struct cpufreq_policy *policy, unsigned int index) |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 136 | { |
| 137 | struct cpufreq_frequency_table *freq_table = exynos_info->freq_table; |
Sachin Kamat | 229b21e | 2013-01-31 17:13:39 -0800 | [diff] [blame] | 138 | int ret = 0; |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 139 | |
| 140 | mutex_lock(&cpufreq_lock); |
| 141 | |
| 142 | if (frequency_locked) |
| 143 | goto out; |
| 144 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 145 | ret = exynos_cpufreq_scale(freq_table[index].frequency); |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 146 | |
| 147 | out: |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 148 | mutex_unlock(&cpufreq_lock); |
| 149 | |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | #ifdef CONFIG_PM |
| 154 | static int exynos_cpufreq_suspend(struct cpufreq_policy *policy) |
| 155 | { |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static int exynos_cpufreq_resume(struct cpufreq_policy *policy) |
| 160 | { |
| 161 | return 0; |
| 162 | } |
| 163 | #endif |
| 164 | |
| 165 | /** |
| 166 | * exynos_cpufreq_pm_notifier - block CPUFREQ's activities in suspend-resume |
| 167 | * context |
| 168 | * @notifier |
| 169 | * @pm_event |
| 170 | * @v |
| 171 | * |
| 172 | * While frequency_locked == true, target() ignores every frequency but |
| 173 | * locking_frequency. The locking_frequency value is the initial frequency, |
| 174 | * which is set by the bootloader. In order to eliminate possible |
| 175 | * inconsistency in clock values, we save and restore frequencies during |
| 176 | * suspend and resume and block CPUFREQ activities. Note that the standard |
| 177 | * suspend/resume cannot be used as they are too deep (syscore_ops) for |
| 178 | * regulator actions. |
| 179 | */ |
| 180 | static int exynos_cpufreq_pm_notifier(struct notifier_block *notifier, |
| 181 | unsigned long pm_event, void *v) |
| 182 | { |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 183 | int ret; |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 184 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 185 | switch (pm_event) { |
| 186 | case PM_SUSPEND_PREPARE: |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 187 | mutex_lock(&cpufreq_lock); |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 188 | frequency_locked = true; |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 189 | mutex_unlock(&cpufreq_lock); |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 190 | |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 191 | ret = exynos_cpufreq_scale(locking_frequency); |
| 192 | if (ret < 0) |
| 193 | return NOTIFY_BAD; |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 194 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 195 | break; |
| 196 | |
| 197 | case PM_POST_SUSPEND: |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 198 | mutex_lock(&cpufreq_lock); |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 199 | frequency_locked = false; |
Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 200 | mutex_unlock(&cpufreq_lock); |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 201 | break; |
| 202 | } |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 203 | |
| 204 | return NOTIFY_OK; |
| 205 | } |
| 206 | |
| 207 | static struct notifier_block exynos_cpufreq_nb = { |
| 208 | .notifier_call = exynos_cpufreq_pm_notifier, |
| 209 | }; |
| 210 | |
| 211 | static int exynos_cpufreq_cpu_init(struct cpufreq_policy *policy) |
| 212 | { |
Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 213 | policy->clk = exynos_info->cpu_clk; |
Viresh Kumar | b249aba | 2013-10-03 20:29:13 +0530 | [diff] [blame] | 214 | return cpufreq_generic_init(policy, exynos_info->freq_table, 100000); |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static struct cpufreq_driver exynos_driver = { |
Viresh Kumar | ae6b427 | 2013-12-03 11:20:45 +0530 | [diff] [blame] | 218 | .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK, |
Viresh Kumar | eea6181 | 2013-10-03 20:28:06 +0530 | [diff] [blame] | 219 | .verify = cpufreq_generic_frequency_table_verify, |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 220 | .target_index = exynos_target, |
Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 221 | .get = cpufreq_generic_get, |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 222 | .init = exynos_cpufreq_cpu_init, |
Viresh Kumar | eea6181 | 2013-10-03 20:28:06 +0530 | [diff] [blame] | 223 | .exit = cpufreq_generic_exit, |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 224 | .name = "exynos_cpufreq", |
Viresh Kumar | eea6181 | 2013-10-03 20:28:06 +0530 | [diff] [blame] | 225 | .attr = cpufreq_generic_attr, |
Lukasz Majewski | c683c2c | 2013-12-20 15:24:52 +0100 | [diff] [blame] | 226 | #ifdef CONFIG_ARM_EXYNOS_CPU_FREQ_BOOST_SW |
| 227 | .boost_supported = true, |
| 228 | #endif |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 229 | #ifdef CONFIG_PM |
| 230 | .suspend = exynos_cpufreq_suspend, |
| 231 | .resume = exynos_cpufreq_resume, |
| 232 | #endif |
| 233 | }; |
| 234 | |
Lukasz Majewski | d568b6f | 2013-11-28 13:42:42 +0100 | [diff] [blame] | 235 | static int exynos_cpufreq_probe(struct platform_device *pdev) |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 236 | { |
| 237 | int ret = -EINVAL; |
| 238 | |
Viresh Kumar | d5b73cd | 2013-08-06 22:53:06 +0530 | [diff] [blame] | 239 | exynos_info = kzalloc(sizeof(*exynos_info), GFP_KERNEL); |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 240 | if (!exynos_info) |
| 241 | return -ENOMEM; |
| 242 | |
| 243 | if (soc_is_exynos4210()) |
| 244 | ret = exynos4210_cpufreq_init(exynos_info); |
Jaecheol Lee | a35c505 | 2012-03-10 02:59:22 -0800 | [diff] [blame] | 245 | else if (soc_is_exynos4212() || soc_is_exynos4412()) |
| 246 | ret = exynos4x12_cpufreq_init(exynos_info); |
Jaecheol Lee | 562a6cb | 2012-03-10 03:00:02 -0800 | [diff] [blame] | 247 | else if (soc_is_exynos5250()) |
| 248 | ret = exynos5250_cpufreq_init(exynos_info); |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 249 | else |
Amit Daniel Kachhap | c158520 | 2013-04-08 08:17:36 +0000 | [diff] [blame] | 250 | return 0; |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 251 | |
| 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 | |
Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 266 | locking_frequency = clk_get_rate(exynos_info->cpu_clk) / 1000; |
Jonghwan Choi | 6e45eb1 | 2013-01-18 11:09:01 -0800 | [diff] [blame] | 267 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 268 | 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; |
| 276 | err_cpufreq: |
| 277 | unregister_pm_notifier(&exynos_cpufreq_nb); |
| 278 | |
Jonghwan Choi | 184cddd | 2012-12-23 15:51:40 -0800 | [diff] [blame] | 279 | regulator_put(arm_regulator); |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 280 | err_vdd_arm: |
| 281 | kfree(exynos_info); |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 282 | return -EINVAL; |
| 283 | } |
Lukasz Majewski | d568b6f | 2013-11-28 13:42:42 +0100 | [diff] [blame] | 284 | |
| 285 | static struct platform_driver exynos_cpufreq_platdrv = { |
| 286 | .driver = { |
| 287 | .name = "exynos-cpufreq", |
| 288 | .owner = THIS_MODULE, |
| 289 | }, |
| 290 | .probe = exynos_cpufreq_probe, |
| 291 | }; |
| 292 | module_platform_driver(exynos_cpufreq_platdrv); |