Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * ZynqMP Generic PM domain support
4 *
5 * Copyright (C) 2015-2019 Xilinx, Inc.
6 *
7 * Davorin Mista <davorin.mista@aggios.com>
8 * Jolly Shah <jollys@xilinx.com>
9 * Rajan Vaja <rajan.vaja@xilinx.com>
10 */
11
12#include <linux/err.h>
13#include <linux/list.h>
14#include <linux/module.h>
15#include <linux/of_platform.h>
16#include <linux/platform_device.h>
17#include <linux/pm_domain.h>
18#include <linux/slab.h>
19
20#include <linux/firmware/xlnx-zynqmp.h>
21
22#define ZYNQMP_NUM_DOMAINS (100)
23/* Flag stating if PM nodes mapped to the PM domain has been requested */
24#define ZYNQMP_PM_DOMAIN_REQUESTED BIT(0)
25
26static const struct zynqmp_eemi_ops *eemi_ops;
27
28static int min_capability;
29
30/**
31 * struct zynqmp_pm_domain - Wrapper around struct generic_pm_domain
32 * @gpd: Generic power domain
33 * @node_id: PM node ID corresponding to device inside PM domain
34 * @flags: ZynqMP PM domain flags
35 */
36struct zynqmp_pm_domain {
37 struct generic_pm_domain gpd;
38 u32 node_id;
39 u8 flags;
40};
41
42/**
43 * zynqmp_gpd_is_active_wakeup_path() - Check if device is in wakeup source
44 * path
45 * @dev: Device to check for wakeup source path
46 * @not_used: Data member (not required)
47 *
48 * This function is checks device's child hierarchy and checks if any device is
49 * set as wakeup source.
50 *
51 * Return: 1 if device is in wakeup source path else 0
52 */
53static int zynqmp_gpd_is_active_wakeup_path(struct device *dev, void *not_used)
54{
55 int may_wakeup;
56
57 may_wakeup = device_may_wakeup(dev);
58 if (may_wakeup)
59 return may_wakeup;
60
61 return device_for_each_child(dev, NULL,
62 zynqmp_gpd_is_active_wakeup_path);
63}
64
65/**
66 * zynqmp_gpd_power_on() - Power on PM domain
67 * @domain: Generic PM domain
68 *
69 * This function is called before devices inside a PM domain are resumed, to
70 * power on PM domain.
71 *
72 * Return: 0 on success, error code otherwise
73 */
74static int zynqmp_gpd_power_on(struct generic_pm_domain *domain)
75{
76 int ret;
77 struct zynqmp_pm_domain *pd;
78
79 if (!eemi_ops->set_requirement)
80 return -ENXIO;
81
82 pd = container_of(domain, struct zynqmp_pm_domain, gpd);
83 ret = eemi_ops->set_requirement(pd->node_id,
84 ZYNQMP_PM_CAPABILITY_ACCESS,
85 ZYNQMP_PM_MAX_QOS,
86 ZYNQMP_PM_REQUEST_ACK_BLOCKING);
87 if (ret) {
88 pr_err("%s() %s set requirement for node %d failed: %d\n",
89 __func__, domain->name, pd->node_id, ret);
90 return ret;
91 }
92
93 pr_debug("%s() Powered on %s domain\n", __func__, domain->name);
94 return 0;
95}
96
97/**
98 * zynqmp_gpd_power_off() - Power off PM domain
99 * @domain: Generic PM domain
100 *
101 * This function is called after devices inside a PM domain are suspended, to
102 * power off PM domain.
103 *
104 * Return: 0 on success, error code otherwise
105 */
106static int zynqmp_gpd_power_off(struct generic_pm_domain *domain)
107{
108 int ret;
109 struct pm_domain_data *pdd, *tmp;
110 struct zynqmp_pm_domain *pd;
111 u32 capabilities = min_capability;
112 bool may_wakeup;
113
114 if (!eemi_ops->set_requirement)
115 return -ENXIO;
116
117 pd = container_of(domain, struct zynqmp_pm_domain, gpd);
118
119 /* If domain is already released there is nothing to be done */
120 if (!(pd->flags & ZYNQMP_PM_DOMAIN_REQUESTED)) {
121 pr_debug("%s() %s domain is already released\n",
122 __func__, domain->name);
123 return 0;
124 }
125
126 list_for_each_entry_safe(pdd, tmp, &domain->dev_list, list_node) {
127 /* If device is in wakeup path, set capability to WAKEUP */
128 may_wakeup = zynqmp_gpd_is_active_wakeup_path(pdd->dev, NULL);
129 if (may_wakeup) {
130 dev_dbg(pdd->dev, "device is in wakeup path in %s\n",
131 domain->name);
132 capabilities = ZYNQMP_PM_CAPABILITY_WAKEUP;
133 break;
134 }
135 }
136
137 ret = eemi_ops->set_requirement(pd->node_id, capabilities, 0,
138 ZYNQMP_PM_REQUEST_ACK_NO);
139 /**
140 * If powering down of any node inside this domain fails,
141 * report and return the error
142 */
143 if (ret) {
144 pr_err("%s() %s set requirement for node %d failed: %d\n",
145 __func__, domain->name, pd->node_id, ret);
146 return ret;
147 }
148
149 pr_debug("%s() Powered off %s domain\n", __func__, domain->name);
150 return 0;
151}
152
153/**
154 * zynqmp_gpd_attach_dev() - Attach device to the PM domain
155 * @domain: Generic PM domain
156 * @dev: Device to attach
157 *
158 * Return: 0 on success, error code otherwise
159 */
160static int zynqmp_gpd_attach_dev(struct generic_pm_domain *domain,
161 struct device *dev)
162{
163 int ret;
164 struct zynqmp_pm_domain *pd;
165
166 if (!eemi_ops->request_node)
167 return -ENXIO;
168
169 pd = container_of(domain, struct zynqmp_pm_domain, gpd);
170
171 /* If this is not the first device to attach there is nothing to do */
172 if (domain->device_count)
173 return 0;
174
175 ret = eemi_ops->request_node(pd->node_id, 0, 0,
176 ZYNQMP_PM_REQUEST_ACK_BLOCKING);
177 /* If requesting a node fails print and return the error */
178 if (ret) {
179 pr_err("%s() %s request failed for node %d: %d\n",
180 __func__, domain->name, pd->node_id, ret);
181 return ret;
182 }
183
184 pd->flags |= ZYNQMP_PM_DOMAIN_REQUESTED;
185
186 pr_debug("%s() %s attached to %s domain\n", __func__,
187 dev_name(dev), domain->name);
188 return 0;
189}
190
191/**
192 * zynqmp_gpd_detach_dev() - Detach device from the PM domain
193 * @domain: Generic PM domain
194 * @dev: Device to detach
195 */
196static void zynqmp_gpd_detach_dev(struct generic_pm_domain *domain,
197 struct device *dev)
198{
199 int ret;
200 struct zynqmp_pm_domain *pd;
201
202 if (!eemi_ops->release_node)
203 return;
204
205 pd = container_of(domain, struct zynqmp_pm_domain, gpd);
206
207 /* If this is not the last device to detach there is nothing to do */
208 if (domain->device_count)
209 return;
210
211 ret = eemi_ops->release_node(pd->node_id);
212 /* If releasing a node fails print the error and return */
213 if (ret) {
214 pr_err("%s() %s release failed for node %d: %d\n",
215 __func__, domain->name, pd->node_id, ret);
216 return;
217 }
218
219 pd->flags &= ~ZYNQMP_PM_DOMAIN_REQUESTED;
220
221 pr_debug("%s() %s detached from %s domain\n", __func__,
222 dev_name(dev), domain->name);
223}
224
225static struct generic_pm_domain *zynqmp_gpd_xlate
226 (struct of_phandle_args *genpdspec, void *data)
227{
228 struct genpd_onecell_data *genpd_data = data;
229 unsigned int i, idx = genpdspec->args[0];
230 struct zynqmp_pm_domain *pd;
231
232 pd = container_of(genpd_data->domains[0], struct zynqmp_pm_domain, gpd);
233
234 if (genpdspec->args_count != 1)
235 return ERR_PTR(-EINVAL);
236
237 /* Check for existing pm domains */
238 for (i = 0; i < ZYNQMP_NUM_DOMAINS; i++) {
239 if (pd[i].node_id == idx)
240 goto done;
241 }
242
243 /**
244 * Add index in empty node_id of power domain list as no existing
245 * power domain found for current index.
246 */
247 for (i = 0; i < ZYNQMP_NUM_DOMAINS; i++) {
248 if (pd[i].node_id == 0) {
249 pd[i].node_id = idx;
250 break;
251 }
252 }
253
254done:
255 if (!genpd_data->domains[i] || i == ZYNQMP_NUM_DOMAINS)
256 return ERR_PTR(-ENOENT);
257
258 return genpd_data->domains[i];
259}
260
261static int zynqmp_gpd_probe(struct platform_device *pdev)
262{
263 int i;
264 struct genpd_onecell_data *zynqmp_pd_data;
265 struct generic_pm_domain **domains;
266 struct zynqmp_pm_domain *pd;
267 struct device *dev = &pdev->dev;
268
269 eemi_ops = zynqmp_pm_get_eemi_ops();
270 if (IS_ERR(eemi_ops))
271 return PTR_ERR(eemi_ops);
272
273 pd = devm_kcalloc(dev, ZYNQMP_NUM_DOMAINS, sizeof(*pd), GFP_KERNEL);
274 if (!pd)
275 return -ENOMEM;
276
277 zynqmp_pd_data = devm_kzalloc(dev, sizeof(*zynqmp_pd_data), GFP_KERNEL);
278 if (!zynqmp_pd_data)
279 return -ENOMEM;
280
281 zynqmp_pd_data->xlate = zynqmp_gpd_xlate;
282
283 domains = devm_kcalloc(dev, ZYNQMP_NUM_DOMAINS, sizeof(*domains),
284 GFP_KERNEL);
285 if (!domains)
286 return -ENOMEM;
287
288 if (!of_device_is_compatible(dev->parent->of_node,
289 "xlnx,zynqmp-firmware"))
290 min_capability = ZYNQMP_PM_CAPABILITY_UNUSABLE;
291
292 for (i = 0; i < ZYNQMP_NUM_DOMAINS; i++, pd++) {
293 pd->node_id = 0;
294 pd->gpd.name = kasprintf(GFP_KERNEL, "domain%d", i);
295 pd->gpd.power_off = zynqmp_gpd_power_off;
296 pd->gpd.power_on = zynqmp_gpd_power_on;
297 pd->gpd.attach_dev = zynqmp_gpd_attach_dev;
298 pd->gpd.detach_dev = zynqmp_gpd_detach_dev;
299
300 domains[i] = &pd->gpd;
301
302 /* Mark all PM domains as initially powered off */
303 pm_genpd_init(&pd->gpd, NULL, true);
304 }
305
306 zynqmp_pd_data->domains = domains;
307 zynqmp_pd_data->num_domains = ZYNQMP_NUM_DOMAINS;
308 of_genpd_add_provider_onecell(dev->parent->of_node, zynqmp_pd_data);
309
310 return 0;
311}
312
313static int zynqmp_gpd_remove(struct platform_device *pdev)
314{
315 of_genpd_del_provider(pdev->dev.parent->of_node);
316
317 return 0;
318}
319
320static struct platform_driver zynqmp_power_domain_driver = {
321 .driver = {
322 .name = "zynqmp_power_controller",
323 },
324 .probe = zynqmp_gpd_probe,
325 .remove = zynqmp_gpd_remove,
326};
327module_platform_driver(zynqmp_power_domain_driver);
328
329MODULE_ALIAS("platform:zynqmp_power_controller");