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/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>
31 #include <dt-bindings/arm/qcom,ids.h>
33 enum ipq806x_versions {
39 #define IPQ6000_VERSION BIT(2)
41 struct qcom_cpufreq_drv;
43 struct qcom_cpufreq_match_data {
44 int (*get_version)(struct device *cpu_dev,
45 struct nvmem_cell *speedbin_nvmem,
47 struct qcom_cpufreq_drv *drv);
48 const char **genpd_names;
51 struct qcom_cpufreq_drv_cpu {
55 struct qcom_cpufreq_drv {
57 const struct qcom_cpufreq_match_data *data;
58 struct qcom_cpufreq_drv_cpu cpus[];
61 static struct platform_device *cpufreq_dt_pdev, *cpufreq_pdev;
63 static int qcom_cpufreq_simple_get_version(struct device *cpu_dev,
64 struct nvmem_cell *speedbin_nvmem,
66 struct qcom_cpufreq_drv *drv)
71 speedbin = nvmem_cell_read(speedbin_nvmem, NULL);
73 return PTR_ERR(speedbin);
75 dev_dbg(cpu_dev, "speedbin: %d\n", *speedbin);
76 drv->versions = 1 << *speedbin;
81 static void get_krait_bin_format_a(struct device *cpu_dev,
87 pte_efuse = *((u32 *)buf);
89 *speed = pte_efuse & 0xf;
91 *speed = (pte_efuse >> 4) & 0xf;
95 dev_warn(cpu_dev, "Speed bin: Defaulting to %d\n", *speed);
97 dev_dbg(cpu_dev, "Speed bin: %d\n", *speed);
100 *pvs = (pte_efuse >> 10) & 0x7;
102 *pvs = (pte_efuse >> 13) & 0x7;
106 dev_warn(cpu_dev, "PVS bin: Defaulting to %d\n", *pvs);
108 dev_dbg(cpu_dev, "PVS bin: %d\n", *pvs);
112 static void get_krait_bin_format_b(struct device *cpu_dev,
113 int *speed, int *pvs, int *pvs_ver,
116 u32 pte_efuse, redundant_sel;
118 pte_efuse = *((u32 *)buf);
119 redundant_sel = (pte_efuse >> 24) & 0x7;
121 *pvs_ver = (pte_efuse >> 4) & 0x3;
123 switch (redundant_sel) {
125 *pvs = ((pte_efuse >> 28) & 0x8) | ((pte_efuse >> 6) & 0x7);
126 *speed = (pte_efuse >> 27) & 0xf;
129 *pvs = (pte_efuse >> 27) & 0xf;
130 *speed = pte_efuse & 0x7;
133 /* 4 bits of PVS are in efuse register bits 31, 8-6. */
134 *pvs = ((pte_efuse >> 28) & 0x8) | ((pte_efuse >> 6) & 0x7);
135 *speed = pte_efuse & 0x7;
138 /* Check SPEED_BIN_BLOW_STATUS */
139 if (pte_efuse & BIT(3)) {
140 dev_dbg(cpu_dev, "Speed bin: %d\n", *speed);
142 dev_warn(cpu_dev, "Speed bin not set. Defaulting to 0!\n");
146 /* Check PVS_BLOW_STATUS */
147 pte_efuse = *(((u32 *)buf) + 1);
148 pte_efuse &= BIT(21);
150 dev_dbg(cpu_dev, "PVS bin: %d\n", *pvs);
152 dev_warn(cpu_dev, "PVS bin not set. Defaulting to 0!\n");
156 dev_dbg(cpu_dev, "PVS version: %d\n", *pvs_ver);
159 static int qcom_cpufreq_kryo_name_version(struct device *cpu_dev,
160 struct nvmem_cell *speedbin_nvmem,
162 struct qcom_cpufreq_drv *drv)
170 ret = qcom_smem_get_soc_id(&msm_id);
174 speedbin = nvmem_cell_read(speedbin_nvmem, &len);
175 if (IS_ERR(speedbin))
176 return PTR_ERR(speedbin);
179 case QCOM_ID_MSM8996:
180 case QCOM_ID_APQ8096:
181 drv->versions = 1 << (unsigned int)(*speedbin);
183 case QCOM_ID_MSM8996SG:
184 case QCOM_ID_APQ8096SG:
185 drv->versions = 1 << ((unsigned int)(*speedbin) + 4);
196 static int qcom_cpufreq_krait_name_version(struct device *cpu_dev,
197 struct nvmem_cell *speedbin_nvmem,
199 struct qcom_cpufreq_drv *drv)
201 int speed = 0, pvs = 0, pvs_ver = 0;
206 speedbin = nvmem_cell_read(speedbin_nvmem, &len);
208 if (IS_ERR(speedbin))
209 return PTR_ERR(speedbin);
213 get_krait_bin_format_a(cpu_dev, &speed, &pvs, speedbin);
216 get_krait_bin_format_b(cpu_dev, &speed, &pvs, &pvs_ver,
220 dev_err(cpu_dev, "Unable to read nvmem data. Defaulting to 0!\n");
225 snprintf(*pvs_name, sizeof("speedXX-pvsXX-vXX"), "speed%d-pvs%d-v%d",
226 speed, pvs, pvs_ver);
228 drv->versions = (1 << speed);
235 static int qcom_cpufreq_ipq8064_name_version(struct device *cpu_dev,
236 struct nvmem_cell *speedbin_nvmem,
238 struct qcom_cpufreq_drv *drv)
240 int speed = 0, pvs = 0;
245 speedbin = nvmem_cell_read(speedbin_nvmem, &len);
246 if (IS_ERR(speedbin))
247 return PTR_ERR(speedbin);
250 dev_err(cpu_dev, "Unable to read nvmem data. Defaulting to 0!\n");
255 get_krait_bin_format_a(cpu_dev, &speed, &pvs, speedbin);
257 ret = qcom_smem_get_soc_id(&msm_id);
262 case QCOM_ID_IPQ8062:
263 drv->versions = BIT(IPQ8062_VERSION);
265 case QCOM_ID_IPQ8064:
266 case QCOM_ID_IPQ8066:
267 case QCOM_ID_IPQ8068:
268 drv->versions = BIT(IPQ8064_VERSION);
270 case QCOM_ID_IPQ8065:
271 case QCOM_ID_IPQ8069:
272 drv->versions = BIT(IPQ8065_VERSION);
276 "SoC ID %u is not part of IPQ8064 family, limiting to 1.0GHz!\n",
278 drv->versions = BIT(IPQ8062_VERSION);
282 /* IPQ8064 speed is never fused. Only pvs values are fused. */
283 snprintf(*pvs_name, sizeof("speed0-pvsXX"), "speed0-pvs%d", pvs);
290 static int qcom_cpufreq_ipq6018_name_version(struct device *cpu_dev,
291 struct nvmem_cell *speedbin_nvmem,
293 struct qcom_cpufreq_drv *drv)
300 ret = qcom_smem_get_soc_id(&msm_id);
304 speedbin = nvmem_cell_read(speedbin_nvmem, NULL);
305 if (IS_ERR(speedbin))
306 return PTR_ERR(speedbin);
309 case QCOM_ID_IPQ6005:
310 case QCOM_ID_IPQ6010:
311 case QCOM_ID_IPQ6018:
312 case QCOM_ID_IPQ6028:
313 /* Fuse Value Freq BIT to set
314 * ---------------------------------
315 * 2’b0 No Limit BIT(0)
316 * 2’b1 1.5 GHz BIT(1)
318 drv->versions = 1 << (unsigned int)(*speedbin);
320 case QCOM_ID_IPQ6000:
322 * IPQ6018 family only has one bit to advertise the CPU
323 * speed-bin, but that is not enough for IPQ6000 which
324 * is only rated up to 1.2GHz.
325 * So for IPQ6000 manually set BIT(2) based on SMEM ID.
327 drv->versions = IPQ6000_VERSION;
331 "SoC ID %u is not part of IPQ6018 family, limiting to 1.2GHz!\n",
333 drv->versions = IPQ6000_VERSION;
341 static const char *generic_genpd_names[] = { "perf", NULL };
343 static const struct qcom_cpufreq_match_data match_data_kryo = {
344 .get_version = qcom_cpufreq_kryo_name_version,
347 static const struct qcom_cpufreq_match_data match_data_krait = {
348 .get_version = qcom_cpufreq_krait_name_version,
351 static const struct qcom_cpufreq_match_data match_data_msm8909 = {
352 .get_version = qcom_cpufreq_simple_get_version,
353 .genpd_names = generic_genpd_names,
356 static const char *qcs404_genpd_names[] = { "cpr", NULL };
358 static const struct qcom_cpufreq_match_data match_data_qcs404 = {
359 .genpd_names = qcs404_genpd_names,
362 static const struct qcom_cpufreq_match_data match_data_ipq6018 = {
363 .get_version = qcom_cpufreq_ipq6018_name_version,
366 static const struct qcom_cpufreq_match_data match_data_ipq8064 = {
367 .get_version = qcom_cpufreq_ipq8064_name_version,
370 static int qcom_cpufreq_probe(struct platform_device *pdev)
372 struct qcom_cpufreq_drv *drv;
373 struct nvmem_cell *speedbin_nvmem;
374 struct device_node *np;
375 struct device *cpu_dev;
376 char pvs_name_buffer[] = "speedXX-pvsXX-vXX";
377 char *pvs_name = pvs_name_buffer;
379 const struct of_device_id *match;
382 cpu_dev = get_cpu_device(0);
386 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
390 ret = of_device_is_compatible(np, "operating-points-v2-kryo-cpu") ||
391 of_device_is_compatible(np, "operating-points-v2-krait-cpu");
397 drv = devm_kzalloc(&pdev->dev, struct_size(drv, cpus, num_possible_cpus()),
402 match = pdev->dev.platform_data;
403 drv->data = match->data;
407 if (drv->data->get_version) {
408 speedbin_nvmem = of_nvmem_cell_get(np, NULL);
409 if (IS_ERR(speedbin_nvmem))
410 return dev_err_probe(cpu_dev, PTR_ERR(speedbin_nvmem),
411 "Could not get nvmem cell\n");
413 ret = drv->data->get_version(cpu_dev,
414 speedbin_nvmem, &pvs_name, drv);
416 nvmem_cell_put(speedbin_nvmem);
419 nvmem_cell_put(speedbin_nvmem);
423 for_each_possible_cpu(cpu) {
424 struct dev_pm_opp_config config = {
425 .supported_hw = NULL,
428 cpu_dev = get_cpu_device(cpu);
429 if (NULL == cpu_dev) {
434 if (drv->data->get_version) {
435 config.supported_hw = &drv->versions;
436 config.supported_hw_count = 1;
439 config.prop_name = pvs_name;
442 if (drv->data->genpd_names) {
443 config.genpd_names = drv->data->genpd_names;
444 config.virt_devs = NULL;
447 if (config.supported_hw || config.genpd_names) {
448 drv->cpus[cpu].opp_token = dev_pm_opp_set_config(cpu_dev, &config);
449 if (drv->cpus[cpu].opp_token < 0) {
450 ret = drv->cpus[cpu].opp_token;
451 dev_err(cpu_dev, "Failed to set OPP config\n");
457 cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
459 if (!IS_ERR(cpufreq_dt_pdev)) {
460 platform_set_drvdata(pdev, drv);
464 ret = PTR_ERR(cpufreq_dt_pdev);
465 dev_err(cpu_dev, "Failed to register platform device\n");
468 for_each_possible_cpu(cpu)
469 dev_pm_opp_clear_config(drv->cpus[cpu].opp_token);
473 static void qcom_cpufreq_remove(struct platform_device *pdev)
475 struct qcom_cpufreq_drv *drv = platform_get_drvdata(pdev);
478 platform_device_unregister(cpufreq_dt_pdev);
480 for_each_possible_cpu(cpu)
481 dev_pm_opp_clear_config(drv->cpus[cpu].opp_token);
484 static struct platform_driver qcom_cpufreq_driver = {
485 .probe = qcom_cpufreq_probe,
486 .remove_new = qcom_cpufreq_remove,
488 .name = "qcom-cpufreq-nvmem",
492 static const struct of_device_id qcom_cpufreq_match_list[] __initconst = {
493 { .compatible = "qcom,apq8096", .data = &match_data_kryo },
494 { .compatible = "qcom,msm8909", .data = &match_data_msm8909 },
495 { .compatible = "qcom,msm8996", .data = &match_data_kryo },
496 { .compatible = "qcom,qcs404", .data = &match_data_qcs404 },
497 { .compatible = "qcom,ipq6018", .data = &match_data_ipq6018 },
498 { .compatible = "qcom,ipq8064", .data = &match_data_ipq8064 },
499 { .compatible = "qcom,apq8064", .data = &match_data_krait },
500 { .compatible = "qcom,msm8974", .data = &match_data_krait },
501 { .compatible = "qcom,msm8960", .data = &match_data_krait },
504 MODULE_DEVICE_TABLE(of, qcom_cpufreq_match_list);
507 * Since the driver depends on smem and nvmem drivers, which may
508 * return EPROBE_DEFER, all the real activity is done in the probe,
509 * which may be defered as well. The init here is only registering
510 * the driver and the platform device.
512 static int __init qcom_cpufreq_init(void)
514 struct device_node *np = of_find_node_by_path("/");
515 const struct of_device_id *match;
521 match = of_match_node(qcom_cpufreq_match_list, np);
526 ret = platform_driver_register(&qcom_cpufreq_driver);
527 if (unlikely(ret < 0))
530 cpufreq_pdev = platform_device_register_data(NULL, "qcom-cpufreq-nvmem",
531 -1, match, sizeof(*match));
532 ret = PTR_ERR_OR_ZERO(cpufreq_pdev);
536 platform_driver_unregister(&qcom_cpufreq_driver);
539 module_init(qcom_cpufreq_init);
541 static void __exit qcom_cpufreq_exit(void)
543 platform_device_unregister(cpufreq_pdev);
544 platform_driver_unregister(&qcom_cpufreq_driver);
546 module_exit(qcom_cpufreq_exit);
548 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. CPUfreq driver");
549 MODULE_LICENSE("GPL v2");