1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
26struct scpi_pm_domain {
27 struct generic_pm_domain genpd;
28 struct scpi_ops *ops;
29 u32 domain;
30 char name[30];
31};
32
33
34
35
36
37
38enum scpi_power_domain_state {
39 SCPI_PD_STATE_ON = 0,
40 SCPI_PD_STATE_OFF = 3,
41};
42
43#define to_scpi_pd(gpd) container_of(gpd, struct scpi_pm_domain, genpd)
44
45static int scpi_pd_power(struct scpi_pm_domain *pd, bool power_on)
46{
47 int ret;
48 enum scpi_power_domain_state state;
49
50 if (power_on)
51 state = SCPI_PD_STATE_ON;
52 else
53 state = SCPI_PD_STATE_OFF;
54
55 ret = pd->ops->device_set_power_state(pd->domain, state);
56 if (ret)
57 return ret;
58
59 return !(state == pd->ops->device_get_power_state(pd->domain));
60}
61
62static int scpi_pd_power_on(struct generic_pm_domain *domain)
63{
64 struct scpi_pm_domain *pd = to_scpi_pd(domain);
65
66 return scpi_pd_power(pd, true);
67}
68
69static int scpi_pd_power_off(struct generic_pm_domain *domain)
70{
71 struct scpi_pm_domain *pd = to_scpi_pd(domain);
72
73 return scpi_pd_power(pd, false);
74}
75
76static int scpi_pm_domain_probe(struct platform_device *pdev)
77{
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;
85
86 scpi_ops = get_scpi_ops();
87 if (!scpi_ops)
88 return -EPROBE_DEFER;
89
90 if (!np) {
91 dev_err(dev, "device tree node not found\n");
92 return -ENODEV;
93 }
94
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");
98 return -ENODEV;
99 }
100
101 ret = of_property_read_u32(np, "num-domains", &num_domains);
102 if (ret) {
103 dev_err(dev, "number of domains not found\n");
104 return -EINVAL;
105 }
106
107 scpi_pd = devm_kcalloc(dev, num_domains, sizeof(*scpi_pd), GFP_KERNEL);
108 if (!scpi_pd)
109 return -ENOMEM;
110
111 scpi_pd_data = devm_kzalloc(dev, sizeof(*scpi_pd_data), GFP_KERNEL);
112 if (!scpi_pd_data)
113 return -ENOMEM;
114
115 domains = devm_kcalloc(dev, num_domains, sizeof(*domains), GFP_KERNEL);
116 if (!domains)
117 return -ENOMEM;
118
119 for (i = 0; i < num_domains; i++, scpi_pd++) {
120 domains[i] = &scpi_pd->genpd;
121
122 scpi_pd->domain = i;
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;
128
129
130
131
132
133
134
135 pm_genpd_init(&scpi_pd->genpd, NULL, true);
136 }
137
138 scpi_pd_data->domains = domains;
139 scpi_pd_data->num_domains = num_domains;
140
141 of_genpd_add_provider_onecell(np, scpi_pd_data);
142
143 return 0;
144}
145
146static const struct of_device_id scpi_power_domain_ids[] = {
147 { .compatible = "arm,scpi-power-domains", },
148 { }
149};
150MODULE_DEVICE_TABLE(of, scpi_power_domain_ids);
151
152static struct platform_driver scpi_power_domain_driver = {
153 .driver = {
154 .name = "scpi_power_domain",
155 .of_match_table = scpi_power_domain_ids,
156 },
157 .probe = scpi_pm_domain_probe,
158};
159module_platform_driver(scpi_power_domain_driver);
160
161MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
162MODULE_DESCRIPTION("ARM SCPI power domain driver");
163MODULE_LICENSE("GPL v2");
164