Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
0004  * Copyright (c) 2014,2015, Linaro Ltd.
0005  *
0006  * SAW power controller driver
0007  */
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/init.h>
0011 #include <linux/io.h>
0012 #include <linux/slab.h>
0013 #include <linux/of.h>
0014 #include <linux/of_address.h>
0015 #include <linux/of_device.h>
0016 #include <linux/err.h>
0017 #include <linux/platform_device.h>
0018 #include <linux/cpuidle.h>
0019 #include <linux/cpu_pm.h>
0020 #include <linux/qcom_scm.h>
0021 #include <soc/qcom/spm.h>
0022 
0023 #include <asm/proc-fns.h>
0024 #include <asm/suspend.h>
0025 
0026 #include "dt_idle_states.h"
0027 
0028 struct cpuidle_qcom_spm_data {
0029     struct cpuidle_driver cpuidle_driver;
0030     struct spm_driver_data *spm;
0031 };
0032 
0033 static int qcom_pm_collapse(unsigned long int unused)
0034 {
0035     qcom_scm_cpu_power_down(QCOM_SCM_CPU_PWR_DOWN_L2_ON);
0036 
0037     /*
0038      * Returns here only if there was a pending interrupt and we did not
0039      * power down as a result.
0040      */
0041     return -1;
0042 }
0043 
0044 static int qcom_cpu_spc(struct spm_driver_data *drv)
0045 {
0046     int ret;
0047 
0048     spm_set_low_power_mode(drv, PM_SLEEP_MODE_SPC);
0049     ret = cpu_suspend(0, qcom_pm_collapse);
0050     /*
0051      * ARM common code executes WFI without calling into our driver and
0052      * if the SPM mode is not reset, then we may accidently power down the
0053      * cpu when we intended only to gate the cpu clock.
0054      * Ensure the state is set to standby before returning.
0055      */
0056     spm_set_low_power_mode(drv, PM_SLEEP_MODE_STBY);
0057 
0058     return ret;
0059 }
0060 
0061 static int spm_enter_idle_state(struct cpuidle_device *dev,
0062                 struct cpuidle_driver *drv, int idx)
0063 {
0064     struct cpuidle_qcom_spm_data *data = container_of(drv, struct cpuidle_qcom_spm_data,
0065                               cpuidle_driver);
0066 
0067     return CPU_PM_CPU_IDLE_ENTER_PARAM(qcom_cpu_spc, idx, data->spm);
0068 }
0069 
0070 static struct cpuidle_driver qcom_spm_idle_driver = {
0071     .name = "qcom_spm",
0072     .owner = THIS_MODULE,
0073     .states[0] = {
0074         .enter          = spm_enter_idle_state,
0075         .exit_latency       = 1,
0076         .target_residency   = 1,
0077         .power_usage        = UINT_MAX,
0078         .name           = "WFI",
0079         .desc           = "ARM WFI",
0080     }
0081 };
0082 
0083 static const struct of_device_id qcom_idle_state_match[] = {
0084     { .compatible = "qcom,idle-state-spc", .data = spm_enter_idle_state },
0085     { },
0086 };
0087 
0088 static int spm_cpuidle_register(struct device *cpuidle_dev, int cpu)
0089 {
0090     struct platform_device *pdev = NULL;
0091     struct device_node *cpu_node, *saw_node;
0092     struct cpuidle_qcom_spm_data *data = NULL;
0093     int ret;
0094 
0095     cpu_node = of_cpu_device_node_get(cpu);
0096     if (!cpu_node)
0097         return -ENODEV;
0098 
0099     saw_node = of_parse_phandle(cpu_node, "qcom,saw", 0);
0100     if (!saw_node)
0101         return -ENODEV;
0102 
0103     pdev = of_find_device_by_node(saw_node);
0104     of_node_put(saw_node);
0105     of_node_put(cpu_node);
0106     if (!pdev)
0107         return -ENODEV;
0108 
0109     data = devm_kzalloc(cpuidle_dev, sizeof(*data), GFP_KERNEL);
0110     if (!data)
0111         return -ENOMEM;
0112 
0113     data->spm = dev_get_drvdata(&pdev->dev);
0114     if (!data->spm)
0115         return -EINVAL;
0116 
0117     data->cpuidle_driver = qcom_spm_idle_driver;
0118     data->cpuidle_driver.cpumask = (struct cpumask *)cpumask_of(cpu);
0119 
0120     ret = dt_init_idle_driver(&data->cpuidle_driver,
0121                   qcom_idle_state_match, 1);
0122     if (ret <= 0)
0123         return ret ? : -ENODEV;
0124 
0125     return cpuidle_register(&data->cpuidle_driver, NULL);
0126 }
0127 
0128 static int spm_cpuidle_drv_probe(struct platform_device *pdev)
0129 {
0130     int cpu, ret;
0131 
0132     if (!qcom_scm_is_available())
0133         return -EPROBE_DEFER;
0134 
0135     ret = qcom_scm_set_warm_boot_addr(cpu_resume_arm);
0136     if (ret)
0137         return dev_err_probe(&pdev->dev, ret, "set warm boot addr failed");
0138 
0139     for_each_possible_cpu(cpu) {
0140         ret = spm_cpuidle_register(&pdev->dev, cpu);
0141         if (ret && ret != -ENODEV) {
0142             dev_err(&pdev->dev,
0143                 "Cannot register for CPU%d: %d\n", cpu, ret);
0144         }
0145     }
0146 
0147     return 0;
0148 }
0149 
0150 static struct platform_driver spm_cpuidle_driver = {
0151     .probe = spm_cpuidle_drv_probe,
0152     .driver = {
0153         .name = "qcom-spm-cpuidle",
0154         .suppress_bind_attrs = true,
0155     },
0156 };
0157 
0158 static bool __init qcom_spm_find_any_cpu(void)
0159 {
0160     struct device_node *cpu_node, *saw_node;
0161 
0162     for_each_of_cpu_node(cpu_node) {
0163         saw_node = of_parse_phandle(cpu_node, "qcom,saw", 0);
0164         if (of_device_is_available(saw_node)) {
0165             of_node_put(saw_node);
0166             of_node_put(cpu_node);
0167             return true;
0168         }
0169         of_node_put(saw_node);
0170     }
0171     return false;
0172 }
0173 
0174 static int __init qcom_spm_cpuidle_init(void)
0175 {
0176     struct platform_device *pdev;
0177     int ret;
0178 
0179     ret = platform_driver_register(&spm_cpuidle_driver);
0180     if (ret)
0181         return ret;
0182 
0183     /* Make sure there is actually any CPU managed by the SPM */
0184     if (!qcom_spm_find_any_cpu())
0185         return 0;
0186 
0187     pdev = platform_device_register_simple("qcom-spm-cpuidle",
0188                            -1, NULL, 0);
0189     if (IS_ERR(pdev)) {
0190         platform_driver_unregister(&spm_cpuidle_driver);
0191         return PTR_ERR(pdev);
0192     }
0193 
0194     return 0;
0195 }
0196 device_initcall(qcom_spm_cpuidle_init);