1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2014,2015, Linaro Ltd.
5 *
6 * SAW power controller driver
7 */
8
9#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/io.h>
12#include <linux/slab.h>
13#include <linux/of.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>
21
22#include <asm/proc-fns.h>
23#include <asm/suspend.h>
24
25#include "dt_idle_states.h"
26
27struct cpuidle_qcom_spm_data {
28 struct cpuidle_driver cpuidle_driver;
29 struct spm_driver_data *spm;
30};
31
32static int qcom_pm_collapse(unsigned long int unused)
33{
34 qcom_scm_cpu_power_down(QCOM_SCM_CPU_PWR_DOWN_L2_ON);
35
36 /*
37 * Returns here only if there was a pending interrupt and we did not
38 * power down as a result.
39 */
40 return -1;
41}
42
43static int qcom_cpu_spc(struct spm_driver_data *drv)
44{
45 int ret;
46
47 spm_set_low_power_mode(drv, mode: PM_SLEEP_MODE_SPC);
48 ret = cpu_suspend(0, qcom_pm_collapse);
49 /*
50 * ARM common code executes WFI without calling into our driver and
51 * if the SPM mode is not reset, then we may accidentally power down the
52 * cpu when we intended only to gate the cpu clock.
53 * Ensure the state is set to standby before returning.
54 */
55 spm_set_low_power_mode(drv, mode: PM_SLEEP_MODE_STBY);
56
57 return ret;
58}
59
60static __cpuidle int spm_enter_idle_state(struct cpuidle_device *dev,
61 struct cpuidle_driver *drv, int idx)
62{
63 struct cpuidle_qcom_spm_data *data = container_of(drv, struct cpuidle_qcom_spm_data,
64 cpuidle_driver);
65
66 return CPU_PM_CPU_IDLE_ENTER_PARAM(qcom_cpu_spc, idx, data->spm);
67}
68
69static struct cpuidle_driver qcom_spm_idle_driver = {
70 .name = "qcom_spm",
71 .owner = THIS_MODULE,
72 .states[0] = {
73 .enter = spm_enter_idle_state,
74 .exit_latency = 1,
75 .target_residency = 1,
76 .power_usage = UINT_MAX,
77 .name = "WFI",
78 .desc = "ARM WFI",
79 }
80};
81
82static const struct of_device_id qcom_idle_state_match[] = {
83 { .compatible = "qcom,idle-state-spc", .data = spm_enter_idle_state },
84 { },
85};
86
87static int spm_cpuidle_register(struct device *cpuidle_dev, int cpu)
88{
89 struct platform_device *pdev;
90 struct device_node *cpu_node, *saw_node;
91 struct cpuidle_qcom_spm_data *data;
92 int ret;
93
94 cpu_node = of_cpu_device_node_get(cpu);
95 if (!cpu_node)
96 return -ENODEV;
97
98 saw_node = of_parse_phandle(np: cpu_node, phandle_name: "qcom,saw", index: 0);
99 of_node_put(node: cpu_node);
100 if (!saw_node)
101 return -ENODEV;
102
103 pdev = of_find_device_by_node(np: saw_node);
104 of_node_put(node: saw_node);
105 if (!pdev)
106 return -ENODEV;
107
108 data = devm_kzalloc(dev: cpuidle_dev, size: sizeof(*data), GFP_KERNEL);
109 if (!data) {
110 put_device(dev: &pdev->dev);
111 return -ENOMEM;
112 }
113
114 data->spm = dev_get_drvdata(dev: &pdev->dev);
115 put_device(dev: &pdev->dev);
116 if (!data->spm)
117 return -EINVAL;
118
119 data->cpuidle_driver = qcom_spm_idle_driver;
120 data->cpuidle_driver.cpumask = (struct cpumask *)cpumask_of(cpu);
121
122 ret = dt_init_idle_driver(drv: &data->cpuidle_driver,
123 matches: qcom_idle_state_match, start_idx: 1);
124 if (ret <= 0)
125 return ret ? : -ENODEV;
126
127 return cpuidle_register(drv: &data->cpuidle_driver, NULL);
128}
129
130static int spm_cpuidle_drv_probe(struct platform_device *pdev)
131{
132 int cpu, ret;
133
134 if (!qcom_scm_is_available())
135 return -EPROBE_DEFER;
136
137 ret = qcom_scm_set_warm_boot_addr(entry: cpu_resume_arm);
138 if (ret)
139 return dev_err_probe(dev: &pdev->dev, err: ret, fmt: "set warm boot addr failed");
140
141 for_each_present_cpu(cpu) {
142 ret = spm_cpuidle_register(cpuidle_dev: &pdev->dev, cpu);
143 if (ret && ret != -ENODEV) {
144 dev_err(&pdev->dev,
145 "Cannot register for CPU%d: %d\n", cpu, ret);
146 }
147 }
148
149 return 0;
150}
151
152static struct platform_driver spm_cpuidle_driver = {
153 .probe = spm_cpuidle_drv_probe,
154 .driver = {
155 .name = "qcom-spm-cpuidle",
156 .suppress_bind_attrs = true,
157 },
158};
159
160static bool __init qcom_spm_find_any_cpu(void)
161{
162 struct device_node *cpu_node, *saw_node;
163
164 for_each_of_cpu_node(cpu_node) {
165 saw_node = of_parse_phandle(np: cpu_node, phandle_name: "qcom,saw", index: 0);
166 if (of_device_is_available(device: saw_node)) {
167 of_node_put(node: saw_node);
168 of_node_put(node: cpu_node);
169 return true;
170 }
171 of_node_put(node: saw_node);
172 }
173 return false;
174}
175
176static int __init qcom_spm_cpuidle_init(void)
177{
178 struct platform_device *pdev;
179 int ret;
180
181 ret = platform_driver_register(&spm_cpuidle_driver);
182 if (ret)
183 return ret;
184
185 /* Make sure there is actually any CPU managed by the SPM */
186 if (!qcom_spm_find_any_cpu())
187 return 0;
188
189 pdev = platform_device_register_simple(name: "qcom-spm-cpuidle",
190 id: -1, NULL, num: 0);
191 if (IS_ERR(ptr: pdev)) {
192 platform_driver_unregister(&spm_cpuidle_driver);
193 return PTR_ERR(ptr: pdev);
194 }
195
196 return 0;
197}
198device_initcall(qcom_spm_cpuidle_init);
199

source code of linux/drivers/cpuidle/cpuidle-qcom-spm.c