1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2014,2015, Linaro Ltd.
6 * SAW power controller driver
9 #include <linux/kernel.h>
10 #include <linux/init.h>
12 #include <linux/slab.h>
14 #include <linux/of_platform.h>
15 #include <linux/err.h>
16 #include <linux/platform_device.h>
17 #include <linux/cpuidle.h>
18 #include <linux/cpu_pm.h>
19 #include <linux/firmware/qcom/qcom_scm.h>
20 #include <soc/qcom/spm.h>
22 #include <asm/proc-fns.h>
23 #include <asm/suspend.h>
25 #include "dt_idle_states.h"
27 struct cpuidle_qcom_spm_data {
28 struct cpuidle_driver cpuidle_driver;
29 struct spm_driver_data *spm;
32 static int qcom_pm_collapse(unsigned long int unused)
34 qcom_scm_cpu_power_down(QCOM_SCM_CPU_PWR_DOWN_L2_ON);
37 * Returns here only if there was a pending interrupt and we did not
38 * power down as a result.
43 static int qcom_cpu_spc(struct spm_driver_data *drv)
47 spm_set_low_power_mode(drv, PM_SLEEP_MODE_SPC);
48 ret = cpu_suspend(0, qcom_pm_collapse);
50 * ARM common code executes WFI without calling into our driver and
51 * if the SPM mode is not reset, then we may accidently power down the
52 * cpu when we intended only to gate the cpu clock.
53 * Ensure the state is set to standby before returning.
55 spm_set_low_power_mode(drv, PM_SLEEP_MODE_STBY);
60 static __cpuidle int spm_enter_idle_state(struct cpuidle_device *dev,
61 struct cpuidle_driver *drv, int idx)
63 struct cpuidle_qcom_spm_data *data = container_of(drv, struct cpuidle_qcom_spm_data,
66 return CPU_PM_CPU_IDLE_ENTER_PARAM(qcom_cpu_spc, idx, data->spm);
69 static struct cpuidle_driver qcom_spm_idle_driver = {
73 .enter = spm_enter_idle_state,
75 .target_residency = 1,
76 .power_usage = UINT_MAX,
82 static const struct of_device_id qcom_idle_state_match[] = {
83 { .compatible = "qcom,idle-state-spc", .data = spm_enter_idle_state },
87 static int spm_cpuidle_register(struct device *cpuidle_dev, int cpu)
89 struct platform_device *pdev = NULL;
90 struct device_node *cpu_node, *saw_node;
91 struct cpuidle_qcom_spm_data *data = NULL;
94 cpu_node = of_cpu_device_node_get(cpu);
98 saw_node = of_parse_phandle(cpu_node, "qcom,saw", 0);
102 pdev = of_find_device_by_node(saw_node);
103 of_node_put(saw_node);
104 of_node_put(cpu_node);
108 data = devm_kzalloc(cpuidle_dev, sizeof(*data), GFP_KERNEL);
112 data->spm = dev_get_drvdata(&pdev->dev);
116 data->cpuidle_driver = qcom_spm_idle_driver;
117 data->cpuidle_driver.cpumask = (struct cpumask *)cpumask_of(cpu);
119 ret = dt_init_idle_driver(&data->cpuidle_driver,
120 qcom_idle_state_match, 1);
122 return ret ? : -ENODEV;
124 return cpuidle_register(&data->cpuidle_driver, NULL);
127 static int spm_cpuidle_drv_probe(struct platform_device *pdev)
131 if (!qcom_scm_is_available())
132 return -EPROBE_DEFER;
134 ret = qcom_scm_set_warm_boot_addr(cpu_resume_arm);
136 return dev_err_probe(&pdev->dev, ret, "set warm boot addr failed");
138 for_each_possible_cpu(cpu) {
139 ret = spm_cpuidle_register(&pdev->dev, cpu);
140 if (ret && ret != -ENODEV) {
142 "Cannot register for CPU%d: %d\n", cpu, ret);
149 static struct platform_driver spm_cpuidle_driver = {
150 .probe = spm_cpuidle_drv_probe,
152 .name = "qcom-spm-cpuidle",
153 .suppress_bind_attrs = true,
157 static bool __init qcom_spm_find_any_cpu(void)
159 struct device_node *cpu_node, *saw_node;
161 for_each_of_cpu_node(cpu_node) {
162 saw_node = of_parse_phandle(cpu_node, "qcom,saw", 0);
163 if (of_device_is_available(saw_node)) {
164 of_node_put(saw_node);
165 of_node_put(cpu_node);
168 of_node_put(saw_node);
173 static int __init qcom_spm_cpuidle_init(void)
175 struct platform_device *pdev;
178 ret = platform_driver_register(&spm_cpuidle_driver);
182 /* Make sure there is actually any CPU managed by the SPM */
183 if (!qcom_spm_find_any_cpu())
186 pdev = platform_device_register_simple("qcom-spm-cpuidle",
189 platform_driver_unregister(&spm_cpuidle_driver);
190 return PTR_ERR(pdev);
195 device_initcall(qcom_spm_cpuidle_init);