intel_audio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. static const struct {
  52. int clock;
  53. u32 config;
  54. } hdmi_audio_clock[] = {
  55. { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  56. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  57. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  58. { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  59. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  60. { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  61. { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  62. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  63. { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  64. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  65. };
  66. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  67. static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
  68. {
  69. int i;
  70. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  71. if (mode->clock == hdmi_audio_clock[i].clock)
  72. break;
  73. }
  74. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  75. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
  76. i = 1;
  77. }
  78. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  79. hdmi_audio_clock[i].clock,
  80. hdmi_audio_clock[i].config);
  81. return hdmi_audio_clock[i].config;
  82. }
  83. static bool intel_eld_uptodate(struct drm_connector *connector,
  84. int reg_eldv, uint32_t bits_eldv,
  85. int reg_elda, uint32_t bits_elda,
  86. int reg_edid)
  87. {
  88. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  89. uint8_t *eld = connector->eld;
  90. uint32_t tmp;
  91. int i;
  92. tmp = I915_READ(reg_eldv);
  93. tmp &= bits_eldv;
  94. if (!tmp)
  95. return false;
  96. tmp = I915_READ(reg_elda);
  97. tmp &= ~bits_elda;
  98. I915_WRITE(reg_elda, tmp);
  99. for (i = 0; i < drm_eld_size(eld) / 4; i++)
  100. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  101. return false;
  102. return true;
  103. }
  104. static void g4x_audio_codec_disable(struct intel_encoder *encoder)
  105. {
  106. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  107. uint32_t eldv, tmp;
  108. DRM_DEBUG_KMS("Disable audio codec\n");
  109. tmp = I915_READ(G4X_AUD_VID_DID);
  110. if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
  111. eldv = G4X_ELDV_DEVCL_DEVBLC;
  112. else
  113. eldv = G4X_ELDV_DEVCTG;
  114. /* Invalidate ELD */
  115. tmp = I915_READ(G4X_AUD_CNTL_ST);
  116. tmp &= ~eldv;
  117. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  118. }
  119. static void g4x_audio_codec_enable(struct drm_connector *connector,
  120. struct intel_encoder *encoder,
  121. struct drm_display_mode *mode)
  122. {
  123. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  124. uint8_t *eld = connector->eld;
  125. uint32_t eldv;
  126. uint32_t tmp;
  127. int len, i;
  128. DRM_DEBUG_KMS("Enable audio codec, %u bytes ELD\n", eld[2]);
  129. tmp = I915_READ(G4X_AUD_VID_DID);
  130. if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
  131. eldv = G4X_ELDV_DEVCL_DEVBLC;
  132. else
  133. eldv = G4X_ELDV_DEVCTG;
  134. if (intel_eld_uptodate(connector,
  135. G4X_AUD_CNTL_ST, eldv,
  136. G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
  137. G4X_HDMIW_HDMIEDID))
  138. return;
  139. tmp = I915_READ(G4X_AUD_CNTL_ST);
  140. tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
  141. len = (tmp >> 9) & 0x1f; /* ELD buffer size */
  142. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  143. len = min(drm_eld_size(eld) / 4, len);
  144. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  145. for (i = 0; i < len; i++)
  146. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  147. tmp = I915_READ(G4X_AUD_CNTL_ST);
  148. tmp |= eldv;
  149. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  150. }
  151. static void hsw_audio_codec_disable(struct intel_encoder *encoder)
  152. {
  153. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  154. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  155. enum pipe pipe = intel_crtc->pipe;
  156. uint32_t tmp;
  157. DRM_DEBUG_KMS("Disable audio codec on pipe %c\n", pipe_name(pipe));
  158. /* Disable timestamps */
  159. tmp = I915_READ(HSW_AUD_CFG(pipe));
  160. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  161. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  162. tmp &= ~AUD_CONFIG_UPPER_N_MASK;
  163. tmp &= ~AUD_CONFIG_LOWER_N_MASK;
  164. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  165. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  166. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  167. /* Invalidate ELD */
  168. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  169. tmp &= ~AUDIO_ELD_VALID(pipe);
  170. tmp &= ~AUDIO_OUTPUT_ENABLE(pipe);
  171. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  172. }
  173. static void hsw_audio_codec_enable(struct drm_connector *connector,
  174. struct intel_encoder *encoder,
  175. struct drm_display_mode *mode)
  176. {
  177. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  178. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  179. enum pipe pipe = intel_crtc->pipe;
  180. const uint8_t *eld = connector->eld;
  181. uint32_t tmp;
  182. int len, i;
  183. DRM_DEBUG_KMS("Enable audio codec on pipe %c, %u bytes ELD\n",
  184. pipe_name(pipe), drm_eld_size(eld));
  185. /* Enable audio presence detect, invalidate ELD */
  186. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  187. tmp |= AUDIO_OUTPUT_ENABLE(pipe);
  188. tmp &= ~AUDIO_ELD_VALID(pipe);
  189. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  190. /*
  191. * FIXME: We're supposed to wait for vblank here, but we have vblanks
  192. * disabled during the mode set. The proper fix would be to push the
  193. * rest of the setup into a vblank work item, queued here, but the
  194. * infrastructure is not there yet.
  195. */
  196. /* Reset ELD write address */
  197. tmp = I915_READ(HSW_AUD_DIP_ELD_CTRL(pipe));
  198. tmp &= ~IBX_ELD_ADDRESS_MASK;
  199. I915_WRITE(HSW_AUD_DIP_ELD_CTRL(pipe), tmp);
  200. /* Up to 84 bytes of hw ELD buffer */
  201. len = min(drm_eld_size(eld), 84);
  202. for (i = 0; i < len / 4; i++)
  203. I915_WRITE(HSW_AUD_EDID_DATA(pipe), *((uint32_t *)eld + i));
  204. /* ELD valid */
  205. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  206. tmp |= AUDIO_ELD_VALID(pipe);
  207. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  208. /* Enable timestamps */
  209. tmp = I915_READ(HSW_AUD_CFG(pipe));
  210. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  211. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  212. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  213. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  214. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  215. else
  216. tmp |= audio_config_hdmi_pixel_clock(mode);
  217. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  218. }
  219. static void ilk_audio_codec_disable(struct intel_encoder *encoder)
  220. {
  221. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  222. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  223. struct intel_digital_port *intel_dig_port =
  224. enc_to_dig_port(&encoder->base);
  225. enum port port = intel_dig_port->port;
  226. enum pipe pipe = intel_crtc->pipe;
  227. uint32_t tmp, eldv;
  228. int aud_config;
  229. int aud_cntrl_st2;
  230. DRM_DEBUG_KMS("Disable audio codec on port %c, pipe %c\n",
  231. port_name(port), pipe_name(pipe));
  232. if (WARN_ON(port == PORT_A))
  233. return;
  234. if (HAS_PCH_IBX(dev_priv->dev)) {
  235. aud_config = IBX_AUD_CFG(pipe);
  236. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  237. } else if (IS_VALLEYVIEW(dev_priv)) {
  238. aud_config = VLV_AUD_CFG(pipe);
  239. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  240. } else {
  241. aud_config = CPT_AUD_CFG(pipe);
  242. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  243. }
  244. /* Disable timestamps */
  245. tmp = I915_READ(aud_config);
  246. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  247. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  248. tmp &= ~AUD_CONFIG_UPPER_N_MASK;
  249. tmp &= ~AUD_CONFIG_LOWER_N_MASK;
  250. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  251. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  252. I915_WRITE(aud_config, tmp);
  253. eldv = IBX_ELD_VALID(port);
  254. /* Invalidate ELD */
  255. tmp = I915_READ(aud_cntrl_st2);
  256. tmp &= ~eldv;
  257. I915_WRITE(aud_cntrl_st2, tmp);
  258. }
  259. static void ilk_audio_codec_enable(struct drm_connector *connector,
  260. struct intel_encoder *encoder,
  261. struct drm_display_mode *mode)
  262. {
  263. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  264. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  265. struct intel_digital_port *intel_dig_port =
  266. enc_to_dig_port(&encoder->base);
  267. enum port port = intel_dig_port->port;
  268. enum pipe pipe = intel_crtc->pipe;
  269. uint8_t *eld = connector->eld;
  270. uint32_t eldv;
  271. uint32_t tmp;
  272. int len, i;
  273. int hdmiw_hdmiedid;
  274. int aud_config;
  275. int aud_cntl_st;
  276. int aud_cntrl_st2;
  277. DRM_DEBUG_KMS("Enable audio codec on port %c, pipe %c, %u bytes ELD\n",
  278. port_name(port), pipe_name(pipe), drm_eld_size(eld));
  279. if (WARN_ON(port == PORT_A))
  280. return;
  281. /*
  282. * FIXME: We're supposed to wait for vblank here, but we have vblanks
  283. * disabled during the mode set. The proper fix would be to push the
  284. * rest of the setup into a vblank work item, queued here, but the
  285. * infrastructure is not there yet.
  286. */
  287. if (HAS_PCH_IBX(connector->dev)) {
  288. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  289. aud_config = IBX_AUD_CFG(pipe);
  290. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  291. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  292. } else if (IS_VALLEYVIEW(connector->dev)) {
  293. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  294. aud_config = VLV_AUD_CFG(pipe);
  295. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  296. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  297. } else {
  298. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  299. aud_config = CPT_AUD_CFG(pipe);
  300. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  301. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  302. }
  303. eldv = IBX_ELD_VALID(port);
  304. /* Invalidate ELD */
  305. tmp = I915_READ(aud_cntrl_st2);
  306. tmp &= ~eldv;
  307. I915_WRITE(aud_cntrl_st2, tmp);
  308. /* Reset ELD write address */
  309. tmp = I915_READ(aud_cntl_st);
  310. tmp &= ~IBX_ELD_ADDRESS_MASK;
  311. I915_WRITE(aud_cntl_st, tmp);
  312. /* Up to 84 bytes of hw ELD buffer */
  313. len = min(drm_eld_size(eld), 84);
  314. for (i = 0; i < len / 4; i++)
  315. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  316. /* ELD valid */
  317. tmp = I915_READ(aud_cntrl_st2);
  318. tmp |= eldv;
  319. I915_WRITE(aud_cntrl_st2, tmp);
  320. /* Enable timestamps */
  321. tmp = I915_READ(aud_config);
  322. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  323. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  324. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  325. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
  326. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  327. else
  328. tmp |= audio_config_hdmi_pixel_clock(mode);
  329. I915_WRITE(aud_config, tmp);
  330. }
  331. /**
  332. * intel_audio_codec_enable - Enable the audio codec for HD audio
  333. * @intel_encoder: encoder on which to enable audio
  334. *
  335. * The enable sequences may only be performed after enabling the transcoder and
  336. * port, and after completed link training.
  337. */
  338. void intel_audio_codec_enable(struct intel_encoder *intel_encoder)
  339. {
  340. struct drm_encoder *encoder = &intel_encoder->base;
  341. struct intel_crtc *crtc = to_intel_crtc(encoder->crtc);
  342. struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  343. struct drm_connector *connector;
  344. struct drm_device *dev = encoder->dev;
  345. struct drm_i915_private *dev_priv = dev->dev_private;
  346. struct i915_audio_component *acomp = dev_priv->audio_component;
  347. struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
  348. enum port port = intel_dig_port->port;
  349. connector = drm_select_eld(encoder, mode);
  350. if (!connector)
  351. return;
  352. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  353. connector->base.id,
  354. connector->name,
  355. connector->encoder->base.id,
  356. connector->encoder->name);
  357. /* ELD Conn_Type */
  358. connector->eld[5] &= ~(3 << 2);
  359. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  360. connector->eld[5] |= (1 << 2);
  361. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  362. if (dev_priv->display.audio_codec_enable)
  363. dev_priv->display.audio_codec_enable(connector, intel_encoder, mode);
  364. if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
  365. acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr, (int) port);
  366. }
  367. /**
  368. * intel_audio_codec_disable - Disable the audio codec for HD audio
  369. * @intel_encoder: encoder on which to disable audio
  370. *
  371. * The disable sequences must be performed before disabling the transcoder or
  372. * port.
  373. */
  374. void intel_audio_codec_disable(struct intel_encoder *intel_encoder)
  375. {
  376. struct drm_encoder *encoder = &intel_encoder->base;
  377. struct drm_device *dev = encoder->dev;
  378. struct drm_i915_private *dev_priv = dev->dev_private;
  379. struct i915_audio_component *acomp = dev_priv->audio_component;
  380. struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
  381. enum port port = intel_dig_port->port;
  382. if (dev_priv->display.audio_codec_disable)
  383. dev_priv->display.audio_codec_disable(intel_encoder);
  384. if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
  385. acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr, (int) port);
  386. }
  387. /**
  388. * intel_init_audio - Set up chip specific audio functions
  389. * @dev: drm device
  390. */
  391. void intel_init_audio(struct drm_device *dev)
  392. {
  393. struct drm_i915_private *dev_priv = dev->dev_private;
  394. if (IS_G4X(dev)) {
  395. dev_priv->display.audio_codec_enable = g4x_audio_codec_enable;
  396. dev_priv->display.audio_codec_disable = g4x_audio_codec_disable;
  397. } else if (IS_VALLEYVIEW(dev)) {
  398. dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
  399. dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
  400. } else if (IS_HASWELL(dev) || INTEL_INFO(dev)->gen >= 8) {
  401. dev_priv->display.audio_codec_enable = hsw_audio_codec_enable;
  402. dev_priv->display.audio_codec_disable = hsw_audio_codec_disable;
  403. } else if (HAS_PCH_SPLIT(dev)) {
  404. dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
  405. dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
  406. }
  407. }
  408. static void i915_audio_component_get_power(struct device *dev)
  409. {
  410. intel_display_power_get(dev_to_i915(dev), POWER_DOMAIN_AUDIO);
  411. }
  412. static void i915_audio_component_put_power(struct device *dev)
  413. {
  414. intel_display_power_put(dev_to_i915(dev), POWER_DOMAIN_AUDIO);
  415. }
  416. static void i915_audio_component_codec_wake_override(struct device *dev,
  417. bool enable)
  418. {
  419. struct drm_i915_private *dev_priv = dev_to_i915(dev);
  420. u32 tmp;
  421. if (!IS_SKYLAKE(dev_priv))
  422. return;
  423. /*
  424. * Enable/disable generating the codec wake signal, overriding the
  425. * internal logic to generate the codec wake to controller.
  426. */
  427. tmp = I915_READ(HSW_AUD_CHICKENBIT);
  428. tmp &= ~SKL_AUD_CODEC_WAKE_SIGNAL;
  429. I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
  430. usleep_range(1000, 1500);
  431. if (enable) {
  432. tmp = I915_READ(HSW_AUD_CHICKENBIT);
  433. tmp |= SKL_AUD_CODEC_WAKE_SIGNAL;
  434. I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
  435. usleep_range(1000, 1500);
  436. }
  437. }
  438. /* Get CDCLK in kHz */
  439. static int i915_audio_component_get_cdclk_freq(struct device *dev)
  440. {
  441. struct drm_i915_private *dev_priv = dev_to_i915(dev);
  442. int ret;
  443. if (WARN_ON_ONCE(!HAS_DDI(dev_priv)))
  444. return -ENODEV;
  445. intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
  446. ret = dev_priv->display.get_display_clock_speed(dev_priv->dev);
  447. intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO);
  448. return ret;
  449. }
  450. static const struct i915_audio_component_ops i915_audio_component_ops = {
  451. .owner = THIS_MODULE,
  452. .get_power = i915_audio_component_get_power,
  453. .put_power = i915_audio_component_put_power,
  454. .codec_wake_override = i915_audio_component_codec_wake_override,
  455. .get_cdclk_freq = i915_audio_component_get_cdclk_freq,
  456. };
  457. static int i915_audio_component_bind(struct device *i915_dev,
  458. struct device *hda_dev, void *data)
  459. {
  460. struct i915_audio_component *acomp = data;
  461. struct drm_i915_private *dev_priv = dev_to_i915(i915_dev);
  462. if (WARN_ON(acomp->ops || acomp->dev))
  463. return -EEXIST;
  464. drm_modeset_lock_all(dev_priv->dev);
  465. acomp->ops = &i915_audio_component_ops;
  466. acomp->dev = i915_dev;
  467. dev_priv->audio_component = acomp;
  468. drm_modeset_unlock_all(dev_priv->dev);
  469. return 0;
  470. }
  471. static void i915_audio_component_unbind(struct device *i915_dev,
  472. struct device *hda_dev, void *data)
  473. {
  474. struct i915_audio_component *acomp = data;
  475. struct drm_i915_private *dev_priv = dev_to_i915(i915_dev);
  476. drm_modeset_lock_all(dev_priv->dev);
  477. acomp->ops = NULL;
  478. acomp->dev = NULL;
  479. dev_priv->audio_component = NULL;
  480. drm_modeset_unlock_all(dev_priv->dev);
  481. }
  482. static const struct component_ops i915_audio_component_bind_ops = {
  483. .bind = i915_audio_component_bind,
  484. .unbind = i915_audio_component_unbind,
  485. };
  486. /**
  487. * i915_audio_component_init - initialize and register the audio component
  488. * @dev_priv: i915 device instance
  489. *
  490. * This will register with the component framework a child component which
  491. * will bind dynamically to the snd_hda_intel driver's corresponding master
  492. * component when the latter is registered. During binding the child
  493. * initializes an instance of struct i915_audio_component which it receives
  494. * from the master. The master can then start to use the interface defined by
  495. * this struct. Each side can break the binding at any point by deregistering
  496. * its own component after which each side's component unbind callback is
  497. * called.
  498. *
  499. * We ignore any error during registration and continue with reduced
  500. * functionality (i.e. without HDMI audio).
  501. */
  502. void i915_audio_component_init(struct drm_i915_private *dev_priv)
  503. {
  504. int ret;
  505. ret = component_add(dev_priv->dev->dev, &i915_audio_component_bind_ops);
  506. if (ret < 0) {
  507. DRM_ERROR("failed to add audio component (%d)\n", ret);
  508. /* continue with reduced functionality */
  509. return;
  510. }
  511. dev_priv->audio_component_registered = true;
  512. }
  513. /**
  514. * i915_audio_component_cleanup - deregister the audio component
  515. * @dev_priv: i915 device instance
  516. *
  517. * Deregisters the audio component, breaking any existing binding to the
  518. * corresponding snd_hda_intel driver's master component.
  519. */
  520. void i915_audio_component_cleanup(struct drm_i915_private *dev_priv)
  521. {
  522. if (!dev_priv->audio_component_registered)
  523. return;
  524. component_del(dev_priv->dev->dev, &i915_audio_component_bind_ops);
  525. dev_priv->audio_component_registered = false;
  526. }