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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2024 Linaro Ltd.
*/
#define dev_fmt(fmt) "pwrctrl: " fmt
#include <linux/device.h>
#include <linux/export.h>
#include <linux/kernel.h>
#include <linux/of.h>
#include <linux/of_graph.h>
#include <linux/of_platform.h>
#include <linux/pci.h>
#include <linux/pci-pwrctrl.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/slab.h>
#include "../pci.h"
static int pci_pwrctrl_notify(struct notifier_block *nb, unsigned long action,
void *data)
{
struct pci_pwrctrl *pwrctrl = container_of(nb, struct pci_pwrctrl, nb);
struct device *dev = data;
if (dev_fwnode(dev) != dev_fwnode(pwrctrl->dev))
return NOTIFY_DONE;
switch (action) {
case BUS_NOTIFY_ADD_DEVICE:
/*
* We will have two struct device objects bound to two different
* drivers on different buses but consuming the same DT node. We
* must not bind the pins twice in this case but only once for
* the first device to be added.
*
* If we got here then the PCI device is the second after the
* power control platform device. Mark its OF node as reused.
*/
dev->of_node_reused = true;
break;
}
return NOTIFY_DONE;
}
/**
* pci_pwrctrl_init() - Initialize the PCI power control context struct
*
* @pwrctrl: PCI power control data
* @dev: Parent device
*/
void pci_pwrctrl_init(struct pci_pwrctrl *pwrctrl, struct device *dev)
{
pwrctrl->dev = dev;
dev_set_drvdata(dev, pwrctrl);
}
EXPORT_SYMBOL_GPL(pci_pwrctrl_init);
/**
* pci_pwrctrl_device_set_ready() - Notify the pwrctrl subsystem that the PCI
* device is powered-up and ready to be detected.
*
* @pwrctrl: PCI power control data.
*
* Returns:
* 0 on success, negative error number on error.
*
* Note:
* This function returning 0 doesn't mean the device was detected. It means,
* that the bus rescan was successfully started. The device will get bound to
* its PCI driver asynchronously.
*/
int pci_pwrctrl_device_set_ready(struct pci_pwrctrl *pwrctrl)
{
int ret;
if (!pwrctrl->dev)
return -ENODEV;
pwrctrl->nb.notifier_call = pci_pwrctrl_notify;
ret = bus_register_notifier(&pci_bus_type, &pwrctrl->nb);
if (ret)
return ret;
return 0;
}
EXPORT_SYMBOL_GPL(pci_pwrctrl_device_set_ready);
/**
* pci_pwrctrl_device_unset_ready() - Notify the pwrctrl subsystem that the PCI
* device is about to be powered-down.
*
* @pwrctrl: PCI power control data.
*/
void pci_pwrctrl_device_unset_ready(struct pci_pwrctrl *pwrctrl)
{
/*
* We don't have to delete the link here. Typically, this function
* is only called when the power control device is being detached. If
* it is being detached then the child PCI device must have already
* been unbound too or the device core wouldn't let us unbind.
*/
bus_unregister_notifier(&pci_bus_type, &pwrctrl->nb);
}
EXPORT_SYMBOL_GPL(pci_pwrctrl_device_unset_ready);
static void devm_pci_pwrctrl_device_unset_ready(void *data)
{
struct pci_pwrctrl *pwrctrl = data;
pci_pwrctrl_device_unset_ready(pwrctrl);
}
/**
* devm_pci_pwrctrl_device_set_ready - Managed variant of
* pci_pwrctrl_device_set_ready().
*
* @dev: Device managing this pwrctrl provider.
* @pwrctrl: PCI power control data.
*
* Returns:
* 0 on success, negative error number on error.
*/
int devm_pci_pwrctrl_device_set_ready(struct device *dev,
struct pci_pwrctrl *pwrctrl)
{
int ret;
ret = pci_pwrctrl_device_set_ready(pwrctrl);
if (ret)
return ret;
return devm_add_action_or_reset(dev,
devm_pci_pwrctrl_device_unset_ready,
pwrctrl);
}
EXPORT_SYMBOL_GPL(devm_pci_pwrctrl_device_set_ready);
static int __pci_pwrctrl_power_off_device(struct device *dev)
{
struct pci_pwrctrl *pwrctrl = dev_get_drvdata(dev);
if (!pwrctrl)
return 0;
return pwrctrl->power_off(pwrctrl);
}
static void pci_pwrctrl_power_off_device(struct device_node *np)
{
struct platform_device *pdev;
int ret;
for_each_available_child_of_node_scoped(np, child)
pci_pwrctrl_power_off_device(child);
pdev = of_find_device_by_node(np);
if (!pdev)
return;
if (device_is_bound(&pdev->dev)) {
ret = __pci_pwrctrl_power_off_device(&pdev->dev);
if (ret)
dev_err(&pdev->dev, "Failed to power off device: %d", ret);
}
platform_device_put(pdev);
}
/**
* pci_pwrctrl_power_off_devices - Power off pwrctrl devices
*
* @parent: PCI host controller device
*
* Recursively traverse all pwrctrl devices for the devicetree hierarchy
* below the specified PCI host controller and power them off in a depth
* first manner.
*/
void pci_pwrctrl_power_off_devices(struct device *parent)
{
struct device_node *np = parent->of_node;
for_each_available_child_of_node_scoped(np, child)
pci_pwrctrl_power_off_device(child);
}
EXPORT_SYMBOL_GPL(pci_pwrctrl_power_off_devices);
static int __pci_pwrctrl_power_on_device(struct device *dev)
{
struct pci_pwrctrl *pwrctrl = dev_get_drvdata(dev);
if (!pwrctrl)
return 0;
return pwrctrl->power_on(pwrctrl);
}
/*
* Power on the devices in a depth first manner. Before powering on the device,
* make sure its driver is bound.
*/
static int pci_pwrctrl_power_on_device(struct device_node *np)
{
struct platform_device *pdev;
int ret;
for_each_available_child_of_node_scoped(np, child) {
ret = pci_pwrctrl_power_on_device(child);
if (ret)
return ret;
}
pdev = of_find_device_by_node(np);
if (!pdev)
return 0;
if (device_is_bound(&pdev->dev)) {
ret = __pci_pwrctrl_power_on_device(&pdev->dev);
} else {
/* FIXME: Use blocking wait instead of probe deferral */
dev_dbg(&pdev->dev, "driver is not bound\n");
ret = -EPROBE_DEFER;
}
platform_device_put(pdev);
return ret;
}
/**
* pci_pwrctrl_power_on_devices - Power on pwrctrl devices
*
* @parent: PCI host controller device
*
* Recursively traverse all pwrctrl devices for the devicetree hierarchy
* below the specified PCI host controller and power them on in a depth
* first manner. On error, all powered on devices will be powered off.
*
* Return: 0 on success, -EPROBE_DEFER if any pwrctrl driver is not bound, an
* appropriate error code otherwise.
*/
int pci_pwrctrl_power_on_devices(struct device *parent)
{
struct device_node *np = parent->of_node;
struct device_node *child = NULL;
int ret;
for_each_available_child_of_node(np, child) {
ret = pci_pwrctrl_power_on_device(child);
if (ret)
goto err_power_off;
}
return 0;
err_power_off:
for_each_available_child_of_node_scoped(np, tmp) {
if (tmp == child)
break;
pci_pwrctrl_power_off_device(tmp);
}
of_node_put(child);
return ret;
}
EXPORT_SYMBOL_GPL(pci_pwrctrl_power_on_devices);
/*
* Check whether the pwrctrl device really needs to be created or not. The
* pwrctrl device will only be created if the node satisfies below requirements:
*
* 1. Presence of compatible property with "pci" prefix to match against the
* pwrctrl driver (AND)
* 2. At least one of the power supplies defined in the devicetree node of the
* device (OR) in the remote endpoint parent node to indicate pwrctrl
* requirement.
*/
static bool pci_pwrctrl_is_required(struct device_node *np)
{
struct device_node *endpoint;
const char *compat;
int ret;
ret = of_property_read_string(np, "compatible", &compat);
if (ret < 0)
return false;
if (!strstarts(compat, "pci"))
return false;
if (of_pci_supply_present(np))
return true;
if (of_graph_is_present(np)) {
for_each_endpoint_of_node(np, endpoint) {
struct device_node *remote __free(device_node) =
of_graph_get_remote_port_parent(endpoint);
if (remote) {
if (of_pci_supply_present(remote)) {
of_node_put(endpoint);
return true;
}
}
}
}
return false;
}
static int pci_pwrctrl_create_device(struct device_node *np,
struct device *parent)
{
struct platform_device *pdev;
int ret;
for_each_available_child_of_node_scoped(np, child) {
ret = pci_pwrctrl_create_device(child, parent);
if (ret)
return ret;
}
/* Bail out if the platform device is already available for the node */
pdev = of_find_device_by_node(np);
if (pdev) {
platform_device_put(pdev);
return 0;
}
if (!pci_pwrctrl_is_required(np)) {
dev_dbg(parent, "Skipping OF node: %s\n", np->name);
return 0;
}
/* Now create the pwrctrl device */
pdev = of_platform_device_create(np, NULL, parent);
if (!pdev) {
dev_err(parent, "Failed to create pwrctrl device for node: %s\n", np->name);
return -EINVAL;
}
return 0;
}
/**
* pci_pwrctrl_create_devices - Create pwrctrl devices
*
* @parent: PCI host controller device
*
* Recursively create pwrctrl devices for the devicetree hierarchy below
* the specified PCI host controller in a depth first manner. On error, all
* created devices will be destroyed.
*
* Return: 0 on success, negative error number on error.
*/
int pci_pwrctrl_create_devices(struct device *parent)
{
int ret;
for_each_available_child_of_node_scoped(parent->of_node, child) {
ret = pci_pwrctrl_create_device(child, parent);
if (ret) {
pci_pwrctrl_destroy_devices(parent);
return ret;
}
}
return 0;
}
EXPORT_SYMBOL_GPL(pci_pwrctrl_create_devices);
static void pci_pwrctrl_destroy_device(struct device_node *np)
{
struct platform_device *pdev;
for_each_available_child_of_node_scoped(np, child)
pci_pwrctrl_destroy_device(child);
pdev = of_find_device_by_node(np);
if (!pdev)
return;
of_device_unregister(pdev);
platform_device_put(pdev);
of_node_clear_flag(np, OF_POPULATED);
}
/**
* pci_pwrctrl_destroy_devices - Destroy pwrctrl devices
*
* @parent: PCI host controller device
*
* Recursively destroy pwrctrl devices for the devicetree hierarchy below
* the specified PCI host controller in a depth first manner.
*/
void pci_pwrctrl_destroy_devices(struct device *parent)
{
struct device_node *np = parent->of_node;
for_each_available_child_of_node_scoped(np, child)
pci_pwrctrl_destroy_device(child);
}
EXPORT_SYMBOL_GPL(pci_pwrctrl_destroy_devices);
MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
MODULE_DESCRIPTION("PCI Device Power Control core driver");
MODULE_LICENSE("GPL");
|