xref: /linux/drivers/cpuidle/cpuidle-qcom-spm.c (revision c83b49383b595be50647f0c764a48c78b5f3c4f8)
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 
27 struct cpuidle_qcom_spm_data {
28 	struct cpuidle_driver cpuidle_driver;
29 	struct spm_driver_data *spm;
30 };
31 
32 static 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 
43 static int qcom_cpu_spc(struct spm_driver_data *drv)
44 {
45 	int ret;
46 
47 	spm_set_low_power_mode(drv, 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 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.
54 	 */
55 	spm_set_low_power_mode(drv, PM_SLEEP_MODE_STBY);
56 
57 	return ret;
58 }
59 
60 static __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 
69 static 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 
82 static const struct of_device_id qcom_idle_state_match[] = {
83 	{ .compatible = "qcom,idle-state-spc", .data = spm_enter_idle_state },
84 	{ },
85 };
86 
87 static int spm_cpuidle_register(struct device *cpuidle_dev, int cpu)
88 {
89 	struct platform_device *pdev = NULL;
90 	struct device_node *cpu_node, *saw_node;
91 	struct cpuidle_qcom_spm_data *data = NULL;
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(cpu_node, "qcom,saw", 0);
99 	if (!saw_node)
100 		return -ENODEV;
101 
102 	pdev = of_find_device_by_node(saw_node);
103 	of_node_put(saw_node);
104 	of_node_put(cpu_node);
105 	if (!pdev)
106 		return -ENODEV;
107 
108 	data = devm_kzalloc(cpuidle_dev, sizeof(*data), GFP_KERNEL);
109 	if (!data)
110 		return -ENOMEM;
111 
112 	data->spm = dev_get_drvdata(&pdev->dev);
113 	if (!data->spm)
114 		return -EINVAL;
115 
116 	data->cpuidle_driver = qcom_spm_idle_driver;
117 	data->cpuidle_driver.cpumask = (struct cpumask *)cpumask_of(cpu);
118 
119 	ret = dt_init_idle_driver(&data->cpuidle_driver,
120 				  qcom_idle_state_match, 1);
121 	if (ret <= 0)
122 		return ret ? : -ENODEV;
123 
124 	return cpuidle_register(&data->cpuidle_driver, NULL);
125 }
126 
127 static int spm_cpuidle_drv_probe(struct platform_device *pdev)
128 {
129 	int cpu, ret;
130 
131 	if (!qcom_scm_is_available())
132 		return -EPROBE_DEFER;
133 
134 	ret = qcom_scm_set_warm_boot_addr(cpu_resume_arm);
135 	if (ret)
136 		return dev_err_probe(&pdev->dev, ret, "set warm boot addr failed");
137 
138 	for_each_possible_cpu(cpu) {
139 		ret = spm_cpuidle_register(&pdev->dev, cpu);
140 		if (ret && ret != -ENODEV) {
141 			dev_err(&pdev->dev,
142 				"Cannot register for CPU%d: %d\n", cpu, ret);
143 		}
144 	}
145 
146 	return 0;
147 }
148 
149 static struct platform_driver spm_cpuidle_driver = {
150 	.probe = spm_cpuidle_drv_probe,
151 	.driver = {
152 		.name = "qcom-spm-cpuidle",
153 		.suppress_bind_attrs = true,
154 	},
155 };
156 
157 static bool __init qcom_spm_find_any_cpu(void)
158 {
159 	struct device_node *cpu_node, *saw_node;
160 
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);
166 			return true;
167 		}
168 		of_node_put(saw_node);
169 	}
170 	return false;
171 }
172 
173 static int __init qcom_spm_cpuidle_init(void)
174 {
175 	struct platform_device *pdev;
176 	int ret;
177 
178 	ret = platform_driver_register(&spm_cpuidle_driver);
179 	if (ret)
180 		return ret;
181 
182 	/* Make sure there is actually any CPU managed by the SPM */
183 	if (!qcom_spm_find_any_cpu())
184 		return 0;
185 
186 	pdev = platform_device_register_simple("qcom-spm-cpuidle",
187 					       -1, NULL, 0);
188 	if (IS_ERR(pdev)) {
189 		platform_driver_unregister(&spm_cpuidle_driver);
190 		return PTR_ERR(pdev);
191 	}
192 
193 	return 0;
194 }
195 device_initcall(qcom_spm_cpuidle_init);
196