xref: /linux/drivers/of/platform.c (revision c27dfca4555bf74dd7dd7161d8ef2790ec1c7283)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *    Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
4  *			 <benh@kernel.crashing.org>
5  *    and		 Arnd Bergmann, IBM Corp.
6  *    Merged from powerpc/kernel/of_platform.c and
7  *    sparc{,64}/kernel/of_device.c by Stephen Rothwell
8  */
9 
10 #define pr_fmt(fmt)	"OF: " fmt
11 
12 #include <linux/errno.h>
13 #include <linux/module.h>
14 #include <linux/amba/bus.h>
15 #include <linux/device.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/slab.h>
18 #include <linux/of_address.h>
19 #include <linux/of_device.h>
20 #include <linux/of_irq.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 
24 #include "of_private.h"
25 
26 const struct of_device_id of_default_bus_match_table[] = {
27 	{ .compatible = "simple-bus", },
28 	{ .compatible = "simple-mfd", },
29 	{ .compatible = "isa", },
30 #ifdef CONFIG_ARM_AMBA
31 	{ .compatible = "arm,amba-bus", },
32 #endif /* CONFIG_ARM_AMBA */
33 	{} /* Empty terminated list */
34 };
35 
36 /**
37  * of_find_device_by_node - Find the platform_device associated with a node
38  * @np: Pointer to device tree node
39  *
40  * Takes a reference to the embedded struct device which needs to be dropped
41  * after use.
42  *
43  * Return: platform_device pointer, or NULL if not found
44  */
45 struct platform_device *of_find_device_by_node(struct device_node *np)
46 {
47 	struct device *dev;
48 
49 	dev = bus_find_device_by_of_node(&platform_bus_type, np);
50 	return dev ? to_platform_device(dev) : NULL;
51 }
52 EXPORT_SYMBOL(of_find_device_by_node);
53 
54 int of_device_add(struct platform_device *ofdev)
55 {
56 	BUG_ON(ofdev->dev.of_node == NULL);
57 
58 	/* name and id have to be set so that the platform bus doesn't get
59 	 * confused on matching */
60 	ofdev->name = dev_name(&ofdev->dev);
61 	ofdev->id = PLATFORM_DEVID_NONE;
62 
63 	/*
64 	 * If this device has not binding numa node in devicetree, that is
65 	 * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this
66 	 * device is on the same node as the parent.
67 	 */
68 	set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
69 
70 	return device_add(&ofdev->dev);
71 }
72 
73 int of_device_register(struct platform_device *pdev)
74 {
75 	device_initialize(&pdev->dev);
76 	return of_device_add(pdev);
77 }
78 EXPORT_SYMBOL(of_device_register);
79 
80 void of_device_unregister(struct platform_device *ofdev)
81 {
82 	device_unregister(&ofdev->dev);
83 }
84 EXPORT_SYMBOL(of_device_unregister);
85 
86 #ifdef CONFIG_OF_ADDRESS
87 static const struct of_device_id of_skipped_node_table[] = {
88 	{ .compatible = "operating-points-v2", },
89 	{} /* Empty terminated list */
90 };
91 
92 /*
93  * The following routines scan a subtree and registers a device for
94  * each applicable node.
95  *
96  * Note: sparc doesn't use these routines because it has a different
97  * mechanism for creating devices from device tree nodes.
98  */
99 
100 /**
101  * of_device_alloc - Allocate and initialize an of_device
102  * @np: device node to assign to device
103  * @bus_id: Name to assign to the device.  May be null to use default name.
104  * @parent: Parent device.
105  */
106 struct platform_device *of_device_alloc(struct device_node *np,
107 				  const char *bus_id,
108 				  struct device *parent)
109 {
110 	struct platform_device *dev;
111 	int rc, i, num_reg = 0;
112 	struct resource *res;
113 
114 	dev = platform_device_alloc("", PLATFORM_DEVID_NONE);
115 	if (!dev)
116 		return NULL;
117 
118 	/* count the io resources */
119 	num_reg = of_address_count(np);
120 
121 	/* Populate the resource table */
122 	if (num_reg) {
123 		res = kcalloc(num_reg, sizeof(*res), GFP_KERNEL);
124 		if (!res) {
125 			platform_device_put(dev);
126 			return NULL;
127 		}
128 
129 		dev->num_resources = num_reg;
130 		dev->resource = res;
131 		for (i = 0; i < num_reg; i++, res++) {
132 			rc = of_address_to_resource(np, i, res);
133 			WARN_ON(rc);
134 		}
135 	}
136 
137 	/* setup generic device info */
138 	device_set_node(&dev->dev, of_fwnode_handle(of_node_get(np)));
139 	dev->dev.parent = parent ? : &platform_bus;
140 
141 	if (bus_id)
142 		dev_set_name(&dev->dev, "%s", bus_id);
143 	else
144 		of_device_make_bus_id(&dev->dev);
145 
146 	return dev;
147 }
148 EXPORT_SYMBOL(of_device_alloc);
149 
150 /**
151  * of_platform_device_create_pdata - Alloc, initialize and register an of_device
152  * @np: pointer to node to create device for
153  * @bus_id: name to assign device
154  * @platform_data: pointer to populate platform_data pointer with
155  * @parent: Linux device model parent device.
156  *
157  * Return: Pointer to created platform device, or NULL if a device was not
158  * registered.  Unavailable devices will not get registered.
159  */
160 static struct platform_device *of_platform_device_create_pdata(
161 					struct device_node *np,
162 					const char *bus_id,
163 					void *platform_data,
164 					struct device *parent)
165 {
166 	struct platform_device *dev;
167 
168 	if (!of_device_is_available(np) ||
169 	    of_node_test_and_set_flag(np, OF_POPULATED))
170 		return NULL;
171 
172 	dev = of_device_alloc(np, bus_id, parent);
173 	if (!dev)
174 		goto err_clear_flag;
175 
176 	dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
177 	if (!dev->dev.dma_mask)
178 		dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
179 	dev->dev.bus = &platform_bus_type;
180 	dev->dev.platform_data = platform_data;
181 	of_msi_configure(&dev->dev, dev->dev.of_node);
182 
183 	if (of_device_add(dev) != 0) {
184 		platform_device_put(dev);
185 		goto err_clear_flag;
186 	}
187 
188 	return dev;
189 
190 err_clear_flag:
191 	of_node_clear_flag(np, OF_POPULATED);
192 	return NULL;
193 }
194 
195 /**
196  * of_platform_device_create - Alloc, initialize and register an of_device
197  * @np: pointer to node to create device for
198  * @bus_id: name to assign device
199  * @parent: Linux device model parent device.
200  *
201  * Return: Pointer to created platform device, or NULL if a device was not
202  * registered.  Unavailable devices will not get registered.
203  */
204 struct platform_device *of_platform_device_create(struct device_node *np,
205 					    const char *bus_id,
206 					    struct device *parent)
207 {
208 	return of_platform_device_create_pdata(np, bus_id, NULL, parent);
209 }
210 EXPORT_SYMBOL(of_platform_device_create);
211 
212 #ifdef CONFIG_ARM_AMBA
213 static struct amba_device *of_amba_device_create(struct device_node *node,
214 						 const char *bus_id,
215 						 void *platform_data,
216 						 struct device *parent)
217 {
218 	struct amba_device *dev;
219 	int ret;
220 
221 	pr_debug("Creating amba device %pOF\n", node);
222 
223 	if (!of_device_is_available(node) ||
224 	    of_node_test_and_set_flag(node, OF_POPULATED))
225 		return NULL;
226 
227 	dev = amba_device_alloc(NULL, 0, 0);
228 	if (!dev)
229 		goto err_clear_flag;
230 
231 	/* AMBA devices only support a single DMA mask */
232 	dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
233 	dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
234 
235 	/* setup generic device info */
236 	device_set_node(&dev->dev, of_fwnode_handle(node));
237 	dev->dev.parent = parent ? : &platform_bus;
238 	dev->dev.platform_data = platform_data;
239 	if (bus_id)
240 		dev_set_name(&dev->dev, "%s", bus_id);
241 	else
242 		of_device_make_bus_id(&dev->dev);
243 
244 	/* Allow the HW Peripheral ID to be overridden */
245 	of_property_read_u32(node, "arm,primecell-periphid", &dev->periphid);
246 
247 	ret = of_address_to_resource(node, 0, &dev->res);
248 	if (ret) {
249 		pr_err("amba: of_address_to_resource() failed (%d) for %pOF\n",
250 		       ret, node);
251 		goto err_free;
252 	}
253 
254 	ret = amba_device_add(dev, &iomem_resource);
255 	if (ret) {
256 		pr_err("amba_device_add() failed (%d) for %pOF\n",
257 		       ret, node);
258 		goto err_free;
259 	}
260 
261 	return dev;
262 
263 err_free:
264 	amba_device_put(dev);
265 err_clear_flag:
266 	of_node_clear_flag(node, OF_POPULATED);
267 	return NULL;
268 }
269 #else /* CONFIG_ARM_AMBA */
270 static struct amba_device *of_amba_device_create(struct device_node *node,
271 						 const char *bus_id,
272 						 void *platform_data,
273 						 struct device *parent)
274 {
275 	return NULL;
276 }
277 #endif /* CONFIG_ARM_AMBA */
278 
279 /*
280  * of_dev_lookup() - Given a device node, lookup the preferred Linux name
281  */
282 static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
283 				 struct device_node *np)
284 {
285 	const struct of_dev_auxdata *auxdata;
286 	struct resource res;
287 	int compatible = 0;
288 
289 	if (!lookup)
290 		return NULL;
291 
292 	auxdata = lookup;
293 	for (; auxdata->compatible; auxdata++) {
294 		if (!of_device_is_compatible(np, auxdata->compatible))
295 			continue;
296 		compatible++;
297 		if (!of_address_to_resource(np, 0, &res))
298 			if (res.start != auxdata->phys_addr)
299 				continue;
300 		pr_debug("%pOF: devname=%s\n", np, auxdata->name);
301 		return auxdata;
302 	}
303 
304 	if (!compatible)
305 		return NULL;
306 
307 	/* Try compatible match if no phys_addr and name are specified */
308 	auxdata = lookup;
309 	for (; auxdata->compatible; auxdata++) {
310 		if (!of_device_is_compatible(np, auxdata->compatible))
311 			continue;
312 		if (!auxdata->phys_addr && !auxdata->name) {
313 			pr_debug("%pOF: compatible match\n", np);
314 			return auxdata;
315 		}
316 	}
317 
318 	return NULL;
319 }
320 
321 /**
322  * of_platform_bus_create() - Create a device for a node and its children.
323  * @bus: device node of the bus to instantiate
324  * @matches: match table for bus nodes
325  * @lookup: auxdata table for matching id and platform_data with device nodes
326  * @parent: parent for new device, or NULL for top level.
327  * @strict: require compatible property
328  *
329  * Creates a platform_device for the provided device_node, and optionally
330  * recursively create devices for all the child nodes.
331  */
332 static int of_platform_bus_create(struct device_node *bus,
333 				  const struct of_device_id *matches,
334 				  const struct of_dev_auxdata *lookup,
335 				  struct device *parent, bool strict)
336 {
337 	const struct of_dev_auxdata *auxdata;
338 	struct device_node *child;
339 	struct platform_device *dev;
340 	const char *bus_id = NULL;
341 	void *platform_data = NULL;
342 	int rc = 0;
343 
344 	/* Make sure it has a compatible property */
345 	if (strict && (!of_get_property(bus, "compatible", NULL))) {
346 		pr_debug("%s() - skipping %pOF, no compatible prop\n",
347 			 __func__, bus);
348 		return 0;
349 	}
350 
351 	/* Skip nodes for which we don't want to create devices */
352 	if (unlikely(of_match_node(of_skipped_node_table, bus))) {
353 		pr_debug("%s() - skipping %pOF node\n", __func__, bus);
354 		return 0;
355 	}
356 
357 	if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
358 		pr_debug("%s() - skipping %pOF, already populated\n",
359 			__func__, bus);
360 		return 0;
361 	}
362 
363 	auxdata = of_dev_lookup(lookup, bus);
364 	if (auxdata) {
365 		bus_id = auxdata->name;
366 		platform_data = auxdata->platform_data;
367 	}
368 
369 	if (of_device_is_compatible(bus, "arm,primecell")) {
370 		/*
371 		 * Don't return an error here to keep compatibility with older
372 		 * device tree files.
373 		 */
374 		of_amba_device_create(bus, bus_id, platform_data, parent);
375 		return 0;
376 	}
377 
378 	dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
379 	if (!dev || !of_match_node(matches, bus))
380 		return 0;
381 
382 	for_each_child_of_node(bus, child) {
383 		pr_debug("   create child: %pOF\n", child);
384 		rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
385 		if (rc) {
386 			of_node_put(child);
387 			break;
388 		}
389 	}
390 	of_node_set_flag(bus, OF_POPULATED_BUS);
391 	return rc;
392 }
393 
394 /**
395  * of_platform_bus_probe() - Probe the device-tree for platform buses
396  * @root: parent of the first level to probe or NULL for the root of the tree
397  * @matches: match table for bus nodes
398  * @parent: parent to hook devices from, NULL for toplevel
399  *
400  * Note that children of the provided root are not instantiated as devices
401  * unless the specified root itself matches the bus list and is not NULL.
402  */
403 int of_platform_bus_probe(struct device_node *root,
404 			  const struct of_device_id *matches,
405 			  struct device *parent)
406 {
407 	struct device_node *child;
408 	int rc = 0;
409 
410 	root = root ? of_node_get(root) : of_find_node_by_path("/");
411 	if (!root)
412 		return -EINVAL;
413 
414 	pr_debug("%s()\n", __func__);
415 	pr_debug(" starting at: %pOF\n", root);
416 
417 	/* Do a self check of bus type, if there's a match, create children */
418 	if (of_match_node(matches, root)) {
419 		rc = of_platform_bus_create(root, matches, NULL, parent, false);
420 	} else for_each_child_of_node(root, child) {
421 		if (!of_match_node(matches, child))
422 			continue;
423 		rc = of_platform_bus_create(child, matches, NULL, parent, false);
424 		if (rc) {
425 			of_node_put(child);
426 			break;
427 		}
428 	}
429 
430 	of_node_put(root);
431 	return rc;
432 }
433 EXPORT_SYMBOL(of_platform_bus_probe);
434 
435 /**
436  * of_platform_populate() - Populate platform_devices from device tree data
437  * @root: parent of the first level to probe or NULL for the root of the tree
438  * @matches: match table, NULL to use the default
439  * @lookup: auxdata table for matching id and platform_data with device nodes
440  * @parent: parent to hook devices from, NULL for toplevel
441  *
442  * Similar to of_platform_bus_probe(), this function walks the device tree
443  * and creates devices from nodes.  It differs in that it follows the modern
444  * convention of requiring all device nodes to have a 'compatible' property,
445  * and it is suitable for creating devices which are children of the root
446  * node (of_platform_bus_probe will only create children of the root which
447  * are selected by the @matches argument).
448  *
449  * New board support should be using this function instead of
450  * of_platform_bus_probe().
451  *
452  * Return: 0 on success, < 0 on failure.
453  */
454 int of_platform_populate(struct device_node *root,
455 			const struct of_device_id *matches,
456 			const struct of_dev_auxdata *lookup,
457 			struct device *parent)
458 {
459 	struct device_node *child;
460 	int rc = 0;
461 
462 	root = root ? of_node_get(root) : of_find_node_by_path("/");
463 	if (!root)
464 		return -EINVAL;
465 
466 	pr_debug("%s()\n", __func__);
467 	pr_debug(" starting at: %pOF\n", root);
468 
469 	device_links_supplier_sync_state_pause();
470 	for_each_child_of_node(root, child) {
471 		rc = of_platform_bus_create(child, matches, lookup, parent, true);
472 		if (rc) {
473 			of_node_put(child);
474 			break;
475 		}
476 	}
477 	device_links_supplier_sync_state_resume();
478 
479 	of_node_set_flag(root, OF_POPULATED_BUS);
480 
481 	of_node_put(root);
482 	return rc;
483 }
484 EXPORT_SYMBOL_GPL(of_platform_populate);
485 
486 int of_platform_default_populate(struct device_node *root,
487 				 const struct of_dev_auxdata *lookup,
488 				 struct device *parent)
489 {
490 	return of_platform_populate(root, of_default_bus_match_table, lookup,
491 				    parent);
492 }
493 EXPORT_SYMBOL_GPL(of_platform_default_populate);
494 
495 static const struct of_device_id reserved_mem_matches[] = {
496 	{ .compatible = "phram" },
497 	{ .compatible = "qcom,rmtfs-mem" },
498 	{ .compatible = "qcom,cmd-db" },
499 	{ .compatible = "qcom,smem" },
500 	{ .compatible = "ramoops" },
501 	{ .compatible = "nvmem-rmem" },
502 	{ .compatible = "google,open-dice" },
503 	{}
504 };
505 
506 static int __init of_platform_default_populate_init(void)
507 {
508 	struct device_node *node;
509 
510 	device_links_supplier_sync_state_pause();
511 
512 	if (!of_have_populated_dt())
513 		return -ENODEV;
514 
515 	if (IS_ENABLED(CONFIG_PPC)) {
516 		struct device_node *boot_display = NULL;
517 		struct platform_device *dev;
518 		int display_number = 0;
519 		int ret;
520 
521 		/* Check if we have a MacOS display without a node spec */
522 		if (of_property_present(of_chosen, "linux,bootx-noscreen")) {
523 			/*
524 			 * The old code tried to work out which node was the MacOS
525 			 * display based on the address. I'm dropping that since the
526 			 * lack of a node spec only happens with old BootX versions
527 			 * (users can update) and with this code, they'll still get
528 			 * a display (just not the palette hacks).
529 			 */
530 			dev = platform_device_alloc("bootx-noscreen", 0);
531 			if (WARN_ON(!dev))
532 				return -ENOMEM;
533 			ret = platform_device_add(dev);
534 			if (WARN_ON(ret)) {
535 				platform_device_put(dev);
536 				return ret;
537 			}
538 		}
539 
540 		/*
541 		 * For OF framebuffers, first create the device for the boot display,
542 		 * then for the other framebuffers. Only fail for the boot display;
543 		 * ignore errors for the rest.
544 		 */
545 		for_each_node_by_type(node, "display") {
546 			if (!of_get_property(node, "linux,opened", NULL) ||
547 			    !of_get_property(node, "linux,boot-display", NULL))
548 				continue;
549 			dev = of_platform_device_create(node, "of-display", NULL);
550 			of_node_put(node);
551 			if (WARN_ON(!dev))
552 				return -ENOMEM;
553 			boot_display = node;
554 			display_number++;
555 			break;
556 		}
557 		for_each_node_by_type(node, "display") {
558 			char buf[14];
559 			const char *of_display_format = "of-display.%d";
560 
561 			if (!of_get_property(node, "linux,opened", NULL) || node == boot_display)
562 				continue;
563 			ret = snprintf(buf, sizeof(buf), of_display_format, display_number++);
564 			if (ret < sizeof(buf))
565 				of_platform_device_create(node, buf, NULL);
566 		}
567 
568 	} else {
569 		/*
570 		 * Handle certain compatibles explicitly, since we don't want to create
571 		 * platform_devices for every node in /reserved-memory with a
572 		 * "compatible",
573 		 */
574 		for_each_matching_node(node, reserved_mem_matches)
575 			of_platform_device_create(node, NULL, NULL);
576 
577 		node = of_find_node_by_path("/firmware");
578 		if (node) {
579 			of_platform_populate(node, NULL, NULL, NULL);
580 			of_node_put(node);
581 		}
582 
583 		node = of_get_compatible_child(of_chosen, "simple-framebuffer");
584 		of_platform_device_create(node, NULL, NULL);
585 		of_node_put(node);
586 
587 		/* Populate everything else. */
588 		of_platform_default_populate(NULL, NULL, NULL);
589 	}
590 
591 	return 0;
592 }
593 arch_initcall_sync(of_platform_default_populate_init);
594 
595 static int __init of_platform_sync_state_init(void)
596 {
597 	device_links_supplier_sync_state_resume();
598 	return 0;
599 }
600 late_initcall_sync(of_platform_sync_state_init);
601 
602 int of_platform_device_destroy(struct device *dev, void *data)
603 {
604 	/* Do not touch devices not populated from the device tree */
605 	if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
606 		return 0;
607 
608 	/* Recurse for any nodes that were treated as busses */
609 	if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
610 		device_for_each_child(dev, NULL, of_platform_device_destroy);
611 
612 	of_node_clear_flag(dev->of_node, OF_POPULATED);
613 	of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
614 
615 	if (dev->bus == &platform_bus_type)
616 		platform_device_unregister(to_platform_device(dev));
617 #ifdef CONFIG_ARM_AMBA
618 	else if (dev->bus == &amba_bustype)
619 		amba_device_unregister(to_amba_device(dev));
620 #endif
621 
622 	return 0;
623 }
624 EXPORT_SYMBOL_GPL(of_platform_device_destroy);
625 
626 /**
627  * of_platform_depopulate() - Remove devices populated from device tree
628  * @parent: device which children will be removed
629  *
630  * Complementary to of_platform_populate(), this function removes children
631  * of the given device (and, recurrently, their children) that have been
632  * created from their respective device tree nodes (and only those,
633  * leaving others - eg. manually created - unharmed).
634  */
635 void of_platform_depopulate(struct device *parent)
636 {
637 	if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
638 		device_for_each_child_reverse(parent, NULL, of_platform_device_destroy);
639 		of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
640 	}
641 }
642 EXPORT_SYMBOL_GPL(of_platform_depopulate);
643 
644 static void devm_of_platform_populate_release(struct device *dev, void *res)
645 {
646 	of_platform_depopulate(*(struct device **)res);
647 }
648 
649 /**
650  * devm_of_platform_populate() - Populate platform_devices from device tree data
651  * @dev: device that requested to populate from device tree data
652  *
653  * Similar to of_platform_populate(), but will automatically call
654  * of_platform_depopulate() when the device is unbound from the bus.
655  *
656  * Return: 0 on success, < 0 on failure.
657  */
658 int devm_of_platform_populate(struct device *dev)
659 {
660 	struct device **ptr;
661 	int ret;
662 
663 	if (!dev)
664 		return -EINVAL;
665 
666 	ptr = devres_alloc(devm_of_platform_populate_release,
667 			   sizeof(*ptr), GFP_KERNEL);
668 	if (!ptr)
669 		return -ENOMEM;
670 
671 	ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
672 	if (ret) {
673 		devres_free(ptr);
674 	} else {
675 		*ptr = dev;
676 		devres_add(dev, ptr);
677 	}
678 
679 	return ret;
680 }
681 EXPORT_SYMBOL_GPL(devm_of_platform_populate);
682 
683 static int devm_of_platform_match(struct device *dev, void *res, void *data)
684 {
685 	struct device **ptr = res;
686 
687 	if (!ptr) {
688 		WARN_ON(!ptr);
689 		return 0;
690 	}
691 
692 	return *ptr == data;
693 }
694 
695 /**
696  * devm_of_platform_depopulate() - Remove devices populated from device tree
697  * @dev: device that requested to depopulate from device tree data
698  *
699  * Complementary to devm_of_platform_populate(), this function removes children
700  * of the given device (and, recurrently, their children) that have been
701  * created from their respective device tree nodes (and only those,
702  * leaving others - eg. manually created - unharmed).
703  */
704 void devm_of_platform_depopulate(struct device *dev)
705 {
706 	int ret;
707 
708 	ret = devres_release(dev, devm_of_platform_populate_release,
709 			     devm_of_platform_match, dev);
710 
711 	WARN_ON(ret);
712 }
713 EXPORT_SYMBOL_GPL(devm_of_platform_depopulate);
714 
715 #ifdef CONFIG_OF_DYNAMIC
716 static int of_platform_notify(struct notifier_block *nb,
717 				unsigned long action, void *arg)
718 {
719 	struct of_reconfig_data *rd = arg;
720 	struct platform_device *pdev_parent, *pdev;
721 	bool children_left;
722 
723 	switch (of_reconfig_get_state_change(action, rd)) {
724 	case OF_RECONFIG_CHANGE_ADD:
725 		/* verify that the parent is a bus */
726 		if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
727 			return NOTIFY_OK;	/* not for us */
728 
729 		/* already populated? (driver using of_populate manually) */
730 		if (of_node_check_flag(rd->dn, OF_POPULATED))
731 			return NOTIFY_OK;
732 
733 		/*
734 		 * Clear the flag before adding the device so that fw_devlink
735 		 * doesn't skip adding consumers to this device.
736 		 */
737 		rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
738 		/* pdev_parent may be NULL when no bus platform device */
739 		pdev_parent = of_find_device_by_node(rd->dn->parent);
740 		pdev = of_platform_device_create(rd->dn, NULL,
741 				pdev_parent ? &pdev_parent->dev : NULL);
742 		platform_device_put(pdev_parent);
743 
744 		if (pdev == NULL) {
745 			pr_err("%s: failed to create for '%pOF'\n",
746 					__func__, rd->dn);
747 			/* of_platform_device_create tosses the error code */
748 			return notifier_from_errno(-EINVAL);
749 		}
750 		break;
751 
752 	case OF_RECONFIG_CHANGE_REMOVE:
753 
754 		/* already depopulated? */
755 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
756 			return NOTIFY_OK;
757 
758 		/* find our device by node */
759 		pdev = of_find_device_by_node(rd->dn);
760 		if (pdev == NULL)
761 			return NOTIFY_OK;	/* no? not meant for us */
762 
763 		/* unregister takes one ref away */
764 		of_platform_device_destroy(&pdev->dev, &children_left);
765 
766 		/* and put the reference of the find */
767 		platform_device_put(pdev);
768 		break;
769 	}
770 
771 	return NOTIFY_OK;
772 }
773 
774 static struct notifier_block platform_of_notifier = {
775 	.notifier_call = of_platform_notify,
776 };
777 
778 void of_platform_register_reconfig_notifier(void)
779 {
780 	WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
781 }
782 #endif /* CONFIG_OF_DYNAMIC */
783 
784 #endif /* CONFIG_OF_ADDRESS */
785