driver.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #ifndef __LINUX_GPIO_DRIVER_H
  2. #define __LINUX_GPIO_DRIVER_H
  3. #include <linux/types.h>
  4. struct device;
  5. struct gpio_desc;
  6. /**
  7. * struct gpio_chip - abstract a GPIO controller
  8. * @label: for diagnostics
  9. * @dev: optional device providing the GPIOs
  10. * @owner: helps prevent removal of modules exporting active GPIOs
  11. * @list: links gpio_chips together for traversal
  12. * @request: optional hook for chip-specific activation, such as
  13. * enabling module power and clock; may sleep
  14. * @free: optional hook for chip-specific deactivation, such as
  15. * disabling module power and clock; may sleep
  16. * @get_direction: returns direction for signal "offset", 0=out, 1=in,
  17. * (same as GPIOF_DIR_XXX), or negative error
  18. * @direction_input: configures signal "offset" as input, or returns error
  19. * @direction_output: configures signal "offset" as output, or returns error
  20. * @get: returns value for signal "offset"; for output signals this
  21. * returns either the value actually sensed, or zero
  22. * @set: assigns output value for signal "offset"
  23. * @set_debounce: optional hook for setting debounce time for specified gpio in
  24. * interrupt triggered gpio chips
  25. * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
  26. * implementation may not sleep
  27. * @dbg_show: optional routine to show contents in debugfs; default code
  28. * will be used when this is omitted, but custom code can show extra
  29. * state (such as pullup/pulldown configuration).
  30. * @base: identifies the first GPIO number handled by this chip; or, if
  31. * negative during registration, requests dynamic ID allocation.
  32. * @ngpio: the number of GPIOs handled by this controller; the last GPIO
  33. * handled is (base + ngpio - 1).
  34. * @desc: array of ngpio descriptors. Private.
  35. * @can_sleep: flag must be set iff get()/set() methods sleep, as they
  36. * must while accessing GPIO expander chips over I2C or SPI
  37. * @names: if set, must be an array of strings to use as alternative
  38. * names for the GPIOs in this chip. Any entry in the array
  39. * may be NULL if there is no alias for the GPIO, however the
  40. * array must be @ngpio entries long. A name can include a single printk
  41. * format specifier for an unsigned int. It is substituted by the actual
  42. * number of the gpio.
  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. const char *label;
  56. struct device *dev;
  57. struct module *owner;
  58. struct list_head list;
  59. int (*request)(struct gpio_chip *chip,
  60. unsigned offset);
  61. void (*free)(struct gpio_chip *chip,
  62. unsigned offset);
  63. int (*get_direction)(struct gpio_chip *chip,
  64. unsigned offset);
  65. int (*direction_input)(struct gpio_chip *chip,
  66. unsigned offset);
  67. int (*direction_output)(struct gpio_chip *chip,
  68. unsigned offset, int value);
  69. int (*get)(struct gpio_chip *chip,
  70. unsigned offset);
  71. void (*set)(struct gpio_chip *chip,
  72. unsigned offset, int value);
  73. int (*set_debounce)(struct gpio_chip *chip,
  74. unsigned offset,
  75. unsigned debounce);
  76. int (*to_irq)(struct gpio_chip *chip,
  77. unsigned offset);
  78. void (*dbg_show)(struct seq_file *s,
  79. struct gpio_chip *chip);
  80. int base;
  81. u16 ngpio;
  82. struct gpio_desc *desc;
  83. const char *const *names;
  84. unsigned can_sleep:1;
  85. unsigned exported:1;
  86. #if defined(CONFIG_OF_GPIO)
  87. /*
  88. * If CONFIG_OF is enabled, then all GPIO controllers described in the
  89. * device tree automatically may have an OF translation
  90. */
  91. struct device_node *of_node;
  92. int of_gpio_n_cells;
  93. int (*of_xlate)(struct gpio_chip *gc,
  94. const struct of_phandle_args *gpiospec, u32 *flags);
  95. #endif
  96. #ifdef CONFIG_PINCTRL
  97. /*
  98. * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
  99. * describe the actual pin range which they serve in an SoC. This
  100. * information would be used by pinctrl subsystem to configure
  101. * corresponding pins for gpio usage.
  102. */
  103. struct list_head pin_ranges;
  104. #endif
  105. };
  106. extern const char *gpiochip_is_requested(struct gpio_chip *chip,
  107. unsigned offset);
  108. /* add/remove chips */
  109. extern int gpiochip_add(struct gpio_chip *chip);
  110. extern int __must_check gpiochip_remove(struct gpio_chip *chip);
  111. extern struct gpio_chip *gpiochip_find(void *data,
  112. int (*match)(struct gpio_chip *chip, void *data));
  113. /* lock/unlock as IRQ */
  114. int gpiod_lock_as_irq(struct gpio_desc *desc);
  115. void gpiod_unlock_as_irq(struct gpio_desc *desc);
  116. #endif