consumer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. /**
  16. * Struct containing an array of descriptors that can be obtained using
  17. * gpiod_get_array().
  18. */
  19. struct gpio_descs {
  20. unsigned int ndescs;
  21. struct gpio_desc *desc[];
  22. };
  23. #define GPIOD_FLAGS_BIT_DIR_SET BIT(0)
  24. #define GPIOD_FLAGS_BIT_DIR_OUT BIT(1)
  25. #define GPIOD_FLAGS_BIT_DIR_VAL BIT(2)
  26. /**
  27. * Optional flags that can be passed to one of gpiod_* to configure direction
  28. * and output value. These values cannot be OR'd.
  29. */
  30. enum gpiod_flags {
  31. GPIOD_ASIS = 0,
  32. GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET,
  33. GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,
  34. GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
  35. GPIOD_FLAGS_BIT_DIR_VAL,
  36. };
  37. #ifdef CONFIG_GPIOLIB
  38. /* Return the number of GPIOs associated with a device / function */
  39. int gpiod_count(struct device *dev, const char *con_id);
  40. /* Acquire and dispose GPIOs */
  41. struct gpio_desc *__must_check gpiod_get(struct device *dev,
  42. const char *con_id,
  43. enum gpiod_flags flags);
  44. struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
  45. const char *con_id,
  46. unsigned int idx,
  47. enum gpiod_flags flags);
  48. struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
  49. const char *con_id,
  50. enum gpiod_flags flags);
  51. struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
  52. const char *con_id,
  53. unsigned int index,
  54. enum gpiod_flags flags);
  55. struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
  56. const char *con_id,
  57. enum gpiod_flags flags);
  58. struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
  59. const char *con_id,
  60. enum gpiod_flags flags);
  61. void gpiod_put(struct gpio_desc *desc);
  62. void gpiod_put_array(struct gpio_descs *descs);
  63. struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
  64. const char *con_id,
  65. enum gpiod_flags flags);
  66. struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
  67. const char *con_id,
  68. unsigned int idx,
  69. enum gpiod_flags flags);
  70. struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
  71. const char *con_id,
  72. enum gpiod_flags flags);
  73. struct gpio_desc *__must_check
  74. devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
  75. unsigned int index, enum gpiod_flags flags);
  76. struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
  77. const char *con_id,
  78. enum gpiod_flags flags);
  79. struct gpio_descs *__must_check
  80. devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
  81. enum gpiod_flags flags);
  82. void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
  83. void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
  84. int gpiod_get_direction(struct gpio_desc *desc);
  85. int gpiod_direction_input(struct gpio_desc *desc);
  86. int gpiod_direction_output(struct gpio_desc *desc, int value);
  87. int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
  88. /* Value get/set from non-sleeping context */
  89. int gpiod_get_value(const struct gpio_desc *desc);
  90. void gpiod_set_value(struct gpio_desc *desc, int value);
  91. void gpiod_set_array_value(unsigned int array_size,
  92. struct gpio_desc **desc_array, int *value_array);
  93. int gpiod_get_raw_value(const struct gpio_desc *desc);
  94. void gpiod_set_raw_value(struct gpio_desc *desc, int value);
  95. void gpiod_set_raw_array_value(unsigned int array_size,
  96. struct gpio_desc **desc_array,
  97. int *value_array);
  98. /* Value get/set from sleeping context */
  99. int gpiod_get_value_cansleep(const struct gpio_desc *desc);
  100. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
  101. void gpiod_set_array_value_cansleep(unsigned int array_size,
  102. struct gpio_desc **desc_array,
  103. int *value_array);
  104. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
  105. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
  106. void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
  107. struct gpio_desc **desc_array,
  108. int *value_array);
  109. int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
  110. int gpiod_is_active_low(const struct gpio_desc *desc);
  111. int gpiod_cansleep(const struct gpio_desc *desc);
  112. int gpiod_to_irq(const struct gpio_desc *desc);
  113. /* Convert between the old gpio_ and new gpiod_ interfaces */
  114. struct gpio_desc *gpio_to_desc(unsigned gpio);
  115. int desc_to_gpio(const struct gpio_desc *desc);
  116. /* Child properties interface */
  117. struct fwnode_handle;
  118. struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
  119. const char *propname);
  120. struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
  121. const char *con_id,
  122. struct fwnode_handle *child);
  123. #else /* CONFIG_GPIOLIB */
  124. static inline int gpiod_count(struct device *dev, const char *con_id)
  125. {
  126. return 0;
  127. }
  128. static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
  129. const char *con_id,
  130. enum gpiod_flags flags)
  131. {
  132. return ERR_PTR(-ENOSYS);
  133. }
  134. static inline struct gpio_desc *__must_check
  135. gpiod_get_index(struct device *dev,
  136. const char *con_id,
  137. unsigned int idx,
  138. enum gpiod_flags flags)
  139. {
  140. return ERR_PTR(-ENOSYS);
  141. }
  142. static inline struct gpio_desc *__must_check
  143. gpiod_get_optional(struct device *dev, const char *con_id,
  144. enum gpiod_flags flags)
  145. {
  146. return ERR_PTR(-ENOSYS);
  147. }
  148. static inline struct gpio_desc *__must_check
  149. gpiod_get_index_optional(struct device *dev, const char *con_id,
  150. unsigned int index, enum gpiod_flags flags)
  151. {
  152. return ERR_PTR(-ENOSYS);
  153. }
  154. static inline struct gpio_descs *__must_check
  155. gpiod_get_array(struct device *dev, const char *con_id,
  156. enum gpiod_flags flags)
  157. {
  158. return ERR_PTR(-ENOSYS);
  159. }
  160. static inline struct gpio_descs *__must_check
  161. gpiod_get_array_optional(struct device *dev, const char *con_id,
  162. enum gpiod_flags flags)
  163. {
  164. return ERR_PTR(-ENOSYS);
  165. }
  166. static inline void gpiod_put(struct gpio_desc *desc)
  167. {
  168. might_sleep();
  169. /* GPIO can never have been requested */
  170. WARN_ON(1);
  171. }
  172. static inline void gpiod_put_array(struct gpio_descs *descs)
  173. {
  174. might_sleep();
  175. /* GPIO can never have been requested */
  176. WARN_ON(1);
  177. }
  178. static inline struct gpio_desc *__must_check
  179. devm_gpiod_get(struct device *dev,
  180. const char *con_id,
  181. enum gpiod_flags flags)
  182. {
  183. return ERR_PTR(-ENOSYS);
  184. }
  185. static inline
  186. struct gpio_desc *__must_check
  187. devm_gpiod_get_index(struct device *dev,
  188. const char *con_id,
  189. unsigned int idx,
  190. enum gpiod_flags flags)
  191. {
  192. return ERR_PTR(-ENOSYS);
  193. }
  194. static inline struct gpio_desc *__must_check
  195. devm_gpiod_get_optional(struct device *dev, const char *con_id,
  196. enum gpiod_flags flags)
  197. {
  198. return ERR_PTR(-ENOSYS);
  199. }
  200. static inline struct gpio_desc *__must_check
  201. devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
  202. unsigned int index, enum gpiod_flags flags)
  203. {
  204. return ERR_PTR(-ENOSYS);
  205. }
  206. static inline struct gpio_descs *__must_check
  207. devm_gpiod_get_array(struct device *dev, const char *con_id,
  208. enum gpiod_flags flags)
  209. {
  210. return ERR_PTR(-ENOSYS);
  211. }
  212. static inline struct gpio_descs *__must_check
  213. devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
  214. enum gpiod_flags flags)
  215. {
  216. return ERR_PTR(-ENOSYS);
  217. }
  218. static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
  219. {
  220. might_sleep();
  221. /* GPIO can never have been requested */
  222. WARN_ON(1);
  223. }
  224. static inline void devm_gpiod_put_array(struct device *dev,
  225. struct gpio_descs *descs)
  226. {
  227. might_sleep();
  228. /* GPIO can never have been requested */
  229. WARN_ON(1);
  230. }
  231. static inline int gpiod_get_direction(const struct gpio_desc *desc)
  232. {
  233. /* GPIO can never have been requested */
  234. WARN_ON(1);
  235. return -ENOSYS;
  236. }
  237. static inline int gpiod_direction_input(struct gpio_desc *desc)
  238. {
  239. /* GPIO can never have been requested */
  240. WARN_ON(1);
  241. return -ENOSYS;
  242. }
  243. static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
  244. {
  245. /* GPIO can never have been requested */
  246. WARN_ON(1);
  247. return -ENOSYS;
  248. }
  249. static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  250. {
  251. /* GPIO can never have been requested */
  252. WARN_ON(1);
  253. return -ENOSYS;
  254. }
  255. static inline int gpiod_get_value(const struct gpio_desc *desc)
  256. {
  257. /* GPIO can never have been requested */
  258. WARN_ON(1);
  259. return 0;
  260. }
  261. static inline void gpiod_set_value(struct gpio_desc *desc, int value)
  262. {
  263. /* GPIO can never have been requested */
  264. WARN_ON(1);
  265. }
  266. static inline void gpiod_set_array_value(unsigned int array_size,
  267. struct gpio_desc **desc_array,
  268. int *value_array)
  269. {
  270. /* GPIO can never have been requested */
  271. WARN_ON(1);
  272. }
  273. static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
  274. {
  275. /* GPIO can never have been requested */
  276. WARN_ON(1);
  277. return 0;
  278. }
  279. static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  280. {
  281. /* GPIO can never have been requested */
  282. WARN_ON(1);
  283. }
  284. static inline void gpiod_set_raw_array_value(unsigned int array_size,
  285. struct gpio_desc **desc_array,
  286. int *value_array)
  287. {
  288. /* GPIO can never have been requested */
  289. WARN_ON(1);
  290. }
  291. static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  292. {
  293. /* GPIO can never have been requested */
  294. WARN_ON(1);
  295. return 0;
  296. }
  297. static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  298. {
  299. /* GPIO can never have been requested */
  300. WARN_ON(1);
  301. }
  302. static inline void gpiod_set_array_value_cansleep(unsigned int array_size,
  303. struct gpio_desc **desc_array,
  304. int *value_array)
  305. {
  306. /* GPIO can never have been requested */
  307. WARN_ON(1);
  308. }
  309. static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  310. {
  311. /* GPIO can never have been requested */
  312. WARN_ON(1);
  313. return 0;
  314. }
  315. static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
  316. int value)
  317. {
  318. /* GPIO can never have been requested */
  319. WARN_ON(1);
  320. }
  321. static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
  322. struct gpio_desc **desc_array,
  323. int *value_array)
  324. {
  325. /* GPIO can never have been requested */
  326. WARN_ON(1);
  327. }
  328. static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
  329. {
  330. /* GPIO can never have been requested */
  331. WARN_ON(1);
  332. return -ENOSYS;
  333. }
  334. static inline int gpiod_is_active_low(const struct gpio_desc *desc)
  335. {
  336. /* GPIO can never have been requested */
  337. WARN_ON(1);
  338. return 0;
  339. }
  340. static inline int gpiod_cansleep(const struct gpio_desc *desc)
  341. {
  342. /* GPIO can never have been requested */
  343. WARN_ON(1);
  344. return 0;
  345. }
  346. static inline int gpiod_to_irq(const struct gpio_desc *desc)
  347. {
  348. /* GPIO can never have been requested */
  349. WARN_ON(1);
  350. return -EINVAL;
  351. }
  352. static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
  353. {
  354. return ERR_PTR(-EINVAL);
  355. }
  356. static inline int desc_to_gpio(const struct gpio_desc *desc)
  357. {
  358. /* GPIO can never have been requested */
  359. WARN_ON(1);
  360. return -EINVAL;
  361. }
  362. /* Child properties interface */
  363. struct fwnode_handle;
  364. static inline struct gpio_desc *fwnode_get_named_gpiod(
  365. struct fwnode_handle *fwnode, const char *propname)
  366. {
  367. return ERR_PTR(-ENOSYS);
  368. }
  369. static inline struct gpio_desc *devm_get_gpiod_from_child(
  370. struct device *dev, const char *con_id, struct fwnode_handle *child)
  371. {
  372. return ERR_PTR(-ENOSYS);
  373. }
  374. #endif /* CONFIG_GPIOLIB */
  375. #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
  376. int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
  377. int gpiod_export_link(struct device *dev, const char *name,
  378. struct gpio_desc *desc);
  379. void gpiod_unexport(struct gpio_desc *desc);
  380. #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  381. static inline int gpiod_export(struct gpio_desc *desc,
  382. bool direction_may_change)
  383. {
  384. return -ENOSYS;
  385. }
  386. static inline int gpiod_export_link(struct device *dev, const char *name,
  387. struct gpio_desc *desc)
  388. {
  389. return -ENOSYS;
  390. }
  391. static inline void gpiod_unexport(struct gpio_desc *desc)
  392. {
  393. }
  394. #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  395. #endif