3150a4dfb86fe953a7e2b88c96ad26983f1a7ba7
[openwrt/openwrt.git] / target / linux / ipq806x / patches / 0172-cpufreq-Add-a-cpufreq-krait-based-on-cpufreq-cpu0.patch
1 From 5cf343c60d7557aefb468603065cac5062f11b8d Mon Sep 17 00:00:00 2001
2 From: Stephen Boyd <sboyd@codeaurora.org>
3 Date: Fri, 30 May 2014 16:36:11 -0700
4 Subject: [PATCH 172/182] cpufreq: Add a cpufreq-krait based on cpufreq-cpu0
5
6 Krait processors have individual clocks for each CPU that can
7 scale independently from one another. cpufreq-cpu0 is fairly
8 close to this, but assumes that there is only one clock for all
9 CPUs. Add a driver to support the Krait configuration.
10
11 TODO: Merge into cpufreq-cpu0? Or make generic?
12
13 Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
14 ---
15 drivers/cpufreq/Kconfig | 13 +++
16 drivers/cpufreq/Makefile | 1 +
17 drivers/cpufreq/cpufreq-krait.c | 190 +++++++++++++++++++++++++++++++++++++++
18 3 files changed, 204 insertions(+)
19 create mode 100644 drivers/cpufreq/cpufreq-krait.c
20
21 diff --git a/drivers/cpufreq/Kconfig b/drivers/cpufreq/Kconfig
22 index 4b029c0..4051528 100644
23 --- a/drivers/cpufreq/Kconfig
24 +++ b/drivers/cpufreq/Kconfig
25 @@ -194,6 +194,19 @@ config GENERIC_CPUFREQ_CPU0
26
27 If in doubt, say N.
28
29 +config GENERIC_CPUFREQ_KRAIT
30 + tristate "Krait cpufreq driver"
31 + depends on HAVE_CLK && OF
32 + # if CPU_THERMAL is on and THERMAL=m, CPU0 cannot be =y:
33 + depends on !CPU_THERMAL || THERMAL
34 + select PM_OPP
35 + help
36 + This adds a generic cpufreq driver for CPU0 frequency management.
37 + It supports both uniprocessor (UP) and symmetric multiprocessor (SMP)
38 + systems which share clock and voltage across all CPUs.
39 +
40 + If in doubt, say N.
41 +
42 menu "x86 CPU frequency scaling drivers"
43 depends on X86
44 source "drivers/cpufreq/Kconfig.x86"
45 diff --git a/drivers/cpufreq/Makefile b/drivers/cpufreq/Makefile
46 index 7494565..f6f4485 100644
47 --- a/drivers/cpufreq/Makefile
48 +++ b/drivers/cpufreq/Makefile
49 @@ -12,6 +12,7 @@ obj-$(CONFIG_CPU_FREQ_GOV_CONSERVATIVE) += cpufreq_conservative.o
50 obj-$(CONFIG_CPU_FREQ_GOV_COMMON) += cpufreq_governor.o
51
52 obj-$(CONFIG_GENERIC_CPUFREQ_CPU0) += cpufreq-cpu0.o
53 +obj-$(CONFIG_GENERIC_CPUFREQ_KRAIT) += cpufreq-krait.o
54
55 ##################################################################################
56 # x86 drivers.
57 diff --git a/drivers/cpufreq/cpufreq-krait.c b/drivers/cpufreq/cpufreq-krait.c
58 new file mode 100644
59 index 0000000..7b38b9c
60 --- /dev/null
61 +++ b/drivers/cpufreq/cpufreq-krait.c
62 @@ -0,0 +1,190 @@
63 +/*
64 + * Copyright (C) 2012 Freescale Semiconductor, Inc.
65 + * Copyright (c) 2014, The Linux Foundation. All rights reserved.
66 + *
67 + * The OPP code in function krait_set_target() is reused from
68 + * drivers/cpufreq/omap-cpufreq.c
69 + *
70 + * This program is free software; you can redistribute it and/or modify
71 + * it under the terms of the GNU General Public License version 2 as
72 + * published by the Free Software Foundation.
73 + */
74 +
75 +#include <linux/clk.h>
76 +#include <linux/cpu.h>
77 +#include <linux/cpu_cooling.h>
78 +#include <linux/cpufreq.h>
79 +#include <linux/cpumask.h>
80 +#include <linux/err.h>
81 +#include <linux/module.h>
82 +#include <linux/of.h>
83 +#include <linux/pm_opp.h>
84 +#include <linux/platform_device.h>
85 +#include <linux/slab.h>
86 +#include <linux/thermal.h>
87 +
88 +static unsigned int transition_latency;
89 +
90 +static struct device *cpu_dev;
91 +static DEFINE_PER_CPU(struct clk *, krait_cpu_clks);
92 +static struct cpufreq_frequency_table *freq_table;
93 +static struct thermal_cooling_device *cdev;
94 +
95 +static int krait_set_target(struct cpufreq_policy *policy, unsigned int index)
96 +{
97 + unsigned long volt = 0, volt_old = 0;
98 + unsigned int old_freq, new_freq;
99 + long freq_Hz, freq_exact;
100 + int ret;
101 + struct clk *cpu_clk;
102 +
103 + cpu_clk = per_cpu(krait_cpu_clks, policy->cpu);
104 +
105 + freq_Hz = clk_round_rate(cpu_clk, freq_table[index].frequency * 1000);
106 + if (freq_Hz <= 0)
107 + freq_Hz = freq_table[index].frequency * 1000;
108 +
109 + freq_exact = freq_Hz;
110 + new_freq = freq_Hz / 1000;
111 + old_freq = clk_get_rate(cpu_clk) / 1000;
112 +
113 + pr_debug("%u MHz, %ld mV --> %u MHz, %ld mV\n",
114 + old_freq / 1000, volt_old ? volt_old / 1000 : -1,
115 + new_freq / 1000, volt ? volt / 1000 : -1);
116 +
117 + ret = clk_set_rate(cpu_clk, freq_exact);
118 + if (ret)
119 + pr_err("failed to set clock rate: %d\n", ret);
120 +
121 + return ret;
122 +}
123 +
124 +static int krait_cpufreq_init(struct cpufreq_policy *policy)
125 +{
126 + int ret;
127 +
128 + policy->clk = per_cpu(krait_cpu_clks, policy->cpu);
129 +
130 + ret = cpufreq_table_validate_and_show(policy, freq_table);
131 + if (ret) {
132 + pr_err("%s: invalid frequency table: %d\n", __func__, ret);
133 + return ret;
134 + }
135 +
136 + policy->cpuinfo.transition_latency = transition_latency;
137 +
138 + return 0;
139 +}
140 +
141 +static struct cpufreq_driver krait_cpufreq_driver = {
142 + .flags = CPUFREQ_STICKY,
143 + .verify = cpufreq_generic_frequency_table_verify,
144 + .target_index = krait_set_target,
145 + .get = cpufreq_generic_get,
146 + .init = krait_cpufreq_init,
147 + .name = "generic_krait",
148 + .attr = cpufreq_generic_attr,
149 +};
150 +
151 +static int krait_cpufreq_probe(struct platform_device *pdev)
152 +{
153 + struct device_node *np;
154 + int ret;
155 + unsigned int cpu;
156 + struct device *dev;
157 + struct clk *clk;
158 +
159 + cpu_dev = get_cpu_device(0);
160 + if (!cpu_dev) {
161 + pr_err("failed to get krait device\n");
162 + return -ENODEV;
163 + }
164 +
165 + np = of_node_get(cpu_dev->of_node);
166 + if (!np) {
167 + pr_err("failed to find krait node\n");
168 + return -ENOENT;
169 + }
170 +
171 + for_each_possible_cpu(cpu) {
172 + dev = get_cpu_device(cpu);
173 + if (!dev) {
174 + pr_err("failed to get krait device\n");
175 + ret = -ENOENT;
176 + goto out_put_node;
177 + }
178 + per_cpu(krait_cpu_clks, cpu) = clk = devm_clk_get(dev, NULL);
179 + if (IS_ERR(clk)) {
180 + ret = PTR_ERR(clk);
181 + goto out_put_node;
182 + }
183 + }
184 +
185 + ret = of_init_opp_table(cpu_dev);
186 + if (ret) {
187 + pr_err("failed to init OPP table: %d\n", ret);
188 + goto out_put_node;
189 + }
190 +
191 + ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
192 + if (ret) {
193 + pr_err("failed to init cpufreq table: %d\n", ret);
194 + goto out_put_node;
195 + }
196 +
197 + if (of_property_read_u32(np, "clock-latency", &transition_latency))
198 + transition_latency = CPUFREQ_ETERNAL;
199 +
200 + ret = cpufreq_register_driver(&krait_cpufreq_driver);
201 + if (ret) {
202 + pr_err("failed register driver: %d\n", ret);
203 + goto out_free_table;
204 + }
205 + of_node_put(np);
206 +
207 + /*
208 + * For now, just loading the cooling device;
209 + * thermal DT code takes care of matching them.
210 + */
211 + for_each_possible_cpu(cpu) {
212 + dev = get_cpu_device(cpu);
213 + np = of_node_get(dev->of_node);
214 + if (of_find_property(np, "#cooling-cells", NULL)) {
215 + cdev = of_cpufreq_cooling_register(np, cpumask_of(cpu));
216 + if (IS_ERR(cdev))
217 + pr_err("running cpufreq without cooling device: %ld\n",
218 + PTR_ERR(cdev));
219 + }
220 + of_node_put(np);
221 + }
222 +
223 + return 0;
224 +
225 +out_free_table:
226 + dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
227 +out_put_node:
228 + of_node_put(np);
229 + return ret;
230 +}
231 +
232 +static int krait_cpufreq_remove(struct platform_device *pdev)
233 +{
234 + cpufreq_cooling_unregister(cdev);
235 + cpufreq_unregister_driver(&krait_cpufreq_driver);
236 + dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
237 +
238 + return 0;
239 +}
240 +
241 +static struct platform_driver krait_cpufreq_platdrv = {
242 + .driver = {
243 + .name = "cpufreq-krait",
244 + .owner = THIS_MODULE,
245 + },
246 + .probe = krait_cpufreq_probe,
247 + .remove = krait_cpufreq_remove,
248 +};
249 +module_platform_driver(krait_cpufreq_platdrv);
250 +
251 +MODULE_DESCRIPTION("Krait CPUfreq driver");
252 +MODULE_LICENSE("GPL v2");
253 --
254 1.7.10.4
255