1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *    Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
4  *			 <[email protected]>
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  */
of_find_device_by_node(struct device_node * np)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 
of_device_add(struct platform_device * ofdev)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 
of_device_register(struct platform_device * pdev)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 
of_device_unregister(struct platform_device * ofdev)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  */
of_device_alloc(struct device_node * np,const char * bus_id,struct device * parent)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  */
of_platform_device_create_pdata(struct device_node * np,const char * bus_id,void * platform_data,struct device * parent)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  */
of_platform_device_create(struct device_node * np,const char * bus_id,struct device * parent)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
of_amba_device_create(struct device_node * node,const char * bus_id,void * platform_data,struct device * parent)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 */
of_amba_device_create(struct device_node * node,const char * bus_id,void * platform_data,struct device * parent)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  */
of_dev_lookup(const struct of_dev_auxdata * lookup,struct device_node * np)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  */
of_platform_bus_create(struct device_node * bus,const struct of_device_id * matches,const struct of_dev_auxdata * lookup,struct device * parent,bool strict)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_get_property(bus, "compatible", NULL))) {
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  */
of_platform_bus_probe(struct device_node * root,const struct of_device_id * matches,struct device * parent)393 int of_platform_bus_probe(struct device_node *root,
394 			  const struct of_device_id *matches,
395 			  struct device *parent)
396 {
397 	struct device_node *child;
398 	int rc = 0;
399 
400 	root = root ? of_node_get(root) : of_find_node_by_path("/");
401 	if (!root)
402 		return -EINVAL;
403 
404 	pr_debug("%s()\n", __func__);
405 	pr_debug(" starting at: %pOF\n", root);
406 
407 	/* Do a self check of bus type, if there's a match, create children */
408 	if (of_match_node(matches, root)) {
409 		rc = of_platform_bus_create(root, matches, NULL, parent, false);
410 	} else for_each_child_of_node(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 			of_node_put(child);
416 			break;
417 		}
418 	}
419 
420 	of_node_put(root);
421 	return rc;
422 }
423 EXPORT_SYMBOL(of_platform_bus_probe);
424 
425 /**
426  * of_platform_populate() - Populate platform_devices from device tree data
427  * @root: parent of the first level to probe or NULL for the root of the tree
428  * @matches: match table, NULL to use the default
429  * @lookup: auxdata table for matching id and platform_data with device nodes
430  * @parent: parent to hook devices from, NULL for toplevel
431  *
432  * Similar to of_platform_bus_probe(), this function walks the device tree
433  * and creates devices from nodes.  It differs in that it follows the modern
434  * convention of requiring all device nodes to have a 'compatible' property,
435  * and it is suitable for creating devices which are children of the root
436  * node (of_platform_bus_probe will only create children of the root which
437  * are selected by the @matches argument).
438  *
439  * New board support should be using this function instead of
440  * of_platform_bus_probe().
441  *
442  * Return: 0 on success, < 0 on failure.
443  */
of_platform_populate(struct device_node * root,const struct of_device_id * matches,const struct of_dev_auxdata * lookup,struct device * parent)444 int of_platform_populate(struct device_node *root,
445 			const struct of_device_id *matches,
446 			const struct of_dev_auxdata *lookup,
447 			struct device *parent)
448 {
449 	int rc = 0;
450 
451 	root = root ? of_node_get(root) : of_find_node_by_path("/");
452 	if (!root)
453 		return -EINVAL;
454 
455 	pr_debug("%s()\n", __func__);
456 	pr_debug(" starting at: %pOF\n", root);
457 
458 	device_links_supplier_sync_state_pause();
459 	for_each_child_of_node_scoped(root, child) {
460 		rc = of_platform_bus_create(child, matches, lookup, parent, true);
461 		if (rc)
462 			break;
463 	}
464 	device_links_supplier_sync_state_resume();
465 
466 	of_node_set_flag(root, OF_POPULATED_BUS);
467 
468 	of_node_put(root);
469 	return rc;
470 }
471 EXPORT_SYMBOL_GPL(of_platform_populate);
472 
of_platform_default_populate(struct device_node * root,const struct of_dev_auxdata * lookup,struct device * parent)473 int of_platform_default_populate(struct device_node *root,
474 				 const struct of_dev_auxdata *lookup,
475 				 struct device *parent)
476 {
477 	static const struct of_device_id match_table[] = {
478 		{ .compatible = "simple-bus", },
479 		{ .compatible = "simple-mfd", },
480 		{ .compatible = "isa", },
481 #ifdef CONFIG_ARM_AMBA
482 		{ .compatible = "arm,amba-bus", },
483 #endif /* CONFIG_ARM_AMBA */
484 		{} /* Empty terminated list */
485 	};
486 
487 	return of_platform_populate(root, match_table, lookup, parent);
488 }
489 EXPORT_SYMBOL_GPL(of_platform_default_populate);
490 
491 static const struct of_device_id reserved_mem_matches[] = {
492 	{ .compatible = "phram" },
493 	{ .compatible = "qcom,rmtfs-mem" },
494 	{ .compatible = "qcom,cmd-db" },
495 	{ .compatible = "qcom,smem" },
496 	{ .compatible = "ramoops" },
497 	{ .compatible = "nvmem-rmem" },
498 	{ .compatible = "google,open-dice" },
499 	{}
500 };
501 
of_platform_default_populate_init(void)502 static int __init of_platform_default_populate_init(void)
503 {
504 	struct device_node *node;
505 
506 	device_links_supplier_sync_state_pause();
507 
508 	if (IS_ENABLED(CONFIG_PPC)) {
509 		struct device_node *boot_display = NULL;
510 		struct platform_device *dev;
511 		int display_number = 0;
512 		int ret;
513 
514 		/* Check if we have a MacOS display without a node spec */
515 		if (of_property_present(of_chosen, "linux,bootx-noscreen")) {
516 			/*
517 			 * The old code tried to work out which node was the MacOS
518 			 * display based on the address. I'm dropping that since the
519 			 * lack of a node spec only happens with old BootX versions
520 			 * (users can update) and with this code, they'll still get
521 			 * a display (just not the palette hacks).
522 			 */
523 			dev = platform_device_alloc("bootx-noscreen", 0);
524 			if (WARN_ON(!dev))
525 				return -ENOMEM;
526 			ret = platform_device_add(dev);
527 			if (WARN_ON(ret)) {
528 				platform_device_put(dev);
529 				return ret;
530 			}
531 		}
532 
533 		/*
534 		 * For OF framebuffers, first create the device for the boot display,
535 		 * then for the other framebuffers. Only fail for the boot display;
536 		 * ignore errors for the rest.
537 		 */
538 		for_each_node_by_type(node, "display") {
539 			if (!of_get_property(node, "linux,opened", NULL) ||
540 			    !of_get_property(node, "linux,boot-display", NULL))
541 				continue;
542 			dev = of_platform_device_create(node, "of-display", NULL);
543 			of_node_put(node);
544 			if (WARN_ON(!dev))
545 				return -ENOMEM;
546 			boot_display = node;
547 			display_number++;
548 			break;
549 		}
550 		for_each_node_by_type(node, "display") {
551 			char buf[14];
552 			const char *of_display_format = "of-display.%d";
553 
554 			if (!of_get_property(node, "linux,opened", NULL) || node == boot_display)
555 				continue;
556 			ret = snprintf(buf, sizeof(buf), of_display_format, display_number++);
557 			if (ret < sizeof(buf))
558 				of_platform_device_create(node, buf, NULL);
559 		}
560 
561 	} else {
562 		/*
563 		 * Handle certain compatibles explicitly, since we don't want to create
564 		 * platform_devices for every node in /reserved-memory with a
565 		 * "compatible",
566 		 */
567 		for_each_matching_node(node, reserved_mem_matches)
568 			of_platform_device_create(node, NULL, NULL);
569 
570 		node = of_find_node_by_path("/firmware");
571 		if (node) {
572 			of_platform_populate(node, NULL, NULL, NULL);
573 			of_node_put(node);
574 		}
575 
576 		node = of_get_compatible_child(of_chosen, "simple-framebuffer");
577 		if (node) {
578 			/*
579 			 * Since a "simple-framebuffer" device is already added
580 			 * here, disable the Generic System Framebuffers (sysfb)
581 			 * to prevent it from registering another device for the
582 			 * system framebuffer later (e.g: using the screen_info
583 			 * data that may had been filled as well).
584 			 *
585 			 * This can happen for example on DT systems that do EFI
586 			 * booting and may provide a GOP handle to the EFI stub.
587 			 */
588 			sysfb_disable(NULL);
589 			of_platform_device_create(node, NULL, NULL);
590 			of_node_put(node);
591 		}
592 
593 		/* Populate everything else. */
594 		of_platform_default_populate(NULL, NULL, NULL);
595 	}
596 
597 	return 0;
598 }
599 arch_initcall_sync(of_platform_default_populate_init);
600 
of_platform_sync_state_init(void)601 static int __init of_platform_sync_state_init(void)
602 {
603 	device_links_supplier_sync_state_resume();
604 	return 0;
605 }
606 late_initcall_sync(of_platform_sync_state_init);
607 
of_platform_device_destroy(struct device * dev,void * data)608 int of_platform_device_destroy(struct device *dev, void *data)
609 {
610 	/* Do not touch devices not populated from the device tree */
611 	if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
612 		return 0;
613 
614 	/* Recurse for any nodes that were treated as busses */
615 	if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
616 		device_for_each_child(dev, NULL, of_platform_device_destroy);
617 
618 	of_node_clear_flag(dev->of_node, OF_POPULATED);
619 	of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
620 
621 	if (dev->bus == &platform_bus_type)
622 		platform_device_unregister(to_platform_device(dev));
623 #ifdef CONFIG_ARM_AMBA
624 	else if (dev->bus == &amba_bustype)
625 		amba_device_unregister(to_amba_device(dev));
626 #endif
627 
628 	return 0;
629 }
630 EXPORT_SYMBOL_GPL(of_platform_device_destroy);
631 
632 /**
633  * of_platform_depopulate() - Remove devices populated from device tree
634  * @parent: device which children will be removed
635  *
636  * Complementary to of_platform_populate(), this function removes children
637  * of the given device (and, recursively, their children) that have been
638  * created from their respective device tree nodes (and only those,
639  * leaving others - eg. manually created - unharmed).
640  */
of_platform_depopulate(struct device * parent)641 void of_platform_depopulate(struct device *parent)
642 {
643 	if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
644 		device_for_each_child_reverse(parent, NULL, of_platform_device_destroy);
645 		of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
646 	}
647 }
648 EXPORT_SYMBOL_GPL(of_platform_depopulate);
649 
devm_of_platform_populate_release(struct device * dev,void * res)650 static void devm_of_platform_populate_release(struct device *dev, void *res)
651 {
652 	of_platform_depopulate(*(struct device **)res);
653 }
654 
655 /**
656  * devm_of_platform_populate() - Populate platform_devices from device tree data
657  * @dev: device that requested to populate from device tree data
658  *
659  * Similar to of_platform_populate(), but will automatically call
660  * of_platform_depopulate() when the device is unbound from the bus.
661  *
662  * Return: 0 on success, < 0 on failure.
663  */
devm_of_platform_populate(struct device * dev)664 int devm_of_platform_populate(struct device *dev)
665 {
666 	struct device **ptr;
667 	int ret;
668 
669 	if (!dev)
670 		return -EINVAL;
671 
672 	ptr = devres_alloc(devm_of_platform_populate_release,
673 			   sizeof(*ptr), GFP_KERNEL);
674 	if (!ptr)
675 		return -ENOMEM;
676 
677 	ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
678 	if (ret) {
679 		devres_free(ptr);
680 	} else {
681 		*ptr = dev;
682 		devres_add(dev, ptr);
683 	}
684 
685 	return ret;
686 }
687 EXPORT_SYMBOL_GPL(devm_of_platform_populate);
688 
devm_of_platform_match(struct device * dev,void * res,void * data)689 static int devm_of_platform_match(struct device *dev, void *res, void *data)
690 {
691 	struct device **ptr = res;
692 
693 	if (!ptr) {
694 		WARN_ON(!ptr);
695 		return 0;
696 	}
697 
698 	return *ptr == data;
699 }
700 
701 /**
702  * devm_of_platform_depopulate() - Remove devices populated from device tree
703  * @dev: device that requested to depopulate from device tree data
704  *
705  * Complementary to devm_of_platform_populate(), this function removes children
706  * of the given device (and, recursively, their children) that have been
707  * created from their respective device tree nodes (and only those,
708  * leaving others - eg. manually created - unharmed).
709  */
devm_of_platform_depopulate(struct device * dev)710 void devm_of_platform_depopulate(struct device *dev)
711 {
712 	int ret;
713 
714 	ret = devres_release(dev, devm_of_platform_populate_release,
715 			     devm_of_platform_match, dev);
716 
717 	WARN_ON(ret);
718 }
719 EXPORT_SYMBOL_GPL(devm_of_platform_depopulate);
720 
721 #ifdef CONFIG_OF_DYNAMIC
of_platform_notify(struct notifier_block * nb,unsigned long action,void * arg)722 static int of_platform_notify(struct notifier_block *nb,
723 				unsigned long action, void *arg)
724 {
725 	struct of_reconfig_data *rd = arg;
726 	struct platform_device *pdev_parent, *pdev;
727 	bool children_left;
728 	struct device_node *parent;
729 
730 	switch (of_reconfig_get_state_change(action, rd)) {
731 	case OF_RECONFIG_CHANGE_ADD:
732 		parent = rd->dn->parent;
733 		/* verify that the parent is a bus (or the root node) */
734 		if (!of_node_is_root(parent) &&
735 		    !of_node_check_flag(parent, OF_POPULATED_BUS))
736 			return NOTIFY_OK;	/* not for us */
737 
738 		/* already populated? (driver using of_populate manually) */
739 		if (of_node_check_flag(rd->dn, OF_POPULATED))
740 			return NOTIFY_OK;
741 
742 		/*
743 		 * Clear the flag before adding the device so that fw_devlink
744 		 * doesn't skip adding consumers to this device.
745 		 */
746 		rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
747 		/* pdev_parent may be NULL when no bus platform device */
748 		pdev_parent = of_find_device_by_node(parent);
749 		pdev = of_platform_device_create(rd->dn, NULL,
750 				pdev_parent ? &pdev_parent->dev : NULL);
751 		platform_device_put(pdev_parent);
752 
753 		if (pdev == NULL) {
754 			pr_err("%s: failed to create for '%pOF'\n",
755 					__func__, rd->dn);
756 			/* of_platform_device_create tosses the error code */
757 			return notifier_from_errno(-EINVAL);
758 		}
759 		break;
760 
761 	case OF_RECONFIG_CHANGE_REMOVE:
762 
763 		/* already depopulated? */
764 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
765 			return NOTIFY_OK;
766 
767 		/* find our device by node */
768 		pdev = of_find_device_by_node(rd->dn);
769 		if (pdev == NULL)
770 			return NOTIFY_OK;	/* no? not meant for us */
771 
772 		/* unregister takes one ref away */
773 		of_platform_device_destroy(&pdev->dev, &children_left);
774 
775 		/* and put the reference of the find */
776 		platform_device_put(pdev);
777 		break;
778 	}
779 
780 	return NOTIFY_OK;
781 }
782 
783 static struct notifier_block platform_of_notifier = {
784 	.notifier_call = of_platform_notify,
785 };
786 
of_platform_register_reconfig_notifier(void)787 void of_platform_register_reconfig_notifier(void)
788 {
789 	WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
790 }
791 #endif /* CONFIG_OF_DYNAMIC */
792 
793 #endif /* CONFIG_OF_ADDRESS */
794