xref: /linux/drivers/gpu/drm/i915/display/intel_audio.c (revision 7c8d74e8131217e928fb92904cac5362e348744f)
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);
322df0566a6SJani Nikula 
323df0566a6SJani Nikula 	/* Invalidate ELD */
324*7c8d74e8SVille Syrjälä 	intel_de_rmw(i915, G4X_AUD_CNTL_ST,
325*7c8d74e8SVille Syrjälä 		     G4X_ELD_VALID, 0);
326df0566a6SJani Nikula }
327df0566a6SJani Nikula 
328df0566a6SJani Nikula static void g4x_audio_codec_enable(struct intel_encoder *encoder,
329df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
330df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
331df0566a6SJani Nikula {
33246e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
333df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
33450a4a926SVille Syrjälä 	const u32 *eld = (const u32 *)connector->eld;
3350234cda2SVille Syrjälä 	int eld_buffer_size, len, i;
336df0566a6SJani Nikula 
337*7c8d74e8SVille Syrjälä 	intel_de_rmw(i915, G4X_AUD_CNTL_ST,
338*7c8d74e8SVille Syrjälä 		     G4X_ELD_VALID | G4X_ELD_ADDRESS_MASK, 0);
339df0566a6SJani Nikula 
3400234cda2SVille Syrjälä 	eld_buffer_size = g4x_eld_buffer_size(i915);
34150a4a926SVille Syrjälä 	len = min(drm_eld_size(connector->eld) / 4, eld_buffer_size);
3421c0ab71aSVille Syrjälä 
343df0566a6SJani Nikula 	for (i = 0; i < len; i++)
34450a4a926SVille Syrjälä 		intel_de_write(i915, G4X_HDMIW_HDMIEDID, eld[i]);
3450234cda2SVille Syrjälä 	for (; i < eld_buffer_size; i++)
3460234cda2SVille Syrjälä 		intel_de_write(i915, G4X_HDMIW_HDMIEDID, 0);
3470234cda2SVille Syrjälä 
3480234cda2SVille Syrjälä 	drm_WARN_ON(&i915->drm,
3490234cda2SVille Syrjälä 		    (intel_de_read(i915, G4X_AUD_CNTL_ST) & G4X_ELD_ADDRESS_MASK) != 0);
350df0566a6SJani Nikula 
351*7c8d74e8SVille Syrjälä 	intel_de_rmw(i915, G4X_AUD_CNTL_ST,
352*7c8d74e8SVille Syrjälä 		     0, G4X_ELD_VALID);
353df0566a6SJani Nikula }
354df0566a6SJani Nikula 
355df0566a6SJani Nikula static void
356df0566a6SJani Nikula hsw_dp_audio_config_update(struct intel_encoder *encoder,
357df0566a6SJani Nikula 			   const struct intel_crtc_state *crtc_state)
358df0566a6SJani Nikula {
35946e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
36046e61ee4SVille Syrjälä 	struct i915_audio_component *acomp = i915->display.audio.component;
361df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
362df0566a6SJani Nikula 	enum port port = encoder->port;
363df0566a6SJani Nikula 	const struct dp_aud_n_m *nm;
364df0566a6SJani Nikula 	int rate;
365df0566a6SJani Nikula 	u32 tmp;
366df0566a6SJani Nikula 
367df0566a6SJani Nikula 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
368df0566a6SJani Nikula 	nm = audio_config_dp_get_n_m(crtc_state, rate);
369df0566a6SJani Nikula 	if (nm)
37046e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "using Maud %u, Naud %u\n", nm->m,
37163855149SWambui Karuga 			    nm->n);
372df0566a6SJani Nikula 	else
37346e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "using automatic Maud, Naud\n");
374df0566a6SJani Nikula 
37546e61ee4SVille Syrjälä 	tmp = intel_de_read(i915, HSW_AUD_CFG(cpu_transcoder));
376df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
377df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
378df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
379df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_VALUE_INDEX;
380df0566a6SJani Nikula 
381df0566a6SJani Nikula 	if (nm) {
382df0566a6SJani Nikula 		tmp &= ~AUD_CONFIG_N_MASK;
383df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N(nm->n);
384df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
385df0566a6SJani Nikula 	}
386df0566a6SJani Nikula 
38746e61ee4SVille Syrjälä 	intel_de_write(i915, HSW_AUD_CFG(cpu_transcoder), tmp);
388df0566a6SJani Nikula 
38946e61ee4SVille Syrjälä 	tmp = intel_de_read(i915, HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
390df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_M_MASK;
391df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
392df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
393df0566a6SJani Nikula 
394df0566a6SJani Nikula 	if (nm) {
395df0566a6SJani Nikula 		tmp |= nm->m;
396df0566a6SJani Nikula 		tmp |= AUD_M_CTS_M_VALUE_INDEX;
397df0566a6SJani Nikula 		tmp |= AUD_M_CTS_M_PROG_ENABLE;
398df0566a6SJani Nikula 	}
399df0566a6SJani Nikula 
40046e61ee4SVille Syrjälä 	intel_de_write(i915, HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
401df0566a6SJani Nikula }
402df0566a6SJani Nikula 
403df0566a6SJani Nikula static void
404df0566a6SJani Nikula hsw_hdmi_audio_config_update(struct intel_encoder *encoder,
405df0566a6SJani Nikula 			     const struct intel_crtc_state *crtc_state)
406df0566a6SJani Nikula {
40746e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
40846e61ee4SVille Syrjälä 	struct i915_audio_component *acomp = i915->display.audio.component;
409df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
410df0566a6SJani Nikula 	enum port port = encoder->port;
411df0566a6SJani Nikula 	int n, rate;
412df0566a6SJani Nikula 	u32 tmp;
413df0566a6SJani Nikula 
414df0566a6SJani Nikula 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
415df0566a6SJani Nikula 
41646e61ee4SVille Syrjälä 	tmp = intel_de_read(i915, HSW_AUD_CFG(cpu_transcoder));
417df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
418df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
419df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
420df0566a6SJani Nikula 	tmp |= audio_config_hdmi_pixel_clock(crtc_state);
421df0566a6SJani Nikula 
422df0566a6SJani Nikula 	n = audio_config_hdmi_get_n(crtc_state, rate);
423df0566a6SJani Nikula 	if (n != 0) {
42446e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "using N %d\n", n);
425df0566a6SJani Nikula 
426df0566a6SJani Nikula 		tmp &= ~AUD_CONFIG_N_MASK;
427df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N(n);
428df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
429df0566a6SJani Nikula 	} else {
43046e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "using automatic N\n");
431df0566a6SJani Nikula 	}
432df0566a6SJani Nikula 
43346e61ee4SVille Syrjälä 	intel_de_write(i915, HSW_AUD_CFG(cpu_transcoder), tmp);
434df0566a6SJani Nikula 
435df0566a6SJani Nikula 	/*
436df0566a6SJani Nikula 	 * Let's disable "Enable CTS or M Prog bit"
437df0566a6SJani Nikula 	 * and let HW calculate the value
438df0566a6SJani Nikula 	 */
43946e61ee4SVille Syrjälä 	tmp = intel_de_read(i915, HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
440df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
441df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
44246e61ee4SVille Syrjälä 	intel_de_write(i915, HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
443df0566a6SJani Nikula }
444df0566a6SJani Nikula 
445df0566a6SJani Nikula static void
446df0566a6SJani Nikula hsw_audio_config_update(struct intel_encoder *encoder,
447df0566a6SJani Nikula 			const struct intel_crtc_state *crtc_state)
448df0566a6SJani Nikula {
449df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state))
450df0566a6SJani Nikula 		hsw_dp_audio_config_update(encoder, crtc_state);
451df0566a6SJani Nikula 	else
452df0566a6SJani Nikula 		hsw_hdmi_audio_config_update(encoder, crtc_state);
453df0566a6SJani Nikula }
454df0566a6SJani Nikula 
4551c0ab71aSVille Syrjälä /* ELD buffer size in dwords */
4561c0ab71aSVille Syrjälä static int hsw_eld_buffer_size(struct drm_i915_private *i915,
4571c0ab71aSVille Syrjälä 			       enum transcoder cpu_transcoder)
4581c0ab71aSVille Syrjälä {
4591c0ab71aSVille Syrjälä 	u32 tmp;
4601c0ab71aSVille Syrjälä 
4611c0ab71aSVille Syrjälä 	tmp = intel_de_read(i915, HSW_AUD_DIP_ELD_CTRL(cpu_transcoder));
4621c0ab71aSVille Syrjälä 
4631c0ab71aSVille Syrjälä 	return REG_FIELD_GET(IBX_ELD_BUFFER_SIZE_MASK, tmp);
4641c0ab71aSVille Syrjälä }
4651c0ab71aSVille Syrjälä 
466df0566a6SJani Nikula static void hsw_audio_codec_disable(struct intel_encoder *encoder,
467df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
468df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
469df0566a6SJani Nikula {
47046e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
471df0566a6SJani Nikula 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
472df0566a6SJani Nikula 
47346e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
474df0566a6SJani Nikula 
475df0566a6SJani Nikula 	/* Disable timestamps */
476*7c8d74e8SVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_CFG(cpu_transcoder),
477*7c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_VALUE_INDEX |
478*7c8d74e8SVille Syrjälä 		     AUD_CONFIG_UPPER_N_MASK |
479*7c8d74e8SVille Syrjälä 		     AUD_CONFIG_LOWER_N_MASK,
480*7c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_PROG_ENABLE |
481*7c8d74e8SVille Syrjälä 		     (intel_crtc_has_dp_encoder(old_crtc_state) ?
482*7c8d74e8SVille Syrjälä 		      AUD_CONFIG_N_VALUE_INDEX : 0));
483df0566a6SJani Nikula 
484*7c8d74e8SVille Syrjälä 	/* Disable audio presence detect, invalidate ELD */
485*7c8d74e8SVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_PIN_ELD_CP_VLD,
486*7c8d74e8SVille Syrjälä 		     AUDIO_ELD_VALID(cpu_transcoder) |
487*7c8d74e8SVille Syrjälä 		     AUDIO_OUTPUT_ENABLE(cpu_transcoder), 0);
488df0566a6SJani Nikula 
48946e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
490df0566a6SJani Nikula }
491df0566a6SJani Nikula 
4922dd43144SVille Syrjälä static unsigned int calc_hblank_early_prog(struct intel_encoder *encoder,
49348b8b04cSUma Shankar 					   const struct intel_crtc_state *crtc_state)
49448b8b04cSUma Shankar {
49548b8b04cSUma Shankar 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
49648b8b04cSUma Shankar 	unsigned int link_clks_available, link_clks_required;
49748b8b04cSUma Shankar 	unsigned int tu_data, tu_line, link_clks_active;
498d19b29beSVille Syrjälä 	unsigned int h_active, h_total, hblank_delta, pixel_clk;
499d19b29beSVille Syrjälä 	unsigned int fec_coeff, cdclk, vdsc_bpp;
50041ee86d6SVille Syrjälä 	unsigned int link_clk, lanes;
5012dd43144SVille Syrjälä 	unsigned int hblank_rise;
50248b8b04cSUma Shankar 
50348b8b04cSUma Shankar 	h_active = crtc_state->hw.adjusted_mode.crtc_hdisplay;
50448b8b04cSUma Shankar 	h_total = crtc_state->hw.adjusted_mode.crtc_htotal;
50548b8b04cSUma Shankar 	pixel_clk = crtc_state->hw.adjusted_mode.crtc_clock;
50648b8b04cSUma Shankar 	vdsc_bpp = crtc_state->dsc.compressed_bpp;
507d51309b4SJani Nikula 	cdclk = i915->display.cdclk.hw.cdclk;
50848b8b04cSUma Shankar 	/* fec= 0.972261, using rounding multiplier of 1000000 */
50948b8b04cSUma Shankar 	fec_coeff = 972261;
51041ee86d6SVille Syrjälä 	link_clk = crtc_state->port_clock;
51141ee86d6SVille Syrjälä 	lanes = crtc_state->lane_count;
51248b8b04cSUma Shankar 
51348b8b04cSUma Shankar 	drm_dbg_kms(&i915->drm, "h_active = %u link_clk = %u :"
51448b8b04cSUma Shankar 		    "lanes = %u vdsc_bpp = %u cdclk = %u\n",
51541ee86d6SVille Syrjälä 		    h_active, link_clk, lanes, vdsc_bpp, cdclk);
51648b8b04cSUma Shankar 
5172dd43144SVille Syrjälä 	if (WARN_ON(!link_clk || !pixel_clk || !lanes || !vdsc_bpp || !cdclk))
51811ebc232SJani Nikula 		return 0;
51911ebc232SJani Nikula 
5202dd43144SVille Syrjälä 	link_clks_available = (h_total - h_active) * link_clk / pixel_clk - 28;
5212dd43144SVille Syrjälä 	link_clks_required = DIV_ROUND_UP(192000 * h_total, 1000 * pixel_clk) * (48 / lanes + 2);
52248b8b04cSUma Shankar 
52348b8b04cSUma Shankar 	if (link_clks_available > link_clks_required)
52448b8b04cSUma Shankar 		hblank_delta = 32;
52548b8b04cSUma Shankar 	else
5262dd43144SVille Syrjälä 		hblank_delta = DIV64_U64_ROUND_UP(mul_u32_u32(5 * (link_clk + cdclk), pixel_clk),
5272dd43144SVille Syrjälä 						  mul_u32_u32(link_clk, cdclk));
52848b8b04cSUma Shankar 
5292dd43144SVille Syrjälä 	tu_data = div64_u64(mul_u32_u32(pixel_clk * vdsc_bpp * 8, 1000000),
5302dd43144SVille Syrjälä 			    mul_u32_u32(link_clk * lanes, fec_coeff));
5312dd43144SVille Syrjälä 	tu_line = div64_u64(h_active * mul_u32_u32(link_clk, fec_coeff),
5322dd43144SVille Syrjälä 			    mul_u32_u32(64 * pixel_clk, 1000000));
53348b8b04cSUma Shankar 	link_clks_active  = (tu_line - 1) * 64 + tu_data;
53448b8b04cSUma Shankar 
5352dd43144SVille Syrjälä 	hblank_rise = (link_clks_active + 6 * DIV_ROUND_UP(link_clks_active, 250) + 4) * pixel_clk / link_clk;
53648b8b04cSUma Shankar 
5372dd43144SVille Syrjälä 	return h_active - hblank_rise + hblank_delta;
53848b8b04cSUma Shankar }
53948b8b04cSUma Shankar 
5402dd43144SVille Syrjälä static unsigned int calc_samples_room(const struct intel_crtc_state *crtc_state)
54148b8b04cSUma Shankar {
54248b8b04cSUma Shankar 	unsigned int h_active, h_total, pixel_clk;
54341ee86d6SVille Syrjälä 	unsigned int link_clk, lanes;
54448b8b04cSUma Shankar 
54548b8b04cSUma Shankar 	h_active = crtc_state->hw.adjusted_mode.hdisplay;
54648b8b04cSUma Shankar 	h_total = crtc_state->hw.adjusted_mode.htotal;
54748b8b04cSUma Shankar 	pixel_clk = crtc_state->hw.adjusted_mode.clock;
54841ee86d6SVille Syrjälä 	link_clk = crtc_state->port_clock;
54941ee86d6SVille Syrjälä 	lanes = crtc_state->lane_count;
55048b8b04cSUma Shankar 
5512dd43144SVille Syrjälä 	return ((h_total - h_active) * link_clk - 12 * pixel_clk) /
5522dd43144SVille Syrjälä 		(pixel_clk * (48 / lanes + 2));
55348b8b04cSUma Shankar }
55448b8b04cSUma Shankar 
55548b8b04cSUma Shankar static void enable_audio_dsc_wa(struct intel_encoder *encoder,
55648b8b04cSUma Shankar 				const struct intel_crtc_state *crtc_state)
55748b8b04cSUma Shankar {
55848b8b04cSUma Shankar 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
55948b8b04cSUma Shankar 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
56048b8b04cSUma Shankar 	enum pipe pipe = crtc->pipe;
56111ebc232SJani Nikula 	unsigned int hblank_early_prog, samples_room;
56248b8b04cSUma Shankar 	unsigned int val;
56348b8b04cSUma Shankar 
564005e9537SMatt Roper 	if (DISPLAY_VER(i915) < 11)
56548b8b04cSUma Shankar 		return;
56648b8b04cSUma Shankar 
56748b8b04cSUma Shankar 	val = intel_de_read(i915, AUD_CONFIG_BE);
56848b8b04cSUma Shankar 
56993e7e61eSLucas De Marchi 	if (DISPLAY_VER(i915) == 11)
57048b8b04cSUma Shankar 		val |= HBLANK_EARLY_ENABLE_ICL(pipe);
571005e9537SMatt Roper 	else if (DISPLAY_VER(i915) >= 12)
57248b8b04cSUma Shankar 		val |= HBLANK_EARLY_ENABLE_TGL(pipe);
57348b8b04cSUma Shankar 
57448b8b04cSUma Shankar 	if (crtc_state->dsc.compression_enable &&
57531824c03SJani Nikula 	    crtc_state->hw.adjusted_mode.hdisplay >= 3840 &&
57631824c03SJani Nikula 	    crtc_state->hw.adjusted_mode.vdisplay >= 2160) {
57748b8b04cSUma Shankar 		/* Get hblank early enable value required */
578f4c50deeSJani Nikula 		val &= ~HBLANK_START_COUNT_MASK(pipe);
5792dd43144SVille Syrjälä 		hblank_early_prog = calc_hblank_early_prog(encoder, crtc_state);
580f4c50deeSJani Nikula 		if (hblank_early_prog < 32)
58148b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_32);
582f4c50deeSJani Nikula 		else if (hblank_early_prog < 64)
58348b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_64);
584f4c50deeSJani Nikula 		else if (hblank_early_prog < 96)
58548b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_96);
586f4c50deeSJani Nikula 		else
58748b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_128);
58848b8b04cSUma Shankar 
58948b8b04cSUma Shankar 		/* Get samples room value required */
590f4c50deeSJani Nikula 		val &= ~NUMBER_SAMPLES_PER_LINE_MASK(pipe);
5912dd43144SVille Syrjälä 		samples_room = calc_samples_room(crtc_state);
592f4c50deeSJani Nikula 		if (samples_room < 3)
59348b8b04cSUma Shankar 			val |= NUMBER_SAMPLES_PER_LINE(pipe, samples_room);
594f4c50deeSJani Nikula 		else /* Program 0 i.e "All Samples available in buffer" */
59548b8b04cSUma Shankar 			val |= NUMBER_SAMPLES_PER_LINE(pipe, 0x0);
59648b8b04cSUma Shankar 	}
59748b8b04cSUma Shankar 
59848b8b04cSUma Shankar 	intel_de_write(i915, AUD_CONFIG_BE, val);
59948b8b04cSUma Shankar }
60048b8b04cSUma Shankar 
601df0566a6SJani Nikula static void hsw_audio_codec_enable(struct intel_encoder *encoder,
602df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
603df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
604df0566a6SJani Nikula {
60546e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
606df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
607df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
60850a4a926SVille Syrjälä 	const u32 *eld = (const u32 *)connector->eld;
6090234cda2SVille Syrjälä 	int eld_buffer_size, len, i;
610df0566a6SJani Nikula 
61146e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
612df0566a6SJani Nikula 
61348b8b04cSUma Shankar 	/* Enable Audio WA for 4k DSC usecases */
61448b8b04cSUma Shankar 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP))
61548b8b04cSUma Shankar 		enable_audio_dsc_wa(encoder, crtc_state);
61648b8b04cSUma Shankar 
617df0566a6SJani Nikula 	/* Enable audio presence detect, invalidate ELD */
618*7c8d74e8SVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_PIN_ELD_CP_VLD,
619*7c8d74e8SVille Syrjälä 		     AUDIO_ELD_VALID(cpu_transcoder),
620*7c8d74e8SVille Syrjälä 		     AUDIO_OUTPUT_ENABLE(cpu_transcoder));
621df0566a6SJani Nikula 
622df0566a6SJani Nikula 	/*
623df0566a6SJani Nikula 	 * FIXME: We're supposed to wait for vblank here, but we have vblanks
624df0566a6SJani Nikula 	 * disabled during the mode set. The proper fix would be to push the
625df0566a6SJani Nikula 	 * rest of the setup into a vblank work item, queued here, but the
626df0566a6SJani Nikula 	 * infrastructure is not there yet.
627df0566a6SJani Nikula 	 */
628df0566a6SJani Nikula 
629*7c8d74e8SVille Syrjälä 	/* Reset ELD address */
630*7c8d74e8SVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_DIP_ELD_CTRL(cpu_transcoder),
631*7c8d74e8SVille Syrjälä 		     IBX_ELD_ADDRESS_MASK, 0);
632df0566a6SJani Nikula 
6330234cda2SVille Syrjälä 	eld_buffer_size = hsw_eld_buffer_size(i915, cpu_transcoder);
63450a4a926SVille Syrjälä 	len = min(drm_eld_size(connector->eld) / 4, eld_buffer_size);
6351c0ab71aSVille Syrjälä 
6361c0ab71aSVille Syrjälä 	for (i = 0; i < len; i++)
63750a4a926SVille Syrjälä 		intel_de_write(i915, HSW_AUD_EDID_DATA(cpu_transcoder), eld[i]);
6380234cda2SVille Syrjälä 	for (; i < eld_buffer_size; i++)
6390234cda2SVille Syrjälä 		intel_de_write(i915, HSW_AUD_EDID_DATA(cpu_transcoder), 0);
6400234cda2SVille Syrjälä 
6410234cda2SVille Syrjälä 	drm_WARN_ON(&i915->drm,
6420234cda2SVille Syrjälä 		    (intel_de_read(i915, HSW_AUD_DIP_ELD_CTRL(cpu_transcoder)) &
6430234cda2SVille Syrjälä 		     IBX_ELD_ADDRESS_MASK) != 0);
644df0566a6SJani Nikula 
645df0566a6SJani Nikula 	/* ELD valid */
646*7c8d74e8SVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_PIN_ELD_CP_VLD,
647*7c8d74e8SVille Syrjälä 		     0, AUDIO_ELD_VALID(cpu_transcoder));
648df0566a6SJani Nikula 
649df0566a6SJani Nikula 	/* Enable timestamps */
650df0566a6SJani Nikula 	hsw_audio_config_update(encoder, crtc_state);
651df0566a6SJani Nikula 
65246e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
653df0566a6SJani Nikula }
654df0566a6SJani Nikula 
655669d7fd6SVille Syrjälä struct ilk_audio_regs {
656669d7fd6SVille Syrjälä 	i915_reg_t hdmiw_hdmiedid, aud_config, aud_cntl_st, aud_cntrl_st2;
657669d7fd6SVille Syrjälä };
658669d7fd6SVille Syrjälä 
659669d7fd6SVille Syrjälä static void ilk_audio_regs_init(struct drm_i915_private *i915,
660669d7fd6SVille Syrjälä 				enum pipe pipe,
661669d7fd6SVille Syrjälä 				struct ilk_audio_regs *regs)
662669d7fd6SVille Syrjälä {
663669d7fd6SVille Syrjälä 	if (HAS_PCH_IBX(i915)) {
664669d7fd6SVille Syrjälä 		regs->hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
665669d7fd6SVille Syrjälä 		regs->aud_config = IBX_AUD_CFG(pipe);
666669d7fd6SVille Syrjälä 		regs->aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
667669d7fd6SVille Syrjälä 		regs->aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
668669d7fd6SVille Syrjälä 	} else if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) {
669669d7fd6SVille Syrjälä 		regs->hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
670669d7fd6SVille Syrjälä 		regs->aud_config = VLV_AUD_CFG(pipe);
671669d7fd6SVille Syrjälä 		regs->aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
672669d7fd6SVille Syrjälä 		regs->aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
673669d7fd6SVille Syrjälä 	} else {
674669d7fd6SVille Syrjälä 		regs->hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
675669d7fd6SVille Syrjälä 		regs->aud_config = CPT_AUD_CFG(pipe);
676669d7fd6SVille Syrjälä 		regs->aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
677669d7fd6SVille Syrjälä 		regs->aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
678669d7fd6SVille Syrjälä 	}
679669d7fd6SVille Syrjälä }
680669d7fd6SVille Syrjälä 
6811c0ab71aSVille Syrjälä /* ELD buffer size in dwords */
6821c0ab71aSVille Syrjälä static int ilk_eld_buffer_size(struct drm_i915_private *i915,
6831c0ab71aSVille Syrjälä 			       enum pipe pipe)
6841c0ab71aSVille Syrjälä {
6851c0ab71aSVille Syrjälä 	struct ilk_audio_regs regs;
6861c0ab71aSVille Syrjälä 	u32 tmp;
6871c0ab71aSVille Syrjälä 
6881c0ab71aSVille Syrjälä 	ilk_audio_regs_init(i915, pipe, &regs);
6891c0ab71aSVille Syrjälä 
6901c0ab71aSVille Syrjälä 	tmp = intel_de_read(i915, regs.aud_cntl_st);
6911c0ab71aSVille Syrjälä 
6921c0ab71aSVille Syrjälä 	return REG_FIELD_GET(IBX_ELD_BUFFER_SIZE_MASK, tmp);
6931c0ab71aSVille Syrjälä }
6941c0ab71aSVille Syrjälä 
695df0566a6SJani Nikula static void ilk_audio_codec_disable(struct intel_encoder *encoder,
696df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
697df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
698df0566a6SJani Nikula {
69946e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
7002225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
701df0566a6SJani Nikula 	enum port port = encoder->port;
702*7c8d74e8SVille Syrjälä 	enum pipe pipe = crtc->pipe;
703669d7fd6SVille Syrjälä 	struct ilk_audio_regs regs;
704df0566a6SJani Nikula 
70546e61ee4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, port == PORT_A))
706df0566a6SJani Nikula 		return;
707df0566a6SJani Nikula 
708669d7fd6SVille Syrjälä 	ilk_audio_regs_init(i915, pipe, &regs);
709df0566a6SJani Nikula 
7109f4a5125SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
7119f4a5125SVille Syrjälä 
712df0566a6SJani Nikula 	/* Disable timestamps */
713*7c8d74e8SVille Syrjälä 	intel_de_rmw(i915, regs.aud_config,
714*7c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_VALUE_INDEX |
715*7c8d74e8SVille Syrjälä 		     AUD_CONFIG_UPPER_N_MASK |
716*7c8d74e8SVille Syrjälä 		     AUD_CONFIG_LOWER_N_MASK,
717*7c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_PROG_ENABLE |
718*7c8d74e8SVille Syrjälä 		     (intel_crtc_has_dp_encoder(old_crtc_state) ?
719*7c8d74e8SVille Syrjälä 		      AUD_CONFIG_N_VALUE_INDEX : 0));
720df0566a6SJani Nikula 
721df0566a6SJani Nikula 	/* Invalidate ELD */
722*7c8d74e8SVille Syrjälä 	intel_de_rmw(i915, regs.aud_cntrl_st2,
723*7c8d74e8SVille Syrjälä 		     IBX_ELD_VALID(port), 0);
7249f4a5125SVille Syrjälä 
7259f4a5125SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
726df0566a6SJani Nikula }
727df0566a6SJani Nikula 
728df0566a6SJani Nikula static void ilk_audio_codec_enable(struct intel_encoder *encoder,
729df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
730df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
731df0566a6SJani Nikula {
73246e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
7332225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
734df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
73550a4a926SVille Syrjälä 	const u32 *eld = (const u32 *)connector->eld;
736df0566a6SJani Nikula 	enum port port = encoder->port;
737*7c8d74e8SVille Syrjälä 	enum pipe pipe = crtc->pipe;
7380234cda2SVille Syrjälä 	int eld_buffer_size, len, i;
739669d7fd6SVille Syrjälä 	struct ilk_audio_regs regs;
740df0566a6SJani Nikula 
74146e61ee4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, port == PORT_A))
742df0566a6SJani Nikula 		return;
743df0566a6SJani Nikula 
744df0566a6SJani Nikula 	/*
745df0566a6SJani Nikula 	 * FIXME: We're supposed to wait for vblank here, but we have vblanks
746df0566a6SJani Nikula 	 * disabled during the mode set. The proper fix would be to push the
747df0566a6SJani Nikula 	 * rest of the setup into a vblank work item, queued here, but the
748df0566a6SJani Nikula 	 * infrastructure is not there yet.
749df0566a6SJani Nikula 	 */
750df0566a6SJani Nikula 
751669d7fd6SVille Syrjälä 	ilk_audio_regs_init(i915, pipe, &regs);
752df0566a6SJani Nikula 
7539f4a5125SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
754df0566a6SJani Nikula 
755df0566a6SJani Nikula 	/* Invalidate ELD */
756*7c8d74e8SVille Syrjälä 	intel_de_rmw(i915, regs.aud_cntrl_st2,
757*7c8d74e8SVille Syrjälä 		     IBX_ELD_VALID(port), 0);
758df0566a6SJani Nikula 
759*7c8d74e8SVille Syrjälä 	/* Reset ELD address */
760*7c8d74e8SVille Syrjälä 	intel_de_rmw(i915, regs.aud_cntl_st,
761*7c8d74e8SVille Syrjälä 		     IBX_ELD_ADDRESS_MASK, 0);
762df0566a6SJani Nikula 
7630234cda2SVille Syrjälä 	eld_buffer_size = ilk_eld_buffer_size(i915, pipe);
76450a4a926SVille Syrjälä 	len = min(drm_eld_size(connector->eld) / 4, eld_buffer_size);
7651c0ab71aSVille Syrjälä 
7661c0ab71aSVille Syrjälä 	for (i = 0; i < len; i++)
76750a4a926SVille Syrjälä 		intel_de_write(i915, regs.hdmiw_hdmiedid, eld[i]);
7680234cda2SVille Syrjälä 	for (; i < eld_buffer_size; i++)
7690234cda2SVille Syrjälä 		intel_de_write(i915, regs.hdmiw_hdmiedid, 0);
7700234cda2SVille Syrjälä 
7710234cda2SVille Syrjälä 	drm_WARN_ON(&i915->drm,
7720234cda2SVille Syrjälä 		    (intel_de_read(i915, regs.aud_cntl_st) & IBX_ELD_ADDRESS_MASK) != 0);
773df0566a6SJani Nikula 
774df0566a6SJani Nikula 	/* ELD valid */
775*7c8d74e8SVille Syrjälä 	intel_de_rmw(i915, regs.aud_cntrl_st2,
776*7c8d74e8SVille Syrjälä 		     0, IBX_ELD_VALID(port));
777df0566a6SJani Nikula 
778df0566a6SJani Nikula 	/* Enable timestamps */
779*7c8d74e8SVille Syrjälä 	intel_de_rmw(i915, regs.aud_config,
780*7c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_VALUE_INDEX |
781*7c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_PROG_ENABLE |
782*7c8d74e8SVille Syrjälä 		     AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK,
783*7c8d74e8SVille Syrjälä 		     (intel_crtc_has_dp_encoder(crtc_state) ?
784*7c8d74e8SVille Syrjälä 		      AUD_CONFIG_N_VALUE_INDEX :
785*7c8d74e8SVille Syrjälä 		      audio_config_hdmi_pixel_clock(crtc_state)));
7869f4a5125SVille Syrjälä 
7879f4a5125SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
788df0566a6SJani Nikula }
789df0566a6SJani Nikula 
790df0566a6SJani Nikula /**
791df0566a6SJani Nikula  * intel_audio_codec_enable - Enable the audio codec for HD audio
792df0566a6SJani Nikula  * @encoder: encoder on which to enable audio
793df0566a6SJani Nikula  * @crtc_state: pointer to the current crtc state.
794df0566a6SJani Nikula  * @conn_state: pointer to the current connector state.
795df0566a6SJani Nikula  *
796df0566a6SJani Nikula  * The enable sequences may only be performed after enabling the transcoder and
797df0566a6SJani Nikula  * port, and after completed link training.
798df0566a6SJani Nikula  */
799df0566a6SJani Nikula void intel_audio_codec_enable(struct intel_encoder *encoder,
800df0566a6SJani Nikula 			      const struct intel_crtc_state *crtc_state,
801df0566a6SJani Nikula 			      const struct drm_connector_state *conn_state)
802df0566a6SJani Nikula {
80346e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
80446e61ee4SVille Syrjälä 	struct i915_audio_component *acomp = i915->display.audio.component;
8052225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
806df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
807df0566a6SJani Nikula 	const struct drm_display_mode *adjusted_mode =
8081326a92cSMaarten Lankhorst 		&crtc_state->hw.adjusted_mode;
809df0566a6SJani Nikula 	enum port port = encoder->port;
810df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
811df0566a6SJani Nikula 
812179db7c1SJani Nikula 	if (!crtc_state->has_audio)
813179db7c1SJani Nikula 		return;
814179db7c1SJani Nikula 
81546e61ee4SVille Syrjälä 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s][ENCODER:%d:%s] Enable audio codec on pipe %c, %u bytes ELD\n",
8161f31e35fSJani Nikula 		    connector->base.id, connector->name,
8171f31e35fSJani Nikula 		    encoder->base.base.id, encoder->base.name,
818945ae909SJani Nikula 		    pipe_name(pipe), drm_eld_size(connector->eld));
8191f31e35fSJani Nikula 
820df0566a6SJani Nikula 	/* FIXME precompute the ELD in .compute_config() */
821df0566a6SJani Nikula 	if (!connector->eld[0])
82246e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm,
82363855149SWambui Karuga 			    "Bogus ELD on [CONNECTOR:%d:%s]\n",
824df0566a6SJani Nikula 			    connector->base.id, connector->name);
825df0566a6SJani Nikula 
826df0566a6SJani Nikula 	connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
827df0566a6SJani Nikula 
82846e61ee4SVille Syrjälä 	if (i915->display.funcs.audio)
82946e61ee4SVille Syrjälä 		i915->display.funcs.audio->audio_codec_enable(encoder,
830df0566a6SJani Nikula 								  crtc_state,
831df0566a6SJani Nikula 								  conn_state);
832df0566a6SJani Nikula 
83346e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
834df0566a6SJani Nikula 	encoder->audio_connector = connector;
835df0566a6SJani Nikula 
836df0566a6SJani Nikula 	/* referred in audio callbacks */
83746e61ee4SVille Syrjälä 	i915->display.audio.encoder_map[pipe] = encoder;
83846e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
839df0566a6SJani Nikula 
840df0566a6SJani Nikula 	if (acomp && acomp->base.audio_ops &&
841df0566a6SJani Nikula 	    acomp->base.audio_ops->pin_eld_notify) {
842df0566a6SJani Nikula 		/* audio drivers expect pipe = -1 to indicate Non-MST cases */
843df0566a6SJani Nikula 		if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
844df0566a6SJani Nikula 			pipe = -1;
845df0566a6SJani Nikula 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
846df0566a6SJani Nikula 						 (int) port, (int) pipe);
847df0566a6SJani Nikula 	}
848df0566a6SJani Nikula 
84946e61ee4SVille Syrjälä 	intel_lpe_audio_notify(i915, pipe, port, connector->eld,
850df0566a6SJani Nikula 			       crtc_state->port_clock,
851df0566a6SJani Nikula 			       intel_crtc_has_dp_encoder(crtc_state));
852df0566a6SJani Nikula }
853df0566a6SJani Nikula 
854df0566a6SJani Nikula /**
855df0566a6SJani Nikula  * intel_audio_codec_disable - Disable the audio codec for HD audio
856df0566a6SJani Nikula  * @encoder: encoder on which to disable audio
857df0566a6SJani Nikula  * @old_crtc_state: pointer to the old crtc state.
858df0566a6SJani Nikula  * @old_conn_state: pointer to the old connector state.
859df0566a6SJani Nikula  *
860df0566a6SJani Nikula  * The disable sequences must be performed before disabling the transcoder or
861df0566a6SJani Nikula  * port.
862df0566a6SJani Nikula  */
863df0566a6SJani Nikula void intel_audio_codec_disable(struct intel_encoder *encoder,
864df0566a6SJani Nikula 			       const struct intel_crtc_state *old_crtc_state,
865df0566a6SJani Nikula 			       const struct drm_connector_state *old_conn_state)
866df0566a6SJani Nikula {
86746e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
86846e61ee4SVille Syrjälä 	struct i915_audio_component *acomp = i915->display.audio.component;
8692225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
8701f31e35fSJani Nikula 	struct drm_connector *connector = old_conn_state->connector;
871df0566a6SJani Nikula 	enum port port = encoder->port;
872df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
873df0566a6SJani Nikula 
874179db7c1SJani Nikula 	if (!old_crtc_state->has_audio)
875179db7c1SJani Nikula 		return;
876179db7c1SJani Nikula 
87746e61ee4SVille Syrjälä 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s][ENCODER:%d:%s] Disable audio codec on pipe %c\n",
8781f31e35fSJani Nikula 		    connector->base.id, connector->name,
879945ae909SJani Nikula 		    encoder->base.base.id, encoder->base.name, pipe_name(pipe));
8801f31e35fSJani Nikula 
88146e61ee4SVille Syrjälä 	if (i915->display.funcs.audio)
88246e61ee4SVille Syrjälä 		i915->display.funcs.audio->audio_codec_disable(encoder,
883df0566a6SJani Nikula 								   old_crtc_state,
884df0566a6SJani Nikula 								   old_conn_state);
885df0566a6SJani Nikula 
88646e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
887df0566a6SJani Nikula 	encoder->audio_connector = NULL;
88846e61ee4SVille Syrjälä 	i915->display.audio.encoder_map[pipe] = NULL;
88946e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
890df0566a6SJani Nikula 
891df0566a6SJani Nikula 	if (acomp && acomp->base.audio_ops &&
892df0566a6SJani Nikula 	    acomp->base.audio_ops->pin_eld_notify) {
893df0566a6SJani Nikula 		/* audio drivers expect pipe = -1 to indicate Non-MST cases */
894df0566a6SJani Nikula 		if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
895df0566a6SJani Nikula 			pipe = -1;
896df0566a6SJani Nikula 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
897df0566a6SJani Nikula 						 (int) port, (int) pipe);
898df0566a6SJani Nikula 	}
899df0566a6SJani Nikula 
90046e61ee4SVille Syrjälä 	intel_lpe_audio_notify(i915, pipe, port, NULL, 0, false);
901df0566a6SJani Nikula }
902df0566a6SJani Nikula 
9030a108bcaSDave Airlie static const struct intel_audio_funcs g4x_audio_funcs = {
9040a108bcaSDave Airlie 	.audio_codec_enable = g4x_audio_codec_enable,
9050a108bcaSDave Airlie 	.audio_codec_disable = g4x_audio_codec_disable,
9060a108bcaSDave Airlie };
9070a108bcaSDave Airlie 
9080a108bcaSDave Airlie static const struct intel_audio_funcs ilk_audio_funcs = {
9090a108bcaSDave Airlie 	.audio_codec_enable = ilk_audio_codec_enable,
9100a108bcaSDave Airlie 	.audio_codec_disable = ilk_audio_codec_disable,
9110a108bcaSDave Airlie };
9120a108bcaSDave Airlie 
9130a108bcaSDave Airlie static const struct intel_audio_funcs hsw_audio_funcs = {
9140a108bcaSDave Airlie 	.audio_codec_enable = hsw_audio_codec_enable,
9150a108bcaSDave Airlie 	.audio_codec_disable = hsw_audio_codec_disable,
9160a108bcaSDave Airlie };
9170a108bcaSDave Airlie 
918df0566a6SJani Nikula /**
919f47a0e35SJani Nikula  * intel_audio_hooks_init - Set up chip specific audio hooks
92046e61ee4SVille Syrjälä  * @i915: device private
921df0566a6SJani Nikula  */
92246e61ee4SVille Syrjälä void intel_audio_hooks_init(struct drm_i915_private *i915)
923df0566a6SJani Nikula {
92446e61ee4SVille Syrjälä 	if (IS_G4X(i915))
92546e61ee4SVille Syrjälä 		i915->display.funcs.audio = &g4x_audio_funcs;
92646e61ee4SVille Syrjälä 	else if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
92746e61ee4SVille Syrjälä 		i915->display.funcs.audio = &ilk_audio_funcs;
92846e61ee4SVille Syrjälä 	else if (IS_HASWELL(i915) || DISPLAY_VER(i915) >= 8)
92946e61ee4SVille Syrjälä 		i915->display.funcs.audio = &hsw_audio_funcs;
93046e61ee4SVille Syrjälä 	else if (HAS_PCH_SPLIT(i915))
93146e61ee4SVille Syrjälä 		i915->display.funcs.audio = &ilk_audio_funcs;
932df0566a6SJani Nikula }
933df0566a6SJani Nikula 
934112a87c4SKai Vehmanen struct aud_ts_cdclk_m_n {
935112a87c4SKai Vehmanen 	u8 m;
936112a87c4SKai Vehmanen 	u16 n;
937112a87c4SKai Vehmanen };
938112a87c4SKai Vehmanen 
939112a87c4SKai Vehmanen void intel_audio_cdclk_change_pre(struct drm_i915_private *i915)
940112a87c4SKai Vehmanen {
941112a87c4SKai Vehmanen 	if (DISPLAY_VER(i915) >= 13)
942112a87c4SKai Vehmanen 		intel_de_rmw(i915, AUD_TS_CDCLK_M, AUD_TS_CDCLK_M_EN, 0);
943112a87c4SKai Vehmanen }
944112a87c4SKai Vehmanen 
945112a87c4SKai Vehmanen static void get_aud_ts_cdclk_m_n(int refclk, int cdclk, struct aud_ts_cdclk_m_n *aud_ts)
946112a87c4SKai Vehmanen {
947112a87c4SKai Vehmanen 	if (refclk == 24000)
948112a87c4SKai Vehmanen 		aud_ts->m = 12;
949112a87c4SKai Vehmanen 	else
950112a87c4SKai Vehmanen 		aud_ts->m = 15;
951112a87c4SKai Vehmanen 
952112a87c4SKai Vehmanen 	aud_ts->n = cdclk * aud_ts->m / 24000;
953112a87c4SKai Vehmanen }
954112a87c4SKai Vehmanen 
955112a87c4SKai Vehmanen void intel_audio_cdclk_change_post(struct drm_i915_private *i915)
956112a87c4SKai Vehmanen {
957112a87c4SKai Vehmanen 	struct aud_ts_cdclk_m_n aud_ts;
958112a87c4SKai Vehmanen 
959112a87c4SKai Vehmanen 	if (DISPLAY_VER(i915) >= 13) {
960d51309b4SJani Nikula 		get_aud_ts_cdclk_m_n(i915->display.cdclk.hw.ref, i915->display.cdclk.hw.cdclk, &aud_ts);
961112a87c4SKai Vehmanen 
962112a87c4SKai Vehmanen 		intel_de_write(i915, AUD_TS_CDCLK_N, aud_ts.n);
963112a87c4SKai Vehmanen 		intel_de_write(i915, AUD_TS_CDCLK_M, aud_ts.m | AUD_TS_CDCLK_M_EN);
964112a87c4SKai Vehmanen 		drm_dbg_kms(&i915->drm, "aud_ts_cdclk set to M=%u, N=%u\n", aud_ts.m, aud_ts.n);
965112a87c4SKai Vehmanen 	}
966112a87c4SKai Vehmanen }
967112a87c4SKai Vehmanen 
96828a30b45SVille Syrjälä static int glk_force_audio_cdclk_commit(struct intel_atomic_state *state,
96908e3ed3aSChris Wilson 					struct intel_crtc *crtc,
97028a30b45SVille Syrjälä 					bool enable)
97128a30b45SVille Syrjälä {
97228a30b45SVille Syrjälä 	struct intel_cdclk_state *cdclk_state;
97328a30b45SVille Syrjälä 	int ret;
97428a30b45SVille Syrjälä 
97528a30b45SVille Syrjälä 	/* need to hold at least one crtc lock for the global state */
97628a30b45SVille Syrjälä 	ret = drm_modeset_lock(&crtc->base.mutex, state->base.acquire_ctx);
97728a30b45SVille Syrjälä 	if (ret)
97828a30b45SVille Syrjälä 		return ret;
97928a30b45SVille Syrjälä 
98028a30b45SVille Syrjälä 	cdclk_state = intel_atomic_get_cdclk_state(state);
98128a30b45SVille Syrjälä 	if (IS_ERR(cdclk_state))
98228a30b45SVille Syrjälä 		return PTR_ERR(cdclk_state);
98328a30b45SVille Syrjälä 
98428a30b45SVille Syrjälä 	cdclk_state->force_min_cdclk = enable ? 2 * 96000 : 0;
98528a30b45SVille Syrjälä 
98628a30b45SVille Syrjälä 	return drm_atomic_commit(&state->base);
98728a30b45SVille Syrjälä }
98828a30b45SVille Syrjälä 
98946e61ee4SVille Syrjälä static void glk_force_audio_cdclk(struct drm_i915_private *i915,
990df0566a6SJani Nikula 				  bool enable)
991df0566a6SJani Nikula {
992df0566a6SJani Nikula 	struct drm_modeset_acquire_ctx ctx;
993df0566a6SJani Nikula 	struct drm_atomic_state *state;
99408e3ed3aSChris Wilson 	struct intel_crtc *crtc;
995df0566a6SJani Nikula 	int ret;
996df0566a6SJani Nikula 
99746e61ee4SVille Syrjälä 	crtc = intel_first_crtc(i915);
99808e3ed3aSChris Wilson 	if (!crtc)
99908e3ed3aSChris Wilson 		return;
100008e3ed3aSChris Wilson 
1001df0566a6SJani Nikula 	drm_modeset_acquire_init(&ctx, 0);
100246e61ee4SVille Syrjälä 	state = drm_atomic_state_alloc(&i915->drm);
100346e61ee4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !state))
1004df0566a6SJani Nikula 		return;
1005df0566a6SJani Nikula 
1006df0566a6SJani Nikula 	state->acquire_ctx = &ctx;
1007df0566a6SJani Nikula 
1008df0566a6SJani Nikula retry:
100908e3ed3aSChris Wilson 	ret = glk_force_audio_cdclk_commit(to_intel_atomic_state(state), crtc,
101008e3ed3aSChris Wilson 					   enable);
1011df0566a6SJani Nikula 	if (ret == -EDEADLK) {
1012df0566a6SJani Nikula 		drm_atomic_state_clear(state);
1013df0566a6SJani Nikula 		drm_modeset_backoff(&ctx);
1014df0566a6SJani Nikula 		goto retry;
1015df0566a6SJani Nikula 	}
1016df0566a6SJani Nikula 
101746e61ee4SVille Syrjälä 	drm_WARN_ON(&i915->drm, ret);
1018df0566a6SJani Nikula 
1019df0566a6SJani Nikula 	drm_atomic_state_put(state);
1020df0566a6SJani Nikula 
1021df0566a6SJani Nikula 	drm_modeset_drop_locks(&ctx);
1022df0566a6SJani Nikula 	drm_modeset_acquire_fini(&ctx);
1023df0566a6SJani Nikula }
1024df0566a6SJani Nikula 
1025df0566a6SJani Nikula static unsigned long i915_audio_component_get_power(struct device *kdev)
1026df0566a6SJani Nikula {
102746e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1028df0566a6SJani Nikula 	intel_wakeref_t ret;
1029df0566a6SJani Nikula 
1030df0566a6SJani Nikula 	/* Catch potential impedance mismatches before they occur! */
1031df0566a6SJani Nikula 	BUILD_BUG_ON(sizeof(intel_wakeref_t) > sizeof(unsigned long));
1032df0566a6SJani Nikula 
103346e61ee4SVille Syrjälä 	ret = intel_display_power_get(i915, POWER_DOMAIN_AUDIO_PLAYBACK);
1034df0566a6SJani Nikula 
103546e61ee4SVille Syrjälä 	if (i915->display.audio.power_refcount++ == 0) {
103646e61ee4SVille Syrjälä 		if (DISPLAY_VER(i915) >= 9) {
103746e61ee4SVille Syrjälä 			intel_de_write(i915, AUD_FREQ_CNTRL,
103846e61ee4SVille Syrjälä 				       i915->display.audio.freq_cntrl);
103946e61ee4SVille Syrjälä 			drm_dbg_kms(&i915->drm,
104063855149SWambui Karuga 				    "restored AUD_FREQ_CNTRL to 0x%x\n",
104146e61ee4SVille Syrjälä 				    i915->display.audio.freq_cntrl);
104287c16945SKai Vehmanen 		}
104387c16945SKai Vehmanen 
104487c16945SKai Vehmanen 		/* Force CDCLK to 2*BCLK as long as we need audio powered. */
104546e61ee4SVille Syrjälä 		if (IS_GEMINILAKE(i915))
104646e61ee4SVille Syrjälä 			glk_force_audio_cdclk(i915, true);
10471580d3cdSKai Vehmanen 
104846e61ee4SVille Syrjälä 		if (DISPLAY_VER(i915) >= 10)
1049*7c8d74e8SVille Syrjälä 			intel_de_rmw(i915, AUD_PIN_BUF_CTL,
1050*7c8d74e8SVille Syrjälä 				     0, AUD_PIN_BUF_ENABLE);
105187c16945SKai Vehmanen 	}
1052df0566a6SJani Nikula 
1053df0566a6SJani Nikula 	return ret;
1054df0566a6SJani Nikula }
1055df0566a6SJani Nikula 
1056df0566a6SJani Nikula static void i915_audio_component_put_power(struct device *kdev,
1057df0566a6SJani Nikula 					   unsigned long cookie)
1058df0566a6SJani Nikula {
105946e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1060df0566a6SJani Nikula 
1061df0566a6SJani Nikula 	/* Stop forcing CDCLK to 2*BCLK if no need for audio to be powered. */
106246e61ee4SVille Syrjälä 	if (--i915->display.audio.power_refcount == 0)
106346e61ee4SVille Syrjälä 		if (IS_GEMINILAKE(i915))
106446e61ee4SVille Syrjälä 			glk_force_audio_cdclk(i915, false);
1065df0566a6SJani Nikula 
106646e61ee4SVille Syrjälä 	intel_display_power_put(i915, POWER_DOMAIN_AUDIO_PLAYBACK, cookie);
1067df0566a6SJani Nikula }
1068df0566a6SJani Nikula 
1069df0566a6SJani Nikula static void i915_audio_component_codec_wake_override(struct device *kdev,
1070df0566a6SJani Nikula 						     bool enable)
1071df0566a6SJani Nikula {
107246e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1073df0566a6SJani Nikula 	unsigned long cookie;
1074df0566a6SJani Nikula 
107546e61ee4SVille Syrjälä 	if (DISPLAY_VER(i915) < 9)
1076df0566a6SJani Nikula 		return;
1077df0566a6SJani Nikula 
1078df0566a6SJani Nikula 	cookie = i915_audio_component_get_power(kdev);
1079df0566a6SJani Nikula 
1080df0566a6SJani Nikula 	/*
1081df0566a6SJani Nikula 	 * Enable/disable generating the codec wake signal, overriding the
1082df0566a6SJani Nikula 	 * internal logic to generate the codec wake to controller.
1083df0566a6SJani Nikula 	 */
1084*7c8d74e8SVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_CHICKENBIT,
1085*7c8d74e8SVille Syrjälä 		     SKL_AUD_CODEC_WAKE_SIGNAL, 0);
1086df0566a6SJani Nikula 	usleep_range(1000, 1500);
1087df0566a6SJani Nikula 
1088df0566a6SJani Nikula 	if (enable) {
1089*7c8d74e8SVille Syrjälä 		intel_de_rmw(i915, HSW_AUD_CHICKENBIT,
1090*7c8d74e8SVille Syrjälä 			     0, SKL_AUD_CODEC_WAKE_SIGNAL);
1091df0566a6SJani Nikula 		usleep_range(1000, 1500);
1092df0566a6SJani Nikula 	}
1093df0566a6SJani Nikula 
1094df0566a6SJani Nikula 	i915_audio_component_put_power(kdev, cookie);
1095df0566a6SJani Nikula }
1096df0566a6SJani Nikula 
1097df0566a6SJani Nikula /* Get CDCLK in kHz  */
1098df0566a6SJani Nikula static int i915_audio_component_get_cdclk_freq(struct device *kdev)
1099df0566a6SJani Nikula {
110046e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1101df0566a6SJani Nikula 
110246e61ee4SVille Syrjälä 	if (drm_WARN_ON_ONCE(&i915->drm, !HAS_DDI(i915)))
1103df0566a6SJani Nikula 		return -ENODEV;
1104df0566a6SJani Nikula 
110546e61ee4SVille Syrjälä 	return i915->display.cdclk.hw.cdclk;
1106df0566a6SJani Nikula }
1107df0566a6SJani Nikula 
1108df0566a6SJani Nikula /*
1109df0566a6SJani Nikula  * get the intel_encoder according to the parameter port and pipe
1110df0566a6SJani Nikula  * intel_encoder is saved by the index of pipe
1111ca3cfb9dSJani Nikula  * MST & (pipe >= 0): return the audio.encoder_map[pipe],
1112df0566a6SJani Nikula  *   when port is matched
1113df0566a6SJani Nikula  * MST & (pipe < 0): this is invalid
1114df0566a6SJani Nikula  * Non-MST & (pipe >= 0): only pipe = 0 (the first device entry)
1115df0566a6SJani Nikula  *   will get the right intel_encoder with port matched
1116df0566a6SJani Nikula  * Non-MST & (pipe < 0): get the right intel_encoder with port matched
1117df0566a6SJani Nikula  */
111846e61ee4SVille Syrjälä static struct intel_encoder *get_saved_enc(struct drm_i915_private *i915,
1119df0566a6SJani Nikula 					   int port, int pipe)
1120df0566a6SJani Nikula {
1121df0566a6SJani Nikula 	struct intel_encoder *encoder;
1122df0566a6SJani Nikula 
1123df0566a6SJani Nikula 	/* MST */
1124df0566a6SJani Nikula 	if (pipe >= 0) {
112546e61ee4SVille Syrjälä 		if (drm_WARN_ON(&i915->drm,
112646e61ee4SVille Syrjälä 				pipe >= ARRAY_SIZE(i915->display.audio.encoder_map)))
1127df0566a6SJani Nikula 			return NULL;
1128df0566a6SJani Nikula 
112946e61ee4SVille Syrjälä 		encoder = i915->display.audio.encoder_map[pipe];
1130df0566a6SJani Nikula 		/*
1131df0566a6SJani Nikula 		 * when bootup, audio driver may not know it is
1132df0566a6SJani Nikula 		 * MST or not. So it will poll all the port & pipe
1133df0566a6SJani Nikula 		 * combinations
1134df0566a6SJani Nikula 		 */
1135df0566a6SJani Nikula 		if (encoder != NULL && encoder->port == port &&
1136df0566a6SJani Nikula 		    encoder->type == INTEL_OUTPUT_DP_MST)
1137df0566a6SJani Nikula 			return encoder;
1138df0566a6SJani Nikula 	}
1139df0566a6SJani Nikula 
1140df0566a6SJani Nikula 	/* Non-MST */
1141df0566a6SJani Nikula 	if (pipe > 0)
1142df0566a6SJani Nikula 		return NULL;
1143df0566a6SJani Nikula 
114446e61ee4SVille Syrjälä 	for_each_pipe(i915, pipe) {
114546e61ee4SVille Syrjälä 		encoder = i915->display.audio.encoder_map[pipe];
1146df0566a6SJani Nikula 		if (encoder == NULL)
1147df0566a6SJani Nikula 			continue;
1148df0566a6SJani Nikula 
1149df0566a6SJani Nikula 		if (encoder->type == INTEL_OUTPUT_DP_MST)
1150df0566a6SJani Nikula 			continue;
1151df0566a6SJani Nikula 
1152df0566a6SJani Nikula 		if (port == encoder->port)
1153df0566a6SJani Nikula 			return encoder;
1154df0566a6SJani Nikula 	}
1155df0566a6SJani Nikula 
1156df0566a6SJani Nikula 	return NULL;
1157df0566a6SJani Nikula }
1158df0566a6SJani Nikula 
1159df0566a6SJani Nikula static int i915_audio_component_sync_audio_rate(struct device *kdev, int port,
1160df0566a6SJani Nikula 						int pipe, int rate)
1161df0566a6SJani Nikula {
116246e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
116346e61ee4SVille Syrjälä 	struct i915_audio_component *acomp = i915->display.audio.component;
1164df0566a6SJani Nikula 	struct intel_encoder *encoder;
1165df0566a6SJani Nikula 	struct intel_crtc *crtc;
1166df0566a6SJani Nikula 	unsigned long cookie;
1167df0566a6SJani Nikula 	int err = 0;
1168df0566a6SJani Nikula 
116946e61ee4SVille Syrjälä 	if (!HAS_DDI(i915))
1170df0566a6SJani Nikula 		return 0;
1171df0566a6SJani Nikula 
1172df0566a6SJani Nikula 	cookie = i915_audio_component_get_power(kdev);
117346e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
1174df0566a6SJani Nikula 
1175df0566a6SJani Nikula 	/* 1. get the pipe */
117646e61ee4SVille Syrjälä 	encoder = get_saved_enc(i915, port, pipe);
1177df0566a6SJani Nikula 	if (!encoder || !encoder->base.crtc) {
117846e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "Not valid for port %c\n",
117963855149SWambui Karuga 			    port_name(port));
1180df0566a6SJani Nikula 		err = -ENODEV;
1181df0566a6SJani Nikula 		goto unlock;
1182df0566a6SJani Nikula 	}
1183df0566a6SJani Nikula 
1184df0566a6SJani Nikula 	crtc = to_intel_crtc(encoder->base.crtc);
1185df0566a6SJani Nikula 
1186df0566a6SJani Nikula 	/* port must be valid now, otherwise the pipe will be invalid */
1187df0566a6SJani Nikula 	acomp->aud_sample_rate[port] = rate;
1188df0566a6SJani Nikula 
1189df0566a6SJani Nikula 	hsw_audio_config_update(encoder, crtc->config);
1190df0566a6SJani Nikula 
1191df0566a6SJani Nikula  unlock:
119246e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
1193df0566a6SJani Nikula 	i915_audio_component_put_power(kdev, cookie);
1194df0566a6SJani Nikula 	return err;
1195df0566a6SJani Nikula }
1196df0566a6SJani Nikula 
1197df0566a6SJani Nikula static int i915_audio_component_get_eld(struct device *kdev, int port,
1198df0566a6SJani Nikula 					int pipe, bool *enabled,
1199df0566a6SJani Nikula 					unsigned char *buf, int max_bytes)
1200df0566a6SJani Nikula {
120146e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1202df0566a6SJani Nikula 	struct intel_encoder *intel_encoder;
1203df0566a6SJani Nikula 	const u8 *eld;
1204df0566a6SJani Nikula 	int ret = -EINVAL;
1205df0566a6SJani Nikula 
120646e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
1207df0566a6SJani Nikula 
120846e61ee4SVille Syrjälä 	intel_encoder = get_saved_enc(i915, port, pipe);
1209df0566a6SJani Nikula 	if (!intel_encoder) {
121046e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "Not valid for port %c\n",
121163855149SWambui Karuga 			    port_name(port));
121246e61ee4SVille Syrjälä 		mutex_unlock(&i915->display.audio.mutex);
1213df0566a6SJani Nikula 		return ret;
1214df0566a6SJani Nikula 	}
1215df0566a6SJani Nikula 
1216df0566a6SJani Nikula 	ret = 0;
1217df0566a6SJani Nikula 	*enabled = intel_encoder->audio_connector != NULL;
1218df0566a6SJani Nikula 	if (*enabled) {
1219df0566a6SJani Nikula 		eld = intel_encoder->audio_connector->eld;
1220df0566a6SJani Nikula 		ret = drm_eld_size(eld);
1221df0566a6SJani Nikula 		memcpy(buf, eld, min(max_bytes, ret));
1222df0566a6SJani Nikula 	}
1223df0566a6SJani Nikula 
122446e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
1225df0566a6SJani Nikula 	return ret;
1226df0566a6SJani Nikula }
1227df0566a6SJani Nikula 
1228df0566a6SJani Nikula static const struct drm_audio_component_ops i915_audio_component_ops = {
1229df0566a6SJani Nikula 	.owner		= THIS_MODULE,
1230df0566a6SJani Nikula 	.get_power	= i915_audio_component_get_power,
1231df0566a6SJani Nikula 	.put_power	= i915_audio_component_put_power,
1232df0566a6SJani Nikula 	.codec_wake_override = i915_audio_component_codec_wake_override,
1233df0566a6SJani Nikula 	.get_cdclk_freq	= i915_audio_component_get_cdclk_freq,
1234df0566a6SJani Nikula 	.sync_audio_rate = i915_audio_component_sync_audio_rate,
1235df0566a6SJani Nikula 	.get_eld	= i915_audio_component_get_eld,
1236df0566a6SJani Nikula };
1237df0566a6SJani Nikula 
1238df0566a6SJani Nikula static int i915_audio_component_bind(struct device *i915_kdev,
1239df0566a6SJani Nikula 				     struct device *hda_kdev, void *data)
1240df0566a6SJani Nikula {
1241df0566a6SJani Nikula 	struct i915_audio_component *acomp = data;
124246e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(i915_kdev);
1243df0566a6SJani Nikula 	int i;
1244df0566a6SJani Nikula 
124546e61ee4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, acomp->base.ops || acomp->base.dev))
1246df0566a6SJani Nikula 		return -EEXIST;
1247df0566a6SJani Nikula 
124846e61ee4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm,
12499a3b466bSPankaj Bharadiya 			!device_link_add(hda_kdev, i915_kdev,
12509a3b466bSPankaj Bharadiya 					 DL_FLAG_STATELESS)))
1251df0566a6SJani Nikula 		return -ENOMEM;
1252df0566a6SJani Nikula 
125346e61ee4SVille Syrjälä 	drm_modeset_lock_all(&i915->drm);
1254df0566a6SJani Nikula 	acomp->base.ops = &i915_audio_component_ops;
1255df0566a6SJani Nikula 	acomp->base.dev = i915_kdev;
1256df0566a6SJani Nikula 	BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
1257df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
1258df0566a6SJani Nikula 		acomp->aud_sample_rate[i] = 0;
125946e61ee4SVille Syrjälä 	i915->display.audio.component = acomp;
126046e61ee4SVille Syrjälä 	drm_modeset_unlock_all(&i915->drm);
1261df0566a6SJani Nikula 
1262df0566a6SJani Nikula 	return 0;
1263df0566a6SJani Nikula }
1264df0566a6SJani Nikula 
1265df0566a6SJani Nikula static void i915_audio_component_unbind(struct device *i915_kdev,
1266df0566a6SJani Nikula 					struct device *hda_kdev, void *data)
1267df0566a6SJani Nikula {
1268df0566a6SJani Nikula 	struct i915_audio_component *acomp = data;
126946e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(i915_kdev);
1270df0566a6SJani Nikula 
127146e61ee4SVille Syrjälä 	drm_modeset_lock_all(&i915->drm);
1272df0566a6SJani Nikula 	acomp->base.ops = NULL;
1273df0566a6SJani Nikula 	acomp->base.dev = NULL;
127446e61ee4SVille Syrjälä 	i915->display.audio.component = NULL;
127546e61ee4SVille Syrjälä 	drm_modeset_unlock_all(&i915->drm);
1276df0566a6SJani Nikula 
1277df0566a6SJani Nikula 	device_link_remove(hda_kdev, i915_kdev);
1278b4ed131dSJani Nikula 
127946e61ee4SVille Syrjälä 	if (i915->display.audio.power_refcount)
128046e61ee4SVille Syrjälä 		drm_err(&i915->drm, "audio power refcount %d after unbind\n",
128146e61ee4SVille Syrjälä 			i915->display.audio.power_refcount);
1282df0566a6SJani Nikula }
1283df0566a6SJani Nikula 
1284df0566a6SJani Nikula static const struct component_ops i915_audio_component_bind_ops = {
1285df0566a6SJani Nikula 	.bind	= i915_audio_component_bind,
1286df0566a6SJani Nikula 	.unbind	= i915_audio_component_unbind,
1287df0566a6SJani Nikula };
1288df0566a6SJani Nikula 
1289989634fbSKai Vehmanen #define AUD_FREQ_TMODE_SHIFT	14
1290989634fbSKai Vehmanen #define AUD_FREQ_4T		0
1291989634fbSKai Vehmanen #define AUD_FREQ_8T		(2 << AUD_FREQ_TMODE_SHIFT)
1292989634fbSKai Vehmanen #define AUD_FREQ_PULLCLKS(x)	(((x) & 0x3) << 11)
1293989634fbSKai Vehmanen #define AUD_FREQ_BCLK_96M	BIT(4)
1294989634fbSKai Vehmanen 
1295989634fbSKai Vehmanen #define AUD_FREQ_GEN12          (AUD_FREQ_8T | AUD_FREQ_PULLCLKS(0) | AUD_FREQ_BCLK_96M)
1296989634fbSKai Vehmanen #define AUD_FREQ_TGL_BROKEN     (AUD_FREQ_8T | AUD_FREQ_PULLCLKS(2) | AUD_FREQ_BCLK_96M)
1297989634fbSKai Vehmanen 
1298df0566a6SJani Nikula /**
1299df0566a6SJani Nikula  * i915_audio_component_init - initialize and register the audio component
130046e61ee4SVille Syrjälä  * @i915: i915 device instance
1301df0566a6SJani Nikula  *
1302df0566a6SJani Nikula  * This will register with the component framework a child component which
1303df0566a6SJani Nikula  * will bind dynamically to the snd_hda_intel driver's corresponding master
1304df0566a6SJani Nikula  * component when the latter is registered. During binding the child
1305df0566a6SJani Nikula  * initializes an instance of struct i915_audio_component which it receives
1306df0566a6SJani Nikula  * from the master. The master can then start to use the interface defined by
1307df0566a6SJani Nikula  * this struct. Each side can break the binding at any point by deregistering
1308df0566a6SJani Nikula  * its own component after which each side's component unbind callback is
1309df0566a6SJani Nikula  * called.
1310df0566a6SJani Nikula  *
1311df0566a6SJani Nikula  * We ignore any error during registration and continue with reduced
1312df0566a6SJani Nikula  * functionality (i.e. without HDMI audio).
1313df0566a6SJani Nikula  */
131446e61ee4SVille Syrjälä static void i915_audio_component_init(struct drm_i915_private *i915)
1315df0566a6SJani Nikula {
1316989634fbSKai Vehmanen 	u32 aud_freq, aud_freq_init;
1317df0566a6SJani Nikula 	int ret;
1318df0566a6SJani Nikula 
131946e61ee4SVille Syrjälä 	ret = component_add_typed(i915->drm.dev,
1320df0566a6SJani Nikula 				  &i915_audio_component_bind_ops,
1321df0566a6SJani Nikula 				  I915_COMPONENT_AUDIO);
1322df0566a6SJani Nikula 	if (ret < 0) {
132346e61ee4SVille Syrjälä 		drm_err(&i915->drm,
132463855149SWambui Karuga 			"failed to add audio component (%d)\n", ret);
1325df0566a6SJani Nikula 		/* continue with reduced functionality */
1326df0566a6SJani Nikula 		return;
1327df0566a6SJani Nikula 	}
1328df0566a6SJani Nikula 
132946e61ee4SVille Syrjälä 	if (DISPLAY_VER(i915) >= 9) {
133046e61ee4SVille Syrjälä 		aud_freq_init = intel_de_read(i915, AUD_FREQ_CNTRL);
1331989634fbSKai Vehmanen 
133246e61ee4SVille Syrjälä 		if (DISPLAY_VER(i915) >= 12)
1333989634fbSKai Vehmanen 			aud_freq = AUD_FREQ_GEN12;
1334989634fbSKai Vehmanen 		else
1335989634fbSKai Vehmanen 			aud_freq = aud_freq_init;
1336989634fbSKai Vehmanen 
1337c6b40ee3SKai-Heng Feng 		/* use BIOS provided value for TGL and RKL unless it is a known bad value */
133846e61ee4SVille Syrjälä 		if ((IS_TIGERLAKE(i915) || IS_ROCKETLAKE(i915)) &&
1339c6b40ee3SKai-Heng Feng 		    aud_freq_init != AUD_FREQ_TGL_BROKEN)
1340989634fbSKai Vehmanen 			aud_freq = aud_freq_init;
1341989634fbSKai Vehmanen 
134246e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "use AUD_FREQ_CNTRL of 0x%x (init value 0x%x)\n",
1343989634fbSKai Vehmanen 			    aud_freq, aud_freq_init);
1344989634fbSKai Vehmanen 
134546e61ee4SVille Syrjälä 		i915->display.audio.freq_cntrl = aud_freq;
134687c16945SKai Vehmanen 	}
134787c16945SKai Vehmanen 
1348112a87c4SKai Vehmanen 	/* init with current cdclk */
134946e61ee4SVille Syrjälä 	intel_audio_cdclk_change_post(i915);
1350112a87c4SKai Vehmanen 
135146e61ee4SVille Syrjälä 	i915->display.audio.component_registered = true;
1352df0566a6SJani Nikula }
1353df0566a6SJani Nikula 
1354df0566a6SJani Nikula /**
1355df0566a6SJani Nikula  * i915_audio_component_cleanup - deregister the audio component
135646e61ee4SVille Syrjälä  * @i915: i915 device instance
1357df0566a6SJani Nikula  *
1358df0566a6SJani Nikula  * Deregisters the audio component, breaking any existing binding to the
1359df0566a6SJani Nikula  * corresponding snd_hda_intel driver's master component.
1360df0566a6SJani Nikula  */
136146e61ee4SVille Syrjälä static void i915_audio_component_cleanup(struct drm_i915_private *i915)
1362df0566a6SJani Nikula {
136346e61ee4SVille Syrjälä 	if (!i915->display.audio.component_registered)
1364df0566a6SJani Nikula 		return;
1365df0566a6SJani Nikula 
136646e61ee4SVille Syrjälä 	component_del(i915->drm.dev, &i915_audio_component_bind_ops);
136746e61ee4SVille Syrjälä 	i915->display.audio.component_registered = false;
1368df0566a6SJani Nikula }
1369df0566a6SJani Nikula 
1370df0566a6SJani Nikula /**
1371df0566a6SJani Nikula  * intel_audio_init() - Initialize the audio driver either using
1372df0566a6SJani Nikula  * component framework or using lpe audio bridge
137346e61ee4SVille Syrjälä  * @i915: the i915 drm device private data
1374df0566a6SJani Nikula  *
1375df0566a6SJani Nikula  */
137646e61ee4SVille Syrjälä void intel_audio_init(struct drm_i915_private *i915)
1377df0566a6SJani Nikula {
137846e61ee4SVille Syrjälä 	if (intel_lpe_audio_init(i915) < 0)
137946e61ee4SVille Syrjälä 		i915_audio_component_init(i915);
1380df0566a6SJani Nikula }
1381df0566a6SJani Nikula 
1382df0566a6SJani Nikula /**
1383df0566a6SJani Nikula  * intel_audio_deinit() - deinitialize the audio driver
138446e61ee4SVille Syrjälä  * @i915: the i915 drm device private data
1385df0566a6SJani Nikula  *
1386df0566a6SJani Nikula  */
138746e61ee4SVille Syrjälä void intel_audio_deinit(struct drm_i915_private *i915)
1388df0566a6SJani Nikula {
138946e61ee4SVille Syrjälä 	if (i915->display.audio.lpe.platdev != NULL)
139046e61ee4SVille Syrjälä 		intel_lpe_audio_teardown(i915);
1391df0566a6SJani Nikula 	else
139246e61ee4SVille Syrjälä 		i915_audio_component_cleanup(i915);
1393df0566a6SJani Nikula }
1394