driver.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. This
  51. * implies that if the chip supports IRQs, these IRQs need to be threaded
  52. * as the chip access may sleep when e.g. reading out the IRQ status
  53. * registers.
  54. * @exported: flags if the gpiochip is exported for use from sysfs. Private.
  55. * @irq_not_threaded: flag must be set if @can_sleep is set but the
  56. * IRQs don't need to be threaded
  57. *
  58. * A gpio_chip can help platforms abstract various sources of GPIOs so
  59. * they can all be accessed through a common programing interface.
  60. * Example sources would be SOC controllers, FPGAs, multifunction
  61. * chips, dedicated GPIO expanders, and so on.
  62. *
  63. * Each chip controls a number of signals, identified in method calls
  64. * by "offset" values in the range 0..(@ngpio - 1). When those signals
  65. * are referenced through calls like gpio_get_value(gpio), the offset
  66. * is calculated by subtracting @base from the gpio number.
  67. */
  68. struct gpio_chip {
  69. const char *label;
  70. struct device *dev;
  71. struct module *owner;
  72. struct list_head list;
  73. int (*request)(struct gpio_chip *chip,
  74. unsigned offset);
  75. void (*free)(struct gpio_chip *chip,
  76. unsigned offset);
  77. int (*get_direction)(struct gpio_chip *chip,
  78. unsigned offset);
  79. int (*direction_input)(struct gpio_chip *chip,
  80. unsigned offset);
  81. int (*direction_output)(struct gpio_chip *chip,
  82. unsigned offset, int value);
  83. int (*get)(struct gpio_chip *chip,
  84. unsigned offset);
  85. void (*set)(struct gpio_chip *chip,
  86. unsigned offset, int value);
  87. int (*set_debounce)(struct gpio_chip *chip,
  88. unsigned offset,
  89. unsigned debounce);
  90. int (*to_irq)(struct gpio_chip *chip,
  91. unsigned offset);
  92. void (*dbg_show)(struct seq_file *s,
  93. struct gpio_chip *chip);
  94. int base;
  95. u16 ngpio;
  96. struct gpio_desc *desc;
  97. const char *const *names;
  98. bool can_sleep;
  99. bool irq_not_threaded;
  100. bool exported;
  101. #ifdef CONFIG_GPIOLIB_IRQCHIP
  102. /*
  103. * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip inside the gpiolib
  104. * to handle IRQs for most practical cases.
  105. */
  106. struct irq_chip *irqchip;
  107. struct irq_domain *irqdomain;
  108. unsigned int irq_base;
  109. irq_flow_handler_t irq_handler;
  110. unsigned int irq_default_type;
  111. #endif
  112. #if defined(CONFIG_OF_GPIO)
  113. /*
  114. * If CONFIG_OF is enabled, then all GPIO controllers described in the
  115. * device tree automatically may have an OF translation
  116. */
  117. struct device_node *of_node;
  118. int of_gpio_n_cells;
  119. int (*of_xlate)(struct gpio_chip *gc,
  120. const struct of_phandle_args *gpiospec, u32 *flags);
  121. #endif
  122. #ifdef CONFIG_PINCTRL
  123. /*
  124. * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
  125. * describe the actual pin range which they serve in an SoC. This
  126. * information would be used by pinctrl subsystem to configure
  127. * corresponding pins for gpio usage.
  128. */
  129. struct list_head pin_ranges;
  130. #endif
  131. };
  132. extern const char *gpiochip_is_requested(struct gpio_chip *chip,
  133. unsigned offset);
  134. /* add/remove chips */
  135. extern int gpiochip_add(struct gpio_chip *chip);
  136. extern void gpiochip_remove(struct gpio_chip *chip);
  137. extern struct gpio_chip *gpiochip_find(void *data,
  138. int (*match)(struct gpio_chip *chip, void *data));
  139. /* lock/unlock as IRQ */
  140. int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset);
  141. void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset);
  142. struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
  143. #ifdef CONFIG_GPIOLIB_IRQCHIP
  144. void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
  145. struct irq_chip *irqchip,
  146. int parent_irq,
  147. irq_flow_handler_t parent_handler);
  148. int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
  149. struct irq_chip *irqchip,
  150. unsigned int first_irq,
  151. irq_flow_handler_t handler,
  152. unsigned int type);
  153. #endif /* CONFIG_GPIOLIB_IRQCHIP */
  154. struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
  155. const char *label);
  156. void gpiochip_free_own_desc(struct gpio_desc *desc);
  157. #else /* CONFIG_GPIOLIB */
  158. static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
  159. {
  160. /* GPIO can never have been requested */
  161. WARN_ON(1);
  162. return ERR_PTR(-ENODEV);
  163. }
  164. #endif /* CONFIG_GPIOLIB */
  165. #endif