gpiolib.h 7.3 KB

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