aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ipq806x/patches-5.4/098-3-add-fab-scaling-support-with-cpufreq.patch
blob: fe9143218f3d7568760eda6e2a9866642abd1d6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
--- a/drivers/clk/qcom/Makefile
+++ b/drivers/clk/qcom/Makefile
@@ -15,6 +15,7 @@ clk-qcom-$(CONFIG_KRAIT_CLOCKS) += clk-k
 clk-qcom-y += clk-hfpll.o
 clk-qcom-y += reset.o
 clk-qcom-$(CONFIG_QCOM_GDSC) += gdsc.o
+clk-qcom-y += fab_scaling.o
 
 # Keep alphabetically sorted by config
 obj-$(CONFIG_APQ_GCC_8084) += gcc-apq8084.o
--- /dev/null
+++ b/drivers/clk/qcom/fab_scaling.c
@@ -0,0 +1,172 @@
+/*
+ * Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/clk.h>
+#include <linux/clk-provider.h>
+#include <linux/slab.h>
+#include <linux/fab_scaling.h>
+
+struct qcom_fab_scaling_data {
+	u32 fab_freq_high;
+	u32 fab_freq_nominal;
+	u32 cpu_freq_threshold;
+	struct clk *apps_fab_clk;
+	struct clk *ddr_fab_clk;
+};
+
+static struct qcom_fab_scaling_data *drv_data;
+
+int scale_fabrics(unsigned long max_cpu_freq)
+{	
+	struct clk *apps_fab_clk = drv_data->apps_fab_clk,
+	           *ddr_fab_clk = drv_data->ddr_fab_clk;
+	unsigned long target_freq, cur_freq;
+	int ret;
+
+	/* Skip fab scaling if the driver is not ready */
+	if (!apps_fab_clk || !ddr_fab_clk)
+		return 0;
+
+	if (max_cpu_freq > drv_data->cpu_freq_threshold)
+		target_freq = drv_data->fab_freq_high;
+	else
+		target_freq = drv_data->fab_freq_nominal;
+
+	cur_freq = clk_get_rate(ddr_fab_clk);
+
+	if (target_freq != cur_freq) {
+		ret = clk_set_rate(apps_fab_clk, target_freq);
+		if (ret)
+			return ret;
+		ret = clk_set_rate(ddr_fab_clk, target_freq);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(scale_fabrics);
+
+static int ipq806x_fab_scaling_probe(struct platform_device *pdev)
+{
+	struct device_node *np = pdev->dev.of_node;
+	struct clk *apps_fab_clk, *ddr_fab_clk;
+	int ret;
+
+	if (!np)
+		return -ENODEV;
+	
+	drv_data = kzalloc(sizeof(*drv_data), GFP_KERNEL);
+	if (!drv_data)
+		return -ENOMEM;
+
+	if (of_property_read_u32(np, "fab_freq_high", &drv_data->fab_freq_high)) {
+		pr_err("FABRICS turbo freq not found. Using defaults...\n");
+		drv_data->fab_freq_high = 533000000;
+	}
+
+	if (of_property_read_u32(np, "fab_freq_nominal", &drv_data->fab_freq_nominal)) {
+		pr_err("FABRICS nominal freq not found. Using defaults...\n");
+		drv_data->fab_freq_nominal = 400000000;
+	}
+
+	if (of_property_read_u32(np, "cpu_freq_threshold", &drv_data->cpu_freq_threshold)) {
+		pr_err("FABRICS cpu freq threshold not found. Using defaults...\n");
+		drv_data->cpu_freq_threshold = 1000000000;
+	}
+
+	apps_fab_clk = devm_clk_get(&pdev->dev, "apps-fab-clk");
+	ret = PTR_ERR_OR_ZERO(apps_fab_clk);
+	if (ret) {
+		/*
+		 * If apps fab clk node is present, but clock is not yet
+		 * registered, we should try defering probe.
+		 */
+		if (ret != -EPROBE_DEFER) {
+			pr_err("Failed to get APPS FABRIC clock: %d\n", ret);
+			ret = -ENODEV;
+		}
+		goto err;
+	}
+
+	clk_prepare_enable(apps_fab_clk);
+	clk_set_rate(apps_fab_clk, drv_data->fab_freq_high);
+	drv_data->apps_fab_clk = apps_fab_clk;
+
+	ddr_fab_clk = devm_clk_get(&pdev->dev, "ddr-fab-clk");
+	ret = PTR_ERR_OR_ZERO(ddr_fab_clk);
+	if (ret) {
+		/*
+		 * If ddr fab clk node is present, but clock is not yet
+		 * registered, we should try defering probe.
+		 */
+		if (ret != -EPROBE_DEFER) {
+			pr_err("Failed to get DDR FABRIC clock: %d\n", ret);
+			ddr_fab_clk = NULL;
+			ret = -ENODEV;
+		}
+		goto err;
+	}
+
+	clk_prepare_enable(ddr_fab_clk);
+	clk_set_rate(ddr_fab_clk, drv_data->fab_freq_high);
+	drv_data->ddr_fab_clk = ddr_fab_clk;
+
+	return 0;
+err:
+	kfree(drv_data);
+	return ret;
+}
+
+static int ipq806x_fab_scaling_remove(struct platform_device *pdev)
+{
+	kfree(drv_data);
+	return 0;
+}
+
+static const struct of_device_id fab_scaling_ipq806x_match_table[] = {
+	{ .compatible = "qcom,fab-scaling" },
+	{ }
+};
+
+static struct platform_driver fab_scaling_ipq806x_driver = {
+	.probe		= ipq806x_fab_scaling_probe,
+	.remove		= ipq806x_fab_scaling_remove,
+	.driver		= {
+		.name   = "fab-scaling",
+		.of_match_table = fab_scaling_ipq806x_match_table,
+	},
+};
+
+static int __init fab_scaling_ipq806x_init(void)
+{
+	return platform_driver_register(&fab_scaling_ipq806x_driver);
+}
+late_initcall(fab_scaling_ipq806x_init);
+
+static void __exit fab_scaling_ipq806x_exit(void)
+{
+	platform_driver_unregister(&fab_scaling_ipq806x_driver);
+}
+module_exit(fab_scaling_ipq806x_exit);
--- /dev/null
+++ b/include/linux/fab_scaling.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+
+#ifndef __FAB_SCALING_H
+#define __FAB_SCALING_H
+
+/**
+ * scale_fabrics - Scale DDR and APPS FABRICS
+ *
+ * This function monitors all the registered clocks and does APPS
+ * and DDR FABRIC scaling based on the idle frequencies with which
+ * it was registered.
+ *
+ */
+int scale_fabrics(unsigned long max_cpu_freq);
+
+#endif
--- a/drivers/cpufreq/qcom-cpufreq-krait.c
+++ b/drivers/cpufreq/qcom-cpufreq-krait.c
@@ -15,6 +15,7 @@
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
 #include <linux/thermal.h>
+#include <linux/fab_scaling.h>
 
 #include "cpufreq-dt.h"
 
@@ -54,6 +55,13 @@ static int set_target(struct cpufreq_pol
 		level = dev_pm_opp_get_level(opp);
 		dev_pm_opp_put(opp);
 
+		/*
+		 * Scale fabrics with max freq across all cores
+		 */
+		ret = scale_fabrics(target_freq);
+		if (ret)
+			return ret;
+
 		opp = dev_pm_opp_find_level_exact(&l2_pdev->dev, level);
 		if (IS_ERR(opp)) {
 			dev_err(&l2_pdev->dev,