gpio.h 5.9 KB

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