blob: e8a4a7ed38c165f7a785f6539c6f1b06d39fa7dd [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
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;
Jaecheol Leea125a172012-01-07 20:18:35 +090027static struct regulator *arm_regulator;
Jaecheol Leea125a172012-01-07 20:18:35 +090028static unsigned int locking_frequency;
Jaecheol Leea125a172012-01-07 20:18:35 +090029
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080030static int exynos_cpufreq_get_index(unsigned int freq)
Jaecheol Leea125a172012-01-07 20:18:35 +090031{
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080032 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
33 int index;
34
35 for (index = 0;
36 freq_table[index].frequency != CPUFREQ_TABLE_END; index++)
37 if (freq_table[index].frequency == freq)
38 break;
39
40 if (freq_table[index].frequency == CPUFREQ_TABLE_END)
41 return -EINVAL;
42
43 return index;
44}
45
46static int exynos_cpufreq_scale(unsigned int target_freq)
47{
Jaecheol Leea125a172012-01-07 20:18:35 +090048 struct cpufreq_frequency_table *freq_table = exynos_info->freq_table;
49 unsigned int *volt_table = exynos_info->volt_table;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080050 struct cpufreq_policy *policy = cpufreq_cpu_get(0);
51 unsigned int arm_volt, safe_arm_volt = 0;
Jaecheol Leea125a172012-01-07 20:18:35 +090052 unsigned int mpll_freq_khz = exynos_info->mpll_freq_khz;
Viresh Kumard4019f02013-08-14 19:38:24 +053053 unsigned int old_freq;
Sachin Kamatd271d072013-01-25 10:18:09 -080054 int index, old_index;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080055 int ret = 0;
Jaecheol Leea125a172012-01-07 20:18:35 +090056
Viresh Kumard4019f02013-08-14 19:38:24 +053057 old_freq = policy->cur;
Jaecheol Leea125a172012-01-07 20:18:35 +090058
Jonghwa Lee53df1ad2012-07-20 02:54:02 +000059 /*
60 * The policy max have been changed so that we cannot get proper
61 * old_index with cpufreq_frequency_table_target(). Thus, ignore
LABBE Corentin05851232013-09-26 16:50:21 +020062 * policy and get the index from the raw frequency table.
Jonghwa Lee53df1ad2012-07-20 02:54:02 +000063 */
Viresh Kumard4019f02013-08-14 19:38:24 +053064 old_index = exynos_cpufreq_get_index(old_freq);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080065 if (old_index < 0) {
66 ret = old_index;
Jaecheol Leea125a172012-01-07 20:18:35 +090067 goto out;
68 }
69
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080070 index = exynos_cpufreq_get_index(target_freq);
71 if (index < 0) {
72 ret = index;
Jaecheol Leea125a172012-01-07 20:18:35 +090073 goto out;
74 }
75
Jaecheol Leea125a172012-01-07 20:18:35 +090076 /*
77 * ARM clock source will be changed APLL to MPLL temporary
78 * To support this level, need to control regulator for
79 * required voltage level
80 */
81 if (exynos_info->need_apll_change != NULL) {
82 if (exynos_info->need_apll_change(old_index, index) &&
83 (freq_table[index].frequency < mpll_freq_khz) &&
84 (freq_table[old_index].frequency < mpll_freq_khz))
85 safe_arm_volt = volt_table[exynos_info->pll_safe_idx];
86 }
87 arm_volt = volt_table[index];
88
Jaecheol Leea125a172012-01-07 20:18:35 +090089 /* When the new frequency is higher than current frequency */
Viresh Kumard4019f02013-08-14 19:38:24 +053090 if ((target_freq > old_freq) && !safe_arm_volt) {
Jaecheol Leea125a172012-01-07 20:18:35 +090091 /* Firstly, voltage up to increase frequency */
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080092 ret = regulator_set_voltage(arm_regulator, arm_volt, arm_volt);
93 if (ret) {
94 pr_err("%s: failed to set cpu voltage to %d\n",
95 __func__, arm_volt);
Viresh Kumard4019f02013-08-14 19:38:24 +053096 return ret;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -080097 }
Jaecheol Leea125a172012-01-07 20:18:35 +090098 }
99
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800100 if (safe_arm_volt) {
101 ret = regulator_set_voltage(arm_regulator, safe_arm_volt,
Jaecheol Leea125a172012-01-07 20:18:35 +0900102 safe_arm_volt);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800103 if (ret) {
104 pr_err("%s: failed to set cpu voltage to %d\n",
105 __func__, safe_arm_volt);
Viresh Kumard4019f02013-08-14 19:38:24 +0530106 return ret;
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800107 }
108 }
Jonghwan Choi857d90f2012-12-23 15:57:39 -0800109
110 exynos_info->set_freq(old_index, index);
Jaecheol Leea125a172012-01-07 20:18:35 +0900111
Jaecheol Leea125a172012-01-07 20:18:35 +0900112 /* When the new frequency is lower than current frequency */
Viresh Kumard4019f02013-08-14 19:38:24 +0530113 if ((target_freq < old_freq) ||
114 ((target_freq > old_freq) && safe_arm_volt)) {
Jaecheol Leea125a172012-01-07 20:18:35 +0900115 /* down the voltage after frequency change */
Manish Badarkhe006454a2013-10-09 20:43:37 +0530116 ret = regulator_set_voltage(arm_regulator, arm_volt,
Jaecheol Leea125a172012-01-07 20:18:35 +0900117 arm_volt);
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800118 if (ret) {
119 pr_err("%s: failed to set cpu voltage to %d\n",
120 __func__, arm_volt);
121 goto out;
122 }
Jaecheol Leea125a172012-01-07 20:18:35 +0900123 }
124
125out:
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800126 cpufreq_cpu_put(policy);
127
128 return ret;
129}
130
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530131static int exynos_target(struct cpufreq_policy *policy, unsigned int index)
Jonghwan Choi0e0e4252012-12-23 15:57:48 -0800132{
Viresh Kumard248bb82014-03-04 11:00:28 +0800133 return exynos_cpufreq_scale(exynos_info->freq_table[index].frequency);
Jaecheol Leea125a172012-01-07 20:18:35 +0900134}
135
Jaecheol Leea125a172012-01-07 20:18:35 +0900136static int exynos_cpufreq_cpu_init(struct cpufreq_policy *policy)
137{
Viresh Kumar652ed952014-01-09 20:38:43 +0530138 policy->clk = exynos_info->cpu_clk;
Viresh Kumard248bb82014-03-04 11:00:28 +0800139 policy->suspend_freq = locking_frequency;
Viresh Kumarb249aba2013-10-03 20:29:13 +0530140 return cpufreq_generic_init(policy, exynos_info->freq_table, 100000);
Jaecheol Leea125a172012-01-07 20:18:35 +0900141}
142
143static struct cpufreq_driver exynos_driver = {
Viresh Kumarae6b4272013-12-03 11:20:45 +0530144 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
Viresh Kumareea61812013-10-03 20:28:06 +0530145 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530146 .target_index = exynos_target,
Viresh Kumar652ed952014-01-09 20:38:43 +0530147 .get = cpufreq_generic_get,
Jaecheol Leea125a172012-01-07 20:18:35 +0900148 .init = exynos_cpufreq_cpu_init,
149 .name = "exynos_cpufreq",
Viresh Kumareea61812013-10-03 20:28:06 +0530150 .attr = cpufreq_generic_attr,
Lukasz Majewskic683c2c2013-12-20 15:24:52 +0100151#ifdef CONFIG_ARM_EXYNOS_CPU_FREQ_BOOST_SW
152 .boost_supported = true,
153#endif
Jaecheol Leea125a172012-01-07 20:18:35 +0900154#ifdef CONFIG_PM
Viresh Kumard248bb82014-03-04 11:00:28 +0800155 .suspend = cpufreq_generic_suspend,
Jaecheol Leea125a172012-01-07 20:18:35 +0900156#endif
157};
158
Lukasz Majewskid568b6f2013-11-28 13:42:42 +0100159static int exynos_cpufreq_probe(struct platform_device *pdev)
Jaecheol Leea125a172012-01-07 20:18:35 +0900160{
161 int ret = -EINVAL;
162
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530163 exynos_info = kzalloc(sizeof(*exynos_info), GFP_KERNEL);
Jaecheol Leea125a172012-01-07 20:18:35 +0900164 if (!exynos_info)
165 return -ENOMEM;
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) {
188 pr_err("%s: No set_freq function (ERR)\n", __func__);
189 goto err_vdd_arm;
190 }
191
192 arm_regulator = regulator_get(NULL, "vdd_arm");
193 if (IS_ERR(arm_regulator)) {
194 pr_err("%s: failed to get resource vdd_arm\n", __func__);
195 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
Viresh Kumard248bb82014-03-04 11:00:28 +0800204 pr_err("%s: failed to register cpufreq driver\n", __func__);
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);