gpio.h 9.6 KB

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