23b319648dc9377f48ce81cd3b3d6990b102d897
[openwrt/staging/jow.git] / target / linux / mediatek / patches-5.15 / 350-09-cpufreq-mediatek-Move-voltage-limits-to-platform-dat.patch
1 From be2354b064e6bafbbad599ae2e10569ba4f7d5a6 Mon Sep 17 00:00:00 2001
2 From: Rex-BC Chen <rex-bc.chen@mediatek.com>
3 Date: Thu, 5 May 2022 19:52:19 +0800
4 Subject: [PATCH 09/21] cpufreq: mediatek: Move voltage limits to platform data
5
6 Voltages and shifts are defined as macros originally.
7 There are different requirements of these values for each MediaTek SoCs.
8 Therefore, we add the platform data and move these values into it.
9
10 Signed-off-by: Jia-Wei Chang <jia-wei.chang@mediatek.com>
11 Signed-off-by: Rex-BC Chen <rex-bc.chen@mediatek.com>
12 Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
13 Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
14 ---
15 drivers/cpufreq/mediatek-cpufreq.c | 84 +++++++++++++++++++++---------
16 1 file changed, 58 insertions(+), 26 deletions(-)
17
18 --- a/drivers/cpufreq/mediatek-cpufreq.c
19 +++ b/drivers/cpufreq/mediatek-cpufreq.c
20 @@ -10,15 +10,21 @@
21 #include <linux/cpumask.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 +#include <linux/of_platform.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_opp.h>
27 #include <linux/regulator/consumer.h>
28
29 -#define MIN_VOLT_SHIFT (100000)
30 -#define MAX_VOLT_SHIFT (200000)
31 -#define MAX_VOLT_LIMIT (1150000)
32 #define VOLT_TOL (10000)
33
34 +struct mtk_cpufreq_platform_data {
35 + int min_volt_shift;
36 + int max_volt_shift;
37 + int proc_max_volt;
38 + int sram_min_volt;
39 + int sram_max_volt;
40 +};
41 +
42 /*
43 * The struct mtk_cpu_dvfs_info holds necessary information for doing CPU DVFS
44 * on each CPU power/clock domain of Mediatek SoCs. Each CPU cluster in
45 @@ -41,6 +47,7 @@ struct mtk_cpu_dvfs_info {
46 int intermediate_voltage;
47 bool need_voltage_tracking;
48 int pre_vproc;
49 + const struct mtk_cpufreq_platform_data *soc_data;
50 };
51
52 static struct platform_device *cpufreq_pdev;
53 @@ -62,6 +69,7 @@ static struct mtk_cpu_dvfs_info *mtk_cpu
54 static int mtk_cpufreq_voltage_tracking(struct mtk_cpu_dvfs_info *info,
55 int new_vproc)
56 {
57 + const struct mtk_cpufreq_platform_data *soc_data = info->soc_data;
58 struct regulator *proc_reg = info->proc_reg;
59 struct regulator *sram_reg = info->sram_reg;
60 int pre_vproc, pre_vsram, new_vsram, vsram, vproc, ret;
61 @@ -73,7 +81,8 @@ static int mtk_cpufreq_voltage_tracking(
62 return pre_vproc;
63 }
64 /* Vsram should not exceed the maximum allowed voltage of SoC. */
65 - new_vsram = min(new_vproc + MIN_VOLT_SHIFT, MAX_VOLT_LIMIT);
66 + new_vsram = min(new_vproc + soc_data->min_volt_shift,
67 + soc_data->sram_max_volt);
68
69 if (pre_vproc < new_vproc) {
70 /*
71 @@ -96,10 +105,11 @@ static int mtk_cpufreq_voltage_tracking(
72 return pre_vproc;
73 }
74
75 - vsram = min(new_vsram, pre_vproc + MAX_VOLT_SHIFT);
76 + vsram = min(new_vsram,
77 + pre_vproc + soc_data->min_volt_shift);
78
79 - if (vsram + VOLT_TOL >= MAX_VOLT_LIMIT) {
80 - vsram = MAX_VOLT_LIMIT;
81 + if (vsram + VOLT_TOL >= soc_data->sram_max_volt) {
82 + vsram = soc_data->sram_max_volt;
83
84 /*
85 * If the target Vsram hits the maximum voltage,
86 @@ -117,7 +127,7 @@ static int mtk_cpufreq_voltage_tracking(
87 ret = regulator_set_voltage(sram_reg, vsram,
88 vsram + VOLT_TOL);
89
90 - vproc = vsram - MIN_VOLT_SHIFT;
91 + vproc = vsram - soc_data->min_volt_shift;
92 }
93 if (ret)
94 return ret;
95 @@ -151,7 +161,8 @@ static int mtk_cpufreq_voltage_tracking(
96 return pre_vsram;
97 }
98
99 - vproc = max(new_vproc, pre_vsram - MAX_VOLT_SHIFT);
100 + vproc = max(new_vproc,
101 + pre_vsram - soc_data->max_volt_shift);
102 ret = regulator_set_voltage(proc_reg, vproc,
103 vproc + VOLT_TOL);
104 if (ret)
105 @@ -160,10 +171,11 @@ static int mtk_cpufreq_voltage_tracking(
106 if (vproc == new_vproc)
107 vsram = new_vsram;
108 else
109 - vsram = max(new_vsram, vproc + MIN_VOLT_SHIFT);
110 + vsram = max(new_vsram,
111 + vproc + soc_data->min_volt_shift);
112
113 - if (vsram + VOLT_TOL >= MAX_VOLT_LIMIT) {
114 - vsram = MAX_VOLT_LIMIT;
115 + if (vsram + VOLT_TOL >= soc_data->sram_max_volt) {
116 + vsram = soc_data->sram_max_volt;
117
118 /*
119 * If the target Vsram hits the maximum voltage,
120 @@ -194,13 +206,14 @@ static int mtk_cpufreq_voltage_tracking(
121
122 static int mtk_cpufreq_set_voltage(struct mtk_cpu_dvfs_info *info, int vproc)
123 {
124 + const struct mtk_cpufreq_platform_data *soc_data = info->soc_data;
125 int ret;
126
127 if (info->need_voltage_tracking)
128 ret = mtk_cpufreq_voltage_tracking(info, vproc);
129 else
130 ret = regulator_set_voltage(info->proc_reg, vproc,
131 - MAX_VOLT_LIMIT);
132 + soc_data->proc_max_volt);
133 if (!ret)
134 info->pre_vproc = vproc;
135
136 @@ -509,9 +522,17 @@ static struct cpufreq_driver mtk_cpufreq
137
138 static int mtk_cpufreq_probe(struct platform_device *pdev)
139 {
140 + const struct mtk_cpufreq_platform_data *data;
141 struct mtk_cpu_dvfs_info *info, *tmp;
142 int cpu, ret;
143
144 + data = dev_get_platdata(&pdev->dev);
145 + if (!data) {
146 + dev_err(&pdev->dev,
147 + "failed to get mtk cpufreq platform data\n");
148 + return -ENODEV;
149 + }
150 +
151 for_each_possible_cpu(cpu) {
152 info = mtk_cpu_dvfs_info_lookup(cpu);
153 if (info)
154 @@ -523,6 +544,7 @@ static int mtk_cpufreq_probe(struct plat
155 goto release_dvfs_info_list;
156 }
157
158 + info->soc_data = data;
159 ret = mtk_cpu_dvfs_info_init(info, cpu);
160 if (ret) {
161 dev_err(&pdev->dev,
162 @@ -558,20 +580,27 @@ static struct platform_driver mtk_cpufre
163 .probe = mtk_cpufreq_probe,
164 };
165
166 +static const struct mtk_cpufreq_platform_data mt2701_platform_data = {
167 + .min_volt_shift = 100000,
168 + .max_volt_shift = 200000,
169 + .proc_max_volt = 1150000,
170 + .sram_min_volt = 0,
171 + .sram_max_volt = 1150000,
172 +};
173 +
174 /* List of machines supported by this driver */
175 static const struct of_device_id mtk_cpufreq_machines[] __initconst = {
176 - { .compatible = "mediatek,mt2701", },
177 - { .compatible = "mediatek,mt2712", },
178 - { .compatible = "mediatek,mt7622", },
179 - { .compatible = "mediatek,mt7623", },
180 - { .compatible = "mediatek,mt8167", },
181 - { .compatible = "mediatek,mt817x", },
182 - { .compatible = "mediatek,mt8173", },
183 - { .compatible = "mediatek,mt8176", },
184 - { .compatible = "mediatek,mt8183", },
185 - { .compatible = "mediatek,mt8365", },
186 - { .compatible = "mediatek,mt8516", },
187 -
188 + { .compatible = "mediatek,mt2701", .data = &mt2701_platform_data },
189 + { .compatible = "mediatek,mt2712", .data = &mt2701_platform_data },
190 + { .compatible = "mediatek,mt7622", .data = &mt2701_platform_data },
191 + { .compatible = "mediatek,mt7623", .data = &mt2701_platform_data },
192 + { .compatible = "mediatek,mt8167", .data = &mt2701_platform_data },
193 + { .compatible = "mediatek,mt817x", .data = &mt2701_platform_data },
194 + { .compatible = "mediatek,mt8173", .data = &mt2701_platform_data },
195 + { .compatible = "mediatek,mt8176", .data = &mt2701_platform_data },
196 + { .compatible = "mediatek,mt8183", .data = &mt2701_platform_data },
197 + { .compatible = "mediatek,mt8365", .data = &mt2701_platform_data },
198 + { .compatible = "mediatek,mt8516", .data = &mt2701_platform_data },
199 { }
200 };
201 MODULE_DEVICE_TABLE(of, mtk_cpufreq_machines);
202 @@ -580,6 +609,7 @@ static int __init mtk_cpufreq_driver_ini
203 {
204 struct device_node *np;
205 const struct of_device_id *match;
206 + const struct mtk_cpufreq_platform_data *data;
207 int err;
208
209 np = of_find_node_by_path("/");
210 @@ -592,6 +622,7 @@ static int __init mtk_cpufreq_driver_ini
211 pr_debug("Machine is not compatible with mtk-cpufreq\n");
212 return -ENODEV;
213 }
214 + data = match->data;
215
216 err = platform_driver_register(&mtk_cpufreq_platdrv);
217 if (err)
218 @@ -603,7 +634,8 @@ static int __init mtk_cpufreq_driver_ini
219 * and the device registration codes are put here to handle defer
220 * probing.
221 */
222 - cpufreq_pdev = platform_device_register_simple("mtk-cpufreq", -1, NULL, 0);
223 + cpufreq_pdev = platform_device_register_data(NULL, "mtk-cpufreq", -1,
224 + data, sizeof(*data));
225 if (IS_ERR(cpufreq_pdev)) {
226 pr_err("failed to register mtk-cpufreq platform device\n");
227 platform_driver_unregister(&mtk_cpufreq_platdrv);