gpiolib.h 8.3 KB

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