driver.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. struct seq_file;
  7. /**
  8. * struct gpio_chip - abstract a GPIO controller
  9. * @label: for diagnostics
  10. * @dev: optional device providing the GPIOs
  11. * @owner: helps prevent removal of modules exporting active GPIOs
  12. * @list: links gpio_chips together for traversal
  13. * @request: optional hook for chip-specific activation, such as
  14. * enabling module power and clock; may sleep
  15. * @free: optional hook for chip-specific deactivation, such as
  16. * disabling module power and clock; may sleep
  17. * @get_direction: returns direction for signal "offset", 0=out, 1=in,
  18. * (same as GPIOF_DIR_XXX), or negative error
  19. * @direction_input: configures signal "offset" as input, or returns error
  20. * @direction_output: configures signal "offset" as output, or returns error
  21. * @get: returns value for signal "offset"; for output signals this
  22. * returns either the value actually sensed, or zero
  23. * @set: assigns output value for signal "offset"
  24. * @set_debounce: optional hook for setting debounce time for specified gpio in
  25. * interrupt triggered gpio chips
  26. * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
  27. * implementation may not sleep
  28. * @dbg_show: optional routine to show contents in debugfs; default code
  29. * will be used when this is omitted, but custom code can show extra
  30. * state (such as pullup/pulldown configuration).
  31. * @base: identifies the first GPIO number handled by this chip; or, if
  32. * negative during registration, requests dynamic ID allocation.
  33. * @ngpio: the number of GPIOs handled by this controller; the last GPIO
  34. * handled is (base + ngpio - 1).
  35. * @desc: array of ngpio descriptors. Private.
  36. * @can_sleep: flag must be set iff get()/set() methods sleep, as they
  37. * must while accessing GPIO expander chips over I2C or SPI
  38. * @names: if set, must be an array of strings to use as alternative
  39. * names for the GPIOs in this chip. Any entry in the array
  40. * may be NULL if there is no alias for the GPIO, however the
  41. * array must be @ngpio entries long. A name can include a single printk
  42. * format specifier for an unsigned int. It is substituted by the actual
  43. * number of the gpio.
  44. *
  45. * A gpio_chip can help platforms abstract various sources of GPIOs so
  46. * they can all be accessed through a common programing interface.
  47. * Example sources would be SOC controllers, FPGAs, multifunction
  48. * chips, dedicated GPIO expanders, and so on.
  49. *
  50. * Each chip controls a number of signals, identified in method calls
  51. * by "offset" values in the range 0..(@ngpio - 1). When those signals
  52. * are referenced through calls like gpio_get_value(gpio), the offset
  53. * is calculated by subtracting @base from the gpio number.
  54. */
  55. struct gpio_chip {
  56. const char *label;
  57. struct device *dev;
  58. struct module *owner;
  59. struct list_head list;
  60. int (*request)(struct gpio_chip *chip,
  61. unsigned offset);
  62. void (*free)(struct gpio_chip *chip,
  63. unsigned offset);
  64. int (*get_direction)(struct gpio_chip *chip,
  65. unsigned offset);
  66. int (*direction_input)(struct gpio_chip *chip,
  67. unsigned offset);
  68. int (*direction_output)(struct gpio_chip *chip,
  69. unsigned offset, int value);
  70. int (*get)(struct gpio_chip *chip,
  71. unsigned offset);
  72. void (*set)(struct gpio_chip *chip,
  73. unsigned offset, int value);
  74. int (*set_debounce)(struct gpio_chip *chip,
  75. unsigned offset,
  76. unsigned debounce);
  77. int (*to_irq)(struct gpio_chip *chip,
  78. unsigned offset);
  79. void (*dbg_show)(struct seq_file *s,
  80. struct gpio_chip *chip);
  81. int base;
  82. u16 ngpio;
  83. struct gpio_desc *desc;
  84. const char *const *names;
  85. unsigned can_sleep:1;
  86. unsigned exported:1;
  87. #if defined(CONFIG_OF_GPIO)
  88. /*
  89. * If CONFIG_OF is enabled, then all GPIO controllers described in the
  90. * device tree automatically may have an OF translation
  91. */
  92. struct device_node *of_node;
  93. int of_gpio_n_cells;
  94. int (*of_xlate)(struct gpio_chip *gc,
  95. const struct of_phandle_args *gpiospec, u32 *flags);
  96. #endif
  97. #ifdef CONFIG_PINCTRL
  98. /*
  99. * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
  100. * describe the actual pin range which they serve in an SoC. This
  101. * information would be used by pinctrl subsystem to configure
  102. * corresponding pins for gpio usage.
  103. */
  104. struct list_head pin_ranges;
  105. #endif
  106. };
  107. extern const char *gpiochip_is_requested(struct gpio_chip *chip,
  108. unsigned offset);
  109. /* add/remove chips */
  110. extern int gpiochip_add(struct gpio_chip *chip);
  111. extern int __must_check gpiochip_remove(struct gpio_chip *chip);
  112. extern struct gpio_chip *gpiochip_find(void *data,
  113. int (*match)(struct gpio_chip *chip, void *data));
  114. /* lock/unlock as IRQ */
  115. int gpiod_lock_as_irq(struct gpio_desc *desc);
  116. void gpiod_unlock_as_irq(struct gpio_desc *desc);
  117. enum gpio_lookup_flags {
  118. GPIO_ACTIVE_HIGH = (0 << 0),
  119. GPIO_ACTIVE_LOW = (1 << 0),
  120. GPIO_OPEN_DRAIN = (1 << 1),
  121. GPIO_OPEN_SOURCE = (1 << 2),
  122. };
  123. /**
  124. * Lookup table for associating GPIOs to specific devices and functions using
  125. * platform data.
  126. */
  127. struct gpiod_lookup {
  128. struct list_head list;
  129. /*
  130. * name of the chip the GPIO belongs to
  131. */
  132. const char *chip_label;
  133. /*
  134. * hardware number (i.e. relative to the chip) of the GPIO
  135. */
  136. u16 chip_hwnum;
  137. /*
  138. * name of device that can claim this GPIO
  139. */
  140. const char *dev_id;
  141. /*
  142. * name of the GPIO from the device's point of view
  143. */
  144. const char *con_id;
  145. /*
  146. * index of the GPIO in case several GPIOs share the same name
  147. */
  148. unsigned int idx;
  149. /*
  150. * mask of GPIO_* values
  151. */
  152. enum gpio_lookup_flags flags;
  153. };
  154. /*
  155. * Simple definition of a single GPIO under a con_id
  156. */
  157. #define GPIO_LOOKUP(_chip_label, _chip_hwnum, _dev_id, _con_id, _flags) \
  158. GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _dev_id, _con_id, 0, _flags)
  159. /*
  160. * Use this macro if you need to have several GPIOs under the same con_id.
  161. * Each GPIO needs to use a different index and can be accessed using
  162. * gpiod_get_index()
  163. */
  164. #define GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _dev_id, _con_id, _idx, \
  165. _flags) \
  166. { \
  167. .chip_label = _chip_label, \
  168. .chip_hwnum = _chip_hwnum, \
  169. .dev_id = _dev_id, \
  170. .con_id = _con_id, \
  171. .idx = _idx, \
  172. .flags = _flags, \
  173. }
  174. void gpiod_add_table(struct gpiod_lookup *table, size_t size);
  175. #endif