intel_audio.c 30 KB

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