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>
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
);
48 scpi_cpufreq_set_target(struct cpufreq_policy
*policy
, unsigned int index
)
50 u64 rate
= policy
->freq_table
[index
].frequency
* 1000;
51 struct scpi_data
*priv
= policy
->driver_data
;
54 ret
= clk_set_rate(priv
->clk
, rate
);
59 if (clk_get_rate(priv
->clk
) != rate
)
66 scpi_get_sharing_cpus(struct device
*cpu_dev
, struct cpumask
*cpumask
)
68 int cpu
, domain
, tdomain
;
69 struct device
*tcpu_dev
;
71 domain
= scpi_ops
->device_domain_id(cpu_dev
);
75 for_each_possible_cpu(cpu
) {
76 if (cpu
== cpu_dev
->id
)
79 tcpu_dev
= get_cpu_device(cpu
);
83 tdomain
= scpi_ops
->device_domain_id(tcpu_dev
);
84 if (tdomain
== domain
)
85 cpumask_set_cpu(cpu
, cpumask
);
91 static int scpi_cpufreq_init(struct cpufreq_policy
*policy
)
95 struct device
*cpu_dev
;
96 struct scpi_data
*priv
;
97 struct cpufreq_frequency_table
*freq_table
;
99 cpu_dev
= get_cpu_device(policy
->cpu
);
101 pr_err("failed to get cpu%d device\n", policy
->cpu
);
105 ret
= scpi_ops
->add_opps_to_device(cpu_dev
);
107 dev_warn(cpu_dev
, "failed to add opps to the device\n");
111 ret
= scpi_get_sharing_cpus(cpu_dev
, policy
->cpus
);
113 dev_warn(cpu_dev
, "failed to get sharing cpumask\n");
117 ret
= dev_pm_opp_set_sharing_cpus(cpu_dev
, policy
->cpus
);
119 dev_err(cpu_dev
, "%s: failed to mark OPPs as shared: %d\n",
124 ret
= dev_pm_opp_get_opp_count(cpu_dev
);
126 dev_dbg(cpu_dev
, "OPP table is not ready, deferring probe\n");
131 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
137 ret
= dev_pm_opp_init_cpufreq_table(cpu_dev
, &freq_table
);
139 dev_err(cpu_dev
, "failed to init cpufreq table: %d\n", ret
);
143 priv
->cpu_dev
= cpu_dev
;
144 priv
->clk
= clk_get(cpu_dev
, NULL
);
145 if (IS_ERR(priv
->clk
)) {
146 dev_err(cpu_dev
, "%s: Failed to get clk for cpu: %d\n",
147 __func__
, cpu_dev
->id
);
148 ret
= PTR_ERR(priv
->clk
);
149 goto out_free_cpufreq_table
;
152 policy
->driver_data
= priv
;
153 policy
->freq_table
= freq_table
;
155 /* scpi allows DVFS request for any domain from any CPU */
156 policy
->dvfs_possible_from_any_cpu
= true;
158 latency
= scpi_ops
->get_transition_latency(cpu_dev
);
160 latency
= CPUFREQ_ETERNAL
;
162 policy
->cpuinfo
.transition_latency
= latency
;
164 policy
->fast_switch_possible
= false;
166 dev_pm_opp_of_register_em(cpu_dev
, policy
->cpus
);
170 out_free_cpufreq_table
:
171 dev_pm_opp_free_cpufreq_table(cpu_dev
, &freq_table
);
175 dev_pm_opp_remove_all_dynamic(cpu_dev
);
180 static int scpi_cpufreq_exit(struct cpufreq_policy
*policy
)
182 struct scpi_data
*priv
= policy
->driver_data
;
185 dev_pm_opp_free_cpufreq_table(priv
->cpu_dev
, &policy
->freq_table
);
186 dev_pm_opp_remove_all_dynamic(priv
->cpu_dev
);
192 static struct cpufreq_driver scpi_cpufreq_driver
= {
193 .name
= "scpi-cpufreq",
194 .flags
= CPUFREQ_STICKY
| CPUFREQ_HAVE_GOVERNOR_PER_POLICY
|
195 CPUFREQ_NEED_INITIAL_FREQ_CHECK
|
196 CPUFREQ_IS_COOLING_DEV
,
197 .verify
= cpufreq_generic_frequency_table_verify
,
198 .attr
= cpufreq_generic_attr
,
199 .get
= scpi_cpufreq_get_rate
,
200 .init
= scpi_cpufreq_init
,
201 .exit
= scpi_cpufreq_exit
,
202 .target_index
= scpi_cpufreq_set_target
,
205 static int scpi_cpufreq_probe(struct platform_device
*pdev
)
209 scpi_ops
= get_scpi_ops();
213 ret
= cpufreq_register_driver(&scpi_cpufreq_driver
);
215 dev_err(&pdev
->dev
, "%s: registering cpufreq failed, err: %d\n",
220 static int scpi_cpufreq_remove(struct platform_device
*pdev
)
222 cpufreq_unregister_driver(&scpi_cpufreq_driver
);
227 static struct platform_driver scpi_cpufreq_platdrv
= {
229 .name
= "scpi-cpufreq",
231 .probe
= scpi_cpufreq_probe
,
232 .remove
= scpi_cpufreq_remove
,
234 module_platform_driver(scpi_cpufreq_platdrv
);
236 MODULE_ALIAS("platform:scpi-cpufreq");
237 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
238 MODULE_DESCRIPTION("ARM SCPI CPUFreq interface driver");
239 MODULE_LICENSE("GPL v2");