1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SCMI Generic power domain support.
4 *
5 * Copyright (C) 2018-2021 ARM Ltd.
6 */
7
8 #include <linux/err.h>
9 #include <linux/io.h>
10 #include <linux/module.h>
11 #include <linux/pm_domain.h>
12 #include <linux/scmi_protocol.h>
13
14 static const struct scmi_power_proto_ops *power_ops;
15
16 struct scmi_pm_domain {
17 struct generic_pm_domain genpd;
18 const struct scmi_protocol_handle *ph;
19 const char *name;
20 u32 domain;
21 };
22
23 #define to_scmi_pd(gpd) container_of(gpd, struct scmi_pm_domain, genpd)
24
scmi_pd_power(struct generic_pm_domain * domain,u32 state)25 static int scmi_pd_power(struct generic_pm_domain *domain, u32 state)
26 {
27 struct scmi_pm_domain *pd = to_scmi_pd(domain);
28
29 return power_ops->state_set(pd->ph, pd->domain, state);
30 }
31
scmi_pd_power_on(struct generic_pm_domain * domain)32 static int scmi_pd_power_on(struct generic_pm_domain *domain)
33 {
34 return scmi_pd_power(domain, SCMI_POWER_STATE_GENERIC_ON);
35 }
36
scmi_pd_power_off(struct generic_pm_domain * domain)37 static int scmi_pd_power_off(struct generic_pm_domain *domain)
38 {
39 return scmi_pd_power(domain, SCMI_POWER_STATE_GENERIC_OFF);
40 }
41
scmi_pm_domain_probe(struct scmi_device * sdev)42 static int scmi_pm_domain_probe(struct scmi_device *sdev)
43 {
44 int num_domains, i;
45 struct device *dev = &sdev->dev;
46 struct device_node *np = dev->of_node;
47 struct scmi_pm_domain *scmi_pd;
48 struct genpd_onecell_data *scmi_pd_data;
49 struct generic_pm_domain **domains;
50 const struct scmi_handle *handle = sdev->handle;
51 struct scmi_protocol_handle *ph;
52
53 if (!handle)
54 return -ENODEV;
55
56 power_ops = handle->devm_protocol_get(sdev, SCMI_PROTOCOL_POWER, &ph);
57 if (IS_ERR(power_ops))
58 return PTR_ERR(power_ops);
59
60 num_domains = power_ops->num_domains_get(ph);
61 if (num_domains < 0) {
62 dev_err(dev, "number of domains not found\n");
63 return num_domains;
64 }
65
66 scmi_pd = devm_kcalloc(dev, num_domains, sizeof(*scmi_pd), GFP_KERNEL);
67 if (!scmi_pd)
68 return -ENOMEM;
69
70 scmi_pd_data = devm_kzalloc(dev, sizeof(*scmi_pd_data), GFP_KERNEL);
71 if (!scmi_pd_data)
72 return -ENOMEM;
73
74 domains = devm_kcalloc(dev, num_domains, sizeof(*domains), GFP_KERNEL);
75 if (!domains)
76 return -ENOMEM;
77
78 for (i = 0; i < num_domains; i++, scmi_pd++) {
79 u32 state;
80
81 if (power_ops->state_get(ph, i, &state)) {
82 dev_warn(dev, "failed to get state for domain %d\n", i);
83 continue;
84 }
85
86 /*
87 * Register the explicit power on request to the firmware so
88 * that it is tracked as used by OSPM agent and not
89 * accidentally turned off with OSPM's knowledge
90 */
91 if (state == SCMI_POWER_STATE_GENERIC_ON)
92 power_ops->state_set(ph, i, state);
93
94 scmi_pd->domain = i;
95 scmi_pd->ph = ph;
96 scmi_pd->name = power_ops->name_get(ph, i);
97 scmi_pd->genpd.name = scmi_pd->name;
98 scmi_pd->genpd.power_off = scmi_pd_power_off;
99 scmi_pd->genpd.power_on = scmi_pd_power_on;
100 scmi_pd->genpd.flags = GENPD_FLAG_ACTIVE_WAKEUP;
101
102 pm_genpd_init(&scmi_pd->genpd, NULL,
103 state == SCMI_POWER_STATE_GENERIC_OFF);
104
105 domains[i] = &scmi_pd->genpd;
106 }
107
108 scmi_pd_data->domains = domains;
109 scmi_pd_data->num_domains = num_domains;
110
111 dev_set_drvdata(dev, scmi_pd_data);
112
113 return of_genpd_add_provider_onecell(np, scmi_pd_data);
114 }
115
scmi_pm_domain_remove(struct scmi_device * sdev)116 static void scmi_pm_domain_remove(struct scmi_device *sdev)
117 {
118 int i;
119 struct genpd_onecell_data *scmi_pd_data;
120 struct device *dev = &sdev->dev;
121 struct device_node *np = dev->of_node;
122
123 of_genpd_del_provider(np);
124
125 scmi_pd_data = dev_get_drvdata(dev);
126 for (i = 0; i < scmi_pd_data->num_domains; i++) {
127 if (!scmi_pd_data->domains[i])
128 continue;
129 pm_genpd_remove(scmi_pd_data->domains[i]);
130 }
131 }
132
133 static const struct scmi_device_id scmi_id_table[] = {
134 { SCMI_PROTOCOL_POWER, "genpd" },
135 { },
136 };
137 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
138
139 static struct scmi_driver scmi_power_domain_driver = {
140 .name = "scmi-power-domain",
141 .probe = scmi_pm_domain_probe,
142 .remove = scmi_pm_domain_remove,
143 .id_table = scmi_id_table,
144 };
145 module_scmi_driver(scmi_power_domain_driver);
146
147 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
148 MODULE_DESCRIPTION("ARM SCMI power domain driver");
149 MODULE_LICENSE("GPL v2");
150