summaryrefslogtreecommitdiffstats
path: root/drivers/soc/xilinx/zynqmp_pm_domains.c
blob: 354d256e6e006dac32935e7187f0a7245f68568f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// SPDX-License-Identifier: GPL-2.0
/*
 * ZynqMP Generic PM domain support
 *
 *  Copyright (C) 2015-2018 Xilinx, Inc.
 *
 *  Davorin Mista <davorin.mista@aggios.com>
 *  Jolly Shah <jollys@xilinx.com>
 *  Rajan Vaja <rajan.vaja@xilinx.com>
 */

#include <linux/err.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/pm_domain.h>
#include <linux/slab.h>

#include <linux/firmware/xlnx-zynqmp.h>

#define ZYNQMP_NUM_DOMAINS		(100)
/* Flag stating if PM nodes mapped to the PM domain has been requested */
#define ZYNQMP_PM_DOMAIN_REQUESTED	BIT(0)

/**
 * struct zynqmp_pm_domain - Wrapper around struct generic_pm_domain
 * @gpd:		Generic power domain
 * @node_id:		PM node ID corresponding to device inside PM domain
 * @flags:		ZynqMP PM domain flags
 */
struct zynqmp_pm_domain {
	struct generic_pm_domain gpd;
	u32 node_id;
	u8 flags;
};

/**
 * zynqmp_gpd_is_active_wakeup_path() - Check if device is in wakeup source
 *					path
 * @dev:	Device to check for wakeup source path
 * @not_used:	Data member (not required)
 *
 * This function is checks device's child hierarchy and checks if any device is
 * set as wakeup source.
 *
 * Return: 1 if device is in wakeup source path else 0
 */
static int zynqmp_gpd_is_active_wakeup_path(struct device *dev, void *not_used)
{
	int may_wakeup;

	may_wakeup = device_may_wakeup(dev);
	if (may_wakeup)
		return may_wakeup;

	return device_for_each_child(dev, NULL,
			zynqmp_gpd_is_active_wakeup_path);
}

/**
 * zynqmp_gpd_power_on() - Power on PM domain
 * @domain:	Generic PM domain
 *
 * This function is called before devices inside a PM domain are resumed, to
 * power on PM domain.
 *
 * Return: 0 on success, error code otherwise
 */
static int zynqmp_gpd_power_on(struct generic_pm_domain *domain)
{
	int ret;
	struct zynqmp_pm_domain *pd;
	const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();

	if (!eemi_ops || !eemi_ops->set_requirement)
		return -ENXIO;

	pd = container_of(domain, struct zynqmp_pm_domain, gpd);
	ret = eemi_ops->set_requirement(pd->node_id,
					ZYNQMP_PM_CAPABILITY_ACCESS,
					ZYNQMP_PM_MAX_QOS,
					ZYNQMP_PM_REQUEST_ACK_BLOCKING);
	if (ret) {
		pr_err("%s() %s set requirement for node %d failed: %d\n",
		       __func__, domain->name, pd->node_id, ret);
		return ret;
	}

	pr_debug("%s() Powered on %s domain\n", __func__, domain->name);
	return 0;
}

/**
 * zynqmp_gpd_power_off() - Power off PM domain
 * @domain:	Generic PM domain
 *
 * This function is called after devices inside a PM domain are suspended, to
 * power off PM domain.
 *
 * Return: 0 on success, error code otherwise
 */
static int zynqmp_gpd_power_off(struct generic_pm_domain *domain)
{
	int ret;
	struct pm_domain_data *pdd, *tmp;
	struct zynqmp_pm_domain *pd;
	u32 capabilities = 0;
	bool may_wakeup;
	const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();

	if (!eemi_ops || !eemi_ops->set_requirement)
		return -ENXIO;

	pd = container_of(domain, struct zynqmp_pm_domain, gpd);

	/* If domain is already released there is nothing to be done */
	if (!(pd->flags & ZYNQMP_PM_DOMAIN_REQUESTED)) {
		pr_debug("%s() %s domain is already released\n",
			 __func__, domain->name);
		return 0;
	}

	list_for_each_entry_safe(pdd, tmp, &domain->dev_list, list_node) {
		/* If device is in wakeup path, set capability to WAKEUP */
		may_wakeup = zynqmp_gpd_is_active_wakeup_path(pdd->dev, NULL);
		if (may_wakeup) {
			dev_dbg(pdd->dev, "device is in wakeup path in %s\n",
				domain->name);
			capabilities = ZYNQMP_PM_CAPABILITY_WAKEUP;
			break;
		}
	}

	ret = eemi_ops->set_requirement(pd->node_id, capabilities, 0,
					ZYNQMP_PM_REQUEST_ACK_NO);
	/**
	 * If powering down of any node inside this domain fails,
	 * report and return the error
	 */
	if (ret) {
		pr_err("%s() %s set requirement for node %d failed: %d\n",
		       __func__, domain->name, pd->node_id, ret);
		return ret;
	}

	pr_debug("%s() Powered off %s domain\n", __func__, domain->name);
	return 0;
}

/**
 * zynqmp_gpd_attach_dev() - Attach device to the PM domain
 * @domain:	Generic PM domain
 * @dev:	Device to attach
 *
 * Return: 0 on success, error code otherwise
 */
static int zynqmp_gpd_attach_dev(struct generic_pm_domain *domain,
				 struct device *dev)
{
	int ret;
	struct zynqmp_pm_domain *pd;
	const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();

