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
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>
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
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
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
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.
42 menu "x86 CPU frequency scaling drivers"
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
52 obj-$(CONFIG_GENERIC_CPUFREQ_CPU0) += cpufreq-cpu0.o
53 +obj-$(CONFIG_GENERIC_CPUFREQ_KRAIT) += cpufreq-krait.o
55 ##################################################################################
57 diff --git a/drivers/cpufreq/cpufreq-krait.c b/drivers/cpufreq/cpufreq-krait.c
59 index 0000000..7b38b9c
61 +++ b/drivers/cpufreq/cpufreq-krait.c
64 + * Copyright (C) 2012 Freescale Semiconductor, Inc.
65 + * Copyright (c) 2014, The Linux Foundation. All rights reserved.
67 + * The OPP code in function krait_set_target() is reused from
68 + * drivers/cpufreq/omap-cpufreq.c
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.
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>
88 +static unsigned int transition_latency;
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;
95 +static int krait_set_target(struct cpufreq_policy *policy, unsigned int index)
97 + unsigned long volt = 0, volt_old = 0;
98 + unsigned int old_freq, new_freq;
99 + long freq_Hz, freq_exact;
101 + struct clk *cpu_clk;
103 + cpu_clk = per_cpu(krait_cpu_clks, policy->cpu);
105 + freq_Hz = clk_round_rate(cpu_clk, freq_table[index].frequency * 1000);
107 + freq_Hz = freq_table[index].frequency * 1000;
109 + freq_exact = freq_Hz;
110 + new_freq = freq_Hz / 1000;
111 + old_freq = clk_get_rate(cpu_clk) / 1000;
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);
117 + ret = clk_set_rate(cpu_clk, freq_exact);
119 + pr_err("failed to set clock rate: %d\n", ret);
124 +static int krait_cpufreq_init(struct cpufreq_policy *policy)
128 + policy->clk = per_cpu(krait_cpu_clks, policy->cpu);
130 + ret = cpufreq_table_validate_and_show(policy, freq_table);
132 + pr_err("%s: invalid frequency table: %d\n", __func__, ret);
136 + policy->cpuinfo.transition_latency = transition_latency;
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,
151 +static int krait_cpufreq_probe(struct platform_device *pdev)
153 + struct device_node *np;
156 + struct device *dev;
159 + cpu_dev = get_cpu_device(0);
161 + pr_err("failed to get krait device\n");
165 + np = of_node_get(cpu_dev->of_node);
167 + pr_err("failed to find krait node\n");
171 + for_each_possible_cpu(cpu) {
172 + dev = get_cpu_device(cpu);
174 + pr_err("failed to get krait device\n");
178 + per_cpu(krait_cpu_clks, cpu) = clk = devm_clk_get(dev, NULL);
180 + ret = PTR_ERR(clk);
185 + ret = of_init_opp_table(cpu_dev);
187 + pr_err("failed to init OPP table: %d\n", ret);
191 + ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
193 + pr_err("failed to init cpufreq table: %d\n", ret);
197 + if (of_property_read_u32(np, "clock-latency", &transition_latency))
198 + transition_latency = CPUFREQ_ETERNAL;
200 + ret = cpufreq_register_driver(&krait_cpufreq_driver);
202 + pr_err("failed register driver: %d\n", ret);
203 + goto out_free_table;
208 + * For now, just loading the cooling device;
209 + * thermal DT code takes care of matching them.
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));
217 + pr_err("running cpufreq without cooling device: %ld\n",
226 + dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
232 +static int krait_cpufreq_remove(struct platform_device *pdev)
234 + cpufreq_cooling_unregister(cdev);
235 + cpufreq_unregister_driver(&krait_cpufreq_driver);
236 + dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
241 +static struct platform_driver krait_cpufreq_platdrv = {
243 + .name = "cpufreq-krait",
244 + .owner = THIS_MODULE,
246 + .probe = krait_cpufreq_probe,
247 + .remove = krait_cpufreq_remove,
249 +module_platform_driver(krait_cpufreq_platdrv);
251 +MODULE_DESCRIPTION("Krait CPUfreq driver");
252 +MODULE_LICENSE("GPL v2");