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-pwrctrl.h>
11 #include <linux/property.h>
12 #include <linux/slab.h>
14 static int pci_pwrctrl_notify(struct notifier_block
*nb
, unsigned long action
,
17 struct pci_pwrctrl
*pwrctrl
= container_of(nb
, struct pci_pwrctrl
, nb
);
18 struct device
*dev
= data
;
20 if (dev_fwnode(dev
) != dev_fwnode(pwrctrl
->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;
41 static void rescan_work_func(struct work_struct
*work
)
43 struct pci_pwrctrl
*pwrctrl
= container_of(work
,
44 struct pci_pwrctrl
, work
);
46 pci_lock_rescan_remove();
47 pci_rescan_bus(to_pci_dev(pwrctrl
->dev
->parent
)->bus
);
48 pci_unlock_rescan_remove();
52 * pci_pwrctrl_init() - Initialize the PCI power control context struct
54 * @pwrctrl: PCI power control data
57 void pci_pwrctrl_init(struct pci_pwrctrl
*pwrctrl
, struct device
*dev
)
60 INIT_WORK(&pwrctrl
->work
, rescan_work_func
);
62 EXPORT_SYMBOL_GPL(pci_pwrctrl_init
);
65 * pci_pwrctrl_device_set_ready() - Notify the pwrctrl subsystem that the PCI
66 * device is powered-up and ready to be detected.
68 * @pwrctrl: PCI power control data.
71 * 0 on success, negative error number on error.
74 * This function returning 0 doesn't mean the device was detected. It means,
75 * that the bus rescan was successfully started. The device will get bound to
76 * its PCI driver asynchronously.
78 int pci_pwrctrl_device_set_ready(struct pci_pwrctrl
*pwrctrl
)
85 pwrctrl
->nb
.notifier_call
= pci_pwrctrl_notify
;
86 ret
= bus_register_notifier(&pci_bus_type
, &pwrctrl
->nb
);
90 schedule_work(&pwrctrl
->work
);
94 EXPORT_SYMBOL_GPL(pci_pwrctrl_device_set_ready
);
97 * pci_pwrctrl_device_unset_ready() - Notify the pwrctrl subsystem that the PCI
98 * device is about to be powered-down.
100 * @pwrctrl: PCI power control data.
102 void pci_pwrctrl_device_unset_ready(struct pci_pwrctrl
*pwrctrl
)
105 * We don't have to delete the link here. Typically, this function
106 * is only called when the power control device is being detached. If
107 * it is being detached then the child PCI device must have already
108 * been unbound too or the device core wouldn't let us unbind.
110 bus_unregister_notifier(&pci_bus_type
, &pwrctrl
->nb
);
112 EXPORT_SYMBOL_GPL(pci_pwrctrl_device_unset_ready
);
114 static void devm_pci_pwrctrl_device_unset_ready(void *data
)
116 struct pci_pwrctrl
*pwrctrl
= data
;
118 pci_pwrctrl_device_unset_ready(pwrctrl
);
122 * devm_pci_pwrctrl_device_set_ready - Managed variant of
123 * pci_pwrctrl_device_set_ready().
125 * @dev: Device managing this pwrctrl provider.
126 * @pwrctrl: PCI power control data.
129 * 0 on success, negative error number on error.
131 int devm_pci_pwrctrl_device_set_ready(struct device
*dev
,
132 struct pci_pwrctrl
*pwrctrl
)
136 ret
= pci_pwrctrl_device_set_ready(pwrctrl
);
140 return devm_add_action_or_reset(dev
,
141 devm_pci_pwrctrl_device_unset_ready
,
144 EXPORT_SYMBOL_GPL(devm_pci_pwrctrl_device_set_ready
);
146 MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
147 MODULE_DESCRIPTION("PCI Device Power Control core driver");
148 MODULE_LICENSE("GPL");