gpio.h 8.0 KB

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