xref: /linux/drivers/usb/core/of.c (revision fd639726bf15fca8ee1a00dce8e0096d0ad9bd18)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * of.c		The helpers for hcd device tree support
4  *
5  * Copyright (C) 2016 Freescale Semiconductor, Inc.
6  * Author: Peter Chen <peter.chen@freescale.com>
7  */
8 
9 #include <linux/of.h>
10 #include <linux/of_platform.h>
11 #include <linux/usb/of.h>
12 
13 /**
14  * usb_of_get_child_node - Find the device node match port number
15  * @parent: the parent device node
16  * @portnum: the port number which device is connecting
17  *
18  * Find the node from device tree according to its port number.
19  *
20  * Return: A pointer to the node with incremented refcount if found, or
21  * %NULL otherwise.
22  */
23 struct device_node *usb_of_get_child_node(struct device_node *parent,
24 					int portnum)
25 {
26 	struct device_node *node;
27 	u32 port;
28 
29 	for_each_child_of_node(parent, node) {
30 		if (!of_property_read_u32(node, "reg", &port)) {
31 			if (port == portnum)
32 				return node;
33 		}
34 	}
35 
36 	return NULL;
37 }
38 EXPORT_SYMBOL_GPL(usb_of_get_child_node);
39 
40 /**
41  * usb_of_get_companion_dev - Find the companion device
42  * @dev: the device pointer to find a companion
43  *
44  * Find the companion device from platform bus.
45  *
46  * Takes a reference to the returned struct device which needs to be dropped
47  * after use.
48  *
49  * Return: On success, a pointer to the companion device, %NULL on failure.
50  */
51 struct device *usb_of_get_companion_dev(struct device *dev)
52 {
53 	struct device_node *node;
54 	struct platform_device *pdev = NULL;
55 
56 	node = of_parse_phandle(dev->of_node, "companion", 0);
57 	if (node)
58 		pdev = of_find_device_by_node(node);
59 
60 	of_node_put(node);
61 
62 	return pdev ? &pdev->dev : NULL;
63 }
64 EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
65