| 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> | 
| Lukasz Majewski | d568b6f | 2013-11-28 13:42:42 +0100 | [diff] [blame] | 19 | #include <linux/platform_device.h> | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 20 |  | 
| Jaecheol Lee | 6c523c6 | 2012-01-07 20:18:39 +0900 | [diff] [blame] | 21 | #include <plat/cpu.h> | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 22 |  | 
| Kukjin Kim | c4aaa29 | 2012-12-28 16:29:10 -0800 | [diff] [blame] | 23 | #include "exynos-cpufreq.h" | 
|  | 24 |  | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 25 | static struct exynos_dvfs_info *exynos_info; | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 26 | static struct regulator *arm_regulator; | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 27 | static unsigned int locking_frequency; | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 28 |  | 
| Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 29 | static int exynos_cpufreq_get_index(unsigned int freq) | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 30 | { | 
| Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 31 | struct cpufreq_frequency_table *freq_table = exynos_info->freq_table; | 
|  | 32 | int index; | 
|  | 33 |  | 
|  | 34 | for (index = 0; | 
|  | 35 | freq_table[index].frequency != CPUFREQ_TABLE_END; index++) | 
|  | 36 | if (freq_table[index].frequency == freq) | 
|  | 37 | break; | 
|  | 38 |  | 
|  | 39 | if (freq_table[index].frequency == CPUFREQ_TABLE_END) | 
|  | 40 | return -EINVAL; | 
|  | 41 |  | 
|  | 42 | return index; | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | static int exynos_cpufreq_scale(unsigned int target_freq) | 
|  | 46 | { | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 47 | struct cpufreq_frequency_table *freq_table = exynos_info->freq_table; | 
|  | 48 | unsigned int *volt_table = exynos_info->volt_table; | 
| Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 49 | struct cpufreq_policy *policy = cpufreq_cpu_get(0); | 
|  | 50 | unsigned int arm_volt, safe_arm_volt = 0; | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 51 | unsigned int mpll_freq_khz = exynos_info->mpll_freq_khz; | 
| Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 52 | unsigned int old_freq; | 
| Sachin Kamat | d271d07 | 2013-01-25 10:18:09 -0800 | [diff] [blame] | 53 | int index, old_index; | 
| Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 54 | int ret = 0; | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 55 |  | 
| Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 56 | old_freq = policy->cur; | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 57 |  | 
| Jonghwa Lee | 53df1ad | 2012-07-20 02:54:02 +0000 | [diff] [blame] | 58 | /* | 
|  | 59 | * The policy max have been changed so that we cannot get proper | 
|  | 60 | * old_index with cpufreq_frequency_table_target(). Thus, ignore | 
| LABBE Corentin | 0585123 | 2013-09-26 16:50:21 +0200 | [diff] [blame] | 61 | * policy and get the index from the raw frequency table. | 
| Jonghwa Lee | 53df1ad | 2012-07-20 02:54:02 +0000 | [diff] [blame] | 62 | */ | 
| Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 63 | old_index = exynos_cpufreq_get_index(old_freq); | 
| Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 64 | if (old_index < 0) { | 
|  | 65 | ret = old_index; | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 66 | goto out; | 
|  | 67 | } | 
|  | 68 |  | 
| Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 69 | index = exynos_cpufreq_get_index(target_freq); | 
|  | 70 | if (index < 0) { | 
|  | 71 | ret = index; | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 72 | goto out; | 
|  | 73 | } | 
|  | 74 |  | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 75 | /* | 
|  | 76 | * ARM clock source will be changed APLL to MPLL temporary | 
|  | 77 | * To support this level, need to control regulator for | 
|  | 78 | * required voltage level | 
|  | 79 | */ | 
|  | 80 | if (exynos_info->need_apll_change != NULL) { | 
|  | 81 | if (exynos_info->need_apll_change(old_index, index) && | 
|  | 82 | (freq_table[index].frequency < mpll_freq_khz) && | 
|  | 83 | (freq_table[old_index].frequency < mpll_freq_khz)) | 
|  | 84 | safe_arm_volt = volt_table[exynos_info->pll_safe_idx]; | 
|  | 85 | } | 
|  | 86 | arm_volt = volt_table[index]; | 
|  | 87 |  | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 88 | /* When the new frequency is higher than current frequency */ | 
| Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 89 | if ((target_freq > old_freq) && !safe_arm_volt) { | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 90 | /* Firstly, voltage up to increase frequency */ | 
| Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 91 | ret = regulator_set_voltage(arm_regulator, arm_volt, arm_volt); | 
|  | 92 | if (ret) { | 
|  | 93 | pr_err("%s: failed to set cpu voltage to %d\n", | 
|  | 94 | __func__, arm_volt); | 
| Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 95 | return ret; | 
| Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 96 | } | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 97 | } | 
|  | 98 |  | 
| Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 99 | if (safe_arm_volt) { | 
|  | 100 | ret = regulator_set_voltage(arm_regulator, safe_arm_volt, | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 101 | safe_arm_volt); | 
| Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 102 | if (ret) { | 
|  | 103 | pr_err("%s: failed to set cpu voltage to %d\n", | 
|  | 104 | __func__, safe_arm_volt); | 
| Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 105 | return ret; | 
| Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 106 | } | 
|  | 107 | } | 
| Jonghwan Choi | 857d90f | 2012-12-23 15:57:39 -0800 | [diff] [blame] | 108 |  | 
|  | 109 | exynos_info->set_freq(old_index, index); | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 110 |  | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 111 | /* When the new frequency is lower than current frequency */ | 
| Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 112 | if ((target_freq < old_freq) || | 
|  | 113 | ((target_freq > old_freq) && safe_arm_volt)) { | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 114 | /* down the voltage after frequency change */ | 
| Manish Badarkhe | 006454a | 2013-10-09 20:43:37 +0530 | [diff] [blame] | 115 | ret = regulator_set_voltage(arm_regulator, arm_volt, | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 116 | arm_volt); | 
| Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 117 | if (ret) { | 
|  | 118 | pr_err("%s: failed to set cpu voltage to %d\n", | 
|  | 119 | __func__, arm_volt); | 
|  | 120 | goto out; | 
|  | 121 | } | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 122 | } | 
|  | 123 |  | 
|  | 124 | out: | 
| Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 125 | cpufreq_cpu_put(policy); | 
|  | 126 |  | 
|  | 127 | return ret; | 
|  | 128 | } | 
|  | 129 |  | 
| Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 130 | static int exynos_target(struct cpufreq_policy *policy, unsigned int index) | 
| Jonghwan Choi | 0e0e425 | 2012-12-23 15:57:48 -0800 | [diff] [blame] | 131 | { | 
| Viresh Kumar | d248bb8 | 2014-03-04 11:00:28 +0800 | [diff] [blame^] | 132 | return exynos_cpufreq_scale(exynos_info->freq_table[index].frequency); | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 133 | } | 
|  | 134 |  | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 135 | static int exynos_cpufreq_cpu_init(struct cpufreq_policy *policy) | 
|  | 136 | { | 
| Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 137 | policy->clk = exynos_info->cpu_clk; | 
| Viresh Kumar | d248bb8 | 2014-03-04 11:00:28 +0800 | [diff] [blame^] | 138 | policy->suspend_freq = locking_frequency; | 
| Viresh Kumar | b249aba | 2013-10-03 20:29:13 +0530 | [diff] [blame] | 139 | return cpufreq_generic_init(policy, exynos_info->freq_table, 100000); | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 140 | } | 
|  | 141 |  | 
|  | 142 | static struct cpufreq_driver exynos_driver = { | 
| Viresh Kumar | ae6b427 | 2013-12-03 11:20:45 +0530 | [diff] [blame] | 143 | .flags		= CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK, | 
| Viresh Kumar | eea6181 | 2013-10-03 20:28:06 +0530 | [diff] [blame] | 144 | .verify		= cpufreq_generic_frequency_table_verify, | 
| Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 145 | .target_index	= exynos_target, | 
| Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 146 | .get		= cpufreq_generic_get, | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 147 | .init		= exynos_cpufreq_cpu_init, | 
| Viresh Kumar | eea6181 | 2013-10-03 20:28:06 +0530 | [diff] [blame] | 148 | .exit		= cpufreq_generic_exit, | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 149 | .name		= "exynos_cpufreq", | 
| Viresh Kumar | eea6181 | 2013-10-03 20:28:06 +0530 | [diff] [blame] | 150 | .attr		= cpufreq_generic_attr, | 
| Lukasz Majewski | c683c2c | 2013-12-20 15:24:52 +0100 | [diff] [blame] | 151 | #ifdef CONFIG_ARM_EXYNOS_CPU_FREQ_BOOST_SW | 
|  | 152 | .boost_supported = true, | 
|  | 153 | #endif | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 154 | #ifdef CONFIG_PM | 
| Viresh Kumar | d248bb8 | 2014-03-04 11:00:28 +0800 | [diff] [blame^] | 155 | .suspend	= cpufreq_generic_suspend, | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 156 | #endif | 
|  | 157 | }; | 
|  | 158 |  | 
| Lukasz Majewski | d568b6f | 2013-11-28 13:42:42 +0100 | [diff] [blame] | 159 | static int exynos_cpufreq_probe(struct platform_device *pdev) | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 160 | { | 
|  | 161 | int ret = -EINVAL; | 
|  | 162 |  | 
| Viresh Kumar | d5b73cd | 2013-08-06 22:53:06 +0530 | [diff] [blame] | 163 | exynos_info = kzalloc(sizeof(*exynos_info), GFP_KERNEL); | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 164 | if (!exynos_info) | 
|  | 165 | return -ENOMEM; | 
|  | 166 |  | 
|  | 167 | if (soc_is_exynos4210()) | 
|  | 168 | ret = exynos4210_cpufreq_init(exynos_info); | 
| Jaecheol Lee | a35c505 | 2012-03-10 02:59:22 -0800 | [diff] [blame] | 169 | else if (soc_is_exynos4212() || soc_is_exynos4412()) | 
|  | 170 | ret = exynos4x12_cpufreq_init(exynos_info); | 
| Jaecheol Lee | 562a6cb | 2012-03-10 03:00:02 -0800 | [diff] [blame] | 171 | else if (soc_is_exynos5250()) | 
|  | 172 | ret = exynos5250_cpufreq_init(exynos_info); | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 173 | else | 
| Amit Daniel Kachhap | c158520 | 2013-04-08 08:17:36 +0000 | [diff] [blame] | 174 | return 0; | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 175 |  | 
|  | 176 | if (ret) | 
|  | 177 | goto err_vdd_arm; | 
|  | 178 |  | 
|  | 179 | if (exynos_info->set_freq == NULL) { | 
|  | 180 | pr_err("%s: No set_freq function (ERR)\n", __func__); | 
|  | 181 | goto err_vdd_arm; | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | arm_regulator = regulator_get(NULL, "vdd_arm"); | 
|  | 185 | if (IS_ERR(arm_regulator)) { | 
|  | 186 | pr_err("%s: failed to get resource vdd_arm\n", __func__); | 
|  | 187 | goto err_vdd_arm; | 
|  | 188 | } | 
|  | 189 |  | 
| Viresh Kumar | d248bb8 | 2014-03-04 11:00:28 +0800 | [diff] [blame^] | 190 | /* Done here as we want to capture boot frequency */ | 
| Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 191 | locking_frequency = clk_get_rate(exynos_info->cpu_clk) / 1000; | 
| Jonghwan Choi | 6e45eb1 | 2013-01-18 11:09:01 -0800 | [diff] [blame] | 192 |  | 
| Viresh Kumar | d248bb8 | 2014-03-04 11:00:28 +0800 | [diff] [blame^] | 193 | if (!cpufreq_register_driver(&exynos_driver)) | 
|  | 194 | return 0; | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 195 |  | 
| Viresh Kumar | d248bb8 | 2014-03-04 11:00:28 +0800 | [diff] [blame^] | 196 | pr_err("%s: failed to register cpufreq driver\n", __func__); | 
| Jonghwan Choi | 184cddd | 2012-12-23 15:51:40 -0800 | [diff] [blame] | 197 | regulator_put(arm_regulator); | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 198 | err_vdd_arm: | 
|  | 199 | kfree(exynos_info); | 
| Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 200 | return -EINVAL; | 
|  | 201 | } | 
| Lukasz Majewski | d568b6f | 2013-11-28 13:42:42 +0100 | [diff] [blame] | 202 |  | 
|  | 203 | static struct platform_driver exynos_cpufreq_platdrv = { | 
|  | 204 | .driver = { | 
|  | 205 | .name	= "exynos-cpufreq", | 
|  | 206 | .owner	= THIS_MODULE, | 
|  | 207 | }, | 
|  | 208 | .probe = exynos_cpufreq_probe, | 
|  | 209 | }; | 
|  | 210 | module_platform_driver(exynos_cpufreq_platdrv); |