gpio.h 5.1 KB

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