of_helpers.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/string.h>
  3. #include <linux/err.h>
  4. #include <linux/slab.h>
  5. #include <linux/of.h>
  6. #include <asm/prom.h>
  7. #include "of_helpers.h"
  8. /**
  9. * pseries_of_derive_parent - basically like dirname(1)
  10. * @path: the full_name of a node to be added to the tree
  11. *
  12. * Returns the node which should be the parent of the node
  13. * described by path. E.g., for path = "/foo/bar", returns
  14. * the node with full_name = "/foo".
  15. */
  16. struct device_node *pseries_of_derive_parent(const char *path)
  17. {
  18. struct device_node *parent;
  19. char *parent_path = "/";
  20. const char *tail;
  21. /* We do not want the trailing '/' character */
  22. tail = kbasename(path) - 1;
  23. /* reject if path is "/" */
  24. if (!strcmp(path, "/"))
  25. return ERR_PTR(-EINVAL);
  26. if (tail > path) {
  27. parent_path = kstrndup(path, tail - path, GFP_KERNEL);
  28. if (!parent_path)
  29. return ERR_PTR(-ENOMEM);
  30. }
  31. parent = of_find_node_by_path(parent_path);
  32. if (strcmp(parent_path, "/"))
  33. kfree(parent_path);
  34. return parent ? parent : ERR_PTR(-EINVAL);
  35. }
  36. /* Helper Routines to convert between drc_index to cpu numbers */
  37. int of_read_drc_info_cell(struct property **prop, const __be32 **curval,
  38. struct of_drc_info *data)
  39. {
  40. const char *p;
  41. const __be32 *p2;
  42. if (!data)
  43. return -EINVAL;
  44. /* Get drc-type:encode-string */
  45. p = data->drc_type = (char*) (*curval);
  46. p = of_prop_next_string(*prop, p);
  47. if (!p)
  48. return -EINVAL;
  49. /* Get drc-name-prefix:encode-string */
  50. data->drc_name_prefix = (char *)p;
  51. p = of_prop_next_string(*prop, p);
  52. if (!p)
  53. return -EINVAL;
  54. /* Get drc-index-start:encode-int */
  55. p2 = (const __be32 *)p;
  56. p2 = of_prop_next_u32(*prop, p2, &data->drc_index_start);
  57. if (!p2)
  58. return -EINVAL;
  59. /* Get drc-name-suffix-start:encode-int */
  60. p2 = of_prop_next_u32(*prop, p2, &data->drc_name_suffix_start);
  61. if (!p2)
  62. return -EINVAL;
  63. /* Get number-sequential-elements:encode-int */
  64. p2 = of_prop_next_u32(*prop, p2, &data->num_sequential_elems);
  65. if (!p2)
  66. return -EINVAL;
  67. /* Get sequential-increment:encode-int */
  68. p2 = of_prop_next_u32(*prop, p2, &data->sequential_inc);
  69. if (!p2)
  70. return -EINVAL;
  71. /* Get drc-power-domain:encode-int */
  72. p2 = of_prop_next_u32(*prop, p2, &data->drc_power_domain);
  73. if (!p2)
  74. return -EINVAL;
  75. /* Should now know end of current entry */
  76. (*curval) = (void *)p2;
  77. data->last_drc_index = data->drc_index_start +
  78. ((data->num_sequential_elems - 1) * data->sequential_inc);
  79. return 0;
  80. }
  81. EXPORT_SYMBOL(of_read_drc_info_cell);