1 // SPDX-License-Identifier: GPL-2.0-only
3 * System Control and Power Interface (SCPI) based CPUFreq Interface driver
5 * Copyright (C) 2015 ARM Ltd.
6 * Sudeep Holla <sudeep.holla@arm.com>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
25 struct device
*cpu_dev
;
28 static struct scpi_ops
*scpi_ops
;
30 static unsigned int scpi_cpufreq_get_rate(unsigned int cpu
)
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
);
40 scpi_cpufreq_set_target(struct cpufreq_policy
*policy
, unsigned int index
)
42 u64 rate
= policy
->freq_table
[index
].frequency
* 1000;
43 struct scpi_data
*priv
= policy
->driver_data
;
46 ret
= clk_set_rate(priv
->clk
, rate
);
51 if (clk_get_rate(priv
->clk
) != rate
)
58 scpi_get_sharing_cpus(struct device
*cpu_dev
, struct cpumask
*cpumask
)
60 int cpu
, domain
, tdomain
;
61 struct device
*tcpu_dev
;
63 domain
= scpi_ops
->device_domain_id(cpu_dev
);
67 for_each_possible_cpu(cpu
) {
68 if (cpu
== cpu_dev
->id
)
71 tcpu_dev
= get_cpu_device(cpu
);
75 tdomain
= scpi_ops
->device_domain_id(tcpu_dev
);
76 if (tdomain
== domain
)
77 cpumask_set_cpu(cpu
, cpumask
);
83 static int scpi_cpufreq_init(struct cpufreq_policy
*policy
)
87 struct device
*cpu_dev
;
88 struct scpi_data
*priv
;
89 struct cpufreq_frequency_table
*freq_table
;
91 cpu_dev
= get_cpu_device(policy
->cpu
);
93 pr_err("failed to get cpu%d device\n", policy
->cpu
);
97 ret
= scpi_ops
->add_opps_to_device(cpu_dev
);
99 dev_warn(cpu_dev
, "failed to add opps to the device\n");
103 ret
= scpi_get_sharing_cpus(cpu_dev
, policy
->cpus
);
105 dev_warn(cpu_dev
, "failed to get sharing cpumask\n");
109 ret
= dev_pm_opp_set_sharing_cpus(cpu_dev
, policy
->cpus
);
111 dev_err(cpu_dev
, "%s: failed to mark OPPs as shared: %d\n",
116 ret
= dev_pm_opp_get_opp_count(cpu_dev
);
118 dev_dbg(cpu_dev
, "OPP table is not ready, deferring probe\n");
123 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
129 ret
= dev_pm_opp_init_cpufreq_table(cpu_dev
, &freq_table
);
131 dev_err(cpu_dev
, "failed to init cpufreq table: %d\n", ret
);
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
;
144 policy
->driver_data
= priv
;
145 policy
->freq_table
= freq_table
;
147 /* scpi allows DVFS request for any domain from any CPU */
148 policy
->dvfs_possible_from_any_cpu
= true;
150 latency
= scpi_ops
->get_transition_latency(cpu_dev
);
152 latency
= CPUFREQ_ETERNAL
;
154 policy
->cpuinfo
.transition_latency
= latency
;
156 policy
->fast_switch_possible
= false;
160 out_free_cpufreq_table
:
161 dev_pm_opp_free_cpufreq_table(cpu_dev
, &freq_table
);
165 dev_pm_opp_remove_all_dynamic(cpu_dev
);
170 static void scpi_cpufreq_exit(struct cpufreq_policy
*policy
)
172 struct scpi_data
*priv
= policy
->driver_data
;
175 dev_pm_opp_free_cpufreq_table(priv
->cpu_dev
, &policy
->freq_table
);
176 dev_pm_opp_remove_all_dynamic(priv
->cpu_dev
);
180 static struct cpufreq_driver scpi_cpufreq_driver
= {
181 .name
= "scpi-cpufreq",
182 .flags
= CPUFREQ_HAVE_GOVERNOR_PER_POLICY
|
183 CPUFREQ_NEED_INITIAL_FREQ_CHECK
|
184 CPUFREQ_IS_COOLING_DEV
,
185 .verify
= cpufreq_generic_frequency_table_verify
,
186 .attr
= cpufreq_generic_attr
,
187 .get
= scpi_cpufreq_get_rate
,
188 .init
= scpi_cpufreq_init
,
189 .exit
= scpi_cpufreq_exit
,
190 .target_index
= scpi_cpufreq_set_target
,
191 .register_em
= cpufreq_register_em_with_opp
,
194 static int scpi_cpufreq_probe(struct platform_device
*pdev
)
198 scpi_ops
= get_scpi_ops();
202 ret
= cpufreq_register_driver(&scpi_cpufreq_driver
);
204 dev_err(&pdev
->dev
, "%s: registering cpufreq failed, err: %d\n",
209 static void scpi_cpufreq_remove(struct platform_device
*pdev
)
211 cpufreq_unregister_driver(&scpi_cpufreq_driver
);
215 static struct platform_driver scpi_cpufreq_platdrv
= {
217 .name
= "scpi-cpufreq",
219 .probe
= scpi_cpufreq_probe
,
220 .remove_new
= scpi_cpufreq_remove
,
222 module_platform_driver(scpi_cpufreq_platdrv
);
224 MODULE_ALIAS("platform:scpi-cpufreq");
225 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
226 MODULE_DESCRIPTION("ARM SCPI CPUFreq interface driver");
227 MODULE_LICENSE("GPL v2");