gpiolib.h 8.4 KB

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