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);
51 static void rescan_work_func(struct work_struct *work)
53 struct pci_pwrctl *pwrctl = container_of(work, struct pci_pwrctl, work);
55 pci_lock_rescan_remove();
56 pci_rescan_bus(to_pci_dev(pwrctl->dev->parent)->bus);
57 pci_unlock_rescan_remove();
61 * pci_pwrctl_init() - Initialize the PCI power control context struct
63 * @pwrctl: PCI power control data
66 void pci_pwrctl_init(struct pci_pwrctl *pwrctl, struct device *dev)
69 INIT_WORK(&pwrctl->work, rescan_work_func);
71 EXPORT_SYMBOL_GPL(pci_pwrctl_init);
74 * pci_pwrctl_device_set_ready() - Notify the pwrctl subsystem that the PCI
75 * device is powered-up and ready to be detected.
77 * @pwrctl: PCI power control data.
80 * 0 on success, negative error number on error.
83 * This function returning 0 doesn't mean the device was detected. It means,
84 * that the bus rescan was successfully started. The device will get bound to
85 * its PCI driver asynchronously.
87 int pci_pwrctl_device_set_ready(struct pci_pwrctl *pwrctl)
94 pwrctl->nb.notifier_call = pci_pwrctl_notify;
95 ret = bus_register_notifier(&pci_bus_type, &pwrctl->nb);
99 schedule_work(&pwrctl->work);
103 EXPORT_SYMBOL_GPL(pci_pwrctl_device_set_ready);
106 * pci_pwrctl_device_unset_ready() - Notify the pwrctl subsystem that the PCI
107 * device is about to be powered-down.
109 * @pwrctl: PCI power control data.
111 void pci_pwrctl_device_unset_ready(struct pci_pwrctl *pwrctl)
114 * We don't have to delete the link here. Typically, this function
115 * is only called when the power control device is being detached. If
116 * it is being detached then the child PCI device must have already
117 * been unbound too or the device core wouldn't let us unbind.
119 bus_unregister_notifier(&pci_bus_type, &pwrctl->nb);
121 EXPORT_SYMBOL_GPL(pci_pwrctl_device_unset_ready);
123 static void devm_pci_pwrctl_device_unset_ready(void *data)
125 struct pci_pwrctl *pwrctl = data;
127 pci_pwrctl_device_unset_ready(pwrctl);
131 * devm_pci_pwrctl_device_set_ready - Managed variant of
132 * pci_pwrctl_device_set_ready().
134 * @dev: Device managing this pwrctl provider.
135 * @pwrctl: PCI power control data.
138 * 0 on success, negative error number on error.
140 int devm_pci_pwrctl_device_set_ready(struct device *dev,
141 struct pci_pwrctl *pwrctl)
145 ret = pci_pwrctl_device_set_ready(pwrctl);
149 return devm_add_action_or_reset(dev,
150 devm_pci_pwrctl_device_unset_ready,
153 EXPORT_SYMBOL_GPL(devm_pci_pwrctl_device_set_ready);
156 MODULE_DESCRIPTION("PCI Device Power Control core driver");
157 MODULE_LICENSE("GPL");