gpio.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * Copyright (c) 2011, NVIDIA Corp. All rights reserved.
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #ifndef _ASM_GENERIC_GPIO_H_
  7. #define _ASM_GENERIC_GPIO_H_
  8. /*
  9. * Generic GPIO API for U-Boot
  10. *
  11. * GPIOs are numbered from 0 to GPIO_COUNT-1 which value is defined
  12. * by the SOC/architecture.
  13. *
  14. * Each GPIO can be an input or output. If an input then its value can
  15. * be read as 0 or 1. If an output then its value can be set to 0 or 1.
  16. * If you try to write an input then the value is undefined. If you try
  17. * to read an output, barring something very unusual, you will get
  18. * back the value of the output that you previously set.
  19. *
  20. * In some cases the operation may fail, for example if the GPIO number
  21. * is out of range, or the GPIO is not available because its pin is
  22. * being used by another function. In that case, functions may return
  23. * an error value of -1.
  24. */
  25. /**
  26. * Request a GPIO. This should be called before any of the other functions
  27. * are used on this GPIO.
  28. *
  29. * Note: With driver model, the label is allocated so there is no need for
  30. * the caller to preserve it.
  31. *
  32. * @param gp GPIO number
  33. * @param label User label for this GPIO
  34. * @return 0 if ok, -1 on error
  35. */
  36. int gpio_request(unsigned gpio, const char *label);
  37. /**
  38. * Stop using the GPIO. This function should not alter pin configuration.
  39. *
  40. * @param gpio GPIO number
  41. * @return 0 if ok, -1 on error
  42. */
  43. int gpio_free(unsigned gpio);
  44. /**
  45. * Make a GPIO an input.
  46. *
  47. * @param gpio GPIO number
  48. * @return 0 if ok, -1 on error
  49. */
  50. int gpio_direction_input(unsigned gpio);
  51. /**
  52. * Make a GPIO an output, and set its value.
  53. *
  54. * @param gpio GPIO number
  55. * @param value GPIO value (0 for low or 1 for high)
  56. * @return 0 if ok, -1 on error
  57. */
  58. int gpio_direction_output(unsigned gpio, int value);
  59. /**
  60. * Get a GPIO's value. This will work whether the GPIO is an input
  61. * or an output.
  62. *
  63. * @param gpio GPIO number
  64. * @return 0 if low, 1 if high, -1 on error
  65. */
  66. int gpio_get_value(unsigned gpio);
  67. /**
  68. * Set an output GPIO's value. The GPIO must already be an output or
  69. * this function may have no effect.
  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_set_value(unsigned gpio, int value);
  76. /* State of a GPIO, as reported by get_function() */
  77. enum gpio_func_t {
  78. GPIOF_INPUT = 0,
  79. GPIOF_OUTPUT,
  80. GPIOF_UNUSED, /* Not claimed */
  81. GPIOF_UNKNOWN, /* Not known */
  82. GPIOF_FUNC, /* Not used as a GPIO */
  83. GPIOF_COUNT,
  84. };
  85. struct udevice;
  86. /**
  87. * gpio_get_status() - get the current GPIO status as a string
  88. *
  89. * Obtain the current GPIO status as a string which can be presented to the
  90. * user. A typical string is:
  91. *
  92. * "b4: in: 1 [x] sdmmc_cd"
  93. *
  94. * which means this is GPIO bank b, offset 4, currently set to input, current
  95. * value 1, [x] means that it is requested and the owner is 'sdmmc_cd'
  96. *
  97. * @dev: Device to check
  98. * @offset: Offset of device GPIO to check
  99. * @buf: Place to put string
  100. * @buffsize: Size of string including \0
  101. */
  102. int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize);
  103. /**
  104. * gpio_get_function() - get the current function for a GPIO pin
  105. *
  106. * Note this returns GPIOF_UNUSED if the GPIO is not requested.
  107. *
  108. * @dev: Device to check
  109. * @offset: Offset of device GPIO to check
  110. * @namep: If non-NULL, this is set to the nane given when the GPIO
  111. * was requested, or -1 if it has not been requested
  112. * @return -ENODATA if the driver returned an unknown function,
  113. * -ENODEV if the device is not active, -EINVAL if the offset is invalid.
  114. * GPIOF_UNUSED if the GPIO has not been requested. Otherwise returns the
  115. * function from enum gpio_func_t.
  116. */
  117. int gpio_get_function(struct udevice *dev, int offset, const char **namep);
  118. /**
  119. * gpio_get_raw_function() - get the current raw function for a GPIO pin
  120. *
  121. * Note this does not return GPIOF_UNUSED - it will always return the GPIO
  122. * driver's view of a pin function, even if it is not correctly set up.
  123. *
  124. * @dev: Device to check
  125. * @offset: Offset of device GPIO to check
  126. * @namep: If non-NULL, this is set to the nane given when the GPIO
  127. * was requested, or -1 if it has not been requested
  128. * @return -ENODATA if the driver returned an unknown function,
  129. * -ENODEV if the device is not active, -EINVAL if the offset is invalid.
  130. * Otherwise returns the function from enum gpio_func_t.
  131. */
  132. int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep);
  133. /**
  134. * gpio_requestf() - request a GPIO using a format string for the owner
  135. *
  136. * This is a helper function for gpio_request(). It allows you to provide
  137. * a printf()-format string for the GPIO owner. It calls gpio_request() with
  138. * the string that is created
  139. */
  140. int gpio_requestf(unsigned gpio, const char *fmt, ...)
  141. __attribute__ ((format (__printf__, 2, 3)));
  142. /**
  143. * struct struct dm_gpio_ops - Driver model GPIO operations
  144. *
  145. * Refer to functions above for description. These function largely copy
  146. * the old API.
  147. *
  148. * This is trying to be close to Linux GPIO API. Once the U-Boot uses the
  149. * new DM GPIO API, this should be really easy to flip over to the Linux
  150. * GPIO API-alike interface.
  151. *
  152. * Also it would be useful to standardise additional functions like
  153. * pullup, slew rate and drive strength.
  154. *
  155. * gpio_request)( and gpio_free() are optional - if NULL then they will
  156. * not be called.
  157. *
  158. * Note that @offset is the offset from the base GPIO of the device. So
  159. * offset 0 is the device's first GPIO and offset o-1 is the last GPIO,
  160. * where o is the number of GPIO lines controlled by the device. A device
  161. * is typically used to control a single bank of GPIOs. Within complex
  162. * SoCs there may be many banks and therefore many devices all referring
  163. * to the different IO addresses within the SoC.
  164. *
  165. * The uclass combines all GPIO devices together to provide a consistent
  166. * numbering from 0 to n-1, where n is the number of GPIOs in total across
  167. * all devices. Be careful not to confuse offset with gpio in the parameters.
  168. */
  169. struct dm_gpio_ops {
  170. int (*request)(struct udevice *dev, unsigned offset, const char *label);
  171. int (*free)(struct udevice *dev, unsigned offset);
  172. int (*direction_input)(struct udevice *dev, unsigned offset);
  173. int (*direction_output)(struct udevice *dev, unsigned offset,
  174. int value);
  175. int (*get_value)(struct udevice *dev, unsigned offset);
  176. int (*set_value)(struct udevice *dev, unsigned offset, int value);
  177. /**
  178. * get_function() Get the GPIO function
  179. *
  180. * @dev: Device to check
  181. * @offset: GPIO offset within that device
  182. * @return current function - GPIOF_...
  183. */
  184. int (*get_function)(struct udevice *dev, unsigned offset);
  185. };
  186. /**
  187. * struct gpio_dev_priv - information about a device used by the uclass
  188. *
  189. * The uclass combines all active GPIO devices into a unified numbering
  190. * scheme. To do this it maintains some private information about each
  191. * device.
  192. *
  193. * To implement driver model support in your GPIO driver, add a probe
  194. * handler, and set @gpio_count and @bank_name correctly in that handler.
  195. * This tells the uclass the name of the GPIO bank and the number of GPIOs
  196. * it contains.
  197. *
  198. * @bank_name: Name of the GPIO device (e.g 'a' means GPIOs will be called
  199. * 'A0', 'A1', etc.
  200. * @gpio_count: Number of GPIOs in this device
  201. * @gpio_base: Base GPIO number for this device. For the first active device
  202. * this will be 0; the numbering for others will follow sequentially so that
  203. * @gpio_base for device 1 will equal the number of GPIOs in device 0.
  204. * @name: Array of pointers to the name for each GPIO in this bank. The
  205. * value of the pointer will be NULL if the GPIO has not been claimed.
  206. */
  207. struct gpio_dev_priv {
  208. const char *bank_name;
  209. unsigned gpio_count;
  210. unsigned gpio_base;
  211. char **name;
  212. };
  213. /* Access the GPIO operations for a device */
  214. #define gpio_get_ops(dev) ((struct dm_gpio_ops *)(dev)->driver->ops)
  215. /**
  216. * gpio_get_bank_info - Return information about a GPIO bank/device
  217. *
  218. * This looks up a device and returns both its GPIO base name and the number
  219. * of GPIOs it controls.
  220. *
  221. * @dev: Device to look up
  222. * @offset_count: Returns number of GPIOs within this bank
  223. * @return bank name of this device
  224. */
  225. const char *gpio_get_bank_info(struct udevice *dev, int *offset_count);
  226. /**
  227. * gpio_lookup_name - Look up a GPIO name and return its details
  228. *
  229. * This is used to convert a named GPIO into a device, offset and GPIO
  230. * number.
  231. *
  232. * @name: GPIO name to look up
  233. * @devp: Returns pointer to device which contains this GPIO
  234. * @offsetp: Returns the offset number within this device
  235. * @gpiop: Returns the absolute GPIO number, numbered from 0
  236. */
  237. int gpio_lookup_name(const char *name, struct udevice **devp,
  238. unsigned int *offsetp, unsigned int *gpiop);
  239. /**
  240. * get_gpios() - Turn the values of a list of GPIOs into an integer
  241. *
  242. * This puts the value of the first GPIO into bit 0, the second into bit 1,
  243. * etc. then returns the resulting integer.
  244. *
  245. * @gpio_list: List of GPIOs to collect
  246. * @return resulting integer value
  247. */
  248. unsigned gpio_get_values_as_int(const int *gpio_list);
  249. #endif /* _ASM_GENERIC_GPIO_H_ */