consumer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #ifndef __LINUX_GPIO_CONSUMER_H
  2. #define __LINUX_GPIO_CONSUMER_H
  3. #include <linux/bug.h>
  4. #include <linux/err.h>
  5. #include <linux/kernel.h>
  6. struct device;
  7. /**
  8. * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are
  9. * preferable to the old integer-based handles.
  10. *
  11. * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid
  12. * until the GPIO is released.
  13. */
  14. struct gpio_desc;
  15. #define GPIOD_FLAGS_BIT_DIR_SET BIT(0)
  16. #define GPIOD_FLAGS_BIT_DIR_OUT BIT(1)
  17. #define GPIOD_FLAGS_BIT_DIR_VAL BIT(2)
  18. /**
  19. * Optional flags that can be passed to one of gpiod_* to configure direction
  20. * and output value. These values cannot be OR'd.
  21. */
  22. enum gpiod_flags {
  23. GPIOD_ASIS = 0,
  24. GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET,
  25. GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,
  26. GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
  27. GPIOD_FLAGS_BIT_DIR_VAL,
  28. };
  29. #ifdef CONFIG_GPIOLIB
  30. /* Acquire and dispose GPIOs */
  31. struct gpio_desc *__must_check __gpiod_get(struct device *dev,
  32. const char *con_id,
  33. enum gpiod_flags flags);
  34. struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
  35. const char *con_id,
  36. unsigned int idx,
  37. enum gpiod_flags flags);
  38. struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
  39. const char *con_id,
  40. enum gpiod_flags flags);
  41. struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
  42. const char *con_id,
  43. unsigned int index,
  44. enum gpiod_flags flags);
  45. void gpiod_put(struct gpio_desc *desc);
  46. struct gpio_desc *__must_check __devm_gpiod_get(struct device *dev,
  47. const char *con_id,
  48. enum gpiod_flags flags);
  49. struct gpio_desc *__must_check __devm_gpiod_get_index(struct device *dev,
  50. const char *con_id,
  51. unsigned int idx,
  52. enum gpiod_flags flags);
  53. struct gpio_desc *__must_check __devm_gpiod_get_optional(struct device *dev,
  54. const char *con_id,
  55. enum gpiod_flags flags);
  56. struct gpio_desc *__must_check
  57. __devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
  58. unsigned int index, enum gpiod_flags flags);
  59. void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
  60. int gpiod_get_direction(const struct gpio_desc *desc);
  61. int gpiod_direction_input(struct gpio_desc *desc);
  62. int gpiod_direction_output(struct gpio_desc *desc, int value);
  63. int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
  64. /* Value get/set from non-sleeping context */
  65. int gpiod_get_value(const struct gpio_desc *desc);
  66. void gpiod_set_value(struct gpio_desc *desc, int value);
  67. int gpiod_get_raw_value(const struct gpio_desc *desc);
  68. void gpiod_set_raw_value(struct gpio_desc *desc, int value);
  69. /* Value get/set from sleeping context */
  70. int gpiod_get_value_cansleep(const struct gpio_desc *desc);
  71. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
  72. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
  73. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
  74. int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
  75. int gpiod_is_active_low(const struct gpio_desc *desc);
  76. int gpiod_cansleep(const struct gpio_desc *desc);
  77. int gpiod_to_irq(const struct gpio_desc *desc);
  78. /* Convert between the old gpio_ and new gpiod_ interfaces */
  79. struct gpio_desc *gpio_to_desc(unsigned gpio);
  80. int desc_to_gpio(const struct gpio_desc *desc);
  81. #else /* CONFIG_GPIOLIB */
  82. static inline struct gpio_desc *__must_check __gpiod_get(struct device *dev,
  83. const char *con_id,
  84. enum gpiod_flags flags)
  85. {
  86. return ERR_PTR(-ENOSYS);
  87. }
  88. static inline struct gpio_desc *__must_check
  89. __gpiod_get_index(struct device *dev,
  90. const char *con_id,
  91. unsigned int idx,
  92. enum gpiod_flags flags)
  93. {
  94. return ERR_PTR(-ENOSYS);
  95. }
  96. static inline struct gpio_desc *__must_check
  97. __gpiod_get_optional(struct device *dev, const char *con_id,
  98. enum gpiod_flags flags)
  99. {
  100. return ERR_PTR(-ENOSYS);
  101. }
  102. static inline struct gpio_desc *__must_check
  103. __gpiod_get_index_optional(struct device *dev, const char *con_id,
  104. unsigned int index, enum gpiod_flags flags)
  105. {
  106. return ERR_PTR(-ENOSYS);
  107. }
  108. static inline void gpiod_put(struct gpio_desc *desc)
  109. {
  110. might_sleep();
  111. /* GPIO can never have been requested */
  112. WARN_ON(1);
  113. }
  114. static inline struct gpio_desc *__must_check
  115. __devm_gpiod_get(struct device *dev,
  116. const char *con_id,
  117. enum gpiod_flags flags)
  118. {
  119. return ERR_PTR(-ENOSYS);
  120. }
  121. static inline
  122. struct gpio_desc *__must_check
  123. __devm_gpiod_get_index(struct device *dev,
  124. const char *con_id,
  125. unsigned int idx,
  126. enum gpiod_flags flags)
  127. {
  128. return ERR_PTR(-ENOSYS);
  129. }
  130. static inline struct gpio_desc *__must_check
  131. __devm_gpiod_get_optional(struct device *dev, const char *con_id,
  132. enum gpiod_flags flags)
  133. {
  134. return ERR_PTR(-ENOSYS);
  135. }
  136. static inline struct gpio_desc *__must_check
  137. __devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
  138. unsigned int index, enum gpiod_flags flags)
  139. {
  140. return ERR_PTR(-ENOSYS);
  141. }
  142. static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
  143. {
  144. might_sleep();
  145. /* GPIO can never have been requested */
  146. WARN_ON(1);
  147. }
  148. static inline int gpiod_get_direction(const struct gpio_desc *desc)
  149. {
  150. /* GPIO can never have been requested */
  151. WARN_ON(1);
  152. return -ENOSYS;
  153. }
  154. static inline int gpiod_direction_input(struct gpio_desc *desc)
  155. {
  156. /* GPIO can never have been requested */
  157. WARN_ON(1);
  158. return -ENOSYS;
  159. }
  160. static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
  161. {
  162. /* GPIO can never have been requested */
  163. WARN_ON(1);
  164. return -ENOSYS;
  165. }
  166. static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  167. {
  168. /* GPIO can never have been requested */
  169. WARN_ON(1);
  170. return -ENOSYS;
  171. }
  172. static inline int gpiod_get_value(const struct gpio_desc *desc)
  173. {
  174. /* GPIO can never have been requested */
  175. WARN_ON(1);
  176. return 0;
  177. }
  178. static inline void gpiod_set_value(struct gpio_desc *desc, int value)
  179. {
  180. /* GPIO can never have been requested */
  181. WARN_ON(1);
  182. }
  183. static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
  184. {
  185. /* GPIO can never have been requested */
  186. WARN_ON(1);
  187. return 0;
  188. }
  189. static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  190. {
  191. /* GPIO can never have been requested */
  192. WARN_ON(1);
  193. }
  194. static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  195. {
  196. /* GPIO can never have been requested */
  197. WARN_ON(1);
  198. return 0;
  199. }
  200. static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  201. {
  202. /* GPIO can never have been requested */
  203. WARN_ON(1);
  204. }
  205. static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  206. {
  207. /* GPIO can never have been requested */
  208. WARN_ON(1);
  209. return 0;
  210. }
  211. static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
  212. int value)
  213. {
  214. /* GPIO can never have been requested */
  215. WARN_ON(1);
  216. }
  217. static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
  218. {
  219. /* GPIO can never have been requested */
  220. WARN_ON(1);
  221. return -ENOSYS;
  222. }
  223. static inline int gpiod_is_active_low(const struct gpio_desc *desc)
  224. {
  225. /* GPIO can never have been requested */
  226. WARN_ON(1);
  227. return 0;
  228. }
  229. static inline int gpiod_cansleep(const struct gpio_desc *desc)
  230. {
  231. /* GPIO can never have been requested */
  232. WARN_ON(1);
  233. return 0;
  234. }
  235. static inline int gpiod_to_irq(const struct gpio_desc *desc)
  236. {
  237. /* GPIO can never have been requested */
  238. WARN_ON(1);
  239. return -EINVAL;
  240. }
  241. static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
  242. {
  243. return ERR_PTR(-EINVAL);
  244. }
  245. static inline int desc_to_gpio(const struct gpio_desc *desc)
  246. {
  247. /* GPIO can never have been requested */
  248. WARN_ON(1);
  249. return -EINVAL;
  250. }
  251. #endif /* CONFIG_GPIOLIB */
  252. /*
  253. * Vararg-hacks! This is done to transition the kernel to always pass
  254. * the options flags argument to the below functions. During a transition
  255. * phase these vararg macros make both old-and-newstyle code compile,
  256. * but when all calls to the elder API are removed, these should go away
  257. * and the __gpiod_get() etc functions above be renamed just gpiod_get()
  258. * etc.
  259. */
  260. #define __gpiod_get(dev, con_id, flags, ...) __gpiod_get(dev, con_id, flags)
  261. #define gpiod_get(varargs...) __gpiod_get(varargs, 0)
  262. #define __gpiod_get_index(dev, con_id, index, flags, ...) \
  263. __gpiod_get_index(dev, con_id, index, flags)
  264. #define gpiod_get_index(varargs...) __gpiod_get_index(varargs, 0)
  265. #define __gpiod_get_optional(dev, con_id, flags, ...) \
  266. __gpiod_get_optional(dev, con_id, flags)
  267. #define gpiod_get_optional(varargs...) __gpiod_get_optional(varargs, 0)
  268. #define __gpiod_get_index_optional(dev, con_id, index, flags, ...) \
  269. __gpiod_get_index_optional(dev, con_id, index, flags)
  270. #define gpiod_get_index_optional(varargs...) \
  271. __gpiod_get_index_optional(varargs, 0)
  272. #define __devm_gpiod_get(dev, con_id, flags, ...) \
  273. __devm_gpiod_get(dev, con_id, flags)
  274. #define devm_gpiod_get(varargs...) __devm_gpiod_get(varargs, 0)
  275. #define __devm_gpiod_get_index(dev, con_id, index, flags, ...) \
  276. __devm_gpiod_get_index(dev, con_id, index, flags)
  277. #define devm_gpiod_get_index(varargs...) __devm_gpiod_get_index(varargs, 0)
  278. #define __devm_gpiod_get_optional(dev, con_id, flags, ...) \
  279. __devm_gpiod_get_optional(dev, con_id, flags)
  280. #define devm_gpiod_get_optional(varargs...) \
  281. __devm_gpiod_get_optional(varargs, 0)
  282. #define __devm_gpiod_get_index_optional(dev, con_id, index, flags, ...) \
  283. __devm_gpiod_get_index_optional(dev, con_id, index, flags)
  284. #define devm_gpiod_get_index_optional(varargs...) \
  285. __devm_gpiod_get_index_optional(varargs, 0)
  286. #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
  287. int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
  288. int gpiod_export_link(struct device *dev, const char *name,
  289. struct gpio_desc *desc);
  290. int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
  291. void gpiod_unexport(struct gpio_desc *desc);
  292. #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  293. static inline int gpiod_export(struct gpio_desc *desc,
  294. bool direction_may_change)
  295. {
  296. return -ENOSYS;
  297. }
  298. static inline int gpiod_export_link(struct device *dev, const char *name,
  299. struct gpio_desc *desc)
  300. {
  301. return -ENOSYS;
  302. }
  303. static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
  304. {
  305. return -ENOSYS;
  306. }
  307. static inline void gpiod_unexport(struct gpio_desc *desc)
  308. {
  309. }
  310. #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  311. #endif