consumer.h 16 KB

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