gpio.h 5.1 KB

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