1 From dd77db4143290689d3a5e1ec61627233d0711b66 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] FROMLIST: cpufreq: Add a cpufreq-krait based on cpufreq-cpu0
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.
11 TODO: Merge into cpufreq-cpu0? Or make generic?
13 Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
16 drivers/cpufreq/Kconfig | 13 +++
17 drivers/cpufreq/Makefile | 1 +
18 drivers/cpufreq/cpufreq-krait.c | 190 ++++++++++++++++++++++++++++++++++++++++
19 3 files changed, 204 insertions(+)
20 create mode 100644 drivers/cpufreq/cpufreq-krait.c
22 --- a/drivers/cpufreq/Kconfig
23 +++ b/drivers/cpufreq/Kconfig
24 @@ -198,6 +198,19 @@ config CPUFREQ_DT
28 +config GENERIC_CPUFREQ_KRAIT
29 + tristate "Krait cpufreq driver"
30 + depends on HAVE_CLK && OF
31 + # if CPU_THERMAL is on and THERMAL=m, CPU0 cannot be =y:
32 + depends on !CPU_THERMAL || THERMAL
35 + This adds a generic cpufreq driver for CPU0 frequency management.
36 + It supports both uniprocessor (UP) and symmetric multiprocessor (SMP)
37 + systems which share clock and voltage across all CPUs.
42 source "drivers/cpufreq/Kconfig.x86"
44 --- a/drivers/cpufreq/Makefile
45 +++ b/drivers/cpufreq/Makefile
46 @@ -13,6 +13,7 @@ obj-$(CONFIG_CPU_FREQ_GOV_CONSERVATIVE)
47 obj-$(CONFIG_CPU_FREQ_GOV_COMMON) += cpufreq_governor.o
49 obj-$(CONFIG_CPUFREQ_DT) += cpufreq-dt.o
50 +obj-$(CONFIG_GENERIC_CPUFREQ_KRAIT) += cpufreq-krait.o
52 ##################################################################################
55 +++ b/drivers/cpufreq/cpufreq-krait.c
58 + * Copyright (C) 2012 Freescale Semiconductor, Inc.
59 + * Copyright (c) 2014, The Linux Foundation. All rights reserved.
61 + * The OPP code in function krait_set_target() is reused from
62 + * drivers/cpufreq/omap-cpufreq.c
64 + * This program is free software; you can redistribute it and/or modify
65 + * it under the terms of the GNU General Public License version 2 as
66 + * published by the Free Software Foundation.
69 +#include <linux/clk.h>
70 +#include <linux/cpu.h>
71 +#include <linux/cpu_cooling.h>
72 +#include <linux/cpufreq.h>
73 +#include <linux/cpumask.h>
74 +#include <linux/err.h>
75 +#include <linux/module.h>
76 +#include <linux/of.h>
77 +#include <linux/pm_opp.h>
78 +#include <linux/platform_device.h>
79 +#include <linux/regulator/consumer.h>
80 +#include <linux/slab.h>
81 +#include <linux/thermal.h>
83 +static unsigned int transition_latency;
84 +static unsigned int voltage_tolerance; /* in percentage */
86 +static struct device *cpu_dev;
87 +static DEFINE_PER_CPU(struct clk *, krait_cpu_clks);
88 +static DEFINE_PER_CPU(struct regulator *, krait_supply_core);
89 +static struct cpufreq_frequency_table *freq_table;
90 +static struct thermal_cooling_device *cdev;
92 +struct cache_points {
93 + unsigned long cache_freq;
94 + unsigned int cache_volt;
95 + unsigned long cpu_freq;
98 +static struct regulator *krait_l2_reg;
99 +static struct clk *krait_l2_clk;
100 +static struct cache_points *krait_l2_points;
101 +static int nr_krait_l2_points;
103 +static int krait_parse_cache_points(struct device *dev,
104 + struct device_node *of_node)
106 + const struct property *prop;
110 + prop = of_find_property(of_node, "cache-points-kHz", NULL);
117 + * Each OPP is a set of tuples consisting of frequency and
118 + * cpu-frequency like <freq-kHz volt-uV freq-kHz>.
120 + nr = prop->length / sizeof(u32);
122 + dev_err(dev, "%s: Invalid cache points\n", __func__);
127 + krait_l2_points = devm_kcalloc(dev, nr, sizeof(*krait_l2_points),
129 + if (!krait_l2_points)
131 + nr_krait_l2_points = nr;
133 + for (i = 0, val = prop->value; i < nr; i++) {
134 + unsigned long cache_freq = be32_to_cpup(val++) * 1000;
135 + unsigned int cache_volt = be32_to_cpup(val++);
136 + unsigned long cpu_freq = be32_to_cpup(val++) * 1000;
138 + krait_l2_points[i].cache_freq = cache_freq;
139 + krait_l2_points[i].cache_volt = cache_volt;
140 + krait_l2_points[i].cpu_freq = cpu_freq;
146 +static int krait_set_target(struct cpufreq_policy *policy, unsigned int index)
148 + struct dev_pm_opp *opp;
149 + unsigned long volt = 0, volt_old = 0, tol = 0;
150 + unsigned long freq, max_cpu_freq = 0;
151 + unsigned int old_freq, new_freq;
152 + long freq_Hz, freq_exact;
154 + struct clk *cpu_clk;
155 + struct regulator *core;
158 + cpu_clk = per_cpu(krait_cpu_clks, policy->cpu);
160 + freq_Hz = clk_round_rate(cpu_clk, freq_table[index].frequency * 1000);
162 + freq_Hz = freq_table[index].frequency * 1000;
164 + freq_exact = freq_Hz;
165 + new_freq = freq_Hz / 1000;
166 + old_freq = clk_get_rate(cpu_clk) / 1000;
168 + core = per_cpu(krait_supply_core, policy->cpu);
171 + opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_Hz);
174 + pr_err("failed to find OPP for %ld\n", freq_Hz);
175 + return PTR_ERR(opp);
177 + volt = dev_pm_opp_get_voltage(opp);
179 + tol = volt * voltage_tolerance / 100;
180 + volt_old = regulator_get_voltage(core);
182 + pr_debug("%u MHz, %ld mV --> %u MHz, %ld mV\n",
183 + old_freq / 1000, volt_old ? volt_old / 1000 : -1,
184 + new_freq / 1000, volt ? volt / 1000 : -1);
186 + /* scaling up? scale voltage before frequency */
187 + if (new_freq > old_freq) {
188 + ret = regulator_set_voltage_tol(core, volt, tol);
190 + pr_err("failed to scale voltage up: %d\n", ret);
195 + ret = clk_set_rate(cpu_clk, freq_exact);
197 + pr_err("failed to set clock rate: %d\n", ret);
201 + /* scaling down? scale voltage after frequency */
202 + if (new_freq < old_freq) {
203 + ret = regulator_set_voltage_tol(core, volt, tol);
205 + pr_err("failed to scale voltage down: %d\n", ret);
206 + clk_set_rate(cpu_clk, old_freq * 1000);
210 + for_each_possible_cpu(cpu) {
211 + freq = clk_get_rate(per_cpu(krait_cpu_clks, cpu));
212 + max_cpu_freq = max(max_cpu_freq, freq);
215 + for (i = 0; i < nr_krait_l2_points; i++) {
216 + if (max_cpu_freq >= krait_l2_points[i].cpu_freq) {
217 + if (krait_l2_reg) {
218 + ret = regulator_set_voltage_tol(krait_l2_reg,
219 + krait_l2_points[i].cache_volt,
222 + pr_err("failed to scale l2 voltage: %d\n",
226 + ret = clk_set_rate(krait_l2_clk,
227 + krait_l2_points[i].cache_freq);
229 + pr_err("failed to scale l2 clk: %d\n", ret);
238 +static int krait_cpufreq_init(struct cpufreq_policy *policy)
242 + policy->clk = per_cpu(krait_cpu_clks, policy->cpu);
244 + ret = cpufreq_table_validate_and_show(policy, freq_table);
246 + pr_err("%s: invalid frequency table: %d\n", __func__, ret);
250 + policy->cpuinfo.transition_latency = transition_latency;
255 +static struct cpufreq_driver krait_cpufreq_driver = {
256 + .flags = CPUFREQ_STICKY,
257 + .verify = cpufreq_generic_frequency_table_verify,
258 + .target_index = krait_set_target,
259 + .get = cpufreq_generic_get,
260 + .init = krait_cpufreq_init,
261 + .name = "generic_krait",
262 + .attr = cpufreq_generic_attr,
265 +static int krait_cpufreq_probe(struct platform_device *pdev)
267 + struct device_node *np, *cache;
270 + struct device *dev;
272 + struct regulator *core;
273 + unsigned long freq_Hz, freq, max_cpu_freq = 0;
274 + struct dev_pm_opp *opp;
275 + unsigned long volt, tol;
277 + cpu_dev = get_cpu_device(0);
279 + pr_err("failed to get krait device\n");
283 + np = of_node_get(cpu_dev->of_node);
285 + pr_err("failed to find krait node\n");
289 + ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
291 + pr_err("failed to init cpufreq table: %d\n", ret);
295 + of_property_read_u32(np, "voltage-tolerance", &voltage_tolerance);
297 + if (of_property_read_u32(np, "clock-latency", &transition_latency))
298 + transition_latency = CPUFREQ_ETERNAL;
300 + cache = of_find_next_cache_node(np);
302 + struct device_node *vdd;
304 + vdd = of_parse_phandle(cache, "vdd_dig-supply", 0);
306 + krait_l2_reg = regulator_get(NULL, vdd->name);
307 + if (IS_ERR(krait_l2_reg)) {
308 + pr_warn("failed to get l2 vdd_dig supply\n");
309 + krait_l2_reg = NULL;
314 + krait_l2_clk = of_clk_get(cache, 0);
315 + if (!IS_ERR(krait_l2_clk)) {
316 + ret = krait_parse_cache_points(&pdev->dev, cache);
318 + clk_put(krait_l2_clk);
320 + if (IS_ERR(krait_l2_clk) || ret)
321 + krait_l2_clk = NULL;
324 + for_each_possible_cpu(cpu) {
325 + dev = get_cpu_device(cpu);
327 + pr_err("failed to get krait device\n");
329 + goto out_free_table;
331 + per_cpu(krait_cpu_clks, cpu) = clk = devm_clk_get(dev, NULL);
333 + ret = PTR_ERR(clk);
334 + goto out_free_table;
336 + core = devm_regulator_get(dev, "core");
337 + if (IS_ERR(core)) {
338 + pr_debug("failed to get core regulator\n");
339 + ret = PTR_ERR(core);
340 + goto out_free_table;
342 + per_cpu(krait_supply_core, cpu) = core;
344 + freq = freq_Hz = clk_get_rate(clk);
347 + opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_Hz);
350 + pr_err("failed to find OPP for %ld\n", freq_Hz);
351 + ret = PTR_ERR(opp);
352 + goto out_free_table;
354 + volt = dev_pm_opp_get_voltage(opp);
357 + tol = volt * voltage_tolerance / 100;
358 + ret = regulator_set_voltage_tol(core, volt, tol);
360 + pr_err("failed to scale voltage up: %d\n", ret);
361 + goto out_free_table;
363 + ret = regulator_enable(core);
365 + pr_err("failed to enable regulator: %d\n", ret);
366 + goto out_free_table;
368 + max_cpu_freq = max(max_cpu_freq, freq);
371 + for (i = 0; i < nr_krait_l2_points; i++) {
372 + if (max_cpu_freq >= krait_l2_points[i].cpu_freq) {
373 + if (krait_l2_reg) {
374 + ret = regulator_set_voltage_tol(krait_l2_reg,
375 + krait_l2_points[i].cache_volt,
378 + pr_err("failed to scale l2 voltage: %d\n",
380 + ret = regulator_enable(krait_l2_reg);
382 + pr_err("failed to enable l2 voltage: %d\n",
390 + ret = cpufreq_register_driver(&krait_cpufreq_driver);
392 + pr_err("failed register driver: %d\n", ret);
393 + goto out_free_table;
398 + * For now, just loading the cooling device;
399 + * thermal DT code takes care of matching them.
401 + for_each_possible_cpu(cpu) {
402 + dev = get_cpu_device(cpu);
403 + np = of_node_get(dev->of_node);
404 + if (of_find_property(np, "#cooling-cells", NULL)) {
405 + cdev = of_cpufreq_cooling_register(np, cpumask_of(cpu));
407 + pr_err("running cpufreq without cooling device: %ld\n",
416 + regulator_put(krait_l2_reg);
417 + clk_put(krait_l2_clk);
418 + dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
424 +static int krait_cpufreq_remove(struct platform_device *pdev)
426 + cpufreq_cooling_unregister(cdev);
427 + cpufreq_unregister_driver(&krait_cpufreq_driver);
428 + dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
429 + clk_put(krait_l2_clk);
430 + regulator_put(krait_l2_reg);
435 +static struct platform_driver krait_cpufreq_platdrv = {
437 + .name = "cpufreq-krait",
438 + .owner = THIS_MODULE,
440 + .probe = krait_cpufreq_probe,
441 + .remove = krait_cpufreq_remove,
443 +module_platform_driver(krait_cpufreq_platdrv);
445 +MODULE_DESCRIPTION("Krait CPUfreq driver");
446 +MODULE_LICENSE("GPL v2");
447 --- a/drivers/cpufreq/qcom-cpufreq.c
448 +++ b/drivers/cpufreq/qcom-cpufreq.c
449 @@ -168,11 +168,8 @@ static int __init qcom_cpufreq_populate_
451 static int __init qcom_cpufreq_driver_init(void)
453 - struct cpufreq_dt_platform_data pdata = { .independent_clocks = true };
454 struct platform_device_info devinfo = {
455 - .name = "cpufreq-dt",
457 - .size_data = sizeof(pdata),
458 + .name = "cpufreq-krait",
460 struct device *cpu_dev;
461 struct device_node *np;