intel_hdmi.c 8.9 KB

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