consumer.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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(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(unsigned int array_size,
  96. struct gpio_desc **desc_array, int *value_array);
  97. /* Value get/set from sleeping context */
  98. int gpiod_get_value_cansleep(const struct gpio_desc *desc);
  99. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
  100. void gpiod_set_array_cansleep(unsigned int array_size,
  101. struct gpio_desc **desc_array,
  102. int *value_array);
  103. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
  104. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
  105. void gpiod_set_raw_array_cansleep(unsigned int array_size,
  106. struct gpio_desc **desc_array,
  107. int *value_array);
  108. int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
  109. int gpiod_is_active_low(const struct gpio_desc *desc);
  110. int gpiod_cansleep(const struct gpio_desc *desc);
  111. int gpiod_to_irq(const struct gpio_desc *desc);
  112. /* Convert between the old gpio_ and new gpiod_ interfaces */
  113. struct gpio_desc *gpio_to_desc(unsigned gpio);
  114. int desc_to_gpio(const struct gpio_desc *desc);
  115. /* Child properties interface */
  116. struct fwnode_handle;
  117. struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
  118. const char *propname);
  119. struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
  120. const char *con_id,
  121. struct fwnode_handle *child);
  122. #else /* CONFIG_GPIOLIB */
  123. static inline int gpiod_count(struct device *dev, const char *con_id)
  124. {
  125. return 0;
  126. }
  127. static inline struct gpio_desc *__must_check __gpiod_get(struct device *dev,
  128. const char *con_id,
  129. enum gpiod_flags flags)
  130. {
  131. return ERR_PTR(-ENOSYS);
  132. }
  133. static inline struct gpio_desc *__must_check
  134. __gpiod_get_index(struct device *dev,
  135. const char *con_id,
  136. unsigned int idx,
  137. enum gpiod_flags flags)
  138. {
  139. return ERR_PTR(-ENOSYS);
  140. }
  141. static inline struct gpio_desc *__must_check
  142. __gpiod_get_optional(struct device *dev, const char *con_id,
  143. enum gpiod_flags flags)
  144. {
  145. return ERR_PTR(-ENOSYS);
  146. }
  147. static inline struct gpio_desc *__must_check
  148. __gpiod_get_index_optional(struct device *dev, const char *con_id,
  149. unsigned int index, enum gpiod_flags flags)
  150. {
  151. return ERR_PTR(-ENOSYS);
  152. }
  153. static inline struct gpio_descs *__must_check
  154. gpiod_get_array(struct device *dev, const char *con_id,
  155. enum gpiod_flags flags)
  156. {
  157. return ERR_PTR(-ENOSYS);
  158. }
  159. static inline struct gpio_descs *__must_check
  160. gpiod_get_array_optional(struct device *dev, const char *con_id,
  161. enum gpiod_flags flags)
  162. {
  163. return ERR_PTR(-ENOSYS);
  164. }
  165. static inline void gpiod_put(struct gpio_desc *desc)
  166. {
  167. might_sleep();
  168. /* GPIO can never have been requested */
  169. WARN_ON(1);
  170. }
  171. static inline void gpiod_put_array(struct gpio_descs *descs)
  172. {
  173. might_sleep();
  174. /* GPIO can never have been requested */
  175. WARN_ON(1);
  176. }
  177. static inline struct gpio_desc *__must_check
  178. __devm_gpiod_get(struct device *dev,
  179. const char *con_id,
  180. enum gpiod_flags flags)
  181. {
  182. return ERR_PTR(-ENOSYS);
  183. }
  184. static inline
  185. struct gpio_desc *__must_check
  186. __devm_gpiod_get_index(struct device *dev,
  187. const char *con_id,
  188. unsigned int idx,
  189. enum gpiod_flags flags)
  190. {
  191. return ERR_PTR(-ENOSYS);
  192. }
  193. static inline struct gpio_desc *__must_check
  194. __devm_gpiod_get_optional(struct device *dev, const char *con_id,
  195. enum gpiod_flags flags)
  196. {
  197. return ERR_PTR(-ENOSYS);
  198. }
  199. static inline struct gpio_desc *__must_check
  200. __devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
  201. unsigned int index, enum gpiod_flags flags)
  202. {
  203. return ERR_PTR(-ENOSYS);
  204. }
  205. static inline struct gpio_descs *__must_check
  206. devm_gpiod_get_array(struct device *dev, const char *con_id,
  207. enum gpiod_flags flags)
  208. {
  209. return ERR_PTR(-ENOSYS);
  210. }
  211. static inline struct gpio_descs *__must_check
  212. devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
  213. enum gpiod_flags flags)
  214. {
  215. return ERR_PTR(-ENOSYS);
  216. }
  217. static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
  218. {
  219. might_sleep();
  220. /* GPIO can never have been requested */
  221. WARN_ON(1);
  222. }
  223. static inline void devm_gpiod_put_array(struct device *dev,
  224. struct gpio_descs *descs)
  225. {
  226. might_sleep();
  227. /* GPIO can never have been requested */
  228. WARN_ON(1);
  229. }
  230. static inline int gpiod_get_direction(const struct gpio_desc *desc)
  231. {
  232. /* GPIO can never have been requested */
  233. WARN_ON(1);
  234. return -ENOSYS;
  235. }
  236. static inline int gpiod_direction_input(struct gpio_desc *desc)
  237. {
  238. /* GPIO can never have been requested */
  239. WARN_ON(1);
  240. return -ENOSYS;
  241. }
  242. static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
  243. {
  244. /* GPIO can never have been requested */
  245. WARN_ON(1);
  246. return -ENOSYS;
  247. }
  248. static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  249. {
  250. /* GPIO can never have been requested */
  251. WARN_ON(1);
  252. return -ENOSYS;
  253. }
  254. static inline int gpiod_get_value(const struct gpio_desc *desc)
  255. {
  256. /* GPIO can never have been requested */
  257. WARN_ON(1);
  258. return 0;
  259. }
  260. static inline void gpiod_set_value(struct gpio_desc *desc, int value)
  261. {
  262. /* GPIO can never have been requested */
  263. WARN_ON(1);
  264. }
  265. static inline void gpiod_set_array(unsigned int array_size,
  266. struct gpio_desc **desc_array,
  267. int *value_array)
  268. {
  269. /* GPIO can never have been requested */
  270. WARN_ON(1);
  271. }
  272. static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
  273. {
  274. /* GPIO can never have been requested */
  275. WARN_ON(1);
  276. return 0;
  277. }
  278. static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  279. {
  280. /* GPIO can never have been requested */
  281. WARN_ON(1);
  282. }
  283. static inline void gpiod_set_raw_array(unsigned int array_size,
  284. struct gpio_desc **desc_array,
  285. int *value_array)
  286. {
  287. /* GPIO can never have been requested */
  288. WARN_ON(1);
  289. }
  290. static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  291. {
  292. /* GPIO can never have been requested */
  293. WARN_ON(1);
  294. return 0;
  295. }
  296. static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  297. {
  298. /* GPIO can never have been requested */
  299. WARN_ON(1);
  300. }
  301. static inline void gpiod_set_array_cansleep(unsigned int array_size,
  302. struct gpio_desc **desc_array,
  303. int *value_array)
  304. {
  305. /* GPIO can never have been requested */
  306. WARN_ON(1);
  307. }
  308. static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  309. {
  310. /* GPIO can never have been requested */
  311. WARN_ON(1);
  312. return 0;
  313. }
  314. static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
  315. int value)
  316. {
  317. /* GPIO can never have been requested */
  318. WARN_ON(1);
  319. }
  320. static inline void gpiod_set_raw_array_cansleep(unsigned int array_size,
  321. struct gpio_desc **desc_array,
  322. int *value_array)
  323. {
  324. /* GPIO can never have been requested */
  325. WARN_ON(1);
  326. }
  327. static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
  328. {
  329. /* GPIO can never have been requested */
  330. WARN_ON(1);
  331. return -ENOSYS;
  332. }
  333. static inline int gpiod_is_active_low(const struct gpio_desc *desc)
  334. {
  335. /* GPIO can never have been requested */
  336. WARN_ON(1);
  337. return 0;
  338. }
  339. static inline int gpiod_cansleep(const struct gpio_desc *desc)
  340. {
  341. /* GPIO can never have been requested */
  342. WARN_ON(1);
  343. return 0;
  344. }
  345. static inline int gpiod_to_irq(const struct gpio_desc *desc)
  346. {
  347. /* GPIO can never have been requested */
  348. WARN_ON(1);
  349. return -EINVAL;
  350. }
  351. static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
  352. {
  353. return ERR_PTR(-EINVAL);
  354. }
  355. static inline int desc_to_gpio(const struct gpio_desc *desc)
  356. {
  357. /* GPIO can never have been requested */
  358. WARN_ON(1);
  359. return -EINVAL;
  360. }
  361. #endif /* CONFIG_GPIOLIB */
  362. /*
  363. * Vararg-hacks! This is done to transition the kernel to always pass
  364. * the options flags argument to the below functions. During a transition
  365. * phase these vararg macros make both old-and-newstyle code compile,
  366. * but when all calls to the elder API are removed, these should go away
  367. * and the __gpiod_get() etc functions above be renamed just gpiod_get()
  368. * etc.
  369. */
  370. #define __gpiod_get(dev, con_id, flags, ...) __gpiod_get(dev, con_id, flags)
  371. #define gpiod_get(varargs...) __gpiod_get(varargs, GPIOD_ASIS)
  372. #define __gpiod_get_index(dev, con_id, index, flags, ...) \
  373. __gpiod_get_index(dev, con_id, index, flags)
  374. #define gpiod_get_index(varargs...) __gpiod_get_index(varargs, GPIOD_ASIS)
  375. #define __gpiod_get_optional(dev, con_id, flags, ...) \
  376. __gpiod_get_optional(dev, con_id, flags)
  377. #define gpiod_get_optional(varargs...) __gpiod_get_optional(varargs, GPIOD_ASIS)
  378. #define __gpiod_get_index_optional(dev, con_id, index, flags, ...) \
  379. __gpiod_get_index_optional(dev, con_id, index, flags)
  380. #define gpiod_get_index_optional(varargs...) \
  381. __gpiod_get_index_optional(varargs, GPIOD_ASIS)
  382. #define __devm_gpiod_get(dev, con_id, flags, ...) \
  383. __devm_gpiod_get(dev, con_id, flags)
  384. #define devm_gpiod_get(varargs...) __devm_gpiod_get(varargs, GPIOD_ASIS)
  385. #define __devm_gpiod_get_index(dev, con_id, index, flags, ...) \
  386. __devm_gpiod_get_index(dev, con_id, index, flags)
  387. #define devm_gpiod_get_index(varargs...) \
  388. __devm_gpiod_get_index(varargs, GPIOD_ASIS)
  389. #define __devm_gpiod_get_optional(dev, con_id, flags, ...) \
  390. __devm_gpiod_get_optional(dev, con_id, flags)
  391. #define devm_gpiod_get_optional(varargs...) \
  392. __devm_gpiod_get_optional(varargs, GPIOD_ASIS)
  393. #define __devm_gpiod_get_index_optional(dev, con_id, index, flags, ...) \
  394. __devm_gpiod_get_index_optional(dev, con_id, index, flags)
  395. #define devm_gpiod_get_index_optional(varargs...) \
  396. __devm_gpiod_get_index_optional(varargs, GPIOD_ASIS)
  397. #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
  398. int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
  399. int gpiod_export_link(struct device *dev, const char *name,
  400. struct gpio_desc *desc);
  401. int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
  402. void gpiod_unexport(struct gpio_desc *desc);
  403. #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  404. static inline int gpiod_export(struct gpio_desc *desc,
  405. bool direction_may_change)
  406. {
  407. return -ENOSYS;
  408. }
  409. static inline int gpiod_export_link(struct device *dev, const char *name,
  410. struct gpio_desc *desc)
  411. {
  412. return -ENOSYS;
  413. }
  414. static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
  415. {
  416. return -ENOSYS;
  417. }
  418. static inline void gpiod_unexport(struct gpio_desc *desc)
  419. {
  420. }
  421. #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  422. #endif