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