intel_audio.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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 <drm/intel_lpe_audio.h>
  27. #include "intel_drv.h"
  28. #include <drm/drmP.h>
  29. #include <drm/drm_edid.h>
  30. #include "i915_drv.h"
  31. /**
  32. * DOC: High Definition Audio over HDMI and Display Port
  33. *
  34. * The graphics and audio drivers together support High Definition Audio over
  35. * HDMI and Display Port. The audio programming sequences are divided into audio
  36. * codec and controller enable and disable sequences. The graphics driver
  37. * handles the audio codec sequences, while the audio driver handles the audio
  38. * controller sequences.
  39. *
  40. * The disable sequences must be performed before disabling the transcoder or
  41. * port. The enable sequences may only be performed after enabling the
  42. * transcoder and port, and after completed link training. Therefore the audio
  43. * enable/disable sequences are part of the modeset sequence.
  44. *
  45. * The codec and controller sequences could be done either parallel or serial,
  46. * but generally the ELDV/PD change in the codec sequence indicates to the audio
  47. * driver that the controller sequence should start. Indeed, most of the
  48. * co-operation between the graphics and audio drivers is handled via audio
  49. * related registers. (The notable exception is the power management, not
  50. * covered here.)
  51. *
  52. * The struct &i915_audio_component is used to interact between the graphics
  53. * and audio drivers. The struct &i915_audio_component_ops @ops in it is
  54. * defined in graphics driver and called in audio driver. The
  55. * struct &i915_audio_component_audio_ops @audio_ops is called from i915 driver.
  56. */
  57. /* DP N/M table */
  58. #define LC_540M 540000
  59. #define LC_270M 270000
  60. #define LC_162M 162000
  61. struct dp_aud_n_m {
  62. int sample_rate;
  63. int clock;
  64. u16 m;
  65. u16 n;
  66. };
  67. /* Values according to DP 1.4 Table 2-104 */
  68. static const struct dp_aud_n_m dp_aud_n_m[] = {
  69. { 32000, LC_162M, 1024, 10125 },
  70. { 44100, LC_162M, 784, 5625 },
  71. { 48000, LC_162M, 512, 3375 },
  72. { 64000, LC_162M, 2048, 10125 },
  73. { 88200, LC_162M, 1568, 5625 },
  74. { 96000, LC_162M, 1024, 3375 },
  75. { 128000, LC_162M, 4096, 10125 },
  76. { 176400, LC_162M, 3136, 5625 },
  77. { 192000, LC_162M, 2048, 3375 },
  78. { 32000, LC_270M, 1024, 16875 },
  79. { 44100, LC_270M, 784, 9375 },
  80. { 48000, LC_270M, 512, 5625 },
  81. { 64000, LC_270M, 2048, 16875 },
  82. { 88200, LC_270M, 1568, 9375 },
  83. { 96000, LC_270M, 1024, 5625 },
  84. { 128000, LC_270M, 4096, 16875 },
  85. { 176400, LC_270M, 3136, 9375 },
  86. { 192000, LC_270M, 2048, 5625 },
  87. { 32000, LC_540M, 1024, 33750 },
  88. { 44100, LC_540M, 784, 18750 },
  89. { 48000, LC_540M, 512, 11250 },
  90. { 64000, LC_540M, 2048, 33750 },
  91. { 88200, LC_540M, 1568, 18750 },
  92. { 96000, LC_540M, 1024, 11250 },
  93. { 128000, LC_540M, 4096, 33750 },
  94. { 176400, LC_540M, 3136, 18750 },
  95. { 192000, LC_540M, 2048, 11250 },
  96. };
  97. static const struct dp_aud_n_m *
  98. audio_config_dp_get_n_m(struct intel_crtc *intel_crtc, int rate)
  99. {
  100. int i;
  101. for (i = 0; i < ARRAY_SIZE(dp_aud_n_m); i++) {
  102. if (rate == dp_aud_n_m[i].sample_rate &&
  103. intel_crtc->config->port_clock == dp_aud_n_m[i].clock)
  104. return &dp_aud_n_m[i];
  105. }
  106. return NULL;
  107. }
  108. static const struct {
  109. int clock;
  110. u32 config;
  111. } hdmi_audio_clock[] = {
  112. { 25175, AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  113. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  114. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  115. { 27027, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  116. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  117. { 54054, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  118. { 74176, AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  119. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  120. { 148352, AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  121. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  122. };
  123. /* HDMI N/CTS table */
  124. #define TMDS_297M 297000
  125. #define TMDS_296M 296703
  126. static const struct {
  127. int sample_rate;
  128. int clock;
  129. int n;
  130. int cts;
  131. } hdmi_aud_ncts[] = {
  132. { 44100, TMDS_296M, 4459, 234375 },
  133. { 44100, TMDS_297M, 4704, 247500 },
  134. { 48000, TMDS_296M, 5824, 281250 },
  135. { 48000, TMDS_297M, 5120, 247500 },
  136. { 32000, TMDS_296M, 5824, 421875 },
  137. { 32000, TMDS_297M, 3072, 222750 },
  138. { 88200, TMDS_296M, 8918, 234375 },
  139. { 88200, TMDS_297M, 9408, 247500 },
  140. { 96000, TMDS_296M, 11648, 281250 },
  141. { 96000, TMDS_297M, 10240, 247500 },
  142. { 176400, TMDS_296M, 17836, 234375 },
  143. { 176400, TMDS_297M, 18816, 247500 },
  144. { 192000, TMDS_296M, 23296, 281250 },
  145. { 192000, TMDS_297M, 20480, 247500 },
  146. };
  147. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  148. static u32 audio_config_hdmi_pixel_clock(const struct drm_display_mode *adjusted_mode)
  149. {
  150. int i;
  151. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  152. if (adjusted_mode->crtc_clock == hdmi_audio_clock[i].clock)
  153. break;
  154. }
  155. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  156. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n",
  157. adjusted_mode->crtc_clock);
  158. i = 1;
  159. }
  160. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  161. hdmi_audio_clock[i].clock,
  162. hdmi_audio_clock[i].config);
  163. return hdmi_audio_clock[i].config;
  164. }
  165. static int audio_config_hdmi_get_n(const struct drm_display_mode *adjusted_mode,
  166. int rate)
  167. {
  168. int i;
  169. for (i = 0; i < ARRAY_SIZE(hdmi_aud_ncts); i++) {
  170. if (rate == hdmi_aud_ncts[i].sample_rate &&
  171. adjusted_mode->crtc_clock == hdmi_aud_ncts[i].clock) {
  172. return hdmi_aud_ncts[i].n;
  173. }
  174. }
  175. return 0;
  176. }
  177. static bool intel_eld_uptodate(struct drm_connector *connector,
  178. i915_reg_t reg_eldv, uint32_t bits_eldv,
  179. i915_reg_t reg_elda, uint32_t bits_elda,
  180. i915_reg_t reg_edid)
  181. {
  182. struct drm_i915_private *dev_priv = to_i915(connector->dev);
  183. uint8_t *eld = connector->eld;
  184. uint32_t tmp;
  185. int i;
  186. tmp = I915_READ(reg_eldv);
  187. tmp &= bits_eldv;
  188. if (!tmp)
  189. return false;
  190. tmp = I915_READ(reg_elda);
  191. tmp &= ~bits_elda;
  192. I915_WRITE(reg_elda, tmp);
  193. for (i = 0; i < drm_eld_size(eld) / 4; i++)
  194. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  195. return false;
  196. return true;
  197. }
  198. static void g4x_audio_codec_disable(struct intel_encoder *encoder)
  199. {
  200. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  201. uint32_t eldv, tmp;
  202. DRM_DEBUG_KMS("Disable audio codec\n");
  203. tmp = I915_READ(G4X_AUD_VID_DID);
  204. if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
  205. eldv = G4X_ELDV_DEVCL_DEVBLC;
  206. else
  207. eldv = G4X_ELDV_DEVCTG;
  208. /* Invalidate ELD */
  209. tmp = I915_READ(G4X_AUD_CNTL_ST);
  210. tmp &= ~eldv;
  211. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  212. }
  213. static void g4x_audio_codec_enable(struct drm_connector *connector,
  214. struct intel_encoder *encoder,
  215. const struct drm_display_mode *adjusted_mode)
  216. {
  217. struct drm_i915_private *dev_priv = to_i915(connector->dev);
  218. uint8_t *eld = connector->eld;
  219. uint32_t eldv;
  220. uint32_t tmp;
  221. int len, i;
  222. DRM_DEBUG_KMS("Enable audio codec, %u bytes ELD\n", eld[2]);
  223. tmp = I915_READ(G4X_AUD_VID_DID);
  224. if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
  225. eldv = G4X_ELDV_DEVCL_DEVBLC;
  226. else
  227. eldv = G4X_ELDV_DEVCTG;
  228. if (intel_eld_uptodate(connector,
  229. G4X_AUD_CNTL_ST, eldv,
  230. G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
  231. G4X_HDMIW_HDMIEDID))
  232. return;
  233. tmp = I915_READ(G4X_AUD_CNTL_ST);
  234. tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
  235. len = (tmp >> 9) & 0x1f; /* ELD buffer size */
  236. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  237. len = min(drm_eld_size(eld) / 4, len);
  238. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  239. for (i = 0; i < len; i++)
  240. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  241. tmp = I915_READ(G4X_AUD_CNTL_ST);
  242. tmp |= eldv;
  243. I915_WRITE(G4X_AUD_CNTL_ST, tmp);
  244. }
  245. static void
  246. hsw_dp_audio_config_update(struct intel_crtc *intel_crtc, enum port port,
  247. const struct drm_display_mode *adjusted_mode)
  248. {
  249. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  250. struct i915_audio_component *acomp = dev_priv->audio_component;
  251. int rate = acomp ? acomp->aud_sample_rate[port] : 0;
  252. const struct dp_aud_n_m *nm = audio_config_dp_get_n_m(intel_crtc, rate);
  253. enum pipe pipe = intel_crtc->pipe;
  254. u32 tmp;
  255. if (nm)
  256. DRM_DEBUG_KMS("using Maud %u, Naud %u\n", nm->m, nm->n);
  257. else
  258. DRM_DEBUG_KMS("using automatic Maud, Naud\n");
  259. tmp = I915_READ(HSW_AUD_CFG(pipe));
  260. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  261. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  262. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  263. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  264. if (nm) {
  265. tmp &= ~AUD_CONFIG_N_MASK;
  266. tmp |= AUD_CONFIG_N(nm->n);
  267. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  268. }
  269. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  270. tmp = I915_READ(HSW_AUD_M_CTS_ENABLE(pipe));
  271. tmp &= ~AUD_CONFIG_M_MASK;
  272. tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
  273. tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
  274. if (nm) {
  275. tmp |= nm->m;
  276. tmp |= AUD_M_CTS_M_VALUE_INDEX;
  277. tmp |= AUD_M_CTS_M_PROG_ENABLE;
  278. }
  279. I915_WRITE(HSW_AUD_M_CTS_ENABLE(pipe), tmp);
  280. }
  281. static void
  282. hsw_hdmi_audio_config_update(struct intel_crtc *intel_crtc, enum port port,
  283. const struct drm_display_mode *adjusted_mode)
  284. {
  285. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  286. struct i915_audio_component *acomp = dev_priv->audio_component;
  287. int rate = acomp ? acomp->aud_sample_rate[port] : 0;
  288. enum pipe pipe = intel_crtc->pipe;
  289. int n;
  290. u32 tmp;
  291. tmp = I915_READ(HSW_AUD_CFG(pipe));
  292. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  293. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  294. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  295. tmp |= audio_config_hdmi_pixel_clock(adjusted_mode);
  296. n = audio_config_hdmi_get_n(adjusted_mode, rate);
  297. if (n != 0) {
  298. DRM_DEBUG_KMS("using N %d\n", n);
  299. tmp &= ~AUD_CONFIG_N_MASK;
  300. tmp |= AUD_CONFIG_N(n);
  301. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  302. } else {
  303. DRM_DEBUG_KMS("using automatic N\n");
  304. }
  305. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  306. /*
  307. * Let's disable "Enable CTS or M Prog bit"
  308. * and let HW calculate the value
  309. */
  310. tmp = I915_READ(HSW_AUD_M_CTS_ENABLE(pipe));
  311. tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
  312. tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
  313. I915_WRITE(HSW_AUD_M_CTS_ENABLE(pipe), tmp);
  314. }
  315. static void
  316. hsw_audio_config_update(struct intel_crtc *intel_crtc, enum port port,
  317. const struct drm_display_mode *adjusted_mode)
  318. {
  319. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  320. hsw_dp_audio_config_update(intel_crtc, port, adjusted_mode);
  321. else
  322. hsw_hdmi_audio_config_update(intel_crtc, port, adjusted_mode);
  323. }
  324. static void hsw_audio_codec_disable(struct intel_encoder *encoder)
  325. {
  326. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  327. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  328. enum pipe pipe = intel_crtc->pipe;
  329. uint32_t tmp;
  330. DRM_DEBUG_KMS("Disable audio codec on pipe %c\n", pipe_name(pipe));
  331. mutex_lock(&dev_priv->av_mutex);
  332. /* Disable timestamps */
  333. tmp = I915_READ(HSW_AUD_CFG(pipe));
  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_crtc_has_dp_encoder(intel_crtc->config))
  339. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  340. I915_WRITE(HSW_AUD_CFG(pipe), tmp);
  341. /* Invalidate ELD */
  342. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  343. tmp &= ~AUDIO_ELD_VALID(pipe);
  344. tmp &= ~AUDIO_OUTPUT_ENABLE(pipe);
  345. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  346. mutex_unlock(&dev_priv->av_mutex);
  347. }
  348. static void hsw_audio_codec_enable(struct drm_connector *connector,
  349. struct intel_encoder *intel_encoder,
  350. const struct drm_display_mode *adjusted_mode)
  351. {
  352. struct drm_i915_private *dev_priv = to_i915(connector->dev);
  353. struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  354. enum pipe pipe = intel_crtc->pipe;
  355. enum port port = intel_encoder->port;
  356. const uint8_t *eld = connector->eld;
  357. uint32_t tmp;
  358. int len, i;
  359. DRM_DEBUG_KMS("Enable audio codec on pipe %c, %u bytes ELD\n",
  360. pipe_name(pipe), drm_eld_size(eld));
  361. mutex_lock(&dev_priv->av_mutex);
  362. /* Enable audio presence detect, invalidate ELD */
  363. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  364. tmp |= AUDIO_OUTPUT_ENABLE(pipe);
  365. tmp &= ~AUDIO_ELD_VALID(pipe);
  366. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  367. /*
  368. * FIXME: We're supposed to wait for vblank here, but we have vblanks
  369. * disabled during the mode set. The proper fix would be to push the
  370. * rest of the setup into a vblank work item, queued here, but the
  371. * infrastructure is not there yet.
  372. */
  373. /* Reset ELD write address */
  374. tmp = I915_READ(HSW_AUD_DIP_ELD_CTRL(pipe));
  375. tmp &= ~IBX_ELD_ADDRESS_MASK;
  376. I915_WRITE(HSW_AUD_DIP_ELD_CTRL(pipe), tmp);
  377. /* Up to 84 bytes of hw ELD buffer */
  378. len = min(drm_eld_size(eld), 84);
  379. for (i = 0; i < len / 4; i++)
  380. I915_WRITE(HSW_AUD_EDID_DATA(pipe), *((uint32_t *)eld + i));
  381. /* ELD valid */
  382. tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
  383. tmp |= AUDIO_ELD_VALID(pipe);
  384. I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
  385. /* Enable timestamps */
  386. hsw_audio_config_update(intel_crtc, port, adjusted_mode);
  387. mutex_unlock(&dev_priv->av_mutex);
  388. }
  389. static void ilk_audio_codec_disable(struct intel_encoder *intel_encoder)
  390. {
  391. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  392. struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  393. enum pipe pipe = intel_crtc->pipe;
  394. enum port port = intel_encoder->port;
  395. uint32_t tmp, eldv;
  396. i915_reg_t aud_config, aud_cntrl_st2;
  397. DRM_DEBUG_KMS("Disable audio codec on port %c, pipe %c\n",
  398. port_name(port), pipe_name(pipe));
  399. if (WARN_ON(port == PORT_A))
  400. return;
  401. if (HAS_PCH_IBX(dev_priv)) {
  402. aud_config = IBX_AUD_CFG(pipe);
  403. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  404. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  405. aud_config = VLV_AUD_CFG(pipe);
  406. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  407. } else {
  408. aud_config = CPT_AUD_CFG(pipe);
  409. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  410. }
  411. /* Disable timestamps */
  412. tmp = I915_READ(aud_config);
  413. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  414. tmp |= AUD_CONFIG_N_PROG_ENABLE;
  415. tmp &= ~AUD_CONFIG_UPPER_N_MASK;
  416. tmp &= ~AUD_CONFIG_LOWER_N_MASK;
  417. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  418. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  419. I915_WRITE(aud_config, tmp);
  420. eldv = IBX_ELD_VALID(port);
  421. /* Invalidate ELD */
  422. tmp = I915_READ(aud_cntrl_st2);
  423. tmp &= ~eldv;
  424. I915_WRITE(aud_cntrl_st2, tmp);
  425. }
  426. static void ilk_audio_codec_enable(struct drm_connector *connector,
  427. struct intel_encoder *intel_encoder,
  428. const struct drm_display_mode *adjusted_mode)
  429. {
  430. struct drm_i915_private *dev_priv = to_i915(connector->dev);
  431. struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  432. enum pipe pipe = intel_crtc->pipe;
  433. enum port port = intel_encoder->port;
  434. uint8_t *eld = connector->eld;
  435. uint32_t tmp, eldv;
  436. int len, i;
  437. i915_reg_t hdmiw_hdmiedid, aud_config, aud_cntl_st, aud_cntrl_st2;
  438. DRM_DEBUG_KMS("Enable audio codec on port %c, pipe %c, %u bytes ELD\n",
  439. port_name(port), pipe_name(pipe), drm_eld_size(eld));
  440. if (WARN_ON(port == PORT_A))
  441. return;
  442. /*
  443. * FIXME: We're supposed to wait for vblank here, but we have vblanks
  444. * disabled during the mode set. The proper fix would be to push the
  445. * rest of the setup into a vblank work item, queued here, but the
  446. * infrastructure is not there yet.
  447. */
  448. if (HAS_PCH_IBX(dev_priv)) {
  449. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  450. aud_config = IBX_AUD_CFG(pipe);
  451. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  452. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  453. } else if (IS_VALLEYVIEW(dev_priv) ||
  454. IS_CHERRYVIEW(dev_priv)) {
  455. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  456. aud_config = VLV_AUD_CFG(pipe);
  457. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  458. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  459. } else {
  460. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  461. aud_config = CPT_AUD_CFG(pipe);
  462. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  463. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  464. }
  465. eldv = IBX_ELD_VALID(port);
  466. /* Invalidate ELD */
  467. tmp = I915_READ(aud_cntrl_st2);
  468. tmp &= ~eldv;
  469. I915_WRITE(aud_cntrl_st2, tmp);
  470. /* Reset ELD write address */
  471. tmp = I915_READ(aud_cntl_st);
  472. tmp &= ~IBX_ELD_ADDRESS_MASK;
  473. I915_WRITE(aud_cntl_st, tmp);
  474. /* Up to 84 bytes of hw ELD buffer */
  475. len = min(drm_eld_size(eld), 84);
  476. for (i = 0; i < len / 4; i++)
  477. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  478. /* ELD valid */
  479. tmp = I915_READ(aud_cntrl_st2);
  480. tmp |= eldv;
  481. I915_WRITE(aud_cntrl_st2, tmp);
  482. /* Enable timestamps */
  483. tmp = I915_READ(aud_config);
  484. tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
  485. tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
  486. tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
  487. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  488. tmp |= AUD_CONFIG_N_VALUE_INDEX;
  489. else
  490. tmp |= audio_config_hdmi_pixel_clock(adjusted_mode);
  491. I915_WRITE(aud_config, tmp);
  492. }
  493. /**
  494. * intel_audio_codec_enable - Enable the audio codec for HD audio
  495. * @intel_encoder: encoder on which to enable audio
  496. * @crtc_state: pointer to the current crtc state.
  497. * @conn_state: pointer to the current connector state.
  498. *
  499. * The enable sequences may only be performed after enabling the transcoder and
  500. * port, and after completed link training.
  501. */
  502. void intel_audio_codec_enable(struct intel_encoder *intel_encoder,
  503. const struct intel_crtc_state *crtc_state,
  504. const struct drm_connector_state *conn_state)
  505. {
  506. struct drm_encoder *encoder = &intel_encoder->base;
  507. const struct drm_display_mode *adjusted_mode = &crtc_state->base.adjusted_mode;
  508. struct drm_connector *connector;
  509. struct drm_i915_private *dev_priv = to_i915(encoder->dev);
  510. struct i915_audio_component *acomp = dev_priv->audio_component;
  511. enum port port = intel_encoder->port;
  512. enum pipe pipe = to_intel_crtc(crtc_state->base.crtc)->pipe;
  513. connector = conn_state->connector;
  514. if (!connector || !connector->eld[0])
  515. return;
  516. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  517. connector->base.id,
  518. connector->name,
  519. connector->encoder->base.id,
  520. connector->encoder->name);
  521. /* ELD Conn_Type */
  522. connector->eld[5] &= ~(3 << 2);
  523. if (intel_crtc_has_dp_encoder(crtc_state))
  524. connector->eld[5] |= (1 << 2);
  525. connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
  526. if (dev_priv->display.audio_codec_enable)
  527. dev_priv->display.audio_codec_enable(connector, intel_encoder,
  528. adjusted_mode);
  529. mutex_lock(&dev_priv->av_mutex);
  530. intel_encoder->audio_connector = connector;
  531. /* referred in audio callbacks */
  532. dev_priv->av_enc_map[pipe] = intel_encoder;
  533. mutex_unlock(&dev_priv->av_mutex);
  534. if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify) {
  535. /* audio drivers expect pipe = -1 to indicate Non-MST cases */
  536. if (intel_encoder->type != INTEL_OUTPUT_DP_MST)
  537. pipe = -1;
  538. acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
  539. (int) port, (int) pipe);
  540. }
  541. switch (intel_encoder->type) {
  542. case INTEL_OUTPUT_HDMI:
  543. intel_lpe_audio_notify(dev_priv, connector->eld, port, pipe,
  544. crtc_state->port_clock,
  545. false, 0);
  546. break;
  547. case INTEL_OUTPUT_DP:
  548. intel_lpe_audio_notify(dev_priv, connector->eld, port, pipe,
  549. adjusted_mode->crtc_clock,
  550. true, crtc_state->port_clock);
  551. break;
  552. default:
  553. break;
  554. }
  555. }
  556. /**
  557. * intel_audio_codec_disable - Disable the audio codec for HD audio
  558. * @intel_encoder: encoder on which to disable audio
  559. *
  560. * The disable sequences must be performed before disabling the transcoder or
  561. * port.
  562. */
  563. void intel_audio_codec_disable(struct intel_encoder *intel_encoder)
  564. {
  565. struct drm_encoder *encoder = &intel_encoder->base;
  566. struct drm_i915_private *dev_priv = to_i915(encoder->dev);
  567. struct i915_audio_component *acomp = dev_priv->audio_component;
  568. enum port port = intel_encoder->port;
  569. struct intel_crtc *crtc = to_intel_crtc(encoder->crtc);
  570. enum pipe pipe = crtc->pipe;
  571. if (dev_priv->display.audio_codec_disable)
  572. dev_priv->display.audio_codec_disable(intel_encoder);
  573. mutex_lock(&dev_priv->av_mutex);
  574. intel_encoder->audio_connector = NULL;
  575. dev_priv->av_enc_map[pipe] = NULL;
  576. mutex_unlock(&dev_priv->av_mutex);
  577. if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify) {
  578. /* audio drivers expect pipe = -1 to indicate Non-MST cases */
  579. if (intel_encoder->type != INTEL_OUTPUT_DP_MST)
  580. pipe = -1;
  581. acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
  582. (int) port, (int) pipe);
  583. }
  584. intel_lpe_audio_notify(dev_priv, NULL, port, pipe, 0, false, 0);
  585. }
  586. /**
  587. * intel_init_audio_hooks - Set up chip specific audio hooks
  588. * @dev_priv: device private
  589. */
  590. void intel_init_audio_hooks(struct drm_i915_private *dev_priv)
  591. {
  592. if (IS_G4X(dev_priv)) {
  593. dev_priv->display.audio_codec_enable = g4x_audio_codec_enable;
  594. dev_priv->display.audio_codec_disable = g4x_audio_codec_disable;
  595. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  596. dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
  597. dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
  598. } else if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8) {
  599. dev_priv->display.audio_codec_enable = hsw_audio_codec_enable;
  600. dev_priv->display.audio_codec_disable = hsw_audio_codec_disable;
  601. } else if (HAS_PCH_SPLIT(dev_priv)) {
  602. dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
  603. dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
  604. }
  605. }
  606. static void i915_audio_component_get_power(struct device *kdev)
  607. {
  608. intel_display_power_get(kdev_to_i915(kdev), POWER_DOMAIN_AUDIO);
  609. }
  610. static void i915_audio_component_put_power(struct device *kdev)
  611. {
  612. intel_display_power_put(kdev_to_i915(kdev), POWER_DOMAIN_AUDIO);
  613. }
  614. static void i915_audio_component_codec_wake_override(struct device *kdev,
  615. bool enable)
  616. {
  617. struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
  618. u32 tmp;
  619. if (!IS_GEN9_BC(dev_priv))
  620. return;
  621. i915_audio_component_get_power(kdev);
  622. /*
  623. * Enable/disable generating the codec wake signal, overriding the
  624. * internal logic to generate the codec wake to controller.
  625. */
  626. tmp = I915_READ(HSW_AUD_CHICKENBIT);
  627. tmp &= ~SKL_AUD_CODEC_WAKE_SIGNAL;
  628. I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
  629. usleep_range(1000, 1500);
  630. if (enable) {
  631. tmp = I915_READ(HSW_AUD_CHICKENBIT);
  632. tmp |= SKL_AUD_CODEC_WAKE_SIGNAL;
  633. I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
  634. usleep_range(1000, 1500);
  635. }
  636. i915_audio_component_put_power(kdev);
  637. }
  638. /* Get CDCLK in kHz */
  639. static int i915_audio_component_get_cdclk_freq(struct device *kdev)
  640. {
  641. struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
  642. if (WARN_ON_ONCE(!HAS_DDI(dev_priv)))
  643. return -ENODEV;
  644. return dev_priv->cdclk.hw.cdclk;
  645. }
  646. /*
  647. * get the intel_encoder according to the parameter port and pipe
  648. * intel_encoder is saved by the index of pipe
  649. * MST & (pipe >= 0): return the av_enc_map[pipe],
  650. * when port is matched
  651. * MST & (pipe < 0): this is invalid
  652. * Non-MST & (pipe >= 0): only pipe = 0 (the first device entry)
  653. * will get the right intel_encoder with port matched
  654. * Non-MST & (pipe < 0): get the right intel_encoder with port matched
  655. */
  656. static struct intel_encoder *get_saved_enc(struct drm_i915_private *dev_priv,
  657. int port, int pipe)
  658. {
  659. struct intel_encoder *encoder;
  660. if (WARN_ON(pipe >= I915_MAX_PIPES))
  661. return NULL;
  662. /* MST */
  663. if (pipe >= 0) {
  664. encoder = dev_priv->av_enc_map[pipe];
  665. /*
  666. * when bootup, audio driver may not know it is
  667. * MST or not. So it will poll all the port & pipe
  668. * combinations
  669. */
  670. if (encoder != NULL && encoder->port == port &&
  671. encoder->type == INTEL_OUTPUT_DP_MST)
  672. return encoder;
  673. }
  674. /* Non-MST */
  675. if (pipe > 0)
  676. return NULL;
  677. for_each_pipe(dev_priv, pipe) {
  678. encoder = dev_priv->av_enc_map[pipe];
  679. if (encoder == NULL)
  680. continue;
  681. if (encoder->type == INTEL_OUTPUT_DP_MST)
  682. continue;
  683. if (port == encoder->port)
  684. return encoder;
  685. }
  686. return NULL;
  687. }
  688. static int i915_audio_component_sync_audio_rate(struct device *kdev, int port,
  689. int pipe, int rate)
  690. {
  691. struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
  692. struct intel_encoder *intel_encoder;
  693. struct intel_crtc *crtc;
  694. struct drm_display_mode *adjusted_mode;
  695. struct i915_audio_component *acomp = dev_priv->audio_component;
  696. int err = 0;
  697. if (!HAS_DDI(dev_priv))
  698. return 0;
  699. i915_audio_component_get_power(kdev);
  700. mutex_lock(&dev_priv->av_mutex);
  701. /* 1. get the pipe */
  702. intel_encoder = get_saved_enc(dev_priv, port, pipe);
  703. if (!intel_encoder || !intel_encoder->base.crtc) {
  704. DRM_DEBUG_KMS("Not valid for port %c\n", port_name(port));
  705. err = -ENODEV;
  706. goto unlock;
  707. }
  708. /* pipe passed from the audio driver will be -1 for Non-MST case */
  709. crtc = to_intel_crtc(intel_encoder->base.crtc);
  710. pipe = crtc->pipe;
  711. adjusted_mode = &crtc->config->base.adjusted_mode;
  712. /* port must be valid now, otherwise the pipe will be invalid */
  713. acomp->aud_sample_rate[port] = rate;
  714. hsw_audio_config_update(crtc, port, adjusted_mode);
  715. unlock:
  716. mutex_unlock(&dev_priv->av_mutex);
  717. i915_audio_component_put_power(kdev);
  718. return err;
  719. }
  720. static int i915_audio_component_get_eld(struct device *kdev, int port,
  721. int pipe, bool *enabled,
  722. unsigned char *buf, int max_bytes)
  723. {
  724. struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
  725. struct intel_encoder *intel_encoder;
  726. const u8 *eld;
  727. int ret = -EINVAL;
  728. mutex_lock(&dev_priv->av_mutex);
  729. intel_encoder = get_saved_enc(dev_priv, port, pipe);
  730. if (!intel_encoder) {
  731. DRM_DEBUG_KMS("Not valid for port %c\n", port_name(port));
  732. mutex_unlock(&dev_priv->av_mutex);
  733. return ret;
  734. }
  735. ret = 0;
  736. *enabled = intel_encoder->audio_connector != NULL;
  737. if (*enabled) {
  738. eld = intel_encoder->audio_connector->eld;
  739. ret = drm_eld_size(eld);
  740. memcpy(buf, eld, min(max_bytes, ret));
  741. }
  742. mutex_unlock(&dev_priv->av_mutex);
  743. return ret;
  744. }
  745. static const struct i915_audio_component_ops i915_audio_component_ops = {
  746. .owner = THIS_MODULE,
  747. .get_power = i915_audio_component_get_power,
  748. .put_power = i915_audio_component_put_power,
  749. .codec_wake_override = i915_audio_component_codec_wake_override,
  750. .get_cdclk_freq = i915_audio_component_get_cdclk_freq,
  751. .sync_audio_rate = i915_audio_component_sync_audio_rate,
  752. .get_eld = i915_audio_component_get_eld,
  753. };
  754. static int i915_audio_component_bind(struct device *i915_kdev,
  755. struct device *hda_kdev, void *data)
  756. {
  757. struct i915_audio_component *acomp = data;
  758. struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
  759. int i;
  760. if (WARN_ON(acomp->ops || acomp->dev))
  761. return -EEXIST;
  762. drm_modeset_lock_all(&dev_priv->drm);
  763. acomp->ops = &i915_audio_component_ops;
  764. acomp->dev = i915_kdev;
  765. BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
  766. for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
  767. acomp->aud_sample_rate[i] = 0;
  768. dev_priv->audio_component = acomp;
  769. drm_modeset_unlock_all(&dev_priv->drm);
  770. return 0;
  771. }
  772. static void i915_audio_component_unbind(struct device *i915_kdev,
  773. struct device *hda_kdev, void *data)
  774. {
  775. struct i915_audio_component *acomp = data;
  776. struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
  777. drm_modeset_lock_all(&dev_priv->drm);
  778. acomp->ops = NULL;
  779. acomp->dev = NULL;
  780. dev_priv->audio_component = NULL;
  781. drm_modeset_unlock_all(&dev_priv->drm);
  782. }
  783. static const struct component_ops i915_audio_component_bind_ops = {
  784. .bind = i915_audio_component_bind,
  785. .unbind = i915_audio_component_unbind,
  786. };
  787. /**
  788. * i915_audio_component_init - initialize and register the audio component
  789. * @dev_priv: i915 device instance
  790. *
  791. * This will register with the component framework a child component which
  792. * will bind dynamically to the snd_hda_intel driver's corresponding master
  793. * component when the latter is registered. During binding the child
  794. * initializes an instance of struct i915_audio_component which it receives
  795. * from the master. The master can then start to use the interface defined by
  796. * this struct. Each side can break the binding at any point by deregistering
  797. * its own component after which each side's component unbind callback is
  798. * called.
  799. *
  800. * We ignore any error during registration and continue with reduced
  801. * functionality (i.e. without HDMI audio).
  802. */
  803. void i915_audio_component_init(struct drm_i915_private *dev_priv)
  804. {
  805. int ret;
  806. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  807. return;
  808. ret = component_add(dev_priv->drm.dev, &i915_audio_component_bind_ops);
  809. if (ret < 0) {
  810. DRM_ERROR("failed to add audio component (%d)\n", ret);
  811. /* continue with reduced functionality */
  812. return;
  813. }
  814. dev_priv->audio_component_registered = true;
  815. }
  816. /**
  817. * i915_audio_component_cleanup - deregister the audio component
  818. * @dev_priv: i915 device instance
  819. *
  820. * Deregisters the audio component, breaking any existing binding to the
  821. * corresponding snd_hda_intel driver's master component.
  822. */
  823. void i915_audio_component_cleanup(struct drm_i915_private *dev_priv)
  824. {
  825. if (!dev_priv->audio_component_registered)
  826. return;
  827. component_del(dev_priv->drm.dev, &i915_audio_component_bind_ops);
  828. dev_priv->audio_component_registered = false;
  829. }
  830. /**
  831. * intel_audio_init() - Initialize the audio driver either using
  832. * component framework or using lpe audio bridge
  833. * @dev_priv: the i915 drm device private data
  834. *
  835. */
  836. void intel_audio_init(struct drm_i915_private *dev_priv)
  837. {
  838. if (intel_lpe_audio_init(dev_priv) < 0)
  839. i915_audio_component_init(dev_priv);
  840. }
  841. /**
  842. * intel_audio_deinit() - deinitialize the audio driver
  843. * @dev_priv: the i915 drm device private data
  844. *
  845. */
  846. void intel_audio_deinit(struct drm_i915_private *dev_priv)
  847. {
  848. if ((dev_priv)->lpe_audio.platdev != NULL)
  849. intel_lpe_audio_teardown(dev_priv);
  850. else
  851. i915_audio_component_cleanup(dev_priv);
  852. }