gpio.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. struct gpio_desc {
  87. struct udevice *dev; /* Device, NULL for invalid GPIO */
  88. unsigned long flags;
  89. #define GPIOD_REQUESTED (1 << 0) /* Requested/claimed */
  90. #define GPIOD_IS_OUT (1 << 1) /* GPIO is an output */
  91. #define GPIOD_IS_IN (1 << 2) /* GPIO is an output */
  92. #define GPIOD_ACTIVE_LOW (1 << 3) /* value has active low */
  93. #define GPIOD_IS_OUT_ACTIVE (1 << 4) /* set output active */
  94. uint offset; /* GPIO offset within the device */
  95. /*
  96. * We could consider adding the GPIO label in here. Possibly we could
  97. * use this structure for internal GPIO information.
  98. */
  99. };
  100. /**
  101. * gpio_get_status() - get the current GPIO status as a string
  102. *
  103. * Obtain the current GPIO status as a string which can be presented to the
  104. * user. A typical string is:
  105. *
  106. * "b4: in: 1 [x] sdmmc_cd"
  107. *
  108. * which means this is GPIO bank b, offset 4, currently set to input, current
  109. * value 1, [x] means that it is requested and the owner is 'sdmmc_cd'
  110. *
  111. * @dev: Device to check
  112. * @offset: Offset of device GPIO to check
  113. * @buf: Place to put string
  114. * @buffsize: Size of string including \0
  115. */
  116. int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize);
  117. /**
  118. * gpio_get_function() - get the current function for a GPIO pin
  119. *
  120. * Note this returns GPIOF_UNUSED if the GPIO is not requested.
  121. *
  122. * @dev: Device to check
  123. * @offset: Offset of device GPIO to check
  124. * @namep: If non-NULL, this is set to the nane given when the GPIO
  125. * was requested, or -1 if it has not been requested
  126. * @return -ENODATA if the driver returned an unknown function,
  127. * -ENODEV if the device is not active, -EINVAL if the offset is invalid.
  128. * GPIOF_UNUSED if the GPIO has not been requested. Otherwise returns the
  129. * function from enum gpio_func_t.
  130. */
  131. int gpio_get_function(struct udevice *dev, int offset, const char **namep);
  132. /**
  133. * gpio_get_raw_function() - get the current raw function for a GPIO pin
  134. *
  135. * Note this does not return GPIOF_UNUSED - it will always return the GPIO
  136. * driver's view of a pin function, even if it is not correctly set up.
  137. *
  138. * @dev: Device to check
  139. * @offset: Offset of device GPIO to check
  140. * @namep: If non-NULL, this is set to the nane given when the GPIO
  141. * was requested, or -1 if it has not been requested
  142. * @return -ENODATA if the driver returned an unknown function,
  143. * -ENODEV if the device is not active, -EINVAL if the offset is invalid.
  144. * Otherwise returns the function from enum gpio_func_t.
  145. */
  146. int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep);
  147. /**
  148. * gpio_requestf() - request a GPIO using a format string for the owner
  149. *
  150. * This is a helper function for gpio_request(). It allows you to provide
  151. * a printf()-format string for the GPIO owner. It calls gpio_request() with
  152. * the string that is created
  153. */
  154. int gpio_requestf(unsigned gpio, const char *fmt, ...)
  155. __attribute__ ((format (__printf__, 2, 3)));
  156. /**
  157. * struct struct dm_gpio_ops - Driver model GPIO operations
  158. *
  159. * Refer to functions above for description. These function largely copy
  160. * the old API.
  161. *
  162. * This is trying to be close to Linux GPIO API. Once the U-Boot uses the
  163. * new DM GPIO API, this should be really easy to flip over to the Linux
  164. * GPIO API-alike interface.
  165. *
  166. * Also it would be useful to standardise additional functions like
  167. * pullup, slew rate and drive strength.
  168. *
  169. * gpio_request)( and gpio_free() are optional - if NULL then they will
  170. * not be called.
  171. *
  172. * Note that @offset is the offset from the base GPIO of the device. So
  173. * offset 0 is the device's first GPIO and offset o-1 is the last GPIO,
  174. * where o is the number of GPIO lines controlled by the device. A device
  175. * is typically used to control a single bank of GPIOs. Within complex
  176. * SoCs there may be many banks and therefore many devices all referring
  177. * to the different IO addresses within the SoC.
  178. *
  179. * The uclass combines all GPIO devices together to provide a consistent
  180. * numbering from 0 to n-1, where n is the number of GPIOs in total across
  181. * all devices. Be careful not to confuse offset with gpio in the parameters.
  182. */
  183. struct dm_gpio_ops {
  184. int (*request)(struct udevice *dev, unsigned offset, const char *label);
  185. int (*free)(struct udevice *dev, unsigned offset);
  186. int (*direction_input)(struct udevice *dev, unsigned offset);
  187. int (*direction_output)(struct udevice *dev, unsigned offset,
  188. int value);
  189. int (*get_value)(struct udevice *dev, unsigned offset);
  190. int (*set_value)(struct udevice *dev, unsigned offset, int value);
  191. /**
  192. * get_function() Get the GPIO function
  193. *
  194. * @dev: Device to check
  195. * @offset: GPIO offset within that device
  196. * @return current function - GPIOF_...
  197. */
  198. int (*get_function)(struct udevice *dev, unsigned offset);
  199. };
  200. /**
  201. * struct gpio_dev_priv - information about a device used by the uclass
  202. *
  203. * The uclass combines all active GPIO devices into a unified numbering
  204. * scheme. To do this it maintains some private information about each
  205. * device.
  206. *
  207. * To implement driver model support in your GPIO driver, add a probe
  208. * handler, and set @gpio_count and @bank_name correctly in that handler.
  209. * This tells the uclass the name of the GPIO bank and the number of GPIOs
  210. * it contains.
  211. *
  212. * @bank_name: Name of the GPIO device (e.g 'a' means GPIOs will be called
  213. * 'A0', 'A1', etc.
  214. * @gpio_count: Number of GPIOs in this device
  215. * @gpio_base: Base GPIO number for this device. For the first active device
  216. * this will be 0; the numbering for others will follow sequentially so that
  217. * @gpio_base for device 1 will equal the number of GPIOs in device 0.
  218. * @name: Array of pointers to the name for each GPIO in this bank. The
  219. * value of the pointer will be NULL if the GPIO has not been claimed.
  220. */
  221. struct gpio_dev_priv {
  222. const char *bank_name;
  223. unsigned gpio_count;
  224. unsigned gpio_base;
  225. char **name;
  226. };
  227. /* Access the GPIO operations for a device */
  228. #define gpio_get_ops(dev) ((struct dm_gpio_ops *)(dev)->driver->ops)
  229. /**
  230. * gpio_get_bank_info - Return information about a GPIO bank/device
  231. *
  232. * This looks up a device and returns both its GPIO base name and the number
  233. * of GPIOs it controls.
  234. *
  235. * @dev: Device to look up
  236. * @offset_count: Returns number of GPIOs within this bank
  237. * @return bank name of this device
  238. */
  239. const char *gpio_get_bank_info(struct udevice *dev, int *offset_count);
  240. /**
  241. * gpio_lookup_name - Look up a GPIO name and return its details
  242. *
  243. * This is used to convert a named GPIO into a device, offset and GPIO
  244. * number.
  245. *
  246. * @name: GPIO name to look up
  247. * @devp: Returns pointer to device which contains this GPIO
  248. * @offsetp: Returns the offset number within this device
  249. * @gpiop: Returns the absolute GPIO number, numbered from 0
  250. */
  251. int gpio_lookup_name(const char *name, struct udevice **devp,
  252. unsigned int *offsetp, unsigned int *gpiop);
  253. /**
  254. * get_gpios() - Turn the values of a list of GPIOs into an integer
  255. *
  256. * This puts the value of the first GPIO into bit 0, the second into bit 1,
  257. * etc. then returns the resulting integer.
  258. *
  259. * @gpio_list: List of GPIOs to collect
  260. * @return resulting integer value
  261. */
  262. unsigned gpio_get_values_as_int(const int *gpio_list);
  263. #endif /* _ASM_GENERIC_GPIO_H_ */