intel_audio.c 19 KB

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