gpio.h 7.8 KB

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