blob: 01e77913a4144c5b11d9566b94e7a5c2cbfcf406 [file] [log] [blame]
Thomas Gleixner97fb5e82019-05-29 07:17:58 -07001// SPDX-License-Identifier: GPL-2.0-only
Lina Iyer7ce75bb2015-04-09 13:20:41 -06002/*
3 * Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2014,2015, Linaro Ltd.
5 *
Lina Iyer3b904b02016-01-04 10:58:28 -07006 * SAW power controller driver
Lina Iyer7ce75bb2015-04-09 13:20:41 -06007 */
8
Lina Iyer7ce75bb2015-04-09 13:20:41 -06009#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_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>
AngeloGioacchino Del Regno60f36922021-07-29 17:56:05 +020021#include <soc/qcom/spm.h>
Lina Iyer7ce75bb2015-04-09 13:20:41 -060022
Lina Iyer7ce75bb2015-04-09 13:20:41 -060023#include <asm/proc-fns.h>
24#include <asm/suspend.h>
25
Stephan Gerholda871be62020-04-16 10:58:21 +020026#include "dt_idle_states.h"
27
AngeloGioacchino Del Regno60f36922021-07-29 17:56:05 +020028struct cpuidle_qcom_spm_data {
Stephan Gerholda871be62020-04-16 10:58:21 +020029 struct cpuidle_driver cpuidle_driver;
AngeloGioacchino Del Regno60f36922021-07-29 17:56:05 +020030 struct spm_driver_data *spm;
Lina Iyer7ce75bb2015-04-09 13:20:41 -060031};
32
Lina Iyer7ce75bb2015-04-09 13:20:41 -060033static int qcom_pm_collapse(unsigned long int unused)
34{
35 qcom_scm_cpu_power_down(QCOM_SCM_CPU_PWR_DOWN_L2_ON);
36
37 /*
38 * Returns here only if there was a pending interrupt and we did not
39 * power down as a result.
40 */
41 return -1;
42}
43
Stephan Gerholda871be62020-04-16 10:58:21 +020044static int qcom_cpu_spc(struct spm_driver_data *drv)
Lina Iyer7ce75bb2015-04-09 13:20:41 -060045{
46 int ret;
Lina Iyer7ce75bb2015-04-09 13:20:41 -060047
48 spm_set_low_power_mode(drv, PM_SLEEP_MODE_SPC);
49 ret = cpu_suspend(0, qcom_pm_collapse);
50 /*
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.
55 */
56 spm_set_low_power_mode(drv, PM_SLEEP_MODE_STBY);
57
58 return ret;
59}
60
Stephan Gerholda871be62020-04-16 10:58:21 +020061static int spm_enter_idle_state(struct cpuidle_device *dev,
62 struct cpuidle_driver *drv, int idx)
Lina Iyer7ce75bb2015-04-09 13:20:41 -060063{
AngeloGioacchino Del Regno60f36922021-07-29 17:56:05 +020064 struct cpuidle_qcom_spm_data *data = container_of(drv, struct cpuidle_qcom_spm_data,
65 cpuidle_driver);
Stephan Gerholda871be62020-04-16 10:58:21 +020066
AngeloGioacchino Del Regno60f36922021-07-29 17:56:05 +020067 return CPU_PM_CPU_IDLE_ENTER_PARAM(qcom_cpu_spc, idx, data->spm);
Lina Iyer7ce75bb2015-04-09 13:20:41 -060068}
69
Stephan Gerholda871be62020-04-16 10:58:21 +020070static struct cpuidle_driver qcom_spm_idle_driver = {
71 .name = "qcom_spm",
72 .owner = THIS_MODULE,
73 .states[0] = {
74 .enter = spm_enter_idle_state,
75 .exit_latency = 1,
76 .target_residency = 1,
77 .power_usage = UINT_MAX,
78 .name = "WFI",
79 .desc = "ARM WFI",
80 }
81};
82
83static const struct of_device_id qcom_idle_state_match[] = {
84 { .compatible = "qcom,idle-state-spc", .data = spm_enter_idle_state },
Lina Iyer7ce75bb2015-04-09 13:20:41 -060085 { },
86};
87
AngeloGioacchino Del Regno60f36922021-07-29 17:56:05 +020088static int spm_cpuidle_register(struct device *cpuidle_dev, int cpu)
Lina Iyer7ce75bb2015-04-09 13:20:41 -060089{
AngeloGioacchino Del Regno60f36922021-07-29 17:56:05 +020090 struct platform_device *pdev = NULL;
91 struct device_node *cpu_node, *saw_node;
92 struct cpuidle_qcom_spm_data *data = NULL;
Stephan Gerholda871be62020-04-16 10:58:21 +020093 int ret;
Lina Iyer7ce75bb2015-04-09 13:20:41 -060094
AngeloGioacchino Del Regno60f36922021-07-29 17:56:05 +020095 cpu_node = of_cpu_device_node_get(cpu);
96 if (!cpu_node)
97 return -ENODEV;
Felix Fietkau61a3bd102018-07-23 16:17:35 +020098
AngeloGioacchino Del Regno60f36922021-07-29 17:56:05 +020099 saw_node = of_parse_phandle(cpu_node, "qcom,saw", 0);
100 if (!saw_node)
101 return -ENODEV;
102
103 pdev = of_find_device_by_node(saw_node);
104 of_node_put(saw_node);
105 of_node_put(cpu_node);
106 if (!pdev)
107 return -ENODEV;
108
109 data = devm_kzalloc(cpuidle_dev, sizeof(*data), GFP_KERNEL);
110 if (!data)
111 return -ENOMEM;
112
113 data->spm = dev_get_drvdata(&pdev->dev);
114 if (!data->spm)
115 return -EINVAL;
116
117 data->cpuidle_driver = qcom_spm_idle_driver;
118 data->cpuidle_driver.cpumask = (struct cpumask *)cpumask_of(cpu);
119
120 ret = dt_init_idle_driver(&data->cpuidle_driver,
121 qcom_idle_state_match, 1);
Stephan Gerholda871be62020-04-16 10:58:21 +0200122 if (ret <= 0)
123 return ret ? : -ENODEV;
Lina Iyer7ce75bb2015-04-09 13:20:41 -0600124
AngeloGioacchino Del Regno60f36922021-07-29 17:56:05 +0200125 ret = qcom_scm_set_warm_boot_addr(cpu_resume_arm, cpumask_of(cpu));
126 if (ret)
127 return ret;
128
129 return cpuidle_register(&data->cpuidle_driver, NULL);
Lina Iyer7ce75bb2015-04-09 13:20:41 -0600130}
131
AngeloGioacchino Del Regno60f36922021-07-29 17:56:05 +0200132static int spm_cpuidle_drv_probe(struct platform_device *pdev)
Lina Iyer7ce75bb2015-04-09 13:20:41 -0600133{
Stephan Gerholda871be62020-04-16 10:58:21 +0200134 int cpu, ret;
135
136 if (!qcom_scm_is_available())
137 return -EPROBE_DEFER;
Lina Iyer7ce75bb2015-04-09 13:20:41 -0600138
AngeloGioacchino Del Regno60f36922021-07-29 17:56:05 +0200139 for_each_possible_cpu(cpu) {
140 ret = spm_cpuidle_register(&pdev->dev, cpu);
141 if (ret && ret != -ENODEV) {
142 dev_err(&pdev->dev,
143 "Cannot register for CPU%d: %d\n", cpu, ret);
144 }
145 }
Lina Iyer7ce75bb2015-04-09 13:20:41 -0600146
Lina Iyer7ce75bb2015-04-09 13:20:41 -0600147 return 0;
148}
149
AngeloGioacchino Del Regno60f36922021-07-29 17:56:05 +0200150static struct platform_driver spm_cpuidle_driver = {
151 .probe = spm_cpuidle_drv_probe,
Lina Iyer7ce75bb2015-04-09 13:20:41 -0600152 .driver = {
AngeloGioacchino Del Regno60f36922021-07-29 17:56:05 +0200153 .name = "qcom-spm-cpuidle",
154 .suppress_bind_attrs = true,
Lina Iyer7ce75bb2015-04-09 13:20:41 -0600155 },
156};
Lina Iyer7ce75bb2015-04-09 13:20:41 -0600157
AngeloGioacchino Del Regno60f36922021-07-29 17:56:05 +0200158static int __init qcom_spm_cpuidle_init(void)
159{
160 struct platform_device *pdev;
161 int ret;
162
163 ret = platform_driver_register(&spm_cpuidle_driver);
164 if (ret)
165 return ret;
166
167 pdev = platform_device_register_simple("qcom-spm-cpuidle",
168 -1, NULL, 0);
169 if (IS_ERR(pdev)) {
170 platform_driver_unregister(&spm_cpuidle_driver);
171 return PTR_ERR(pdev);
172 }
173
174 return 0;
175}
176device_initcall(qcom_spm_cpuidle_init);