gpio.h 3.9 KB

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