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>
27 #include <linux/pm_domain.h>
28 #include <linux/pm_opp.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/slab.h>
31 #include <linux/soc/qcom/smem.h>
33 #include <dt-bindings/arm/qcom,ids.h>
35 enum ipq806x_versions
{
41 #define IPQ6000_VERSION BIT(2)
43 enum ipq8074_versions
{
44 IPQ8074_HAWKEYE_VERSION
= 0,
45 IPQ8074_ACORN_VERSION
,
48 struct qcom_cpufreq_drv
;
50 struct qcom_cpufreq_match_data
{
51 int (*get_version
)(struct device
*cpu_dev
,
52 struct nvmem_cell
*speedbin_nvmem
,
54 struct qcom_cpufreq_drv
*drv
);
55 const char **genpd_names
;
58 struct qcom_cpufreq_drv_cpu
{
60 struct device
**virt_devs
;
63 struct qcom_cpufreq_drv
{
65 const struct qcom_cpufreq_match_data
*data
;
66 struct qcom_cpufreq_drv_cpu cpus
[];
69 static struct platform_device
*cpufreq_dt_pdev
, *cpufreq_pdev
;
71 static int qcom_cpufreq_simple_get_version(struct device
*cpu_dev
,
72 struct nvmem_cell
*speedbin_nvmem
,
74 struct qcom_cpufreq_drv
*drv
)
79 speedbin
= nvmem_cell_read(speedbin_nvmem
, NULL
);
81 return PTR_ERR(speedbin
);
83 dev_dbg(cpu_dev
, "speedbin: %d\n", *speedbin
);
84 drv
->versions
= 1 << *speedbin
;
89 static void get_krait_bin_format_a(struct device
*cpu_dev
,
95 pte_efuse
= *((u32
*)buf
);
97 *speed
= pte_efuse
& 0xf;
99 *speed
= (pte_efuse
>> 4) & 0xf;
103 dev_warn(cpu_dev
, "Speed bin: Defaulting to %d\n", *speed
);
105 dev_dbg(cpu_dev
, "Speed bin: %d\n", *speed
);
108 *pvs
= (pte_efuse
>> 10) & 0x7;
110 *pvs
= (pte_efuse
>> 13) & 0x7;
114 dev_warn(cpu_dev
, "PVS bin: Defaulting to %d\n", *pvs
);
116 dev_dbg(cpu_dev
, "PVS bin: %d\n", *pvs
);
120 static void get_krait_bin_format_b(struct device
*cpu_dev
,
121 int *speed
, int *pvs
, int *pvs_ver
,
124 u32 pte_efuse
, redundant_sel
;
126 pte_efuse
= *((u32
*)buf
);
127 redundant_sel
= (pte_efuse
>> 24) & 0x7;
129 *pvs_ver
= (pte_efuse
>> 4) & 0x3;
131 switch (redundant_sel
) {
133 *pvs
= ((pte_efuse
>> 28) & 0x8) | ((pte_efuse
>> 6) & 0x7);
134 *speed
= (pte_efuse
>> 27) & 0xf;
137 *pvs
= (pte_efuse
>> 27) & 0xf;
138 *speed
= pte_efuse
& 0x7;
141 /* 4 bits of PVS are in efuse register bits 31, 8-6. */
142 *pvs
= ((pte_efuse
>> 28) & 0x8) | ((pte_efuse
>> 6) & 0x7);
143 *speed
= pte_efuse
& 0x7;
146 /* Check SPEED_BIN_BLOW_STATUS */
147 if (pte_efuse
& BIT(3)) {
148 dev_dbg(cpu_dev
, "Speed bin: %d\n", *speed
);
150 dev_warn(cpu_dev
, "Speed bin not set. Defaulting to 0!\n");
154 /* Check PVS_BLOW_STATUS */
155 pte_efuse
= *(((u32
*)buf
) + 1);
156 pte_efuse
&= BIT(21);
158 dev_dbg(cpu_dev
, "PVS bin: %d\n", *pvs
);
160 dev_warn(cpu_dev
, "PVS bin not set. Defaulting to 0!\n");
164 dev_dbg(cpu_dev
, "PVS version: %d\n", *pvs_ver
);
167 static int qcom_cpufreq_kryo_name_version(struct device
*cpu_dev
,
168 struct nvmem_cell
*speedbin_nvmem
,
170 struct qcom_cpufreq_drv
*drv
)
178 ret
= qcom_smem_get_soc_id(&msm_id
);
182 speedbin
= nvmem_cell_read(speedbin_nvmem
, &len
);
183 if (IS_ERR(speedbin
))
184 return PTR_ERR(speedbin
);
187 case QCOM_ID_MSM8996
:
188 case QCOM_ID_APQ8096
:
189 case QCOM_ID_IPQ5332
:
190 case QCOM_ID_IPQ5322
:
191 case QCOM_ID_IPQ5312
:
192 case QCOM_ID_IPQ5302
:
193 case QCOM_ID_IPQ5300
:
194 case QCOM_ID_IPQ5321
:
195 case QCOM_ID_IPQ9514
:
196 case QCOM_ID_IPQ9550
:
197 case QCOM_ID_IPQ9554
:
198 case QCOM_ID_IPQ9570
:
199 case QCOM_ID_IPQ9574
:
200 drv
->versions
= 1 << (unsigned int)(*speedbin
);
202 case QCOM_ID_MSM8996SG
:
203 case QCOM_ID_APQ8096SG
:
204 drv
->versions
= 1 << ((unsigned int)(*speedbin
) + 4);
215 static int qcom_cpufreq_krait_name_version(struct device
*cpu_dev
,
216 struct nvmem_cell
*speedbin_nvmem
,
218 struct qcom_cpufreq_drv
*drv
)
220 int speed
= 0, pvs
= 0, pvs_ver
= 0;
225 speedbin
= nvmem_cell_read(speedbin_nvmem
, &len
);
227 if (IS_ERR(speedbin
))
228 return PTR_ERR(speedbin
);
232 get_krait_bin_format_a(cpu_dev
, &speed
, &pvs
, speedbin
);
235 get_krait_bin_format_b(cpu_dev
, &speed
, &pvs
, &pvs_ver
,
239 dev_err(cpu_dev
, "Unable to read nvmem data. Defaulting to 0!\n");
244 snprintf(*pvs_name
, sizeof("speedXX-pvsXX-vXX"), "speed%d-pvs%d-v%d",
245 speed
, pvs
, pvs_ver
);
247 drv
->versions
= (1 << speed
);
254 static int qcom_cpufreq_ipq8064_name_version(struct device
*cpu_dev
,
255 struct nvmem_cell
*speedbin_nvmem
,
257 struct qcom_cpufreq_drv
*drv
)
259 int speed
= 0, pvs
= 0;
264 speedbin
= nvmem_cell_read(speedbin_nvmem
, &len
);
265 if (IS_ERR(speedbin
))
266 return PTR_ERR(speedbin
);
269 dev_err(cpu_dev
, "Unable to read nvmem data. Defaulting to 0!\n");
274 get_krait_bin_format_a(cpu_dev
, &speed
, &pvs
, speedbin
);
276 ret
= qcom_smem_get_soc_id(&msm_id
);
281 case QCOM_ID_IPQ8062
:
282 drv
->versions
= BIT(IPQ8062_VERSION
);
284 case QCOM_ID_IPQ8064
:
285 case QCOM_ID_IPQ8066
:
286 case QCOM_ID_IPQ8068
:
287 drv
->versions
= BIT(IPQ8064_VERSION
);
289 case QCOM_ID_IPQ8065
:
290 case QCOM_ID_IPQ8069
:
291 drv
->versions
= BIT(IPQ8065_VERSION
);
295 "SoC ID %u is not part of IPQ8064 family, limiting to 1.0GHz!\n",
297 drv
->versions
= BIT(IPQ8062_VERSION
);
301 /* IPQ8064 speed is never fused. Only pvs values are fused. */
302 snprintf(*pvs_name
, sizeof("speed0-pvsXX"), "speed0-pvs%d", pvs
);
309 static int qcom_cpufreq_ipq6018_name_version(struct device
*cpu_dev
,
310 struct nvmem_cell
*speedbin_nvmem
,
312 struct qcom_cpufreq_drv
*drv
)
319 ret
= qcom_smem_get_soc_id(&msm_id
);
323 speedbin
= nvmem_cell_read(speedbin_nvmem
, NULL
);
324 if (IS_ERR(speedbin
))
325 return PTR_ERR(speedbin
);
328 case QCOM_ID_IPQ6005
:
329 case QCOM_ID_IPQ6010
:
330 case QCOM_ID_IPQ6018
:
331 case QCOM_ID_IPQ6028
:
332 /* Fuse Value Freq BIT to set
333 * ---------------------------------
334 * 2’b0 No Limit BIT(0)
335 * 2’b1 1.5 GHz BIT(1)
337 drv
->versions
= 1 << (unsigned int)(*speedbin
);
339 case QCOM_ID_IPQ6000
:
341 * IPQ6018 family only has one bit to advertise the CPU
342 * speed-bin, but that is not enough for IPQ6000 which
343 * is only rated up to 1.2GHz.
344 * So for IPQ6000 manually set BIT(2) based on SMEM ID.
346 drv
->versions
= IPQ6000_VERSION
;
350 "SoC ID %u is not part of IPQ6018 family, limiting to 1.2GHz!\n",
352 drv
->versions
= IPQ6000_VERSION
;
360 static int qcom_cpufreq_ipq8074_name_version(struct device
*cpu_dev
,
361 struct nvmem_cell
*speedbin_nvmem
,
363 struct qcom_cpufreq_drv
*drv
)
369 ret
= qcom_smem_get_soc_id(&msm_id
);
374 case QCOM_ID_IPQ8070A
:
375 case QCOM_ID_IPQ8071A
:
376 case QCOM_ID_IPQ8172
:
377 case QCOM_ID_IPQ8173
:
378 case QCOM_ID_IPQ8174
:
379 drv
->versions
= BIT(IPQ8074_ACORN_VERSION
);
381 case QCOM_ID_IPQ8072A
:
382 case QCOM_ID_IPQ8074A
:
383 case QCOM_ID_IPQ8076A
:
384 case QCOM_ID_IPQ8078A
:
385 drv
->versions
= BIT(IPQ8074_HAWKEYE_VERSION
);
389 "SoC ID %u is not part of IPQ8074 family, limiting to 1.4GHz!\n",
391 drv
->versions
= BIT(IPQ8074_ACORN_VERSION
);
398 static const char *generic_genpd_names
[] = { "perf", NULL
};
400 static const struct qcom_cpufreq_match_data match_data_kryo
= {
401 .get_version
= qcom_cpufreq_kryo_name_version
,
404 static const struct qcom_cpufreq_match_data match_data_krait
= {
405 .get_version
= qcom_cpufreq_krait_name_version
,
408 static const struct qcom_cpufreq_match_data match_data_msm8909
= {
409 .get_version
= qcom_cpufreq_simple_get_version
,
410 .genpd_names
= generic_genpd_names
,
413 static const char *qcs404_genpd_names
[] = { "cpr", NULL
};
415 static const struct qcom_cpufreq_match_data match_data_qcs404
= {
416 .genpd_names
= qcs404_genpd_names
,
419 static const struct qcom_cpufreq_match_data match_data_ipq6018
= {
420 .get_version
= qcom_cpufreq_ipq6018_name_version
,
423 static const struct qcom_cpufreq_match_data match_data_ipq8064
= {
424 .get_version
= qcom_cpufreq_ipq8064_name_version
,
427 static const struct qcom_cpufreq_match_data match_data_ipq8074
= {
428 .get_version
= qcom_cpufreq_ipq8074_name_version
,
431 static void qcom_cpufreq_suspend_virt_devs(struct qcom_cpufreq_drv
*drv
, unsigned int cpu
)
433 const char * const *name
= drv
->data
->genpd_names
;
436 if (!drv
->cpus
[cpu
].virt_devs
)
439 for (i
= 0; *name
; i
++, name
++)
440 device_set_awake_path(drv
->cpus
[cpu
].virt_devs
[i
]);
443 static void qcom_cpufreq_put_virt_devs(struct qcom_cpufreq_drv
*drv
, unsigned int cpu
)
445 const char * const *name
= drv
->data
->genpd_names
;
448 if (!drv
->cpus
[cpu
].virt_devs
)
451 for (i
= 0; *name
; i
++, name
++)
452 pm_runtime_put(drv
->cpus
[cpu
].virt_devs
[i
]);
455 static int qcom_cpufreq_probe(struct platform_device
*pdev
)
457 struct qcom_cpufreq_drv
*drv
;
458 struct nvmem_cell
*speedbin_nvmem
;
459 struct device
*cpu_dev
;
460 char pvs_name_buffer
[] = "speedXX-pvsXX-vXX";
461 char *pvs_name
= pvs_name_buffer
;
463 const struct of_device_id
*match
;
466 cpu_dev
= get_cpu_device(0);
470 struct device_node
*np
__free(device_node
) =
471 dev_pm_opp_of_get_opp_desc_node(cpu_dev
);
475 ret
= of_device_is_compatible(np
, "operating-points-v2-kryo-cpu") ||
476 of_device_is_compatible(np
, "operating-points-v2-krait-cpu");
480 drv
= devm_kzalloc(&pdev
->dev
, struct_size(drv
, cpus
, num_possible_cpus()),
485 match
= pdev
->dev
.platform_data
;
486 drv
->data
= match
->data
;
490 if (drv
->data
->get_version
) {
491 speedbin_nvmem
= of_nvmem_cell_get(np
, NULL
);
492 if (IS_ERR(speedbin_nvmem
))
493 return dev_err_probe(cpu_dev
, PTR_ERR(speedbin_nvmem
),
494 "Could not get nvmem cell\n");
496 ret
= drv
->data
->get_version(cpu_dev
,
497 speedbin_nvmem
, &pvs_name
, drv
);
499 nvmem_cell_put(speedbin_nvmem
);
502 nvmem_cell_put(speedbin_nvmem
);
505 for_each_possible_cpu(cpu
) {
506 struct device
**virt_devs
= NULL
;
507 struct dev_pm_opp_config config
= {
508 .supported_hw
= NULL
,
511 cpu_dev
= get_cpu_device(cpu
);
512 if (NULL
== cpu_dev
) {
517 if (drv
->data
->get_version
) {
518 config
.supported_hw
= &drv
->versions
;
519 config
.supported_hw_count
= 1;
522 config
.prop_name
= pvs_name
;
525 if (drv
->data
->genpd_names
) {
526 config
.genpd_names
= drv
->data
->genpd_names
;
527 config
.virt_devs
= &virt_devs
;
530 if (config
.supported_hw
|| config
.genpd_names
) {
531 drv
->cpus
[cpu
].opp_token
= dev_pm_opp_set_config(cpu_dev
, &config
);
532 if (drv
->cpus
[cpu
].opp_token
< 0) {
533 ret
= drv
->cpus
[cpu
].opp_token
;
534 dev_err(cpu_dev
, "Failed to set OPP config\n");
540 const char * const *name
= config
.genpd_names
;
543 for (i
= 0; *name
; i
++, name
++) {
544 ret
= pm_runtime_resume_and_get(virt_devs
[i
]);
546 dev_err(cpu_dev
, "failed to resume %s: %d\n",
549 /* Rollback previous PM runtime calls */
550 name
= config
.genpd_names
;
551 for (j
= 0; *name
&& j
< i
; j
++, name
++)
552 pm_runtime_put(virt_devs
[j
]);
557 drv
->cpus
[cpu
].virt_devs
= virt_devs
;
561 cpufreq_dt_pdev
= platform_device_register_simple("cpufreq-dt", -1,
563 if (!IS_ERR(cpufreq_dt_pdev
)) {
564 platform_set_drvdata(pdev
, drv
);
568 ret
= PTR_ERR(cpufreq_dt_pdev
);
569 dev_err(cpu_dev
, "Failed to register platform device\n");
572 for_each_possible_cpu(cpu
) {
573 qcom_cpufreq_put_virt_devs(drv
, cpu
);
574 dev_pm_opp_clear_config(drv
->cpus
[cpu
].opp_token
);
579 static void qcom_cpufreq_remove(struct platform_device
*pdev
)
581 struct qcom_cpufreq_drv
*drv
= platform_get_drvdata(pdev
);
584 platform_device_unregister(cpufreq_dt_pdev
);
586 for_each_possible_cpu(cpu
) {
587 qcom_cpufreq_put_virt_devs(drv
, cpu
);
588 dev_pm_opp_clear_config(drv
->cpus
[cpu
].opp_token
);
592 static int qcom_cpufreq_suspend(struct device
*dev
)
594 struct qcom_cpufreq_drv
*drv
= dev_get_drvdata(dev
);
597 for_each_possible_cpu(cpu
)
598 qcom_cpufreq_suspend_virt_devs(drv
, cpu
);
603 static DEFINE_SIMPLE_DEV_PM_OPS(qcom_cpufreq_pm_ops
, qcom_cpufreq_suspend
, NULL
);
605 static struct platform_driver qcom_cpufreq_driver
= {
606 .probe
= qcom_cpufreq_probe
,
607 .remove_new
= qcom_cpufreq_remove
,
609 .name
= "qcom-cpufreq-nvmem",
610 .pm
= pm_sleep_ptr(&qcom_cpufreq_pm_ops
),
614 static const struct of_device_id qcom_cpufreq_match_list
[] __initconst __maybe_unused
= {
615 { .compatible
= "qcom,apq8096", .data
= &match_data_kryo
},
616 { .compatible
= "qcom,msm8909", .data
= &match_data_msm8909
},
617 { .compatible
= "qcom,msm8996", .data
= &match_data_kryo
},
618 { .compatible
= "qcom,qcs404", .data
= &match_data_qcs404
},
619 { .compatible
= "qcom,ipq5332", .data
= &match_data_kryo
},
620 { .compatible
= "qcom,ipq6018", .data
= &match_data_ipq6018
},
621 { .compatible
= "qcom,ipq8064", .data
= &match_data_ipq8064
},
622 { .compatible
= "qcom,ipq8074", .data
= &match_data_ipq8074
},
623 { .compatible
= "qcom,apq8064", .data
= &match_data_krait
},
624 { .compatible
= "qcom,ipq9574", .data
= &match_data_kryo
},
625 { .compatible
= "qcom,msm8974", .data
= &match_data_krait
},
626 { .compatible
= "qcom,msm8960", .data
= &match_data_krait
},
629 MODULE_DEVICE_TABLE(of
, qcom_cpufreq_match_list
);
632 * Since the driver depends on smem and nvmem drivers, which may
633 * return EPROBE_DEFER, all the real activity is done in the probe,
634 * which may be defered as well. The init here is only registering
635 * the driver and the platform device.
637 static int __init
qcom_cpufreq_init(void)
639 struct device_node
*np
__free(device_node
) = of_find_node_by_path("/");
640 const struct of_device_id
*match
;
646 match
= of_match_node(qcom_cpufreq_match_list
, np
);
650 ret
= platform_driver_register(&qcom_cpufreq_driver
);
651 if (unlikely(ret
< 0))
654 cpufreq_pdev
= platform_device_register_data(NULL
, "qcom-cpufreq-nvmem",
655 -1, match
, sizeof(*match
));
656 ret
= PTR_ERR_OR_ZERO(cpufreq_pdev
);
660 platform_driver_unregister(&qcom_cpufreq_driver
);
663 module_init(qcom_cpufreq_init
);
665 static void __exit
qcom_cpufreq_exit(void)
667 platform_device_unregister(cpufreq_pdev
);
668 platform_driver_unregister(&qcom_cpufreq_driver
);
670 module_exit(qcom_cpufreq_exit
);
672 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. CPUfreq driver");
673 MODULE_LICENSE("GPL v2");