gpio.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. * struct struct dm_gpio_ops - Driver model GPIO operations
  135. *
  136. * Refer to functions above for description. These function largely copy
  137. * the old API.
  138. *
  139. * This is trying to be close to Linux GPIO API. Once the U-Boot uses the
  140. * new DM GPIO API, this should be really easy to flip over to the Linux
  141. * GPIO API-alike interface.
  142. *
  143. * Also it would be useful to standardise additional functions like
  144. * pullup, slew rate and drive strength.
  145. *
  146. * gpio_request)( and gpio_free() are optional - if NULL then they will
  147. * not be called.
  148. *
  149. * Note that @offset is the offset from the base GPIO of the device. So
  150. * offset 0 is the device's first GPIO and offset o-1 is the last GPIO,
  151. * where o is the number of GPIO lines controlled by the device. A device
  152. * is typically used to control a single bank of GPIOs. Within complex
  153. * SoCs there may be many banks and therefore many devices all referring
  154. * to the different IO addresses within the SoC.
  155. *
  156. * The uclass combines all GPIO devices together to provide a consistent
  157. * numbering from 0 to n-1, where n is the number of GPIOs in total across
  158. * all devices. Be careful not to confuse offset with gpio in the parameters.
  159. */
  160. struct dm_gpio_ops {
  161. int (*request)(struct udevice *dev, unsigned offset, const char *label);
  162. int (*free)(struct udevice *dev, unsigned offset);
  163. int (*direction_input)(struct udevice *dev, unsigned offset);
  164. int (*direction_output)(struct udevice *dev, unsigned offset,
  165. int value);
  166. int (*get_value)(struct udevice *dev, unsigned offset);
  167. int (*set_value)(struct udevice *dev, unsigned offset, int value);
  168. /**
  169. * get_function() Get the GPIO function
  170. *
  171. * @dev: Device to check
  172. * @offset: GPIO offset within that device
  173. * @return current function - GPIOF_...
  174. */
  175. int (*get_function)(struct udevice *dev, unsigned offset);
  176. int (*get_state)(struct udevice *dev, unsigned offset, char *state,
  177. int maxlen);
  178. };
  179. /**
  180. * struct gpio_dev_priv - information about a device used by the uclass
  181. *
  182. * The uclass combines all active GPIO devices into a unified numbering
  183. * scheme. To do this it maintains some private information about each
  184. * device.
  185. *
  186. * To implement driver model support in your GPIO driver, add a probe
  187. * handler, and set @gpio_count and @bank_name correctly in that handler.
  188. * This tells the uclass the name of the GPIO bank and the number of GPIOs
  189. * it contains.
  190. *
  191. * @bank_name: Name of the GPIO device (e.g 'a' means GPIOs will be called
  192. * 'A0', 'A1', etc.
  193. * @gpio_count: Number of GPIOs in this device
  194. * @gpio_base: Base GPIO number for this device. For the first active device
  195. * this will be 0; the numbering for others will follow sequentially so that
  196. * @gpio_base for device 1 will equal the number of GPIOs in device 0.
  197. * @name: Array of pointers to the name for each GPIO in this bank. The
  198. * value of the pointer will be NULL if the GPIO has not been claimed.
  199. */
  200. struct gpio_dev_priv {
  201. const char *bank_name;
  202. unsigned gpio_count;
  203. unsigned gpio_base;
  204. char **name;
  205. };
  206. /* Access the GPIO operations for a device */
  207. #define gpio_get_ops(dev) ((struct dm_gpio_ops *)(dev)->driver->ops)
  208. /**
  209. * gpio_get_bank_info - Return information about a GPIO bank/device
  210. *
  211. * This looks up a device and returns both its GPIO base name and the number
  212. * of GPIOs it controls.
  213. *
  214. * @dev: Device to look up
  215. * @offset_count: Returns number of GPIOs within this bank
  216. * @return bank name of this device
  217. */
  218. const char *gpio_get_bank_info(struct udevice *dev, int *offset_count);
  219. /**
  220. * gpio_lookup_name - Look up a GPIO name and return its details
  221. *
  222. * This is used to convert a named GPIO into a device, offset and GPIO
  223. * number.
  224. *
  225. * @name: GPIO name to look up
  226. * @devp: Returns pointer to device which contains this GPIO
  227. * @offsetp: Returns the offset number within this device
  228. * @gpiop: Returns the absolute GPIO number, numbered from 0
  229. */
  230. int gpio_lookup_name(const char *name, struct udevice **devp,
  231. unsigned int *offsetp, unsigned int *gpiop);
  232. #endif /* _ASM_GENERIC_GPIO_H_ */