of_gpio.h 4.1 KB

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