gpio.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors.
  4. * Copyright (c) 2011, NVIDIA Corp. All rights reserved.
  5. */
  6. #ifndef _ASM_GENERIC_GPIO_H_
  7. #define _ASM_GENERIC_GPIO_H_
  8. #include <dm/ofnode.h>
  9. #include <linux/bitops.h>
  10. struct acpi_gpio;
  11. struct ofnode_phandle_args;
  12. /*
  13. * Generic GPIO API for U-Boot
  14. *
  15. * --
  16. * NB: This is deprecated. Please use the driver model functions instead:
  17. *
  18. * - gpio_request_by_name()
  19. * - dm_gpio_get_value() etc.
  20. *
  21. * For now we need a dm_ prefix on some functions to avoid name collision.
  22. * --
  23. *
  24. * GPIOs are numbered from 0 to GPIO_COUNT-1 which value is defined
  25. * by the SOC/architecture.
  26. *
  27. * Each GPIO can be an input or output. If an input then its value can
  28. * be read as 0 or 1. If an output then its value can be set to 0 or 1.
  29. * If you try to write an input then the value is undefined. If you try
  30. * to read an output, barring something very unusual, you will get
  31. * back the value of the output that you previously set.
  32. *
  33. * In some cases the operation may fail, for example if the GPIO number
  34. * is out of range, or the GPIO is not available because its pin is
  35. * being used by another function. In that case, functions may return
  36. * an error value of -1.
  37. */
  38. /**
  39. * @deprecated Please use driver model instead
  40. * Request a GPIO. This should be called before any of the other functions
  41. * are used on this GPIO.
  42. *
  43. * Note: With driver model, the label is allocated so there is no need for
  44. * the caller to preserve it.
  45. *
  46. * @param gpio GPIO number
  47. * @param label User label for this GPIO
  48. * @return 0 if ok, -1 on error
  49. */
  50. int gpio_request(unsigned gpio, const char *label);
  51. /**
  52. * @deprecated Please use driver model instead
  53. * Stop using the GPIO. This function should not alter pin configuration.
  54. *
  55. * @param gpio GPIO number
  56. * @return 0 if ok, -1 on error
  57. */
  58. int gpio_free(unsigned gpio);
  59. /**
  60. * @deprecated Please use driver model instead
  61. * Make a GPIO an input.
  62. *
  63. * @param gpio GPIO number
  64. * @return 0 if ok, -1 on error
  65. */
  66. int gpio_direction_input(unsigned gpio);
  67. /**
  68. * @deprecated Please use driver model instead
  69. * Make a GPIO an output, and set its value.
  70. *
  71. * @param gpio GPIO number
  72. * @param value GPIO value (0 for low or 1 for high)
  73. * @return 0 if ok, -1 on error
  74. */
  75. int gpio_direction_output(unsigned gpio, int value);
  76. /**
  77. * @deprecated Please use driver model instead
  78. * Get a GPIO's value. This will work whether the GPIO is an input
  79. * or an output.
  80. *
  81. * @param gpio GPIO number
  82. * @return 0 if low, 1 if high, -1 on error
  83. */
  84. int gpio_get_value(unsigned gpio);
  85. /**
  86. * @deprecated Please use driver model instead
  87. * Set an output GPIO's value. The GPIO must already be an output or
  88. * this function may have no effect.
  89. *
  90. * @param gpio GPIO number
  91. * @param value GPIO value (0 for low or 1 for high)
  92. * @return 0 if ok, -1 on error
  93. */
  94. int gpio_set_value(unsigned gpio, int value);
  95. /* State of a GPIO, as reported by get_function() */
  96. enum gpio_func_t {
  97. GPIOF_INPUT = 0,
  98. GPIOF_OUTPUT,
  99. GPIOF_UNUSED, /* Not claimed */
  100. GPIOF_UNKNOWN, /* Not known */
  101. GPIOF_FUNC, /* Not used as a GPIO */
  102. GPIOF_COUNT,
  103. };
  104. struct udevice;
  105. struct gpio_desc {
  106. struct udevice *dev; /* Device, NULL for invalid GPIO */
  107. unsigned long flags;
  108. #define GPIOD_IS_OUT BIT(1) /* GPIO is an output */
  109. #define GPIOD_IS_IN BIT(2) /* GPIO is an input */
  110. #define GPIOD_ACTIVE_LOW BIT(3) /* GPIO is active when value is low */
  111. #define GPIOD_IS_OUT_ACTIVE BIT(4) /* set output active */
  112. #define GPIOD_OPEN_DRAIN BIT(5) /* GPIO is open drain type */
  113. #define GPIOD_OPEN_SOURCE BIT(6) /* GPIO is open source type */
  114. #define GPIOD_PULL_UP BIT(7) /* GPIO has pull-up enabled */
  115. #define GPIOD_PULL_DOWN BIT(8) /* GPIO has pull-down enabled */
  116. uint offset; /* GPIO offset within the device */
  117. /*
  118. * We could consider adding the GPIO label in here. Possibly we could
  119. * use this structure for internal GPIO information.
  120. */
  121. };
  122. /* helper to compute the value of the gpio output */
  123. #define GPIOD_FLAGS_OUTPUT_MASK (GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE)
  124. #define GPIOD_FLAGS_OUTPUT(flags) \
  125. (((((flags) & GPIOD_FLAGS_OUTPUT_MASK) == GPIOD_IS_OUT_ACTIVE) || \
  126. (((flags) & GPIOD_FLAGS_OUTPUT_MASK) == GPIOD_ACTIVE_LOW)))
  127. /**
  128. * dm_gpio_is_valid() - Check if a GPIO is valid
  129. *
  130. * @desc: GPIO description containing device, offset and flags,
  131. * previously returned by gpio_request_by_name()
  132. * @return true if valid, false if not
  133. */
  134. static inline bool dm_gpio_is_valid(const struct gpio_desc *desc)
  135. {
  136. return desc->dev != NULL;
  137. }
  138. /**
  139. * gpio_get_status() - get the current GPIO status as a string
  140. *
  141. * Obtain the current GPIO status as a string which can be presented to the
  142. * user. A typical string is:
  143. *
  144. * "b4: in: 1 [x] sdmmc_cd"
  145. *
  146. * which means this is GPIO bank b, offset 4, currently set to input, current
  147. * value 1, [x] means that it is requested and the owner is 'sdmmc_cd'
  148. *
  149. * TODO(sjg@chromium.org): This should use struct gpio_desc
  150. *
  151. * @dev: Device to check
  152. * @offset: Offset of device GPIO to check
  153. * @buf: Place to put string
  154. * @buffsize: Size of string including \0
  155. */
  156. int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize);
  157. /**
  158. * gpio_get_function() - get the current function for a GPIO pin
  159. *
  160. * Note this returns GPIOF_UNUSED if the GPIO is not requested.
  161. *
  162. * TODO(sjg@chromium.org): This should use struct gpio_desc
  163. *
  164. * @dev: Device to check
  165. * @offset: Offset of device GPIO to check
  166. * @namep: If non-NULL, this is set to the name given when the GPIO
  167. * was requested, or -1 if it has not been requested
  168. * @return -ENODATA if the driver returned an unknown function,
  169. * -ENODEV if the device is not active, -EINVAL if the offset is invalid.
  170. * GPIOF_UNUSED if the GPIO has not been requested. Otherwise returns the
  171. * function from enum gpio_func_t.
  172. */
  173. int gpio_get_function(struct udevice *dev, int offset, const char **namep);
  174. /**
  175. * gpio_get_raw_function() - get the current raw function for a GPIO pin
  176. *
  177. * Note this does not return GPIOF_UNUSED - it will always return the GPIO
  178. * driver's view of a pin function, even if it is not correctly set up.
  179. *
  180. * TODO(sjg@chromium.org): This should use struct gpio_desc
  181. *
  182. * @dev: Device to check
  183. * @offset: Offset of device GPIO to check
  184. * @namep: If non-NULL, this is set to the name given when the GPIO
  185. * was requested, or -1 if it has not been requested
  186. * @return -ENODATA if the driver returned an unknown function,
  187. * -ENODEV if the device is not active, -EINVAL if the offset is invalid.
  188. * Otherwise returns the function from enum gpio_func_t.
  189. */
  190. int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep);
  191. /**
  192. * gpio_requestf() - request a GPIO using a format string for the owner
  193. *
  194. * This is a helper function for gpio_request(). It allows you to provide
  195. * a printf()-format string for the GPIO owner. It calls gpio_request() with
  196. * the string that is created
  197. */
  198. int gpio_requestf(unsigned gpio, const char *fmt, ...)
  199. __attribute__ ((format (__printf__, 2, 3)));
  200. struct fdtdec_phandle_args;
  201. /**
  202. * gpio_xlate_offs_flags() - implementation for common use of dm_gpio_ops.xlate
  203. *
  204. * This routine sets the offset field to args[0] and the flags field to
  205. * GPIOD_ACTIVE_LOW if the GPIO_ACTIVE_LOW flag is present in args[1].
  206. */
  207. int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
  208. struct ofnode_phandle_args *args);
  209. /**
  210. * struct struct dm_gpio_ops - Driver model GPIO operations
  211. *
  212. * Refer to functions above for description. These function largely copy
  213. * the old API.
  214. *
  215. * This is trying to be close to Linux GPIO API. Once the U-Boot uses the
  216. * new DM GPIO API, this should be really easy to flip over to the Linux
  217. * GPIO API-alike interface.
  218. *
  219. * Also it would be useful to standardise additional functions like
  220. * pullup, slew rate and drive strength.
  221. *
  222. * gpio_request() and gpio_free() are optional - if NULL then they will
  223. * not be called.
  224. *
  225. * Note that @offset is the offset from the base GPIO of the device. So
  226. * offset 0 is the device's first GPIO and offset o-1 is the last GPIO,
  227. * where o is the number of GPIO lines controlled by the device. A device
  228. * is typically used to control a single bank of GPIOs. Within complex
  229. * SoCs there may be many banks and therefore many devices all referring
  230. * to the different IO addresses within the SoC.
  231. *
  232. * The uclass combines all GPIO devices together to provide a consistent
  233. * numbering from 0 to n-1, where n is the number of GPIOs in total across
  234. * all devices. Be careful not to confuse offset with gpio in the parameters.
  235. */
  236. struct dm_gpio_ops {
  237. int (*request)(struct udevice *dev, unsigned offset, const char *label);
  238. int (*rfree)(struct udevice *dev, unsigned int offset);
  239. int (*direction_input)(struct udevice *dev, unsigned offset);
  240. int (*direction_output)(struct udevice *dev, unsigned offset,
  241. int value);
  242. int (*get_value)(struct udevice *dev, unsigned offset);
  243. int (*set_value)(struct udevice *dev, unsigned offset, int value);
  244. /**
  245. * get_function() Get the GPIO function
  246. *
  247. * @dev: Device to check
  248. * @offset: GPIO offset within that device
  249. * @return current function - GPIOF_...
  250. */
  251. int (*get_function)(struct udevice *dev, unsigned offset);
  252. /**
  253. * xlate() - Translate phandle arguments into a GPIO description
  254. *
  255. * This function should set up the fields in desc according to the
  256. * information in the arguments. The uclass will have set up:
  257. *
  258. * @desc->dev to @dev
  259. * @desc->flags to 0
  260. * @desc->offset to 0
  261. *
  262. * This method is optional and defaults to gpio_xlate_offs_flags,
  263. * which will parse offset and the GPIO_ACTIVE_LOW flag in the first
  264. * two arguments.
  265. *
  266. * Note that @dev is passed in as a parameter to follow driver model
  267. * uclass conventions, even though it is already available as
  268. * desc->dev.
  269. *
  270. * @dev: GPIO device
  271. * @desc: Place to put GPIO description
  272. * @args: Arguments provided in description
  273. * @return 0 if OK, -ve on error
  274. */
  275. int (*xlate)(struct udevice *dev, struct gpio_desc *desc,
  276. struct ofnode_phandle_args *args);
  277. /**
  278. * set_dir_flags() - Set GPIO dir flags
  279. *
  280. * This function should set up the GPIO configuration according to the
  281. * information provide by the direction flags bitfield.
  282. *
  283. * This method is optional.
  284. *
  285. * @dev: GPIO device
  286. * @offset: GPIO offset within that device
  287. * @flags: GPIO configuration to use
  288. * @return 0 if OK, -ve on error
  289. */
  290. int (*set_dir_flags)(struct udevice *dev, unsigned int offset,
  291. ulong flags);
  292. /**
  293. * get_dir_flags() - Get GPIO dir flags
  294. *
  295. * This function return the GPIO direction flags used.
  296. *
  297. * This method is optional.
  298. *
  299. * @dev: GPIO device
  300. * @offset: GPIO offset within that device
  301. * @flags: place to put the used direction flags by GPIO
  302. * @return 0 if OK, -ve on error
  303. */
  304. int (*get_dir_flags)(struct udevice *dev, unsigned int offset,
  305. ulong *flags);
  306. #if CONFIG_IS_ENABLED(ACPIGEN)
  307. /**
  308. * get_acpi() - Get the ACPI info for a GPIO
  309. *
  310. * This converts a GPIO to an ACPI structure for adding to the ACPI
  311. * tables.
  312. *
  313. * @desc: GPIO description to convert
  314. * @gpio: Output ACPI GPIO information
  315. * @return ACPI pin number or -ve on error
  316. */
  317. int (*get_acpi)(const struct gpio_desc *desc, struct acpi_gpio *gpio);
  318. #endif
  319. };
  320. /**
  321. * struct gpio_dev_priv - information about a device used by the uclass
  322. *
  323. * The uclass combines all active GPIO devices into a unified numbering
  324. * scheme. To do this it maintains some private information about each
  325. * device.
  326. *
  327. * To implement driver model support in your GPIO driver, add a probe
  328. * handler, and set @gpio_count and @bank_name correctly in that handler.
  329. * This tells the uclass the name of the GPIO bank and the number of GPIOs
  330. * it contains.
  331. *
  332. * @bank_name: Name of the GPIO device (e.g 'a' means GPIOs will be called
  333. * 'A0', 'A1', etc.
  334. * @gpio_count: Number of GPIOs in this device
  335. * @gpio_base: Base GPIO number for this device. For the first active device
  336. * this will be 0; the numbering for others will follow sequentially so that
  337. * @gpio_base for device 1 will equal the number of GPIOs in device 0.
  338. * @name: Array of pointers to the name for each GPIO in this bank. The
  339. * value of the pointer will be NULL if the GPIO has not been claimed.
  340. */
  341. struct gpio_dev_priv {
  342. const char *bank_name;
  343. unsigned gpio_count;
  344. unsigned gpio_base;
  345. char **name;
  346. };
  347. /* Access the GPIO operations for a device */
  348. #define gpio_get_ops(dev) ((struct dm_gpio_ops *)(dev)->driver->ops)
  349. /**
  350. * gpio_get_bank_info - Return information about a GPIO bank/device
  351. *
  352. * This looks up a device and returns both its GPIO base name and the number
  353. * of GPIOs it controls.
  354. *
  355. * @dev: Device to look up
  356. * @offset_count: Returns number of GPIOs within this bank
  357. * @return bank name of this device
  358. */
  359. const char *gpio_get_bank_info(struct udevice *dev, int *offset_count);
  360. /**
  361. * dm_gpio_lookup_name() - Look up a named GPIO and return its description
  362. *
  363. * The name of a GPIO is typically its bank name followed by a number from 0.
  364. * For example A0 is the first GPIO in bank A. Each bank is a separate driver
  365. * model device.
  366. *
  367. * @name: Name to look up
  368. * @desc: Returns description, on success
  369. * @return 0 if OK, -ve on error
  370. */
  371. int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc);
  372. /**
  373. * gpio_hog_lookup_name() - Look up a named GPIO and return the gpio descr.
  374. *
  375. * @name: Name to look up
  376. * @desc: Returns GPIO description, on success, else NULL
  377. * @return: Returns 0 if OK, else -ENODEV
  378. */
  379. int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc);
  380. /**
  381. * gpio_hog_probe_all() - probe all gpio devices with
  382. * gpio-hog subnodes.
  383. *
  384. * @return: Returns return value from device_probe()
  385. */
  386. int gpio_hog_probe_all(void);
  387. /**
  388. * gpio_lookup_name - Look up a GPIO name and return its details
  389. *
  390. * This is used to convert a named GPIO into a device, offset and GPIO
  391. * number.
  392. *
  393. * @name: GPIO name to look up
  394. * @devp: Returns pointer to device which contains this GPIO
  395. * @offsetp: Returns the offset number within this device
  396. * @gpiop: Returns the absolute GPIO number, numbered from 0
  397. */
  398. int gpio_lookup_name(const char *name, struct udevice **devp,
  399. unsigned int *offsetp, unsigned int *gpiop);
  400. /**
  401. * gpio_get_values_as_int() - Turn the values of a list of GPIOs into an int
  402. *
  403. * This puts the value of the first GPIO into bit 0, the second into bit 1,
  404. * etc. then returns the resulting integer.
  405. *
  406. * @gpio_list: List of GPIOs to collect
  407. * @return resulting integer value, or -ve on error
  408. */
  409. int gpio_get_values_as_int(const int *gpio_list);
  410. /**
  411. * dm_gpio_get_values_as_int() - Turn the values of a list of GPIOs into an int
  412. *
  413. * This puts the value of the first GPIO into bit 0, the second into bit 1,
  414. * etc. then returns the resulting integer.
  415. *
  416. * @desc_list: List of GPIOs to collect
  417. * @count: Number of GPIOs
  418. * @return resulting integer value, or -ve on error
  419. */
  420. int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count);
  421. /**
  422. * gpio_claim_vector() - claim a number of GPIOs for input
  423. *
  424. * @gpio_num_array: array of gpios to claim, terminated by -1
  425. * @fmt: format string for GPIO names, e.g. "board_id%d"
  426. * @return 0 if OK, -ve on error
  427. */
  428. int gpio_claim_vector(const int *gpio_num_array, const char *fmt);
  429. /**
  430. * gpio_request_by_name() - Locate and request a GPIO by name
  431. *
  432. * This operates by looking up the given list name in the device (device
  433. * tree property) and requesting the GPIO for use. The property must exist
  434. * in @dev's node.
  435. *
  436. * Use @flags to specify whether the GPIO should be an input or output. In
  437. * principle this can also come from the device tree binding but most
  438. * bindings don't provide this information. Specifically, when the GPIO uclass
  439. * calls the xlate() method, it can return default flags, which are then
  440. * ORed with this @flags.
  441. *
  442. * If we find that requesting the GPIO is not always needed we could add a
  443. * new function or a new GPIOD_NO_REQUEST flag.
  444. *
  445. * At present driver model has no reference counting so if one device
  446. * requests a GPIO which subsequently is unbound, the @desc->dev pointer
  447. * will be invalid. However this will only happen if the GPIO device is
  448. * unbound, not if it is removed, so this seems like a reasonable limitation
  449. * for now. There is no real use case for unbinding drivers in normal
  450. * operation.
  451. *
  452. * The device tree binding is doc/device-tree-bindings/gpio/gpio.txt in
  453. * generate terms and each specific device may add additional details in
  454. * a binding file in the same directory.
  455. *
  456. * @dev: Device requesting the GPIO
  457. * @list_name: Name of GPIO list (e.g. "board-id-gpios")
  458. * @index: Index number of the GPIO in that list use request (0=first)
  459. * @desc: Returns GPIO description information. If there is no such
  460. * GPIO, @desc->dev will be NULL.
  461. * @flags: Indicates the GPIO input/output settings (GPIOD_...)
  462. * @return 0 if OK, -ENOENT if the GPIO does not exist, -EINVAL if there is
  463. * something wrong with the list, or other -ve for another error (e.g.
  464. * -EBUSY if a GPIO was already requested)
  465. */
  466. int gpio_request_by_name(struct udevice *dev, const char *list_name,
  467. int index, struct gpio_desc *desc, int flags);
  468. /**
  469. * gpio_request_list_by_name() - Request a list of GPIOs
  470. *
  471. * Reads all the GPIOs from a list and requests them. See
  472. * gpio_request_by_name() for additional details. Lists should not be
  473. * misused to hold unrelated or optional GPIOs. They should only be used
  474. * for things like parallel data lines. A zero phandle terminates the list
  475. * the list.
  476. *
  477. * This function will either succeed, and request all GPIOs in the list, or
  478. * fail and request none (it will free already-requested GPIOs in case of
  479. * an error part-way through).
  480. *
  481. * @dev: Device requesting the GPIO
  482. * @list_name: Name of GPIO list (e.g. "board-id-gpios")
  483. * @desc_list: Returns a list of GPIO description information
  484. * @max_count: Maximum number of GPIOs to return (@desc_list must be at least
  485. * this big)
  486. * @flags: Indicates the GPIO input/output settings (GPIOD_...)
  487. * @return number of GPIOs requested, or -ve on error
  488. */
  489. int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
  490. struct gpio_desc *desc_list, int max_count,
  491. int flags);
  492. /**
  493. * dm_gpio_request() - manually request a GPIO
  494. *
  495. * Note: This function should only be used for testing / debugging. Instead.
  496. * use gpio_request_by_name() to pull GPIOs from the device tree.
  497. *
  498. * @desc: GPIO description of GPIO to request (see dm_gpio_lookup_name())
  499. * @label: Label to attach to the GPIO while claimed
  500. * @return 0 if OK, -ve on error
  501. */
  502. int dm_gpio_request(struct gpio_desc *desc, const char *label);
  503. /**
  504. * gpio_get_list_count() - Returns the number of GPIOs in a list
  505. *
  506. * Counts the GPIOs in a list. See gpio_request_by_name() for additional
  507. * details.
  508. *
  509. * @dev: Device requesting the GPIO
  510. * @list_name: Name of GPIO list (e.g. "board-id-gpios")
  511. * @return number of GPIOs (0 for an empty property) or -ENOENT if the list
  512. * does not exist
  513. */
  514. int gpio_get_list_count(struct udevice *dev, const char *list_name);
  515. /**
  516. * gpio_request_by_name_nodev() - request GPIOs without a device
  517. *
  518. * This is a version of gpio_request_list_by_name() that does not use a
  519. * device. Avoid it unless the caller is not yet using driver model
  520. */
  521. int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
  522. struct gpio_desc *desc, int flags);
  523. /**
  524. * gpio_request_list_by_name_nodev() - request GPIOs without a device
  525. *
  526. * This is a version of gpio_request_list_by_name() that does not use a
  527. * device. Avoid it unless the caller is not yet using driver model
  528. */
  529. int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
  530. struct gpio_desc *desc_list, int max_count,
  531. int flags);
  532. /**
  533. * gpio_dev_request_index() - request single GPIO from gpio device
  534. *
  535. * @dev: GPIO device
  536. * @nodename: Name of node for which gpio gets requested, used
  537. * for the gpio label name
  538. * @list_name: Name of GPIO list (e.g. "board-id-gpios")
  539. * @index: Index number of the GPIO in that list use request (0=first)
  540. * @flags: GPIOD_* flags
  541. * @dtflags: GPIO flags read from DT defined see GPIOD_*
  542. * @desc: returns GPIO descriptor filled from this function
  543. * @return: return value from gpio_request_tail()
  544. */
  545. int gpio_dev_request_index(struct udevice *dev, const char *nodename,
  546. char *list_name, int index, int flags,
  547. int dtflags, struct gpio_desc *desc);
  548. /**
  549. * dm_gpio_free() - Free a single GPIO
  550. *
  551. * This frees a single GPIOs previously returned from gpio_request_by_name().
  552. *
  553. * @dev: Device which requested the GPIO
  554. * @desc: GPIO to free
  555. * @return 0 if OK, -ve on error
  556. */
  557. int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc);
  558. /**
  559. * gpio_free_list() - Free a list of GPIOs
  560. *
  561. * This frees a list of GPIOs previously returned from
  562. * gpio_request_list_by_name().
  563. *
  564. * @dev: Device which requested the GPIOs
  565. * @desc: List of GPIOs to free
  566. * @count: Number of GPIOs in the list
  567. * @return 0 if OK, -ve on error
  568. */
  569. int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count);
  570. /**
  571. * gpio_free_list_nodev() - free GPIOs without a device
  572. *
  573. * This is a version of gpio_free_list() that does not use a
  574. * device. Avoid it unless the caller is not yet using driver model
  575. */
  576. int gpio_free_list_nodev(struct gpio_desc *desc, int count);
  577. /**
  578. * dm_gpio_get_value() - Get the value of a GPIO
  579. *
  580. * This is the driver model version of the existing gpio_get_value() function
  581. * and should be used instead of that.
  582. *
  583. * For now, these functions have a dm_ prefix since they conflict with
  584. * existing names.
  585. *
  586. * @desc: GPIO description containing device, offset and flags,
  587. * previously returned by gpio_request_by_name()
  588. * @return GPIO value (0 for inactive, 1 for active) or -ve on error
  589. */
  590. int dm_gpio_get_value(const struct gpio_desc *desc);
  591. int dm_gpio_set_value(const struct gpio_desc *desc, int value);
  592. /**
  593. * dm_gpio_set_dir() - Set the direction for a GPIO
  594. *
  595. * This sets up the direction according to the GPIO flags: desc->flags.
  596. *
  597. * @desc: GPIO description containing device, offset and flags,
  598. * previously returned by gpio_request_by_name()
  599. * @return 0 if OK, -ve on error
  600. */
  601. int dm_gpio_set_dir(struct gpio_desc *desc);
  602. /**
  603. * dm_gpio_set_dir_flags() - Set direction using description and added flags
  604. *
  605. * This sets up the direction according to the provided flags and the GPIO
  606. * description (desc->flags) which include direction information.
  607. * Note that desc->flags is updated by this function.
  608. *
  609. * @desc: GPIO description containing device, offset and flags,
  610. * previously returned by gpio_request_by_name()
  611. * @flags: New flags to use
  612. * @return 0 if OK, -ve on error, in which case desc->flags is not updated
  613. */
  614. int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags);
  615. /**
  616. * dm_gpio_get_dir_flags() - Get direction flags
  617. *
  618. * read the current direction flags
  619. *
  620. * @desc: GPIO description containing device, offset and flags,
  621. * previously returned by gpio_request_by_name()
  622. * @flags: place to put the used flags
  623. * @return 0 if OK, -ve on error, in which case desc->flags is not updated
  624. */
  625. int dm_gpio_get_dir_flags(struct gpio_desc *desc, ulong *flags);
  626. /**
  627. * gpio_get_number() - Get the global GPIO number of a GPIO
  628. *
  629. * This should only be used for debugging or interest. It returns the number
  630. * that should be used for gpio_get_value() etc. to access this GPIO.
  631. *
  632. * @desc: GPIO description containing device, offset and flags,
  633. * previously returned by gpio_request_by_name()
  634. * @return GPIO number, or -ve if not found
  635. */
  636. int gpio_get_number(const struct gpio_desc *desc);
  637. /**
  638. * gpio_get_acpi() - Get the ACPI pin for a GPIO
  639. *
  640. * This converts a GPIO to an ACPI pin number for adding to the ACPI
  641. * tables. If the GPIO is invalid, the pin_count and pins[0] are set to 0
  642. *
  643. * @desc: GPIO description to convert
  644. * @gpio: Output ACPI GPIO information
  645. * @return ACPI pin number or -ve on error
  646. */
  647. int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio);
  648. /**
  649. * devm_gpiod_get_index - Resource-managed gpiod_get()
  650. * @dev: GPIO consumer
  651. * @con_id: function within the GPIO consumer
  652. * @index: index of the GPIO to obtain in the consumer
  653. * @flags: optional GPIO initialization flags
  654. *
  655. * Managed gpiod_get(). GPIO descriptors returned from this function are
  656. * automatically disposed on device unbind.
  657. * Return the GPIO descriptor corresponding to the function con_id of device
  658. * dev, -ENOENT if no GPIO has been assigned to the requested function, or
  659. * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
  660. */
  661. struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
  662. unsigned int index, int flags);
  663. #define devm_gpiod_get(dev, id, flags) devm_gpiod_get_index(dev, id, 0, flags)
  664. /**
  665. * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
  666. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  667. * @con_id: function within the GPIO consumer
  668. * @index: index of the GPIO to obtain in the consumer
  669. * @flags: optional GPIO initialization flags
  670. *
  671. * This is equivalent to devm_gpiod_get(), except that when no GPIO was
  672. * assigned to the requested function it will return NULL. This is convenient
  673. * for drivers that need to handle optional GPIOs.
  674. */
  675. struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
  676. const char *id,
  677. unsigned int index,
  678. int flags);
  679. #define devm_gpiod_get_optional(dev, id, flags) \
  680. devm_gpiod_get_index_optional(dev, id, 0, flags)
  681. /**
  682. * devm_gpiod_put - Resource-managed gpiod_put()
  683. * @dev: GPIO consumer
  684. * @desc: GPIO descriptor to dispose of
  685. *
  686. * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
  687. * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
  688. * will be disposed of by the resource management code.
  689. */
  690. void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc);
  691. #endif /* _ASM_GENERIC_GPIO_H_ */