gpiolib.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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/err.h>
  15. #include <linux/device.h>
  16. #include <linux/module.h>
  17. #include <linux/cdev.h>
  18. enum of_gpio_flags;
  19. enum gpiod_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. 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. * @flags: GPIO initialization flags
  73. * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
  74. * @polarity: interrupt polarity as provided by ACPI
  75. * @triggering: triggering type as provided by ACPI
  76. */
  77. struct acpi_gpio_info {
  78. enum gpiod_flags flags;
  79. bool gpioint;
  80. int polarity;
  81. int triggering;
  82. };
  83. /* gpio suffixes used for ACPI and device tree lookup */
  84. static const char * const gpio_suffixes[] = { "gpios", "gpio" };
  85. #ifdef CONFIG_OF_GPIO
  86. struct gpio_desc *of_find_gpio(struct device *dev,
  87. const char *con_id,
  88. unsigned int idx,
  89. enum gpio_lookup_flags *flags);
  90. struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
  91. const char *list_name, int index, enum of_gpio_flags *flags);
  92. int of_gpiochip_add(struct gpio_chip *gc);
  93. void of_gpiochip_remove(struct gpio_chip *gc);
  94. #else
  95. static inline struct gpio_desc *of_find_gpio(struct device *dev,
  96. const char *con_id,
  97. unsigned int idx,
  98. enum gpio_lookup_flags *flags)
  99. {
  100. return ERR_PTR(-ENOENT);
  101. }
  102. static inline struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
  103. const char *list_name, int index, enum of_gpio_flags *flags)
  104. {
  105. return ERR_PTR(-ENOENT);
  106. }
  107. static inline int of_gpiochip_add(struct gpio_chip *gc) { return 0; }
  108. static inline void of_gpiochip_remove(struct gpio_chip *gc) { }
  109. #endif /* CONFIG_OF_GPIO */
  110. #ifdef CONFIG_ACPI
  111. void acpi_gpiochip_add(struct gpio_chip *chip);
  112. void acpi_gpiochip_remove(struct gpio_chip *chip);
  113. void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
  114. void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
  115. int acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags,
  116. enum gpiod_flags update);
  117. struct gpio_desc *acpi_find_gpio(struct device *dev,
  118. const char *con_id,
  119. unsigned int idx,
  120. enum gpiod_flags *dflags,
  121. enum gpio_lookup_flags *lookupflags);
  122. struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
  123. const char *propname, int index,
  124. struct acpi_gpio_info *info);
  125. int acpi_gpio_count(struct device *dev, const char *con_id);
  126. bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id);
  127. #else
  128. static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
  129. static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
  130. static inline void
  131. acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
  132. static inline void
  133. acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
  134. static inline int
  135. acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
  136. {
  137. return 0;
  138. }
  139. static inline struct gpio_desc *
  140. acpi_find_gpio(struct device *dev, const char *con_id,
  141. unsigned int idx, enum gpiod_flags *dflags,
  142. enum gpio_lookup_flags *lookupflags)
  143. {
  144. return ERR_PTR(-ENOENT);
  145. }
  146. static inline struct gpio_desc *
  147. acpi_node_get_gpiod(struct fwnode_handle *fwnode, const char *propname,
  148. int index, struct acpi_gpio_info *info)
  149. {
  150. return ERR_PTR(-ENXIO);
  151. }
  152. static inline int acpi_gpio_count(struct device *dev, const char *con_id)
  153. {
  154. return -ENODEV;
  155. }
  156. static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
  157. const char *con_id)
  158. {
  159. return false;
  160. }
  161. #endif
  162. struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
  163. int gpiod_get_array_value_complex(bool raw, bool can_sleep,
  164. unsigned int array_size,
  165. struct gpio_desc **desc_array,
  166. int *value_array);
  167. void gpiod_set_array_value_complex(bool raw, bool can_sleep,
  168. unsigned int array_size,
  169. struct gpio_desc **desc_array,
  170. int *value_array);
  171. extern struct spinlock gpio_lock;
  172. extern struct list_head gpio_devices;
  173. struct gpio_desc {
  174. struct gpio_device *gdev;
  175. unsigned long flags;
  176. /* flag symbols are bit numbers */
  177. #define FLAG_REQUESTED 0
  178. #define FLAG_IS_OUT 1
  179. #define FLAG_EXPORT 2 /* protected by sysfs_lock */
  180. #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
  181. #define FLAG_ACTIVE_LOW 6 /* value has active low */
  182. #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
  183. #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
  184. #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
  185. #define FLAG_IS_HOGGED 11 /* GPIO is hogged */
  186. #define FLAG_SLEEP_MAY_LOSE_VALUE 12 /* GPIO may lose value in sleep */
  187. /* Connection label */
  188. const char *label;
  189. /* Name of the GPIO */
  190. const char *name;
  191. };
  192. int gpiod_request(struct gpio_desc *desc, const char *label);
  193. void gpiod_free(struct gpio_desc *desc);
  194. int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
  195. unsigned long lflags, enum gpiod_flags dflags);
  196. int gpiod_hog(struct gpio_desc *desc, const char *name,
  197. unsigned long lflags, enum gpiod_flags dflags);
  198. /*
  199. * Return the GPIO number of the passed descriptor relative to its chip
  200. */
  201. static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
  202. {
  203. return desc - &desc->gdev->descs[0];
  204. }
  205. void devprop_gpiochip_set_names(struct gpio_chip *chip);
  206. /* With descriptor prefix */
  207. #define gpiod_emerg(desc, fmt, ...) \
  208. pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  209. ##__VA_ARGS__)
  210. #define gpiod_crit(desc, fmt, ...) \
  211. pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  212. ##__VA_ARGS__)
  213. #define gpiod_err(desc, fmt, ...) \
  214. pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  215. ##__VA_ARGS__)
  216. #define gpiod_warn(desc, fmt, ...) \
  217. pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  218. ##__VA_ARGS__)
  219. #define gpiod_info(desc, fmt, ...) \
  220. pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  221. ##__VA_ARGS__)
  222. #define gpiod_dbg(desc, fmt, ...) \
  223. pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  224. ##__VA_ARGS__)
  225. /* With chip prefix */
  226. #define chip_emerg(chip, fmt, ...) \
  227. dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  228. #define chip_crit(chip, fmt, ...) \
  229. dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  230. #define chip_err(chip, fmt, ...) \
  231. dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  232. #define chip_warn(chip, fmt, ...) \
  233. dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  234. #define chip_info(chip, fmt, ...) \
  235. dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  236. #define chip_dbg(chip, fmt, ...) \
  237. dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  238. #ifdef CONFIG_GPIO_SYSFS
  239. int gpiochip_sysfs_register(struct gpio_device *gdev);
  240. void gpiochip_sysfs_unregister(struct gpio_device *gdev);
  241. #else
  242. static inline int gpiochip_sysfs_register(struct gpio_device *gdev)
  243. {
  244. return 0;
  245. }
  246. static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev)
  247. {
  248. }
  249. #endif /* CONFIG_GPIO_SYSFS */
  250. #endif /* GPIOLIB_H */