hw_ddc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_ddc.h"
  29. #include "reg_helper.h"
  30. #include "gpio_regs.h"
  31. #undef FN
  32. #define FN(reg_name, field_name) \
  33. ddc->shifts->field_name, ddc->masks->field_name
  34. #define CTX \
  35. ddc->base.base.ctx
  36. #define REG(reg)\
  37. (ddc->regs->reg)
  38. static void destruct(
  39. struct hw_ddc *pin)
  40. {
  41. dal_hw_gpio_destruct(&pin->base);
  42. }
  43. static void destroy(
  44. struct hw_gpio_pin **ptr)
  45. {
  46. struct hw_ddc *pin = HW_DDC_FROM_BASE(*ptr);
  47. destruct(pin);
  48. kfree(pin);
  49. *ptr = NULL;
  50. }
  51. static enum gpio_result set_config(
  52. struct hw_gpio_pin *ptr,
  53. const struct gpio_config_data *config_data)
  54. {
  55. struct hw_ddc *ddc = HW_DDC_FROM_BASE(ptr);
  56. struct hw_gpio *hw_gpio = NULL;
  57. uint32_t regval;
  58. uint32_t ddc_data_pd_en = 0;
  59. uint32_t ddc_clk_pd_en = 0;
  60. uint32_t aux_pad_mode = 0;
  61. hw_gpio = &ddc->base;
  62. if (hw_gpio == NULL) {
  63. ASSERT_CRITICAL(false);
  64. return GPIO_RESULT_NULL_HANDLE;
  65. }
  66. regval = REG_GET_3(gpio.MASK_reg,
  67. DC_GPIO_DDC1DATA_PD_EN, &ddc_data_pd_en,
  68. DC_GPIO_DDC1CLK_PD_EN, &ddc_clk_pd_en,
  69. AUX_PAD1_MODE, &aux_pad_mode);
  70. switch (config_data->config.ddc.type) {
  71. case GPIO_DDC_CONFIG_TYPE_MODE_I2C:
  72. /* On plug-in, there is a transient level on the pad
  73. * which must be discharged through the internal pull-down.
  74. * Enable internal pull-down, 2.5msec discharge time
  75. * is required for detection of AUX mode */
  76. if (hw_gpio->base.en != GPIO_DDC_LINE_VIP_PAD) {
  77. if (!ddc_data_pd_en || !ddc_clk_pd_en) {
  78. REG_SET_2(gpio.MASK_reg, regval,
  79. DC_GPIO_DDC1DATA_PD_EN, 1,
  80. DC_GPIO_DDC1CLK_PD_EN, 1);
  81. if (config_data->type ==
  82. GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE)
  83. msleep(3);
  84. }
  85. } else {
  86. uint32_t reg2;
  87. uint32_t sda_pd_dis = 0;
  88. uint32_t scl_pd_dis = 0;
  89. reg2 = REG_GET_2(gpio.MASK_reg,
  90. DC_GPIO_SDA_PD_DIS, &sda_pd_dis,
  91. DC_GPIO_SCL_PD_DIS, &scl_pd_dis);
  92. if (sda_pd_dis) {
  93. REG_SET(gpio.MASK_reg, regval,
  94. DC_GPIO_SDA_PD_DIS, 0);
  95. if (config_data->type ==
  96. GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE)
  97. msleep(3);
  98. }
  99. if (!scl_pd_dis) {
  100. REG_SET(gpio.MASK_reg, regval,
  101. DC_GPIO_SCL_PD_DIS, 1);
  102. if (config_data->type ==
  103. GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE)
  104. msleep(3);
  105. }
  106. }
  107. if (aux_pad_mode) {
  108. /* let pins to get de-asserted
  109. * before setting pad to I2C mode */
  110. if (config_data->config.ddc.data_en_bit_present ||
  111. config_data->config.ddc.clock_en_bit_present)
  112. /* [anaumov] in DAL2, there was
  113. * dc_service_delay_in_microseconds(2000); */
  114. msleep(2);
  115. /* set the I2C pad mode */
  116. /* read the register again,
  117. * some bits may have been changed */
  118. REG_UPDATE(gpio.MASK_reg,
  119. AUX_PAD1_MODE, 0);
  120. }
  121. return GPIO_RESULT_OK;
  122. case GPIO_DDC_CONFIG_TYPE_MODE_AUX:
  123. /* set the AUX pad mode */
  124. if (!aux_pad_mode) {
  125. REG_SET(gpio.MASK_reg, regval,
  126. AUX_PAD1_MODE, 1);
  127. }
  128. return GPIO_RESULT_OK;
  129. case GPIO_DDC_CONFIG_TYPE_POLL_FOR_CONNECT:
  130. if ((hw_gpio->base.en >= GPIO_DDC_LINE_DDC1) &&
  131. (hw_gpio->base.en <= GPIO_DDC_LINE_DDC_VGA)) {
  132. REG_UPDATE_3(ddc_setup,
  133. DC_I2C_DDC1_ENABLE, 1,
  134. DC_I2C_DDC1_EDID_DETECT_ENABLE, 1,
  135. DC_I2C_DDC1_EDID_DETECT_MODE, 0);
  136. return GPIO_RESULT_OK;
  137. }
  138. break;
  139. case GPIO_DDC_CONFIG_TYPE_POLL_FOR_DISCONNECT:
  140. if ((hw_gpio->base.en >= GPIO_DDC_LINE_DDC1) &&
  141. (hw_gpio->base.en <= GPIO_DDC_LINE_DDC_VGA)) {
  142. REG_UPDATE_3(ddc_setup,
  143. DC_I2C_DDC1_ENABLE, 1,
  144. DC_I2C_DDC1_EDID_DETECT_ENABLE, 1,
  145. DC_I2C_DDC1_EDID_DETECT_MODE, 1);
  146. return GPIO_RESULT_OK;
  147. }
  148. break;
  149. case GPIO_DDC_CONFIG_TYPE_DISABLE_POLLING:
  150. if ((hw_gpio->base.en >= GPIO_DDC_LINE_DDC1) &&
  151. (hw_gpio->base.en <= GPIO_DDC_LINE_DDC_VGA)) {
  152. REG_UPDATE_2(ddc_setup,
  153. DC_I2C_DDC1_ENABLE, 0,
  154. DC_I2C_DDC1_EDID_DETECT_ENABLE, 0);
  155. return GPIO_RESULT_OK;
  156. }
  157. break;
  158. }
  159. BREAK_TO_DEBUGGER();
  160. return GPIO_RESULT_NON_SPECIFIC_ERROR;
  161. }
  162. static const struct hw_gpio_pin_funcs funcs = {
  163. .destroy = destroy,
  164. .open = dal_hw_gpio_open,
  165. .get_value = dal_hw_gpio_get_value,
  166. .set_value = dal_hw_gpio_set_value,
  167. .set_config = set_config,
  168. .change_mode = dal_hw_gpio_change_mode,
  169. .close = dal_hw_gpio_close,
  170. };
  171. static void construct(
  172. struct hw_ddc *ddc,
  173. enum gpio_id id,
  174. uint32_t en,
  175. struct dc_context *ctx)
  176. {
  177. dal_hw_gpio_construct(&ddc->base, id, en, ctx);
  178. ddc->base.base.funcs = &funcs;
  179. }
  180. struct hw_gpio_pin *dal_hw_ddc_create(
  181. struct dc_context *ctx,
  182. enum gpio_id id,
  183. uint32_t en)
  184. {
  185. struct hw_ddc *pin;
  186. if ((en < GPIO_DDC_LINE_MIN) || (en > GPIO_DDC_LINE_MAX)) {
  187. ASSERT_CRITICAL(false);
  188. return NULL;
  189. }
  190. pin = kzalloc(sizeof(struct hw_ddc), GFP_KERNEL);
  191. if (!pin) {
  192. ASSERT_CRITICAL(false);
  193. return NULL;
  194. }
  195. construct(pin, id, en, ctx);
  196. return &pin->base.base;
  197. }