2 * Copyright © 2014 Intel Corporation
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:
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
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.
24 #include <linux/component.h>
25 #include <linux/kernel.h>
27 #include <drm/drm_edid.h>
28 #include <drm/i915_component.h>
31 #include "intel_atomic.h"
32 #include "intel_audio.h"
33 #include "intel_cdclk.h"
34 #include "intel_display_types.h"
35 #include "intel_lpe_audio.h"
38 * DOC: High Definition Audio over HDMI and Display Port
40 * The graphics and audio drivers together support High Definition Audio over
41 * HDMI and Display Port. The audio programming sequences are divided into audio
42 * codec and controller enable and disable sequences. The graphics driver
43 * handles the audio codec sequences, while the audio driver handles the audio
44 * controller sequences.
46 * The disable sequences must be performed before disabling the transcoder or
47 * port. The enable sequences may only be performed after enabling the
48 * transcoder and port, and after completed link training. Therefore the audio
49 * enable/disable sequences are part of the modeset sequence.
51 * The codec and controller sequences could be done either parallel or serial,
52 * but generally the ELDV/PD change in the codec sequence indicates to the audio
53 * driver that the controller sequence should start. Indeed, most of the
54 * co-operation between the graphics and audio drivers is handled via audio
55 * related registers. (The notable exception is the power management, not
58 * The struct &i915_audio_component is used to interact between the graphics
59 * and audio drivers. The struct &i915_audio_component_ops @ops in it is
60 * defined in graphics driver and called in audio driver. The
61 * struct &i915_audio_component_audio_ops @audio_ops is called from i915 driver.
65 #define LC_810M 810000
66 #define LC_540M 540000
67 #define LC_270M 270000
68 #define LC_162M 162000
77 struct hdmi_aud_ncts {
84 /* Values according to DP 1.4 Table 2-104 */
85 static const struct dp_aud_n_m dp_aud_n_m[] = {
86 { 32000, LC_162M, 1024, 10125 },
87 { 44100, LC_162M, 784, 5625 },
88 { 48000, LC_162M, 512, 3375 },
89 { 64000, LC_162M, 2048, 10125 },
90 { 88200, LC_162M, 1568, 5625 },
91 { 96000, LC_162M, 1024, 3375 },
92 { 128000, LC_162M, 4096, 10125 },
93 { 176400, LC_162M, 3136, 5625 },
94 { 192000, LC_162M, 2048, 3375 },
95 { 32000, LC_270M, 1024, 16875 },
96 { 44100, LC_270M, 784, 9375 },
97 { 48000, LC_270M, 512, 5625 },
98 { 64000, LC_270M, 2048, 16875 },
99 { 88200, LC_270M, 1568, 9375 },
100 { 96000, LC_270M, 1024, 5625 },
101 { 128000, LC_270M, 4096, 16875 },
102 { 176400, LC_270M, 3136, 9375 },
103 { 192000, LC_270M, 2048, 5625 },
104 { 32000, LC_540M, 1024, 33750 },
105 { 44100, LC_540M, 784, 18750 },
106 { 48000, LC_540M, 512, 11250 },
107 { 64000, LC_540M, 2048, 33750 },
108 { 88200, LC_540M, 1568, 18750 },
109 { 96000, LC_540M, 1024, 11250 },
110 { 128000, LC_540M, 4096, 33750 },
111 { 176400, LC_540M, 3136, 18750 },
112 { 192000, LC_540M, 2048, 11250 },
113 { 32000, LC_810M, 1024, 50625 },
114 { 44100, LC_810M, 784, 28125 },
115 { 48000, LC_810M, 512, 16875 },
116 { 64000, LC_810M, 2048, 50625 },
117 { 88200, LC_810M, 1568, 28125 },
118 { 96000, LC_810M, 1024, 16875 },
119 { 128000, LC_810M, 4096, 50625 },
120 { 176400, LC_810M, 3136, 28125 },
121 { 192000, LC_810M, 2048, 16875 },
124 static const struct dp_aud_n_m *
125 audio_config_dp_get_n_m(const struct intel_crtc_state *crtc_state, int rate)
129 for (i = 0; i < ARRAY_SIZE(dp_aud_n_m); i++) {
130 if (rate == dp_aud_n_m[i].sample_rate &&
131 crtc_state->port_clock == dp_aud_n_m[i].clock)
132 return &dp_aud_n_m[i];
138 static const struct {
141 } hdmi_audio_clock[] = {
142 { 25175, AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
143 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
144 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
145 { 27027, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
146 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
147 { 54054, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
148 { 74176, AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
149 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
150 { 148352, AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
151 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
152 { 296703, AUD_CONFIG_PIXEL_CLOCK_HDMI_296703 },
153 { 297000, AUD_CONFIG_PIXEL_CLOCK_HDMI_297000 },
154 { 593407, AUD_CONFIG_PIXEL_CLOCK_HDMI_593407 },
155 { 594000, AUD_CONFIG_PIXEL_CLOCK_HDMI_594000 },
158 /* HDMI N/CTS table */
159 #define TMDS_297M 297000
160 #define TMDS_296M 296703
161 #define TMDS_594M 594000
162 #define TMDS_593M 593407
164 static const struct hdmi_aud_ncts hdmi_aud_ncts_24bpp[] = {
165 { 32000, TMDS_296M, 5824, 421875 },
166 { 32000, TMDS_297M, 3072, 222750 },
167 { 32000, TMDS_593M, 5824, 843750 },
168 { 32000, TMDS_594M, 3072, 445500 },
169 { 44100, TMDS_296M, 4459, 234375 },
170 { 44100, TMDS_297M, 4704, 247500 },
171 { 44100, TMDS_593M, 8918, 937500 },
172 { 44100, TMDS_594M, 9408, 990000 },
173 { 88200, TMDS_296M, 8918, 234375 },
174 { 88200, TMDS_297M, 9408, 247500 },
175 { 88200, TMDS_593M, 17836, 937500 },
176 { 88200, TMDS_594M, 18816, 990000 },
177 { 176400, TMDS_296M, 17836, 234375 },
178 { 176400, TMDS_297M, 18816, 247500 },
179 { 176400, TMDS_593M, 35672, 937500 },
180 { 176400, TMDS_594M, 37632, 990000 },
181 { 48000, TMDS_296M, 5824, 281250 },
182 { 48000, TMDS_297M, 5120, 247500 },
183 { 48000, TMDS_593M, 5824, 562500 },
184 { 48000, TMDS_594M, 6144, 594000 },
185 { 96000, TMDS_296M, 11648, 281250 },
186 { 96000, TMDS_297M, 10240, 247500 },
187 { 96000, TMDS_593M, 11648, 562500 },
188 { 96000, TMDS_594M, 12288, 594000 },
189 { 192000, TMDS_296M, 23296, 281250 },
190 { 192000, TMDS_297M, 20480, 247500 },
191 { 192000, TMDS_593M, 23296, 562500 },
192 { 192000, TMDS_594M, 24576, 594000 },
195 /* Appendix C - N & CTS values for deep color from HDMI 2.0 spec*/
196 /* HDMI N/CTS table for 10 bit deep color(30 bpp)*/
197 #define TMDS_371M 371250
198 #define TMDS_370M 370878
200 static const struct hdmi_aud_ncts hdmi_aud_ncts_30bpp[] = {
201 { 32000, TMDS_370M, 5824, 527344 },
202 { 32000, TMDS_371M, 6144, 556875 },
203 { 44100, TMDS_370M, 8918, 585938 },
204 { 44100, TMDS_371M, 4704, 309375 },
205 { 88200, TMDS_370M, 17836, 585938 },
206 { 88200, TMDS_371M, 9408, 309375 },
207 { 176400, TMDS_370M, 35672, 585938 },
208 { 176400, TMDS_371M, 18816, 309375 },
209 { 48000, TMDS_370M, 11648, 703125 },
210 { 48000, TMDS_371M, 5120, 309375 },
211 { 96000, TMDS_370M, 23296, 703125 },
212 { 96000, TMDS_371M, 10240, 309375 },
213 { 192000, TMDS_370M, 46592, 703125 },
214 { 192000, TMDS_371M, 20480, 309375 },
217 /* HDMI N/CTS table for 12 bit deep color(36 bpp)*/
218 #define TMDS_445_5M 445500
219 #define TMDS_445M 445054
221 static const struct hdmi_aud_ncts hdmi_aud_ncts_36bpp[] = {
222 { 32000, TMDS_445M, 5824, 632813 },
223 { 32000, TMDS_445_5M, 4096, 445500 },
224 { 44100, TMDS_445M, 8918, 703125 },
225 { 44100, TMDS_445_5M, 4704, 371250 },
226 { 88200, TMDS_445M, 17836, 703125 },
227 { 88200, TMDS_445_5M, 9408, 371250 },
228 { 176400, TMDS_445M, 35672, 703125 },
229 { 176400, TMDS_445_5M, 18816, 371250 },
230 { 48000, TMDS_445M, 5824, 421875 },
231 { 48000, TMDS_445_5M, 5120, 371250 },
232 { 96000, TMDS_445M, 11648, 421875 },
233 { 96000, TMDS_445_5M, 10240, 371250 },
234 { 192000, TMDS_445M, 23296, 421875 },
235 { 192000, TMDS_445_5M, 20480, 371250 },
238 /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
239 static u32 audio_config_hdmi_pixel_clock(const struct intel_crtc_state *crtc_state)
241 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
242 const struct drm_display_mode *adjusted_mode =
243 &crtc_state->hw.adjusted_mode;
246 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
247 if (adjusted_mode->crtc_clock == hdmi_audio_clock[i].clock)
251 if (INTEL_GEN(dev_priv) < 12 && adjusted_mode->crtc_clock > 148500)
252 i = ARRAY_SIZE(hdmi_audio_clock);
254 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
255 drm_dbg_kms(&dev_priv->drm,
256 "HDMI audio pixel clock setting for %d not found, falling back to defaults\n",
257 adjusted_mode->crtc_clock);
261 drm_dbg_kms(&dev_priv->drm,
262 "Configuring HDMI audio for pixel clock %d (0x%08x)\n",
263 hdmi_audio_clock[i].clock,
264 hdmi_audio_clock[i].config);
266 return hdmi_audio_clock[i].config;
269 static int audio_config_hdmi_get_n(const struct intel_crtc_state *crtc_state,
272 const struct hdmi_aud_ncts *hdmi_ncts_table;
275 if (crtc_state->pipe_bpp == 36) {
276 hdmi_ncts_table = hdmi_aud_ncts_36bpp;
277 size = ARRAY_SIZE(hdmi_aud_ncts_36bpp);
278 } else if (crtc_state->pipe_bpp == 30) {
279 hdmi_ncts_table = hdmi_aud_ncts_30bpp;
280 size = ARRAY_SIZE(hdmi_aud_ncts_30bpp);
282 hdmi_ncts_table = hdmi_aud_ncts_24bpp;
283 size = ARRAY_SIZE(hdmi_aud_ncts_24bpp);
286 for (i = 0; i < size; i++) {
287 if (rate == hdmi_ncts_table[i].sample_rate &&
288 crtc_state->port_clock == hdmi_ncts_table[i].clock) {
289 return hdmi_ncts_table[i].n;
295 static bool intel_eld_uptodate(struct drm_connector *connector,
296 i915_reg_t reg_eldv, u32 bits_eldv,
297 i915_reg_t reg_elda, u32 bits_elda,
300 struct drm_i915_private *dev_priv = to_i915(connector->dev);
301 const u8 *eld = connector->eld;
305 tmp = intel_de_read(dev_priv, reg_eldv);
311 tmp = intel_de_read(dev_priv, reg_elda);
313 intel_de_write(dev_priv, reg_elda, tmp);
315 for (i = 0; i < drm_eld_size(eld) / 4; i++)
316 if (intel_de_read(dev_priv, reg_edid) != *((const u32 *)eld + i))
322 static void g4x_audio_codec_disable(struct intel_encoder *encoder,
323 const struct intel_crtc_state *old_crtc_state,
324 const struct drm_connector_state *old_conn_state)
326 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
329 drm_dbg_kms(&dev_priv->drm, "Disable audio codec\n");
331 tmp = intel_de_read(dev_priv, G4X_AUD_VID_DID);
332 if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
333 eldv = G4X_ELDV_DEVCL_DEVBLC;
335 eldv = G4X_ELDV_DEVCTG;
338 tmp = intel_de_read(dev_priv, G4X_AUD_CNTL_ST);
340 intel_de_write(dev_priv, G4X_AUD_CNTL_ST, tmp);
343 static void g4x_audio_codec_enable(struct intel_encoder *encoder,
344 const struct intel_crtc_state *crtc_state,
345 const struct drm_connector_state *conn_state)
347 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
348 struct drm_connector *connector = conn_state->connector;
349 const u8 *eld = connector->eld;
354 drm_dbg_kms(&dev_priv->drm, "Enable audio codec, %u bytes ELD\n",
357 tmp = intel_de_read(dev_priv, G4X_AUD_VID_DID);
358 if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
359 eldv = G4X_ELDV_DEVCL_DEVBLC;
361 eldv = G4X_ELDV_DEVCTG;
363 if (intel_eld_uptodate(connector,
364 G4X_AUD_CNTL_ST, eldv,
365 G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
369 tmp = intel_de_read(dev_priv, G4X_AUD_CNTL_ST);
370 tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
371 len = (tmp >> 9) & 0x1f; /* ELD buffer size */
372 intel_de_write(dev_priv, G4X_AUD_CNTL_ST, tmp);
374 len = min(drm_eld_size(eld) / 4, len);
375 drm_dbg(&dev_priv->drm, "ELD size %d\n", len);
376 for (i = 0; i < len; i++)
377 intel_de_write(dev_priv, G4X_HDMIW_HDMIEDID,
378 *((const u32 *)eld + i));
380 tmp = intel_de_read(dev_priv, G4X_AUD_CNTL_ST);
382 intel_de_write(dev_priv, G4X_AUD_CNTL_ST, tmp);
386 hsw_dp_audio_config_update(struct intel_encoder *encoder,
387 const struct intel_crtc_state *crtc_state)
389 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
390 struct i915_audio_component *acomp = dev_priv->audio_component;
391 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
392 enum port port = encoder->port;
393 const struct dp_aud_n_m *nm;
397 rate = acomp ? acomp->aud_sample_rate[port] : 0;
398 nm = audio_config_dp_get_n_m(crtc_state, rate);
400 drm_dbg_kms(&dev_priv->drm, "using Maud %u, Naud %u\n", nm->m,
403 drm_dbg_kms(&dev_priv->drm, "using automatic Maud, Naud\n");
405 tmp = intel_de_read(dev_priv, HSW_AUD_CFG(cpu_transcoder));
406 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
407 tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
408 tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
409 tmp |= AUD_CONFIG_N_VALUE_INDEX;
412 tmp &= ~AUD_CONFIG_N_MASK;
413 tmp |= AUD_CONFIG_N(nm->n);
414 tmp |= AUD_CONFIG_N_PROG_ENABLE;
417 intel_de_write(dev_priv, HSW_AUD_CFG(cpu_transcoder), tmp);
419 tmp = intel_de_read(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
420 tmp &= ~AUD_CONFIG_M_MASK;
421 tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
422 tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
426 tmp |= AUD_M_CTS_M_VALUE_INDEX;
427 tmp |= AUD_M_CTS_M_PROG_ENABLE;
430 intel_de_write(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
434 hsw_hdmi_audio_config_update(struct intel_encoder *encoder,
435 const struct intel_crtc_state *crtc_state)
437 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
438 struct i915_audio_component *acomp = dev_priv->audio_component;
439 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
440 enum port port = encoder->port;
444 rate = acomp ? acomp->aud_sample_rate[port] : 0;
446 tmp = intel_de_read(dev_priv, HSW_AUD_CFG(cpu_transcoder));
447 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
448 tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
449 tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
450 tmp |= audio_config_hdmi_pixel_clock(crtc_state);
452 n = audio_config_hdmi_get_n(crtc_state, rate);
454 drm_dbg_kms(&dev_priv->drm, "using N %d\n", n);
456 tmp &= ~AUD_CONFIG_N_MASK;
457 tmp |= AUD_CONFIG_N(n);
458 tmp |= AUD_CONFIG_N_PROG_ENABLE;
460 drm_dbg_kms(&dev_priv->drm, "using automatic N\n");
463 intel_de_write(dev_priv, HSW_AUD_CFG(cpu_transcoder), tmp);
466 * Let's disable "Enable CTS or M Prog bit"
467 * and let HW calculate the value
469 tmp = intel_de_read(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
470 tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
471 tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
472 intel_de_write(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
476 hsw_audio_config_update(struct intel_encoder *encoder,
477 const struct intel_crtc_state *crtc_state)
479 if (intel_crtc_has_dp_encoder(crtc_state))
480 hsw_dp_audio_config_update(encoder, crtc_state);
482 hsw_hdmi_audio_config_update(encoder, crtc_state);
485 static void hsw_audio_codec_disable(struct intel_encoder *encoder,
486 const struct intel_crtc_state *old_crtc_state,
487 const struct drm_connector_state *old_conn_state)
489 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
490 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
493 drm_dbg_kms(&dev_priv->drm, "Disable audio codec on transcoder %s\n",
494 transcoder_name(cpu_transcoder));
496 mutex_lock(&dev_priv->av_mutex);
498 /* Disable timestamps */
499 tmp = intel_de_read(dev_priv, HSW_AUD_CFG(cpu_transcoder));
500 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
501 tmp |= AUD_CONFIG_N_PROG_ENABLE;
502 tmp &= ~AUD_CONFIG_UPPER_N_MASK;
503 tmp &= ~AUD_CONFIG_LOWER_N_MASK;
504 if (intel_crtc_has_dp_encoder(old_crtc_state))
505 tmp |= AUD_CONFIG_N_VALUE_INDEX;
506 intel_de_write(dev_priv, HSW_AUD_CFG(cpu_transcoder), tmp);
509 tmp = intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD);
510 tmp &= ~AUDIO_ELD_VALID(cpu_transcoder);
511 tmp &= ~AUDIO_OUTPUT_ENABLE(cpu_transcoder);
512 intel_de_write(dev_priv, HSW_AUD_PIN_ELD_CP_VLD, tmp);
514 mutex_unlock(&dev_priv->av_mutex);
517 /* Add a factor to take care of rounding and truncations */
518 #define ROUNDING_FACTOR 10000
520 static unsigned int get_hblank_early_enable_config(struct intel_encoder *encoder,
521 const struct intel_crtc_state *crtc_state)
523 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
524 unsigned int link_clks_available, link_clks_required;
525 unsigned int tu_data, tu_line, link_clks_active;
526 unsigned int hblank_rise, hblank_early_prog;
527 unsigned int h_active, h_total, hblank_delta, pixel_clk, v_total;
528 unsigned int fec_coeff, refresh_rate, cdclk, vdsc_bpp;
530 h_active = crtc_state->hw.adjusted_mode.crtc_hdisplay;
531 h_total = crtc_state->hw.adjusted_mode.crtc_htotal;
532 v_total = crtc_state->hw.adjusted_mode.crtc_vtotal;
533 pixel_clk = crtc_state->hw.adjusted_mode.crtc_clock;
534 refresh_rate = crtc_state->hw.adjusted_mode.vrefresh;
535 vdsc_bpp = crtc_state->dsc.compressed_bpp;
536 cdclk = i915->cdclk.hw.cdclk;
537 /* fec= 0.972261, using rounding multiplier of 1000000 */
540 drm_dbg_kms(&i915->drm, "h_active = %u link_clk = %u :"
541 "lanes = %u vdsc_bpp = %u cdclk = %u\n",
542 h_active, crtc_state->port_clock, crtc_state->lane_count,
545 link_clks_available = ((((h_total - h_active) *
546 ((crtc_state->port_clock * ROUNDING_FACTOR) /
547 pixel_clk)) / ROUNDING_FACTOR) - 28);
549 link_clks_required = DIV_ROUND_UP(192000, (refresh_rate *
551 crtc_state->lane_count) + 2);
553 if (link_clks_available > link_clks_required)
556 hblank_delta = DIV_ROUND_UP(((((5 * ROUNDING_FACTOR) /
557 crtc_state->port_clock) + ((5 *
559 cdclk)) * pixel_clk),
562 tu_data = (pixel_clk * vdsc_bpp * 8) / ((crtc_state->port_clock *
563 crtc_state->lane_count * fec_coeff) / 1000000);
564 tu_line = (((h_active * crtc_state->port_clock * fec_coeff) /
565 1000000) / (64 * pixel_clk));
566 link_clks_active = (tu_line - 1) * 64 + tu_data;
568 hblank_rise = ((link_clks_active + 6 * DIV_ROUND_UP(link_clks_active,
569 250) + 4) * ((pixel_clk * ROUNDING_FACTOR) /
570 crtc_state->port_clock)) / ROUNDING_FACTOR;
572 hblank_early_prog = h_active - hblank_rise + hblank_delta;
574 return hblank_early_prog;
577 static unsigned int get_sample_room_req_config(const struct intel_crtc_state *crtc_state)
579 unsigned int h_active, h_total, pixel_clk;
580 unsigned int samples_room;
582 h_active = crtc_state->hw.adjusted_mode.hdisplay;
583 h_total = crtc_state->hw.adjusted_mode.htotal;
584 pixel_clk = crtc_state->hw.adjusted_mode.clock;
586 samples_room = ((((h_total - h_active) * ((crtc_state->port_clock *
587 ROUNDING_FACTOR) / pixel_clk)) /
588 ROUNDING_FACTOR) - 12) / ((48 /
589 crtc_state->lane_count) + 2);
594 static void enable_audio_dsc_wa(struct intel_encoder *encoder,
595 const struct intel_crtc_state *crtc_state)
597 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
598 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
599 enum pipe pipe = crtc->pipe;
600 unsigned int hblank_early_prog, samples_room, h_active;
603 if (INTEL_GEN(i915) < 11)
606 h_active = crtc_state->hw.adjusted_mode.hdisplay;
608 if (!(h_active && crtc_state->port_clock && crtc_state->lane_count &&
609 crtc_state->dsc.compressed_bpp && i915->cdclk.hw.cdclk)) {
610 drm_err(&i915->drm, "Null Params rcvd for hblank early enabling\n");
615 val = intel_de_read(i915, AUD_CONFIG_BE);
617 if (INTEL_GEN(i915) == 11)
618 val |= HBLANK_EARLY_ENABLE_ICL(pipe);
619 else if (INTEL_GEN(i915) >= 12)
620 val |= HBLANK_EARLY_ENABLE_TGL(pipe);
622 if (crtc_state->dsc.compression_enable &&
623 (crtc_state->hw.adjusted_mode.hdisplay >= 3840 &&
624 crtc_state->hw.adjusted_mode.vdisplay >= 2160)) {
625 /* Get hblank early enable value required */
626 hblank_early_prog = get_hblank_early_enable_config(encoder,
628 if (hblank_early_prog < 32) {
629 val &= ~HBLANK_START_COUNT_MASK(pipe);
630 val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_32);
631 } else if (hblank_early_prog < 64) {
632 val &= ~HBLANK_START_COUNT_MASK(pipe);
633 val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_64);
634 } else if (hblank_early_prog < 96) {
635 val &= ~HBLANK_START_COUNT_MASK(pipe);
636 val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_96);
638 val &= ~HBLANK_START_COUNT_MASK(pipe);
639 val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_128);
642 /* Get samples room value required */
643 samples_room = get_sample_room_req_config(crtc_state);
644 if (samples_room < 3) {
645 val &= ~NUMBER_SAMPLES_PER_LINE_MASK(pipe);
646 val |= NUMBER_SAMPLES_PER_LINE(pipe, samples_room);
648 /* Program 0 i.e "All Samples available in buffer" */
649 val &= ~NUMBER_SAMPLES_PER_LINE_MASK(pipe);
650 val |= NUMBER_SAMPLES_PER_LINE(pipe, 0x0);
654 intel_de_write(i915, AUD_CONFIG_BE, val);
657 #undef ROUNDING_FACTOR
659 static void hsw_audio_codec_enable(struct intel_encoder *encoder,
660 const struct intel_crtc_state *crtc_state,
661 const struct drm_connector_state *conn_state)
663 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
664 struct drm_connector *connector = conn_state->connector;
665 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
666 const u8 *eld = connector->eld;
670 drm_dbg_kms(&dev_priv->drm,
671 "Enable audio codec on transcoder %s, %u bytes ELD\n",
672 transcoder_name(cpu_transcoder), drm_eld_size(eld));
674 mutex_lock(&dev_priv->av_mutex);
676 /* Enable Audio WA for 4k DSC usecases */
677 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP))
678 enable_audio_dsc_wa(encoder, crtc_state);
680 /* Enable audio presence detect, invalidate ELD */
681 tmp = intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD);
682 tmp |= AUDIO_OUTPUT_ENABLE(cpu_transcoder);
683 tmp &= ~AUDIO_ELD_VALID(cpu_transcoder);
684 intel_de_write(dev_priv, HSW_AUD_PIN_ELD_CP_VLD, tmp);
687 * FIXME: We're supposed to wait for vblank here, but we have vblanks
688 * disabled during the mode set. The proper fix would be to push the
689 * rest of the setup into a vblank work item, queued here, but the
690 * infrastructure is not there yet.
693 /* Reset ELD write address */
694 tmp = intel_de_read(dev_priv, HSW_AUD_DIP_ELD_CTRL(cpu_transcoder));
695 tmp &= ~IBX_ELD_ADDRESS_MASK;
696 intel_de_write(dev_priv, HSW_AUD_DIP_ELD_CTRL(cpu_transcoder), tmp);
698 /* Up to 84 bytes of hw ELD buffer */
699 len = min(drm_eld_size(eld), 84);
700 for (i = 0; i < len / 4; i++)
701 intel_de_write(dev_priv, HSW_AUD_EDID_DATA(cpu_transcoder),
702 *((const u32 *)eld + i));
705 tmp = intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD);
706 tmp |= AUDIO_ELD_VALID(cpu_transcoder);
707 intel_de_write(dev_priv, HSW_AUD_PIN_ELD_CP_VLD, tmp);
709 /* Enable timestamps */
710 hsw_audio_config_update(encoder, crtc_state);
712 mutex_unlock(&dev_priv->av_mutex);
715 static void ilk_audio_codec_disable(struct intel_encoder *encoder,
716 const struct intel_crtc_state *old_crtc_state,
717 const struct drm_connector_state *old_conn_state)
719 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
720 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
721 enum pipe pipe = crtc->pipe;
722 enum port port = encoder->port;
724 i915_reg_t aud_config, aud_cntrl_st2;
726 drm_dbg_kms(&dev_priv->drm,
727 "Disable audio codec on [ENCODER:%d:%s], pipe %c\n",
728 encoder->base.base.id, encoder->base.name,
731 if (drm_WARN_ON(&dev_priv->drm, port == PORT_A))
734 if (HAS_PCH_IBX(dev_priv)) {
735 aud_config = IBX_AUD_CFG(pipe);
736 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
737 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
738 aud_config = VLV_AUD_CFG(pipe);
739 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
741 aud_config = CPT_AUD_CFG(pipe);
742 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
745 /* Disable timestamps */
746 tmp = intel_de_read(dev_priv, aud_config);
747 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
748 tmp |= AUD_CONFIG_N_PROG_ENABLE;
749 tmp &= ~AUD_CONFIG_UPPER_N_MASK;
750 tmp &= ~AUD_CONFIG_LOWER_N_MASK;
751 if (intel_crtc_has_dp_encoder(old_crtc_state))
752 tmp |= AUD_CONFIG_N_VALUE_INDEX;
753 intel_de_write(dev_priv, aud_config, tmp);
755 eldv = IBX_ELD_VALID(port);
758 tmp = intel_de_read(dev_priv, aud_cntrl_st2);
760 intel_de_write(dev_priv, aud_cntrl_st2, tmp);
763 static void ilk_audio_codec_enable(struct intel_encoder *encoder,
764 const struct intel_crtc_state *crtc_state,
765 const struct drm_connector_state *conn_state)
767 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
768 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
769 struct drm_connector *connector = conn_state->connector;
770 enum pipe pipe = crtc->pipe;
771 enum port port = encoder->port;
772 const u8 *eld = connector->eld;
775 i915_reg_t hdmiw_hdmiedid, aud_config, aud_cntl_st, aud_cntrl_st2;
777 drm_dbg_kms(&dev_priv->drm,
778 "Enable audio codec on [ENCODER:%d:%s], pipe %c, %u bytes ELD\n",
779 encoder->base.base.id, encoder->base.name,
780 pipe_name(pipe), drm_eld_size(eld));
782 if (drm_WARN_ON(&dev_priv->drm, port == PORT_A))
786 * FIXME: We're supposed to wait for vblank here, but we have vblanks
787 * disabled during the mode set. The proper fix would be to push the
788 * rest of the setup into a vblank work item, queued here, but the
789 * infrastructure is not there yet.
792 if (HAS_PCH_IBX(dev_priv)) {
793 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
794 aud_config = IBX_AUD_CFG(pipe);
795 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
796 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
797 } else if (IS_VALLEYVIEW(dev_priv) ||
798 IS_CHERRYVIEW(dev_priv)) {
799 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
800 aud_config = VLV_AUD_CFG(pipe);
801 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
802 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
804 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
805 aud_config = CPT_AUD_CFG(pipe);
806 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
807 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
810 eldv = IBX_ELD_VALID(port);
813 tmp = intel_de_read(dev_priv, aud_cntrl_st2);
815 intel_de_write(dev_priv, aud_cntrl_st2, tmp);
817 /* Reset ELD write address */
818 tmp = intel_de_read(dev_priv, aud_cntl_st);
819 tmp &= ~IBX_ELD_ADDRESS_MASK;
820 intel_de_write(dev_priv, aud_cntl_st, tmp);
822 /* Up to 84 bytes of hw ELD buffer */
823 len = min(drm_eld_size(eld), 84);
824 for (i = 0; i < len / 4; i++)
825 intel_de_write(dev_priv, hdmiw_hdmiedid,
826 *((const u32 *)eld + i));
829 tmp = intel_de_read(dev_priv, aud_cntrl_st2);
831 intel_de_write(dev_priv, aud_cntrl_st2, tmp);
833 /* Enable timestamps */
834 tmp = intel_de_read(dev_priv, aud_config);
835 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
836 tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
837 tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
838 if (intel_crtc_has_dp_encoder(crtc_state))
839 tmp |= AUD_CONFIG_N_VALUE_INDEX;
841 tmp |= audio_config_hdmi_pixel_clock(crtc_state);
842 intel_de_write(dev_priv, aud_config, tmp);
846 * intel_audio_codec_enable - Enable the audio codec for HD audio
847 * @encoder: encoder on which to enable audio
848 * @crtc_state: pointer to the current crtc state.
849 * @conn_state: pointer to the current connector state.
851 * The enable sequences may only be performed after enabling the transcoder and
852 * port, and after completed link training.
854 void intel_audio_codec_enable(struct intel_encoder *encoder,
855 const struct intel_crtc_state *crtc_state,
856 const struct drm_connector_state *conn_state)
858 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
859 struct i915_audio_component *acomp = dev_priv->audio_component;
860 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
861 struct drm_connector *connector = conn_state->connector;
862 const struct drm_display_mode *adjusted_mode =
863 &crtc_state->hw.adjusted_mode;
864 enum port port = encoder->port;
865 enum pipe pipe = crtc->pipe;
867 /* FIXME precompute the ELD in .compute_config() */
868 if (!connector->eld[0])
869 drm_dbg_kms(&dev_priv->drm,
870 "Bogus ELD on [CONNECTOR:%d:%s]\n",
871 connector->base.id, connector->name);
873 drm_dbg(&dev_priv->drm, "ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
876 encoder->base.base.id,
879 connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
881 if (dev_priv->display.audio_codec_enable)
882 dev_priv->display.audio_codec_enable(encoder,
886 mutex_lock(&dev_priv->av_mutex);
887 encoder->audio_connector = connector;
889 /* referred in audio callbacks */
890 dev_priv->av_enc_map[pipe] = encoder;
891 mutex_unlock(&dev_priv->av_mutex);
893 if (acomp && acomp->base.audio_ops &&
894 acomp->base.audio_ops->pin_eld_notify) {
895 /* audio drivers expect pipe = -1 to indicate Non-MST cases */
896 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
898 acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
899 (int) port, (int) pipe);
902 intel_lpe_audio_notify(dev_priv, pipe, port, connector->eld,
903 crtc_state->port_clock,
904 intel_crtc_has_dp_encoder(crtc_state));
908 * intel_audio_codec_disable - Disable the audio codec for HD audio
909 * @encoder: encoder on which to disable audio
910 * @old_crtc_state: pointer to the old crtc state.
911 * @old_conn_state: pointer to the old connector state.
913 * The disable sequences must be performed before disabling the transcoder or
916 void intel_audio_codec_disable(struct intel_encoder *encoder,
917 const struct intel_crtc_state *old_crtc_state,
918 const struct drm_connector_state *old_conn_state)
920 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
921 struct i915_audio_component *acomp = dev_priv->audio_component;
922 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
923 enum port port = encoder->port;
924 enum pipe pipe = crtc->pipe;
926 if (dev_priv->display.audio_codec_disable)
927 dev_priv->display.audio_codec_disable(encoder,
931 mutex_lock(&dev_priv->av_mutex);
932 encoder->audio_connector = NULL;
933 dev_priv->av_enc_map[pipe] = NULL;
934 mutex_unlock(&dev_priv->av_mutex);
936 if (acomp && acomp->base.audio_ops &&
937 acomp->base.audio_ops->pin_eld_notify) {
938 /* audio drivers expect pipe = -1 to indicate Non-MST cases */
939 if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
941 acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
942 (int) port, (int) pipe);
945 intel_lpe_audio_notify(dev_priv, pipe, port, NULL, 0, false);
949 * intel_init_audio_hooks - Set up chip specific audio hooks
950 * @dev_priv: device private
952 void intel_init_audio_hooks(struct drm_i915_private *dev_priv)
954 if (IS_G4X(dev_priv)) {
955 dev_priv->display.audio_codec_enable = g4x_audio_codec_enable;
956 dev_priv->display.audio_codec_disable = g4x_audio_codec_disable;
957 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
958 dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
959 dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
960 } else if (IS_HASWELL(dev_priv) || INTEL_GEN(dev_priv) >= 8) {
961 dev_priv->display.audio_codec_enable = hsw_audio_codec_enable;
962 dev_priv->display.audio_codec_disable = hsw_audio_codec_disable;
963 } else if (HAS_PCH_SPLIT(dev_priv)) {
964 dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
965 dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
969 static int glk_force_audio_cdclk_commit(struct intel_atomic_state *state,
970 struct intel_crtc *crtc,
973 struct intel_cdclk_state *cdclk_state;
976 /* need to hold at least one crtc lock for the global state */
977 ret = drm_modeset_lock(&crtc->base.mutex, state->base.acquire_ctx);
981 cdclk_state = intel_atomic_get_cdclk_state(state);
982 if (IS_ERR(cdclk_state))
983 return PTR_ERR(cdclk_state);
985 cdclk_state->force_min_cdclk_changed = true;
986 cdclk_state->force_min_cdclk = enable ? 2 * 96000 : 0;
988 ret = intel_atomic_lock_global_state(&cdclk_state->base);
992 return drm_atomic_commit(&state->base);
995 static void glk_force_audio_cdclk(struct drm_i915_private *dev_priv,
998 struct drm_modeset_acquire_ctx ctx;
999 struct drm_atomic_state *state;
1000 struct intel_crtc *crtc;
1003 crtc = intel_get_first_crtc(dev_priv);
1007 drm_modeset_acquire_init(&ctx, 0);
1008 state = drm_atomic_state_alloc(&dev_priv->drm);
1009 if (drm_WARN_ON(&dev_priv->drm, !state))
1012 state->acquire_ctx = &ctx;
1015 ret = glk_force_audio_cdclk_commit(to_intel_atomic_state(state), crtc,
1017 if (ret == -EDEADLK) {
1018 drm_atomic_state_clear(state);
1019 drm_modeset_backoff(&ctx);
1023 drm_WARN_ON(&dev_priv->drm, ret);
1025 drm_atomic_state_put(state);
1027 drm_modeset_drop_locks(&ctx);
1028 drm_modeset_acquire_fini(&ctx);
1031 static unsigned long i915_audio_component_get_power(struct device *kdev)
1033 struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1034 intel_wakeref_t ret;
1036 /* Catch potential impedance mismatches before they occur! */
1037 BUILD_BUG_ON(sizeof(intel_wakeref_t) > sizeof(unsigned long));
1039 ret = intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
1041 if (dev_priv->audio_power_refcount++ == 0) {
1042 if (INTEL_GEN(dev_priv) >= 9) {
1043 intel_de_write(dev_priv, AUD_FREQ_CNTRL,
1044 dev_priv->audio_freq_cntrl);
1045 drm_dbg_kms(&dev_priv->drm,
1046 "restored AUD_FREQ_CNTRL to 0x%x\n",
1047 dev_priv->audio_freq_cntrl);
1050 /* Force CDCLK to 2*BCLK as long as we need audio powered. */
1051 if (IS_GEMINILAKE(dev_priv))
1052 glk_force_audio_cdclk(dev_priv, true);
1054 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
1055 intel_de_write(dev_priv, AUD_PIN_BUF_CTL,
1056 (intel_de_read(dev_priv, AUD_PIN_BUF_CTL) | AUD_PIN_BUF_ENABLE));
1062 static void i915_audio_component_put_power(struct device *kdev,
1063 unsigned long cookie)
1065 struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1067 /* Stop forcing CDCLK to 2*BCLK if no need for audio to be powered. */
1068 if (--dev_priv->audio_power_refcount == 0)
1069 if (IS_GEMINILAKE(dev_priv))
1070 glk_force_audio_cdclk(dev_priv, false);
1072 intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO, cookie);
1075 static void i915_audio_component_codec_wake_override(struct device *kdev,
1078 struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1079 unsigned long cookie;
1082 if (INTEL_GEN(dev_priv) < 9)
1085 cookie = i915_audio_component_get_power(kdev);
1088 * Enable/disable generating the codec wake signal, overriding the
1089 * internal logic to generate the codec wake to controller.
1091 tmp = intel_de_read(dev_priv, HSW_AUD_CHICKENBIT);
1092 tmp &= ~SKL_AUD_CODEC_WAKE_SIGNAL;
1093 intel_de_write(dev_priv, HSW_AUD_CHICKENBIT, tmp);
1094 usleep_range(1000, 1500);
1097 tmp = intel_de_read(dev_priv, HSW_AUD_CHICKENBIT);
1098 tmp |= SKL_AUD_CODEC_WAKE_SIGNAL;
1099 intel_de_write(dev_priv, HSW_AUD_CHICKENBIT, tmp);
1100 usleep_range(1000, 1500);
1103 i915_audio_component_put_power(kdev, cookie);
1106 /* Get CDCLK in kHz */
1107 static int i915_audio_component_get_cdclk_freq(struct device *kdev)
1109 struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1111 if (drm_WARN_ON_ONCE(&dev_priv->drm, !HAS_DDI(dev_priv)))
1114 return dev_priv->cdclk.hw.cdclk;
1118 * get the intel_encoder according to the parameter port and pipe
1119 * intel_encoder is saved by the index of pipe
1120 * MST & (pipe >= 0): return the av_enc_map[pipe],
1121 * when port is matched
1122 * MST & (pipe < 0): this is invalid
1123 * Non-MST & (pipe >= 0): only pipe = 0 (the first device entry)
1124 * will get the right intel_encoder with port matched
1125 * Non-MST & (pipe < 0): get the right intel_encoder with port matched
1127 static struct intel_encoder *get_saved_enc(struct drm_i915_private *dev_priv,
1130 struct intel_encoder *encoder;
1134 if (drm_WARN_ON(&dev_priv->drm,
1135 pipe >= ARRAY_SIZE(dev_priv->av_enc_map)))
1138 encoder = dev_priv->av_enc_map[pipe];
1140 * when bootup, audio driver may not know it is
1141 * MST or not. So it will poll all the port & pipe
1144 if (encoder != NULL && encoder->port == port &&
1145 encoder->type == INTEL_OUTPUT_DP_MST)
1153 for_each_pipe(dev_priv, pipe) {
1154 encoder = dev_priv->av_enc_map[pipe];
1155 if (encoder == NULL)
1158 if (encoder->type == INTEL_OUTPUT_DP_MST)
1161 if (port == encoder->port)
1168 static int i915_audio_component_sync_audio_rate(struct device *kdev, int port,
1171 struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1172 struct i915_audio_component *acomp = dev_priv->audio_component;
1173 struct intel_encoder *encoder;
1174 struct intel_crtc *crtc;
1175 unsigned long cookie;
1178 if (!HAS_DDI(dev_priv))
1181 cookie = i915_audio_component_get_power(kdev);
1182 mutex_lock(&dev_priv->av_mutex);
1184 /* 1. get the pipe */
1185 encoder = get_saved_enc(dev_priv, port, pipe);
1186 if (!encoder || !encoder->base.crtc) {
1187 drm_dbg_kms(&dev_priv->drm, "Not valid for port %c\n",
1193 crtc = to_intel_crtc(encoder->base.crtc);
1195 /* port must be valid now, otherwise the pipe will be invalid */
1196 acomp->aud_sample_rate[port] = rate;
1198 hsw_audio_config_update(encoder, crtc->config);
1201 mutex_unlock(&dev_priv->av_mutex);
1202 i915_audio_component_put_power(kdev, cookie);
1206 static int i915_audio_component_get_eld(struct device *kdev, int port,
1207 int pipe, bool *enabled,
1208 unsigned char *buf, int max_bytes)
1210 struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1211 struct intel_encoder *intel_encoder;
1215 mutex_lock(&dev_priv->av_mutex);
1217 intel_encoder = get_saved_enc(dev_priv, port, pipe);
1218 if (!intel_encoder) {
1219 drm_dbg_kms(&dev_priv->drm, "Not valid for port %c\n",
1221 mutex_unlock(&dev_priv->av_mutex);
1226 *enabled = intel_encoder->audio_connector != NULL;
1228 eld = intel_encoder->audio_connector->eld;
1229 ret = drm_eld_size(eld);
1230 memcpy(buf, eld, min(max_bytes, ret));
1233 mutex_unlock(&dev_priv->av_mutex);
1237 static const struct drm_audio_component_ops i915_audio_component_ops = {
1238 .owner = THIS_MODULE,
1239 .get_power = i915_audio_component_get_power,
1240 .put_power = i915_audio_component_put_power,
1241 .codec_wake_override = i915_audio_component_codec_wake_override,
1242 .get_cdclk_freq = i915_audio_component_get_cdclk_freq,
1243 .sync_audio_rate = i915_audio_component_sync_audio_rate,
1244 .get_eld = i915_audio_component_get_eld,
1247 static int i915_audio_component_bind(struct device *i915_kdev,
1248 struct device *hda_kdev, void *data)
1250 struct i915_audio_component *acomp = data;
1251 struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
1254 if (drm_WARN_ON(&dev_priv->drm, acomp->base.ops || acomp->base.dev))
1257 if (drm_WARN_ON(&dev_priv->drm,
1258 !device_link_add(hda_kdev, i915_kdev,
1259 DL_FLAG_STATELESS)))
1262 drm_modeset_lock_all(&dev_priv->drm);
1263 acomp->base.ops = &i915_audio_component_ops;
1264 acomp->base.dev = i915_kdev;
1265 BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
1266 for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
1267 acomp->aud_sample_rate[i] = 0;
1268 dev_priv->audio_component = acomp;
1269 drm_modeset_unlock_all(&dev_priv->drm);
1274 static void i915_audio_component_unbind(struct device *i915_kdev,
1275 struct device *hda_kdev, void *data)
1277 struct i915_audio_component *acomp = data;
1278 struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
1280 drm_modeset_lock_all(&dev_priv->drm);
1281 acomp->base.ops = NULL;
1282 acomp->base.dev = NULL;
1283 dev_priv->audio_component = NULL;
1284 drm_modeset_unlock_all(&dev_priv->drm);
1286 device_link_remove(hda_kdev, i915_kdev);
1289 static const struct component_ops i915_audio_component_bind_ops = {
1290 .bind = i915_audio_component_bind,
1291 .unbind = i915_audio_component_unbind,
1295 * i915_audio_component_init - initialize and register the audio component
1296 * @dev_priv: i915 device instance
1298 * This will register with the component framework a child component which
1299 * will bind dynamically to the snd_hda_intel driver's corresponding master
1300 * component when the latter is registered. During binding the child
1301 * initializes an instance of struct i915_audio_component which it receives
1302 * from the master. The master can then start to use the interface defined by
1303 * this struct. Each side can break the binding at any point by deregistering
1304 * its own component after which each side's component unbind callback is
1307 * We ignore any error during registration and continue with reduced
1308 * functionality (i.e. without HDMI audio).
1310 static void i915_audio_component_init(struct drm_i915_private *dev_priv)
1314 ret = component_add_typed(dev_priv->drm.dev,
1315 &i915_audio_component_bind_ops,
1316 I915_COMPONENT_AUDIO);
1318 drm_err(&dev_priv->drm,
1319 "failed to add audio component (%d)\n", ret);
1320 /* continue with reduced functionality */
1324 if (INTEL_GEN(dev_priv) >= 9) {
1325 dev_priv->audio_freq_cntrl = intel_de_read(dev_priv,
1327 drm_dbg_kms(&dev_priv->drm,
1328 "init value of AUD_FREQ_CNTRL of 0x%x\n",
1329 dev_priv->audio_freq_cntrl);
1332 dev_priv->audio_component_registered = true;
1336 * i915_audio_component_cleanup - deregister the audio component
1337 * @dev_priv: i915 device instance
1339 * Deregisters the audio component, breaking any existing binding to the
1340 * corresponding snd_hda_intel driver's master component.
1342 static void i915_audio_component_cleanup(struct drm_i915_private *dev_priv)
1344 if (!dev_priv->audio_component_registered)
1347 component_del(dev_priv->drm.dev, &i915_audio_component_bind_ops);
1348 dev_priv->audio_component_registered = false;
1352 * intel_audio_init() - Initialize the audio driver either using
1353 * component framework or using lpe audio bridge
1354 * @dev_priv: the i915 drm device private data
1357 void intel_audio_init(struct drm_i915_private *dev_priv)
1359 if (intel_lpe_audio_init(dev_priv) < 0)
1360 i915_audio_component_init(dev_priv);
1364 * intel_audio_deinit() - deinitialize the audio driver
1365 * @dev_priv: the i915 drm device private data
1368 void intel_audio_deinit(struct drm_i915_private *dev_priv)
1370 if ((dev_priv)->lpe_audio.platdev != NULL)
1371 intel_lpe_audio_teardown(dev_priv);
1373 i915_audio_component_cleanup(dev_priv);