blob: d83965737462340492a6ae9fecc88b259cb24420 [file] [log] [blame]
Shawn Guo1dd538f2013-02-04 05:46:29 +00001/*
2 * Copyright (C) 2013 Freescale Semiconductor, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/clk.h>
Sudeep KarkadaNageshab494b482013-09-10 18:59:47 +010010#include <linux/cpu.h>
Shawn Guo1dd538f2013-02-04 05:46:29 +000011#include <linux/cpufreq.h>
12#include <linux/delay.h>
13#include <linux/err.h>
14#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/opp.h>
17#include <linux/platform_device.h>
18#include <linux/regulator/consumer.h>
19
20#define PU_SOC_VOLTAGE_NORMAL 1250000
21#define PU_SOC_VOLTAGE_HIGH 1275000
22#define FREQ_1P2_GHZ 1200000000
23
24static struct regulator *arm_reg;
25static struct regulator *pu_reg;
26static struct regulator *soc_reg;
27
28static struct clk *arm_clk;
29static struct clk *pll1_sys_clk;
30static struct clk *pll1_sw_clk;
31static struct clk *step_clk;
32static struct clk *pll2_pfd2_396m_clk;
33
34static struct device *cpu_dev;
35static struct cpufreq_frequency_table *freq_table;
36static unsigned int transition_latency;
37
Shawn Guo1dd538f2013-02-04 05:46:29 +000038static unsigned int imx6q_get_speed(unsigned int cpu)
39{
40 return clk_get_rate(arm_clk) / 1000;
41}
42
43static int imx6q_set_target(struct cpufreq_policy *policy,
44 unsigned int target_freq, unsigned int relation)
45{
46 struct cpufreq_freqs freqs;
47 struct opp *opp;
48 unsigned long freq_hz, volt, volt_old;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053049 unsigned int index;
Shawn Guo1dd538f2013-02-04 05:46:29 +000050 int ret;
51
52 ret = cpufreq_frequency_table_target(policy, freq_table, target_freq,
53 relation, &index);
54 if (ret) {
55 dev_err(cpu_dev, "failed to match target frequency %d: %d\n",
56 target_freq, ret);
57 return ret;
58 }
59
60 freqs.new = freq_table[index].frequency;
61 freq_hz = freqs.new * 1000;
62 freqs.old = clk_get_rate(arm_clk) / 1000;
63
64 if (freqs.old == freqs.new)
65 return 0;
66
Shawn Guo1dd538f2013-02-04 05:46:29 +000067 rcu_read_lock();
68 opp = opp_find_freq_ceil(cpu_dev, &freq_hz);
69 if (IS_ERR(opp)) {
70 rcu_read_unlock();
71 dev_err(cpu_dev, "failed to find OPP for %ld\n", freq_hz);
72 return PTR_ERR(opp);
73 }
74
75 volt = opp_get_voltage(opp);
76 rcu_read_unlock();
77 volt_old = regulator_get_voltage(arm_reg);
78
79 dev_dbg(cpu_dev, "%u MHz, %ld mV --> %u MHz, %ld mV\n",
80 freqs.old / 1000, volt_old / 1000,
81 freqs.new / 1000, volt / 1000);
82
Viresh Kumar5a571c32013-06-19 11:18:20 +053083 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
84
Shawn Guo1dd538f2013-02-04 05:46:29 +000085 /* scaling up? scale voltage before frequency */
86 if (freqs.new > freqs.old) {
87 ret = regulator_set_voltage_tol(arm_reg, volt, 0);
88 if (ret) {
89 dev_err(cpu_dev,
90 "failed to scale vddarm up: %d\n", ret);
Viresh Kumar5a571c32013-06-19 11:18:20 +053091 freqs.new = freqs.old;
92 goto post_notify;
Shawn Guo1dd538f2013-02-04 05:46:29 +000093 }
94
95 /*
96 * Need to increase vddpu and vddsoc for safety
97 * if we are about to run at 1.2 GHz.
98 */
99 if (freqs.new == FREQ_1P2_GHZ / 1000) {
100 regulator_set_voltage_tol(pu_reg,
101 PU_SOC_VOLTAGE_HIGH, 0);
102 regulator_set_voltage_tol(soc_reg,
103 PU_SOC_VOLTAGE_HIGH, 0);
104 }
105 }
106
107 /*
108 * The setpoints are selected per PLL/PDF frequencies, so we need to
109 * reprogram PLL for frequency scaling. The procedure of reprogramming
110 * PLL1 is as below.
111 *
112 * - Enable pll2_pfd2_396m_clk and reparent pll1_sw_clk to it
113 * - Reprogram pll1_sys_clk and reparent pll1_sw_clk back to it
114 * - Disable pll2_pfd2_396m_clk
115 */
Shawn Guo1dd538f2013-02-04 05:46:29 +0000116 clk_set_parent(step_clk, pll2_pfd2_396m_clk);
117 clk_set_parent(pll1_sw_clk, step_clk);
118 if (freq_hz > clk_get_rate(pll2_pfd2_396m_clk)) {
119 clk_set_rate(pll1_sys_clk, freqs.new * 1000);
Shawn Guo1dd538f2013-02-04 05:46:29 +0000120 clk_set_parent(pll1_sw_clk, pll1_sys_clk);
Shawn Guo1dd538f2013-02-04 05:46:29 +0000121 }
122
123 /* Ensure the arm clock divider is what we expect */
124 ret = clk_set_rate(arm_clk, freqs.new * 1000);
125 if (ret) {
126 dev_err(cpu_dev, "failed to set clock rate: %d\n", ret);
127 regulator_set_voltage_tol(arm_reg, volt_old, 0);
Viresh Kumar5a571c32013-06-19 11:18:20 +0530128 freqs.new = freqs.old;
129 goto post_notify;
Shawn Guo1dd538f2013-02-04 05:46:29 +0000130 }
131
132 /* scaling down? scale voltage after frequency */
133 if (freqs.new < freqs.old) {
134 ret = regulator_set_voltage_tol(arm_reg, volt, 0);
Viresh Kumar5a571c32013-06-19 11:18:20 +0530135 if (ret) {
Shawn Guo1dd538f2013-02-04 05:46:29 +0000136 dev_warn(cpu_dev,
137 "failed to scale vddarm down: %d\n", ret);
Viresh Kumar5a571c32013-06-19 11:18:20 +0530138 ret = 0;
139 }
Shawn Guo1dd538f2013-02-04 05:46:29 +0000140
141 if (freqs.old == FREQ_1P2_GHZ / 1000) {
142 regulator_set_voltage_tol(pu_reg,
143 PU_SOC_VOLTAGE_NORMAL, 0);
144 regulator_set_voltage_tol(soc_reg,
145 PU_SOC_VOLTAGE_NORMAL, 0);
146 }
147 }
148
Viresh Kumar5a571c32013-06-19 11:18:20 +0530149post_notify:
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530150 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Shawn Guo1dd538f2013-02-04 05:46:29 +0000151
Viresh Kumar5a571c32013-06-19 11:18:20 +0530152 return ret;
Shawn Guo1dd538f2013-02-04 05:46:29 +0000153}
154
155static int imx6q_cpufreq_init(struct cpufreq_policy *policy)
156{
157 int ret;
158
Viresh Kumar9ff4a802013-09-16 18:56:18 +0530159 ret = cpufreq_table_validate_and_show(policy, freq_table);
Shawn Guo1dd538f2013-02-04 05:46:29 +0000160 if (ret) {
161 dev_err(cpu_dev, "invalid frequency table: %d\n", ret);
162 return ret;
163 }
164
165 policy->cpuinfo.transition_latency = transition_latency;
166 policy->cur = clk_get_rate(arm_clk) / 1000;
167 cpumask_setall(policy->cpus);
Shawn Guo1dd538f2013-02-04 05:46:29 +0000168
169 return 0;
170}
171
Shawn Guo1dd538f2013-02-04 05:46:29 +0000172static struct cpufreq_driver imx6q_cpufreq_driver = {
Viresh Kumar4f6ba382013-10-03 20:28:08 +0530173 .verify = cpufreq_generic_frequency_table_verify,
Shawn Guo1dd538f2013-02-04 05:46:29 +0000174 .target = imx6q_set_target,
175 .get = imx6q_get_speed,
176 .init = imx6q_cpufreq_init,
Viresh Kumar4f6ba382013-10-03 20:28:08 +0530177 .exit = cpufreq_generic_exit,
Shawn Guo1dd538f2013-02-04 05:46:29 +0000178 .name = "imx6q-cpufreq",
Viresh Kumar4f6ba382013-10-03 20:28:08 +0530179 .attr = cpufreq_generic_attr,
Shawn Guo1dd538f2013-02-04 05:46:29 +0000180};
181
182static int imx6q_cpufreq_probe(struct platform_device *pdev)
183{
184 struct device_node *np;
185 struct opp *opp;
186 unsigned long min_volt, max_volt;
187 int num, ret;
188
Sudeep KarkadaNageshab494b482013-09-10 18:59:47 +0100189 cpu_dev = get_cpu_device(0);
190 if (!cpu_dev) {
191 pr_err("failed to get cpu0 device\n");
192 return -ENODEV;
193 }
Shawn Guo1dd538f2013-02-04 05:46:29 +0000194
Sudeep KarkadaNageshacdc58d62013-06-17 14:58:48 +0100195 np = of_node_get(cpu_dev->of_node);
Shawn Guo1dd538f2013-02-04 05:46:29 +0000196 if (!np) {
197 dev_err(cpu_dev, "failed to find cpu0 node\n");
198 return -ENOENT;
199 }
200
Shawn Guo1dd538f2013-02-04 05:46:29 +0000201 arm_clk = devm_clk_get(cpu_dev, "arm");
202 pll1_sys_clk = devm_clk_get(cpu_dev, "pll1_sys");
203 pll1_sw_clk = devm_clk_get(cpu_dev, "pll1_sw");
204 step_clk = devm_clk_get(cpu_dev, "step");
205 pll2_pfd2_396m_clk = devm_clk_get(cpu_dev, "pll2_pfd2_396m");
206 if (IS_ERR(arm_clk) || IS_ERR(pll1_sys_clk) || IS_ERR(pll1_sw_clk) ||
207 IS_ERR(step_clk) || IS_ERR(pll2_pfd2_396m_clk)) {
208 dev_err(cpu_dev, "failed to get clocks\n");
209 ret = -ENOENT;
210 goto put_node;
211 }
212
213 arm_reg = devm_regulator_get(cpu_dev, "arm");
214 pu_reg = devm_regulator_get(cpu_dev, "pu");
215 soc_reg = devm_regulator_get(cpu_dev, "soc");
Wei Yongjun3a3656d2013-02-22 04:39:30 +0000216 if (IS_ERR(arm_reg) || IS_ERR(pu_reg) || IS_ERR(soc_reg)) {
Shawn Guo1dd538f2013-02-04 05:46:29 +0000217 dev_err(cpu_dev, "failed to get regulators\n");
218 ret = -ENOENT;
219 goto put_node;
220 }
221
222 /* We expect an OPP table supplied by platform */
223 num = opp_get_opp_count(cpu_dev);
224 if (num < 0) {
225 ret = num;
226 dev_err(cpu_dev, "no OPP table is found: %d\n", ret);
227 goto put_node;
228 }
229
230 ret = opp_init_cpufreq_table(cpu_dev, &freq_table);
231 if (ret) {
232 dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
233 goto put_node;
234 }
235
236 if (of_property_read_u32(np, "clock-latency", &transition_latency))
237 transition_latency = CPUFREQ_ETERNAL;
238
239 /*
240 * OPP is maintained in order of increasing frequency, and
241 * freq_table initialised from OPP is therefore sorted in the
242 * same order.
243 */
244 rcu_read_lock();
245 opp = opp_find_freq_exact(cpu_dev,
246 freq_table[0].frequency * 1000, true);
247 min_volt = opp_get_voltage(opp);
248 opp = opp_find_freq_exact(cpu_dev,
249 freq_table[--num].frequency * 1000, true);
250 max_volt = opp_get_voltage(opp);
251 rcu_read_unlock();
252 ret = regulator_set_voltage_time(arm_reg, min_volt, max_volt);
253 if (ret > 0)
254 transition_latency += ret * 1000;
255
256 /* Count vddpu and vddsoc latency in for 1.2 GHz support */
257 if (freq_table[num].frequency == FREQ_1P2_GHZ / 1000) {
258 ret = regulator_set_voltage_time(pu_reg, PU_SOC_VOLTAGE_NORMAL,
259 PU_SOC_VOLTAGE_HIGH);
260 if (ret > 0)
261 transition_latency += ret * 1000;
262 ret = regulator_set_voltage_time(soc_reg, PU_SOC_VOLTAGE_NORMAL,
263 PU_SOC_VOLTAGE_HIGH);
264 if (ret > 0)
265 transition_latency += ret * 1000;
266 }
267
268 ret = cpufreq_register_driver(&imx6q_cpufreq_driver);
269 if (ret) {
270 dev_err(cpu_dev, "failed register driver: %d\n", ret);
271 goto free_freq_table;
272 }
273
274 of_node_put(np);
275 return 0;
276
277free_freq_table:
278 opp_free_cpufreq_table(cpu_dev, &freq_table);
279put_node:
280 of_node_put(np);
281 return ret;
282}
283
284static int imx6q_cpufreq_remove(struct platform_device *pdev)
285{
286 cpufreq_unregister_driver(&imx6q_cpufreq_driver);
287 opp_free_cpufreq_table(cpu_dev, &freq_table);
288
289 return 0;
290}
291
292static struct platform_driver imx6q_cpufreq_platdrv = {
293 .driver = {
294 .name = "imx6q-cpufreq",
295 .owner = THIS_MODULE,
296 },
297 .probe = imx6q_cpufreq_probe,
298 .remove = imx6q_cpufreq_remove,
299};
300module_platform_driver(imx6q_cpufreq_platdrv);
301
302MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
303MODULE_DESCRIPTION("Freescale i.MX6Q cpufreq driver");
304MODULE_LICENSE("GPL");