gpiolib.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Internal GPIO functions.
  4. *
  5. * Copyright (C) 2013, Intel Corporation
  6. * Author: Mika Westerberg <[email protected]>
  7. */
  8. #ifndef GPIOLIB_H
  9. #define GPIOLIB_H
  10. #include <linux/gpio/driver.h>
  11. #include <linux/gpio/consumer.h> /* for enum gpiod_flags */
  12. #include <linux/err.h>
  13. #include <linux/device.h>
  14. #include <linux/module.h>
  15. #include <linux/cdev.h>
  16. #include <linux/rwsem.h>
  17. #include <linux/android_kabi.h>
  18. #define GPIOCHIP_NAME "gpiochip"
  19. /**
  20. * struct gpio_device - internal state container for GPIO devices
  21. * @id: numerical ID number for the GPIO chip
  22. * @dev: the GPIO device struct
  23. * @chrdev: character device for the GPIO device
  24. * @mockdev: class device used by the deprecated sysfs interface (may be
  25. * NULL)
  26. * @owner: helps prevent removal of modules exporting active GPIOs
  27. * @chip: pointer to the corresponding gpiochip, holding static
  28. * data for this device
  29. * @descs: array of ngpio descriptors.
  30. * @ngpio: the number of GPIO lines on this GPIO device, equal to the size
  31. * of the @descs array.
  32. * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned
  33. * at device creation time.
  34. * @label: a descriptive name for the GPIO device, such as the part number
  35. * or name of the IP component in a System on Chip.
  36. * @data: per-instance data assigned by the driver
  37. * @list: links gpio_device:s together for traversal
  38. * @notifier: used to notify subscribers about lines being requested, released
  39. * or reconfigured
  40. * @sem: protects the structure from a NULL-pointer dereference of @chip by
  41. * user-space operations when the device gets unregistered during
  42. * a hot-unplug event
  43. * @pin_ranges: range of pins served by the GPIO driver
  44. *
  45. * This state container holds most of the runtime variable data
  46. * for a GPIO device and can hold references and live on after the
  47. * GPIO chip has been removed, if it is still being used from
  48. * userspace.
  49. */
  50. struct gpio_device {
  51. int id;
  52. struct device dev;
  53. struct cdev chrdev;
  54. struct device *mockdev;
  55. struct module *owner;
  56. struct gpio_chip *chip;
  57. struct gpio_desc *descs;
  58. int base;
  59. u16 ngpio;
  60. const char *label;
  61. void *data;
  62. struct list_head list;
  63. struct blocking_notifier_head notifier;
  64. struct rw_semaphore sem;
  65. #ifdef CONFIG_PINCTRL
  66. /*
  67. * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
  68. * describe the actual pin range which they serve in an SoC. This
  69. * information would be used by pinctrl subsystem to configure
  70. * corresponding pins for gpio usage.
  71. */
  72. struct list_head pin_ranges;
  73. #endif
  74. ANDROID_KABI_RESERVE(1);
  75. };
  76. /* gpio suffixes used for ACPI and device tree lookup */
  77. static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };
  78. /**
  79. * struct gpio_array - Opaque descriptor for a structure of GPIO array attributes
  80. *
  81. * @desc: Array of pointers to the GPIO descriptors
  82. * @size: Number of elements in desc
  83. * @chip: Parent GPIO chip
  84. * @get_mask: Get mask used in fastpath
  85. * @set_mask: Set mask used in fastpath
  86. * @invert_mask: Invert mask used in fastpath
  87. *
  88. * This structure is attached to struct gpiod_descs obtained from
  89. * gpiod_get_array() and can be passed back to get/set array functions in order
  90. * to activate fast processing path if applicable.
  91. */
  92. struct gpio_array {
  93. struct gpio_desc **desc;
  94. unsigned int size;
  95. struct gpio_chip *chip;
  96. unsigned long *get_mask;
  97. unsigned long *set_mask;
  98. ANDROID_KABI_RESERVE(1);
  99. unsigned long invert_mask[];
  100. };
  101. struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum);
  102. #define for_each_gpio_desc(gc, desc) \
  103. for (unsigned int __i = 0; \
  104. __i < gc->ngpio && (desc = gpiochip_get_desc(gc, __i)); \
  105. __i++) \
  106. #define for_each_gpio_desc_with_flag(gc, desc, flag) \
  107. for_each_gpio_desc(gc, desc) \
  108. if (!test_bit(flag, &desc->flags)) {} else
  109. int gpiod_get_array_value_complex(bool raw, bool can_sleep,
  110. unsigned int array_size,
  111. struct gpio_desc **desc_array,
  112. struct gpio_array *array_info,
  113. unsigned long *value_bitmap);
  114. int gpiod_set_array_value_complex(bool raw, bool can_sleep,
  115. unsigned int array_size,
  116. struct gpio_desc **desc_array,
  117. struct gpio_array *array_info,
  118. unsigned long *value_bitmap);
  119. extern spinlock_t gpio_lock;
  120. extern struct list_head gpio_devices;
  121. /**
  122. * struct gpio_desc - Opaque descriptor for a GPIO
  123. *
  124. * @gdev: Pointer to the parent GPIO device
  125. * @flags: Binary descriptor flags
  126. * @label: Name of the consumer
  127. * @name: Line name
  128. * @hog: Pointer to the device node that hogs this line (if any)
  129. * @debounce_period_us: Debounce period in microseconds
  130. *
  131. * These are obtained using gpiod_get() and are preferable to the old
  132. * integer-based handles.
  133. *
  134. * Contrary to integers, a pointer to a &struct gpio_desc is guaranteed to be
  135. * valid until the GPIO is released.
  136. */
  137. struct gpio_desc {
  138. struct gpio_device *gdev;
  139. unsigned long flags;
  140. /* flag symbols are bit numbers */
  141. #define FLAG_REQUESTED 0
  142. #define FLAG_IS_OUT 1
  143. #define FLAG_EXPORT 2 /* protected by sysfs_lock */
  144. #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
  145. #define FLAG_ACTIVE_LOW 6 /* value has active low */
  146. #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
  147. #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
  148. #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
  149. #define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */
  150. #define FLAG_IS_HOGGED 11 /* GPIO is hogged */
  151. #define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
  152. #define FLAG_PULL_UP 13 /* GPIO has pull up enabled */
  153. #define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */
  154. #define FLAG_BIAS_DISABLE 15 /* GPIO has pull disabled */
  155. #define FLAG_EDGE_RISING 16 /* GPIO CDEV detects rising edge events */
  156. #define FLAG_EDGE_FALLING 17 /* GPIO CDEV detects falling edge events */
  157. #define FLAG_EVENT_CLOCK_REALTIME 18 /* GPIO CDEV reports REALTIME timestamps in events */
  158. #define FLAG_EVENT_CLOCK_HTE 19 /* GPIO CDEV reports hardware timestamps in events */
  159. /* Connection label */
  160. const char *label;
  161. /* Name of the GPIO */
  162. const char *name;
  163. #ifdef CONFIG_OF_DYNAMIC
  164. struct device_node *hog;
  165. #endif
  166. #ifdef CONFIG_GPIO_CDEV
  167. /* debounce period in microseconds */
  168. unsigned int debounce_period_us;
  169. #endif
  170. ANDROID_KABI_RESERVE(1);
  171. };
  172. #define gpiod_not_found(desc) (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
  173. int gpiod_request(struct gpio_desc *desc, const char *label);
  174. void gpiod_free(struct gpio_desc *desc);
  175. static inline int gpiod_request_user(struct gpio_desc *desc, const char *label)
  176. {
  177. int ret;
  178. ret = gpiod_request(desc, label);
  179. if (ret == -EPROBE_DEFER)
  180. ret = -ENODEV;
  181. return ret;
  182. }
  183. int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
  184. unsigned long lflags, enum gpiod_flags dflags);
  185. int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce);
  186. int gpiod_hog(struct gpio_desc *desc, const char *name,
  187. unsigned long lflags, enum gpiod_flags dflags);
  188. /*
  189. * Return the GPIO number of the passed descriptor relative to its chip
  190. */
  191. static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
  192. {
  193. return desc - &desc->gdev->descs[0];
  194. }
  195. /* With descriptor prefix */
  196. #define gpiod_emerg(desc, fmt, ...) \
  197. pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  198. ##__VA_ARGS__)
  199. #define gpiod_crit(desc, fmt, ...) \
  200. pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  201. ##__VA_ARGS__)
  202. #define gpiod_err(desc, fmt, ...) \
  203. pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  204. ##__VA_ARGS__)
  205. #define gpiod_warn(desc, fmt, ...) \
  206. pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  207. ##__VA_ARGS__)
  208. #define gpiod_info(desc, fmt, ...) \
  209. pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  210. ##__VA_ARGS__)
  211. #define gpiod_dbg(desc, fmt, ...) \
  212. pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  213. ##__VA_ARGS__)
  214. /* With chip prefix */
  215. #define chip_emerg(gc, fmt, ...) \
  216. dev_emerg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
  217. #define chip_crit(gc, fmt, ...) \
  218. dev_crit(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
  219. #define chip_err(gc, fmt, ...) \
  220. dev_err(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
  221. #define chip_warn(gc, fmt, ...) \
  222. dev_warn(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
  223. #define chip_info(gc, fmt, ...) \
  224. dev_info(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
  225. #define chip_dbg(gc, fmt, ...) \
  226. dev_dbg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
  227. #endif /* GPIOLIB_H */