gpio.h 3.9 KB

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