Skip to content

Commit 4565d26

Browse files
author
Bartosz Golaszewski
committed
PCI/pwrctl: Add PCI power control core code
Some PCI devices must be powered-on before they can be detected on the bus. Introduce a simple framework reusing the existing PCI OF infrastructure. The way this works is: a DT node representing a PCI device connected to the port can be matched against its power control platform driver. If the match succeeds, the driver is responsible for powering-up the device and calling pci_pwrctl_device_set_ready() which will trigger a PCI bus rescan as well as subscribe to PCI bus notifications. When the device is detected and created, we'll make it consume the same DT node that the platform device did. When the device is bound, we'll create a device link between it and the parent power control device. Tested-by: Amit Pundir <[email protected]> Tested-by: Neil Armstrong <[email protected]> # on SM8550-QRD, SM8650-QRD & SM8650-HDK Tested-by: Caleb Connolly <[email protected]> # OnePlus 8T Acked-by: Bjorn Helgaas <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Bartosz Golaszewski <[email protected]>
1 parent 8fb1861 commit 4565d26

File tree

7 files changed

+210
-0
lines changed

7 files changed

+210
-0
lines changed

MAINTAINERS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17349,6 +17349,14 @@ F: Documentation/driver-api/pci/p2pdma.rst
1734917349
F: drivers/pci/p2pdma.c
1735017350
F: include/linux/pci-p2pdma.h
1735117351

17352+
PCI POWER CONTROL
17353+
M: Bartosz Golaszewski <[email protected]>
17354+
17355+
S: Maintained
17356+
T: git git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git
17357+
F: drivers/pci/pwrctl/*
17358+
F: include/linux/pci-pwrctl.h
17359+
1735217360
PCI SUBSYSTEM
1735317361
M: Bjorn Helgaas <[email protected]>
1735417362

drivers/pci/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,5 +296,6 @@ source "drivers/pci/hotplug/Kconfig"
296296
source "drivers/pci/controller/Kconfig"
297297
source "drivers/pci/endpoint/Kconfig"
298298
source "drivers/pci/switch/Kconfig"
299+
source "drivers/pci/pwrctl/Kconfig"
299300

300301
endif

drivers/pci/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ obj-$(CONFIG_PCI) += access.o bus.o probe.o host-bridge.o \
99

1010
obj-$(CONFIG_PCI) += msi/
1111
obj-$(CONFIG_PCI) += pcie/
12+
obj-$(CONFIG_PCI) += pwrctl/
1213

1314
ifdef CONFIG_PCI
1415
obj-$(CONFIG_PROC_FS) += proc.o

drivers/pci/pwrctl/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
3+
menu "PCI Power control drivers"
4+
5+
config PCI_PWRCTL
6+
tristate
7+
8+
endmenu

drivers/pci/pwrctl/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
3+
obj-$(CONFIG_PCI_PWRCTL) += pci-pwrctl-core.o
4+
pci-pwrctl-core-y := core.o

drivers/pci/pwrctl/core.c

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2024 Linaro Ltd.
4+
*/
5+
6+
#include <linux/device.h>
7+
#include <linux/export.h>
8+
#include <linux/kernel.h>
9+
#include <linux/pci.h>
10+
#include <linux/pci-pwrctl.h>
11+
#include <linux/property.h>
12+
#include <linux/slab.h>
13+
14+
static int pci_pwrctl_notify(struct notifier_block *nb, unsigned long action,
15+
void *data)
16+
{
17+
struct pci_pwrctl *pwrctl = container_of(nb, struct pci_pwrctl, nb);
18+
struct device *dev = data;
19+
20+
if (dev_fwnode(dev) != dev_fwnode(pwrctl->dev))
21+
return NOTIFY_DONE;
22+
23+
switch (action) {
24+
case BUS_NOTIFY_ADD_DEVICE:
25+
/*
26+
* We will have two struct device objects bound to two different
27+
* drivers on different buses but consuming the same DT node. We
28+
* must not bind the pins twice in this case but only once for
29+
* the first device to be added.
30+
*
31+
* If we got here then the PCI device is the second after the
32+
* power control platform device. Mark its OF node as reused.
33+
*/
34+
dev->of_node_reused = true;
35+
break;
36+
case BUS_NOTIFY_BOUND_DRIVER:
37+
pwrctl->link = device_link_add(dev, pwrctl->dev,
38+
DL_FLAG_AUTOREMOVE_CONSUMER);
39+
if (!pwrctl->link)
40+
dev_err(pwrctl->dev, "Failed to add device link\n");
41+
break;
42+
case BUS_NOTIFY_UNBOUND_DRIVER:
43+
if (pwrctl->link)
44+
device_link_remove(dev, pwrctl->dev);
45+
break;
46+
}
47+
48+
return NOTIFY_DONE;
49+
}
50+
51+
/**
52+
* pci_pwrctl_device_set_ready() - Notify the pwrctl subsystem that the PCI
53+
* device is powered-up and ready to be detected.
54+
*
55+
* @pwrctl: PCI power control data.
56+
*
57+
* Returns:
58+
* 0 on success, negative error number on error.
59+
*
60+
* Note:
61+
* This function returning 0 doesn't mean the device was detected. It means,
62+
* that the bus rescan was successfully started. The device will get bound to
63+
* its PCI driver asynchronously.
64+
*/
65+
int pci_pwrctl_device_set_ready(struct pci_pwrctl *pwrctl)
66+
{
67+
int ret;
68+
69+
if (!pwrctl->dev)
70+
return -ENODEV;
71+
72+
pwrctl->nb.notifier_call = pci_pwrctl_notify;
73+
ret = bus_register_notifier(&pci_bus_type, &pwrctl->nb);
74+
if (ret)
75+
return ret;
76+
77+
pci_lock_rescan_remove();
78+
pci_rescan_bus(to_pci_dev(pwrctl->dev->parent)->bus);
79+
pci_unlock_rescan_remove();
80+
81+
return 0;
82+
}
83+
EXPORT_SYMBOL_GPL(pci_pwrctl_device_set_ready);
84+
85+
/**
86+
* pci_pwrctl_device_unset_ready() - Notify the pwrctl subsystem that the PCI
87+
* device is about to be powered-down.
88+
*
89+
* @pwrctl: PCI power control data.
90+
*/
91+
void pci_pwrctl_device_unset_ready(struct pci_pwrctl *pwrctl)
92+
{
93+
/*
94+
* We don't have to delete the link here. Typically, this function
95+
* is only called when the power control device is being detached. If
96+
* it is being detached then the child PCI device must have already
97+
* been unbound too or the device core wouldn't let us unbind.
98+
*/
99+
bus_unregister_notifier(&pci_bus_type, &pwrctl->nb);
100+
}
101+
EXPORT_SYMBOL_GPL(pci_pwrctl_device_unset_ready);
102+
103+
static void devm_pci_pwrctl_device_unset_ready(void *data)
104+
{
105+
struct pci_pwrctl *pwrctl = data;
106+
107+
pci_pwrctl_device_unset_ready(pwrctl);
108+
}
109+
110+
/**
111+
* devm_pci_pwrctl_device_set_ready - Managed variant of
112+
* pci_pwrctl_device_set_ready().
113+
*
114+
* @dev: Device managing this pwrctl provider.
115+
* @pwrctl: PCI power control data.
116+
*
117+
* Returns:
118+
* 0 on success, negative error number on error.
119+
*/
120+
int devm_pci_pwrctl_device_set_ready(struct device *dev,
121+
struct pci_pwrctl *pwrctl)
122+
{
123+
int ret;
124+
125+
ret = pci_pwrctl_device_set_ready(pwrctl);
126+
if (ret)
127+
return ret;
128+
129+
return devm_add_action_or_reset(dev,
130+
devm_pci_pwrctl_device_unset_ready,
131+
pwrctl);
132+
}
133+
EXPORT_SYMBOL_GPL(devm_pci_pwrctl_device_set_ready);
134+
135+
MODULE_AUTHOR("Bartosz Golaszewski <[email protected]>");
136+
MODULE_DESCRIPTION("PCI Device Power Control core driver");
137+
MODULE_LICENSE("GPL");

