consumer.h 15 KB

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