intel_hdmi.c 13 KB

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