driver.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #include <linux/lockdep.h>
  9. #include <linux/pinctrl/pinctrl.h>
  10. struct device;
  11. struct gpio_desc;
  12. struct of_phandle_args;
  13. struct device_node;
  14. struct seq_file;
  15. #ifdef CONFIG_GPIOLIB
  16. /**
  17. * struct gpio_chip - abstract a GPIO controller
  18. * @label: for diagnostics
  19. * @dev: optional device providing the GPIOs
  20. * @cdev: class device used by sysfs interface (may be NULL)
  21. * @owner: helps prevent removal of modules exporting active GPIOs
  22. * @list: links gpio_chips together for traversal
  23. * @request: optional hook for chip-specific activation, such as
  24. * enabling module power and clock; may sleep
  25. * @free: optional hook for chip-specific deactivation, such as
  26. * disabling module power and clock; may sleep
  27. * @get_direction: returns direction for signal "offset", 0=out, 1=in,
  28. * (same as GPIOF_DIR_XXX), or negative error
  29. * @direction_input: configures signal "offset" as input, or returns error
  30. * @direction_output: configures signal "offset" as output, or returns error
  31. * @get: returns value for signal "offset"; for output signals this
  32. * returns either the value actually sensed, or zero
  33. * @set: assigns output value for signal "offset"
  34. * @set_multiple: assigns output values for multiple signals defined by "mask"
  35. * @set_debounce: optional hook for setting debounce time for specified gpio in
  36. * interrupt triggered gpio chips
  37. * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
  38. * implementation may not sleep
  39. * @dbg_show: optional routine to show contents in debugfs; default code
  40. * will be used when this is omitted, but custom code can show extra
  41. * state (such as pullup/pulldown configuration).
  42. * @base: identifies the first GPIO number handled by this chip;
  43. * or, if negative during registration, requests dynamic ID allocation.
  44. * DEPRECATION: providing anything non-negative and nailing the base
  45. * offset of GPIO chips is deprecated. Please pass -1 as base to
  46. * let gpiolib select the chip base in all possible cases. We want to
  47. * get rid of the static GPIO number space in the long run.
  48. * @ngpio: the number of GPIOs handled by this controller; the last GPIO
  49. * handled is (base + ngpio - 1).
  50. * @desc: array of ngpio descriptors. Private.
  51. * @names: if set, must be an array of strings to use as alternative
  52. * names for the GPIOs in this chip. Any entry in the array
  53. * may be NULL if there is no alias for the GPIO, however the
  54. * array must be @ngpio entries long. A name can include a single printk
  55. * format specifier for an unsigned int. It is substituted by the actual
  56. * number of the gpio.
  57. * @can_sleep: flag must be set iff get()/set() methods sleep, as they
  58. * must while accessing GPIO expander chips over I2C or SPI. This
  59. * implies that if the chip supports IRQs, these IRQs need to be threaded
  60. * as the chip access may sleep when e.g. reading out the IRQ status
  61. * registers.
  62. * @irq_not_threaded: flag must be set if @can_sleep is set but the
  63. * IRQs don't need to be threaded
  64. * @irqchip: GPIO IRQ chip impl, provided by GPIO driver
  65. * @irqdomain: Interrupt translation domain; responsible for mapping
  66. * between GPIO hwirq number and linux irq number
  67. * @irq_base: first linux IRQ number assigned to GPIO IRQ chip (deprecated)
  68. * @irq_handler: the irq handler to use (often a predefined irq core function)
  69. * for GPIO IRQs, provided by GPIO driver
  70. * @irq_default_type: default IRQ triggering type applied during GPIO driver
  71. * initialization, provided by GPIO driver
  72. * @irq_parent: GPIO IRQ chip parent/bank linux irq number,
  73. * provided by GPIO driver
  74. * @lock_key: per GPIO IRQ chip lockdep class
  75. *
  76. * A gpio_chip can help platforms abstract various sources of GPIOs so
  77. * they can all be accessed through a common programing interface.
  78. * Example sources would be SOC controllers, FPGAs, multifunction
  79. * chips, dedicated GPIO expanders, and so on.
  80. *
  81. * Each chip controls a number of signals, identified in method calls
  82. * by "offset" values in the range 0..(@ngpio - 1). When those signals
  83. * are referenced through calls like gpio_get_value(gpio), the offset
  84. * is calculated by subtracting @base from the gpio number.
  85. */
  86. struct gpio_chip {
  87. const char *label;
  88. struct device *dev;
  89. struct device *cdev;
  90. struct module *owner;
  91. struct list_head list;
  92. int (*request)(struct gpio_chip *chip,
  93. unsigned offset);
  94. void (*free)(struct gpio_chip *chip,
  95. unsigned offset);
  96. int (*get_direction)(struct gpio_chip *chip,
  97. unsigned offset);
  98. int (*direction_input)(struct gpio_chip *chip,
  99. unsigned offset);
  100. int (*direction_output)(struct gpio_chip *chip,
  101. unsigned offset, int value);
  102. int (*get)(struct gpio_chip *chip,
  103. unsigned offset);
  104. void (*set)(struct gpio_chip *chip,
  105. unsigned offset, int value);
  106. void (*set_multiple)(struct gpio_chip *chip,
  107. unsigned long *mask,
  108. unsigned long *bits);
  109. int (*set_debounce)(struct gpio_chip *chip,
  110. unsigned offset,
  111. unsigned debounce);
  112. int (*to_irq)(struct gpio_chip *chip,
  113. unsigned offset);
  114. void (*dbg_show)(struct seq_file *s,
  115. struct gpio_chip *chip);
  116. int base;
  117. u16 ngpio;
  118. struct gpio_desc *desc;
  119. const char *const *names;
  120. bool can_sleep;
  121. bool irq_not_threaded;
  122. #ifdef CONFIG_GPIOLIB_IRQCHIP
  123. /*
  124. * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip inside the gpiolib
  125. * to handle IRQs for most practical cases.
  126. */
  127. struct irq_chip *irqchip;
  128. struct irq_domain *irqdomain;
  129. unsigned int irq_base;
  130. irq_flow_handler_t irq_handler;
  131. unsigned int irq_default_type;
  132. int irq_parent;
  133. struct lock_class_key *lock_key;
  134. #endif
  135. #if defined(CONFIG_OF_GPIO)
  136. /*
  137. * If CONFIG_OF is enabled, then all GPIO controllers described in the
  138. * device tree automatically may have an OF translation
  139. */
  140. struct device_node *of_node;
  141. int of_gpio_n_cells;
  142. int (*of_xlate)(struct gpio_chip *gc,
  143. const struct of_phandle_args *gpiospec, u32 *flags);
  144. #endif
  145. #ifdef CONFIG_PINCTRL
  146. /*
  147. * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
  148. * describe the actual pin range which they serve in an SoC. This
  149. * information would be used by pinctrl subsystem to configure
  150. * corresponding pins for gpio usage.
  151. */
  152. struct list_head pin_ranges;
  153. #endif
  154. };
  155. extern const char *gpiochip_is_requested(struct gpio_chip *chip,
  156. unsigned offset);
  157. /* add/remove chips */
  158. extern int gpiochip_add(struct gpio_chip *chip);
  159. extern void gpiochip_remove(struct gpio_chip *chip);
  160. extern struct gpio_chip *gpiochip_find(void *data,
  161. int (*match)(struct gpio_chip *chip, void *data));
  162. /* lock/unlock as IRQ */
  163. int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset);
  164. void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset);
  165. struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
  166. #ifdef CONFIG_GPIOLIB_IRQCHIP
  167. void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
  168. struct irq_chip *irqchip,
  169. int parent_irq,
  170. irq_flow_handler_t parent_handler);
  171. int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
  172. struct irq_chip *irqchip,
  173. unsigned int first_irq,
  174. irq_flow_handler_t handler,
  175. unsigned int type,
  176. struct lock_class_key *lock_key);
  177. #ifdef CONFIG_LOCKDEP
  178. #define gpiochip_irqchip_add(...) \
  179. ( \
  180. ({ \
  181. static struct lock_class_key _key; \
  182. _gpiochip_irqchip_add(__VA_ARGS__, &_key); \
  183. }) \
  184. )
  185. #else
  186. #define gpiochip_irqchip_add(...) \
  187. _gpiochip_irqchip_add(__VA_ARGS__, NULL)
  188. #endif
  189. #endif /* CONFIG_GPIOLIB_IRQCHIP */
  190. #ifdef CONFIG_PINCTRL
  191. /**
  192. * struct gpio_pin_range - pin range controlled by a gpio chip
  193. * @head: list for maintaining set of pin ranges, used internally
  194. * @pctldev: pinctrl device which handles corresponding pins
  195. * @range: actual range of pins controlled by a gpio controller
  196. */
  197. struct gpio_pin_range {
  198. struct list_head node;
  199. struct pinctrl_dev *pctldev;
  200. struct pinctrl_gpio_range range;
  201. };
  202. int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  203. unsigned int gpio_offset, unsigned int pin_offset,
  204. unsigned int npins);
  205. int gpiochip_add_pingroup_range(struct gpio_chip *chip,
  206. struct pinctrl_dev *pctldev,
  207. unsigned int gpio_offset, const char *pin_group);
  208. void gpiochip_remove_pin_ranges(struct gpio_chip *chip);
  209. #else
  210. static inline int
  211. gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  212. unsigned int gpio_offset, unsigned int pin_offset,
  213. unsigned int npins)
  214. {
  215. return 0;
  216. }
  217. static inline int
  218. gpiochip_add_pingroup_range(struct gpio_chip *chip,
  219. struct pinctrl_dev *pctldev,
  220. unsigned int gpio_offset, const char *pin_group)
  221. {
  222. return 0;
  223. }
  224. static inline void
  225. gpiochip_remove_pin_ranges(struct gpio_chip *chip)
  226. {
  227. }
  228. #endif /* CONFIG_PINCTRL */
  229. struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
  230. const char *label);
  231. void gpiochip_free_own_desc(struct gpio_desc *desc);
  232. #else /* CONFIG_GPIOLIB */
  233. static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
  234. {
  235. /* GPIO can never have been requested */
  236. WARN_ON(1);
  237. return ERR_PTR(-ENODEV);
  238. }
  239. #endif /* CONFIG_GPIOLIB */
  240. #endif