1 // SPDX-License-Identifier: GPL-2.0
3 * ZynqMP Generic PM domain support
5 * Copyright (C) 2015-2018 Xilinx, Inc.
7 * Davorin Mista <davorin.mista@aggios.com>
8 * Jolly Shah <jollys@xilinx.com>
9 * Rajan Vaja <rajan.vaja@xilinx.com>
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>
20 #include <linux/firmware/xlnx-zynqmp.h>
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)
27 * struct zynqmp_pm_domain - Wrapper around struct generic_pm_domain
28 * @gpd: Generic power domain
29 * @node_id: PM node ID corresponding to device inside PM domain
30 * @flags: ZynqMP PM domain flags
32 struct zynqmp_pm_domain
{
33 struct generic_pm_domain gpd
;
39 * zynqmp_gpd_is_active_wakeup_path() - Check if device is in wakeup source
41 * @dev: Device to check for wakeup source path
42 * @not_used: Data member (not required)
44 * This function is checks device's child hierarchy and checks if any device is
45 * set as wakeup source.
47 * Return: 1 if device is in wakeup source path else 0
49 static int zynqmp_gpd_is_active_wakeup_path(struct device
*dev
, void *not_used
)
53 may_wakeup
= device_may_wakeup(dev
);
57 return device_for_each_child(dev
, NULL
,
58 zynqmp_gpd_is_active_wakeup_path
);
62 * zynqmp_gpd_power_on() - Power on PM domain
63 * @domain: Generic PM domain
65 * This function is called before devices inside a PM domain are resumed, to
68 * Return: 0 on success, error code otherwise
70 static int zynqmp_gpd_power_on(struct generic_pm_domain
*domain
)
73 struct zynqmp_pm_domain
*pd
;
74 const struct zynqmp_eemi_ops
*eemi_ops
= zynqmp_pm_get_eemi_ops();
76 if (!eemi_ops
|| !eemi_ops
->set_requirement
)
79 pd
= container_of(domain
, struct zynqmp_pm_domain
, gpd
);
80 ret
= eemi_ops
->set_requirement(pd
->node_id
,
81 ZYNQMP_PM_CAPABILITY_ACCESS
,
83 ZYNQMP_PM_REQUEST_ACK_BLOCKING
);
85 pr_err("%s() %s set requirement for node %d failed: %d\n",
86 __func__
, domain
->name
, pd
->node_id
, ret
);
90 pr_debug("%s() Powered on %s domain\n", __func__
, domain
->name
);
95 * zynqmp_gpd_power_off() - Power off PM domain
96 * @domain: Generic PM domain
98 * This function is called after devices inside a PM domain are suspended, to
99 * power off PM domain.
101 * Return: 0 on success, error code otherwise
103 static int zynqmp_gpd_power_off(struct generic_pm_domain
*domain
)
106 struct pm_domain_data
*pdd
, *tmp
;
107 struct zynqmp_pm_domain
*pd
;
108 u32 capabilities
= 0;
110 const struct zynqmp_eemi_ops
*eemi_ops
= zynqmp_pm_get_eemi_ops();
112 if (!eemi_ops
|| !eemi_ops
->set_requirement
)
115 pd
= container_of(domain
, struct zynqmp_pm_domain
, gpd
);
117 /* If domain is already released there is nothing to be done */
118 if (!(pd
->flags
& ZYNQMP_PM_DOMAIN_REQUESTED
)) {
119 pr_debug("%s() %s domain is already released\n",
120 __func__
, domain
->name
);
124 list_for_each_entry_safe(pdd
, tmp
, &domain
->dev_list
, list_node
) {
125 /* If device is in wakeup path, set capability to WAKEUP */
126 may_wakeup
= zynqmp_gpd_is_active_wakeup_path(pdd
->dev
, NULL
);
128 dev_dbg(pdd
->dev
, "device is in wakeup path in %s\n",
130 capabilities
= ZYNQMP_PM_CAPABILITY_WAKEUP
;
135 ret
= eemi_ops
->set_requirement(pd
->node_id
, capabilities
, 0,
136 ZYNQMP_PM_REQUEST_ACK_NO
);
138 * If powering down of any node inside this domain fails,
139 * report and return the error
142 pr_err("%s() %s set requirement for node %d failed: %d\n",
143 __func__
, domain
->name
, pd
->node_id
, ret
);
147 pr_debug("%s() Powered off %s domain\n", __func__
, domain
->name
);
152 * zynqmp_gpd_attach_dev() - Attach device to the PM domain
153 * @domain: Generic PM domain
154 * @dev: Device to attach
156 * Return: 0 on success, error code otherwise
158 static int zynqmp_gpd_attach_dev(struct generic_pm_domain
*domain
,
162 struct zynqmp_pm_domain
*pd
;
163 const struct zynqmp_eemi_ops
*eemi_ops
= zynqmp_pm_get_eemi_ops();
165 if (!eemi_ops
|| !eemi_ops
->request_node
)
168 pd
= container_of(domain
, struct zynqmp_pm_domain
, gpd
);
170 /* If this is not the first device to attach there is nothing to do */
171 if (domain
->device_count
)
174 ret
= eemi_ops
->request_node(pd
->node_id
, 0, 0,
175 ZYNQMP_PM_REQUEST_ACK_BLOCKING
);
176 /* If requesting a node fails print and return the error */
178 pr_err("%s() %s request failed for node %d: %d\n",
179 __func__
, domain
->name
, pd
->node_id
, ret
);
183 pd
->flags
|= ZYNQMP_PM_DOMAIN_REQUESTED
;
185 pr_debug("%s() %s attached to %s domain\n", __func__
,
186 dev_name(dev
), domain
->name
);
191 * zynqmp_gpd_detach_dev() - Detach device from the PM domain
192 * @domain: Generic PM domain
193 * @dev: Device to detach
195 static void zynqmp_gpd_detach_dev(struct generic_pm_domain
*domain
,
199 struct zynqmp_pm_domain
*pd
;
200 const struct zynqmp_eemi_ops
*eemi_ops
= zynqmp_pm_get_eemi_ops();
202 if (!eemi_ops
|| !eemi_ops
->release_node
)
205 pd
= container_of(domain
, struct zynqmp_pm_domain
, gpd
);
207 /* If this is not the last device to detach there is nothing to do */
208 if (domain
->device_count
)
211 ret
= eemi_ops
->release_node(pd
->node_id
);
212 /* If releasing a node fails print the error and return */
214 pr_err("%s() %s release failed for node %d: %d\n",
215 __func__
, domain
->name
, pd
->node_id
, ret
);
219 pd
->flags
&= ~ZYNQMP_PM_DOMAIN_REQUESTED
;
221 pr_debug("%s() %s detached from %s domain\n", __func__
,
222 dev_name(dev
), domain
->name
);
225 static struct generic_pm_domain
*zynqmp_gpd_xlate
226 (struct of_phandle_args
*genpdspec
, void *data
)
228 struct genpd_onecell_data
*genpd_data
= data
;
229 unsigned int i
, idx
= genpdspec
->args
[0];
230 struct zynqmp_pm_domain
*pd
;
232 pd
= container_of(genpd_data
->domains
[0], struct zynqmp_pm_domain
, gpd
);
234 if (genpdspec
->args_count
!= 1)
235 return ERR_PTR(-EINVAL
);
237 /* Check for existing pm domains */
238 for (i
= 0; i
< ZYNQMP_NUM_DOMAINS
; i
++) {
239 if (pd
[i
].node_id
== idx
)
244 * Add index in empty node_id of power domain list as no existing
245 * power domain found for current index.
247 for (i
= 0; i
< ZYNQMP_NUM_DOMAINS
; i
++) {
248 if (pd
[i
].node_id
== 0) {
255 if (!genpd_data
->domains
[i
] || i
== ZYNQMP_NUM_DOMAINS
)
256 return ERR_PTR(-ENOENT
);
258 return genpd_data
->domains
[i
];
261 static int zynqmp_gpd_probe(struct platform_device
*pdev
)
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
;
269 pd
= devm_kcalloc(dev
, ZYNQMP_NUM_DOMAINS
, sizeof(*pd
), GFP_KERNEL
);
273 zynqmp_pd_data
= devm_kzalloc(dev
, sizeof(*zynqmp_pd_data
), GFP_KERNEL
);
277 zynqmp_pd_data
->xlate
= zynqmp_gpd_xlate
;
279 domains
= devm_kcalloc(dev
, ZYNQMP_NUM_DOMAINS
, sizeof(*domains
),
284 for (i
= 0; i
< ZYNQMP_NUM_DOMAINS
; i
++, pd
++) {
286 pd
->gpd
.name
= kasprintf(GFP_KERNEL
, "domain%d", i
);
287 pd
->gpd
.power_off
= zynqmp_gpd_power_off
;
288 pd
->gpd
.power_on
= zynqmp_gpd_power_on
;
289 pd
->gpd
.attach_dev
= zynqmp_gpd_attach_dev
;
290 pd
->gpd
.detach_dev
= zynqmp_gpd_detach_dev
;
292 domains
[i
] = &pd
->gpd
;
294 /* Mark all PM domains as initially powered off */
295 pm_genpd_init(&pd
->gpd
, NULL
, true);
298 zynqmp_pd_data
->domains
= domains
;
299 zynqmp_pd_data
->num_domains
= ZYNQMP_NUM_DOMAINS
;
300 of_genpd_add_provider_onecell(dev
->parent
->of_node
, zynqmp_pd_data
);
305 static int zynqmp_gpd_remove(struct platform_device
*pdev
)
307 of_genpd_del_provider(pdev
->dev
.parent
->of_node
);
312 static struct platform_driver zynqmp_power_domain_driver
= {
314 .name
= "zynqmp_power_controller",
316 .probe
= zynqmp_gpd_probe
,
317 .remove
= zynqmp_gpd_remove
,
319 module_platform_driver(zynqmp_power_domain_driver
);
321 MODULE_ALIAS("platform:zynqmp_power_controller");