intel_hdmi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 {
  39. struct intel_encoder base;
  40. u32 sdvox_reg;
  41. int ddc_bus;
  42. bool has_hdmi_sink;
  43. bool has_audio;
  44. int force_audio;
  45. struct drm_property *force_audio_property;
  46. };
  47. static struct intel_hdmi *enc_to_intel_hdmi(struct drm_encoder *encoder)
  48. {
  49. return container_of(encoder, struct intel_hdmi, base.base);
  50. }
  51. static struct intel_hdmi *intel_attached_hdmi(struct drm_connector *connector)
  52. {
  53. return container_of(intel_attached_encoder(connector),
  54. struct intel_hdmi, base);
  55. }
  56. void intel_dip_infoframe_csum(struct dip_infoframe *avi_if)
  57. {
  58. uint8_t *data = (uint8_t *)avi_if;
  59. uint8_t sum = 0;
  60. unsigned i;
  61. avi_if->checksum = 0;
  62. avi_if->ecc = 0;
  63. for (i = 0; i < sizeof(*avi_if); i++)
  64. sum += data[i];
  65. avi_if->checksum = 0x100 - sum;
  66. }
  67. static void intel_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
  68. {
  69. struct dip_infoframe avi_if = {
  70. .type = DIP_TYPE_AVI,
  71. .ver = DIP_VERSION_AVI,
  72. .len = DIP_LEN_AVI,
  73. };
  74. uint32_t *data = (uint32_t *)&avi_if;
  75. struct drm_device *dev = encoder->dev;
  76. struct drm_i915_private *dev_priv = dev->dev_private;
  77. struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
  78. u32 port;
  79. unsigned i;
  80. if (!intel_hdmi->has_hdmi_sink)
  81. return;
  82. /* XXX first guess at handling video port, is this corrent? */
  83. if (intel_hdmi->sdvox_reg == SDVOB)
  84. port = VIDEO_DIP_PORT_B;
  85. else if (intel_hdmi->sdvox_reg == SDVOC)
  86. port = VIDEO_DIP_PORT_C;
  87. else
  88. return;
  89. I915_WRITE(VIDEO_DIP_CTL, VIDEO_DIP_ENABLE | port |
  90. VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC);
  91. intel_dip_infoframe_csum(&avi_if);
  92. for (i = 0; i < sizeof(avi_if); i += 4) {
  93. I915_WRITE(VIDEO_DIP_DATA, *data);
  94. data++;
  95. }
  96. I915_WRITE(VIDEO_DIP_CTL, VIDEO_DIP_ENABLE | port |
  97. VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC |
  98. VIDEO_DIP_ENABLE_AVI);
  99. }
  100. static void intel_hdmi_mode_set(struct drm_encoder *encoder,
  101. struct drm_display_mode *mode,
  102. struct drm_display_mode *adjusted_mode)
  103. {
  104. struct drm_device *dev = encoder->dev;
  105. struct drm_i915_private *dev_priv = dev->dev_private;
  106. struct drm_crtc *crtc = encoder->crtc;
  107. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  108. struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
  109. u32 sdvox;
  110. sdvox = SDVO_ENCODING_HDMI | SDVO_BORDER_ENABLE;
  111. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  112. sdvox |= SDVO_VSYNC_ACTIVE_HIGH;
  113. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  114. sdvox |= SDVO_HSYNC_ACTIVE_HIGH;
  115. /* Required on CPT */
  116. if (intel_hdmi->has_hdmi_sink && HAS_PCH_CPT(dev))
  117. sdvox |= HDMI_MODE_SELECT;
  118. if (intel_hdmi->has_audio) {
  119. sdvox |= SDVO_AUDIO_ENABLE;
  120. sdvox |= SDVO_NULL_PACKETS_DURING_VSYNC;
  121. }
  122. if (intel_crtc->pipe == 1) {
  123. if (HAS_PCH_CPT(dev))
  124. sdvox |= PORT_TRANS_B_SEL_CPT;
  125. else
  126. sdvox |= SDVO_PIPE_B_SELECT;
  127. }
  128. I915_WRITE(intel_hdmi->sdvox_reg, sdvox);
  129. POSTING_READ(intel_hdmi->sdvox_reg);
  130. intel_hdmi_set_avi_infoframe(encoder);
  131. }
  132. static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
  133. {
  134. struct drm_device *dev = encoder->dev;
  135. struct drm_i915_private *dev_priv = dev->dev_private;
  136. struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
  137. u32 temp;
  138. temp = I915_READ(intel_hdmi->sdvox_reg);
  139. /* HW workaround, need to toggle enable bit off and on for 12bpc, but
  140. * we do this anyway which shows more stable in testing.
  141. */
  142. if (HAS_PCH_SPLIT(dev)) {
  143. I915_WRITE(intel_hdmi->sdvox_reg, temp & ~SDVO_ENABLE);
  144. POSTING_READ(intel_hdmi->sdvox_reg);
  145. }
  146. if (mode != DRM_MODE_DPMS_ON) {
  147. temp &= ~SDVO_ENABLE;
  148. } else {
  149. temp |= SDVO_ENABLE;
  150. }
  151. I915_WRITE(intel_hdmi->sdvox_reg, temp);
  152. POSTING_READ(intel_hdmi->sdvox_reg);
  153. /* HW workaround, need to write this twice for issue that may result
  154. * in first write getting masked.
  155. */
  156. if (HAS_PCH_SPLIT(dev)) {
  157. I915_WRITE(intel_hdmi->sdvox_reg, temp);
  158. POSTING_READ(intel_hdmi->sdvox_reg);
  159. }
  160. }
  161. static int intel_hdmi_mode_valid(struct drm_connector *connector,
  162. struct drm_display_mode *mode)
  163. {
  164. if (mode->clock > 165000)
  165. return MODE_CLOCK_HIGH;
  166. if (mode->clock < 20000)
  167. return MODE_CLOCK_HIGH;
  168. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  169. return MODE_NO_DBLESCAN;
  170. return MODE_OK;
  171. }
  172. static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
  173. struct drm_display_mode *mode,
  174. struct drm_display_mode *adjusted_mode)
  175. {
  176. return true;
  177. }
  178. static enum drm_connector_status
  179. intel_hdmi_detect(struct drm_connector *connector, bool force)
  180. {
  181. struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
  182. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  183. struct edid *edid;
  184. enum drm_connector_status status = connector_status_disconnected;
  185. intel_hdmi->has_hdmi_sink = false;
  186. intel_hdmi->has_audio = false;
  187. edid = drm_get_edid(connector,
  188. &dev_priv->gmbus[intel_hdmi->ddc_bus].adapter);
  189. if (edid) {
  190. if (edid->input & DRM_EDID_INPUT_DIGITAL) {
  191. status = connector_status_connected;
  192. intel_hdmi->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
  193. intel_hdmi->has_audio = drm_detect_monitor_audio(edid);
  194. }
  195. connector->display_info.raw_edid = NULL;
  196. kfree(edid);
  197. }
  198. if (status == connector_status_connected) {
  199. if (intel_hdmi->force_audio)
  200. intel_hdmi->has_audio = intel_hdmi->force_audio > 0;
  201. }
  202. return status;
  203. }
  204. static int intel_hdmi_get_modes(struct drm_connector *connector)
  205. {
  206. struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
  207. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  208. /* We should parse the EDID data and find out if it's an HDMI sink so
  209. * we can send audio to it.
  210. */
  211. return intel_ddc_get_modes(connector,
  212. &dev_priv->gmbus[intel_hdmi->ddc_bus].adapter);
  213. }
  214. static int
  215. intel_hdmi_set_property(struct drm_connector *connector,
  216. struct drm_property *property,
  217. uint64_t val)
  218. {
  219. struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
  220. int ret;
  221. ret = drm_connector_property_set_value(connector, property, val);
  222. if (ret)
  223. return ret;
  224. if (property == intel_hdmi->force_audio_property) {
  225. if (val == intel_hdmi->force_audio)
  226. return 0;
  227. intel_hdmi->force_audio = val;
  228. if (val > 0 && intel_hdmi->has_audio)
  229. return 0;
  230. if (val < 0 && !intel_hdmi->has_audio)
  231. return 0;
  232. intel_hdmi->has_audio = val > 0;
  233. goto done;
  234. }
  235. return -EINVAL;
  236. done:
  237. if (intel_hdmi->base.base.crtc) {
  238. struct drm_crtc *crtc = intel_hdmi->base.base.crtc;
  239. drm_crtc_helper_set_mode(crtc, &crtc->mode,
  240. crtc->x, crtc->y,
  241. crtc->fb);
  242. }
  243. return 0;
  244. }
  245. static void intel_hdmi_destroy(struct drm_connector *connector)
  246. {
  247. drm_sysfs_connector_remove(connector);
  248. drm_connector_cleanup(connector);
  249. kfree(connector);
  250. }
  251. static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
  252. .dpms = intel_hdmi_dpms,
  253. .mode_fixup = intel_hdmi_mode_fixup,
  254. .prepare = intel_encoder_prepare,
  255. .mode_set = intel_hdmi_mode_set,
  256. .commit = intel_encoder_commit,
  257. };
  258. static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
  259. .dpms = drm_helper_connector_dpms,
  260. .detect = intel_hdmi_detect,
  261. .fill_modes = drm_helper_probe_single_connector_modes,
  262. .set_property = intel_hdmi_set_property,
  263. .destroy = intel_hdmi_destroy,
  264. };
  265. static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
  266. .get_modes = intel_hdmi_get_modes,
  267. .mode_valid = intel_hdmi_mode_valid,
  268. .best_encoder = intel_best_encoder,
  269. };
  270. static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
  271. .destroy = intel_encoder_destroy,
  272. };
  273. static void
  274. intel_hdmi_add_properties(struct intel_hdmi *intel_hdmi, struct drm_connector *connector)
  275. {
  276. struct drm_device *dev = connector->dev;
  277. intel_hdmi->force_audio_property =
  278. drm_property_create(dev, DRM_MODE_PROP_RANGE, "force_audio", 2);
  279. if (intel_hdmi->force_audio_property) {
  280. intel_hdmi->force_audio_property->values[0] = -1;
  281. intel_hdmi->force_audio_property->values[1] = 1;
  282. drm_connector_attach_property(connector, intel_hdmi->force_audio_property, 0);
  283. }
  284. }
  285. void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
  286. {
  287. struct drm_i915_private *dev_priv = dev->dev_private;
  288. struct drm_connector *connector;
  289. struct intel_encoder *intel_encoder;
  290. struct intel_connector *intel_connector;
  291. struct intel_hdmi *intel_hdmi;
  292. intel_hdmi = kzalloc(sizeof(struct intel_hdmi), GFP_KERNEL);
  293. if (!intel_hdmi)
  294. return;
  295. intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
  296. if (!intel_connector) {
  297. kfree(intel_hdmi);
  298. return;
  299. }
  300. intel_encoder = &intel_hdmi->base;
  301. drm_encoder_init(dev, &intel_encoder->base, &intel_hdmi_enc_funcs,
  302. DRM_MODE_ENCODER_TMDS);
  303. connector = &intel_connector->base;
  304. drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
  305. DRM_MODE_CONNECTOR_HDMIA);
  306. drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
  307. intel_encoder->type = INTEL_OUTPUT_HDMI;
  308. connector->polled = DRM_CONNECTOR_POLL_HPD;
  309. connector->interlace_allowed = 0;
  310. connector->doublescan_allowed = 0;
  311. intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
  312. /* Set up the DDC bus. */
  313. if (sdvox_reg == SDVOB) {
  314. intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
  315. intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
  316. dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
  317. } else if (sdvox_reg == SDVOC) {
  318. intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
  319. intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
  320. dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
  321. } else if (sdvox_reg == HDMIB) {
  322. intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
  323. intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
  324. dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
  325. } else if (sdvox_reg == HDMIC) {
  326. intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
  327. intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
  328. dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
  329. } else if (sdvox_reg == HDMID) {
  330. intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
  331. intel_hdmi->ddc_bus = GMBUS_PORT_DPD;
  332. dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
  333. }
  334. intel_hdmi->sdvox_reg = sdvox_reg;
  335. drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs);
  336. intel_hdmi_add_properties(intel_hdmi, connector);
  337. intel_connector_attach_encoder(intel_connector, intel_encoder);
  338. drm_sysfs_connector_add(connector);
  339. /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
  340. * 0xd. Failure to do so will result in spurious interrupts being
  341. * generated on the port when a cable is not attached.
  342. */
  343. if (IS_G4X(dev) && !IS_GM45(dev)) {
  344. u32 temp = I915_READ(PEG_BAND_GAP_DATA);
  345. I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
  346. }
  347. }