gpiolib.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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_array {
  167. struct gpio_desc **desc;
  168. unsigned int size;
  169. struct gpio_chip *chip;
  170. unsigned long *get_mask;
  171. unsigned long *set_mask;
  172. unsigned long invert_mask[];
  173. };
  174. struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
  175. int gpiod_get_array_value_complex(bool raw, bool can_sleep,
  176. unsigned int array_size,
  177. struct gpio_desc **desc_array,
  178. struct gpio_array *array_info,
  179. unsigned long *value_bitmap);
  180. int gpiod_set_array_value_complex(bool raw, bool can_sleep,
  181. unsigned int array_size,
  182. struct gpio_desc **desc_array,
  183. struct gpio_array *array_info,
  184. unsigned long *value_bitmap);
  185. /* This is just passed between gpiolib and devres */
  186. struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
  187. const char *propname, int index,
  188. enum gpiod_flags dflags,
  189. const char *label);
  190. extern struct spinlock gpio_lock;
  191. extern struct list_head gpio_devices;
  192. struct gpio_desc {
  193. struct gpio_device *gdev;
  194. unsigned long flags;
  195. /* flag symbols are bit numbers */
  196. #define FLAG_REQUESTED 0
  197. #define FLAG_IS_OUT 1
  198. #define FLAG_EXPORT 2 /* protected by sysfs_lock */
  199. #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
  200. #define FLAG_ACTIVE_LOW 6 /* value has active low */
  201. #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
  202. #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
  203. #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
  204. #define FLAG_IS_HOGGED 11 /* GPIO is hogged */
  205. #define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
  206. /* Connection label */
  207. const char *label;
  208. /* Name of the GPIO */
  209. const char *name;
  210. };
  211. int gpiod_request(struct gpio_desc *desc, const char *label);
  212. void gpiod_free(struct gpio_desc *desc);
  213. int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
  214. unsigned long lflags, enum gpiod_flags dflags);
  215. int gpiod_hog(struct gpio_desc *desc, const char *name,
  216. unsigned long lflags, enum gpiod_flags dflags);
  217. /*
  218. * Return the GPIO number of the passed descriptor relative to its chip
  219. */
  220. static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
  221. {
  222. return desc - &desc->gdev->descs[0];
  223. }
  224. void devprop_gpiochip_set_names(struct gpio_chip *chip,
  225. const struct fwnode_handle *fwnode);
  226. /* With descriptor prefix */
  227. #define gpiod_emerg(desc, fmt, ...) \
  228. pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  229. ##__VA_ARGS__)
  230. #define gpiod_crit(desc, fmt, ...) \
  231. pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  232. ##__VA_ARGS__)
  233. #define gpiod_err(desc, fmt, ...) \
  234. pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  235. ##__VA_ARGS__)
  236. #define gpiod_warn(desc, fmt, ...) \
  237. pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  238. ##__VA_ARGS__)
  239. #define gpiod_info(desc, fmt, ...) \
  240. pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  241. ##__VA_ARGS__)
  242. #define gpiod_dbg(desc, fmt, ...) \
  243. pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  244. ##__VA_ARGS__)
  245. /* With chip prefix */
  246. #define chip_emerg(chip, fmt, ...) \
  247. dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  248. #define chip_crit(chip, fmt, ...) \
  249. dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  250. #define chip_err(chip, fmt, ...) \
  251. dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  252. #define chip_warn(chip, fmt, ...) \
  253. dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  254. #define chip_info(chip, fmt, ...) \
  255. dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  256. #define chip_dbg(chip, fmt, ...) \
  257. dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  258. #ifdef CONFIG_GPIO_SYSFS
  259. int gpiochip_sysfs_register(struct gpio_device *gdev);
  260. void gpiochip_sysfs_unregister(struct gpio_device *gdev);
  261. #else
  262. static inline int gpiochip_sysfs_register(struct gpio_device *gdev)
  263. {
  264. return 0;
  265. }
  266. static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev)
  267. {
  268. }
  269. #endif /* CONFIG_GPIO_SYSFS */
  270. #endif /* GPIOLIB_H */