6c519c2c27c397e5ab3b6a0eae5ebca4ec295c43
[openwrt/staging/noltari.git] / target / linux / ipq806x / patches-4.19 / 0037-qcom-cpufreq-nvmem-Refactor-the-driver.patch
1 From 57f2f8b4aa0c6b41a284da82bfa40dc3b2abe9a5 Mon Sep 17 00:00:00 2001
2 From: Niklas Cassel <niklas.cassel@linaro.org>
3 Date: Thu, 25 Jul 2019 12:41:33 +0200
4 Subject: [PATCH] cpufreq: qcom: Refactor the driver to make it easier to
5 extend
6
7 Refactor the driver to make it easier to extend in a later commit.
8
9 Create a driver struct to collect all common resources, in order to make
10 it easier to free up all common resources.
11 Create a driver match_data struct to make it easier to extend the driver
12 with support for new features that might only be supported on certain SoCs.
13
14 Co-developed-by: Jorge Ramirez-Ortiz <jorge.ramirez-ortiz@linaro.org>
15 Signed-off-by: Jorge Ramirez-Ortiz <jorge.ramirez-ortiz@linaro.org>
16 Signed-off-by: Niklas Cassel <niklas.cassel@linaro.org>
17 Reviewed-by: Ilia Lin <ilia.lin@kernel.org>
18 Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
19 ---
20 drivers/cpufreq/qcom-cpufreq-nvmem.c | 123 +++++++++++++++++----------
21 1 file changed, 79 insertions(+), 44 deletions(-)
22
23 diff --git a/drivers/cpufreq/qcom-cpufreq-nvmem.c b/drivers/cpufreq/qcom-cpufreq-nvmem.c
24 index fd08120768af2..2d798a1685c5d 100644
25 --- a/drivers/cpufreq/qcom-cpufreq-nvmem.c
26 +++ b/drivers/cpufreq/qcom-cpufreq-nvmem.c
27 @@ -43,6 +43,20 @@ enum _msm8996_version {
28 NUM_OF_MSM8996_VERSIONS,
29 };
30
31 +struct qcom_cpufreq_drv;
32 +
33 +struct qcom_cpufreq_match_data {
34 + int (*get_version)(struct device *cpu_dev,
35 + struct nvmem_cell *speedbin_nvmem,
36 + struct qcom_cpufreq_drv *drv);
37 +};
38 +
39 +struct qcom_cpufreq_drv {
40 + struct opp_table **opp_tables;
41 + u32 versions;
42 + const struct qcom_cpufreq_match_data *data;
43 +};
44 +
45 static struct platform_device *cpufreq_dt_pdev, *cpufreq_pdev;
46
47 static enum _msm8996_version qcom_cpufreq_get_msm_id(void)
48 @@ -76,7 +90,7 @@ static enum _msm8996_version qcom_cpufreq_get_msm_id(void)
49
50 static int qcom_cpufreq_kryo_name_version(struct device *cpu_dev,
51 struct nvmem_cell *speedbin_nvmem,
52 - u32 *versions)
53 + struct qcom_cpufreq_drv *drv)
54 {
55 size_t len;
56 u8 *speedbin;
57 @@ -94,10 +108,10 @@ static int qcom_cpufreq_kryo_name_version(struct device *cpu_dev,
58
59 switch (msm8996_version) {
60 case MSM8996_V3:
61 - *versions = 1 << (unsigned int)(*speedbin);
62 + drv->versions = 1 << (unsigned int)(*speedbin);
63 break;
64 case MSM8996_SG:
65 - *versions = 1 << ((unsigned int)(*speedbin) + 4);
66 + drv->versions = 1 << ((unsigned int)(*speedbin) + 4);
67 break;
68 default:
69 BUG();
70 @@ -108,17 +122,17 @@ static int qcom_cpufreq_kryo_name_version(struct device *cpu_dev,
71 return 0;
72 }
73
74 +static const struct qcom_cpufreq_match_data match_data_kryo = {
75 + .get_version = qcom_cpufreq_kryo_name_version,
76 +};
77 +
78 static int qcom_cpufreq_probe(struct platform_device *pdev)
79 {
80 - struct opp_table **opp_tables;
81 - int (*get_version)(struct device *cpu_dev,
82 - struct nvmem_cell *speedbin_nvmem,
83 - u32 *versions);
84 + struct qcom_cpufreq_drv *drv;
85 struct nvmem_cell *speedbin_nvmem;
86 struct device_node *np;
87 struct device *cpu_dev;
88 unsigned cpu;
89 - u32 versions;
90 const struct of_device_id *match;
91 int ret;
92
93 @@ -126,11 +140,6 @@ static int qcom_cpufreq_probe(struct platform_device *pdev)
94 if (!cpu_dev)
95 return -ENODEV;
96
97 - match = pdev->dev.platform_data;
98 - get_version = match->data;
99 - if (!get_version)
100 - return -ENODEV;
101 -
102 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
103 if (!np)
104 return -ENOENT;
105 @@ -141,23 +150,43 @@ static int qcom_cpufreq_probe(struct platform_device *pdev)
106 return -ENOENT;
107 }
108
109 - speedbin_nvmem = of_nvmem_cell_get(np, NULL);
110 - of_node_put(np);
111 - if (IS_ERR(speedbin_nvmem)) {
112 - if (PTR_ERR(speedbin_nvmem) != -EPROBE_DEFER)
113 - dev_err(cpu_dev, "Could not get nvmem cell: %ld\n",
114 - PTR_ERR(speedbin_nvmem));
115 - return PTR_ERR(speedbin_nvmem);
116 + drv = kzalloc(sizeof(*drv), GFP_KERNEL);
117 + if (!drv)
118 + return -ENOMEM;
119 +
120 + match = pdev->dev.platform_data;
121 + drv->data = match->data;
122 + if (!drv->data) {
123 + ret = -ENODEV;
124 + goto free_drv;
125 }
126
127 - ret = get_version(cpu_dev, speedbin_nvmem, &versions);
128 - nvmem_cell_put(speedbin_nvmem);
129 - if (ret)
130 - return ret;
131 + if (drv->data->get_version) {
132 + speedbin_nvmem = of_nvmem_cell_get(np, NULL);
133 + if (IS_ERR(speedbin_nvmem)) {
134 + if (PTR_ERR(speedbin_nvmem) != -EPROBE_DEFER)
135 + dev_err(cpu_dev,
136 + "Could not get nvmem cell: %ld\n",
137 + PTR_ERR(speedbin_nvmem));
138 + ret = PTR_ERR(speedbin_nvmem);
139 + goto free_drv;
140 + }
141
142 - opp_tables = kcalloc(num_possible_cpus(), sizeof(*opp_tables), GFP_KERNEL);
143 - if (!opp_tables)
144 - return -ENOMEM;
145 + ret = drv->data->get_version(cpu_dev, speedbin_nvmem, drv);
146 + if (ret) {
147 + nvmem_cell_put(speedbin_nvmem);
148 + goto free_drv;
149 + }
150 + nvmem_cell_put(speedbin_nvmem);
151 + }
152 + of_node_put(np);
153 +
154 + drv->opp_tables = kcalloc(num_possible_cpus(), sizeof(*drv->opp_tables),
155 + GFP_KERNEL);
156 + if (!drv->opp_tables) {
157 + ret = -ENOMEM;
158 + goto free_drv;
159 + }
160
161 for_each_possible_cpu(cpu) {
162 cpu_dev = get_cpu_device(cpu);
163 @@ -166,19 +195,23 @@ static int qcom_cpufreq_probe(struct platform_device *pdev)
164 goto free_opp;
165 }
166
167 - opp_tables[cpu] = dev_pm_opp_set_supported_hw(cpu_dev,
168 - &versions, 1);
169 - if (IS_ERR(opp_tables[cpu])) {
170 - ret = PTR_ERR(opp_tables[cpu]);
171 - dev_err(cpu_dev, "Failed to set supported hardware\n");
172 - goto free_opp;
173 + if (drv->data->get_version) {
174 + drv->opp_tables[cpu] =
175 + dev_pm_opp_set_supported_hw(cpu_dev,
176 + &drv->versions, 1);
177 + if (IS_ERR(drv->opp_tables[cpu])) {
178 + ret = PTR_ERR(drv->opp_tables[cpu]);
179 + dev_err(cpu_dev,
180 + "Failed to set supported hardware\n");
181 + goto free_opp;
182 + }
183 }
184 }
185
186 cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
187 NULL, 0);
188 if (!IS_ERR(cpufreq_dt_pdev)) {
189 - platform_set_drvdata(pdev, opp_tables);
190 + platform_set_drvdata(pdev, drv);
191 return 0;
192 }
193
194 @@ -187,26 +220,30 @@ static int qcom_cpufreq_probe(struct platform_device *pdev)
195
196 free_opp:
197 for_each_possible_cpu(cpu) {
198 - if (IS_ERR_OR_NULL(opp_tables[cpu]))
199 + if (IS_ERR_OR_NULL(drv->opp_tables[cpu]))
200 break;
201 - dev_pm_opp_put_supported_hw(opp_tables[cpu]);
202 + dev_pm_opp_put_supported_hw(drv->opp_tables[cpu]);
203 }
204 - kfree(opp_tables);
205 + kfree(drv->opp_tables);
206 +free_drv:
207 + kfree(drv);
208
209 return ret;
210 }
211
212 static int qcom_cpufreq_remove(struct platform_device *pdev)
213 {
214 - struct opp_table **opp_tables = platform_get_drvdata(pdev);
215 + struct qcom_cpufreq_drv *drv = platform_get_drvdata(pdev);
216 unsigned int cpu;
217
218 platform_device_unregister(cpufreq_dt_pdev);
219
220 for_each_possible_cpu(cpu)
221 - dev_pm_opp_put_supported_hw(opp_tables[cpu]);
222 + if (drv->opp_tables[cpu])
223 + dev_pm_opp_put_supported_hw(drv->opp_tables[cpu]);
224
225 - kfree(opp_tables);
226 + kfree(drv->opp_tables);
227 + kfree(drv);
228
229 return 0;
230 }
231 @@ -220,10 +257,8 @@ static struct platform_driver qcom_cpufreq_driver = {
232 };
233
234 static const struct of_device_id qcom_cpufreq_match_list[] __initconst = {
235 - { .compatible = "qcom,apq8096",
236 - .data = qcom_cpufreq_kryo_name_version },
237 - { .compatible = "qcom,msm8996",
238 - .data = qcom_cpufreq_kryo_name_version },
239 + { .compatible = "qcom,apq8096", .data = &match_data_kryo },
240 + { .compatible = "qcom,msm8996", .data = &match_data_kryo },
241 {},
242 };
243