consumer.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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_OUT_LOW | GPIOD_FLAGS_BIT_OPEN_DRAIN,
  39. GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_OPEN_DRAIN,
  40. };
  41. #ifdef CONFIG_GPIOLIB
  42. /* Return the number of GPIOs associated with a device / function */
  43. int gpiod_count(struct device *dev, const char *con_id);
  44. /* Acquire and dispose GPIOs */
  45. struct gpio_desc *__must_check gpiod_get(struct device *dev,
  46. const char *con_id,
  47. enum gpiod_flags flags);
  48. struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
  49. const char *con_id,
  50. unsigned int idx,
  51. enum gpiod_flags flags);
  52. struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
  53. const char *con_id,
  54. enum gpiod_flags flags);
  55. struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
  56. const char *con_id,
  57. unsigned int index,
  58. enum gpiod_flags flags);
  59. struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
  60. const char *con_id,
  61. enum gpiod_flags flags);
  62. struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
  63. const char *con_id,
  64. enum gpiod_flags flags);
  65. void gpiod_put(struct gpio_desc *desc);
  66. void gpiod_put_array(struct gpio_descs *descs);
  67. struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
  68. const char *con_id,
  69. enum gpiod_flags flags);
  70. struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
  71. const char *con_id,
  72. unsigned int idx,
  73. enum gpiod_flags flags);
  74. struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
  75. const char *con_id,
  76. enum gpiod_flags flags);
  77. struct gpio_desc *__must_check
  78. devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
  79. unsigned int index, enum gpiod_flags flags);
  80. struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
  81. const char *con_id,
  82. enum gpiod_flags flags);
  83. struct gpio_descs *__must_check
  84. devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
  85. enum gpiod_flags flags);
  86. void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
  87. void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
  88. int gpiod_get_direction(struct gpio_desc *desc);
  89. int gpiod_direction_input(struct gpio_desc *desc);
  90. int gpiod_direction_output(struct gpio_desc *desc, int value);
  91. int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
  92. /* Value get/set from non-sleeping context */
  93. int gpiod_get_value(const struct gpio_desc *desc);
  94. int gpiod_get_array_value(unsigned int array_size,
  95. struct gpio_desc **desc_array,
  96. unsigned long *value_bitmap);
  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,
  100. unsigned long *value_bitmap);
  101. int gpiod_get_raw_value(const struct gpio_desc *desc);
  102. int gpiod_get_raw_array_value(unsigned int array_size,
  103. struct gpio_desc **desc_array,
  104. unsigned long *value_bitmap);
  105. void gpiod_set_raw_value(struct gpio_desc *desc, int value);
  106. int gpiod_set_raw_array_value(unsigned int array_size,
  107. struct gpio_desc **desc_array,
  108. unsigned long *value_bitmap);
  109. /* Value get/set from sleeping context */
  110. int gpiod_get_value_cansleep(const struct gpio_desc *desc);
  111. int gpiod_get_array_value_cansleep(unsigned int array_size,
  112. struct gpio_desc **desc_array,
  113. unsigned long *value_bitmap);
  114. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
  115. void gpiod_set_array_value_cansleep(unsigned int array_size,
  116. struct gpio_desc **desc_array,
  117. unsigned long *value_bitmap);
  118. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
  119. int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
  120. struct gpio_desc **desc_array,
  121. unsigned long *value_bitmap);
  122. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
  123. int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
  124. struct gpio_desc **desc_array,
  125. unsigned long *value_bitmap);
  126. int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
  127. int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
  128. int gpiod_is_active_low(const struct gpio_desc *desc);
  129. int gpiod_cansleep(const struct gpio_desc *desc);
  130. int gpiod_to_irq(const struct gpio_desc *desc);
  131. void gpiod_set_consumer_name(struct gpio_desc *desc, const char *name);
  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. unsigned long *value_bitmap)
  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. unsigned long *value_bitmap)
  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. unsigned long *value_bitmap)
  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 int gpiod_set_raw_array_value(unsigned int array_size,
  330. struct gpio_desc **desc_array,
  331. unsigned long *value_bitmap)
  332. {
  333. /* GPIO can never have been requested */
  334. WARN_ON(1);
  335. return 0;
  336. }
  337. static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  338. {
  339. /* GPIO can never have been requested */
  340. WARN_ON(1);
  341. return 0;
  342. }
  343. static inline int gpiod_get_array_value_cansleep(unsigned int array_size,
  344. struct gpio_desc **desc_array,
  345. unsigned long *value_bitmap)
  346. {
  347. /* GPIO can never have been requested */
  348. WARN_ON(1);
  349. return 0;
  350. }
  351. static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  352. {
  353. /* GPIO can never have been requested */
  354. WARN_ON(1);
  355. }
  356. static inline void gpiod_set_array_value_cansleep(unsigned int array_size,
  357. struct gpio_desc **desc_array,
  358. unsigned long *value_bitmap)
  359. {
  360. /* GPIO can never have been requested */
  361. WARN_ON(1);
  362. }
  363. static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  364. {
  365. /* GPIO can never have been requested */
  366. WARN_ON(1);
  367. return 0;
  368. }
  369. static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
  370. struct gpio_desc **desc_array,
  371. unsigned long *value_bitmap)
  372. {
  373. /* GPIO can never have been requested */
  374. WARN_ON(1);
  375. return 0;
  376. }
  377. static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
  378. int value)
  379. {
  380. /* GPIO can never have been requested */
  381. WARN_ON(1);
  382. }
  383. static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
  384. struct gpio_desc **desc_array,
  385. unsigned long *value_bitmap)
  386. {
  387. /* GPIO can never have been requested */
  388. WARN_ON(1);
  389. return 0;
  390. }
  391. static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
  392. {
  393. /* GPIO can never have been requested */
  394. WARN_ON(1);
  395. return -ENOSYS;
  396. }
  397. static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
  398. {
  399. /* GPIO can never have been requested */
  400. WARN_ON(1);
  401. return -ENOSYS;
  402. }
  403. static inline int gpiod_is_active_low(const struct gpio_desc *desc)
  404. {
  405. /* GPIO can never have been requested */
  406. WARN_ON(1);
  407. return 0;
  408. }
  409. static inline int gpiod_cansleep(const struct gpio_desc *desc)
  410. {
  411. /* GPIO can never have been requested */
  412. WARN_ON(1);
  413. return 0;
  414. }
  415. static inline int gpiod_to_irq(const struct gpio_desc *desc)
  416. {
  417. /* GPIO can never have been requested */
  418. WARN_ON(1);
  419. return -EINVAL;
  420. }
  421. static inline void gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
  422. {
  423. /* GPIO can never have been requested */
  424. WARN_ON(1);
  425. }
  426. static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
  427. {
  428. return ERR_PTR(-EINVAL);
  429. }
  430. static inline int desc_to_gpio(const struct gpio_desc *desc)
  431. {
  432. /* GPIO can never have been requested */
  433. WARN_ON(1);
  434. return -EINVAL;
  435. }
  436. /* Child properties interface */
  437. struct device_node;
  438. struct fwnode_handle;
  439. static inline
  440. struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
  441. struct device_node *node,
  442. const char *propname, int index,
  443. enum gpiod_flags dflags,
  444. const char *label)
  445. {
  446. return ERR_PTR(-ENOSYS);
  447. }
  448. static inline
  449. struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
  450. const char *propname, int index,
  451. enum gpiod_flags dflags,
  452. const char *label)
  453. {
  454. return ERR_PTR(-ENOSYS);
  455. }
  456. static inline
  457. struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
  458. const char *con_id, int index,
  459. struct fwnode_handle *child,
  460. enum gpiod_flags flags,
  461. const char *label)
  462. {
  463. return ERR_PTR(-ENOSYS);
  464. }
  465. #endif /* CONFIG_GPIOLIB */
  466. static inline
  467. struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
  468. const char *con_id,
  469. struct fwnode_handle *child,
  470. enum gpiod_flags flags,
  471. const char *label)
  472. {
  473. return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child,
  474. flags, label);
  475. }
  476. #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
  477. int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
  478. int gpiod_export_link(struct device *dev, const char *name,
  479. struct gpio_desc *desc);
  480. void gpiod_unexport(struct gpio_desc *desc);
  481. #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  482. static inline int gpiod_export(struct gpio_desc *desc,
  483. bool direction_may_change)
  484. {
  485. return -ENOSYS;
  486. }
  487. static inline int gpiod_export_link(struct device *dev, const char *name,
  488. struct gpio_desc *desc)
  489. {
  490. return -ENOSYS;
  491. }
  492. static inline void gpiod_unexport(struct gpio_desc *desc)
  493. {
  494. }
  495. #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  496. #endif