hostapd: Fix compile against mbedtsl 3.6
[openwrt/staging/nbd.git] / target / linux / ipq806x / patches-6.1 / 115-01-devfreq-add-ipq806x-fabric-scaling-driver.patch
1 From 13f075999935bb696dbab63243923179f06fa05e Mon Sep 17 00:00:00 2001
2 From: Christian Marangi <ansuelsmth@gmail.com>
3 Date: Thu, 16 Jun 2022 19:56:08 +0200
4 Subject: [PATCH 3/4] devfreq: add ipq806x fabric scaling driver
5
6 Add ipq806x fabric scaling driver using the devfreq passive governor.
7
8 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
9 ---
10 drivers/devfreq/Kconfig | 11 ++
11 drivers/devfreq/Makefile | 1 +
12 drivers/devfreq/ipq806x-fab-devfreq.c | 155 ++++++++++++++++++++++++++
13 3 files changed, 167 insertions(+)
14 create mode 100644 drivers/devfreq/ipq806x-fab-devfreq.c
15
16 --- a/drivers/devfreq/Kconfig
17 +++ b/drivers/devfreq/Kconfig
18 @@ -162,6 +162,17 @@ config ARM_KRAIT_CACHE_DEVFREQ
19 based on the max frequency across all core and the range set in the device
20 dts. If provided this scale also the regulator attached to the l2 cache.
21
22 +config ARM_IPQ806X_FAB_DEVFREQ
23 + tristate "Scaling support for ipq806x Soc Fabric"
24 + depends on ARCH_QCOM || COMPILE_TEST
25 + select DEVFREQ_GOV_PASSIVE
26 + help
27 + This adds the DEVFREQ driver for the ipq806x Soc Fabric.
28 +
29 + The driver register with the cpufreq notifier and find the right frequency
30 + based on the max frequency across all core and the range set in the device
31 + dts.
32 +
33 source "drivers/devfreq/event/Kconfig"
34
35 endif # PM_DEVFREQ
36 --- a/drivers/devfreq/Makefile
37 +++ b/drivers/devfreq/Makefile
38 @@ -16,6 +16,7 @@ obj-$(CONFIG_ARM_RK3399_DMC_DEVFREQ) +=
39 obj-$(CONFIG_ARM_SUN8I_A33_MBUS_DEVFREQ) += sun8i-a33-mbus.o
40 obj-$(CONFIG_ARM_TEGRA_DEVFREQ) += tegra30-devfreq.o
41 obj-$(CONFIG_ARM_KRAIT_CACHE_DEVFREQ) += krait-cache-devfreq.o
42 +obj-$(CONFIG_ARM_IPQ806X_FAB_DEVFREQ) += ipq806x-fab-devfreq.o
43
44 # DEVFREQ Event Drivers
45 obj-$(CONFIG_PM_DEVFREQ_EVENT) += event/
46 --- /dev/null
47 +++ b/drivers/devfreq/ipq806x-fab-devfreq.c
48 @@ -0,0 +1,155 @@
49 +// SPDX-License-Identifier: GPL-2.0
50 +
51 +#include <linux/kernel.h>
52 +#include <linux/init.h>
53 +#include <linux/module.h>
54 +#include <linux/cpufreq.h>
55 +#include <linux/devfreq.h>
56 +#include <linux/of.h>
57 +#include <linux/platform_device.h>
58 +#include <linux/clk.h>
59 +#include <linux/slab.h>
60 +#include <linux/pm_opp.h>
61 +
62 +#include "governor.h"
63 +
64 +struct ipq806x_fab_data {
65 + struct clk *fab_clk;
66 + struct clk *ddr_clk;
67 +};
68 +
69 +static int ipq806x_fab_get_cur_freq(struct device *dev, unsigned long *freq)
70 +{
71 + struct ipq806x_fab_data *data = dev_get_drvdata(dev);
72 +
73 + *freq = clk_get_rate(data->fab_clk);
74 +
75 + return 0;
76 +};
77 +
78 +static int ipq806x_fab_target(struct device *dev, unsigned long *freq,
79 + u32 flags)
80 +{
81 + struct ipq806x_fab_data *data = dev_get_drvdata(dev);
82 + struct dev_pm_opp *opp;
83 + int ret;
84 +
85 + opp = dev_pm_opp_find_freq_ceil(dev, freq);
86 + if (unlikely(IS_ERR(opp)))
87 + return PTR_ERR(opp);
88 +
89 + dev_pm_opp_put(opp);
90 +
91 + ret = clk_set_rate(data->fab_clk, *freq);
92 + if (ret)
93 + return ret;
94 +
95 + return clk_set_rate(data->ddr_clk, *freq);
96 +};
97 +
98 +static int ipq806x_fab_get_dev_status(struct device *dev,
99 + struct devfreq_dev_status *stat)
100 +{
101 + struct ipq806x_fab_data *data = dev_get_drvdata(dev);
102 +
103 + stat->busy_time = 0;
104 + stat->total_time = 0;
105 + stat->current_frequency = clk_get_rate(data->fab_clk);
106 +
107 + return 0;
108 +};
109 +
110 +static struct devfreq_dev_profile ipq806x_fab_devfreq_profile = {
111 + .target = ipq806x_fab_target,
112 + .get_dev_status = ipq806x_fab_get_dev_status,
113 + .get_cur_freq = ipq806x_fab_get_cur_freq
114 +};
115 +
116 +static struct devfreq_passive_data devfreq_gov_data = {
117 + .parent_type = CPUFREQ_PARENT_DEV,
118 +};
119 +
120 +static int ipq806x_fab_probe(struct platform_device *pdev)
121 +{
122 + struct device *dev = &pdev->dev;
123 + struct ipq806x_fab_data *data;
124 + struct devfreq *devfreq;
125 + struct clk *clk;
126 + int ret;
127 +
128 + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
129 + if (!data)
130 + return -ENOMEM;
131 +
132 + clk = devm_clk_get(dev, "apps-fab-clk");
133 + if (IS_ERR(clk)) {
134 + dev_err_probe(dev, PTR_ERR(clk), "failed to get apps fab clk\n");
135 + return PTR_ERR(clk);
136 + }
137 +
138 + clk_prepare_enable(clk);
139 + data->fab_clk = clk;
140 +
141 + clk = devm_clk_get(dev, "ddr-fab-clk");
142 + if (IS_ERR(clk)) {
143 + dev_err_probe(dev, PTR_ERR(clk), "failed to get ddr fab clk\n");
144 + goto err_ddr;
145 + }
146 +
147 + clk_prepare_enable(clk);
148 + data->ddr_clk = clk;
149 +
150 + ret = dev_pm_opp_of_add_table(dev);
151 + if (ret) {
152 + dev_err(dev, "failed to parse fab freq thresholds\n");
153 + return ret;
154 + }
155 +
156 + dev_set_drvdata(dev, data);
157 +
158 + devfreq = devm_devfreq_add_device(&pdev->dev, &ipq806x_fab_devfreq_profile,
159 + DEVFREQ_GOV_PASSIVE, &devfreq_gov_data);
160 + if (IS_ERR(devfreq))
161 + dev_pm_opp_remove_table(dev);
162 +
163 + return PTR_ERR_OR_ZERO(devfreq);
164 +
165 +err_ddr:
166 + clk_unprepare(data->fab_clk);
167 + clk_put(data->fab_clk);
168 + return PTR_ERR(clk);
169 +};
170 +
171 +static int ipq806x_fab_remove(struct platform_device *pdev)
172 +{
173 + struct ipq806x_fab_data *data = dev_get_drvdata(&pdev->dev);
174 +
175 + clk_unprepare(data->fab_clk);
176 + clk_put(data->fab_clk);
177 +
178 + clk_unprepare(data->ddr_clk);
179 + clk_put(data->ddr_clk);
180 +
181 + dev_pm_opp_remove_table(&pdev->dev);
182 +
183 + return 0;
184 +};
185 +
186 +static const struct of_device_id ipq806x_fab_match_table[] = {
187 + { .compatible = "qcom,fab-scaling" },
188 + {}
189 +};
190 +
191 +static struct platform_driver ipq806x_fab_driver = {
192 + .probe = ipq806x_fab_probe,
193 + .remove = ipq806x_fab_remove,
194 + .driver = {
195 + .name = "ipq806x-fab-scaling",
196 + .of_match_table = ipq806x_fab_match_table,
197 + },
198 +};
199 +module_platform_driver(ipq806x_fab_driver);
200 +
201 +MODULE_DESCRIPTION("ipq806x Fab Scaling driver");
202 +MODULE_AUTHOR("Christian Marangi <ansuelsmth@gmail.com>");
203 +MODULE_LICENSE("GPL v2");