blob: 1e0ec57bf6e35074ac00ff0b58252e2e7077d7e0 [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>
Lukasz Majewskid568b6f2013-11-28 13:42:42 +010019#include <linux/platform_device.h>
Jonghwan Choibe1f7c82014-05-17 08:19:30 +090020#include <linux/of.h>
Jaecheol Leea125a172012-01-07 20:18:35 +090021
Kukjin Kimc4aaa292012-12-28 16:29:10 -080022#include "exynos-cpufreq.h"
23
Jaecheol Leea125a172012-01-07 20:18:35 +090024static struct exynos_dvfs_info *exynos_info;
Jaecheol Leea125a172012-01-07 20:18:35 +090025static struct regulator *arm_regulator;
Jaecheol Leea125a172012-01-07 20:18:35 +090026static unsigned int locking_frequency;
Jaecheol Leea125a172012-01-07 20:18:35 +090027
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080028static int exynos_cpufreq_get_index(unsigned int freq)
Jaecheol Leea125a172012-01-07 20:18:35 +090029{
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080030 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
Stratos Karafotis041526f2014-04-25 23:15:38 +030031 struct cpufreq_frequency_table *pos;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080032
Stratos Karafotis041526f2014-04-25 23:15:38 +030033 cpufreq_for_each_entry(pos, freq_table)
34 if (pos->frequency == freq)
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080035 break;
36
Stratos Karafotis041526f2014-04-25 23:15:38 +030037 if (pos->frequency == CPUFREQ_TABLE_END)
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080038 return -EINVAL;
39
Stratos Karafotis041526f2014-04-25 23:15:38 +030040 return pos - freq_table;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080041}
42
43static int exynos_cpufreq_scale(unsigned int target_freq)
44{
Jaecheol Leea125a172012-01-07 20:18:35 +090045 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
46 unsigned int *volt_table = exynos_info->volt_table;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080047 struct cpufreq_policy *policy = cpufreq_cpu_get(0);
48 unsigned int arm_volt, safe_arm_volt = 0;
Jaecheol Leea125a172012-01-07 20:18:35 +090049 unsigned int mpll_freq_khz = exynos_info->mpll_freq_khz;
Chanwoo Choie5eaa442014-04-18 11:20:33 +090050 struct device *dev = exynos_info->dev;
Viresh Kumard4019f02013-08-14 19:38:24 +053051 unsigned int old_freq;
Sachin Kamatd271d072013-01-25 10:18:09 -080052 int index, old_index;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080053 int ret = 0;
Jaecheol Leea125a172012-01-07 20:18:35 +090054
Viresh Kumard4019f02013-08-14 19:38:24 +053055 old_freq = policy->cur;
Jaecheol Leea125a172012-01-07 20:18:35 +090056
Jonghwa Lee53df1ad2012-07-20 02:54:02 +000057 /*
58 * The policy max have been changed so that we cannot get proper
59 * old_index with cpufreq_frequency_table_target(). Thus, ignore
LABBE Corentin05851232013-09-26 16:50:21 +020060 * policy and get the index from the raw frequency table.
Jonghwa Lee53df1ad2012-07-20 02:54:02 +000061 */
Viresh Kumard4019f02013-08-14 19:38:24 +053062 old_index = exynos_cpufreq_get_index(old_freq);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080063 if (old_index < 0) {
64 ret = old_index;
Jaecheol Leea125a172012-01-07 20:18:35 +090065 goto out;
66 }
67
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080068 index = exynos_cpufreq_get_index(target_freq);
69 if (index < 0) {
70 ret = index;
Jaecheol Leea125a172012-01-07 20:18:35 +090071 goto out;
72 }
73
Jaecheol Leea125a172012-01-07 20:18:35 +090074 /*
75 * ARM clock source will be changed APLL to MPLL temporary
76 * To support this level, need to control regulator for
77 * required voltage level
78 */
79 if (exynos_info->need_apll_change != NULL) {
80 if (exynos_info->need_apll_change(old_index, index) &&
81 (freq_table[index].frequency < mpll_freq_khz) &&
82 (freq_table[old_index].frequency < mpll_freq_khz))
83 safe_arm_volt = volt_table[exynos_info->pll_safe_idx];
84 }
85 arm_volt = volt_table[index];
86
Jaecheol Leea125a172012-01-07 20:18:35 +090087 /* When the new frequency is higher than current frequency */
Viresh Kumard4019f02013-08-14 19:38:24 +053088 if ((target_freq > old_freq) && !safe_arm_volt) {
Jaecheol Leea125a172012-01-07 20:18:35 +090089 /* Firstly, voltage up to increase frequency */
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080090 ret = regulator_set_voltage(arm_regulator, arm_volt, arm_volt);
91 if (ret) {
Chanwoo Choie5eaa442014-04-18 11:20:33 +090092 dev_err(dev, "failed to set cpu voltage to %d\n",
93 arm_volt);
Viresh Kumard4019f02013-08-14 19:38:24 +053094 return ret;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080095 }
Jaecheol Leea125a172012-01-07 20:18:35 +090096 }
97
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080098 if (safe_arm_volt) {
99 ret = regulator_set_voltage(arm_regulator, safe_arm_volt,
Jaecheol Leea125a172012-01-07 20:18:35 +0900100 safe_arm_volt);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800101 if (ret) {
Chanwoo Choie5eaa442014-04-18 11:20:33 +0900102 dev_err(dev, "failed to set cpu voltage to %d\n",
103 safe_arm_volt);
Viresh Kumard4019f02013-08-14 19:38:24 +0530104 return ret;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800105 }
106 }
Jonghwan Choi857d90f2012-12-23 15:57:39 -0800107
108 exynos_info->set_freq(old_index, index);
Jaecheol Leea125a172012-01-07 20:18:35 +0900109
Jaecheol Leea125a172012-01-07 20:18:35 +0900110 /* When the new frequency is lower than current frequency */
Viresh Kumard4019f02013-08-14 19:38:24 +0530111 if ((target_freq < old_freq) ||
112 ((target_freq > old_freq) && safe_arm_volt)) {
Jaecheol Leea125a172012-01-07 20:18:35 +0900113 /* down the voltage after frequency change */
Manish Badarkhe006454a2013-10-09 20:43:37 +0530114 ret = regulator_set_voltage(arm_regulator, arm_volt,
Jaecheol Leea125a172012-01-07 20:18:35 +0900115 arm_volt);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800116 if (ret) {
Chanwoo Choie5eaa442014-04-18 11:20:33 +0900117 dev_err(dev, "failed to set cpu voltage to %d\n",
118 arm_volt);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800119 goto out;
120 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900121 }
122
123out:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800124 cpufreq_cpu_put(policy);
125
126 return ret;
127}
128
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530129static int exynos_target(struct cpufreq_policy *policy, unsigned int index)
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800130{
Viresh Kumard248bb82014-03-04 11:00:28 +0800131 return exynos_cpufreq_scale(exynos_info->freq_table[index].frequency);
Jaecheol Leea125a172012-01-07 20:18:35 +0900132}
133
Jaecheol Leea125a172012-01-07 20:18:35 +0900134static int exynos_cpufreq_cpu_init(struct cpufreq_policy *policy)
135{
Viresh Kumar652ed952014-01-09 20:38:43 +0530136 policy->clk = exynos_info->cpu_clk;
Viresh Kumard248bb82014-03-04 11:00:28 +0800137 policy->suspend_freq = locking_frequency;
Viresh Kumarb249aba2013-10-03 20:29:13 +0530138 return cpufreq_generic_init(policy, exynos_info->freq_table, 100000);
Jaecheol Leea125a172012-01-07 20:18:35 +0900139}
140
141static struct cpufreq_driver exynos_driver = {
Viresh Kumarae6b4272013-12-03 11:20:45 +0530142 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
Viresh Kumareea61812013-10-03 20:28:06 +0530143 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530144 .target_index = exynos_target,
Viresh Kumar652ed952014-01-09 20:38:43 +0530145 .get = cpufreq_generic_get,
Jaecheol Leea125a172012-01-07 20:18:35 +0900146 .init = exynos_cpufreq_cpu_init,
147 .name = "exynos_cpufreq",
Viresh Kumareea61812013-10-03 20:28:06 +0530148 .attr = cpufreq_generic_attr,
Lukasz Majewskic683c2c2013-12-20 15:24:52 +0100149#ifdef CONFIG_ARM_EXYNOS_CPU_FREQ_BOOST_SW
150 .boost_supported = true,
151#endif
Jaecheol Leea125a172012-01-07 20:18:35 +0900152#ifdef CONFIG_PM
Viresh Kumard248bb82014-03-04 11:00:28 +0800153 .suspend = cpufreq_generic_suspend,
Jaecheol Leea125a172012-01-07 20:18:35 +0900154#endif
155};
156
Lukasz Majewskid568b6f2013-11-28 13:42:42 +0100157static int exynos_cpufreq_probe(struct platform_device *pdev)
Jaecheol Leea125a172012-01-07 20:18:35 +0900158{
159 int ret = -EINVAL;
160
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530161 exynos_info = kzalloc(sizeof(*exynos_info), GFP_KERNEL);
Jaecheol Leea125a172012-01-07 20:18:35 +0900162 if (!exynos_info)
163 return -ENOMEM;
164
Chanwoo Choie5eaa442014-04-18 11:20:33 +0900165 exynos_info->dev = &pdev->dev;
166
Jonghwan Choibe1f7c82014-05-17 08:19:30 +0900167 if (of_machine_is_compatible("samsung,exynos4210")) {
168 exynos_info->type = EXYNOS_SOC_4210;
Jaecheol Leea125a172012-01-07 20:18:35 +0900169 ret = exynos4210_cpufreq_init(exynos_info);
Jonghwan Choibe1f7c82014-05-17 08:19:30 +0900170 } else if (of_machine_is_compatible("samsung,exynos4212")) {
171 exynos_info->type = EXYNOS_SOC_4212;
Jaecheol Leea35c5052012-03-10 02:59:22 -0800172 ret = exynos4x12_cpufreq_init(exynos_info);
Jonghwan Choibe1f7c82014-05-17 08:19:30 +0900173 } else if (of_machine_is_compatible("samsung,exynos4412")) {
174 exynos_info->type = EXYNOS_SOC_4412;
175 ret = exynos4x12_cpufreq_init(exynos_info);
176 } else if (of_machine_is_compatible("samsung,exynos5250")) {
177 exynos_info->type = EXYNOS_SOC_5250;
Jaecheol Lee562a6cb2012-03-10 03:00:02 -0800178 ret = exynos5250_cpufreq_init(exynos_info);
Jonghwan Choibe1f7c82014-05-17 08:19:30 +0900179 } else {
180 pr_err("%s: Unknown SoC type\n", __func__);
181 return -ENODEV;
182 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900183
184 if (ret)
185 goto err_vdd_arm;
186
187 if (exynos_info->set_freq == NULL) {
Chanwoo Choie5eaa442014-04-18 11:20:33 +0900188 dev_err(&pdev->dev, "No set_freq function (ERR)\n");
Jaecheol Leea125a172012-01-07 20:18:35 +0900189 goto err_vdd_arm;
190 }
191
192 arm_regulator = regulator_get(NULL, "vdd_arm");
193 if (IS_ERR(arm_regulator)) {
Chanwoo Choie5eaa442014-04-18 11:20:33 +0900194 dev_err(&pdev->dev, "failed to get resource vdd_arm\n");
Jaecheol Leea125a172012-01-07 20:18:35 +0900195 goto err_vdd_arm;
196 }
197
Viresh Kumard248bb82014-03-04 11:00:28 +0800198 /* Done here as we want to capture boot frequency */
Viresh Kumar652ed952014-01-09 20:38:43 +0530199 locking_frequency = clk_get_rate(exynos_info->cpu_clk) / 1000;
Jonghwan Choi6e45eb12013-01-18 11:09:01 -0800200
Viresh Kumard248bb82014-03-04 11:00:28 +0800201 if (!cpufreq_register_driver(&exynos_driver))
202 return 0;
Jaecheol Leea125a172012-01-07 20:18:35 +0900203
Chanwoo Choie5eaa442014-04-18 11:20:33 +0900204 dev_err(&pdev->dev, "failed to register cpufreq driver\n");
Jonghwan Choi184cddd2012-12-23 15:51:40 -0800205 regulator_put(arm_regulator);
Jaecheol Leea125a172012-01-07 20:18:35 +0900206err_vdd_arm:
207 kfree(exynos_info);
Jaecheol Leea125a172012-01-07 20:18:35 +0900208 return -EINVAL;
209}
Lukasz Majewskid568b6f2013-11-28 13:42:42 +0100210
211static struct platform_driver exynos_cpufreq_platdrv = {
212 .driver = {
213 .name = "exynos-cpufreq",
214 .owner = THIS_MODULE,
215 },
216 .probe = exynos_cpufreq_probe,
217};
218module_platform_driver(exynos_cpufreq_platdrv);