consumer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. enum gpiod_flags dflags,
  121. const char *label);
  122. struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
  123. const char *con_id,
  124. struct fwnode_handle *child,
  125. enum gpiod_flags flags,
  126. const char *label);
  127. #else /* CONFIG_GPIOLIB */
  128. static inline int gpiod_count(struct device *dev, const char *con_id)
  129. {
  130. return 0;
  131. }
  132. static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
  133. const char *con_id,
  134. enum gpiod_flags flags)
  135. {
  136. return ERR_PTR(-ENOSYS);
  137. }
  138. static inline struct gpio_desc *__must_check
  139. gpiod_get_index(struct device *dev,
  140. const char *con_id,
  141. unsigned int idx,
  142. enum gpiod_flags flags)
  143. {
  144. return ERR_PTR(-ENOSYS);
  145. }
  146. static inline struct gpio_desc *__must_check
  147. gpiod_get_optional(struct device *dev, const char *con_id,
  148. enum gpiod_flags flags)
  149. {
  150. return ERR_PTR(-ENOSYS);
  151. }
  152. static inline struct gpio_desc *__must_check
  153. gpiod_get_index_optional(struct device *dev, const char *con_id,
  154. unsigned int index, enum gpiod_flags flags)
  155. {
  156. return ERR_PTR(-ENOSYS);
  157. }
  158. static inline struct gpio_descs *__must_check
  159. gpiod_get_array(struct device *dev, const char *con_id,
  160. enum gpiod_flags flags)
  161. {
  162. return ERR_PTR(-ENOSYS);
  163. }
  164. static inline struct gpio_descs *__must_check
  165. gpiod_get_array_optional(struct device *dev, const char *con_id,
  166. enum gpiod_flags flags)
  167. {
  168. return ERR_PTR(-ENOSYS);
  169. }
  170. static inline void gpiod_put(struct gpio_desc *desc)
  171. {
  172. might_sleep();
  173. /* GPIO can never have been requested */
  174. WARN_ON(1);
  175. }
  176. static inline void gpiod_put_array(struct gpio_descs *descs)
  177. {
  178. might_sleep();
  179. /* GPIO can never have been requested */
  180. WARN_ON(1);
  181. }
  182. static inline struct gpio_desc *__must_check
  183. devm_gpiod_get(struct device *dev,
  184. const char *con_id,
  185. enum gpiod_flags flags)
  186. {
  187. return ERR_PTR(-ENOSYS);
  188. }
  189. static inline
  190. struct gpio_desc *__must_check
  191. devm_gpiod_get_index(struct device *dev,
  192. const char *con_id,
  193. unsigned int idx,
  194. enum gpiod_flags flags)
  195. {
  196. return ERR_PTR(-ENOSYS);
  197. }
  198. static inline struct gpio_desc *__must_check
  199. devm_gpiod_get_optional(struct device *dev, const char *con_id,
  200. enum gpiod_flags flags)
  201. {
  202. return ERR_PTR(-ENOSYS);
  203. }
  204. static inline struct gpio_desc *__must_check
  205. devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
  206. unsigned int index, enum gpiod_flags flags)
  207. {
  208. return ERR_PTR(-ENOSYS);
  209. }
  210. static inline struct gpio_descs *__must_check
  211. devm_gpiod_get_array(struct device *dev, const char *con_id,
  212. enum gpiod_flags flags)
  213. {
  214. return ERR_PTR(-ENOSYS);
  215. }
  216. static inline struct gpio_descs *__must_check
  217. devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
  218. enum gpiod_flags flags)
  219. {
  220. return ERR_PTR(-ENOSYS);
  221. }
  222. static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
  223. {
  224. might_sleep();
  225. /* GPIO can never have been requested */
  226. WARN_ON(1);
  227. }
  228. static inline void devm_gpiod_put_array(struct device *dev,
  229. struct gpio_descs *descs)
  230. {
  231. might_sleep();
  232. /* GPIO can never have been requested */
  233. WARN_ON(1);
  234. }
  235. static inline int gpiod_get_direction(const struct gpio_desc *desc)
  236. {
  237. /* GPIO can never have been requested */
  238. WARN_ON(1);
  239. return -ENOSYS;
  240. }
  241. static inline int gpiod_direction_input(struct gpio_desc *desc)
  242. {
  243. /* GPIO can never have been requested */
  244. WARN_ON(1);
  245. return -ENOSYS;
  246. }
  247. static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
  248. {
  249. /* GPIO can never have been requested */
  250. WARN_ON(1);
  251. return -ENOSYS;
  252. }
  253. static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  254. {
  255. /* GPIO can never have been requested */
  256. WARN_ON(1);
  257. return -ENOSYS;
  258. }
  259. static inline int gpiod_get_value(const struct gpio_desc *desc)
  260. {
  261. /* GPIO can never have been requested */
  262. WARN_ON(1);
  263. return 0;
  264. }
  265. static inline void gpiod_set_value(struct gpio_desc *desc, int value)
  266. {
  267. /* GPIO can never have been requested */
  268. WARN_ON(1);
  269. }
  270. static inline void gpiod_set_array_value(unsigned int array_size,
  271. struct gpio_desc **desc_array,
  272. int *value_array)
  273. {
  274. /* GPIO can never have been requested */
  275. WARN_ON(1);
  276. }
  277. static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
  278. {
  279. /* GPIO can never have been requested */
  280. WARN_ON(1);
  281. return 0;
  282. }
  283. static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  284. {
  285. /* GPIO can never have been requested */
  286. WARN_ON(1);
  287. }
  288. static inline void gpiod_set_raw_array_value(unsigned int array_size,
  289. struct gpio_desc **desc_array,
  290. int *value_array)
  291. {
  292. /* GPIO can never have been requested */
  293. WARN_ON(1);
  294. }
  295. static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  296. {
  297. /* GPIO can never have been requested */
  298. WARN_ON(1);
  299. return 0;
  300. }
  301. static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  302. {
  303. /* GPIO can never have been requested */
  304. WARN_ON(1);
  305. }
  306. static inline void gpiod_set_array_value_cansleep(unsigned int array_size,
  307. struct gpio_desc **desc_array,
  308. int *value_array)
  309. {
  310. /* GPIO can never have been requested */
  311. WARN_ON(1);
  312. }
  313. static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  314. {
  315. /* GPIO can never have been requested */
  316. WARN_ON(1);
  317. return 0;
  318. }
  319. static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
  320. int value)
  321. {
  322. /* GPIO can never have been requested */
  323. WARN_ON(1);
  324. }
  325. static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
  326. struct gpio_desc **desc_array,
  327. int *value_array)
  328. {
  329. /* GPIO can never have been requested */
  330. WARN_ON(1);
  331. }
  332. static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
  333. {
  334. /* GPIO can never have been requested */
  335. WARN_ON(1);
  336. return -ENOSYS;
  337. }
  338. static inline int gpiod_is_active_low(const struct gpio_desc *desc)
  339. {
  340. /* GPIO can never have been requested */
  341. WARN_ON(1);
  342. return 0;
  343. }
  344. static inline int gpiod_cansleep(const struct gpio_desc *desc)
  345. {
  346. /* GPIO can never have been requested */
  347. WARN_ON(1);
  348. return 0;
  349. }
  350. static inline int gpiod_to_irq(const struct gpio_desc *desc)
  351. {
  352. /* GPIO can never have been requested */
  353. WARN_ON(1);
  354. return -EINVAL;
  355. }
  356. static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
  357. {
  358. return ERR_PTR(-EINVAL);
  359. }
  360. static inline int desc_to_gpio(const struct gpio_desc *desc)
  361. {
  362. /* GPIO can never have been requested */
  363. WARN_ON(1);
  364. return -EINVAL;
  365. }
  366. /* Child properties interface */
  367. struct fwnode_handle;
  368. static inline
  369. struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
  370. const char *propname,
  371. enum gpiod_flags dflags,
  372. const char *label)
  373. {
  374. return ERR_PTR(-ENOSYS);
  375. }
  376. static inline
  377. struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
  378. const char *con_id,
  379. struct fwnode_handle *child,
  380. enum gpiod_flags flags,
  381. const char *label)
  382. {
  383. return ERR_PTR(-ENOSYS);
  384. }
  385. #endif /* CONFIG_GPIOLIB */
  386. #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
  387. int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
  388. int gpiod_export_link(struct device *dev, const char *name,
  389. struct gpio_desc *desc);
  390. void gpiod_unexport(struct gpio_desc *desc);
  391. #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  392. static inline int gpiod_export(struct gpio_desc *desc,
  393. bool direction_may_change)
  394. {
  395. return -ENOSYS;
  396. }
  397. static inline int gpiod_export_link(struct device *dev, const char *name,
  398. struct gpio_desc *desc)
  399. {
  400. return -ENOSYS;
  401. }
  402. static inline void gpiod_unexport(struct gpio_desc *desc)
  403. {
  404. }
  405. #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  406. #endif