	if (!eemi_ops || !eemi_ops->request_node)
		return -ENXIO;

	pd = container_of(domain, struct zynqmp_pm_domain, gpd);

	/* If this is not the first device to attach there is nothing to do */
	if (domain->device_count)
		return 0;

	ret = eemi_ops->request_node(pd->node_id, 0, 0,
				     ZYNQMP_PM_REQUEST_ACK_BLOCKING);
	/* If requesting a node fails print and return the error */
	if (ret) {
		pr_err("%s() %s request failed for node %d: %d\n",
		       __func__, domain->name, pd->node_id, ret);
		return ret;
	}

	pd->flags |= ZYNQMP_PM_DOMAIN_REQUESTED;

	pr_debug("%s() %s attached to %s domain\n", __func__,
		 dev_name(dev), domain->name);
	return 0;
}

/**
 * zynqmp_gpd_detach_dev() - Detach device from the PM domain
 * @domain:	Generic PM domain
 * @dev:	Device to detach
 */
static void zynqmp_gpd_detach_dev(struct generic_pm_domain *domain,
				  struct device *dev)
{
	int ret;
	struct zynqmp_pm_domain *pd;
	const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();

	if (!eemi_ops || !eemi_ops->release_node)
		return;

	pd = container_of(domain, struct zynqmp_pm_domain, gpd);

	/* If this is not the last device to detach there is nothing to do */
	if (domain->device_count)
		return;

	ret = eemi_ops->release_node(pd->node_id);
	/* If releasing a node fails print the error and return */
	if (ret) {
		pr_err("%s() %s release failed for node %d: %d\n",
		       __func__, domain->name, pd->node_id, ret);
		return;
	}

	pd->flags &= ~ZYNQMP_PM_DOMAIN_REQUESTED;

	pr_debug("%s() %s detached from %s domain\n", __func__,
		 dev_name(dev), domain->name);
}

static struct generic_pm_domain *zynqmp_gpd_xlate
				(struct of_phandle_args *genpdspec, void *data)
{
	struct genpd_onecell_data *genpd_data = data;
	unsigned int i, idx = genpdspec->args[0];
	struct zynqmp_pm_domain *pd;

	pd = container_of(genpd_data->domains[0], struct zynqmp_pm_domain, gpd);

	if (genpdspec->args_count != 1)
		return ERR_PTR(-EINVAL);

	/* Check for existing pm domains */
	for (i = 0; i < ZYNQMP_NUM_DOMAINS; i++) {
		if (pd[i].node_id == idx)
			goto done;
	}

	/**
	 * Add index in empty node_id of power domain list as no existing
	 * power domain found for current index.
	 */
	for (i = 0; i < ZYNQMP_NUM_DOMAINS; i++) {
		if (pd[i].node_id == 0) {
			pd[i].node_id = idx;
			break;
		}
	}

done:
	if (!genpd_data->domains[i] || i == ZYNQMP_NUM_DOMAINS)
		return ERR_PTR(-ENOENT);

	return genpd_data->domains[i];
}

static int zynqmp_gpd_probe(struct platform_device *pdev)
{
	int i;
	struct genpd_onecell_data *zynqmp_pd_data;
	struct generic_pm_domain **domains;
	struct zynqmp_pm_domain *pd;
	struct device *dev = &pdev->dev;

	pd = devm_kcalloc(dev, ZYNQMP_NUM_DOMAINS, sizeof(*pd), GFP_KERNEL);
	if (!pd)
		return -ENOMEM;

	zynqmp_pd_data = devm_kzalloc(dev, sizeof(*zynqmp_pd_data), GFP_KERNEL);
	if (!zynqmp_pd_data)
		return -ENOMEM;

	zynqmp_pd_data->xlate = zynqmp_gpd_xlate;

	domains = devm_kcalloc(dev, ZYNQMP_NUM_DOMAINS, sizeof(*domains),
			       GFP_KERNEL);
	if (!domains)
		return -ENOMEM;

	for (i = 0; i < ZYNQMP_NUM_DOMAINS; i++, pd++) {
		pd->node_id = 0;
		pd->gpd.name = kasprintf(GFP_KERNEL, "domain%d", i);
		pd->gpd.power_off = zynqmp_gpd_power_off;
		pd->gpd.power_on = zynqmp_gpd_power_on;
		pd->gpd.attach_dev = zynqmp_gpd_attach_dev;
		pd->gpd.detach_dev = zynqmp_gpd_detach_dev;

		domains[i] = &pd->gpd;

		/* Mark all PM domains as initially powered off */
		pm_genpd_init(&pd->gpd, NULL, true);
	}

	zynqmp_pd_data->domains = domains;
	zynqmp_pd_data->num_domains = ZYNQMP_NUM_DOMAINS;
	of_genpd_add_provider_onecell(dev->parent->of_node, zynqmp_pd_data);

	return 0;
}

static int zynqmp_gpd_remove(struct platform_device *pdev)
{
	of_genpd_del_provider(pdev->dev.parent->of_node);

	return 0;
}

static struct platform_driver zynqmp_power_domain_driver = {
	.driver	= {
		.name = "zynqmp_power_controller",
	},
	.probe = zynqmp_gpd_probe,
	.remove = zynqmp_gpd_remove,
};
module_platform_driver(zynqmp_power_domain_driver);

MODULE_ALIAS("platform:zynqmp_power_controller");