gpio.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #include <linux/pinctrl/pinctrl.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. #define ARCH_NR_GPIOS 256
  26. #endif
  27. /*
  28. * "valid" GPIO numbers are nonnegative and may be passed to
  29. * setup routines like gpio_request(). only some valid numbers
  30. * can successfully be requested and used.
  31. *
  32. * Invalid GPIO numbers are useful for indicating no-such-GPIO in
  33. * platform data and other tables.
  34. */
  35. static inline bool gpio_is_valid(int number)
  36. {
  37. return number >= 0 && number < ARCH_NR_GPIOS;
  38. }
  39. struct device;
  40. struct gpio;
  41. struct seq_file;
  42. struct module;
  43. struct device_node;
  44. struct gpio_desc;
  45. /* caller holds gpio_lock *OR* gpio is marked as requested */
  46. static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
  47. {
  48. return gpiod_to_chip(gpio_to_desc(gpio));
  49. }
  50. /* Always use the library code for GPIO management calls,
  51. * or when sleeping may be involved.
  52. */
  53. extern int gpio_request(unsigned gpio, const char *label);
  54. extern void gpio_free(unsigned gpio);
  55. static inline int gpio_direction_input(unsigned gpio)
  56. {
  57. return gpiod_direction_input(gpio_to_desc(gpio));
  58. }
  59. static inline int gpio_direction_output(unsigned gpio, int value)
  60. {
  61. return gpiod_direction_output(gpio_to_desc(gpio), value);
  62. }
  63. static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
  64. {
  65. return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
  66. }
  67. static inline int gpio_get_value_cansleep(unsigned gpio)
  68. {
  69. return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
  70. }
  71. static inline void gpio_set_value_cansleep(unsigned gpio, int value)
  72. {
  73. return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
  74. }
  75. /* A platform's <asm/gpio.h> code may want to inline the I/O calls when
  76. * the GPIO is constant and refers to some always-present controller,
  77. * giving direct access to chip registers and tight bitbanging loops.
  78. */
  79. static inline int __gpio_get_value(unsigned gpio)
  80. {
  81. return gpiod_get_raw_value(gpio_to_desc(gpio));
  82. }
  83. static inline void __gpio_set_value(unsigned gpio, int value)
  84. {
  85. return gpiod_set_raw_value(gpio_to_desc(gpio), value);
  86. }
  87. static inline int __gpio_cansleep(unsigned gpio)
  88. {
  89. return gpiod_cansleep(gpio_to_desc(gpio));
  90. }
  91. static inline int __gpio_to_irq(unsigned gpio)
  92. {
  93. return gpiod_to_irq(gpio_to_desc(gpio));
  94. }
  95. extern int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset);
  96. extern void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset);
  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 int gpio_sysfs_set_active_low(unsigned gpio, int value)
  114. {
  115. return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
  116. }
  117. static inline void gpio_unexport(unsigned gpio)
  118. {
  119. gpiod_unexport(gpio_to_desc(gpio));
  120. }
  121. #ifdef CONFIG_PINCTRL
  122. /**
  123. * struct gpio_pin_range - pin range controlled by a gpio chip
  124. * @head: list for maintaining set of pin ranges, used internally
  125. * @pctldev: pinctrl device which handles corresponding pins
  126. * @range: actual range of pins controlled by a gpio controller
  127. */
  128. struct gpio_pin_range {
  129. struct list_head node;
  130. struct pinctrl_dev *pctldev;
  131. struct pinctrl_gpio_range range;
  132. };
  133. int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  134. unsigned int gpio_offset, unsigned int pin_offset,
  135. unsigned int npins);
  136. int gpiochip_add_pingroup_range(struct gpio_chip *chip,
  137. struct pinctrl_dev *pctldev,
  138. unsigned int gpio_offset, const char *pin_group);
  139. void gpiochip_remove_pin_ranges(struct gpio_chip *chip);
  140. #else
  141. static inline int
  142. gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  143. unsigned int gpio_offset, unsigned int pin_offset,
  144. unsigned int npins)
  145. {
  146. return 0;
  147. }
  148. static inline int
  149. gpiochip_add_pingroup_range(struct gpio_chip *chip,
  150. struct pinctrl_dev *pctldev,
  151. unsigned int gpio_offset, const char *pin_group)
  152. {
  153. return 0;
  154. }
  155. static inline void
  156. gpiochip_remove_pin_ranges(struct gpio_chip *chip)
  157. {
  158. }
  159. #endif /* CONFIG_PINCTRL */
  160. #else /* !CONFIG_GPIOLIB */
  161. static inline bool gpio_is_valid(int number)
  162. {
  163. /* only non-negative numbers are valid */
  164. return number >= 0;
  165. }
  166. /* platforms that don't directly support access to GPIOs through I2C, SPI,
  167. * or other blocking infrastructure can use these wrappers.
  168. */
  169. static inline int gpio_cansleep(unsigned gpio)
  170. {
  171. return 0;
  172. }
  173. static inline int gpio_get_value_cansleep(unsigned gpio)
  174. {
  175. might_sleep();
  176. return __gpio_get_value(gpio);
  177. }
  178. static inline void gpio_set_value_cansleep(unsigned gpio, int value)
  179. {
  180. might_sleep();
  181. __gpio_set_value(gpio, value);
  182. }
  183. #endif /* !CONFIG_GPIOLIB */
  184. #endif /* _ASM_GENERIC_GPIO_H */