intel_hdmi.c 13 KB

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