intel_hdmi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. temp = I915_READ(hdmi_priv->sdvox_reg);
  72. /* HW workaround, need to toggle enable bit off and on for 12bpc, but
  73. * we do this anyway which shows more stable in testing.
  74. */
  75. if (IS_IRONLAKE(dev)) {
  76. I915_WRITE(hdmi_priv->sdvox_reg, temp & ~SDVO_ENABLE);
  77. POSTING_READ(hdmi_priv->sdvox_reg);
  78. }
  79. if (mode != DRM_MODE_DPMS_ON) {
  80. temp &= ~SDVO_ENABLE;
  81. } else {
  82. temp |= SDVO_ENABLE;
  83. }
  84. I915_WRITE(hdmi_priv->sdvox_reg, temp);
  85. POSTING_READ(hdmi_priv->sdvox_reg);
  86. /* HW workaround, need to write this twice for issue that may result
  87. * in first write getting masked.
  88. */
  89. if (IS_IRONLAKE(dev)) {
  90. I915_WRITE(hdmi_priv->sdvox_reg, temp);
  91. POSTING_READ(hdmi_priv->sdvox_reg);
  92. }
  93. }
  94. static void intel_hdmi_save(struct drm_connector *connector)
  95. {
  96. struct drm_device *dev = connector->dev;
  97. struct drm_i915_private *dev_priv = dev->dev_private;
  98. struct intel_output *intel_output = to_intel_output(connector);
  99. struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
  100. hdmi_priv->save_SDVOX = I915_READ(hdmi_priv->sdvox_reg);
  101. }
  102. static void intel_hdmi_restore(struct drm_connector *connector)
  103. {
  104. struct drm_device *dev = connector->dev;
  105. struct drm_i915_private *dev_priv = dev->dev_private;
  106. struct intel_output *intel_output = to_intel_output(connector);
  107. struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
  108. I915_WRITE(hdmi_priv->sdvox_reg, hdmi_priv->save_SDVOX);
  109. POSTING_READ(hdmi_priv->sdvox_reg);
  110. }
  111. static int intel_hdmi_mode_valid(struct drm_connector *connector,
  112. struct drm_display_mode *mode)
  113. {
  114. if (mode->clock > 165000)
  115. return MODE_CLOCK_HIGH;
  116. if (mode->clock < 20000)
  117. return MODE_CLOCK_HIGH;
  118. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  119. return MODE_NO_DBLESCAN;
  120. return MODE_OK;
  121. }
  122. static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
  123. struct drm_display_mode *mode,
  124. struct drm_display_mode *adjusted_mode)
  125. {
  126. return true;
  127. }
  128. static enum drm_connector_status
  129. intel_hdmi_detect(struct drm_connector *connector)
  130. {
  131. struct intel_output *intel_output = to_intel_output(connector);
  132. struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
  133. struct edid *edid = NULL;
  134. enum drm_connector_status status = connector_status_disconnected;
  135. hdmi_priv->has_hdmi_sink = false;
  136. edid = drm_get_edid(&intel_output->base,
  137. intel_output->ddc_bus);
  138. if (edid) {
  139. if (edid->input & DRM_EDID_INPUT_DIGITAL) {
  140. status = connector_status_connected;
  141. hdmi_priv->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
  142. }
  143. intel_output->base.display_info.raw_edid = NULL;
  144. kfree(edid);
  145. }
  146. return status;
  147. }
  148. static int intel_hdmi_get_modes(struct drm_connector *connector)
  149. {
  150. struct intel_output *intel_output = to_intel_output(connector);
  151. /* We should parse the EDID data and find out if it's an HDMI sink so
  152. * we can send audio to it.
  153. */
  154. return intel_ddc_get_modes(intel_output);
  155. }
  156. static void intel_hdmi_destroy(struct drm_connector *connector)
  157. {
  158. struct intel_output *intel_output = to_intel_output(connector);
  159. if (intel_output->i2c_bus)
  160. intel_i2c_destroy(intel_output->i2c_bus);
  161. drm_sysfs_connector_remove(connector);
  162. drm_connector_cleanup(connector);
  163. kfree(intel_output);
  164. }
  165. static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
  166. .dpms = intel_hdmi_dpms,
  167. .mode_fixup = intel_hdmi_mode_fixup,
  168. .prepare = intel_encoder_prepare,
  169. .mode_set = intel_hdmi_mode_set,
  170. .commit = intel_encoder_commit,
  171. };
  172. static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
  173. .dpms = drm_helper_connector_dpms,
  174. .save = intel_hdmi_save,
  175. .restore = intel_hdmi_restore,
  176. .detect = intel_hdmi_detect,
  177. .fill_modes = drm_helper_probe_single_connector_modes,
  178. .destroy = intel_hdmi_destroy,
  179. };
  180. static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
  181. .get_modes = intel_hdmi_get_modes,
  182. .mode_valid = intel_hdmi_mode_valid,
  183. .best_encoder = intel_best_encoder,
  184. };
  185. static void intel_hdmi_enc_destroy(struct drm_encoder *encoder)
  186. {
  187. drm_encoder_cleanup(encoder);
  188. }
  189. static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
  190. .destroy = intel_hdmi_enc_destroy,
  191. };
  192. /*
  193. * Enumerate the child dev array parsed from VBT to check whether
  194. * the given HDMI is present.
  195. * If it is present, return 1.
  196. * If it is not present, return false.
  197. * If no child dev is parsed from VBT, it assumes that the given
  198. * HDMI is present.
  199. */
  200. static int hdmi_is_present_in_vbt(struct drm_device *dev, int hdmi_reg)
  201. {
  202. struct drm_i915_private *dev_priv = dev->dev_private;
  203. struct child_device_config *p_child;
  204. int i, hdmi_port, ret;
  205. if (!dev_priv->child_dev_num)
  206. return 1;
  207. if (hdmi_reg == SDVOB)
  208. hdmi_port = DVO_B;
  209. else if (hdmi_reg == SDVOC)
  210. hdmi_port = DVO_C;
  211. else if (hdmi_reg == HDMIB)
  212. hdmi_port = DVO_B;
  213. else if (hdmi_reg == HDMIC)
  214. hdmi_port = DVO_C;
  215. else if (hdmi_reg == HDMID)
  216. hdmi_port = DVO_D;
  217. else
  218. return 0;
  219. ret = 0;
  220. for (i = 0; i < dev_priv->child_dev_num; i++) {
  221. p_child = dev_priv->child_dev + i;
  222. /*
  223. * If the device type is not HDMI, continue.
  224. */
  225. if (p_child->device_type != DEVICE_TYPE_HDMI)
  226. continue;
  227. /* Find the HDMI port */
  228. if (p_child->dvo_port == hdmi_port) {
  229. ret = 1;
  230. break;
  231. }
  232. }
  233. return ret;
  234. }
  235. void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
  236. {
  237. struct drm_i915_private *dev_priv = dev->dev_private;
  238. struct drm_connector *connector;
  239. struct intel_output *intel_output;
  240. struct intel_hdmi_priv *hdmi_priv;
  241. if (!hdmi_is_present_in_vbt(dev, sdvox_reg)) {
  242. DRM_DEBUG_KMS("HDMI is not present. Ignored it \n");
  243. return;
  244. }
  245. intel_output = kcalloc(sizeof(struct intel_output) +
  246. sizeof(struct intel_hdmi_priv), 1, GFP_KERNEL);
  247. if (!intel_output)
  248. return;
  249. hdmi_priv = (struct intel_hdmi_priv *)(intel_output + 1);
  250. connector = &intel_output->base;
  251. drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
  252. DRM_MODE_CONNECTOR_HDMIA);
  253. drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
  254. intel_output->type = INTEL_OUTPUT_HDMI;
  255. connector->interlace_allowed = 0;
  256. connector->doublescan_allowed = 0;
  257. intel_output->crtc_mask = (1 << 0) | (1 << 1);
  258. /* Set up the DDC bus. */
  259. if (sdvox_reg == SDVOB) {
  260. intel_output->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
  261. intel_output->ddc_bus = intel_i2c_create(dev, GPIOE, "HDMIB");
  262. dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
  263. } else if (sdvox_reg == SDVOC) {
  264. intel_output->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
  265. intel_output->ddc_bus = intel_i2c_create(dev, GPIOD, "HDMIC");
  266. dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
  267. } else if (sdvox_reg == HDMIB) {
  268. intel_output->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
  269. intel_output->ddc_bus = intel_i2c_create(dev, PCH_GPIOE,
  270. "HDMIB");
  271. dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
  272. } else if (sdvox_reg == HDMIC) {
  273. intel_output->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
  274. intel_output->ddc_bus = intel_i2c_create(dev, PCH_GPIOD,
  275. "HDMIC");
  276. dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
  277. } else if (sdvox_reg == HDMID) {
  278. intel_output->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
  279. intel_output->ddc_bus = intel_i2c_create(dev, PCH_GPIOF,
  280. "HDMID");
  281. dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
  282. }
  283. if (!intel_output->ddc_bus)
  284. goto err_connector;
  285. hdmi_priv->sdvox_reg = sdvox_reg;
  286. intel_output->dev_priv = hdmi_priv;
  287. drm_encoder_init(dev, &intel_output->enc, &intel_hdmi_enc_funcs,
  288. DRM_MODE_ENCODER_TMDS);
  289. drm_encoder_helper_add(&intel_output->enc, &intel_hdmi_helper_funcs);
  290. drm_mode_connector_attach_encoder(&intel_output->base,
  291. &intel_output->enc);
  292. drm_sysfs_connector_add(connector);
  293. /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
  294. * 0xd. Failure to do so will result in spurious interrupts being
  295. * generated on the port when a cable is not attached.
  296. */
  297. if (IS_G4X(dev) && !IS_GM45(dev)) {
  298. u32 temp = I915_READ(PEG_BAND_GAP_DATA);
  299. I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
  300. }
  301. return;
  302. err_connector:
  303. drm_connector_cleanup(connector);
  304. kfree(intel_output);
  305. return;
  306. }