1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
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.
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>
25 #include <linux/of_device.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_domain.h>
28 #include <linux/pm_opp.h>
29 #include <linux/slab.h>
30 #include <linux/soc/qcom/smem.h>
32 #define MSM_ID_SMEM 137
41 enum _msm8996_version {
44 NUM_OF_MSM8996_VERSIONS,
47 struct qcom_cpufreq_drv;
49 struct qcom_cpufreq_match_data {
50 int (*get_version)(struct device *cpu_dev,
51 struct nvmem_cell *speedbin_nvmem,
53 struct qcom_cpufreq_drv *drv);
54 const char **genpd_names;
57 struct qcom_cpufreq_drv {
58 struct opp_table **names_opp_tables;
59 struct opp_table **hw_opp_tables;
60 struct opp_table **genpd_opp_tables;
62 const struct qcom_cpufreq_match_data *data;
65 static struct platform_device *cpufreq_dt_pdev, *cpufreq_pdev;
67 static void get_krait_bin_format_a(struct device *cpu_dev,
68 int *speed, int *pvs, int *pvs_ver,
69 struct nvmem_cell *pvs_nvmem, u8 *buf)
73 pte_efuse = *((u32 *)buf);
75 *speed = pte_efuse & 0xf;
77 *speed = (pte_efuse >> 4) & 0xf;
81 dev_warn(cpu_dev, "Speed bin: Defaulting to %d\n", *speed);
83 dev_dbg(cpu_dev, "Speed bin: %d\n", *speed);
86 *pvs = (pte_efuse >> 10) & 0x7;
88 *pvs = (pte_efuse >> 13) & 0x7;
92 dev_warn(cpu_dev, "PVS bin: Defaulting to %d\n", *pvs);
94 dev_dbg(cpu_dev, "PVS bin: %d\n", *pvs);
98 static void get_krait_bin_format_b(struct device *cpu_dev,
99 int *speed, int *pvs, int *pvs_ver,
100 struct nvmem_cell *pvs_nvmem, u8 *buf)
102 u32 pte_efuse, redundant_sel;
104 pte_efuse = *((u32 *)buf);
105 redundant_sel = (pte_efuse >> 24) & 0x7;
107 *pvs_ver = (pte_efuse >> 4) & 0x3;
109 switch (redundant_sel) {
111 *pvs = ((pte_efuse >> 28) & 0x8) | ((pte_efuse >> 6) & 0x7);
112 *speed = (pte_efuse >> 27) & 0xf;
115 *pvs = (pte_efuse >> 27) & 0xf;
116 *speed = pte_efuse & 0x7;
119 /* 4 bits of PVS are in efuse register bits 31, 8-6. */
120 *pvs = ((pte_efuse >> 28) & 0x8) | ((pte_efuse >> 6) & 0x7);
121 *speed = pte_efuse & 0x7;
124 /* Check SPEED_BIN_BLOW_STATUS */
125 if (pte_efuse & BIT(3)) {
126 dev_dbg(cpu_dev, "Speed bin: %d\n", *speed);
128 dev_warn(cpu_dev, "Speed bin not set. Defaulting to 0!\n");
132 /* Check PVS_BLOW_STATUS */
133 pte_efuse = *(((u32 *)buf) + 1);
134 pte_efuse &= BIT(21);
136 dev_dbg(cpu_dev, "PVS bin: %d\n", *pvs);
138 dev_warn(cpu_dev, "PVS bin not set. Defaulting to 0!\n");
142 dev_dbg(cpu_dev, "PVS version: %d\n", *pvs_ver);
145 static enum _msm8996_version qcom_cpufreq_get_msm_id(void)
149 enum _msm8996_version version;
151 msm_id = qcom_smem_get(QCOM_SMEM_HOST_ANY, MSM_ID_SMEM, &len);
153 return NUM_OF_MSM8996_VERSIONS;
155 /* The first 4 bytes are format, next to them is the actual msm-id */
158 switch ((enum _msm_id)*msm_id) {
161 version = MSM8996_V3;
165 version = MSM8996_SG;
168 version = NUM_OF_MSM8996_VERSIONS;
174 static int qcom_cpufreq_kryo_name_version(struct device *cpu_dev,
175 struct nvmem_cell *speedbin_nvmem,
177 struct qcom_cpufreq_drv *drv)
181 enum _msm8996_version msm8996_version;
184 msm8996_version = qcom_cpufreq_get_msm_id();
185 if (NUM_OF_MSM8996_VERSIONS == msm8996_version) {
186 dev_err(cpu_dev, "Not Snapdragon 820/821!");
190 speedbin = nvmem_cell_read(speedbin_nvmem, &len);
191 if (IS_ERR(speedbin))
192 return PTR_ERR(speedbin);
194 switch (msm8996_version) {
196 drv->versions = 1 << (unsigned int)(*speedbin);
199 drv->versions = 1 << ((unsigned int)(*speedbin) + 4);
210 static int qcom_cpufreq_krait_name_version(struct device *cpu_dev,
211 struct nvmem_cell *speedbin_nvmem,
213 struct qcom_cpufreq_drv *drv)
215 int speed = 0, pvs = 0, pvs_ver = 0;
220 speedbin = nvmem_cell_read(speedbin_nvmem, &len);
222 if (IS_ERR(speedbin))
223 return PTR_ERR(speedbin);
227 get_krait_bin_format_a(cpu_dev, &speed, &pvs, &pvs_ver,
228 speedbin_nvmem, speedbin);
231 get_krait_bin_format_b(cpu_dev, &speed, &pvs, &pvs_ver,
232 speedbin_nvmem, speedbin);
235 dev_err(cpu_dev, "Unable to read nvmem data. Defaulting to 0!\n");
240 snprintf(*pvs_name, sizeof("speedXX-pvsXX-vXX"), "speed%d-pvs%d-v%d",
241 speed, pvs, pvs_ver);
243 drv->versions = (1 << speed);
250 static const struct qcom_cpufreq_match_data match_data_kryo = {
251 .get_version = qcom_cpufreq_kryo_name_version,
254 static const struct qcom_cpufreq_match_data match_data_krait = {
255 .get_version = qcom_cpufreq_krait_name_version,
258 static const char *qcs404_genpd_names[] = { "cpr", NULL };
260 static const struct qcom_cpufreq_match_data match_data_qcs404 = {
261 .genpd_names = qcs404_genpd_names,
264 static int qcom_cpufreq_probe(struct platform_device *pdev)
266 struct qcom_cpufreq_drv *drv;
267 struct nvmem_cell *speedbin_nvmem;
268 struct device_node *np;
269 struct device *cpu_dev;
270 char pvs_name_buffer[] = "speedXX-pvsXX-vXX";
271 char *pvs_name = pvs_name_buffer;
273 const struct of_device_id *match;
276 cpu_dev = get_cpu_device(0);
280 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
284 ret = of_device_is_compatible(np, "operating-points-v2-kryo-cpu");
290 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
294 match = pdev->dev.platform_data;
295 drv->data = match->data;
301 if (drv->data->get_version) {
302 speedbin_nvmem = of_nvmem_cell_get(np, NULL);
303 if (IS_ERR(speedbin_nvmem)) {
304 if (PTR_ERR(speedbin_nvmem) != -EPROBE_DEFER)
306 "Could not get nvmem cell: %ld\n",
307 PTR_ERR(speedbin_nvmem));
308 ret = PTR_ERR(speedbin_nvmem);
312 ret = drv->data->get_version(cpu_dev,
313 speedbin_nvmem, &pvs_name, drv);
315 nvmem_cell_put(speedbin_nvmem);
318 nvmem_cell_put(speedbin_nvmem);
322 drv->names_opp_tables = kcalloc(num_possible_cpus(),
323 sizeof(*drv->names_opp_tables),
325 if (!drv->names_opp_tables) {
329 drv->hw_opp_tables = kcalloc(num_possible_cpus(),
330 sizeof(*drv->hw_opp_tables),
332 if (!drv->hw_opp_tables) {
337 drv->genpd_opp_tables = kcalloc(num_possible_cpus(),
338 sizeof(*drv->genpd_opp_tables),
340 if (!drv->genpd_opp_tables) {
345 for_each_possible_cpu(cpu) {
346 cpu_dev = get_cpu_device(cpu);
347 if (NULL == cpu_dev) {
352 if (drv->data->get_version) {
355 drv->names_opp_tables[cpu] = dev_pm_opp_set_prop_name(
358 if (IS_ERR(drv->names_opp_tables[cpu])) {
359 ret = PTR_ERR(drv->names_opp_tables[cpu]);
360 dev_err(cpu_dev, "Failed to add OPP name %s\n",
366 drv->hw_opp_tables[cpu] = dev_pm_opp_set_supported_hw(
367 cpu_dev, &drv->versions, 1);
368 if (IS_ERR(drv->hw_opp_tables[cpu])) {
369 ret = PTR_ERR(drv->hw_opp_tables[cpu]);
371 "Failed to set supported hardware\n");
376 if (drv->data->genpd_names) {
377 drv->genpd_opp_tables[cpu] =
378 dev_pm_opp_attach_genpd(cpu_dev,
379 drv->data->genpd_names,
381 if (IS_ERR(drv->genpd_opp_tables[cpu])) {
382 ret = PTR_ERR(drv->genpd_opp_tables[cpu]);
383 if (ret != -EPROBE_DEFER)
385 "Could not attach to pm_domain: %d\n",
392 cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
394 if (!IS_ERR(cpufreq_dt_pdev)) {
395 platform_set_drvdata(pdev, drv);
399 ret = PTR_ERR(cpufreq_dt_pdev);
400 dev_err(cpu_dev, "Failed to register platform device\n");
403 for_each_possible_cpu(cpu) {
404 if (IS_ERR_OR_NULL(drv->genpd_opp_tables[cpu]))
406 dev_pm_opp_detach_genpd(drv->genpd_opp_tables[cpu]);
408 kfree(drv->genpd_opp_tables);
410 for_each_possible_cpu(cpu) {
411 if (IS_ERR_OR_NULL(drv->names_opp_tables[cpu]))
413 dev_pm_opp_put_prop_name(drv->names_opp_tables[cpu]);
415 for_each_possible_cpu(cpu) {
416 if (IS_ERR_OR_NULL(drv->hw_opp_tables[cpu]))
418 dev_pm_opp_put_supported_hw(drv->hw_opp_tables[cpu]);
420 kfree(drv->hw_opp_tables);
422 kfree(drv->names_opp_tables);
429 static int qcom_cpufreq_remove(struct platform_device *pdev)
431 struct qcom_cpufreq_drv *drv = platform_get_drvdata(pdev);
434 platform_device_unregister(cpufreq_dt_pdev);
436 for_each_possible_cpu(cpu) {
437 if (drv->names_opp_tables[cpu])
438 dev_pm_opp_put_supported_hw(drv->names_opp_tables[cpu]);
439 if (drv->hw_opp_tables[cpu])
440 dev_pm_opp_put_supported_hw(drv->hw_opp_tables[cpu]);
441 if (drv->genpd_opp_tables[cpu])
442 dev_pm_opp_detach_genpd(drv->genpd_opp_tables[cpu]);
445 kfree(drv->names_opp_tables);
446 kfree(drv->hw_opp_tables);
447 kfree(drv->genpd_opp_tables);
453 static struct platform_driver qcom_cpufreq_driver = {
454 .probe = qcom_cpufreq_probe,
455 .remove = qcom_cpufreq_remove,
457 .name = "qcom-cpufreq-nvmem",
461 static const struct of_device_id qcom_cpufreq_match_list[] __initconst = {
462 { .compatible = "qcom,apq8096", .data = &match_data_kryo },
463 { .compatible = "qcom,msm8996", .data = &match_data_kryo },
464 { .compatible = "qcom,qcs404", .data = &match_data_qcs404 },
465 { .compatible = "qcom,ipq8064", .data = &match_data_krait },
466 { .compatible = "qcom,apq8064", .data = &match_data_krait },
467 { .compatible = "qcom,msm8974", .data = &match_data_krait },
468 { .compatible = "qcom,msm8960", .data = &match_data_krait },
471 MODULE_DEVICE_TABLE(of, qcom_cpufreq_match_list);
474 * Since the driver depends on smem and nvmem drivers, which may
475 * return EPROBE_DEFER, all the real activity is done in the probe,
476 * which may be defered as well. The init here is only registering
477 * the driver and the platform device.
479 static int __init qcom_cpufreq_init(void)
481 struct device_node *np = of_find_node_by_path("/");
482 const struct of_device_id *match;
488 match = of_match_node(qcom_cpufreq_match_list, np);
493 ret = platform_driver_register(&qcom_cpufreq_driver);
494 if (unlikely(ret < 0))
497 cpufreq_pdev = platform_device_register_data(NULL, "qcom-cpufreq-nvmem",
498 -1, match, sizeof(*match));
499 ret = PTR_ERR_OR_ZERO(cpufreq_pdev);
503 platform_driver_unregister(&qcom_cpufreq_driver);
506 module_init(qcom_cpufreq_init);
508 static void __exit qcom_cpufreq_exit(void)
510 platform_device_unregister(cpufreq_pdev);
511 platform_driver_unregister(&qcom_cpufreq_driver);
513 module_exit(qcom_cpufreq_exit);
515 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. CPUfreq driver");
516 MODULE_LICENSE("GPL v2");