xref: /linux/drivers/gpu/drm/i915/display/intel_audio.c (revision 343cb0f9234ec5f5d86e47c33d2c6fa649cef2fa)
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"
33b43edc50SJani Nikula #include "intel_audio_regs.h"
3428a30b45SVille Syrjälä #include "intel_cdclk.h"
35fd2b94a5SJani Nikula #include "intel_crtc.h"
367785ae0bSVille Syrjälä #include "intel_de.h"
371d455f8dSJani Nikula #include "intel_display_types.h"
38df0566a6SJani Nikula #include "intel_lpe_audio.h"
39df0566a6SJani Nikula 
40df0566a6SJani Nikula /**
41df0566a6SJani Nikula  * DOC: High Definition Audio over HDMI and Display Port
42df0566a6SJani Nikula  *
43df0566a6SJani Nikula  * The graphics and audio drivers together support High Definition Audio over
44df0566a6SJani Nikula  * HDMI and Display Port. The audio programming sequences are divided into audio
45df0566a6SJani Nikula  * codec and controller enable and disable sequences. The graphics driver
46df0566a6SJani Nikula  * handles the audio codec sequences, while the audio driver handles the audio
47df0566a6SJani Nikula  * controller sequences.
48df0566a6SJani Nikula  *
49df0566a6SJani Nikula  * The disable sequences must be performed before disabling the transcoder or
50df0566a6SJani Nikula  * port. The enable sequences may only be performed after enabling the
51df0566a6SJani Nikula  * transcoder and port, and after completed link training. Therefore the audio
52df0566a6SJani Nikula  * enable/disable sequences are part of the modeset sequence.
53df0566a6SJani Nikula  *
54df0566a6SJani Nikula  * The codec and controller sequences could be done either parallel or serial,
55df0566a6SJani Nikula  * but generally the ELDV/PD change in the codec sequence indicates to the audio
56df0566a6SJani Nikula  * driver that the controller sequence should start. Indeed, most of the
57df0566a6SJani Nikula  * co-operation between the graphics and audio drivers is handled via audio
58df0566a6SJani Nikula  * related registers. (The notable exception is the power management, not
59df0566a6SJani Nikula  * covered here.)
60df0566a6SJani Nikula  *
61df0566a6SJani Nikula  * The struct &i915_audio_component is used to interact between the graphics
62df0566a6SJani Nikula  * and audio drivers. The struct &i915_audio_component_ops @ops in it is
63df0566a6SJani Nikula  * defined in graphics driver and called in audio driver. The
64df0566a6SJani Nikula  * struct &i915_audio_component_audio_ops @audio_ops is called from i915 driver.
65df0566a6SJani Nikula  */
66df0566a6SJani Nikula 
675d453746SJani Nikula struct intel_audio_funcs {
685d453746SJani Nikula 	void (*audio_codec_enable)(struct intel_encoder *encoder,
695d453746SJani Nikula 				   const struct intel_crtc_state *crtc_state,
705d453746SJani Nikula 				   const struct drm_connector_state *conn_state);
715d453746SJani Nikula 	void (*audio_codec_disable)(struct intel_encoder *encoder,
725d453746SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
735d453746SJani Nikula 				    const struct drm_connector_state *old_conn_state);
745d453746SJani Nikula };
755d453746SJani Nikula 
76df0566a6SJani Nikula /* DP N/M table */
77df0566a6SJani Nikula #define LC_810M	810000
78df0566a6SJani Nikula #define LC_540M	540000
79df0566a6SJani Nikula #define LC_270M	270000
80df0566a6SJani Nikula #define LC_162M	162000
81df0566a6SJani Nikula 
82df0566a6SJani Nikula struct dp_aud_n_m {
83df0566a6SJani Nikula 	int sample_rate;
84df0566a6SJani Nikula 	int clock;
85df0566a6SJani Nikula 	u16 m;
86df0566a6SJani Nikula 	u16 n;
87df0566a6SJani Nikula };
88df0566a6SJani Nikula 
892c291417SAditya Swarup struct hdmi_aud_ncts {
902c291417SAditya Swarup 	int sample_rate;
912c291417SAditya Swarup 	int clock;
922c291417SAditya Swarup 	int n;
932c291417SAditya Swarup 	int cts;
942c291417SAditya Swarup };
952c291417SAditya Swarup 
96df0566a6SJani Nikula /* Values according to DP 1.4 Table 2-104 */
97df0566a6SJani Nikula static const struct dp_aud_n_m dp_aud_n_m[] = {
98df0566a6SJani Nikula 	{ 32000, LC_162M, 1024, 10125 },
99df0566a6SJani Nikula 	{ 44100, LC_162M, 784, 5625 },
100df0566a6SJani Nikula 	{ 48000, LC_162M, 512, 3375 },
101df0566a6SJani Nikula 	{ 64000, LC_162M, 2048, 10125 },
102df0566a6SJani Nikula 	{ 88200, LC_162M, 1568, 5625 },
103df0566a6SJani Nikula 	{ 96000, LC_162M, 1024, 3375 },
104df0566a6SJani Nikula 	{ 128000, LC_162M, 4096, 10125 },
105df0566a6SJani Nikula 	{ 176400, LC_162M, 3136, 5625 },
106df0566a6SJani Nikula 	{ 192000, LC_162M, 2048, 3375 },
107df0566a6SJani Nikula 	{ 32000, LC_270M, 1024, 16875 },
108df0566a6SJani Nikula 	{ 44100, LC_270M, 784, 9375 },
109df0566a6SJani Nikula 	{ 48000, LC_270M, 512, 5625 },
110df0566a6SJani Nikula 	{ 64000, LC_270M, 2048, 16875 },
111df0566a6SJani Nikula 	{ 88200, LC_270M, 1568, 9375 },
112df0566a6SJani Nikula 	{ 96000, LC_270M, 1024, 5625 },
113df0566a6SJani Nikula 	{ 128000, LC_270M, 4096, 16875 },
114df0566a6SJani Nikula 	{ 176400, LC_270M, 3136, 9375 },
115df0566a6SJani Nikula 	{ 192000, LC_270M, 2048, 5625 },
116df0566a6SJani Nikula 	{ 32000, LC_540M, 1024, 33750 },
117df0566a6SJani Nikula 	{ 44100, LC_540M, 784, 18750 },
118df0566a6SJani Nikula 	{ 48000, LC_540M, 512, 11250 },
119df0566a6SJani Nikula 	{ 64000, LC_540M, 2048, 33750 },
120df0566a6SJani Nikula 	{ 88200, LC_540M, 1568, 18750 },
121df0566a6SJani Nikula 	{ 96000, LC_540M, 1024, 11250 },
122df0566a6SJani Nikula 	{ 128000, LC_540M, 4096, 33750 },
123df0566a6SJani Nikula 	{ 176400, LC_540M, 3136, 18750 },
124df0566a6SJani Nikula 	{ 192000, LC_540M, 2048, 11250 },
125df0566a6SJani Nikula 	{ 32000, LC_810M, 1024, 50625 },
126df0566a6SJani Nikula 	{ 44100, LC_810M, 784, 28125 },
127df0566a6SJani Nikula 	{ 48000, LC_810M, 512, 16875 },
128df0566a6SJani Nikula 	{ 64000, LC_810M, 2048, 50625 },
129df0566a6SJani Nikula 	{ 88200, LC_810M, 1568, 28125 },
130df0566a6SJani Nikula 	{ 96000, LC_810M, 1024, 16875 },
131df0566a6SJani Nikula 	{ 128000, LC_810M, 4096, 50625 },
132df0566a6SJani Nikula 	{ 176400, LC_810M, 3136, 28125 },
133df0566a6SJani Nikula 	{ 192000, LC_810M, 2048, 16875 },
134df0566a6SJani Nikula };
135df0566a6SJani Nikula 
136df0566a6SJani Nikula static const struct dp_aud_n_m *
137df0566a6SJani Nikula audio_config_dp_get_n_m(const struct intel_crtc_state *crtc_state, int rate)
138df0566a6SJani Nikula {
139df0566a6SJani Nikula 	int i;
140df0566a6SJani Nikula 
141df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(dp_aud_n_m); i++) {
142df0566a6SJani Nikula 		if (rate == dp_aud_n_m[i].sample_rate &&
143df0566a6SJani Nikula 		    crtc_state->port_clock == dp_aud_n_m[i].clock)
144df0566a6SJani Nikula 			return &dp_aud_n_m[i];
145df0566a6SJani Nikula 	}
146df0566a6SJani Nikula 
147df0566a6SJani Nikula 	return NULL;
148df0566a6SJani Nikula }
149df0566a6SJani Nikula 
150df0566a6SJani Nikula static const struct {
151df0566a6SJani Nikula 	int clock;
152df0566a6SJani Nikula 	u32 config;
153df0566a6SJani Nikula } hdmi_audio_clock[] = {
154df0566a6SJani Nikula 	{ 25175, AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
155df0566a6SJani Nikula 	{ 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
156df0566a6SJani Nikula 	{ 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
157df0566a6SJani Nikula 	{ 27027, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
158df0566a6SJani Nikula 	{ 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
159df0566a6SJani Nikula 	{ 54054, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
160df0566a6SJani Nikula 	{ 74176, AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
161df0566a6SJani Nikula 	{ 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
162df0566a6SJani Nikula 	{ 148352, AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
163df0566a6SJani Nikula 	{ 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
1641aae3065SKai Vehmanen 	{ 296703, AUD_CONFIG_PIXEL_CLOCK_HDMI_296703 },
1651aae3065SKai Vehmanen 	{ 297000, AUD_CONFIG_PIXEL_CLOCK_HDMI_297000 },
1661aae3065SKai Vehmanen 	{ 593407, AUD_CONFIG_PIXEL_CLOCK_HDMI_593407 },
1671aae3065SKai Vehmanen 	{ 594000, AUD_CONFIG_PIXEL_CLOCK_HDMI_594000 },
168df0566a6SJani Nikula };
169df0566a6SJani Nikula 
170df0566a6SJani Nikula /* HDMI N/CTS table */
171df0566a6SJani Nikula #define TMDS_297M 297000
172df0566a6SJani Nikula #define TMDS_296M 296703
173df0566a6SJani Nikula #define TMDS_594M 594000
174df0566a6SJani Nikula #define TMDS_593M 593407
175df0566a6SJani Nikula 
1762c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_24bpp[] = {
177df0566a6SJani Nikula 	{ 32000, TMDS_296M, 5824, 421875 },
178df0566a6SJani Nikula 	{ 32000, TMDS_297M, 3072, 222750 },
179df0566a6SJani Nikula 	{ 32000, TMDS_593M, 5824, 843750 },
180df0566a6SJani Nikula 	{ 32000, TMDS_594M, 3072, 445500 },
181df0566a6SJani Nikula 	{ 44100, TMDS_296M, 4459, 234375 },
182df0566a6SJani Nikula 	{ 44100, TMDS_297M, 4704, 247500 },
183df0566a6SJani Nikula 	{ 44100, TMDS_593M, 8918, 937500 },
184df0566a6SJani Nikula 	{ 44100, TMDS_594M, 9408, 990000 },
185df0566a6SJani Nikula 	{ 88200, TMDS_296M, 8918, 234375 },
186df0566a6SJani Nikula 	{ 88200, TMDS_297M, 9408, 247500 },
187df0566a6SJani Nikula 	{ 88200, TMDS_593M, 17836, 937500 },
188df0566a6SJani Nikula 	{ 88200, TMDS_594M, 18816, 990000 },
189df0566a6SJani Nikula 	{ 176400, TMDS_296M, 17836, 234375 },
190df0566a6SJani Nikula 	{ 176400, TMDS_297M, 18816, 247500 },
191df0566a6SJani Nikula 	{ 176400, TMDS_593M, 35672, 937500 },
192df0566a6SJani Nikula 	{ 176400, TMDS_594M, 37632, 990000 },
193df0566a6SJani Nikula 	{ 48000, TMDS_296M, 5824, 281250 },
194df0566a6SJani Nikula 	{ 48000, TMDS_297M, 5120, 247500 },
195df0566a6SJani Nikula 	{ 48000, TMDS_593M, 5824, 562500 },
196df0566a6SJani Nikula 	{ 48000, TMDS_594M, 6144, 594000 },
197df0566a6SJani Nikula 	{ 96000, TMDS_296M, 11648, 281250 },
198df0566a6SJani Nikula 	{ 96000, TMDS_297M, 10240, 247500 },
199df0566a6SJani Nikula 	{ 96000, TMDS_593M, 11648, 562500 },
200df0566a6SJani Nikula 	{ 96000, TMDS_594M, 12288, 594000 },
201df0566a6SJani Nikula 	{ 192000, TMDS_296M, 23296, 281250 },
202df0566a6SJani Nikula 	{ 192000, TMDS_297M, 20480, 247500 },
203df0566a6SJani Nikula 	{ 192000, TMDS_593M, 23296, 562500 },
204df0566a6SJani Nikula 	{ 192000, TMDS_594M, 24576, 594000 },
205df0566a6SJani Nikula };
206df0566a6SJani Nikula 
2072c291417SAditya Swarup /* Appendix C - N & CTS values for deep color from HDMI 2.0 spec*/
2082c291417SAditya Swarup /* HDMI N/CTS table for 10 bit deep color(30 bpp)*/
2092c291417SAditya Swarup #define TMDS_371M 371250
2102c291417SAditya Swarup #define TMDS_370M 370878
2112c291417SAditya Swarup 
2122c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_30bpp[] = {
2132c291417SAditya Swarup 	{ 32000, TMDS_370M, 5824, 527344 },
2142c291417SAditya Swarup 	{ 32000, TMDS_371M, 6144, 556875 },
2152c291417SAditya Swarup 	{ 44100, TMDS_370M, 8918, 585938 },
2162c291417SAditya Swarup 	{ 44100, TMDS_371M, 4704, 309375 },
2172c291417SAditya Swarup 	{ 88200, TMDS_370M, 17836, 585938 },
2182c291417SAditya Swarup 	{ 88200, TMDS_371M, 9408, 309375 },
2192c291417SAditya Swarup 	{ 176400, TMDS_370M, 35672, 585938 },
2202c291417SAditya Swarup 	{ 176400, TMDS_371M, 18816, 309375 },
2212c291417SAditya Swarup 	{ 48000, TMDS_370M, 11648, 703125 },
2222c291417SAditya Swarup 	{ 48000, TMDS_371M, 5120, 309375 },
2232c291417SAditya Swarup 	{ 96000, TMDS_370M, 23296, 703125 },
2242c291417SAditya Swarup 	{ 96000, TMDS_371M, 10240, 309375 },
2252c291417SAditya Swarup 	{ 192000, TMDS_370M, 46592, 703125 },
2262c291417SAditya Swarup 	{ 192000, TMDS_371M, 20480, 309375 },
2272c291417SAditya Swarup };
2282c291417SAditya Swarup 
2292c291417SAditya Swarup /* HDMI N/CTS table for 12 bit deep color(36 bpp)*/
2302c291417SAditya Swarup #define TMDS_445_5M 445500
2312c291417SAditya Swarup #define TMDS_445M 445054
2322c291417SAditya Swarup 
2332c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_36bpp[] = {
2342c291417SAditya Swarup 	{ 32000, TMDS_445M, 5824, 632813 },
2352c291417SAditya Swarup 	{ 32000, TMDS_445_5M, 4096, 445500 },
2362c291417SAditya Swarup 	{ 44100, TMDS_445M, 8918, 703125 },
2372c291417SAditya Swarup 	{ 44100, TMDS_445_5M, 4704, 371250 },
2382c291417SAditya Swarup 	{ 88200, TMDS_445M, 17836, 703125 },
2392c291417SAditya Swarup 	{ 88200, TMDS_445_5M, 9408, 371250 },
2402c291417SAditya Swarup 	{ 176400, TMDS_445M, 35672, 703125 },
2412c291417SAditya Swarup 	{ 176400, TMDS_445_5M, 18816, 371250 },
2422c291417SAditya Swarup 	{ 48000, TMDS_445M, 5824, 421875 },
2432c291417SAditya Swarup 	{ 48000, TMDS_445_5M, 5120, 371250 },
2442c291417SAditya Swarup 	{ 96000, TMDS_445M, 11648, 421875 },
2452c291417SAditya Swarup 	{ 96000, TMDS_445_5M, 10240, 371250 },
2462c291417SAditya Swarup 	{ 192000, TMDS_445M, 23296, 421875 },
2472c291417SAditya Swarup 	{ 192000, TMDS_445_5M, 20480, 371250 },
2482c291417SAditya Swarup };
2492c291417SAditya Swarup 
250df0566a6SJani Nikula /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
251df0566a6SJani Nikula static u32 audio_config_hdmi_pixel_clock(const struct intel_crtc_state *crtc_state)
252df0566a6SJani Nikula {
25346e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
254df0566a6SJani Nikula 	const struct drm_display_mode *adjusted_mode =
2551326a92cSMaarten Lankhorst 		&crtc_state->hw.adjusted_mode;
256df0566a6SJani Nikula 	int i;
257df0566a6SJani Nikula 
258df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
259df0566a6SJani Nikula 		if (adjusted_mode->crtc_clock == hdmi_audio_clock[i].clock)
260df0566a6SJani Nikula 			break;
261df0566a6SJani Nikula 	}
262df0566a6SJani Nikula 
26346e61ee4SVille Syrjälä 	if (DISPLAY_VER(i915) < 12 && adjusted_mode->crtc_clock > 148500)
2641aae3065SKai Vehmanen 		i = ARRAY_SIZE(hdmi_audio_clock);
2651aae3065SKai Vehmanen 
266df0566a6SJani Nikula 	if (i == ARRAY_SIZE(hdmi_audio_clock)) {
26746e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm,
2689282a66cSJani Nikula 			    "HDMI audio pixel clock setting for %d not found, falling back to defaults\n",
269df0566a6SJani Nikula 			    adjusted_mode->crtc_clock);
270df0566a6SJani Nikula 		i = 1;
271df0566a6SJani Nikula 	}
272df0566a6SJani Nikula 
27346e61ee4SVille Syrjälä 	drm_dbg_kms(&i915->drm,
2749282a66cSJani Nikula 		    "Configuring HDMI audio for pixel clock %d (0x%08x)\n",
275df0566a6SJani Nikula 		    hdmi_audio_clock[i].clock,
276df0566a6SJani Nikula 		    hdmi_audio_clock[i].config);
277df0566a6SJani Nikula 
278df0566a6SJani Nikula 	return hdmi_audio_clock[i].config;
279df0566a6SJani Nikula }
280df0566a6SJani Nikula 
281df0566a6SJani Nikula static int audio_config_hdmi_get_n(const struct intel_crtc_state *crtc_state,
282df0566a6SJani Nikula 				   int rate)
283df0566a6SJani Nikula {
2842c291417SAditya Swarup 	const struct hdmi_aud_ncts *hdmi_ncts_table;
2852c291417SAditya Swarup 	int i, size;
286df0566a6SJani Nikula 
2872c291417SAditya Swarup 	if (crtc_state->pipe_bpp == 36) {
2882c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_36bpp;
2892c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_36bpp);
2902c291417SAditya Swarup 	} else if (crtc_state->pipe_bpp == 30) {
2912c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_30bpp;
2922c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_30bpp);
2932c291417SAditya Swarup 	} else {
2942c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_24bpp;
2952c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_24bpp);
2962c291417SAditya Swarup 	}
2972c291417SAditya Swarup 
2982c291417SAditya Swarup 	for (i = 0; i < size; i++) {
2992c291417SAditya Swarup 		if (rate == hdmi_ncts_table[i].sample_rate &&
3002c291417SAditya Swarup 		    crtc_state->port_clock == hdmi_ncts_table[i].clock) {
3012c291417SAditya Swarup 			return hdmi_ncts_table[i].n;
302df0566a6SJani Nikula 		}
303df0566a6SJani Nikula 	}
304df0566a6SJani Nikula 	return 0;
305df0566a6SJani Nikula }
306df0566a6SJani Nikula 
3071c0ab71aSVille Syrjälä /* ELD buffer size in dwords */
3081c0ab71aSVille Syrjälä static int g4x_eld_buffer_size(struct drm_i915_private *i915)
3091c0ab71aSVille Syrjälä {
3101c0ab71aSVille Syrjälä 	u32 tmp;
3111c0ab71aSVille Syrjälä 
3121c0ab71aSVille Syrjälä 	tmp = intel_de_read(i915, G4X_AUD_CNTL_ST);
3131c0ab71aSVille Syrjälä 
3141c0ab71aSVille Syrjälä 	return REG_FIELD_GET(G4X_ELD_BUFFER_SIZE_MASK, tmp);
3151c0ab71aSVille Syrjälä }
3161c0ab71aSVille Syrjälä 
317df0566a6SJani Nikula static void g4x_audio_codec_disable(struct intel_encoder *encoder,
318df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
319df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
320df0566a6SJani Nikula {
32146e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
322c3c5dc1dSVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
323df0566a6SJani Nikula 
324df0566a6SJani Nikula 	/* Invalidate ELD */
3257c8d74e8SVille Syrjälä 	intel_de_rmw(i915, G4X_AUD_CNTL_ST,
3267c8d74e8SVille Syrjälä 		     G4X_ELD_VALID, 0);
327c3c5dc1dSVille Syrjälä 
328c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
329c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
330df0566a6SJani Nikula }
331df0566a6SJani Nikula 
332df0566a6SJani Nikula static void g4x_audio_codec_enable(struct intel_encoder *encoder,
333df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
334df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
335df0566a6SJani Nikula {
33646e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
337c3c5dc1dSVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
338df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
33950a4a926SVille Syrjälä 	const u32 *eld = (const u32 *)connector->eld;
3400234cda2SVille Syrjälä 	int eld_buffer_size, len, i;
341df0566a6SJani Nikula 
342c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
343c3c5dc1dSVille Syrjälä 
3447c8d74e8SVille Syrjälä 	intel_de_rmw(i915, G4X_AUD_CNTL_ST,
3457c8d74e8SVille Syrjälä 		     G4X_ELD_VALID | G4X_ELD_ADDRESS_MASK, 0);
346df0566a6SJani Nikula 
3470234cda2SVille Syrjälä 	eld_buffer_size = g4x_eld_buffer_size(i915);
34850a4a926SVille Syrjälä 	len = min(drm_eld_size(connector->eld) / 4, eld_buffer_size);
3491c0ab71aSVille Syrjälä 
350df0566a6SJani Nikula 	for (i = 0; i < len; i++)
35150a4a926SVille Syrjälä 		intel_de_write(i915, G4X_HDMIW_HDMIEDID, eld[i]);
3520234cda2SVille Syrjälä 	for (; i < eld_buffer_size; i++)
3530234cda2SVille Syrjälä 		intel_de_write(i915, G4X_HDMIW_HDMIEDID, 0);
3540234cda2SVille Syrjälä 
3550234cda2SVille Syrjälä 	drm_WARN_ON(&i915->drm,
3560234cda2SVille Syrjälä 		    (intel_de_read(i915, G4X_AUD_CNTL_ST) & G4X_ELD_ADDRESS_MASK) != 0);
357df0566a6SJani Nikula 
3587c8d74e8SVille Syrjälä 	intel_de_rmw(i915, G4X_AUD_CNTL_ST,
3597c8d74e8SVille Syrjälä 		     0, G4X_ELD_VALID);
360df0566a6SJani Nikula }
361df0566a6SJani Nikula 
362df0566a6SJani Nikula static void
363df0566a6SJani Nikula hsw_dp_audio_config_update(struct intel_encoder *encoder,
364df0566a6SJani Nikula 			   const struct intel_crtc_state *crtc_state)
365df0566a6SJani Nikula {
36646e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
36746e61ee4SVille Syrjälä 	struct i915_audio_component *acomp = i915->display.audio.component;
368df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
369df0566a6SJani Nikula 	enum port port = encoder->port;
370df0566a6SJani Nikula 	const struct dp_aud_n_m *nm;
371df0566a6SJani Nikula 	int rate;
372df0566a6SJani Nikula 	u32 tmp;
373df0566a6SJani Nikula 
374df0566a6SJani Nikula 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
375df0566a6SJani Nikula 	nm = audio_config_dp_get_n_m(crtc_state, rate);
376df0566a6SJani Nikula 	if (nm)
37746e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "using Maud %u, Naud %u\n", nm->m,
37863855149SWambui Karuga 			    nm->n);
379df0566a6SJani Nikula 	else
38046e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "using automatic Maud, Naud\n");
381df0566a6SJani Nikula 
38246e61ee4SVille Syrjälä 	tmp = intel_de_read(i915, HSW_AUD_CFG(cpu_transcoder));
383df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
384df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
385df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
386df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_VALUE_INDEX;
387df0566a6SJani Nikula 
388df0566a6SJani Nikula 	if (nm) {
389df0566a6SJani Nikula 		tmp &= ~AUD_CONFIG_N_MASK;
390df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N(nm->n);
391df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
392df0566a6SJani Nikula 	}
393df0566a6SJani Nikula 
39446e61ee4SVille Syrjälä 	intel_de_write(i915, HSW_AUD_CFG(cpu_transcoder), tmp);
395df0566a6SJani Nikula 
39646e61ee4SVille Syrjälä 	tmp = intel_de_read(i915, HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
397df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_M_MASK;
398df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
399df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
400df0566a6SJani Nikula 
401df0566a6SJani Nikula 	if (nm) {
402df0566a6SJani Nikula 		tmp |= nm->m;
403df0566a6SJani Nikula 		tmp |= AUD_M_CTS_M_VALUE_INDEX;
404df0566a6SJani Nikula 		tmp |= AUD_M_CTS_M_PROG_ENABLE;
405df0566a6SJani Nikula 	}
406df0566a6SJani Nikula 
40746e61ee4SVille Syrjälä 	intel_de_write(i915, HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
408df0566a6SJani Nikula }
409df0566a6SJani Nikula 
410df0566a6SJani Nikula static void
411df0566a6SJani Nikula hsw_hdmi_audio_config_update(struct intel_encoder *encoder,
412df0566a6SJani Nikula 			     const struct intel_crtc_state *crtc_state)
413df0566a6SJani Nikula {
41446e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
41546e61ee4SVille Syrjälä 	struct i915_audio_component *acomp = i915->display.audio.component;
416df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
417df0566a6SJani Nikula 	enum port port = encoder->port;
418df0566a6SJani Nikula 	int n, rate;
419df0566a6SJani Nikula 	u32 tmp;
420df0566a6SJani Nikula 
421df0566a6SJani Nikula 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
422df0566a6SJani Nikula 
42346e61ee4SVille Syrjälä 	tmp = intel_de_read(i915, HSW_AUD_CFG(cpu_transcoder));
424df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
425df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
426df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
427df0566a6SJani Nikula 	tmp |= audio_config_hdmi_pixel_clock(crtc_state);
428df0566a6SJani Nikula 
429df0566a6SJani Nikula 	n = audio_config_hdmi_get_n(crtc_state, rate);
430df0566a6SJani Nikula 	if (n != 0) {
43146e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "using N %d\n", n);
432df0566a6SJani Nikula 
433df0566a6SJani Nikula 		tmp &= ~AUD_CONFIG_N_MASK;
434df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N(n);
435df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
436df0566a6SJani Nikula 	} else {
43746e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "using automatic N\n");
438df0566a6SJani Nikula 	}
439df0566a6SJani Nikula 
44046e61ee4SVille Syrjälä 	intel_de_write(i915, HSW_AUD_CFG(cpu_transcoder), tmp);
441df0566a6SJani Nikula 
442df0566a6SJani Nikula 	/*
443df0566a6SJani Nikula 	 * Let's disable "Enable CTS or M Prog bit"
444df0566a6SJani Nikula 	 * and let HW calculate the value
445df0566a6SJani Nikula 	 */
44646e61ee4SVille Syrjälä 	tmp = intel_de_read(i915, HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
447df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
448df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
44946e61ee4SVille Syrjälä 	intel_de_write(i915, HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
450df0566a6SJani Nikula }
451df0566a6SJani Nikula 
452df0566a6SJani Nikula static void
453df0566a6SJani Nikula hsw_audio_config_update(struct intel_encoder *encoder,
454df0566a6SJani Nikula 			const struct intel_crtc_state *crtc_state)
455df0566a6SJani Nikula {
456df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state))
457df0566a6SJani Nikula 		hsw_dp_audio_config_update(encoder, crtc_state);
458df0566a6SJani Nikula 	else
459df0566a6SJani Nikula 		hsw_hdmi_audio_config_update(encoder, crtc_state);
460df0566a6SJani Nikula }
461df0566a6SJani Nikula 
4621c0ab71aSVille Syrjälä /* ELD buffer size in dwords */
4631c0ab71aSVille Syrjälä static int hsw_eld_buffer_size(struct drm_i915_private *i915,
4641c0ab71aSVille Syrjälä 			       enum transcoder cpu_transcoder)
4651c0ab71aSVille Syrjälä {
4661c0ab71aSVille Syrjälä 	u32 tmp;
4671c0ab71aSVille Syrjälä 
4681c0ab71aSVille Syrjälä 	tmp = intel_de_read(i915, HSW_AUD_DIP_ELD_CTRL(cpu_transcoder));
4691c0ab71aSVille Syrjälä 
4701c0ab71aSVille Syrjälä 	return REG_FIELD_GET(IBX_ELD_BUFFER_SIZE_MASK, tmp);
4711c0ab71aSVille Syrjälä }
4721c0ab71aSVille Syrjälä 
473df0566a6SJani Nikula static void hsw_audio_codec_disable(struct intel_encoder *encoder,
474df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
475df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
476df0566a6SJani Nikula {
47746e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
478c3c5dc1dSVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
479df0566a6SJani Nikula 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
480df0566a6SJani Nikula 
48146e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
482df0566a6SJani Nikula 
483df0566a6SJani Nikula 	/* Disable timestamps */
4847c8d74e8SVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_CFG(cpu_transcoder),
4857c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_VALUE_INDEX |
4867c8d74e8SVille Syrjälä 		     AUD_CONFIG_UPPER_N_MASK |
4877c8d74e8SVille Syrjälä 		     AUD_CONFIG_LOWER_N_MASK,
4887c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_PROG_ENABLE |
4897c8d74e8SVille Syrjälä 		     (intel_crtc_has_dp_encoder(old_crtc_state) ?
4907c8d74e8SVille Syrjälä 		      AUD_CONFIG_N_VALUE_INDEX : 0));
491df0566a6SJani Nikula 
492cbbda2ffSVille Syrjälä 	/* Invalidate ELD */
4937c8d74e8SVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_PIN_ELD_CP_VLD,
494cbbda2ffSVille Syrjälä 		     AUDIO_ELD_VALID(cpu_transcoder), 0);
495cbbda2ffSVille Syrjälä 
496c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
497c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
498c3c5dc1dSVille Syrjälä 
499cbbda2ffSVille Syrjälä 	/* Disable audio presence detect */
500cbbda2ffSVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_PIN_ELD_CP_VLD,
5017c8d74e8SVille Syrjälä 		     AUDIO_OUTPUT_ENABLE(cpu_transcoder), 0);
502df0566a6SJani Nikula 
50346e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
504df0566a6SJani Nikula }
505df0566a6SJani Nikula 
5062dd43144SVille Syrjälä static unsigned int calc_hblank_early_prog(struct intel_encoder *encoder,
50748b8b04cSUma Shankar 					   const struct intel_crtc_state *crtc_state)
50848b8b04cSUma Shankar {
50948b8b04cSUma Shankar 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
51048b8b04cSUma Shankar 	unsigned int link_clks_available, link_clks_required;
51148b8b04cSUma Shankar 	unsigned int tu_data, tu_line, link_clks_active;
512d19b29beSVille Syrjälä 	unsigned int h_active, h_total, hblank_delta, pixel_clk;
513d19b29beSVille Syrjälä 	unsigned int fec_coeff, cdclk, vdsc_bpp;
51441ee86d6SVille Syrjälä 	unsigned int link_clk, lanes;
5152dd43144SVille Syrjälä 	unsigned int hblank_rise;
51648b8b04cSUma Shankar 
51748b8b04cSUma Shankar 	h_active = crtc_state->hw.adjusted_mode.crtc_hdisplay;
51848b8b04cSUma Shankar 	h_total = crtc_state->hw.adjusted_mode.crtc_htotal;
51948b8b04cSUma Shankar 	pixel_clk = crtc_state->hw.adjusted_mode.crtc_clock;
52048b8b04cSUma Shankar 	vdsc_bpp = crtc_state->dsc.compressed_bpp;
521d51309b4SJani Nikula 	cdclk = i915->display.cdclk.hw.cdclk;
52248b8b04cSUma Shankar 	/* fec= 0.972261, using rounding multiplier of 1000000 */
52348b8b04cSUma Shankar 	fec_coeff = 972261;
52441ee86d6SVille Syrjälä 	link_clk = crtc_state->port_clock;
52541ee86d6SVille Syrjälä 	lanes = crtc_state->lane_count;
52648b8b04cSUma Shankar 
52748b8b04cSUma Shankar 	drm_dbg_kms(&i915->drm, "h_active = %u link_clk = %u :"
52848b8b04cSUma Shankar 		    "lanes = %u vdsc_bpp = %u cdclk = %u\n",
52941ee86d6SVille Syrjälä 		    h_active, link_clk, lanes, vdsc_bpp, cdclk);
53048b8b04cSUma Shankar 
5312dd43144SVille Syrjälä 	if (WARN_ON(!link_clk || !pixel_clk || !lanes || !vdsc_bpp || !cdclk))
53211ebc232SJani Nikula 		return 0;
53311ebc232SJani Nikula 
5342dd43144SVille Syrjälä 	link_clks_available = (h_total - h_active) * link_clk / pixel_clk - 28;
5352dd43144SVille Syrjälä 	link_clks_required = DIV_ROUND_UP(192000 * h_total, 1000 * pixel_clk) * (48 / lanes + 2);
53648b8b04cSUma Shankar 
53748b8b04cSUma Shankar 	if (link_clks_available > link_clks_required)
53848b8b04cSUma Shankar 		hblank_delta = 32;
53948b8b04cSUma Shankar 	else
5402dd43144SVille Syrjälä 		hblank_delta = DIV64_U64_ROUND_UP(mul_u32_u32(5 * (link_clk + cdclk), pixel_clk),
5412dd43144SVille Syrjälä 						  mul_u32_u32(link_clk, cdclk));
54248b8b04cSUma Shankar 
5432dd43144SVille Syrjälä 	tu_data = div64_u64(mul_u32_u32(pixel_clk * vdsc_bpp * 8, 1000000),
5442dd43144SVille Syrjälä 			    mul_u32_u32(link_clk * lanes, fec_coeff));
5452dd43144SVille Syrjälä 	tu_line = div64_u64(h_active * mul_u32_u32(link_clk, fec_coeff),
5462dd43144SVille Syrjälä 			    mul_u32_u32(64 * pixel_clk, 1000000));
54748b8b04cSUma Shankar 	link_clks_active  = (tu_line - 1) * 64 + tu_data;
54848b8b04cSUma Shankar 
5492dd43144SVille Syrjälä 	hblank_rise = (link_clks_active + 6 * DIV_ROUND_UP(link_clks_active, 250) + 4) * pixel_clk / link_clk;
55048b8b04cSUma Shankar 
5512dd43144SVille Syrjälä 	return h_active - hblank_rise + hblank_delta;
55248b8b04cSUma Shankar }
55348b8b04cSUma Shankar 
5542dd43144SVille Syrjälä static unsigned int calc_samples_room(const struct intel_crtc_state *crtc_state)
55548b8b04cSUma Shankar {
55648b8b04cSUma Shankar 	unsigned int h_active, h_total, pixel_clk;
55741ee86d6SVille Syrjälä 	unsigned int link_clk, lanes;
55848b8b04cSUma Shankar 
55948b8b04cSUma Shankar 	h_active = crtc_state->hw.adjusted_mode.hdisplay;
56048b8b04cSUma Shankar 	h_total = crtc_state->hw.adjusted_mode.htotal;
56148b8b04cSUma Shankar 	pixel_clk = crtc_state->hw.adjusted_mode.clock;
56241ee86d6SVille Syrjälä 	link_clk = crtc_state->port_clock;
56341ee86d6SVille Syrjälä 	lanes = crtc_state->lane_count;
56448b8b04cSUma Shankar 
5652dd43144SVille Syrjälä 	return ((h_total - h_active) * link_clk - 12 * pixel_clk) /
5662dd43144SVille Syrjälä 		(pixel_clk * (48 / lanes + 2));
56748b8b04cSUma Shankar }
56848b8b04cSUma Shankar 
56948b8b04cSUma Shankar static void enable_audio_dsc_wa(struct intel_encoder *encoder,
57048b8b04cSUma Shankar 				const struct intel_crtc_state *crtc_state)
57148b8b04cSUma Shankar {
57248b8b04cSUma Shankar 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
57348b8b04cSUma Shankar 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
57448b8b04cSUma Shankar 	enum pipe pipe = crtc->pipe;
57511ebc232SJani Nikula 	unsigned int hblank_early_prog, samples_room;
57648b8b04cSUma Shankar 	unsigned int val;
57748b8b04cSUma Shankar 
578005e9537SMatt Roper 	if (DISPLAY_VER(i915) < 11)
57948b8b04cSUma Shankar 		return;
58048b8b04cSUma Shankar 
58148b8b04cSUma Shankar 	val = intel_de_read(i915, AUD_CONFIG_BE);
58248b8b04cSUma Shankar 
58393e7e61eSLucas De Marchi 	if (DISPLAY_VER(i915) == 11)
58448b8b04cSUma Shankar 		val |= HBLANK_EARLY_ENABLE_ICL(pipe);
585005e9537SMatt Roper 	else if (DISPLAY_VER(i915) >= 12)
58648b8b04cSUma Shankar 		val |= HBLANK_EARLY_ENABLE_TGL(pipe);
58748b8b04cSUma Shankar 
58848b8b04cSUma Shankar 	if (crtc_state->dsc.compression_enable &&
58931824c03SJani Nikula 	    crtc_state->hw.adjusted_mode.hdisplay >= 3840 &&
59031824c03SJani Nikula 	    crtc_state->hw.adjusted_mode.vdisplay >= 2160) {
59148b8b04cSUma Shankar 		/* Get hblank early enable value required */
592f4c50deeSJani Nikula 		val &= ~HBLANK_START_COUNT_MASK(pipe);
5932dd43144SVille Syrjälä 		hblank_early_prog = calc_hblank_early_prog(encoder, crtc_state);
594f4c50deeSJani Nikula 		if (hblank_early_prog < 32)
59548b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_32);
596f4c50deeSJani Nikula 		else if (hblank_early_prog < 64)
59748b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_64);
598f4c50deeSJani Nikula 		else if (hblank_early_prog < 96)
59948b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_96);
600f4c50deeSJani Nikula 		else
60148b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_128);
60248b8b04cSUma Shankar 
60348b8b04cSUma Shankar 		/* Get samples room value required */
604f4c50deeSJani Nikula 		val &= ~NUMBER_SAMPLES_PER_LINE_MASK(pipe);
6052dd43144SVille Syrjälä 		samples_room = calc_samples_room(crtc_state);
606f4c50deeSJani Nikula 		if (samples_room < 3)
60748b8b04cSUma Shankar 			val |= NUMBER_SAMPLES_PER_LINE(pipe, samples_room);
608f4c50deeSJani Nikula 		else /* Program 0 i.e "All Samples available in buffer" */
60948b8b04cSUma Shankar 			val |= NUMBER_SAMPLES_PER_LINE(pipe, 0x0);
61048b8b04cSUma Shankar 	}
61148b8b04cSUma Shankar 
61248b8b04cSUma Shankar 	intel_de_write(i915, AUD_CONFIG_BE, val);
61348b8b04cSUma Shankar }
61448b8b04cSUma Shankar 
615df0566a6SJani Nikula static void hsw_audio_codec_enable(struct intel_encoder *encoder,
616df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
617df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
618df0566a6SJani Nikula {
61946e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
620c3c5dc1dSVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
621df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
622df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
62350a4a926SVille Syrjälä 	const u32 *eld = (const u32 *)connector->eld;
6240234cda2SVille Syrjälä 	int eld_buffer_size, len, i;
625df0566a6SJani Nikula 
62646e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
627df0566a6SJani Nikula 
62848b8b04cSUma Shankar 	/* Enable Audio WA for 4k DSC usecases */
62948b8b04cSUma Shankar 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP))
63048b8b04cSUma Shankar 		enable_audio_dsc_wa(encoder, crtc_state);
63148b8b04cSUma Shankar 
632cbbda2ffSVille Syrjälä 	/* Enable audio presence detect */
6337c8d74e8SVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_PIN_ELD_CP_VLD,
634cbbda2ffSVille Syrjälä 		     0, AUDIO_OUTPUT_ENABLE(cpu_transcoder));
635cbbda2ffSVille Syrjälä 
636c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
637c3c5dc1dSVille Syrjälä 
638cbbda2ffSVille Syrjälä 	/* Invalidate ELD */
639cbbda2ffSVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_PIN_ELD_CP_VLD,
640cbbda2ffSVille Syrjälä 		     AUDIO_ELD_VALID(cpu_transcoder), 0);
641df0566a6SJani Nikula 
6427c8d74e8SVille Syrjälä 	/* Reset ELD address */
6437c8d74e8SVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_DIP_ELD_CTRL(cpu_transcoder),
6447c8d74e8SVille Syrjälä 		     IBX_ELD_ADDRESS_MASK, 0);
645df0566a6SJani Nikula 
6460234cda2SVille Syrjälä 	eld_buffer_size = hsw_eld_buffer_size(i915, cpu_transcoder);
64750a4a926SVille Syrjälä 	len = min(drm_eld_size(connector->eld) / 4, eld_buffer_size);
6481c0ab71aSVille Syrjälä 
6491c0ab71aSVille Syrjälä 	for (i = 0; i < len; i++)
65050a4a926SVille Syrjälä 		intel_de_write(i915, HSW_AUD_EDID_DATA(cpu_transcoder), eld[i]);
6510234cda2SVille Syrjälä 	for (; i < eld_buffer_size; i++)
6520234cda2SVille Syrjälä 		intel_de_write(i915, HSW_AUD_EDID_DATA(cpu_transcoder), 0);
6530234cda2SVille Syrjälä 
6540234cda2SVille Syrjälä 	drm_WARN_ON(&i915->drm,
6550234cda2SVille Syrjälä 		    (intel_de_read(i915, HSW_AUD_DIP_ELD_CTRL(cpu_transcoder)) &
6560234cda2SVille Syrjälä 		     IBX_ELD_ADDRESS_MASK) != 0);
657df0566a6SJani Nikula 
658df0566a6SJani Nikula 	/* ELD valid */
6597c8d74e8SVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_PIN_ELD_CP_VLD,
6607c8d74e8SVille Syrjälä 		     0, AUDIO_ELD_VALID(cpu_transcoder));
661df0566a6SJani Nikula 
662df0566a6SJani Nikula 	/* Enable timestamps */
663df0566a6SJani Nikula 	hsw_audio_config_update(encoder, crtc_state);
664df0566a6SJani Nikula 
66546e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
666df0566a6SJani Nikula }
667df0566a6SJani Nikula 
668669d7fd6SVille Syrjälä struct ilk_audio_regs {
669669d7fd6SVille Syrjälä 	i915_reg_t hdmiw_hdmiedid, aud_config, aud_cntl_st, aud_cntrl_st2;
670669d7fd6SVille Syrjälä };
671669d7fd6SVille Syrjälä 
672669d7fd6SVille Syrjälä static void ilk_audio_regs_init(struct drm_i915_private *i915,
673669d7fd6SVille Syrjälä 				enum pipe pipe,
674669d7fd6SVille Syrjälä 				struct ilk_audio_regs *regs)
675669d7fd6SVille Syrjälä {
676669d7fd6SVille Syrjälä 	if (HAS_PCH_IBX(i915)) {
677669d7fd6SVille Syrjälä 		regs->hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
678669d7fd6SVille Syrjälä 		regs->aud_config = IBX_AUD_CFG(pipe);
679669d7fd6SVille Syrjälä 		regs->aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
680669d7fd6SVille Syrjälä 		regs->aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
681669d7fd6SVille Syrjälä 	} else if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) {
682669d7fd6SVille Syrjälä 		regs->hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
683669d7fd6SVille Syrjälä 		regs->aud_config = VLV_AUD_CFG(pipe);
684669d7fd6SVille Syrjälä 		regs->aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
685669d7fd6SVille Syrjälä 		regs->aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
686669d7fd6SVille Syrjälä 	} else {
687669d7fd6SVille Syrjälä 		regs->hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
688669d7fd6SVille Syrjälä 		regs->aud_config = CPT_AUD_CFG(pipe);
689669d7fd6SVille Syrjälä 		regs->aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
690669d7fd6SVille Syrjälä 		regs->aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
691669d7fd6SVille Syrjälä 	}
692669d7fd6SVille Syrjälä }
693669d7fd6SVille Syrjälä 
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 {
69846e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
6992225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
700df0566a6SJani Nikula 	enum port port = encoder->port;
7017c8d74e8SVille Syrjälä 	enum pipe pipe = crtc->pipe;
702669d7fd6SVille Syrjälä 	struct ilk_audio_regs regs;
703df0566a6SJani Nikula 
70446e61ee4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, port == PORT_A))
705df0566a6SJani Nikula 		return;
706df0566a6SJani Nikula 
707669d7fd6SVille Syrjälä 	ilk_audio_regs_init(i915, pipe, &regs);
708df0566a6SJani Nikula 
7099f4a5125SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
7109f4a5125SVille Syrjälä 
711df0566a6SJani Nikula 	/* Disable timestamps */
7127c8d74e8SVille Syrjälä 	intel_de_rmw(i915, regs.aud_config,
7137c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_VALUE_INDEX |
7147c8d74e8SVille Syrjälä 		     AUD_CONFIG_UPPER_N_MASK |
7157c8d74e8SVille Syrjälä 		     AUD_CONFIG_LOWER_N_MASK,
7167c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_PROG_ENABLE |
7177c8d74e8SVille Syrjälä 		     (intel_crtc_has_dp_encoder(old_crtc_state) ?
7187c8d74e8SVille Syrjälä 		      AUD_CONFIG_N_VALUE_INDEX : 0));
719df0566a6SJani Nikula 
720df0566a6SJani Nikula 	/* Invalidate ELD */
7217c8d74e8SVille Syrjälä 	intel_de_rmw(i915, regs.aud_cntrl_st2,
7227c8d74e8SVille Syrjälä 		     IBX_ELD_VALID(port), 0);
7239f4a5125SVille Syrjälä 
7249f4a5125SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
725c3c5dc1dSVille Syrjälä 
726c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
727c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
728df0566a6SJani Nikula }
729df0566a6SJani Nikula 
730df0566a6SJani Nikula static void ilk_audio_codec_enable(struct intel_encoder *encoder,
731df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
732df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
733df0566a6SJani Nikula {
73446e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
7352225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
736df0566a6SJani Nikula 	enum port port = encoder->port;
7377c8d74e8SVille Syrjälä 	enum pipe pipe = crtc->pipe;
738669d7fd6SVille Syrjälä 	struct ilk_audio_regs regs;
739df0566a6SJani Nikula 
74046e61ee4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, port == PORT_A))
741df0566a6SJani Nikula 		return;
742df0566a6SJani Nikula 
743c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
744df0566a6SJani Nikula 
745669d7fd6SVille Syrjälä 	ilk_audio_regs_init(i915, pipe, &regs);
746df0566a6SJani Nikula 
7479f4a5125SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
748df0566a6SJani Nikula 
749df0566a6SJani Nikula 	/* Invalidate ELD */
7507c8d74e8SVille Syrjälä 	intel_de_rmw(i915, regs.aud_cntrl_st2,
7517c8d74e8SVille Syrjälä 		     IBX_ELD_VALID(port), 0);
752df0566a6SJani Nikula 
753*343cb0f9SVille Syrjälä 	/*
754*343cb0f9SVille Syrjälä 	 * The audio componenent is used to convey the ELD
755*343cb0f9SVille Syrjälä 	 * instead using of the hardware ELD buffer.
756*343cb0f9SVille Syrjälä 	 */
757df0566a6SJani Nikula 
758df0566a6SJani Nikula 	/* Enable timestamps */
7597c8d74e8SVille Syrjälä 	intel_de_rmw(i915, regs.aud_config,
7607c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_VALUE_INDEX |
7617c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_PROG_ENABLE |
7627c8d74e8SVille Syrjälä 		     AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK,
7637c8d74e8SVille Syrjälä 		     (intel_crtc_has_dp_encoder(crtc_state) ?
7647c8d74e8SVille Syrjälä 		      AUD_CONFIG_N_VALUE_INDEX :
7657c8d74e8SVille Syrjälä 		      audio_config_hdmi_pixel_clock(crtc_state)));
7669f4a5125SVille Syrjälä 
7679f4a5125SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
768df0566a6SJani Nikula }
769df0566a6SJani Nikula 
7708853750dSVinod Govindapillai void intel_audio_sdp_split_update(struct intel_encoder *encoder,
7718853750dSVinod Govindapillai 				  const struct intel_crtc_state *crtc_state)
7728853750dSVinod Govindapillai {
7738853750dSVinod Govindapillai 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
7748853750dSVinod Govindapillai 	enum transcoder trans = crtc_state->cpu_transcoder;
7758853750dSVinod Govindapillai 
7768853750dSVinod Govindapillai 	if (HAS_DP20(i915))
7778853750dSVinod Govindapillai 		intel_de_rmw(i915, AUD_DP_2DOT0_CTRL(trans), AUD_ENABLE_SDP_SPLIT,
7788853750dSVinod Govindapillai 			     crtc_state->sdp_split_enable ? AUD_ENABLE_SDP_SPLIT : 0);
7798853750dSVinod Govindapillai }
7808853750dSVinod Govindapillai 
781df0566a6SJani Nikula /**
782df0566a6SJani Nikula  * intel_audio_codec_enable - Enable the audio codec for HD audio
783df0566a6SJani Nikula  * @encoder: encoder on which to enable audio
784df0566a6SJani Nikula  * @crtc_state: pointer to the current crtc state.
785df0566a6SJani Nikula  * @conn_state: pointer to the current connector state.
786df0566a6SJani Nikula  *
787df0566a6SJani Nikula  * The enable sequences may only be performed after enabling the transcoder and
788df0566a6SJani Nikula  * port, and after completed link training.
789df0566a6SJani Nikula  */
790df0566a6SJani Nikula void intel_audio_codec_enable(struct intel_encoder *encoder,
791df0566a6SJani Nikula 			      const struct intel_crtc_state *crtc_state,
792df0566a6SJani Nikula 			      const struct drm_connector_state *conn_state)
793df0566a6SJani Nikula {
79446e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
79546e61ee4SVille Syrjälä 	struct i915_audio_component *acomp = i915->display.audio.component;
7962225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
797df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
798df0566a6SJani Nikula 	const struct drm_display_mode *adjusted_mode =
7991326a92cSMaarten Lankhorst 		&crtc_state->hw.adjusted_mode;
800df0566a6SJani Nikula 	enum port port = encoder->port;
801df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
802df0566a6SJani Nikula 
803179db7c1SJani Nikula 	if (!crtc_state->has_audio)
804179db7c1SJani Nikula 		return;
805179db7c1SJani Nikula 
80646e61ee4SVille Syrjälä 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s][ENCODER:%d:%s] Enable audio codec on pipe %c, %u bytes ELD\n",
8071f31e35fSJani Nikula 		    connector->base.id, connector->name,
8081f31e35fSJani Nikula 		    encoder->base.base.id, encoder->base.name,
809945ae909SJani Nikula 		    pipe_name(pipe), drm_eld_size(connector->eld));
8101f31e35fSJani Nikula 
811df0566a6SJani Nikula 	/* FIXME precompute the ELD in .compute_config() */
812df0566a6SJani Nikula 	if (!connector->eld[0])
81346e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm,
81463855149SWambui Karuga 			    "Bogus ELD on [CONNECTOR:%d:%s]\n",
815df0566a6SJani Nikula 			    connector->base.id, connector->name);
816df0566a6SJani Nikula 
817df0566a6SJani Nikula 	connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
818df0566a6SJani Nikula 
81946e61ee4SVille Syrjälä 	if (i915->display.funcs.audio)
82046e61ee4SVille Syrjälä 		i915->display.funcs.audio->audio_codec_enable(encoder,
821df0566a6SJani Nikula 							      crtc_state,
822df0566a6SJani Nikula 							      conn_state);
823df0566a6SJani Nikula 
82446e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
825df0566a6SJani Nikula 	encoder->audio_connector = connector;
826df0566a6SJani Nikula 
827df0566a6SJani Nikula 	/* referred in audio callbacks */
82846e61ee4SVille Syrjälä 	i915->display.audio.encoder_map[pipe] = encoder;
82946e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
830df0566a6SJani Nikula 
831df0566a6SJani Nikula 	if (acomp && acomp->base.audio_ops &&
832df0566a6SJani Nikula 	    acomp->base.audio_ops->pin_eld_notify) {
833df0566a6SJani Nikula 		/* audio drivers expect pipe = -1 to indicate Non-MST cases */
834df0566a6SJani Nikula 		if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
835df0566a6SJani Nikula 			pipe = -1;
836df0566a6SJani Nikula 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
837df0566a6SJani Nikula 						      (int)port, (int)pipe);
838df0566a6SJani Nikula 	}
839df0566a6SJani Nikula 
84046e61ee4SVille Syrjälä 	intel_lpe_audio_notify(i915, pipe, port, connector->eld,
841df0566a6SJani Nikula 			       crtc_state->port_clock,
842df0566a6SJani Nikula 			       intel_crtc_has_dp_encoder(crtc_state));
843df0566a6SJani Nikula }
844df0566a6SJani Nikula 
845df0566a6SJani Nikula /**
846df0566a6SJani Nikula  * intel_audio_codec_disable - Disable the audio codec for HD audio
847df0566a6SJani Nikula  * @encoder: encoder on which to disable audio
848df0566a6SJani Nikula  * @old_crtc_state: pointer to the old crtc state.
849df0566a6SJani Nikula  * @old_conn_state: pointer to the old connector state.
850df0566a6SJani Nikula  *
851df0566a6SJani Nikula  * The disable sequences must be performed before disabling the transcoder or
852df0566a6SJani Nikula  * port.
853df0566a6SJani Nikula  */
854df0566a6SJani Nikula void intel_audio_codec_disable(struct intel_encoder *encoder,
855df0566a6SJani Nikula 			       const struct intel_crtc_state *old_crtc_state,
856df0566a6SJani Nikula 			       const struct drm_connector_state *old_conn_state)
857df0566a6SJani Nikula {
85846e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
85946e61ee4SVille Syrjälä 	struct i915_audio_component *acomp = i915->display.audio.component;
8602225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
8611f31e35fSJani Nikula 	struct drm_connector *connector = old_conn_state->connector;
862df0566a6SJani Nikula 	enum port port = encoder->port;
863df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
864df0566a6SJani Nikula 
865179db7c1SJani Nikula 	if (!old_crtc_state->has_audio)
866179db7c1SJani Nikula 		return;
867179db7c1SJani Nikula 
86846e61ee4SVille Syrjälä 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s][ENCODER:%d:%s] Disable audio codec on pipe %c\n",
8691f31e35fSJani Nikula 		    connector->base.id, connector->name,
870945ae909SJani Nikula 		    encoder->base.base.id, encoder->base.name, pipe_name(pipe));
8711f31e35fSJani Nikula 
87246e61ee4SVille Syrjälä 	if (i915->display.funcs.audio)
87346e61ee4SVille Syrjälä 		i915->display.funcs.audio->audio_codec_disable(encoder,
874df0566a6SJani Nikula 							       old_crtc_state,
875df0566a6SJani Nikula 							       old_conn_state);
876df0566a6SJani Nikula 
87746e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
878df0566a6SJani Nikula 	encoder->audio_connector = NULL;
87946e61ee4SVille Syrjälä 	i915->display.audio.encoder_map[pipe] = NULL;
88046e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
881df0566a6SJani Nikula 
882df0566a6SJani Nikula 	if (acomp && acomp->base.audio_ops &&
883df0566a6SJani Nikula 	    acomp->base.audio_ops->pin_eld_notify) {
884df0566a6SJani Nikula 		/* audio drivers expect pipe = -1 to indicate Non-MST cases */
885df0566a6SJani Nikula 		if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
886df0566a6SJani Nikula 			pipe = -1;
887df0566a6SJani Nikula 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
888df0566a6SJani Nikula 						      (int)port, (int)pipe);
889df0566a6SJani Nikula 	}
890df0566a6SJani Nikula 
89146e61ee4SVille Syrjälä 	intel_lpe_audio_notify(i915, pipe, port, NULL, 0, false);
892df0566a6SJani Nikula }
893df0566a6SJani Nikula 
8940a108bcaSDave Airlie static const struct intel_audio_funcs g4x_audio_funcs = {
8950a108bcaSDave Airlie 	.audio_codec_enable = g4x_audio_codec_enable,
8960a108bcaSDave Airlie 	.audio_codec_disable = g4x_audio_codec_disable,
8970a108bcaSDave Airlie };
8980a108bcaSDave Airlie 
8990a108bcaSDave Airlie static const struct intel_audio_funcs ilk_audio_funcs = {
9000a108bcaSDave Airlie 	.audio_codec_enable = ilk_audio_codec_enable,
9010a108bcaSDave Airlie 	.audio_codec_disable = ilk_audio_codec_disable,
9020a108bcaSDave Airlie };
9030a108bcaSDave Airlie 
9040a108bcaSDave Airlie static const struct intel_audio_funcs hsw_audio_funcs = {
9050a108bcaSDave Airlie 	.audio_codec_enable = hsw_audio_codec_enable,
9060a108bcaSDave Airlie 	.audio_codec_disable = hsw_audio_codec_disable,
9070a108bcaSDave Airlie };
9080a108bcaSDave Airlie 
909df0566a6SJani Nikula /**
910f47a0e35SJani Nikula  * intel_audio_hooks_init - Set up chip specific audio hooks
91146e61ee4SVille Syrjälä  * @i915: device private
912df0566a6SJani Nikula  */
91346e61ee4SVille Syrjälä void intel_audio_hooks_init(struct drm_i915_private *i915)
914df0566a6SJani Nikula {
91546e61ee4SVille Syrjälä 	if (IS_G4X(i915))
91646e61ee4SVille Syrjälä 		i915->display.funcs.audio = &g4x_audio_funcs;
91746e61ee4SVille Syrjälä 	else if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
91846e61ee4SVille Syrjälä 		i915->display.funcs.audio = &ilk_audio_funcs;
91946e61ee4SVille Syrjälä 	else if (IS_HASWELL(i915) || DISPLAY_VER(i915) >= 8)
92046e61ee4SVille Syrjälä 		i915->display.funcs.audio = &hsw_audio_funcs;
92146e61ee4SVille Syrjälä 	else if (HAS_PCH_SPLIT(i915))
92246e61ee4SVille Syrjälä 		i915->display.funcs.audio = &ilk_audio_funcs;
923df0566a6SJani Nikula }
924df0566a6SJani Nikula 
925112a87c4SKai Vehmanen struct aud_ts_cdclk_m_n {
926112a87c4SKai Vehmanen 	u8 m;
927112a87c4SKai Vehmanen 	u16 n;
928112a87c4SKai Vehmanen };
929112a87c4SKai Vehmanen 
930112a87c4SKai Vehmanen void intel_audio_cdclk_change_pre(struct drm_i915_private *i915)
931112a87c4SKai Vehmanen {
932112a87c4SKai Vehmanen 	if (DISPLAY_VER(i915) >= 13)
933112a87c4SKai Vehmanen 		intel_de_rmw(i915, AUD_TS_CDCLK_M, AUD_TS_CDCLK_M_EN, 0);
934112a87c4SKai Vehmanen }
935112a87c4SKai Vehmanen 
936112a87c4SKai Vehmanen static void get_aud_ts_cdclk_m_n(int refclk, int cdclk, struct aud_ts_cdclk_m_n *aud_ts)
937112a87c4SKai Vehmanen {
938112a87c4SKai Vehmanen 	if (refclk == 24000)
939112a87c4SKai Vehmanen 		aud_ts->m = 12;
940112a87c4SKai Vehmanen 	else
941112a87c4SKai Vehmanen 		aud_ts->m = 15;
942112a87c4SKai Vehmanen 
943112a87c4SKai Vehmanen 	aud_ts->n = cdclk * aud_ts->m / 24000;
944112a87c4SKai Vehmanen }
945112a87c4SKai Vehmanen 
946112a87c4SKai Vehmanen void intel_audio_cdclk_change_post(struct drm_i915_private *i915)
947112a87c4SKai Vehmanen {
948112a87c4SKai Vehmanen 	struct aud_ts_cdclk_m_n aud_ts;
949112a87c4SKai Vehmanen 
950112a87c4SKai Vehmanen 	if (DISPLAY_VER(i915) >= 13) {
951d51309b4SJani Nikula 		get_aud_ts_cdclk_m_n(i915->display.cdclk.hw.ref, i915->display.cdclk.hw.cdclk, &aud_ts);
952112a87c4SKai Vehmanen 
953112a87c4SKai Vehmanen 		intel_de_write(i915, AUD_TS_CDCLK_N, aud_ts.n);
954112a87c4SKai Vehmanen 		intel_de_write(i915, AUD_TS_CDCLK_M, aud_ts.m | AUD_TS_CDCLK_M_EN);
955112a87c4SKai Vehmanen 		drm_dbg_kms(&i915->drm, "aud_ts_cdclk set to M=%u, N=%u\n", aud_ts.m, aud_ts.n);
956112a87c4SKai Vehmanen 	}
957112a87c4SKai Vehmanen }
958112a87c4SKai Vehmanen 
95928a30b45SVille Syrjälä static int glk_force_audio_cdclk_commit(struct intel_atomic_state *state,
96008e3ed3aSChris Wilson 					struct intel_crtc *crtc,
96128a30b45SVille Syrjälä 					bool enable)
96228a30b45SVille Syrjälä {
96328a30b45SVille Syrjälä 	struct intel_cdclk_state *cdclk_state;
96428a30b45SVille Syrjälä 	int ret;
96528a30b45SVille Syrjälä 
96628a30b45SVille Syrjälä 	/* need to hold at least one crtc lock for the global state */
96728a30b45SVille Syrjälä 	ret = drm_modeset_lock(&crtc->base.mutex, state->base.acquire_ctx);
96828a30b45SVille Syrjälä 	if (ret)
96928a30b45SVille Syrjälä 		return ret;
97028a30b45SVille Syrjälä 
97128a30b45SVille Syrjälä 	cdclk_state = intel_atomic_get_cdclk_state(state);
97228a30b45SVille Syrjälä 	if (IS_ERR(cdclk_state))
97328a30b45SVille Syrjälä 		return PTR_ERR(cdclk_state);
97428a30b45SVille Syrjälä 
97528a30b45SVille Syrjälä 	cdclk_state->force_min_cdclk = enable ? 2 * 96000 : 0;
97628a30b45SVille Syrjälä 
97728a30b45SVille Syrjälä 	return drm_atomic_commit(&state->base);
97828a30b45SVille Syrjälä }
97928a30b45SVille Syrjälä 
98046e61ee4SVille Syrjälä static void glk_force_audio_cdclk(struct drm_i915_private *i915,
981df0566a6SJani Nikula 				  bool enable)
982df0566a6SJani Nikula {
983df0566a6SJani Nikula 	struct drm_modeset_acquire_ctx ctx;
984df0566a6SJani Nikula 	struct drm_atomic_state *state;
98508e3ed3aSChris Wilson 	struct intel_crtc *crtc;
986df0566a6SJani Nikula 	int ret;
987df0566a6SJani Nikula 
98846e61ee4SVille Syrjälä 	crtc = intel_first_crtc(i915);
98908e3ed3aSChris Wilson 	if (!crtc)
99008e3ed3aSChris Wilson 		return;
99108e3ed3aSChris Wilson 
992df0566a6SJani Nikula 	drm_modeset_acquire_init(&ctx, 0);
99346e61ee4SVille Syrjälä 	state = drm_atomic_state_alloc(&i915->drm);
99446e61ee4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !state))
995df0566a6SJani Nikula 		return;
996df0566a6SJani Nikula 
997df0566a6SJani Nikula 	state->acquire_ctx = &ctx;
998df0566a6SJani Nikula 
999df0566a6SJani Nikula retry:
100008e3ed3aSChris Wilson 	ret = glk_force_audio_cdclk_commit(to_intel_atomic_state(state), crtc,
100108e3ed3aSChris Wilson 					   enable);
1002df0566a6SJani Nikula 	if (ret == -EDEADLK) {
1003df0566a6SJani Nikula 		drm_atomic_state_clear(state);
1004df0566a6SJani Nikula 		drm_modeset_backoff(&ctx);
1005df0566a6SJani Nikula 		goto retry;
1006df0566a6SJani Nikula 	}
1007df0566a6SJani Nikula 
100846e61ee4SVille Syrjälä 	drm_WARN_ON(&i915->drm, ret);
1009df0566a6SJani Nikula 
1010df0566a6SJani Nikula 	drm_atomic_state_put(state);
1011df0566a6SJani Nikula 
1012df0566a6SJani Nikula 	drm_modeset_drop_locks(&ctx);
1013df0566a6SJani Nikula 	drm_modeset_acquire_fini(&ctx);
1014df0566a6SJani Nikula }
1015df0566a6SJani Nikula 
1016df0566a6SJani Nikula static unsigned long i915_audio_component_get_power(struct device *kdev)
1017df0566a6SJani Nikula {
101846e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1019df0566a6SJani Nikula 	intel_wakeref_t ret;
1020df0566a6SJani Nikula 
1021df0566a6SJani Nikula 	/* Catch potential impedance mismatches before they occur! */
1022df0566a6SJani Nikula 	BUILD_BUG_ON(sizeof(intel_wakeref_t) > sizeof(unsigned long));
1023df0566a6SJani Nikula 
102446e61ee4SVille Syrjälä 	ret = intel_display_power_get(i915, POWER_DOMAIN_AUDIO_PLAYBACK);
1025df0566a6SJani Nikula 
102646e61ee4SVille Syrjälä 	if (i915->display.audio.power_refcount++ == 0) {
102746e61ee4SVille Syrjälä 		if (DISPLAY_VER(i915) >= 9) {
102846e61ee4SVille Syrjälä 			intel_de_write(i915, AUD_FREQ_CNTRL,
102946e61ee4SVille Syrjälä 				       i915->display.audio.freq_cntrl);
103046e61ee4SVille Syrjälä 			drm_dbg_kms(&i915->drm,
103163855149SWambui Karuga 				    "restored AUD_FREQ_CNTRL to 0x%x\n",
103246e61ee4SVille Syrjälä 				    i915->display.audio.freq_cntrl);
103387c16945SKai Vehmanen 		}
103487c16945SKai Vehmanen 
103587c16945SKai Vehmanen 		/* Force CDCLK to 2*BCLK as long as we need audio powered. */
103646e61ee4SVille Syrjälä 		if (IS_GEMINILAKE(i915))
103746e61ee4SVille Syrjälä 			glk_force_audio_cdclk(i915, true);
10381580d3cdSKai Vehmanen 
103946e61ee4SVille Syrjälä 		if (DISPLAY_VER(i915) >= 10)
10407c8d74e8SVille Syrjälä 			intel_de_rmw(i915, AUD_PIN_BUF_CTL,
10417c8d74e8SVille Syrjälä 				     0, AUD_PIN_BUF_ENABLE);
104287c16945SKai Vehmanen 	}
1043df0566a6SJani Nikula 
1044df0566a6SJani Nikula 	return ret;
1045df0566a6SJani Nikula }
1046df0566a6SJani Nikula 
1047df0566a6SJani Nikula static void i915_audio_component_put_power(struct device *kdev,
1048df0566a6SJani Nikula 					   unsigned long cookie)
1049df0566a6SJani Nikula {
105046e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1051df0566a6SJani Nikula 
1052df0566a6SJani Nikula 	/* Stop forcing CDCLK to 2*BCLK if no need for audio to be powered. */
105346e61ee4SVille Syrjälä 	if (--i915->display.audio.power_refcount == 0)
105446e61ee4SVille Syrjälä 		if (IS_GEMINILAKE(i915))
105546e61ee4SVille Syrjälä 			glk_force_audio_cdclk(i915, false);
1056df0566a6SJani Nikula 
105746e61ee4SVille Syrjälä 	intel_display_power_put(i915, POWER_DOMAIN_AUDIO_PLAYBACK, cookie);
1058df0566a6SJani Nikula }
1059df0566a6SJani Nikula 
1060df0566a6SJani Nikula static void i915_audio_component_codec_wake_override(struct device *kdev,
1061df0566a6SJani Nikula 						     bool enable)
1062df0566a6SJani Nikula {
106346e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1064df0566a6SJani Nikula 	unsigned long cookie;
1065df0566a6SJani Nikula 
106646e61ee4SVille Syrjälä 	if (DISPLAY_VER(i915) < 9)
1067df0566a6SJani Nikula 		return;
1068df0566a6SJani Nikula 
1069df0566a6SJani Nikula 	cookie = i915_audio_component_get_power(kdev);
1070df0566a6SJani Nikula 
1071df0566a6SJani Nikula 	/*
1072df0566a6SJani Nikula 	 * Enable/disable generating the codec wake signal, overriding the
1073df0566a6SJani Nikula 	 * internal logic to generate the codec wake to controller.
1074df0566a6SJani Nikula 	 */
10757c8d74e8SVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_CHICKENBIT,
10767c8d74e8SVille Syrjälä 		     SKL_AUD_CODEC_WAKE_SIGNAL, 0);
1077df0566a6SJani Nikula 	usleep_range(1000, 1500);
1078df0566a6SJani Nikula 
1079df0566a6SJani Nikula 	if (enable) {
10807c8d74e8SVille Syrjälä 		intel_de_rmw(i915, HSW_AUD_CHICKENBIT,
10817c8d74e8SVille Syrjälä 			     0, SKL_AUD_CODEC_WAKE_SIGNAL);
1082df0566a6SJani Nikula 		usleep_range(1000, 1500);
1083df0566a6SJani Nikula 	}
1084df0566a6SJani Nikula 
1085df0566a6SJani Nikula 	i915_audio_component_put_power(kdev, cookie);
1086df0566a6SJani Nikula }
1087df0566a6SJani Nikula 
1088df0566a6SJani Nikula /* Get CDCLK in kHz  */
1089df0566a6SJani Nikula static int i915_audio_component_get_cdclk_freq(struct device *kdev)
1090df0566a6SJani Nikula {
109146e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1092df0566a6SJani Nikula 
109346e61ee4SVille Syrjälä 	if (drm_WARN_ON_ONCE(&i915->drm, !HAS_DDI(i915)))
1094df0566a6SJani Nikula 		return -ENODEV;
1095df0566a6SJani Nikula 
109646e61ee4SVille Syrjälä 	return i915->display.cdclk.hw.cdclk;
1097df0566a6SJani Nikula }
1098df0566a6SJani Nikula 
1099df0566a6SJani Nikula /*
1100df0566a6SJani Nikula  * get the intel_encoder according to the parameter port and pipe
1101df0566a6SJani Nikula  * intel_encoder is saved by the index of pipe
1102ca3cfb9dSJani Nikula  * MST & (pipe >= 0): return the audio.encoder_map[pipe],
1103df0566a6SJani Nikula  *   when port is matched
1104df0566a6SJani Nikula  * MST & (pipe < 0): this is invalid
1105df0566a6SJani Nikula  * Non-MST & (pipe >= 0): only pipe = 0 (the first device entry)
1106df0566a6SJani Nikula  *   will get the right intel_encoder with port matched
1107df0566a6SJani Nikula  * Non-MST & (pipe < 0): get the right intel_encoder with port matched
1108df0566a6SJani Nikula  */
110946e61ee4SVille Syrjälä static struct intel_encoder *get_saved_enc(struct drm_i915_private *i915,
1110df0566a6SJani Nikula 					   int port, int pipe)
1111df0566a6SJani Nikula {
1112df0566a6SJani Nikula 	/* MST */
1113df0566a6SJani Nikula 	if (pipe >= 0) {
1114734d06d2SVille Syrjälä 		struct intel_encoder *encoder;
1115734d06d2SVille Syrjälä 
111646e61ee4SVille Syrjälä 		if (drm_WARN_ON(&i915->drm,
111746e61ee4SVille Syrjälä 				pipe >= ARRAY_SIZE(i915->display.audio.encoder_map)))
1118df0566a6SJani Nikula 			return NULL;
1119df0566a6SJani Nikula 
112046e61ee4SVille Syrjälä 		encoder = i915->display.audio.encoder_map[pipe];
1121df0566a6SJani Nikula 		/*
1122df0566a6SJani Nikula 		 * when bootup, audio driver may not know it is
1123df0566a6SJani Nikula 		 * MST or not. So it will poll all the port & pipe
1124df0566a6SJani Nikula 		 * combinations
1125df0566a6SJani Nikula 		 */
1126734d06d2SVille Syrjälä 		if (encoder && encoder->port == port &&
1127df0566a6SJani Nikula 		    encoder->type == INTEL_OUTPUT_DP_MST)
1128df0566a6SJani Nikula 			return encoder;
1129df0566a6SJani Nikula 	}
1130df0566a6SJani Nikula 
1131df0566a6SJani Nikula 	/* Non-MST */
1132df0566a6SJani Nikula 	if (pipe > 0)
1133df0566a6SJani Nikula 		return NULL;
1134df0566a6SJani Nikula 
113546e61ee4SVille Syrjälä 	for_each_pipe(i915, pipe) {
1136734d06d2SVille Syrjälä 		struct intel_encoder *encoder;
1137734d06d2SVille Syrjälä 
113846e61ee4SVille Syrjälä 		encoder = i915->display.audio.encoder_map[pipe];
1139df0566a6SJani Nikula 
1140734d06d2SVille Syrjälä 		if (encoder && encoder->port == port &&
1141734d06d2SVille Syrjälä 		    encoder->type != INTEL_OUTPUT_DP_MST)
1142df0566a6SJani Nikula 			return encoder;
1143df0566a6SJani Nikula 	}
1144df0566a6SJani Nikula 
1145df0566a6SJani Nikula 	return NULL;
1146df0566a6SJani Nikula }
1147df0566a6SJani Nikula 
1148df0566a6SJani Nikula static int i915_audio_component_sync_audio_rate(struct device *kdev, int port,
1149df0566a6SJani Nikula 						int pipe, int rate)
1150df0566a6SJani Nikula {
115146e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
115246e61ee4SVille Syrjälä 	struct i915_audio_component *acomp = i915->display.audio.component;
1153df0566a6SJani Nikula 	struct intel_encoder *encoder;
1154df0566a6SJani Nikula 	struct intel_crtc *crtc;
1155df0566a6SJani Nikula 	unsigned long cookie;
1156df0566a6SJani Nikula 	int err = 0;
1157df0566a6SJani Nikula 
115846e61ee4SVille Syrjälä 	if (!HAS_DDI(i915))
1159df0566a6SJani Nikula 		return 0;
1160df0566a6SJani Nikula 
1161df0566a6SJani Nikula 	cookie = i915_audio_component_get_power(kdev);
116246e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
1163df0566a6SJani Nikula 
1164df0566a6SJani Nikula 	/* 1. get the pipe */
116546e61ee4SVille Syrjälä 	encoder = get_saved_enc(i915, port, pipe);
1166df0566a6SJani Nikula 	if (!encoder || !encoder->base.crtc) {
116746e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "Not valid for port %c\n",
116863855149SWambui Karuga 			    port_name(port));
1169df0566a6SJani Nikula 		err = -ENODEV;
1170df0566a6SJani Nikula 		goto unlock;
1171df0566a6SJani Nikula 	}
1172df0566a6SJani Nikula 
1173df0566a6SJani Nikula 	crtc = to_intel_crtc(encoder->base.crtc);
1174df0566a6SJani Nikula 
1175df0566a6SJani Nikula 	/* port must be valid now, otherwise the pipe will be invalid */
1176df0566a6SJani Nikula 	acomp->aud_sample_rate[port] = rate;
1177df0566a6SJani Nikula 
1178df0566a6SJani Nikula 	hsw_audio_config_update(encoder, crtc->config);
1179df0566a6SJani Nikula 
1180df0566a6SJani Nikula  unlock:
118146e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
1182df0566a6SJani Nikula 	i915_audio_component_put_power(kdev, cookie);
1183df0566a6SJani Nikula 	return err;
1184df0566a6SJani Nikula }
1185df0566a6SJani Nikula 
1186df0566a6SJani Nikula static int i915_audio_component_get_eld(struct device *kdev, int port,
1187df0566a6SJani Nikula 					int pipe, bool *enabled,
1188df0566a6SJani Nikula 					unsigned char *buf, int max_bytes)
1189df0566a6SJani Nikula {
119046e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1191df0566a6SJani Nikula 	struct intel_encoder *intel_encoder;
1192df0566a6SJani Nikula 	const u8 *eld;
1193df0566a6SJani Nikula 	int ret = -EINVAL;
1194df0566a6SJani Nikula 
119546e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
1196df0566a6SJani Nikula 
119746e61ee4SVille Syrjälä 	intel_encoder = get_saved_enc(i915, port, pipe);
1198df0566a6SJani Nikula 	if (!intel_encoder) {
119946e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "Not valid for port %c\n",
120063855149SWambui Karuga 			    port_name(port));
120146e61ee4SVille Syrjälä 		mutex_unlock(&i915->display.audio.mutex);
1202df0566a6SJani Nikula 		return ret;
1203df0566a6SJani Nikula 	}
1204df0566a6SJani Nikula 
1205df0566a6SJani Nikula 	ret = 0;
1206df0566a6SJani Nikula 	*enabled = intel_encoder->audio_connector != NULL;
1207df0566a6SJani Nikula 	if (*enabled) {
1208df0566a6SJani Nikula 		eld = intel_encoder->audio_connector->eld;
1209df0566a6SJani Nikula 		ret = drm_eld_size(eld);
1210df0566a6SJani Nikula 		memcpy(buf, eld, min(max_bytes, ret));
1211df0566a6SJani Nikula 	}
1212df0566a6SJani Nikula 
121346e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
1214df0566a6SJani Nikula 	return ret;
1215df0566a6SJani Nikula }
1216df0566a6SJani Nikula 
1217df0566a6SJani Nikula static const struct drm_audio_component_ops i915_audio_component_ops = {
1218df0566a6SJani Nikula 	.owner		= THIS_MODULE,
1219df0566a6SJani Nikula 	.get_power	= i915_audio_component_get_power,
1220df0566a6SJani Nikula 	.put_power	= i915_audio_component_put_power,
1221df0566a6SJani Nikula 	.codec_wake_override = i915_audio_component_codec_wake_override,
1222df0566a6SJani Nikula 	.get_cdclk_freq	= i915_audio_component_get_cdclk_freq,
1223df0566a6SJani Nikula 	.sync_audio_rate = i915_audio_component_sync_audio_rate,
1224df0566a6SJani Nikula 	.get_eld	= i915_audio_component_get_eld,
1225df0566a6SJani Nikula };
1226df0566a6SJani Nikula 
1227df0566a6SJani Nikula static int i915_audio_component_bind(struct device *i915_kdev,
1228df0566a6SJani Nikula 				     struct device *hda_kdev, void *data)
1229df0566a6SJani Nikula {
1230df0566a6SJani Nikula 	struct i915_audio_component *acomp = data;
123146e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(i915_kdev);
1232df0566a6SJani Nikula 	int i;
1233df0566a6SJani Nikula 
123446e61ee4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, acomp->base.ops || acomp->base.dev))
1235df0566a6SJani Nikula 		return -EEXIST;
1236df0566a6SJani Nikula 
123746e61ee4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm,
12389a3b466bSPankaj Bharadiya 			!device_link_add(hda_kdev, i915_kdev,
12399a3b466bSPankaj Bharadiya 					 DL_FLAG_STATELESS)))
1240df0566a6SJani Nikula 		return -ENOMEM;
1241df0566a6SJani Nikula 
124246e61ee4SVille Syrjälä 	drm_modeset_lock_all(&i915->drm);
1243df0566a6SJani Nikula 	acomp->base.ops = &i915_audio_component_ops;
1244df0566a6SJani Nikula 	acomp->base.dev = i915_kdev;
1245df0566a6SJani Nikula 	BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
1246df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
1247df0566a6SJani Nikula 		acomp->aud_sample_rate[i] = 0;
124846e61ee4SVille Syrjälä 	i915->display.audio.component = acomp;
124946e61ee4SVille Syrjälä 	drm_modeset_unlock_all(&i915->drm);
1250df0566a6SJani Nikula 
1251df0566a6SJani Nikula 	return 0;
1252df0566a6SJani Nikula }
1253df0566a6SJani Nikula 
1254df0566a6SJani Nikula static void i915_audio_component_unbind(struct device *i915_kdev,
1255df0566a6SJani Nikula 					struct device *hda_kdev, void *data)
1256df0566a6SJani Nikula {
1257df0566a6SJani Nikula 	struct i915_audio_component *acomp = data;
125846e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(i915_kdev);
1259df0566a6SJani Nikula 
126046e61ee4SVille Syrjälä 	drm_modeset_lock_all(&i915->drm);
1261df0566a6SJani Nikula 	acomp->base.ops = NULL;
1262df0566a6SJani Nikula 	acomp->base.dev = NULL;
126346e61ee4SVille Syrjälä 	i915->display.audio.component = NULL;
126446e61ee4SVille Syrjälä 	drm_modeset_unlock_all(&i915->drm);
1265df0566a6SJani Nikula 
1266df0566a6SJani Nikula 	device_link_remove(hda_kdev, i915_kdev);
1267b4ed131dSJani Nikula 
126846e61ee4SVille Syrjälä 	if (i915->display.audio.power_refcount)
126946e61ee4SVille Syrjälä 		drm_err(&i915->drm, "audio power refcount %d after unbind\n",
127046e61ee4SVille Syrjälä 			i915->display.audio.power_refcount);
1271df0566a6SJani Nikula }
1272df0566a6SJani Nikula 
1273df0566a6SJani Nikula static const struct component_ops i915_audio_component_bind_ops = {
1274df0566a6SJani Nikula 	.bind	= i915_audio_component_bind,
1275df0566a6SJani Nikula 	.unbind	= i915_audio_component_unbind,
1276df0566a6SJani Nikula };
1277df0566a6SJani Nikula 
1278989634fbSKai Vehmanen #define AUD_FREQ_TMODE_SHIFT	14
1279989634fbSKai Vehmanen #define AUD_FREQ_4T		0
1280989634fbSKai Vehmanen #define AUD_FREQ_8T		(2 << AUD_FREQ_TMODE_SHIFT)
1281989634fbSKai Vehmanen #define AUD_FREQ_PULLCLKS(x)	(((x) & 0x3) << 11)
1282989634fbSKai Vehmanen #define AUD_FREQ_BCLK_96M	BIT(4)
1283989634fbSKai Vehmanen 
1284989634fbSKai Vehmanen #define AUD_FREQ_GEN12          (AUD_FREQ_8T | AUD_FREQ_PULLCLKS(0) | AUD_FREQ_BCLK_96M)
1285989634fbSKai Vehmanen #define AUD_FREQ_TGL_BROKEN     (AUD_FREQ_8T | AUD_FREQ_PULLCLKS(2) | AUD_FREQ_BCLK_96M)
1286989634fbSKai Vehmanen 
1287df0566a6SJani Nikula /**
1288df0566a6SJani Nikula  * i915_audio_component_init - initialize and register the audio component
128946e61ee4SVille Syrjälä  * @i915: i915 device instance
1290df0566a6SJani Nikula  *
1291df0566a6SJani Nikula  * This will register with the component framework a child component which
1292df0566a6SJani Nikula  * will bind dynamically to the snd_hda_intel driver's corresponding master
1293df0566a6SJani Nikula  * component when the latter is registered. During binding the child
1294df0566a6SJani Nikula  * initializes an instance of struct i915_audio_component which it receives
1295df0566a6SJani Nikula  * from the master. The master can then start to use the interface defined by
1296df0566a6SJani Nikula  * this struct. Each side can break the binding at any point by deregistering
1297df0566a6SJani Nikula  * its own component after which each side's component unbind callback is
1298df0566a6SJani Nikula  * called.
1299df0566a6SJani Nikula  *
1300df0566a6SJani Nikula  * We ignore any error during registration and continue with reduced
1301df0566a6SJani Nikula  * functionality (i.e. without HDMI audio).
1302df0566a6SJani Nikula  */
130346e61ee4SVille Syrjälä static void i915_audio_component_init(struct drm_i915_private *i915)
1304df0566a6SJani Nikula {
1305989634fbSKai Vehmanen 	u32 aud_freq, aud_freq_init;
1306df0566a6SJani Nikula 	int ret;
1307df0566a6SJani Nikula 
130846e61ee4SVille Syrjälä 	ret = component_add_typed(i915->drm.dev,
1309df0566a6SJani Nikula 				  &i915_audio_component_bind_ops,
1310df0566a6SJani Nikula 				  I915_COMPONENT_AUDIO);
1311df0566a6SJani Nikula 	if (ret < 0) {
131246e61ee4SVille Syrjälä 		drm_err(&i915->drm,
131363855149SWambui Karuga 			"failed to add audio component (%d)\n", ret);
1314df0566a6SJani Nikula 		/* continue with reduced functionality */
1315df0566a6SJani Nikula 		return;
1316df0566a6SJani Nikula 	}
1317df0566a6SJani Nikula 
131846e61ee4SVille Syrjälä 	if (DISPLAY_VER(i915) >= 9) {
131946e61ee4SVille Syrjälä 		aud_freq_init = intel_de_read(i915, AUD_FREQ_CNTRL);
1320989634fbSKai Vehmanen 
132146e61ee4SVille Syrjälä 		if (DISPLAY_VER(i915) >= 12)
1322989634fbSKai Vehmanen 			aud_freq = AUD_FREQ_GEN12;
1323989634fbSKai Vehmanen 		else
1324989634fbSKai Vehmanen 			aud_freq = aud_freq_init;
1325989634fbSKai Vehmanen 
1326c6b40ee3SKai-Heng Feng 		/* use BIOS provided value for TGL and RKL unless it is a known bad value */
132746e61ee4SVille Syrjälä 		if ((IS_TIGERLAKE(i915) || IS_ROCKETLAKE(i915)) &&
1328c6b40ee3SKai-Heng Feng 		    aud_freq_init != AUD_FREQ_TGL_BROKEN)
1329989634fbSKai Vehmanen 			aud_freq = aud_freq_init;
1330989634fbSKai Vehmanen 
133146e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "use AUD_FREQ_CNTRL of 0x%x (init value 0x%x)\n",
1332989634fbSKai Vehmanen 			    aud_freq, aud_freq_init);
1333989634fbSKai Vehmanen 
133446e61ee4SVille Syrjälä 		i915->display.audio.freq_cntrl = aud_freq;
133587c16945SKai Vehmanen 	}
133687c16945SKai Vehmanen 
1337112a87c4SKai Vehmanen 	/* init with current cdclk */
133846e61ee4SVille Syrjälä 	intel_audio_cdclk_change_post(i915);
1339112a87c4SKai Vehmanen 
134046e61ee4SVille Syrjälä 	i915->display.audio.component_registered = true;
1341df0566a6SJani Nikula }
1342df0566a6SJani Nikula 
1343df0566a6SJani Nikula /**
1344df0566a6SJani Nikula  * i915_audio_component_cleanup - deregister the audio component
134546e61ee4SVille Syrjälä  * @i915: i915 device instance
1346df0566a6SJani Nikula  *
1347df0566a6SJani Nikula  * Deregisters the audio component, breaking any existing binding to the
1348df0566a6SJani Nikula  * corresponding snd_hda_intel driver's master component.
1349df0566a6SJani Nikula  */
135046e61ee4SVille Syrjälä static void i915_audio_component_cleanup(struct drm_i915_private *i915)
1351df0566a6SJani Nikula {
135246e61ee4SVille Syrjälä 	if (!i915->display.audio.component_registered)
1353df0566a6SJani Nikula 		return;
1354df0566a6SJani Nikula 
135546e61ee4SVille Syrjälä 	component_del(i915->drm.dev, &i915_audio_component_bind_ops);
135646e61ee4SVille Syrjälä 	i915->display.audio.component_registered = false;
1357df0566a6SJani Nikula }
1358df0566a6SJani Nikula 
1359df0566a6SJani Nikula /**
1360df0566a6SJani Nikula  * intel_audio_init() - Initialize the audio driver either using
1361df0566a6SJani Nikula  * component framework or using lpe audio bridge
136246e61ee4SVille Syrjälä  * @i915: the i915 drm device private data
1363df0566a6SJani Nikula  *
1364df0566a6SJani Nikula  */
136546e61ee4SVille Syrjälä void intel_audio_init(struct drm_i915_private *i915)
1366df0566a6SJani Nikula {
136746e61ee4SVille Syrjälä 	if (intel_lpe_audio_init(i915) < 0)
136846e61ee4SVille Syrjälä 		i915_audio_component_init(i915);
1369df0566a6SJani Nikula }
1370df0566a6SJani Nikula 
1371df0566a6SJani Nikula /**
1372df0566a6SJani Nikula  * intel_audio_deinit() - deinitialize the audio driver
137346e61ee4SVille Syrjälä  * @i915: the i915 drm device private data
1374df0566a6SJani Nikula  *
1375df0566a6SJani Nikula  */
137646e61ee4SVille Syrjälä void intel_audio_deinit(struct drm_i915_private *i915)
1377df0566a6SJani Nikula {
137846e61ee4SVille Syrjälä 	if (i915->display.audio.lpe.platdev != NULL)
137946e61ee4SVille Syrjälä 		intel_lpe_audio_teardown(i915);
1380df0566a6SJani Nikula 	else
138146e61ee4SVille Syrjälä 		i915_audio_component_cleanup(i915);
1382df0566a6SJani Nikula }
1383