gpio.h 4.3 KB

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