hw_hpd.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "hw_hpd.h"
  29. #include "reg_helper.h"
  30. #include "hpd_regs.h"
  31. #undef FN
  32. #define FN(reg_name, field_name) \
  33. hpd->shifts->field_name, hpd->masks->field_name
  34. #define CTX \
  35. hpd->base.base.ctx
  36. #define REG(reg)\
  37. (hpd->regs->reg)
  38. static bool dal_hw_hpd_construct(
  39. struct hw_hpd *pin,
  40. enum gpio_id id,
  41. uint32_t en,
  42. struct dc_context *ctx)
  43. {
  44. if (!dal_hw_gpio_construct(&pin->base, id, en, ctx))
  45. return false;
  46. return true;
  47. }
  48. static void dal_hw_hpd_destruct(
  49. struct hw_hpd *pin)
  50. {
  51. dal_hw_gpio_destruct(&pin->base);
  52. }
  53. static void destruct(
  54. struct hw_hpd *hpd)
  55. {
  56. dal_hw_hpd_destruct(hpd);
  57. }
  58. static void destroy(
  59. struct hw_gpio_pin **ptr)
  60. {
  61. struct hw_hpd *hpd = HW_HPD_FROM_BASE(*ptr);
  62. destruct(hpd);
  63. dm_free(hpd);
  64. *ptr = NULL;
  65. }
  66. static enum gpio_result get_value(
  67. const struct hw_gpio_pin *ptr,
  68. uint32_t *value)
  69. {
  70. struct hw_hpd *hpd = HW_HPD_FROM_BASE(ptr);
  71. uint32_t hpd_delayed = 0;
  72. /* in Interrupt mode we ask for SENSE bit */
  73. if (ptr->mode == GPIO_MODE_INTERRUPT) {
  74. REG_GET(int_status,
  75. DC_HPD_SENSE_DELAYED, &hpd_delayed);
  76. *value = hpd_delayed;
  77. return GPIO_RESULT_OK;
  78. }
  79. /* in any other modes, operate as normal GPIO */
  80. return dal_hw_gpio_get_value(ptr, value);
  81. }
  82. static enum gpio_result set_config(
  83. struct hw_gpio_pin *ptr,
  84. const struct gpio_config_data *config_data)
  85. {
  86. struct hw_hpd *hpd = HW_HPD_FROM_BASE(ptr);
  87. if (!config_data)
  88. return GPIO_RESULT_INVALID_DATA;
  89. REG_UPDATE_2(toggle_filt_cntl,
  90. DC_HPD_CONNECT_INT_DELAY, config_data->config.hpd.delay_on_connect / 10,
  91. DC_HPD_DISCONNECT_INT_DELAY, config_data->config.hpd.delay_on_disconnect / 10);
  92. return GPIO_RESULT_OK;
  93. }
  94. static const struct hw_gpio_pin_funcs funcs = {
  95. .destroy = destroy,
  96. .open = dal_hw_gpio_open,
  97. .get_value = get_value,
  98. .set_value = dal_hw_gpio_set_value,
  99. .set_config = set_config,
  100. .change_mode = dal_hw_gpio_change_mode,
  101. .close = dal_hw_gpio_close,
  102. };
  103. static bool construct(
  104. struct hw_hpd *hpd,
  105. enum gpio_id id,
  106. uint32_t en,
  107. struct dc_context *ctx)
  108. {
  109. if (id != GPIO_ID_HPD) {
  110. ASSERT_CRITICAL(false);
  111. return false;
  112. }
  113. if ((en < GPIO_HPD_MIN) || (en > GPIO_HPD_MAX)) {
  114. ASSERT_CRITICAL(false);
  115. return false;
  116. }
  117. if (!dal_hw_hpd_construct(hpd, id, en, ctx)) {
  118. ASSERT_CRITICAL(false);
  119. return false;
  120. }
  121. hpd->base.base.funcs = &funcs;
  122. return true;
  123. }
  124. struct hw_gpio_pin *dal_hw_hpd_create(
  125. struct dc_context *ctx,
  126. enum gpio_id id,
  127. uint32_t en)
  128. {
  129. struct hw_hpd *hpd = dm_alloc(sizeof(struct hw_hpd));
  130. if (!hpd) {
  131. ASSERT_CRITICAL(false);
  132. return NULL;
  133. }
  134. if (construct(hpd, id, en, ctx))
  135. return &hpd->base.base;
  136. ASSERT_CRITICAL(false);
  137. dm_free(hpd);
  138. return NULL;
  139. }