include/linux/pci-pwrctl.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright (C) 2024 Linaro Ltd.
4+
*/
5+
6+
#ifndef __PCI_PWRCTL_H__
7+
#define __PCI_PWRCTL_H__
8+
9+
#include <linux/notifier.h>
10+
11+
struct device;
12+
struct device_link;
13+
14+
/*
15+
* This is a simple framework for solving the issue of PCI devices that require
16+
* certain resources (regulators, GPIOs, clocks) to be enabled before the
17+
* device can actually be detected on the PCI bus.
18+
*
19+
* The idea is to reuse the platform bus to populate OF nodes describing the
20+
* PCI device and its resources, let these platform devices probe and enable
21+
* relevant resources and then trigger a rescan of the PCI bus allowing for the
22+
* same device (with a second associated struct device) to be registered with
23+
* the PCI subsystem.
24+
*
25+
* To preserve a correct hierarchy for PCI power management and device reset,
26+
* we create a device link between the power control platform device (parent)
27+
* and the supplied PCI device (child).
28+
*/
29+
30+
/**
31+
* struct pci_pwrctl - PCI device power control context.
32+
* @dev: Address of the power controlling device.
33+
*
34+
* An object of this type must be allocated by the PCI power control device and
35+
* passed to the pwrctl subsystem to trigger a bus rescan and setup a device
36+
* link with the device once it's up.
37+
*/
38+
struct pci_pwrctl {
39+
struct device *dev;
40+
41+
/* Private: don't use. */
42+
struct notifier_block nb;
43+
struct device_link *link;
44+
};
45+
46+
int pci_pwrctl_device_set_ready(struct pci_pwrctl *pwrctl);
47+
void pci_pwrctl_device_unset_ready(struct pci_pwrctl *pwrctl);
48+
int devm_pci_pwrctl_device_set_ready(struct device *dev,
49+
struct pci_pwrctl *pwrctl);
50+
51+
#endif /* __PCI_PWRCTL_H__ */

0 commit comments

Comments
 (0)