gpiolib.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/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. enum of_gpio_flags;
  17. enum gpio_lookup_flags;
  18. struct acpi_device;
  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. *
  39. * This state container holds most of the runtime variable data
  40. * for a GPIO device and can hold references and live on after the
  41. * GPIO chip has been removed, if it is still being used from
  42. * userspace.
  43. */
  44. struct gpio_device {
  45. int id;
  46. struct device dev;
  47. struct cdev chrdev;
  48. struct device *mockdev;
  49. struct module *owner;
  50. struct gpio_chip *chip;
  51. struct gpio_desc *descs;
  52. int base;
  53. u16 ngpio;
  54. const char *label;
  55. void *data;
  56. struct list_head list;
  57. #ifdef CONFIG_PINCTRL
  58. /*
  59. * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
  60. * describe the actual pin range which they serve in an SoC. This
  61. * information would be used by pinctrl subsystem to configure
  62. * corresponding pins for gpio usage.
  63. */
  64. struct list_head pin_ranges;
  65. #endif
  66. };
  67. /**
  68. * struct acpi_gpio_info - ACPI GPIO specific information
  69. * @adev: reference to ACPI device which consumes GPIO resource
  70. * @flags: GPIO initialization flags
  71. * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
  72. * @polarity: interrupt polarity as provided by ACPI
  73. * @triggering: triggering type as provided by ACPI
  74. * @quirks: Linux specific quirks as provided by struct acpi_gpio_mapping
  75. */
  76. struct acpi_gpio_info {
  77. struct acpi_device *adev;
  78. enum gpiod_flags flags;
  79. bool gpioint;
  80. int polarity;
  81. int triggering;
  82. unsigned int quirks;
  83. };
  84. /* gpio suffixes used for ACPI and device tree lookup */
  85. static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };
  86. #ifdef CONFIG_OF_GPIO
  87. struct gpio_desc *of_find_gpio(struct device *dev,
  88. const char *con_id,
  89. unsigned int idx,
  90. enum gpio_lookup_flags *flags);
  91. struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
  92. const char *list_name, int index, enum of_gpio_flags *flags);
  93. int of_gpiochip_add(struct gpio_chip *gc);
  94. void of_gpiochip_remove(struct gpio_chip *gc);
  95. #else
  96. static inline struct gpio_desc *of_find_gpio(struct device *dev,
  97. const char *con_id,
  98. unsigned int idx,
  99. enum gpio_lookup_flags *flags)
  100. {
  101. return ERR_PTR(-ENOENT);
  102. }
  103. static inline struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
  104. const char *list_name, int index, enum of_gpio_flags *flags)
  105. {
  106. return ERR_PTR(-ENOENT);
  107. }
  108. static inline int of_gpiochip_add(struct gpio_chip *gc) { return 0; }
  109. static inline void of_gpiochip_remove(struct gpio_chip *gc) { }
  110. #endif /* CONFIG_OF_GPIO */
  111. #ifdef CONFIG_ACPI
  112. void acpi_gpiochip_add(struct gpio_chip *chip);
  113. void acpi_gpiochip_remove(struct gpio_chip *chip);
  114. void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
  115. void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
  116. int acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags,
  117. struct acpi_gpio_info *info);
  118. struct gpio_desc *acpi_find_gpio(struct device *dev,
  119. const char *con_id,
  120. unsigned int idx,
  121. enum gpiod_flags *dflags,
  122. enum gpio_lookup_flags *lookupflags);
  123. struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
  124. const char *propname, int index,
  125. struct acpi_gpio_info *info);
  126. int acpi_gpio_count(struct device *dev, const char *con_id);
  127. bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id);
  128. #else
  129. static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
  130. static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
  131. static inline void
  132. acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
  133. static inline void
  134. acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
  135. static inline int
  136. acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info)
  137. {
  138. return 0;
  139. }
  140. static inline struct gpio_desc *
  141. acpi_find_gpio(struct device *dev, const char *con_id,
  142. unsigned int idx, enum gpiod_flags *dflags,
  143. enum gpio_lookup_flags *lookupflags)
  144. {
  145. return ERR_PTR(-ENOENT);
  146. }
  147. static inline struct gpio_desc *
  148. acpi_node_get_gpiod(struct fwnode_handle *fwnode, const char *propname,
  149. int index, struct acpi_gpio_info *info)
  150. {
  151. return ERR_PTR(-ENXIO);
  152. }
  153. static inline int acpi_gpio_count(struct device *dev, const char *con_id)
  154. {
  155. return -ENODEV;
  156. }
  157. static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
  158. const char *con_id)
  159. {
  160. return false;
  161. }
  162. #endif
  163. struct gpio_array {
  164. struct gpio_desc **desc;
  165. unsigned int size;
  166. struct gpio_chip *chip;
  167. unsigned long *get_mask;
  168. unsigned long *set_mask;
  169. unsigned long invert_mask[];
  170. };
  171. struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
  172. int gpiod_get_array_value_complex(bool raw, bool can_sleep,
  173. unsigned int array_size,
  174. struct gpio_desc **desc_array,
  175. struct gpio_array *array_info,
  176. unsigned long *value_bitmap);
  177. int gpiod_set_array_value_complex(bool raw, bool can_sleep,
  178. unsigned int array_size,
  179. struct gpio_desc **desc_array,
  180. struct gpio_array *array_info,
  181. unsigned long *value_bitmap);
  182. /* This is just passed between gpiolib and devres */
  183. struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
  184. const char *propname, int index,
  185. enum gpiod_flags dflags,
  186. const char *label);
  187. extern struct spinlock gpio_lock;
  188. extern struct list_head gpio_devices;
  189. struct gpio_desc {
  190. struct gpio_device *gdev;
  191. unsigned long flags;
  192. /* flag symbols are bit numbers */
  193. #define FLAG_REQUESTED 0
  194. #define FLAG_IS_OUT 1
  195. #define FLAG_EXPORT 2 /* protected by sysfs_lock */
  196. #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
  197. #define FLAG_ACTIVE_LOW 6 /* value has active low */
  198. #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
  199. #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
  200. #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
  201. #define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */
  202. #define FLAG_IS_HOGGED 11 /* GPIO is hogged */
  203. #define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
  204. /* Connection label */
  205. const char *label;
  206. /* Name of the GPIO */
  207. const char *name;
  208. };
  209. int gpiod_request(struct gpio_desc *desc, const char *label);
  210. void gpiod_free(struct gpio_desc *desc);
  211. int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
  212. unsigned long lflags, enum gpiod_flags dflags);
  213. int gpiod_hog(struct gpio_desc *desc, const char *name,
  214. unsigned long lflags, enum gpiod_flags dflags);
  215. /*
  216. * Return the GPIO number of the passed descriptor relative to its chip
  217. */
  218. static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
  219. {
  220. return desc - &desc->gdev->descs[0];
  221. }
  222. void devprop_gpiochip_set_names(struct gpio_chip *chip,
  223. const struct fwnode_handle *fwnode);
  224. /* With descriptor prefix */
  225. #define gpiod_emerg(desc, fmt, ...) \
  226. pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  227. ##__VA_ARGS__)
  228. #define gpiod_crit(desc, fmt, ...) \
  229. pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  230. ##__VA_ARGS__)
  231. #define gpiod_err(desc, fmt, ...) \
  232. pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  233. ##__VA_ARGS__)
  234. #define gpiod_warn(desc, fmt, ...) \
  235. pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  236. ##__VA_ARGS__)
  237. #define gpiod_info(desc, fmt, ...) \
  238. pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  239. ##__VA_ARGS__)
  240. #define gpiod_dbg(desc, fmt, ...) \
  241. pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  242. ##__VA_ARGS__)
  243. /* With chip prefix */
  244. #define chip_emerg(chip, fmt, ...) \
  245. dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  246. #define chip_crit(chip, fmt, ...) \
  247. dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  248. #define chip_err(chip, fmt, ...) \
  249. dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  250. #define chip_warn(chip, fmt, ...) \
  251. dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  252. #define chip_info(chip, fmt, ...) \
  253. dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  254. #define chip_dbg(chip, fmt, ...) \
  255. dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  256. #ifdef CONFIG_GPIO_SYSFS
  257. int gpiochip_sysfs_register(struct gpio_device *gdev);
  258. void gpiochip_sysfs_unregister(struct gpio_device *gdev);
  259. #else
  260. static inline int gpiochip_sysfs_register(struct gpio_device *gdev)
  261. {
  262. return 0;
  263. }
  264. static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev)
  265. {
  266. }
  267. #endif /* CONFIG_GPIO_SYSFS */
  268. #endif /* GPIOLIB_H */