blob: d563cbcf76cfbfb197aa1e45be1dc5f700102770 [file] [log] [blame]
Peter Chen69bec722016-02-19 17:26:15 +08001/*
2 * of.c The helpers for hcd device tree support
3 *
4 * Copyright (C) 2016 Freescale Semiconductor, Inc.
5 * Author: Peter Chen <peter.chen@freescale.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 of
9 * the License as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/of.h>
Yoshihiro Shimoda5095cb82017-02-21 19:59:47 +090021#include <linux/of_platform.h>
Peter Chen0573f2c2016-06-15 11:25:43 +080022#include <linux/usb/of.h>
Peter Chen69bec722016-02-19 17:26:15 +080023
24/**
25 * usb_of_get_child_node - Find the device node match port number
26 * @parent: the parent device node
27 * @portnum: the port number which device is connecting
28 *
29 * Find the node from device tree according to its port number.
30 *
31 * Return: On success, a pointer to the device node, %NULL on failure.
32 */
33struct device_node *usb_of_get_child_node(struct device_node *parent,
34 int portnum)
35{
36 struct device_node *node;
37 u32 port;
38
39 for_each_child_of_node(parent, node) {
40 if (!of_property_read_u32(node, "reg", &port)) {
41 if (port == portnum)
42 return node;
43 }
44 }
45
46 return NULL;
47}
48EXPORT_SYMBOL_GPL(usb_of_get_child_node);
49
Yoshihiro Shimoda5095cb82017-02-21 19:59:47 +090050/**
51 * usb_of_get_companion_dev - Find the companion device
52 * @dev: the device pointer to find a companion
53 *
54 * Find the companion device from platform bus.
55 *
Johan Hovoldef53b922017-05-16 16:26:14 +020056 * Takes a reference to the returned struct device which needs to be dropped
57 * after use.
58 *
Yoshihiro Shimoda5095cb82017-02-21 19:59:47 +090059 * Return: On success, a pointer to the companion device, %NULL on failure.
60 */
61struct device *usb_of_get_companion_dev(struct device *dev)
62{
63 struct device_node *node;
64 struct platform_device *pdev = NULL;
65
66 node = of_parse_phandle(dev->of_node, "companion", 0);
67 if (node)
68 pdev = of_find_device_by_node(node);
69
70 of_node_put(node);
71
72 return pdev ? &pdev->dev : NULL;
73}
74EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);