hdmi_hpd.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 Red Hat
  4. * Author: Rob Clark <robdclark@gmail.com>
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/gpio/consumer.h>
  8. #include <linux/pinctrl/consumer.h>
  9. #include "msm_kms.h"
  10. #include "hdmi.h"
  11. static void msm_hdmi_phy_reset(struct hdmi *hdmi)
  12. {
  13. unsigned int val;
  14. val = hdmi_read(hdmi, REG_HDMI_PHY_CTRL);
  15. if (val & HDMI_PHY_CTRL_SW_RESET_LOW) {
  16. /* pull low */
  17. hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
  18. val & ~HDMI_PHY_CTRL_SW_RESET);
  19. } else {
  20. /* pull high */
  21. hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
  22. val | HDMI_PHY_CTRL_SW_RESET);
  23. }
  24. if (val & HDMI_PHY_CTRL_SW_RESET_PLL_LOW) {
  25. /* pull low */
  26. hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
  27. val & ~HDMI_PHY_CTRL_SW_RESET_PLL);
  28. } else {
  29. /* pull high */
  30. hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
  31. val | HDMI_PHY_CTRL_SW_RESET_PLL);
  32. }
  33. msleep(100);
  34. if (val & HDMI_PHY_CTRL_SW_RESET_LOW) {
  35. /* pull high */
  36. hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
  37. val | HDMI_PHY_CTRL_SW_RESET);
  38. } else {
  39. /* pull low */
  40. hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
  41. val & ~HDMI_PHY_CTRL_SW_RESET);
  42. }
  43. if (val & HDMI_PHY_CTRL_SW_RESET_PLL_LOW) {
  44. /* pull high */
  45. hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
  46. val | HDMI_PHY_CTRL_SW_RESET_PLL);
  47. } else {
  48. /* pull low */
  49. hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
  50. val & ~HDMI_PHY_CTRL_SW_RESET_PLL);
  51. }
  52. }
  53. void msm_hdmi_hpd_enable(struct drm_bridge *bridge)
  54. {
  55. struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
  56. struct hdmi *hdmi = hdmi_bridge->hdmi;
  57. struct device *dev = &hdmi->pdev->dev;
  58. uint32_t hpd_ctrl;
  59. int ret;
  60. unsigned long flags;
  61. if (hdmi->hpd_gpiod)
  62. gpiod_set_value_cansleep(hdmi->hpd_gpiod, 1);
  63. ret = pm_runtime_resume_and_get(dev);
  64. if (WARN_ON(ret))
  65. return;
  66. mutex_lock(&hdmi->state_mutex);
  67. msm_hdmi_set_mode(hdmi, false);
  68. msm_hdmi_phy_reset(hdmi);
  69. msm_hdmi_set_mode(hdmi, true);
  70. hdmi->hpd_enabled = true;
  71. mutex_unlock(&hdmi->state_mutex);
  72. hdmi_write(hdmi, REG_HDMI_USEC_REFTIMER, 0x0001001b);
  73. /* enable HPD events: */
  74. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
  75. HDMI_HPD_INT_CTRL_INT_CONNECT |
  76. HDMI_HPD_INT_CTRL_INT_EN);
  77. /* set timeout to 4.1ms (max) for hardware debounce */
  78. spin_lock_irqsave(&hdmi->reg_lock, flags);
  79. hpd_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
  80. hpd_ctrl |= HDMI_HPD_CTRL_TIMEOUT(0x1fff);
  81. /* Toggle HPD circuit to trigger HPD sense */
  82. hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
  83. ~HDMI_HPD_CTRL_ENABLE & hpd_ctrl);
  84. hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
  85. HDMI_HPD_CTRL_ENABLE | hpd_ctrl);
  86. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  87. }
  88. void msm_hdmi_hpd_disable(struct drm_bridge *bridge)
  89. {
  90. struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
  91. struct hdmi *hdmi = hdmi_bridge->hdmi;
  92. struct device *dev = &hdmi->pdev->dev;
  93. /* Disable HPD interrupt */
  94. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, 0);
  95. mutex_lock(&hdmi->state_mutex);
  96. hdmi->hpd_enabled = false;
  97. msm_hdmi_set_mode(hdmi, hdmi->power_on);
  98. mutex_unlock(&hdmi->state_mutex);
  99. pm_runtime_put(dev);
  100. }
  101. void msm_hdmi_hpd_irq(struct drm_bridge *bridge)
  102. {
  103. struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
  104. struct hdmi *hdmi = hdmi_bridge->hdmi;
  105. uint32_t hpd_int_status, hpd_int_ctrl;
  106. /* Process HPD: */
  107. hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
  108. hpd_int_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_INT_CTRL);
  109. if ((hpd_int_ctrl & HDMI_HPD_INT_CTRL_INT_EN) &&
  110. (hpd_int_status & HDMI_HPD_INT_STATUS_INT)) {
  111. bool detected = !!(hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED);
  112. /* ack & disable (temporarily) HPD events: */
  113. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
  114. HDMI_HPD_INT_CTRL_INT_ACK);
  115. DBG("status=%04x, ctrl=%04x", hpd_int_status, hpd_int_ctrl);
  116. /* detect disconnect if we are connected or visa versa: */
  117. hpd_int_ctrl = HDMI_HPD_INT_CTRL_INT_EN;
  118. if (!detected)
  119. hpd_int_ctrl |= HDMI_HPD_INT_CTRL_INT_CONNECT;
  120. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, hpd_int_ctrl);
  121. queue_work(hdmi->workq, &hdmi_bridge->hpd_work);
  122. }
  123. }
  124. static enum drm_connector_status detect_reg(struct hdmi *hdmi)
  125. {
  126. u32 hpd_int_status = 0;
  127. int ret;
  128. ret = pm_runtime_resume_and_get(&hdmi->pdev->dev);
  129. if (ret)
  130. goto out;
  131. hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
  132. out:
  133. pm_runtime_put(&hdmi->pdev->dev);
  134. return (hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED) ?
  135. connector_status_connected : connector_status_disconnected;
  136. }
  137. #define HPD_GPIO_INDEX 2
  138. static enum drm_connector_status detect_gpio(struct hdmi *hdmi)
  139. {
  140. return gpiod_get_value(hdmi->hpd_gpiod) ?
  141. connector_status_connected :
  142. connector_status_disconnected;
  143. }
  144. enum drm_connector_status
  145. msm_hdmi_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)
  146. {
  147. struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
  148. struct hdmi *hdmi = hdmi_bridge->hdmi;
  149. enum drm_connector_status stat_gpio, stat_reg;
  150. int retry = 20;
  151. /*
  152. * some platforms may not have hpd gpio. Rely only on the status
  153. * provided by REG_HDMI_HPD_INT_STATUS in this case.
  154. */
  155. if (!hdmi->hpd_gpiod)
  156. return detect_reg(hdmi);
  157. do {
  158. stat_gpio = detect_gpio(hdmi);
  159. stat_reg = detect_reg(hdmi);
  160. if (stat_gpio == stat_reg)
  161. break;
  162. mdelay(10);
  163. } while (--retry);
  164. /* the status we get from reading gpio seems to be more reliable,
  165. * so trust that one the most if we didn't manage to get hdmi and
  166. * gpio status to agree:
  167. */
  168. if (stat_gpio != stat_reg) {
  169. DBG("HDMI_HPD_INT_STATUS tells us: %d", stat_reg);
  170. DBG("hpd gpio tells us: %d", stat_gpio);
  171. }
  172. return stat_gpio;
  173. }