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 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,
31 +struct qcom_cpufreq_drv;
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);
39 +struct qcom_cpufreq_drv {
40 + struct opp_table **opp_tables;
42 + const struct qcom_cpufreq_match_data *data;
45 static struct platform_device *cpufreq_dt_pdev, *cpufreq_pdev;
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)
50 static int qcom_cpufreq_kryo_name_version(struct device *cpu_dev,
51 struct nvmem_cell *speedbin_nvmem,
53 + struct qcom_cpufreq_drv *drv)
57 @@ -94,10 +108,10 @@ static int qcom_cpufreq_kryo_name_version(struct device *cpu_dev,
59 switch (msm8996_version) {
61 - *versions = 1 << (unsigned int)(*speedbin);
62 + drv->versions = 1 << (unsigned int)(*speedbin);
65 - *versions = 1 << ((unsigned int)(*speedbin) + 4);
66 + drv->versions = 1 << ((unsigned int)(*speedbin) + 4);
70 @@ -108,17 +122,17 @@ static int qcom_cpufreq_kryo_name_version(struct device *cpu_dev,
74 +static const struct qcom_cpufreq_match_data match_data_kryo = {
75 + .get_version = qcom_cpufreq_kryo_name_version,
78 static int qcom_cpufreq_probe(struct platform_device *pdev)
80 - struct opp_table **opp_tables;
81 - int (*get_version)(struct device *cpu_dev,
82 - struct nvmem_cell *speedbin_nvmem,
84 + struct qcom_cpufreq_drv *drv;
85 struct nvmem_cell *speedbin_nvmem;
86 struct device_node *np;
87 struct device *cpu_dev;
90 const struct of_device_id *match;
93 @@ -126,11 +140,6 @@ static int qcom_cpufreq_probe(struct platform_device *pdev)
97 - match = pdev->dev.platform_data;
98 - get_version = match->data;
102 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
105 @@ -141,23 +150,43 @@ static int qcom_cpufreq_probe(struct platform_device *pdev)
109 - speedbin_nvmem = of_nvmem_cell_get(np, NULL);
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);
120 + match = pdev->dev.platform_data;
121 + drv->data = match->data;
127 - ret = get_version(cpu_dev, speedbin_nvmem, &versions);
128 - nvmem_cell_put(speedbin_nvmem);
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)
136 + "Could not get nvmem cell: %ld\n",
137 + PTR_ERR(speedbin_nvmem));
138 + ret = PTR_ERR(speedbin_nvmem);
142 - opp_tables = kcalloc(num_possible_cpus(), sizeof(*opp_tables), GFP_KERNEL);
145 + ret = drv->data->get_version(cpu_dev, speedbin_nvmem, drv);
147 + nvmem_cell_put(speedbin_nvmem);
150 + nvmem_cell_put(speedbin_nvmem);
154 + drv->opp_tables = kcalloc(num_possible_cpus(), sizeof(*drv->opp_tables),
156 + if (!drv->opp_tables) {
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)
167 - opp_tables[cpu] = dev_pm_opp_set_supported_hw(cpu_dev,
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");
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]);
180 + "Failed to set supported hardware\n");
186 cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
188 if (!IS_ERR(cpufreq_dt_pdev)) {
189 - platform_set_drvdata(pdev, opp_tables);
190 + platform_set_drvdata(pdev, drv);
194 @@ -187,26 +220,30 @@ static int qcom_cpufreq_probe(struct platform_device *pdev)
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]))
201 - dev_pm_opp_put_supported_hw(opp_tables[cpu]);
202 + dev_pm_opp_put_supported_hw(drv->opp_tables[cpu]);
205 + kfree(drv->opp_tables);
212 static int qcom_cpufreq_remove(struct platform_device *pdev)
214 - struct opp_table **opp_tables = platform_get_drvdata(pdev);
215 + struct qcom_cpufreq_drv *drv = platform_get_drvdata(pdev);
218 platform_device_unregister(cpufreq_dt_pdev);
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]);
226 + kfree(drv->opp_tables);
231 @@ -220,10 +257,8 @@ static struct platform_driver qcom_cpufreq_driver = {
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 },