xref: /linux/drivers/of/platform.c (revision e2d10998e4293a27c0389870b5fdf736a71d61ef)
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 = kzalloc_objs(*res, num_reg);
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, *reserved;
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 		reserved = of_find_node_by_path("/reserved-memory");
567 		if (reserved) {
568 			for_each_child_of_node(reserved, node) {
569 				if (of_match_node(reserved_mem_matches, node))
570 					of_platform_device_create(node, NULL, NULL);
571 			}
572 			of_node_put(reserved);
573 		}
574 
575 		node = of_find_node_by_path("/firmware");
576 		if (node) {
577 			of_platform_default_populate(node, NULL, NULL);
578 			of_node_put(node);
579 		}
580 
581 		node = of_get_compatible_child(of_chosen, "simple-framebuffer");
582 		if (node) {
583 			/*
584 			 * Since a "simple-framebuffer" device is already added
585 			 * here, disable the Generic System Framebuffers (sysfb)
586 			 * to prevent it from registering another device for the
587 			 * system framebuffer later (e.g: using the screen_info
588 			 * data that may had been filled as well).
589 			 *
590 			 * This can happen for example on DT systems that do EFI
591 			 * booting and may provide a GOP handle to the EFI stub.
592 			 */
593 			sysfb_disable(NULL);
594 			of_platform_device_create(node, NULL, NULL);
595 			of_node_put(node);
596 		}
597 
598 		/* Populate everything else. */
599 		of_platform_default_populate(NULL, NULL, NULL);
600 	}
601 
602 	return 0;
603 }
604 arch_initcall_sync(of_platform_default_populate_init);
605 
606 static int __init of_platform_sync_state_init(void)
607 {
608 	device_links_supplier_sync_state_resume();
609 	return 0;
610 }
611 late_initcall_sync(of_platform_sync_state_init);
612 
613 int of_platform_device_destroy(struct device *dev, void *data)
614 {
615 	/* Do not touch devices not populated from the device tree */
616 	if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
617 		return 0;
618 
619 	/* Recurse for any nodes that were treated as busses */
620 	if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
621 		device_for_each_child(dev, NULL, of_platform_device_destroy);
622 
623 	of_node_clear_flag(dev->of_node, OF_POPULATED);
624 	of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
625 
626 	if (dev->bus == &platform_bus_type)
627 		platform_device_unregister(to_platform_device(dev));
628 #ifdef CONFIG_ARM_AMBA
629 	else if (dev->bus == &amba_bustype)
630 		amba_device_unregister(to_amba_device(dev));
631 #endif
632 
633 	return 0;
634 }
635 EXPORT_SYMBOL_GPL(of_platform_device_destroy);
636 
637 /**
638  * of_platform_depopulate() - Remove devices populated from device tree
639  * @parent: device which children will be removed
640  *
641  * Complementary to of_platform_populate(), this function removes children
642  * of the given device (and, recursively, their children) that have been
643  * created from their respective device tree nodes (and only those,
644  * leaving others - eg. manually created - unharmed).
645  */
646 void of_platform_depopulate(struct device *parent)
647 {
648 	if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
649 		device_for_each_child_reverse(parent, NULL, of_platform_device_destroy);
650 		of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
651 	}
652 }
653 EXPORT_SYMBOL_GPL(of_platform_depopulate);
654 
655 static void devm_of_platform_populate_release(struct device *dev, void *res)
656 {
657 	of_platform_depopulate(*(struct device **)res);
658 }
659 
660 /**
661  * devm_of_platform_populate() - Populate platform_devices from device tree data
662  * @dev: device that requested to populate from device tree data
663  *
664  * Similar to of_platform_populate(), but will automatically call
665  * of_platform_depopulate() when the device is unbound from the bus.
666  *
667  * Return: 0 on success, < 0 on failure.
668  */
669 int devm_of_platform_populate(struct device *dev)
670 {
671 	struct device **ptr;
672 	int ret;
673 
674 	if (!dev)
675 		return -EINVAL;
676 
677 	ptr = devres_alloc(devm_of_platform_populate_release,
678 			   sizeof(*ptr), GFP_KERNEL);
679 	if (!ptr)
680 		return -ENOMEM;
681 
682 	ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
683 	if (ret) {
684 		devres_free(ptr);
685 	} else {
686 		*ptr = dev;
687 		devres_add(dev, ptr);
688 	}
689 
690 	return ret;
691 }
692 EXPORT_SYMBOL_GPL(devm_of_platform_populate);
693 
694 static int devm_of_platform_match(struct device *dev, void *res, void *data)
695 {
696 	struct device **ptr = res;
697 
698 	if (!ptr) {
699 		WARN_ON(!ptr);
700 		return 0;
701 	}
702 
703 	return *ptr == data;
704 }
705 
706 /**
707  * devm_of_platform_depopulate() - Remove devices populated from device tree
708  * @dev: device that requested to depopulate from device tree data
709  *
710  * Complementary to devm_of_platform_populate(), this function removes children
711  * of the given device (and, recursively, their children) that have been
712  * created from their respective device tree nodes (and only those,
713  * leaving others - eg. manually created - unharmed).
714  */
715 void devm_of_platform_depopulate(struct device *dev)
716 {
717 	int ret;
718 
719 	ret = devres_release(dev, devm_of_platform_populate_release,
720 			     devm_of_platform_match, dev);
721 
722 	WARN_ON(ret);
723 }
724 EXPORT_SYMBOL_GPL(devm_of_platform_depopulate);
725 
726 #ifdef CONFIG_OF_DYNAMIC
727 static int of_platform_notify(struct notifier_block *nb,
728 				unsigned long action, void *arg)
729 {
730 	struct of_reconfig_data *rd = arg;
731 	struct platform_device *pdev_parent, *pdev;
732 	bool children_left;
733 	struct device_node *parent;
734 
735 	switch (of_reconfig_get_state_change(action, rd)) {
736 	case OF_RECONFIG_CHANGE_ADD:
737 		parent = rd->dn->parent;
738 		/* verify that the parent is a bus (or the root node) */
739 		if (!of_node_is_root(parent) &&
740 		    !of_node_check_flag(parent, OF_POPULATED_BUS))
741 			return NOTIFY_OK;	/* not for us */
742 
743 		/* already populated? (driver using of_populate manually) */
744 		if (of_node_check_flag(rd->dn, OF_POPULATED))
745 			return NOTIFY_OK;
746 
747 		/*
748 		 * Clear the flag before adding the device so that fw_devlink
749 		 * doesn't skip adding consumers to this device.
750 		 */
751 		fwnode_clear_flag(&rd->dn->fwnode, FWNODE_FLAG_NOT_DEVICE);
752 		/* pdev_parent may be NULL when no bus platform device */
753 		pdev_parent = of_find_device_by_node(parent);
754 		pdev = of_platform_device_create(rd->dn, NULL,
755 				pdev_parent ? &pdev_parent->dev : NULL);
756 		platform_device_put(pdev_parent);
757 
758 		if (pdev == NULL) {
759 			pr_err("%s: failed to create for '%pOF'\n",
760 					__func__, rd->dn);
761 			/* of_platform_device_create tosses the error code */
762 			return notifier_from_errno(-EINVAL);
763 		}
764 		break;
765 
766 	case OF_RECONFIG_CHANGE_REMOVE:
767 
768 		/* already depopulated? */
769 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
770 			return NOTIFY_OK;
771 
772 		/* find our device by node */
773 		pdev = of_find_device_by_node(rd->dn);
774 		if (pdev == NULL)
775 			return NOTIFY_OK;	/* no? not meant for us */
776 
777 		/* unregister takes one ref away */
778 		of_platform_device_destroy(&pdev->dev, &children_left);
779 
780 		/* and put the reference of the find */
781 		platform_device_put(pdev);
782 		break;
783 	}
784 
785 	return NOTIFY_OK;
786 }
787 
788 static struct notifier_block platform_of_notifier = {
789 	.notifier_call = of_platform_notify,
790 };
791 
792 void of_platform_register_reconfig_notifier(void)
793 {
794 	WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
795 }
796 #endif /* CONFIG_OF_DYNAMIC */
797 
798 #endif /* CONFIG_OF_ADDRESS */
799