intel_hdmi.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright 2006 Dave Airlie <airlied@linux.ie>
  3. * Copyright © 2006-2009 Intel Corporation
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors:
  25. * Eric Anholt <eric@anholt.net>
  26. * Jesse Barnes <jesse.barnes@intel.com>
  27. */
  28. #include <linux/i2c.h>
  29. #include <linux/slab.h>
  30. #include <linux/delay.h>
  31. #include "drmP.h"
  32. #include "drm.h"
  33. #include "drm_crtc.h"
  34. #include "drm_edid.h"
  35. #include "intel_drv.h"
  36. #include "i915_drm.h"
  37. #include "i915_drv.h"
  38. struct intel_hdmi {
  39. struct intel_encoder base;
  40. u32 sdvox_reg;
  41. int ddc_bus;
  42. bool has_hdmi_sink;
  43. };
  44. static struct intel_hdmi *enc_to_intel_hdmi(struct drm_encoder *encoder)
  45. {
  46. return container_of(encoder, struct intel_hdmi, base.base);
  47. }
  48. static struct intel_hdmi *intel_attached_hdmi(struct drm_connector *connector)
  49. {
  50. return container_of(intel_attached_encoder(connector),
  51. struct intel_hdmi, base);
  52. }
  53. static void intel_hdmi_mode_set(struct drm_encoder *encoder,
  54. struct drm_display_mode *mode,
  55. struct drm_display_mode *adjusted_mode)
  56. {
  57. struct drm_device *dev = encoder->dev;
  58. struct drm_i915_private *dev_priv = dev->dev_private;
  59. struct drm_crtc *crtc = encoder->crtc;
  60. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  61. struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
  62. u32 sdvox;
  63. sdvox = SDVO_ENCODING_HDMI | SDVO_BORDER_ENABLE;
  64. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  65. sdvox |= SDVO_VSYNC_ACTIVE_HIGH;
  66. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  67. sdvox |= SDVO_HSYNC_ACTIVE_HIGH;
  68. if (intel_hdmi->has_hdmi_sink) {
  69. sdvox |= SDVO_AUDIO_ENABLE;
  70. if (HAS_PCH_CPT(dev))
  71. sdvox |= HDMI_MODE_SELECT;
  72. }
  73. if (intel_crtc->pipe == 1) {
  74. if (HAS_PCH_CPT(dev))
  75. sdvox |= PORT_TRANS_B_SEL_CPT;
  76. else
  77. sdvox |= SDVO_PIPE_B_SELECT;
  78. }
  79. I915_WRITE(intel_hdmi->sdvox_reg, sdvox);
  80. POSTING_READ(intel_hdmi->sdvox_reg);
  81. }
  82. static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
  83. {
  84. struct drm_device *dev = encoder->dev;
  85. struct drm_i915_private *dev_priv = dev->dev_private;
  86. struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
  87. u32 temp;
  88. temp = I915_READ(intel_hdmi->sdvox_reg);
  89. /* HW workaround, need to toggle enable bit off and on for 12bpc, but
  90. * we do this anyway which shows more stable in testing.
  91. */
  92. if (HAS_PCH_SPLIT(dev)) {
  93. I915_WRITE(intel_hdmi->sdvox_reg, temp & ~SDVO_ENABLE);
  94. POSTING_READ(intel_hdmi->sdvox_reg);
  95. }
  96. if (mode != DRM_MODE_DPMS_ON) {
  97. temp &= ~SDVO_ENABLE;
  98. } else {
  99. temp |= SDVO_ENABLE;
  100. }
  101. I915_WRITE(intel_hdmi->sdvox_reg, temp);
  102. POSTING_READ(intel_hdmi->sdvox_reg);
  103. /* HW workaround, need to write this twice for issue that may result
  104. * in first write getting masked.
  105. */
  106. if (HAS_PCH_SPLIT(dev)) {
  107. I915_WRITE(intel_hdmi->sdvox_reg, temp);
  108. POSTING_READ(intel_hdmi->sdvox_reg);
  109. }
  110. }
  111. static int intel_hdmi_mode_valid(struct drm_connector *connector,
  112. struct drm_display_mode *mode)
  113. {
  114. if (mode->clock > 165000)
  115. return MODE_CLOCK_HIGH;
  116. if (mode->clock < 20000)
  117. return MODE_CLOCK_HIGH;
  118. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  119. return MODE_NO_DBLESCAN;
  120. return MODE_OK;
  121. }
  122. static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
  123. struct drm_display_mode *mode,
  124. struct drm_display_mode *adjusted_mode)
  125. {
  126. return true;
  127. }
  128. static enum drm_connector_status
  129. intel_hdmi_detect(struct drm_connector *connector, bool force)
  130. {
  131. struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
  132. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  133. struct edid *edid;
  134. enum drm_connector_status status = connector_status_disconnected;
  135. intel_hdmi->has_hdmi_sink = false;
  136. edid = drm_get_edid(connector,
  137. &dev_priv->gmbus[intel_hdmi->ddc_bus].adapter);
  138. if (edid) {
  139. if (edid->input & DRM_EDID_INPUT_DIGITAL) {
  140. status = connector_status_connected;
  141. intel_hdmi->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
  142. }
  143. connector->display_info.raw_edid = NULL;
  144. kfree(edid);
  145. }
  146. return status;
  147. }
  148. static int intel_hdmi_get_modes(struct drm_connector *connector)
  149. {
  150. struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
  151. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  152. /* We should parse the EDID data and find out if it's an HDMI sink so
  153. * we can send audio to it.
  154. */
  155. return intel_ddc_get_modes(connector,
  156. &dev_priv->gmbus[intel_hdmi->ddc_bus].adapter);
  157. }
  158. static void intel_hdmi_destroy(struct drm_connector *connector)
  159. {
  160. drm_sysfs_connector_remove(connector);
  161. drm_connector_cleanup(connector);
  162. kfree(connector);
  163. }
  164. static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
  165. .dpms = intel_hdmi_dpms,
  166. .mode_fixup = intel_hdmi_mode_fixup,
  167. .prepare = intel_encoder_prepare,
  168. .mode_set = intel_hdmi_mode_set,
  169. .commit = intel_encoder_commit,
  170. };
  171. static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
  172. .dpms = drm_helper_connector_dpms,
  173. .detect = intel_hdmi_detect,
  174. .fill_modes = drm_helper_probe_single_connector_modes,
  175. .destroy = intel_hdmi_destroy,
  176. };
  177. static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
  178. .get_modes = intel_hdmi_get_modes,
  179. .mode_valid = intel_hdmi_mode_valid,
  180. .best_encoder = intel_best_encoder,
  181. };
  182. static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
  183. .destroy = intel_encoder_destroy,
  184. };
  185. void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
  186. {
  187. struct drm_i915_private *dev_priv = dev->dev_private;
  188. struct drm_connector *connector;
  189. struct intel_encoder *intel_encoder;
  190. struct intel_connector *intel_connector;
  191. struct intel_hdmi *intel_hdmi;
  192. intel_hdmi = kzalloc(sizeof(struct intel_hdmi), GFP_KERNEL);
  193. if (!intel_hdmi)
  194. return;
  195. intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
  196. if (!intel_connector) {
  197. kfree(intel_hdmi);
  198. return;
  199. }
  200. intel_encoder = &intel_hdmi->base;
  201. drm_encoder_init(dev, &intel_encoder->base, &intel_hdmi_enc_funcs,
  202. DRM_MODE_ENCODER_TMDS);
  203. connector = &intel_connector->base;
  204. drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
  205. DRM_MODE_CONNECTOR_HDMIA);
  206. drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
  207. intel_encoder->type = INTEL_OUTPUT_HDMI;
  208. connector->polled = DRM_CONNECTOR_POLL_HPD;
  209. connector->interlace_allowed = 0;
  210. connector->doublescan_allowed = 0;
  211. intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
  212. /* Set up the DDC bus. */
  213. if (sdvox_reg == SDVOB) {
  214. intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
  215. intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
  216. dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
  217. } else if (sdvox_reg == SDVOC) {
  218. intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
  219. intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
  220. dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
  221. } else if (sdvox_reg == HDMIB) {
  222. intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
  223. intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
  224. dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
  225. } else if (sdvox_reg == HDMIC) {
  226. intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
  227. intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
  228. dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
  229. } else if (sdvox_reg == HDMID) {
  230. intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
  231. intel_hdmi->ddc_bus = GMBUS_PORT_DPD;
  232. dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
  233. }
  234. intel_hdmi->sdvox_reg = sdvox_reg;
  235. drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs);
  236. intel_connector_attach_encoder(intel_connector, intel_encoder);
  237. drm_sysfs_connector_add(connector);
  238. /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
  239. * 0xd. Failure to do so will result in spurious interrupts being
  240. * generated on the port when a cable is not attached.
  241. */
  242. if (IS_G4X(dev) && !IS_GM45(dev)) {
  243. u32 temp = I915_READ(PEG_BAND_GAP_DATA);
  244. I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
  245. }
  246. }