gpio.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. /* Platforms may implement their GPIO interface with library code,
  10. * at a small performance cost for non-inlined operations and some
  11. * extra memory (for code and for per-GPIO table entries).
  12. *
  13. * While the GPIO programming interface defines valid GPIO numbers
  14. * to be in the range 0..MAX_INT, this library restricts them to the
  15. * smaller range 0..ARCH_NR_GPIOS-1.
  16. *
  17. * ARCH_NR_GPIOS is somewhat arbitrary; it usually reflects the sum of
  18. * builtin/SoC GPIOs plus a number of GPIOs on expanders; the latter is
  19. * actually an estimate of a board-specific value.
  20. */
  21. #ifndef ARCH_NR_GPIOS
  22. #define ARCH_NR_GPIOS 256
  23. #endif
  24. /*
  25. * "valid" GPIO numbers are nonnegative and may be passed to
  26. * setup routines like gpio_request(). only some valid numbers
  27. * can successfully be requested and used.
  28. *
  29. * Invalid GPIO numbers are useful for indicating no-such-GPIO in
  30. * platform data and other tables.
  31. */
  32. static inline bool gpio_is_valid(int number)
  33. {
  34. return number >= 0 && number < ARCH_NR_GPIOS;
  35. }
  36. struct device;
  37. struct gpio;
  38. struct seq_file;
  39. struct module;
  40. struct device_node;
  41. /**
  42. * struct gpio_chip - abstract a GPIO controller
  43. * @label: for diagnostics
  44. * @dev: optional device providing the GPIOs
  45. * @owner: helps prevent removal of modules exporting active GPIOs
  46. * @request: optional hook for chip-specific activation, such as
  47. * enabling module power and clock; may sleep
  48. * @free: optional hook for chip-specific deactivation, such as
  49. * disabling module power and clock; may sleep
  50. * @get_direction: returns direction for signal "offset", 0=out, 1=in,
  51. * (same as GPIOF_DIR_XXX), or negative error
  52. * @direction_input: configures signal "offset" as input, or returns error
  53. * @get: returns value for signal "offset"; for output signals this
  54. * returns either the value actually sensed, or zero
  55. * @direction_output: configures signal "offset" as output, or returns error
  56. * @set_debounce: optional hook for setting debounce time for specified gpio in
  57. * interrupt triggered gpio chips
  58. * @set: assigns output value for signal "offset"
  59. * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
  60. * implementation may not sleep
  61. * @dbg_show: optional routine to show contents in debugfs; default code
  62. * will be used when this is omitted, but custom code can show extra
  63. * state (such as pullup/pulldown configuration).
  64. * @base: identifies the first GPIO number handled by this chip; or, if
  65. * negative during registration, requests dynamic ID allocation.
  66. * @ngpio: the number of GPIOs handled by this controller; the last GPIO
  67. * handled is (base + ngpio - 1).
  68. * @can_sleep: flag must be set iff get()/set() methods sleep, as they
  69. * must while accessing GPIO expander chips over I2C or SPI
  70. * @names: if set, must be an array of strings to use as alternative
  71. * names for the GPIOs in this chip. Any entry in the array
  72. * may be NULL if there is no alias for the GPIO, however the
  73. * array must be @ngpio entries long. A name can include a single printk
  74. * format specifier for an unsigned int. It is substituted by the actual
  75. * number of the gpio.
  76. *
  77. * A gpio_chip can help platforms abstract various sources of GPIOs so
  78. * they can all be accessed through a common programing interface.
  79. * Example sources would be SOC controllers, FPGAs, multifunction
  80. * chips, dedicated GPIO expanders, and so on.
  81. *
  82. * Each chip controls a number of signals, identified in method calls
  83. * by "offset" values in the range 0..(@ngpio - 1). When those signals
  84. * are referenced through calls like gpio_get_value(gpio), the offset
  85. * is calculated by subtracting @base from the gpio number.
  86. */
  87. struct gpio_chip {
  88. const char *label;
  89. struct device *dev;
  90. struct module *owner;
  91. int (*request)(struct gpio_chip *chip,
  92. unsigned offset);
  93. void (*free)(struct gpio_chip *chip,
  94. unsigned offset);
  95. int (*get_direction)(struct gpio_chip *chip,
  96. unsigned offset);
  97. int (*direction_input)(struct gpio_chip *chip,
  98. unsigned offset);
  99. int (*get)(struct gpio_chip *chip,
  100. unsigned offset);
  101. int (*direction_output)(struct gpio_chip *chip,
  102. unsigned offset, int value);
  103. int (*set_debounce)(struct gpio_chip *chip,
  104. unsigned offset, unsigned debounce);
  105. void (*set)(struct gpio_chip *chip,
  106. unsigned offset, int value);
  107. int (*to_irq)(struct gpio_chip *chip,
  108. unsigned offset);
  109. void (*dbg_show)(struct seq_file *s,
  110. struct gpio_chip *chip);
  111. int base;
  112. u16 ngpio;
  113. const char *const *names;
  114. unsigned can_sleep:1;
  115. unsigned exported:1;
  116. #if defined(CONFIG_OF_GPIO)
  117. /*
  118. * If CONFIG_OF is enabled, then all GPIO controllers described in the
  119. * device tree automatically may have an OF translation
  120. */
  121. struct device_node *of_node;
  122. int of_gpio_n_cells;
  123. int (*of_xlate)(struct gpio_chip *gc,
  124. const struct of_phandle_args *gpiospec, u32 *flags);
  125. #endif
  126. };
  127. extern const char *gpiochip_is_requested(struct gpio_chip *chip,
  128. unsigned offset);
  129. extern struct gpio_chip *gpio_to_chip(unsigned gpio);
  130. extern int __must_check gpiochip_reserve(int start, int ngpio);
  131. /* add/remove chips */
  132. extern int gpiochip_add(struct gpio_chip *chip);
  133. extern int __must_check gpiochip_remove(struct gpio_chip *chip);
  134. extern struct gpio_chip *gpiochip_find(void *data,
  135. int (*match)(struct gpio_chip *chip,
  136. void *data));
  137. /* Always use the library code for GPIO management calls,
  138. * or when sleeping may be involved.
  139. */
  140. extern int gpio_request(unsigned gpio, const char *label);
  141. extern void gpio_free(unsigned gpio);
  142. extern int gpio_direction_input(unsigned gpio);
  143. extern int gpio_direction_output(unsigned gpio, int value);
  144. extern int gpio_set_debounce(unsigned gpio, unsigned debounce);
  145. extern int gpio_get_value_cansleep(unsigned gpio);
  146. extern void gpio_set_value_cansleep(unsigned gpio, int value);
  147. /* A platform's <asm/gpio.h> code may want to inline the I/O calls when
  148. * the GPIO is constant and refers to some always-present controller,
  149. * giving direct access to chip registers and tight bitbanging loops.
  150. */
  151. extern int __gpio_get_value(unsigned gpio);
  152. extern void __gpio_set_value(unsigned gpio, int value);
  153. extern int __gpio_cansleep(unsigned gpio);
  154. extern int __gpio_to_irq(unsigned gpio);
  155. extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
  156. extern int gpio_request_array(const struct gpio *array, size_t num);
  157. extern void gpio_free_array(const struct gpio *array, size_t num);
  158. /* bindings for managed devices that want to request gpios */
  159. int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
  160. int devm_gpio_request_one(struct device *dev, unsigned gpio,
  161. unsigned long flags, const char *label);
  162. void devm_gpio_free(struct device *dev, unsigned int gpio);
  163. #ifdef CONFIG_GPIO_SYSFS
  164. /*
  165. * A sysfs interface can be exported by individual drivers if they want,
  166. * but more typically is configured entirely from userspace.
  167. */
  168. extern int gpio_export(unsigned gpio, bool direction_may_change);
  169. extern int gpio_export_link(struct device *dev, const char *name,
  170. unsigned gpio);
  171. extern int gpio_sysfs_set_active_low(unsigned gpio, int value);
  172. extern void gpio_unexport(unsigned gpio);
  173. #endif /* CONFIG_GPIO_SYSFS */
  174. #else /* !CONFIG_GPIOLIB */
  175. static inline bool gpio_is_valid(int number)
  176. {
  177. /* only non-negative numbers are valid */
  178. return number >= 0;
  179. }
  180. /* platforms that don't directly support access to GPIOs through I2C, SPI,
  181. * or other blocking infrastructure can use these wrappers.
  182. */
  183. static inline int gpio_cansleep(unsigned gpio)
  184. {
  185. return 0;
  186. }
  187. static inline int gpio_get_value_cansleep(unsigned gpio)
  188. {
  189. might_sleep();
  190. return __gpio_get_value(gpio);
  191. }
  192. static inline void gpio_set_value_cansleep(unsigned gpio, int value)
  193. {
  194. might_sleep();
  195. __gpio_set_value(gpio, value);
  196. }
  197. #endif /* !CONFIG_GPIOLIB */
  198. #ifndef CONFIG_GPIO_SYSFS
  199. struct device;
  200. /* sysfs support is only available with gpiolib, where it's optional */
  201. static inline int gpio_export(unsigned gpio, bool direction_may_change)
  202. {
  203. return -ENOSYS;
  204. }
  205. static inline int gpio_export_link(struct device *dev, const char *name,
  206. unsigned gpio)
  207. {
  208. return -ENOSYS;
  209. }
  210. static inline int gpio_sysfs_set_active_low(unsigned gpio, int value)
  211. {
  212. return -ENOSYS;
  213. }
  214. static inline void gpio_unexport(unsigned gpio)
  215. {
  216. }
  217. #endif /* CONFIG_GPIO_SYSFS */
  218. #endif /* _ASM_GENERIC_GPIO_H */