1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2024 Linaro Ltd.
4 */
5
6 #define dev_fmt(fmt) "pwrctrl: " fmt
7
8 #include <linux/device.h>
9 #include <linux/export.h>
10 #include <linux/kernel.h>
11 #include <linux/of.h>
12 #include <linux/of_graph.h>
13 #include <linux/of_platform.h>
14 #include <linux/pci.h>
15 #include <linux/pci-pwrctrl.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/slab.h>
19
20 #include "../pci.h"
21
pci_pwrctrl_notify(struct notifier_block * nb,unsigned long action,void * data)22 static int pci_pwrctrl_notify(struct notifier_block *nb, unsigned long action,
23 void *data)
24 {
25 struct pci_pwrctrl *pwrctrl = container_of(nb, struct pci_pwrctrl, nb);
26 struct device *dev = data;
27
28 if (dev_fwnode(dev) != dev_fwnode(pwrctrl->dev))
29 return NOTIFY_DONE;
30
31 switch (action) {
32 case BUS_NOTIFY_ADD_DEVICE:
33 /*
34 * We will have two struct device objects bound to two different
35 * drivers on different buses but consuming the same DT node. We
36 * must not bind the pins twice in this case but only once for
37 * the first device to be added.
38 *
39 * If we got here then the PCI device is the second after the
40 * power control platform device. Mark its OF node as reused.
41 */
42 dev->of_node_reused = true;
43 break;
44 }
45
46 return NOTIFY_DONE;
47 }
48
49 /**
50 * pci_pwrctrl_init() - Initialize the PCI power control context struct
51 *
52 * @pwrctrl: PCI power control data
53 * @dev: Parent device
54 */
pci_pwrctrl_init(struct pci_pwrctrl * pwrctrl,struct device * dev)55 void pci_pwrctrl_init(struct pci_pwrctrl *pwrctrl, struct device *dev)
56 {
57 pwrctrl->dev = dev;
58 dev_set_drvdata(dev, pwrctrl);
59 }
60 EXPORT_SYMBOL_GPL(pci_pwrctrl_init);
61
62 /**
63 * pci_pwrctrl_device_set_ready() - Notify the pwrctrl subsystem that the PCI
64 * device is powered-up and ready to be detected.
65 *
66 * @pwrctrl: PCI power control data.
67 *
68 * Returns:
69 * 0 on success, negative error number on error.
70 *
71 * Note:
72 * This function returning 0 doesn't mean the device was detected. It means,
73 * that the bus rescan was successfully started. The device will get bound to
74 * its PCI driver asynchronously.
75 */
pci_pwrctrl_device_set_ready(struct pci_pwrctrl * pwrctrl)76 int pci_pwrctrl_device_set_ready(struct pci_pwrctrl *pwrctrl)
77 {
78 int ret;
79
80 if (!pwrctrl->dev)
81 return -ENODEV;
82
83 pwrctrl->nb.notifier_call = pci_pwrctrl_notify;
84 ret = bus_register_notifier(&pci_bus_type, &pwrctrl->nb);
85 if (ret)
86 return ret;
87
88 return 0;
89 }
90 EXPORT_SYMBOL_GPL(pci_pwrctrl_device_set_ready);
91
92 /**
93 * pci_pwrctrl_device_unset_ready() - Notify the pwrctrl subsystem that the PCI
94 * device is about to be powered-down.
95 *
96 * @pwrctrl: PCI power control data.
97 */
pci_pwrctrl_device_unset_ready(struct pci_pwrctrl * pwrctrl)98 void pci_pwrctrl_device_unset_ready(struct pci_pwrctrl *pwrctrl)
99 {
100 /*
101 * We don't have to delete the link here. Typically, this function
102 * is only called when the power control device is being detached. If
103 * it is being detached then the child PCI device must have already
104 * been unbound too or the device core wouldn't let us unbind.
105 */
106 bus_unregister_notifier(&pci_bus_type, &pwrctrl->nb);
107 }
108 EXPORT_SYMBOL_GPL(pci_pwrctrl_device_unset_ready);
109
devm_pci_pwrctrl_device_unset_ready(void * data)110 static void devm_pci_pwrctrl_device_unset_ready(void *data)
111 {
112 struct pci_pwrctrl *pwrctrl = data;
113
114 pci_pwrctrl_device_unset_ready(pwrctrl);
115 }
116
117 /**
118 * devm_pci_pwrctrl_device_set_ready - Managed variant of
119 * pci_pwrctrl_device_set_ready().
120 *
121 * @dev: Device managing this pwrctrl provider.
122 * @pwrctrl: PCI power control data.
123 *
124 * Returns:
125 * 0 on success, negative error number on error.
126 */
devm_pci_pwrctrl_device_set_ready(struct device * dev,struct pci_pwrctrl * pwrctrl)127 int devm_pci_pwrctrl_device_set_ready(struct device *dev,
128 struct pci_pwrctrl *pwrctrl)
129 {
130 int ret;
131
132 ret = pci_pwrctrl_device_set_ready(pwrctrl);
133 if (ret)
134 return ret;
135
136 return devm_add_action_or_reset(dev,
137 devm_pci_pwrctrl_device_unset_ready,
138 pwrctrl);
139 }
140 EXPORT_SYMBOL_GPL(devm_pci_pwrctrl_device_set_ready);
141
__pci_pwrctrl_power_off_device(struct device * dev)142 static int __pci_pwrctrl_power_off_device(struct device *dev)
143 {
144 struct pci_pwrctrl *pwrctrl = dev_get_drvdata(dev);
145
146 if (!pwrctrl)
147 return 0;
148
149 return pwrctrl->power_off(pwrctrl);
150 }
151
pci_pwrctrl_power_off_device(struct device_node * np)152 static void pci_pwrctrl_power_off_device(struct device_node *np)
153 {
154 struct platform_device *pdev;
155 int ret;
156
157 for_each_available_child_of_node_scoped(np, child)
158 pci_pwrctrl_power_off_device(child);
159
160 pdev = of_find_device_by_node(np);
161 if (!pdev)
162 return;
163
164 if (device_is_bound(&pdev->dev)) {
165 ret = __pci_pwrctrl_power_off_device(&pdev->dev);
166 if (ret)
167 dev_err(&pdev->dev, "Failed to power off device: %d", ret);
168 }
169
170 platform_device_put(pdev);
171 }
172
173 /**
174 * pci_pwrctrl_power_off_devices - Power off pwrctrl devices
175 *
176 * @parent: PCI host controller device
177 *
178 * Recursively traverse all pwrctrl devices for the devicetree hierarchy
179 * below the specified PCI host controller and power them off in a depth
180 * first manner.
181 */
pci_pwrctrl_power_off_devices(struct device * parent)182 void pci_pwrctrl_power_off_devices(struct device *parent)
183 {
184 struct device_node *np = parent->of_node;
185
186 for_each_available_child_of_node_scoped(np, child)
187 pci_pwrctrl_power_off_device(child);
188 }
189 EXPORT_SYMBOL_GPL(pci_pwrctrl_power_off_devices);
190
__pci_pwrctrl_power_on_device(struct device * dev)191 static int __pci_pwrctrl_power_on_device(struct device *dev)
192 {
193 struct pci_pwrctrl *pwrctrl = dev_get_drvdata(dev);
194
195 if (!pwrctrl)
196 return 0;
197
198 return pwrctrl->power_on(pwrctrl);
199 }
200
201 /*
202 * Power on the devices in a depth first manner. Before powering on the device,
203 * make sure its driver is bound.
204 */
pci_pwrctrl_power_on_device(struct device_node * np)205 static int pci_pwrctrl_power_on_device(struct device_node *np)
206 {
207 struct platform_device *pdev;
208 int ret;
209
210 for_each_available_child_of_node_scoped(np, child) {
211 ret = pci_pwrctrl_power_on_device(child);
212 if (ret)
213 return ret;
214 }
215
216 pdev = of_find_device_by_node(np);
217 if (!pdev)
218 return 0;
219
220 if (device_is_bound(&pdev->dev)) {
221 ret = __pci_pwrctrl_power_on_device(&pdev->dev);
222 } else {
223 /* FIXME: Use blocking wait instead of probe deferral */
224 dev_dbg(&pdev->dev, "driver is not bound\n");
225 ret = -EPROBE_DEFER;
226 }
227
228 platform_device_put(pdev);
229
230 return ret;
231 }
232
233 /**
234 * pci_pwrctrl_power_on_devices - Power on pwrctrl devices
235 *
236 * @parent: PCI host controller device
237 *
238 * Recursively traverse all pwrctrl devices for the devicetree hierarchy
239 * below the specified PCI host controller and power them on in a depth
240 * first manner. On error, all powered on devices will be powered off.
241 *
242 * Return: 0 on success, -EPROBE_DEFER if any pwrctrl driver is not bound, an
243 * appropriate error code otherwise.
244 */
pci_pwrctrl_power_on_devices(struct device * parent)245 int pci_pwrctrl_power_on_devices(struct device *parent)
246 {
247 struct device_node *np = parent->of_node;
248 struct device_node *child = NULL;
249 int ret;
250
251 for_each_available_child_of_node(np, child) {
252 ret = pci_pwrctrl_power_on_device(child);
253 if (ret)
254 goto err_power_off;
255 }
256
257 return 0;
258
259 err_power_off:
260 for_each_available_child_of_node_scoped(np, tmp) {
261 if (tmp == child)
262 break;
263 pci_pwrctrl_power_off_device(tmp);
264 }
265 of_node_put(child);
266
267 return ret;
268 }
269 EXPORT_SYMBOL_GPL(pci_pwrctrl_power_on_devices);
270
271 /*
272 * Check whether the pwrctrl device really needs to be created or not. The
273 * pwrctrl device will only be created if the node satisfies below requirements:
274 *
275 * 1. Presence of compatible property with "pci" prefix to match against the
276 * pwrctrl driver (AND)
277 * 2. At least one of the power supplies defined in the devicetree node of the
278 * device (OR) in the remote endpoint parent node to indicate pwrctrl
279 * requirement.
280 */
pci_pwrctrl_is_required(struct device_node * np)281 static bool pci_pwrctrl_is_required(struct device_node *np)
282 {
283 struct device_node *endpoint;
284 const char *compat;
285 int ret;
286
287 ret = of_property_read_string(np, "compatible", &compat);
288 if (ret < 0)
289 return false;
290
291 if (!strstarts(compat, "pci"))
292 return false;
293
294 if (of_pci_supply_present(np))
295 return true;
296
297 if (of_graph_is_present(np)) {
298 for_each_endpoint_of_node(np, endpoint) {
299 struct device_node *remote __free(device_node) =
300 of_graph_get_remote_port_parent(endpoint);
301 if (remote) {
302 if (of_pci_supply_present(remote)) {
303 of_node_put(endpoint);
304 return true;
305 }
306 }
307 }
308 }
309
310 return false;
311 }
312
pci_pwrctrl_create_device(struct device_node * np,struct device * parent)313 static int pci_pwrctrl_create_device(struct device_node *np,
314 struct device *parent)
315 {
316 struct platform_device *pdev;
317 int ret;
318
319 for_each_available_child_of_node_scoped(np, child) {
320 ret = pci_pwrctrl_create_device(child, parent);
321 if (ret)
322 return ret;
323 }
324
325 /* Bail out if the platform device is already available for the node */
326 pdev = of_find_device_by_node(np);
327 if (pdev) {
328 platform_device_put(pdev);
329 return 0;
330 }
331
332 if (!pci_pwrctrl_is_required(np)) {
333 dev_dbg(parent, "Skipping OF node: %s\n", np->name);
334 return 0;
335 }
336
337 /* Now create the pwrctrl device */
338 pdev = of_platform_device_create(np, NULL, parent);
339 if (!pdev) {
340 dev_err(parent, "Failed to create pwrctrl device for node: %s\n", np->name);
341 return -EINVAL;
342 }
343
344 return 0;
345 }
346
347 /**
348 * pci_pwrctrl_create_devices - Create pwrctrl devices
349 *
350 * @parent: PCI host controller device
351 *
352 * Recursively create pwrctrl devices for the devicetree hierarchy below
353 * the specified PCI host controller in a depth first manner. On error, all
354 * created devices will be destroyed.
355 *
356 * Return: 0 on success, negative error number on error.
357 */
pci_pwrctrl_create_devices(struct device * parent)358 int pci_pwrctrl_create_devices(struct device *parent)
359 {
360 int ret;
361
362 for_each_available_child_of_node_scoped(parent->of_node, child) {
363 ret = pci_pwrctrl_create_device(child, parent);
364 if (ret) {
365 pci_pwrctrl_destroy_devices(parent);
366 return ret;
367 }
368 }
369
370 return 0;
371 }
372 EXPORT_SYMBOL_GPL(pci_pwrctrl_create_devices);
373
pci_pwrctrl_destroy_device(struct device_node * np)374 static void pci_pwrctrl_destroy_device(struct device_node *np)
375 {
376 struct platform_device *pdev;
377
378 for_each_available_child_of_node_scoped(np, child)
379 pci_pwrctrl_destroy_device(child);
380
381 pdev = of_find_device_by_node(np);
382 if (!pdev)
383 return;
384
385 of_device_unregister(pdev);
386 platform_device_put(pdev);
387
388 of_node_clear_flag(np, OF_POPULATED);
389 }
390
391 /**
392 * pci_pwrctrl_destroy_devices - Destroy pwrctrl devices
393 *
394 * @parent: PCI host controller device
395 *
396 * Recursively destroy pwrctrl devices for the devicetree hierarchy below
397 * the specified PCI host controller in a depth first manner.
398 */
pci_pwrctrl_destroy_devices(struct device * parent)399 void pci_pwrctrl_destroy_devices(struct device *parent)
400 {
401 struct device_node *np = parent->of_node;
402
403 for_each_available_child_of_node_scoped(np, child)
404 pci_pwrctrl_destroy_device(child);
405 }
406 EXPORT_SYMBOL_GPL(pci_pwrctrl_destroy_devices);
407
408 MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
409 MODULE_DESCRIPTION("PCI Device Power Control core driver");
410 MODULE_LICENSE("GPL");
411