gpio.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. *
  51. * A gpio_chip can help platforms abstract various sources of GPIOs so
  52. * they can all be accessed through a common programing interface.
  53. * Example sources would be SOC controllers, FPGAs, multifunction
  54. * chips, dedicated GPIO expanders, and so on.
  55. *
  56. * Each chip controls a number of signals, identified in method calls
  57. * by "offset" values in the range 0..(@ngpio - 1). When those signals
  58. * are referenced through calls like gpio_get_value(gpio), the offset
  59. * is calculated by subtracting @base from the gpio number.
  60. */
  61. struct gpio_chip {
  62. const char *label;
  63. struct device *dev;
  64. struct module *owner;
  65. int (*request)(struct gpio_chip *chip,
  66. unsigned offset);
  67. void (*free)(struct gpio_chip *chip,
  68. unsigned offset);
  69. int (*direction_input)(struct gpio_chip *chip,
  70. unsigned offset);
  71. int (*get)(struct gpio_chip *chip,
  72. unsigned offset);
  73. int (*direction_output)(struct gpio_chip *chip,
  74. unsigned offset, int value);
  75. void (*set)(struct gpio_chip *chip,
  76. unsigned offset, int value);
  77. int (*to_irq)(struct gpio_chip *chip,
  78. unsigned offset);
  79. void (*dbg_show)(struct seq_file *s,
  80. struct gpio_chip *chip);
  81. int base;
  82. u16 ngpio;
  83. unsigned can_sleep:1;
  84. unsigned exported:1;
  85. };
  86. extern const char *gpiochip_is_requested(struct gpio_chip *chip,
  87. unsigned offset);
  88. extern int __must_check gpiochip_reserve(int start, int ngpio);
  89. /* add/remove chips */
  90. extern int gpiochip_add(struct gpio_chip *chip);
  91. extern int __must_check gpiochip_remove(struct gpio_chip *chip);
  92. /* Always use the library code for GPIO management calls,
  93. * or when sleeping may be involved.
  94. */
  95. extern int gpio_request(unsigned gpio, const char *label);
  96. extern void gpio_free(unsigned gpio);
  97. extern int gpio_direction_input(unsigned gpio);
  98. extern int gpio_direction_output(unsigned gpio, int value);
  99. extern int gpio_get_value_cansleep(unsigned gpio);
  100. extern void gpio_set_value_cansleep(unsigned gpio, int value);
  101. /* A platform's <asm/gpio.h> code may want to inline the I/O calls when
  102. * the GPIO is constant and refers to some always-present controller,
  103. * giving direct access to chip registers and tight bitbanging loops.
  104. */
  105. extern int __gpio_get_value(unsigned gpio);
  106. extern void __gpio_set_value(unsigned gpio, int value);
  107. extern int __gpio_cansleep(unsigned gpio);
  108. extern int __gpio_to_irq(unsigned gpio);
  109. #ifdef CONFIG_GPIO_SYSFS
  110. /*
  111. * A sysfs interface can be exported by individual drivers if they want,
  112. * but more typically is configured entirely from userspace.
  113. */
  114. extern int gpio_export(unsigned gpio, bool direction_may_change);
  115. extern void gpio_unexport(unsigned gpio);
  116. #endif /* CONFIG_GPIO_SYSFS */
  117. #else /* !CONFIG_HAVE_GPIO_LIB */
  118. static inline int gpio_is_valid(int number)
  119. {
  120. /* only non-negative numbers are valid */
  121. return number >= 0;
  122. }
  123. /* platforms that don't directly support access to GPIOs through I2C, SPI,
  124. * or other blocking infrastructure can use these wrappers.
  125. */
  126. static inline int gpio_cansleep(unsigned gpio)
  127. {
  128. return 0;
  129. }
  130. static inline int gpio_get_value_cansleep(unsigned gpio)
  131. {
  132. might_sleep();
  133. return gpio_get_value(gpio);
  134. }
  135. static inline void gpio_set_value_cansleep(unsigned gpio, int value)
  136. {
  137. might_sleep();
  138. gpio_set_value(gpio, value);
  139. }
  140. #endif /* !CONFIG_HAVE_GPIO_LIB */
  141. #ifndef CONFIG_GPIO_SYSFS
  142. /* sysfs support is only available with gpiolib, where it's optional */
  143. static inline int gpio_export(unsigned gpio, bool direction_may_change)
  144. {
  145. return -ENOSYS;
  146. }
  147. static inline void gpio_unexport(unsigned gpio)
  148. {
  149. }
  150. #endif /* CONFIG_GPIO_SYSFS */
  151. #endif /* _ASM_GENERIC_GPIO_H */