gpio.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.
  56. *
  57. * A gpio_chip can help platforms abstract various sources of GPIOs so
  58. * they can all be accessed through a common programing interface.
  59. * Example sources would be SOC controllers, FPGAs, multifunction
  60. * chips, dedicated GPIO expanders, and so on.
  61. *
  62. * Each chip controls a number of signals, identified in method calls
  63. * by "offset" values in the range 0..(@ngpio - 1). When those signals
  64. * are referenced through calls like gpio_get_value(gpio), the offset
  65. * is calculated by subtracting @base from the gpio number.
  66. */
  67. struct gpio_chip {
  68. const char *label;
  69. struct device *dev;
  70. struct module *owner;
  71. int (*request)(struct gpio_chip *chip,
  72. unsigned offset);
  73. void (*free)(struct gpio_chip *chip,
  74. unsigned offset);
  75. int (*direction_input)(struct gpio_chip *chip,
  76. unsigned offset);
  77. int (*get)(struct gpio_chip *chip,
  78. unsigned offset);
  79. int (*direction_output)(struct gpio_chip *chip,
  80. unsigned offset, int value);
  81. void (*set)(struct gpio_chip *chip,
  82. unsigned offset, int value);
  83. int (*to_irq)(struct gpio_chip *chip,
  84. unsigned offset);
  85. void (*dbg_show)(struct seq_file *s,
  86. struct gpio_chip *chip);
  87. int base;
  88. u16 ngpio;
  89. char **names;
  90. unsigned can_sleep:1;
  91. unsigned exported:1;
  92. };
  93. extern const char *gpiochip_is_requested(struct gpio_chip *chip,
  94. unsigned offset);
  95. extern int __must_check gpiochip_reserve(int start, int ngpio);
  96. /* add/remove chips */
  97. extern int gpiochip_add(struct gpio_chip *chip);
  98. extern int __must_check gpiochip_remove(struct gpio_chip *chip);
  99. /* Always use the library code for GPIO management calls,
  100. * or when sleeping may be involved.
  101. */
  102. extern int gpio_request(unsigned gpio, const char *label);
  103. extern void gpio_free(unsigned gpio);
  104. extern int gpio_direction_input(unsigned gpio);
  105. extern int gpio_direction_output(unsigned gpio, int value);
  106. extern int gpio_get_value_cansleep(unsigned gpio);
  107. extern void gpio_set_value_cansleep(unsigned gpio, int value);
  108. /* A platform's <asm/gpio.h> code may want to inline the I/O calls when
  109. * the GPIO is constant and refers to some always-present controller,
  110. * giving direct access to chip registers and tight bitbanging loops.
  111. */
  112. extern int __gpio_get_value(unsigned gpio);
  113. extern void __gpio_set_value(unsigned gpio, int value);
  114. extern int __gpio_cansleep(unsigned gpio);
  115. extern int __gpio_to_irq(unsigned gpio);
  116. #define GPIOF_DIR_OUT (0 << 0)
  117. #define GPIOF_DIR_IN (1 << 0)
  118. #define GPIOF_INIT_LOW (0 << 1)
  119. #define GPIOF_INIT_HIGH (1 << 1)
  120. #define GPIOF_IN (GPIOF_DIR_IN)
  121. #define GPIOF_OUT_INIT_LOW (GPIOF_DIR_OUT | GPIOF_INIT_LOW)
  122. #define GPIOF_OUT_INIT_HIGH (GPIOF_DIR_OUT | GPIOF_INIT_HIGH)
  123. /**
  124. * struct gpio - a structure describing a GPIO with configuration
  125. * @gpio: the GPIO number
  126. * @flags: GPIO configuration as specified by GPIOF_*
  127. * @label: a literal description string of this GPIO
  128. */
  129. struct gpio {
  130. unsigned gpio;
  131. unsigned long flags;
  132. const char *label;
  133. };
  134. extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
  135. extern int gpio_request_array(struct gpio *array, size_t num);
  136. extern void gpio_free_array(struct gpio *array, size_t num);
  137. #ifdef CONFIG_GPIO_SYSFS
  138. /*
  139. * A sysfs interface can be exported by individual drivers if they want,
  140. * but more typically is configured entirely from userspace.
  141. */
  142. extern int gpio_export(unsigned gpio, bool direction_may_change);
  143. extern int gpio_export_link(struct device *dev, const char *name,
  144. unsigned gpio);
  145. extern int gpio_sysfs_set_active_low(unsigned gpio, int value);
  146. extern void gpio_unexport(unsigned gpio);
  147. #endif /* CONFIG_GPIO_SYSFS */
  148. #else /* !CONFIG_HAVE_GPIO_LIB */
  149. static inline int gpio_is_valid(int number)
  150. {
  151. /* only non-negative numbers are valid */
  152. return number >= 0;
  153. }
  154. /* platforms that don't directly support access to GPIOs through I2C, SPI,
  155. * or other blocking infrastructure can use these wrappers.
  156. */
  157. static inline int gpio_cansleep(unsigned gpio)
  158. {
  159. return 0;
  160. }
  161. static inline int gpio_get_value_cansleep(unsigned gpio)
  162. {
  163. might_sleep();
  164. return gpio_get_value(gpio);
  165. }
  166. static inline void gpio_set_value_cansleep(unsigned gpio, int value)
  167. {
  168. might_sleep();
  169. gpio_set_value(gpio, value);
  170. }
  171. #endif /* !CONFIG_HAVE_GPIO_LIB */
  172. #ifndef CONFIG_GPIO_SYSFS
  173. struct device;
  174. /* sysfs support is only available with gpiolib, where it's optional */
  175. static inline int gpio_export(unsigned gpio, bool direction_may_change)
  176. {
  177. return -ENOSYS;
  178. }
  179. static inline int gpio_export_link(struct device *dev, const char *name,
  180. unsigned gpio)
  181. {
  182. return -ENOSYS;
  183. }
  184. static inline int gpio_sysfs_set_active_low(unsigned gpio, int value)
  185. {
  186. return -ENOSYS;
  187. }
  188. static inline void gpio_unexport(unsigned gpio)
  189. {
  190. }
  191. #endif /* CONFIG_GPIO_SYSFS */
  192. #endif /* _ASM_GENERIC_GPIO_H */