1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2024 Linaro Ltd.
6 #include <linux/device.h>
7 #include <linux/export.h>
8 #include <linux/kernel.h>
10 #include <linux/pci-pwrctl.h>
11 #include <linux/property.h>
12 #include <linux/slab.h>
14 static int pci_pwrctl_notify(struct notifier_block *nb, unsigned long action,
17 struct pci_pwrctl *pwrctl = container_of(nb, struct pci_pwrctl, nb);
18 struct device *dev = data;
20 if (dev_fwnode(dev) != dev_fwnode(pwrctl->dev))
24 case BUS_NOTIFY_ADD_DEVICE:
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.
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.
34 dev->of_node_reused = true;
36 case BUS_NOTIFY_BOUND_DRIVER:
37 pwrctl->link = device_link_add(dev, pwrctl->dev,
38 DL_FLAG_AUTOREMOVE_CONSUMER);
40 dev_err(pwrctl->dev, "Failed to add device link\n");
42 case BUS_NOTIFY_UNBOUND_DRIVER:
44 device_link_remove(dev, pwrctl->dev);
52 * pci_pwrctl_device_set_ready() - Notify the pwrctl subsystem that the PCI
53 * device is powered-up and ready to be detected.
55 * @pwrctl: PCI power control data.
58 * 0 on success, negative error number on error.
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.
65 int pci_pwrctl_device_set_ready(struct pci_pwrctl *pwrctl)
72 pwrctl->nb.notifier_call = pci_pwrctl_notify;
73 ret = bus_register_notifier(&pci_bus_type, &pwrctl->nb);
77 pci_lock_rescan_remove();
78 pci_rescan_bus(to_pci_dev(pwrctl->dev->parent)->bus);
79 pci_unlock_rescan_remove();
83 EXPORT_SYMBOL_GPL(pci_pwrctl_device_set_ready);
86 * pci_pwrctl_device_unset_ready() - Notify the pwrctl subsystem that the PCI
87 * device is about to be powered-down.
89 * @pwrctl: PCI power control data.
91 void pci_pwrctl_device_unset_ready(struct pci_pwrctl *pwrctl)
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.
99 bus_unregister_notifier(&pci_bus_type, &pwrctl->nb);
101 EXPORT_SYMBOL_GPL(pci_pwrctl_device_unset_ready);
103 static void devm_pci_pwrctl_device_unset_ready(void *data)
105 struct pci_pwrctl *pwrctl = data;
107 pci_pwrctl_device_unset_ready(pwrctl);
111 * devm_pci_pwrctl_device_set_ready - Managed variant of
112 * pci_pwrctl_device_set_ready().
114 * @dev: Device managing this pwrctl provider.
115 * @pwrctl: PCI power control data.
118 * 0 on success, negative error number on error.
120 int devm_pci_pwrctl_device_set_ready(struct device *dev,
121 struct pci_pwrctl *pwrctl)
125 ret = pci_pwrctl_device_set_ready(pwrctl);
129 return devm_add_action_or_reset(dev,
130 devm_pci_pwrctl_device_unset_ready,
133 EXPORT_SYMBOL_GPL(devm_pci_pwrctl_device_set_ready);
136 MODULE_DESCRIPTION("PCI Device Power Control core driver");
137 MODULE_LICENSE("GPL");