of_gpio.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * OF helpers for the GPIO API
  4. *
  5. * Copyright (c) 2007-2008 MontaVista Software, Inc.
  6. *
  7. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  8. */
  9. #ifndef __LINUX_OF_GPIO_H
  10. #define __LINUX_OF_GPIO_H
  11. #include <linux/compiler.h>
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/gpio.h>
  15. #include <linux/of.h>
  16. struct device_node;
  17. /*
  18. * This is Linux-specific flags. By default controllers' and Linux' mapping
  19. * match, but GPIO controllers are free to translate their own flags to
  20. * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended.
  21. */
  22. enum of_gpio_flags {
  23. OF_GPIO_ACTIVE_LOW = 0x1,
  24. OF_GPIO_SINGLE_ENDED = 0x2,
  25. OF_GPIO_OPEN_DRAIN = 0x4,
  26. OF_GPIO_TRANSITORY = 0x8,
  27. };
  28. #ifdef CONFIG_OF_GPIO
  29. /*
  30. * OF GPIO chip for memory mapped banks
  31. */
  32. struct of_mm_gpio_chip {
  33. struct gpio_chip gc;
  34. void (*save_regs)(struct of_mm_gpio_chip *mm_gc);
  35. void __iomem *regs;
  36. };
  37. static inline struct of_mm_gpio_chip *to_of_mm_gpio_chip(struct gpio_chip *gc)
  38. {
  39. return container_of(gc, struct of_mm_gpio_chip, gc);
  40. }
  41. extern int of_get_named_gpio_flags(struct device_node *np,
  42. const char *list_name, int index, enum of_gpio_flags *flags);
  43. extern int of_mm_gpiochip_add_data(struct device_node *np,
  44. struct of_mm_gpio_chip *mm_gc,
  45. void *data);
  46. static inline int of_mm_gpiochip_add(struct device_node *np,
  47. struct of_mm_gpio_chip *mm_gc)
  48. {
  49. return of_mm_gpiochip_add_data(np, mm_gc, NULL);
  50. }
  51. extern void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc);
  52. extern int of_gpio_simple_xlate(struct gpio_chip *gc,
  53. const struct of_phandle_args *gpiospec,
  54. u32 *flags);
  55. #else /* CONFIG_OF_GPIO */
  56. /* Drivers may not strictly depend on the GPIO support, so let them link. */
  57. static inline int of_get_named_gpio_flags(struct device_node *np,
  58. const char *list_name, int index, enum of_gpio_flags *flags)
  59. {
  60. if (flags)
  61. *flags = 0;
  62. return -ENOSYS;
  63. }
  64. static inline int of_gpio_simple_xlate(struct gpio_chip *gc,
  65. const struct of_phandle_args *gpiospec,
  66. u32 *flags)
  67. {
  68. return -ENOSYS;
  69. }
  70. #endif /* CONFIG_OF_GPIO */
  71. /**
  72. * of_gpio_named_count() - Count GPIOs for a device
  73. * @np: device node to count GPIOs for
  74. * @propname: property name containing gpio specifier(s)
  75. *
  76. * The function returns the count of GPIOs specified for a node.
  77. * Note that the empty GPIO specifiers count too. Returns either
  78. * Number of gpios defined in property,
  79. * -EINVAL for an incorrectly formed gpios property, or
  80. * -ENOENT for a missing gpios property
  81. *
  82. * Example:
  83. * gpios = <0
  84. * &gpio1 1 2
  85. * 0
  86. * &gpio2 3 4>;
  87. *
  88. * The above example defines four GPIOs, two of which are not specified.
  89. * This function will return '4'
  90. */
  91. static inline int of_gpio_named_count(struct device_node *np, const char* propname)
  92. {
  93. return of_count_phandle_with_args(np, propname, "#gpio-cells");
  94. }
  95. /**
  96. * of_gpio_count() - Count GPIOs for a device
  97. * @np: device node to count GPIOs for
  98. *
  99. * Same as of_gpio_named_count, but hard coded to use the 'gpios' property
  100. */
  101. static inline int of_gpio_count(struct device_node *np)
  102. {
  103. return of_gpio_named_count(np, "gpios");
  104. }
  105. static inline int of_get_gpio_flags(struct device_node *np, int index,
  106. enum of_gpio_flags *flags)
  107. {
  108. return of_get_named_gpio_flags(np, "gpios", index, flags);
  109. }
  110. /**
  111. * of_get_named_gpio() - Get a GPIO number to use with GPIO API
  112. * @np: device node to get GPIO from
  113. * @propname: Name of property containing gpio specifier(s)
  114. * @index: index of the GPIO
  115. *
  116. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  117. * value on the error condition.
  118. */
  119. static inline int of_get_named_gpio(struct device_node *np,
  120. const char *propname, int index)
  121. {
  122. return of_get_named_gpio_flags(np, propname, index, NULL);
  123. }
  124. /**
  125. * of_get_gpio() - Get a GPIO number to use with GPIO API
  126. * @np: device node to get GPIO from
  127. * @index: index of the GPIO
  128. *
  129. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  130. * value on the error condition.
  131. */
  132. static inline int of_get_gpio(struct device_node *np, int index)
  133. {
  134. return of_get_gpio_flags(np, index, NULL);
  135. }
  136. #endif /* __LINUX_OF_GPIO_H */