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 const struct zynqmp_eemi_ops
*eemi_ops
;
28 static int min_capability
;
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
36 struct zynqmp_pm_domain
{
37 struct generic_pm_domain gpd
;
43 * zynqmp_gpd_is_active_wakeup_path() - Check if device is in wakeup source
45 * @dev: Device to check for wakeup source path
46 * @not_used: Data member (not required)
48 * This function is checks device's child hierarchy and checks if any device is
49 * set as wakeup source.
51 * Return: 1 if device is in wakeup source path else 0
53 static int zynqmp_gpd_is_active_wakeup_path(struct device
*dev
, void *not_used
)
57 may_wakeup
= device_may_wakeup(dev
);
61 return device_for_each_child(dev
, NULL
,
62 zynqmp_gpd_is_active_wakeup_path
);
66 * zynqmp_gpd_power_on() - Power on PM domain
67 * @domain: Generic PM domain
69 * This function is called before devices inside a PM domain are resumed, to
72 * Return: 0 on success, error code otherwise
74 static int zynqmp_gpd_power_on(struct generic_pm_domain
*domain
)
77 struct zynqmp_pm_domain
*pd
;
79 if (!eemi_ops
->set_requirement
)
82 pd
= container_of(domain
, struct zynqmp_pm_domain
, gpd
);
83 ret
= eemi_ops
->set_requirement(pd
->node_id
,
84 ZYNQMP_PM_CAPABILITY_ACCESS
,
86 ZYNQMP_PM_REQUEST_ACK_BLOCKING
);
88 pr_err("%s() %s set requirement for node %d failed: %d\n",
89 __func__
, domain
->name
, pd
->node_id
, ret
);
93 pr_debug("%s() Powered on %s domain\n", __func__
, domain
->name
);
98 * zynqmp_gpd_power_off() - Power off PM domain
99 * @domain: Generic PM domain
101 * This function is called after devices inside a PM domain are suspended, to
102 * power off PM domain.
104 * Return: 0 on success, error code otherwise
106 static int zynqmp_gpd_power_off(struct generic_pm_domain
*domain
)
109 struct pm_domain_data
*pdd
, *tmp
;
110 struct zynqmp_pm_domain
*pd
;
111 u32 capabilities
= min_capability
;
114 if (!eemi_ops
->set_requirement
)
117 pd
= container_of(domain
, struct zynqmp_pm_domain
, gpd
);
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
);
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
);
130 dev_dbg(pdd
->dev
, "device is in wakeup path in %s\n",
132 capabilities
= ZYNQMP_PM_CAPABILITY_WAKEUP
;
137 ret
= eemi_ops
->set_requirement(pd
->node_id
, capabilities
, 0,
138 ZYNQMP_PM_REQUEST_ACK_NO
);
140 * If powering down of any node inside this domain fails,
141 * report and return the error
144 pr_err("%s() %s set requirement for node %d failed: %d\n",
145 __func__
, domain
->name
, pd
->node_id
, ret
);
149 pr_debug("%s() Powered off %s domain\n", __func__
, domain
->name
);
154 * zynqmp_gpd_attach_dev() - Attach device to the PM domain
155 * @domain: Generic PM domain
156 * @dev: Device to attach
158 * Return: 0 on success, error code otherwise
160 static int zynqmp_gpd_attach_dev(struct generic_pm_domain
*domain
,
164 struct zynqmp_pm_domain
*pd
;
166 if (!eemi_ops
->request_node
)
169 pd
= container_of(domain
, struct zynqmp_pm_domain
, gpd
);
171 /* If this is not the first device to attach there is nothing to do */
172 if (domain
->device_count
)
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 */
179 pr_err("%s() %s request failed for node %d: %d\n",
180 __func__
, domain
->name
, pd
->node_id
, ret
);
184 pd
->flags
|= ZYNQMP_PM_DOMAIN_REQUESTED
;
186 pr_debug("%s() %s attached to %s domain\n", __func__
,
187 dev_name(dev
), domain
->name
);
192 * zynqmp_gpd_detach_dev() - Detach device from the PM domain
193 * @domain: Generic PM domain
194 * @dev: Device to detach
196 static void zynqmp_gpd_detach_dev(struct generic_pm_domain
*domain
,
200 struct zynqmp_pm_domain
*pd
;
202 if (!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 eemi_ops
= zynqmp_pm_get_eemi_ops();
270 if (IS_ERR(eemi_ops
))
271 return PTR_ERR(eemi_ops
);
273 pd
= devm_kcalloc(dev
, ZYNQMP_NUM_DOMAINS
, sizeof(*pd
), GFP_KERNEL
);
277 zynqmp_pd_data
= devm_kzalloc(dev
, sizeof(*zynqmp_pd_data
), GFP_KERNEL
);
281 zynqmp_pd_data
->xlate
= zynqmp_gpd_xlate
;
283 domains
= devm_kcalloc(dev
, ZYNQMP_NUM_DOMAINS
, sizeof(*domains
),
288 if (!of_device_is_compatible(dev
->parent
->of_node
,
289 "xlnx,zynqmp-firmware"))
290 min_capability
= ZYNQMP_PM_CAPABILITY_UNUSABLE
;
292 for (i
= 0; i
< ZYNQMP_NUM_DOMAINS
; i
++, pd
++) {
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
;
300 domains
[i
] = &pd
->gpd
;
302 /* Mark all PM domains as initially powered off */
303 pm_genpd_init(&pd
->gpd
, NULL
, true);
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
);
313 static int zynqmp_gpd_remove(struct platform_device
*pdev
)
315 of_genpd_del_provider(pdev
->dev
.parent
->of_node
);
320 static struct platform_driver zynqmp_power_domain_driver
= {
322 .name
= "zynqmp_power_controller",
324 .probe
= zynqmp_gpd_probe
,
325 .remove
= zynqmp_gpd_remove
,
327 module_platform_driver(zynqmp_power_domain_driver
);
329 MODULE_ALIAS("platform:zynqmp_power_controller");