1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
6 #include <linux/bitfield.h>
7 #include <linux/cpufreq.h>
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/of_address.h>
12 #include <linux/of_platform.h>
13 #include <linux/pm_opp.h>
14 #include <linux/slab.h>
16 #define LUT_MAX_ENTRIES 40U
17 #define LUT_SRC GENMASK(31, 30)
18 #define LUT_L_VAL GENMASK(7, 0)
19 #define LUT_CORE_COUNT GENMASK(18, 16)
20 #define LUT_VOLT GENMASK(11, 0)
21 #define LUT_ROW_SIZE 32
23 #define LUT_TURBO_IND 1
25 /* Register offsets */
26 #define REG_ENABLE 0x0
27 #define REG_FREQ_LUT 0x110
28 #define REG_VOLT_LUT 0x114
29 #define REG_PERF_STATE 0x920
31 static unsigned long cpu_hw_rate, xo_rate;
32 static struct platform_device *global_pdev;
34 static int qcom_cpufreq_hw_target_index(struct cpufreq_policy *policy,
37 void __iomem *perf_state_reg = policy->driver_data;
38 unsigned long freq = policy->freq_table[index].frequency;
40 writel_relaxed(index, perf_state_reg);
42 arch_set_freq_scale(policy->related_cpus, freq,
43 policy->cpuinfo.max_freq);
47 static unsigned int qcom_cpufreq_hw_get(unsigned int cpu)
49 void __iomem *perf_state_reg;
50 struct cpufreq_policy *policy;
53 policy = cpufreq_cpu_get_raw(cpu);
57 perf_state_reg = policy->driver_data;
59 index = readl_relaxed(perf_state_reg);
60 index = min(index, LUT_MAX_ENTRIES - 1);
62 return policy->freq_table[index].frequency;
65 static unsigned int qcom_cpufreq_hw_fast_switch(struct cpufreq_policy *policy,
66 unsigned int target_freq)
68 void __iomem *perf_state_reg = policy->driver_data;
72 index = policy->cached_resolved_idx;
76 writel_relaxed(index, perf_state_reg);
78 freq = policy->freq_table[index].frequency;
79 arch_set_freq_scale(policy->related_cpus, freq,
80 policy->cpuinfo.max_freq);
85 static int qcom_cpufreq_hw_read_lut(struct device *cpu_dev,
86 struct cpufreq_policy *policy,
89 u32 data, src, lval, i, core_count, prev_freq = 0, freq;
91 struct cpufreq_frequency_table *table;
93 table = kcalloc(LUT_MAX_ENTRIES + 1, sizeof(*table), GFP_KERNEL);
97 for (i = 0; i < LUT_MAX_ENTRIES; i++) {
98 data = readl_relaxed(base + REG_FREQ_LUT +
100 src = FIELD_GET(LUT_SRC, data);
101 lval = FIELD_GET(LUT_L_VAL, data);
102 core_count = FIELD_GET(LUT_CORE_COUNT, data);
104 data = readl_relaxed(base + REG_VOLT_LUT +
106 volt = FIELD_GET(LUT_VOLT, data) * 1000;
109 freq = xo_rate * lval / 1000;
111 freq = cpu_hw_rate / 1000;
113 if (freq != prev_freq && core_count != LUT_TURBO_IND) {
114 table[i].frequency = freq;
115 dev_pm_opp_add(cpu_dev, freq * 1000, volt);
116 dev_dbg(cpu_dev, "index=%d freq=%d, core_count %d\n", i,
118 } else if (core_count == LUT_TURBO_IND) {
119 table[i].frequency = CPUFREQ_ENTRY_INVALID;
123 * Two of the same frequencies with the same core counts means
126 if (i > 0 && prev_freq == freq) {
127 struct cpufreq_frequency_table *prev = &table[i - 1];
130 * Only treat the last frequency that might be a boost
131 * as the boost frequency
133 if (prev->frequency == CPUFREQ_ENTRY_INVALID) {
134 prev->frequency = prev_freq;
135 prev->flags = CPUFREQ_BOOST_FREQ;
136 dev_pm_opp_add(cpu_dev, prev_freq * 1000, volt);
145 table[i].frequency = CPUFREQ_TABLE_END;
146 policy->freq_table = table;
147 dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus);
152 static void qcom_get_related_cpus(int index, struct cpumask *m)
154 struct device_node *cpu_np;
155 struct of_phandle_args args;
158 for_each_possible_cpu(cpu) {
159 cpu_np = of_cpu_device_node_get(cpu);
163 ret = of_parse_phandle_with_args(cpu_np, "qcom,freq-domain",
164 "#freq-domain-cells", 0,
170 if (index == args.args[0])
171 cpumask_set_cpu(cpu, m);
175 static int qcom_cpufreq_hw_cpu_init(struct cpufreq_policy *policy)
177 struct device *dev = &global_pdev->dev;
178 struct of_phandle_args args;
179 struct device_node *cpu_np;
180 struct device *cpu_dev;
181 struct resource *res;
185 cpu_dev = get_cpu_device(policy->cpu);
187 pr_err("%s: failed to get cpu%d device\n", __func__,
192 cpu_np = of_cpu_device_node_get(policy->cpu);
196 ret = of_parse_phandle_with_args(cpu_np, "qcom,freq-domain",
197 "#freq-domain-cells", 0, &args);
202 index = args.args[0];
204 res = platform_get_resource(global_pdev, IORESOURCE_MEM, index);
208 base = devm_ioremap(dev, res->start, resource_size(res));
212 /* HW should be in enabled state to proceed */
213 if (!(readl_relaxed(base + REG_ENABLE) & 0x1)) {
214 dev_err(dev, "Domain-%d cpufreq hardware not enabled\n", index);
219 qcom_get_related_cpus(index, policy->cpus);
220 if (!cpumask_weight(policy->cpus)) {
221 dev_err(dev, "Domain-%d failed to get related CPUs\n", index);
226 policy->driver_data = base + REG_PERF_STATE;
228 ret = qcom_cpufreq_hw_read_lut(cpu_dev, policy, base);
230 dev_err(dev, "Domain-%d failed to read LUT\n", index);
234 ret = dev_pm_opp_get_opp_count(cpu_dev);
236 dev_err(cpu_dev, "Failed to add OPPs\n");
241 dev_pm_opp_of_register_em(policy->cpus);
243 policy->fast_switch_possible = true;
247 devm_iounmap(dev, base);
251 static int qcom_cpufreq_hw_cpu_exit(struct cpufreq_policy *policy)
253 struct device *cpu_dev = get_cpu_device(policy->cpu);
254 void __iomem *base = policy->driver_data - REG_PERF_STATE;
256 dev_pm_opp_remove_all_dynamic(cpu_dev);
257 kfree(policy->freq_table);
258 devm_iounmap(&global_pdev->dev, base);
263 static struct freq_attr *qcom_cpufreq_hw_attr[] = {
264 &cpufreq_freq_attr_scaling_available_freqs,
265 &cpufreq_freq_attr_scaling_boost_freqs,
269 static struct cpufreq_driver cpufreq_qcom_hw_driver = {
270 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK |
271 CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
272 CPUFREQ_IS_COOLING_DEV,
273 .verify = cpufreq_generic_frequency_table_verify,
274 .target_index = qcom_cpufreq_hw_target_index,
275 .get = qcom_cpufreq_hw_get,
276 .init = qcom_cpufreq_hw_cpu_init,
277 .exit = qcom_cpufreq_hw_cpu_exit,
278 .fast_switch = qcom_cpufreq_hw_fast_switch,
279 .name = "qcom-cpufreq-hw",
280 .attr = qcom_cpufreq_hw_attr,
283 static int qcom_cpufreq_hw_driver_probe(struct platform_device *pdev)
288 clk = clk_get(&pdev->dev, "xo");
292 xo_rate = clk_get_rate(clk);
295 clk = clk_get(&pdev->dev, "alternate");
299 cpu_hw_rate = clk_get_rate(clk) / CLK_HW_DIV;
304 ret = cpufreq_register_driver(&cpufreq_qcom_hw_driver);
306 dev_err(&pdev->dev, "CPUFreq HW driver failed to register\n");
308 dev_dbg(&pdev->dev, "QCOM CPUFreq HW driver initialized\n");
313 static int qcom_cpufreq_hw_driver_remove(struct platform_device *pdev)
315 return cpufreq_unregister_driver(&cpufreq_qcom_hw_driver);
318 static const struct of_device_id qcom_cpufreq_hw_match[] = {
319 { .compatible = "qcom,cpufreq-hw" },
322 MODULE_DEVICE_TABLE(of, qcom_cpufreq_hw_match);
324 static struct platform_driver qcom_cpufreq_hw_driver = {
325 .probe = qcom_cpufreq_hw_driver_probe,
326 .remove = qcom_cpufreq_hw_driver_remove,
328 .name = "qcom-cpufreq-hw",
329 .of_match_table = qcom_cpufreq_hw_match,
333 static int __init qcom_cpufreq_hw_init(void)
335 return platform_driver_register(&qcom_cpufreq_hw_driver);
337 device_initcall(qcom_cpufreq_hw_init);
339 static void __exit qcom_cpufreq_hw_exit(void)
341 platform_driver_unregister(&qcom_cpufreq_hw_driver);
343 module_exit(qcom_cpufreq_hw_exit);
345 MODULE_DESCRIPTION("QCOM CPUFREQ HW Driver");
346 MODULE_LICENSE("GPL v2");