hw_gpio.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright 2012-15 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: AMD
  23. *
  24. */
  25. #include "dm_services.h"
  26. #include "include/gpio_types.h"
  27. #include "hw_gpio.h"
  28. #include "reg_helper.h"
  29. #include "gpio_regs.h"
  30. #undef FN
  31. #define FN(reg_name, field_name) \
  32. gpio->regs->field_name ## _shift, gpio->regs->field_name ## _mask
  33. #define CTX \
  34. gpio->base.ctx
  35. #define REG(reg)\
  36. (gpio->regs->reg)
  37. static void store_registers(
  38. struct hw_gpio *gpio)
  39. {
  40. REG_GET(MASK_reg, MASK, &gpio->store.mask);
  41. REG_GET(A_reg, A, &gpio->store.a);
  42. REG_GET(EN_reg, EN, &gpio->store.en);
  43. /* TODO store GPIO_MUX_CONTROL if we ever use it */
  44. }
  45. static void restore_registers(
  46. struct hw_gpio *gpio)
  47. {
  48. REG_UPDATE(MASK_reg, MASK, gpio->store.mask);
  49. REG_UPDATE(A_reg, A, gpio->store.a);
  50. REG_UPDATE(EN_reg, EN, gpio->store.en);
  51. /* TODO restore GPIO_MUX_CONTROL if we ever use it */
  52. }
  53. bool dal_hw_gpio_open(
  54. struct hw_gpio_pin *ptr,
  55. enum gpio_mode mode)
  56. {
  57. struct hw_gpio *pin = FROM_HW_GPIO_PIN(ptr);
  58. store_registers(pin);
  59. ptr->opened = (dal_hw_gpio_config_mode(pin, mode) == GPIO_RESULT_OK);
  60. return ptr->opened;
  61. }
  62. enum gpio_result dal_hw_gpio_get_value(
  63. const struct hw_gpio_pin *ptr,
  64. uint32_t *value)
  65. {
  66. const struct hw_gpio *gpio = FROM_HW_GPIO_PIN(ptr);
  67. enum gpio_result result = GPIO_RESULT_OK;
  68. switch (ptr->mode) {
  69. case GPIO_MODE_INPUT:
  70. case GPIO_MODE_OUTPUT:
  71. case GPIO_MODE_HARDWARE:
  72. case GPIO_MODE_FAST_OUTPUT:
  73. REG_GET(Y_reg, Y, value);
  74. break;
  75. default:
  76. result = GPIO_RESULT_NON_SPECIFIC_ERROR;
  77. }
  78. return result;
  79. }
  80. enum gpio_result dal_hw_gpio_set_value(
  81. const struct hw_gpio_pin *ptr,
  82. uint32_t value)
  83. {
  84. struct hw_gpio *gpio = FROM_HW_GPIO_PIN(ptr);
  85. /* This is the public interface
  86. * where the input comes from client, not shifted yet
  87. * (because client does not know the shifts). */
  88. switch (ptr->mode) {
  89. case GPIO_MODE_OUTPUT:
  90. REG_UPDATE(A_reg, A, value);
  91. return GPIO_RESULT_OK;
  92. case GPIO_MODE_FAST_OUTPUT:
  93. /* We use (EN) to faster switch (used in DDC GPIO).
  94. * So (A) is grounded, output is driven by (EN = 0)
  95. * to pull the line down (output == 0) and (EN=1)
  96. * then output is tri-state */
  97. REG_UPDATE(EN_reg, EN, ~value);
  98. return GPIO_RESULT_OK;
  99. default:
  100. return GPIO_RESULT_NON_SPECIFIC_ERROR;
  101. }
  102. }
  103. enum gpio_result dal_hw_gpio_change_mode(
  104. struct hw_gpio_pin *ptr,
  105. enum gpio_mode mode)
  106. {
  107. struct hw_gpio *pin = FROM_HW_GPIO_PIN(ptr);
  108. return dal_hw_gpio_config_mode(pin, mode);
  109. }
  110. void dal_hw_gpio_close(
  111. struct hw_gpio_pin *ptr)
  112. {
  113. struct hw_gpio *pin = FROM_HW_GPIO_PIN(ptr);
  114. restore_registers(pin);
  115. ptr->mode = GPIO_MODE_UNKNOWN;
  116. ptr->opened = false;
  117. }
  118. enum gpio_result dal_hw_gpio_config_mode(
  119. struct hw_gpio *gpio,
  120. enum gpio_mode mode)
  121. {
  122. gpio->base.mode = mode;
  123. switch (mode) {
  124. case GPIO_MODE_INPUT:
  125. /* turn off output enable, act as input pin;
  126. * program the pin as GPIO, mask out signal driven by HW */
  127. REG_UPDATE(EN_reg, EN, 0);
  128. REG_UPDATE(MASK_reg, MASK, 1);
  129. return GPIO_RESULT_OK;
  130. case GPIO_MODE_OUTPUT:
  131. /* turn on output enable, act as output pin;
  132. * program the pin as GPIO, mask out signal driven by HW */
  133. REG_UPDATE(A_reg, A, 0);
  134. REG_UPDATE(MASK_reg, MASK, 1);
  135. return GPIO_RESULT_OK;
  136. case GPIO_MODE_FAST_OUTPUT:
  137. /* grounding the A register then use the EN register bit
  138. * will have faster effect on the rise time */
  139. REG_UPDATE(A_reg, A, 0);
  140. REG_UPDATE(MASK_reg, MASK, 1);
  141. return GPIO_RESULT_OK;
  142. case GPIO_MODE_HARDWARE:
  143. /* program the pin as tri-state, pin is driven by HW */
  144. REG_UPDATE(MASK_reg, MASK, 0);
  145. return GPIO_RESULT_OK;
  146. case GPIO_MODE_INTERRUPT:
  147. /* Interrupt mode supported only by HPD (IrqGpio) pins. */
  148. REG_UPDATE(MASK_reg, MASK, 0);
  149. return GPIO_RESULT_OK;
  150. default:
  151. return GPIO_RESULT_NON_SPECIFIC_ERROR;
  152. }
  153. }
  154. void dal_hw_gpio_construct(
  155. struct hw_gpio *pin,
  156. enum gpio_id id,
  157. uint32_t en,
  158. struct dc_context *ctx)
  159. {
  160. pin->base.ctx = ctx;
  161. pin->base.id = id;
  162. pin->base.en = en;
  163. pin->base.mode = GPIO_MODE_UNKNOWN;
  164. pin->base.opened = false;
  165. pin->store.mask = 0;
  166. pin->store.a = 0;
  167. pin->store.en = 0;
  168. pin->store.mux = 0;
  169. pin->mux_supported = false;
  170. }
  171. void dal_hw_gpio_destruct(
  172. struct hw_gpio *pin)
  173. {
  174. ASSERT(!pin->base.opened);
  175. }