gpio.h 4.4 KB

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