xref: /linux/drivers/gpu/drm/i915/display/intel_audio.c (revision 179db7c1be5c09aa1b1493d45e6f88aebd525b96)
1df0566a6SJani Nikula /*
2df0566a6SJani Nikula  * Copyright © 2014 Intel Corporation
3df0566a6SJani Nikula  *
4df0566a6SJani Nikula  * Permission is hereby granted, free of charge, to any person obtaining a
5df0566a6SJani Nikula  * copy of this software and associated documentation files (the "Software"),
6df0566a6SJani Nikula  * to deal in the Software without restriction, including without limitation
7df0566a6SJani Nikula  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8df0566a6SJani Nikula  * and/or sell copies of the Software, and to permit persons to whom the
9df0566a6SJani Nikula  * Software is furnished to do so, subject to the following conditions:
10df0566a6SJani Nikula  *
11df0566a6SJani Nikula  * The above copyright notice and this permission notice (including the next
12df0566a6SJani Nikula  * paragraph) shall be included in all copies or substantial portions of the
13df0566a6SJani Nikula  * Software.
14df0566a6SJani Nikula  *
15df0566a6SJani Nikula  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16df0566a6SJani Nikula  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17df0566a6SJani Nikula  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18df0566a6SJani Nikula  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19df0566a6SJani Nikula  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20df0566a6SJani Nikula  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21df0566a6SJani Nikula  * DEALINGS IN THE SOFTWARE.
22df0566a6SJani Nikula  */
23df0566a6SJani Nikula 
24df0566a6SJani Nikula #include <linux/component.h>
25df0566a6SJani Nikula #include <linux/kernel.h>
26df0566a6SJani Nikula 
27df0566a6SJani Nikula #include <drm/drm_edid.h>
28df0566a6SJani Nikula #include <drm/i915_component.h>
29df0566a6SJani Nikula 
30df0566a6SJani Nikula #include "i915_drv.h"
311d5a95b5SVille Syrjälä #include "intel_atomic.h"
32df0566a6SJani Nikula #include "intel_audio.h"
3328a30b45SVille Syrjälä #include "intel_cdclk.h"
34fd2b94a5SJani Nikula #include "intel_crtc.h"
357785ae0bSVille Syrjälä #include "intel_de.h"
361d455f8dSJani Nikula #include "intel_display_types.h"
37df0566a6SJani Nikula #include "intel_lpe_audio.h"
38df0566a6SJani Nikula 
39df0566a6SJani Nikula /**
40df0566a6SJani Nikula  * DOC: High Definition Audio over HDMI and Display Port
41df0566a6SJani Nikula  *
42df0566a6SJani Nikula  * The graphics and audio drivers together support High Definition Audio over
43df0566a6SJani Nikula  * HDMI and Display Port. The audio programming sequences are divided into audio
44df0566a6SJani Nikula  * codec and controller enable and disable sequences. The graphics driver
45df0566a6SJani Nikula  * handles the audio codec sequences, while the audio driver handles the audio
46df0566a6SJani Nikula  * controller sequences.
47df0566a6SJani Nikula  *
48df0566a6SJani Nikula  * The disable sequences must be performed before disabling the transcoder or
49df0566a6SJani Nikula  * port. The enable sequences may only be performed after enabling the
50df0566a6SJani Nikula  * transcoder and port, and after completed link training. Therefore the audio
51df0566a6SJani Nikula  * enable/disable sequences are part of the modeset sequence.
52df0566a6SJani Nikula  *
53df0566a6SJani Nikula  * The codec and controller sequences could be done either parallel or serial,
54df0566a6SJani Nikula  * but generally the ELDV/PD change in the codec sequence indicates to the audio
55df0566a6SJani Nikula  * driver that the controller sequence should start. Indeed, most of the
56df0566a6SJani Nikula  * co-operation between the graphics and audio drivers is handled via audio
57df0566a6SJani Nikula  * related registers. (The notable exception is the power management, not
58df0566a6SJani Nikula  * covered here.)
59df0566a6SJani Nikula  *
60df0566a6SJani Nikula  * The struct &i915_audio_component is used to interact between the graphics
61df0566a6SJani Nikula  * and audio drivers. The struct &i915_audio_component_ops @ops in it is
62df0566a6SJani Nikula  * defined in graphics driver and called in audio driver. The
63df0566a6SJani Nikula  * struct &i915_audio_component_audio_ops @audio_ops is called from i915 driver.
64df0566a6SJani Nikula  */
65df0566a6SJani Nikula 
665d453746SJani Nikula struct intel_audio_funcs {
675d453746SJani Nikula 	void (*audio_codec_enable)(struct intel_encoder *encoder,
685d453746SJani Nikula 				   const struct intel_crtc_state *crtc_state,
695d453746SJani Nikula 				   const struct drm_connector_state *conn_state);
705d453746SJani Nikula 	void (*audio_codec_disable)(struct intel_encoder *encoder,
715d453746SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
725d453746SJani Nikula 				    const struct drm_connector_state *old_conn_state);
735d453746SJani Nikula };
745d453746SJani Nikula 
75df0566a6SJani Nikula /* DP N/M table */
76df0566a6SJani Nikula #define LC_810M	810000
77df0566a6SJani Nikula #define LC_540M	540000
78df0566a6SJani Nikula #define LC_270M	270000
79df0566a6SJani Nikula #define LC_162M	162000
80df0566a6SJani Nikula 
81df0566a6SJani Nikula struct dp_aud_n_m {
82df0566a6SJani Nikula 	int sample_rate;
83df0566a6SJani Nikula 	int clock;
84df0566a6SJani Nikula 	u16 m;
85df0566a6SJani Nikula 	u16 n;
86df0566a6SJani Nikula };
87df0566a6SJani Nikula 
882c291417SAditya Swarup struct hdmi_aud_ncts {
892c291417SAditya Swarup 	int sample_rate;
902c291417SAditya Swarup 	int clock;
912c291417SAditya Swarup 	int n;
922c291417SAditya Swarup 	int cts;
932c291417SAditya Swarup };
942c291417SAditya Swarup 
95df0566a6SJani Nikula /* Values according to DP 1.4 Table 2-104 */
96df0566a6SJani Nikula static const struct dp_aud_n_m dp_aud_n_m[] = {
97df0566a6SJani Nikula 	{ 32000, LC_162M, 1024, 10125 },
98df0566a6SJani Nikula 	{ 44100, LC_162M, 784, 5625 },
99df0566a6SJani Nikula 	{ 48000, LC_162M, 512, 3375 },
100df0566a6SJani Nikula 	{ 64000, LC_162M, 2048, 10125 },
101df0566a6SJani Nikula 	{ 88200, LC_162M, 1568, 5625 },
102df0566a6SJani Nikula 	{ 96000, LC_162M, 1024, 3375 },
103df0566a6SJani Nikula 	{ 128000, LC_162M, 4096, 10125 },
104df0566a6SJani Nikula 	{ 176400, LC_162M, 3136, 5625 },
105df0566a6SJani Nikula 	{ 192000, LC_162M, 2048, 3375 },
106df0566a6SJani Nikula 	{ 32000, LC_270M, 1024, 16875 },
107df0566a6SJani Nikula 	{ 44100, LC_270M, 784, 9375 },
108df0566a6SJani Nikula 	{ 48000, LC_270M, 512, 5625 },
109df0566a6SJani Nikula 	{ 64000, LC_270M, 2048, 16875 },
110df0566a6SJani Nikula 	{ 88200, LC_270M, 1568, 9375 },
111df0566a6SJani Nikula 	{ 96000, LC_270M, 1024, 5625 },
112df0566a6SJani Nikula 	{ 128000, LC_270M, 4096, 16875 },
113df0566a6SJani Nikula 	{ 176400, LC_270M, 3136, 9375 },
114df0566a6SJani Nikula 	{ 192000, LC_270M, 2048, 5625 },
115df0566a6SJani Nikula 	{ 32000, LC_540M, 1024, 33750 },
116df0566a6SJani Nikula 	{ 44100, LC_540M, 784, 18750 },
117df0566a6SJani Nikula 	{ 48000, LC_540M, 512, 11250 },
118df0566a6SJani Nikula 	{ 64000, LC_540M, 2048, 33750 },
119df0566a6SJani Nikula 	{ 88200, LC_540M, 1568, 18750 },
120df0566a6SJani Nikula 	{ 96000, LC_540M, 1024, 11250 },
121df0566a6SJani Nikula 	{ 128000, LC_540M, 4096, 33750 },
122df0566a6SJani Nikula 	{ 176400, LC_540M, 3136, 18750 },
123df0566a6SJani Nikula 	{ 192000, LC_540M, 2048, 11250 },
124df0566a6SJani Nikula 	{ 32000, LC_810M, 1024, 50625 },
125df0566a6SJani Nikula 	{ 44100, LC_810M, 784, 28125 },
126df0566a6SJani Nikula 	{ 48000, LC_810M, 512, 16875 },
127df0566a6SJani Nikula 	{ 64000, LC_810M, 2048, 50625 },
128df0566a6SJani Nikula 	{ 88200, LC_810M, 1568, 28125 },
129df0566a6SJani Nikula 	{ 96000, LC_810M, 1024, 16875 },
130df0566a6SJani Nikula 	{ 128000, LC_810M, 4096, 50625 },
131df0566a6SJani Nikula 	{ 176400, LC_810M, 3136, 28125 },
132df0566a6SJani Nikula 	{ 192000, LC_810M, 2048, 16875 },
133df0566a6SJani Nikula };
134df0566a6SJani Nikula 
135df0566a6SJani Nikula static const struct dp_aud_n_m *
136df0566a6SJani Nikula audio_config_dp_get_n_m(const struct intel_crtc_state *crtc_state, int rate)
137df0566a6SJani Nikula {
138df0566a6SJani Nikula 	int i;
139df0566a6SJani Nikula 
140df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(dp_aud_n_m); i++) {
141df0566a6SJani Nikula 		if (rate == dp_aud_n_m[i].sample_rate &&
142df0566a6SJani Nikula 		    crtc_state->port_clock == dp_aud_n_m[i].clock)
143df0566a6SJani Nikula 			return &dp_aud_n_m[i];
144df0566a6SJani Nikula 	}
145df0566a6SJani Nikula 
146df0566a6SJani Nikula 	return NULL;
147df0566a6SJani Nikula }
148df0566a6SJani Nikula 
149df0566a6SJani Nikula static const struct {
150df0566a6SJani Nikula 	int clock;
151df0566a6SJani Nikula 	u32 config;
152df0566a6SJani Nikula } hdmi_audio_clock[] = {
153df0566a6SJani Nikula 	{ 25175, AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
154df0566a6SJani Nikula 	{ 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
155df0566a6SJani Nikula 	{ 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
156df0566a6SJani Nikula 	{ 27027, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
157df0566a6SJani Nikula 	{ 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
158df0566a6SJani Nikula 	{ 54054, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
159df0566a6SJani Nikula 	{ 74176, AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
160df0566a6SJani Nikula 	{ 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
161df0566a6SJani Nikula 	{ 148352, AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
162df0566a6SJani Nikula 	{ 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
1631aae3065SKai Vehmanen 	{ 296703, AUD_CONFIG_PIXEL_CLOCK_HDMI_296703 },
1641aae3065SKai Vehmanen 	{ 297000, AUD_CONFIG_PIXEL_CLOCK_HDMI_297000 },
1651aae3065SKai Vehmanen 	{ 593407, AUD_CONFIG_PIXEL_CLOCK_HDMI_593407 },
1661aae3065SKai Vehmanen 	{ 594000, AUD_CONFIG_PIXEL_CLOCK_HDMI_594000 },
167df0566a6SJani Nikula };
168df0566a6SJani Nikula 
169df0566a6SJani Nikula /* HDMI N/CTS table */
170df0566a6SJani Nikula #define TMDS_297M 297000
171df0566a6SJani Nikula #define TMDS_296M 296703
172df0566a6SJani Nikula #define TMDS_594M 594000
173df0566a6SJani Nikula #define TMDS_593M 593407
174df0566a6SJani Nikula 
1752c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_24bpp[] = {
176df0566a6SJani Nikula 	{ 32000, TMDS_296M, 5824, 421875 },
177df0566a6SJani Nikula 	{ 32000, TMDS_297M, 3072, 222750 },
178df0566a6SJani Nikula 	{ 32000, TMDS_593M, 5824, 843750 },
179df0566a6SJani Nikula 	{ 32000, TMDS_594M, 3072, 445500 },
180df0566a6SJani Nikula 	{ 44100, TMDS_296M, 4459, 234375 },
181df0566a6SJani Nikula 	{ 44100, TMDS_297M, 4704, 247500 },
182df0566a6SJani Nikula 	{ 44100, TMDS_593M, 8918, 937500 },
183df0566a6SJani Nikula 	{ 44100, TMDS_594M, 9408, 990000 },
184df0566a6SJani Nikula 	{ 88200, TMDS_296M, 8918, 234375 },
185df0566a6SJani Nikula 	{ 88200, TMDS_297M, 9408, 247500 },
186df0566a6SJani Nikula 	{ 88200, TMDS_593M, 17836, 937500 },
187df0566a6SJani Nikula 	{ 88200, TMDS_594M, 18816, 990000 },
188df0566a6SJani Nikula 	{ 176400, TMDS_296M, 17836, 234375 },
189df0566a6SJani Nikula 	{ 176400, TMDS_297M, 18816, 247500 },
190df0566a6SJani Nikula 	{ 176400, TMDS_593M, 35672, 937500 },
191df0566a6SJani Nikula 	{ 176400, TMDS_594M, 37632, 990000 },
192df0566a6SJani Nikula 	{ 48000, TMDS_296M, 5824, 281250 },
193df0566a6SJani Nikula 	{ 48000, TMDS_297M, 5120, 247500 },
194df0566a6SJani Nikula 	{ 48000, TMDS_593M, 5824, 562500 },
195df0566a6SJani Nikula 	{ 48000, TMDS_594M, 6144, 594000 },
196df0566a6SJani Nikula 	{ 96000, TMDS_296M, 11648, 281250 },
197df0566a6SJani Nikula 	{ 96000, TMDS_297M, 10240, 247500 },
198df0566a6SJani Nikula 	{ 96000, TMDS_593M, 11648, 562500 },
199df0566a6SJani Nikula 	{ 96000, TMDS_594M, 12288, 594000 },
200df0566a6SJani Nikula 	{ 192000, TMDS_296M, 23296, 281250 },
201df0566a6SJani Nikula 	{ 192000, TMDS_297M, 20480, 247500 },
202df0566a6SJani Nikula 	{ 192000, TMDS_593M, 23296, 562500 },
203df0566a6SJani Nikula 	{ 192000, TMDS_594M, 24576, 594000 },
204df0566a6SJani Nikula };
205df0566a6SJani Nikula 
2062c291417SAditya Swarup /* Appendix C - N & CTS values for deep color from HDMI 2.0 spec*/
2072c291417SAditya Swarup /* HDMI N/CTS table for 10 bit deep color(30 bpp)*/
2082c291417SAditya Swarup #define TMDS_371M 371250
2092c291417SAditya Swarup #define TMDS_370M 370878
2102c291417SAditya Swarup 
2112c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_30bpp[] = {
2122c291417SAditya Swarup 	{ 32000, TMDS_370M, 5824, 527344 },
2132c291417SAditya Swarup 	{ 32000, TMDS_371M, 6144, 556875 },
2142c291417SAditya Swarup 	{ 44100, TMDS_370M, 8918, 585938 },
2152c291417SAditya Swarup 	{ 44100, TMDS_371M, 4704, 309375 },
2162c291417SAditya Swarup 	{ 88200, TMDS_370M, 17836, 585938 },
2172c291417SAditya Swarup 	{ 88200, TMDS_371M, 9408, 309375 },
2182c291417SAditya Swarup 	{ 176400, TMDS_370M, 35672, 585938 },
2192c291417SAditya Swarup 	{ 176400, TMDS_371M, 18816, 309375 },
2202c291417SAditya Swarup 	{ 48000, TMDS_370M, 11648, 703125 },
2212c291417SAditya Swarup 	{ 48000, TMDS_371M, 5120, 309375 },
2222c291417SAditya Swarup 	{ 96000, TMDS_370M, 23296, 703125 },
2232c291417SAditya Swarup 	{ 96000, TMDS_371M, 10240, 309375 },
2242c291417SAditya Swarup 	{ 192000, TMDS_370M, 46592, 703125 },
2252c291417SAditya Swarup 	{ 192000, TMDS_371M, 20480, 309375 },
2262c291417SAditya Swarup };
2272c291417SAditya Swarup 
2282c291417SAditya Swarup /* HDMI N/CTS table for 12 bit deep color(36 bpp)*/
2292c291417SAditya Swarup #define TMDS_445_5M 445500
2302c291417SAditya Swarup #define TMDS_445M 445054
2312c291417SAditya Swarup 
2322c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_36bpp[] = {
2332c291417SAditya Swarup 	{ 32000, TMDS_445M, 5824, 632813 },
2342c291417SAditya Swarup 	{ 32000, TMDS_445_5M, 4096, 445500 },
2352c291417SAditya Swarup 	{ 44100, TMDS_445M, 8918, 703125 },
2362c291417SAditya Swarup 	{ 44100, TMDS_445_5M, 4704, 371250 },
2372c291417SAditya Swarup 	{ 88200, TMDS_445M, 17836, 703125 },
2382c291417SAditya Swarup 	{ 88200, TMDS_445_5M, 9408, 371250 },
2392c291417SAditya Swarup 	{ 176400, TMDS_445M, 35672, 703125 },
2402c291417SAditya Swarup 	{ 176400, TMDS_445_5M, 18816, 371250 },
2412c291417SAditya Swarup 	{ 48000, TMDS_445M, 5824, 421875 },
2422c291417SAditya Swarup 	{ 48000, TMDS_445_5M, 5120, 371250 },
2432c291417SAditya Swarup 	{ 96000, TMDS_445M, 11648, 421875 },
2442c291417SAditya Swarup 	{ 96000, TMDS_445_5M, 10240, 371250 },
2452c291417SAditya Swarup 	{ 192000, TMDS_445M, 23296, 421875 },
2462c291417SAditya Swarup 	{ 192000, TMDS_445_5M, 20480, 371250 },
2472c291417SAditya Swarup };
2482c291417SAditya Swarup 
249df0566a6SJani Nikula /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
250df0566a6SJani Nikula static u32 audio_config_hdmi_pixel_clock(const struct intel_crtc_state *crtc_state)
251df0566a6SJani Nikula {
2521aae3065SKai Vehmanen 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
253df0566a6SJani Nikula 	const struct drm_display_mode *adjusted_mode =
2541326a92cSMaarten Lankhorst 		&crtc_state->hw.adjusted_mode;
255df0566a6SJani Nikula 	int i;
256df0566a6SJani Nikula 
257df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
258df0566a6SJani Nikula 		if (adjusted_mode->crtc_clock == hdmi_audio_clock[i].clock)
259df0566a6SJani Nikula 			break;
260df0566a6SJani Nikula 	}
261df0566a6SJani Nikula 
262005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) < 12 && adjusted_mode->crtc_clock > 148500)
2631aae3065SKai Vehmanen 		i = ARRAY_SIZE(hdmi_audio_clock);
2641aae3065SKai Vehmanen 
265df0566a6SJani Nikula 	if (i == ARRAY_SIZE(hdmi_audio_clock)) {
2669282a66cSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
2679282a66cSJani Nikula 			    "HDMI audio pixel clock setting for %d not found, falling back to defaults\n",
268df0566a6SJani Nikula 			    adjusted_mode->crtc_clock);
269df0566a6SJani Nikula 		i = 1;
270df0566a6SJani Nikula 	}
271df0566a6SJani Nikula 
2729282a66cSJani Nikula 	drm_dbg_kms(&dev_priv->drm,
2739282a66cSJani Nikula 		    "Configuring HDMI audio for pixel clock %d (0x%08x)\n",
274df0566a6SJani Nikula 		    hdmi_audio_clock[i].clock,
275df0566a6SJani Nikula 		    hdmi_audio_clock[i].config);
276df0566a6SJani Nikula 
277df0566a6SJani Nikula 	return hdmi_audio_clock[i].config;
278df0566a6SJani Nikula }
279df0566a6SJani Nikula 
280df0566a6SJani Nikula static int audio_config_hdmi_get_n(const struct intel_crtc_state *crtc_state,
281df0566a6SJani Nikula 				   int rate)
282df0566a6SJani Nikula {
2832c291417SAditya Swarup 	const struct hdmi_aud_ncts *hdmi_ncts_table;
2842c291417SAditya Swarup 	int i, size;
285df0566a6SJani Nikula 
2862c291417SAditya Swarup 	if (crtc_state->pipe_bpp == 36) {
2872c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_36bpp;
2882c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_36bpp);
2892c291417SAditya Swarup 	} else if (crtc_state->pipe_bpp == 30) {
2902c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_30bpp;
2912c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_30bpp);
2922c291417SAditya Swarup 	} else {
2932c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_24bpp;
2942c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_24bpp);
2952c291417SAditya Swarup 	}
2962c291417SAditya Swarup 
2972c291417SAditya Swarup 	for (i = 0; i < size; i++) {
2982c291417SAditya Swarup 		if (rate == hdmi_ncts_table[i].sample_rate &&
2992c291417SAditya Swarup 		    crtc_state->port_clock == hdmi_ncts_table[i].clock) {
3002c291417SAditya Swarup 			return hdmi_ncts_table[i].n;
301df0566a6SJani Nikula 		}
302df0566a6SJani Nikula 	}
303df0566a6SJani Nikula 	return 0;
304df0566a6SJani Nikula }
305df0566a6SJani Nikula 
306df0566a6SJani Nikula static bool intel_eld_uptodate(struct drm_connector *connector,
307df0566a6SJani Nikula 			       i915_reg_t reg_eldv, u32 bits_eldv,
308df0566a6SJani Nikula 			       i915_reg_t reg_elda, u32 bits_elda,
309df0566a6SJani Nikula 			       i915_reg_t reg_edid)
310df0566a6SJani Nikula {
311df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
312df0566a6SJani Nikula 	const u8 *eld = connector->eld;
313df0566a6SJani Nikula 	u32 tmp;
314df0566a6SJani Nikula 	int i;
315df0566a6SJani Nikula 
31649e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, reg_eldv);
317df0566a6SJani Nikula 	tmp &= bits_eldv;
318df0566a6SJani Nikula 
319df0566a6SJani Nikula 	if (!tmp)
320df0566a6SJani Nikula 		return false;
321df0566a6SJani Nikula 
32249e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, reg_elda);
323df0566a6SJani Nikula 	tmp &= ~bits_elda;
32449e659bcSJani Nikula 	intel_de_write(dev_priv, reg_elda, tmp);
325df0566a6SJani Nikula 
326df0566a6SJani Nikula 	for (i = 0; i < drm_eld_size(eld) / 4; i++)
32749e659bcSJani Nikula 		if (intel_de_read(dev_priv, reg_edid) != *((const u32 *)eld + i))
328df0566a6SJani Nikula 			return false;
329df0566a6SJani Nikula 
330df0566a6SJani Nikula 	return true;
331df0566a6SJani Nikula }
332df0566a6SJani Nikula 
333df0566a6SJani Nikula static void g4x_audio_codec_disable(struct intel_encoder *encoder,
334df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
335df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
336df0566a6SJani Nikula {
337df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
338df0566a6SJani Nikula 	u32 eldv, tmp;
339df0566a6SJani Nikula 
34049e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_VID_DID);
341df0566a6SJani Nikula 	if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
342df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCL_DEVBLC;
343df0566a6SJani Nikula 	else
344df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCTG;
345df0566a6SJani Nikula 
346df0566a6SJani Nikula 	/* Invalidate ELD */
34749e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_CNTL_ST);
348df0566a6SJani Nikula 	tmp &= ~eldv;
34949e659bcSJani Nikula 	intel_de_write(dev_priv, G4X_AUD_CNTL_ST, tmp);
350df0566a6SJani Nikula }
351df0566a6SJani Nikula 
352df0566a6SJani Nikula static void g4x_audio_codec_enable(struct intel_encoder *encoder,
353df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
354df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
355df0566a6SJani Nikula {
356df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
357df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
358df0566a6SJani Nikula 	const u8 *eld = connector->eld;
359df0566a6SJani Nikula 	u32 eldv;
360df0566a6SJani Nikula 	u32 tmp;
361df0566a6SJani Nikula 	int len, i;
362df0566a6SJani Nikula 
36349e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_VID_DID);
364df0566a6SJani Nikula 	if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
365df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCL_DEVBLC;
366df0566a6SJani Nikula 	else
367df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCTG;
368df0566a6SJani Nikula 
369df0566a6SJani Nikula 	if (intel_eld_uptodate(connector,
370df0566a6SJani Nikula 			       G4X_AUD_CNTL_ST, eldv,
371df0566a6SJani Nikula 			       G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
372df0566a6SJani Nikula 			       G4X_HDMIW_HDMIEDID))
373df0566a6SJani Nikula 		return;
374df0566a6SJani Nikula 
37549e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_CNTL_ST);
376df0566a6SJani Nikula 	tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
377df0566a6SJani Nikula 	len = (tmp >> 9) & 0x1f;		/* ELD buffer size */
37849e659bcSJani Nikula 	intel_de_write(dev_priv, G4X_AUD_CNTL_ST, tmp);
379df0566a6SJani Nikula 
380df0566a6SJani Nikula 	len = min(drm_eld_size(eld) / 4, len);
381df0566a6SJani Nikula 	for (i = 0; i < len; i++)
38249e659bcSJani Nikula 		intel_de_write(dev_priv, G4X_HDMIW_HDMIEDID,
38349e659bcSJani Nikula 			       *((const u32 *)eld + i));
384df0566a6SJani Nikula 
38549e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_CNTL_ST);
386df0566a6SJani Nikula 	tmp |= eldv;
38749e659bcSJani Nikula 	intel_de_write(dev_priv, G4X_AUD_CNTL_ST, tmp);
388df0566a6SJani Nikula }
389df0566a6SJani Nikula 
390df0566a6SJani Nikula static void
391df0566a6SJani Nikula hsw_dp_audio_config_update(struct intel_encoder *encoder,
392df0566a6SJani Nikula 			   const struct intel_crtc_state *crtc_state)
393df0566a6SJani Nikula {
394df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
395ca3cfb9dSJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio.component;
396df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
397df0566a6SJani Nikula 	enum port port = encoder->port;
398df0566a6SJani Nikula 	const struct dp_aud_n_m *nm;
399df0566a6SJani Nikula 	int rate;
400df0566a6SJani Nikula 	u32 tmp;
401df0566a6SJani Nikula 
402df0566a6SJani Nikula 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
403df0566a6SJani Nikula 	nm = audio_config_dp_get_n_m(crtc_state, rate);
404df0566a6SJani Nikula 	if (nm)
40563855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using Maud %u, Naud %u\n", nm->m,
40663855149SWambui Karuga 			    nm->n);
407df0566a6SJani Nikula 	else
40863855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using automatic Maud, Naud\n");
409df0566a6SJani Nikula 
41049e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_CFG(cpu_transcoder));
411df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
412df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
413df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
414df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_VALUE_INDEX;
415df0566a6SJani Nikula 
416df0566a6SJani Nikula 	if (nm) {
417df0566a6SJani Nikula 		tmp &= ~AUD_CONFIG_N_MASK;
418df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N(nm->n);
419df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
420df0566a6SJani Nikula 	}
421df0566a6SJani Nikula 
42249e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_CFG(cpu_transcoder), tmp);
423df0566a6SJani Nikula 
42449e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
425df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_M_MASK;
426df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
427df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
428df0566a6SJani Nikula 
429df0566a6SJani Nikula 	if (nm) {
430df0566a6SJani Nikula 		tmp |= nm->m;
431df0566a6SJani Nikula 		tmp |= AUD_M_CTS_M_VALUE_INDEX;
432df0566a6SJani Nikula 		tmp |= AUD_M_CTS_M_PROG_ENABLE;
433df0566a6SJani Nikula 	}
434df0566a6SJani Nikula 
43549e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
436df0566a6SJani Nikula }
437df0566a6SJani Nikula 
438df0566a6SJani Nikula static void
439df0566a6SJani Nikula hsw_hdmi_audio_config_update(struct intel_encoder *encoder,
440df0566a6SJani Nikula 			     const struct intel_crtc_state *crtc_state)
441df0566a6SJani Nikula {
442df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
443ca3cfb9dSJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio.component;
444df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
445df0566a6SJani Nikula 	enum port port = encoder->port;
446df0566a6SJani Nikula 	int n, rate;
447df0566a6SJani Nikula 	u32 tmp;
448df0566a6SJani Nikula 
449df0566a6SJani Nikula 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
450df0566a6SJani Nikula 
45149e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_CFG(cpu_transcoder));
452df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
453df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
454df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
455df0566a6SJani Nikula 	tmp |= audio_config_hdmi_pixel_clock(crtc_state);
456df0566a6SJani Nikula 
457df0566a6SJani Nikula 	n = audio_config_hdmi_get_n(crtc_state, rate);
458df0566a6SJani Nikula 	if (n != 0) {
45963855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using N %d\n", n);
460df0566a6SJani Nikula 
461df0566a6SJani Nikula 		tmp &= ~AUD_CONFIG_N_MASK;
462df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N(n);
463df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
464df0566a6SJani Nikula 	} else {
46563855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using automatic N\n");
466df0566a6SJani Nikula 	}
467df0566a6SJani Nikula 
46849e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_CFG(cpu_transcoder), tmp);
469df0566a6SJani Nikula 
470df0566a6SJani Nikula 	/*
471df0566a6SJani Nikula 	 * Let's disable "Enable CTS or M Prog bit"
472df0566a6SJani Nikula 	 * and let HW calculate the value
473df0566a6SJani Nikula 	 */
47449e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
475df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
476df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
47749e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
478df0566a6SJani Nikula }
479df0566a6SJani Nikula 
480df0566a6SJani Nikula static void
481df0566a6SJani Nikula hsw_audio_config_update(struct intel_encoder *encoder,
482df0566a6SJani Nikula 			const struct intel_crtc_state *crtc_state)
483df0566a6SJani Nikula {
484df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state))
485df0566a6SJani Nikula 		hsw_dp_audio_config_update(encoder, crtc_state);
486df0566a6SJani Nikula 	else
487df0566a6SJani Nikula 		hsw_hdmi_audio_config_update(encoder, crtc_state);
488df0566a6SJani Nikula }
489df0566a6SJani Nikula 
490df0566a6SJani Nikula static void hsw_audio_codec_disable(struct intel_encoder *encoder,
491df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
492df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
493df0566a6SJani Nikula {
494df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
495df0566a6SJani Nikula 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
496df0566a6SJani Nikula 	u32 tmp;
497df0566a6SJani Nikula 
498ca3cfb9dSJani Nikula 	mutex_lock(&dev_priv->audio.mutex);
499df0566a6SJani Nikula 
500df0566a6SJani Nikula 	/* Disable timestamps */
50149e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_CFG(cpu_transcoder));
502df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
503df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_PROG_ENABLE;
504df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_UPPER_N_MASK;
505df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_LOWER_N_MASK;
506df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(old_crtc_state))
507df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
50849e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_CFG(cpu_transcoder), tmp);
509df0566a6SJani Nikula 
510df0566a6SJani Nikula 	/* Invalidate ELD */
51149e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD);
512df0566a6SJani Nikula 	tmp &= ~AUDIO_ELD_VALID(cpu_transcoder);
513df0566a6SJani Nikula 	tmp &= ~AUDIO_OUTPUT_ENABLE(cpu_transcoder);
51449e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_PIN_ELD_CP_VLD, tmp);
515df0566a6SJani Nikula 
516ca3cfb9dSJani Nikula 	mutex_unlock(&dev_priv->audio.mutex);
517df0566a6SJani Nikula }
518df0566a6SJani Nikula 
5192dd43144SVille Syrjälä static unsigned int calc_hblank_early_prog(struct intel_encoder *encoder,
52048b8b04cSUma Shankar 					   const struct intel_crtc_state *crtc_state)
52148b8b04cSUma Shankar {
52248b8b04cSUma Shankar 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
52348b8b04cSUma Shankar 	unsigned int link_clks_available, link_clks_required;
52448b8b04cSUma Shankar 	unsigned int tu_data, tu_line, link_clks_active;
525d19b29beSVille Syrjälä 	unsigned int h_active, h_total, hblank_delta, pixel_clk;
526d19b29beSVille Syrjälä 	unsigned int fec_coeff, cdclk, vdsc_bpp;
52741ee86d6SVille Syrjälä 	unsigned int link_clk, lanes;
5282dd43144SVille Syrjälä 	unsigned int hblank_rise;
52948b8b04cSUma Shankar 
53048b8b04cSUma Shankar 	h_active = crtc_state->hw.adjusted_mode.crtc_hdisplay;
53148b8b04cSUma Shankar 	h_total = crtc_state->hw.adjusted_mode.crtc_htotal;
53248b8b04cSUma Shankar 	pixel_clk = crtc_state->hw.adjusted_mode.crtc_clock;
53348b8b04cSUma Shankar 	vdsc_bpp = crtc_state->dsc.compressed_bpp;
53448b8b04cSUma Shankar 	cdclk = i915->cdclk.hw.cdclk;
53548b8b04cSUma Shankar 	/* fec= 0.972261, using rounding multiplier of 1000000 */
53648b8b04cSUma Shankar 	fec_coeff = 972261;
53741ee86d6SVille Syrjälä 	link_clk = crtc_state->port_clock;
53841ee86d6SVille Syrjälä 	lanes = crtc_state->lane_count;
53948b8b04cSUma Shankar 
54048b8b04cSUma Shankar 	drm_dbg_kms(&i915->drm, "h_active = %u link_clk = %u :"
54148b8b04cSUma Shankar 		    "lanes = %u vdsc_bpp = %u cdclk = %u\n",
54241ee86d6SVille Syrjälä 		    h_active, link_clk, lanes, vdsc_bpp, cdclk);
54348b8b04cSUma Shankar 
5442dd43144SVille Syrjälä 	if (WARN_ON(!link_clk || !pixel_clk || !lanes || !vdsc_bpp || !cdclk))
54511ebc232SJani Nikula 		return 0;
54611ebc232SJani Nikula 
5472dd43144SVille Syrjälä 	link_clks_available = (h_total - h_active) * link_clk / pixel_clk - 28;
5482dd43144SVille Syrjälä 	link_clks_required = DIV_ROUND_UP(192000 * h_total, 1000 * pixel_clk) * (48 / lanes + 2);
54948b8b04cSUma Shankar 
55048b8b04cSUma Shankar 	if (link_clks_available > link_clks_required)
55148b8b04cSUma Shankar 		hblank_delta = 32;
55248b8b04cSUma Shankar 	else
5532dd43144SVille Syrjälä 		hblank_delta = DIV64_U64_ROUND_UP(mul_u32_u32(5 * (link_clk + cdclk), pixel_clk),
5542dd43144SVille Syrjälä 						  mul_u32_u32(link_clk, cdclk));
55548b8b04cSUma Shankar 
5562dd43144SVille Syrjälä 	tu_data = div64_u64(mul_u32_u32(pixel_clk * vdsc_bpp * 8, 1000000),
5572dd43144SVille Syrjälä 			    mul_u32_u32(link_clk * lanes, fec_coeff));
5582dd43144SVille Syrjälä 	tu_line = div64_u64(h_active * mul_u32_u32(link_clk, fec_coeff),
5592dd43144SVille Syrjälä 			    mul_u32_u32(64 * pixel_clk, 1000000));
56048b8b04cSUma Shankar 	link_clks_active  = (tu_line - 1) * 64 + tu_data;
56148b8b04cSUma Shankar 
5622dd43144SVille Syrjälä 	hblank_rise = (link_clks_active + 6 * DIV_ROUND_UP(link_clks_active, 250) + 4) * pixel_clk / link_clk;
56348b8b04cSUma Shankar 
5642dd43144SVille Syrjälä 	return h_active - hblank_rise + hblank_delta;
56548b8b04cSUma Shankar }
56648b8b04cSUma Shankar 
5672dd43144SVille Syrjälä static unsigned int calc_samples_room(const struct intel_crtc_state *crtc_state)
56848b8b04cSUma Shankar {
56948b8b04cSUma Shankar 	unsigned int h_active, h_total, pixel_clk;
57041ee86d6SVille Syrjälä 	unsigned int link_clk, lanes;
57148b8b04cSUma Shankar 
57248b8b04cSUma Shankar 	h_active = crtc_state->hw.adjusted_mode.hdisplay;
57348b8b04cSUma Shankar 	h_total = crtc_state->hw.adjusted_mode.htotal;
57448b8b04cSUma Shankar 	pixel_clk = crtc_state->hw.adjusted_mode.clock;
57541ee86d6SVille Syrjälä 	link_clk = crtc_state->port_clock;
57641ee86d6SVille Syrjälä 	lanes = crtc_state->lane_count;
57748b8b04cSUma Shankar 
5782dd43144SVille Syrjälä 	return ((h_total - h_active) * link_clk - 12 * pixel_clk) /
5792dd43144SVille Syrjälä 		(pixel_clk * (48 / lanes + 2));
58048b8b04cSUma Shankar }
58148b8b04cSUma Shankar 
58248b8b04cSUma Shankar static void enable_audio_dsc_wa(struct intel_encoder *encoder,
58348b8b04cSUma Shankar 				const struct intel_crtc_state *crtc_state)
58448b8b04cSUma Shankar {
58548b8b04cSUma Shankar 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
58648b8b04cSUma Shankar 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
58748b8b04cSUma Shankar 	enum pipe pipe = crtc->pipe;
58811ebc232SJani Nikula 	unsigned int hblank_early_prog, samples_room;
58948b8b04cSUma Shankar 	unsigned int val;
59048b8b04cSUma Shankar 
591005e9537SMatt Roper 	if (DISPLAY_VER(i915) < 11)
59248b8b04cSUma Shankar 		return;
59348b8b04cSUma Shankar 
59448b8b04cSUma Shankar 	val = intel_de_read(i915, AUD_CONFIG_BE);
59548b8b04cSUma Shankar 
59693e7e61eSLucas De Marchi 	if (DISPLAY_VER(i915) == 11)
59748b8b04cSUma Shankar 		val |= HBLANK_EARLY_ENABLE_ICL(pipe);
598005e9537SMatt Roper 	else if (DISPLAY_VER(i915) >= 12)
59948b8b04cSUma Shankar 		val |= HBLANK_EARLY_ENABLE_TGL(pipe);
60048b8b04cSUma Shankar 
60148b8b04cSUma Shankar 	if (crtc_state->dsc.compression_enable &&
60231824c03SJani Nikula 	    crtc_state->hw.adjusted_mode.hdisplay >= 3840 &&
60331824c03SJani Nikula 	    crtc_state->hw.adjusted_mode.vdisplay >= 2160) {
60448b8b04cSUma Shankar 		/* Get hblank early enable value required */
605f4c50deeSJani Nikula 		val &= ~HBLANK_START_COUNT_MASK(pipe);
6062dd43144SVille Syrjälä 		hblank_early_prog = calc_hblank_early_prog(encoder, crtc_state);
607f4c50deeSJani Nikula 		if (hblank_early_prog < 32)
60848b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_32);
609f4c50deeSJani Nikula 		else if (hblank_early_prog < 64)
61048b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_64);
611f4c50deeSJani Nikula 		else if (hblank_early_prog < 96)
61248b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_96);
613f4c50deeSJani Nikula 		else
61448b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_128);
61548b8b04cSUma Shankar 
61648b8b04cSUma Shankar 		/* Get samples room value required */
617f4c50deeSJani Nikula 		val &= ~NUMBER_SAMPLES_PER_LINE_MASK(pipe);
6182dd43144SVille Syrjälä 		samples_room = calc_samples_room(crtc_state);
619f4c50deeSJani Nikula 		if (samples_room < 3)
62048b8b04cSUma Shankar 			val |= NUMBER_SAMPLES_PER_LINE(pipe, samples_room);
621f4c50deeSJani Nikula 		else /* Program 0 i.e "All Samples available in buffer" */
62248b8b04cSUma Shankar 			val |= NUMBER_SAMPLES_PER_LINE(pipe, 0x0);
62348b8b04cSUma Shankar 	}
62448b8b04cSUma Shankar 
62548b8b04cSUma Shankar 	intel_de_write(i915, AUD_CONFIG_BE, val);
62648b8b04cSUma Shankar }
62748b8b04cSUma Shankar 
62848b8b04cSUma Shankar #undef ROUNDING_FACTOR
62948b8b04cSUma Shankar 
630df0566a6SJani Nikula static void hsw_audio_codec_enable(struct intel_encoder *encoder,
631df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
632df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
633df0566a6SJani Nikula {
634df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
635df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
636df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
637df0566a6SJani Nikula 	const u8 *eld = connector->eld;
638df0566a6SJani Nikula 	u32 tmp;
639df0566a6SJani Nikula 	int len, i;
640df0566a6SJani Nikula 
641ca3cfb9dSJani Nikula 	mutex_lock(&dev_priv->audio.mutex);
642df0566a6SJani Nikula 
64348b8b04cSUma Shankar 	/* Enable Audio WA for 4k DSC usecases */
64448b8b04cSUma Shankar 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP))
64548b8b04cSUma Shankar 		enable_audio_dsc_wa(encoder, crtc_state);
64648b8b04cSUma Shankar 
647df0566a6SJani Nikula 	/* Enable audio presence detect, invalidate ELD */
64849e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD);
649df0566a6SJani Nikula 	tmp |= AUDIO_OUTPUT_ENABLE(cpu_transcoder);
650df0566a6SJani Nikula 	tmp &= ~AUDIO_ELD_VALID(cpu_transcoder);
65149e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_PIN_ELD_CP_VLD, tmp);
652df0566a6SJani Nikula 
653df0566a6SJani Nikula 	/*
654df0566a6SJani Nikula 	 * FIXME: We're supposed to wait for vblank here, but we have vblanks
655df0566a6SJani Nikula 	 * disabled during the mode set. The proper fix would be to push the
656df0566a6SJani Nikula 	 * rest of the setup into a vblank work item, queued here, but the
657df0566a6SJani Nikula 	 * infrastructure is not there yet.
658df0566a6SJani Nikula 	 */
659df0566a6SJani Nikula 
660df0566a6SJani Nikula 	/* Reset ELD write address */
66149e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_DIP_ELD_CTRL(cpu_transcoder));
662df0566a6SJani Nikula 	tmp &= ~IBX_ELD_ADDRESS_MASK;
66349e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_DIP_ELD_CTRL(cpu_transcoder), tmp);
664df0566a6SJani Nikula 
665df0566a6SJani Nikula 	/* Up to 84 bytes of hw ELD buffer */
666df0566a6SJani Nikula 	len = min(drm_eld_size(eld), 84);
667df0566a6SJani Nikula 	for (i = 0; i < len / 4; i++)
66849e659bcSJani Nikula 		intel_de_write(dev_priv, HSW_AUD_EDID_DATA(cpu_transcoder),
66949e659bcSJani Nikula 			       *((const u32 *)eld + i));
670df0566a6SJani Nikula 
671df0566a6SJani Nikula 	/* ELD valid */
67249e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD);
673df0566a6SJani Nikula 	tmp |= AUDIO_ELD_VALID(cpu_transcoder);
67449e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_PIN_ELD_CP_VLD, tmp);
675df0566a6SJani Nikula 
676df0566a6SJani Nikula 	/* Enable timestamps */
677df0566a6SJani Nikula 	hsw_audio_config_update(encoder, crtc_state);
678df0566a6SJani Nikula 
679ca3cfb9dSJani Nikula 	mutex_unlock(&dev_priv->audio.mutex);
680df0566a6SJani Nikula }
681df0566a6SJani Nikula 
682df0566a6SJani Nikula static void ilk_audio_codec_disable(struct intel_encoder *encoder,
683df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
684df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
685df0566a6SJani Nikula {
686df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
6872225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
688df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
689df0566a6SJani Nikula 	enum port port = encoder->port;
690df0566a6SJani Nikula 	u32 tmp, eldv;
691df0566a6SJani Nikula 	i915_reg_t aud_config, aud_cntrl_st2;
692df0566a6SJani Nikula 
6939a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, port == PORT_A))
694df0566a6SJani Nikula 		return;
695df0566a6SJani Nikula 
696df0566a6SJani Nikula 	if (HAS_PCH_IBX(dev_priv)) {
697df0566a6SJani Nikula 		aud_config = IBX_AUD_CFG(pipe);
698df0566a6SJani Nikula 		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
699df0566a6SJani Nikula 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
700df0566a6SJani Nikula 		aud_config = VLV_AUD_CFG(pipe);
701df0566a6SJani Nikula 		aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
702df0566a6SJani Nikula 	} else {
703df0566a6SJani Nikula 		aud_config = CPT_AUD_CFG(pipe);
704df0566a6SJani Nikula 		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
705df0566a6SJani Nikula 	}
706df0566a6SJani Nikula 
707df0566a6SJani Nikula 	/* Disable timestamps */
70849e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_config);
709df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
710df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_PROG_ENABLE;
711df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_UPPER_N_MASK;
712df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_LOWER_N_MASK;
713df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(old_crtc_state))
714df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
71549e659bcSJani Nikula 	intel_de_write(dev_priv, aud_config, tmp);
716df0566a6SJani Nikula 
717df0566a6SJani Nikula 	eldv = IBX_ELD_VALID(port);
718df0566a6SJani Nikula 
719df0566a6SJani Nikula 	/* Invalidate ELD */
72049e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_cntrl_st2);
721df0566a6SJani Nikula 	tmp &= ~eldv;
72249e659bcSJani Nikula 	intel_de_write(dev_priv, aud_cntrl_st2, tmp);
723df0566a6SJani Nikula }
724df0566a6SJani Nikula 
725df0566a6SJani Nikula static void ilk_audio_codec_enable(struct intel_encoder *encoder,
726df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
727df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
728df0566a6SJani Nikula {
729df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
7302225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
731df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
732df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
733df0566a6SJani Nikula 	enum port port = encoder->port;
734df0566a6SJani Nikula 	const u8 *eld = connector->eld;
735df0566a6SJani Nikula 	u32 tmp, eldv;
736df0566a6SJani Nikula 	int len, i;
737df0566a6SJani Nikula 	i915_reg_t hdmiw_hdmiedid, aud_config, aud_cntl_st, aud_cntrl_st2;
738df0566a6SJani Nikula 
7399a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, port == PORT_A))
740df0566a6SJani Nikula 		return;
741df0566a6SJani Nikula 
742df0566a6SJani Nikula 	/*
743df0566a6SJani Nikula 	 * FIXME: We're supposed to wait for vblank here, but we have vblanks
744df0566a6SJani Nikula 	 * disabled during the mode set. The proper fix would be to push the
745df0566a6SJani Nikula 	 * rest of the setup into a vblank work item, queued here, but the
746df0566a6SJani Nikula 	 * infrastructure is not there yet.
747df0566a6SJani Nikula 	 */
748df0566a6SJani Nikula 
749df0566a6SJani Nikula 	if (HAS_PCH_IBX(dev_priv)) {
750df0566a6SJani Nikula 		hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
751df0566a6SJani Nikula 		aud_config = IBX_AUD_CFG(pipe);
752df0566a6SJani Nikula 		aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
753df0566a6SJani Nikula 		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
754df0566a6SJani Nikula 	} else if (IS_VALLEYVIEW(dev_priv) ||
755df0566a6SJani Nikula 		   IS_CHERRYVIEW(dev_priv)) {
756df0566a6SJani Nikula 		hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
757df0566a6SJani Nikula 		aud_config = VLV_AUD_CFG(pipe);
758df0566a6SJani Nikula 		aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
759df0566a6SJani Nikula 		aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
760df0566a6SJani Nikula 	} else {
761df0566a6SJani Nikula 		hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
762df0566a6SJani Nikula 		aud_config = CPT_AUD_CFG(pipe);
763df0566a6SJani Nikula 		aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
764df0566a6SJani Nikula 		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
765df0566a6SJani Nikula 	}
766df0566a6SJani Nikula 
767df0566a6SJani Nikula 	eldv = IBX_ELD_VALID(port);
768df0566a6SJani Nikula 
769df0566a6SJani Nikula 	/* Invalidate ELD */
77049e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_cntrl_st2);
771df0566a6SJani Nikula 	tmp &= ~eldv;
77249e659bcSJani Nikula 	intel_de_write(dev_priv, aud_cntrl_st2, tmp);
773df0566a6SJani Nikula 
774df0566a6SJani Nikula 	/* Reset ELD write address */
77549e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_cntl_st);
776df0566a6SJani Nikula 	tmp &= ~IBX_ELD_ADDRESS_MASK;
77749e659bcSJani Nikula 	intel_de_write(dev_priv, aud_cntl_st, tmp);
778df0566a6SJani Nikula 
779df0566a6SJani Nikula 	/* Up to 84 bytes of hw ELD buffer */
780df0566a6SJani Nikula 	len = min(drm_eld_size(eld), 84);
781df0566a6SJani Nikula 	for (i = 0; i < len / 4; i++)
78249e659bcSJani Nikula 		intel_de_write(dev_priv, hdmiw_hdmiedid,
78349e659bcSJani Nikula 			       *((const u32 *)eld + i));
784df0566a6SJani Nikula 
785df0566a6SJani Nikula 	/* ELD valid */
78649e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_cntrl_st2);
787df0566a6SJani Nikula 	tmp |= eldv;
78849e659bcSJani Nikula 	intel_de_write(dev_priv, aud_cntrl_st2, tmp);
789df0566a6SJani Nikula 
790df0566a6SJani Nikula 	/* Enable timestamps */
79149e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_config);
792df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
793df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
794df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
795df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state))
796df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
797df0566a6SJani Nikula 	else
798df0566a6SJani Nikula 		tmp |= audio_config_hdmi_pixel_clock(crtc_state);
79949e659bcSJani Nikula 	intel_de_write(dev_priv, aud_config, tmp);
800df0566a6SJani Nikula }
801df0566a6SJani Nikula 
802df0566a6SJani Nikula /**
803df0566a6SJani Nikula  * intel_audio_codec_enable - Enable the audio codec for HD audio
804df0566a6SJani Nikula  * @encoder: encoder on which to enable audio
805df0566a6SJani Nikula  * @crtc_state: pointer to the current crtc state.
806df0566a6SJani Nikula  * @conn_state: pointer to the current connector state.
807df0566a6SJani Nikula  *
808df0566a6SJani Nikula  * The enable sequences may only be performed after enabling the transcoder and
809df0566a6SJani Nikula  * port, and after completed link training.
810df0566a6SJani Nikula  */
811df0566a6SJani Nikula void intel_audio_codec_enable(struct intel_encoder *encoder,
812df0566a6SJani Nikula 			      const struct intel_crtc_state *crtc_state,
813df0566a6SJani Nikula 			      const struct drm_connector_state *conn_state)
814df0566a6SJani Nikula {
815df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
816ca3cfb9dSJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio.component;
8172225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
818df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
819df0566a6SJani Nikula 	const struct drm_display_mode *adjusted_mode =
8201326a92cSMaarten Lankhorst 		&crtc_state->hw.adjusted_mode;
821df0566a6SJani Nikula 	enum port port = encoder->port;
822df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
823df0566a6SJani Nikula 
824*179db7c1SJani Nikula 	if (!crtc_state->has_audio)
825*179db7c1SJani Nikula 		return;
826*179db7c1SJani Nikula 
8271f31e35fSJani Nikula 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s][ENCODER:%d:%s] Enable audio codec on pipe %c, %u bytes ELD\n",
8281f31e35fSJani Nikula 		    connector->base.id, connector->name,
8291f31e35fSJani Nikula 		    encoder->base.base.id, encoder->base.name,
8301f31e35fSJani Nikula 		    pipe, drm_eld_size(connector->eld));
8311f31e35fSJani Nikula 
832df0566a6SJani Nikula 	/* FIXME precompute the ELD in .compute_config() */
833df0566a6SJani Nikula 	if (!connector->eld[0])
83463855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
83563855149SWambui Karuga 			    "Bogus ELD on [CONNECTOR:%d:%s]\n",
836df0566a6SJani Nikula 			    connector->base.id, connector->name);
837df0566a6SJani Nikula 
838df0566a6SJani Nikula 	connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
839df0566a6SJani Nikula 
840ca3cfb9dSJani Nikula 	if (dev_priv->audio.funcs)
841ca3cfb9dSJani Nikula 		dev_priv->audio.funcs->audio_codec_enable(encoder,
842df0566a6SJani Nikula 							  crtc_state,
843df0566a6SJani Nikula 							  conn_state);
844df0566a6SJani Nikula 
845ca3cfb9dSJani Nikula 	mutex_lock(&dev_priv->audio.mutex);
846df0566a6SJani Nikula 	encoder->audio_connector = connector;
847df0566a6SJani Nikula 
848df0566a6SJani Nikula 	/* referred in audio callbacks */
849ca3cfb9dSJani Nikula 	dev_priv->audio.encoder_map[pipe] = encoder;
850ca3cfb9dSJani Nikula 	mutex_unlock(&dev_priv->audio.mutex);
851df0566a6SJani Nikula 
852df0566a6SJani Nikula 	if (acomp && acomp->base.audio_ops &&
853df0566a6SJani Nikula 	    acomp->base.audio_ops->pin_eld_notify) {
854df0566a6SJani Nikula 		/* audio drivers expect pipe = -1 to indicate Non-MST cases */
855df0566a6SJani Nikula 		if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
856df0566a6SJani Nikula 			pipe = -1;
857df0566a6SJani Nikula 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
858df0566a6SJani Nikula 						 (int) port, (int) pipe);
859df0566a6SJani Nikula 	}
860df0566a6SJani Nikula 
861df0566a6SJani Nikula 	intel_lpe_audio_notify(dev_priv, pipe, port, connector->eld,
862df0566a6SJani Nikula 			       crtc_state->port_clock,
863df0566a6SJani Nikula 			       intel_crtc_has_dp_encoder(crtc_state));
864df0566a6SJani Nikula }
865df0566a6SJani Nikula 
866df0566a6SJani Nikula /**
867df0566a6SJani Nikula  * intel_audio_codec_disable - Disable the audio codec for HD audio
868df0566a6SJani Nikula  * @encoder: encoder on which to disable audio
869df0566a6SJani Nikula  * @old_crtc_state: pointer to the old crtc state.
870df0566a6SJani Nikula  * @old_conn_state: pointer to the old connector state.
871df0566a6SJani Nikula  *
872df0566a6SJani Nikula  * The disable sequences must be performed before disabling the transcoder or
873df0566a6SJani Nikula  * port.
874df0566a6SJani Nikula  */
875df0566a6SJani Nikula void intel_audio_codec_disable(struct intel_encoder *encoder,
876df0566a6SJani Nikula 			       const struct intel_crtc_state *old_crtc_state,
877df0566a6SJani Nikula 			       const struct drm_connector_state *old_conn_state)
878df0566a6SJani Nikula {
879df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
880ca3cfb9dSJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio.component;
8812225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
8821f31e35fSJani Nikula 	struct drm_connector *connector = old_conn_state->connector;
883df0566a6SJani Nikula 	enum port port = encoder->port;
884df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
885df0566a6SJani Nikula 
886*179db7c1SJani Nikula 	if (!old_crtc_state->has_audio)
887*179db7c1SJani Nikula 		return;
888*179db7c1SJani Nikula 
8891f31e35fSJani Nikula 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s][ENCODER:%d:%s] Disable audio codec on pipe %c\n",
8901f31e35fSJani Nikula 		    connector->base.id, connector->name,
8911f31e35fSJani Nikula 		    encoder->base.base.id, encoder->base.name, pipe);
8921f31e35fSJani Nikula 
893ca3cfb9dSJani Nikula 	if (dev_priv->audio.funcs)
894ca3cfb9dSJani Nikula 		dev_priv->audio.funcs->audio_codec_disable(encoder,
895df0566a6SJani Nikula 							   old_crtc_state,
896df0566a6SJani Nikula 							   old_conn_state);
897df0566a6SJani Nikula 
898ca3cfb9dSJani Nikula 	mutex_lock(&dev_priv->audio.mutex);
899df0566a6SJani Nikula 	encoder->audio_connector = NULL;
900ca3cfb9dSJani Nikula 	dev_priv->audio.encoder_map[pipe] = NULL;
901ca3cfb9dSJani Nikula 	mutex_unlock(&dev_priv->audio.mutex);
902df0566a6SJani Nikula 
903df0566a6SJani Nikula 	if (acomp && acomp->base.audio_ops &&
904df0566a6SJani Nikula 	    acomp->base.audio_ops->pin_eld_notify) {
905df0566a6SJani Nikula 		/* audio drivers expect pipe = -1 to indicate Non-MST cases */
906df0566a6SJani Nikula 		if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
907df0566a6SJani Nikula 			pipe = -1;
908df0566a6SJani Nikula 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
909df0566a6SJani Nikula 						 (int) port, (int) pipe);
910df0566a6SJani Nikula 	}
911df0566a6SJani Nikula 
912df0566a6SJani Nikula 	intel_lpe_audio_notify(dev_priv, pipe, port, NULL, 0, false);
913df0566a6SJani Nikula }
914df0566a6SJani Nikula 
9150a108bcaSDave Airlie static const struct intel_audio_funcs g4x_audio_funcs = {
9160a108bcaSDave Airlie 	.audio_codec_enable = g4x_audio_codec_enable,
9170a108bcaSDave Airlie 	.audio_codec_disable = g4x_audio_codec_disable,
9180a108bcaSDave Airlie };
9190a108bcaSDave Airlie 
9200a108bcaSDave Airlie static const struct intel_audio_funcs ilk_audio_funcs = {
9210a108bcaSDave Airlie 	.audio_codec_enable = ilk_audio_codec_enable,
9220a108bcaSDave Airlie 	.audio_codec_disable = ilk_audio_codec_disable,
9230a108bcaSDave Airlie };
9240a108bcaSDave Airlie 
9250a108bcaSDave Airlie static const struct intel_audio_funcs hsw_audio_funcs = {
9260a108bcaSDave Airlie 	.audio_codec_enable = hsw_audio_codec_enable,
9270a108bcaSDave Airlie 	.audio_codec_disable = hsw_audio_codec_disable,
9280a108bcaSDave Airlie };
9290a108bcaSDave Airlie 
930df0566a6SJani Nikula /**
931f47a0e35SJani Nikula  * intel_audio_hooks_init - Set up chip specific audio hooks
932df0566a6SJani Nikula  * @dev_priv: device private
933df0566a6SJani Nikula  */
934f47a0e35SJani Nikula void intel_audio_hooks_init(struct drm_i915_private *dev_priv)
935df0566a6SJani Nikula {
936df0566a6SJani Nikula 	if (IS_G4X(dev_priv)) {
937ca3cfb9dSJani Nikula 		dev_priv->audio.funcs = &g4x_audio_funcs;
938df0566a6SJani Nikula 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
939ca3cfb9dSJani Nikula 		dev_priv->audio.funcs = &ilk_audio_funcs;
940005e9537SMatt Roper 	} else if (IS_HASWELL(dev_priv) || DISPLAY_VER(dev_priv) >= 8) {
941ca3cfb9dSJani Nikula 		dev_priv->audio.funcs = &hsw_audio_funcs;
942df0566a6SJani Nikula 	} else if (HAS_PCH_SPLIT(dev_priv)) {
943ca3cfb9dSJani Nikula 		dev_priv->audio.funcs = &ilk_audio_funcs;
944df0566a6SJani Nikula 	}
945df0566a6SJani Nikula }
946df0566a6SJani Nikula 
947112a87c4SKai Vehmanen struct aud_ts_cdclk_m_n {
948112a87c4SKai Vehmanen 	u8 m;
949112a87c4SKai Vehmanen 	u16 n;
950112a87c4SKai Vehmanen };
951112a87c4SKai Vehmanen 
952112a87c4SKai Vehmanen void intel_audio_cdclk_change_pre(struct drm_i915_private *i915)
953112a87c4SKai Vehmanen {
954112a87c4SKai Vehmanen 	if (DISPLAY_VER(i915) >= 13)
955112a87c4SKai Vehmanen 		intel_de_rmw(i915, AUD_TS_CDCLK_M, AUD_TS_CDCLK_M_EN, 0);
956112a87c4SKai Vehmanen }
957112a87c4SKai Vehmanen 
958112a87c4SKai Vehmanen static void get_aud_ts_cdclk_m_n(int refclk, int cdclk, struct aud_ts_cdclk_m_n *aud_ts)
959112a87c4SKai Vehmanen {
960112a87c4SKai Vehmanen 	if (refclk == 24000)
961112a87c4SKai Vehmanen 		aud_ts->m = 12;
962112a87c4SKai Vehmanen 	else
963112a87c4SKai Vehmanen 		aud_ts->m = 15;
964112a87c4SKai Vehmanen 
965112a87c4SKai Vehmanen 	aud_ts->n = cdclk * aud_ts->m / 24000;
966112a87c4SKai Vehmanen }
967112a87c4SKai Vehmanen 
968112a87c4SKai Vehmanen void intel_audio_cdclk_change_post(struct drm_i915_private *i915)
969112a87c4SKai Vehmanen {
970112a87c4SKai Vehmanen 	struct aud_ts_cdclk_m_n aud_ts;
971112a87c4SKai Vehmanen 
972112a87c4SKai Vehmanen 	if (DISPLAY_VER(i915) >= 13) {
973112a87c4SKai Vehmanen 		get_aud_ts_cdclk_m_n(i915->cdclk.hw.ref, i915->cdclk.hw.cdclk, &aud_ts);
974112a87c4SKai Vehmanen 
975112a87c4SKai Vehmanen 		intel_de_write(i915, AUD_TS_CDCLK_N, aud_ts.n);
976112a87c4SKai Vehmanen 		intel_de_write(i915, AUD_TS_CDCLK_M, aud_ts.m | AUD_TS_CDCLK_M_EN);
977112a87c4SKai Vehmanen 		drm_dbg_kms(&i915->drm, "aud_ts_cdclk set to M=%u, N=%u\n", aud_ts.m, aud_ts.n);
978112a87c4SKai Vehmanen 	}
979112a87c4SKai Vehmanen }
980112a87c4SKai Vehmanen 
98128a30b45SVille Syrjälä static int glk_force_audio_cdclk_commit(struct intel_atomic_state *state,
98208e3ed3aSChris Wilson 					struct intel_crtc *crtc,
98328a30b45SVille Syrjälä 					bool enable)
98428a30b45SVille Syrjälä {
98528a30b45SVille Syrjälä 	struct intel_cdclk_state *cdclk_state;
98628a30b45SVille Syrjälä 	int ret;
98728a30b45SVille Syrjälä 
98828a30b45SVille Syrjälä 	/* need to hold at least one crtc lock for the global state */
98928a30b45SVille Syrjälä 	ret = drm_modeset_lock(&crtc->base.mutex, state->base.acquire_ctx);
99028a30b45SVille Syrjälä 	if (ret)
99128a30b45SVille Syrjälä 		return ret;
99228a30b45SVille Syrjälä 
99328a30b45SVille Syrjälä 	cdclk_state = intel_atomic_get_cdclk_state(state);
99428a30b45SVille Syrjälä 	if (IS_ERR(cdclk_state))
99528a30b45SVille Syrjälä 		return PTR_ERR(cdclk_state);
99628a30b45SVille Syrjälä 
99728a30b45SVille Syrjälä 	cdclk_state->force_min_cdclk = enable ? 2 * 96000 : 0;
99828a30b45SVille Syrjälä 
99928a30b45SVille Syrjälä 	return drm_atomic_commit(&state->base);
100028a30b45SVille Syrjälä }
100128a30b45SVille Syrjälä 
1002df0566a6SJani Nikula static void glk_force_audio_cdclk(struct drm_i915_private *dev_priv,
1003df0566a6SJani Nikula 				  bool enable)
1004df0566a6SJani Nikula {
1005df0566a6SJani Nikula 	struct drm_modeset_acquire_ctx ctx;
1006df0566a6SJani Nikula 	struct drm_atomic_state *state;
100708e3ed3aSChris Wilson 	struct intel_crtc *crtc;
1008df0566a6SJani Nikula 	int ret;
1009df0566a6SJani Nikula 
10107d41745aSVille Syrjälä 	crtc = intel_first_crtc(dev_priv);
101108e3ed3aSChris Wilson 	if (!crtc)
101208e3ed3aSChris Wilson 		return;
101308e3ed3aSChris Wilson 
1014df0566a6SJani Nikula 	drm_modeset_acquire_init(&ctx, 0);
1015df0566a6SJani Nikula 	state = drm_atomic_state_alloc(&dev_priv->drm);
10169a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !state))
1017df0566a6SJani Nikula 		return;
1018df0566a6SJani Nikula 
1019df0566a6SJani Nikula 	state->acquire_ctx = &ctx;
1020df0566a6SJani Nikula 
1021df0566a6SJani Nikula retry:
102208e3ed3aSChris Wilson 	ret = glk_force_audio_cdclk_commit(to_intel_atomic_state(state), crtc,
102308e3ed3aSChris Wilson 					   enable);
1024df0566a6SJani Nikula 	if (ret == -EDEADLK) {
1025df0566a6SJani Nikula 		drm_atomic_state_clear(state);
1026df0566a6SJani Nikula 		drm_modeset_backoff(&ctx);
1027df0566a6SJani Nikula 		goto retry;
1028df0566a6SJani Nikula 	}
1029df0566a6SJani Nikula 
10309a3b466bSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, ret);
1031df0566a6SJani Nikula 
1032df0566a6SJani Nikula 	drm_atomic_state_put(state);
1033df0566a6SJani Nikula 
1034df0566a6SJani Nikula 	drm_modeset_drop_locks(&ctx);
1035df0566a6SJani Nikula 	drm_modeset_acquire_fini(&ctx);
1036df0566a6SJani Nikula }
1037df0566a6SJani Nikula 
1038df0566a6SJani Nikula static unsigned long i915_audio_component_get_power(struct device *kdev)
1039df0566a6SJani Nikula {
1040df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1041df0566a6SJani Nikula 	intel_wakeref_t ret;
1042df0566a6SJani Nikula 
1043df0566a6SJani Nikula 	/* Catch potential impedance mismatches before they occur! */
1044df0566a6SJani Nikula 	BUILD_BUG_ON(sizeof(intel_wakeref_t) > sizeof(unsigned long));
1045df0566a6SJani Nikula 
1046615a7724SAnshuman Gupta 	ret = intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO_PLAYBACK);
1047df0566a6SJani Nikula 
1048ca3cfb9dSJani Nikula 	if (dev_priv->audio.power_refcount++ == 0) {
1049005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 9) {
105049e659bcSJani Nikula 			intel_de_write(dev_priv, AUD_FREQ_CNTRL,
1051ca3cfb9dSJani Nikula 				       dev_priv->audio.freq_cntrl);
105263855149SWambui Karuga 			drm_dbg_kms(&dev_priv->drm,
105363855149SWambui Karuga 				    "restored AUD_FREQ_CNTRL to 0x%x\n",
1054ca3cfb9dSJani Nikula 				    dev_priv->audio.freq_cntrl);
105587c16945SKai Vehmanen 		}
105687c16945SKai Vehmanen 
105787c16945SKai Vehmanen 		/* Force CDCLK to 2*BCLK as long as we need audio powered. */
10581ee48a61SKai Vehmanen 		if (IS_GEMINILAKE(dev_priv))
1059df0566a6SJani Nikula 			glk_force_audio_cdclk(dev_priv, true);
10601580d3cdSKai Vehmanen 
10612b5a4562SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 10)
106249e659bcSJani Nikula 			intel_de_write(dev_priv, AUD_PIN_BUF_CTL,
106349e659bcSJani Nikula 				       (intel_de_read(dev_priv, AUD_PIN_BUF_CTL) | AUD_PIN_BUF_ENABLE));
106487c16945SKai Vehmanen 	}
1065df0566a6SJani Nikula 
1066df0566a6SJani Nikula 	return ret;
1067df0566a6SJani Nikula }
1068df0566a6SJani Nikula 
1069df0566a6SJani Nikula static void i915_audio_component_put_power(struct device *kdev,
1070df0566a6SJani Nikula 					   unsigned long cookie)
1071df0566a6SJani Nikula {
1072df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1073df0566a6SJani Nikula 
1074df0566a6SJani Nikula 	/* Stop forcing CDCLK to 2*BCLK if no need for audio to be powered. */
1075ca3cfb9dSJani Nikula 	if (--dev_priv->audio.power_refcount == 0)
10761ee48a61SKai Vehmanen 		if (IS_GEMINILAKE(dev_priv))
1077df0566a6SJani Nikula 			glk_force_audio_cdclk(dev_priv, false);
1078df0566a6SJani Nikula 
1079615a7724SAnshuman Gupta 	intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO_PLAYBACK, cookie);
1080df0566a6SJani Nikula }
1081df0566a6SJani Nikula 
1082df0566a6SJani Nikula static void i915_audio_component_codec_wake_override(struct device *kdev,
1083df0566a6SJani Nikula 						     bool enable)
1084df0566a6SJani Nikula {
1085df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1086df0566a6SJani Nikula 	unsigned long cookie;
1087df0566a6SJani Nikula 	u32 tmp;
1088df0566a6SJani Nikula 
1089005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) < 9)
1090df0566a6SJani Nikula 		return;
1091df0566a6SJani Nikula 
1092df0566a6SJani Nikula 	cookie = i915_audio_component_get_power(kdev);
1093df0566a6SJani Nikula 
1094df0566a6SJani Nikula 	/*
1095df0566a6SJani Nikula 	 * Enable/disable generating the codec wake signal, overriding the
1096df0566a6SJani Nikula 	 * internal logic to generate the codec wake to controller.
1097df0566a6SJani Nikula 	 */
109849e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_CHICKENBIT);
1099df0566a6SJani Nikula 	tmp &= ~SKL_AUD_CODEC_WAKE_SIGNAL;
110049e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_CHICKENBIT, tmp);
1101df0566a6SJani Nikula 	usleep_range(1000, 1500);
1102df0566a6SJani Nikula 
1103df0566a6SJani Nikula 	if (enable) {
110449e659bcSJani Nikula 		tmp = intel_de_read(dev_priv, HSW_AUD_CHICKENBIT);
1105df0566a6SJani Nikula 		tmp |= SKL_AUD_CODEC_WAKE_SIGNAL;
110649e659bcSJani Nikula 		intel_de_write(dev_priv, HSW_AUD_CHICKENBIT, tmp);
1107df0566a6SJani Nikula 		usleep_range(1000, 1500);
1108df0566a6SJani Nikula 	}
1109df0566a6SJani Nikula 
1110df0566a6SJani Nikula 	i915_audio_component_put_power(kdev, cookie);
1111df0566a6SJani Nikula }
1112df0566a6SJani Nikula 
1113df0566a6SJani Nikula /* Get CDCLK in kHz  */
1114df0566a6SJani Nikula static int i915_audio_component_get_cdclk_freq(struct device *kdev)
1115df0566a6SJani Nikula {
1116df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1117df0566a6SJani Nikula 
11189a3b466bSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !HAS_DDI(dev_priv)))
1119df0566a6SJani Nikula 		return -ENODEV;
1120df0566a6SJani Nikula 
1121df0566a6SJani Nikula 	return dev_priv->cdclk.hw.cdclk;
1122df0566a6SJani Nikula }
1123df0566a6SJani Nikula 
1124df0566a6SJani Nikula /*
1125df0566a6SJani Nikula  * get the intel_encoder according to the parameter port and pipe
1126df0566a6SJani Nikula  * intel_encoder is saved by the index of pipe
1127ca3cfb9dSJani Nikula  * MST & (pipe >= 0): return the audio.encoder_map[pipe],
1128df0566a6SJani Nikula  *   when port is matched
1129df0566a6SJani Nikula  * MST & (pipe < 0): this is invalid
1130df0566a6SJani Nikula  * Non-MST & (pipe >= 0): only pipe = 0 (the first device entry)
1131df0566a6SJani Nikula  *   will get the right intel_encoder with port matched
1132df0566a6SJani Nikula  * Non-MST & (pipe < 0): get the right intel_encoder with port matched
1133df0566a6SJani Nikula  */
1134df0566a6SJani Nikula static struct intel_encoder *get_saved_enc(struct drm_i915_private *dev_priv,
1135df0566a6SJani Nikula 					       int port, int pipe)
1136df0566a6SJani Nikula {
1137df0566a6SJani Nikula 	struct intel_encoder *encoder;
1138df0566a6SJani Nikula 
1139df0566a6SJani Nikula 	/* MST */
1140df0566a6SJani Nikula 	if (pipe >= 0) {
11419a3b466bSPankaj Bharadiya 		if (drm_WARN_ON(&dev_priv->drm,
1142ca3cfb9dSJani Nikula 				pipe >= ARRAY_SIZE(dev_priv->audio.encoder_map)))
1143df0566a6SJani Nikula 			return NULL;
1144df0566a6SJani Nikula 
1145ca3cfb9dSJani Nikula 		encoder = dev_priv->audio.encoder_map[pipe];
1146df0566a6SJani Nikula 		/*
1147df0566a6SJani Nikula 		 * when bootup, audio driver may not know it is
1148df0566a6SJani Nikula 		 * MST or not. So it will poll all the port & pipe
1149df0566a6SJani Nikula 		 * combinations
1150df0566a6SJani Nikula 		 */
1151df0566a6SJani Nikula 		if (encoder != NULL && encoder->port == port &&
1152df0566a6SJani Nikula 		    encoder->type == INTEL_OUTPUT_DP_MST)
1153df0566a6SJani Nikula 			return encoder;
1154df0566a6SJani Nikula 	}
1155df0566a6SJani Nikula 
1156df0566a6SJani Nikula 	/* Non-MST */
1157df0566a6SJani Nikula 	if (pipe > 0)
1158df0566a6SJani Nikula 		return NULL;
1159df0566a6SJani Nikula 
1160df0566a6SJani Nikula 	for_each_pipe(dev_priv, pipe) {
1161ca3cfb9dSJani Nikula 		encoder = dev_priv->audio.encoder_map[pipe];
1162df0566a6SJani Nikula 		if (encoder == NULL)
1163df0566a6SJani Nikula 			continue;
1164df0566a6SJani Nikula 
1165df0566a6SJani Nikula 		if (encoder->type == INTEL_OUTPUT_DP_MST)
1166df0566a6SJani Nikula 			continue;
1167df0566a6SJani Nikula 
1168df0566a6SJani Nikula 		if (port == encoder->port)
1169df0566a6SJani Nikula 			return encoder;
1170df0566a6SJani Nikula 	}
1171df0566a6SJani Nikula 
1172df0566a6SJani Nikula 	return NULL;
1173df0566a6SJani Nikula }
1174df0566a6SJani Nikula 
1175df0566a6SJani Nikula static int i915_audio_component_sync_audio_rate(struct device *kdev, int port,
1176df0566a6SJani Nikula 						int pipe, int rate)
1177df0566a6SJani Nikula {
1178df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1179ca3cfb9dSJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio.component;
1180df0566a6SJani Nikula 	struct intel_encoder *encoder;
1181df0566a6SJani Nikula 	struct intel_crtc *crtc;
1182df0566a6SJani Nikula 	unsigned long cookie;
1183df0566a6SJani Nikula 	int err = 0;
1184df0566a6SJani Nikula 
1185df0566a6SJani Nikula 	if (!HAS_DDI(dev_priv))
1186df0566a6SJani Nikula 		return 0;
1187df0566a6SJani Nikula 
1188df0566a6SJani Nikula 	cookie = i915_audio_component_get_power(kdev);
1189ca3cfb9dSJani Nikula 	mutex_lock(&dev_priv->audio.mutex);
1190df0566a6SJani Nikula 
1191df0566a6SJani Nikula 	/* 1. get the pipe */
1192df0566a6SJani Nikula 	encoder = get_saved_enc(dev_priv, port, pipe);
1193df0566a6SJani Nikula 	if (!encoder || !encoder->base.crtc) {
119463855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "Not valid for port %c\n",
119563855149SWambui Karuga 			    port_name(port));
1196df0566a6SJani Nikula 		err = -ENODEV;
1197df0566a6SJani Nikula 		goto unlock;
1198df0566a6SJani Nikula 	}
1199df0566a6SJani Nikula 
1200df0566a6SJani Nikula 	crtc = to_intel_crtc(encoder->base.crtc);
1201df0566a6SJani Nikula 
1202df0566a6SJani Nikula 	/* port must be valid now, otherwise the pipe will be invalid */
1203df0566a6SJani Nikula 	acomp->aud_sample_rate[port] = rate;
1204df0566a6SJani Nikula 
1205df0566a6SJani Nikula 	hsw_audio_config_update(encoder, crtc->config);
1206df0566a6SJani Nikula 
1207df0566a6SJani Nikula  unlock:
1208ca3cfb9dSJani Nikula 	mutex_unlock(&dev_priv->audio.mutex);
1209df0566a6SJani Nikula 	i915_audio_component_put_power(kdev, cookie);
1210df0566a6SJani Nikula 	return err;
1211df0566a6SJani Nikula }
1212df0566a6SJani Nikula 
1213df0566a6SJani Nikula static int i915_audio_component_get_eld(struct device *kdev, int port,
1214df0566a6SJani Nikula 					int pipe, bool *enabled,
1215df0566a6SJani Nikula 					unsigned char *buf, int max_bytes)
1216df0566a6SJani Nikula {
1217df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1218df0566a6SJani Nikula 	struct intel_encoder *intel_encoder;
1219df0566a6SJani Nikula 	const u8 *eld;
1220df0566a6SJani Nikula 	int ret = -EINVAL;
1221df0566a6SJani Nikula 
1222ca3cfb9dSJani Nikula 	mutex_lock(&dev_priv->audio.mutex);
1223df0566a6SJani Nikula 
1224df0566a6SJani Nikula 	intel_encoder = get_saved_enc(dev_priv, port, pipe);
1225df0566a6SJani Nikula 	if (!intel_encoder) {
122663855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "Not valid for port %c\n",
122763855149SWambui Karuga 			    port_name(port));
1228ca3cfb9dSJani Nikula 		mutex_unlock(&dev_priv->audio.mutex);
1229df0566a6SJani Nikula 		return ret;
1230df0566a6SJani Nikula 	}
1231df0566a6SJani Nikula 
1232df0566a6SJani Nikula 	ret = 0;
1233df0566a6SJani Nikula 	*enabled = intel_encoder->audio_connector != NULL;
1234df0566a6SJani Nikula 	if (*enabled) {
1235df0566a6SJani Nikula 		eld = intel_encoder->audio_connector->eld;
1236df0566a6SJani Nikula 		ret = drm_eld_size(eld);
1237df0566a6SJani Nikula 		memcpy(buf, eld, min(max_bytes, ret));
1238df0566a6SJani Nikula 	}
1239df0566a6SJani Nikula 
1240ca3cfb9dSJani Nikula 	mutex_unlock(&dev_priv->audio.mutex);
1241df0566a6SJani Nikula 	return ret;
1242df0566a6SJani Nikula }
1243df0566a6SJani Nikula 
1244df0566a6SJani Nikula static const struct drm_audio_component_ops i915_audio_component_ops = {
1245df0566a6SJani Nikula 	.owner		= THIS_MODULE,
1246df0566a6SJani Nikula 	.get_power	= i915_audio_component_get_power,
1247df0566a6SJani Nikula 	.put_power	= i915_audio_component_put_power,
1248df0566a6SJani Nikula 	.codec_wake_override = i915_audio_component_codec_wake_override,
1249df0566a6SJani Nikula 	.get_cdclk_freq	= i915_audio_component_get_cdclk_freq,
1250df0566a6SJani Nikula 	.sync_audio_rate = i915_audio_component_sync_audio_rate,
1251df0566a6SJani Nikula 	.get_eld	= i915_audio_component_get_eld,
1252df0566a6SJani Nikula };
1253df0566a6SJani Nikula 
1254df0566a6SJani Nikula static int i915_audio_component_bind(struct device *i915_kdev,
1255df0566a6SJani Nikula 				     struct device *hda_kdev, void *data)
1256df0566a6SJani Nikula {
1257df0566a6SJani Nikula 	struct i915_audio_component *acomp = data;
1258df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
1259df0566a6SJani Nikula 	int i;
1260df0566a6SJani Nikula 
12619a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, acomp->base.ops || acomp->base.dev))
1262df0566a6SJani Nikula 		return -EEXIST;
1263df0566a6SJani Nikula 
12649a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
12659a3b466bSPankaj Bharadiya 			!device_link_add(hda_kdev, i915_kdev,
12669a3b466bSPankaj Bharadiya 					 DL_FLAG_STATELESS)))
1267df0566a6SJani Nikula 		return -ENOMEM;
1268df0566a6SJani Nikula 
1269df0566a6SJani Nikula 	drm_modeset_lock_all(&dev_priv->drm);
1270df0566a6SJani Nikula 	acomp->base.ops = &i915_audio_component_ops;
1271df0566a6SJani Nikula 	acomp->base.dev = i915_kdev;
1272df0566a6SJani Nikula 	BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
1273df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
1274df0566a6SJani Nikula 		acomp->aud_sample_rate[i] = 0;
1275ca3cfb9dSJani Nikula 	dev_priv->audio.component = acomp;
1276df0566a6SJani Nikula 	drm_modeset_unlock_all(&dev_priv->drm);
1277df0566a6SJani Nikula 
1278df0566a6SJani Nikula 	return 0;
1279df0566a6SJani Nikula }
1280df0566a6SJani Nikula 
1281df0566a6SJani Nikula static void i915_audio_component_unbind(struct device *i915_kdev,
1282df0566a6SJani Nikula 					struct device *hda_kdev, void *data)
1283df0566a6SJani Nikula {
1284df0566a6SJani Nikula 	struct i915_audio_component *acomp = data;
1285df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
1286df0566a6SJani Nikula 
1287df0566a6SJani Nikula 	drm_modeset_lock_all(&dev_priv->drm);
1288df0566a6SJani Nikula 	acomp->base.ops = NULL;
1289df0566a6SJani Nikula 	acomp->base.dev = NULL;
1290ca3cfb9dSJani Nikula 	dev_priv->audio.component = NULL;
1291df0566a6SJani Nikula 	drm_modeset_unlock_all(&dev_priv->drm);
1292df0566a6SJani Nikula 
1293df0566a6SJani Nikula 	device_link_remove(hda_kdev, i915_kdev);
1294b4ed131dSJani Nikula 
1295ca3cfb9dSJani Nikula 	if (dev_priv->audio.power_refcount)
1296b4ed131dSJani Nikula 		drm_err(&dev_priv->drm, "audio power refcount %d after unbind\n",
1297ca3cfb9dSJani Nikula 			dev_priv->audio.power_refcount);
1298df0566a6SJani Nikula }
1299df0566a6SJani Nikula 
1300df0566a6SJani Nikula static const struct component_ops i915_audio_component_bind_ops = {
1301df0566a6SJani Nikula 	.bind	= i915_audio_component_bind,
1302df0566a6SJani Nikula 	.unbind	= i915_audio_component_unbind,
1303df0566a6SJani Nikula };
1304df0566a6SJani Nikula 
1305989634fbSKai Vehmanen #define AUD_FREQ_TMODE_SHIFT	14
1306989634fbSKai Vehmanen #define AUD_FREQ_4T		0
1307989634fbSKai Vehmanen #define AUD_FREQ_8T		(2 << AUD_FREQ_TMODE_SHIFT)
1308989634fbSKai Vehmanen #define AUD_FREQ_PULLCLKS(x)	(((x) & 0x3) << 11)
1309989634fbSKai Vehmanen #define AUD_FREQ_BCLK_96M	BIT(4)
1310989634fbSKai Vehmanen 
1311989634fbSKai Vehmanen #define AUD_FREQ_GEN12          (AUD_FREQ_8T | AUD_FREQ_PULLCLKS(0) | AUD_FREQ_BCLK_96M)
1312989634fbSKai Vehmanen #define AUD_FREQ_TGL_BROKEN     (AUD_FREQ_8T | AUD_FREQ_PULLCLKS(2) | AUD_FREQ_BCLK_96M)
1313989634fbSKai Vehmanen 
1314df0566a6SJani Nikula /**
1315df0566a6SJani Nikula  * i915_audio_component_init - initialize and register the audio component
1316df0566a6SJani Nikula  * @dev_priv: i915 device instance
1317df0566a6SJani Nikula  *
1318df0566a6SJani Nikula  * This will register with the component framework a child component which
1319df0566a6SJani Nikula  * will bind dynamically to the snd_hda_intel driver's corresponding master
1320df0566a6SJani Nikula  * component when the latter is registered. During binding the child
1321df0566a6SJani Nikula  * initializes an instance of struct i915_audio_component which it receives
1322df0566a6SJani Nikula  * from the master. The master can then start to use the interface defined by
1323df0566a6SJani Nikula  * this struct. Each side can break the binding at any point by deregistering
1324df0566a6SJani Nikula  * its own component after which each side's component unbind callback is
1325df0566a6SJani Nikula  * called.
1326df0566a6SJani Nikula  *
1327df0566a6SJani Nikula  * We ignore any error during registration and continue with reduced
1328df0566a6SJani Nikula  * functionality (i.e. without HDMI audio).
1329df0566a6SJani Nikula  */
1330df0566a6SJani Nikula static void i915_audio_component_init(struct drm_i915_private *dev_priv)
1331df0566a6SJani Nikula {
1332989634fbSKai Vehmanen 	u32 aud_freq, aud_freq_init;
1333df0566a6SJani Nikula 	int ret;
1334df0566a6SJani Nikula 
1335df0566a6SJani Nikula 	ret = component_add_typed(dev_priv->drm.dev,
1336df0566a6SJani Nikula 				  &i915_audio_component_bind_ops,
1337df0566a6SJani Nikula 				  I915_COMPONENT_AUDIO);
1338df0566a6SJani Nikula 	if (ret < 0) {
133963855149SWambui Karuga 		drm_err(&dev_priv->drm,
134063855149SWambui Karuga 			"failed to add audio component (%d)\n", ret);
1341df0566a6SJani Nikula 		/* continue with reduced functionality */
1342df0566a6SJani Nikula 		return;
1343df0566a6SJani Nikula 	}
1344df0566a6SJani Nikula 
1345005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 9) {
1346989634fbSKai Vehmanen 		aud_freq_init = intel_de_read(dev_priv, AUD_FREQ_CNTRL);
1347989634fbSKai Vehmanen 
134827ec10b3SLucas De Marchi 		if (DISPLAY_VER(dev_priv) >= 12)
1349989634fbSKai Vehmanen 			aud_freq = AUD_FREQ_GEN12;
1350989634fbSKai Vehmanen 		else
1351989634fbSKai Vehmanen 			aud_freq = aud_freq_init;
1352989634fbSKai Vehmanen 
1353c6b40ee3SKai-Heng Feng 		/* use BIOS provided value for TGL and RKL unless it is a known bad value */
1354c6b40ee3SKai-Heng Feng 		if ((IS_TIGERLAKE(dev_priv) || IS_ROCKETLAKE(dev_priv)) &&
1355c6b40ee3SKai-Heng Feng 		    aud_freq_init != AUD_FREQ_TGL_BROKEN)
1356989634fbSKai Vehmanen 			aud_freq = aud_freq_init;
1357989634fbSKai Vehmanen 
1358989634fbSKai Vehmanen 		drm_dbg_kms(&dev_priv->drm, "use AUD_FREQ_CNTRL of 0x%x (init value 0x%x)\n",
1359989634fbSKai Vehmanen 			    aud_freq, aud_freq_init);
1360989634fbSKai Vehmanen 
1361ca3cfb9dSJani Nikula 		dev_priv->audio.freq_cntrl = aud_freq;
136287c16945SKai Vehmanen 	}
136387c16945SKai Vehmanen 
1364112a87c4SKai Vehmanen 	/* init with current cdclk */
1365112a87c4SKai Vehmanen 	intel_audio_cdclk_change_post(dev_priv);
1366112a87c4SKai Vehmanen 
1367ca3cfb9dSJani Nikula 	dev_priv->audio.component_registered = true;
1368df0566a6SJani Nikula }
1369df0566a6SJani Nikula 
1370df0566a6SJani Nikula /**
1371df0566a6SJani Nikula  * i915_audio_component_cleanup - deregister the audio component
1372df0566a6SJani Nikula  * @dev_priv: i915 device instance
1373df0566a6SJani Nikula  *
1374df0566a6SJani Nikula  * Deregisters the audio component, breaking any existing binding to the
1375df0566a6SJani Nikula  * corresponding snd_hda_intel driver's master component.
1376df0566a6SJani Nikula  */
1377df0566a6SJani Nikula static void i915_audio_component_cleanup(struct drm_i915_private *dev_priv)
1378df0566a6SJani Nikula {
1379ca3cfb9dSJani Nikula 	if (!dev_priv->audio.component_registered)
1380df0566a6SJani Nikula 		return;
1381df0566a6SJani Nikula 
1382df0566a6SJani Nikula 	component_del(dev_priv->drm.dev, &i915_audio_component_bind_ops);
1383ca3cfb9dSJani Nikula 	dev_priv->audio.component_registered = false;
1384df0566a6SJani Nikula }
1385df0566a6SJani Nikula 
1386df0566a6SJani Nikula /**
1387df0566a6SJani Nikula  * intel_audio_init() - Initialize the audio driver either using
1388df0566a6SJani Nikula  * component framework or using lpe audio bridge
1389df0566a6SJani Nikula  * @dev_priv: the i915 drm device private data
1390df0566a6SJani Nikula  *
1391df0566a6SJani Nikula  */
1392df0566a6SJani Nikula void intel_audio_init(struct drm_i915_private *dev_priv)
1393df0566a6SJani Nikula {
1394df0566a6SJani Nikula 	if (intel_lpe_audio_init(dev_priv) < 0)
1395df0566a6SJani Nikula 		i915_audio_component_init(dev_priv);
1396df0566a6SJani Nikula }
1397df0566a6SJani Nikula 
1398df0566a6SJani Nikula /**
1399df0566a6SJani Nikula  * intel_audio_deinit() - deinitialize the audio driver
1400df0566a6SJani Nikula  * @dev_priv: the i915 drm device private data
1401df0566a6SJani Nikula  *
1402df0566a6SJani Nikula  */
1403df0566a6SJani Nikula void intel_audio_deinit(struct drm_i915_private *dev_priv)
1404df0566a6SJani Nikula {
1405ca3cfb9dSJani Nikula 	if ((dev_priv)->audio.lpe.platdev != NULL)
1406df0566a6SJani Nikula 		intel_lpe_audio_teardown(dev_priv);
1407df0566a6SJani Nikula 	else
1408df0566a6SJani Nikula 		i915_audio_component_cleanup(dev_priv);
1409df0566a6SJani Nikula }
1410