gpiolib-swnode.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Software Node helpers for the GPIO API
  4. *
  5. * Copyright 2022 Google LLC
  6. */
  7. #define pr_fmt(fmt) "gpiolib: swnode: " fmt
  8. #include <linux/err.h>
  9. #include <linux/errno.h>
  10. #include <linux/export.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/printk.h>
  14. #include <linux/property.h>
  15. #include <linux/string.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/gpio/driver.h>
  18. #include "gpiolib.h"
  19. #include "gpiolib-swnode.h"
  20. #define GPIOLIB_SWNODE_UNDEFINED_NAME "swnode-gpio-undefined"
  21. static struct gpio_device *swnode_get_gpio_device(struct fwnode_handle *fwnode)
  22. {
  23. const struct software_node *gdev_node;
  24. struct gpio_device *gdev;
  25. gdev_node = to_software_node(fwnode);
  26. if (!gdev_node || !gdev_node->name)
  27. return ERR_PTR(-EINVAL);
  28. /*
  29. * Check for a special node that identifies undefined GPIOs, this is
  30. * primarily used as a key for internal chip selects in SPI bindings.
  31. */
  32. if (IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED) &&
  33. !strcmp(gdev_node->name, GPIOLIB_SWNODE_UNDEFINED_NAME))
  34. return ERR_PTR(-ENOENT);
  35. gdev = gpio_device_find_by_label(gdev_node->name);
  36. return gdev ?: ERR_PTR(-EPROBE_DEFER);
  37. }
  38. static int swnode_gpio_get_reference(const struct fwnode_handle *fwnode,
  39. const char *propname, unsigned int idx,
  40. struct fwnode_reference_args *args)
  41. {
  42. /*
  43. * We expect all swnode-described GPIOs have GPIO number and
  44. * polarity arguments, hence nargs is set to 2.
  45. */
  46. return fwnode_property_get_reference_args(fwnode, propname, NULL, 2, idx, args);
  47. }
  48. struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode,
  49. const char *con_id, unsigned int idx,
  50. unsigned long *flags)
  51. {
  52. const struct software_node *swnode;
  53. struct fwnode_reference_args args;
  54. struct gpio_desc *desc;
  55. char propname[32]; /* 32 is max size of property name */
  56. int ret = 0;
  57. swnode = to_software_node(fwnode);
  58. if (!swnode)
  59. return ERR_PTR(-EINVAL);
  60. for_each_gpio_property_name(propname, con_id) {
  61. ret = swnode_gpio_get_reference(fwnode, propname, idx, &args);
  62. if (ret == 0)
  63. break;
  64. }
  65. if (ret) {
  66. pr_debug("%s: can't parse '%s' property of node '%pfwP[%d]'\n",
  67. __func__, propname, fwnode, idx);
  68. return ERR_PTR(ret);
  69. }
  70. struct gpio_device *gdev __free(gpio_device_put) =
  71. swnode_get_gpio_device(args.fwnode);
  72. fwnode_handle_put(args.fwnode);
  73. if (IS_ERR(gdev))
  74. return ERR_CAST(gdev);
  75. /*
  76. * FIXME: The GPIO device reference is put at return but the descriptor
  77. * is passed on. Find a proper solution.
  78. */
  79. desc = gpio_device_get_desc(gdev, args.args[0]);
  80. *flags = args.args[1]; /* We expect native GPIO flags */
  81. pr_debug("%s: parsed '%s' property of node '%pfwP[%d]' - status (%d)\n",
  82. __func__, propname, fwnode, idx, PTR_ERR_OR_ZERO(desc));
  83. return desc;
  84. }
  85. /**
  86. * swnode_gpio_count - count the GPIOs associated with a device / function
  87. * @fwnode: firmware node of the GPIO consumer, can be %NULL for
  88. * system-global GPIOs
  89. * @con_id: function within the GPIO consumer
  90. *
  91. * Returns:
  92. * The number of GPIOs associated with a device / function or %-ENOENT,
  93. * if no GPIO has been assigned to the requested function.
  94. */
  95. int swnode_gpio_count(const struct fwnode_handle *fwnode, const char *con_id)
  96. {
  97. struct fwnode_reference_args args;
  98. char propname[32];
  99. int count;
  100. /*
  101. * This is not very efficient, but GPIO lists usually have only
  102. * 1 or 2 entries.
  103. */
  104. for_each_gpio_property_name(propname, con_id) {
  105. count = 0;
  106. while (swnode_gpio_get_reference(fwnode, propname, count, &args) == 0) {
  107. fwnode_handle_put(args.fwnode);
  108. count++;
  109. }
  110. if (count)
  111. return count;
  112. }
  113. return -ENOENT;
  114. }
  115. #if IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED)
  116. /*
  117. * A special node that identifies undefined GPIOs, this is primarily used as
  118. * a key for internal chip selects in SPI bindings.
  119. */
  120. const struct software_node swnode_gpio_undefined = {
  121. .name = GPIOLIB_SWNODE_UNDEFINED_NAME,
  122. };
  123. EXPORT_SYMBOL_NS_GPL(swnode_gpio_undefined, GPIO_SWNODE);
  124. static int __init swnode_gpio_init(void)
  125. {
  126. int ret;
  127. ret = software_node_register(&swnode_gpio_undefined);
  128. if (ret < 0)
  129. pr_err("failed to register swnode: %d\n", ret);
  130. return ret;
  131. }
  132. subsys_initcall(swnode_gpio_init);
  133. static void __exit swnode_gpio_cleanup(void)
  134. {
  135. software_node_unregister(&swnode_gpio_undefined);
  136. }
  137. __exitcall(swnode_gpio_cleanup);
  138. #endif