gpio.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * @param gp GPIO number
  30. * @param label User label for this GPIO
  31. * @return 0 if ok, -1 on error
  32. */
  33. int gpio_request(unsigned gpio, const char *label);
  34. /**
  35. * Stop using the GPIO. This function should not alter pin configuration.
  36. *
  37. * @param gpio GPIO number
  38. * @return 0 if ok, -1 on error
  39. */
  40. int gpio_free(unsigned gpio);
  41. /**
  42. * Make a GPIO an input.
  43. *
  44. * @param gpio GPIO number
  45. * @return 0 if ok, -1 on error
  46. */
  47. int gpio_direction_input(unsigned gpio);
  48. /**
  49. * Make a GPIO an output, and set its value.
  50. *
  51. * @param gpio GPIO number
  52. * @param value GPIO value (0 for low or 1 for high)
  53. * @return 0 if ok, -1 on error
  54. */
  55. int gpio_direction_output(unsigned gpio, int value);
  56. /**
  57. * Get a GPIO's value. This will work whether the GPIO is an input
  58. * or an output.
  59. *
  60. * @param gpio GPIO number
  61. * @return 0 if low, 1 if high, -1 on error
  62. */
  63. int gpio_get_value(unsigned gpio);
  64. /**
  65. * Set an output GPIO's value. The GPIO must already be an output or
  66. * this function may have no effect.
  67. *
  68. * @param gpio GPIO number
  69. * @param value GPIO value (0 for low or 1 for high)
  70. * @return 0 if ok, -1 on error
  71. */
  72. int gpio_set_value(unsigned gpio, int value);
  73. /* State of a GPIO, as reported by get_function() */
  74. enum {
  75. GPIOF_INPUT = 0,
  76. GPIOF_OUTPUT,
  77. GPIOF_UNUSED, /* Not claimed */
  78. GPIOF_UNKNOWN, /* Not known */
  79. GPIOF_FUNC, /* Not used as a GPIO */
  80. GPIOF_COUNT,
  81. };
  82. struct udevice;
  83. /**
  84. * struct struct dm_gpio_ops - Driver model GPIO operations
  85. *
  86. * Refer to functions above for description. These function largely copy
  87. * the old API.
  88. *
  89. * This is trying to be close to Linux GPIO API. Once the U-Boot uses the
  90. * new DM GPIO API, this should be really easy to flip over to the Linux
  91. * GPIO API-alike interface.
  92. *
  93. * Also it would be useful to standardise additional functions like
  94. * pullup, slew rate and drive strength.
  95. *
  96. * gpio_request)( and gpio_free() are optional - if NULL then they will
  97. * not be called.
  98. *
  99. * Note that @offset is the offset from the base GPIO of the device. So
  100. * offset 0 is the device's first GPIO and offset o-1 is the last GPIO,
  101. * where o is the number of GPIO lines controlled by the device. A device
  102. * is typically used to control a single bank of GPIOs. Within complex
  103. * SoCs there may be many banks and therefore many devices all referring
  104. * to the different IO addresses within the SoC.
  105. *
  106. * The uclass combines all GPIO devices together to provide a consistent
  107. * numbering from 0 to n-1, where n is the number of GPIOs in total across
  108. * all devices. Be careful not to confuse offset with gpio in the parameters.
  109. */
  110. struct dm_gpio_ops {
  111. int (*request)(struct udevice *dev, unsigned offset, const char *label);
  112. int (*free)(struct udevice *dev, unsigned offset);
  113. int (*direction_input)(struct udevice *dev, unsigned offset);
  114. int (*direction_output)(struct udevice *dev, unsigned offset,
  115. int value);
  116. int (*get_value)(struct udevice *dev, unsigned offset);
  117. int (*set_value)(struct udevice *dev, unsigned offset, int value);
  118. /**
  119. * get_function() Get the GPIO function
  120. *
  121. * @dev: Device to check
  122. * @offset: GPIO offset within that device
  123. * @return current function - GPIOF_...
  124. */
  125. int (*get_function)(struct udevice *dev, unsigned offset);
  126. int (*get_state)(struct udevice *dev, unsigned offset, char *state,
  127. int maxlen);
  128. };
  129. /**
  130. * struct gpio_dev_priv - information about a device used by the uclass
  131. *
  132. * The uclass combines all active GPIO devices into a unified numbering
  133. * scheme. To do this it maintains some private information about each
  134. * device.
  135. *
  136. * To implement driver model support in your GPIO driver, add a probe
  137. * handler, and set @gpio_count and @bank_name correctly in that handler.
  138. * This tells the uclass the name of the GPIO bank and the number of GPIOs
  139. * it contains.
  140. *
  141. * @bank_name: Name of the GPIO device (e.g 'a' means GPIOs will be called
  142. * 'A0', 'A1', etc.
  143. * @gpio_count: Number of GPIOs in this device
  144. * @gpio_base: Base GPIO number for this device. For the first active device
  145. * this will be 0; the numbering for others will follow sequentially so that
  146. * @gpio_base for device 1 will equal the number of GPIOs in device 0.
  147. */
  148. struct gpio_dev_priv {
  149. const char *bank_name;
  150. unsigned gpio_count;
  151. unsigned gpio_base;
  152. };
  153. /* Access the GPIO operations for a device */
  154. #define gpio_get_ops(dev) ((struct dm_gpio_ops *)(dev)->driver->ops)
  155. /**
  156. * gpio_get_bank_info - Return information about a GPIO bank/device
  157. *
  158. * This looks up a device and returns both its GPIO base name and the number
  159. * of GPIOs it controls.
  160. *
  161. * @dev: Device to look up
  162. * @offset_count: Returns number of GPIOs within this bank
  163. * @return bank name of this device
  164. */
  165. const char *gpio_get_bank_info(struct udevice *dev, int *offset_count);
  166. /**
  167. * gpio_lookup_name - Look up a GPIO name and return its details
  168. *
  169. * This is used to convert a named GPIO into a device, offset and GPIO
  170. * number.
  171. *
  172. * @name: GPIO name to look up
  173. * @devp: Returns pointer to device which contains this GPIO
  174. * @offsetp: Returns the offset number within this device
  175. * @gpiop: Returns the absolute GPIO number, numbered from 0
  176. */
  177. int gpio_lookup_name(const char *name, struct udevice **devp,
  178. unsigned int *offsetp, unsigned int *gpiop);
  179. #endif /* _ASM_GENERIC_GPIO_H_ */