driver.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. /**
  118. * Lookup table for associating GPIOs to specific devices and functions using
  119. * platform data.
  120. */
  121. struct gpiod_lookup {
  122. struct list_head list;
  123. /*
  124. * name of the chip the GPIO belongs to
  125. */
  126. const char *chip_label;
  127. /*
  128. * hardware number (i.e. relative to the chip) of the GPIO
  129. */
  130. u16 chip_hwnum;
  131. /*
  132. * name of device that can claim this GPIO
  133. */
  134. const char *dev_id;
  135. /*
  136. * name of the GPIO from the device's point of view
  137. */
  138. const char *con_id;
  139. /*
  140. * index of the GPIO in case several GPIOs share the same name
  141. */
  142. unsigned int idx;
  143. /*
  144. * mask of GPIOF_* values
  145. */
  146. unsigned long flags;
  147. };
  148. /*
  149. * Simple definition of a single GPIO under a con_id
  150. */
  151. #define GPIO_LOOKUP(_chip_label, _chip_hwnum, _dev_id, _con_id, _flags) \
  152. GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _dev_id, _con_id, 0, _flags)
  153. /*
  154. * Use this macro if you need to have several GPIOs under the same con_id.
  155. * Each GPIO needs to use a different index and can be accessed using
  156. * gpiod_get_index()
  157. */
  158. #define GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _dev_id, _con_id, _idx, \
  159. _flags) \
  160. { \
  161. .chip_label = _chip_label, \
  162. .chip_hwnum = _chip_hwnum, \
  163. .dev_id = _dev_id, \
  164. .con_id = _con_id, \
  165. .idx = _idx, \
  166. .flags = _flags, \
  167. }
  168. void gpiod_add_table(struct gpiod_lookup *table, size_t size);
  169. #endif