xref: /linux/drivers/pci/pwrctrl/core.c (revision 9db826206f9b7c3b5449848e79adea4756a1605a)
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->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  */
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 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 
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  */
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 
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  */
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  */
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 static int pci_pwrctrl_create_device(struct device_node *np,
272 				     struct device *parent)
273 {
274 	struct platform_device *pdev;
275 	int ret;
276 
277 	for_each_available_child_of_node_scoped(np, child) {
278 		ret = pci_pwrctrl_create_device(child, parent);
279 		if (ret)
280 			return ret;
281 	}
282 
283 	/* Bail out if the platform device is already available for the node */
284 	pdev = of_find_device_by_node(np);
285 	if (pdev) {
286 		platform_device_put(pdev);
287 		return 0;
288 	}
289 
290 	/*
291 	 * Sanity check to make sure that the node has the compatible property
292 	 * to allow driver binding.
293 	 */
294 	if (!of_property_present(np, "compatible"))
295 		return 0;
296 
297 	/*
298 	 * Check whether the pwrctrl device really needs to be created or not.
299 	 * This is decided based on at least one of the power supplies defined
300 	 * in the devicetree node of the device or the graph property.
301 	 */
302 	if (!of_pci_supply_present(np) && !of_graph_is_present(np)) {
303 		dev_dbg(parent, "Skipping OF node: %s\n", np->name);
304 		return 0;
305 	}
306 
307 	/* Now create the pwrctrl device */
308 	pdev = of_platform_device_create(np, NULL, parent);
309 	if (!pdev) {
310 		dev_err(parent, "Failed to create pwrctrl device for node: %s\n", np->name);
311 		return -EINVAL;
312 	}
313 
314 	return 0;
315 }
316 
317 /**
318  * pci_pwrctrl_create_devices - Create pwrctrl devices
319  *
320  * @parent: PCI host controller device
321  *
322  * Recursively create pwrctrl devices for the devicetree hierarchy below
323  * the specified PCI host controller in a depth first manner. On error, all
324  * created devices will be destroyed.
325  *
326  * Return: 0 on success, negative error number on error.
327  */
328 int pci_pwrctrl_create_devices(struct device *parent)
329 {
330 	int ret;
331 
332 	for_each_available_child_of_node_scoped(parent->of_node, child) {
333 		ret = pci_pwrctrl_create_device(child, parent);
334 		if (ret) {
335 			pci_pwrctrl_destroy_devices(parent);
336 			return ret;
337 		}
338 	}
339 
340 	return 0;
341 }
342 EXPORT_SYMBOL_GPL(pci_pwrctrl_create_devices);
343 
344 static void pci_pwrctrl_destroy_device(struct device_node *np)
345 {
346 	struct platform_device *pdev;
347 
348 	for_each_available_child_of_node_scoped(np, child)
349 		pci_pwrctrl_destroy_device(child);
350 
351 	pdev = of_find_device_by_node(np);
352 	if (!pdev)
353 		return;
354 
355 	of_device_unregister(pdev);
356 	platform_device_put(pdev);
357 
358 	of_node_clear_flag(np, OF_POPULATED);
359 }
360 
361 /**
362  * pci_pwrctrl_destroy_devices - Destroy pwrctrl devices
363  *
364  * @parent: PCI host controller device
365  *
366  * Recursively destroy pwrctrl devices for the devicetree hierarchy below
367  * the specified PCI host controller in a depth first manner.
368  */
369 void pci_pwrctrl_destroy_devices(struct device *parent)
370 {
371 	struct device_node *np = parent->of_node;
372 
373 	for_each_available_child_of_node_scoped(np, child)
374 		pci_pwrctrl_destroy_device(child);
375 }
376 EXPORT_SYMBOL_GPL(pci_pwrctrl_destroy_devices);
377 
378 MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
379 MODULE_DESCRIPTION("PCI Device Power Control core driver");
380 MODULE_LICENSE("GPL");
381