blob: 87a98ec77773a965078861a84670e70d37697864 [file] [log] [blame]
Sudeep Holla8def3102015-03-30 10:59:52 +01001/*
2 * System Control and Power Interface (SCPI) based CPUFreq Interface driver
3 *
4 * It provides necessary ops to arm_big_little cpufreq driver.
5 *
6 * Copyright (C) 2015 ARM Ltd.
7 * Sudeep Holla <sudeep.holla@arm.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
14 * kind, whether express or implied; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Sudeep Holla343a8d172018-01-10 16:44:15 +000021#include <linux/clk.h>
Sudeep Hollad9975b02016-05-03 15:05:05 +010022#include <linux/cpu.h>
Sudeep Holla8def3102015-03-30 10:59:52 +010023#include <linux/cpufreq.h>
Sudeep Holla343a8d172018-01-10 16:44:15 +000024#include <linux/cpumask.h>
25#include <linux/cpu_cooling.h>
26#include <linux/export.h>
Sudeep Holla8def3102015-03-30 10:59:52 +010027#include <linux/module.h>
Sudeep Holla343a8d172018-01-10 16:44:15 +000028#include <linux/of_platform.h>
Sudeep Holla8def3102015-03-30 10:59:52 +010029#include <linux/pm_opp.h>
30#include <linux/scpi_protocol.h>
Sudeep Holla343a8d172018-01-10 16:44:15 +000031#include <linux/slab.h>
Sudeep Holla8def3102015-03-30 10:59:52 +010032#include <linux/types.h>
33
Sudeep Holla343a8d172018-01-10 16:44:15 +000034struct scpi_data {
35 struct clk *clk;
36 struct device *cpu_dev;
37 struct thermal_cooling_device *cdev;
38};
Sudeep Holla8def3102015-03-30 10:59:52 +010039
40static struct scpi_ops *scpi_ops;
41
Sudeep Holla343a8d172018-01-10 16:44:15 +000042static unsigned int scpi_cpufreq_get_rate(unsigned int cpu)
Sudeep Holla8def3102015-03-30 10:59:52 +010043{
Sudeep Holla343a8d172018-01-10 16:44:15 +000044 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
45 struct scpi_data *priv = policy->driver_data;
46 unsigned long rate = clk_get_rate(priv->clk);
47
48 return rate / 1000;
Sudeep Holla8def3102015-03-30 10:59:52 +010049}
50
Sudeep Holla343a8d172018-01-10 16:44:15 +000051static int
52scpi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index)
53{
Dietmar Eggemann9326fdf2018-02-26 13:11:03 +000054 unsigned long freq = policy->freq_table[index].frequency;
Sudeep Holla343a8d172018-01-10 16:44:15 +000055 struct scpi_data *priv = policy->driver_data;
Dietmar Eggemann9326fdf2018-02-26 13:11:03 +000056 u64 rate = freq * 1000;
Sudeep Holla343a8d172018-01-10 16:44:15 +000057 int ret;
58
59 ret = clk_set_rate(priv->clk, rate);
Sudeep Holla343a8d172018-01-10 16:44:15 +000060
Dietmar Eggemann9326fdf2018-02-26 13:11:03 +000061 if (ret)
62 return ret;
63
64 if (clk_get_rate(priv->clk) != rate)
65 return -EIO;
66
67 arch_set_freq_scale(policy->related_cpus, freq,
68 policy->cpuinfo.max_freq);
69
70 return 0;
Sudeep Holla343a8d172018-01-10 16:44:15 +000071}
72
73static int
74scpi_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
75{
76 int cpu, domain, tdomain;
77 struct device *tcpu_dev;
78
79 domain = scpi_ops->device_domain_id(cpu_dev);
80 if (domain < 0)
81 return domain;
82
83 for_each_possible_cpu(cpu) {
84 if (cpu == cpu_dev->id)
85 continue;
86
87 tcpu_dev = get_cpu_device(cpu);
88 if (!tcpu_dev)
89 continue;
90
91 tdomain = scpi_ops->device_domain_id(tcpu_dev);
92 if (tdomain == domain)
93 cpumask_set_cpu(cpu, cpumask);
94 }
95
96 return 0;
97}
98
99static int scpi_cpufreq_init(struct cpufreq_policy *policy)
Sudeep Holla8def3102015-03-30 10:59:52 +0100100{
Sudeep Hollac0f2e212017-04-27 15:20:37 +0100101 int ret;
Sudeep Holla343a8d172018-01-10 16:44:15 +0000102 unsigned int latency;
103 struct device *cpu_dev;
104 struct scpi_data *priv;
105 struct cpufreq_frequency_table *freq_table;
106
107 cpu_dev = get_cpu_device(policy->cpu);
108 if (!cpu_dev) {
109 pr_err("failed to get cpu%d device\n", policy->cpu);
110 return -ENODEV;
111 }
Sudeep Holla8def3102015-03-30 10:59:52 +0100112
Sudeep Hollac0f2e212017-04-27 15:20:37 +0100113 ret = scpi_ops->add_opps_to_device(cpu_dev);
114 if (ret) {
115 dev_warn(cpu_dev, "failed to add opps to the device\n");
116 return ret;
Sudeep Hollad9975b02016-05-03 15:05:05 +0100117 }
118
Sudeep Holla343a8d172018-01-10 16:44:15 +0000119 ret = scpi_get_sharing_cpus(cpu_dev, policy->cpus);
120 if (ret) {
121 dev_warn(cpu_dev, "failed to get sharing cpumask\n");
122 return ret;
123 }
124
125 ret = dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus);
126 if (ret) {
Sudeep Hollad9975b02016-05-03 15:05:05 +0100127 dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
128 __func__, ret);
Sudeep Holla343a8d172018-01-10 16:44:15 +0000129 return ret;
130 }
131
132 ret = dev_pm_opp_get_opp_count(cpu_dev);
133 if (ret <= 0) {
134 dev_dbg(cpu_dev, "OPP table is not ready, deferring probe\n");
135 ret = -EPROBE_DEFER;
136 goto out_free_opp;
137 }
138
139 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
140 if (!priv) {
141 ret = -ENOMEM;
142 goto out_free_opp;
143 }
144
145 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
146 if (ret) {
147 dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
148 goto out_free_priv;
149 }
150
151 priv->cpu_dev = cpu_dev;
152 priv->clk = clk_get(cpu_dev, NULL);
153 if (IS_ERR(priv->clk)) {
154 dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d\n",
155 __func__, cpu_dev->id);
Wei Yongjun07253902018-01-23 02:09:02 +0000156 ret = PTR_ERR(priv->clk);
Sudeep Holla343a8d172018-01-10 16:44:15 +0000157 goto out_free_cpufreq_table;
158 }
159
160 policy->driver_data = priv;
Viresh Kumar472ada62018-02-26 10:39:04 +0530161 policy->freq_table = freq_table;
Sudeep Holla343a8d172018-01-10 16:44:15 +0000162
163 /* scpi allows DVFS request for any domain from any CPU */
164 policy->dvfs_possible_from_any_cpu = true;
165
166 latency = scpi_ops->get_transition_latency(cpu_dev);
167 if (!latency)
168 latency = CPUFREQ_ETERNAL;
169
170 policy->cpuinfo.transition_latency = latency;
171
172 policy->fast_switch_possible = false;
173 return 0;
174
Sudeep Holla343a8d172018-01-10 16:44:15 +0000175out_free_cpufreq_table:
176 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
177out_free_priv:
178 kfree(priv);
179out_free_opp:
180 dev_pm_opp_cpumask_remove_table(policy->cpus);
181
Sudeep Hollad9975b02016-05-03 15:05:05 +0100182 return ret;
Sudeep Holla8def3102015-03-30 10:59:52 +0100183}
184
Sudeep Holla343a8d172018-01-10 16:44:15 +0000185static int scpi_cpufreq_exit(struct cpufreq_policy *policy)
186{
187 struct scpi_data *priv = policy->driver_data;
188
189 cpufreq_cooling_unregister(priv->cdev);
190 clk_put(priv->clk);
191 dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
192 kfree(priv);
193 dev_pm_opp_cpumask_remove_table(policy->related_cpus);
194
195 return 0;
196}
197
198static void scpi_cpufreq_ready(struct cpufreq_policy *policy)
199{
200 struct scpi_data *priv = policy->driver_data;
Sudeep Holla343a8d172018-01-10 16:44:15 +0000201
Sudeep Hollaeb85c502018-01-22 14:41:07 +0000202 priv->cdev = of_cpufreq_cooling_register(policy);
Sudeep Holla343a8d172018-01-10 16:44:15 +0000203}
204
205static struct cpufreq_driver scpi_cpufreq_driver = {
206 .name = "scpi-cpufreq",
207 .flags = CPUFREQ_STICKY | CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
208 CPUFREQ_NEED_INITIAL_FREQ_CHECK,
209 .verify = cpufreq_generic_frequency_table_verify,
210 .attr = cpufreq_generic_attr,
211 .get = scpi_cpufreq_get_rate,
212 .init = scpi_cpufreq_init,
213 .exit = scpi_cpufreq_exit,
214 .ready = scpi_cpufreq_ready,
215 .target_index = scpi_cpufreq_set_target,
Sudeep Holla8def3102015-03-30 10:59:52 +0100216};
217
218static int scpi_cpufreq_probe(struct platform_device *pdev)
219{
Sudeep Holla343a8d172018-01-10 16:44:15 +0000220 int ret;
221
Sudeep Holla8def3102015-03-30 10:59:52 +0100222 scpi_ops = get_scpi_ops();
223 if (!scpi_ops)
224 return -EIO;
225
Sudeep Holla343a8d172018-01-10 16:44:15 +0000226 ret = cpufreq_register_driver(&scpi_cpufreq_driver);
227 if (ret)
228 dev_err(&pdev->dev, "%s: registering cpufreq failed, err: %d\n",
229 __func__, ret);
230 return ret;
Sudeep Holla8def3102015-03-30 10:59:52 +0100231}
232
233static int scpi_cpufreq_remove(struct platform_device *pdev)
234{
Sudeep Holla343a8d172018-01-10 16:44:15 +0000235 cpufreq_unregister_driver(&scpi_cpufreq_driver);
Sudeep Holla8def3102015-03-30 10:59:52 +0100236 scpi_ops = NULL;
237 return 0;
238}
239
240static struct platform_driver scpi_cpufreq_platdrv = {
241 .driver = {
242 .name = "scpi-cpufreq",
Sudeep Holla8def3102015-03-30 10:59:52 +0100243 },
244 .probe = scpi_cpufreq_probe,
245 .remove = scpi_cpufreq_remove,
246};
247module_platform_driver(scpi_cpufreq_platdrv);
248
249MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
250MODULE_DESCRIPTION("ARM SCPI CPUFreq interface driver");
251MODULE_LICENSE("GPL v2");