]>
Commit | Line | Data |
---|---|---|
8def3103 SH |
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 | ||
343a8d17 | 21 | #include <linux/clk.h> |
d9975b0b | 22 | #include <linux/cpu.h> |
8def3103 | 23 | #include <linux/cpufreq.h> |
343a8d17 | 24 | #include <linux/cpumask.h> |
343a8d17 | 25 | #include <linux/export.h> |
8def3103 | 26 | #include <linux/module.h> |
343a8d17 | 27 | #include <linux/of_platform.h> |
8def3103 SH |
28 | #include <linux/pm_opp.h> |
29 | #include <linux/scpi_protocol.h> | |
343a8d17 | 30 | #include <linux/slab.h> |
8def3103 SH |
31 | #include <linux/types.h> |
32 | ||
343a8d17 SH |
33 | struct scpi_data { |
34 | struct clk *clk; | |
35 | struct device *cpu_dev; | |
343a8d17 | 36 | }; |
8def3103 SH |
37 | |
38 | static struct scpi_ops *scpi_ops; | |
39 | ||
343a8d17 SH |
40 | static unsigned int scpi_cpufreq_get_rate(unsigned int cpu) |
41 | { | |
42 | struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); | |
43 | struct scpi_data *priv = policy->driver_data; | |
44 | unsigned long rate = clk_get_rate(priv->clk); | |
45 | ||
46 | return rate / 1000; | |
47 | } | |
48 | ||
49 | static int | |
50 | scpi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index) | |
51 | { | |
9326fdf3 | 52 | unsigned long freq = policy->freq_table[index].frequency; |
343a8d17 | 53 | struct scpi_data *priv = policy->driver_data; |
9326fdf3 | 54 | u64 rate = freq * 1000; |
343a8d17 SH |
55 | int ret; |
56 | ||
57 | ret = clk_set_rate(priv->clk, rate); | |
343a8d17 | 58 | |
9326fdf3 DE |
59 | if (ret) |
60 | return ret; | |
61 | ||
62 | if (clk_get_rate(priv->clk) != rate) | |
63 | return -EIO; | |
64 | ||
65 | arch_set_freq_scale(policy->related_cpus, freq, | |
66 | policy->cpuinfo.max_freq); | |
67 | ||
68 | return 0; | |
343a8d17 SH |
69 | } |
70 | ||
71 | static int | |
72 | scpi_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask) | |
8def3103 | 73 | { |
343a8d17 SH |
74 | int cpu, domain, tdomain; |
75 | struct device *tcpu_dev; | |
76 | ||
77 | domain = scpi_ops->device_domain_id(cpu_dev); | |
78 | if (domain < 0) | |
79 | return domain; | |
80 | ||
81 | for_each_possible_cpu(cpu) { | |
82 | if (cpu == cpu_dev->id) | |
83 | continue; | |
84 | ||
85 | tcpu_dev = get_cpu_device(cpu); | |
86 | if (!tcpu_dev) | |
87 | continue; | |
88 | ||
89 | tdomain = scpi_ops->device_domain_id(tcpu_dev); | |
90 | if (tdomain == domain) | |
91 | cpumask_set_cpu(cpu, cpumask); | |
92 | } | |
93 | ||
94 | return 0; | |
d9975b0b SH |
95 | } |
96 | ||
343a8d17 | 97 | static int scpi_cpufreq_init(struct cpufreq_policy *policy) |
d9975b0b | 98 | { |
c0f2e219 | 99 | int ret; |
343a8d17 SH |
100 | unsigned int latency; |
101 | struct device *cpu_dev; | |
102 | struct scpi_data *priv; | |
103 | struct cpufreq_frequency_table *freq_table; | |
104 | ||
105 | cpu_dev = get_cpu_device(policy->cpu); | |
106 | if (!cpu_dev) { | |
107 | pr_err("failed to get cpu%d device\n", policy->cpu); | |
108 | return -ENODEV; | |
109 | } | |
8def3103 | 110 | |
c0f2e219 SH |
111 | ret = scpi_ops->add_opps_to_device(cpu_dev); |
112 | if (ret) { | |
113 | dev_warn(cpu_dev, "failed to add opps to the device\n"); | |
114 | return ret; | |
8def3103 | 115 | } |
8def3103 | 116 | |
343a8d17 SH |
117 | ret = scpi_get_sharing_cpus(cpu_dev, policy->cpus); |
118 | if (ret) { | |
119 | dev_warn(cpu_dev, "failed to get sharing cpumask\n"); | |
120 | return ret; | |
121 | } | |
122 | ||
123 | ret = dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus); | |
124 | if (ret) { | |
d9975b0b SH |
125 | dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n", |
126 | __func__, ret); | |
343a8d17 SH |
127 | return ret; |
128 | } | |
129 | ||
130 | ret = dev_pm_opp_get_opp_count(cpu_dev); | |
131 | if (ret <= 0) { | |
132 | dev_dbg(cpu_dev, "OPP table is not ready, deferring probe\n"); | |
133 | ret = -EPROBE_DEFER; | |
134 | goto out_free_opp; | |
135 | } | |
136 | ||
137 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); | |
138 | if (!priv) { | |
139 | ret = -ENOMEM; | |
140 | goto out_free_opp; | |
141 | } | |
142 | ||
143 | ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table); | |
144 | if (ret) { | |
145 | dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret); | |
146 | goto out_free_priv; | |
147 | } | |
148 | ||
149 | priv->cpu_dev = cpu_dev; | |
150 | priv->clk = clk_get(cpu_dev, NULL); | |
151 | if (IS_ERR(priv->clk)) { | |
152 | dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d\n", | |
153 | __func__, cpu_dev->id); | |
0725390d | 154 | ret = PTR_ERR(priv->clk); |
343a8d17 SH |
155 | goto out_free_cpufreq_table; |
156 | } | |
157 | ||
158 | policy->driver_data = priv; | |
472ada60 | 159 | policy->freq_table = freq_table; |
343a8d17 SH |
160 | |
161 | /* scpi allows DVFS request for any domain from any CPU */ | |
162 | policy->dvfs_possible_from_any_cpu = true; | |
163 | ||
164 | latency = scpi_ops->get_transition_latency(cpu_dev); | |
165 | if (!latency) | |
166 | latency = CPUFREQ_ETERNAL; | |
167 | ||
168 | policy->cpuinfo.transition_latency = latency; | |
169 | ||
170 | policy->fast_switch_possible = false; | |
6915d7ad QP |
171 | |
172 | dev_pm_opp_of_register_em(policy->cpus); | |
173 | ||
343a8d17 SH |
174 | return 0; |
175 | ||
343a8d17 SH |
176 | out_free_cpufreq_table: |
177 | dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table); | |
178 | out_free_priv: | |
179 | kfree(priv); | |
180 | out_free_opp: | |
1690d8bb | 181 | dev_pm_opp_remove_all_dynamic(cpu_dev); |
343a8d17 | 182 | |
d9975b0b | 183 | return ret; |
8def3103 SH |
184 | } |
185 | ||
343a8d17 SH |
186 | static int scpi_cpufreq_exit(struct cpufreq_policy *policy) |
187 | { | |
188 | struct scpi_data *priv = policy->driver_data; | |
189 | ||
343a8d17 SH |
190 | clk_put(priv->clk); |
191 | dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table); | |
1690d8bb | 192 | dev_pm_opp_remove_all_dynamic(priv->cpu_dev); |
31d4c528 | 193 | kfree(priv); |
343a8d17 SH |
194 | |
195 | return 0; | |
196 | } | |
197 | ||
343a8d17 SH |
198 | static struct cpufreq_driver scpi_cpufreq_driver = { |
199 | .name = "scpi-cpufreq", | |
200 | .flags = CPUFREQ_STICKY | CPUFREQ_HAVE_GOVERNOR_PER_POLICY | | |
cb772b8c AK |
201 | CPUFREQ_NEED_INITIAL_FREQ_CHECK | |
202 | CPUFREQ_IS_COOLING_DEV, | |
343a8d17 SH |
203 | .verify = cpufreq_generic_frequency_table_verify, |
204 | .attr = cpufreq_generic_attr, | |
205 | .get = scpi_cpufreq_get_rate, | |
206 | .init = scpi_cpufreq_init, | |
207 | .exit = scpi_cpufreq_exit, | |
343a8d17 | 208 | .target_index = scpi_cpufreq_set_target, |
8def3103 SH |
209 | }; |
210 | ||
211 | static int scpi_cpufreq_probe(struct platform_device *pdev) | |
212 | { | |
343a8d17 SH |
213 | int ret; |
214 | ||
8def3103 SH |
215 | scpi_ops = get_scpi_ops(); |
216 | if (!scpi_ops) | |
217 | return -EIO; | |
218 | ||
343a8d17 SH |
219 | ret = cpufreq_register_driver(&scpi_cpufreq_driver); |
220 | if (ret) | |
221 | dev_err(&pdev->dev, "%s: registering cpufreq failed, err: %d\n", | |
222 | __func__, ret); | |
223 | return ret; | |
8def3103 SH |
224 | } |
225 | ||
226 | static int scpi_cpufreq_remove(struct platform_device *pdev) | |
227 | { | |
343a8d17 | 228 | cpufreq_unregister_driver(&scpi_cpufreq_driver); |
8def3103 SH |
229 | scpi_ops = NULL; |
230 | return 0; | |
231 | } | |
232 | ||
233 | static struct platform_driver scpi_cpufreq_platdrv = { | |
234 | .driver = { | |
235 | .name = "scpi-cpufreq", | |
8def3103 SH |
236 | }, |
237 | .probe = scpi_cpufreq_probe, | |
238 | .remove = scpi_cpufreq_remove, | |
239 | }; | |
240 | module_platform_driver(scpi_cpufreq_platdrv); | |
241 | ||
242 | MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>"); | |
243 | MODULE_DESCRIPTION("ARM SCPI CPUFreq interface driver"); | |
244 | MODULE_LICENSE("GPL v2"); |