intel_hdmi.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/delay.h>
  30. #include "drmP.h"
  31. #include "drm.h"
  32. #include "drm_crtc.h"
  33. #include "intel_drv.h"
  34. #include "i915_drm.h"
  35. #include "i915_drv.h"
  36. struct intel_hdmi_priv {
  37. u32 sdvox_reg;
  38. u32 save_SDVOX;
  39. int has_hdmi_sink;
  40. };
  41. static void intel_hdmi_mode_set(struct drm_encoder *encoder,
  42. struct drm_display_mode *mode,
  43. struct drm_display_mode *adjusted_mode)
  44. {
  45. struct drm_device *dev = encoder->dev;
  46. struct drm_i915_private *dev_priv = dev->dev_private;
  47. struct drm_crtc *crtc = encoder->crtc;
  48. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  49. struct intel_output *intel_output = enc_to_intel_output(encoder);
  50. struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
  51. u32 sdvox;
  52. sdvox = SDVO_ENCODING_HDMI |
  53. SDVO_BORDER_ENABLE |
  54. SDVO_VSYNC_ACTIVE_HIGH |
  55. SDVO_HSYNC_ACTIVE_HIGH;
  56. if (hdmi_priv->has_hdmi_sink)
  57. sdvox |= SDVO_AUDIO_ENABLE;
  58. if (intel_crtc->pipe == 1)
  59. sdvox |= SDVO_PIPE_B_SELECT;
  60. I915_WRITE(hdmi_priv->sdvox_reg, sdvox);
  61. POSTING_READ(hdmi_priv->sdvox_reg);
  62. }
  63. static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
  64. {
  65. struct drm_device *dev = encoder->dev;
  66. struct drm_i915_private *dev_priv = dev->dev_private;
  67. struct intel_output *intel_output = enc_to_intel_output(encoder);
  68. struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
  69. u32 temp;
  70. if (mode != DRM_MODE_DPMS_ON) {
  71. temp = I915_READ(hdmi_priv->sdvox_reg);
  72. I915_WRITE(hdmi_priv->sdvox_reg, temp & ~SDVO_ENABLE);
  73. } else {
  74. temp = I915_READ(hdmi_priv->sdvox_reg);
  75. I915_WRITE(hdmi_priv->sdvox_reg, temp | SDVO_ENABLE);
  76. }
  77. POSTING_READ(hdmi_priv->sdvox_reg);
  78. }
  79. static void intel_hdmi_save(struct drm_connector *connector)
  80. {
  81. struct drm_device *dev = connector->dev;
  82. struct drm_i915_private *dev_priv = dev->dev_private;
  83. struct intel_output *intel_output = to_intel_output(connector);
  84. struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
  85. hdmi_priv->save_SDVOX = I915_READ(hdmi_priv->sdvox_reg);
  86. }
  87. static void intel_hdmi_restore(struct drm_connector *connector)
  88. {
  89. struct drm_device *dev = connector->dev;
  90. struct drm_i915_private *dev_priv = dev->dev_private;
  91. struct intel_output *intel_output = to_intel_output(connector);
  92. struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
  93. I915_WRITE(hdmi_priv->sdvox_reg, hdmi_priv->save_SDVOX);
  94. POSTING_READ(hdmi_priv->sdvox_reg);
  95. }
  96. static int intel_hdmi_mode_valid(struct drm_connector *connector,
  97. struct drm_display_mode *mode)
  98. {
  99. if (mode->clock > 165000)
  100. return MODE_CLOCK_HIGH;
  101. if (mode->clock < 20000)
  102. return MODE_CLOCK_HIGH;
  103. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  104. return MODE_NO_DBLESCAN;
  105. return MODE_OK;
  106. }
  107. static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
  108. struct drm_display_mode *mode,
  109. struct drm_display_mode *adjusted_mode)
  110. {
  111. return true;
  112. }
  113. static enum drm_connector_status
  114. intel_hdmi_detect(struct drm_connector *connector)
  115. {
  116. struct drm_device *dev = connector->dev;
  117. struct drm_i915_private *dev_priv = dev->dev_private;
  118. struct intel_output *intel_output = to_intel_output(connector);
  119. struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
  120. u32 temp, bit;
  121. temp = I915_READ(PORT_HOTPLUG_EN);
  122. I915_WRITE(PORT_HOTPLUG_EN,
  123. temp |
  124. HDMIB_HOTPLUG_INT_EN |
  125. HDMIC_HOTPLUG_INT_EN |
  126. HDMID_HOTPLUG_INT_EN);
  127. POSTING_READ(PORT_HOTPLUG_EN);
  128. switch (hdmi_priv->sdvox_reg) {
  129. case SDVOB:
  130. bit = HDMIB_HOTPLUG_INT_STATUS;
  131. break;
  132. case SDVOC:
  133. bit = HDMIC_HOTPLUG_INT_STATUS;
  134. break;
  135. default:
  136. return connector_status_unknown;
  137. }
  138. if ((I915_READ(PORT_HOTPLUG_STAT) & bit) != 0)
  139. return connector_status_connected;
  140. else
  141. return connector_status_disconnected;
  142. }
  143. static int intel_hdmi_get_modes(struct drm_connector *connector)
  144. {
  145. struct intel_output *intel_output = to_intel_output(connector);
  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(intel_output);
  150. }
  151. static void intel_hdmi_destroy(struct drm_connector *connector)
  152. {
  153. struct intel_output *intel_output = to_intel_output(connector);
  154. if (intel_output->i2c_bus)
  155. intel_i2c_destroy(intel_output->i2c_bus);
  156. drm_sysfs_connector_remove(connector);
  157. drm_connector_cleanup(connector);
  158. kfree(intel_output);
  159. }
  160. static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
  161. .dpms = intel_hdmi_dpms,
  162. .mode_fixup = intel_hdmi_mode_fixup,
  163. .prepare = intel_encoder_prepare,
  164. .mode_set = intel_hdmi_mode_set,
  165. .commit = intel_encoder_commit,
  166. };
  167. static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
  168. .save = intel_hdmi_save,
  169. .restore = intel_hdmi_restore,
  170. .detect = intel_hdmi_detect,
  171. .fill_modes = drm_helper_probe_single_connector_modes,
  172. .destroy = intel_hdmi_destroy,
  173. };
  174. static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
  175. .get_modes = intel_hdmi_get_modes,
  176. .mode_valid = intel_hdmi_mode_valid,
  177. .best_encoder = intel_best_encoder,
  178. };
  179. static void intel_hdmi_enc_destroy(struct drm_encoder *encoder)
  180. {
  181. drm_encoder_cleanup(encoder);
  182. }
  183. static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
  184. .destroy = intel_hdmi_enc_destroy,
  185. };
  186. void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
  187. {
  188. struct drm_i915_private *dev_priv = dev->dev_private;
  189. struct drm_connector *connector;
  190. struct intel_output *intel_output;
  191. struct intel_hdmi_priv *hdmi_priv;
  192. intel_output = kcalloc(sizeof(struct intel_output) +
  193. sizeof(struct intel_hdmi_priv), 1, GFP_KERNEL);
  194. if (!intel_output)
  195. return;
  196. hdmi_priv = (struct intel_hdmi_priv *)(intel_output + 1);
  197. connector = &intel_output->base;
  198. drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
  199. DRM_MODE_CONNECTOR_DVID);
  200. drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
  201. intel_output->type = INTEL_OUTPUT_HDMI;
  202. connector->interlace_allowed = 0;
  203. connector->doublescan_allowed = 0;
  204. /* Set up the DDC bus. */
  205. if (sdvox_reg == SDVOB)
  206. intel_output->ddc_bus = intel_i2c_create(dev, GPIOE, "HDMIB");
  207. else
  208. intel_output->ddc_bus = intel_i2c_create(dev, GPIOD, "HDMIC");
  209. if (!intel_output->ddc_bus)
  210. goto err_connector;
  211. hdmi_priv->sdvox_reg = sdvox_reg;
  212. intel_output->dev_priv = hdmi_priv;
  213. drm_encoder_init(dev, &intel_output->enc, &intel_hdmi_enc_funcs,
  214. DRM_MODE_ENCODER_TMDS);
  215. drm_encoder_helper_add(&intel_output->enc, &intel_hdmi_helper_funcs);
  216. drm_mode_connector_attach_encoder(&intel_output->base,
  217. &intel_output->enc);
  218. drm_sysfs_connector_add(connector);
  219. /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
  220. * 0xd. Failure to do so will result in spurious interrupts being
  221. * generated on the port when a cable is not attached.
  222. */
  223. if (IS_G4X(dev) && !IS_GM45(dev)) {
  224. u32 temp = I915_READ(PEG_BAND_GAP_DATA);
  225. I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
  226. }
  227. return;
  228. err_connector:
  229. drm_connector_cleanup(connector);
  230. kfree(intel_output);
  231. return;
  232. }