Linux Audio

Check our new training course

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