gpio.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_state() */
  74. enum {
  75. GPIOF_INPUT = 0,
  76. GPIOF_OUTPUT,
  77. GPIOF_UNKNOWN,
  78. };
  79. struct device;
  80. /**
  81. * struct struct dm_gpio_ops - Driver model GPIO operations
  82. *
  83. * Refer to functions above for description. These function largely copy
  84. * the old API.
  85. *
  86. * This is trying to be close to Linux GPIO API. Once the U-Boot uses the
  87. * new DM GPIO API, this should be really easy to flip over to the Linux
  88. * GPIO API-alike interface.
  89. *
  90. * Akso it would be useful to standardise additional functions like
  91. * pullup, slew rate and drive strength.
  92. *
  93. * gpio_request)( and gpio_free() are optional - if NULL then they will
  94. * not be called.
  95. *
  96. * Note that @offset is the offset from the base GPIO of the device. So
  97. * offset 0 is the device's first GPIO and offset o-1 is the last GPIO,
  98. * where o is the number of GPIO lines controlled by the device. A device
  99. * is typically used to control a single bank of GPIOs. Within complex
  100. * SoCs there may be many banks and therefore many devices all referring
  101. * to the different IO addresses within the SoC.
  102. *
  103. * The uclass combines all GPIO devices togther to provide a consistent
  104. * numbering from 0 to n-1, where n is the number of GPIOs in total across
  105. * all devices. Be careful not to confuse offset with gpio in the parameters.
  106. */
  107. struct dm_gpio_ops {
  108. int (*request)(struct device *dev, unsigned offset, const char *label);
  109. int (*free)(struct device *dev, unsigned offset);
  110. int (*direction_input)(struct device *dev, unsigned offset);
  111. int (*direction_output)(struct device *dev, unsigned offset,
  112. int value);
  113. int (*get_value)(struct device *dev, unsigned offset);
  114. int (*set_value)(struct device *dev, unsigned offset, int value);
  115. int (*get_function)(struct device *dev, unsigned offset);
  116. int (*get_state)(struct device *dev, unsigned offset, char *state,
  117. int maxlen);
  118. };
  119. /**
  120. * struct gpio_dev_priv - information about a device used by the uclass
  121. *
  122. * The uclass combines all active GPIO devices into a unified numbering
  123. * scheme. To do this it maintains some private information aobut each
  124. * device.
  125. *
  126. * To implement driver model support in your GPIO driver, add a probe
  127. * handler, and set @gpio_count and @bank_name correctly in that handler.
  128. * This tells the uclass the name of the GPIO bank and the number of GPIOs
  129. * it contains.
  130. *
  131. * @bank_name: Name of the GPIO device (e.g 'a' means GPIOs will be called
  132. * 'A0', 'A1', etc.
  133. * @gpio_count: Number of GPIOs in this device
  134. * @gpio_base: Base GPIO number for this device. For the first active device
  135. * this will be 0; the numbering for others will follow sequentially so that
  136. * @gpio_base for device 1 will equal the number of GPIOs in device 0.
  137. */
  138. struct gpio_dev_priv {
  139. const char *bank_name;
  140. unsigned gpio_count;
  141. unsigned gpio_base;
  142. };
  143. /* Access the GPIO operations for a device */
  144. #define gpio_get_ops(dev) ((struct dm_gpio_ops *)(dev)->driver->ops)
  145. /**
  146. * gpio_get_bank_info - Return information about a GPIO bank/device
  147. *
  148. * This looks up a device and returns both its GPIO base name and the number
  149. * of GPIOs it controls.
  150. *
  151. * @dev: Device to look up
  152. * @offset_count: Returns number of GPIOs within this bank
  153. * @return bank name of this device
  154. */
  155. const char *gpio_get_bank_info(struct device *dev, int *offset_count);
  156. /**
  157. * gpio_lookup_name - Look up a GPIO name and return its details
  158. *
  159. * This is used to convert a named GPIO into a device, offset and GPIO
  160. * number.
  161. *
  162. * @name: GPIO name to look up
  163. * @devp: Returns pointer to device which contains this GPIO
  164. * @offsetp: Returns the offset number within this device
  165. * @gpiop: Returns the absolute GPIO number, numbered from 0
  166. */
  167. int gpio_lookup_name(const char *name, struct device **devp,
  168. unsigned int *offsetp, unsigned int *gpiop);
  169. #endif /* _ASM_GENERIC_GPIO_H_ */