intel_hdmi.c 9.6 KB

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