Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * SCPI Generic power domain support.
0004  *
0005  * Copyright (C) 2016 ARM Ltd.
0006  */
0007 
0008 #include <linux/err.h>
0009 #include <linux/io.h>
0010 #include <linux/module.h>
0011 #include <linux/of_platform.h>
0012 #include <linux/pm_domain.h>
0013 #include <linux/scpi_protocol.h>
0014 
0015 struct scpi_pm_domain {
0016     struct generic_pm_domain genpd;
0017     struct scpi_ops *ops;
0018     u32 domain;
0019 };
0020 
0021 /*
0022  * These device power state values are not well-defined in the specification.
0023  * In case, different implementations use different values, we can make these
0024  * specific to compatibles rather than getting these values from device tree.
0025  */
0026 enum scpi_power_domain_state {
0027     SCPI_PD_STATE_ON = 0,
0028     SCPI_PD_STATE_OFF = 3,
0029 };
0030 
0031 #define to_scpi_pd(gpd) container_of(gpd, struct scpi_pm_domain, genpd)
0032 
0033 static int scpi_pd_power(struct scpi_pm_domain *pd, bool power_on)
0034 {
0035     int ret;
0036     enum scpi_power_domain_state state;
0037 
0038     if (power_on)
0039         state = SCPI_PD_STATE_ON;
0040     else
0041         state = SCPI_PD_STATE_OFF;
0042 
0043     ret = pd->ops->device_set_power_state(pd->domain, state);
0044     if (ret)
0045         return ret;
0046 
0047     return !(state == pd->ops->device_get_power_state(pd->domain));
0048 }
0049 
0050 static int scpi_pd_power_on(struct generic_pm_domain *domain)
0051 {
0052     struct scpi_pm_domain *pd = to_scpi_pd(domain);
0053 
0054     return scpi_pd_power(pd, true);
0055 }
0056 
0057 static int scpi_pd_power_off(struct generic_pm_domain *domain)
0058 {
0059     struct scpi_pm_domain *pd = to_scpi_pd(domain);
0060 
0061     return scpi_pd_power(pd, false);
0062 }
0063 
0064 static int scpi_pm_domain_probe(struct platform_device *pdev)
0065 {
0066     struct device *dev = &pdev->dev;
0067     struct device_node *np = dev->of_node;
0068     struct scpi_pm_domain *scpi_pd;
0069     struct genpd_onecell_data *scpi_pd_data;
0070     struct generic_pm_domain **domains;
0071     struct scpi_ops *scpi_ops;
0072     int ret, num_domains, i;
0073 
0074     scpi_ops = get_scpi_ops();
0075     if (!scpi_ops)
0076         return -EPROBE_DEFER;
0077 
0078     if (!np) {
0079         dev_err(dev, "device tree node not found\n");
0080         return -ENODEV;
0081     }
0082 
0083     if (!scpi_ops->device_set_power_state ||
0084         !scpi_ops->device_get_power_state) {
0085         dev_err(dev, "power domains not supported in the firmware\n");
0086         return -ENODEV;
0087     }
0088 
0089     ret = of_property_read_u32(np, "num-domains", &num_domains);
0090     if (ret) {
0091         dev_err(dev, "number of domains not found\n");
0092         return -EINVAL;
0093     }
0094 
0095     scpi_pd = devm_kcalloc(dev, num_domains, sizeof(*scpi_pd), GFP_KERNEL);
0096     if (!scpi_pd)
0097         return -ENOMEM;
0098 
0099     scpi_pd_data = devm_kzalloc(dev, sizeof(*scpi_pd_data), GFP_KERNEL);
0100     if (!scpi_pd_data)
0101         return -ENOMEM;
0102 
0103     domains = devm_kcalloc(dev, num_domains, sizeof(*domains), GFP_KERNEL);
0104     if (!domains)
0105         return -ENOMEM;
0106 
0107     for (i = 0; i < num_domains; i++, scpi_pd++) {
0108         domains[i] = &scpi_pd->genpd;
0109 
0110         scpi_pd->domain = i;
0111         scpi_pd->ops = scpi_ops;
0112         scpi_pd->genpd.name = devm_kasprintf(dev, GFP_KERNEL,
0113                              "%pOFn.%d", np, i);
0114         if (!scpi_pd->genpd.name) {
0115             dev_err(dev, "Failed to allocate genpd name:%pOFn.%d\n",
0116                 np, i);
0117             continue;
0118         }
0119         scpi_pd->genpd.power_off = scpi_pd_power_off;
0120         scpi_pd->genpd.power_on = scpi_pd_power_on;
0121 
0122         /*
0123          * Treat all power domains as off at boot.
0124          *
0125          * The SCP firmware itself may have switched on some domains,
0126          * but for reference counting purpose, keep it this way.
0127          */
0128         pm_genpd_init(&scpi_pd->genpd, NULL, true);
0129     }
0130 
0131     scpi_pd_data->domains = domains;
0132     scpi_pd_data->num_domains = num_domains;
0133 
0134     of_genpd_add_provider_onecell(np, scpi_pd_data);
0135 
0136     return 0;
0137 }
0138 
0139 static const struct of_device_id scpi_power_domain_ids[] = {
0140     { .compatible = "arm,scpi-power-domains", },
0141     { /* sentinel */ }
0142 };
0143 MODULE_DEVICE_TABLE(of, scpi_power_domain_ids);
0144 
0145 static struct platform_driver scpi_power_domain_driver = {
0146     .driver = {
0147         .name = "scpi_power_domain",
0148         .of_match_table = scpi_power_domain_ids,
0149     },
0150     .probe = scpi_pm_domain_probe,
0151 };
0152 module_platform_driver(scpi_power_domain_driver);
0153 
0154 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
0155 MODULE_DESCRIPTION("ARM SCPI power domain driver");
0156 MODULE_LICENSE("GPL v2");