gpiolib.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. struct acpi_device;
  21. /**
  22. * struct gpio_device - internal state container for GPIO devices
  23. * @id: numerical ID number for the GPIO chip
  24. * @dev: the GPIO device struct
  25. * @chrdev: character device for the GPIO device
  26. * @mockdev: class device used by the deprecated sysfs interface (may be
  27. * NULL)
  28. * @owner: helps prevent removal of modules exporting active GPIOs
  29. * @chip: pointer to the corresponding gpiochip, holding static
  30. * data for this device
  31. * @list: links gpio_device:s together for traversal
  32. *
  33. * This state container holds most of the runtime variable data
  34. * for a GPIO device and can hold references and live on after the
  35. * GPIO chip has been removed, if it is still being used from
  36. * userspace.
  37. */
  38. struct gpio_device {
  39. int id;
  40. struct device dev;
  41. struct cdev chrdev;
  42. struct device *mockdev;
  43. struct module *owner;
  44. struct gpio_chip *chip;
  45. struct list_head list;
  46. };
  47. /**
  48. * struct acpi_gpio_info - ACPI GPIO specific information
  49. * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
  50. * @active_low: in case of @gpioint, the pin is active low
  51. */
  52. struct acpi_gpio_info {
  53. bool gpioint;
  54. int polarity;
  55. int triggering;
  56. };
  57. /* gpio suffixes used for ACPI and device tree lookup */
  58. static const char * const gpio_suffixes[] = { "gpios", "gpio" };
  59. #ifdef CONFIG_ACPI
  60. void acpi_gpiochip_add(struct gpio_chip *chip);
  61. void acpi_gpiochip_remove(struct gpio_chip *chip);
  62. void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
  63. void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
  64. struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
  65. const char *propname, int index,
  66. struct acpi_gpio_info *info);
  67. struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
  68. const char *propname, int index,
  69. struct acpi_gpio_info *info);
  70. int acpi_gpio_count(struct device *dev, const char *con_id);
  71. bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id);
  72. #else
  73. static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
  74. static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
  75. static inline void
  76. acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
  77. static inline void
  78. acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
  79. static inline struct gpio_desc *
  80. acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname,
  81. int index, struct acpi_gpio_info *info)
  82. {
  83. return ERR_PTR(-ENOSYS);
  84. }
  85. static inline struct gpio_desc *
  86. acpi_node_get_gpiod(struct fwnode_handle *fwnode, const char *propname,
  87. int index, struct acpi_gpio_info *info)
  88. {
  89. return ERR_PTR(-ENXIO);
  90. }
  91. static inline int acpi_gpio_count(struct device *dev, const char *con_id)
  92. {
  93. return -ENODEV;
  94. }
  95. static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
  96. const char *con_id)
  97. {
  98. return false;
  99. }
  100. #endif
  101. struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
  102. const char *list_name, int index, enum of_gpio_flags *flags);
  103. struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
  104. extern struct spinlock gpio_lock;
  105. extern struct list_head gpio_devices;
  106. struct gpio_desc {
  107. struct gpio_chip *chip;
  108. unsigned long flags;
  109. /* flag symbols are bit numbers */
  110. #define FLAG_REQUESTED 0
  111. #define FLAG_IS_OUT 1
  112. #define FLAG_EXPORT 2 /* protected by sysfs_lock */
  113. #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
  114. #define FLAG_ACTIVE_LOW 6 /* value has active low */
  115. #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
  116. #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
  117. #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
  118. #define FLAG_IS_HOGGED 11 /* GPIO is hogged */
  119. /* Connection label */
  120. const char *label;
  121. /* Name of the GPIO */
  122. const char *name;
  123. };
  124. int gpiod_request(struct gpio_desc *desc, const char *label);
  125. void gpiod_free(struct gpio_desc *desc);
  126. int gpiod_hog(struct gpio_desc *desc, const char *name,
  127. unsigned long lflags, enum gpiod_flags dflags);
  128. /*
  129. * Return the GPIO number of the passed descriptor relative to its chip
  130. */
  131. static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc)
  132. {
  133. return desc - &desc->chip->desc[0];
  134. }
  135. /* With descriptor prefix */
  136. #define gpiod_emerg(desc, fmt, ...) \
  137. pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  138. ##__VA_ARGS__)
  139. #define gpiod_crit(desc, fmt, ...) \
  140. pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  141. ##__VA_ARGS__)
  142. #define gpiod_err(desc, fmt, ...) \
  143. pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  144. ##__VA_ARGS__)
  145. #define gpiod_warn(desc, fmt, ...) \
  146. pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  147. ##__VA_ARGS__)
  148. #define gpiod_info(desc, fmt, ...) \
  149. pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  150. ##__VA_ARGS__)
  151. #define gpiod_dbg(desc, fmt, ...) \
  152. pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  153. ##__VA_ARGS__)
  154. /* With chip prefix */
  155. #define chip_emerg(chip, fmt, ...) \
  156. dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  157. #define chip_crit(chip, fmt, ...) \
  158. dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  159. #define chip_err(chip, fmt, ...) \
  160. dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  161. #define chip_warn(chip, fmt, ...) \
  162. dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  163. #define chip_info(chip, fmt, ...) \
  164. dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  165. #define chip_dbg(chip, fmt, ...) \
  166. dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
  167. #ifdef CONFIG_GPIO_SYSFS
  168. int gpiochip_sysfs_register(struct gpio_device *gdev);
  169. void gpiochip_sysfs_unregister(struct gpio_device *gdev);
  170. #else
  171. static inline int gpiochip_sysfs_register(struct gpio_device *gdev)
  172. {
  173. return 0;
  174. }
  175. static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev)
  176. {
  177. }
  178. #endif /* CONFIG_GPIO_SYSFS */
  179. #endif /* GPIOLIB_H */