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
7 Refactor the driver to make it easier to extend in a later commit.
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.
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>
20 drivers/cpufreq/qcom-cpufreq-nvmem.c | 123 +++++++++++++++++----------
21 1 file changed, 79 insertions(+), 44 deletions(-)
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,
29 +struct qcom_cpufreq_drv;
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);
37 +struct qcom_cpufreq_drv {
38 + struct opp_table **opp_tables;
40 + const struct qcom_cpufreq_match_data *data;
43 static struct platform_device *cpufreq_dt_pdev, *cpufreq_pdev;
45 static enum _msm8996_version qcom_cpufreq_get_msm_id(void)
46 @@ -76,7 +90,7 @@ static enum _msm8996_version qcom_cpufre
48 static int qcom_cpufreq_kryo_name_version(struct device *cpu_dev,
49 struct nvmem_cell *speedbin_nvmem,
51 + struct qcom_cpufreq_drv *drv)
55 @@ -94,10 +108,10 @@ static int qcom_cpufreq_kryo_name_versio
57 switch (msm8996_version) {
59 - *versions = 1 << (unsigned int)(*speedbin);
60 + drv->versions = 1 << (unsigned int)(*speedbin);
63 - *versions = 1 << ((unsigned int)(*speedbin) + 4);
64 + drv->versions = 1 << ((unsigned int)(*speedbin) + 4);
68 @@ -108,17 +122,17 @@ static int qcom_cpufreq_kryo_name_versio
72 +static const struct qcom_cpufreq_match_data match_data_kryo = {
73 + .get_version = qcom_cpufreq_kryo_name_version,
76 static int qcom_cpufreq_probe(struct platform_device *pdev)
78 - struct opp_table **opp_tables;
79 - int (*get_version)(struct device *cpu_dev,
80 - struct nvmem_cell *speedbin_nvmem,
82 + struct qcom_cpufreq_drv *drv;
83 struct nvmem_cell *speedbin_nvmem;
84 struct device_node *np;
85 struct device *cpu_dev;
88 const struct of_device_id *match;
91 @@ -126,11 +140,6 @@ static int qcom_cpufreq_probe(struct pla
95 - match = pdev->dev.platform_data;
96 - get_version = match->data;
100 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
103 @@ -141,23 +150,43 @@ static int qcom_cpufreq_probe(struct pla
107 - speedbin_nvmem = of_nvmem_cell_get(np, NULL);
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);
118 + match = pdev->dev.platform_data;
119 + drv->data = match->data;
125 - ret = get_version(cpu_dev, speedbin_nvmem, &versions);
126 - nvmem_cell_put(speedbin_nvmem);
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)
134 + "Could not get nvmem cell: %ld\n",
135 + PTR_ERR(speedbin_nvmem));
136 + ret = PTR_ERR(speedbin_nvmem);
140 - opp_tables = kcalloc(num_possible_cpus(), sizeof(*opp_tables), GFP_KERNEL);
143 + ret = drv->data->get_version(cpu_dev, speedbin_nvmem, drv);
145 + nvmem_cell_put(speedbin_nvmem);
148 + nvmem_cell_put(speedbin_nvmem);
152 + drv->opp_tables = kcalloc(num_possible_cpus(), sizeof(*drv->opp_tables),
154 + if (!drv->opp_tables) {
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
165 - opp_tables[cpu] = dev_pm_opp_set_supported_hw(cpu_dev,
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");
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]);
178 + "Failed to set supported hardware\n");
184 cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
186 if (!IS_ERR(cpufreq_dt_pdev)) {
187 - platform_set_drvdata(pdev, opp_tables);
188 + platform_set_drvdata(pdev, drv);
192 @@ -187,26 +220,30 @@ static int qcom_cpufreq_probe(struct pla
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]))
199 - dev_pm_opp_put_supported_hw(opp_tables[cpu]);
200 + dev_pm_opp_put_supported_hw(drv->opp_tables[cpu]);
203 + kfree(drv->opp_tables);
210 static int qcom_cpufreq_remove(struct platform_device *pdev)
212 - struct opp_table **opp_tables = platform_get_drvdata(pdev);
213 + struct qcom_cpufreq_drv *drv = platform_get_drvdata(pdev);
216 platform_device_unregister(cpufreq_dt_pdev);
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]);
224 + kfree(drv->opp_tables);
229 @@ -220,10 +257,8 @@ static struct platform_driver qcom_cpufr
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 },