intel_audio.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. static const struct {
  75. int sample_rate;
  76. int clock;
  77. int n;
  78. int cts;
  79. } aud_ncts[] = {
  80. { 44100, TMDS_296M, 4459, 234375 },
  81. { 44100, TMDS_297M, 4704, 247500 },
  82. { 48000, TMDS_296M, 5824, 281250 },
  83. { 48000, TMDS_297M, 5120, 247500 },
  84. { 32000, TMDS_296M, 5824, 421875 },
  85. { 32000, TMDS_297M, 3072, 222750 },
  86. { 88200, TMDS_296M, 8918, 234375 },
  87. { 88200, TMDS_297M, 9408, 247500 },
  88. { 96000, TMDS_296M, 11648, 281250 },
  89. { 96000, TMDS_297M, 10240, 247500 },
  90. { 176400, TMDS_296M, 17836, 234375 },
  91. { 176400, TMDS_297M, 18816, 247500 },
  92. { 192000, TMDS_296M, 23296, 281250 },
  93. { 192000, TMDS_297M, 20480, 247500 },
  94. };
  95. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  96. static u32 audio_config_hdmi_pixel_clock(const struct drm_display_mode *adjusted_mode)
  97. {
  98. int i;
  99. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  100. if (adjusted_mode->crtc_clock == hdmi_audio_clock[i].clock)
  101. break;
  102. }
  103. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  104. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n",
  105. adjusted_mode->crtc_clock);
  106. i = 1;
  107. }
  108. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  109. hdmi_audio_clock[i].clock,
  110. hdmi_audio_clock[i].config);
  111. return hdmi_audio_clock[i].config;
  112. }
  113. static int audio_config_get_n(const struct drm_display_mode *mode, int rate)
  114. {
  115. int i;
  116. for (i = 0; i < ARRAY_SIZE(aud_ncts); i++) {
  117. if ((rate == aud_ncts[i].sample_rate) &&
  118. (mode->clock == aud_ncts[i].clock)) {
  119. return aud_ncts[i].n;
  120. }
  121. }
  122. return 0;
  123. }
  124. static uint32_t audio_config_setup_n_reg(int n, uint32_t val)
  125. {
  126. int n_low, n_up;
  127. uint32_t tmp = val;
  128. n_low = n & 0xfff;
  129. n_up = (n >> 12) & 0xff;
  130. tmp &= ~(AUD_CONFIG_UPPER_N_MASK | AUD_CONFIG_LOWER_N_MASK);
  131. tmp |= ((n_up << AUD_CONFIG_UPPER_N_SHIFT) |
  132. (n_low << AUD_CONFIG_LOWER_N_SHIFT) |
  133. AUD_CONFIG_N_PROG_ENABLE);
  134. return tmp;
  135. }
  136. /* check whether N/CTS/M need be set manually */
  137. static bool audio_rate_need_prog(struct intel_crtc *crtc,
  138. const struct drm_display_mode *mode)
  139. {
  140. if (((mode->clock == TMDS_297M) ||
  141. (mode->clock == TMDS_296M)) &&
  142. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  143. return true;
  144. else
  145. return false;
  146. }
  147. static bool intel_eld_uptodate(struct drm_connector *connector,
  148. i915_reg_t reg_eldv, uint32_t bits_eldv,
  149. i915_reg_t reg_elda, uint32_t bits_elda,
  150. i915_reg_t reg_edid)
  151. {
  152. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  153. uint8_t *eld = connector->eld;
  154. uint32_t tmp;
  155. int i;
  156. tmp = I915_READ(reg_eldv);
  157. tmp &= bits_eldv;
  158. if (!tmp)
  159. return false;
  160. tmp = I915_READ(reg_elda);
  161. tmp &= ~bits_elda;
  162. I915_WRITE(reg_elda, tmp);
  163. for (i = 0; i < drm_eld_size(eld) / 4; i++)
  164. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  165. return false;
  166. return true;
  167. }
  168. static void g4x_audio_codec_disable(struct intel_encoder *encoder)
  169. {
  170. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  171. uint32_t eldv, tmp;
  172. DRM_DEBUG_KMS("Disable audio codec\n");
  173. tmp = I915_READ(G4X_AUD_VID_DID);
  174. if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
  175. eldv = G4X_ELDV_DEVCL_DEVBLC;
  176. else
  177. eldv = G4X_ELDV_DEVCTG;
  178. /* Invalidate ELD */
  179. tmp = I915_READ(G4X_AUD_CNTL_ST);
  180. tmp &= ~eldv;
  181. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  182. }
  183. static void g4x_audio_codec_enable(struct drm_connector *connector,
  184. struct intel_encoder *encoder,
  185. const struct drm_display_mode *adjusted_mode)
  186. {
  187. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  188. uint8_t *eld = connector->eld;
  189. uint32_t eldv;
  190. uint32_t tmp;
  191. int len, i;
  192. DRM_DEBUG_KMS("Enable audio codec, %u bytes ELD\n", eld[2]);
  193. tmp = I915_READ(G4X_AUD_VID_DID);
  194. if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
  195. eldv = G4X_ELDV_DEVCL_DEVBLC;
  196. else
  197. eldv = G4X_ELDV_DEVCTG;
  198. if (intel_eld_uptodate(connector,
  199. G4X_AUD_CNTL_ST, eldv,
  200. G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
  201. G4X_HDMIW_HDMIEDID))
  202. return;
  203. tmp = I915_READ(G4X_AUD_CNTL_ST);
  204. tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
  205. len = (tmp >> 9) & 0x1f; /* ELD buffer size */
  206. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  207. len = min(drm_eld_size(eld) / 4, len);
  208. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  209. for (i = 0; i < len; i++)
  210. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  211. tmp = I915_READ(G4X_AUD_CNTL_ST);
  212. tmp |= eldv;
  213. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  214. }
  215. static void hsw_audio_codec_disable(struct intel_encoder *encoder)
  216. {
  217. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  218. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  219. enum pipe pipe = intel_crtc->pipe;
  220. uint32_t tmp;
  221. DRM_DEBUG_KMS("Disable audio codec on pipe %c\n", pipe_name(pipe));
  222. mutex_lock(&dev_priv->av_mutex);
  223. /* Disable timestamps */
  224. tmp = I915_READ(HSW_AUD_CFG(pipe));
  225. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  226. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  227. tmp &= ~AUD_CONFIG_UPPER_N_MASK;
  228. tmp &= ~AUD_CONFIG_LOWER_N_MASK;
  229. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  230. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  231. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  232. /* Invalidate ELD */
  233. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  234. tmp &= ~AUDIO_ELD_VALID(pipe);
  235. tmp &= ~AUDIO_OUTPUT_ENABLE(pipe);
  236. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  237. mutex_unlock(&dev_priv->av_mutex);
  238. }
  239. static void hsw_audio_codec_enable(struct drm_connector *connector,
  240. struct intel_encoder *encoder,
  241. const struct drm_display_mode *adjusted_mode)
  242. {
  243. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  244. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  245. enum pipe pipe = intel_crtc->pipe;
  246. struct i915_audio_component *acomp = dev_priv->audio_component;
  247. const uint8_t *eld = connector->eld;
  248. struct intel_digital_port *intel_dig_port =
  249. enc_to_dig_port(&encoder->base);
  250. enum port port = intel_dig_port->port;
  251. uint32_t tmp;
  252. int len, i;
  253. int n, rate;
  254. DRM_DEBUG_KMS("Enable audio codec on pipe %c, %u bytes ELD\n",
  255. pipe_name(pipe), drm_eld_size(eld));
  256. mutex_lock(&dev_priv->av_mutex);
  257. /* Enable audio presence detect, invalidate ELD */
  258. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  259. tmp |= AUDIO_OUTPUT_ENABLE(pipe);
  260. tmp &= ~AUDIO_ELD_VALID(pipe);
  261. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  262. /*
  263. * FIXME: We're supposed to wait for vblank here, but we have vblanks
  264. * disabled during the mode set. The proper fix would be to push the
  265. * rest of the setup into a vblank work item, queued here, but the
  266. * infrastructure is not there yet.
  267. */
  268. /* Reset ELD write address */
  269. tmp = I915_READ(HSW_AUD_DIP_ELD_CTRL(pipe));
  270. tmp &= ~IBX_ELD_ADDRESS_MASK;
  271. I915_WRITE(HSW_AUD_DIP_ELD_CTRL(pipe), tmp);
  272. /* Up to 84 bytes of hw ELD buffer */
  273. len = min(drm_eld_size(eld), 84);
  274. for (i = 0; i < len / 4; i++)
  275. I915_WRITE(HSW_AUD_EDID_DATA(pipe), *((uint32_t *)eld + i));
  276. /* ELD valid */
  277. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  278. tmp |= AUDIO_ELD_VALID(pipe);
  279. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  280. /* Enable timestamps */
  281. tmp = I915_READ(HSW_AUD_CFG(pipe));
  282. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  283. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  284. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  285. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  286. else
  287. tmp |= audio_config_hdmi_pixel_clock(adjusted_mode);
  288. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  289. if (audio_rate_need_prog(intel_crtc, adjusted_mode)) {
  290. if (!acomp)
  291. rate = 0;
  292. else if (port >= PORT_A && port <= PORT_E)
  293. rate = acomp->aud_sample_rate[port];
  294. else {
  295. DRM_ERROR("invalid port: %d\n", port);
  296. rate = 0;
  297. }
  298. n = audio_config_get_n(adjusted_mode, rate);
  299. if (n != 0)
  300. tmp = audio_config_setup_n_reg(n, tmp);
  301. else
  302. DRM_DEBUG_KMS("no suitable N value is found\n");
  303. }
  304. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  305. mutex_unlock(&dev_priv->av_mutex);
  306. }
  307. static void ilk_audio_codec_disable(struct intel_encoder *encoder)
  308. {
  309. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  310. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  311. struct intel_digital_port *intel_dig_port =
  312. enc_to_dig_port(&encoder->base);
  313. enum port port = intel_dig_port->port;
  314. enum pipe pipe = intel_crtc->pipe;
  315. uint32_t tmp, eldv;
  316. i915_reg_t aud_config, aud_cntrl_st2;
  317. DRM_DEBUG_KMS("Disable audio codec on port %c, pipe %c\n",
  318. port_name(port), pipe_name(pipe));
  319. if (WARN_ON(port == PORT_A))
  320. return;
  321. if (HAS_PCH_IBX(dev_priv)) {
  322. aud_config = IBX_AUD_CFG(pipe);
  323. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  324. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  325. aud_config = VLV_AUD_CFG(pipe);
  326. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  327. } else {
  328. aud_config = CPT_AUD_CFG(pipe);
  329. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  330. }
  331. /* Disable timestamps */
  332. tmp = I915_READ(aud_config);
  333. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  334. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  335. tmp &= ~AUD_CONFIG_UPPER_N_MASK;
  336. tmp &= ~AUD_CONFIG_LOWER_N_MASK;
  337. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  338. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  339. I915_WRITE(aud_config, tmp);
  340. eldv = IBX_ELD_VALID(port);
  341. /* Invalidate ELD */
  342. tmp = I915_READ(aud_cntrl_st2);
  343. tmp &= ~eldv;
  344. I915_WRITE(aud_cntrl_st2, tmp);
  345. }
  346. static void ilk_audio_codec_enable(struct drm_connector *connector,
  347. struct intel_encoder *encoder,
  348. const struct drm_display_mode *adjusted_mode)
  349. {
  350. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  351. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  352. struct intel_digital_port *intel_dig_port =
  353. enc_to_dig_port(&encoder->base);
  354. enum port port = intel_dig_port->port;
  355. enum pipe pipe = intel_crtc->pipe;
  356. uint8_t *eld = connector->eld;
  357. uint32_t eldv;
  358. uint32_t tmp;
  359. int len, i;
  360. i915_reg_t hdmiw_hdmiedid, aud_config, aud_cntl_st, aud_cntrl_st2;
  361. DRM_DEBUG_KMS("Enable audio codec on port %c, pipe %c, %u bytes ELD\n",
  362. port_name(port), pipe_name(pipe), drm_eld_size(eld));
  363. if (WARN_ON(port == PORT_A))
  364. return;
  365. /*
  366. * FIXME: We're supposed to wait for vblank here, but we have vblanks
  367. * disabled during the mode set. The proper fix would be to push the
  368. * rest of the setup into a vblank work item, queued here, but the
  369. * infrastructure is not there yet.
  370. */
  371. if (HAS_PCH_IBX(connector->dev)) {
  372. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  373. aud_config = IBX_AUD_CFG(pipe);
  374. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  375. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  376. } else if (IS_VALLEYVIEW(connector->dev) ||
  377. IS_CHERRYVIEW(connector->dev)) {
  378. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  379. aud_config = VLV_AUD_CFG(pipe);
  380. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  381. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  382. } else {
  383. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  384. aud_config = CPT_AUD_CFG(pipe);
  385. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  386. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  387. }
  388. eldv = IBX_ELD_VALID(port);
  389. /* Invalidate ELD */
  390. tmp = I915_READ(aud_cntrl_st2);
  391. tmp &= ~eldv;
  392. I915_WRITE(aud_cntrl_st2, tmp);
  393. /* Reset ELD write address */
  394. tmp = I915_READ(aud_cntl_st);
  395. tmp &= ~IBX_ELD_ADDRESS_MASK;
  396. I915_WRITE(aud_cntl_st, tmp);
  397. /* Up to 84 bytes of hw ELD buffer */
  398. len = min(drm_eld_size(eld), 84);
  399. for (i = 0; i < len / 4; i++)
  400. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  401. /* ELD valid */
  402. tmp = I915_READ(aud_cntrl_st2);
  403. tmp |= eldv;
  404. I915_WRITE(aud_cntrl_st2, tmp);
  405. /* Enable timestamps */
  406. tmp = I915_READ(aud_config);
  407. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  408. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  409. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  410. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  411. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  412. else
  413. tmp |= audio_config_hdmi_pixel_clock(adjusted_mode);
  414. I915_WRITE(aud_config, tmp);
  415. }
  416. /**
  417. * intel_audio_codec_enable - Enable the audio codec for HD audio
  418. * @intel_encoder: encoder on which to enable audio
  419. *
  420. * The enable sequences may only be performed after enabling the transcoder and
  421. * port, and after completed link training.
  422. */
  423. void intel_audio_codec_enable(struct intel_encoder *intel_encoder)
  424. {
  425. struct drm_encoder *encoder = &intel_encoder->base;
  426. struct intel_crtc *crtc = to_intel_crtc(encoder->crtc);
  427. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  428. struct drm_connector *connector;
  429. struct drm_device *dev = encoder->dev;
  430. struct drm_i915_private *dev_priv = dev->dev_private;
  431. struct i915_audio_component *acomp = dev_priv->audio_component;
  432. struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
  433. enum port port = intel_dig_port->port;
  434. connector = drm_select_eld(encoder);
  435. if (!connector)
  436. return;
  437. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  438. connector->base.id,
  439. connector->name,
  440. connector->encoder->base.id,
  441. connector->encoder->name);
  442. /* ELD Conn_Type */
  443. connector->eld[5] &= ~(3 << 2);
  444. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  445. connector->eld[5] |= (1 << 2);
  446. connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
  447. if (dev_priv->display.audio_codec_enable)
  448. dev_priv->display.audio_codec_enable(connector, intel_encoder,
  449. adjusted_mode);
  450. mutex_lock(&dev_priv->av_mutex);
  451. intel_dig_port->audio_connector = connector;
  452. /* referred in audio callbacks */
  453. dev_priv->dig_port_map[port] = intel_encoder;
  454. mutex_unlock(&dev_priv->av_mutex);
  455. if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
  456. acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr, (int) port);
  457. }
  458. /**
  459. * intel_audio_codec_disable - Disable the audio codec for HD audio
  460. * @intel_encoder: encoder on which to disable audio
  461. *
  462. * The disable sequences must be performed before disabling the transcoder or
  463. * port.
  464. */
  465. void intel_audio_codec_disable(struct intel_encoder *intel_encoder)
  466. {
  467. struct drm_encoder *encoder = &intel_encoder->base;
  468. struct drm_device *dev = encoder->dev;
  469. struct drm_i915_private *dev_priv = dev->dev_private;
  470. struct i915_audio_component *acomp = dev_priv->audio_component;
  471. struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
  472. enum port port = intel_dig_port->port;
  473. if (dev_priv->display.audio_codec_disable)
  474. dev_priv->display.audio_codec_disable(intel_encoder);
  475. mutex_lock(&dev_priv->av_mutex);
  476. intel_dig_port->audio_connector = NULL;
  477. dev_priv->dig_port_map[port] = NULL;
  478. mutex_unlock(&dev_priv->av_mutex);
  479. if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
  480. acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr, (int) port);
  481. }
  482. /**
  483. * intel_init_audio_hooks - Set up chip specific audio hooks
  484. * @dev_priv: device private
  485. */
  486. void intel_init_audio_hooks(struct drm_i915_private *dev_priv)
  487. {
  488. if (IS_G4X(dev_priv)) {
  489. dev_priv->display.audio_codec_enable = g4x_audio_codec_enable;
  490. dev_priv->display.audio_codec_disable = g4x_audio_codec_disable;
  491. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  492. dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
  493. dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
  494. } else if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8) {
  495. dev_priv->display.audio_codec_enable = hsw_audio_codec_enable;
  496. dev_priv->display.audio_codec_disable = hsw_audio_codec_disable;
  497. } else if (HAS_PCH_SPLIT(dev_priv)) {
  498. dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
  499. dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
  500. }
  501. }
  502. static void i915_audio_component_get_power(struct device *dev)
  503. {
  504. intel_display_power_get(dev_to_i915(dev), POWER_DOMAIN_AUDIO);
  505. }
  506. static void i915_audio_component_put_power(struct device *dev)
  507. {
  508. intel_display_power_put(dev_to_i915(dev), POWER_DOMAIN_AUDIO);
  509. }
  510. static void i915_audio_component_codec_wake_override(struct device *dev,
  511. bool enable)
  512. {
  513. struct drm_i915_private *dev_priv = dev_to_i915(dev);
  514. u32 tmp;
  515. if (!IS_SKYLAKE(dev_priv) && !IS_KABYLAKE(dev_priv))
  516. return;
  517. /*
  518. * Enable/disable generating the codec wake signal, overriding the
  519. * internal logic to generate the codec wake to controller.
  520. */
  521. tmp = I915_READ(HSW_AUD_CHICKENBIT);
  522. tmp &= ~SKL_AUD_CODEC_WAKE_SIGNAL;
  523. I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
  524. usleep_range(1000, 1500);
  525. if (enable) {
  526. tmp = I915_READ(HSW_AUD_CHICKENBIT);
  527. tmp |= SKL_AUD_CODEC_WAKE_SIGNAL;
  528. I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
  529. usleep_range(1000, 1500);
  530. }
  531. }
  532. /* Get CDCLK in kHz */
  533. static int i915_audio_component_get_cdclk_freq(struct device *dev)
  534. {
  535. struct drm_i915_private *dev_priv = dev_to_i915(dev);
  536. int ret;
  537. if (WARN_ON_ONCE(!HAS_DDI(dev_priv)))
  538. return -ENODEV;
  539. intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
  540. ret = dev_priv->display.get_display_clock_speed(dev_priv->dev);
  541. intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO);
  542. return ret;
  543. }
  544. static int i915_audio_component_sync_audio_rate(struct device *dev,
  545. int port, int rate)
  546. {
  547. struct drm_i915_private *dev_priv = dev_to_i915(dev);
  548. struct intel_encoder *intel_encoder;
  549. struct intel_crtc *crtc;
  550. struct drm_display_mode *mode;
  551. struct i915_audio_component *acomp = dev_priv->audio_component;
  552. enum pipe pipe = INVALID_PIPE;
  553. u32 tmp;
  554. int n;
  555. int err = 0;
  556. /* HSW, BDW, SKL, KBL need this fix */
  557. if (!IS_SKYLAKE(dev_priv) &&
  558. !IS_KABYLAKE(dev_priv) &&
  559. !IS_BROADWELL(dev_priv) &&
  560. !IS_HASWELL(dev_priv))
  561. return 0;
  562. mutex_lock(&dev_priv->av_mutex);
  563. /* 1. get the pipe */
  564. intel_encoder = dev_priv->dig_port_map[port];
  565. /* intel_encoder might be NULL for DP MST */
  566. if (!intel_encoder || !intel_encoder->base.crtc ||
  567. intel_encoder->type != INTEL_OUTPUT_HDMI) {
  568. DRM_DEBUG_KMS("no valid port %c\n", port_name(port));
  569. err = -ENODEV;
  570. goto unlock;
  571. }
  572. crtc = to_intel_crtc(intel_encoder->base.crtc);
  573. pipe = crtc->pipe;
  574. if (pipe == INVALID_PIPE) {
  575. DRM_DEBUG_KMS("no pipe for the port %c\n", port_name(port));
  576. err = -ENODEV;
  577. goto unlock;
  578. }
  579. DRM_DEBUG_KMS("pipe %c connects port %c\n",
  580. pipe_name(pipe), port_name(port));
  581. mode = &crtc->config->base.adjusted_mode;
  582. /* port must be valid now, otherwise the pipe will be invalid */
  583. acomp->aud_sample_rate[port] = rate;
  584. /* 2. check whether to set the N/CTS/M manually or not */
  585. if (!audio_rate_need_prog(crtc, mode)) {
  586. tmp = I915_READ(HSW_AUD_CFG(pipe));
  587. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  588. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  589. goto unlock;
  590. }
  591. n = audio_config_get_n(mode, rate);
  592. if (n == 0) {
  593. DRM_DEBUG_KMS("Using automatic mode for N value on port %c\n",
  594. port_name(port));
  595. tmp = I915_READ(HSW_AUD_CFG(pipe));
  596. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  597. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  598. goto unlock;
  599. }
  600. /* 3. set the N/CTS/M */
  601. tmp = I915_READ(HSW_AUD_CFG(pipe));
  602. tmp = audio_config_setup_n_reg(n, tmp);
  603. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  604. unlock:
  605. mutex_unlock(&dev_priv->av_mutex);
  606. return err;
  607. }
  608. static int i915_audio_component_get_eld(struct device *dev, int port,
  609. bool *enabled,
  610. unsigned char *buf, int max_bytes)
  611. {
  612. struct drm_i915_private *dev_priv = dev_to_i915(dev);
  613. struct intel_encoder *intel_encoder;
  614. struct intel_digital_port *intel_dig_port;
  615. const u8 *eld;
  616. int ret = -EINVAL;
  617. mutex_lock(&dev_priv->av_mutex);
  618. intel_encoder = dev_priv->dig_port_map[port];
  619. /* intel_encoder might be NULL for DP MST */
  620. if (intel_encoder) {
  621. ret = 0;
  622. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  623. *enabled = intel_dig_port->audio_connector != NULL;
  624. if (*enabled) {
  625. eld = intel_dig_port->audio_connector->eld;
  626. ret = drm_eld_size(eld);
  627. memcpy(buf, eld, min(max_bytes, ret));
  628. }
  629. }
  630. mutex_unlock(&dev_priv->av_mutex);
  631. return ret;
  632. }
  633. static const struct i915_audio_component_ops i915_audio_component_ops = {
  634. .owner = THIS_MODULE,
  635. .get_power = i915_audio_component_get_power,
  636. .put_power = i915_audio_component_put_power,
  637. .codec_wake_override = i915_audio_component_codec_wake_override,
  638. .get_cdclk_freq = i915_audio_component_get_cdclk_freq,
  639. .sync_audio_rate = i915_audio_component_sync_audio_rate,
  640. .get_eld = i915_audio_component_get_eld,
  641. };
  642. static int i915_audio_component_bind(struct device *i915_dev,
  643. struct device *hda_dev, void *data)
  644. {
  645. struct i915_audio_component *acomp = data;
  646. struct drm_i915_private *dev_priv = dev_to_i915(i915_dev);
  647. int i;
  648. if (WARN_ON(acomp->ops || acomp->dev))
  649. return -EEXIST;
  650. drm_modeset_lock_all(dev_priv->dev);
  651. acomp->ops = &i915_audio_component_ops;
  652. acomp->dev = i915_dev;
  653. BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
  654. for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
  655. acomp->aud_sample_rate[i] = 0;
  656. dev_priv->audio_component = acomp;
  657. drm_modeset_unlock_all(dev_priv->dev);
  658. return 0;
  659. }
  660. static void i915_audio_component_unbind(struct device *i915_dev,
  661. struct device *hda_dev, void *data)
  662. {
  663. struct i915_audio_component *acomp = data;
  664. struct drm_i915_private *dev_priv = dev_to_i915(i915_dev);
  665. drm_modeset_lock_all(dev_priv->dev);
  666. acomp->ops = NULL;
  667. acomp->dev = NULL;
  668. dev_priv->audio_component = NULL;
  669. drm_modeset_unlock_all(dev_priv->dev);
  670. }
  671. static const struct component_ops i915_audio_component_bind_ops = {
  672. .bind = i915_audio_component_bind,
  673. .unbind = i915_audio_component_unbind,
  674. };
  675. /**
  676. * i915_audio_component_init - initialize and register the audio component
  677. * @dev_priv: i915 device instance
  678. *
  679. * This will register with the component framework a child component which
  680. * will bind dynamically to the snd_hda_intel driver's corresponding master
  681. * component when the latter is registered. During binding the child
  682. * initializes an instance of struct i915_audio_component which it receives
  683. * from the master. The master can then start to use the interface defined by
  684. * this struct. Each side can break the binding at any point by deregistering
  685. * its own component after which each side's component unbind callback is
  686. * called.
  687. *
  688. * We ignore any error during registration and continue with reduced
  689. * functionality (i.e. without HDMI audio).
  690. */
  691. void i915_audio_component_init(struct drm_i915_private *dev_priv)
  692. {
  693. int ret;
  694. ret = component_add(dev_priv->dev->dev, &i915_audio_component_bind_ops);
  695. if (ret < 0) {
  696. DRM_ERROR("failed to add audio component (%d)\n", ret);
  697. /* continue with reduced functionality */
  698. return;
  699. }
  700. dev_priv->audio_component_registered = true;
  701. }
  702. /**
  703. * i915_audio_component_cleanup - deregister the audio component
  704. * @dev_priv: i915 device instance
  705. *
  706. * Deregisters the audio component, breaking any existing binding to the
  707. * corresponding snd_hda_intel driver's master component.
  708. */
  709. void i915_audio_component_cleanup(struct drm_i915_private *dev_priv)
  710. {
  711. if (!dev_priv->audio_component_registered)
  712. return;
  713. component_del(dev_priv->dev->dev, &i915_audio_component_bind_ops);
  714. dev_priv->audio_component_registered = false;
  715. }