intel_audio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * Copyright © 2014 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <linux/kernel.h>
  24. #include <drm/drmP.h>
  25. #include <drm/drm_edid.h>
  26. #include "intel_drv.h"
  27. #include "i915_drv.h"
  28. /**
  29. * DOC: High Definition Audio over HDMI and Display Port
  30. *
  31. * The graphics and audio drivers together support High Definition Audio over
  32. * HDMI and Display Port. The audio programming sequences are divided into audio
  33. * codec and controller enable and disable sequences. The graphics driver
  34. * handles the audio codec sequences, while the audio driver handles the audio
  35. * controller sequences.
  36. *
  37. * The disable sequences must be performed before disabling the transcoder or
  38. * port. The enable sequences may only be performed after enabling the
  39. * transcoder and port, and after completed link training.
  40. *
  41. * The codec and controller sequences could be done either parallel or serial,
  42. * but generally the ELDV/PD change in the codec sequence indicates to the audio
  43. * driver that the controller sequence should start. Indeed, most of the
  44. * co-operation between the graphics and audio drivers is handled via audio
  45. * related registers. (The notable exception is the power management, not
  46. * covered here.)
  47. */
  48. static const struct {
  49. int clock;
  50. u32 config;
  51. } hdmi_audio_clock[] = {
  52. { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  53. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  54. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  55. { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  56. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  57. { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  58. { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  59. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  60. { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  61. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  62. };
  63. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  64. static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
  65. {
  66. int i;
  67. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  68. if (mode->clock == hdmi_audio_clock[i].clock)
  69. break;
  70. }
  71. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  72. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
  73. i = 1;
  74. }
  75. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  76. hdmi_audio_clock[i].clock,
  77. hdmi_audio_clock[i].config);
  78. return hdmi_audio_clock[i].config;
  79. }
  80. static bool intel_eld_uptodate(struct drm_connector *connector,
  81. int reg_eldv, uint32_t bits_eldv,
  82. int reg_elda, uint32_t bits_elda,
  83. int reg_edid)
  84. {
  85. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  86. uint8_t *eld = connector->eld;
  87. uint32_t tmp;
  88. int i;
  89. tmp = I915_READ(reg_eldv);
  90. tmp &= bits_eldv;
  91. if (!tmp)
  92. return false;
  93. tmp = I915_READ(reg_elda);
  94. tmp &= ~bits_elda;
  95. I915_WRITE(reg_elda, tmp);
  96. for (i = 0; i < drm_eld_size(eld) / 4; i++)
  97. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  98. return false;
  99. return true;
  100. }
  101. static void g4x_audio_codec_disable(struct intel_encoder *encoder)
  102. {
  103. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  104. uint32_t eldv, tmp;
  105. DRM_DEBUG_KMS("Disable audio codec\n");
  106. tmp = I915_READ(G4X_AUD_VID_DID);
  107. if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
  108. eldv = G4X_ELDV_DEVCL_DEVBLC;
  109. else
  110. eldv = G4X_ELDV_DEVCTG;
  111. /* Invalidate ELD */
  112. tmp = I915_READ(G4X_AUD_CNTL_ST);
  113. tmp &= ~eldv;
  114. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  115. }
  116. static void g4x_audio_codec_enable(struct drm_connector *connector,
  117. struct intel_encoder *encoder,
  118. struct drm_display_mode *mode)
  119. {
  120. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  121. uint8_t *eld = connector->eld;
  122. uint32_t eldv;
  123. uint32_t tmp;
  124. int len, i;
  125. DRM_DEBUG_KMS("Enable audio codec, %u bytes ELD\n", eld[2]);
  126. tmp = I915_READ(G4X_AUD_VID_DID);
  127. if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
  128. eldv = G4X_ELDV_DEVCL_DEVBLC;
  129. else
  130. eldv = G4X_ELDV_DEVCTG;
  131. if (intel_eld_uptodate(connector,
  132. G4X_AUD_CNTL_ST, eldv,
  133. G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
  134. G4X_HDMIW_HDMIEDID))
  135. return;
  136. tmp = I915_READ(G4X_AUD_CNTL_ST);
  137. tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
  138. len = (tmp >> 9) & 0x1f; /* ELD buffer size */
  139. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  140. len = min(drm_eld_size(eld) / 4, len);
  141. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  142. for (i = 0; i < len; i++)
  143. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  144. tmp = I915_READ(G4X_AUD_CNTL_ST);
  145. tmp |= eldv;
  146. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  147. }
  148. static void hsw_audio_codec_disable(struct intel_encoder *encoder)
  149. {
  150. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  151. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  152. enum pipe pipe = intel_crtc->pipe;
  153. uint32_t tmp;
  154. DRM_DEBUG_KMS("Disable audio codec on pipe %c\n", pipe_name(pipe));
  155. /* Disable timestamps */
  156. tmp = I915_READ(HSW_AUD_CFG(pipe));
  157. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  158. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  159. tmp &= ~AUD_CONFIG_UPPER_N_MASK;
  160. tmp &= ~AUD_CONFIG_LOWER_N_MASK;
  161. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  162. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  163. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  164. /* Invalidate ELD */
  165. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  166. tmp &= ~AUDIO_ELD_VALID(pipe);
  167. tmp &= ~AUDIO_OUTPUT_ENABLE(pipe);
  168. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  169. }
  170. static void hsw_audio_codec_enable(struct drm_connector *connector,
  171. struct intel_encoder *encoder,
  172. struct drm_display_mode *mode)
  173. {
  174. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  175. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  176. enum pipe pipe = intel_crtc->pipe;
  177. const uint8_t *eld = connector->eld;
  178. uint32_t tmp;
  179. int len, i;
  180. DRM_DEBUG_KMS("Enable audio codec on pipe %c, %u bytes ELD\n",
  181. pipe_name(pipe), drm_eld_size(eld));
  182. /* Enable audio presence detect, invalidate ELD */
  183. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  184. tmp |= AUDIO_OUTPUT_ENABLE(pipe);
  185. tmp &= ~AUDIO_ELD_VALID(pipe);
  186. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  187. /*
  188. * FIXME: We're supposed to wait for vblank here, but we have vblanks
  189. * disabled during the mode set. The proper fix would be to push the
  190. * rest of the setup into a vblank work item, queued here, but the
  191. * infrastructure is not there yet.
  192. */
  193. /* Reset ELD write address */
  194. tmp = I915_READ(HSW_AUD_DIP_ELD_CTRL(pipe));
  195. tmp &= ~IBX_ELD_ADDRESS_MASK;
  196. I915_WRITE(HSW_AUD_DIP_ELD_CTRL(pipe), tmp);
  197. /* Up to 84 bytes of hw ELD buffer */
  198. len = min(drm_eld_size(eld), 84);
  199. for (i = 0; i < len / 4; i++)
  200. I915_WRITE(HSW_AUD_EDID_DATA(pipe), *((uint32_t *)eld + i));
  201. /* ELD valid */
  202. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  203. tmp |= AUDIO_ELD_VALID(pipe);
  204. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  205. /* Enable timestamps */
  206. tmp = I915_READ(HSW_AUD_CFG(pipe));
  207. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  208. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  209. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  210. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  211. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  212. else
  213. tmp |= audio_config_hdmi_pixel_clock(mode);
  214. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  215. }
  216. static void ilk_audio_codec_disable(struct intel_encoder *encoder)
  217. {
  218. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  219. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  220. struct intel_digital_port *intel_dig_port =
  221. enc_to_dig_port(&encoder->base);
  222. enum port port = intel_dig_port->port;
  223. enum pipe pipe = intel_crtc->pipe;
  224. uint32_t tmp, eldv;
  225. int aud_config;
  226. int aud_cntrl_st2;
  227. DRM_DEBUG_KMS("Disable audio codec on port %c, pipe %c\n",
  228. port_name(port), pipe_name(pipe));
  229. if (HAS_PCH_IBX(dev_priv->dev)) {
  230. aud_config = IBX_AUD_CFG(pipe);
  231. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  232. } else if (IS_VALLEYVIEW(dev_priv)) {
  233. aud_config = VLV_AUD_CFG(pipe);
  234. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  235. } else {
  236. aud_config = CPT_AUD_CFG(pipe);
  237. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  238. }
  239. /* Disable timestamps */
  240. tmp = I915_READ(aud_config);
  241. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  242. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  243. tmp &= ~AUD_CONFIG_UPPER_N_MASK;
  244. tmp &= ~AUD_CONFIG_LOWER_N_MASK;
  245. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  246. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  247. I915_WRITE(aud_config, tmp);
  248. if (WARN_ON(!port)) {
  249. eldv = IBX_ELD_VALID(PORT_B) | IBX_ELD_VALID(PORT_C) |
  250. IBX_ELD_VALID(PORT_D);
  251. } else {
  252. eldv = IBX_ELD_VALID(port);
  253. }
  254. /* Invalidate ELD */
  255. tmp = I915_READ(aud_cntrl_st2);
  256. tmp &= ~eldv;
  257. I915_WRITE(aud_cntrl_st2, tmp);
  258. }
  259. static void ilk_audio_codec_enable(struct drm_connector *connector,
  260. struct intel_encoder *encoder,
  261. struct drm_display_mode *mode)
  262. {
  263. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  264. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  265. struct intel_digital_port *intel_dig_port =
  266. enc_to_dig_port(&encoder->base);
  267. enum port port = intel_dig_port->port;
  268. enum pipe pipe = intel_crtc->pipe;
  269. uint8_t *eld = connector->eld;
  270. uint32_t eldv;
  271. uint32_t tmp;
  272. int len, i;
  273. int hdmiw_hdmiedid;
  274. int aud_config;
  275. int aud_cntl_st;
  276. int aud_cntrl_st2;
  277. DRM_DEBUG_KMS("Enable audio codec on port %c, pipe %c, %u bytes ELD\n",
  278. port_name(port), pipe_name(pipe), drm_eld_size(eld));
  279. /*
  280. * FIXME: We're supposed to wait for vblank here, but we have vblanks
  281. * disabled during the mode set. The proper fix would be to push the
  282. * rest of the setup into a vblank work item, queued here, but the
  283. * infrastructure is not there yet.
  284. */
  285. if (HAS_PCH_IBX(connector->dev)) {
  286. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  287. aud_config = IBX_AUD_CFG(pipe);
  288. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  289. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  290. } else if (IS_VALLEYVIEW(connector->dev)) {
  291. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  292. aud_config = VLV_AUD_CFG(pipe);
  293. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  294. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  295. } else {
  296. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  297. aud_config = CPT_AUD_CFG(pipe);
  298. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  299. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  300. }
  301. if (WARN_ON(!port)) {
  302. eldv = IBX_ELD_VALID(PORT_B) | IBX_ELD_VALID(PORT_C) |
  303. IBX_ELD_VALID(PORT_D);
  304. } else {
  305. eldv = IBX_ELD_VALID(port);
  306. }
  307. /* Invalidate ELD */
  308. tmp = I915_READ(aud_cntrl_st2);
  309. tmp &= ~eldv;
  310. I915_WRITE(aud_cntrl_st2, tmp);
  311. /* Reset ELD write address */
  312. tmp = I915_READ(aud_cntl_st);
  313. tmp &= ~IBX_ELD_ADDRESS_MASK;
  314. I915_WRITE(aud_cntl_st, tmp);
  315. /* Up to 84 bytes of hw ELD buffer */
  316. len = min(drm_eld_size(eld), 84);
  317. for (i = 0; i < len / 4; i++)
  318. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  319. /* ELD valid */
  320. tmp = I915_READ(aud_cntrl_st2);
  321. tmp |= eldv;
  322. I915_WRITE(aud_cntrl_st2, tmp);
  323. /* Enable timestamps */
  324. tmp = I915_READ(aud_config);
  325. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  326. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  327. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  328. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  329. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  330. else
  331. tmp |= audio_config_hdmi_pixel_clock(mode);
  332. I915_WRITE(aud_config, tmp);
  333. }
  334. /**
  335. * intel_audio_codec_enable - Enable the audio codec for HD audio
  336. * @intel_encoder: encoder on which to enable audio
  337. *
  338. * The enable sequences may only be performed after enabling the transcoder and
  339. * port, and after completed link training.
  340. */
  341. void intel_audio_codec_enable(struct intel_encoder *intel_encoder)
  342. {
  343. struct drm_encoder *encoder = &intel_encoder->base;
  344. struct intel_crtc *crtc = to_intel_crtc(encoder->crtc);
  345. struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  346. struct drm_connector *connector;
  347. struct drm_device *dev = encoder->dev;
  348. struct drm_i915_private *dev_priv = dev->dev_private;
  349. connector = drm_select_eld(encoder, mode);
  350. if (!connector)
  351. return;
  352. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  353. connector->base.id,
  354. connector->name,
  355. connector->encoder->base.id,
  356. connector->encoder->name);
  357. /* ELD Conn_Type */
  358. connector->eld[5] &= ~(3 << 2);
  359. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  360. connector->eld[5] |= (1 << 2);
  361. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  362. if (dev_priv->display.audio_codec_enable)
  363. dev_priv->display.audio_codec_enable(connector, intel_encoder, mode);
  364. }
  365. /**
  366. * intel_audio_codec_disable - Disable the audio codec for HD audio
  367. * @encoder: encoder on which to disable audio
  368. *
  369. * The disable sequences must be performed before disabling the transcoder or
  370. * port.
  371. */
  372. void intel_audio_codec_disable(struct intel_encoder *encoder)
  373. {
  374. struct drm_device *dev = encoder->base.dev;
  375. struct drm_i915_private *dev_priv = dev->dev_private;
  376. if (dev_priv->display.audio_codec_disable)
  377. dev_priv->display.audio_codec_disable(encoder);
  378. }
  379. /**
  380. * intel_init_audio - Set up chip specific audio functions
  381. * @dev: drm device
  382. */
  383. void intel_init_audio(struct drm_device *dev)
  384. {
  385. struct drm_i915_private *dev_priv = dev->dev_private;
  386. if (IS_G4X(dev)) {
  387. dev_priv->display.audio_codec_enable = g4x_audio_codec_enable;
  388. dev_priv->display.audio_codec_disable = g4x_audio_codec_disable;
  389. } else if (IS_VALLEYVIEW(dev)) {
  390. dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
  391. dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
  392. } else if (IS_HASWELL(dev) || INTEL_INFO(dev)->gen >= 8) {
  393. dev_priv->display.audio_codec_enable = hsw_audio_codec_enable;
  394. dev_priv->display.audio_codec_disable = hsw_audio_codec_disable;
  395. } else if (HAS_PCH_SPLIT(dev)) {
  396. dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
  397. dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
  398. }
  399. }