ramips: refresh patches
[oweals/openwrt.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 --- a/drivers/cpufreq/qcom-cpufreq-nvmem.c
24 +++ b/drivers/cpufreq/qcom-cpufreq-nvmem.c
25 @@ -43,6 +43,20 @@ enum _msm8996_version {
26         NUM_OF_MSM8996_VERSIONS,
27  };
28  
29 +struct qcom_cpufreq_drv;
30 +
31 +struct qcom_cpufreq_match_data {
32 +       int (*get_version)(struct device *cpu_dev,
33 +                          struct nvmem_cell *speedbin_nvmem,
34 +                          struct qcom_cpufreq_drv *drv);
35 +};
36 +
37 +struct qcom_cpufreq_drv {
38 +       struct opp_table **opp_tables;
39 +       u32 versions;
40 +       const struct qcom_cpufreq_match_data *data;
41 +};
42 +
43  static struct platform_device *cpufreq_dt_pdev, *cpufreq_pdev;
44  
45  static enum _msm8996_version qcom_cpufreq_get_msm_id(void)
46 @@ -76,7 +90,7 @@ static enum _msm8996_version qcom_cpufre
47  
48  static int qcom_cpufreq_kryo_name_version(struct device *cpu_dev,
49                                           struct nvmem_cell *speedbin_nvmem,
50 -                                         u32 *versions)
51 +                                         struct qcom_cpufreq_drv *drv)
52  {
53         size_t len;
54         u8 *speedbin;
55 @@ -94,10 +108,10 @@ static int qcom_cpufreq_kryo_name_versio
56  
57         switch (msm8996_version) {
58         case MSM8996_V3:
59 -               *versions = 1 << (unsigned int)(*speedbin);
60 +               drv->versions = 1 << (unsigned int)(*speedbin);
61                 break;
62         case MSM8996_SG:
63 -               *versions = 1 << ((unsigned int)(*speedbin) + 4);
64 +               drv->versions = 1 << ((unsigned int)(*speedbin) + 4);
65                 break;
66         default:
67                 BUG();
68 @@ -108,17 +122,17 @@ static int qcom_cpufreq_kryo_name_versio
69         return 0;
70  }
71  
72 +static const struct qcom_cpufreq_match_data match_data_kryo = {
73 +       .get_version = qcom_cpufreq_kryo_name_version,
74 +};
75 +
76  static int qcom_cpufreq_probe(struct platform_device *pdev)
77  {
78 -       struct opp_table **opp_tables;
79 -       int (*get_version)(struct device *cpu_dev,
80 -                          struct nvmem_cell *speedbin_nvmem,
81 -                          u32 *versions);
82 +       struct qcom_cpufreq_drv *drv;
83         struct nvmem_cell *speedbin_nvmem;
84         struct device_node *np;
85         struct device *cpu_dev;
86         unsigned cpu;
87 -       u32 versions;
88         const struct of_device_id *match;
89         int ret;
90  
91 @@ -126,11 +140,6 @@ static int qcom_cpufreq_probe(struct pla
92         if (!cpu_dev)
93                 return -ENODEV;
94  
95 -       match = pdev->dev.platform_data;
96 -       get_version = match->data;
97 -       if (!get_version)
98 -               return -ENODEV;
99 -
100         np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
101         if (!np)
102                 return -ENOENT;
103 @@ -141,23 +150,43 @@ static int qcom_cpufreq_probe(struct pla
104                 return -ENOENT;
105         }
106  
107 -       speedbin_nvmem = of_nvmem_cell_get(np, NULL);
108 -       of_node_put(np);
109 -       if (IS_ERR(speedbin_nvmem)) {
110 -               if (PTR_ERR(speedbin_nvmem) != -EPROBE_DEFER)
111 -                       dev_err(cpu_dev, "Could not get nvmem cell: %ld\n",
112 -                               PTR_ERR(speedbin_nvmem));
113 -               return PTR_ERR(speedbin_nvmem);
114 +       drv = kzalloc(sizeof(*drv), GFP_KERNEL);
115 +       if (!drv)
116 +               return -ENOMEM;
117 +
118 +       match = pdev->dev.platform_data;
119 +       drv->data = match->data;
120 +       if (!drv->data) {
121 +               ret = -ENODEV;
122 +               goto free_drv;
123         }
124  
125 -       ret = get_version(cpu_dev, speedbin_nvmem, &versions);
126 -       nvmem_cell_put(speedbin_nvmem);
127 -       if (ret)
128 -               return ret;
129 +       if (drv->data->get_version) {
130 +               speedbin_nvmem = of_nvmem_cell_get(np, NULL);
131 +               if (IS_ERR(speedbin_nvmem)) {
132 +                       if (PTR_ERR(speedbin_nvmem) != -EPROBE_DEFER)
133 +                               dev_err(cpu_dev,
134 +                                       "Could not get nvmem cell: %ld\n",
135 +                                       PTR_ERR(speedbin_nvmem));
136 +                       ret = PTR_ERR(speedbin_nvmem);
137 +                       goto free_drv;
138 +               }
139  
140 -       opp_tables = kcalloc(num_possible_cpus(), sizeof(*opp_tables), GFP_KERNEL);
141 -       if (!opp_tables)
142 -               return -ENOMEM;
143 +               ret = drv->data->get_version(cpu_dev, speedbin_nvmem, drv);
144 +               if (ret) {
145 +                       nvmem_cell_put(speedbin_nvmem);
146 +                       goto free_drv;
147 +               }
148 +               nvmem_cell_put(speedbin_nvmem);
149 +       }
150 +       of_node_put(np);
151 +
152 +       drv->opp_tables = kcalloc(num_possible_cpus(), sizeof(*drv->opp_tables),
153 +                                 GFP_KERNEL);
154 +       if (!drv->opp_tables) {
155 +               ret = -ENOMEM;
156 +               goto free_drv;
157 +       }
158  
159         for_each_possible_cpu(cpu) {
160                 cpu_dev = get_cpu_device(cpu);
161 @@ -166,19 +195,23 @@ static int qcom_cpufreq_probe(struct pla
162                         goto free_opp;
163                 }
164  
165 -               opp_tables[cpu] = dev_pm_opp_set_supported_hw(cpu_dev,
166 -                                                             &versions, 1);
167 -               if (IS_ERR(opp_tables[cpu])) {
168 -                       ret = PTR_ERR(opp_tables[cpu]);
169 -                       dev_err(cpu_dev, "Failed to set supported hardware\n");
170 -                       goto free_opp;
171 +               if (drv->data->get_version) {
172 +                       drv->opp_tables[cpu] =
173 +                               dev_pm_opp_set_supported_hw(cpu_dev,
174 +                                                           &drv->versions, 1);
175 +                       if (IS_ERR(drv->opp_tables[cpu])) {
176 +                               ret = PTR_ERR(drv->opp_tables[cpu]);
177 +                               dev_err(cpu_dev,
178 +                                       "Failed to set supported hardware\n");
179 +                               goto free_opp;
180 +                       }
181                 }
182         }
183  
184         cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
185                                                           NULL, 0);
186         if (!IS_ERR(cpufreq_dt_pdev)) {
187 -               platform_set_drvdata(pdev, opp_tables);
188 +               platform_set_drvdata(pdev, drv);
189                 return 0;
190         }
191  
192 @@ -187,26 +220,30 @@ static int qcom_cpufreq_probe(struct pla
193  
194  free_opp:
195         for_each_possible_cpu(cpu) {
196 -               if (IS_ERR_OR_NULL(opp_tables[cpu]))
197 +               if (IS_ERR_OR_NULL(drv->opp_tables[cpu]))
198                         break;
199 -               dev_pm_opp_put_supported_hw(opp_tables[cpu]);
200 +               dev_pm_opp_put_supported_hw(drv->opp_tables[cpu]);
201         }
202 -       kfree(opp_tables);
203 +       kfree(drv->opp_tables);
204 +free_drv:
205 +       kfree(drv);
206  
207         return ret;
208  }
209  
210  static int qcom_cpufreq_remove(struct platform_device *pdev)
211  {
212 -       struct opp_table **opp_tables = platform_get_drvdata(pdev);
213 +       struct qcom_cpufreq_drv *drv = platform_get_drvdata(pdev);
214         unsigned int cpu;
215  
216         platform_device_unregister(cpufreq_dt_pdev);
217  
218         for_each_possible_cpu(cpu)
219 -               dev_pm_opp_put_supported_hw(opp_tables[cpu]);
220 +               if (drv->opp_tables[cpu])
221 +                       dev_pm_opp_put_supported_hw(drv->opp_tables[cpu]);
222  
223 -       kfree(opp_tables);
224 +       kfree(drv->opp_tables);
225 +       kfree(drv);
226  
227         return 0;
228  }
229 @@ -220,10 +257,8 @@ static struct platform_driver qcom_cpufr
230  };
231  
232  static const struct of_device_id qcom_cpufreq_match_list[] __initconst = {
233 -       { .compatible = "qcom,apq8096",
234 -         .data = qcom_cpufreq_kryo_name_version },
235 -       { .compatible = "qcom,msm8996",
236 -         .data = qcom_cpufreq_kryo_name_version },
237 +       { .compatible = "qcom,apq8096", .data = &match_data_kryo },
238 +       { .compatible = "qcom,msm8996", .data = &match_data_kryo },
239         {},
240  };
241