driver.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * @names: if set, must be an array of strings to use as alternative
  37. * names for the GPIOs in this chip. Any entry in the array
  38. * may be NULL if there is no alias for the GPIO, however the
  39. * array must be @ngpio entries long. A name can include a single printk
  40. * format specifier for an unsigned int. It is substituted by the actual
  41. * number of the gpio.
  42. * @can_sleep: flag must be set iff get()/set() methods sleep, as they
  43. * must while accessing GPIO expander chips over I2C or SPI
  44. * @exported: flags if the gpiochip is exported for use from sysfs. Private.
  45. *
  46. * A gpio_chip can help platforms abstract various sources of GPIOs so
  47. * they can all be accessed through a common programing interface.
  48. * Example sources would be SOC controllers, FPGAs, multifunction
  49. * chips, dedicated GPIO expanders, and so on.
  50. *
  51. * Each chip controls a number of signals, identified in method calls
  52. * by "offset" values in the range 0..(@ngpio - 1). When those signals
  53. * are referenced through calls like gpio_get_value(gpio), the offset
  54. * is calculated by subtracting @base from the gpio number.
  55. */
  56. struct gpio_chip {
  57. const char *label;
  58. struct device *dev;
  59. struct module *owner;
  60. struct list_head list;
  61. int (*request)(struct gpio_chip *chip,
  62. unsigned offset);
  63. void (*free)(struct gpio_chip *chip,
  64. unsigned offset);
  65. int (*get_direction)(struct gpio_chip *chip,
  66. unsigned offset);
  67. int (*direction_input)(struct gpio_chip *chip,
  68. unsigned offset);
  69. int (*direction_output)(struct gpio_chip *chip,
  70. unsigned offset, int value);
  71. int (*get)(struct gpio_chip *chip,
  72. unsigned offset);
  73. void (*set)(struct gpio_chip *chip,
  74. unsigned offset, int value);
  75. int (*set_debounce)(struct gpio_chip *chip,
  76. unsigned offset,
  77. unsigned debounce);
  78. int (*to_irq)(struct gpio_chip *chip,
  79. unsigned offset);
  80. void (*dbg_show)(struct seq_file *s,
  81. struct gpio_chip *chip);
  82. int base;
  83. u16 ngpio;
  84. struct gpio_desc *desc;
  85. const char *const *names;
  86. bool can_sleep;
  87. bool exported;
  88. #if defined(CONFIG_OF_GPIO)
  89. /*
  90. * If CONFIG_OF is enabled, then all GPIO controllers described in the
  91. * device tree automatically may have an OF translation
  92. */
  93. struct device_node *of_node;
  94. int of_gpio_n_cells;
  95. int (*of_xlate)(struct gpio_chip *gc,
  96. const struct of_phandle_args *gpiospec, u32 *flags);
  97. #endif
  98. #ifdef CONFIG_PINCTRL
  99. /*
  100. * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
  101. * describe the actual pin range which they serve in an SoC. This
  102. * information would be used by pinctrl subsystem to configure
  103. * corresponding pins for gpio usage.
  104. */
  105. struct list_head pin_ranges;
  106. #endif
  107. };
  108. extern const char *gpiochip_is_requested(struct gpio_chip *chip,
  109. unsigned offset);
  110. /* add/remove chips */
  111. extern int gpiochip_add(struct gpio_chip *chip);
  112. extern int __must_check gpiochip_remove(struct gpio_chip *chip);
  113. extern struct gpio_chip *gpiochip_find(void *data,
  114. int (*match)(struct gpio_chip *chip, void *data));
  115. /* lock/unlock as IRQ */
  116. int gpiod_lock_as_irq(struct gpio_desc *desc);
  117. void gpiod_unlock_as_irq(struct gpio_desc *desc);
  118. /**
  119. * Lookup table for associating GPIOs to specific devices and functions using
  120. * platform data.
  121. */
  122. struct gpiod_lookup {
  123. struct list_head list;
  124. /*
  125. * name of the chip the GPIO belongs to
  126. */
  127. const char *chip_label;
  128. /*
  129. * hardware number (i.e. relative to the chip) of the GPIO
  130. */
  131. u16 chip_hwnum;
  132. /*
  133. * name of device that can claim this GPIO
  134. */
  135. const char *dev_id;
  136. /*
  137. * name of the GPIO from the device's point of view
  138. */
  139. const char *con_id;
  140. /*
  141. * index of the GPIO in case several GPIOs share the same name
  142. */
  143. unsigned int idx;
  144. /*
  145. * mask of GPIOF_* values
  146. */
  147. unsigned long flags;
  148. };
  149. /*
  150. * Simple definition of a single GPIO under a con_id
  151. */
  152. #define GPIO_LOOKUP(_chip_label, _chip_hwnum, _dev_id, _con_id, _flags) \
  153. GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _dev_id, _con_id, 0, _flags)
  154. /*
  155. * Use this macro if you need to have several GPIOs under the same con_id.
  156. * Each GPIO needs to use a different index and can be accessed using
  157. * gpiod_get_index()
  158. */
  159. #define GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _dev_id, _con_id, _idx, \
  160. _flags) \
  161. { \
  162. .chip_label = _chip_label, \
  163. .chip_hwnum = _chip_hwnum, \
  164. .dev_id = _dev_id, \
  165. .con_id = _con_id, \
  166. .idx = _idx, \
  167. .flags = _flags, \
  168. }
  169. void gpiod_add_table(struct gpiod_lookup *table, size_t size);
  170. #endif