gpiolib.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Internal GPIO functions.
  4. *
  5. * Copyright (C) 2013, Intel Corporation
  6. * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
  7. */
  8. #ifndef GPIOLIB_H
  9. #define GPIOLIB_H
  10. #include <linux/cdev.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/gpio/consumer.h> /* for enum gpiod_flags */
  14. #include <linux/gpio/driver.h>
  15. #include <linux/module.h>
  16. #include <linux/notifier.h>
  17. #include <linux/srcu.h>
  18. #define GPIOCHIP_NAME "gpiochip"
  19. /**
  20. * struct gpio_device - internal state container for GPIO devices
  21. * @dev: the GPIO device struct
  22. * @chrdev: character device for the GPIO device
  23. * @id: numerical ID number for the GPIO chip
  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. * @desc_srcu: ensures consistent state of GPIO descriptors exposed to users
  31. * @ngpio: the number of GPIO lines on this GPIO device, equal to the size
  32. * of the @descs array.
  33. * @can_sleep: indicate whether the GPIO chip driver's callbacks can sleep
  34. * implying that they cannot be used from atomic context
  35. * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned
  36. * at device creation time.
  37. * @label: a descriptive name for the GPIO device, such as the part number
  38. * or name of the IP component in a System on Chip.
  39. * @data: per-instance data assigned by the driver
  40. * @list: links gpio_device:s together for traversal
  41. * @line_state_notifier: used to notify subscribers about lines being
  42. * requested, released or reconfigured
  43. * @device_notifier: used to notify character device wait queues about the GPIO
  44. * device being unregistered
  45. * @srcu: protects the pointer to the underlying GPIO chip
  46. * @pin_ranges: range of pins served by the GPIO driver
  47. *
  48. * This state container holds most of the runtime variable data
  49. * for a GPIO device and can hold references and live on after the
  50. * GPIO chip has been removed, if it is still being used from
  51. * userspace.
  52. */
  53. struct gpio_device {
  54. struct device dev;
  55. struct cdev chrdev;
  56. int id;
  57. struct device *mockdev;
  58. struct module *owner;
  59. struct gpio_chip __rcu *chip;
  60. struct gpio_desc *descs;
  61. struct srcu_struct desc_srcu;
  62. unsigned int base;
  63. u16 ngpio;
  64. bool can_sleep;
  65. const char *label;
  66. void *data;
  67. struct list_head list;
  68. struct blocking_notifier_head line_state_notifier;
  69. struct blocking_notifier_head device_notifier;
  70. struct srcu_struct srcu;
  71. #ifdef CONFIG_PINCTRL
  72. /*
  73. * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
  74. * describe the actual pin range which they serve in an SoC. This
  75. * information would be used by pinctrl subsystem to configure
  76. * corresponding pins for gpio usage.
  77. */
  78. struct list_head pin_ranges;
  79. #endif
  80. };
  81. static inline struct gpio_device *to_gpio_device(struct device *dev)
  82. {
  83. return container_of(dev, struct gpio_device, dev);
  84. }
  85. /* GPIO suffixes used for ACPI and device tree lookup */
  86. extern const char *const gpio_suffixes[];
  87. #define for_each_gpio_property_name(propname, con_id) \
  88. for (const char * const *__suffixes = gpio_suffixes; \
  89. *__suffixes && ({ \
  90. const char *__gs = *__suffixes; \
  91. \
  92. if (con_id) \
  93. snprintf(propname, sizeof(propname), "%s-%s", con_id, __gs); \
  94. else \
  95. snprintf(propname, sizeof(propname), "%s", __gs); \
  96. 1; \
  97. }); \
  98. __suffixes++)
  99. /**
  100. * struct gpio_array - Opaque descriptor for a structure of GPIO array attributes
  101. *
  102. * @desc: Array of pointers to the GPIO descriptors
  103. * @size: Number of elements in desc
  104. * @chip: Parent GPIO chip
  105. * @get_mask: Get mask used in fastpath
  106. * @set_mask: Set mask used in fastpath
  107. * @invert_mask: Invert mask used in fastpath
  108. *
  109. * This structure is attached to struct gpiod_descs obtained from
  110. * gpiod_get_array() and can be passed back to get/set array functions in order
  111. * to activate fast processing path if applicable.
  112. */
  113. struct gpio_array {
  114. struct gpio_desc **desc;
  115. unsigned int size;
  116. struct gpio_chip *chip;
  117. unsigned long *get_mask;
  118. unsigned long *set_mask;
  119. unsigned long invert_mask[];
  120. };
  121. #define for_each_gpio_desc(gc, desc) \
  122. for (unsigned int __i = 0; \
  123. __i < gc->ngpio && (desc = gpiochip_get_desc(gc, __i)); \
  124. __i++) \
  125. #define for_each_gpio_desc_with_flag(gc, desc, flag) \
  126. for_each_gpio_desc(gc, desc) \
  127. if (!test_bit(flag, &desc->flags)) {} else
  128. int gpiod_get_array_value_complex(bool raw, bool can_sleep,
  129. unsigned int array_size,
  130. struct gpio_desc **desc_array,
  131. struct gpio_array *array_info,
  132. unsigned long *value_bitmap);
  133. int gpiod_set_array_value_complex(bool raw, bool can_sleep,
  134. unsigned int array_size,
  135. struct gpio_desc **desc_array,
  136. struct gpio_array *array_info,
  137. unsigned long *value_bitmap);
  138. int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
  139. void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action);
  140. struct gpio_desc_label {
  141. struct rcu_head rh;
  142. char str[];
  143. };
  144. /**
  145. * struct gpio_desc - Opaque descriptor for a GPIO
  146. *
  147. * @gdev: Pointer to the parent GPIO device
  148. * @flags: Binary descriptor flags
  149. * @label: Name of the consumer
  150. * @name: Line name
  151. * @hog: Pointer to the device node that hogs this line (if any)
  152. *
  153. * These are obtained using gpiod_get() and are preferable to the old
  154. * integer-based handles.
  155. *
  156. * Contrary to integers, a pointer to a &struct gpio_desc is guaranteed to be
  157. * valid until the GPIO is released.
  158. */
  159. struct gpio_desc {
  160. struct gpio_device *gdev;
  161. unsigned long flags;
  162. /* flag symbols are bit numbers */
  163. #define FLAG_REQUESTED 0
  164. #define FLAG_IS_OUT 1
  165. #define FLAG_EXPORT 2 /* protected by sysfs_lock */
  166. #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
  167. #define FLAG_ACTIVE_LOW 6 /* value has active low */
  168. #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
  169. #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
  170. #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
  171. #define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */
  172. #define FLAG_IS_HOGGED 11 /* GPIO is hogged */
  173. #define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
  174. #define FLAG_PULL_UP 13 /* GPIO has pull up enabled */
  175. #define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */
  176. #define FLAG_BIAS_DISABLE 15 /* GPIO has pull disabled */
  177. #define FLAG_EDGE_RISING 16 /* GPIO CDEV detects rising edge events */
  178. #define FLAG_EDGE_FALLING 17 /* GPIO CDEV detects falling edge events */
  179. #define FLAG_EVENT_CLOCK_REALTIME 18 /* GPIO CDEV reports REALTIME timestamps in events */
  180. #define FLAG_EVENT_CLOCK_HTE 19 /* GPIO CDEV reports hardware timestamps in events */
  181. /* Connection label */
  182. struct gpio_desc_label __rcu *label;
  183. /* Name of the GPIO */
  184. const char *name;
  185. #ifdef CONFIG_OF_DYNAMIC
  186. struct device_node *hog;
  187. #endif
  188. };
  189. #define gpiod_not_found(desc) (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
  190. struct gpio_chip_guard {
  191. struct gpio_device *gdev;
  192. struct gpio_chip *gc;
  193. int idx;
  194. };
  195. DEFINE_CLASS(gpio_chip_guard,
  196. struct gpio_chip_guard,
  197. srcu_read_unlock(&_T.gdev->srcu, _T.idx),
  198. ({
  199. struct gpio_chip_guard _guard;
  200. _guard.gdev = desc->gdev;
  201. _guard.idx = srcu_read_lock(&_guard.gdev->srcu);
  202. _guard.gc = srcu_dereference(_guard.gdev->chip,
  203. &_guard.gdev->srcu);
  204. _guard;
  205. }),
  206. struct gpio_desc *desc)
  207. int gpiod_request(struct gpio_desc *desc, const char *label);
  208. void gpiod_free(struct gpio_desc *desc);
  209. static inline int gpiod_request_user(struct gpio_desc *desc, const char *label)
  210. {
  211. int ret;
  212. ret = gpiod_request(desc, label);
  213. if (ret == -EPROBE_DEFER)
  214. ret = -ENODEV;
  215. return ret;
  216. }
  217. struct gpio_desc *gpiod_find_and_request(struct device *consumer,
  218. struct fwnode_handle *fwnode,
  219. const char *con_id,
  220. unsigned int idx,
  221. enum gpiod_flags flags,
  222. const char *label,
  223. bool platform_lookup_allowed);
  224. int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
  225. unsigned long lflags, enum gpiod_flags dflags);
  226. int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce);
  227. int gpiod_hog(struct gpio_desc *desc, const char *name,
  228. unsigned long lflags, enum gpiod_flags dflags);
  229. int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev);
  230. struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum);
  231. const char *gpiod_get_label(struct gpio_desc *desc);
  232. /*
  233. * Return the GPIO number of the passed descriptor relative to its chip
  234. */
  235. static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
  236. {
  237. return desc - &desc->gdev->descs[0];
  238. }
  239. /* With descriptor prefix */
  240. #define gpiod_err(desc, fmt, ...) \
  241. do { \
  242. scoped_guard(srcu, &desc->gdev->desc_srcu) { \
  243. pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), \
  244. gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \
  245. } \
  246. } while (0)
  247. #define gpiod_warn(desc, fmt, ...) \
  248. do { \
  249. scoped_guard(srcu, &desc->gdev->desc_srcu) { \
  250. pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), \
  251. gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \
  252. } \
  253. } while (0)
  254. #define gpiod_dbg(desc, fmt, ...) \
  255. do { \
  256. scoped_guard(srcu, &desc->gdev->desc_srcu) { \
  257. pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), \
  258. gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \
  259. } \
  260. } while (0)
  261. /* With chip prefix */
  262. #define chip_err(gc, fmt, ...) \
  263. dev_err(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
  264. #define chip_warn(gc, fmt, ...) \
  265. dev_warn(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
  266. #define chip_info(gc, fmt, ...) \
  267. dev_info(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
  268. #define chip_dbg(gc, fmt, ...) \
  269. dev_dbg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
  270. #endif /* GPIOLIB_H */