1 // SPDX-License-Identifier: GPL-2.0
3 * ZynqMP Generic PM domain support
5 * Copyright (C) 2015-2019 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)
26 static int min_capability
;
29 * struct zynqmp_pm_domain - Wrapper around struct generic_pm_domain
30 * @gpd: Generic power domain
31 * @node_id: PM node ID corresponding to device inside PM domain
32 * @flags: ZynqMP PM domain flags
34 struct zynqmp_pm_domain
{
35 struct generic_pm_domain gpd
;
41 * zynqmp_gpd_is_active_wakeup_path() - Check if device is in wakeup source
43 * @dev: Device to check for wakeup source path
44 * @not_used: Data member (not required)
46 * This function is checks device's child hierarchy and checks if any device is
47 * set as wakeup source.
49 * Return: 1 if device is in wakeup source path else 0
51 static int zynqmp_gpd_is_active_wakeup_path(struct device
*dev
, void *not_used
)
55 may_wakeup
= device_may_wakeup(dev
);
59 return device_for_each_child(dev
, NULL
,
60 zynqmp_gpd_is_active_wakeup_path
);
64 * zynqmp_gpd_power_on() - Power on PM domain
65 * @domain: Generic PM domain
67 * This function is called before devices inside a PM domain are resumed, to
70 * Return: 0 on success, error code otherwise
72 static int zynqmp_gpd_power_on(struct generic_pm_domain
*domain
)
75 struct zynqmp_pm_domain
*pd
;
77 pd
= container_of(domain
, struct zynqmp_pm_domain
, gpd
);
78 ret
= zynqmp_pm_set_requirement(pd
->node_id
,
79 ZYNQMP_PM_CAPABILITY_ACCESS
,
81 ZYNQMP_PM_REQUEST_ACK_BLOCKING
);
83 pr_err("%s() %s set requirement for node %d failed: %d\n",
84 __func__
, domain
->name
, pd
->node_id
, ret
);
88 pr_debug("%s() Powered on %s domain\n", __func__
, domain
->name
);
93 * zynqmp_gpd_power_off() - Power off PM domain
94 * @domain: Generic PM domain
96 * This function is called after devices inside a PM domain are suspended, to
97 * power off PM domain.
99 * Return: 0 on success, error code otherwise
101 static int zynqmp_gpd_power_off(struct generic_pm_domain
*domain
)
104 struct pm_domain_data
*pdd
, *tmp
;
105 struct zynqmp_pm_domain
*pd
;
106 u32 capabilities
= min_capability
;
109 pd
= container_of(domain
, struct zynqmp_pm_domain
, gpd
);
111 /* If domain is already released there is nothing to be done */
112 if (!(pd
->flags
& ZYNQMP_PM_DOMAIN_REQUESTED
)) {
113 pr_debug("%s() %s domain is already released\n",
114 __func__
, domain
->name
);
118 list_for_each_entry_safe(pdd
, tmp
, &domain
->dev_list
, list_node
) {
119 /* If device is in wakeup path, set capability to WAKEUP */
120 may_wakeup
= zynqmp_gpd_is_active_wakeup_path(pdd
->dev
, NULL
);
122 dev_dbg(pdd
->dev
, "device is in wakeup path in %s\n",
124 capabilities
= ZYNQMP_PM_CAPABILITY_WAKEUP
;
129 ret
= zynqmp_pm_set_requirement(pd
->node_id
, capabilities
, 0,
130 ZYNQMP_PM_REQUEST_ACK_NO
);
132 * If powering down of any node inside this domain fails,
133 * report and return the error
136 pr_err("%s() %s set requirement for node %d failed: %d\n",
137 __func__
, domain
->name
, pd
->node_id
, ret
);
141 pr_debug("%s() Powered off %s domain\n", __func__
, domain
->name
);
146 * zynqmp_gpd_attach_dev() - Attach device to the PM domain
147 * @domain: Generic PM domain
148 * @dev: Device to attach
150 * Return: 0 on success, error code otherwise
152 static int zynqmp_gpd_attach_dev(struct generic_pm_domain
*domain
,
156 struct zynqmp_pm_domain
*pd
;
158 pd
= container_of(domain
, struct zynqmp_pm_domain
, gpd
);
160 /* If this is not the first device to attach there is nothing to do */
161 if (domain
->device_count
)
164 ret
= zynqmp_pm_request_node(pd
->node_id
, 0, 0,
165 ZYNQMP_PM_REQUEST_ACK_BLOCKING
);
166 /* If requesting a node fails print and return the error */
168 pr_err("%s() %s request failed for node %d: %d\n",
169 __func__
, domain
->name
, pd
->node_id
, ret
);
173 pd
->flags
|= ZYNQMP_PM_DOMAIN_REQUESTED
;
175 pr_debug("%s() %s attached to %s domain\n", __func__
,
176 dev_name(dev
), domain
->name
);
181 * zynqmp_gpd_detach_dev() - Detach device from the PM domain
182 * @domain: Generic PM domain
183 * @dev: Device to detach
185 static void zynqmp_gpd_detach_dev(struct generic_pm_domain
*domain
,
189 struct zynqmp_pm_domain
*pd
;
191 pd
= container_of(domain
, struct zynqmp_pm_domain
, gpd
);
193 /* If this is not the last device to detach there is nothing to do */
194 if (domain
->device_count
)
197 ret
= zynqmp_pm_release_node(pd
->node_id
);
198 /* If releasing a node fails print the error and return */
200 pr_err("%s() %s release failed for node %d: %d\n",
201 __func__
, domain
->name
, pd
->node_id
, ret
);
205 pd
->flags
&= ~ZYNQMP_PM_DOMAIN_REQUESTED
;
207 pr_debug("%s() %s detached from %s domain\n", __func__
,
208 dev_name(dev
), domain
->name
);
211 static struct generic_pm_domain
*zynqmp_gpd_xlate
212 (struct of_phandle_args
*genpdspec
, void *data
)
214 struct genpd_onecell_data
*genpd_data
= data
;
215 unsigned int i
, idx
= genpdspec
->args
[0];
216 struct zynqmp_pm_domain
*pd
;
218 pd
= container_of(genpd_data
->domains
[0], struct zynqmp_pm_domain
, gpd
);
220 if (genpdspec
->args_count
!= 1)
221 return ERR_PTR(-EINVAL
);
223 /* Check for existing pm domains */
224 for (i
= 0; i
< ZYNQMP_NUM_DOMAINS
; i
++) {
225 if (pd
[i
].node_id
== idx
)
230 * Add index in empty node_id of power domain list as no existing
231 * power domain found for current index.
233 for (i
= 0; i
< ZYNQMP_NUM_DOMAINS
; i
++) {
234 if (pd
[i
].node_id
== 0) {
241 if (!genpd_data
->domains
[i
] || i
== ZYNQMP_NUM_DOMAINS
)
242 return ERR_PTR(-ENOENT
);
244 return genpd_data
->domains
[i
];
247 static int zynqmp_gpd_probe(struct platform_device
*pdev
)
250 struct genpd_onecell_data
*zynqmp_pd_data
;
251 struct generic_pm_domain
**domains
;
252 struct zynqmp_pm_domain
*pd
;
253 struct device
*dev
= &pdev
->dev
;
255 pd
= devm_kcalloc(dev
, ZYNQMP_NUM_DOMAINS
, sizeof(*pd
), GFP_KERNEL
);
259 zynqmp_pd_data
= devm_kzalloc(dev
, sizeof(*zynqmp_pd_data
), GFP_KERNEL
);
263 zynqmp_pd_data
->xlate
= zynqmp_gpd_xlate
;
265 domains
= devm_kcalloc(dev
, ZYNQMP_NUM_DOMAINS
, sizeof(*domains
),
270 if (!of_device_is_compatible(dev
->parent
->of_node
,
271 "xlnx,zynqmp-firmware"))
272 min_capability
= ZYNQMP_PM_CAPABILITY_UNUSABLE
;
274 for (i
= 0; i
< ZYNQMP_NUM_DOMAINS
; i
++, pd
++) {
276 pd
->gpd
.name
= kasprintf(GFP_KERNEL
, "domain%d", i
);
277 pd
->gpd
.power_off
= zynqmp_gpd_power_off
;
278 pd
->gpd
.power_on
= zynqmp_gpd_power_on
;
279 pd
->gpd
.attach_dev
= zynqmp_gpd_attach_dev
;
280 pd
->gpd
.detach_dev
= zynqmp_gpd_detach_dev
;
282 domains
[i
] = &pd
->gpd
;
284 /* Mark all PM domains as initially powered off */
285 pm_genpd_init(&pd
->gpd
, NULL
, true);
288 zynqmp_pd_data
->domains
= domains
;
289 zynqmp_pd_data
->num_domains
= ZYNQMP_NUM_DOMAINS
;
290 of_genpd_add_provider_onecell(dev
->parent
->of_node
, zynqmp_pd_data
);
295 static int zynqmp_gpd_remove(struct platform_device
*pdev
)
297 of_genpd_del_provider(pdev
->dev
.parent
->of_node
);
302 static struct platform_driver zynqmp_power_domain_driver
= {
304 .name
= "zynqmp_power_controller",
306 .probe
= zynqmp_gpd_probe
,
307 .remove
= zynqmp_gpd_remove
,
309 module_platform_driver(zynqmp_power_domain_driver
);
311 MODULE_ALIAS("platform:zynqmp_power_controller");