gpiolib.h 9.5 KB

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