driver.h 7.8 KB

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