consumer.h 14 KB

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