xref: /linux/drivers/pci/pwrctrl/core.c (revision 26ae421f7f49f8a6a32d15b1d21a782b46a1bad5)
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 
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_set_of_node_reused(dev);
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  */
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  */
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  */
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 
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  */
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 
142 /*
143  * Check whether the pwrctrl device really needs to be created or not. The
144  * pwrctrl device will only be created if the node satisfies below requirements:
145  *
146  * 1. Presence of compatible property with "pci" prefix to match against the
147  *    pwrctrl driver (AND)
148  * 2. At least one of the power supplies defined in the devicetree node of the
149  *    device (OR) in the remote endpoint parent node to indicate pwrctrl
150  *    requirement.
151  */
152 static bool pci_pwrctrl_is_required(struct device_node *np)
153 {
154 	struct device_node *endpoint;
155 	const char *compat;
156 	int ret;
157 
158 	ret = of_property_read_string(np, "compatible", &compat);
159 	if (ret < 0)
160 		return false;
161 
162 	if (!strstarts(compat, "pci"))
163 		return false;
164 
165 	if (of_pci_supply_present(np))
166 		return true;
167 
168 	if (of_graph_is_present(np)) {
169 		for_each_endpoint_of_node(np, endpoint) {
170 			struct device_node *remote __free(device_node) =
171 				of_graph_get_remote_port_parent(endpoint);
172 			if (remote) {
173 				if (of_pci_supply_present(remote)) {
174 					of_node_put(endpoint);
175 					return true;
176 				}
177 			}
178 		}
179 	}
180 
181 	return false;
182 }
183 
184 static int __pci_pwrctrl_power_off_device(struct device *dev)
185 {
186 	struct pci_pwrctrl *pwrctrl = dev_get_drvdata(dev);
187 
188 	if (!pwrctrl)
189 		return 0;
190 
191 	return pwrctrl->power_off(pwrctrl);
192 }
193 
194 static void pci_pwrctrl_power_off_device(struct device_node *np)
195 {
196 	struct platform_device *pdev;
197 	int ret;
198 
199 	for_each_available_child_of_node_scoped(np, child)
200 		pci_pwrctrl_power_off_device(child);
201 
202 	if (!pci_pwrctrl_is_required(np))
203 		return;
204 
205 	pdev = of_find_device_by_node(np);
206 	if (!pdev)
207 		return;
208 
209 	scoped_guard(device, &pdev->dev) {
210 		if (device_is_bound(&pdev->dev)) {
211 			ret = __pci_pwrctrl_power_off_device(&pdev->dev);
212 			if (ret)
213 				dev_err(&pdev->dev, "Failed to power off device: %d", ret);
214 		}
215 	}
216 
217 	platform_device_put(pdev);
218 }
219 
220 /**
221  * pci_pwrctrl_power_off_devices - Power off pwrctrl devices
222  *
223  * @parent: PCI host controller device
224  *
225  * Recursively traverse all pwrctrl devices for the devicetree hierarchy
226  * below the specified PCI host controller and power them off in a depth
227  * first manner.
228  */
229 void pci_pwrctrl_power_off_devices(struct device *parent)
230 {
231 	struct device_node *np = parent->of_node;
232 
233 	for_each_available_child_of_node_scoped(np, child)
234 		pci_pwrctrl_power_off_device(child);
235 }
236 EXPORT_SYMBOL_GPL(pci_pwrctrl_power_off_devices);
237 
238 static int __pci_pwrctrl_power_on_device(struct device *dev)
239 {
240 	struct pci_pwrctrl *pwrctrl = dev_get_drvdata(dev);
241 
242 	if (!pwrctrl)
243 		return 0;
244 
245 	return pwrctrl->power_on(pwrctrl);
246 }
247 
248 /*
249  * Power on the devices in a depth first manner. Before powering on the device,
250  * make sure its driver is bound.
251  */
252 static int pci_pwrctrl_power_on_device(struct device_node *np)
253 {
254 	struct platform_device *pdev;
255 	int ret = 0;
256 
257 	for_each_available_child_of_node_scoped(np, child) {
258 		ret = pci_pwrctrl_power_on_device(child);
259 		if (ret)
260 			return ret;
261 	}
262 
263 	if (!pci_pwrctrl_is_required(np))
264 		return 0;
265 
266 	pdev = of_find_device_by_node(np);
267 	if (!pdev)
268 		return 0;
269 
270 	scoped_guard(device, &pdev->dev) {
271 		if (device_is_bound(&pdev->dev)) {
272 			ret = __pci_pwrctrl_power_on_device(&pdev->dev);
273 		} else {
274 			/* FIXME: Use blocking wait instead of probe deferral */
275 			dev_dbg(&pdev->dev, "driver is not bound\n");
276 			ret = -EPROBE_DEFER;
277 		}
278 	}
279 
280 	platform_device_put(pdev);
281 
282 	return ret;
283 }
284 
285 /**
286  * pci_pwrctrl_power_on_devices - Power on pwrctrl devices
287  *
288  * @parent: PCI host controller device
289  *
290  * Recursively traverse all pwrctrl devices for the devicetree hierarchy
291  * below the specified PCI host controller and power them on in a depth
292  * first manner. On error, all powered on devices will be powered off.
293  *
294  * Return: 0 on success, -EPROBE_DEFER if any pwrctrl driver is not bound, an
295  * appropriate error code otherwise.
296  */
297 int pci_pwrctrl_power_on_devices(struct device *parent)
298 {
299 	struct device_node *np = parent->of_node;
300 	struct device_node *child = NULL;
301 	int ret;
302 
303 	for_each_available_child_of_node(np, child) {
304 		ret = pci_pwrctrl_power_on_device(child);
305 		if (ret)
306 			goto err_power_off;
307 	}
308 
309 	return 0;
310 
311 err_power_off:
312 	for_each_available_child_of_node_scoped(np, tmp) {
313 		if (tmp == child)
314 			break;
315 		pci_pwrctrl_power_off_device(tmp);
316 	}
317 	of_node_put(child);
318 
319 	return ret;
320 }
321 EXPORT_SYMBOL_GPL(pci_pwrctrl_power_on_devices);
322 
323 static int pci_pwrctrl_create_device(struct device_node *np,
324 				     struct device *parent)
325 {
326 	struct platform_device *pdev;
327 	int ret;
328 
329 	for_each_available_child_of_node_scoped(np, child) {
330 		ret = pci_pwrctrl_create_device(child, parent);
331 		if (ret)
332 			return ret;
333 	}
334 
335 	/* Bail out if the platform device is already available for the node */
336 	pdev = of_find_device_by_node(np);
337 	if (pdev) {
338 		platform_device_put(pdev);
339 		return 0;
340 	}
341 
342 	if (!pci_pwrctrl_is_required(np)) {
343 		dev_dbg(parent, "Skipping OF node: %s\n", np->name);
344 		return 0;
345 	}
346 
347 	/* Now create the pwrctrl device */
348 	pdev = of_platform_device_create(np, NULL, parent);
349 	if (!pdev) {
350 		dev_err(parent, "Failed to create pwrctrl device for node: %s\n", np->name);
351 		return -EINVAL;
352 	}
353 
354 	return 0;
355 }
356 
357 /**
358  * pci_pwrctrl_create_devices - Create pwrctrl devices
359  *
360  * @parent: PCI host controller device
361  *
362  * Recursively create pwrctrl devices for the devicetree hierarchy below
363  * the specified PCI host controller in a depth first manner. On error, all
364  * created devices will be destroyed.
365  *
366  * Return: 0 on success, negative error number on error.
367  */
368 int pci_pwrctrl_create_devices(struct device *parent)
369 {
370 	int ret;
371 
372 	for_each_available_child_of_node_scoped(parent->of_node, child) {
373 		ret = pci_pwrctrl_create_device(child, parent);
374 		if (ret) {
375 			pci_pwrctrl_destroy_devices(parent);
376 			return ret;
377 		}
378 	}
379 
380 	return 0;
381 }
382 EXPORT_SYMBOL_GPL(pci_pwrctrl_create_devices);
383 
384 static void pci_pwrctrl_destroy_device(struct device_node *np)
385 {
386 	struct platform_device *pdev;
387 
388 	for_each_available_child_of_node_scoped(np, child)
389 		pci_pwrctrl_destroy_device(child);
390 
391 	pdev = of_find_device_by_node(np);
392 	if (!pdev)
393 		return;
394 
395 	of_device_unregister(pdev);
396 	platform_device_put(pdev);
397 
398 	of_node_clear_flag(np, OF_POPULATED);
399 }
400 
401 /**
402  * pci_pwrctrl_destroy_devices - Destroy pwrctrl devices
403  *
404  * @parent: PCI host controller device
405  *
406  * Recursively destroy pwrctrl devices for the devicetree hierarchy below
407  * the specified PCI host controller in a depth first manner.
408  */
409 void pci_pwrctrl_destroy_devices(struct device *parent)
410 {
411 	struct device_node *np = parent->of_node;
412 
413 	for_each_available_child_of_node_scoped(np, child)
414 		pci_pwrctrl_destroy_device(child);
415 }
416 EXPORT_SYMBOL_GPL(pci_pwrctrl_destroy_devices);
417 
418 MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
419 MODULE_DESCRIPTION("PCI Device Power Control core driver");
420 MODULE_LICENSE("GPL");
421