blob: 247fcbfa4cb5b6c029035e49f1a4daf869668e71 [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{
54 struct scpi_data *priv = policy->driver_data;
55 u64 rate = policy->freq_table[index].frequency * 1000;
56 int ret;
57
58 ret = clk_set_rate(priv->clk, rate);
59 if (!ret && (clk_get_rate(priv->clk) != rate))
60 ret = -EIO;
61
62 return ret;
63}
64
65static int
66scpi_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
67{
68 int cpu, domain, tdomain;
69 struct device *tcpu_dev;
70
71 domain = scpi_ops->device_domain_id(cpu_dev);
72 if (domain < 0)
73 return domain;
74
75 for_each_possible_cpu(cpu) {
76 if (cpu == cpu_dev->id)
77 continue;
78
79 tcpu_dev = get_cpu_device(cpu);
80 if (!tcpu_dev)
81 continue;
82
83 tdomain = scpi_ops->device_domain_id(tcpu_dev);
84 if (tdomain == domain)
85 cpumask_set_cpu(cpu, cpumask);
86 }
87
88 return 0;
89}
90
91static int scpi_cpufreq_init(struct cpufreq_policy *policy)
Sudeep Holla8def3102015-03-30 10:59:52 +010092{
Sudeep Hollac0f2e212017-04-27 15:20:37 +010093 int ret;
Sudeep Holla343a8d172018-01-10 16:44:15 +000094 unsigned int latency;
95 struct device *cpu_dev;
96 struct scpi_data *priv;
97 struct cpufreq_frequency_table *freq_table;
98
99 cpu_dev = get_cpu_device(policy->cpu);
100 if (!cpu_dev) {
101 pr_err("failed to get cpu%d device\n", policy->cpu);
102 return -ENODEV;
103 }
Sudeep Holla8def3102015-03-30 10:59:52 +0100104
Sudeep Hollac0f2e212017-04-27 15:20:37 +0100105 ret = scpi_ops->add_opps_to_device(cpu_dev);
106 if (ret) {
107 dev_warn(cpu_dev, "failed to add opps to the device\n");
108 return ret;
Sudeep Hollad9975b02016-05-03 15:05:05 +0100109 }
110
Sudeep Holla343a8d172018-01-10 16:44:15 +0000111 ret = scpi_get_sharing_cpus(cpu_dev, policy->cpus);
112 if (ret) {
113 dev_warn(cpu_dev, "failed to get sharing cpumask\n");
114 return ret;
115 }
116
117 ret = dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus);
118 if (ret) {
Sudeep Hollad9975b02016-05-03 15:05:05 +0100119 dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
120 __func__, ret);
Sudeep Holla343a8d172018-01-10 16:44:15 +0000121 return ret;
122 }
123
124 ret = dev_pm_opp_get_opp_count(cpu_dev);
125 if (ret <= 0) {
126 dev_dbg(cpu_dev, "OPP table is not ready, deferring probe\n");
127 ret = -EPROBE_DEFER;
128 goto out_free_opp;
129 }
130
131 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
132 if (!priv) {
133 ret = -ENOMEM;
134 goto out_free_opp;
135 }
136
137 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
138 if (ret) {
139 dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
140 goto out_free_priv;
141 }
142
143 priv->cpu_dev = cpu_dev;
144 priv->clk = clk_get(cpu_dev, NULL);
145 if (IS_ERR(priv->clk)) {
146 dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d\n",
147 __func__, cpu_dev->id);
148 goto out_free_cpufreq_table;
149 }
150
151 policy->driver_data = priv;
152
153 ret = cpufreq_table_validate_and_show(policy, freq_table);
154 if (ret) {
155 dev_err(cpu_dev, "%s: invalid frequency table: %d\n", __func__,
156 ret);
157 goto out_put_clk;
158 }
159
160 /* scpi allows DVFS request for any domain from any CPU */
161 policy->dvfs_possible_from_any_cpu = true;
162
163 latency = scpi_ops->get_transition_latency(cpu_dev);
164 if (!latency)
165 latency = CPUFREQ_ETERNAL;
166
167 policy->cpuinfo.transition_latency = latency;
168
169 policy->fast_switch_possible = false;
170 return 0;
171
172out_put_clk:
173 clk_put(priv->clk);
174out_free_cpufreq_table:
175 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
176out_free_priv:
177 kfree(priv);
178out_free_opp:
179 dev_pm_opp_cpumask_remove_table(policy->cpus);
180
Sudeep Hollad9975b02016-05-03 15:05:05 +0100181 return ret;
Sudeep Holla8def3102015-03-30 10:59:52 +0100182}
183
Sudeep Holla343a8d172018-01-10 16:44:15 +0000184static int scpi_cpufreq_exit(struct cpufreq_policy *policy)
185{
186 struct scpi_data *priv = policy->driver_data;
187
188 cpufreq_cooling_unregister(priv->cdev);
189 clk_put(priv->clk);
190 dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
191 kfree(priv);
192 dev_pm_opp_cpumask_remove_table(policy->related_cpus);
193
194 return 0;
195}
196
197static void scpi_cpufreq_ready(struct cpufreq_policy *policy)
198{
199 struct scpi_data *priv = policy->driver_data;
200 struct thermal_cooling_device *cdev;
201
202 cdev = of_cpufreq_cooling_register(policy);
203 if (!IS_ERR(cdev))
204 priv->cdev = cdev;
205}
206
207static struct cpufreq_driver scpi_cpufreq_driver = {
208 .name = "scpi-cpufreq",
209 .flags = CPUFREQ_STICKY | CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
210 CPUFREQ_NEED_INITIAL_FREQ_CHECK,
211 .verify = cpufreq_generic_frequency_table_verify,
212 .attr = cpufreq_generic_attr,
213 .get = scpi_cpufreq_get_rate,
214 .init = scpi_cpufreq_init,
215 .exit = scpi_cpufreq_exit,
216 .ready = scpi_cpufreq_ready,
217 .target_index = scpi_cpufreq_set_target,
Sudeep Holla8def3102015-03-30 10:59:52 +0100218};
219
220static int scpi_cpufreq_probe(struct platform_device *pdev)
221{
Sudeep Holla343a8d172018-01-10 16:44:15 +0000222 int ret;
223
Sudeep Holla8def3102015-03-30 10:59:52 +0100224 scpi_ops = get_scpi_ops();
225 if (!scpi_ops)
226 return -EIO;
227
Sudeep Holla343a8d172018-01-10 16:44:15 +0000228 ret = cpufreq_register_driver(&scpi_cpufreq_driver);
229 if (ret)
230 dev_err(&pdev->dev, "%s: registering cpufreq failed, err: %d\n",
231 __func__, ret);
232 return ret;
Sudeep Holla8def3102015-03-30 10:59:52 +0100233}
234
235static int scpi_cpufreq_remove(struct platform_device *pdev)
236{
Sudeep Holla343a8d172018-01-10 16:44:15 +0000237 cpufreq_unregister_driver(&scpi_cpufreq_driver);
Sudeep Holla8def3102015-03-30 10:59:52 +0100238 scpi_ops = NULL;
239 return 0;
240}
241
242static struct platform_driver scpi_cpufreq_platdrv = {
243 .driver = {
244 .name = "scpi-cpufreq",
Sudeep Holla8def3102015-03-30 10:59:52 +0100245 },
246 .probe = scpi_cpufreq_probe,
247 .remove = scpi_cpufreq_remove,
248};
249module_platform_driver(scpi_cpufreq_platdrv);
250
251MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
252MODULE_DESCRIPTION("ARM SCPI CPUFreq interface driver");
253MODULE_LICENSE("GPL v2");