gpio.h 7.4 KB

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