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_address.h>
15 #include <linux/of_device.h>
16 #include <linux/err.h>
17 #include <linux/platform_device.h>
18 #include <linux/cpuidle.h>
19 #include <linux/cpu_pm.h>
20 #include <linux/qcom_scm.h>
21 #include <soc/qcom/spm.h>
23 #include <asm/proc-fns.h>
24 #include <asm/suspend.h>
26 #include "dt_idle_states.h"
28 struct cpuidle_qcom_spm_data {
29 struct cpuidle_driver cpuidle_driver;
30 struct spm_driver_data *spm;
33 static int qcom_pm_collapse(unsigned long int unused)
35 qcom_scm_cpu_power_down(QCOM_SCM_CPU_PWR_DOWN_L2_ON);
38 * Returns here only if there was a pending interrupt and we did not
39 * power down as a result.
44 static int qcom_cpu_spc(struct spm_driver_data *drv)
48 spm_set_low_power_mode(drv, PM_SLEEP_MODE_SPC);
49 ret = cpu_suspend(0, qcom_pm_collapse);
51 * ARM common code executes WFI without calling into our driver and
52 * if the SPM mode is not reset, then we may accidently power down the
53 * cpu when we intended only to gate the cpu clock.
54 * Ensure the state is set to standby before returning.
56 spm_set_low_power_mode(drv, PM_SLEEP_MODE_STBY);
61 static int spm_enter_idle_state(struct cpuidle_device *dev,
62 struct cpuidle_driver *drv, int idx)
64 struct cpuidle_qcom_spm_data *data = container_of(drv, struct cpuidle_qcom_spm_data,
67 return CPU_PM_CPU_IDLE_ENTER_PARAM(qcom_cpu_spc, idx, data->spm);
70 static struct cpuidle_driver qcom_spm_idle_driver = {
74 .enter = spm_enter_idle_state,
76 .target_residency = 1,
77 .power_usage = UINT_MAX,
83 static const struct of_device_id qcom_idle_state_match[] = {
84 { .compatible = "qcom,idle-state-spc", .data = spm_enter_idle_state },
88 static int spm_cpuidle_register(struct device *cpuidle_dev, int cpu)
90 struct platform_device *pdev = NULL;
91 struct device_node *cpu_node, *saw_node;
92 struct cpuidle_qcom_spm_data *data = NULL;
95 cpu_node = of_cpu_device_node_get(cpu);
99 saw_node = of_parse_phandle(cpu_node, "qcom,saw", 0);
103 pdev = of_find_device_by_node(saw_node);
104 of_node_put(saw_node);
105 of_node_put(cpu_node);
109 data = devm_kzalloc(cpuidle_dev, sizeof(*data), GFP_KERNEL);
113 data->spm = dev_get_drvdata(&pdev->dev);
117 data->cpuidle_driver = qcom_spm_idle_driver;
118 data->cpuidle_driver.cpumask = (struct cpumask *)cpumask_of(cpu);
120 ret = dt_init_idle_driver(&data->cpuidle_driver,
121 qcom_idle_state_match, 1);
123 return ret ? : -ENODEV;
125 return cpuidle_register(&data->cpuidle_driver, NULL);
128 static int spm_cpuidle_drv_probe(struct platform_device *pdev)
132 if (!qcom_scm_is_available())
133 return -EPROBE_DEFER;
135 ret = qcom_scm_set_warm_boot_addr(cpu_resume_arm);
137 return dev_err_probe(&pdev->dev, ret, "set warm boot addr failed");
139 for_each_possible_cpu(cpu) {
140 ret = spm_cpuidle_register(&pdev->dev, cpu);
141 if (ret && ret != -ENODEV) {
143 "Cannot register for CPU%d: %d\n", cpu, ret);
150 static struct platform_driver spm_cpuidle_driver = {
151 .probe = spm_cpuidle_drv_probe,
153 .name = "qcom-spm-cpuidle",
154 .suppress_bind_attrs = true,
158 static bool __init qcom_spm_find_any_cpu(void)
160 struct device_node *cpu_node, *saw_node;
162 for_each_of_cpu_node(cpu_node) {
163 saw_node = of_parse_phandle(cpu_node, "qcom,saw", 0);
164 if (of_device_is_available(saw_node)) {
165 of_node_put(saw_node);
166 of_node_put(cpu_node);
169 of_node_put(saw_node);
174 static int __init qcom_spm_cpuidle_init(void)
176 struct platform_device *pdev;
179 ret = platform_driver_register(&spm_cpuidle_driver);
183 /* Make sure there is actually any CPU managed by the SPM */
184 if (!qcom_spm_find_any_cpu())
187 pdev = platform_device_register_simple("qcom-spm-cpuidle",
190 platform_driver_unregister(&spm_cpuidle_driver);
191 return PTR_ERR(pdev);
196 device_initcall(qcom_spm_cpuidle_init);