of_gpio.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/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. };
  29. #ifdef CONFIG_OF_GPIO
  30. #include <linux/kernel.h>
  31. /*
  32. * OF GPIO chip for memory mapped banks
  33. */
  34. struct of_mm_gpio_chip {
  35. struct gpio_chip gc;
  36. void (*save_regs)(struct of_mm_gpio_chip *mm_gc);
  37. void __iomem *regs;
  38. };
  39. static inline struct of_mm_gpio_chip *to_of_mm_gpio_chip(struct gpio_chip *gc)
  40. {
  41. return container_of(gc, struct of_mm_gpio_chip, gc);
  42. }
  43. extern int of_get_named_gpio_flags(struct device_node *np,
  44. const char *list_name, int index, enum of_gpio_flags *flags);
  45. extern int of_mm_gpiochip_add_data(struct device_node *np,
  46. struct of_mm_gpio_chip *mm_gc,
  47. void *data);
  48. static inline int of_mm_gpiochip_add(struct device_node *np,
  49. struct of_mm_gpio_chip *mm_gc)
  50. {
  51. return of_mm_gpiochip_add_data(np, mm_gc, NULL);
  52. }
  53. extern void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc);
  54. #else /* CONFIG_OF_GPIO */
  55. #include <linux/errno.h>
  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. #endif /* CONFIG_OF_GPIO */
  65. /**
  66. * of_gpio_named_count() - Count GPIOs for a device
  67. * @np: device node to count GPIOs for
  68. * @propname: property name containing gpio specifier(s)
  69. *
  70. * The function returns the count of GPIOs specified for a node.
  71. * Note that the empty GPIO specifiers count too. Returns either
  72. * Number of gpios defined in property,
  73. * -EINVAL for an incorrectly formed gpios property, or
  74. * -ENOENT for a missing gpios property
  75. *
  76. * Example:
  77. * gpios = <0
  78. * &gpio1 1 2
  79. * 0
  80. * &gpio2 3 4>;
  81. *
  82. * The above example defines four GPIOs, two of which are not specified.
  83. * This function will return '4'
  84. */
  85. static inline int of_gpio_named_count(struct device_node *np, const char* propname)
  86. {
  87. return of_count_phandle_with_args(np, propname, "#gpio-cells");
  88. }
  89. /**
  90. * of_gpio_count() - Count GPIOs for a device
  91. * @np: device node to count GPIOs for
  92. *
  93. * Same as of_gpio_named_count, but hard coded to use the 'gpios' property
  94. */
  95. static inline int of_gpio_count(struct device_node *np)
  96. {
  97. return of_gpio_named_count(np, "gpios");
  98. }
  99. static inline int of_get_gpio_flags(struct device_node *np, int index,
  100. enum of_gpio_flags *flags)
  101. {
  102. return of_get_named_gpio_flags(np, "gpios", index, flags);
  103. }
  104. /**
  105. * of_get_named_gpio() - Get a GPIO number to use with GPIO API
  106. * @np: device node to get GPIO from
  107. * @propname: Name of property containing gpio specifier(s)
  108. * @index: index of the GPIO
  109. *
  110. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  111. * value on the error condition.
  112. */
  113. static inline int of_get_named_gpio(struct device_node *np,
  114. const char *propname, int index)
  115. {
  116. return of_get_named_gpio_flags(np, propname, index, NULL);
  117. }
  118. /**
  119. * of_get_gpio() - Get a GPIO number to use with GPIO API
  120. * @np: device node to get GPIO from
  121. * @index: index of the GPIO
  122. *
  123. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  124. * value on the error condition.
  125. */
  126. static inline int of_get_gpio(struct device_node *np, int index)
  127. {
  128. return of_get_gpio_flags(np, index, NULL);
  129. }
  130. #endif /* __LINUX_OF_GPIO_H */