consumer.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
  22. const char *con_id);
  23. struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
  24. const char *con_id,
  25. unsigned int index);
  26. void gpiod_put(struct gpio_desc *desc);
  27. struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
  28. const char *con_id);
  29. struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
  30. const char *con_id,
  31. unsigned int idx);
  32. struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
  33. const char *con_id);
  34. struct gpio_desc *__must_check
  35. devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
  36. unsigned int index);
  37. void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
  38. int gpiod_get_direction(const struct gpio_desc *desc);
  39. int gpiod_direction_input(struct gpio_desc *desc);
  40. int gpiod_direction_output(struct gpio_desc *desc, int value);
  41. int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
  42. /* Value get/set from non-sleeping context */
  43. int gpiod_get_value(const struct gpio_desc *desc);
  44. void gpiod_set_value(struct gpio_desc *desc, int value);
  45. int gpiod_get_raw_value(const struct gpio_desc *desc);
  46. void gpiod_set_raw_value(struct gpio_desc *desc, int value);
  47. /* Value get/set from sleeping context */
  48. int gpiod_get_value_cansleep(const struct gpio_desc *desc);
  49. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
  50. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
  51. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
  52. int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
  53. int gpiod_is_active_low(const struct gpio_desc *desc);
  54. int gpiod_cansleep(const struct gpio_desc *desc);
  55. int gpiod_to_irq(const struct gpio_desc *desc);
  56. /* Convert between the old gpio_ and new gpiod_ interfaces */
  57. struct gpio_desc *gpio_to_desc(unsigned gpio);
  58. int desc_to_gpio(const struct gpio_desc *desc);
  59. #else /* CONFIG_GPIOLIB */
  60. static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
  61. const char *con_id)
  62. {
  63. return ERR_PTR(-ENOSYS);
  64. }
  65. static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
  66. const char *con_id,
  67. unsigned int idx)
  68. {
  69. return ERR_PTR(-ENOSYS);
  70. }
  71. static inline struct gpio_desc *__must_check
  72. gpiod_get_optional(struct device *dev, const char *con_id)
  73. {
  74. return ERR_PTR(-ENOSYS);
  75. }
  76. static inline struct gpio_desc *__must_check
  77. gpiod_get_index_optional(struct device *dev, const char *con_id,
  78. unsigned int index)
  79. {
  80. return ERR_PTR(-ENOSYS);
  81. }
  82. static inline void gpiod_put(struct gpio_desc *desc)
  83. {
  84. might_sleep();
  85. /* GPIO can never have been requested */
  86. WARN_ON(1);
  87. }
  88. static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
  89. const char *con_id)
  90. {
  91. return ERR_PTR(-ENOSYS);
  92. }
  93. static inline
  94. struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
  95. const char *con_id,
  96. unsigned int idx)
  97. {
  98. return ERR_PTR(-ENOSYS);
  99. }
  100. static inline struct gpio_desc *__must_check
  101. devm_gpiod_get_optional(struct device *dev, const char *con_id)
  102. {
  103. return ERR_PTR(-ENOSYS);
  104. }
  105. static inline struct gpio_desc *__must_check
  106. devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
  107. unsigned int index)
  108. {
  109. return ERR_PTR(-ENOSYS);
  110. }
  111. static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
  112. {
  113. might_sleep();
  114. /* GPIO can never have been requested */
  115. WARN_ON(1);
  116. }
  117. static inline int gpiod_get_direction(const struct gpio_desc *desc)
  118. {
  119. /* GPIO can never have been requested */
  120. WARN_ON(1);
  121. return -ENOSYS;
  122. }
  123. static inline int gpiod_direction_input(struct gpio_desc *desc)
  124. {
  125. /* GPIO can never have been requested */
  126. WARN_ON(1);
  127. return -ENOSYS;
  128. }
  129. static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
  130. {
  131. /* GPIO can never have been requested */
  132. WARN_ON(1);
  133. return -ENOSYS;
  134. }
  135. static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  136. {
  137. /* GPIO can never have been requested */
  138. WARN_ON(1);
  139. return -ENOSYS;
  140. }
  141. static inline int gpiod_get_value(const struct gpio_desc *desc)
  142. {
  143. /* GPIO can never have been requested */
  144. WARN_ON(1);
  145. return 0;
  146. }
  147. static inline void gpiod_set_value(struct gpio_desc *desc, int value)
  148. {
  149. /* GPIO can never have been requested */
  150. WARN_ON(1);
  151. }
  152. static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
  153. {
  154. /* GPIO can never have been requested */
  155. WARN_ON(1);
  156. return 0;
  157. }
  158. static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  159. {
  160. /* GPIO can never have been requested */
  161. WARN_ON(1);
  162. }
  163. static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  164. {
  165. /* GPIO can never have been requested */
  166. WARN_ON(1);
  167. return 0;
  168. }
  169. static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  170. {
  171. /* GPIO can never have been requested */
  172. WARN_ON(1);
  173. }
  174. static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  175. {
  176. /* GPIO can never have been requested */
  177. WARN_ON(1);
  178. return 0;
  179. }
  180. static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
  181. int value)
  182. {
  183. /* GPIO can never have been requested */
  184. WARN_ON(1);
  185. }
  186. static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
  187. {
  188. /* GPIO can never have been requested */
  189. WARN_ON(1);
  190. return -ENOSYS;
  191. }
  192. static inline int gpiod_is_active_low(const struct gpio_desc *desc)
  193. {
  194. /* GPIO can never have been requested */
  195. WARN_ON(1);
  196. return 0;
  197. }
  198. static inline int gpiod_cansleep(const struct gpio_desc *desc)
  199. {
  200. /* GPIO can never have been requested */
  201. WARN_ON(1);
  202. return 0;
  203. }
  204. static inline int gpiod_to_irq(const struct gpio_desc *desc)
  205. {
  206. /* GPIO can never have been requested */
  207. WARN_ON(1);
  208. return -EINVAL;
  209. }
  210. static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
  211. {
  212. return ERR_PTR(-EINVAL);
  213. }
  214. static inline int desc_to_gpio(const struct gpio_desc *desc)
  215. {
  216. /* GPIO can never have been requested */
  217. WARN_ON(1);
  218. return -EINVAL;
  219. }
  220. #endif /* CONFIG_GPIOLIB */
  221. #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
  222. int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
  223. int gpiod_export_link(struct device *dev, const char *name,
  224. struct gpio_desc *desc);
  225. int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
  226. void gpiod_unexport(struct gpio_desc *desc);
  227. #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  228. static inline int gpiod_export(struct gpio_desc *desc,
  229. bool direction_may_change)
  230. {
  231. return -ENOSYS;
  232. }
  233. static inline int gpiod_export_link(struct device *dev, const char *name,
  234. struct gpio_desc *desc)
  235. {
  236. return -ENOSYS;
  237. }
  238. static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
  239. {
  240. return -ENOSYS;
  241. }
  242. static inline void gpiod_unexport(struct gpio_desc *desc)
  243. {
  244. }
  245. #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  246. #endif