consumer.h 15 KB

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