gpiolib.h 6.3 KB

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