xref: /linux/drivers/usb/core/of.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
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  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2  of
10  * the License as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <linux/of.h>
22 #include <linux/of_platform.h>
23 #include <linux/usb/of.h>
24 
25 /**
26  * usb_of_get_child_node - Find the device node match port number
27  * @parent: the parent device node
28  * @portnum: the port number which device is connecting
29  *
30  * Find the node from device tree according to its port number.
31  *
32  * Return: A pointer to the node with incremented refcount if found, or
33  * %NULL otherwise.
34  */
35 struct device_node *usb_of_get_child_node(struct device_node *parent,
36 					int portnum)
37 {
38 	struct device_node *node;
39 	u32 port;
40 
41 	for_each_child_of_node(parent, node) {
42 		if (!of_property_read_u32(node, "reg", &port)) {
43 			if (port == portnum)
44 				return node;
45 		}
46 	}
47 
48 	return NULL;
49 }
50 EXPORT_SYMBOL_GPL(usb_of_get_child_node);
51 
52 /**
53  * usb_of_get_companion_dev - Find the companion device
54  * @dev: the device pointer to find a companion
55  *
56  * Find the companion device from platform bus.
57  *
58  * Takes a reference to the returned struct device which needs to be dropped
59  * after use.
60  *
61  * Return: On success, a pointer to the companion device, %NULL on failure.
62  */
63 struct device *usb_of_get_companion_dev(struct device *dev)
64 {
65 	struct device_node *node;
66 	struct platform_device *pdev = NULL;
67 
68 	node = of_parse_phandle(dev->of_node, "companion", 0);
69 	if (node)
70 		pdev = of_find_device_by_node(node);
71 
72 	of_node_put(node);
73 
74 	return pdev ? &pdev->dev : NULL;
75 }
76 EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
77