intel_hdmi.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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_priv {
  39. u32 sdvox_reg;
  40. bool has_hdmi_sink;
  41. };
  42. static void intel_hdmi_mode_set(struct drm_encoder *encoder,
  43. struct drm_display_mode *mode,
  44. struct drm_display_mode *adjusted_mode)
  45. {
  46. struct drm_device *dev = encoder->dev;
  47. struct drm_i915_private *dev_priv = dev->dev_private;
  48. struct drm_crtc *crtc = encoder->crtc;
  49. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  50. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  51. struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
  52. u32 sdvox;
  53. sdvox = SDVO_ENCODING_HDMI |
  54. SDVO_BORDER_ENABLE |
  55. SDVO_VSYNC_ACTIVE_HIGH |
  56. SDVO_HSYNC_ACTIVE_HIGH;
  57. if (hdmi_priv->has_hdmi_sink) {
  58. sdvox |= SDVO_AUDIO_ENABLE;
  59. if (HAS_PCH_CPT(dev))
  60. sdvox |= HDMI_MODE_SELECT;
  61. }
  62. if (intel_crtc->pipe == 1) {
  63. if (HAS_PCH_CPT(dev))
  64. sdvox |= PORT_TRANS_B_SEL_CPT;
  65. else
  66. sdvox |= SDVO_PIPE_B_SELECT;
  67. }
  68. I915_WRITE(hdmi_priv->sdvox_reg, sdvox);
  69. POSTING_READ(hdmi_priv->sdvox_reg);
  70. }
  71. static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
  72. {
  73. struct drm_device *dev = encoder->dev;
  74. struct drm_i915_private *dev_priv = dev->dev_private;
  75. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  76. struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
  77. u32 temp;
  78. temp = I915_READ(hdmi_priv->sdvox_reg);
  79. /* HW workaround, need to toggle enable bit off and on for 12bpc, but
  80. * we do this anyway which shows more stable in testing.
  81. */
  82. if (HAS_PCH_SPLIT(dev)) {
  83. I915_WRITE(hdmi_priv->sdvox_reg, temp & ~SDVO_ENABLE);
  84. POSTING_READ(hdmi_priv->sdvox_reg);
  85. }
  86. if (mode != DRM_MODE_DPMS_ON) {
  87. temp &= ~SDVO_ENABLE;
  88. } else {
  89. temp |= SDVO_ENABLE;
  90. }
  91. I915_WRITE(hdmi_priv->sdvox_reg, temp);
  92. POSTING_READ(hdmi_priv->sdvox_reg);
  93. /* HW workaround, need to write this twice for issue that may result
  94. * in first write getting masked.
  95. */
  96. if (HAS_PCH_SPLIT(dev)) {
  97. I915_WRITE(hdmi_priv->sdvox_reg, temp);
  98. POSTING_READ(hdmi_priv->sdvox_reg);
  99. }
  100. }
  101. static int intel_hdmi_mode_valid(struct drm_connector *connector,
  102. struct drm_display_mode *mode)
  103. {
  104. if (mode->clock > 165000)
  105. return MODE_CLOCK_HIGH;
  106. if (mode->clock < 20000)
  107. return MODE_CLOCK_HIGH;
  108. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  109. return MODE_NO_DBLESCAN;
  110. return MODE_OK;
  111. }
  112. static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
  113. struct drm_display_mode *mode,
  114. struct drm_display_mode *adjusted_mode)
  115. {
  116. return true;
  117. }
  118. static enum drm_connector_status
  119. intel_hdmi_detect(struct drm_connector *connector)
  120. {
  121. struct drm_encoder *encoder = intel_attached_encoder(connector);
  122. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  123. struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
  124. struct edid *edid = NULL;
  125. enum drm_connector_status status = connector_status_disconnected;
  126. hdmi_priv->has_hdmi_sink = false;
  127. edid = drm_get_edid(connector,
  128. intel_encoder->ddc_bus);
  129. if (edid) {
  130. if (edid->input & DRM_EDID_INPUT_DIGITAL) {
  131. status = connector_status_connected;
  132. hdmi_priv->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
  133. }
  134. connector->display_info.raw_edid = NULL;
  135. kfree(edid);
  136. }
  137. return status;
  138. }
  139. static int intel_hdmi_get_modes(struct drm_connector *connector)
  140. {
  141. struct drm_encoder *encoder = intel_attached_encoder(connector);
  142. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  143. /* We should parse the EDID data and find out if it's an HDMI sink so
  144. * we can send audio to it.
  145. */
  146. return intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
  147. }
  148. static void intel_hdmi_destroy(struct drm_connector *connector)
  149. {
  150. drm_sysfs_connector_remove(connector);
  151. drm_connector_cleanup(connector);
  152. kfree(connector);
  153. }
  154. static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
  155. .dpms = intel_hdmi_dpms,
  156. .mode_fixup = intel_hdmi_mode_fixup,
  157. .prepare = intel_encoder_prepare,
  158. .mode_set = intel_hdmi_mode_set,
  159. .commit = intel_encoder_commit,
  160. };
  161. static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
  162. .dpms = drm_helper_connector_dpms,
  163. .detect = intel_hdmi_detect,
  164. .fill_modes = drm_helper_probe_single_connector_modes,
  165. .destroy = intel_hdmi_destroy,
  166. };
  167. static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
  168. .get_modes = intel_hdmi_get_modes,
  169. .mode_valid = intel_hdmi_mode_valid,
  170. .best_encoder = intel_attached_encoder,
  171. };
  172. static void intel_hdmi_enc_destroy(struct drm_encoder *encoder)
  173. {
  174. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  175. if (intel_encoder->i2c_bus)
  176. intel_i2c_destroy(intel_encoder->i2c_bus);
  177. drm_encoder_cleanup(encoder);
  178. kfree(intel_encoder);
  179. }
  180. static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
  181. .destroy = intel_hdmi_enc_destroy,
  182. };
  183. void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
  184. {
  185. struct drm_i915_private *dev_priv = dev->dev_private;
  186. struct drm_connector *connector;
  187. struct intel_encoder *intel_encoder;
  188. struct intel_connector *intel_connector;
  189. struct intel_hdmi_priv *hdmi_priv;
  190. intel_encoder = kcalloc(sizeof(struct intel_encoder) +
  191. sizeof(struct intel_hdmi_priv), 1, GFP_KERNEL);
  192. if (!intel_encoder)
  193. return;
  194. intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
  195. if (!intel_connector) {
  196. kfree(intel_encoder);
  197. return;
  198. }
  199. hdmi_priv = (struct intel_hdmi_priv *)(intel_encoder + 1);
  200. connector = &intel_connector->base;
  201. drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
  202. DRM_MODE_CONNECTOR_HDMIA);
  203. drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
  204. intel_encoder->type = INTEL_OUTPUT_HDMI;
  205. connector->polled = DRM_CONNECTOR_POLL_HPD;
  206. connector->interlace_allowed = 0;
  207. connector->doublescan_allowed = 0;
  208. intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
  209. /* Set up the DDC bus. */
  210. if (sdvox_reg == SDVOB) {
  211. intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
  212. intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOE, "HDMIB");
  213. dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
  214. } else if (sdvox_reg == SDVOC) {
  215. intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
  216. intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOD, "HDMIC");
  217. dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
  218. } else if (sdvox_reg == HDMIB) {
  219. intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
  220. intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOE,
  221. "HDMIB");
  222. dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
  223. } else if (sdvox_reg == HDMIC) {
  224. intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
  225. intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOD,
  226. "HDMIC");
  227. dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
  228. } else if (sdvox_reg == HDMID) {
  229. intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
  230. intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOF,
  231. "HDMID");
  232. dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
  233. }
  234. if (!intel_encoder->ddc_bus)
  235. goto err_connector;
  236. hdmi_priv->sdvox_reg = sdvox_reg;
  237. intel_encoder->dev_priv = hdmi_priv;
  238. drm_encoder_init(dev, &intel_encoder->enc, &intel_hdmi_enc_funcs,
  239. DRM_MODE_ENCODER_TMDS);
  240. drm_encoder_helper_add(&intel_encoder->enc, &intel_hdmi_helper_funcs);
  241. drm_mode_connector_attach_encoder(&intel_connector->base,
  242. &intel_encoder->enc);
  243. drm_sysfs_connector_add(connector);
  244. /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
  245. * 0xd. Failure to do so will result in spurious interrupts being
  246. * generated on the port when a cable is not attached.
  247. */
  248. if (IS_G4X(dev) && !IS_GM45(dev)) {
  249. u32 temp = I915_READ(PEG_BAND_GAP_DATA);
  250. I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
  251. }
  252. return;
  253. err_connector:
  254. drm_connector_cleanup(connector);
  255. kfree(intel_encoder);
  256. kfree(intel_connector);
  257. return;
  258. }