intel_audio.c 30 KB

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