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/clk.h>
22 #include <linux/cpu.h>
23 #include <linux/cpufreq.h>
24 #include <linux/cpumask.h>
25 #include <linux/cpu_cooling.h>
26 #include <linux/export.h>
27 #include <linux/module.h>
28 #include <linux/of_platform.h>
29 #include <linux/pm_opp.h>
30 #include <linux/scpi_protocol.h>
31 #include <linux/slab.h>
32 #include <linux/types.h>
33 
34 struct scpi_data {
35 	struct clk *clk;
36 	struct device *cpu_dev;
37 	struct thermal_cooling_device *cdev;
38 };
39 
40 static struct scpi_ops *scpi_ops;
41 
42 static unsigned int scpi_cpufreq_get_rate(unsigned int cpu)
43 {
44 	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;
49 }
50 
51 static int
52 scpi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index)
53 {
54 	unsigned long freq = policy->freq_table[index].frequency;
55 	struct scpi_data *priv = policy->driver_data;
56 	u64 rate = freq * 1000;
57 	int ret;
58 
59 	ret = clk_set_rate(priv->clk, rate);
60 
61 	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;
71 }
72 
73 static int
74 scpi_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 
99 static int scpi_cpufreq_init(struct cpufreq_policy *policy)
100 {
101 	int ret;
102 	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 	}
112 
113 	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;
117 	}
118 
119 	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) {
127 		dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
128 			__func__, ret);
129 		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);
156 		ret = PTR_ERR(priv->clk);
157 		goto out_free_cpufreq_table;
158 	}
159 
160 	policy->driver_data = priv;
161 
162 	ret = cpufreq_table_validate_and_show(policy, freq_table);
163 	if (ret) {
164 		dev_err(cpu_dev, "%s: invalid frequency table: %d\n", __func__,
165 			ret);
166 		goto out_put_clk;
167 	}
168 
169 	/* scpi allows DVFS request for any domain from any CPU */
170 	policy->dvfs_possible_from_any_cpu = true;
171 
172 	latency = scpi_ops->get_transition_latency(cpu_dev);
173 	if (!latency)
174 		latency = CPUFREQ_ETERNAL;
175 
176 	policy->cpuinfo.transition_latency = latency;
177 
178 	policy->fast_switch_possible = false;
179 	return 0;
180 
181 out_put_clk:
182 	clk_put(priv->clk);
183 out_free_cpufreq_table:
184 	dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
185 out_free_priv:
186 	kfree(priv);
187 out_free_opp:
188 	dev_pm_opp_cpumask_remove_table(policy->cpus);
189 
190 	return ret;
191 }
192 
193 static int scpi_cpufreq_exit(struct cpufreq_policy *policy)
194 {
195 	struct scpi_data *priv = policy->driver_data;
196 
197 	cpufreq_cooling_unregister(priv->cdev);
198 	clk_put(priv->clk);
199 	dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
200 	kfree(priv);
201 	dev_pm_opp_cpumask_remove_table(policy->related_cpus);
202 
203 	return 0;
204 }
205 
206 static void scpi_cpufreq_ready(struct cpufreq_policy *policy)
207 {
208 	struct scpi_data *priv = policy->driver_data;
209 
210 	priv->cdev = of_cpufreq_cooling_register(policy);
211 }
212 
213 static struct cpufreq_driver scpi_cpufreq_driver = {
214 	.name	= "scpi-cpufreq",
215 	.flags	= CPUFREQ_STICKY | CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
216 		  CPUFREQ_NEED_INITIAL_FREQ_CHECK,
217 	.verify	= cpufreq_generic_frequency_table_verify,
218 	.attr	= cpufreq_generic_attr,
219 	.get	= scpi_cpufreq_get_rate,
220 	.init	= scpi_cpufreq_init,
221 	.exit	= scpi_cpufreq_exit,
222 	.ready	= scpi_cpufreq_ready,
223 	.target_index	= scpi_cpufreq_set_target,
224 };
225 
226 static int scpi_cpufreq_probe(struct platform_device *pdev)
227 {
228 	int ret;
229 
230 	scpi_ops = get_scpi_ops();
231 	if (!scpi_ops)
232 		return -EIO;
233 
234 	ret = cpufreq_register_driver(&scpi_cpufreq_driver);
235 	if (ret)
236 		dev_err(&pdev->dev, "%s: registering cpufreq failed, err: %d\n",
237 			__func__, ret);
238 	return ret;
239 }
240 
241 static int scpi_cpufreq_remove(struct platform_device *pdev)
242 {
243 	cpufreq_unregister_driver(&scpi_cpufreq_driver);
244 	scpi_ops = NULL;
245 	return 0;
246 }
247 
248 static struct platform_driver scpi_cpufreq_platdrv = {
249 	.driver = {
250 		.name	= "scpi-cpufreq",
251 	},
252 	.probe		= scpi_cpufreq_probe,
253 	.remove		= scpi_cpufreq_remove,
254 };
255 module_platform_driver(scpi_cpufreq_platdrv);
256 
257 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
258 MODULE_DESCRIPTION("ARM SCPI CPUFreq interface driver");
259 MODULE_LICENSE("GPL v2");
260