blob: 20d1f85d5f5a4e6e8312547082e49824c0967c79 [file] [log] [blame]
Sudeep Holla8def3102015-03-30 10:59:52 +01001/*
2 * System Control and Power Interface (SCPI) based CPUFreq Interface driver
3 *
Sudeep Holla8def3102015-03-30 10:59:52 +01004 * Copyright (C) 2015 ARM Ltd.
5 * Sudeep Holla <sudeep.holla@arm.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Sudeep Holla343a8d172018-01-10 16:44:15 +000019#include <linux/clk.h>
Sudeep Hollad9975b02016-05-03 15:05:05 +010020#include <linux/cpu.h>
Sudeep Holla8def3102015-03-30 10:59:52 +010021#include <linux/cpufreq.h>
Sudeep Holla343a8d172018-01-10 16:44:15 +000022#include <linux/cpumask.h>
Sudeep Holla343a8d172018-01-10 16:44:15 +000023#include <linux/export.h>
Sudeep Holla8def3102015-03-30 10:59:52 +010024#include <linux/module.h>
Sudeep Holla343a8d172018-01-10 16:44:15 +000025#include <linux/of_platform.h>
Sudeep Holla8def3102015-03-30 10:59:52 +010026#include <linux/pm_opp.h>
27#include <linux/scpi_protocol.h>
Sudeep Holla343a8d172018-01-10 16:44:15 +000028#include <linux/slab.h>
Sudeep Holla8def3102015-03-30 10:59:52 +010029#include <linux/types.h>
30
Sudeep Holla343a8d172018-01-10 16:44:15 +000031struct scpi_data {
32 struct clk *clk;
33 struct device *cpu_dev;
Sudeep Holla343a8d172018-01-10 16:44:15 +000034};
Sudeep Holla8def3102015-03-30 10:59:52 +010035
36static struct scpi_ops *scpi_ops;
37
Sudeep Holla343a8d172018-01-10 16:44:15 +000038static unsigned int scpi_cpufreq_get_rate(unsigned int cpu)
Sudeep Holla8def3102015-03-30 10:59:52 +010039{
Sudeep Holla343a8d172018-01-10 16:44:15 +000040 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
41 struct scpi_data *priv = policy->driver_data;
42 unsigned long rate = clk_get_rate(priv->clk);
43
44 return rate / 1000;
Sudeep Holla8def3102015-03-30 10:59:52 +010045}
46
Sudeep Holla343a8d172018-01-10 16:44:15 +000047static int
48scpi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index)
49{
Dietmar Eggemann9326fdf2018-02-26 13:11:03 +000050 unsigned long freq = policy->freq_table[index].frequency;
Sudeep Holla343a8d172018-01-10 16:44:15 +000051 struct scpi_data *priv = policy->driver_data;
Dietmar Eggemann9326fdf2018-02-26 13:11:03 +000052 u64 rate = freq * 1000;
Sudeep Holla343a8d172018-01-10 16:44:15 +000053 int ret;
54
55 ret = clk_set_rate(priv->clk, rate);
Sudeep Holla343a8d172018-01-10 16:44:15 +000056
Dietmar Eggemann9326fdf2018-02-26 13:11:03 +000057 if (ret)
58 return ret;
59
60 if (clk_get_rate(priv->clk) != rate)
61 return -EIO;
62
63 arch_set_freq_scale(policy->related_cpus, freq,
64 policy->cpuinfo.max_freq);
65
66 return 0;
Sudeep Holla343a8d172018-01-10 16:44:15 +000067}
68
69static int
70scpi_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
71{
72 int cpu, domain, tdomain;
73 struct device *tcpu_dev;
74
75 domain = scpi_ops->device_domain_id(cpu_dev);
76 if (domain < 0)
77 return domain;
78
79 for_each_possible_cpu(cpu) {
80 if (cpu == cpu_dev->id)
81 continue;
82
83 tcpu_dev = get_cpu_device(cpu);
84 if (!tcpu_dev)
85 continue;
86
87 tdomain = scpi_ops->device_domain_id(tcpu_dev);
88 if (tdomain == domain)
89 cpumask_set_cpu(cpu, cpumask);
90 }
91
92 return 0;
93}
94
95static int scpi_cpufreq_init(struct cpufreq_policy *policy)
Sudeep Holla8def3102015-03-30 10:59:52 +010096{
Sudeep Hollac0f2e212017-04-27 15:20:37 +010097 int ret;
Sudeep Holla343a8d172018-01-10 16:44:15 +000098 unsigned int latency;
99 struct device *cpu_dev;
100 struct scpi_data *priv;
101 struct cpufreq_frequency_table *freq_table;
102
103 cpu_dev = get_cpu_device(policy->cpu);
104 if (!cpu_dev) {
105 pr_err("failed to get cpu%d device\n", policy->cpu);
106 return -ENODEV;
107 }
Sudeep Holla8def3102015-03-30 10:59:52 +0100108
Sudeep Hollac0f2e212017-04-27 15:20:37 +0100109 ret = scpi_ops->add_opps_to_device(cpu_dev);
110 if (ret) {
111 dev_warn(cpu_dev, "failed to add opps to the device\n");
112 return ret;
Sudeep Hollad9975b02016-05-03 15:05:05 +0100113 }
114
Sudeep Holla343a8d172018-01-10 16:44:15 +0000115 ret = scpi_get_sharing_cpus(cpu_dev, policy->cpus);
116 if (ret) {
117 dev_warn(cpu_dev, "failed to get sharing cpumask\n");
118 return ret;
119 }
120
121 ret = dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus);
122 if (ret) {
Sudeep Hollad9975b02016-05-03 15:05:05 +0100123 dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
124 __func__, ret);
Sudeep Holla343a8d172018-01-10 16:44:15 +0000125 return ret;
126 }
127
128 ret = dev_pm_opp_get_opp_count(cpu_dev);
129 if (ret <= 0) {
130 dev_dbg(cpu_dev, "OPP table is not ready, deferring probe\n");
131 ret = -EPROBE_DEFER;
132 goto out_free_opp;
133 }
134
135 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
136 if (!priv) {
137 ret = -ENOMEM;
138 goto out_free_opp;
139 }
140
141 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
142 if (ret) {
143 dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
144 goto out_free_priv;
145 }
146
147 priv->cpu_dev = cpu_dev;
148 priv->clk = clk_get(cpu_dev, NULL);
149 if (IS_ERR(priv->clk)) {
150 dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d\n",
151 __func__, cpu_dev->id);
Wei Yongjun07253902018-01-23 02:09:02 +0000152 ret = PTR_ERR(priv->clk);
Sudeep Holla343a8d172018-01-10 16:44:15 +0000153 goto out_free_cpufreq_table;
154 }
155
156 policy->driver_data = priv;
Viresh Kumar472ada62018-02-26 10:39:04 +0530157 policy->freq_table = freq_table;
Sudeep Holla343a8d172018-01-10 16:44:15 +0000158
159 /* scpi allows DVFS request for any domain from any CPU */
160 policy->dvfs_possible_from_any_cpu = true;
161
162 latency = scpi_ops->get_transition_latency(cpu_dev);
163 if (!latency)
164 latency = CPUFREQ_ETERNAL;
165
166 policy->cpuinfo.transition_latency = latency;
167
168 policy->fast_switch_possible = false;
Quentin Perret6915d7a2019-02-04 11:09:50 +0000169
170 dev_pm_opp_of_register_em(policy->cpus);
171
Sudeep Holla343a8d172018-01-10 16:44:15 +0000172 return 0;
173
Sudeep Holla343a8d172018-01-10 16:44:15 +0000174out_free_cpufreq_table:
175 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
176out_free_priv:
177 kfree(priv);
178out_free_opp:
Viresh Kumar1690d8b2019-01-04 15:14:33 +0530179 dev_pm_opp_remove_all_dynamic(cpu_dev);
Sudeep Holla343a8d172018-01-10 16:44:15 +0000180
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
Sudeep Holla343a8d172018-01-10 16:44:15 +0000188 clk_put(priv->clk);
189 dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
Viresh Kumar1690d8b2019-01-04 15:14:33 +0530190 dev_pm_opp_remove_all_dynamic(priv->cpu_dev);
Vincent Stehlé31d4c522019-03-27 23:06:42 +0100191 kfree(priv);
Sudeep Holla343a8d172018-01-10 16:44:15 +0000192
193 return 0;
194}
195
Sudeep Holla343a8d172018-01-10 16:44:15 +0000196static struct cpufreq_driver scpi_cpufreq_driver = {
197 .name = "scpi-cpufreq",
198 .flags = CPUFREQ_STICKY | CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
Amit Kucheriacb772b82019-01-29 10:25:15 +0530199 CPUFREQ_NEED_INITIAL_FREQ_CHECK |
200 CPUFREQ_IS_COOLING_DEV,
Sudeep Holla343a8d172018-01-10 16:44:15 +0000201 .verify = cpufreq_generic_frequency_table_verify,
202 .attr = cpufreq_generic_attr,
203 .get = scpi_cpufreq_get_rate,
204 .init = scpi_cpufreq_init,
205 .exit = scpi_cpufreq_exit,
Sudeep Holla343a8d172018-01-10 16:44:15 +0000206 .target_index = scpi_cpufreq_set_target,
Sudeep Holla8def3102015-03-30 10:59:52 +0100207};
208
209static int scpi_cpufreq_probe(struct platform_device *pdev)
210{
Sudeep Holla343a8d172018-01-10 16:44:15 +0000211 int ret;
212
Sudeep Holla8def3102015-03-30 10:59:52 +0100213 scpi_ops = get_scpi_ops();
214 if (!scpi_ops)
215 return -EIO;
216
Sudeep Holla343a8d172018-01-10 16:44:15 +0000217 ret = cpufreq_register_driver(&scpi_cpufreq_driver);
218 if (ret)
219 dev_err(&pdev->dev, "%s: registering cpufreq failed, err: %d\n",
220 __func__, ret);
221 return ret;
Sudeep Holla8def3102015-03-30 10:59:52 +0100222}
223
224static int scpi_cpufreq_remove(struct platform_device *pdev)
225{
Sudeep Holla343a8d172018-01-10 16:44:15 +0000226 cpufreq_unregister_driver(&scpi_cpufreq_driver);
Sudeep Holla8def3102015-03-30 10:59:52 +0100227 scpi_ops = NULL;
228 return 0;
229}
230
231static struct platform_driver scpi_cpufreq_platdrv = {
232 .driver = {
233 .name = "scpi-cpufreq",
Sudeep Holla8def3102015-03-30 10:59:52 +0100234 },
235 .probe = scpi_cpufreq_probe,
236 .remove = scpi_cpufreq_remove,
237};
238module_platform_driver(scpi_cpufreq_platdrv);
239
240MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
241MODULE_DESCRIPTION("ARM SCPI CPUFreq interface driver");
242MODULE_LICENSE("GPL v2");