GNU Linux-libre 6.1.86-gnu
[releases.git] / drivers / cpufreq / qcom-cpufreq-nvmem.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2018, The Linux Foundation. All rights reserved.
4  */
5
6 /*
7  * In Certain QCOM SoCs like apq8096 and msm8996 that have KRYO processors,
8  * the CPU frequency subset and voltage value of each OPP varies
9  * based on the silicon variant in use. Qualcomm Process Voltage Scaling Tables
10  * defines the voltage and frequency value based on the msm-id in SMEM
11  * and speedbin blown in the efuse combination.
12  * The qcom-cpufreq-nvmem driver reads the msm-id and efuse value from the SoC
13  * to provide the OPP framework with required information.
14  * This is used to determine the voltage and frequency value for each OPP of
15  * operating-points-v2 table when it is parsed by the OPP framework.
16  */
17
18 #include <linux/cpu.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/nvmem-consumer.h>
24 #include <linux/of.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_domain.h>
27 #include <linux/pm_opp.h>
28 #include <linux/slab.h>
29 #include <linux/soc/qcom/smem.h>
30
31 #define MSM_ID_SMEM     137
32
33 enum _msm_id {
34         MSM8996V3 = 0xF6ul,
35         APQ8096V3 = 0x123ul,
36         MSM8996SG = 0x131ul,
37         APQ8096SG = 0x138ul,
38 };
39
40 enum _msm8996_version {
41         MSM8996_V3,
42         MSM8996_SG,
43         NUM_OF_MSM8996_VERSIONS,
44 };
45
46 struct qcom_cpufreq_drv;
47
48 struct qcom_cpufreq_match_data {
49         int (*get_version)(struct device *cpu_dev,
50                            struct nvmem_cell *speedbin_nvmem,
51                            char **pvs_name,
52                            struct qcom_cpufreq_drv *drv);
53         const char **genpd_names;
54 };
55
56 struct qcom_cpufreq_drv {
57         int *opp_tokens;
58         u32 versions;
59         const struct qcom_cpufreq_match_data *data;
60 };
61
62 static struct platform_device *cpufreq_dt_pdev, *cpufreq_pdev;
63
64 static void get_krait_bin_format_a(struct device *cpu_dev,
65                                           int *speed, int *pvs, int *pvs_ver,
66                                           u8 *buf)
67 {
68         u32 pte_efuse;
69
70         pte_efuse = *((u32 *)buf);
71
72         *speed = pte_efuse & 0xf;
73         if (*speed == 0xf)
74                 *speed = (pte_efuse >> 4) & 0xf;
75
76         if (*speed == 0xf) {
77                 *speed = 0;
78                 dev_warn(cpu_dev, "Speed bin: Defaulting to %d\n", *speed);
79         } else {
80                 dev_dbg(cpu_dev, "Speed bin: %d\n", *speed);
81         }
82
83         *pvs = (pte_efuse >> 10) & 0x7;
84         if (*pvs == 0x7)
85                 *pvs = (pte_efuse >> 13) & 0x7;
86
87         if (*pvs == 0x7) {
88                 *pvs = 0;
89                 dev_warn(cpu_dev, "PVS bin: Defaulting to %d\n", *pvs);
90         } else {
91                 dev_dbg(cpu_dev, "PVS bin: %d\n", *pvs);
92         }
93 }
94
95 static void get_krait_bin_format_b(struct device *cpu_dev,
96                                           int *speed, int *pvs, int *pvs_ver,
97                                           u8 *buf)
98 {
99         u32 pte_efuse, redundant_sel;
100
101         pte_efuse = *((u32 *)buf);
102         redundant_sel = (pte_efuse >> 24) & 0x7;
103
104         *pvs_ver = (pte_efuse >> 4) & 0x3;
105
106         switch (redundant_sel) {
107         case 1:
108                 *pvs = ((pte_efuse >> 28) & 0x8) | ((pte_efuse >> 6) & 0x7);
109                 *speed = (pte_efuse >> 27) & 0xf;
110                 break;
111         case 2:
112                 *pvs = (pte_efuse >> 27) & 0xf;
113                 *speed = pte_efuse & 0x7;
114                 break;
115         default:
116                 /* 4 bits of PVS are in efuse register bits 31, 8-6. */
117                 *pvs = ((pte_efuse >> 28) & 0x8) | ((pte_efuse >> 6) & 0x7);
118                 *speed = pte_efuse & 0x7;
119         }
120
121         /* Check SPEED_BIN_BLOW_STATUS */
122         if (pte_efuse & BIT(3)) {
123                 dev_dbg(cpu_dev, "Speed bin: %d\n", *speed);
124         } else {
125                 dev_warn(cpu_dev, "Speed bin not set. Defaulting to 0!\n");
126                 *speed = 0;
127         }
128
129         /* Check PVS_BLOW_STATUS */
130         pte_efuse = *(((u32 *)buf) + 1);
131         pte_efuse &= BIT(21);
132         if (pte_efuse) {
133                 dev_dbg(cpu_dev, "PVS bin: %d\n", *pvs);
134         } else {
135                 dev_warn(cpu_dev, "PVS bin not set. Defaulting to 0!\n");
136                 *pvs = 0;
137         }
138
139         dev_dbg(cpu_dev, "PVS version: %d\n", *pvs_ver);
140 }
141
142 static enum _msm8996_version qcom_cpufreq_get_msm_id(void)
143 {
144         size_t len;
145         u32 *msm_id;
146         enum _msm8996_version version;
147
148         msm_id = qcom_smem_get(QCOM_SMEM_HOST_ANY, MSM_ID_SMEM, &len);
149         if (IS_ERR(msm_id))
150                 return NUM_OF_MSM8996_VERSIONS;
151
152         /* The first 4 bytes are format, next to them is the actual msm-id */
153         msm_id++;
154
155         switch ((enum _msm_id)*msm_id) {
156         case MSM8996V3:
157         case APQ8096V3:
158                 version = MSM8996_V3;
159                 break;
160         case MSM8996SG:
161         case APQ8096SG:
162                 version = MSM8996_SG;
163                 break;
164         default:
165                 version = NUM_OF_MSM8996_VERSIONS;
166         }
167
168         return version;
169 }
170
171 static int qcom_cpufreq_kryo_name_version(struct device *cpu_dev,
172                                           struct nvmem_cell *speedbin_nvmem,
173                                           char **pvs_name,
174                                           struct qcom_cpufreq_drv *drv)
175 {
176         size_t len;
177         u8 *speedbin;
178         enum _msm8996_version msm8996_version;
179         *pvs_name = NULL;
180
181         msm8996_version = qcom_cpufreq_get_msm_id();
182         if (NUM_OF_MSM8996_VERSIONS == msm8996_version) {
183                 dev_err(cpu_dev, "Not Snapdragon 820/821!");
184                 return -ENODEV;
185         }
186
187         speedbin = nvmem_cell_read(speedbin_nvmem, &len);
188         if (IS_ERR(speedbin))
189                 return PTR_ERR(speedbin);
190
191         switch (msm8996_version) {
192         case MSM8996_V3:
193                 drv->versions = 1 << (unsigned int)(*speedbin);
194                 break;
195         case MSM8996_SG:
196                 drv->versions = 1 << ((unsigned int)(*speedbin) + 4);
197                 break;
198         default:
199                 BUG();
200                 break;
201         }
202
203         kfree(speedbin);
204         return 0;
205 }
206
207 static int qcom_cpufreq_krait_name_version(struct device *cpu_dev,
208                                            struct nvmem_cell *speedbin_nvmem,
209                                            char **pvs_name,
210                                            struct qcom_cpufreq_drv *drv)
211 {
212         int speed = 0, pvs = 0, pvs_ver = 0;
213         u8 *speedbin;
214         size_t len;
215         int ret = 0;
216
217         speedbin = nvmem_cell_read(speedbin_nvmem, &len);
218
219         if (IS_ERR(speedbin))
220                 return PTR_ERR(speedbin);
221
222         switch (len) {
223         case 4:
224                 get_krait_bin_format_a(cpu_dev, &speed, &pvs, &pvs_ver,
225                                        speedbin);
226                 break;
227         case 8:
228                 get_krait_bin_format_b(cpu_dev, &speed, &pvs, &pvs_ver,
229                                        speedbin);
230                 break;
231         default:
232                 dev_err(cpu_dev, "Unable to read nvmem data. Defaulting to 0!\n");
233                 ret = -ENODEV;
234                 goto len_error;
235         }
236
237         snprintf(*pvs_name, sizeof("speedXX-pvsXX-vXX"), "speed%d-pvs%d-v%d",
238                  speed, pvs, pvs_ver);
239
240         drv->versions = (1 << speed);
241
242 len_error:
243         kfree(speedbin);
244         return ret;
245 }
246
247 static const struct qcom_cpufreq_match_data match_data_kryo = {
248         .get_version = qcom_cpufreq_kryo_name_version,
249 };
250
251 static const struct qcom_cpufreq_match_data match_data_krait = {
252         .get_version = qcom_cpufreq_krait_name_version,
253 };
254
255 static const char *qcs404_genpd_names[] = { "cpr", NULL };
256
257 static const struct qcom_cpufreq_match_data match_data_qcs404 = {
258         .genpd_names = qcs404_genpd_names,
259 };
260
261 static int qcom_cpufreq_probe(struct platform_device *pdev)
262 {
263         struct qcom_cpufreq_drv *drv;
264         struct nvmem_cell *speedbin_nvmem;
265         struct device_node *np;
266         struct device *cpu_dev;
267         char pvs_name_buffer[] = "speedXX-pvsXX-vXX";
268         char *pvs_name = pvs_name_buffer;
269         unsigned cpu;
270         const struct of_device_id *match;
271         int ret;
272
273         cpu_dev = get_cpu_device(0);
274         if (!cpu_dev)
275                 return -ENODEV;
276
277         np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
278         if (!np)
279                 return -ENOENT;
280
281         ret = of_device_is_compatible(np, "operating-points-v2-kryo-cpu");
282         if (!ret) {
283                 of_node_put(np);
284                 return -ENOENT;
285         }
286
287         drv = kzalloc(sizeof(*drv), GFP_KERNEL);
288         if (!drv)
289                 return -ENOMEM;
290
291         match = pdev->dev.platform_data;
292         drv->data = match->data;
293         if (!drv->data) {
294                 ret = -ENODEV;
295                 goto free_drv;
296         }
297
298         if (drv->data->get_version) {
299                 speedbin_nvmem = of_nvmem_cell_get(np, NULL);
300                 if (IS_ERR(speedbin_nvmem)) {
301                         ret = dev_err_probe(cpu_dev, PTR_ERR(speedbin_nvmem),
302                                             "Could not get nvmem cell\n");
303                         goto free_drv;
304                 }
305
306                 ret = drv->data->get_version(cpu_dev,
307                                                         speedbin_nvmem, &pvs_name, drv);
308                 if (ret) {
309                         nvmem_cell_put(speedbin_nvmem);
310                         goto free_drv;
311                 }
312                 nvmem_cell_put(speedbin_nvmem);
313         }
314         of_node_put(np);
315
316         drv->opp_tokens = kcalloc(num_possible_cpus(), sizeof(*drv->opp_tokens),
317                                   GFP_KERNEL);
318         if (!drv->opp_tokens) {
319                 ret = -ENOMEM;
320                 goto free_drv;
321         }
322
323         for_each_possible_cpu(cpu) {
324                 struct dev_pm_opp_config config = {
325                         .supported_hw = NULL,
326                 };
327
328                 cpu_dev = get_cpu_device(cpu);
329                 if (NULL == cpu_dev) {
330                         ret = -ENODEV;
331                         goto free_opp;
332                 }
333
334                 if (drv->data->get_version) {
335                         config.supported_hw = &drv->versions;
336                         config.supported_hw_count = 1;
337
338                         if (pvs_name)
339                                 config.prop_name = pvs_name;
340                 }
341
342                 if (drv->data->genpd_names) {
343                         config.genpd_names = drv->data->genpd_names;
344                         config.virt_devs = NULL;
345                 }
346
347                 if (config.supported_hw || config.genpd_names) {
348                         drv->opp_tokens[cpu] = dev_pm_opp_set_config(cpu_dev, &config);
349                         if (drv->opp_tokens[cpu] < 0) {
350                                 ret = drv->opp_tokens[cpu];
351                                 dev_err(cpu_dev, "Failed to set OPP config\n");
352                                 goto free_opp;
353                         }
354                 }
355         }
356
357         cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
358                                                           NULL, 0);
359         if (!IS_ERR(cpufreq_dt_pdev)) {
360                 platform_set_drvdata(pdev, drv);
361                 return 0;
362         }
363
364         ret = PTR_ERR(cpufreq_dt_pdev);
365         dev_err(cpu_dev, "Failed to register platform device\n");
366
367 free_opp:
368         for_each_possible_cpu(cpu)
369                 dev_pm_opp_clear_config(drv->opp_tokens[cpu]);
370         kfree(drv->opp_tokens);
371 free_drv:
372         kfree(drv);
373
374         return ret;
375 }
376
377 static int qcom_cpufreq_remove(struct platform_device *pdev)
378 {
379         struct qcom_cpufreq_drv *drv = platform_get_drvdata(pdev);
380         unsigned int cpu;
381
382         platform_device_unregister(cpufreq_dt_pdev);
383
384         for_each_possible_cpu(cpu)
385                 dev_pm_opp_clear_config(drv->opp_tokens[cpu]);
386
387         kfree(drv->opp_tokens);
388         kfree(drv);
389
390         return 0;
391 }
392
393 static struct platform_driver qcom_cpufreq_driver = {
394         .probe = qcom_cpufreq_probe,
395         .remove = qcom_cpufreq_remove,
396         .driver = {
397                 .name = "qcom-cpufreq-nvmem",
398         },
399 };
400
401 static const struct of_device_id qcom_cpufreq_match_list[] __initconst = {
402         { .compatible = "qcom,apq8096", .data = &match_data_kryo },
403         { .compatible = "qcom,msm8996", .data = &match_data_kryo },
404         { .compatible = "qcom,qcs404", .data = &match_data_qcs404 },
405         { .compatible = "qcom,ipq8064", .data = &match_data_krait },
406         { .compatible = "qcom,apq8064", .data = &match_data_krait },
407         { .compatible = "qcom,msm8974", .data = &match_data_krait },
408         { .compatible = "qcom,msm8960", .data = &match_data_krait },
409         {},
410 };
411 MODULE_DEVICE_TABLE(of, qcom_cpufreq_match_list);
412
413 /*
414  * Since the driver depends on smem and nvmem drivers, which may
415  * return EPROBE_DEFER, all the real activity is done in the probe,
416  * which may be defered as well. The init here is only registering
417  * the driver and the platform device.
418  */
419 static int __init qcom_cpufreq_init(void)
420 {
421         struct device_node *np = of_find_node_by_path("/");
422         const struct of_device_id *match;
423         int ret;
424
425         if (!np)
426                 return -ENODEV;
427
428         match = of_match_node(qcom_cpufreq_match_list, np);
429         of_node_put(np);
430         if (!match)
431                 return -ENODEV;
432
433         ret = platform_driver_register(&qcom_cpufreq_driver);
434         if (unlikely(ret < 0))
435                 return ret;
436
437         cpufreq_pdev = platform_device_register_data(NULL, "qcom-cpufreq-nvmem",
438                                                      -1, match, sizeof(*match));
439         ret = PTR_ERR_OR_ZERO(cpufreq_pdev);
440         if (0 == ret)
441                 return 0;
442
443         platform_driver_unregister(&qcom_cpufreq_driver);
444         return ret;
445 }
446 module_init(qcom_cpufreq_init);
447
448 static void __exit qcom_cpufreq_exit(void)
449 {
450         platform_device_unregister(cpufreq_pdev);
451         platform_driver_unregister(&qcom_cpufreq_driver);
452 }
453 module_exit(qcom_cpufreq_exit);
454
455 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. CPUfreq driver");
456 MODULE_LICENSE("GPL v2");