Thomas Gleixner | 97fb5e8 | 2019-05-29 07:17:58 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011-2014, The Linux Foundation. All rights reserved. |
| 4 | * Copyright (c) 2014,2015, Linaro Ltd. |
| 5 | * |
Lina Iyer | 3b904b0 | 2016-01-04 10:58:28 -0700 | [diff] [blame] | 6 | * SAW power controller driver |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 7 | */ |
| 8 | |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 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_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 Regno | 60f3692 | 2021-07-29 17:56:05 +0200 | [diff] [blame] | 21 | #include <soc/qcom/spm.h> |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 22 | |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 23 | #include <asm/proc-fns.h> |
| 24 | #include <asm/suspend.h> |
| 25 | |
Stephan Gerhold | a871be6 | 2020-04-16 10:58:21 +0200 | [diff] [blame] | 26 | #include "dt_idle_states.h" |
| 27 | |
AngeloGioacchino Del Regno | 60f3692 | 2021-07-29 17:56:05 +0200 | [diff] [blame] | 28 | struct cpuidle_qcom_spm_data { |
Stephan Gerhold | a871be6 | 2020-04-16 10:58:21 +0200 | [diff] [blame] | 29 | struct cpuidle_driver cpuidle_driver; |
AngeloGioacchino Del Regno | 60f3692 | 2021-07-29 17:56:05 +0200 | [diff] [blame] | 30 | struct spm_driver_data *spm; |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 31 | }; |
| 32 | |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 33 | static 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 Gerhold | a871be6 | 2020-04-16 10:58:21 +0200 | [diff] [blame] | 44 | static int qcom_cpu_spc(struct spm_driver_data *drv) |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 45 | { |
| 46 | int ret; |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 47 | |
| 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 Gerhold | a871be6 | 2020-04-16 10:58:21 +0200 | [diff] [blame] | 61 | static int spm_enter_idle_state(struct cpuidle_device *dev, |
| 62 | struct cpuidle_driver *drv, int idx) |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 63 | { |
AngeloGioacchino Del Regno | 60f3692 | 2021-07-29 17:56:05 +0200 | [diff] [blame] | 64 | struct cpuidle_qcom_spm_data *data = container_of(drv, struct cpuidle_qcom_spm_data, |
| 65 | cpuidle_driver); |
Stephan Gerhold | a871be6 | 2020-04-16 10:58:21 +0200 | [diff] [blame] | 66 | |
AngeloGioacchino Del Regno | 60f3692 | 2021-07-29 17:56:05 +0200 | [diff] [blame] | 67 | return CPU_PM_CPU_IDLE_ENTER_PARAM(qcom_cpu_spc, idx, data->spm); |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 68 | } |
| 69 | |
Stephan Gerhold | a871be6 | 2020-04-16 10:58:21 +0200 | [diff] [blame] | 70 | static 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 | |
| 83 | static const struct of_device_id qcom_idle_state_match[] = { |
| 84 | { .compatible = "qcom,idle-state-spc", .data = spm_enter_idle_state }, |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 85 | { }, |
| 86 | }; |
| 87 | |
AngeloGioacchino Del Regno | 60f3692 | 2021-07-29 17:56:05 +0200 | [diff] [blame] | 88 | static int spm_cpuidle_register(struct device *cpuidle_dev, int cpu) |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 89 | { |
AngeloGioacchino Del Regno | 60f3692 | 2021-07-29 17:56:05 +0200 | [diff] [blame] | 90 | struct platform_device *pdev = NULL; |
| 91 | struct device_node *cpu_node, *saw_node; |
| 92 | struct cpuidle_qcom_spm_data *data = NULL; |
Stephan Gerhold | a871be6 | 2020-04-16 10:58:21 +0200 | [diff] [blame] | 93 | int ret; |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 94 | |
AngeloGioacchino Del Regno | 60f3692 | 2021-07-29 17:56:05 +0200 | [diff] [blame] | 95 | cpu_node = of_cpu_device_node_get(cpu); |
| 96 | if (!cpu_node) |
| 97 | return -ENODEV; |
Felix Fietkau | 61a3bd10 | 2018-07-23 16:17:35 +0200 | [diff] [blame] | 98 | |
AngeloGioacchino Del Regno | 60f3692 | 2021-07-29 17:56:05 +0200 | [diff] [blame] | 99 | 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 Gerhold | a871be6 | 2020-04-16 10:58:21 +0200 | [diff] [blame] | 122 | if (ret <= 0) |
| 123 | return ret ? : -ENODEV; |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 124 | |
AngeloGioacchino Del Regno | 60f3692 | 2021-07-29 17:56:05 +0200 | [diff] [blame] | 125 | 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 Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 130 | } |
| 131 | |
AngeloGioacchino Del Regno | 60f3692 | 2021-07-29 17:56:05 +0200 | [diff] [blame] | 132 | static int spm_cpuidle_drv_probe(struct platform_device *pdev) |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 133 | { |
Stephan Gerhold | a871be6 | 2020-04-16 10:58:21 +0200 | [diff] [blame] | 134 | int cpu, ret; |
| 135 | |
| 136 | if (!qcom_scm_is_available()) |
| 137 | return -EPROBE_DEFER; |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 138 | |
AngeloGioacchino Del Regno | 60f3692 | 2021-07-29 17:56:05 +0200 | [diff] [blame] | 139 | 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 Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 146 | |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
AngeloGioacchino Del Regno | 60f3692 | 2021-07-29 17:56:05 +0200 | [diff] [blame] | 150 | static struct platform_driver spm_cpuidle_driver = { |
| 151 | .probe = spm_cpuidle_drv_probe, |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 152 | .driver = { |
AngeloGioacchino Del Regno | 60f3692 | 2021-07-29 17:56:05 +0200 | [diff] [blame] | 153 | .name = "qcom-spm-cpuidle", |
| 154 | .suppress_bind_attrs = true, |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 155 | }, |
| 156 | }; |
Lina Iyer | 7ce75bb | 2015-04-09 13:20:41 -0600 | [diff] [blame] | 157 | |
AngeloGioacchino Del Regno | 60f3692 | 2021-07-29 17:56:05 +0200 | [diff] [blame] | 158 | static 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 | } |
| 176 | device_initcall(qcom_spm_cpuidle_init); |