intel_audio.c 29 KB

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