intel_audio.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /*
  2. * Copyright © 2014 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/component.h>
  25. #include <drm/i915_component.h>
  26. #include "intel_drv.h"
  27. #include <drm/drmP.h>
  28. #include <drm/drm_edid.h>
  29. #include "i915_drv.h"
  30. /**
  31. * DOC: High Definition Audio over HDMI and Display Port
  32. *
  33. * The graphics and audio drivers together support High Definition Audio over
  34. * HDMI and Display Port. The audio programming sequences are divided into audio
  35. * codec and controller enable and disable sequences. The graphics driver
  36. * handles the audio codec sequences, while the audio driver handles the audio
  37. * controller sequences.
  38. *
  39. * The disable sequences must be performed before disabling the transcoder or
  40. * port. The enable sequences may only be performed after enabling the
  41. * transcoder and port, and after completed link training. Therefore the audio
  42. * enable/disable sequences are part of the modeset sequence.
  43. *
  44. * The codec and controller sequences could be done either parallel or serial,
  45. * but generally the ELDV/PD change in the codec sequence indicates to the audio
  46. * driver that the controller sequence should start. Indeed, most of the
  47. * co-operation between the graphics and audio drivers is handled via audio
  48. * related registers. (The notable exception is the power management, not
  49. * covered here.)
  50. *
  51. * The struct i915_audio_component is used to interact between the graphics
  52. * and audio drivers. The struct i915_audio_component_ops *ops in it is
  53. * defined in graphics driver and called in audio driver. The
  54. * struct i915_audio_component_audio_ops *audio_ops is called from i915 driver.
  55. */
  56. static const struct {
  57. int clock;
  58. u32 config;
  59. } hdmi_audio_clock[] = {
  60. { 25175, AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  61. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  62. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  63. { 27027, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  64. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  65. { 54054, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  66. { 74176, AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  67. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  68. { 148352, AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  69. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  70. };
  71. /* HDMI N/CTS table */
  72. #define TMDS_297M 297000
  73. #define TMDS_296M 296703
  74. #define TMDS_594M 594000
  75. #define TMDS_593M 593407
  76. static const struct {
  77. int sample_rate;
  78. int clock;
  79. int n;
  80. int cts;
  81. } aud_ncts[] = {
  82. { 44100, TMDS_296M, 4459, 234375 },
  83. { 44100, TMDS_297M, 4704, 247500 },
  84. { 48000, TMDS_296M, 5824, 281250 },
  85. { 48000, TMDS_297M, 5120, 247500 },
  86. { 32000, TMDS_296M, 5824, 421875 },
  87. { 32000, TMDS_297M, 3072, 222750 },
  88. { 88200, TMDS_296M, 8918, 234375 },
  89. { 88200, TMDS_297M, 9408, 247500 },
  90. { 96000, TMDS_296M, 11648, 281250 },
  91. { 96000, TMDS_297M, 10240, 247500 },
  92. { 176400, TMDS_296M, 17836, 234375 },
  93. { 176400, TMDS_297M, 18816, 247500 },
  94. { 192000, TMDS_296M, 23296, 281250 },
  95. { 192000, TMDS_297M, 20480, 247500 },
  96. { 44100, TMDS_593M, 8918, 937500 },
  97. { 44100, TMDS_594M, 9408, 990000 },
  98. { 48000, TMDS_593M, 5824, 562500 },
  99. { 48000, TMDS_594M, 6144, 594000 },
  100. { 32000, TMDS_593M, 5824, 843750 },
  101. { 32000, TMDS_594M, 3072, 445500 },
  102. { 88200, TMDS_593M, 17836, 937500 },
  103. { 88200, TMDS_594M, 18816, 990000 },
  104. { 96000, TMDS_593M, 11648, 562500 },
  105. { 96000, TMDS_594M, 12288, 594000 },
  106. { 176400, TMDS_593M, 35672, 937500 },
  107. { 176400, TMDS_594M, 37632, 990000 },
  108. { 192000, TMDS_593M, 23296, 562500 },
  109. { 192000, TMDS_594M, 24576, 594000 },
  110. };
  111. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  112. static u32 audio_config_hdmi_pixel_clock(const struct drm_display_mode *adjusted_mode)
  113. {
  114. int i;
  115. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  116. if (adjusted_mode->crtc_clock == hdmi_audio_clock[i].clock)
  117. break;
  118. }
  119. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  120. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n",
  121. adjusted_mode->crtc_clock);
  122. i = 1;
  123. }
  124. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  125. hdmi_audio_clock[i].clock,
  126. hdmi_audio_clock[i].config);
  127. return hdmi_audio_clock[i].config;
  128. }
  129. static int audio_config_get_n(const struct drm_display_mode *mode, int rate)
  130. {
  131. int i;
  132. for (i = 0; i < ARRAY_SIZE(aud_ncts); i++) {
  133. if ((rate == aud_ncts[i].sample_rate) &&
  134. (mode->clock == aud_ncts[i].clock)) {
  135. return aud_ncts[i].n;
  136. }
  137. }
  138. return 0;
  139. }
  140. static uint32_t audio_config_setup_n_reg(int n, uint32_t val)
  141. {
  142. int n_low, n_up;
  143. uint32_t tmp = val;
  144. n_low = n & 0xfff;
  145. n_up = (n >> 12) & 0xff;
  146. tmp &= ~(AUD_CONFIG_UPPER_N_MASK | AUD_CONFIG_LOWER_N_MASK);
  147. tmp |= ((n_up << AUD_CONFIG_UPPER_N_SHIFT) |
  148. (n_low << AUD_CONFIG_LOWER_N_SHIFT) |
  149. AUD_CONFIG_N_PROG_ENABLE);
  150. return tmp;
  151. }
  152. /* check whether N/CTS/M need be set manually */
  153. static bool audio_rate_need_prog(struct intel_crtc *crtc,
  154. const struct drm_display_mode *mode)
  155. {
  156. if (((mode->clock == TMDS_297M) ||
  157. (mode->clock == TMDS_296M)) &&
  158. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  159. return true;
  160. else
  161. return false;
  162. }
  163. static bool intel_eld_uptodate(struct drm_connector *connector,
  164. int reg_eldv, uint32_t bits_eldv,
  165. int reg_elda, uint32_t bits_elda,
  166. int reg_edid)
  167. {
  168. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  169. uint8_t *eld = connector->eld;
  170. uint32_t tmp;
  171. int i;
  172. tmp = I915_READ(reg_eldv);
  173. tmp &= bits_eldv;
  174. if (!tmp)
  175. return false;
  176. tmp = I915_READ(reg_elda);
  177. tmp &= ~bits_elda;
  178. I915_WRITE(reg_elda, tmp);
  179. for (i = 0; i < drm_eld_size(eld) / 4; i++)
  180. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  181. return false;
  182. return true;
  183. }
  184. static void g4x_audio_codec_disable(struct intel_encoder *encoder)
  185. {
  186. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  187. uint32_t eldv, tmp;
  188. DRM_DEBUG_KMS("Disable audio codec\n");
  189. tmp = I915_READ(G4X_AUD_VID_DID);
  190. if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
  191. eldv = G4X_ELDV_DEVCL_DEVBLC;
  192. else
  193. eldv = G4X_ELDV_DEVCTG;
  194. /* Invalidate ELD */
  195. tmp = I915_READ(G4X_AUD_CNTL_ST);
  196. tmp &= ~eldv;
  197. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  198. }
  199. static void g4x_audio_codec_enable(struct drm_connector *connector,
  200. struct intel_encoder *encoder,
  201. const struct drm_display_mode *adjusted_mode)
  202. {
  203. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  204. uint8_t *eld = connector->eld;
  205. uint32_t eldv;
  206. uint32_t tmp;
  207. int len, i;
  208. DRM_DEBUG_KMS("Enable audio codec, %u bytes ELD\n", eld[2]);
  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. if (intel_eld_uptodate(connector,
  215. G4X_AUD_CNTL_ST, eldv,
  216. G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
  217. G4X_HDMIW_HDMIEDID))
  218. return;
  219. tmp = I915_READ(G4X_AUD_CNTL_ST);
  220. tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
  221. len = (tmp >> 9) & 0x1f; /* ELD buffer size */
  222. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  223. len = min(drm_eld_size(eld) / 4, len);
  224. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  225. for (i = 0; i < len; i++)
  226. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  227. tmp = I915_READ(G4X_AUD_CNTL_ST);
  228. tmp |= eldv;
  229. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  230. }
  231. static void hsw_audio_codec_disable(struct intel_encoder *encoder)
  232. {
  233. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  234. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  235. enum pipe pipe = intel_crtc->pipe;
  236. uint32_t tmp;
  237. DRM_DEBUG_KMS("Disable audio codec on pipe %c\n", pipe_name(pipe));
  238. mutex_lock(&dev_priv->av_mutex);
  239. /* Disable timestamps */
  240. tmp = I915_READ(HSW_AUD_CFG(pipe));
  241. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  242. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  243. tmp &= ~AUD_CONFIG_UPPER_N_MASK;
  244. tmp &= ~AUD_CONFIG_LOWER_N_MASK;
  245. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  246. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  247. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  248. /* Invalidate ELD */
  249. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  250. tmp &= ~AUDIO_ELD_VALID(pipe);
  251. tmp &= ~AUDIO_OUTPUT_ENABLE(pipe);
  252. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  253. mutex_unlock(&dev_priv->av_mutex);
  254. }
  255. static void hsw_audio_codec_enable(struct drm_connector *connector,
  256. struct intel_encoder *encoder,
  257. const struct drm_display_mode *adjusted_mode)
  258. {
  259. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  260. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  261. enum pipe pipe = intel_crtc->pipe;
  262. struct i915_audio_component *acomp = dev_priv->audio_component;
  263. const uint8_t *eld = connector->eld;
  264. struct intel_digital_port *intel_dig_port =
  265. enc_to_dig_port(&encoder->base);
  266. enum port port = intel_dig_port->port;
  267. uint32_t tmp;
  268. int len, i;
  269. int n, rate;
  270. DRM_DEBUG_KMS("Enable audio codec on pipe %c, %u bytes ELD\n",
  271. pipe_name(pipe), drm_eld_size(eld));
  272. mutex_lock(&dev_priv->av_mutex);
  273. /* Enable audio presence detect, invalidate ELD */
  274. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  275. tmp |= AUDIO_OUTPUT_ENABLE(pipe);
  276. tmp &= ~AUDIO_ELD_VALID(pipe);
  277. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  278. /*
  279. * FIXME: We're supposed to wait for vblank here, but we have vblanks
  280. * disabled during the mode set. The proper fix would be to push the
  281. * rest of the setup into a vblank work item, queued here, but the
  282. * infrastructure is not there yet.
  283. */
  284. /* Reset ELD write address */
  285. tmp = I915_READ(HSW_AUD_DIP_ELD_CTRL(pipe));
  286. tmp &= ~IBX_ELD_ADDRESS_MASK;
  287. I915_WRITE(HSW_AUD_DIP_ELD_CTRL(pipe), tmp);
  288. /* Up to 84 bytes of hw ELD buffer */
  289. len = min(drm_eld_size(eld), 84);
  290. for (i = 0; i < len / 4; i++)
  291. I915_WRITE(HSW_AUD_EDID_DATA(pipe), *((uint32_t *)eld + i));
  292. /* ELD valid */
  293. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  294. tmp |= AUDIO_ELD_VALID(pipe);
  295. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  296. /* Enable timestamps */
  297. tmp = I915_READ(HSW_AUD_CFG(pipe));
  298. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  299. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  300. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  301. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  302. else
  303. tmp |= audio_config_hdmi_pixel_clock(adjusted_mode);
  304. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  305. if (audio_rate_need_prog(intel_crtc, adjusted_mode)) {
  306. if (!acomp)
  307. rate = 0;
  308. else if (port >= PORT_A && port <= PORT_E)
  309. rate = acomp->aud_sample_rate[port];
  310. else {
  311. DRM_ERROR("invalid port: %d\n", port);
  312. rate = 0;
  313. }
  314. n = audio_config_get_n(adjusted_mode, rate);
  315. if (n != 0)
  316. tmp = audio_config_setup_n_reg(n, tmp);
  317. else
  318. DRM_DEBUG_KMS("no suitable N value is found\n");
  319. }
  320. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  321. mutex_unlock(&dev_priv->av_mutex);
  322. }
  323. static void ilk_audio_codec_disable(struct intel_encoder *encoder)
  324. {
  325. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  326. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  327. struct intel_digital_port *intel_dig_port =
  328. enc_to_dig_port(&encoder->base);
  329. enum port port = intel_dig_port->port;
  330. enum pipe pipe = intel_crtc->pipe;
  331. uint32_t tmp, eldv;
  332. int aud_config;
  333. int aud_cntrl_st2;
  334. DRM_DEBUG_KMS("Disable audio codec on port %c, pipe %c\n",
  335. port_name(port), pipe_name(pipe));
  336. if (WARN_ON(port == PORT_A))
  337. return;
  338. if (HAS_PCH_IBX(dev_priv->dev)) {
  339. aud_config = IBX_AUD_CFG(pipe);
  340. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  341. } else if (IS_VALLEYVIEW(dev_priv)) {
  342. aud_config = VLV_AUD_CFG(pipe);
  343. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  344. } else {
  345. aud_config = CPT_AUD_CFG(pipe);
  346. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  347. }
  348. /* Disable timestamps */
  349. tmp = I915_READ(aud_config);
  350. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  351. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  352. tmp &= ~AUD_CONFIG_UPPER_N_MASK;
  353. tmp &= ~AUD_CONFIG_LOWER_N_MASK;
  354. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  355. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  356. I915_WRITE(aud_config, tmp);
  357. eldv = IBX_ELD_VALID(port);
  358. /* Invalidate ELD */
  359. tmp = I915_READ(aud_cntrl_st2);
  360. tmp &= ~eldv;
  361. I915_WRITE(aud_cntrl_st2, tmp);
  362. }
  363. static void ilk_audio_codec_enable(struct drm_connector *connector,
  364. struct intel_encoder *encoder,
  365. const struct drm_display_mode *adjusted_mode)
  366. {
  367. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  368. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  369. struct intel_digital_port *intel_dig_port =
  370. enc_to_dig_port(&encoder->base);
  371. enum port port = intel_dig_port->port;
  372. enum pipe pipe = intel_crtc->pipe;
  373. uint8_t *eld = connector->eld;
  374. uint32_t eldv;
  375. uint32_t tmp;
  376. int len, i;
  377. int hdmiw_hdmiedid;
  378. int aud_config;
  379. int aud_cntl_st;
  380. int aud_cntrl_st2;
  381. DRM_DEBUG_KMS("Enable audio codec on port %c, pipe %c, %u bytes ELD\n",
  382. port_name(port), pipe_name(pipe), drm_eld_size(eld));
  383. if (WARN_ON(port == PORT_A))
  384. return;
  385. /*
  386. * FIXME: We're supposed to wait for vblank here, but we have vblanks
  387. * disabled during the mode set. The proper fix would be to push the
  388. * rest of the setup into a vblank work item, queued here, but the
  389. * infrastructure is not there yet.
  390. */
  391. if (HAS_PCH_IBX(connector->dev)) {
  392. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  393. aud_config = IBX_AUD_CFG(pipe);
  394. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  395. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  396. } else if (IS_VALLEYVIEW(connector->dev)) {
  397. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  398. aud_config = VLV_AUD_CFG(pipe);
  399. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  400. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  401. } else {
  402. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  403. aud_config = CPT_AUD_CFG(pipe);
  404. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  405. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  406. }
  407. eldv = IBX_ELD_VALID(port);
  408. /* Invalidate ELD */
  409. tmp = I915_READ(aud_cntrl_st2);
  410. tmp &= ~eldv;
  411. I915_WRITE(aud_cntrl_st2, tmp);
  412. /* Reset ELD write address */
  413. tmp = I915_READ(aud_cntl_st);
  414. tmp &= ~IBX_ELD_ADDRESS_MASK;
  415. I915_WRITE(aud_cntl_st, tmp);
  416. /* Up to 84 bytes of hw ELD buffer */
  417. len = min(drm_eld_size(eld), 84);
  418. for (i = 0; i < len / 4; i++)
  419. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  420. /* ELD valid */
  421. tmp = I915_READ(aud_cntrl_st2);
  422. tmp |= eldv;
  423. I915_WRITE(aud_cntrl_st2, tmp);
  424. /* Enable timestamps */
  425. tmp = I915_READ(aud_config);
  426. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  427. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  428. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  429. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  430. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  431. else
  432. tmp |= audio_config_hdmi_pixel_clock(adjusted_mode);
  433. I915_WRITE(aud_config, tmp);
  434. }
  435. /**
  436. * intel_audio_codec_enable - Enable the audio codec for HD audio
  437. * @intel_encoder: encoder on which to enable audio
  438. *
  439. * The enable sequences may only be performed after enabling the transcoder and
  440. * port, and after completed link training.
  441. */
  442. void intel_audio_codec_enable(struct intel_encoder *intel_encoder)
  443. {
  444. struct drm_encoder *encoder = &intel_encoder->base;
  445. struct intel_crtc *crtc = to_intel_crtc(encoder->crtc);
  446. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  447. struct drm_connector *connector;
  448. struct drm_device *dev = encoder->dev;
  449. struct drm_i915_private *dev_priv = dev->dev_private;
  450. struct i915_audio_component *acomp = dev_priv->audio_component;
  451. struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
  452. enum port port = intel_dig_port->port;
  453. connector = drm_select_eld(encoder);
  454. if (!connector)
  455. return;
  456. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  457. connector->base.id,
  458. connector->name,
  459. connector->encoder->base.id,
  460. connector->encoder->name);
  461. /* ELD Conn_Type */
  462. connector->eld[5] &= ~(3 << 2);
  463. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  464. connector->eld[5] |= (1 << 2);
  465. connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
  466. if (dev_priv->display.audio_codec_enable)
  467. dev_priv->display.audio_codec_enable(connector, intel_encoder,
  468. adjusted_mode);
  469. if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
  470. acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr, (int) port);
  471. }
  472. /**
  473. * intel_audio_codec_disable - Disable the audio codec for HD audio
  474. * @intel_encoder: encoder on which to disable audio
  475. *
  476. * The disable sequences must be performed before disabling the transcoder or
  477. * port.
  478. */
  479. void intel_audio_codec_disable(struct intel_encoder *intel_encoder)
  480. {
  481. struct drm_encoder *encoder = &intel_encoder->base;
  482. struct drm_device *dev = encoder->dev;
  483. struct drm_i915_private *dev_priv = dev->dev_private;
  484. struct i915_audio_component *acomp = dev_priv->audio_component;
  485. struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
  486. enum port port = intel_dig_port->port;
  487. if (dev_priv->display.audio_codec_disable)
  488. dev_priv->display.audio_codec_disable(intel_encoder);
  489. if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
  490. acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr, (int) port);
  491. }
  492. /**
  493. * intel_init_audio - Set up chip specific audio functions
  494. * @dev: drm device
  495. */
  496. void intel_init_audio(struct drm_device *dev)
  497. {
  498. struct drm_i915_private *dev_priv = dev->dev_private;
  499. if (IS_G4X(dev)) {
  500. dev_priv->display.audio_codec_enable = g4x_audio_codec_enable;
  501. dev_priv->display.audio_codec_disable = g4x_audio_codec_disable;
  502. } else if (IS_VALLEYVIEW(dev)) {
  503. dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
  504. dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
  505. } else if (IS_HASWELL(dev) || INTEL_INFO(dev)->gen >= 8) {
  506. dev_priv->display.audio_codec_enable = hsw_audio_codec_enable;
  507. dev_priv->display.audio_codec_disable = hsw_audio_codec_disable;
  508. } else if (HAS_PCH_SPLIT(dev)) {
  509. dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
  510. dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
  511. }
  512. }
  513. static void i915_audio_component_get_power(struct device *dev)
  514. {
  515. intel_display_power_get(dev_to_i915(dev), POWER_DOMAIN_AUDIO);
  516. }
  517. static void i915_audio_component_put_power(struct device *dev)
  518. {
  519. intel_display_power_put(dev_to_i915(dev), POWER_DOMAIN_AUDIO);
  520. }
  521. static void i915_audio_component_codec_wake_override(struct device *dev,
  522. bool enable)
  523. {
  524. struct drm_i915_private *dev_priv = dev_to_i915(dev);
  525. u32 tmp;
  526. if (!IS_SKYLAKE(dev_priv))
  527. return;
  528. /*
  529. * Enable/disable generating the codec wake signal, overriding the
  530. * internal logic to generate the codec wake to controller.
  531. */
  532. tmp = I915_READ(HSW_AUD_CHICKENBIT);
  533. tmp &= ~SKL_AUD_CODEC_WAKE_SIGNAL;
  534. I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
  535. usleep_range(1000, 1500);
  536. if (enable) {
  537. tmp = I915_READ(HSW_AUD_CHICKENBIT);
  538. tmp |= SKL_AUD_CODEC_WAKE_SIGNAL;
  539. I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
  540. usleep_range(1000, 1500);
  541. }
  542. }
  543. /* Get CDCLK in kHz */
  544. static int i915_audio_component_get_cdclk_freq(struct device *dev)
  545. {
  546. struct drm_i915_private *dev_priv = dev_to_i915(dev);
  547. int ret;
  548. if (WARN_ON_ONCE(!HAS_DDI(dev_priv)))
  549. return -ENODEV;
  550. intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
  551. ret = dev_priv->display.get_display_clock_speed(dev_priv->dev);
  552. intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO);
  553. return ret;
  554. }
  555. static int i915_audio_component_sync_audio_rate(struct device *dev,
  556. int port, int rate)
  557. {
  558. struct drm_i915_private *dev_priv = dev_to_i915(dev);
  559. struct drm_device *drm_dev = dev_priv->dev;
  560. struct intel_encoder *intel_encoder;
  561. struct intel_digital_port *intel_dig_port;
  562. struct intel_crtc *crtc;
  563. struct drm_display_mode *mode;
  564. struct i915_audio_component *acomp = dev_priv->audio_component;
  565. enum pipe pipe = -1;
  566. u32 tmp;
  567. int n;
  568. /* HSW, BDW SKL need this fix */
  569. if (!IS_SKYLAKE(dev_priv) &&
  570. !IS_BROADWELL(dev_priv) &&
  571. !IS_HASWELL(dev_priv))
  572. return 0;
  573. mutex_lock(&dev_priv->av_mutex);
  574. /* 1. get the pipe */
  575. for_each_intel_encoder(drm_dev, intel_encoder) {
  576. if (intel_encoder->type != INTEL_OUTPUT_HDMI)
  577. continue;
  578. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  579. if (port == intel_dig_port->port) {
  580. crtc = to_intel_crtc(intel_encoder->base.crtc);
  581. if (!crtc) {
  582. DRM_DEBUG_KMS("%s: crtc is NULL\n", __func__);
  583. continue;
  584. }
  585. pipe = crtc->pipe;
  586. break;
  587. }
  588. }
  589. if (pipe == INVALID_PIPE) {
  590. DRM_DEBUG_KMS("no pipe for the port %c\n", port_name(port));
  591. mutex_unlock(&dev_priv->av_mutex);
  592. return -ENODEV;
  593. }
  594. DRM_DEBUG_KMS("pipe %c connects port %c\n",
  595. pipe_name(pipe), port_name(port));
  596. mode = &crtc->config->base.adjusted_mode;
  597. /* port must be valid now, otherwise the pipe will be invalid */
  598. acomp->aud_sample_rate[port] = rate;
  599. /* 2. check whether to set the N/CTS/M manually or not */
  600. if (!audio_rate_need_prog(crtc, mode)) {
  601. tmp = I915_READ(HSW_AUD_CFG(pipe));
  602. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  603. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  604. mutex_unlock(&dev_priv->av_mutex);
  605. return 0;
  606. }
  607. n = audio_config_get_n(mode, rate);
  608. if (n == 0) {
  609. DRM_DEBUG_KMS("Using automatic mode for N value on port %c\n",
  610. port_name(port));
  611. tmp = I915_READ(HSW_AUD_CFG(pipe));
  612. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  613. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  614. mutex_unlock(&dev_priv->av_mutex);
  615. return 0;
  616. }
  617. /* 3. set the N/CTS/M */
  618. tmp = I915_READ(HSW_AUD_CFG(pipe));
  619. tmp = audio_config_setup_n_reg(n, tmp);
  620. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  621. mutex_unlock(&dev_priv->av_mutex);
  622. return 0;
  623. }
  624. static const struct i915_audio_component_ops i915_audio_component_ops = {
  625. .owner = THIS_MODULE,
  626. .get_power = i915_audio_component_get_power,
  627. .put_power = i915_audio_component_put_power,
  628. .codec_wake_override = i915_audio_component_codec_wake_override,
  629. .get_cdclk_freq = i915_audio_component_get_cdclk_freq,
  630. .sync_audio_rate = i915_audio_component_sync_audio_rate,
  631. };
  632. static int i915_audio_component_bind(struct device *i915_dev,
  633. struct device *hda_dev, void *data)
  634. {
  635. struct i915_audio_component *acomp = data;
  636. struct drm_i915_private *dev_priv = dev_to_i915(i915_dev);
  637. int i;
  638. if (WARN_ON(acomp->ops || acomp->dev))
  639. return -EEXIST;
  640. drm_modeset_lock_all(dev_priv->dev);
  641. acomp->ops = &i915_audio_component_ops;
  642. acomp->dev = i915_dev;
  643. BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
  644. for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
  645. acomp->aud_sample_rate[i] = 0;
  646. dev_priv->audio_component = acomp;
  647. drm_modeset_unlock_all(dev_priv->dev);
  648. return 0;
  649. }
  650. static void i915_audio_component_unbind(struct device *i915_dev,
  651. struct device *hda_dev, void *data)
  652. {
  653. struct i915_audio_component *acomp = data;
  654. struct drm_i915_private *dev_priv = dev_to_i915(i915_dev);
  655. drm_modeset_lock_all(dev_priv->dev);
  656. acomp->ops = NULL;
  657. acomp->dev = NULL;
  658. dev_priv->audio_component = NULL;
  659. drm_modeset_unlock_all(dev_priv->dev);
  660. }
  661. static const struct component_ops i915_audio_component_bind_ops = {
  662. .bind = i915_audio_component_bind,
  663. .unbind = i915_audio_component_unbind,
  664. };
  665. /**
  666. * i915_audio_component_init - initialize and register the audio component
  667. * @dev_priv: i915 device instance
  668. *
  669. * This will register with the component framework a child component which
  670. * will bind dynamically to the snd_hda_intel driver's corresponding master
  671. * component when the latter is registered. During binding the child
  672. * initializes an instance of struct i915_audio_component which it receives
  673. * from the master. The master can then start to use the interface defined by
  674. * this struct. Each side can break the binding at any point by deregistering
  675. * its own component after which each side's component unbind callback is
  676. * called.
  677. *
  678. * We ignore any error during registration and continue with reduced
  679. * functionality (i.e. without HDMI audio).
  680. */
  681. void i915_audio_component_init(struct drm_i915_private *dev_priv)
  682. {
  683. int ret;
  684. ret = component_add(dev_priv->dev->dev, &i915_audio_component_bind_ops);
  685. if (ret < 0) {
  686. DRM_ERROR("failed to add audio component (%d)\n", ret);
  687. /* continue with reduced functionality */
  688. return;
  689. }
  690. dev_priv->audio_component_registered = true;
  691. }
  692. /**
  693. * i915_audio_component_cleanup - deregister the audio component
  694. * @dev_priv: i915 device instance
  695. *
  696. * Deregisters the audio component, breaking any existing binding to the
  697. * corresponding snd_hda_intel driver's master component.
  698. */
  699. void i915_audio_component_cleanup(struct drm_i915_private *dev_priv)
  700. {
  701. if (!dev_priv->audio_component_registered)
  702. return;
  703. component_del(dev_priv->dev->dev, &i915_audio_component_bind_ops);
  704. dev_priv->audio_component_registered = false;
  705. }