gpio.h 4.3 KB

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