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