consumer.h 5.8 KB

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