intel_audio.c 31 KB

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