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
pci_pwrctl_notify(struct notifier_block * nb,unsigned long action,void * data)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
rescan_work_func(struct work_struct * work)51 static void rescan_work_func(struct work_struct *work)
52 {
53 struct pci_pwrctl *pwrctl = container_of(work, struct pci_pwrctl, work);
54
55 pci_lock_rescan_remove();
56 pci_rescan_bus(to_pci_dev(pwrctl->dev->parent)->bus);
57 pci_unlock_rescan_remove();
58 }
59
60 /**
61 * pci_pwrctl_init() - Initialize the PCI power control context struct
62 *
63 * @pwrctl: PCI power control data
64 * @dev: Parent device
65 */
pci_pwrctl_init(struct pci_pwrctl * pwrctl,struct device * dev)66 void pci_pwrctl_init(struct pci_pwrctl *pwrctl, struct device *dev)
67 {
68 pwrctl->dev = dev;
69 INIT_WORK(&pwrctl->work, rescan_work_func);
70 }
71 EXPORT_SYMBOL_GPL(pci_pwrctl_init);
72
73 /**
74 * pci_pwrctl_device_set_ready() - Notify the pwrctl subsystem that the PCI
75 * device is powered-up and ready to be detected.
76 *
77 * @pwrctl: PCI power control data.
78 *
79 * Returns:
80 * 0 on success, negative error number on error.
81 *
82 * Note:
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.
86 */
pci_pwrctl_device_set_ready(struct pci_pwrctl * pwrctl)87 int pci_pwrctl_device_set_ready(struct pci_pwrctl *pwrctl)
88 {
89 int ret;
90
91 if (!pwrctl->dev)
92 return -ENODEV;
93
94 pwrctl->nb.notifier_call = pci_pwrctl_notify;
95 ret = bus_register_notifier(&pci_bus_type, &pwrctl->nb);
96 if (ret)
97 return ret;
98
99 schedule_work(&pwrctl->work);
100
101 return 0;
102 }
103 EXPORT_SYMBOL_GPL(pci_pwrctl_device_set_ready);
104
105 /**
106 * pci_pwrctl_device_unset_ready() - Notify the pwrctl subsystem that the PCI
107 * device is about to be powered-down.
108 *
109 * @pwrctl: PCI power control data.
110 */
pci_pwrctl_device_unset_ready(struct pci_pwrctl * pwrctl)111 void pci_pwrctl_device_unset_ready(struct pci_pwrctl *pwrctl)
112 {
113 /*
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.
118 */
119 bus_unregister_notifier(&pci_bus_type, &pwrctl->nb);
120 }
121 EXPORT_SYMBOL_GPL(pci_pwrctl_device_unset_ready);
122
devm_pci_pwrctl_device_unset_ready(void * data)123 static void devm_pci_pwrctl_device_unset_ready(void *data)
124 {
125 struct pci_pwrctl *pwrctl = data;
126
127 pci_pwrctl_device_unset_ready(pwrctl);
128 }
129
130 /**
131 * devm_pci_pwrctl_device_set_ready - Managed variant of
132 * pci_pwrctl_device_set_ready().
133 *
134 * @dev: Device managing this pwrctl provider.
135 * @pwrctl: PCI power control data.
136 *
137 * Returns:
138 * 0 on success, negative error number on error.
139 */
devm_pci_pwrctl_device_set_ready(struct device * dev,struct pci_pwrctl * pwrctl)140 int devm_pci_pwrctl_device_set_ready(struct device *dev,
141 struct pci_pwrctl *pwrctl)
142 {
143 int ret;
144
145 ret = pci_pwrctl_device_set_ready(pwrctl);
146 if (ret)
147 return ret;
148
149 return devm_add_action_or_reset(dev,
150 devm_pci_pwrctl_device_unset_ready,
151 pwrctl);
152 }
153 EXPORT_SYMBOL_GPL(devm_pci_pwrctl_device_set_ready);
154
155 MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
156 MODULE_DESCRIPTION("PCI Device Power Control core driver");
157 MODULE_LICENSE("GPL");
158