2 * SCPI Generic power domain support.
4 * Copyright (C) 2016 ARM Ltd.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/err.h>
21 #include <linux/module.h>
22 #include <linux/of_platform.h>
23 #include <linux/pm_domain.h>
24 #include <linux/scpi_protocol.h>
26 struct scpi_pm_domain {
27 struct generic_pm_domain genpd;
34 * These device power state values are not well-defined in the specification.
35 * In case, different implementations use different values, we can make these
36 * specific to compatibles rather than getting these values from device tree.
38 enum scpi_power_domain_state {
40 SCPI_PD_STATE_OFF = 3,
43 #define to_scpi_pd(gpd) container_of(gpd, struct scpi_pm_domain, genpd)
45 static int scpi_pd_power(struct scpi_pm_domain *pd, bool power_on)
48 enum scpi_power_domain_state state;
51 state = SCPI_PD_STATE_ON;
53 state = SCPI_PD_STATE_OFF;
55 ret = pd->ops->device_set_power_state(pd->domain, state);
59 return !(state == pd->ops->device_get_power_state(pd->domain));
62 static int scpi_pd_power_on(struct generic_pm_domain *domain)
64 struct scpi_pm_domain *pd = to_scpi_pd(domain);
66 return scpi_pd_power(pd, true);
69 static int scpi_pd_power_off(struct generic_pm_domain *domain)
71 struct scpi_pm_domain *pd = to_scpi_pd(domain);
73 return scpi_pd_power(pd, false);
76 static int scpi_pm_domain_probe(struct platform_device *pdev)
78 struct device *dev = &pdev->dev;
79 struct device_node *np = dev->of_node;
80 struct scpi_pm_domain *scpi_pd;
81 struct genpd_onecell_data *scpi_pd_data;
82 struct generic_pm_domain **domains;
83 struct scpi_ops *scpi_ops;
84 int ret, num_domains, i;
86 scpi_ops = get_scpi_ops();
91 dev_err(dev, "device tree node not found\n");
95 if (!scpi_ops->device_set_power_state ||
96 !scpi_ops->device_get_power_state) {
97 dev_err(dev, "power domains not supported in the firmware\n");
101 ret = of_property_read_u32(np, "num-domains", &num_domains);
103 dev_err(dev, "number of domains not found\n");
107 scpi_pd = devm_kcalloc(dev, num_domains, sizeof(*scpi_pd), GFP_KERNEL);
111 scpi_pd_data = devm_kzalloc(dev, sizeof(*scpi_pd_data), GFP_KERNEL);
115 domains = devm_kcalloc(dev, num_domains, sizeof(*domains), GFP_KERNEL);
119 for (i = 0; i < num_domains; i++, scpi_pd++) {
120 domains[i] = &scpi_pd->genpd;
123 scpi_pd->ops = scpi_ops;
124 sprintf(scpi_pd->name, "%s.%d", np->name, i);
125 scpi_pd->genpd.name = scpi_pd->name;
126 scpi_pd->genpd.power_off = scpi_pd_power_off;
127 scpi_pd->genpd.power_on = scpi_pd_power_on;
130 * Treat all power domains as off at boot.
132 * The SCP firmware itself may have switched on some domains,
133 * but for reference counting purpose, keep it this way.
135 pm_genpd_init(&scpi_pd->genpd, NULL, true);
138 scpi_pd_data->domains = domains;
139 scpi_pd_data->num_domains = num_domains;
141 of_genpd_add_provider_onecell(np, scpi_pd_data);
146 static const struct of_device_id scpi_power_domain_ids[] = {
147 { .compatible = "arm,scpi-power-domains", },
150 MODULE_DEVICE_TABLE(of, scpi_power_domain_ids);
152 static struct platform_driver scpi_power_domain_driver = {
154 .name = "scpi_power_domain",
155 .of_match_table = scpi_power_domain_ids,
157 .probe = scpi_pm_domain_probe,
159 module_platform_driver(scpi_power_domain_driver);
162 MODULE_DESCRIPTION("ARM SCPI power domain driver");
163 MODULE_LICENSE("GPL v2");