of.c 1.6 KB

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