1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/arm-smccc.h>
4 #include <linux/bitfield.h>
5 #include <linux/clk-provider.h>
6 #include <linux/module.h>
7 #include <linux/platform_device.h>
8 #include <linux/pm_domain.h>
9 #include <linux/slab.h>
11 #define AIROHA_SIP_AVS_HANDLE 0x82000301
12 #define AIROHA_AVS_OP_BASE 0xddddddd0
13 #define AIROHA_AVS_OP_MASK GENMASK(1, 0)
14 #define AIROHA_AVS_OP_FREQ_DYN_ADJ (AIROHA_AVS_OP_BASE | \
15 FIELD_PREP(AIROHA_AVS_OP_MASK, 0x1))
16 #define AIROHA_AVS_OP_GET_FREQ (AIROHA_AVS_OP_BASE | \
17 FIELD_PREP(AIROHA_AVS_OP_MASK, 0x2))
19 struct airoha_cpu_pmdomain_priv {
21 struct generic_pm_domain pd;
24 static long airoha_cpu_pmdomain_clk_round(struct clk_hw *hw, unsigned long rate,
25 unsigned long *parent_rate)
30 static unsigned long airoha_cpu_pmdomain_clk_get(struct clk_hw *hw,
31 unsigned long parent_rate)
33 struct arm_smccc_res res;
35 arm_smccc_1_1_invoke(AIROHA_SIP_AVS_HANDLE, AIROHA_AVS_OP_GET_FREQ,
36 0, 0, 0, 0, 0, 0, &res);
38 /* SMCCC returns freq in MHz */
39 return (int)(res.a0 * 1000 * 1000);
42 /* Airoha CPU clk SMCC is always enabled */
43 static int airoha_cpu_pmdomain_clk_is_enabled(struct clk_hw *hw)
48 static const struct clk_ops airoha_cpu_pmdomain_clk_ops = {
49 .recalc_rate = airoha_cpu_pmdomain_clk_get,
50 .is_enabled = airoha_cpu_pmdomain_clk_is_enabled,
51 .round_rate = airoha_cpu_pmdomain_clk_round,
54 static int airoha_cpu_pmdomain_set_performance_state(struct generic_pm_domain *domain,
57 struct arm_smccc_res res;
59 arm_smccc_1_1_invoke(AIROHA_SIP_AVS_HANDLE, AIROHA_AVS_OP_FREQ_DYN_ADJ,
60 0, state, 0, 0, 0, 0, &res);
62 /* SMC signal correct apply by unsetting BIT 0 */
63 return res.a0 & BIT(0) ? -EINVAL : 0;
66 static int airoha_cpu_pmdomain_probe(struct platform_device *pdev)
68 struct airoha_cpu_pmdomain_priv *priv;
69 struct device *dev = &pdev->dev;
70 const struct clk_init_data init = {
72 .ops = &airoha_cpu_pmdomain_clk_ops,
73 /* Clock with no set_rate, can't cache */
74 .flags = CLK_GET_RATE_NOCACHE,
76 struct generic_pm_domain *pd;
79 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
83 /* Init and register a get-only clk for Cpufreq */
84 priv->hw.init = &init;
85 ret = devm_clk_hw_register(dev, &priv->hw);
89 ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
94 /* Init and register a PD for CPU */
97 pd->flags = GENPD_FLAG_ALWAYS_ON;
98 pd->set_performance_state = airoha_cpu_pmdomain_set_performance_state;
100 ret = pm_genpd_init(pd, NULL, false);
104 ret = of_genpd_add_provider_simple(dev->of_node, pd);
106 goto err_add_provider;
108 platform_set_drvdata(pdev, priv);
118 static void airoha_cpu_pmdomain_remove(struct platform_device *pdev)
120 struct airoha_cpu_pmdomain_priv *priv = platform_get_drvdata(pdev);
122 of_genpd_del_provider(pdev->dev.of_node);
123 pm_genpd_remove(&priv->pd);
126 static const struct of_device_id airoha_cpu_pmdomain_of_match[] = {
127 { .compatible = "airoha,en7581-cpufreq" },
130 MODULE_DEVICE_TABLE(of, airoha_cpu_pmdomain_of_match);
132 static struct platform_driver airoha_cpu_pmdomain_driver = {
133 .probe = airoha_cpu_pmdomain_probe,
134 .remove = airoha_cpu_pmdomain_remove,
136 .name = "airoha-cpu-pmdomain",
137 .of_match_table = airoha_cpu_pmdomain_of_match,
140 module_platform_driver(airoha_cpu_pmdomain_driver);
143 MODULE_DESCRIPTION("CPU PM domain driver for Airoha SoCs");
144 MODULE_LICENSE("GPL");