xref: /linux/drivers/cpufreq/airoha-cpufreq.c (revision f4b9d3bf44d59ca4489bd8c489539c27c02e5c6a)
1*84cf9e54SChristian Marangi // SPDX-License-Identifier: GPL-2.0
2*84cf9e54SChristian Marangi 
3*84cf9e54SChristian Marangi #include <linux/bitfield.h>
4*84cf9e54SChristian Marangi #include <linux/cpufreq.h>
5*84cf9e54SChristian Marangi #include <linux/module.h>
6*84cf9e54SChristian Marangi #include <linux/platform_device.h>
7*84cf9e54SChristian Marangi #include <linux/pm_domain.h>
8*84cf9e54SChristian Marangi #include <linux/pm_runtime.h>
9*84cf9e54SChristian Marangi #include <linux/slab.h>
10*84cf9e54SChristian Marangi 
11*84cf9e54SChristian Marangi #include "cpufreq-dt.h"
12*84cf9e54SChristian Marangi 
13*84cf9e54SChristian Marangi struct airoha_cpufreq_priv {
14*84cf9e54SChristian Marangi 	int opp_token;
15*84cf9e54SChristian Marangi 	struct dev_pm_domain_list *pd_list;
16*84cf9e54SChristian Marangi 	struct platform_device *cpufreq_dt;
17*84cf9e54SChristian Marangi };
18*84cf9e54SChristian Marangi 
19*84cf9e54SChristian Marangi static struct platform_device *cpufreq_pdev;
20*84cf9e54SChristian Marangi 
21*84cf9e54SChristian Marangi /* NOP function to disable OPP from setting clock */
22*84cf9e54SChristian Marangi static int airoha_cpufreq_config_clks_nop(struct device *dev,
23*84cf9e54SChristian Marangi 					  struct opp_table *opp_table,
24*84cf9e54SChristian Marangi 					  struct dev_pm_opp *opp,
25*84cf9e54SChristian Marangi 					  void *data, bool scaling_down)
26*84cf9e54SChristian Marangi {
27*84cf9e54SChristian Marangi 	return 0;
28*84cf9e54SChristian Marangi }
29*84cf9e54SChristian Marangi 
30*84cf9e54SChristian Marangi static const char * const airoha_cpufreq_clk_names[] = { "cpu", NULL };
31*84cf9e54SChristian Marangi static const char * const airoha_cpufreq_pd_names[] = { "perf" };
32*84cf9e54SChristian Marangi 
33*84cf9e54SChristian Marangi static int airoha_cpufreq_probe(struct platform_device *pdev)
34*84cf9e54SChristian Marangi {
35*84cf9e54SChristian Marangi 	const struct dev_pm_domain_attach_data attach_data = {
36*84cf9e54SChristian Marangi 		.pd_names = airoha_cpufreq_pd_names,
37*84cf9e54SChristian Marangi 		.num_pd_names = ARRAY_SIZE(airoha_cpufreq_pd_names),
38*84cf9e54SChristian Marangi 		.pd_flags = PD_FLAG_DEV_LINK_ON | PD_FLAG_REQUIRED_OPP,
39*84cf9e54SChristian Marangi 	};
40*84cf9e54SChristian Marangi 	struct dev_pm_opp_config config = {
41*84cf9e54SChristian Marangi 		.clk_names = airoha_cpufreq_clk_names,
42*84cf9e54SChristian Marangi 		.config_clks = airoha_cpufreq_config_clks_nop,
43*84cf9e54SChristian Marangi 	};
44*84cf9e54SChristian Marangi 	struct platform_device *cpufreq_dt;
45*84cf9e54SChristian Marangi 	struct airoha_cpufreq_priv *priv;
46*84cf9e54SChristian Marangi 	struct device *dev = &pdev->dev;
47*84cf9e54SChristian Marangi 	struct device *cpu_dev;
48*84cf9e54SChristian Marangi 	int ret;
49*84cf9e54SChristian Marangi 
50*84cf9e54SChristian Marangi 	/* CPUs refer to the same OPP table */
51*84cf9e54SChristian Marangi 	cpu_dev = get_cpu_device(0);
52*84cf9e54SChristian Marangi 	if (!cpu_dev)
53*84cf9e54SChristian Marangi 		return -ENODEV;
54*84cf9e54SChristian Marangi 
55*84cf9e54SChristian Marangi 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
56*84cf9e54SChristian Marangi 	if (!priv)
57*84cf9e54SChristian Marangi 		return -ENOMEM;
58*84cf9e54SChristian Marangi 
59*84cf9e54SChristian Marangi 	/* Set OPP table conf with NOP config_clks */
60*84cf9e54SChristian Marangi 	priv->opp_token = dev_pm_opp_set_config(cpu_dev, &config);
61*84cf9e54SChristian Marangi 	if (priv->opp_token < 0)
62*84cf9e54SChristian Marangi 		return dev_err_probe(dev, priv->opp_token, "Failed to set OPP config\n");
63*84cf9e54SChristian Marangi 
64*84cf9e54SChristian Marangi 	/* Attach PM for OPP */
65*84cf9e54SChristian Marangi 	ret = dev_pm_domain_attach_list(cpu_dev, &attach_data,
66*84cf9e54SChristian Marangi 					&priv->pd_list);
67*84cf9e54SChristian Marangi 	if (ret)
68*84cf9e54SChristian Marangi 		goto clear_opp_config;
69*84cf9e54SChristian Marangi 
70*84cf9e54SChristian Marangi 	cpufreq_dt = platform_device_register_simple("cpufreq-dt", -1, NULL, 0);
71*84cf9e54SChristian Marangi 	ret = PTR_ERR_OR_ZERO(cpufreq_dt);
72*84cf9e54SChristian Marangi 	if (ret) {
73*84cf9e54SChristian Marangi 		dev_err(dev, "failed to create cpufreq-dt device: %d\n", ret);
74*84cf9e54SChristian Marangi 		goto detach_pm;
75*84cf9e54SChristian Marangi 	}
76*84cf9e54SChristian Marangi 
77*84cf9e54SChristian Marangi 	priv->cpufreq_dt = cpufreq_dt;
78*84cf9e54SChristian Marangi 	platform_set_drvdata(pdev, priv);
79*84cf9e54SChristian Marangi 
80*84cf9e54SChristian Marangi 	return 0;
81*84cf9e54SChristian Marangi 
82*84cf9e54SChristian Marangi detach_pm:
83*84cf9e54SChristian Marangi 	dev_pm_domain_detach_list(priv->pd_list);
84*84cf9e54SChristian Marangi clear_opp_config:
85*84cf9e54SChristian Marangi 	dev_pm_opp_clear_config(priv->opp_token);
86*84cf9e54SChristian Marangi 
87*84cf9e54SChristian Marangi 	return ret;
88*84cf9e54SChristian Marangi }
89*84cf9e54SChristian Marangi 
90*84cf9e54SChristian Marangi static void airoha_cpufreq_remove(struct platform_device *pdev)
91*84cf9e54SChristian Marangi {
92*84cf9e54SChristian Marangi 	struct airoha_cpufreq_priv *priv = platform_get_drvdata(pdev);
93*84cf9e54SChristian Marangi 
94*84cf9e54SChristian Marangi 	platform_device_unregister(priv->cpufreq_dt);
95*84cf9e54SChristian Marangi 
96*84cf9e54SChristian Marangi 	dev_pm_domain_detach_list(priv->pd_list);
97*84cf9e54SChristian Marangi 
98*84cf9e54SChristian Marangi 	dev_pm_opp_clear_config(priv->opp_token);
99*84cf9e54SChristian Marangi }
100*84cf9e54SChristian Marangi 
101*84cf9e54SChristian Marangi static struct platform_driver airoha_cpufreq_driver = {
102*84cf9e54SChristian Marangi 	.probe = airoha_cpufreq_probe,
103*84cf9e54SChristian Marangi 	.remove = airoha_cpufreq_remove,
104*84cf9e54SChristian Marangi 	.driver = {
105*84cf9e54SChristian Marangi 		.name = "airoha-cpufreq",
106*84cf9e54SChristian Marangi 	},
107*84cf9e54SChristian Marangi };
108*84cf9e54SChristian Marangi 
109*84cf9e54SChristian Marangi static const struct of_device_id airoha_cpufreq_match_list[] __initconst = {
110*84cf9e54SChristian Marangi 	{ .compatible = "airoha,en7581" },
111*84cf9e54SChristian Marangi 	{},
112*84cf9e54SChristian Marangi };
113*84cf9e54SChristian Marangi MODULE_DEVICE_TABLE(of, airoha_cpufreq_match_list);
114*84cf9e54SChristian Marangi 
115*84cf9e54SChristian Marangi static int __init airoha_cpufreq_init(void)
116*84cf9e54SChristian Marangi {
117*84cf9e54SChristian Marangi 	struct device_node *np = of_find_node_by_path("/");
118*84cf9e54SChristian Marangi 	const struct of_device_id *match;
119*84cf9e54SChristian Marangi 	int ret;
120*84cf9e54SChristian Marangi 
121*84cf9e54SChristian Marangi 	if (!np)
122*84cf9e54SChristian Marangi 		return -ENODEV;
123*84cf9e54SChristian Marangi 
124*84cf9e54SChristian Marangi 	match = of_match_node(airoha_cpufreq_match_list, np);
125*84cf9e54SChristian Marangi 	of_node_put(np);
126*84cf9e54SChristian Marangi 	if (!match)
127*84cf9e54SChristian Marangi 		return -ENODEV;
128*84cf9e54SChristian Marangi 
129*84cf9e54SChristian Marangi 	ret = platform_driver_register(&airoha_cpufreq_driver);
130*84cf9e54SChristian Marangi 	if (unlikely(ret < 0))
131*84cf9e54SChristian Marangi 		return ret;
132*84cf9e54SChristian Marangi 
133*84cf9e54SChristian Marangi 	cpufreq_pdev = platform_device_register_data(NULL, "airoha-cpufreq",
134*84cf9e54SChristian Marangi 						     -1, match, sizeof(*match));
135*84cf9e54SChristian Marangi 	ret = PTR_ERR_OR_ZERO(cpufreq_pdev);
136*84cf9e54SChristian Marangi 	if (ret)
137*84cf9e54SChristian Marangi 		platform_driver_unregister(&airoha_cpufreq_driver);
138*84cf9e54SChristian Marangi 
139*84cf9e54SChristian Marangi 	return ret;
140*84cf9e54SChristian Marangi }
141*84cf9e54SChristian Marangi module_init(airoha_cpufreq_init);
142*84cf9e54SChristian Marangi 
143*84cf9e54SChristian Marangi static void __exit airoha_cpufreq_exit(void)
144*84cf9e54SChristian Marangi {
145*84cf9e54SChristian Marangi 	platform_device_unregister(cpufreq_pdev);
146*84cf9e54SChristian Marangi 	platform_driver_unregister(&airoha_cpufreq_driver);
147*84cf9e54SChristian Marangi }
148*84cf9e54SChristian Marangi module_exit(airoha_cpufreq_exit);
149*84cf9e54SChristian Marangi 
150*84cf9e54SChristian Marangi MODULE_AUTHOR("Christian Marangi <ansuelsmth@gmail.com>");
151*84cf9e54SChristian Marangi MODULE_DESCRIPTION("CPUfreq driver for Airoha SoCs");
152*84cf9e54SChristian Marangi MODULE_LICENSE("GPL");
153