intel_audio.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. { DIV_ROUND_UP(25200 * 1000, 1001), 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. { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  64. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  65. { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  66. { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  67. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  68. { DIV_ROUND_UP(148500 * 1000, 1001), 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 DIV_ROUND_UP(297000 * 1000, 1001)
  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. int reg_eldv, uint32_t bits_eldv,
  149. int reg_elda, uint32_t bits_elda,
  150. int 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. int aud_config;
  317. int aud_cntrl_st2;
  318. DRM_DEBUG_KMS("Disable audio codec on port %c, pipe %c\n",
  319. port_name(port), pipe_name(pipe));
  320. if (WARN_ON(port == PORT_A))
  321. return;
  322. if (HAS_PCH_IBX(dev_priv->dev)) {
  323. aud_config = IBX_AUD_CFG(pipe);
  324. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  325. } else if (IS_VALLEYVIEW(dev_priv)) {
  326. aud_config = VLV_AUD_CFG(pipe);
  327. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  328. } else {
  329. aud_config = CPT_AUD_CFG(pipe);
  330. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  331. }
  332. /* Disable timestamps */
  333. tmp = I915_READ(aud_config);
  334. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  335. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  336. tmp &= ~AUD_CONFIG_UPPER_N_MASK;
  337. tmp &= ~AUD_CONFIG_LOWER_N_MASK;
  338. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  339. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  340. I915_WRITE(aud_config, tmp);
  341. eldv = IBX_ELD_VALID(port);
  342. /* Invalidate ELD */
  343. tmp = I915_READ(aud_cntrl_st2);
  344. tmp &= ~eldv;
  345. I915_WRITE(aud_cntrl_st2, tmp);
  346. }
  347. static void ilk_audio_codec_enable(struct drm_connector *connector,
  348. struct intel_encoder *encoder,
  349. const struct drm_display_mode *adjusted_mode)
  350. {
  351. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  352. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  353. struct intel_digital_port *intel_dig_port =
  354. enc_to_dig_port(&encoder->base);
  355. enum port port = intel_dig_port->port;
  356. enum pipe pipe = intel_crtc->pipe;
  357. uint8_t *eld = connector->eld;
  358. uint32_t eldv;
  359. uint32_t tmp;
  360. int len, i;
  361. int hdmiw_hdmiedid;
  362. int aud_config;
  363. int aud_cntl_st;
  364. int aud_cntrl_st2;
  365. DRM_DEBUG_KMS("Enable audio codec on port %c, pipe %c, %u bytes ELD\n",
  366. port_name(port), pipe_name(pipe), drm_eld_size(eld));
  367. if (WARN_ON(port == PORT_A))
  368. return;
  369. /*
  370. * FIXME: We're supposed to wait for vblank here, but we have vblanks
  371. * disabled during the mode set. The proper fix would be to push the
  372. * rest of the setup into a vblank work item, queued here, but the
  373. * infrastructure is not there yet.
  374. */
  375. if (HAS_PCH_IBX(connector->dev)) {
  376. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  377. aud_config = IBX_AUD_CFG(pipe);
  378. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  379. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  380. } else if (IS_VALLEYVIEW(connector->dev)) {
  381. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  382. aud_config = VLV_AUD_CFG(pipe);
  383. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  384. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  385. } else {
  386. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  387. aud_config = CPT_AUD_CFG(pipe);
  388. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  389. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  390. }
  391. eldv = IBX_ELD_VALID(port);
  392. /* Invalidate ELD */
  393. tmp = I915_READ(aud_cntrl_st2);
  394. tmp &= ~eldv;
  395. I915_WRITE(aud_cntrl_st2, tmp);
  396. /* Reset ELD write address */
  397. tmp = I915_READ(aud_cntl_st);
  398. tmp &= ~IBX_ELD_ADDRESS_MASK;
  399. I915_WRITE(aud_cntl_st, tmp);
  400. /* Up to 84 bytes of hw ELD buffer */
  401. len = min(drm_eld_size(eld), 84);
  402. for (i = 0; i < len / 4; i++)
  403. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  404. /* ELD valid */
  405. tmp = I915_READ(aud_cntrl_st2);
  406. tmp |= eldv;
  407. I915_WRITE(aud_cntrl_st2, tmp);
  408. /* Enable timestamps */
  409. tmp = I915_READ(aud_config);
  410. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  411. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  412. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  413. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  414. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  415. else
  416. tmp |= audio_config_hdmi_pixel_clock(adjusted_mode);
  417. I915_WRITE(aud_config, tmp);
  418. }
  419. /**
  420. * intel_audio_codec_enable - Enable the audio codec for HD audio
  421. * @intel_encoder: encoder on which to enable audio
  422. *
  423. * The enable sequences may only be performed after enabling the transcoder and
  424. * port, and after completed link training.
  425. */
  426. void intel_audio_codec_enable(struct intel_encoder *intel_encoder)
  427. {
  428. struct drm_encoder *encoder = &intel_encoder->base;
  429. struct intel_crtc *crtc = to_intel_crtc(encoder->crtc);
  430. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  431. struct drm_connector *connector;
  432. struct drm_device *dev = encoder->dev;
  433. struct drm_i915_private *dev_priv = dev->dev_private;
  434. struct i915_audio_component *acomp = dev_priv->audio_component;
  435. struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
  436. enum port port = intel_dig_port->port;
  437. connector = drm_select_eld(encoder);
  438. if (!connector)
  439. return;
  440. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  441. connector->base.id,
  442. connector->name,
  443. connector->encoder->base.id,
  444. connector->encoder->name);
  445. /* ELD Conn_Type */
  446. connector->eld[5] &= ~(3 << 2);
  447. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  448. connector->eld[5] |= (1 << 2);
  449. connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
  450. if (dev_priv->display.audio_codec_enable)
  451. dev_priv->display.audio_codec_enable(connector, intel_encoder,
  452. adjusted_mode);
  453. if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
  454. acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr, (int) port);
  455. }
  456. /**
  457. * intel_audio_codec_disable - Disable the audio codec for HD audio
  458. * @intel_encoder: encoder on which to disable audio
  459. *
  460. * The disable sequences must be performed before disabling the transcoder or
  461. * port.
  462. */
  463. void intel_audio_codec_disable(struct intel_encoder *intel_encoder)
  464. {
  465. struct drm_encoder *encoder = &intel_encoder->base;
  466. struct drm_device *dev = encoder->dev;
  467. struct drm_i915_private *dev_priv = dev->dev_private;
  468. struct i915_audio_component *acomp = dev_priv->audio_component;
  469. struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
  470. enum port port = intel_dig_port->port;
  471. if (dev_priv->display.audio_codec_disable)
  472. dev_priv->display.audio_codec_disable(intel_encoder);
  473. if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
  474. acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr, (int) port);
  475. }
  476. /**
  477. * intel_init_audio - Set up chip specific audio functions
  478. * @dev: drm device
  479. */
  480. void intel_init_audio(struct drm_device *dev)
  481. {
  482. struct drm_i915_private *dev_priv = dev->dev_private;
  483. if (IS_G4X(dev)) {
  484. dev_priv->display.audio_codec_enable = g4x_audio_codec_enable;
  485. dev_priv->display.audio_codec_disable = g4x_audio_codec_disable;
  486. } else if (IS_VALLEYVIEW(dev)) {
  487. dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
  488. dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
  489. } else if (IS_HASWELL(dev) || INTEL_INFO(dev)->gen >= 8) {
  490. dev_priv->display.audio_codec_enable = hsw_audio_codec_enable;
  491. dev_priv->display.audio_codec_disable = hsw_audio_codec_disable;
  492. } else if (HAS_PCH_SPLIT(dev)) {
  493. dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
  494. dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
  495. }
  496. }
  497. static void i915_audio_component_get_power(struct device *dev)
  498. {
  499. intel_display_power_get(dev_to_i915(dev), POWER_DOMAIN_AUDIO);
  500. }
  501. static void i915_audio_component_put_power(struct device *dev)
  502. {
  503. intel_display_power_put(dev_to_i915(dev), POWER_DOMAIN_AUDIO);
  504. }
  505. static void i915_audio_component_codec_wake_override(struct device *dev,
  506. bool enable)
  507. {
  508. struct drm_i915_private *dev_priv = dev_to_i915(dev);
  509. u32 tmp;
  510. if (!IS_SKYLAKE(dev_priv) && !IS_KABYLAKE(dev_priv))
  511. return;
  512. /*
  513. * Enable/disable generating the codec wake signal, overriding the
  514. * internal logic to generate the codec wake to controller.
  515. */
  516. tmp = I915_READ(HSW_AUD_CHICKENBIT);
  517. tmp &= ~SKL_AUD_CODEC_WAKE_SIGNAL;
  518. I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
  519. usleep_range(1000, 1500);
  520. if (enable) {
  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. }
  526. }
  527. /* Get CDCLK in kHz */
  528. static int i915_audio_component_get_cdclk_freq(struct device *dev)
  529. {
  530. struct drm_i915_private *dev_priv = dev_to_i915(dev);
  531. int ret;
  532. if (WARN_ON_ONCE(!HAS_DDI(dev_priv)))
  533. return -ENODEV;
  534. intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
  535. ret = dev_priv->display.get_display_clock_speed(dev_priv->dev);
  536. intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO);
  537. return ret;
  538. }
  539. static int i915_audio_component_sync_audio_rate(struct device *dev,
  540. int port, int rate)
  541. {
  542. struct drm_i915_private *dev_priv = dev_to_i915(dev);
  543. struct drm_device *drm_dev = dev_priv->dev;
  544. struct intel_encoder *intel_encoder;
  545. struct intel_digital_port *intel_dig_port;
  546. struct intel_crtc *crtc;
  547. struct drm_display_mode *mode;
  548. struct i915_audio_component *acomp = dev_priv->audio_component;
  549. enum pipe pipe = -1;
  550. u32 tmp;
  551. int n;
  552. /* HSW, BDW, SKL, KBL need this fix */
  553. if (!IS_SKYLAKE(dev_priv) &&
  554. !IS_KABYLAKE(dev_priv) &&
  555. !IS_BROADWELL(dev_priv) &&
  556. !IS_HASWELL(dev_priv))
  557. return 0;
  558. mutex_lock(&dev_priv->av_mutex);
  559. /* 1. get the pipe */
  560. for_each_intel_encoder(drm_dev, intel_encoder) {
  561. if (intel_encoder->type != INTEL_OUTPUT_HDMI)
  562. continue;
  563. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  564. if (port == intel_dig_port->port) {
  565. crtc = to_intel_crtc(intel_encoder->base.crtc);
  566. if (!crtc) {
  567. DRM_DEBUG_KMS("%s: crtc is NULL\n", __func__);
  568. continue;
  569. }
  570. pipe = crtc->pipe;
  571. break;
  572. }
  573. }
  574. if (pipe == INVALID_PIPE) {
  575. DRM_DEBUG_KMS("no pipe for the port %c\n", port_name(port));
  576. mutex_unlock(&dev_priv->av_mutex);
  577. return -ENODEV;
  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. mutex_unlock(&dev_priv->av_mutex);
  590. return 0;
  591. }
  592. n = audio_config_get_n(mode, rate);
  593. if (n == 0) {
  594. DRM_DEBUG_KMS("Using automatic mode for N value on port %c\n",
  595. port_name(port));
  596. tmp = I915_READ(HSW_AUD_CFG(pipe));
  597. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  598. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  599. mutex_unlock(&dev_priv->av_mutex);
  600. return 0;
  601. }
  602. /* 3. set the N/CTS/M */
  603. tmp = I915_READ(HSW_AUD_CFG(pipe));
  604. tmp = audio_config_setup_n_reg(n, tmp);
  605. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  606. mutex_unlock(&dev_priv->av_mutex);
  607. return 0;
  608. }
  609. static const struct i915_audio_component_ops i915_audio_component_ops = {
  610. .owner = THIS_MODULE,
  611. .get_power = i915_audio_component_get_power,
  612. .put_power = i915_audio_component_put_power,
  613. .codec_wake_override = i915_audio_component_codec_wake_override,
  614. .get_cdclk_freq = i915_audio_component_get_cdclk_freq,
  615. .sync_audio_rate = i915_audio_component_sync_audio_rate,
  616. };
  617. static int i915_audio_component_bind(struct device *i915_dev,
  618. struct device *hda_dev, void *data)
  619. {
  620. struct i915_audio_component *acomp = data;
  621. struct drm_i915_private *dev_priv = dev_to_i915(i915_dev);
  622. int i;
  623. if (WARN_ON(acomp->ops || acomp->dev))
  624. return -EEXIST;
  625. drm_modeset_lock_all(dev_priv->dev);
  626. acomp->ops = &i915_audio_component_ops;
  627. acomp->dev = i915_dev;
  628. BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
  629. for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
  630. acomp->aud_sample_rate[i] = 0;
  631. dev_priv->audio_component = acomp;
  632. drm_modeset_unlock_all(dev_priv->dev);
  633. return 0;
  634. }
  635. static void i915_audio_component_unbind(struct device *i915_dev,
  636. struct device *hda_dev, void *data)
  637. {
  638. struct i915_audio_component *acomp = data;
  639. struct drm_i915_private *dev_priv = dev_to_i915(i915_dev);
  640. drm_modeset_lock_all(dev_priv->dev);
  641. acomp->ops = NULL;
  642. acomp->dev = NULL;
  643. dev_priv->audio_component = NULL;
  644. drm_modeset_unlock_all(dev_priv->dev);
  645. }
  646. static const struct component_ops i915_audio_component_bind_ops = {
  647. .bind = i915_audio_component_bind,
  648. .unbind = i915_audio_component_unbind,
  649. };
  650. /**
  651. * i915_audio_component_init - initialize and register the audio component
  652. * @dev_priv: i915 device instance
  653. *
  654. * This will register with the component framework a child component which
  655. * will bind dynamically to the snd_hda_intel driver's corresponding master
  656. * component when the latter is registered. During binding the child
  657. * initializes an instance of struct i915_audio_component which it receives
  658. * from the master. The master can then start to use the interface defined by
  659. * this struct. Each side can break the binding at any point by deregistering
  660. * its own component after which each side's component unbind callback is
  661. * called.
  662. *
  663. * We ignore any error during registration and continue with reduced
  664. * functionality (i.e. without HDMI audio).
  665. */
  666. void i915_audio_component_init(struct drm_i915_private *dev_priv)
  667. {
  668. int ret;
  669. ret = component_add(dev_priv->dev->dev, &i915_audio_component_bind_ops);
  670. if (ret < 0) {
  671. DRM_ERROR("failed to add audio component (%d)\n", ret);
  672. /* continue with reduced functionality */
  673. return;
  674. }
  675. dev_priv->audio_component_registered = true;
  676. }
  677. /**
  678. * i915_audio_component_cleanup - deregister the audio component
  679. * @dev_priv: i915 device instance
  680. *
  681. * Deregisters the audio component, breaking any existing binding to the
  682. * corresponding snd_hda_intel driver's master component.
  683. */
  684. void i915_audio_component_cleanup(struct drm_i915_private *dev_priv)
  685. {
  686. if (!dev_priv->audio_component_registered)
  687. return;
  688. component_del(dev_priv->dev->dev, &i915_audio_component_bind_ops);
  689. dev_priv->audio_component_registered = false;
  690. }