Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * System Control and Power Interface (SCPI) based CPUFreq Interface driver
4 *
5 * Copyright (C) 2015 ARM Ltd.
6 * Sudeep Holla <sudeep.holla@arm.com>
7 */
8
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11#include <linux/clk.h>
12#include <linux/cpu.h>
13#include <linux/cpufreq.h>
14#include <linux/cpumask.h>
15#include <linux/export.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/pm_opp.h>
19#include <linux/scpi_protocol.h>
20#include <linux/slab.h>
21#include <linux/types.h>
22
23struct scpi_data {
24 struct clk *clk;
25 struct device *cpu_dev;
26};
27
28static struct scpi_ops *scpi_ops;
29
30static unsigned int scpi_cpufreq_get_rate(unsigned int cpu)
31{
32 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
33 struct scpi_data *priv = policy->driver_data;
34 unsigned long rate = clk_get_rate(priv->clk);
35
36 return rate / 1000;
37}
38
39static int
40scpi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index)
41{
42 u64 rate = policy->freq_table[index].frequency * 1000;
43 struct scpi_data *priv = policy->driver_data;
44 int ret;
45
46 ret = clk_set_rate(priv->clk, rate);
47
48 if (ret)
49 return ret;
50
51 if (clk_get_rate(priv->clk) != rate)
52 return -EIO;
53
54 return 0;
55}
56
57static int
58scpi_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
59{
60 int cpu, domain, tdomain;
61 struct device *tcpu_dev;
62
63 domain = scpi_ops->device_domain_id(cpu_dev);
64 if (domain < 0)
65 return domain;
66
67 for_each_possible_cpu(cpu) {
68 if (cpu == cpu_dev->id)
69 continue;
70
71 tcpu_dev = get_cpu_device(cpu);
72 if (!tcpu_dev)
73 continue;
74
75 tdomain = scpi_ops->device_domain_id(tcpu_dev);
76 if (tdomain == domain)
77 cpumask_set_cpu(cpu, cpumask);
78 }
79
80 return 0;
81}
82
83static int scpi_cpufreq_init(struct cpufreq_policy *policy)
84{
85 int ret;
86 unsigned int latency;
87 struct device *cpu_dev;
88 struct scpi_data *priv;
89 struct cpufreq_frequency_table *freq_table;
90
91 cpu_dev = get_cpu_device(policy->cpu);
92 if (!cpu_dev) {
93 pr_err("failed to get cpu%d device\n", policy->cpu);
94 return -ENODEV;
95 }
96
97 ret = scpi_ops->add_opps_to_device(cpu_dev);
98 if (ret) {
99 dev_warn(cpu_dev, "failed to add opps to the device\n");
100 return ret;
101 }
102
103 ret = scpi_get_sharing_cpus(cpu_dev, policy->cpus);
104 if (ret) {
105 dev_warn(cpu_dev, "failed to get sharing cpumask\n");
106 return ret;
107 }
108
109 ret = dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus);
110 if (ret) {
111 dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
112 __func__, ret);
113 return ret;
114 }
115
116 ret = dev_pm_opp_get_opp_count(cpu_dev);
117 if (ret <= 0) {
118 dev_dbg(cpu_dev, "OPP table is not ready, deferring probe\n");
119 ret = -EPROBE_DEFER;
120 goto out_free_opp;
121 }
122
123 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
124 if (!priv) {
125 ret = -ENOMEM;
126 goto out_free_opp;
127 }
128
129 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
130 if (ret) {
131 dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
132 goto out_free_priv;
133 }
134
135 priv->cpu_dev = cpu_dev;
136 priv->clk = clk_get(cpu_dev, NULL);
137 if (IS_ERR(priv->clk)) {
138 dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d\n",
139 __func__, cpu_dev->id);
140 ret = PTR_ERR(priv->clk);
141 goto out_free_cpufreq_table;
142 }
143
144 policy->driver_data = priv;
145 policy->freq_table = freq_table;
146
147 /* scpi allows DVFS request for any domain from any CPU */
148 policy->dvfs_possible_from_any_cpu = true;
149
150 latency = scpi_ops->get_transition_latency(cpu_dev);
151 if (!latency)
152 latency = CPUFREQ_ETERNAL;
153
154 policy->cpuinfo.transition_latency = latency;
155
156 policy->fast_switch_possible = false;
157
158 return 0;
159
160out_free_cpufreq_table:
161 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
162out_free_priv:
163 kfree(priv);
164out_free_opp:
165 dev_pm_opp_remove_all_dynamic(cpu_dev);
166
167 return ret;
168}
169
170static int scpi_cpufreq_exit(struct cpufreq_policy *policy)
171{
172 struct scpi_data *priv = policy->driver_data;
173
174 clk_put(priv->clk);
175 dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
176 dev_pm_opp_remove_all_dynamic(priv->cpu_dev);
177 kfree(priv);
178
179 return 0;
180}
181
182static struct cpufreq_driver scpi_cpufreq_driver = {
183 .name = "scpi-cpufreq",
184 .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
185 CPUFREQ_NEED_INITIAL_FREQ_CHECK |
186 CPUFREQ_IS_COOLING_DEV,
187 .verify = cpufreq_generic_frequency_table_verify,
188 .attr = cpufreq_generic_attr,
189 .get = scpi_cpufreq_get_rate,
190 .init = scpi_cpufreq_init,
191 .exit = scpi_cpufreq_exit,
192 .target_index = scpi_cpufreq_set_target,
193 .register_em = cpufreq_register_em_with_opp,
194};
195
196static int scpi_cpufreq_probe(struct platform_device *pdev)
197{
198 int ret;
199
200 scpi_ops = get_scpi_ops();
201 if (!scpi_ops)
202 return -EIO;
203
204 ret = cpufreq_register_driver(&scpi_cpufreq_driver);
205 if (ret)
206 dev_err(&pdev->dev, "%s: registering cpufreq failed, err: %d\n",
207 __func__, ret);
208 return ret;
209}
210
211static void scpi_cpufreq_remove(struct platform_device *pdev)
212{
213 cpufreq_unregister_driver(&scpi_cpufreq_driver);
214 scpi_ops = NULL;
215}
216
217static struct platform_driver scpi_cpufreq_platdrv = {
218 .driver = {
219 .name = "scpi-cpufreq",
220 },
221 .probe = scpi_cpufreq_probe,
222 .remove_new = scpi_cpufreq_remove,
223};
224module_platform_driver(scpi_cpufreq_platdrv);
225
226MODULE_ALIAS("platform:scpi-cpufreq");
227MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
228MODULE_DESCRIPTION("ARM SCPI CPUFreq interface driver");
229MODULE_LICENSE("GPL v2");
1/*
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
21#include <linux/cpufreq.h>
22#include <linux/module.h>
23#include <linux/platform_device.h>
24#include <linux/pm_opp.h>
25#include <linux/scpi_protocol.h>
26#include <linux/types.h>
27
28#include "arm_big_little.h"
29
30static struct scpi_ops *scpi_ops;
31
32static struct scpi_dvfs_info *scpi_get_dvfs_info(struct device *cpu_dev)
33{
34 int domain = topology_physical_package_id(cpu_dev->id);
35
36 if (domain < 0)
37 return ERR_PTR(-EINVAL);
38 return scpi_ops->dvfs_get_info(domain);
39}
40
41static int scpi_opp_table_ops(struct device *cpu_dev, bool remove)
42{
43 int idx, ret = 0;
44 struct scpi_opp *opp;
45 struct scpi_dvfs_info *info = scpi_get_dvfs_info(cpu_dev);
46
47 if (IS_ERR(info))
48 return PTR_ERR(info);
49
50 if (!info->opps)
51 return -EIO;
52
53 for (opp = info->opps, idx = 0; idx < info->count; idx++, opp++) {
54 if (remove)
55 dev_pm_opp_remove(cpu_dev, opp->freq);
56 else
57 ret = dev_pm_opp_add(cpu_dev, opp->freq,
58 opp->m_volt * 1000);
59 if (ret) {
60 dev_warn(cpu_dev, "failed to add opp %uHz %umV\n",
61 opp->freq, opp->m_volt);
62 while (idx-- > 0)
63 dev_pm_opp_remove(cpu_dev, (--opp)->freq);
64 return ret;
65 }
66 }
67 return ret;
68}
69
70static int scpi_get_transition_latency(struct device *cpu_dev)
71{
72 struct scpi_dvfs_info *info = scpi_get_dvfs_info(cpu_dev);
73
74 if (IS_ERR(info))
75 return PTR_ERR(info);
76 return info->latency;
77}
78
79static int scpi_init_opp_table(struct device *cpu_dev)
80{
81 return scpi_opp_table_ops(cpu_dev, false);
82}
83
84static void scpi_free_opp_table(struct device *cpu_dev)
85{
86 scpi_opp_table_ops(cpu_dev, true);
87}
88
89static struct cpufreq_arm_bL_ops scpi_cpufreq_ops = {
90 .name = "scpi",
91 .get_transition_latency = scpi_get_transition_latency,
92 .init_opp_table = scpi_init_opp_table,
93 .free_opp_table = scpi_free_opp_table,
94};
95
96static int scpi_cpufreq_probe(struct platform_device *pdev)
97{
98 scpi_ops = get_scpi_ops();
99 if (!scpi_ops)
100 return -EIO;
101
102 return bL_cpufreq_register(&scpi_cpufreq_ops);
103}
104
105static int scpi_cpufreq_remove(struct platform_device *pdev)
106{
107 bL_cpufreq_unregister(&scpi_cpufreq_ops);
108 scpi_ops = NULL;
109 return 0;
110}
111
112static struct platform_driver scpi_cpufreq_platdrv = {
113 .driver = {
114 .name = "scpi-cpufreq",
115 .owner = THIS_MODULE,
116 },
117 .probe = scpi_cpufreq_probe,
118 .remove = scpi_cpufreq_remove,
119};
120module_platform_driver(scpi_cpufreq_platdrv);
121
122MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
123MODULE_DESCRIPTION("ARM SCPI CPUFreq interface driver");
124MODULE_LICENSE("GPL v2");