intel_audio.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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. /* DP N/M table */
  57. #define LC_540M 540000
  58. #define LC_270M 270000
  59. #define LC_162M 162000
  60. struct dp_aud_n_m {
  61. int sample_rate;
  62. int clock;
  63. u16 m;
  64. u16 n;
  65. };
  66. /* Values according to DP 1.4 Table 2-104 */
  67. static const struct dp_aud_n_m dp_aud_n_m[] = {
  68. { 32000, LC_162M, 1024, 10125 },
  69. { 44100, LC_162M, 784, 5625 },
  70. { 48000, LC_162M, 512, 3375 },
  71. { 64000, LC_162M, 2048, 10125 },
  72. { 88200, LC_162M, 1568, 5625 },
  73. { 96000, LC_162M, 1024, 3375 },
  74. { 128000, LC_162M, 4096, 10125 },
  75. { 176400, LC_162M, 3136, 5625 },
  76. { 192000, LC_162M, 2048, 3375 },
  77. { 32000, LC_270M, 1024, 16875 },
  78. { 44100, LC_270M, 784, 9375 },
  79. { 48000, LC_270M, 512, 5625 },
  80. { 64000, LC_270M, 2048, 16875 },
  81. { 88200, LC_270M, 1568, 9375 },
  82. { 96000, LC_270M, 1024, 5625 },
  83. { 128000, LC_270M, 4096, 16875 },
  84. { 176400, LC_270M, 3136, 9375 },
  85. { 192000, LC_270M, 2048, 5625 },
  86. { 32000, LC_540M, 1024, 33750 },
  87. { 44100, LC_540M, 784, 18750 },
  88. { 48000, LC_540M, 512, 11250 },
  89. { 64000, LC_540M, 2048, 33750 },
  90. { 88200, LC_540M, 1568, 18750 },
  91. { 96000, LC_540M, 1024, 11250 },
  92. { 128000, LC_540M, 4096, 33750 },
  93. { 176400, LC_540M, 3136, 18750 },
  94. { 192000, LC_540M, 2048, 11250 },
  95. };
  96. static const struct dp_aud_n_m *
  97. audio_config_dp_get_n_m(struct intel_crtc *intel_crtc, int rate)
  98. {
  99. int i;
  100. for (i = 0; i < ARRAY_SIZE(dp_aud_n_m); i++) {
  101. if (rate == dp_aud_n_m[i].sample_rate &&
  102. intel_crtc->config->port_clock == dp_aud_n_m[i].clock)
  103. return &dp_aud_n_m[i];
  104. }
  105. return NULL;
  106. }
  107. static const struct {
  108. int clock;
  109. u32 config;
  110. } hdmi_audio_clock[] = {
  111. { 25175, AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  112. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  113. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  114. { 27027, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  115. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  116. { 54054, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  117. { 74176, AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  118. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  119. { 148352, AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  120. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  121. };
  122. /* HDMI N/CTS table */
  123. #define TMDS_297M 297000
  124. #define TMDS_296M 296703
  125. static const struct {
  126. int sample_rate;
  127. int clock;
  128. int n;
  129. int cts;
  130. } hdmi_aud_ncts[] = {
  131. { 44100, TMDS_296M, 4459, 234375 },
  132. { 44100, TMDS_297M, 4704, 247500 },
  133. { 48000, TMDS_296M, 5824, 281250 },
  134. { 48000, TMDS_297M, 5120, 247500 },
  135. { 32000, TMDS_296M, 5824, 421875 },
  136. { 32000, TMDS_297M, 3072, 222750 },
  137. { 88200, TMDS_296M, 8918, 234375 },
  138. { 88200, TMDS_297M, 9408, 247500 },
  139. { 96000, TMDS_296M, 11648, 281250 },
  140. { 96000, TMDS_297M, 10240, 247500 },
  141. { 176400, TMDS_296M, 17836, 234375 },
  142. { 176400, TMDS_297M, 18816, 247500 },
  143. { 192000, TMDS_296M, 23296, 281250 },
  144. { 192000, TMDS_297M, 20480, 247500 },
  145. };
  146. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  147. static u32 audio_config_hdmi_pixel_clock(const struct drm_display_mode *adjusted_mode)
  148. {
  149. int i;
  150. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  151. if (adjusted_mode->crtc_clock == hdmi_audio_clock[i].clock)
  152. break;
  153. }
  154. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  155. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n",
  156. adjusted_mode->crtc_clock);
  157. i = 1;
  158. }
  159. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  160. hdmi_audio_clock[i].clock,
  161. hdmi_audio_clock[i].config);
  162. return hdmi_audio_clock[i].config;
  163. }
  164. static int audio_config_hdmi_get_n(const struct drm_display_mode *adjusted_mode,
  165. int rate)
  166. {
  167. int i;
  168. for (i = 0; i < ARRAY_SIZE(hdmi_aud_ncts); i++) {
  169. if (rate == hdmi_aud_ncts[i].sample_rate &&
  170. adjusted_mode->crtc_clock == hdmi_aud_ncts[i].clock) {
  171. return hdmi_aud_ncts[i].n;
  172. }
  173. }
  174. return 0;
  175. }
  176. static bool intel_eld_uptodate(struct drm_connector *connector,
  177. i915_reg_t reg_eldv, uint32_t bits_eldv,
  178. i915_reg_t reg_elda, uint32_t bits_elda,
  179. i915_reg_t reg_edid)
  180. {
  181. struct drm_i915_private *dev_priv = to_i915(connector->dev);
  182. uint8_t *eld = connector->eld;
  183. uint32_t tmp;
  184. int i;
  185. tmp = I915_READ(reg_eldv);
  186. tmp &= bits_eldv;
  187. if (!tmp)
  188. return false;
  189. tmp = I915_READ(reg_elda);
  190. tmp &= ~bits_elda;
  191. I915_WRITE(reg_elda, tmp);
  192. for (i = 0; i < drm_eld_size(eld) / 4; i++)
  193. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  194. return false;
  195. return true;
  196. }
  197. static void g4x_audio_codec_disable(struct intel_encoder *encoder)
  198. {
  199. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  200. uint32_t eldv, tmp;
  201. DRM_DEBUG_KMS("Disable audio codec\n");
  202. tmp = I915_READ(G4X_AUD_VID_DID);
  203. if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
  204. eldv = G4X_ELDV_DEVCL_DEVBLC;
  205. else
  206. eldv = G4X_ELDV_DEVCTG;
  207. /* Invalidate ELD */
  208. tmp = I915_READ(G4X_AUD_CNTL_ST);
  209. tmp &= ~eldv;
  210. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  211. }
  212. static void g4x_audio_codec_enable(struct drm_connector *connector,
  213. struct intel_encoder *encoder,
  214. const struct drm_display_mode *adjusted_mode)
  215. {
  216. struct drm_i915_private *dev_priv = to_i915(connector->dev);
  217. uint8_t *eld = connector->eld;
  218. uint32_t eldv;
  219. uint32_t tmp;
  220. int len, i;
  221. DRM_DEBUG_KMS("Enable audio codec, %u bytes ELD\n", eld[2]);
  222. tmp = I915_READ(G4X_AUD_VID_DID);
  223. if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
  224. eldv = G4X_ELDV_DEVCL_DEVBLC;
  225. else
  226. eldv = G4X_ELDV_DEVCTG;
  227. if (intel_eld_uptodate(connector,
  228. G4X_AUD_CNTL_ST, eldv,
  229. G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
  230. G4X_HDMIW_HDMIEDID))
  231. return;
  232. tmp = I915_READ(G4X_AUD_CNTL_ST);
  233. tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
  234. len = (tmp >> 9) & 0x1f; /* ELD buffer size */
  235. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  236. len = min(drm_eld_size(eld) / 4, len);
  237. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  238. for (i = 0; i < len; i++)
  239. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  240. tmp = I915_READ(G4X_AUD_CNTL_ST);
  241. tmp |= eldv;
  242. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  243. }
  244. static void
  245. hsw_dp_audio_config_update(struct intel_crtc *intel_crtc, enum port port,
  246. const struct drm_display_mode *adjusted_mode)
  247. {
  248. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  249. struct i915_audio_component *acomp = dev_priv->audio_component;
  250. int rate = acomp ? acomp->aud_sample_rate[port] : 0;
  251. const struct dp_aud_n_m *nm = audio_config_dp_get_n_m(intel_crtc, rate);
  252. enum pipe pipe = intel_crtc->pipe;
  253. u32 tmp;
  254. if (nm)
  255. DRM_DEBUG_KMS("using Maud %u, Naud %u\n", nm->m, nm->n);
  256. else
  257. DRM_DEBUG_KMS("using automatic Maud, Naud\n");
  258. tmp = I915_READ(HSW_AUD_CFG(pipe));
  259. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  260. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  261. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  262. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  263. if (nm) {
  264. tmp &= ~AUD_CONFIG_N_MASK;
  265. tmp |= AUD_CONFIG_N(nm->n);
  266. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  267. }
  268. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  269. tmp = I915_READ(HSW_AUD_M_CTS_ENABLE(pipe));
  270. tmp &= ~AUD_CONFIG_M_MASK;
  271. tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
  272. tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
  273. if (nm) {
  274. tmp |= nm->m;
  275. tmp |= AUD_M_CTS_M_VALUE_INDEX;
  276. tmp |= AUD_M_CTS_M_PROG_ENABLE;
  277. }
  278. I915_WRITE(HSW_AUD_M_CTS_ENABLE(pipe), tmp);
  279. }
  280. static void
  281. hsw_hdmi_audio_config_update(struct intel_crtc *intel_crtc, enum port port,
  282. const struct drm_display_mode *adjusted_mode)
  283. {
  284. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  285. struct i915_audio_component *acomp = dev_priv->audio_component;
  286. int rate = acomp ? acomp->aud_sample_rate[port] : 0;
  287. enum pipe pipe = intel_crtc->pipe;
  288. int n;
  289. u32 tmp;
  290. tmp = I915_READ(HSW_AUD_CFG(pipe));
  291. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  292. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  293. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  294. tmp |= audio_config_hdmi_pixel_clock(adjusted_mode);
  295. n = audio_config_hdmi_get_n(adjusted_mode, rate);
  296. if (n != 0) {
  297. DRM_DEBUG_KMS("using N %d\n", n);
  298. tmp &= ~AUD_CONFIG_N_MASK;
  299. tmp |= AUD_CONFIG_N(n);
  300. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  301. } else {
  302. DRM_DEBUG_KMS("using automatic N\n");
  303. }
  304. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  305. /*
  306. * Let's disable "Enable CTS or M Prog bit"
  307. * and let HW calculate the value
  308. */
  309. tmp = I915_READ(HSW_AUD_M_CTS_ENABLE(pipe));
  310. tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
  311. tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
  312. I915_WRITE(HSW_AUD_M_CTS_ENABLE(pipe), tmp);
  313. }
  314. static void
  315. hsw_audio_config_update(struct intel_crtc *intel_crtc, enum port port,
  316. const struct drm_display_mode *adjusted_mode)
  317. {
  318. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  319. hsw_dp_audio_config_update(intel_crtc, port, adjusted_mode);
  320. else
  321. hsw_hdmi_audio_config_update(intel_crtc, port, adjusted_mode);
  322. }
  323. static void hsw_audio_codec_disable(struct intel_encoder *encoder)
  324. {
  325. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  326. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  327. enum pipe pipe = intel_crtc->pipe;
  328. uint32_t tmp;
  329. DRM_DEBUG_KMS("Disable audio codec on pipe %c\n", pipe_name(pipe));
  330. mutex_lock(&dev_priv->av_mutex);
  331. /* Disable timestamps */
  332. tmp = I915_READ(HSW_AUD_CFG(pipe));
  333. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  334. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  335. tmp &= ~AUD_CONFIG_UPPER_N_MASK;
  336. tmp &= ~AUD_CONFIG_LOWER_N_MASK;
  337. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  338. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  339. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  340. /* Invalidate ELD */
  341. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  342. tmp &= ~AUDIO_ELD_VALID(pipe);
  343. tmp &= ~AUDIO_OUTPUT_ENABLE(pipe);
  344. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  345. mutex_unlock(&dev_priv->av_mutex);
  346. }
  347. static void hsw_audio_codec_enable(struct drm_connector *connector,
  348. struct intel_encoder *intel_encoder,
  349. const struct drm_display_mode *adjusted_mode)
  350. {
  351. struct drm_i915_private *dev_priv = to_i915(connector->dev);
  352. struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  353. enum pipe pipe = intel_crtc->pipe;
  354. enum port port = intel_encoder->port;
  355. const uint8_t *eld = connector->eld;
  356. uint32_t tmp;
  357. int len, i;
  358. DRM_DEBUG_KMS("Enable audio codec on pipe %c, %u bytes ELD\n",
  359. pipe_name(pipe), drm_eld_size(eld));
  360. mutex_lock(&dev_priv->av_mutex);
  361. /* Enable audio presence detect, invalidate ELD */
  362. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  363. tmp |= AUDIO_OUTPUT_ENABLE(pipe);
  364. tmp &= ~AUDIO_ELD_VALID(pipe);
  365. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  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. /* Reset ELD write address */
  373. tmp = I915_READ(HSW_AUD_DIP_ELD_CTRL(pipe));
  374. tmp &= ~IBX_ELD_ADDRESS_MASK;
  375. I915_WRITE(HSW_AUD_DIP_ELD_CTRL(pipe), tmp);
  376. /* Up to 84 bytes of hw ELD buffer */
  377. len = min(drm_eld_size(eld), 84);
  378. for (i = 0; i < len / 4; i++)
  379. I915_WRITE(HSW_AUD_EDID_DATA(pipe), *((uint32_t *)eld + i));
  380. /* ELD valid */
  381. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  382. tmp |= AUDIO_ELD_VALID(pipe);
  383. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  384. /* Enable timestamps */
  385. hsw_audio_config_update(intel_crtc, port, adjusted_mode);
  386. mutex_unlock(&dev_priv->av_mutex);
  387. }
  388. static void ilk_audio_codec_disable(struct intel_encoder *intel_encoder)
  389. {
  390. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  391. struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  392. enum pipe pipe = intel_crtc->pipe;
  393. enum port port = intel_encoder->port;
  394. uint32_t tmp, eldv;
  395. i915_reg_t aud_config, aud_cntrl_st2;
  396. DRM_DEBUG_KMS("Disable audio codec on port %c, pipe %c\n",
  397. port_name(port), pipe_name(pipe));
  398. if (WARN_ON(port == PORT_A))
  399. return;
  400. if (HAS_PCH_IBX(dev_priv)) {
  401. aud_config = IBX_AUD_CFG(pipe);
  402. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  403. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  404. aud_config = VLV_AUD_CFG(pipe);
  405. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  406. } else {
  407. aud_config = CPT_AUD_CFG(pipe);
  408. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  409. }
  410. /* Disable timestamps */
  411. tmp = I915_READ(aud_config);
  412. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  413. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  414. tmp &= ~AUD_CONFIG_UPPER_N_MASK;
  415. tmp &= ~AUD_CONFIG_LOWER_N_MASK;
  416. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  417. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  418. I915_WRITE(aud_config, tmp);
  419. eldv = IBX_ELD_VALID(port);
  420. /* Invalidate ELD */
  421. tmp = I915_READ(aud_cntrl_st2);
  422. tmp &= ~eldv;
  423. I915_WRITE(aud_cntrl_st2, tmp);
  424. }
  425. static void ilk_audio_codec_enable(struct drm_connector *connector,
  426. struct intel_encoder *intel_encoder,
  427. const struct drm_display_mode *adjusted_mode)
  428. {
  429. struct drm_i915_private *dev_priv = to_i915(connector->dev);
  430. struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  431. enum pipe pipe = intel_crtc->pipe;
  432. enum port port = intel_encoder->port;
  433. uint8_t *eld = connector->eld;
  434. uint32_t tmp, eldv;
  435. int len, i;
  436. i915_reg_t hdmiw_hdmiedid, aud_config, aud_cntl_st, aud_cntrl_st2;
  437. DRM_DEBUG_KMS("Enable audio codec on port %c, pipe %c, %u bytes ELD\n",
  438. port_name(port), pipe_name(pipe), drm_eld_size(eld));
  439. if (WARN_ON(port == PORT_A))
  440. return;
  441. /*
  442. * FIXME: We're supposed to wait for vblank here, but we have vblanks
  443. * disabled during the mode set. The proper fix would be to push the
  444. * rest of the setup into a vblank work item, queued here, but the
  445. * infrastructure is not there yet.
  446. */
  447. if (HAS_PCH_IBX(dev_priv)) {
  448. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  449. aud_config = IBX_AUD_CFG(pipe);
  450. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  451. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  452. } else if (IS_VALLEYVIEW(dev_priv) ||
  453. IS_CHERRYVIEW(dev_priv)) {
  454. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  455. aud_config = VLV_AUD_CFG(pipe);
  456. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  457. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  458. } else {
  459. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  460. aud_config = CPT_AUD_CFG(pipe);
  461. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  462. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  463. }
  464. eldv = IBX_ELD_VALID(port);
  465. /* Invalidate ELD */
  466. tmp = I915_READ(aud_cntrl_st2);
  467. tmp &= ~eldv;
  468. I915_WRITE(aud_cntrl_st2, tmp);
  469. /* Reset ELD write address */
  470. tmp = I915_READ(aud_cntl_st);
  471. tmp &= ~IBX_ELD_ADDRESS_MASK;
  472. I915_WRITE(aud_cntl_st, tmp);
  473. /* Up to 84 bytes of hw ELD buffer */
  474. len = min(drm_eld_size(eld), 84);
  475. for (i = 0; i < len / 4; i++)
  476. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  477. /* ELD valid */
  478. tmp = I915_READ(aud_cntrl_st2);
  479. tmp |= eldv;
  480. I915_WRITE(aud_cntrl_st2, tmp);
  481. /* Enable timestamps */
  482. tmp = I915_READ(aud_config);
  483. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  484. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  485. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  486. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  487. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  488. else
  489. tmp |= audio_config_hdmi_pixel_clock(adjusted_mode);
  490. I915_WRITE(aud_config, tmp);
  491. }
  492. /**
  493. * intel_audio_codec_enable - Enable the audio codec for HD audio
  494. * @intel_encoder: encoder on which to enable audio
  495. * @crtc_state: pointer to the current crtc state.
  496. * @conn_state: pointer to the current connector state.
  497. *
  498. * The enable sequences may only be performed after enabling the transcoder and
  499. * port, and after completed link training.
  500. */
  501. void intel_audio_codec_enable(struct intel_encoder *intel_encoder,
  502. const struct intel_crtc_state *crtc_state,
  503. const struct drm_connector_state *conn_state)
  504. {
  505. struct drm_encoder *encoder = &intel_encoder->base;
  506. const struct drm_display_mode *adjusted_mode = &crtc_state->base.adjusted_mode;
  507. struct drm_connector *connector;
  508. struct drm_i915_private *dev_priv = to_i915(encoder->dev);
  509. struct i915_audio_component *acomp = dev_priv->audio_component;
  510. enum port port = intel_encoder->port;
  511. enum pipe pipe = to_intel_crtc(crtc_state->base.crtc)->pipe;
  512. connector = conn_state->connector;
  513. if (!connector || !connector->eld[0])
  514. return;
  515. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  516. connector->base.id,
  517. connector->name,
  518. connector->encoder->base.id,
  519. connector->encoder->name);
  520. /* ELD Conn_Type */
  521. connector->eld[5] &= ~(3 << 2);
  522. if (intel_crtc_has_dp_encoder(crtc_state))
  523. connector->eld[5] |= (1 << 2);
  524. connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
  525. if (dev_priv->display.audio_codec_enable)
  526. dev_priv->display.audio_codec_enable(connector, intel_encoder,
  527. adjusted_mode);
  528. mutex_lock(&dev_priv->av_mutex);
  529. intel_encoder->audio_connector = connector;
  530. /* referred in audio callbacks */
  531. dev_priv->av_enc_map[pipe] = intel_encoder;
  532. mutex_unlock(&dev_priv->av_mutex);
  533. /* audio drivers expect pipe = -1 to indicate Non-MST cases */
  534. if (intel_encoder->type != INTEL_OUTPUT_DP_MST)
  535. pipe = -1;
  536. if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
  537. acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
  538. (int) port, (int) pipe);
  539. }
  540. /**
  541. * intel_audio_codec_disable - Disable the audio codec for HD audio
  542. * @intel_encoder: encoder on which to disable audio
  543. *
  544. * The disable sequences must be performed before disabling the transcoder or
  545. * port.
  546. */
  547. void intel_audio_codec_disable(struct intel_encoder *intel_encoder)
  548. {
  549. struct drm_encoder *encoder = &intel_encoder->base;
  550. struct drm_i915_private *dev_priv = to_i915(encoder->dev);
  551. struct i915_audio_component *acomp = dev_priv->audio_component;
  552. enum port port = intel_encoder->port;
  553. struct intel_crtc *crtc = to_intel_crtc(encoder->crtc);
  554. enum pipe pipe = crtc->pipe;
  555. if (dev_priv->display.audio_codec_disable)
  556. dev_priv->display.audio_codec_disable(intel_encoder);
  557. mutex_lock(&dev_priv->av_mutex);
  558. intel_encoder->audio_connector = NULL;
  559. dev_priv->av_enc_map[pipe] = NULL;
  560. mutex_unlock(&dev_priv->av_mutex);
  561. /* audio drivers expect pipe = -1 to indicate Non-MST cases */
  562. if (intel_encoder->type != INTEL_OUTPUT_DP_MST)
  563. pipe = -1;
  564. if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
  565. acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
  566. (int) port, (int) pipe);
  567. }
  568. /**
  569. * intel_init_audio_hooks - Set up chip specific audio hooks
  570. * @dev_priv: device private
  571. */
  572. void intel_init_audio_hooks(struct drm_i915_private *dev_priv)
  573. {
  574. if (IS_G4X(dev_priv)) {
  575. dev_priv->display.audio_codec_enable = g4x_audio_codec_enable;
  576. dev_priv->display.audio_codec_disable = g4x_audio_codec_disable;
  577. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  578. dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
  579. dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
  580. } else if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8) {
  581. dev_priv->display.audio_codec_enable = hsw_audio_codec_enable;
  582. dev_priv->display.audio_codec_disable = hsw_audio_codec_disable;
  583. } else if (HAS_PCH_SPLIT(dev_priv)) {
  584. dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
  585. dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
  586. }
  587. }
  588. static void i915_audio_component_get_power(struct device *kdev)
  589. {
  590. intel_display_power_get(kdev_to_i915(kdev), POWER_DOMAIN_AUDIO);
  591. }
  592. static void i915_audio_component_put_power(struct device *kdev)
  593. {
  594. intel_display_power_put(kdev_to_i915(kdev), POWER_DOMAIN_AUDIO);
  595. }
  596. static void i915_audio_component_codec_wake_override(struct device *kdev,
  597. bool enable)
  598. {
  599. struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
  600. u32 tmp;
  601. if (!IS_SKYLAKE(dev_priv) && !IS_KABYLAKE(dev_priv))
  602. return;
  603. i915_audio_component_get_power(kdev);
  604. /*
  605. * Enable/disable generating the codec wake signal, overriding the
  606. * internal logic to generate the codec wake to controller.
  607. */
  608. tmp = I915_READ(HSW_AUD_CHICKENBIT);
  609. tmp &= ~SKL_AUD_CODEC_WAKE_SIGNAL;
  610. I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
  611. usleep_range(1000, 1500);
  612. if (enable) {
  613. tmp = I915_READ(HSW_AUD_CHICKENBIT);
  614. tmp |= SKL_AUD_CODEC_WAKE_SIGNAL;
  615. I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
  616. usleep_range(1000, 1500);
  617. }
  618. i915_audio_component_put_power(kdev);
  619. }
  620. /* Get CDCLK in kHz */
  621. static int i915_audio_component_get_cdclk_freq(struct device *kdev)
  622. {
  623. struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
  624. if (WARN_ON_ONCE(!HAS_DDI(dev_priv)))
  625. return -ENODEV;
  626. return dev_priv->cdclk_freq;
  627. }
  628. /*
  629. * get the intel_encoder according to the parameter port and pipe
  630. * intel_encoder is saved by the index of pipe
  631. * MST & (pipe >= 0): return the av_enc_map[pipe],
  632. * when port is matched
  633. * MST & (pipe < 0): this is invalid
  634. * Non-MST & (pipe >= 0): only pipe = 0 (the first device entry)
  635. * will get the right intel_encoder with port matched
  636. * Non-MST & (pipe < 0): get the right intel_encoder with port matched
  637. */
  638. static struct intel_encoder *get_saved_enc(struct drm_i915_private *dev_priv,
  639. int port, int pipe)
  640. {
  641. struct intel_encoder *encoder;
  642. if (WARN_ON(pipe >= I915_MAX_PIPES))
  643. return NULL;
  644. /* MST */
  645. if (pipe >= 0) {
  646. encoder = dev_priv->av_enc_map[pipe];
  647. /*
  648. * when bootup, audio driver may not know it is
  649. * MST or not. So it will poll all the port & pipe
  650. * combinations
  651. */
  652. if (encoder != NULL && encoder->port == port &&
  653. encoder->type == INTEL_OUTPUT_DP_MST)
  654. return encoder;
  655. }
  656. /* Non-MST */
  657. if (pipe > 0)
  658. return NULL;
  659. for_each_pipe(dev_priv, pipe) {
  660. encoder = dev_priv->av_enc_map[pipe];
  661. if (encoder == NULL)
  662. continue;
  663. if (encoder->type == INTEL_OUTPUT_DP_MST)
  664. continue;
  665. if (port == encoder->port)
  666. return encoder;
  667. }
  668. return NULL;
  669. }
  670. static int i915_audio_component_sync_audio_rate(struct device *kdev, int port,
  671. int pipe, int rate)
  672. {
  673. struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
  674. struct intel_encoder *intel_encoder;
  675. struct intel_crtc *crtc;
  676. struct drm_display_mode *adjusted_mode;
  677. struct i915_audio_component *acomp = dev_priv->audio_component;
  678. int err = 0;
  679. if (!HAS_DDI(dev_priv))
  680. return 0;
  681. i915_audio_component_get_power(kdev);
  682. mutex_lock(&dev_priv->av_mutex);
  683. /* 1. get the pipe */
  684. intel_encoder = get_saved_enc(dev_priv, port, pipe);
  685. if (!intel_encoder || !intel_encoder->base.crtc) {
  686. DRM_DEBUG_KMS("Not valid for port %c\n", port_name(port));
  687. err = -ENODEV;
  688. goto unlock;
  689. }
  690. /* pipe passed from the audio driver will be -1 for Non-MST case */
  691. crtc = to_intel_crtc(intel_encoder->base.crtc);
  692. pipe = crtc->pipe;
  693. adjusted_mode = &crtc->config->base.adjusted_mode;
  694. /* port must be valid now, otherwise the pipe will be invalid */
  695. acomp->aud_sample_rate[port] = rate;
  696. hsw_audio_config_update(crtc, port, adjusted_mode);
  697. unlock:
  698. mutex_unlock(&dev_priv->av_mutex);
  699. i915_audio_component_put_power(kdev);
  700. return err;
  701. }
  702. static int i915_audio_component_get_eld(struct device *kdev, int port,
  703. int pipe, bool *enabled,
  704. unsigned char *buf, int max_bytes)
  705. {
  706. struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
  707. struct intel_encoder *intel_encoder;
  708. const u8 *eld;
  709. int ret = -EINVAL;
  710. mutex_lock(&dev_priv->av_mutex);
  711. intel_encoder = get_saved_enc(dev_priv, port, pipe);
  712. if (!intel_encoder) {
  713. DRM_DEBUG_KMS("Not valid for port %c\n", port_name(port));
  714. mutex_unlock(&dev_priv->av_mutex);
  715. return ret;
  716. }
  717. ret = 0;
  718. *enabled = intel_encoder->audio_connector != NULL;
  719. if (*enabled) {
  720. eld = intel_encoder->audio_connector->eld;
  721. ret = drm_eld_size(eld);
  722. memcpy(buf, eld, min(max_bytes, ret));
  723. }
  724. mutex_unlock(&dev_priv->av_mutex);
  725. return ret;
  726. }
  727. static const struct i915_audio_component_ops i915_audio_component_ops = {
  728. .owner = THIS_MODULE,
  729. .get_power = i915_audio_component_get_power,
  730. .put_power = i915_audio_component_put_power,
  731. .codec_wake_override = i915_audio_component_codec_wake_override,
  732. .get_cdclk_freq = i915_audio_component_get_cdclk_freq,
  733. .sync_audio_rate = i915_audio_component_sync_audio_rate,
  734. .get_eld = i915_audio_component_get_eld,
  735. };
  736. static int i915_audio_component_bind(struct device *i915_kdev,
  737. struct device *hda_kdev, void *data)
  738. {
  739. struct i915_audio_component *acomp = data;
  740. struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
  741. int i;
  742. if (WARN_ON(acomp->ops || acomp->dev))
  743. return -EEXIST;
  744. drm_modeset_lock_all(&dev_priv->drm);
  745. acomp->ops = &i915_audio_component_ops;
  746. acomp->dev = i915_kdev;
  747. BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
  748. for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
  749. acomp->aud_sample_rate[i] = 0;
  750. dev_priv->audio_component = acomp;
  751. drm_modeset_unlock_all(&dev_priv->drm);
  752. return 0;
  753. }
  754. static void i915_audio_component_unbind(struct device *i915_kdev,
  755. struct device *hda_kdev, void *data)
  756. {
  757. struct i915_audio_component *acomp = data;
  758. struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
  759. drm_modeset_lock_all(&dev_priv->drm);
  760. acomp->ops = NULL;
  761. acomp->dev = NULL;
  762. dev_priv->audio_component = NULL;
  763. drm_modeset_unlock_all(&dev_priv->drm);
  764. }
  765. static const struct component_ops i915_audio_component_bind_ops = {
  766. .bind = i915_audio_component_bind,
  767. .unbind = i915_audio_component_unbind,
  768. };
  769. /**
  770. * i915_audio_component_init - initialize and register the audio component
  771. * @dev_priv: i915 device instance
  772. *
  773. * This will register with the component framework a child component which
  774. * will bind dynamically to the snd_hda_intel driver's corresponding master
  775. * component when the latter is registered. During binding the child
  776. * initializes an instance of struct i915_audio_component which it receives
  777. * from the master. The master can then start to use the interface defined by
  778. * this struct. Each side can break the binding at any point by deregistering
  779. * its own component after which each side's component unbind callback is
  780. * called.
  781. *
  782. * We ignore any error during registration and continue with reduced
  783. * functionality (i.e. without HDMI audio).
  784. */
  785. void i915_audio_component_init(struct drm_i915_private *dev_priv)
  786. {
  787. int ret;
  788. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  789. return;
  790. ret = component_add(dev_priv->drm.dev, &i915_audio_component_bind_ops);
  791. if (ret < 0) {
  792. DRM_ERROR("failed to add audio component (%d)\n", ret);
  793. /* continue with reduced functionality */
  794. return;
  795. }
  796. dev_priv->audio_component_registered = true;
  797. }
  798. /**
  799. * i915_audio_component_cleanup - deregister the audio component
  800. * @dev_priv: i915 device instance
  801. *
  802. * Deregisters the audio component, breaking any existing binding to the
  803. * corresponding snd_hda_intel driver's master component.
  804. */
  805. void i915_audio_component_cleanup(struct drm_i915_private *dev_priv)
  806. {
  807. if (!dev_priv->audio_component_registered)
  808. return;
  809. component_del(dev_priv->drm.dev, &i915_audio_component_bind_ops);
  810. dev_priv->audio_component_registered = false;
  811. }