gpio.h 6.1 KB

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