xref: /linux/drivers/gpu/drm/i915/display/intel_audio.c (revision f47a0e35846740ca72b8ba6be84b5b518abcebce)
1df0566a6SJani Nikula /*
2df0566a6SJani Nikula  * Copyright © 2014 Intel Corporation
3df0566a6SJani Nikula  *
4df0566a6SJani Nikula  * Permission is hereby granted, free of charge, to any person obtaining a
5df0566a6SJani Nikula  * copy of this software and associated documentation files (the "Software"),
6df0566a6SJani Nikula  * to deal in the Software without restriction, including without limitation
7df0566a6SJani Nikula  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8df0566a6SJani Nikula  * and/or sell copies of the Software, and to permit persons to whom the
9df0566a6SJani Nikula  * Software is furnished to do so, subject to the following conditions:
10df0566a6SJani Nikula  *
11df0566a6SJani Nikula  * The above copyright notice and this permission notice (including the next
12df0566a6SJani Nikula  * paragraph) shall be included in all copies or substantial portions of the
13df0566a6SJani Nikula  * Software.
14df0566a6SJani Nikula  *
15df0566a6SJani Nikula  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16df0566a6SJani Nikula  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17df0566a6SJani Nikula  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18df0566a6SJani Nikula  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19df0566a6SJani Nikula  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20df0566a6SJani Nikula  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21df0566a6SJani Nikula  * DEALINGS IN THE SOFTWARE.
22df0566a6SJani Nikula  */
23df0566a6SJani Nikula 
24df0566a6SJani Nikula #include <linux/component.h>
25df0566a6SJani Nikula #include <linux/kernel.h>
26df0566a6SJani Nikula 
27df0566a6SJani Nikula #include <drm/drm_edid.h>
28df0566a6SJani Nikula #include <drm/i915_component.h>
29df0566a6SJani Nikula 
30df0566a6SJani Nikula #include "i915_drv.h"
311d5a95b5SVille Syrjälä #include "intel_atomic.h"
32df0566a6SJani Nikula #include "intel_audio.h"
3328a30b45SVille Syrjälä #include "intel_cdclk.h"
347785ae0bSVille Syrjälä #include "intel_de.h"
351d455f8dSJani Nikula #include "intel_display_types.h"
36df0566a6SJani Nikula #include "intel_lpe_audio.h"
37df0566a6SJani Nikula 
38df0566a6SJani Nikula /**
39df0566a6SJani Nikula  * DOC: High Definition Audio over HDMI and Display Port
40df0566a6SJani Nikula  *
41df0566a6SJani Nikula  * The graphics and audio drivers together support High Definition Audio over
42df0566a6SJani Nikula  * HDMI and Display Port. The audio programming sequences are divided into audio
43df0566a6SJani Nikula  * codec and controller enable and disable sequences. The graphics driver
44df0566a6SJani Nikula  * handles the audio codec sequences, while the audio driver handles the audio
45df0566a6SJani Nikula  * controller sequences.
46df0566a6SJani Nikula  *
47df0566a6SJani Nikula  * The disable sequences must be performed before disabling the transcoder or
48df0566a6SJani Nikula  * port. The enable sequences may only be performed after enabling the
49df0566a6SJani Nikula  * transcoder and port, and after completed link training. Therefore the audio
50df0566a6SJani Nikula  * enable/disable sequences are part of the modeset sequence.
51df0566a6SJani Nikula  *
52df0566a6SJani Nikula  * The codec and controller sequences could be done either parallel or serial,
53df0566a6SJani Nikula  * but generally the ELDV/PD change in the codec sequence indicates to the audio
54df0566a6SJani Nikula  * driver that the controller sequence should start. Indeed, most of the
55df0566a6SJani Nikula  * co-operation between the graphics and audio drivers is handled via audio
56df0566a6SJani Nikula  * related registers. (The notable exception is the power management, not
57df0566a6SJani Nikula  * covered here.)
58df0566a6SJani Nikula  *
59df0566a6SJani Nikula  * The struct &i915_audio_component is used to interact between the graphics
60df0566a6SJani Nikula  * and audio drivers. The struct &i915_audio_component_ops @ops in it is
61df0566a6SJani Nikula  * defined in graphics driver and called in audio driver. The
62df0566a6SJani Nikula  * struct &i915_audio_component_audio_ops @audio_ops is called from i915 driver.
63df0566a6SJani Nikula  */
64df0566a6SJani Nikula 
655d453746SJani Nikula struct intel_audio_funcs {
665d453746SJani Nikula 	void (*audio_codec_enable)(struct intel_encoder *encoder,
675d453746SJani Nikula 				   const struct intel_crtc_state *crtc_state,
685d453746SJani Nikula 				   const struct drm_connector_state *conn_state);
695d453746SJani Nikula 	void (*audio_codec_disable)(struct intel_encoder *encoder,
705d453746SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
715d453746SJani Nikula 				    const struct drm_connector_state *old_conn_state);
725d453746SJani Nikula };
735d453746SJani Nikula 
74df0566a6SJani Nikula /* DP N/M table */
75df0566a6SJani Nikula #define LC_810M	810000
76df0566a6SJani Nikula #define LC_540M	540000
77df0566a6SJani Nikula #define LC_270M	270000
78df0566a6SJani Nikula #define LC_162M	162000
79df0566a6SJani Nikula 
80df0566a6SJani Nikula struct dp_aud_n_m {
81df0566a6SJani Nikula 	int sample_rate;
82df0566a6SJani Nikula 	int clock;
83df0566a6SJani Nikula 	u16 m;
84df0566a6SJani Nikula 	u16 n;
85df0566a6SJani Nikula };
86df0566a6SJani Nikula 
872c291417SAditya Swarup struct hdmi_aud_ncts {
882c291417SAditya Swarup 	int sample_rate;
892c291417SAditya Swarup 	int clock;
902c291417SAditya Swarup 	int n;
912c291417SAditya Swarup 	int cts;
922c291417SAditya Swarup };
932c291417SAditya Swarup 
94df0566a6SJani Nikula /* Values according to DP 1.4 Table 2-104 */
95df0566a6SJani Nikula static const struct dp_aud_n_m dp_aud_n_m[] = {
96df0566a6SJani Nikula 	{ 32000, LC_162M, 1024, 10125 },
97df0566a6SJani Nikula 	{ 44100, LC_162M, 784, 5625 },
98df0566a6SJani Nikula 	{ 48000, LC_162M, 512, 3375 },
99df0566a6SJani Nikula 	{ 64000, LC_162M, 2048, 10125 },
100df0566a6SJani Nikula 	{ 88200, LC_162M, 1568, 5625 },
101df0566a6SJani Nikula 	{ 96000, LC_162M, 1024, 3375 },
102df0566a6SJani Nikula 	{ 128000, LC_162M, 4096, 10125 },
103df0566a6SJani Nikula 	{ 176400, LC_162M, 3136, 5625 },
104df0566a6SJani Nikula 	{ 192000, LC_162M, 2048, 3375 },
105df0566a6SJani Nikula 	{ 32000, LC_270M, 1024, 16875 },
106df0566a6SJani Nikula 	{ 44100, LC_270M, 784, 9375 },
107df0566a6SJani Nikula 	{ 48000, LC_270M, 512, 5625 },
108df0566a6SJani Nikula 	{ 64000, LC_270M, 2048, 16875 },
109df0566a6SJani Nikula 	{ 88200, LC_270M, 1568, 9375 },
110df0566a6SJani Nikula 	{ 96000, LC_270M, 1024, 5625 },
111df0566a6SJani Nikula 	{ 128000, LC_270M, 4096, 16875 },
112df0566a6SJani Nikula 	{ 176400, LC_270M, 3136, 9375 },
113df0566a6SJani Nikula 	{ 192000, LC_270M, 2048, 5625 },
114df0566a6SJani Nikula 	{ 32000, LC_540M, 1024, 33750 },
115df0566a6SJani Nikula 	{ 44100, LC_540M, 784, 18750 },
116df0566a6SJani Nikula 	{ 48000, LC_540M, 512, 11250 },
117df0566a6SJani Nikula 	{ 64000, LC_540M, 2048, 33750 },
118df0566a6SJani Nikula 	{ 88200, LC_540M, 1568, 18750 },
119df0566a6SJani Nikula 	{ 96000, LC_540M, 1024, 11250 },
120df0566a6SJani Nikula 	{ 128000, LC_540M, 4096, 33750 },
121df0566a6SJani Nikula 	{ 176400, LC_540M, 3136, 18750 },
122df0566a6SJani Nikula 	{ 192000, LC_540M, 2048, 11250 },
123df0566a6SJani Nikula 	{ 32000, LC_810M, 1024, 50625 },
124df0566a6SJani Nikula 	{ 44100, LC_810M, 784, 28125 },
125df0566a6SJani Nikula 	{ 48000, LC_810M, 512, 16875 },
126df0566a6SJani Nikula 	{ 64000, LC_810M, 2048, 50625 },
127df0566a6SJani Nikula 	{ 88200, LC_810M, 1568, 28125 },
128df0566a6SJani Nikula 	{ 96000, LC_810M, 1024, 16875 },
129df0566a6SJani Nikula 	{ 128000, LC_810M, 4096, 50625 },
130df0566a6SJani Nikula 	{ 176400, LC_810M, 3136, 28125 },
131df0566a6SJani Nikula 	{ 192000, LC_810M, 2048, 16875 },
132df0566a6SJani Nikula };
133df0566a6SJani Nikula 
134df0566a6SJani Nikula static const struct dp_aud_n_m *
135df0566a6SJani Nikula audio_config_dp_get_n_m(const struct intel_crtc_state *crtc_state, int rate)
136df0566a6SJani Nikula {
137df0566a6SJani Nikula 	int i;
138df0566a6SJani Nikula 
139df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(dp_aud_n_m); i++) {
140df0566a6SJani Nikula 		if (rate == dp_aud_n_m[i].sample_rate &&
141df0566a6SJani Nikula 		    crtc_state->port_clock == dp_aud_n_m[i].clock)
142df0566a6SJani Nikula 			return &dp_aud_n_m[i];
143df0566a6SJani Nikula 	}
144df0566a6SJani Nikula 
145df0566a6SJani Nikula 	return NULL;
146df0566a6SJani Nikula }
147df0566a6SJani Nikula 
148df0566a6SJani Nikula static const struct {
149df0566a6SJani Nikula 	int clock;
150df0566a6SJani Nikula 	u32 config;
151df0566a6SJani Nikula } hdmi_audio_clock[] = {
152df0566a6SJani Nikula 	{ 25175, AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
153df0566a6SJani Nikula 	{ 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
154df0566a6SJani Nikula 	{ 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
155df0566a6SJani Nikula 	{ 27027, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
156df0566a6SJani Nikula 	{ 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
157df0566a6SJani Nikula 	{ 54054, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
158df0566a6SJani Nikula 	{ 74176, AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
159df0566a6SJani Nikula 	{ 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
160df0566a6SJani Nikula 	{ 148352, AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
161df0566a6SJani Nikula 	{ 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
1621aae3065SKai Vehmanen 	{ 296703, AUD_CONFIG_PIXEL_CLOCK_HDMI_296703 },
1631aae3065SKai Vehmanen 	{ 297000, AUD_CONFIG_PIXEL_CLOCK_HDMI_297000 },
1641aae3065SKai Vehmanen 	{ 593407, AUD_CONFIG_PIXEL_CLOCK_HDMI_593407 },
1651aae3065SKai Vehmanen 	{ 594000, AUD_CONFIG_PIXEL_CLOCK_HDMI_594000 },
166df0566a6SJani Nikula };
167df0566a6SJani Nikula 
168df0566a6SJani Nikula /* HDMI N/CTS table */
169df0566a6SJani Nikula #define TMDS_297M 297000
170df0566a6SJani Nikula #define TMDS_296M 296703
171df0566a6SJani Nikula #define TMDS_594M 594000
172df0566a6SJani Nikula #define TMDS_593M 593407
173df0566a6SJani Nikula 
1742c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_24bpp[] = {
175df0566a6SJani Nikula 	{ 32000, TMDS_296M, 5824, 421875 },
176df0566a6SJani Nikula 	{ 32000, TMDS_297M, 3072, 222750 },
177df0566a6SJani Nikula 	{ 32000, TMDS_593M, 5824, 843750 },
178df0566a6SJani Nikula 	{ 32000, TMDS_594M, 3072, 445500 },
179df0566a6SJani Nikula 	{ 44100, TMDS_296M, 4459, 234375 },
180df0566a6SJani Nikula 	{ 44100, TMDS_297M, 4704, 247500 },
181df0566a6SJani Nikula 	{ 44100, TMDS_593M, 8918, 937500 },
182df0566a6SJani Nikula 	{ 44100, TMDS_594M, 9408, 990000 },
183df0566a6SJani Nikula 	{ 88200, TMDS_296M, 8918, 234375 },
184df0566a6SJani Nikula 	{ 88200, TMDS_297M, 9408, 247500 },
185df0566a6SJani Nikula 	{ 88200, TMDS_593M, 17836, 937500 },
186df0566a6SJani Nikula 	{ 88200, TMDS_594M, 18816, 990000 },
187df0566a6SJani Nikula 	{ 176400, TMDS_296M, 17836, 234375 },
188df0566a6SJani Nikula 	{ 176400, TMDS_297M, 18816, 247500 },
189df0566a6SJani Nikula 	{ 176400, TMDS_593M, 35672, 937500 },
190df0566a6SJani Nikula 	{ 176400, TMDS_594M, 37632, 990000 },
191df0566a6SJani Nikula 	{ 48000, TMDS_296M, 5824, 281250 },
192df0566a6SJani Nikula 	{ 48000, TMDS_297M, 5120, 247500 },
193df0566a6SJani Nikula 	{ 48000, TMDS_593M, 5824, 562500 },
194df0566a6SJani Nikula 	{ 48000, TMDS_594M, 6144, 594000 },
195df0566a6SJani Nikula 	{ 96000, TMDS_296M, 11648, 281250 },
196df0566a6SJani Nikula 	{ 96000, TMDS_297M, 10240, 247500 },
197df0566a6SJani Nikula 	{ 96000, TMDS_593M, 11648, 562500 },
198df0566a6SJani Nikula 	{ 96000, TMDS_594M, 12288, 594000 },
199df0566a6SJani Nikula 	{ 192000, TMDS_296M, 23296, 281250 },
200df0566a6SJani Nikula 	{ 192000, TMDS_297M, 20480, 247500 },
201df0566a6SJani Nikula 	{ 192000, TMDS_593M, 23296, 562500 },
202df0566a6SJani Nikula 	{ 192000, TMDS_594M, 24576, 594000 },
203df0566a6SJani Nikula };
204df0566a6SJani Nikula 
2052c291417SAditya Swarup /* Appendix C - N & CTS values for deep color from HDMI 2.0 spec*/
2062c291417SAditya Swarup /* HDMI N/CTS table for 10 bit deep color(30 bpp)*/
2072c291417SAditya Swarup #define TMDS_371M 371250
2082c291417SAditya Swarup #define TMDS_370M 370878
2092c291417SAditya Swarup 
2102c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_30bpp[] = {
2112c291417SAditya Swarup 	{ 32000, TMDS_370M, 5824, 527344 },
2122c291417SAditya Swarup 	{ 32000, TMDS_371M, 6144, 556875 },
2132c291417SAditya Swarup 	{ 44100, TMDS_370M, 8918, 585938 },
2142c291417SAditya Swarup 	{ 44100, TMDS_371M, 4704, 309375 },
2152c291417SAditya Swarup 	{ 88200, TMDS_370M, 17836, 585938 },
2162c291417SAditya Swarup 	{ 88200, TMDS_371M, 9408, 309375 },
2172c291417SAditya Swarup 	{ 176400, TMDS_370M, 35672, 585938 },
2182c291417SAditya Swarup 	{ 176400, TMDS_371M, 18816, 309375 },
2192c291417SAditya Swarup 	{ 48000, TMDS_370M, 11648, 703125 },
2202c291417SAditya Swarup 	{ 48000, TMDS_371M, 5120, 309375 },
2212c291417SAditya Swarup 	{ 96000, TMDS_370M, 23296, 703125 },
2222c291417SAditya Swarup 	{ 96000, TMDS_371M, 10240, 309375 },
2232c291417SAditya Swarup 	{ 192000, TMDS_370M, 46592, 703125 },
2242c291417SAditya Swarup 	{ 192000, TMDS_371M, 20480, 309375 },
2252c291417SAditya Swarup };
2262c291417SAditya Swarup 
2272c291417SAditya Swarup /* HDMI N/CTS table for 12 bit deep color(36 bpp)*/
2282c291417SAditya Swarup #define TMDS_445_5M 445500
2292c291417SAditya Swarup #define TMDS_445M 445054
2302c291417SAditya Swarup 
2312c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_36bpp[] = {
2322c291417SAditya Swarup 	{ 32000, TMDS_445M, 5824, 632813 },
2332c291417SAditya Swarup 	{ 32000, TMDS_445_5M, 4096, 445500 },
2342c291417SAditya Swarup 	{ 44100, TMDS_445M, 8918, 703125 },
2352c291417SAditya Swarup 	{ 44100, TMDS_445_5M, 4704, 371250 },
2362c291417SAditya Swarup 	{ 88200, TMDS_445M, 17836, 703125 },
2372c291417SAditya Swarup 	{ 88200, TMDS_445_5M, 9408, 371250 },
2382c291417SAditya Swarup 	{ 176400, TMDS_445M, 35672, 703125 },
2392c291417SAditya Swarup 	{ 176400, TMDS_445_5M, 18816, 371250 },
2402c291417SAditya Swarup 	{ 48000, TMDS_445M, 5824, 421875 },
2412c291417SAditya Swarup 	{ 48000, TMDS_445_5M, 5120, 371250 },
2422c291417SAditya Swarup 	{ 96000, TMDS_445M, 11648, 421875 },
2432c291417SAditya Swarup 	{ 96000, TMDS_445_5M, 10240, 371250 },
2442c291417SAditya Swarup 	{ 192000, TMDS_445M, 23296, 421875 },
2452c291417SAditya Swarup 	{ 192000, TMDS_445_5M, 20480, 371250 },
2462c291417SAditya Swarup };
2472c291417SAditya Swarup 
248df0566a6SJani Nikula /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
249df0566a6SJani Nikula static u32 audio_config_hdmi_pixel_clock(const struct intel_crtc_state *crtc_state)
250df0566a6SJani Nikula {
2511aae3065SKai Vehmanen 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
252df0566a6SJani Nikula 	const struct drm_display_mode *adjusted_mode =
2531326a92cSMaarten Lankhorst 		&crtc_state->hw.adjusted_mode;
254df0566a6SJani Nikula 	int i;
255df0566a6SJani Nikula 
256df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
257df0566a6SJani Nikula 		if (adjusted_mode->crtc_clock == hdmi_audio_clock[i].clock)
258df0566a6SJani Nikula 			break;
259df0566a6SJani Nikula 	}
260df0566a6SJani Nikula 
261005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) < 12 && adjusted_mode->crtc_clock > 148500)
2621aae3065SKai Vehmanen 		i = ARRAY_SIZE(hdmi_audio_clock);
2631aae3065SKai Vehmanen 
264df0566a6SJani Nikula 	if (i == ARRAY_SIZE(hdmi_audio_clock)) {
2659282a66cSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
2669282a66cSJani Nikula 			    "HDMI audio pixel clock setting for %d not found, falling back to defaults\n",
267df0566a6SJani Nikula 			    adjusted_mode->crtc_clock);
268df0566a6SJani Nikula 		i = 1;
269df0566a6SJani Nikula 	}
270df0566a6SJani Nikula 
2719282a66cSJani Nikula 	drm_dbg_kms(&dev_priv->drm,
2729282a66cSJani Nikula 		    "Configuring HDMI audio for pixel clock %d (0x%08x)\n",
273df0566a6SJani Nikula 		    hdmi_audio_clock[i].clock,
274df0566a6SJani Nikula 		    hdmi_audio_clock[i].config);
275df0566a6SJani Nikula 
276df0566a6SJani Nikula 	return hdmi_audio_clock[i].config;
277df0566a6SJani Nikula }
278df0566a6SJani Nikula 
279df0566a6SJani Nikula static int audio_config_hdmi_get_n(const struct intel_crtc_state *crtc_state,
280df0566a6SJani Nikula 				   int rate)
281df0566a6SJani Nikula {
2822c291417SAditya Swarup 	const struct hdmi_aud_ncts *hdmi_ncts_table;
2832c291417SAditya Swarup 	int i, size;
284df0566a6SJani Nikula 
2852c291417SAditya Swarup 	if (crtc_state->pipe_bpp == 36) {
2862c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_36bpp;
2872c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_36bpp);
2882c291417SAditya Swarup 	} else if (crtc_state->pipe_bpp == 30) {
2892c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_30bpp;
2902c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_30bpp);
2912c291417SAditya Swarup 	} else {
2922c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_24bpp;
2932c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_24bpp);
2942c291417SAditya Swarup 	}
2952c291417SAditya Swarup 
2962c291417SAditya Swarup 	for (i = 0; i < size; i++) {
2972c291417SAditya Swarup 		if (rate == hdmi_ncts_table[i].sample_rate &&
2982c291417SAditya Swarup 		    crtc_state->port_clock == hdmi_ncts_table[i].clock) {
2992c291417SAditya Swarup 			return hdmi_ncts_table[i].n;
300df0566a6SJani Nikula 		}
301df0566a6SJani Nikula 	}
302df0566a6SJani Nikula 	return 0;
303df0566a6SJani Nikula }
304df0566a6SJani Nikula 
305df0566a6SJani Nikula static bool intel_eld_uptodate(struct drm_connector *connector,
306df0566a6SJani Nikula 			       i915_reg_t reg_eldv, u32 bits_eldv,
307df0566a6SJani Nikula 			       i915_reg_t reg_elda, u32 bits_elda,
308df0566a6SJani Nikula 			       i915_reg_t reg_edid)
309df0566a6SJani Nikula {
310df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
311df0566a6SJani Nikula 	const u8 *eld = connector->eld;
312df0566a6SJani Nikula 	u32 tmp;
313df0566a6SJani Nikula 	int i;
314df0566a6SJani Nikula 
31549e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, reg_eldv);
316df0566a6SJani Nikula 	tmp &= bits_eldv;
317df0566a6SJani Nikula 
318df0566a6SJani Nikula 	if (!tmp)
319df0566a6SJani Nikula 		return false;
320df0566a6SJani Nikula 
32149e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, reg_elda);
322df0566a6SJani Nikula 	tmp &= ~bits_elda;
32349e659bcSJani Nikula 	intel_de_write(dev_priv, reg_elda, tmp);
324df0566a6SJani Nikula 
325df0566a6SJani Nikula 	for (i = 0; i < drm_eld_size(eld) / 4; i++)
32649e659bcSJani Nikula 		if (intel_de_read(dev_priv, reg_edid) != *((const u32 *)eld + i))
327df0566a6SJani Nikula 			return false;
328df0566a6SJani Nikula 
329df0566a6SJani Nikula 	return true;
330df0566a6SJani Nikula }
331df0566a6SJani Nikula 
332df0566a6SJani Nikula static void g4x_audio_codec_disable(struct intel_encoder *encoder,
333df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
334df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
335df0566a6SJani Nikula {
336df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
337df0566a6SJani Nikula 	u32 eldv, tmp;
338df0566a6SJani Nikula 
33963855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "Disable audio codec\n");
340df0566a6SJani Nikula 
34149e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_VID_DID);
342df0566a6SJani Nikula 	if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
343df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCL_DEVBLC;
344df0566a6SJani Nikula 	else
345df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCTG;
346df0566a6SJani Nikula 
347df0566a6SJani Nikula 	/* Invalidate ELD */
34849e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_CNTL_ST);
349df0566a6SJani Nikula 	tmp &= ~eldv;
35049e659bcSJani Nikula 	intel_de_write(dev_priv, G4X_AUD_CNTL_ST, tmp);
351df0566a6SJani Nikula }
352df0566a6SJani Nikula 
353df0566a6SJani Nikula static void g4x_audio_codec_enable(struct intel_encoder *encoder,
354df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
355df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
356df0566a6SJani Nikula {
357df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
358df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
359df0566a6SJani Nikula 	const u8 *eld = connector->eld;
360df0566a6SJani Nikula 	u32 eldv;
361df0566a6SJani Nikula 	u32 tmp;
362df0566a6SJani Nikula 	int len, i;
363df0566a6SJani Nikula 
36463855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "Enable audio codec, %u bytes ELD\n",
36563855149SWambui Karuga 		    drm_eld_size(eld));
366df0566a6SJani Nikula 
36749e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_VID_DID);
368df0566a6SJani Nikula 	if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
369df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCL_DEVBLC;
370df0566a6SJani Nikula 	else
371df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCTG;
372df0566a6SJani Nikula 
373df0566a6SJani Nikula 	if (intel_eld_uptodate(connector,
374df0566a6SJani Nikula 			       G4X_AUD_CNTL_ST, eldv,
375df0566a6SJani Nikula 			       G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
376df0566a6SJani Nikula 			       G4X_HDMIW_HDMIEDID))
377df0566a6SJani Nikula 		return;
378df0566a6SJani Nikula 
37949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_CNTL_ST);
380df0566a6SJani Nikula 	tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
381df0566a6SJani Nikula 	len = (tmp >> 9) & 0x1f;		/* ELD buffer size */
38249e659bcSJani Nikula 	intel_de_write(dev_priv, G4X_AUD_CNTL_ST, tmp);
383df0566a6SJani Nikula 
384df0566a6SJani Nikula 	len = min(drm_eld_size(eld) / 4, len);
38563855149SWambui Karuga 	drm_dbg(&dev_priv->drm, "ELD size %d\n", len);
386df0566a6SJani Nikula 	for (i = 0; i < len; i++)
38749e659bcSJani Nikula 		intel_de_write(dev_priv, G4X_HDMIW_HDMIEDID,
38849e659bcSJani Nikula 			       *((const u32 *)eld + i));
389df0566a6SJani Nikula 
39049e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_CNTL_ST);
391df0566a6SJani Nikula 	tmp |= eldv;
39249e659bcSJani Nikula 	intel_de_write(dev_priv, G4X_AUD_CNTL_ST, tmp);
393df0566a6SJani Nikula }
394df0566a6SJani Nikula 
395df0566a6SJani Nikula static void
396df0566a6SJani Nikula hsw_dp_audio_config_update(struct intel_encoder *encoder,
397df0566a6SJani Nikula 			   const struct intel_crtc_state *crtc_state)
398df0566a6SJani Nikula {
399df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
400ca3cfb9dSJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio.component;
401df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
402df0566a6SJani Nikula 	enum port port = encoder->port;
403df0566a6SJani Nikula 	const struct dp_aud_n_m *nm;
404df0566a6SJani Nikula 	int rate;
405df0566a6SJani Nikula 	u32 tmp;
406df0566a6SJani Nikula 
407df0566a6SJani Nikula 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
408df0566a6SJani Nikula 	nm = audio_config_dp_get_n_m(crtc_state, rate);
409df0566a6SJani Nikula 	if (nm)
41063855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using Maud %u, Naud %u\n", nm->m,
41163855149SWambui Karuga 			    nm->n);
412df0566a6SJani Nikula 	else
41363855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using automatic Maud, Naud\n");
414df0566a6SJani Nikula 
41549e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_CFG(cpu_transcoder));
416df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
417df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
418df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
419df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_VALUE_INDEX;
420df0566a6SJani Nikula 
421df0566a6SJani Nikula 	if (nm) {
422df0566a6SJani Nikula 		tmp &= ~AUD_CONFIG_N_MASK;
423df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N(nm->n);
424df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
425df0566a6SJani Nikula 	}
426df0566a6SJani Nikula 
42749e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_CFG(cpu_transcoder), tmp);
428df0566a6SJani Nikula 
42949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
430df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_M_MASK;
431df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
432df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
433df0566a6SJani Nikula 
434df0566a6SJani Nikula 	if (nm) {
435df0566a6SJani Nikula 		tmp |= nm->m;
436df0566a6SJani Nikula 		tmp |= AUD_M_CTS_M_VALUE_INDEX;
437df0566a6SJani Nikula 		tmp |= AUD_M_CTS_M_PROG_ENABLE;
438df0566a6SJani Nikula 	}
439df0566a6SJani Nikula 
44049e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
441df0566a6SJani Nikula }
442df0566a6SJani Nikula 
443df0566a6SJani Nikula static void
444df0566a6SJani Nikula hsw_hdmi_audio_config_update(struct intel_encoder *encoder,
445df0566a6SJani Nikula 			     const struct intel_crtc_state *crtc_state)
446df0566a6SJani Nikula {
447df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
448ca3cfb9dSJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio.component;
449df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
450df0566a6SJani Nikula 	enum port port = encoder->port;
451df0566a6SJani Nikula 	int n, rate;
452df0566a6SJani Nikula 	u32 tmp;
453df0566a6SJani Nikula 
454df0566a6SJani Nikula 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
455df0566a6SJani Nikula 
45649e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_CFG(cpu_transcoder));
457df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
458df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
459df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
460df0566a6SJani Nikula 	tmp |= audio_config_hdmi_pixel_clock(crtc_state);
461df0566a6SJani Nikula 
462df0566a6SJani Nikula 	n = audio_config_hdmi_get_n(crtc_state, rate);
463df0566a6SJani Nikula 	if (n != 0) {
46463855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using N %d\n", n);
465df0566a6SJani Nikula 
466df0566a6SJani Nikula 		tmp &= ~AUD_CONFIG_N_MASK;
467df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N(n);
468df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
469df0566a6SJani Nikula 	} else {
47063855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using automatic N\n");
471df0566a6SJani Nikula 	}
472df0566a6SJani Nikula 
47349e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_CFG(cpu_transcoder), tmp);
474df0566a6SJani Nikula 
475df0566a6SJani Nikula 	/*
476df0566a6SJani Nikula 	 * Let's disable "Enable CTS or M Prog bit"
477df0566a6SJani Nikula 	 * and let HW calculate the value
478df0566a6SJani Nikula 	 */
47949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
480df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
481df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
48249e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
483df0566a6SJani Nikula }
484df0566a6SJani Nikula 
485df0566a6SJani Nikula static void
486df0566a6SJani Nikula hsw_audio_config_update(struct intel_encoder *encoder,
487df0566a6SJani Nikula 			const struct intel_crtc_state *crtc_state)
488df0566a6SJani Nikula {
489df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state))
490df0566a6SJani Nikula 		hsw_dp_audio_config_update(encoder, crtc_state);
491df0566a6SJani Nikula 	else
492df0566a6SJani Nikula 		hsw_hdmi_audio_config_update(encoder, crtc_state);
493df0566a6SJani Nikula }
494df0566a6SJani Nikula 
495df0566a6SJani Nikula static void hsw_audio_codec_disable(struct intel_encoder *encoder,
496df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
497df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
498df0566a6SJani Nikula {
499df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
500df0566a6SJani Nikula 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
501df0566a6SJani Nikula 	u32 tmp;
502df0566a6SJani Nikula 
50363855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "Disable audio codec on transcoder %s\n",
504df0566a6SJani Nikula 		    transcoder_name(cpu_transcoder));
505df0566a6SJani Nikula 
506ca3cfb9dSJani Nikula 	mutex_lock(&dev_priv->audio.mutex);
507df0566a6SJani Nikula 
508df0566a6SJani Nikula 	/* Disable timestamps */
50949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_CFG(cpu_transcoder));
510df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
511df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_PROG_ENABLE;
512df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_UPPER_N_MASK;
513df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_LOWER_N_MASK;
514df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(old_crtc_state))
515df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
51649e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_CFG(cpu_transcoder), tmp);
517df0566a6SJani Nikula 
518df0566a6SJani Nikula 	/* Invalidate ELD */
51949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD);
520df0566a6SJani Nikula 	tmp &= ~AUDIO_ELD_VALID(cpu_transcoder);
521df0566a6SJani Nikula 	tmp &= ~AUDIO_OUTPUT_ENABLE(cpu_transcoder);
52249e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_PIN_ELD_CP_VLD, tmp);
523df0566a6SJani Nikula 
524ca3cfb9dSJani Nikula 	mutex_unlock(&dev_priv->audio.mutex);
525df0566a6SJani Nikula }
526df0566a6SJani Nikula 
5272dd43144SVille Syrjälä static unsigned int calc_hblank_early_prog(struct intel_encoder *encoder,
52848b8b04cSUma Shankar 					   const struct intel_crtc_state *crtc_state)
52948b8b04cSUma Shankar {
53048b8b04cSUma Shankar 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
53148b8b04cSUma Shankar 	unsigned int link_clks_available, link_clks_required;
53248b8b04cSUma Shankar 	unsigned int tu_data, tu_line, link_clks_active;
533d19b29beSVille Syrjälä 	unsigned int h_active, h_total, hblank_delta, pixel_clk;
534d19b29beSVille Syrjälä 	unsigned int fec_coeff, cdclk, vdsc_bpp;
53541ee86d6SVille Syrjälä 	unsigned int link_clk, lanes;
5362dd43144SVille Syrjälä 	unsigned int hblank_rise;
53748b8b04cSUma Shankar 
53848b8b04cSUma Shankar 	h_active = crtc_state->hw.adjusted_mode.crtc_hdisplay;
53948b8b04cSUma Shankar 	h_total = crtc_state->hw.adjusted_mode.crtc_htotal;
54048b8b04cSUma Shankar 	pixel_clk = crtc_state->hw.adjusted_mode.crtc_clock;
54148b8b04cSUma Shankar 	vdsc_bpp = crtc_state->dsc.compressed_bpp;
54248b8b04cSUma Shankar 	cdclk = i915->cdclk.hw.cdclk;
54348b8b04cSUma Shankar 	/* fec= 0.972261, using rounding multiplier of 1000000 */
54448b8b04cSUma Shankar 	fec_coeff = 972261;
54541ee86d6SVille Syrjälä 	link_clk = crtc_state->port_clock;
54641ee86d6SVille Syrjälä 	lanes = crtc_state->lane_count;
54748b8b04cSUma Shankar 
54848b8b04cSUma Shankar 	drm_dbg_kms(&i915->drm, "h_active = %u link_clk = %u :"
54948b8b04cSUma Shankar 		    "lanes = %u vdsc_bpp = %u cdclk = %u\n",
55041ee86d6SVille Syrjälä 		    h_active, link_clk, lanes, vdsc_bpp, cdclk);
55148b8b04cSUma Shankar 
5522dd43144SVille Syrjälä 	if (WARN_ON(!link_clk || !pixel_clk || !lanes || !vdsc_bpp || !cdclk))
55311ebc232SJani Nikula 		return 0;
55411ebc232SJani Nikula 
5552dd43144SVille Syrjälä 	link_clks_available = (h_total - h_active) * link_clk / pixel_clk - 28;
5562dd43144SVille Syrjälä 	link_clks_required = DIV_ROUND_UP(192000 * h_total, 1000 * pixel_clk) * (48 / lanes + 2);
55748b8b04cSUma Shankar 
55848b8b04cSUma Shankar 	if (link_clks_available > link_clks_required)
55948b8b04cSUma Shankar 		hblank_delta = 32;
56048b8b04cSUma Shankar 	else
5612dd43144SVille Syrjälä 		hblank_delta = DIV64_U64_ROUND_UP(mul_u32_u32(5 * (link_clk + cdclk), pixel_clk),
5622dd43144SVille Syrjälä 						  mul_u32_u32(link_clk, cdclk));
56348b8b04cSUma Shankar 
5642dd43144SVille Syrjälä 	tu_data = div64_u64(mul_u32_u32(pixel_clk * vdsc_bpp * 8, 1000000),
5652dd43144SVille Syrjälä 			    mul_u32_u32(link_clk * lanes, fec_coeff));
5662dd43144SVille Syrjälä 	tu_line = div64_u64(h_active * mul_u32_u32(link_clk, fec_coeff),
5672dd43144SVille Syrjälä 			    mul_u32_u32(64 * pixel_clk, 1000000));
56848b8b04cSUma Shankar 	link_clks_active  = (tu_line - 1) * 64 + tu_data;
56948b8b04cSUma Shankar 
5702dd43144SVille Syrjälä 	hblank_rise = (link_clks_active + 6 * DIV_ROUND_UP(link_clks_active, 250) + 4) * pixel_clk / link_clk;
57148b8b04cSUma Shankar 
5722dd43144SVille Syrjälä 	return h_active - hblank_rise + hblank_delta;
57348b8b04cSUma Shankar }
57448b8b04cSUma Shankar 
5752dd43144SVille Syrjälä static unsigned int calc_samples_room(const struct intel_crtc_state *crtc_state)
57648b8b04cSUma Shankar {
57748b8b04cSUma Shankar 	unsigned int h_active, h_total, pixel_clk;
57841ee86d6SVille Syrjälä 	unsigned int link_clk, lanes;
57948b8b04cSUma Shankar 
58048b8b04cSUma Shankar 	h_active = crtc_state->hw.adjusted_mode.hdisplay;
58148b8b04cSUma Shankar 	h_total = crtc_state->hw.adjusted_mode.htotal;
58248b8b04cSUma Shankar 	pixel_clk = crtc_state->hw.adjusted_mode.clock;
58341ee86d6SVille Syrjälä 	link_clk = crtc_state->port_clock;
58441ee86d6SVille Syrjälä 	lanes = crtc_state->lane_count;
58548b8b04cSUma Shankar 
5862dd43144SVille Syrjälä 	return ((h_total - h_active) * link_clk - 12 * pixel_clk) /
5872dd43144SVille Syrjälä 		(pixel_clk * (48 / lanes + 2));
58848b8b04cSUma Shankar }
58948b8b04cSUma Shankar 
59048b8b04cSUma Shankar static void enable_audio_dsc_wa(struct intel_encoder *encoder,
59148b8b04cSUma Shankar 				const struct intel_crtc_state *crtc_state)
59248b8b04cSUma Shankar {
59348b8b04cSUma Shankar 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
59448b8b04cSUma Shankar 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
59548b8b04cSUma Shankar 	enum pipe pipe = crtc->pipe;
59611ebc232SJani Nikula 	unsigned int hblank_early_prog, samples_room;
59748b8b04cSUma Shankar 	unsigned int val;
59848b8b04cSUma Shankar 
599005e9537SMatt Roper 	if (DISPLAY_VER(i915) < 11)
60048b8b04cSUma Shankar 		return;
60148b8b04cSUma Shankar 
60248b8b04cSUma Shankar 	val = intel_de_read(i915, AUD_CONFIG_BE);
60348b8b04cSUma Shankar 
60493e7e61eSLucas De Marchi 	if (DISPLAY_VER(i915) == 11)
60548b8b04cSUma Shankar 		val |= HBLANK_EARLY_ENABLE_ICL(pipe);
606005e9537SMatt Roper 	else if (DISPLAY_VER(i915) >= 12)
60748b8b04cSUma Shankar 		val |= HBLANK_EARLY_ENABLE_TGL(pipe);
60848b8b04cSUma Shankar 
60948b8b04cSUma Shankar 	if (crtc_state->dsc.compression_enable &&
61031824c03SJani Nikula 	    crtc_state->hw.adjusted_mode.hdisplay >= 3840 &&
61131824c03SJani Nikula 	    crtc_state->hw.adjusted_mode.vdisplay >= 2160) {
61248b8b04cSUma Shankar 		/* Get hblank early enable value required */
613f4c50deeSJani Nikula 		val &= ~HBLANK_START_COUNT_MASK(pipe);
6142dd43144SVille Syrjälä 		hblank_early_prog = calc_hblank_early_prog(encoder, crtc_state);
615f4c50deeSJani Nikula 		if (hblank_early_prog < 32)
61648b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_32);
617f4c50deeSJani Nikula 		else if (hblank_early_prog < 64)
61848b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_64);
619f4c50deeSJani Nikula 		else if (hblank_early_prog < 96)
62048b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_96);
621f4c50deeSJani Nikula 		else
62248b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_128);
62348b8b04cSUma Shankar 
62448b8b04cSUma Shankar 		/* Get samples room value required */
625f4c50deeSJani Nikula 		val &= ~NUMBER_SAMPLES_PER_LINE_MASK(pipe);
6262dd43144SVille Syrjälä 		samples_room = calc_samples_room(crtc_state);
627f4c50deeSJani Nikula 		if (samples_room < 3)
62848b8b04cSUma Shankar 			val |= NUMBER_SAMPLES_PER_LINE(pipe, samples_room);
629f4c50deeSJani Nikula 		else /* Program 0 i.e "All Samples available in buffer" */
63048b8b04cSUma Shankar 			val |= NUMBER_SAMPLES_PER_LINE(pipe, 0x0);
63148b8b04cSUma Shankar 	}
63248b8b04cSUma Shankar 
63348b8b04cSUma Shankar 	intel_de_write(i915, AUD_CONFIG_BE, val);
63448b8b04cSUma Shankar }
63548b8b04cSUma Shankar 
63648b8b04cSUma Shankar #undef ROUNDING_FACTOR
63748b8b04cSUma Shankar 
638df0566a6SJani Nikula static void hsw_audio_codec_enable(struct intel_encoder *encoder,
639df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
640df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
641df0566a6SJani Nikula {
642df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
643df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
644df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
645df0566a6SJani Nikula 	const u8 *eld = connector->eld;
646df0566a6SJani Nikula 	u32 tmp;
647df0566a6SJani Nikula 	int len, i;
648df0566a6SJani Nikula 
64963855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
65063855149SWambui Karuga 		    "Enable audio codec on transcoder %s, %u bytes ELD\n",
651df0566a6SJani Nikula 		     transcoder_name(cpu_transcoder), drm_eld_size(eld));
652df0566a6SJani Nikula 
653ca3cfb9dSJani Nikula 	mutex_lock(&dev_priv->audio.mutex);
654df0566a6SJani Nikula 
65548b8b04cSUma Shankar 	/* Enable Audio WA for 4k DSC usecases */
65648b8b04cSUma Shankar 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP))
65748b8b04cSUma Shankar 		enable_audio_dsc_wa(encoder, crtc_state);
65848b8b04cSUma Shankar 
659df0566a6SJani Nikula 	/* Enable audio presence detect, invalidate ELD */
66049e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD);
661df0566a6SJani Nikula 	tmp |= AUDIO_OUTPUT_ENABLE(cpu_transcoder);
662df0566a6SJani Nikula 	tmp &= ~AUDIO_ELD_VALID(cpu_transcoder);
66349e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_PIN_ELD_CP_VLD, tmp);
664df0566a6SJani Nikula 
665df0566a6SJani Nikula 	/*
666df0566a6SJani Nikula 	 * FIXME: We're supposed to wait for vblank here, but we have vblanks
667df0566a6SJani Nikula 	 * disabled during the mode set. The proper fix would be to push the
668df0566a6SJani Nikula 	 * rest of the setup into a vblank work item, queued here, but the
669df0566a6SJani Nikula 	 * infrastructure is not there yet.
670df0566a6SJani Nikula 	 */
671df0566a6SJani Nikula 
672df0566a6SJani Nikula 	/* Reset ELD write address */
67349e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_DIP_ELD_CTRL(cpu_transcoder));
674df0566a6SJani Nikula 	tmp &= ~IBX_ELD_ADDRESS_MASK;
67549e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_DIP_ELD_CTRL(cpu_transcoder), tmp);
676df0566a6SJani Nikula 
677df0566a6SJani Nikula 	/* Up to 84 bytes of hw ELD buffer */
678df0566a6SJani Nikula 	len = min(drm_eld_size(eld), 84);
679df0566a6SJani Nikula 	for (i = 0; i < len / 4; i++)
68049e659bcSJani Nikula 		intel_de_write(dev_priv, HSW_AUD_EDID_DATA(cpu_transcoder),
68149e659bcSJani Nikula 			       *((const u32 *)eld + i));
682df0566a6SJani Nikula 
683df0566a6SJani Nikula 	/* ELD valid */
68449e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD);
685df0566a6SJani Nikula 	tmp |= AUDIO_ELD_VALID(cpu_transcoder);
68649e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_PIN_ELD_CP_VLD, tmp);
687df0566a6SJani Nikula 
688df0566a6SJani Nikula 	/* Enable timestamps */
689df0566a6SJani Nikula 	hsw_audio_config_update(encoder, crtc_state);
690df0566a6SJani Nikula 
691ca3cfb9dSJani Nikula 	mutex_unlock(&dev_priv->audio.mutex);
692df0566a6SJani Nikula }
693df0566a6SJani Nikula 
694df0566a6SJani Nikula static void ilk_audio_codec_disable(struct intel_encoder *encoder,
695df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
696df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
697df0566a6SJani Nikula {
698df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
6992225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
700df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
701df0566a6SJani Nikula 	enum port port = encoder->port;
702df0566a6SJani Nikula 	u32 tmp, eldv;
703df0566a6SJani Nikula 	i915_reg_t aud_config, aud_cntrl_st2;
704df0566a6SJani Nikula 
70563855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
70663855149SWambui Karuga 		    "Disable audio codec on [ENCODER:%d:%s], pipe %c\n",
70766a990ddSVille Syrjälä 		     encoder->base.base.id, encoder->base.name,
70866a990ddSVille Syrjälä 		     pipe_name(pipe));
709df0566a6SJani Nikula 
7109a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, port == PORT_A))
711df0566a6SJani Nikula 		return;
712df0566a6SJani Nikula 
713df0566a6SJani Nikula 	if (HAS_PCH_IBX(dev_priv)) {
714df0566a6SJani Nikula 		aud_config = IBX_AUD_CFG(pipe);
715df0566a6SJani Nikula 		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
716df0566a6SJani Nikula 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
717df0566a6SJani Nikula 		aud_config = VLV_AUD_CFG(pipe);
718df0566a6SJani Nikula 		aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
719df0566a6SJani Nikula 	} else {
720df0566a6SJani Nikula 		aud_config = CPT_AUD_CFG(pipe);
721df0566a6SJani Nikula 		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
722df0566a6SJani Nikula 	}
723df0566a6SJani Nikula 
724df0566a6SJani Nikula 	/* Disable timestamps */
72549e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_config);
726df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
727df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_PROG_ENABLE;
728df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_UPPER_N_MASK;
729df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_LOWER_N_MASK;
730df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(old_crtc_state))
731df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
73249e659bcSJani Nikula 	intel_de_write(dev_priv, aud_config, tmp);
733df0566a6SJani Nikula 
734df0566a6SJani Nikula 	eldv = IBX_ELD_VALID(port);
735df0566a6SJani Nikula 
736df0566a6SJani Nikula 	/* Invalidate ELD */
73749e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_cntrl_st2);
738df0566a6SJani Nikula 	tmp &= ~eldv;
73949e659bcSJani Nikula 	intel_de_write(dev_priv, aud_cntrl_st2, tmp);
740df0566a6SJani Nikula }
741df0566a6SJani Nikula 
742df0566a6SJani Nikula static void ilk_audio_codec_enable(struct intel_encoder *encoder,
743df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
744df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
745df0566a6SJani Nikula {
746df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
7472225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
748df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
749df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
750df0566a6SJani Nikula 	enum port port = encoder->port;
751df0566a6SJani Nikula 	const u8 *eld = connector->eld;
752df0566a6SJani Nikula 	u32 tmp, eldv;
753df0566a6SJani Nikula 	int len, i;
754df0566a6SJani Nikula 	i915_reg_t hdmiw_hdmiedid, aud_config, aud_cntl_st, aud_cntrl_st2;
755df0566a6SJani Nikula 
75663855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
75763855149SWambui Karuga 		    "Enable audio codec on [ENCODER:%d:%s], pipe %c, %u bytes ELD\n",
75866a990ddSVille Syrjälä 		    encoder->base.base.id, encoder->base.name,
75966a990ddSVille Syrjälä 		    pipe_name(pipe), drm_eld_size(eld));
760df0566a6SJani Nikula 
7619a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, port == PORT_A))
762df0566a6SJani Nikula 		return;
763df0566a6SJani Nikula 
764df0566a6SJani Nikula 	/*
765df0566a6SJani Nikula 	 * FIXME: We're supposed to wait for vblank here, but we have vblanks
766df0566a6SJani Nikula 	 * disabled during the mode set. The proper fix would be to push the
767df0566a6SJani Nikula 	 * rest of the setup into a vblank work item, queued here, but the
768df0566a6SJani Nikula 	 * infrastructure is not there yet.
769df0566a6SJani Nikula 	 */
770df0566a6SJani Nikula 
771df0566a6SJani Nikula 	if (HAS_PCH_IBX(dev_priv)) {
772df0566a6SJani Nikula 		hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
773df0566a6SJani Nikula 		aud_config = IBX_AUD_CFG(pipe);
774df0566a6SJani Nikula 		aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
775df0566a6SJani Nikula 		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
776df0566a6SJani Nikula 	} else if (IS_VALLEYVIEW(dev_priv) ||
777df0566a6SJani Nikula 		   IS_CHERRYVIEW(dev_priv)) {
778df0566a6SJani Nikula 		hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
779df0566a6SJani Nikula 		aud_config = VLV_AUD_CFG(pipe);
780df0566a6SJani Nikula 		aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
781df0566a6SJani Nikula 		aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
782df0566a6SJani Nikula 	} else {
783df0566a6SJani Nikula 		hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
784df0566a6SJani Nikula 		aud_config = CPT_AUD_CFG(pipe);
785df0566a6SJani Nikula 		aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
786df0566a6SJani Nikula 		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
787df0566a6SJani Nikula 	}
788df0566a6SJani Nikula 
789df0566a6SJani Nikula 	eldv = IBX_ELD_VALID(port);
790df0566a6SJani Nikula 
791df0566a6SJani Nikula 	/* Invalidate ELD */
79249e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_cntrl_st2);
793df0566a6SJani Nikula 	tmp &= ~eldv;
79449e659bcSJani Nikula 	intel_de_write(dev_priv, aud_cntrl_st2, tmp);
795df0566a6SJani Nikula 
796df0566a6SJani Nikula 	/* Reset ELD write address */
79749e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_cntl_st);
798df0566a6SJani Nikula 	tmp &= ~IBX_ELD_ADDRESS_MASK;
79949e659bcSJani Nikula 	intel_de_write(dev_priv, aud_cntl_st, tmp);
800df0566a6SJani Nikula 
801df0566a6SJani Nikula 	/* Up to 84 bytes of hw ELD buffer */
802df0566a6SJani Nikula 	len = min(drm_eld_size(eld), 84);
803df0566a6SJani Nikula 	for (i = 0; i < len / 4; i++)
80449e659bcSJani Nikula 		intel_de_write(dev_priv, hdmiw_hdmiedid,
80549e659bcSJani Nikula 			       *((const u32 *)eld + i));
806df0566a6SJani Nikula 
807df0566a6SJani Nikula 	/* ELD valid */
80849e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_cntrl_st2);
809df0566a6SJani Nikula 	tmp |= eldv;
81049e659bcSJani Nikula 	intel_de_write(dev_priv, aud_cntrl_st2, tmp);
811df0566a6SJani Nikula 
812df0566a6SJani Nikula 	/* Enable timestamps */
81349e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_config);
814df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
815df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
816df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
817df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state))
818df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
819df0566a6SJani Nikula 	else
820df0566a6SJani Nikula 		tmp |= audio_config_hdmi_pixel_clock(crtc_state);
82149e659bcSJani Nikula 	intel_de_write(dev_priv, aud_config, tmp);
822df0566a6SJani Nikula }
823df0566a6SJani Nikula 
824df0566a6SJani Nikula /**
825df0566a6SJani Nikula  * intel_audio_codec_enable - Enable the audio codec for HD audio
826df0566a6SJani Nikula  * @encoder: encoder on which to enable audio
827df0566a6SJani Nikula  * @crtc_state: pointer to the current crtc state.
828df0566a6SJani Nikula  * @conn_state: pointer to the current connector state.
829df0566a6SJani Nikula  *
830df0566a6SJani Nikula  * The enable sequences may only be performed after enabling the transcoder and
831df0566a6SJani Nikula  * port, and after completed link training.
832df0566a6SJani Nikula  */
833df0566a6SJani Nikula void intel_audio_codec_enable(struct intel_encoder *encoder,
834df0566a6SJani Nikula 			      const struct intel_crtc_state *crtc_state,
835df0566a6SJani Nikula 			      const struct drm_connector_state *conn_state)
836df0566a6SJani Nikula {
837df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
838ca3cfb9dSJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio.component;
8392225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
840df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
841df0566a6SJani Nikula 	const struct drm_display_mode *adjusted_mode =
8421326a92cSMaarten Lankhorst 		&crtc_state->hw.adjusted_mode;
843df0566a6SJani Nikula 	enum port port = encoder->port;
844df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
845df0566a6SJani Nikula 
846df0566a6SJani Nikula 	/* FIXME precompute the ELD in .compute_config() */
847df0566a6SJani Nikula 	if (!connector->eld[0])
84863855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
84963855149SWambui Karuga 			    "Bogus ELD on [CONNECTOR:%d:%s]\n",
850df0566a6SJani Nikula 			    connector->base.id, connector->name);
851df0566a6SJani Nikula 
85263855149SWambui Karuga 	drm_dbg(&dev_priv->drm, "ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
853df0566a6SJani Nikula 		connector->base.id,
854df0566a6SJani Nikula 		connector->name,
85579003e61SVille Syrjälä 		encoder->base.base.id,
85679003e61SVille Syrjälä 		encoder->base.name);
857df0566a6SJani Nikula 
858df0566a6SJani Nikula 	connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
859df0566a6SJani Nikula 
860ca3cfb9dSJani Nikula 	if (dev_priv->audio.funcs)
861ca3cfb9dSJani Nikula 		dev_priv->audio.funcs->audio_codec_enable(encoder,
862df0566a6SJani Nikula 							  crtc_state,
863df0566a6SJani Nikula 							  conn_state);
864df0566a6SJani Nikula 
865ca3cfb9dSJani Nikula 	mutex_lock(&dev_priv->audio.mutex);
866df0566a6SJani Nikula 	encoder->audio_connector = connector;
867df0566a6SJani Nikula 
868df0566a6SJani Nikula 	/* referred in audio callbacks */
869ca3cfb9dSJani Nikula 	dev_priv->audio.encoder_map[pipe] = encoder;
870ca3cfb9dSJani Nikula 	mutex_unlock(&dev_priv->audio.mutex);
871df0566a6SJani Nikula 
872df0566a6SJani Nikula 	if (acomp && acomp->base.audio_ops &&
873df0566a6SJani Nikula 	    acomp->base.audio_ops->pin_eld_notify) {
874df0566a6SJani Nikula 		/* audio drivers expect pipe = -1 to indicate Non-MST cases */
875df0566a6SJani Nikula 		if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
876df0566a6SJani Nikula 			pipe = -1;
877df0566a6SJani Nikula 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
878df0566a6SJani Nikula 						 (int) port, (int) pipe);
879df0566a6SJani Nikula 	}
880df0566a6SJani Nikula 
881df0566a6SJani Nikula 	intel_lpe_audio_notify(dev_priv, pipe, port, connector->eld,
882df0566a6SJani Nikula 			       crtc_state->port_clock,
883df0566a6SJani Nikula 			       intel_crtc_has_dp_encoder(crtc_state));
884df0566a6SJani Nikula }
885df0566a6SJani Nikula 
886df0566a6SJani Nikula /**
887df0566a6SJani Nikula  * intel_audio_codec_disable - Disable the audio codec for HD audio
888df0566a6SJani Nikula  * @encoder: encoder on which to disable audio
889df0566a6SJani Nikula  * @old_crtc_state: pointer to the old crtc state.
890df0566a6SJani Nikula  * @old_conn_state: pointer to the old connector state.
891df0566a6SJani Nikula  *
892df0566a6SJani Nikula  * The disable sequences must be performed before disabling the transcoder or
893df0566a6SJani Nikula  * port.
894df0566a6SJani Nikula  */
895df0566a6SJani Nikula void intel_audio_codec_disable(struct intel_encoder *encoder,
896df0566a6SJani Nikula 			       const struct intel_crtc_state *old_crtc_state,
897df0566a6SJani Nikula 			       const struct drm_connector_state *old_conn_state)
898df0566a6SJani Nikula {
899df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
900ca3cfb9dSJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio.component;
9012225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
902df0566a6SJani Nikula 	enum port port = encoder->port;
903df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
904df0566a6SJani Nikula 
905ca3cfb9dSJani Nikula 	if (dev_priv->audio.funcs)
906ca3cfb9dSJani Nikula 		dev_priv->audio.funcs->audio_codec_disable(encoder,
907df0566a6SJani Nikula 							   old_crtc_state,
908df0566a6SJani Nikula 							   old_conn_state);
909df0566a6SJani Nikula 
910ca3cfb9dSJani Nikula 	mutex_lock(&dev_priv->audio.mutex);
911df0566a6SJani Nikula 	encoder->audio_connector = NULL;
912ca3cfb9dSJani Nikula 	dev_priv->audio.encoder_map[pipe] = NULL;
913ca3cfb9dSJani Nikula 	mutex_unlock(&dev_priv->audio.mutex);
914df0566a6SJani Nikula 
915df0566a6SJani Nikula 	if (acomp && acomp->base.audio_ops &&
916df0566a6SJani Nikula 	    acomp->base.audio_ops->pin_eld_notify) {
917df0566a6SJani Nikula 		/* audio drivers expect pipe = -1 to indicate Non-MST cases */
918df0566a6SJani Nikula 		if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
919df0566a6SJani Nikula 			pipe = -1;
920df0566a6SJani Nikula 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
921df0566a6SJani Nikula 						 (int) port, (int) pipe);
922df0566a6SJani Nikula 	}
923df0566a6SJani Nikula 
924df0566a6SJani Nikula 	intel_lpe_audio_notify(dev_priv, pipe, port, NULL, 0, false);
925df0566a6SJani Nikula }
926df0566a6SJani Nikula 
9270a108bcaSDave Airlie static const struct intel_audio_funcs g4x_audio_funcs = {
9280a108bcaSDave Airlie 	.audio_codec_enable = g4x_audio_codec_enable,
9290a108bcaSDave Airlie 	.audio_codec_disable = g4x_audio_codec_disable,
9300a108bcaSDave Airlie };
9310a108bcaSDave Airlie 
9320a108bcaSDave Airlie static const struct intel_audio_funcs ilk_audio_funcs = {
9330a108bcaSDave Airlie 	.audio_codec_enable = ilk_audio_codec_enable,
9340a108bcaSDave Airlie 	.audio_codec_disable = ilk_audio_codec_disable,
9350a108bcaSDave Airlie };
9360a108bcaSDave Airlie 
9370a108bcaSDave Airlie static const struct intel_audio_funcs hsw_audio_funcs = {
9380a108bcaSDave Airlie 	.audio_codec_enable = hsw_audio_codec_enable,
9390a108bcaSDave Airlie 	.audio_codec_disable = hsw_audio_codec_disable,
9400a108bcaSDave Airlie };
9410a108bcaSDave Airlie 
942df0566a6SJani Nikula /**
943*f47a0e35SJani Nikula  * intel_audio_hooks_init - Set up chip specific audio hooks
944df0566a6SJani Nikula  * @dev_priv: device private
945df0566a6SJani Nikula  */
946*f47a0e35SJani Nikula void intel_audio_hooks_init(struct drm_i915_private *dev_priv)
947df0566a6SJani Nikula {
948df0566a6SJani Nikula 	if (IS_G4X(dev_priv)) {
949ca3cfb9dSJani Nikula 		dev_priv->audio.funcs = &g4x_audio_funcs;
950df0566a6SJani Nikula 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
951ca3cfb9dSJani Nikula 		dev_priv->audio.funcs = &ilk_audio_funcs;
952005e9537SMatt Roper 	} else if (IS_HASWELL(dev_priv) || DISPLAY_VER(dev_priv) >= 8) {
953ca3cfb9dSJani Nikula 		dev_priv->audio.funcs = &hsw_audio_funcs;
954df0566a6SJani Nikula 	} else if (HAS_PCH_SPLIT(dev_priv)) {
955ca3cfb9dSJani Nikula 		dev_priv->audio.funcs = &ilk_audio_funcs;
956df0566a6SJani Nikula 	}
957df0566a6SJani Nikula }
958df0566a6SJani Nikula 
959112a87c4SKai Vehmanen struct aud_ts_cdclk_m_n {
960112a87c4SKai Vehmanen 	u8 m;
961112a87c4SKai Vehmanen 	u16 n;
962112a87c4SKai Vehmanen };
963112a87c4SKai Vehmanen 
964112a87c4SKai Vehmanen void intel_audio_cdclk_change_pre(struct drm_i915_private *i915)
965112a87c4SKai Vehmanen {
966112a87c4SKai Vehmanen 	if (DISPLAY_VER(i915) >= 13)
967112a87c4SKai Vehmanen 		intel_de_rmw(i915, AUD_TS_CDCLK_M, AUD_TS_CDCLK_M_EN, 0);
968112a87c4SKai Vehmanen }
969112a87c4SKai Vehmanen 
970112a87c4SKai Vehmanen static void get_aud_ts_cdclk_m_n(int refclk, int cdclk, struct aud_ts_cdclk_m_n *aud_ts)
971112a87c4SKai Vehmanen {
972112a87c4SKai Vehmanen 	if (refclk == 24000)
973112a87c4SKai Vehmanen 		aud_ts->m = 12;
974112a87c4SKai Vehmanen 	else
975112a87c4SKai Vehmanen 		aud_ts->m = 15;
976112a87c4SKai Vehmanen 
977112a87c4SKai Vehmanen 	aud_ts->n = cdclk * aud_ts->m / 24000;
978112a87c4SKai Vehmanen }
979112a87c4SKai Vehmanen 
980112a87c4SKai Vehmanen void intel_audio_cdclk_change_post(struct drm_i915_private *i915)
981112a87c4SKai Vehmanen {
982112a87c4SKai Vehmanen 	struct aud_ts_cdclk_m_n aud_ts;
983112a87c4SKai Vehmanen 
984112a87c4SKai Vehmanen 	if (DISPLAY_VER(i915) >= 13) {
985112a87c4SKai Vehmanen 		get_aud_ts_cdclk_m_n(i915->cdclk.hw.ref, i915->cdclk.hw.cdclk, &aud_ts);
986112a87c4SKai Vehmanen 
987112a87c4SKai Vehmanen 		intel_de_write(i915, AUD_TS_CDCLK_N, aud_ts.n);
988112a87c4SKai Vehmanen 		intel_de_write(i915, AUD_TS_CDCLK_M, aud_ts.m | AUD_TS_CDCLK_M_EN);
989112a87c4SKai Vehmanen 		drm_dbg_kms(&i915->drm, "aud_ts_cdclk set to M=%u, N=%u\n", aud_ts.m, aud_ts.n);
990112a87c4SKai Vehmanen 	}
991112a87c4SKai Vehmanen }
992112a87c4SKai Vehmanen 
99328a30b45SVille Syrjälä static int glk_force_audio_cdclk_commit(struct intel_atomic_state *state,
99408e3ed3aSChris Wilson 					struct intel_crtc *crtc,
99528a30b45SVille Syrjälä 					bool enable)
99628a30b45SVille Syrjälä {
99728a30b45SVille Syrjälä 	struct intel_cdclk_state *cdclk_state;
99828a30b45SVille Syrjälä 	int ret;
99928a30b45SVille Syrjälä 
100028a30b45SVille Syrjälä 	/* need to hold at least one crtc lock for the global state */
100128a30b45SVille Syrjälä 	ret = drm_modeset_lock(&crtc->base.mutex, state->base.acquire_ctx);
100228a30b45SVille Syrjälä 	if (ret)
100328a30b45SVille Syrjälä 		return ret;
100428a30b45SVille Syrjälä 
100528a30b45SVille Syrjälä 	cdclk_state = intel_atomic_get_cdclk_state(state);
100628a30b45SVille Syrjälä 	if (IS_ERR(cdclk_state))
100728a30b45SVille Syrjälä 		return PTR_ERR(cdclk_state);
100828a30b45SVille Syrjälä 
100928a30b45SVille Syrjälä 	cdclk_state->force_min_cdclk = enable ? 2 * 96000 : 0;
101028a30b45SVille Syrjälä 
101128a30b45SVille Syrjälä 	return drm_atomic_commit(&state->base);
101228a30b45SVille Syrjälä }
101328a30b45SVille Syrjälä 
1014df0566a6SJani Nikula static void glk_force_audio_cdclk(struct drm_i915_private *dev_priv,
1015df0566a6SJani Nikula 				  bool enable)
1016df0566a6SJani Nikula {
1017df0566a6SJani Nikula 	struct drm_modeset_acquire_ctx ctx;
1018df0566a6SJani Nikula 	struct drm_atomic_state *state;
101908e3ed3aSChris Wilson 	struct intel_crtc *crtc;
1020df0566a6SJani Nikula 	int ret;
1021df0566a6SJani Nikula 
1022eae3da27SAnshuman Gupta 	crtc = intel_get_first_crtc(dev_priv);
102308e3ed3aSChris Wilson 	if (!crtc)
102408e3ed3aSChris Wilson 		return;
102508e3ed3aSChris Wilson 
1026df0566a6SJani Nikula 	drm_modeset_acquire_init(&ctx, 0);
1027df0566a6SJani Nikula 	state = drm_atomic_state_alloc(&dev_priv->drm);
10289a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !state))
1029df0566a6SJani Nikula 		return;
1030df0566a6SJani Nikula 
1031df0566a6SJani Nikula 	state->acquire_ctx = &ctx;
1032df0566a6SJani Nikula 
1033df0566a6SJani Nikula retry:
103408e3ed3aSChris Wilson 	ret = glk_force_audio_cdclk_commit(to_intel_atomic_state(state), crtc,
103508e3ed3aSChris Wilson 					   enable);
1036df0566a6SJani Nikula 	if (ret == -EDEADLK) {
1037df0566a6SJani Nikula 		drm_atomic_state_clear(state);
1038df0566a6SJani Nikula 		drm_modeset_backoff(&ctx);
1039df0566a6SJani Nikula 		goto retry;
1040df0566a6SJani Nikula 	}
1041df0566a6SJani Nikula 
10429a3b466bSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, ret);
1043df0566a6SJani Nikula 
1044df0566a6SJani Nikula 	drm_atomic_state_put(state);
1045df0566a6SJani Nikula 
1046df0566a6SJani Nikula 	drm_modeset_drop_locks(&ctx);
1047df0566a6SJani Nikula 	drm_modeset_acquire_fini(&ctx);
1048df0566a6SJani Nikula }
1049df0566a6SJani Nikula 
1050df0566a6SJani Nikula static unsigned long i915_audio_component_get_power(struct device *kdev)
1051df0566a6SJani Nikula {
1052df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1053df0566a6SJani Nikula 	intel_wakeref_t ret;
1054df0566a6SJani Nikula 
1055df0566a6SJani Nikula 	/* Catch potential impedance mismatches before they occur! */
1056df0566a6SJani Nikula 	BUILD_BUG_ON(sizeof(intel_wakeref_t) > sizeof(unsigned long));
1057df0566a6SJani Nikula 
1058615a7724SAnshuman Gupta 	ret = intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO_PLAYBACK);
1059df0566a6SJani Nikula 
1060ca3cfb9dSJani Nikula 	if (dev_priv->audio.power_refcount++ == 0) {
1061005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 9) {
106249e659bcSJani Nikula 			intel_de_write(dev_priv, AUD_FREQ_CNTRL,
1063ca3cfb9dSJani Nikula 				       dev_priv->audio.freq_cntrl);
106463855149SWambui Karuga 			drm_dbg_kms(&dev_priv->drm,
106563855149SWambui Karuga 				    "restored AUD_FREQ_CNTRL to 0x%x\n",
1066ca3cfb9dSJani Nikula 				    dev_priv->audio.freq_cntrl);
106787c16945SKai Vehmanen 		}
106887c16945SKai Vehmanen 
106987c16945SKai Vehmanen 		/* Force CDCLK to 2*BCLK as long as we need audio powered. */
10701ee48a61SKai Vehmanen 		if (IS_GEMINILAKE(dev_priv))
1071df0566a6SJani Nikula 			glk_force_audio_cdclk(dev_priv, true);
10721580d3cdSKai Vehmanen 
10732b5a4562SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 10)
107449e659bcSJani Nikula 			intel_de_write(dev_priv, AUD_PIN_BUF_CTL,
107549e659bcSJani Nikula 				       (intel_de_read(dev_priv, AUD_PIN_BUF_CTL) | AUD_PIN_BUF_ENABLE));
107687c16945SKai Vehmanen 	}
1077df0566a6SJani Nikula 
1078df0566a6SJani Nikula 	return ret;
1079df0566a6SJani Nikula }
1080df0566a6SJani Nikula 
1081df0566a6SJani Nikula static void i915_audio_component_put_power(struct device *kdev,
1082df0566a6SJani Nikula 					   unsigned long cookie)
1083df0566a6SJani Nikula {
1084df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1085df0566a6SJani Nikula 
1086df0566a6SJani Nikula 	/* Stop forcing CDCLK to 2*BCLK if no need for audio to be powered. */
1087ca3cfb9dSJani Nikula 	if (--dev_priv->audio.power_refcount == 0)
10881ee48a61SKai Vehmanen 		if (IS_GEMINILAKE(dev_priv))
1089df0566a6SJani Nikula 			glk_force_audio_cdclk(dev_priv, false);
1090df0566a6SJani Nikula 
1091615a7724SAnshuman Gupta 	intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO_PLAYBACK, cookie);
1092df0566a6SJani Nikula }
1093df0566a6SJani Nikula 
1094df0566a6SJani Nikula static void i915_audio_component_codec_wake_override(struct device *kdev,
1095df0566a6SJani Nikula 						     bool enable)
1096df0566a6SJani Nikula {
1097df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1098df0566a6SJani Nikula 	unsigned long cookie;
1099df0566a6SJani Nikula 	u32 tmp;
1100df0566a6SJani Nikula 
1101005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) < 9)
1102df0566a6SJani Nikula 		return;
1103df0566a6SJani Nikula 
1104df0566a6SJani Nikula 	cookie = i915_audio_component_get_power(kdev);
1105df0566a6SJani Nikula 
1106df0566a6SJani Nikula 	/*
1107df0566a6SJani Nikula 	 * Enable/disable generating the codec wake signal, overriding the
1108df0566a6SJani Nikula 	 * internal logic to generate the codec wake to controller.
1109df0566a6SJani Nikula 	 */
111049e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_CHICKENBIT);
1111df0566a6SJani Nikula 	tmp &= ~SKL_AUD_CODEC_WAKE_SIGNAL;
111249e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_CHICKENBIT, tmp);
1113df0566a6SJani Nikula 	usleep_range(1000, 1500);
1114df0566a6SJani Nikula 
1115df0566a6SJani Nikula 	if (enable) {
111649e659bcSJani Nikula 		tmp = intel_de_read(dev_priv, HSW_AUD_CHICKENBIT);
1117df0566a6SJani Nikula 		tmp |= SKL_AUD_CODEC_WAKE_SIGNAL;
111849e659bcSJani Nikula 		intel_de_write(dev_priv, HSW_AUD_CHICKENBIT, tmp);
1119df0566a6SJani Nikula 		usleep_range(1000, 1500);
1120df0566a6SJani Nikula 	}
1121df0566a6SJani Nikula 
1122df0566a6SJani Nikula 	i915_audio_component_put_power(kdev, cookie);
1123df0566a6SJani Nikula }
1124df0566a6SJani Nikula 
1125df0566a6SJani Nikula /* Get CDCLK in kHz  */
1126df0566a6SJani Nikula static int i915_audio_component_get_cdclk_freq(struct device *kdev)
1127df0566a6SJani Nikula {
1128df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1129df0566a6SJani Nikula 
11309a3b466bSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !HAS_DDI(dev_priv)))
1131df0566a6SJani Nikula 		return -ENODEV;
1132df0566a6SJani Nikula 
1133df0566a6SJani Nikula 	return dev_priv->cdclk.hw.cdclk;
1134df0566a6SJani Nikula }
1135df0566a6SJani Nikula 
1136df0566a6SJani Nikula /*
1137df0566a6SJani Nikula  * get the intel_encoder according to the parameter port and pipe
1138df0566a6SJani Nikula  * intel_encoder is saved by the index of pipe
1139ca3cfb9dSJani Nikula  * MST & (pipe >= 0): return the audio.encoder_map[pipe],
1140df0566a6SJani Nikula  *   when port is matched
1141df0566a6SJani Nikula  * MST & (pipe < 0): this is invalid
1142df0566a6SJani Nikula  * Non-MST & (pipe >= 0): only pipe = 0 (the first device entry)
1143df0566a6SJani Nikula  *   will get the right intel_encoder with port matched
1144df0566a6SJani Nikula  * Non-MST & (pipe < 0): get the right intel_encoder with port matched
1145df0566a6SJani Nikula  */
1146df0566a6SJani Nikula static struct intel_encoder *get_saved_enc(struct drm_i915_private *dev_priv,
1147df0566a6SJani Nikula 					       int port, int pipe)
1148df0566a6SJani Nikula {
1149df0566a6SJani Nikula 	struct intel_encoder *encoder;
1150df0566a6SJani Nikula 
1151df0566a6SJani Nikula 	/* MST */
1152df0566a6SJani Nikula 	if (pipe >= 0) {
11539a3b466bSPankaj Bharadiya 		if (drm_WARN_ON(&dev_priv->drm,
1154ca3cfb9dSJani Nikula 				pipe >= ARRAY_SIZE(dev_priv->audio.encoder_map)))
1155df0566a6SJani Nikula 			return NULL;
1156df0566a6SJani Nikula 
1157ca3cfb9dSJani Nikula 		encoder = dev_priv->audio.encoder_map[pipe];
1158df0566a6SJani Nikula 		/*
1159df0566a6SJani Nikula 		 * when bootup, audio driver may not know it is
1160df0566a6SJani Nikula 		 * MST or not. So it will poll all the port & pipe
1161df0566a6SJani Nikula 		 * combinations
1162df0566a6SJani Nikula 		 */
1163df0566a6SJani Nikula 		if (encoder != NULL && encoder->port == port &&
1164df0566a6SJani Nikula 		    encoder->type == INTEL_OUTPUT_DP_MST)
1165df0566a6SJani Nikula 			return encoder;
1166df0566a6SJani Nikula 	}
1167df0566a6SJani Nikula 
1168df0566a6SJani Nikula 	/* Non-MST */
1169df0566a6SJani Nikula 	if (pipe > 0)
1170df0566a6SJani Nikula 		return NULL;
1171df0566a6SJani Nikula 
1172df0566a6SJani Nikula 	for_each_pipe(dev_priv, pipe) {
1173ca3cfb9dSJani Nikula 		encoder = dev_priv->audio.encoder_map[pipe];
1174df0566a6SJani Nikula 		if (encoder == NULL)
1175df0566a6SJani Nikula 			continue;
1176df0566a6SJani Nikula 
1177df0566a6SJani Nikula 		if (encoder->type == INTEL_OUTPUT_DP_MST)
1178df0566a6SJani Nikula 			continue;
1179df0566a6SJani Nikula 
1180df0566a6SJani Nikula 		if (port == encoder->port)
1181df0566a6SJani Nikula 			return encoder;
1182df0566a6SJani Nikula 	}
1183df0566a6SJani Nikula 
1184df0566a6SJani Nikula 	return NULL;
1185df0566a6SJani Nikula }
1186df0566a6SJani Nikula 
1187df0566a6SJani Nikula static int i915_audio_component_sync_audio_rate(struct device *kdev, int port,
1188df0566a6SJani Nikula 						int pipe, int rate)
1189df0566a6SJani Nikula {
1190df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1191ca3cfb9dSJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio.component;
1192df0566a6SJani Nikula 	struct intel_encoder *encoder;
1193df0566a6SJani Nikula 	struct intel_crtc *crtc;
1194df0566a6SJani Nikula 	unsigned long cookie;
1195df0566a6SJani Nikula 	int err = 0;
1196df0566a6SJani Nikula 
1197df0566a6SJani Nikula 	if (!HAS_DDI(dev_priv))
1198df0566a6SJani Nikula 		return 0;
1199df0566a6SJani Nikula 
1200df0566a6SJani Nikula 	cookie = i915_audio_component_get_power(kdev);
1201ca3cfb9dSJani Nikula 	mutex_lock(&dev_priv->audio.mutex);
1202df0566a6SJani Nikula 
1203df0566a6SJani Nikula 	/* 1. get the pipe */
1204df0566a6SJani Nikula 	encoder = get_saved_enc(dev_priv, port, pipe);
1205df0566a6SJani Nikula 	if (!encoder || !encoder->base.crtc) {
120663855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "Not valid for port %c\n",
120763855149SWambui Karuga 			    port_name(port));
1208df0566a6SJani Nikula 		err = -ENODEV;
1209df0566a6SJani Nikula 		goto unlock;
1210df0566a6SJani Nikula 	}
1211df0566a6SJani Nikula 
1212df0566a6SJani Nikula 	crtc = to_intel_crtc(encoder->base.crtc);
1213df0566a6SJani Nikula 
1214df0566a6SJani Nikula 	/* port must be valid now, otherwise the pipe will be invalid */
1215df0566a6SJani Nikula 	acomp->aud_sample_rate[port] = rate;
1216df0566a6SJani Nikula 
1217df0566a6SJani Nikula 	hsw_audio_config_update(encoder, crtc->config);
1218df0566a6SJani Nikula 
1219df0566a6SJani Nikula  unlock:
1220ca3cfb9dSJani Nikula 	mutex_unlock(&dev_priv->audio.mutex);
1221df0566a6SJani Nikula 	i915_audio_component_put_power(kdev, cookie);
1222df0566a6SJani Nikula 	return err;
1223df0566a6SJani Nikula }
1224df0566a6SJani Nikula 
1225df0566a6SJani Nikula static int i915_audio_component_get_eld(struct device *kdev, int port,
1226df0566a6SJani Nikula 					int pipe, bool *enabled,
1227df0566a6SJani Nikula 					unsigned char *buf, int max_bytes)
1228df0566a6SJani Nikula {
1229df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1230df0566a6SJani Nikula 	struct intel_encoder *intel_encoder;
1231df0566a6SJani Nikula 	const u8 *eld;
1232df0566a6SJani Nikula 	int ret = -EINVAL;
1233df0566a6SJani Nikula 
1234ca3cfb9dSJani Nikula 	mutex_lock(&dev_priv->audio.mutex);
1235df0566a6SJani Nikula 
1236df0566a6SJani Nikula 	intel_encoder = get_saved_enc(dev_priv, port, pipe);
1237df0566a6SJani Nikula 	if (!intel_encoder) {
123863855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "Not valid for port %c\n",
123963855149SWambui Karuga 			    port_name(port));
1240ca3cfb9dSJani Nikula 		mutex_unlock(&dev_priv->audio.mutex);
1241df0566a6SJani Nikula 		return ret;
1242df0566a6SJani Nikula 	}
1243df0566a6SJani Nikula 
1244df0566a6SJani Nikula 	ret = 0;
1245df0566a6SJani Nikula 	*enabled = intel_encoder->audio_connector != NULL;
1246df0566a6SJani Nikula 	if (*enabled) {
1247df0566a6SJani Nikula 		eld = intel_encoder->audio_connector->eld;
1248df0566a6SJani Nikula 		ret = drm_eld_size(eld);
1249df0566a6SJani Nikula 		memcpy(buf, eld, min(max_bytes, ret));
1250df0566a6SJani Nikula 	}
1251df0566a6SJani Nikula 
1252ca3cfb9dSJani Nikula 	mutex_unlock(&dev_priv->audio.mutex);
1253df0566a6SJani Nikula 	return ret;
1254df0566a6SJani Nikula }
1255df0566a6SJani Nikula 
1256df0566a6SJani Nikula static const struct drm_audio_component_ops i915_audio_component_ops = {
1257df0566a6SJani Nikula 	.owner		= THIS_MODULE,
1258df0566a6SJani Nikula 	.get_power	= i915_audio_component_get_power,
1259df0566a6SJani Nikula 	.put_power	= i915_audio_component_put_power,
1260df0566a6SJani Nikula 	.codec_wake_override = i915_audio_component_codec_wake_override,
1261df0566a6SJani Nikula 	.get_cdclk_freq	= i915_audio_component_get_cdclk_freq,
1262df0566a6SJani Nikula 	.sync_audio_rate = i915_audio_component_sync_audio_rate,
1263df0566a6SJani Nikula 	.get_eld	= i915_audio_component_get_eld,
1264df0566a6SJani Nikula };
1265df0566a6SJani Nikula 
1266df0566a6SJani Nikula static int i915_audio_component_bind(struct device *i915_kdev,
1267df0566a6SJani Nikula 				     struct device *hda_kdev, void *data)
1268df0566a6SJani Nikula {
1269df0566a6SJani Nikula 	struct i915_audio_component *acomp = data;
1270df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
1271df0566a6SJani Nikula 	int i;
1272df0566a6SJani Nikula 
12739a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, acomp->base.ops || acomp->base.dev))
1274df0566a6SJani Nikula 		return -EEXIST;
1275df0566a6SJani Nikula 
12769a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
12779a3b466bSPankaj Bharadiya 			!device_link_add(hda_kdev, i915_kdev,
12789a3b466bSPankaj Bharadiya 					 DL_FLAG_STATELESS)))
1279df0566a6SJani Nikula 		return -ENOMEM;
1280df0566a6SJani Nikula 
1281df0566a6SJani Nikula 	drm_modeset_lock_all(&dev_priv->drm);
1282df0566a6SJani Nikula 	acomp->base.ops = &i915_audio_component_ops;
1283df0566a6SJani Nikula 	acomp->base.dev = i915_kdev;
1284df0566a6SJani Nikula 	BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
1285df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
1286df0566a6SJani Nikula 		acomp->aud_sample_rate[i] = 0;
1287ca3cfb9dSJani Nikula 	dev_priv->audio.component = acomp;
1288df0566a6SJani Nikula 	drm_modeset_unlock_all(&dev_priv->drm);
1289df0566a6SJani Nikula 
1290df0566a6SJani Nikula 	return 0;
1291df0566a6SJani Nikula }
1292df0566a6SJani Nikula 
1293df0566a6SJani Nikula static void i915_audio_component_unbind(struct device *i915_kdev,
1294df0566a6SJani Nikula 					struct device *hda_kdev, void *data)
1295df0566a6SJani Nikula {
1296df0566a6SJani Nikula 	struct i915_audio_component *acomp = data;
1297df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
1298df0566a6SJani Nikula 
1299df0566a6SJani Nikula 	drm_modeset_lock_all(&dev_priv->drm);
1300df0566a6SJani Nikula 	acomp->base.ops = NULL;
1301df0566a6SJani Nikula 	acomp->base.dev = NULL;
1302ca3cfb9dSJani Nikula 	dev_priv->audio.component = NULL;
1303df0566a6SJani Nikula 	drm_modeset_unlock_all(&dev_priv->drm);
1304df0566a6SJani Nikula 
1305df0566a6SJani Nikula 	device_link_remove(hda_kdev, i915_kdev);
1306b4ed131dSJani Nikula 
1307ca3cfb9dSJani Nikula 	if (dev_priv->audio.power_refcount)
1308b4ed131dSJani Nikula 		drm_err(&dev_priv->drm, "audio power refcount %d after unbind\n",
1309ca3cfb9dSJani Nikula 			dev_priv->audio.power_refcount);
1310df0566a6SJani Nikula }
1311df0566a6SJani Nikula 
1312df0566a6SJani Nikula static const struct component_ops i915_audio_component_bind_ops = {
1313df0566a6SJani Nikula 	.bind	= i915_audio_component_bind,
1314df0566a6SJani Nikula 	.unbind	= i915_audio_component_unbind,
1315df0566a6SJani Nikula };
1316df0566a6SJani Nikula 
1317989634fbSKai Vehmanen #define AUD_FREQ_TMODE_SHIFT	14
1318989634fbSKai Vehmanen #define AUD_FREQ_4T		0
1319989634fbSKai Vehmanen #define AUD_FREQ_8T		(2 << AUD_FREQ_TMODE_SHIFT)
1320989634fbSKai Vehmanen #define AUD_FREQ_PULLCLKS(x)	(((x) & 0x3) << 11)
1321989634fbSKai Vehmanen #define AUD_FREQ_BCLK_96M	BIT(4)
1322989634fbSKai Vehmanen 
1323989634fbSKai Vehmanen #define AUD_FREQ_GEN12          (AUD_FREQ_8T | AUD_FREQ_PULLCLKS(0) | AUD_FREQ_BCLK_96M)
1324989634fbSKai Vehmanen #define AUD_FREQ_TGL_BROKEN     (AUD_FREQ_8T | AUD_FREQ_PULLCLKS(2) | AUD_FREQ_BCLK_96M)
1325989634fbSKai Vehmanen 
1326df0566a6SJani Nikula /**
1327df0566a6SJani Nikula  * i915_audio_component_init - initialize and register the audio component
1328df0566a6SJani Nikula  * @dev_priv: i915 device instance
1329df0566a6SJani Nikula  *
1330df0566a6SJani Nikula  * This will register with the component framework a child component which
1331df0566a6SJani Nikula  * will bind dynamically to the snd_hda_intel driver's corresponding master
1332df0566a6SJani Nikula  * component when the latter is registered. During binding the child
1333df0566a6SJani Nikula  * initializes an instance of struct i915_audio_component which it receives
1334df0566a6SJani Nikula  * from the master. The master can then start to use the interface defined by
1335df0566a6SJani Nikula  * this struct. Each side can break the binding at any point by deregistering
1336df0566a6SJani Nikula  * its own component after which each side's component unbind callback is
1337df0566a6SJani Nikula  * called.
1338df0566a6SJani Nikula  *
1339df0566a6SJani Nikula  * We ignore any error during registration and continue with reduced
1340df0566a6SJani Nikula  * functionality (i.e. without HDMI audio).
1341df0566a6SJani Nikula  */
1342df0566a6SJani Nikula static void i915_audio_component_init(struct drm_i915_private *dev_priv)
1343df0566a6SJani Nikula {
1344989634fbSKai Vehmanen 	u32 aud_freq, aud_freq_init;
1345df0566a6SJani Nikula 	int ret;
1346df0566a6SJani Nikula 
1347df0566a6SJani Nikula 	ret = component_add_typed(dev_priv->drm.dev,
1348df0566a6SJani Nikula 				  &i915_audio_component_bind_ops,
1349df0566a6SJani Nikula 				  I915_COMPONENT_AUDIO);
1350df0566a6SJani Nikula 	if (ret < 0) {
135163855149SWambui Karuga 		drm_err(&dev_priv->drm,
135263855149SWambui Karuga 			"failed to add audio component (%d)\n", ret);
1353df0566a6SJani Nikula 		/* continue with reduced functionality */
1354df0566a6SJani Nikula 		return;
1355df0566a6SJani Nikula 	}
1356df0566a6SJani Nikula 
1357005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 9) {
1358989634fbSKai Vehmanen 		aud_freq_init = intel_de_read(dev_priv, AUD_FREQ_CNTRL);
1359989634fbSKai Vehmanen 
136027ec10b3SLucas De Marchi 		if (DISPLAY_VER(dev_priv) >= 12)
1361989634fbSKai Vehmanen 			aud_freq = AUD_FREQ_GEN12;
1362989634fbSKai Vehmanen 		else
1363989634fbSKai Vehmanen 			aud_freq = aud_freq_init;
1364989634fbSKai Vehmanen 
1365c6b40ee3SKai-Heng Feng 		/* use BIOS provided value for TGL and RKL unless it is a known bad value */
1366c6b40ee3SKai-Heng Feng 		if ((IS_TIGERLAKE(dev_priv) || IS_ROCKETLAKE(dev_priv)) &&
1367c6b40ee3SKai-Heng Feng 		    aud_freq_init != AUD_FREQ_TGL_BROKEN)
1368989634fbSKai Vehmanen 			aud_freq = aud_freq_init;
1369989634fbSKai Vehmanen 
1370989634fbSKai Vehmanen 		drm_dbg_kms(&dev_priv->drm, "use AUD_FREQ_CNTRL of 0x%x (init value 0x%x)\n",
1371989634fbSKai Vehmanen 			    aud_freq, aud_freq_init);
1372989634fbSKai Vehmanen 
1373ca3cfb9dSJani Nikula 		dev_priv->audio.freq_cntrl = aud_freq;
137487c16945SKai Vehmanen 	}
137587c16945SKai Vehmanen 
1376112a87c4SKai Vehmanen 	/* init with current cdclk */
1377112a87c4SKai Vehmanen 	intel_audio_cdclk_change_post(dev_priv);
1378112a87c4SKai Vehmanen 
1379ca3cfb9dSJani Nikula 	dev_priv->audio.component_registered = true;
1380df0566a6SJani Nikula }
1381df0566a6SJani Nikula 
1382df0566a6SJani Nikula /**
1383df0566a6SJani Nikula  * i915_audio_component_cleanup - deregister the audio component
1384df0566a6SJani Nikula  * @dev_priv: i915 device instance
1385df0566a6SJani Nikula  *
1386df0566a6SJani Nikula  * Deregisters the audio component, breaking any existing binding to the
1387df0566a6SJani Nikula  * corresponding snd_hda_intel driver's master component.
1388df0566a6SJani Nikula  */
1389df0566a6SJani Nikula static void i915_audio_component_cleanup(struct drm_i915_private *dev_priv)
1390df0566a6SJani Nikula {
1391ca3cfb9dSJani Nikula 	if (!dev_priv->audio.component_registered)
1392df0566a6SJani Nikula 		return;
1393df0566a6SJani Nikula 
1394df0566a6SJani Nikula 	component_del(dev_priv->drm.dev, &i915_audio_component_bind_ops);
1395ca3cfb9dSJani Nikula 	dev_priv->audio.component_registered = false;
1396df0566a6SJani Nikula }
1397df0566a6SJani Nikula 
1398df0566a6SJani Nikula /**
1399df0566a6SJani Nikula  * intel_audio_init() - Initialize the audio driver either using
1400df0566a6SJani Nikula  * component framework or using lpe audio bridge
1401df0566a6SJani Nikula  * @dev_priv: the i915 drm device private data
1402df0566a6SJani Nikula  *
1403df0566a6SJani Nikula  */
1404df0566a6SJani Nikula void intel_audio_init(struct drm_i915_private *dev_priv)
1405df0566a6SJani Nikula {
1406df0566a6SJani Nikula 	if (intel_lpe_audio_init(dev_priv) < 0)
1407df0566a6SJani Nikula 		i915_audio_component_init(dev_priv);
1408df0566a6SJani Nikula }
1409df0566a6SJani Nikula 
1410df0566a6SJani Nikula /**
1411df0566a6SJani Nikula  * intel_audio_deinit() - deinitialize the audio driver
1412df0566a6SJani Nikula  * @dev_priv: the i915 drm device private data
1413df0566a6SJani Nikula  *
1414df0566a6SJani Nikula  */
1415df0566a6SJani Nikula void intel_audio_deinit(struct drm_i915_private *dev_priv)
1416df0566a6SJani Nikula {
1417ca3cfb9dSJani Nikula 	if ((dev_priv)->audio.lpe.platdev != NULL)
1418df0566a6SJani Nikula 		intel_lpe_audio_teardown(dev_priv);
1419df0566a6SJani Nikula 	else
1420df0566a6SJani Nikula 		i915_audio_component_cleanup(dev_priv);
1421df0566a6SJani Nikula }
1422