intel_hdmi.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 "drm_edid.h"
  34. #include "intel_drv.h"
  35. #include "i915_drm.h"
  36. #include "i915_drv.h"
  37. struct intel_hdmi_priv {
  38. u32 sdvox_reg;
  39. u32 save_SDVOX;
  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_output *intel_output = enc_to_intel_output(encoder);
  51. struct intel_hdmi_priv *hdmi_priv = intel_output->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 (intel_crtc->pipe == 1)
  60. sdvox |= SDVO_PIPE_B_SELECT;
  61. I915_WRITE(hdmi_priv->sdvox_reg, sdvox);
  62. POSTING_READ(hdmi_priv->sdvox_reg);
  63. }
  64. static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
  65. {
  66. struct drm_device *dev = encoder->dev;
  67. struct drm_i915_private *dev_priv = dev->dev_private;
  68. struct intel_output *intel_output = enc_to_intel_output(encoder);
  69. struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
  70. u32 temp;
  71. if (mode != DRM_MODE_DPMS_ON) {
  72. temp = I915_READ(hdmi_priv->sdvox_reg);
  73. I915_WRITE(hdmi_priv->sdvox_reg, temp & ~SDVO_ENABLE);
  74. } else {
  75. temp = I915_READ(hdmi_priv->sdvox_reg);
  76. I915_WRITE(hdmi_priv->sdvox_reg, temp | SDVO_ENABLE);
  77. }
  78. POSTING_READ(hdmi_priv->sdvox_reg);
  79. }
  80. static void intel_hdmi_save(struct drm_connector *connector)
  81. {
  82. struct drm_device *dev = connector->dev;
  83. struct drm_i915_private *dev_priv = dev->dev_private;
  84. struct intel_output *intel_output = to_intel_output(connector);
  85. struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
  86. hdmi_priv->save_SDVOX = I915_READ(hdmi_priv->sdvox_reg);
  87. }
  88. static void intel_hdmi_restore(struct drm_connector *connector)
  89. {
  90. struct drm_device *dev = connector->dev;
  91. struct drm_i915_private *dev_priv = dev->dev_private;
  92. struct intel_output *intel_output = to_intel_output(connector);
  93. struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
  94. I915_WRITE(hdmi_priv->sdvox_reg, hdmi_priv->save_SDVOX);
  95. POSTING_READ(hdmi_priv->sdvox_reg);
  96. }
  97. static int intel_hdmi_mode_valid(struct drm_connector *connector,
  98. struct drm_display_mode *mode)
  99. {
  100. if (mode->clock > 165000)
  101. return MODE_CLOCK_HIGH;
  102. if (mode->clock < 20000)
  103. return MODE_CLOCK_HIGH;
  104. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  105. return MODE_NO_DBLESCAN;
  106. return MODE_OK;
  107. }
  108. static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
  109. struct drm_display_mode *mode,
  110. struct drm_display_mode *adjusted_mode)
  111. {
  112. return true;
  113. }
  114. static enum drm_connector_status
  115. intel_hdmi_edid_detect(struct drm_connector *connector)
  116. {
  117. struct intel_output *intel_output = to_intel_output(connector);
  118. struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
  119. struct edid *edid = NULL;
  120. enum drm_connector_status status = connector_status_disconnected;
  121. edid = drm_get_edid(&intel_output->base,
  122. intel_output->ddc_bus);
  123. hdmi_priv->has_hdmi_sink = false;
  124. if (edid) {
  125. if (edid->input & DRM_EDID_INPUT_DIGITAL) {
  126. status = connector_status_connected;
  127. hdmi_priv->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
  128. }
  129. intel_output->base.display_info.raw_edid = NULL;
  130. kfree(edid);
  131. }
  132. return status;
  133. }
  134. static enum drm_connector_status
  135. igdng_hdmi_detect(struct drm_connector *connector)
  136. {
  137. struct intel_output *intel_output = to_intel_output(connector);
  138. struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
  139. /* FIXME hotplug detect */
  140. hdmi_priv->has_hdmi_sink = false;
  141. return intel_hdmi_edid_detect(connector);
  142. }
  143. static enum drm_connector_status
  144. intel_hdmi_detect(struct drm_connector *connector)
  145. {
  146. struct drm_device *dev = connector->dev;
  147. struct drm_i915_private *dev_priv = dev->dev_private;
  148. struct intel_output *intel_output = to_intel_output(connector);
  149. struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
  150. u32 temp, bit;
  151. if (IS_IGDNG(dev))
  152. return igdng_hdmi_detect(connector);
  153. temp = I915_READ(PORT_HOTPLUG_EN);
  154. switch (hdmi_priv->sdvox_reg) {
  155. case SDVOB:
  156. temp |= HDMIB_HOTPLUG_INT_EN;
  157. break;
  158. case SDVOC:
  159. temp |= HDMIC_HOTPLUG_INT_EN;
  160. break;
  161. default:
  162. return connector_status_unknown;
  163. }
  164. I915_WRITE(PORT_HOTPLUG_EN, temp);
  165. POSTING_READ(PORT_HOTPLUG_EN);
  166. switch (hdmi_priv->sdvox_reg) {
  167. case SDVOB:
  168. bit = HDMIB_HOTPLUG_INT_STATUS;
  169. break;
  170. case SDVOC:
  171. bit = HDMIC_HOTPLUG_INT_STATUS;
  172. break;
  173. default:
  174. return connector_status_unknown;
  175. }
  176. if ((I915_READ(PORT_HOTPLUG_STAT) & bit) != 0)
  177. return intel_hdmi_edid_detect(connector);
  178. else
  179. return connector_status_disconnected;
  180. }
  181. static int intel_hdmi_get_modes(struct drm_connector *connector)
  182. {
  183. struct intel_output *intel_output = to_intel_output(connector);
  184. /* We should parse the EDID data and find out if it's an HDMI sink so
  185. * we can send audio to it.
  186. */
  187. return intel_ddc_get_modes(intel_output);
  188. }
  189. static void intel_hdmi_destroy(struct drm_connector *connector)
  190. {
  191. struct intel_output *intel_output = to_intel_output(connector);
  192. if (intel_output->i2c_bus)
  193. intel_i2c_destroy(intel_output->i2c_bus);
  194. drm_sysfs_connector_remove(connector);
  195. drm_connector_cleanup(connector);
  196. kfree(intel_output);
  197. }
  198. static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
  199. .dpms = intel_hdmi_dpms,
  200. .mode_fixup = intel_hdmi_mode_fixup,
  201. .prepare = intel_encoder_prepare,
  202. .mode_set = intel_hdmi_mode_set,
  203. .commit = intel_encoder_commit,
  204. };
  205. static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
  206. .dpms = drm_helper_connector_dpms,
  207. .save = intel_hdmi_save,
  208. .restore = intel_hdmi_restore,
  209. .detect = intel_hdmi_detect,
  210. .fill_modes = drm_helper_probe_single_connector_modes,
  211. .destroy = intel_hdmi_destroy,
  212. };
  213. static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
  214. .get_modes = intel_hdmi_get_modes,
  215. .mode_valid = intel_hdmi_mode_valid,
  216. .best_encoder = intel_best_encoder,
  217. };
  218. static void intel_hdmi_enc_destroy(struct drm_encoder *encoder)
  219. {
  220. drm_encoder_cleanup(encoder);
  221. }
  222. static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
  223. .destroy = intel_hdmi_enc_destroy,
  224. };
  225. void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
  226. {
  227. struct drm_i915_private *dev_priv = dev->dev_private;
  228. struct drm_connector *connector;
  229. struct intel_output *intel_output;
  230. struct intel_hdmi_priv *hdmi_priv;
  231. intel_output = kcalloc(sizeof(struct intel_output) +
  232. sizeof(struct intel_hdmi_priv), 1, GFP_KERNEL);
  233. if (!intel_output)
  234. return;
  235. hdmi_priv = (struct intel_hdmi_priv *)(intel_output + 1);
  236. connector = &intel_output->base;
  237. drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
  238. DRM_MODE_CONNECTOR_DVID);
  239. drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
  240. intel_output->type = INTEL_OUTPUT_HDMI;
  241. connector->interlace_allowed = 0;
  242. connector->doublescan_allowed = 0;
  243. /* Set up the DDC bus. */
  244. if (sdvox_reg == SDVOB)
  245. intel_output->ddc_bus = intel_i2c_create(dev, GPIOE, "HDMIB");
  246. else if (sdvox_reg == SDVOC)
  247. intel_output->ddc_bus = intel_i2c_create(dev, GPIOD, "HDMIC");
  248. else if (sdvox_reg == HDMIB)
  249. intel_output->ddc_bus = intel_i2c_create(dev, PCH_GPIOE,
  250. "HDMIB");
  251. else if (sdvox_reg == HDMIC)
  252. intel_output->ddc_bus = intel_i2c_create(dev, PCH_GPIOD,
  253. "HDMIC");
  254. else if (sdvox_reg == HDMID)
  255. intel_output->ddc_bus = intel_i2c_create(dev, PCH_GPIOF,
  256. "HDMID");
  257. if (!intel_output->ddc_bus)
  258. goto err_connector;
  259. hdmi_priv->sdvox_reg = sdvox_reg;
  260. intel_output->dev_priv = hdmi_priv;
  261. drm_encoder_init(dev, &intel_output->enc, &intel_hdmi_enc_funcs,
  262. DRM_MODE_ENCODER_TMDS);
  263. drm_encoder_helper_add(&intel_output->enc, &intel_hdmi_helper_funcs);
  264. drm_mode_connector_attach_encoder(&intel_output->base,
  265. &intel_output->enc);
  266. drm_sysfs_connector_add(connector);
  267. /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
  268. * 0xd. Failure to do so will result in spurious interrupts being
  269. * generated on the port when a cable is not attached.
  270. */
  271. if (IS_G4X(dev) && !IS_GM45(dev)) {
  272. u32 temp = I915_READ(PEG_BAND_GAP_DATA);
  273. I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
  274. }
  275. return;
  276. err_connector:
  277. drm_connector_cleanup(connector);
  278. kfree(intel_output);
  279. return;
  280. }