of.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  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. #include <linux/of.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/usb/of.h>
  22. /**
  23. * usb_of_get_child_node - Find the device node match port number
  24. * @parent: the parent device node
  25. * @portnum: the port number which device is connecting
  26. *
  27. * Find the node from device tree according to its port number.
  28. *
  29. * Return: A pointer to the node with incremented refcount if found, or
  30. * %NULL otherwise.
  31. */
  32. struct device_node *usb_of_get_child_node(struct device_node *parent,
  33. int portnum)
  34. {
  35. struct device_node *node;
  36. u32 port;
  37. for_each_child_of_node(parent, node) {
  38. if (!of_property_read_u32(node, "reg", &port)) {
  39. if (port == portnum)
  40. return node;
  41. }
  42. }
  43. return NULL;
  44. }
  45. EXPORT_SYMBOL_GPL(usb_of_get_child_node);
  46. /**
  47. * usb_of_get_companion_dev - Find the companion device
  48. * @dev: the device pointer to find a companion
  49. *
  50. * Find the companion device from platform bus.
  51. *
  52. * Takes a reference to the returned struct device which needs to be dropped
  53. * after use.
  54. *
  55. * Return: On success, a pointer to the companion device, %NULL on failure.
  56. */
  57. struct device *usb_of_get_companion_dev(struct device *dev)
  58. {
  59. struct device_node *node;
  60. struct platform_device *pdev = NULL;
  61. node = of_parse_phandle(dev->of_node, "companion", 0);
  62. if (node)
  63. pdev = of_find_device_by_node(node);
  64. of_node_put(node);
  65. return pdev ? &pdev->dev : NULL;
  66. }
  67. EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);