gpio.h 4.2 KB

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