intel_hdmi.c 9.4 KB

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