intel_audio.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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 <linux/component.h>
  25. #include <drm/i915_component.h>
  26. #include "intel_drv.h"
  27. #include <drm/drmP.h>
  28. #include <drm/drm_edid.h>
  29. #include "i915_drv.h"
  30. /**
  31. * DOC: High Definition Audio over HDMI and Display Port
  32. *
  33. * The graphics and audio drivers together support High Definition Audio over
  34. * HDMI and Display Port. The audio programming sequences are divided into audio
  35. * codec and controller enable and disable sequences. The graphics driver
  36. * handles the audio codec sequences, while the audio driver handles the audio
  37. * controller sequences.
  38. *
  39. * The disable sequences must be performed before disabling the transcoder or
  40. * port. The enable sequences may only be performed after enabling the
  41. * transcoder and port, and after completed link training. Therefore the audio
  42. * enable/disable sequences are part of the modeset sequence.
  43. *
  44. * The codec and controller sequences could be done either parallel or serial,
  45. * but generally the ELDV/PD change in the codec sequence indicates to the audio
  46. * driver that the controller sequence should start. Indeed, most of the
  47. * co-operation between the graphics and audio drivers is handled via audio
  48. * related registers. (The notable exception is the power management, not
  49. * covered here.)
  50. *
  51. * The struct i915_audio_component is used to interact between the graphics
  52. * and audio drivers. The struct i915_audio_component_ops *ops in it is
  53. * defined in graphics driver and called in audio driver. The
  54. * struct i915_audio_component_audio_ops *audio_ops is called from i915 driver.
  55. */
  56. static const struct {
  57. int clock;
  58. u32 config;
  59. } hdmi_audio_clock[] = {
  60. { 25175, AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  61. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  62. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  63. { 27027, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  64. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  65. { 54054, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  66. { 74176, AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  67. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  68. { 148352, AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  69. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  70. };
  71. /* HDMI N/CTS table */
  72. #define TMDS_297M 297000
  73. #define TMDS_296M 296703
  74. static const struct {
  75. int sample_rate;
  76. int clock;
  77. int n;
  78. int cts;
  79. } aud_ncts[] = {
  80. { 44100, TMDS_296M, 4459, 234375 },
  81. { 44100, TMDS_297M, 4704, 247500 },
  82. { 48000, TMDS_296M, 5824, 281250 },
  83. { 48000, TMDS_297M, 5120, 247500 },
  84. { 32000, TMDS_296M, 5824, 421875 },
  85. { 32000, TMDS_297M, 3072, 222750 },
  86. { 88200, TMDS_296M, 8918, 234375 },
  87. { 88200, TMDS_297M, 9408, 247500 },
  88. { 96000, TMDS_296M, 11648, 281250 },
  89. { 96000, TMDS_297M, 10240, 247500 },
  90. { 176400, TMDS_296M, 17836, 234375 },
  91. { 176400, TMDS_297M, 18816, 247500 },
  92. { 192000, TMDS_296M, 23296, 281250 },
  93. { 192000, TMDS_297M, 20480, 247500 },
  94. };
  95. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  96. static u32 audio_config_hdmi_pixel_clock(const struct drm_display_mode *adjusted_mode)
  97. {
  98. int i;
  99. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  100. if (adjusted_mode->crtc_clock == hdmi_audio_clock[i].clock)
  101. break;
  102. }
  103. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  104. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n",
  105. adjusted_mode->crtc_clock);
  106. i = 1;
  107. }
  108. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  109. hdmi_audio_clock[i].clock,
  110. hdmi_audio_clock[i].config);
  111. return hdmi_audio_clock[i].config;
  112. }
  113. static int audio_config_get_n(const struct drm_display_mode *mode, int rate)
  114. {
  115. int i;
  116. for (i = 0; i < ARRAY_SIZE(aud_ncts); i++) {
  117. if ((rate == aud_ncts[i].sample_rate) &&
  118. (mode->clock == aud_ncts[i].clock)) {
  119. return aud_ncts[i].n;
  120. }
  121. }
  122. return 0;
  123. }
  124. static uint32_t audio_config_setup_n_reg(int n, uint32_t val)
  125. {
  126. int n_low, n_up;
  127. uint32_t tmp = val;
  128. n_low = n & 0xfff;
  129. n_up = (n >> 12) & 0xff;
  130. tmp &= ~(AUD_CONFIG_UPPER_N_MASK | AUD_CONFIG_LOWER_N_MASK);
  131. tmp |= ((n_up << AUD_CONFIG_UPPER_N_SHIFT) |
  132. (n_low << AUD_CONFIG_LOWER_N_SHIFT) |
  133. AUD_CONFIG_N_PROG_ENABLE);
  134. return tmp;
  135. }
  136. /* check whether N/CTS/M need be set manually */
  137. static bool audio_rate_need_prog(struct intel_crtc *crtc,
  138. const struct drm_display_mode *mode)
  139. {
  140. if (((mode->clock == TMDS_297M) ||
  141. (mode->clock == TMDS_296M)) &&
  142. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  143. return true;
  144. else
  145. return false;
  146. }
  147. static bool intel_eld_uptodate(struct drm_connector *connector,
  148. i915_reg_t reg_eldv, uint32_t bits_eldv,
  149. i915_reg_t reg_elda, uint32_t bits_elda,
  150. i915_reg_t reg_edid)
  151. {
  152. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  153. uint8_t *eld = connector->eld;
  154. uint32_t tmp;
  155. int i;
  156. tmp = I915_READ(reg_eldv);
  157. tmp &= bits_eldv;
  158. if (!tmp)
  159. return false;
  160. tmp = I915_READ(reg_elda);
  161. tmp &= ~bits_elda;
  162. I915_WRITE(reg_elda, tmp);
  163. for (i = 0; i < drm_eld_size(eld) / 4; i++)
  164. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  165. return false;
  166. return true;
  167. }
  168. static void g4x_audio_codec_disable(struct intel_encoder *encoder)
  169. {
  170. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  171. uint32_t eldv, tmp;
  172. DRM_DEBUG_KMS("Disable audio codec\n");
  173. tmp = I915_READ(G4X_AUD_VID_DID);
  174. if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
  175. eldv = G4X_ELDV_DEVCL_DEVBLC;
  176. else
  177. eldv = G4X_ELDV_DEVCTG;
  178. /* Invalidate ELD */
  179. tmp = I915_READ(G4X_AUD_CNTL_ST);
  180. tmp &= ~eldv;
  181. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  182. }
  183. static void g4x_audio_codec_enable(struct drm_connector *connector,
  184. struct intel_encoder *encoder,
  185. const struct drm_display_mode *adjusted_mode)
  186. {
  187. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  188. uint8_t *eld = connector->eld;
  189. uint32_t eldv;
  190. uint32_t tmp;
  191. int len, i;
  192. DRM_DEBUG_KMS("Enable audio codec, %u bytes ELD\n", eld[2]);
  193. tmp = I915_READ(G4X_AUD_VID_DID);
  194. if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
  195. eldv = G4X_ELDV_DEVCL_DEVBLC;
  196. else
  197. eldv = G4X_ELDV_DEVCTG;
  198. if (intel_eld_uptodate(connector,
  199. G4X_AUD_CNTL_ST, eldv,
  200. G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
  201. G4X_HDMIW_HDMIEDID))
  202. return;
  203. tmp = I915_READ(G4X_AUD_CNTL_ST);
  204. tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
  205. len = (tmp >> 9) & 0x1f; /* ELD buffer size */
  206. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  207. len = min(drm_eld_size(eld) / 4, len);
  208. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  209. for (i = 0; i < len; i++)
  210. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  211. tmp = I915_READ(G4X_AUD_CNTL_ST);
  212. tmp |= eldv;
  213. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  214. }
  215. static void hsw_audio_codec_disable(struct intel_encoder *encoder)
  216. {
  217. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  218. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  219. enum pipe pipe = intel_crtc->pipe;
  220. uint32_t tmp;
  221. DRM_DEBUG_KMS("Disable audio codec on pipe %c\n", pipe_name(pipe));
  222. mutex_lock(&dev_priv->av_mutex);
  223. /* Disable timestamps */
  224. tmp = I915_READ(HSW_AUD_CFG(pipe));
  225. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  226. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  227. tmp &= ~AUD_CONFIG_UPPER_N_MASK;
  228. tmp &= ~AUD_CONFIG_LOWER_N_MASK;
  229. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  230. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DP_MST))
  231. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  232. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  233. /* Invalidate ELD */
  234. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  235. tmp &= ~AUDIO_ELD_VALID(pipe);
  236. tmp &= ~AUDIO_OUTPUT_ENABLE(pipe);
  237. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  238. mutex_unlock(&dev_priv->av_mutex);
  239. }
  240. static void hsw_audio_codec_enable(struct drm_connector *connector,
  241. struct intel_encoder *encoder,
  242. const struct drm_display_mode *adjusted_mode)
  243. {
  244. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  245. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  246. enum pipe pipe = intel_crtc->pipe;
  247. struct i915_audio_component *acomp = dev_priv->audio_component;
  248. const uint8_t *eld = connector->eld;
  249. struct intel_digital_port *intel_dig_port =
  250. enc_to_dig_port(&encoder->base);
  251. enum port port = intel_dig_port->port;
  252. uint32_t tmp;
  253. int len, i;
  254. int n, rate;
  255. DRM_DEBUG_KMS("Enable audio codec on pipe %c, %u bytes ELD\n",
  256. pipe_name(pipe), drm_eld_size(eld));
  257. mutex_lock(&dev_priv->av_mutex);
  258. /* Enable audio presence detect, invalidate ELD */
  259. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  260. tmp |= AUDIO_OUTPUT_ENABLE(pipe);
  261. tmp &= ~AUDIO_ELD_VALID(pipe);
  262. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  263. /*
  264. * FIXME: We're supposed to wait for vblank here, but we have vblanks
  265. * disabled during the mode set. The proper fix would be to push the
  266. * rest of the setup into a vblank work item, queued here, but the
  267. * infrastructure is not there yet.
  268. */
  269. /* Reset ELD write address */
  270. tmp = I915_READ(HSW_AUD_DIP_ELD_CTRL(pipe));
  271. tmp &= ~IBX_ELD_ADDRESS_MASK;
  272. I915_WRITE(HSW_AUD_DIP_ELD_CTRL(pipe), tmp);
  273. /* Up to 84 bytes of hw ELD buffer */
  274. len = min(drm_eld_size(eld), 84);
  275. for (i = 0; i < len / 4; i++)
  276. I915_WRITE(HSW_AUD_EDID_DATA(pipe), *((uint32_t *)eld + i));
  277. /* ELD valid */
  278. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  279. tmp |= AUDIO_ELD_VALID(pipe);
  280. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  281. /* Enable timestamps */
  282. tmp = I915_READ(HSW_AUD_CFG(pipe));
  283. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  284. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  285. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  286. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  287. else
  288. tmp |= audio_config_hdmi_pixel_clock(adjusted_mode);
  289. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  290. if (audio_rate_need_prog(intel_crtc, adjusted_mode)) {
  291. if (!acomp)
  292. rate = 0;
  293. else if (port >= PORT_A && port <= PORT_E)
  294. rate = acomp->aud_sample_rate[port];
  295. else {
  296. DRM_ERROR("invalid port: %d\n", port);
  297. rate = 0;
  298. }
  299. n = audio_config_get_n(adjusted_mode, rate);
  300. if (n != 0)
  301. tmp = audio_config_setup_n_reg(n, tmp);
  302. else
  303. DRM_DEBUG_KMS("no suitable N value is found\n");
  304. }
  305. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  306. mutex_unlock(&dev_priv->av_mutex);
  307. }
  308. static void ilk_audio_codec_disable(struct intel_encoder *encoder)
  309. {
  310. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  311. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  312. struct intel_digital_port *intel_dig_port =
  313. enc_to_dig_port(&encoder->base);
  314. enum port port = intel_dig_port->port;
  315. enum pipe pipe = intel_crtc->pipe;
  316. uint32_t tmp, eldv;
  317. i915_reg_t aud_config, aud_cntrl_st2;
  318. DRM_DEBUG_KMS("Disable audio codec on port %c, pipe %c\n",
  319. port_name(port), pipe_name(pipe));
  320. if (WARN_ON(port == PORT_A))
  321. return;
  322. if (HAS_PCH_IBX(dev_priv->dev)) {
  323. aud_config = IBX_AUD_CFG(pipe);
  324. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  325. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  326. aud_config = VLV_AUD_CFG(pipe);
  327. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  328. } else {
  329. aud_config = CPT_AUD_CFG(pipe);
  330. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  331. }
  332. /* Disable timestamps */
  333. tmp = I915_READ(aud_config);
  334. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  335. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  336. tmp &= ~AUD_CONFIG_UPPER_N_MASK;
  337. tmp &= ~AUD_CONFIG_LOWER_N_MASK;
  338. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  339. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  340. I915_WRITE(aud_config, tmp);
  341. eldv = IBX_ELD_VALID(port);
  342. /* Invalidate ELD */
  343. tmp = I915_READ(aud_cntrl_st2);
  344. tmp &= ~eldv;
  345. I915_WRITE(aud_cntrl_st2, tmp);
  346. }
  347. static void ilk_audio_codec_enable(struct drm_connector *connector,
  348. struct intel_encoder *encoder,
  349. const struct drm_display_mode *adjusted_mode)
  350. {
  351. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  352. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  353. struct intel_digital_port *intel_dig_port =
  354. enc_to_dig_port(&encoder->base);
  355. enum port port = intel_dig_port->port;
  356. enum pipe pipe = intel_crtc->pipe;
  357. uint8_t *eld = connector->eld;
  358. uint32_t eldv;
  359. uint32_t tmp;
  360. int len, i;
  361. i915_reg_t hdmiw_hdmiedid, aud_config, aud_cntl_st, aud_cntrl_st2;
  362. DRM_DEBUG_KMS("Enable audio codec on port %c, pipe %c, %u bytes ELD\n",
  363. port_name(port), pipe_name(pipe), drm_eld_size(eld));
  364. if (WARN_ON(port == PORT_A))
  365. return;
  366. /*
  367. * FIXME: We're supposed to wait for vblank here, but we have vblanks
  368. * disabled during the mode set. The proper fix would be to push the
  369. * rest of the setup into a vblank work item, queued here, but the
  370. * infrastructure is not there yet.
  371. */
  372. if (HAS_PCH_IBX(connector->dev)) {
  373. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  374. aud_config = IBX_AUD_CFG(pipe);
  375. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  376. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  377. } else if (IS_VALLEYVIEW(connector->dev) ||
  378. IS_CHERRYVIEW(connector->dev)) {
  379. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  380. aud_config = VLV_AUD_CFG(pipe);
  381. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  382. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  383. } else {
  384. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  385. aud_config = CPT_AUD_CFG(pipe);
  386. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  387. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  388. }
  389. eldv = IBX_ELD_VALID(port);
  390. /* Invalidate ELD */
  391. tmp = I915_READ(aud_cntrl_st2);
  392. tmp &= ~eldv;
  393. I915_WRITE(aud_cntrl_st2, tmp);
  394. /* Reset ELD write address */
  395. tmp = I915_READ(aud_cntl_st);
  396. tmp &= ~IBX_ELD_ADDRESS_MASK;
  397. I915_WRITE(aud_cntl_st, tmp);
  398. /* Up to 84 bytes of hw ELD buffer */
  399. len = min(drm_eld_size(eld), 84);
  400. for (i = 0; i < len / 4; i++)
  401. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  402. /* ELD valid */
  403. tmp = I915_READ(aud_cntrl_st2);
  404. tmp |= eldv;
  405. I915_WRITE(aud_cntrl_st2, tmp);
  406. /* Enable timestamps */
  407. tmp = I915_READ(aud_config);
  408. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  409. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  410. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  411. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  412. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DP_MST))
  413. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  414. else
  415. tmp |= audio_config_hdmi_pixel_clock(adjusted_mode);
  416. I915_WRITE(aud_config, tmp);
  417. }
  418. /**
  419. * intel_audio_codec_enable - Enable the audio codec for HD audio
  420. * @intel_encoder: encoder on which to enable audio
  421. *
  422. * The enable sequences may only be performed after enabling the transcoder and
  423. * port, and after completed link training.
  424. */
  425. void intel_audio_codec_enable(struct intel_encoder *intel_encoder)
  426. {
  427. struct drm_encoder *encoder = &intel_encoder->base;
  428. struct intel_crtc *crtc = to_intel_crtc(encoder->crtc);
  429. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  430. struct drm_connector *connector;
  431. struct drm_device *dev = encoder->dev;
  432. struct drm_i915_private *dev_priv = dev->dev_private;
  433. struct i915_audio_component *acomp = dev_priv->audio_component;
  434. struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
  435. enum port port = intel_dig_port->port;
  436. connector = drm_select_eld(encoder);
  437. if (!connector)
  438. return;
  439. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  440. connector->base.id,
  441. connector->name,
  442. connector->encoder->base.id,
  443. connector->encoder->name);
  444. /* ELD Conn_Type */
  445. connector->eld[5] &= ~(3 << 2);
  446. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  447. intel_pipe_has_type(crtc, INTEL_OUTPUT_DP_MST))
  448. connector->eld[5] |= (1 << 2);
  449. connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
  450. if (dev_priv->display.audio_codec_enable)
  451. dev_priv->display.audio_codec_enable(connector, intel_encoder,
  452. adjusted_mode);
  453. mutex_lock(&dev_priv->av_mutex);
  454. intel_dig_port->audio_connector = connector;
  455. /* referred in audio callbacks */
  456. dev_priv->dig_port_map[port] = intel_encoder;
  457. mutex_unlock(&dev_priv->av_mutex);
  458. if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
  459. acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr, (int) port);
  460. }
  461. /**
  462. * intel_audio_codec_disable - Disable the audio codec for HD audio
  463. * @intel_encoder: encoder on which to disable audio
  464. *
  465. * The disable sequences must be performed before disabling the transcoder or
  466. * port.
  467. */
  468. void intel_audio_codec_disable(struct intel_encoder *intel_encoder)
  469. {
  470. struct drm_encoder *encoder = &intel_encoder->base;
  471. struct drm_device *dev = encoder->dev;
  472. struct drm_i915_private *dev_priv = dev->dev_private;
  473. struct i915_audio_component *acomp = dev_priv->audio_component;
  474. struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
  475. enum port port = intel_dig_port->port;
  476. if (dev_priv->display.audio_codec_disable)
  477. dev_priv->display.audio_codec_disable(intel_encoder);
  478. mutex_lock(&dev_priv->av_mutex);
  479. intel_dig_port->audio_connector = NULL;
  480. dev_priv->dig_port_map[port] = NULL;
  481. mutex_unlock(&dev_priv->av_mutex);
  482. if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
  483. acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr, (int) port);
  484. }
  485. /**
  486. * intel_init_audio - Set up chip specific audio functions
  487. * @dev: drm device
  488. */
  489. void intel_init_audio(struct drm_device *dev)
  490. {
  491. struct drm_i915_private *dev_priv = dev->dev_private;
  492. if (IS_G4X(dev)) {
  493. dev_priv->display.audio_codec_enable = g4x_audio_codec_enable;
  494. dev_priv->display.audio_codec_disable = g4x_audio_codec_disable;
  495. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  496. dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
  497. dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
  498. } else if (IS_HASWELL(dev) || INTEL_INFO(dev)->gen >= 8) {
  499. dev_priv->display.audio_codec_enable = hsw_audio_codec_enable;
  500. dev_priv->display.audio_codec_disable = hsw_audio_codec_disable;
  501. } else if (HAS_PCH_SPLIT(dev)) {
  502. dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
  503. dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
  504. }
  505. }
  506. static void i915_audio_component_get_power(struct device *dev)
  507. {
  508. intel_display_power_get(dev_to_i915(dev), POWER_DOMAIN_AUDIO);
  509. }
  510. static void i915_audio_component_put_power(struct device *dev)
  511. {
  512. intel_display_power_put(dev_to_i915(dev), POWER_DOMAIN_AUDIO);
  513. }
  514. static void i915_audio_component_codec_wake_override(struct device *dev,
  515. bool enable)
  516. {
  517. struct drm_i915_private *dev_priv = dev_to_i915(dev);
  518. u32 tmp;
  519. if (!IS_SKYLAKE(dev_priv) && !IS_KABYLAKE(dev_priv))
  520. return;
  521. /*
  522. * Enable/disable generating the codec wake signal, overriding the
  523. * internal logic to generate the codec wake to controller.
  524. */
  525. tmp = I915_READ(HSW_AUD_CHICKENBIT);
  526. tmp &= ~SKL_AUD_CODEC_WAKE_SIGNAL;
  527. I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
  528. usleep_range(1000, 1500);
  529. if (enable) {
  530. tmp = I915_READ(HSW_AUD_CHICKENBIT);
  531. tmp |= SKL_AUD_CODEC_WAKE_SIGNAL;
  532. I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
  533. usleep_range(1000, 1500);
  534. }
  535. }
  536. /* Get CDCLK in kHz */
  537. static int i915_audio_component_get_cdclk_freq(struct device *dev)
  538. {
  539. struct drm_i915_private *dev_priv = dev_to_i915(dev);
  540. int ret;
  541. if (WARN_ON_ONCE(!HAS_DDI(dev_priv)))
  542. return -ENODEV;
  543. intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
  544. ret = dev_priv->display.get_display_clock_speed(dev_priv->dev);
  545. intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO);
  546. return ret;
  547. }
  548. static int i915_audio_component_sync_audio_rate(struct device *dev,
  549. int port, int rate)
  550. {
  551. struct drm_i915_private *dev_priv = dev_to_i915(dev);
  552. struct intel_encoder *intel_encoder;
  553. struct intel_crtc *crtc;
  554. struct drm_display_mode *mode;
  555. struct i915_audio_component *acomp = dev_priv->audio_component;
  556. enum pipe pipe = INVALID_PIPE;
  557. u32 tmp;
  558. int n;
  559. int err = 0;
  560. /* HSW, BDW, SKL, KBL need this fix */
  561. if (!IS_SKYLAKE(dev_priv) &&
  562. !IS_KABYLAKE(dev_priv) &&
  563. !IS_BROADWELL(dev_priv) &&
  564. !IS_HASWELL(dev_priv))
  565. return 0;
  566. mutex_lock(&dev_priv->av_mutex);
  567. /* 1. get the pipe */
  568. intel_encoder = dev_priv->dig_port_map[port];
  569. /* intel_encoder might be NULL for DP MST */
  570. if (!intel_encoder || !intel_encoder->base.crtc ||
  571. intel_encoder->type != INTEL_OUTPUT_HDMI) {
  572. DRM_DEBUG_KMS("no valid port %c\n", port_name(port));
  573. err = -ENODEV;
  574. goto unlock;
  575. }
  576. crtc = to_intel_crtc(intel_encoder->base.crtc);
  577. pipe = crtc->pipe;
  578. if (pipe == INVALID_PIPE) {
  579. DRM_DEBUG_KMS("no pipe for the port %c\n", port_name(port));
  580. err = -ENODEV;
  581. goto unlock;
  582. }
  583. DRM_DEBUG_KMS("pipe %c connects port %c\n",
  584. pipe_name(pipe), port_name(port));
  585. mode = &crtc->config->base.adjusted_mode;
  586. /* port must be valid now, otherwise the pipe will be invalid */
  587. acomp->aud_sample_rate[port] = rate;
  588. /* 2. check whether to set the N/CTS/M manually or not */
  589. if (!audio_rate_need_prog(crtc, mode)) {
  590. tmp = I915_READ(HSW_AUD_CFG(pipe));
  591. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  592. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  593. goto unlock;
  594. }
  595. n = audio_config_get_n(mode, rate);
  596. if (n == 0) {
  597. DRM_DEBUG_KMS("Using automatic mode for N value on port %c\n",
  598. port_name(port));
  599. tmp = I915_READ(HSW_AUD_CFG(pipe));
  600. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  601. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  602. goto unlock;
  603. }
  604. /* 3. set the N/CTS/M */
  605. tmp = I915_READ(HSW_AUD_CFG(pipe));
  606. tmp = audio_config_setup_n_reg(n, tmp);
  607. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  608. unlock:
  609. mutex_unlock(&dev_priv->av_mutex);
  610. return err;
  611. }
  612. static int i915_audio_component_get_eld(struct device *dev, int port,
  613. bool *enabled,
  614. unsigned char *buf, int max_bytes)
  615. {
  616. struct drm_i915_private *dev_priv = dev_to_i915(dev);
  617. struct intel_encoder *intel_encoder;
  618. struct intel_digital_port *intel_dig_port;
  619. const u8 *eld;
  620. int ret = -EINVAL;
  621. mutex_lock(&dev_priv->av_mutex);
  622. intel_encoder = dev_priv->dig_port_map[port];
  623. /* intel_encoder might be NULL for DP MST */
  624. if (intel_encoder) {
  625. ret = 0;
  626. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  627. *enabled = intel_dig_port->audio_connector != NULL;
  628. if (*enabled) {
  629. eld = intel_dig_port->audio_connector->eld;
  630. ret = drm_eld_size(eld);
  631. memcpy(buf, eld, min(max_bytes, ret));
  632. }
  633. }
  634. mutex_unlock(&dev_priv->av_mutex);
  635. return ret;
  636. }
  637. static const struct i915_audio_component_ops i915_audio_component_ops = {
  638. .owner = THIS_MODULE,
  639. .get_power = i915_audio_component_get_power,
  640. .put_power = i915_audio_component_put_power,
  641. .codec_wake_override = i915_audio_component_codec_wake_override,
  642. .get_cdclk_freq = i915_audio_component_get_cdclk_freq,
  643. .sync_audio_rate = i915_audio_component_sync_audio_rate,
  644. .get_eld = i915_audio_component_get_eld,
  645. };
  646. static int i915_audio_component_bind(struct device *i915_dev,
  647. struct device *hda_dev, void *data)
  648. {
  649. struct i915_audio_component *acomp = data;
  650. struct drm_i915_private *dev_priv = dev_to_i915(i915_dev);
  651. int i;
  652. if (WARN_ON(acomp->ops || acomp->dev))
  653. return -EEXIST;
  654. drm_modeset_lock_all(dev_priv->dev);
  655. acomp->ops = &i915_audio_component_ops;
  656. acomp->dev = i915_dev;
  657. BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
  658. for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
  659. acomp->aud_sample_rate[i] = 0;
  660. dev_priv->audio_component = acomp;
  661. drm_modeset_unlock_all(dev_priv->dev);
  662. return 0;
  663. }
  664. static void i915_audio_component_unbind(struct device *i915_dev,
  665. struct device *hda_dev, void *data)
  666. {
  667. struct i915_audio_component *acomp = data;
  668. struct drm_i915_private *dev_priv = dev_to_i915(i915_dev);
  669. drm_modeset_lock_all(dev_priv->dev);
  670. acomp->ops = NULL;
  671. acomp->dev = NULL;
  672. dev_priv->audio_component = NULL;
  673. drm_modeset_unlock_all(dev_priv->dev);
  674. }
  675. static const struct component_ops i915_audio_component_bind_ops = {
  676. .bind = i915_audio_component_bind,
  677. .unbind = i915_audio_component_unbind,
  678. };
  679. /**
  680. * i915_audio_component_init - initialize and register the audio component
  681. * @dev_priv: i915 device instance
  682. *
  683. * This will register with the component framework a child component which
  684. * will bind dynamically to the snd_hda_intel driver's corresponding master
  685. * component when the latter is registered. During binding the child
  686. * initializes an instance of struct i915_audio_component which it receives
  687. * from the master. The master can then start to use the interface defined by
  688. * this struct. Each side can break the binding at any point by deregistering
  689. * its own component after which each side's component unbind callback is
  690. * called.
  691. *
  692. * We ignore any error during registration and continue with reduced
  693. * functionality (i.e. without HDMI audio).
  694. */
  695. void i915_audio_component_init(struct drm_i915_private *dev_priv)
  696. {
  697. int ret;
  698. ret = component_add(dev_priv->dev->dev, &i915_audio_component_bind_ops);
  699. if (ret < 0) {
  700. DRM_ERROR("failed to add audio component (%d)\n", ret);
  701. /* continue with reduced functionality */
  702. return;
  703. }
  704. dev_priv->audio_component_registered = true;
  705. }
  706. /**
  707. * i915_audio_component_cleanup - deregister the audio component
  708. * @dev_priv: i915 device instance
  709. *
  710. * Deregisters the audio component, breaking any existing binding to the
  711. * corresponding snd_hda_intel driver's master component.
  712. */
  713. void i915_audio_component_cleanup(struct drm_i915_private *dev_priv)
  714. {
  715. if (!dev_priv->audio_component_registered)
  716. return;
  717. component_del(dev_priv->dev->dev, &i915_audio_component_bind_ops);
  718. dev_priv->audio_component_registered = false;
  719. }