1a871be6bSStephan Gerhold // SPDX-License-Identifier: GPL-2.0-only
2a871be6bSStephan Gerhold /*
3a871be6bSStephan Gerhold * Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
4a871be6bSStephan Gerhold * Copyright (c) 2014,2015, Linaro Ltd.
5a871be6bSStephan Gerhold *
6a871be6bSStephan Gerhold * SAW power controller driver
7a871be6bSStephan Gerhold */
8a871be6bSStephan Gerhold
9a871be6bSStephan Gerhold #include <linux/kernel.h>
10a871be6bSStephan Gerhold #include <linux/init.h>
11a871be6bSStephan Gerhold #include <linux/io.h>
12a871be6bSStephan Gerhold #include <linux/slab.h>
13a871be6bSStephan Gerhold #include <linux/of.h>
14*24760e43SRob Herring #include <linux/of_platform.h>
15a871be6bSStephan Gerhold #include <linux/err.h>
16a871be6bSStephan Gerhold #include <linux/platform_device.h>
17a871be6bSStephan Gerhold #include <linux/cpuidle.h>
18a871be6bSStephan Gerhold #include <linux/cpu_pm.h>
193bf90ecaSElliot Berman #include <linux/firmware/qcom/qcom_scm.h>
2060f3692bSAngeloGioacchino Del Regno #include <soc/qcom/spm.h>
21a871be6bSStephan Gerhold
22a871be6bSStephan Gerhold #include <asm/proc-fns.h>
23a871be6bSStephan Gerhold #include <asm/suspend.h>
24a871be6bSStephan Gerhold
25a871be6bSStephan Gerhold #include "dt_idle_states.h"
26a871be6bSStephan Gerhold
2760f3692bSAngeloGioacchino Del Regno struct cpuidle_qcom_spm_data {
28a871be6bSStephan Gerhold struct cpuidle_driver cpuidle_driver;
2960f3692bSAngeloGioacchino Del Regno struct spm_driver_data *spm;
30a871be6bSStephan Gerhold };
31a871be6bSStephan Gerhold
qcom_pm_collapse(unsigned long int unused)32a871be6bSStephan Gerhold static int qcom_pm_collapse(unsigned long int unused)
33a871be6bSStephan Gerhold {
34a871be6bSStephan Gerhold qcom_scm_cpu_power_down(QCOM_SCM_CPU_PWR_DOWN_L2_ON);
35a871be6bSStephan Gerhold
36a871be6bSStephan Gerhold /*
37a871be6bSStephan Gerhold * Returns here only if there was a pending interrupt and we did not
38a871be6bSStephan Gerhold * power down as a result.
39a871be6bSStephan Gerhold */
40a871be6bSStephan Gerhold return -1;
41a871be6bSStephan Gerhold }
42a871be6bSStephan Gerhold
qcom_cpu_spc(struct spm_driver_data * drv)43a871be6bSStephan Gerhold static int qcom_cpu_spc(struct spm_driver_data *drv)
44a871be6bSStephan Gerhold {
45a871be6bSStephan Gerhold int ret;
46a871be6bSStephan Gerhold
47a871be6bSStephan Gerhold spm_set_low_power_mode(drv, PM_SLEEP_MODE_SPC);
48a871be6bSStephan Gerhold ret = cpu_suspend(0, qcom_pm_collapse);
49a871be6bSStephan Gerhold /*
50a871be6bSStephan Gerhold * ARM common code executes WFI without calling into our driver and
51a871be6bSStephan Gerhold * if the SPM mode is not reset, then we may accidently power down the
52a871be6bSStephan Gerhold * cpu when we intended only to gate the cpu clock.
53a871be6bSStephan Gerhold * Ensure the state is set to standby before returning.
54a871be6bSStephan Gerhold */
55a871be6bSStephan Gerhold spm_set_low_power_mode(drv, PM_SLEEP_MODE_STBY);
56a871be6bSStephan Gerhold
57a871be6bSStephan Gerhold return ret;
58a871be6bSStephan Gerhold }
59a871be6bSStephan Gerhold
spm_enter_idle_state(struct cpuidle_device * dev,struct cpuidle_driver * drv,int idx)6069e26b4fSPeter Zijlstra static __cpuidle int spm_enter_idle_state(struct cpuidle_device *dev,
61a871be6bSStephan Gerhold struct cpuidle_driver *drv, int idx)
62a871be6bSStephan Gerhold {
6360f3692bSAngeloGioacchino Del Regno struct cpuidle_qcom_spm_data *data = container_of(drv, struct cpuidle_qcom_spm_data,
64a871be6bSStephan Gerhold cpuidle_driver);
65a871be6bSStephan Gerhold
6660f3692bSAngeloGioacchino Del Regno return CPU_PM_CPU_IDLE_ENTER_PARAM(qcom_cpu_spc, idx, data->spm);
67a871be6bSStephan Gerhold }
68a871be6bSStephan Gerhold
69a871be6bSStephan Gerhold static struct cpuidle_driver qcom_spm_idle_driver = {
70a871be6bSStephan Gerhold .name = "qcom_spm",
71a871be6bSStephan Gerhold .owner = THIS_MODULE,
72a871be6bSStephan Gerhold .states[0] = {
73a871be6bSStephan Gerhold .enter = spm_enter_idle_state,
74a871be6bSStephan Gerhold .exit_latency = 1,
75a871be6bSStephan Gerhold .target_residency = 1,
76a871be6bSStephan Gerhold .power_usage = UINT_MAX,
77a871be6bSStephan Gerhold .name = "WFI",
78a871be6bSStephan Gerhold .desc = "ARM WFI",
79a871be6bSStephan Gerhold }
80a871be6bSStephan Gerhold };
81a871be6bSStephan Gerhold
82a871be6bSStephan Gerhold static const struct of_device_id qcom_idle_state_match[] = {
83a871be6bSStephan Gerhold { .compatible = "qcom,idle-state-spc", .data = spm_enter_idle_state },
84a871be6bSStephan Gerhold { },
85a871be6bSStephan Gerhold };
86a871be6bSStephan Gerhold
spm_cpuidle_register(struct device * cpuidle_dev,int cpu)8760f3692bSAngeloGioacchino Del Regno static int spm_cpuidle_register(struct device *cpuidle_dev, int cpu)
88a871be6bSStephan Gerhold {
8960f3692bSAngeloGioacchino Del Regno struct platform_device *pdev = NULL;
9060f3692bSAngeloGioacchino Del Regno struct device_node *cpu_node, *saw_node;
9160f3692bSAngeloGioacchino Del Regno struct cpuidle_qcom_spm_data *data = NULL;
92a871be6bSStephan Gerhold int ret;
93a871be6bSStephan Gerhold
9460f3692bSAngeloGioacchino Del Regno cpu_node = of_cpu_device_node_get(cpu);
9560f3692bSAngeloGioacchino Del Regno if (!cpu_node)
9660f3692bSAngeloGioacchino Del Regno return -ENODEV;
97a871be6bSStephan Gerhold
9860f3692bSAngeloGioacchino Del Regno saw_node = of_parse_phandle(cpu_node, "qcom,saw", 0);
9960f3692bSAngeloGioacchino Del Regno if (!saw_node)
10060f3692bSAngeloGioacchino Del Regno return -ENODEV;
10160f3692bSAngeloGioacchino Del Regno
10260f3692bSAngeloGioacchino Del Regno pdev = of_find_device_by_node(saw_node);
10360f3692bSAngeloGioacchino Del Regno of_node_put(saw_node);
10460f3692bSAngeloGioacchino Del Regno of_node_put(cpu_node);
10560f3692bSAngeloGioacchino Del Regno if (!pdev)
10660f3692bSAngeloGioacchino Del Regno return -ENODEV;
10760f3692bSAngeloGioacchino Del Regno
10860f3692bSAngeloGioacchino Del Regno data = devm_kzalloc(cpuidle_dev, sizeof(*data), GFP_KERNEL);
10960f3692bSAngeloGioacchino Del Regno if (!data)
11060f3692bSAngeloGioacchino Del Regno return -ENOMEM;
11160f3692bSAngeloGioacchino Del Regno
11260f3692bSAngeloGioacchino Del Regno data->spm = dev_get_drvdata(&pdev->dev);
11360f3692bSAngeloGioacchino Del Regno if (!data->spm)
11460f3692bSAngeloGioacchino Del Regno return -EINVAL;
11560f3692bSAngeloGioacchino Del Regno
11660f3692bSAngeloGioacchino Del Regno data->cpuidle_driver = qcom_spm_idle_driver;
11760f3692bSAngeloGioacchino Del Regno data->cpuidle_driver.cpumask = (struct cpumask *)cpumask_of(cpu);
11860f3692bSAngeloGioacchino Del Regno
11960f3692bSAngeloGioacchino Del Regno ret = dt_init_idle_driver(&data->cpuidle_driver,
12060f3692bSAngeloGioacchino Del Regno qcom_idle_state_match, 1);
121a871be6bSStephan Gerhold if (ret <= 0)
122a871be6bSStephan Gerhold return ret ? : -ENODEV;
123a871be6bSStephan Gerhold
12460f3692bSAngeloGioacchino Del Regno return cpuidle_register(&data->cpuidle_driver, NULL);
125a871be6bSStephan Gerhold }
126a871be6bSStephan Gerhold
spm_cpuidle_drv_probe(struct platform_device * pdev)12760f3692bSAngeloGioacchino Del Regno static int spm_cpuidle_drv_probe(struct platform_device *pdev)
128a871be6bSStephan Gerhold {
129a871be6bSStephan Gerhold int cpu, ret;
130a871be6bSStephan Gerhold
131a871be6bSStephan Gerhold if (!qcom_scm_is_available())
132a871be6bSStephan Gerhold return -EPROBE_DEFER;
133a871be6bSStephan Gerhold
13452beb1fcSStephan Gerhold ret = qcom_scm_set_warm_boot_addr(cpu_resume_arm);
13552beb1fcSStephan Gerhold if (ret)
13652beb1fcSStephan Gerhold return dev_err_probe(&pdev->dev, ret, "set warm boot addr failed");
13752beb1fcSStephan Gerhold
13860f3692bSAngeloGioacchino Del Regno for_each_possible_cpu(cpu) {
13960f3692bSAngeloGioacchino Del Regno ret = spm_cpuidle_register(&pdev->dev, cpu);
14060f3692bSAngeloGioacchino Del Regno if (ret && ret != -ENODEV) {
14160f3692bSAngeloGioacchino Del Regno dev_err(&pdev->dev,
14260f3692bSAngeloGioacchino Del Regno "Cannot register for CPU%d: %d\n", cpu, ret);
14360f3692bSAngeloGioacchino Del Regno }
144a871be6bSStephan Gerhold }
145a871be6bSStephan Gerhold
146a871be6bSStephan Gerhold return 0;
147a871be6bSStephan Gerhold }
148a871be6bSStephan Gerhold
14960f3692bSAngeloGioacchino Del Regno static struct platform_driver spm_cpuidle_driver = {
15060f3692bSAngeloGioacchino Del Regno .probe = spm_cpuidle_drv_probe,
151a871be6bSStephan Gerhold .driver = {
15260f3692bSAngeloGioacchino Del Regno .name = "qcom-spm-cpuidle",
15360f3692bSAngeloGioacchino Del Regno .suppress_bind_attrs = true,
154a871be6bSStephan Gerhold },
155a871be6bSStephan Gerhold };
156a871be6bSStephan Gerhold
qcom_spm_find_any_cpu(void)1570ee30aceSStephan Gerhold static bool __init qcom_spm_find_any_cpu(void)
1580ee30aceSStephan Gerhold {
1590ee30aceSStephan Gerhold struct device_node *cpu_node, *saw_node;
1600ee30aceSStephan Gerhold
1610ee30aceSStephan Gerhold for_each_of_cpu_node(cpu_node) {
1620ee30aceSStephan Gerhold saw_node = of_parse_phandle(cpu_node, "qcom,saw", 0);
1630ee30aceSStephan Gerhold if (of_device_is_available(saw_node)) {
1640ee30aceSStephan Gerhold of_node_put(saw_node);
1650ee30aceSStephan Gerhold of_node_put(cpu_node);
1660ee30aceSStephan Gerhold return true;
1670ee30aceSStephan Gerhold }
1680ee30aceSStephan Gerhold of_node_put(saw_node);
1690ee30aceSStephan Gerhold }
1700ee30aceSStephan Gerhold return false;
1710ee30aceSStephan Gerhold }
1720ee30aceSStephan Gerhold
qcom_spm_cpuidle_init(void)17360f3692bSAngeloGioacchino Del Regno static int __init qcom_spm_cpuidle_init(void)
17460f3692bSAngeloGioacchino Del Regno {
17560f3692bSAngeloGioacchino Del Regno struct platform_device *pdev;
17660f3692bSAngeloGioacchino Del Regno int ret;
17760f3692bSAngeloGioacchino Del Regno
17860f3692bSAngeloGioacchino Del Regno ret = platform_driver_register(&spm_cpuidle_driver);
17960f3692bSAngeloGioacchino Del Regno if (ret)
18060f3692bSAngeloGioacchino Del Regno return ret;
18160f3692bSAngeloGioacchino Del Regno
1820ee30aceSStephan Gerhold /* Make sure there is actually any CPU managed by the SPM */
1830ee30aceSStephan Gerhold if (!qcom_spm_find_any_cpu())
1840ee30aceSStephan Gerhold return 0;
1850ee30aceSStephan Gerhold
18660f3692bSAngeloGioacchino Del Regno pdev = platform_device_register_simple("qcom-spm-cpuidle",
18760f3692bSAngeloGioacchino Del Regno -1, NULL, 0);
18860f3692bSAngeloGioacchino Del Regno if (IS_ERR(pdev)) {
18960f3692bSAngeloGioacchino Del Regno platform_driver_unregister(&spm_cpuidle_driver);
19060f3692bSAngeloGioacchino Del Regno return PTR_ERR(pdev);
19160f3692bSAngeloGioacchino Del Regno }
19260f3692bSAngeloGioacchino Del Regno
19360f3692bSAngeloGioacchino Del Regno return 0;
19460f3692bSAngeloGioacchino Del Regno }
19560f3692bSAngeloGioacchino Del Regno device_initcall(qcom_spm_cpuidle_init);
196