gpio.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_GENERIC_GPIO_H
  3. #define _ASM_GENERIC_GPIO_H
  4. #include <linux/kernel.h>
  5. #include <linux/types.h>
  6. #include <linux/errno.h>
  7. #include <linux/of.h>
  8. #ifdef CONFIG_GPIOLIB
  9. #include <linux/compiler.h>
  10. #include <linux/gpio/driver.h>
  11. #include <linux/gpio/consumer.h>
  12. /* Platforms may implement their GPIO interface with library code,
  13. * at a small performance cost for non-inlined operations and some
  14. * extra memory (for code and for per-GPIO table entries).
  15. *
  16. * While the GPIO programming interface defines valid GPIO numbers
  17. * to be in the range 0..MAX_INT, this library restricts them to the
  18. * smaller range 0..ARCH_NR_GPIOS-1.
  19. *
  20. * ARCH_NR_GPIOS is somewhat arbitrary; it usually reflects the sum of
  21. * builtin/SoC GPIOs plus a number of GPIOs on expanders; the latter is
  22. * actually an estimate of a board-specific value.
  23. */
  24. #ifndef ARCH_NR_GPIOS
  25. #if defined(CONFIG_ARCH_NR_GPIO) && CONFIG_ARCH_NR_GPIO > 0
  26. #define ARCH_NR_GPIOS CONFIG_ARCH_NR_GPIO
  27. #else
  28. #define ARCH_NR_GPIOS 512
  29. #endif
  30. #endif
  31. /*
  32. * "valid" GPIO numbers are nonnegative and may be passed to
  33. * setup routines like gpio_request(). only some valid numbers
  34. * can successfully be requested and used.
  35. *
  36. * Invalid GPIO numbers are useful for indicating no-such-GPIO in
  37. * platform data and other tables.
  38. */
  39. static inline bool gpio_is_valid(int number)
  40. {
  41. return number >= 0 && number < ARCH_NR_GPIOS;
  42. }
  43. struct device;
  44. struct gpio;
  45. struct seq_file;
  46. struct module;
  47. struct device_node;
  48. struct gpio_desc;
  49. /* caller holds gpio_lock *OR* gpio is marked as requested */
  50. static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
  51. {
  52. return gpiod_to_chip(gpio_to_desc(gpio));
  53. }
  54. /* Always use the library code for GPIO management calls,
  55. * or when sleeping may be involved.
  56. */
  57. extern int gpio_request(unsigned gpio, const char *label);
  58. extern void gpio_free(unsigned gpio);
  59. static inline int gpio_direction_input(unsigned gpio)
  60. {
  61. return gpiod_direction_input(gpio_to_desc(gpio));
  62. }
  63. static inline int gpio_direction_output(unsigned gpio, int value)
  64. {
  65. return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
  66. }
  67. static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
  68. {
  69. return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
  70. }
  71. static inline int gpio_get_value_cansleep(unsigned gpio)
  72. {
  73. return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
  74. }
  75. static inline void gpio_set_value_cansleep(unsigned gpio, int value)
  76. {
  77. return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
  78. }
  79. /* A platform's <asm/gpio.h> code may want to inline the I/O calls when
  80. * the GPIO is constant and refers to some always-present controller,
  81. * giving direct access to chip registers and tight bitbanging loops.
  82. */
  83. static inline int __gpio_get_value(unsigned gpio)
  84. {
  85. return gpiod_get_raw_value(gpio_to_desc(gpio));
  86. }
  87. static inline void __gpio_set_value(unsigned gpio, int value)
  88. {
  89. return gpiod_set_raw_value(gpio_to_desc(gpio), value);
  90. }
  91. static inline int __gpio_cansleep(unsigned gpio)
  92. {
  93. return gpiod_cansleep(gpio_to_desc(gpio));
  94. }
  95. static inline int __gpio_to_irq(unsigned gpio)
  96. {
  97. return gpiod_to_irq(gpio_to_desc(gpio));
  98. }
  99. extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
  100. extern int gpio_request_array(const struct gpio *array, size_t num);
  101. extern void gpio_free_array(const struct gpio *array, size_t num);
  102. /*
  103. * A sysfs interface can be exported by individual drivers if they want,
  104. * but more typically is configured entirely from userspace.
  105. */
  106. static inline int gpio_export(unsigned gpio, bool direction_may_change)
  107. {
  108. return gpiod_export(gpio_to_desc(gpio), direction_may_change);
  109. }
  110. static inline int gpio_export_link(struct device *dev, const char *name,
  111. unsigned gpio)
  112. {
  113. return gpiod_export_link(dev, name, gpio_to_desc(gpio));
  114. }
  115. static inline void gpio_unexport(unsigned gpio)
  116. {
  117. gpiod_unexport(gpio_to_desc(gpio));
  118. }
  119. #else /* !CONFIG_GPIOLIB */
  120. static inline bool gpio_is_valid(int number)
  121. {
  122. /* only non-negative numbers are valid */
  123. return number >= 0;
  124. }
  125. /* platforms that don't directly support access to GPIOs through I2C, SPI,
  126. * or other blocking infrastructure can use these wrappers.
  127. */
  128. static inline int gpio_cansleep(unsigned gpio)
  129. {
  130. return 0;
  131. }
  132. static inline int gpio_get_value_cansleep(unsigned gpio)
  133. {
  134. might_sleep();
  135. return __gpio_get_value(gpio);
  136. }
  137. static inline void gpio_set_value_cansleep(unsigned gpio, int value)
  138. {
  139. might_sleep();
  140. __gpio_set_value(gpio, value);
  141. }
  142. #endif /* !CONFIG_GPIOLIB */
  143. #endif /* _ASM_GENERIC_GPIO_H */