GNU Linux-libre 4.14.265-gnu1
[releases.git] / drivers / firmware / scpi_pm_domain.c
1 /*
2  * SCPI Generic power domain support.
3  *
4  * Copyright (C) 2016 ARM Ltd.
5  *
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.
9  *
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
13  * more details.
14  *
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/>.
17  */
18
19 #include <linux/err.h>
20 #include <linux/io.h>
21 #include <linux/module.h>
22 #include <linux/of_platform.h>
23 #include <linux/pm_domain.h>
24 #include <linux/scpi_protocol.h>
25
26 struct scpi_pm_domain {
27         struct generic_pm_domain genpd;
28         struct scpi_ops *ops;
29         u32 domain;
30 };
31
32 /*
33  * These device power state values are not well-defined in the specification.
34  * In case, different implementations use different values, we can make these
35  * specific to compatibles rather than getting these values from device tree.
36  */
37 enum scpi_power_domain_state {
38         SCPI_PD_STATE_ON = 0,
39         SCPI_PD_STATE_OFF = 3,
40 };
41
42 #define to_scpi_pd(gpd) container_of(gpd, struct scpi_pm_domain, genpd)
43
44 static int scpi_pd_power(struct scpi_pm_domain *pd, bool power_on)
45 {
46         int ret;
47         enum scpi_power_domain_state state;
48
49         if (power_on)
50                 state = SCPI_PD_STATE_ON;
51         else
52                 state = SCPI_PD_STATE_OFF;
53
54         ret = pd->ops->device_set_power_state(pd->domain, state);
55         if (ret)
56                 return ret;
57
58         return !(state == pd->ops->device_get_power_state(pd->domain));
59 }
60
61 static int scpi_pd_power_on(struct generic_pm_domain *domain)
62 {
63         struct scpi_pm_domain *pd = to_scpi_pd(domain);
64
65         return scpi_pd_power(pd, true);
66 }
67
68 static int scpi_pd_power_off(struct generic_pm_domain *domain)
69 {
70         struct scpi_pm_domain *pd = to_scpi_pd(domain);
71
72         return scpi_pd_power(pd, false);
73 }
74
75 static int scpi_pm_domain_probe(struct platform_device *pdev)
76 {
77         struct device *dev = &pdev->dev;
78         struct device_node *np = dev->of_node;
79         struct scpi_pm_domain *scpi_pd;
80         struct genpd_onecell_data *scpi_pd_data;
81         struct generic_pm_domain **domains;
82         struct scpi_ops *scpi_ops;
83         int ret, num_domains, i;
84
85         scpi_ops = get_scpi_ops();
86         if (!scpi_ops)
87                 return -EPROBE_DEFER;
88
89         if (!np) {
90                 dev_err(dev, "device tree node not found\n");
91                 return -ENODEV;
92         }
93
94         if (!scpi_ops->device_set_power_state ||
95             !scpi_ops->device_get_power_state) {
96                 dev_err(dev, "power domains not supported in the firmware\n");
97                 return -ENODEV;
98         }
99
100         ret = of_property_read_u32(np, "num-domains", &num_domains);
101         if (ret) {
102                 dev_err(dev, "number of domains not found\n");
103                 return -EINVAL;
104         }
105
106         scpi_pd = devm_kcalloc(dev, num_domains, sizeof(*scpi_pd), GFP_KERNEL);
107         if (!scpi_pd)
108                 return -ENOMEM;
109
110         scpi_pd_data = devm_kzalloc(dev, sizeof(*scpi_pd_data), GFP_KERNEL);
111         if (!scpi_pd_data)
112                 return -ENOMEM;
113
114         domains = devm_kcalloc(dev, num_domains, sizeof(*domains), GFP_KERNEL);
115         if (!domains)
116                 return -ENOMEM;
117
118         for (i = 0; i < num_domains; i++, scpi_pd++) {
119                 domains[i] = &scpi_pd->genpd;
120
121                 scpi_pd->domain = i;
122                 scpi_pd->ops = scpi_ops;
123                 scpi_pd->genpd.name = devm_kasprintf(dev, GFP_KERNEL,
124                                                      "%s.%d", np->name, i);
125                 if (!scpi_pd->genpd.name) {
126                         dev_err(dev, "Failed to allocate genpd name:%s.%d\n",
127                                 np->name, i);
128                         continue;
129                 }
130                 scpi_pd->genpd.power_off = scpi_pd_power_off;
131                 scpi_pd->genpd.power_on = scpi_pd_power_on;
132
133                 /*
134                  * Treat all power domains as off at boot.
135                  *
136                  * The SCP firmware itself may have switched on some domains,
137                  * but for reference counting purpose, keep it this way.
138                  */
139                 pm_genpd_init(&scpi_pd->genpd, NULL, true);
140         }
141
142         scpi_pd_data->domains = domains;
143         scpi_pd_data->num_domains = num_domains;
144
145         of_genpd_add_provider_onecell(np, scpi_pd_data);
146
147         return 0;
148 }
149
150 static const struct of_device_id scpi_power_domain_ids[] = {
151         { .compatible = "arm,scpi-power-domains", },
152         { /* sentinel */ }
153 };
154 MODULE_DEVICE_TABLE(of, scpi_power_domain_ids);
155
156 static struct platform_driver scpi_power_domain_driver = {
157         .driver = {
158                 .name = "scpi_power_domain",
159                 .of_match_table = scpi_power_domain_ids,
160         },
161         .probe = scpi_pm_domain_probe,
162 };
163 module_platform_driver(scpi_power_domain_driver);
164
165 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
166 MODULE_DESCRIPTION("ARM SCPI power domain driver");
167 MODULE_LICENSE("GPL v2");