consumer.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #ifndef __LINUX_GPIO_CONSUMER_H
  2. #define __LINUX_GPIO_CONSUMER_H
  3. #include <linux/err.h>
  4. #include <linux/kernel.h>
  5. struct device;
  6. /**
  7. * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are
  8. * preferable to the old integer-based handles.
  9. *
  10. * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid
  11. * until the GPIO is released.
  12. */
  13. struct gpio_desc;
  14. #ifdef CONFIG_GPIOLIB
  15. /* Acquire and dispose GPIOs */
  16. struct gpio_desc *__must_check gpiod_get(struct device *dev,
  17. const char *con_id);
  18. struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
  19. const char *con_id,
  20. unsigned int idx);
  21. void gpiod_put(struct gpio_desc *desc);
  22. struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
  23. const char *con_id);
  24. struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
  25. const char *con_id,
  26. unsigned int idx);
  27. void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
  28. int gpiod_get_direction(const struct gpio_desc *desc);
  29. int gpiod_direction_input(struct gpio_desc *desc);
  30. int gpiod_direction_output(struct gpio_desc *desc, int value);
  31. int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
  32. /* Value get/set from non-sleeping context */
  33. int gpiod_get_value(const struct gpio_desc *desc);
  34. void gpiod_set_value(struct gpio_desc *desc, int value);
  35. int gpiod_get_raw_value(const struct gpio_desc *desc);
  36. void gpiod_set_raw_value(struct gpio_desc *desc, int value);
  37. /* Value get/set from sleeping context */
  38. int gpiod_get_value_cansleep(const struct gpio_desc *desc);
  39. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
  40. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
  41. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
  42. int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
  43. int gpiod_is_active_low(const struct gpio_desc *desc);
  44. int gpiod_cansleep(const struct gpio_desc *desc);
  45. int gpiod_to_irq(const struct gpio_desc *desc);
  46. /* Convert between the old gpio_ and new gpiod_ interfaces */
  47. struct gpio_desc *gpio_to_desc(unsigned gpio);
  48. int desc_to_gpio(const struct gpio_desc *desc);
  49. #else /* CONFIG_GPIOLIB */
  50. static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
  51. const char *con_id)
  52. {
  53. return ERR_PTR(-ENOSYS);
  54. }
  55. static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
  56. const char *con_id,
  57. unsigned int idx)
  58. {
  59. return ERR_PTR(-ENOSYS);
  60. }
  61. static inline void gpiod_put(struct gpio_desc *desc)
  62. {
  63. might_sleep();
  64. /* GPIO can never have been requested */
  65. WARN_ON(1);
  66. }
  67. static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
  68. const char *con_id)
  69. {
  70. return ERR_PTR(-ENOSYS);
  71. }
  72. static inline
  73. struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
  74. const char *con_id,
  75. unsigned int idx)
  76. {
  77. return ERR_PTR(-ENOSYS);
  78. }
  79. static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
  80. {
  81. might_sleep();
  82. /* GPIO can never have been requested */
  83. WARN_ON(1);
  84. }
  85. static inline int gpiod_get_direction(const struct gpio_desc *desc)
  86. {
  87. /* GPIO can never have been requested */
  88. WARN_ON(1);
  89. return -ENOSYS;
  90. }
  91. static inline int gpiod_direction_input(struct gpio_desc *desc)
  92. {
  93. /* GPIO can never have been requested */
  94. WARN_ON(1);
  95. return -ENOSYS;
  96. }
  97. static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
  98. {
  99. /* GPIO can never have been requested */
  100. WARN_ON(1);
  101. return -ENOSYS;
  102. }
  103. static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  104. {
  105. /* GPIO can never have been requested */
  106. WARN_ON(1);
  107. return -ENOSYS;
  108. }
  109. static inline int gpiod_get_value(const struct gpio_desc *desc)
  110. {
  111. /* GPIO can never have been requested */
  112. WARN_ON(1);
  113. return 0;
  114. }
  115. static inline void gpiod_set_value(struct gpio_desc *desc, int value)
  116. {
  117. /* GPIO can never have been requested */
  118. WARN_ON(1);
  119. }
  120. static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
  121. {
  122. /* GPIO can never have been requested */
  123. WARN_ON(1);
  124. return 0;
  125. }
  126. static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  127. {
  128. /* GPIO can never have been requested */
  129. WARN_ON(1);
  130. }
  131. static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  132. {
  133. /* GPIO can never have been requested */
  134. WARN_ON(1);
  135. return 0;
  136. }
  137. static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  138. {
  139. /* GPIO can never have been requested */
  140. WARN_ON(1);
  141. }
  142. static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  143. {
  144. /* GPIO can never have been requested */
  145. WARN_ON(1);
  146. return 0;
  147. }
  148. static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
  149. int value)
  150. {
  151. /* GPIO can never have been requested */
  152. WARN_ON(1);
  153. }
  154. static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
  155. {
  156. /* GPIO can never have been requested */
  157. WARN_ON(1);
  158. return -ENOSYS;
  159. }
  160. static inline int gpiod_is_active_low(const struct gpio_desc *desc)
  161. {
  162. /* GPIO can never have been requested */
  163. WARN_ON(1);
  164. return 0;
  165. }
  166. static inline int gpiod_cansleep(const struct gpio_desc *desc)
  167. {
  168. /* GPIO can never have been requested */
  169. WARN_ON(1);
  170. return 0;
  171. }
  172. static inline int gpiod_to_irq(const struct gpio_desc *desc)
  173. {
  174. /* GPIO can never have been requested */
  175. WARN_ON(1);
  176. return -EINVAL;
  177. }
  178. static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
  179. {
  180. return ERR_PTR(-EINVAL);
  181. }
  182. static inline int desc_to_gpio(const struct gpio_desc *desc)
  183. {
  184. /* GPIO can never have been requested */
  185. WARN_ON(1);
  186. return -EINVAL;
  187. }
  188. #endif /* CONFIG_GPIOLIB */
  189. #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
  190. int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
  191. int gpiod_export_link(struct device *dev, const char *name,
  192. struct gpio_desc *desc);
  193. int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
  194. void gpiod_unexport(struct gpio_desc *desc);
  195. #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  196. static inline int gpiod_export(struct gpio_desc *desc,
  197. bool direction_may_change)
  198. {
  199. return -ENOSYS;
  200. }
  201. static inline int gpiod_export_link(struct device *dev, const char *name,
  202. struct gpio_desc *desc)
  203. {
  204. return -ENOSYS;
  205. }
  206. static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
  207. {
  208. return -ENOSYS;
  209. }
  210. static inline void gpiod_unexport(struct gpio_desc *desc)
  211. {
  212. }
  213. #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  214. #endif