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 enum ipq8074_versions {
42 IPQ8074_HAWKEYE_VERSION = 0,
43 IPQ8074_ACORN_VERSION,
46 struct qcom_cpufreq_drv;
48 struct qcom_cpufreq_match_data {
49 int (*get_version)(struct device *cpu_dev,
50 struct nvmem_cell *speedbin_nvmem,
52 struct qcom_cpufreq_drv *drv);
53 const char **genpd_names;
56 struct qcom_cpufreq_drv_cpu {
60 struct qcom_cpufreq_drv {
62 const struct qcom_cpufreq_match_data *data;
63 struct qcom_cpufreq_drv_cpu cpus[];
66 static struct platform_device *cpufreq_dt_pdev, *cpufreq_pdev;
68 static int qcom_cpufreq_simple_get_version(struct device *cpu_dev,
69 struct nvmem_cell *speedbin_nvmem,
71 struct qcom_cpufreq_drv *drv)
76 speedbin = nvmem_cell_read(speedbin_nvmem, NULL);
78 return PTR_ERR(speedbin);
80 dev_dbg(cpu_dev, "speedbin: %d\n", *speedbin);
81 drv->versions = 1 << *speedbin;
86 static void get_krait_bin_format_a(struct device *cpu_dev,
92 pte_efuse = *((u32 *)buf);
94 *speed = pte_efuse & 0xf;
96 *speed = (pte_efuse >> 4) & 0xf;
100 dev_warn(cpu_dev, "Speed bin: Defaulting to %d\n", *speed);
102 dev_dbg(cpu_dev, "Speed bin: %d\n", *speed);
105 *pvs = (pte_efuse >> 10) & 0x7;
107 *pvs = (pte_efuse >> 13) & 0x7;
111 dev_warn(cpu_dev, "PVS bin: Defaulting to %d\n", *pvs);
113 dev_dbg(cpu_dev, "PVS bin: %d\n", *pvs);
117 static void get_krait_bin_format_b(struct device *cpu_dev,
118 int *speed, int *pvs, int *pvs_ver,
121 u32 pte_efuse, redundant_sel;
123 pte_efuse = *((u32 *)buf);
124 redundant_sel = (pte_efuse >> 24) & 0x7;
126 *pvs_ver = (pte_efuse >> 4) & 0x3;
128 switch (redundant_sel) {
130 *pvs = ((pte_efuse >> 28) & 0x8) | ((pte_efuse >> 6) & 0x7);
131 *speed = (pte_efuse >> 27) & 0xf;
134 *pvs = (pte_efuse >> 27) & 0xf;
135 *speed = pte_efuse & 0x7;
138 /* 4 bits of PVS are in efuse register bits 31, 8-6. */
139 *pvs = ((pte_efuse >> 28) & 0x8) | ((pte_efuse >> 6) & 0x7);
140 *speed = pte_efuse & 0x7;
143 /* Check SPEED_BIN_BLOW_STATUS */
144 if (pte_efuse & BIT(3)) {
145 dev_dbg(cpu_dev, "Speed bin: %d\n", *speed);
147 dev_warn(cpu_dev, "Speed bin not set. Defaulting to 0!\n");
151 /* Check PVS_BLOW_STATUS */
152 pte_efuse = *(((u32 *)buf) + 1);
153 pte_efuse &= BIT(21);
155 dev_dbg(cpu_dev, "PVS bin: %d\n", *pvs);
157 dev_warn(cpu_dev, "PVS bin not set. Defaulting to 0!\n");
161 dev_dbg(cpu_dev, "PVS version: %d\n", *pvs_ver);
164 static int qcom_cpufreq_kryo_name_version(struct device *cpu_dev,
165 struct nvmem_cell *speedbin_nvmem,
167 struct qcom_cpufreq_drv *drv)
175 ret = qcom_smem_get_soc_id(&msm_id);
179 speedbin = nvmem_cell_read(speedbin_nvmem, &len);
180 if (IS_ERR(speedbin))
181 return PTR_ERR(speedbin);
184 case QCOM_ID_MSM8996:
185 case QCOM_ID_APQ8096:
186 case QCOM_ID_IPQ5332:
187 case QCOM_ID_IPQ5322:
188 case QCOM_ID_IPQ5312:
189 case QCOM_ID_IPQ5302:
190 case QCOM_ID_IPQ5300:
191 case QCOM_ID_IPQ9514:
192 case QCOM_ID_IPQ9550:
193 case QCOM_ID_IPQ9554:
194 case QCOM_ID_IPQ9570:
195 case QCOM_ID_IPQ9574:
196 drv->versions = 1 << (unsigned int)(*speedbin);
198 case QCOM_ID_MSM8996SG:
199 case QCOM_ID_APQ8096SG:
200 drv->versions = 1 << ((unsigned int)(*speedbin) + 4);
211 static int qcom_cpufreq_krait_name_version(struct device *cpu_dev,
212 struct nvmem_cell *speedbin_nvmem,
214 struct qcom_cpufreq_drv *drv)
216 int speed = 0, pvs = 0, pvs_ver = 0;
221 speedbin = nvmem_cell_read(speedbin_nvmem, &len);
223 if (IS_ERR(speedbin))
224 return PTR_ERR(speedbin);
228 get_krait_bin_format_a(cpu_dev, &speed, &pvs, speedbin);
231 get_krait_bin_format_b(cpu_dev, &speed, &pvs, &pvs_ver,
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 int qcom_cpufreq_ipq8064_name_version(struct device *cpu_dev,
251 struct nvmem_cell *speedbin_nvmem,
253 struct qcom_cpufreq_drv *drv)
255 int speed = 0, pvs = 0;
260 speedbin = nvmem_cell_read(speedbin_nvmem, &len);
261 if (IS_ERR(speedbin))
262 return PTR_ERR(speedbin);
265 dev_err(cpu_dev, "Unable to read nvmem data. Defaulting to 0!\n");
270 get_krait_bin_format_a(cpu_dev, &speed, &pvs, speedbin);
272 ret = qcom_smem_get_soc_id(&msm_id);
277 case QCOM_ID_IPQ8062:
278 drv->versions = BIT(IPQ8062_VERSION);
280 case QCOM_ID_IPQ8064:
281 case QCOM_ID_IPQ8066:
282 case QCOM_ID_IPQ8068:
283 drv->versions = BIT(IPQ8064_VERSION);
285 case QCOM_ID_IPQ8065:
286 case QCOM_ID_IPQ8069:
287 drv->versions = BIT(IPQ8065_VERSION);
291 "SoC ID %u is not part of IPQ8064 family, limiting to 1.0GHz!\n",
293 drv->versions = BIT(IPQ8062_VERSION);
297 /* IPQ8064 speed is never fused. Only pvs values are fused. */
298 snprintf(*pvs_name, sizeof("speed0-pvsXX"), "speed0-pvs%d", pvs);
305 static int qcom_cpufreq_ipq6018_name_version(struct device *cpu_dev,
306 struct nvmem_cell *speedbin_nvmem,
308 struct qcom_cpufreq_drv *drv)
315 ret = qcom_smem_get_soc_id(&msm_id);
319 speedbin = nvmem_cell_read(speedbin_nvmem, NULL);
320 if (IS_ERR(speedbin))
321 return PTR_ERR(speedbin);
324 case QCOM_ID_IPQ6005:
325 case QCOM_ID_IPQ6010:
326 case QCOM_ID_IPQ6018:
327 case QCOM_ID_IPQ6028:
328 /* Fuse Value Freq BIT to set
329 * ---------------------------------
330 * 2’b0 No Limit BIT(0)
331 * 2’b1 1.5 GHz BIT(1)
333 drv->versions = 1 << (unsigned int)(*speedbin);
335 case QCOM_ID_IPQ6000:
337 * IPQ6018 family only has one bit to advertise the CPU
338 * speed-bin, but that is not enough for IPQ6000 which
339 * is only rated up to 1.2GHz.
340 * So for IPQ6000 manually set BIT(2) based on SMEM ID.
342 drv->versions = IPQ6000_VERSION;
346 "SoC ID %u is not part of IPQ6018 family, limiting to 1.2GHz!\n",
348 drv->versions = IPQ6000_VERSION;
356 static int qcom_cpufreq_ipq8074_name_version(struct device *cpu_dev,
357 struct nvmem_cell *speedbin_nvmem,
359 struct qcom_cpufreq_drv *drv)
365 ret = qcom_smem_get_soc_id(&msm_id);
370 case QCOM_ID_IPQ8070A:
371 case QCOM_ID_IPQ8071A:
372 case QCOM_ID_IPQ8172:
373 case QCOM_ID_IPQ8173:
374 case QCOM_ID_IPQ8174:
375 drv->versions = BIT(IPQ8074_ACORN_VERSION);
377 case QCOM_ID_IPQ8072A:
378 case QCOM_ID_IPQ8074A:
379 case QCOM_ID_IPQ8076A:
380 case QCOM_ID_IPQ8078A:
381 drv->versions = BIT(IPQ8074_HAWKEYE_VERSION);
385 "SoC ID %u is not part of IPQ8074 family, limiting to 1.4GHz!\n",
387 drv->versions = BIT(IPQ8074_ACORN_VERSION);
394 static const char *generic_genpd_names[] = { "perf", NULL };
396 static const struct qcom_cpufreq_match_data match_data_kryo = {
397 .get_version = qcom_cpufreq_kryo_name_version,
400 static const struct qcom_cpufreq_match_data match_data_krait = {
401 .get_version = qcom_cpufreq_krait_name_version,
404 static const struct qcom_cpufreq_match_data match_data_msm8909 = {
405 .get_version = qcom_cpufreq_simple_get_version,
406 .genpd_names = generic_genpd_names,
409 static const char *qcs404_genpd_names[] = { "cpr", NULL };
411 static const struct qcom_cpufreq_match_data match_data_qcs404 = {
412 .genpd_names = qcs404_genpd_names,
415 static const struct qcom_cpufreq_match_data match_data_ipq6018 = {
416 .get_version = qcom_cpufreq_ipq6018_name_version,
419 static const struct qcom_cpufreq_match_data match_data_ipq8064 = {
420 .get_version = qcom_cpufreq_ipq8064_name_version,
423 static const struct qcom_cpufreq_match_data match_data_ipq8074 = {
424 .get_version = qcom_cpufreq_ipq8074_name_version,
427 static int qcom_cpufreq_probe(struct platform_device *pdev)
429 struct qcom_cpufreq_drv *drv;
430 struct nvmem_cell *speedbin_nvmem;
431 struct device_node *np;
432 struct device *cpu_dev;
433 char pvs_name_buffer[] = "speedXX-pvsXX-vXX";
434 char *pvs_name = pvs_name_buffer;
436 const struct of_device_id *match;
439 cpu_dev = get_cpu_device(0);
443 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
447 ret = of_device_is_compatible(np, "operating-points-v2-kryo-cpu") ||
448 of_device_is_compatible(np, "operating-points-v2-krait-cpu");
454 drv = devm_kzalloc(&pdev->dev, struct_size(drv, cpus, num_possible_cpus()),
459 match = pdev->dev.platform_data;
460 drv->data = match->data;
464 if (drv->data->get_version) {
465 speedbin_nvmem = of_nvmem_cell_get(np, NULL);
466 if (IS_ERR(speedbin_nvmem))
467 return dev_err_probe(cpu_dev, PTR_ERR(speedbin_nvmem),
468 "Could not get nvmem cell\n");
470 ret = drv->data->get_version(cpu_dev,
471 speedbin_nvmem, &pvs_name, drv);
473 nvmem_cell_put(speedbin_nvmem);
476 nvmem_cell_put(speedbin_nvmem);
480 for_each_possible_cpu(cpu) {
481 struct dev_pm_opp_config config = {
482 .supported_hw = NULL,
485 cpu_dev = get_cpu_device(cpu);
486 if (NULL == cpu_dev) {
491 if (drv->data->get_version) {
492 config.supported_hw = &drv->versions;
493 config.supported_hw_count = 1;
496 config.prop_name = pvs_name;
499 if (drv->data->genpd_names) {
500 config.genpd_names = drv->data->genpd_names;
501 config.virt_devs = NULL;
504 if (config.supported_hw || config.genpd_names) {
505 drv->cpus[cpu].opp_token = dev_pm_opp_set_config(cpu_dev, &config);
506 if (drv->cpus[cpu].opp_token < 0) {
507 ret = drv->cpus[cpu].opp_token;
508 dev_err(cpu_dev, "Failed to set OPP config\n");
514 cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
516 if (!IS_ERR(cpufreq_dt_pdev)) {
517 platform_set_drvdata(pdev, drv);
521 ret = PTR_ERR(cpufreq_dt_pdev);
522 dev_err(cpu_dev, "Failed to register platform device\n");
525 for_each_possible_cpu(cpu)
526 dev_pm_opp_clear_config(drv->cpus[cpu].opp_token);
530 static void qcom_cpufreq_remove(struct platform_device *pdev)
532 struct qcom_cpufreq_drv *drv = platform_get_drvdata(pdev);
535 platform_device_unregister(cpufreq_dt_pdev);
537 for_each_possible_cpu(cpu)
538 dev_pm_opp_clear_config(drv->cpus[cpu].opp_token);
541 static struct platform_driver qcom_cpufreq_driver = {
542 .probe = qcom_cpufreq_probe,
543 .remove_new = qcom_cpufreq_remove,
545 .name = "qcom-cpufreq-nvmem",
549 static const struct of_device_id qcom_cpufreq_match_list[] __initconst = {
550 { .compatible = "qcom,apq8096", .data = &match_data_kryo },
551 { .compatible = "qcom,msm8909", .data = &match_data_msm8909 },
552 { .compatible = "qcom,msm8996", .data = &match_data_kryo },
553 { .compatible = "qcom,qcs404", .data = &match_data_qcs404 },
554 { .compatible = "qcom,ipq5332", .data = &match_data_kryo },
555 { .compatible = "qcom,ipq6018", .data = &match_data_ipq6018 },
556 { .compatible = "qcom,ipq8064", .data = &match_data_ipq8064 },
557 { .compatible = "qcom,ipq8074", .data = &match_data_ipq8074 },
558 { .compatible = "qcom,apq8064", .data = &match_data_krait },
559 { .compatible = "qcom,ipq9574", .data = &match_data_kryo },
560 { .compatible = "qcom,msm8974", .data = &match_data_krait },
561 { .compatible = "qcom,msm8960", .data = &match_data_krait },
564 MODULE_DEVICE_TABLE(of, qcom_cpufreq_match_list);
567 * Since the driver depends on smem and nvmem drivers, which may
568 * return EPROBE_DEFER, all the real activity is done in the probe,
569 * which may be defered as well. The init here is only registering
570 * the driver and the platform device.
572 static int __init qcom_cpufreq_init(void)
574 struct device_node *np = of_find_node_by_path("/");
575 const struct of_device_id *match;
581 match = of_match_node(qcom_cpufreq_match_list, np);
586 ret = platform_driver_register(&qcom_cpufreq_driver);
587 if (unlikely(ret < 0))
590 cpufreq_pdev = platform_device_register_data(NULL, "qcom-cpufreq-nvmem",
591 -1, match, sizeof(*match));
592 ret = PTR_ERR_OR_ZERO(cpufreq_pdev);
596 platform_driver_unregister(&qcom_cpufreq_driver);
599 module_init(qcom_cpufreq_init);
601 static void __exit qcom_cpufreq_exit(void)
603 platform_device_unregister(cpufreq_pdev);
604 platform_driver_unregister(&qcom_cpufreq_driver);
606 module_exit(qcom_cpufreq_exit);
608 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. CPUfreq driver");
609 MODULE_LICENSE("GPL v2");