treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / cpufreq / scpi-cpufreq.c
blob20d1f85d5f5a4e6e8312547082e49824c0967c79
1 /*
2 * System Control and Power Interface (SCPI) based CPUFreq Interface driver
4 * Copyright (C) 2015 ARM Ltd.
5 * Sudeep Holla <sudeep.holla@arm.com>
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.
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.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/clk.h>
20 #include <linux/cpu.h>
21 #include <linux/cpufreq.h>
22 #include <linux/cpumask.h>
23 #include <linux/export.h>
24 #include <linux/module.h>
25 #include <linux/of_platform.h>
26 #include <linux/pm_opp.h>
27 #include <linux/scpi_protocol.h>
28 #include <linux/slab.h>
29 #include <linux/types.h>
31 struct scpi_data {
32 struct clk *clk;
33 struct device *cpu_dev;
36 static struct scpi_ops *scpi_ops;
38 static unsigned int scpi_cpufreq_get_rate(unsigned int cpu)
40 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);
44 return rate / 1000;
47 static int
48 scpi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index)
50 unsigned long freq = policy->freq_table[index].frequency;
51 struct scpi_data *priv = policy->driver_data;
52 u64 rate = freq * 1000;
53 int ret;
55 ret = clk_set_rate(priv->clk, rate);
57 if (ret)
58 return ret;
60 if (clk_get_rate(priv->clk) != rate)
61 return -EIO;
63 arch_set_freq_scale(policy->related_cpus, freq,
64 policy->cpuinfo.max_freq);
66 return 0;
69 static int
70 scpi_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
72 int cpu, domain, tdomain;
73 struct device *tcpu_dev;
75 domain = scpi_ops->device_domain_id(cpu_dev);
76 if (domain < 0)
77 return domain;
79 for_each_possible_cpu(cpu) {
80 if (cpu == cpu_dev->id)
81 continue;
83 tcpu_dev = get_cpu_device(cpu);
84 if (!tcpu_dev)
85 continue;
87 tdomain = scpi_ops->device_domain_id(tcpu_dev);
88 if (tdomain == domain)
89 cpumask_set_cpu(cpu, cpumask);
92 return 0;
95 static int scpi_cpufreq_init(struct cpufreq_policy *policy)
97 int ret;
98 unsigned int latency;
99 struct device *cpu_dev;
100 struct scpi_data *priv;
101 struct cpufreq_frequency_table *freq_table;
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;
109 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;
115 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;
121 ret = dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus);
122 if (ret) {
123 dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
124 __func__, ret);
125 return ret;
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;
135 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
136 if (!priv) {
137 ret = -ENOMEM;
138 goto out_free_opp;
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;
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);
152 ret = PTR_ERR(priv->clk);
153 goto out_free_cpufreq_table;
156 policy->driver_data = priv;
157 policy->freq_table = freq_table;
159 /* scpi allows DVFS request for any domain from any CPU */
160 policy->dvfs_possible_from_any_cpu = true;
162 latency = scpi_ops->get_transition_latency(cpu_dev);
163 if (!latency)
164 latency = CPUFREQ_ETERNAL;
166 policy->cpuinfo.transition_latency = latency;
168 policy->fast_switch_possible = false;
170 dev_pm_opp_of_register_em(policy->cpus);
172 return 0;
174 out_free_cpufreq_table:
175 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
176 out_free_priv:
177 kfree(priv);
178 out_free_opp:
179 dev_pm_opp_remove_all_dynamic(cpu_dev);
181 return ret;
184 static int scpi_cpufreq_exit(struct cpufreq_policy *policy)
186 struct scpi_data *priv = policy->driver_data;
188 clk_put(priv->clk);
189 dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
190 dev_pm_opp_remove_all_dynamic(priv->cpu_dev);
191 kfree(priv);
193 return 0;
196 static struct cpufreq_driver scpi_cpufreq_driver = {
197 .name = "scpi-cpufreq",
198 .flags = CPUFREQ_STICKY | CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
199 CPUFREQ_NEED_INITIAL_FREQ_CHECK |
200 CPUFREQ_IS_COOLING_DEV,
201 .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,
206 .target_index = scpi_cpufreq_set_target,
209 static int scpi_cpufreq_probe(struct platform_device *pdev)
211 int ret;
213 scpi_ops = get_scpi_ops();
214 if (!scpi_ops)
215 return -EIO;
217 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;
224 static int scpi_cpufreq_remove(struct platform_device *pdev)
226 cpufreq_unregister_driver(&scpi_cpufreq_driver);
227 scpi_ops = NULL;
228 return 0;
231 static struct platform_driver scpi_cpufreq_platdrv = {
232 .driver = {
233 .name = "scpi-cpufreq",
235 .probe = scpi_cpufreq_probe,
236 .remove = scpi_cpufreq_remove,
238 module_platform_driver(scpi_cpufreq_platdrv);
240 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
241 MODULE_DESCRIPTION("ARM SCPI CPUFreq interface driver");
242 MODULE_LICENSE("GPL v2");