driver.h 6.6 KB

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