xref: /linux/drivers/gpu/drm/i915/display/intel_audio.c (revision 27ec10b3eb2e14588b2ce5b4c8ea5460e7143179)
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"
341d455f8dSJani Nikula #include "intel_display_types.h"
35df0566a6SJani Nikula #include "intel_lpe_audio.h"
36df0566a6SJani Nikula 
37df0566a6SJani Nikula /**
38df0566a6SJani Nikula  * DOC: High Definition Audio over HDMI and Display Port
39df0566a6SJani Nikula  *
40df0566a6SJani Nikula  * The graphics and audio drivers together support High Definition Audio over
41df0566a6SJani Nikula  * HDMI and Display Port. The audio programming sequences are divided into audio
42df0566a6SJani Nikula  * codec and controller enable and disable sequences. The graphics driver
43df0566a6SJani Nikula  * handles the audio codec sequences, while the audio driver handles the audio
44df0566a6SJani Nikula  * controller sequences.
45df0566a6SJani Nikula  *
46df0566a6SJani Nikula  * The disable sequences must be performed before disabling the transcoder or
47df0566a6SJani Nikula  * port. The enable sequences may only be performed after enabling the
48df0566a6SJani Nikula  * transcoder and port, and after completed link training. Therefore the audio
49df0566a6SJani Nikula  * enable/disable sequences are part of the modeset sequence.
50df0566a6SJani Nikula  *
51df0566a6SJani Nikula  * The codec and controller sequences could be done either parallel or serial,
52df0566a6SJani Nikula  * but generally the ELDV/PD change in the codec sequence indicates to the audio
53df0566a6SJani Nikula  * driver that the controller sequence should start. Indeed, most of the
54df0566a6SJani Nikula  * co-operation between the graphics and audio drivers is handled via audio
55df0566a6SJani Nikula  * related registers. (The notable exception is the power management, not
56df0566a6SJani Nikula  * covered here.)
57df0566a6SJani Nikula  *
58df0566a6SJani Nikula  * The struct &i915_audio_component is used to interact between the graphics
59df0566a6SJani Nikula  * and audio drivers. The struct &i915_audio_component_ops @ops in it is
60df0566a6SJani Nikula  * defined in graphics driver and called in audio driver. The
61df0566a6SJani Nikula  * struct &i915_audio_component_audio_ops @audio_ops is called from i915 driver.
62df0566a6SJani Nikula  */
63df0566a6SJani Nikula 
64df0566a6SJani Nikula /* DP N/M table */
65df0566a6SJani Nikula #define LC_810M	810000
66df0566a6SJani Nikula #define LC_540M	540000
67df0566a6SJani Nikula #define LC_270M	270000
68df0566a6SJani Nikula #define LC_162M	162000
69df0566a6SJani Nikula 
70df0566a6SJani Nikula struct dp_aud_n_m {
71df0566a6SJani Nikula 	int sample_rate;
72df0566a6SJani Nikula 	int clock;
73df0566a6SJani Nikula 	u16 m;
74df0566a6SJani Nikula 	u16 n;
75df0566a6SJani Nikula };
76df0566a6SJani Nikula 
772c291417SAditya Swarup struct hdmi_aud_ncts {
782c291417SAditya Swarup 	int sample_rate;
792c291417SAditya Swarup 	int clock;
802c291417SAditya Swarup 	int n;
812c291417SAditya Swarup 	int cts;
822c291417SAditya Swarup };
832c291417SAditya Swarup 
84df0566a6SJani Nikula /* Values according to DP 1.4 Table 2-104 */
85df0566a6SJani Nikula static const struct dp_aud_n_m dp_aud_n_m[] = {
86df0566a6SJani Nikula 	{ 32000, LC_162M, 1024, 10125 },
87df0566a6SJani Nikula 	{ 44100, LC_162M, 784, 5625 },
88df0566a6SJani Nikula 	{ 48000, LC_162M, 512, 3375 },
89df0566a6SJani Nikula 	{ 64000, LC_162M, 2048, 10125 },
90df0566a6SJani Nikula 	{ 88200, LC_162M, 1568, 5625 },
91df0566a6SJani Nikula 	{ 96000, LC_162M, 1024, 3375 },
92df0566a6SJani Nikula 	{ 128000, LC_162M, 4096, 10125 },
93df0566a6SJani Nikula 	{ 176400, LC_162M, 3136, 5625 },
94df0566a6SJani Nikula 	{ 192000, LC_162M, 2048, 3375 },
95df0566a6SJani Nikula 	{ 32000, LC_270M, 1024, 16875 },
96df0566a6SJani Nikula 	{ 44100, LC_270M, 784, 9375 },
97df0566a6SJani Nikula 	{ 48000, LC_270M, 512, 5625 },
98df0566a6SJani Nikula 	{ 64000, LC_270M, 2048, 16875 },
99df0566a6SJani Nikula 	{ 88200, LC_270M, 1568, 9375 },
100df0566a6SJani Nikula 	{ 96000, LC_270M, 1024, 5625 },
101df0566a6SJani Nikula 	{ 128000, LC_270M, 4096, 16875 },
102df0566a6SJani Nikula 	{ 176400, LC_270M, 3136, 9375 },
103df0566a6SJani Nikula 	{ 192000, LC_270M, 2048, 5625 },
104df0566a6SJani Nikula 	{ 32000, LC_540M, 1024, 33750 },
105df0566a6SJani Nikula 	{ 44100, LC_540M, 784, 18750 },
106df0566a6SJani Nikula 	{ 48000, LC_540M, 512, 11250 },
107df0566a6SJani Nikula 	{ 64000, LC_540M, 2048, 33750 },
108df0566a6SJani Nikula 	{ 88200, LC_540M, 1568, 18750 },
109df0566a6SJani Nikula 	{ 96000, LC_540M, 1024, 11250 },
110df0566a6SJani Nikula 	{ 128000, LC_540M, 4096, 33750 },
111df0566a6SJani Nikula 	{ 176400, LC_540M, 3136, 18750 },
112df0566a6SJani Nikula 	{ 192000, LC_540M, 2048, 11250 },
113df0566a6SJani Nikula 	{ 32000, LC_810M, 1024, 50625 },
114df0566a6SJani Nikula 	{ 44100, LC_810M, 784, 28125 },
115df0566a6SJani Nikula 	{ 48000, LC_810M, 512, 16875 },
116df0566a6SJani Nikula 	{ 64000, LC_810M, 2048, 50625 },
117df0566a6SJani Nikula 	{ 88200, LC_810M, 1568, 28125 },
118df0566a6SJani Nikula 	{ 96000, LC_810M, 1024, 16875 },
119df0566a6SJani Nikula 	{ 128000, LC_810M, 4096, 50625 },
120df0566a6SJani Nikula 	{ 176400, LC_810M, 3136, 28125 },
121df0566a6SJani Nikula 	{ 192000, LC_810M, 2048, 16875 },
122df0566a6SJani Nikula };
123df0566a6SJani Nikula 
124df0566a6SJani Nikula static const struct dp_aud_n_m *
125df0566a6SJani Nikula audio_config_dp_get_n_m(const struct intel_crtc_state *crtc_state, int rate)
126df0566a6SJani Nikula {
127df0566a6SJani Nikula 	int i;
128df0566a6SJani Nikula 
129df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(dp_aud_n_m); i++) {
130df0566a6SJani Nikula 		if (rate == dp_aud_n_m[i].sample_rate &&
131df0566a6SJani Nikula 		    crtc_state->port_clock == dp_aud_n_m[i].clock)
132df0566a6SJani Nikula 			return &dp_aud_n_m[i];
133df0566a6SJani Nikula 	}
134df0566a6SJani Nikula 
135df0566a6SJani Nikula 	return NULL;
136df0566a6SJani Nikula }
137df0566a6SJani Nikula 
138df0566a6SJani Nikula static const struct {
139df0566a6SJani Nikula 	int clock;
140df0566a6SJani Nikula 	u32 config;
141df0566a6SJani Nikula } hdmi_audio_clock[] = {
142df0566a6SJani Nikula 	{ 25175, AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
143df0566a6SJani Nikula 	{ 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
144df0566a6SJani Nikula 	{ 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
145df0566a6SJani Nikula 	{ 27027, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
146df0566a6SJani Nikula 	{ 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
147df0566a6SJani Nikula 	{ 54054, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
148df0566a6SJani Nikula 	{ 74176, AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
149df0566a6SJani Nikula 	{ 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
150df0566a6SJani Nikula 	{ 148352, AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
151df0566a6SJani Nikula 	{ 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
1521aae3065SKai Vehmanen 	{ 296703, AUD_CONFIG_PIXEL_CLOCK_HDMI_296703 },
1531aae3065SKai Vehmanen 	{ 297000, AUD_CONFIG_PIXEL_CLOCK_HDMI_297000 },
1541aae3065SKai Vehmanen 	{ 593407, AUD_CONFIG_PIXEL_CLOCK_HDMI_593407 },
1551aae3065SKai Vehmanen 	{ 594000, AUD_CONFIG_PIXEL_CLOCK_HDMI_594000 },
156df0566a6SJani Nikula };
157df0566a6SJani Nikula 
158df0566a6SJani Nikula /* HDMI N/CTS table */
159df0566a6SJani Nikula #define TMDS_297M 297000
160df0566a6SJani Nikula #define TMDS_296M 296703
161df0566a6SJani Nikula #define TMDS_594M 594000
162df0566a6SJani Nikula #define TMDS_593M 593407
163df0566a6SJani Nikula 
1642c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_24bpp[] = {
165df0566a6SJani Nikula 	{ 32000, TMDS_296M, 5824, 421875 },
166df0566a6SJani Nikula 	{ 32000, TMDS_297M, 3072, 222750 },
167df0566a6SJani Nikula 	{ 32000, TMDS_593M, 5824, 843750 },
168df0566a6SJani Nikula 	{ 32000, TMDS_594M, 3072, 445500 },
169df0566a6SJani Nikula 	{ 44100, TMDS_296M, 4459, 234375 },
170df0566a6SJani Nikula 	{ 44100, TMDS_297M, 4704, 247500 },
171df0566a6SJani Nikula 	{ 44100, TMDS_593M, 8918, 937500 },
172df0566a6SJani Nikula 	{ 44100, TMDS_594M, 9408, 990000 },
173df0566a6SJani Nikula 	{ 88200, TMDS_296M, 8918, 234375 },
174df0566a6SJani Nikula 	{ 88200, TMDS_297M, 9408, 247500 },
175df0566a6SJani Nikula 	{ 88200, TMDS_593M, 17836, 937500 },
176df0566a6SJani Nikula 	{ 88200, TMDS_594M, 18816, 990000 },
177df0566a6SJani Nikula 	{ 176400, TMDS_296M, 17836, 234375 },
178df0566a6SJani Nikula 	{ 176400, TMDS_297M, 18816, 247500 },
179df0566a6SJani Nikula 	{ 176400, TMDS_593M, 35672, 937500 },
180df0566a6SJani Nikula 	{ 176400, TMDS_594M, 37632, 990000 },
181df0566a6SJani Nikula 	{ 48000, TMDS_296M, 5824, 281250 },
182df0566a6SJani Nikula 	{ 48000, TMDS_297M, 5120, 247500 },
183df0566a6SJani Nikula 	{ 48000, TMDS_593M, 5824, 562500 },
184df0566a6SJani Nikula 	{ 48000, TMDS_594M, 6144, 594000 },
185df0566a6SJani Nikula 	{ 96000, TMDS_296M, 11648, 281250 },
186df0566a6SJani Nikula 	{ 96000, TMDS_297M, 10240, 247500 },
187df0566a6SJani Nikula 	{ 96000, TMDS_593M, 11648, 562500 },
188df0566a6SJani Nikula 	{ 96000, TMDS_594M, 12288, 594000 },
189df0566a6SJani Nikula 	{ 192000, TMDS_296M, 23296, 281250 },
190df0566a6SJani Nikula 	{ 192000, TMDS_297M, 20480, 247500 },
191df0566a6SJani Nikula 	{ 192000, TMDS_593M, 23296, 562500 },
192df0566a6SJani Nikula 	{ 192000, TMDS_594M, 24576, 594000 },
193df0566a6SJani Nikula };
194df0566a6SJani Nikula 
1952c291417SAditya Swarup /* Appendix C - N & CTS values for deep color from HDMI 2.0 spec*/
1962c291417SAditya Swarup /* HDMI N/CTS table for 10 bit deep color(30 bpp)*/
1972c291417SAditya Swarup #define TMDS_371M 371250
1982c291417SAditya Swarup #define TMDS_370M 370878
1992c291417SAditya Swarup 
2002c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_30bpp[] = {
2012c291417SAditya Swarup 	{ 32000, TMDS_370M, 5824, 527344 },
2022c291417SAditya Swarup 	{ 32000, TMDS_371M, 6144, 556875 },
2032c291417SAditya Swarup 	{ 44100, TMDS_370M, 8918, 585938 },
2042c291417SAditya Swarup 	{ 44100, TMDS_371M, 4704, 309375 },
2052c291417SAditya Swarup 	{ 88200, TMDS_370M, 17836, 585938 },
2062c291417SAditya Swarup 	{ 88200, TMDS_371M, 9408, 309375 },
2072c291417SAditya Swarup 	{ 176400, TMDS_370M, 35672, 585938 },
2082c291417SAditya Swarup 	{ 176400, TMDS_371M, 18816, 309375 },
2092c291417SAditya Swarup 	{ 48000, TMDS_370M, 11648, 703125 },
2102c291417SAditya Swarup 	{ 48000, TMDS_371M, 5120, 309375 },
2112c291417SAditya Swarup 	{ 96000, TMDS_370M, 23296, 703125 },
2122c291417SAditya Swarup 	{ 96000, TMDS_371M, 10240, 309375 },
2132c291417SAditya Swarup 	{ 192000, TMDS_370M, 46592, 703125 },
2142c291417SAditya Swarup 	{ 192000, TMDS_371M, 20480, 309375 },
2152c291417SAditya Swarup };
2162c291417SAditya Swarup 
2172c291417SAditya Swarup /* HDMI N/CTS table for 12 bit deep color(36 bpp)*/
2182c291417SAditya Swarup #define TMDS_445_5M 445500
2192c291417SAditya Swarup #define TMDS_445M 445054
2202c291417SAditya Swarup 
2212c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_36bpp[] = {
2222c291417SAditya Swarup 	{ 32000, TMDS_445M, 5824, 632813 },
2232c291417SAditya Swarup 	{ 32000, TMDS_445_5M, 4096, 445500 },
2242c291417SAditya Swarup 	{ 44100, TMDS_445M, 8918, 703125 },
2252c291417SAditya Swarup 	{ 44100, TMDS_445_5M, 4704, 371250 },
2262c291417SAditya Swarup 	{ 88200, TMDS_445M, 17836, 703125 },
2272c291417SAditya Swarup 	{ 88200, TMDS_445_5M, 9408, 371250 },
2282c291417SAditya Swarup 	{ 176400, TMDS_445M, 35672, 703125 },
2292c291417SAditya Swarup 	{ 176400, TMDS_445_5M, 18816, 371250 },
2302c291417SAditya Swarup 	{ 48000, TMDS_445M, 5824, 421875 },
2312c291417SAditya Swarup 	{ 48000, TMDS_445_5M, 5120, 371250 },
2322c291417SAditya Swarup 	{ 96000, TMDS_445M, 11648, 421875 },
2332c291417SAditya Swarup 	{ 96000, TMDS_445_5M, 10240, 371250 },
2342c291417SAditya Swarup 	{ 192000, TMDS_445M, 23296, 421875 },
2352c291417SAditya Swarup 	{ 192000, TMDS_445_5M, 20480, 371250 },
2362c291417SAditya Swarup };
2372c291417SAditya Swarup 
238df0566a6SJani Nikula /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
239df0566a6SJani Nikula static u32 audio_config_hdmi_pixel_clock(const struct intel_crtc_state *crtc_state)
240df0566a6SJani Nikula {
2411aae3065SKai Vehmanen 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
242df0566a6SJani Nikula 	const struct drm_display_mode *adjusted_mode =
2431326a92cSMaarten Lankhorst 		&crtc_state->hw.adjusted_mode;
244df0566a6SJani Nikula 	int i;
245df0566a6SJani Nikula 
246df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
247df0566a6SJani Nikula 		if (adjusted_mode->crtc_clock == hdmi_audio_clock[i].clock)
248df0566a6SJani Nikula 			break;
249df0566a6SJani Nikula 	}
250df0566a6SJani Nikula 
251005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) < 12 && adjusted_mode->crtc_clock > 148500)
2521aae3065SKai Vehmanen 		i = ARRAY_SIZE(hdmi_audio_clock);
2531aae3065SKai Vehmanen 
254df0566a6SJani Nikula 	if (i == ARRAY_SIZE(hdmi_audio_clock)) {
2559282a66cSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
2569282a66cSJani Nikula 			    "HDMI audio pixel clock setting for %d not found, falling back to defaults\n",
257df0566a6SJani Nikula 			    adjusted_mode->crtc_clock);
258df0566a6SJani Nikula 		i = 1;
259df0566a6SJani Nikula 	}
260df0566a6SJani Nikula 
2619282a66cSJani Nikula 	drm_dbg_kms(&dev_priv->drm,
2629282a66cSJani Nikula 		    "Configuring HDMI audio for pixel clock %d (0x%08x)\n",
263df0566a6SJani Nikula 		    hdmi_audio_clock[i].clock,
264df0566a6SJani Nikula 		    hdmi_audio_clock[i].config);
265df0566a6SJani Nikula 
266df0566a6SJani Nikula 	return hdmi_audio_clock[i].config;
267df0566a6SJani Nikula }
268df0566a6SJani Nikula 
269df0566a6SJani Nikula static int audio_config_hdmi_get_n(const struct intel_crtc_state *crtc_state,
270df0566a6SJani Nikula 				   int rate)
271df0566a6SJani Nikula {
2722c291417SAditya Swarup 	const struct hdmi_aud_ncts *hdmi_ncts_table;
2732c291417SAditya Swarup 	int i, size;
274df0566a6SJani Nikula 
2752c291417SAditya Swarup 	if (crtc_state->pipe_bpp == 36) {
2762c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_36bpp;
2772c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_36bpp);
2782c291417SAditya Swarup 	} else if (crtc_state->pipe_bpp == 30) {
2792c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_30bpp;
2802c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_30bpp);
2812c291417SAditya Swarup 	} else {
2822c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_24bpp;
2832c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_24bpp);
2842c291417SAditya Swarup 	}
2852c291417SAditya Swarup 
2862c291417SAditya Swarup 	for (i = 0; i < size; i++) {
2872c291417SAditya Swarup 		if (rate == hdmi_ncts_table[i].sample_rate &&
2882c291417SAditya Swarup 		    crtc_state->port_clock == hdmi_ncts_table[i].clock) {
2892c291417SAditya Swarup 			return hdmi_ncts_table[i].n;
290df0566a6SJani Nikula 		}
291df0566a6SJani Nikula 	}
292df0566a6SJani Nikula 	return 0;
293df0566a6SJani Nikula }
294df0566a6SJani Nikula 
295df0566a6SJani Nikula static bool intel_eld_uptodate(struct drm_connector *connector,
296df0566a6SJani Nikula 			       i915_reg_t reg_eldv, u32 bits_eldv,
297df0566a6SJani Nikula 			       i915_reg_t reg_elda, u32 bits_elda,
298df0566a6SJani Nikula 			       i915_reg_t reg_edid)
299df0566a6SJani Nikula {
300df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
301df0566a6SJani Nikula 	const u8 *eld = connector->eld;
302df0566a6SJani Nikula 	u32 tmp;
303df0566a6SJani Nikula 	int i;
304df0566a6SJani Nikula 
30549e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, reg_eldv);
306df0566a6SJani Nikula 	tmp &= bits_eldv;
307df0566a6SJani Nikula 
308df0566a6SJani Nikula 	if (!tmp)
309df0566a6SJani Nikula 		return false;
310df0566a6SJani Nikula 
31149e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, reg_elda);
312df0566a6SJani Nikula 	tmp &= ~bits_elda;
31349e659bcSJani Nikula 	intel_de_write(dev_priv, reg_elda, tmp);
314df0566a6SJani Nikula 
315df0566a6SJani Nikula 	for (i = 0; i < drm_eld_size(eld) / 4; i++)
31649e659bcSJani Nikula 		if (intel_de_read(dev_priv, reg_edid) != *((const u32 *)eld + i))
317df0566a6SJani Nikula 			return false;
318df0566a6SJani Nikula 
319df0566a6SJani Nikula 	return true;
320df0566a6SJani Nikula }
321df0566a6SJani Nikula 
322df0566a6SJani Nikula static void g4x_audio_codec_disable(struct intel_encoder *encoder,
323df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
324df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
325df0566a6SJani Nikula {
326df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
327df0566a6SJani Nikula 	u32 eldv, tmp;
328df0566a6SJani Nikula 
32963855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "Disable audio codec\n");
330df0566a6SJani Nikula 
33149e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_VID_DID);
332df0566a6SJani Nikula 	if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
333df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCL_DEVBLC;
334df0566a6SJani Nikula 	else
335df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCTG;
336df0566a6SJani Nikula 
337df0566a6SJani Nikula 	/* Invalidate ELD */
33849e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_CNTL_ST);
339df0566a6SJani Nikula 	tmp &= ~eldv;
34049e659bcSJani Nikula 	intel_de_write(dev_priv, G4X_AUD_CNTL_ST, tmp);
341df0566a6SJani Nikula }
342df0566a6SJani Nikula 
343df0566a6SJani Nikula static void g4x_audio_codec_enable(struct intel_encoder *encoder,
344df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
345df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
346df0566a6SJani Nikula {
347df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
348df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
349df0566a6SJani Nikula 	const u8 *eld = connector->eld;
350df0566a6SJani Nikula 	u32 eldv;
351df0566a6SJani Nikula 	u32 tmp;
352df0566a6SJani Nikula 	int len, i;
353df0566a6SJani Nikula 
35463855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "Enable audio codec, %u bytes ELD\n",
35563855149SWambui Karuga 		    drm_eld_size(eld));
356df0566a6SJani Nikula 
35749e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_VID_DID);
358df0566a6SJani Nikula 	if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
359df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCL_DEVBLC;
360df0566a6SJani Nikula 	else
361df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCTG;
362df0566a6SJani Nikula 
363df0566a6SJani Nikula 	if (intel_eld_uptodate(connector,
364df0566a6SJani Nikula 			       G4X_AUD_CNTL_ST, eldv,
365df0566a6SJani Nikula 			       G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
366df0566a6SJani Nikula 			       G4X_HDMIW_HDMIEDID))
367df0566a6SJani Nikula 		return;
368df0566a6SJani Nikula 
36949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_CNTL_ST);
370df0566a6SJani Nikula 	tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
371df0566a6SJani Nikula 	len = (tmp >> 9) & 0x1f;		/* ELD buffer size */
37249e659bcSJani Nikula 	intel_de_write(dev_priv, G4X_AUD_CNTL_ST, tmp);
373df0566a6SJani Nikula 
374df0566a6SJani Nikula 	len = min(drm_eld_size(eld) / 4, len);
37563855149SWambui Karuga 	drm_dbg(&dev_priv->drm, "ELD size %d\n", len);
376df0566a6SJani Nikula 	for (i = 0; i < len; i++)
37749e659bcSJani Nikula 		intel_de_write(dev_priv, G4X_HDMIW_HDMIEDID,
37849e659bcSJani Nikula 			       *((const u32 *)eld + i));
379df0566a6SJani Nikula 
38049e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_CNTL_ST);
381df0566a6SJani Nikula 	tmp |= eldv;
38249e659bcSJani Nikula 	intel_de_write(dev_priv, G4X_AUD_CNTL_ST, tmp);
383df0566a6SJani Nikula }
384df0566a6SJani Nikula 
385df0566a6SJani Nikula static void
386df0566a6SJani Nikula hsw_dp_audio_config_update(struct intel_encoder *encoder,
387df0566a6SJani Nikula 			   const struct intel_crtc_state *crtc_state)
388df0566a6SJani Nikula {
389df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
390df0566a6SJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio_component;
391df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
392df0566a6SJani Nikula 	enum port port = encoder->port;
393df0566a6SJani Nikula 	const struct dp_aud_n_m *nm;
394df0566a6SJani Nikula 	int rate;
395df0566a6SJani Nikula 	u32 tmp;
396df0566a6SJani Nikula 
397df0566a6SJani Nikula 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
398df0566a6SJani Nikula 	nm = audio_config_dp_get_n_m(crtc_state, rate);
399df0566a6SJani Nikula 	if (nm)
40063855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using Maud %u, Naud %u\n", nm->m,
40163855149SWambui Karuga 			    nm->n);
402df0566a6SJani Nikula 	else
40363855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using automatic Maud, Naud\n");
404df0566a6SJani Nikula 
40549e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_CFG(cpu_transcoder));
406df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
407df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
408df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
409df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_VALUE_INDEX;
410df0566a6SJani Nikula 
411df0566a6SJani Nikula 	if (nm) {
412df0566a6SJani Nikula 		tmp &= ~AUD_CONFIG_N_MASK;
413df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N(nm->n);
414df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
415df0566a6SJani Nikula 	}
416df0566a6SJani Nikula 
41749e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_CFG(cpu_transcoder), tmp);
418df0566a6SJani Nikula 
41949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
420df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_M_MASK;
421df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
422df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
423df0566a6SJani Nikula 
424df0566a6SJani Nikula 	if (nm) {
425df0566a6SJani Nikula 		tmp |= nm->m;
426df0566a6SJani Nikula 		tmp |= AUD_M_CTS_M_VALUE_INDEX;
427df0566a6SJani Nikula 		tmp |= AUD_M_CTS_M_PROG_ENABLE;
428df0566a6SJani Nikula 	}
429df0566a6SJani Nikula 
43049e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
431df0566a6SJani Nikula }
432df0566a6SJani Nikula 
433df0566a6SJani Nikula static void
434df0566a6SJani Nikula hsw_hdmi_audio_config_update(struct intel_encoder *encoder,
435df0566a6SJani Nikula 			     const struct intel_crtc_state *crtc_state)
436df0566a6SJani Nikula {
437df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
438df0566a6SJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio_component;
439df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
440df0566a6SJani Nikula 	enum port port = encoder->port;
441df0566a6SJani Nikula 	int n, rate;
442df0566a6SJani Nikula 	u32 tmp;
443df0566a6SJani Nikula 
444df0566a6SJani Nikula 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
445df0566a6SJani Nikula 
44649e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_CFG(cpu_transcoder));
447df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
448df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
449df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
450df0566a6SJani Nikula 	tmp |= audio_config_hdmi_pixel_clock(crtc_state);
451df0566a6SJani Nikula 
452df0566a6SJani Nikula 	n = audio_config_hdmi_get_n(crtc_state, rate);
453df0566a6SJani Nikula 	if (n != 0) {
45463855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using N %d\n", n);
455df0566a6SJani Nikula 
456df0566a6SJani Nikula 		tmp &= ~AUD_CONFIG_N_MASK;
457df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N(n);
458df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
459df0566a6SJani Nikula 	} else {
46063855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using automatic N\n");
461df0566a6SJani Nikula 	}
462df0566a6SJani Nikula 
46349e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_CFG(cpu_transcoder), tmp);
464df0566a6SJani Nikula 
465df0566a6SJani Nikula 	/*
466df0566a6SJani Nikula 	 * Let's disable "Enable CTS or M Prog bit"
467df0566a6SJani Nikula 	 * and let HW calculate the value
468df0566a6SJani Nikula 	 */
46949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
470df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
471df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
47249e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
473df0566a6SJani Nikula }
474df0566a6SJani Nikula 
475df0566a6SJani Nikula static void
476df0566a6SJani Nikula hsw_audio_config_update(struct intel_encoder *encoder,
477df0566a6SJani Nikula 			const struct intel_crtc_state *crtc_state)
478df0566a6SJani Nikula {
479df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state))
480df0566a6SJani Nikula 		hsw_dp_audio_config_update(encoder, crtc_state);
481df0566a6SJani Nikula 	else
482df0566a6SJani Nikula 		hsw_hdmi_audio_config_update(encoder, crtc_state);
483df0566a6SJani Nikula }
484df0566a6SJani Nikula 
485df0566a6SJani Nikula static void hsw_audio_codec_disable(struct intel_encoder *encoder,
486df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
487df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
488df0566a6SJani Nikula {
489df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
490df0566a6SJani Nikula 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
491df0566a6SJani Nikula 	u32 tmp;
492df0566a6SJani Nikula 
49363855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "Disable audio codec on transcoder %s\n",
494df0566a6SJani Nikula 		    transcoder_name(cpu_transcoder));
495df0566a6SJani Nikula 
496df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
497df0566a6SJani Nikula 
498df0566a6SJani Nikula 	/* Disable timestamps */
49949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_CFG(cpu_transcoder));
500df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
501df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_PROG_ENABLE;
502df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_UPPER_N_MASK;
503df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_LOWER_N_MASK;
504df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(old_crtc_state))
505df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
50649e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_CFG(cpu_transcoder), tmp);
507df0566a6SJani Nikula 
508df0566a6SJani Nikula 	/* Invalidate ELD */
50949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD);
510df0566a6SJani Nikula 	tmp &= ~AUDIO_ELD_VALID(cpu_transcoder);
511df0566a6SJani Nikula 	tmp &= ~AUDIO_OUTPUT_ENABLE(cpu_transcoder);
51249e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_PIN_ELD_CP_VLD, tmp);
513df0566a6SJani Nikula 
514df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
515df0566a6SJani Nikula }
516df0566a6SJani Nikula 
5172dd43144SVille Syrjälä static unsigned int calc_hblank_early_prog(struct intel_encoder *encoder,
51848b8b04cSUma Shankar 					   const struct intel_crtc_state *crtc_state)
51948b8b04cSUma Shankar {
52048b8b04cSUma Shankar 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
52148b8b04cSUma Shankar 	unsigned int link_clks_available, link_clks_required;
52248b8b04cSUma Shankar 	unsigned int tu_data, tu_line, link_clks_active;
523d19b29beSVille Syrjälä 	unsigned int h_active, h_total, hblank_delta, pixel_clk;
524d19b29beSVille Syrjälä 	unsigned int fec_coeff, cdclk, vdsc_bpp;
52541ee86d6SVille Syrjälä 	unsigned int link_clk, lanes;
5262dd43144SVille Syrjälä 	unsigned int hblank_rise;
52748b8b04cSUma Shankar 
52848b8b04cSUma Shankar 	h_active = crtc_state->hw.adjusted_mode.crtc_hdisplay;
52948b8b04cSUma Shankar 	h_total = crtc_state->hw.adjusted_mode.crtc_htotal;
53048b8b04cSUma Shankar 	pixel_clk = crtc_state->hw.adjusted_mode.crtc_clock;
53148b8b04cSUma Shankar 	vdsc_bpp = crtc_state->dsc.compressed_bpp;
53248b8b04cSUma Shankar 	cdclk = i915->cdclk.hw.cdclk;
53348b8b04cSUma Shankar 	/* fec= 0.972261, using rounding multiplier of 1000000 */
53448b8b04cSUma Shankar 	fec_coeff = 972261;
53541ee86d6SVille Syrjälä 	link_clk = crtc_state->port_clock;
53641ee86d6SVille Syrjälä 	lanes = crtc_state->lane_count;
53748b8b04cSUma Shankar 
53848b8b04cSUma Shankar 	drm_dbg_kms(&i915->drm, "h_active = %u link_clk = %u :"
53948b8b04cSUma Shankar 		    "lanes = %u vdsc_bpp = %u cdclk = %u\n",
54041ee86d6SVille Syrjälä 		    h_active, link_clk, lanes, vdsc_bpp, cdclk);
54148b8b04cSUma Shankar 
5422dd43144SVille Syrjälä 	if (WARN_ON(!link_clk || !pixel_clk || !lanes || !vdsc_bpp || !cdclk))
54311ebc232SJani Nikula 		return 0;
54411ebc232SJani Nikula 
5452dd43144SVille Syrjälä 	link_clks_available = (h_total - h_active) * link_clk / pixel_clk - 28;
5462dd43144SVille Syrjälä 	link_clks_required = DIV_ROUND_UP(192000 * h_total, 1000 * pixel_clk) * (48 / lanes + 2);
54748b8b04cSUma Shankar 
54848b8b04cSUma Shankar 	if (link_clks_available > link_clks_required)
54948b8b04cSUma Shankar 		hblank_delta = 32;
55048b8b04cSUma Shankar 	else
5512dd43144SVille Syrjälä 		hblank_delta = DIV64_U64_ROUND_UP(mul_u32_u32(5 * (link_clk + cdclk), pixel_clk),
5522dd43144SVille Syrjälä 						  mul_u32_u32(link_clk, cdclk));
55348b8b04cSUma Shankar 
5542dd43144SVille Syrjälä 	tu_data = div64_u64(mul_u32_u32(pixel_clk * vdsc_bpp * 8, 1000000),
5552dd43144SVille Syrjälä 			    mul_u32_u32(link_clk * lanes, fec_coeff));
5562dd43144SVille Syrjälä 	tu_line = div64_u64(h_active * mul_u32_u32(link_clk, fec_coeff),
5572dd43144SVille Syrjälä 			    mul_u32_u32(64 * pixel_clk, 1000000));
55848b8b04cSUma Shankar 	link_clks_active  = (tu_line - 1) * 64 + tu_data;
55948b8b04cSUma Shankar 
5602dd43144SVille Syrjälä 	hblank_rise = (link_clks_active + 6 * DIV_ROUND_UP(link_clks_active, 250) + 4) * pixel_clk / link_clk;
56148b8b04cSUma Shankar 
5622dd43144SVille Syrjälä 	return h_active - hblank_rise + hblank_delta;
56348b8b04cSUma Shankar }
56448b8b04cSUma Shankar 
5652dd43144SVille Syrjälä static unsigned int calc_samples_room(const struct intel_crtc_state *crtc_state)
56648b8b04cSUma Shankar {
56748b8b04cSUma Shankar 	unsigned int h_active, h_total, pixel_clk;
56841ee86d6SVille Syrjälä 	unsigned int link_clk, lanes;
56948b8b04cSUma Shankar 
57048b8b04cSUma Shankar 	h_active = crtc_state->hw.adjusted_mode.hdisplay;
57148b8b04cSUma Shankar 	h_total = crtc_state->hw.adjusted_mode.htotal;
57248b8b04cSUma Shankar 	pixel_clk = crtc_state->hw.adjusted_mode.clock;
57341ee86d6SVille Syrjälä 	link_clk = crtc_state->port_clock;
57441ee86d6SVille Syrjälä 	lanes = crtc_state->lane_count;
57548b8b04cSUma Shankar 
5762dd43144SVille Syrjälä 	return ((h_total - h_active) * link_clk - 12 * pixel_clk) /
5772dd43144SVille Syrjälä 		(pixel_clk * (48 / lanes + 2));
57848b8b04cSUma Shankar }
57948b8b04cSUma Shankar 
58048b8b04cSUma Shankar static void enable_audio_dsc_wa(struct intel_encoder *encoder,
58148b8b04cSUma Shankar 				const struct intel_crtc_state *crtc_state)
58248b8b04cSUma Shankar {
58348b8b04cSUma Shankar 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
58448b8b04cSUma Shankar 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
58548b8b04cSUma Shankar 	enum pipe pipe = crtc->pipe;
58611ebc232SJani Nikula 	unsigned int hblank_early_prog, samples_room;
58748b8b04cSUma Shankar 	unsigned int val;
58848b8b04cSUma Shankar 
589005e9537SMatt Roper 	if (DISPLAY_VER(i915) < 11)
59048b8b04cSUma Shankar 		return;
59148b8b04cSUma Shankar 
59248b8b04cSUma Shankar 	val = intel_de_read(i915, AUD_CONFIG_BE);
59348b8b04cSUma Shankar 
594005e9537SMatt Roper 	if (IS_DISPLAY_VER(i915, 11))
59548b8b04cSUma Shankar 		val |= HBLANK_EARLY_ENABLE_ICL(pipe);
596005e9537SMatt Roper 	else if (DISPLAY_VER(i915) >= 12)
59748b8b04cSUma Shankar 		val |= HBLANK_EARLY_ENABLE_TGL(pipe);
59848b8b04cSUma Shankar 
59948b8b04cSUma Shankar 	if (crtc_state->dsc.compression_enable &&
60048b8b04cSUma Shankar 	    (crtc_state->hw.adjusted_mode.hdisplay >= 3840 &&
60148b8b04cSUma Shankar 	    crtc_state->hw.adjusted_mode.vdisplay >= 2160)) {
60248b8b04cSUma Shankar 		/* Get hblank early enable value required */
6032dd43144SVille Syrjälä 		hblank_early_prog = calc_hblank_early_prog(encoder, crtc_state);
60448b8b04cSUma Shankar 		if (hblank_early_prog < 32) {
60548b8b04cSUma Shankar 			val &= ~HBLANK_START_COUNT_MASK(pipe);
60648b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_32);
60748b8b04cSUma Shankar 		} else if (hblank_early_prog < 64) {
60848b8b04cSUma Shankar 			val &= ~HBLANK_START_COUNT_MASK(pipe);
60948b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_64);
61048b8b04cSUma Shankar 		} else if (hblank_early_prog < 96) {
61148b8b04cSUma Shankar 			val &= ~HBLANK_START_COUNT_MASK(pipe);
61248b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_96);
61348b8b04cSUma Shankar 		} else {
61448b8b04cSUma Shankar 			val &= ~HBLANK_START_COUNT_MASK(pipe);
61548b8b04cSUma Shankar 			val |= HBLANK_START_COUNT(pipe, HBLANK_START_COUNT_128);
61648b8b04cSUma Shankar 		}
61748b8b04cSUma Shankar 
61848b8b04cSUma Shankar 		/* Get samples room value required */
6192dd43144SVille Syrjälä 		samples_room = calc_samples_room(crtc_state);
62048b8b04cSUma Shankar 		if (samples_room < 3) {
62148b8b04cSUma Shankar 			val &= ~NUMBER_SAMPLES_PER_LINE_MASK(pipe);
62248b8b04cSUma Shankar 			val |= NUMBER_SAMPLES_PER_LINE(pipe, samples_room);
62348b8b04cSUma Shankar 		} else {
62448b8b04cSUma Shankar 			/* Program 0 i.e "All Samples available in buffer" */
62548b8b04cSUma Shankar 			val &= ~NUMBER_SAMPLES_PER_LINE_MASK(pipe);
62648b8b04cSUma Shankar 			val |= NUMBER_SAMPLES_PER_LINE(pipe, 0x0);
62748b8b04cSUma Shankar 		}
62848b8b04cSUma Shankar 	}
62948b8b04cSUma Shankar 
63048b8b04cSUma Shankar 	intel_de_write(i915, AUD_CONFIG_BE, val);
63148b8b04cSUma Shankar }
63248b8b04cSUma Shankar 
63348b8b04cSUma Shankar #undef ROUNDING_FACTOR
63448b8b04cSUma Shankar 
635df0566a6SJani Nikula static void hsw_audio_codec_enable(struct intel_encoder *encoder,
636df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
637df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
638df0566a6SJani Nikula {
639df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
640df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
641df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
642df0566a6SJani Nikula 	const u8 *eld = connector->eld;
643df0566a6SJani Nikula 	u32 tmp;
644df0566a6SJani Nikula 	int len, i;
645df0566a6SJani Nikula 
64663855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
64763855149SWambui Karuga 		    "Enable audio codec on transcoder %s, %u bytes ELD\n",
648df0566a6SJani Nikula 		     transcoder_name(cpu_transcoder), drm_eld_size(eld));
649df0566a6SJani Nikula 
650df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
651df0566a6SJani Nikula 
65248b8b04cSUma Shankar 	/* Enable Audio WA for 4k DSC usecases */
65348b8b04cSUma Shankar 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP))
65448b8b04cSUma Shankar 		enable_audio_dsc_wa(encoder, crtc_state);
65548b8b04cSUma Shankar 
656df0566a6SJani Nikula 	/* Enable audio presence detect, invalidate ELD */
65749e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD);
658df0566a6SJani Nikula 	tmp |= AUDIO_OUTPUT_ENABLE(cpu_transcoder);
659df0566a6SJani Nikula 	tmp &= ~AUDIO_ELD_VALID(cpu_transcoder);
66049e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_PIN_ELD_CP_VLD, tmp);
661df0566a6SJani Nikula 
662df0566a6SJani Nikula 	/*
663df0566a6SJani Nikula 	 * FIXME: We're supposed to wait for vblank here, but we have vblanks
664df0566a6SJani Nikula 	 * disabled during the mode set. The proper fix would be to push the
665df0566a6SJani Nikula 	 * rest of the setup into a vblank work item, queued here, but the
666df0566a6SJani Nikula 	 * infrastructure is not there yet.
667df0566a6SJani Nikula 	 */
668df0566a6SJani Nikula 
669df0566a6SJani Nikula 	/* Reset ELD write address */
67049e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_DIP_ELD_CTRL(cpu_transcoder));
671df0566a6SJani Nikula 	tmp &= ~IBX_ELD_ADDRESS_MASK;
67249e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_DIP_ELD_CTRL(cpu_transcoder), tmp);
673df0566a6SJani Nikula 
674df0566a6SJani Nikula 	/* Up to 84 bytes of hw ELD buffer */
675df0566a6SJani Nikula 	len = min(drm_eld_size(eld), 84);
676df0566a6SJani Nikula 	for (i = 0; i < len / 4; i++)
67749e659bcSJani Nikula 		intel_de_write(dev_priv, HSW_AUD_EDID_DATA(cpu_transcoder),
67849e659bcSJani Nikula 			       *((const u32 *)eld + i));
679df0566a6SJani Nikula 
680df0566a6SJani Nikula 	/* ELD valid */
68149e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD);
682df0566a6SJani Nikula 	tmp |= AUDIO_ELD_VALID(cpu_transcoder);
68349e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_PIN_ELD_CP_VLD, tmp);
684df0566a6SJani Nikula 
685df0566a6SJani Nikula 	/* Enable timestamps */
686df0566a6SJani Nikula 	hsw_audio_config_update(encoder, crtc_state);
687df0566a6SJani Nikula 
688df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
689df0566a6SJani Nikula }
690df0566a6SJani Nikula 
691df0566a6SJani Nikula static void ilk_audio_codec_disable(struct intel_encoder *encoder,
692df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
693df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
694df0566a6SJani Nikula {
695df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
6962225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
697df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
698df0566a6SJani Nikula 	enum port port = encoder->port;
699df0566a6SJani Nikula 	u32 tmp, eldv;
700df0566a6SJani Nikula 	i915_reg_t aud_config, aud_cntrl_st2;
701df0566a6SJani Nikula 
70263855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
70363855149SWambui Karuga 		    "Disable audio codec on [ENCODER:%d:%s], pipe %c\n",
70466a990ddSVille Syrjälä 		     encoder->base.base.id, encoder->base.name,
70566a990ddSVille Syrjälä 		     pipe_name(pipe));
706df0566a6SJani Nikula 
7079a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, port == PORT_A))
708df0566a6SJani Nikula 		return;
709df0566a6SJani Nikula 
710df0566a6SJani Nikula 	if (HAS_PCH_IBX(dev_priv)) {
711df0566a6SJani Nikula 		aud_config = IBX_AUD_CFG(pipe);
712df0566a6SJani Nikula 		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
713df0566a6SJani Nikula 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
714df0566a6SJani Nikula 		aud_config = VLV_AUD_CFG(pipe);
715df0566a6SJani Nikula 		aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
716df0566a6SJani Nikula 	} else {
717df0566a6SJani Nikula 		aud_config = CPT_AUD_CFG(pipe);
718df0566a6SJani Nikula 		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
719df0566a6SJani Nikula 	}
720df0566a6SJani Nikula 
721df0566a6SJani Nikula 	/* Disable timestamps */
72249e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_config);
723df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
724df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_PROG_ENABLE;
725df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_UPPER_N_MASK;
726df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_LOWER_N_MASK;
727df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(old_crtc_state))
728df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
72949e659bcSJani Nikula 	intel_de_write(dev_priv, aud_config, tmp);
730df0566a6SJani Nikula 
731df0566a6SJani Nikula 	eldv = IBX_ELD_VALID(port);
732df0566a6SJani Nikula 
733df0566a6SJani Nikula 	/* Invalidate ELD */
73449e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_cntrl_st2);
735df0566a6SJani Nikula 	tmp &= ~eldv;
73649e659bcSJani Nikula 	intel_de_write(dev_priv, aud_cntrl_st2, tmp);
737df0566a6SJani Nikula }
738df0566a6SJani Nikula 
739df0566a6SJani Nikula static void ilk_audio_codec_enable(struct intel_encoder *encoder,
740df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
741df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
742df0566a6SJani Nikula {
743df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
7442225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
745df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
746df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
747df0566a6SJani Nikula 	enum port port = encoder->port;
748df0566a6SJani Nikula 	const u8 *eld = connector->eld;
749df0566a6SJani Nikula 	u32 tmp, eldv;
750df0566a6SJani Nikula 	int len, i;
751df0566a6SJani Nikula 	i915_reg_t hdmiw_hdmiedid, aud_config, aud_cntl_st, aud_cntrl_st2;
752df0566a6SJani Nikula 
75363855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
75463855149SWambui Karuga 		    "Enable audio codec on [ENCODER:%d:%s], pipe %c, %u bytes ELD\n",
75566a990ddSVille Syrjälä 		    encoder->base.base.id, encoder->base.name,
75666a990ddSVille Syrjälä 		    pipe_name(pipe), drm_eld_size(eld));
757df0566a6SJani Nikula 
7589a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, port == PORT_A))
759df0566a6SJani Nikula 		return;
760df0566a6SJani Nikula 
761df0566a6SJani Nikula 	/*
762df0566a6SJani Nikula 	 * FIXME: We're supposed to wait for vblank here, but we have vblanks
763df0566a6SJani Nikula 	 * disabled during the mode set. The proper fix would be to push the
764df0566a6SJani Nikula 	 * rest of the setup into a vblank work item, queued here, but the
765df0566a6SJani Nikula 	 * infrastructure is not there yet.
766df0566a6SJani Nikula 	 */
767df0566a6SJani Nikula 
768df0566a6SJani Nikula 	if (HAS_PCH_IBX(dev_priv)) {
769df0566a6SJani Nikula 		hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
770df0566a6SJani Nikula 		aud_config = IBX_AUD_CFG(pipe);
771df0566a6SJani Nikula 		aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
772df0566a6SJani Nikula 		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
773df0566a6SJani Nikula 	} else if (IS_VALLEYVIEW(dev_priv) ||
774df0566a6SJani Nikula 		   IS_CHERRYVIEW(dev_priv)) {
775df0566a6SJani Nikula 		hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
776df0566a6SJani Nikula 		aud_config = VLV_AUD_CFG(pipe);
777df0566a6SJani Nikula 		aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
778df0566a6SJani Nikula 		aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
779df0566a6SJani Nikula 	} else {
780df0566a6SJani Nikula 		hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
781df0566a6SJani Nikula 		aud_config = CPT_AUD_CFG(pipe);
782df0566a6SJani Nikula 		aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
783df0566a6SJani Nikula 		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
784df0566a6SJani Nikula 	}
785df0566a6SJani Nikula 
786df0566a6SJani Nikula 	eldv = IBX_ELD_VALID(port);
787df0566a6SJani Nikula 
788df0566a6SJani Nikula 	/* Invalidate ELD */
78949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_cntrl_st2);
790df0566a6SJani Nikula 	tmp &= ~eldv;
79149e659bcSJani Nikula 	intel_de_write(dev_priv, aud_cntrl_st2, tmp);
792df0566a6SJani Nikula 
793df0566a6SJani Nikula 	/* Reset ELD write address */
79449e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_cntl_st);
795df0566a6SJani Nikula 	tmp &= ~IBX_ELD_ADDRESS_MASK;
79649e659bcSJani Nikula 	intel_de_write(dev_priv, aud_cntl_st, tmp);
797df0566a6SJani Nikula 
798df0566a6SJani Nikula 	/* Up to 84 bytes of hw ELD buffer */
799df0566a6SJani Nikula 	len = min(drm_eld_size(eld), 84);
800df0566a6SJani Nikula 	for (i = 0; i < len / 4; i++)
80149e659bcSJani Nikula 		intel_de_write(dev_priv, hdmiw_hdmiedid,
80249e659bcSJani Nikula 			       *((const u32 *)eld + i));
803df0566a6SJani Nikula 
804df0566a6SJani Nikula 	/* ELD valid */
80549e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_cntrl_st2);
806df0566a6SJani Nikula 	tmp |= eldv;
80749e659bcSJani Nikula 	intel_de_write(dev_priv, aud_cntrl_st2, tmp);
808df0566a6SJani Nikula 
809df0566a6SJani Nikula 	/* Enable timestamps */
81049e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_config);
811df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
812df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
813df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
814df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state))
815df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
816df0566a6SJani Nikula 	else
817df0566a6SJani Nikula 		tmp |= audio_config_hdmi_pixel_clock(crtc_state);
81849e659bcSJani Nikula 	intel_de_write(dev_priv, aud_config, tmp);
819df0566a6SJani Nikula }
820df0566a6SJani Nikula 
821df0566a6SJani Nikula /**
822df0566a6SJani Nikula  * intel_audio_codec_enable - Enable the audio codec for HD audio
823df0566a6SJani Nikula  * @encoder: encoder on which to enable audio
824df0566a6SJani Nikula  * @crtc_state: pointer to the current crtc state.
825df0566a6SJani Nikula  * @conn_state: pointer to the current connector state.
826df0566a6SJani Nikula  *
827df0566a6SJani Nikula  * The enable sequences may only be performed after enabling the transcoder and
828df0566a6SJani Nikula  * port, and after completed link training.
829df0566a6SJani Nikula  */
830df0566a6SJani Nikula void intel_audio_codec_enable(struct intel_encoder *encoder,
831df0566a6SJani Nikula 			      const struct intel_crtc_state *crtc_state,
832df0566a6SJani Nikula 			      const struct drm_connector_state *conn_state)
833df0566a6SJani Nikula {
834df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
835df0566a6SJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio_component;
8362225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
837df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
838df0566a6SJani Nikula 	const struct drm_display_mode *adjusted_mode =
8391326a92cSMaarten Lankhorst 		&crtc_state->hw.adjusted_mode;
840df0566a6SJani Nikula 	enum port port = encoder->port;
841df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
842df0566a6SJani Nikula 
843df0566a6SJani Nikula 	/* FIXME precompute the ELD in .compute_config() */
844df0566a6SJani Nikula 	if (!connector->eld[0])
84563855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
84663855149SWambui Karuga 			    "Bogus ELD on [CONNECTOR:%d:%s]\n",
847df0566a6SJani Nikula 			    connector->base.id, connector->name);
848df0566a6SJani Nikula 
84963855149SWambui Karuga 	drm_dbg(&dev_priv->drm, "ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
850df0566a6SJani Nikula 		connector->base.id,
851df0566a6SJani Nikula 		connector->name,
85279003e61SVille Syrjälä 		encoder->base.base.id,
85379003e61SVille Syrjälä 		encoder->base.name);
854df0566a6SJani Nikula 
855df0566a6SJani Nikula 	connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
856df0566a6SJani Nikula 
857df0566a6SJani Nikula 	if (dev_priv->display.audio_codec_enable)
858df0566a6SJani Nikula 		dev_priv->display.audio_codec_enable(encoder,
859df0566a6SJani Nikula 						     crtc_state,
860df0566a6SJani Nikula 						     conn_state);
861df0566a6SJani Nikula 
862df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
863df0566a6SJani Nikula 	encoder->audio_connector = connector;
864df0566a6SJani Nikula 
865df0566a6SJani Nikula 	/* referred in audio callbacks */
866df0566a6SJani Nikula 	dev_priv->av_enc_map[pipe] = encoder;
867df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
868df0566a6SJani Nikula 
869df0566a6SJani Nikula 	if (acomp && acomp->base.audio_ops &&
870df0566a6SJani Nikula 	    acomp->base.audio_ops->pin_eld_notify) {
871df0566a6SJani Nikula 		/* audio drivers expect pipe = -1 to indicate Non-MST cases */
872df0566a6SJani Nikula 		if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
873df0566a6SJani Nikula 			pipe = -1;
874df0566a6SJani Nikula 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
875df0566a6SJani Nikula 						 (int) port, (int) pipe);
876df0566a6SJani Nikula 	}
877df0566a6SJani Nikula 
878df0566a6SJani Nikula 	intel_lpe_audio_notify(dev_priv, pipe, port, connector->eld,
879df0566a6SJani Nikula 			       crtc_state->port_clock,
880df0566a6SJani Nikula 			       intel_crtc_has_dp_encoder(crtc_state));
881df0566a6SJani Nikula }
882df0566a6SJani Nikula 
883df0566a6SJani Nikula /**
884df0566a6SJani Nikula  * intel_audio_codec_disable - Disable the audio codec for HD audio
885df0566a6SJani Nikula  * @encoder: encoder on which to disable audio
886df0566a6SJani Nikula  * @old_crtc_state: pointer to the old crtc state.
887df0566a6SJani Nikula  * @old_conn_state: pointer to the old connector state.
888df0566a6SJani Nikula  *
889df0566a6SJani Nikula  * The disable sequences must be performed before disabling the transcoder or
890df0566a6SJani Nikula  * port.
891df0566a6SJani Nikula  */
892df0566a6SJani Nikula void intel_audio_codec_disable(struct intel_encoder *encoder,
893df0566a6SJani Nikula 			       const struct intel_crtc_state *old_crtc_state,
894df0566a6SJani Nikula 			       const struct drm_connector_state *old_conn_state)
895df0566a6SJani Nikula {
896df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
897df0566a6SJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio_component;
8982225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
899df0566a6SJani Nikula 	enum port port = encoder->port;
900df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
901df0566a6SJani Nikula 
902df0566a6SJani Nikula 	if (dev_priv->display.audio_codec_disable)
903df0566a6SJani Nikula 		dev_priv->display.audio_codec_disable(encoder,
904df0566a6SJani Nikula 						      old_crtc_state,
905df0566a6SJani Nikula 						      old_conn_state);
906df0566a6SJani Nikula 
907df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
908df0566a6SJani Nikula 	encoder->audio_connector = NULL;
909df0566a6SJani Nikula 	dev_priv->av_enc_map[pipe] = NULL;
910df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
911df0566a6SJani Nikula 
912df0566a6SJani Nikula 	if (acomp && acomp->base.audio_ops &&
913df0566a6SJani Nikula 	    acomp->base.audio_ops->pin_eld_notify) {
914df0566a6SJani Nikula 		/* audio drivers expect pipe = -1 to indicate Non-MST cases */
915df0566a6SJani Nikula 		if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
916df0566a6SJani Nikula 			pipe = -1;
917df0566a6SJani Nikula 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
918df0566a6SJani Nikula 						 (int) port, (int) pipe);
919df0566a6SJani Nikula 	}
920df0566a6SJani Nikula 
921df0566a6SJani Nikula 	intel_lpe_audio_notify(dev_priv, pipe, port, NULL, 0, false);
922df0566a6SJani Nikula }
923df0566a6SJani Nikula 
924df0566a6SJani Nikula /**
925df0566a6SJani Nikula  * intel_init_audio_hooks - Set up chip specific audio hooks
926df0566a6SJani Nikula  * @dev_priv: device private
927df0566a6SJani Nikula  */
928df0566a6SJani Nikula void intel_init_audio_hooks(struct drm_i915_private *dev_priv)
929df0566a6SJani Nikula {
930df0566a6SJani Nikula 	if (IS_G4X(dev_priv)) {
931df0566a6SJani Nikula 		dev_priv->display.audio_codec_enable = g4x_audio_codec_enable;
932df0566a6SJani Nikula 		dev_priv->display.audio_codec_disable = g4x_audio_codec_disable;
933df0566a6SJani Nikula 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
934df0566a6SJani Nikula 		dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
935df0566a6SJani Nikula 		dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
936005e9537SMatt Roper 	} else if (IS_HASWELL(dev_priv) || DISPLAY_VER(dev_priv) >= 8) {
937df0566a6SJani Nikula 		dev_priv->display.audio_codec_enable = hsw_audio_codec_enable;
938df0566a6SJani Nikula 		dev_priv->display.audio_codec_disable = hsw_audio_codec_disable;
939df0566a6SJani Nikula 	} else if (HAS_PCH_SPLIT(dev_priv)) {
940df0566a6SJani Nikula 		dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
941df0566a6SJani Nikula 		dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
942df0566a6SJani Nikula 	}
943df0566a6SJani Nikula }
944df0566a6SJani Nikula 
94528a30b45SVille Syrjälä static int glk_force_audio_cdclk_commit(struct intel_atomic_state *state,
94608e3ed3aSChris Wilson 					struct intel_crtc *crtc,
94728a30b45SVille Syrjälä 					bool enable)
94828a30b45SVille Syrjälä {
94928a30b45SVille Syrjälä 	struct intel_cdclk_state *cdclk_state;
95028a30b45SVille Syrjälä 	int ret;
95128a30b45SVille Syrjälä 
95228a30b45SVille Syrjälä 	/* need to hold at least one crtc lock for the global state */
95328a30b45SVille Syrjälä 	ret = drm_modeset_lock(&crtc->base.mutex, state->base.acquire_ctx);
95428a30b45SVille Syrjälä 	if (ret)
95528a30b45SVille Syrjälä 		return ret;
95628a30b45SVille Syrjälä 
95728a30b45SVille Syrjälä 	cdclk_state = intel_atomic_get_cdclk_state(state);
95828a30b45SVille Syrjälä 	if (IS_ERR(cdclk_state))
95928a30b45SVille Syrjälä 		return PTR_ERR(cdclk_state);
96028a30b45SVille Syrjälä 
96128a30b45SVille Syrjälä 	cdclk_state->force_min_cdclk = enable ? 2 * 96000 : 0;
96228a30b45SVille Syrjälä 
96328a30b45SVille Syrjälä 	return drm_atomic_commit(&state->base);
96428a30b45SVille Syrjälä }
96528a30b45SVille Syrjälä 
966df0566a6SJani Nikula static void glk_force_audio_cdclk(struct drm_i915_private *dev_priv,
967df0566a6SJani Nikula 				  bool enable)
968df0566a6SJani Nikula {
969df0566a6SJani Nikula 	struct drm_modeset_acquire_ctx ctx;
970df0566a6SJani Nikula 	struct drm_atomic_state *state;
97108e3ed3aSChris Wilson 	struct intel_crtc *crtc;
972df0566a6SJani Nikula 	int ret;
973df0566a6SJani Nikula 
974eae3da27SAnshuman Gupta 	crtc = intel_get_first_crtc(dev_priv);
97508e3ed3aSChris Wilson 	if (!crtc)
97608e3ed3aSChris Wilson 		return;
97708e3ed3aSChris Wilson 
978df0566a6SJani Nikula 	drm_modeset_acquire_init(&ctx, 0);
979df0566a6SJani Nikula 	state = drm_atomic_state_alloc(&dev_priv->drm);
9809a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !state))
981df0566a6SJani Nikula 		return;
982df0566a6SJani Nikula 
983df0566a6SJani Nikula 	state->acquire_ctx = &ctx;
984df0566a6SJani Nikula 
985df0566a6SJani Nikula retry:
98608e3ed3aSChris Wilson 	ret = glk_force_audio_cdclk_commit(to_intel_atomic_state(state), crtc,
98708e3ed3aSChris Wilson 					   enable);
988df0566a6SJani Nikula 	if (ret == -EDEADLK) {
989df0566a6SJani Nikula 		drm_atomic_state_clear(state);
990df0566a6SJani Nikula 		drm_modeset_backoff(&ctx);
991df0566a6SJani Nikula 		goto retry;
992df0566a6SJani Nikula 	}
993df0566a6SJani Nikula 
9949a3b466bSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, ret);
995df0566a6SJani Nikula 
996df0566a6SJani Nikula 	drm_atomic_state_put(state);
997df0566a6SJani Nikula 
998df0566a6SJani Nikula 	drm_modeset_drop_locks(&ctx);
999df0566a6SJani Nikula 	drm_modeset_acquire_fini(&ctx);
1000df0566a6SJani Nikula }
1001df0566a6SJani Nikula 
1002df0566a6SJani Nikula static unsigned long i915_audio_component_get_power(struct device *kdev)
1003df0566a6SJani Nikula {
1004df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1005df0566a6SJani Nikula 	intel_wakeref_t ret;
1006df0566a6SJani Nikula 
1007df0566a6SJani Nikula 	/* Catch potential impedance mismatches before they occur! */
1008df0566a6SJani Nikula 	BUILD_BUG_ON(sizeof(intel_wakeref_t) > sizeof(unsigned long));
1009df0566a6SJani Nikula 
1010df0566a6SJani Nikula 	ret = intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
1011df0566a6SJani Nikula 
101287c16945SKai Vehmanen 	if (dev_priv->audio_power_refcount++ == 0) {
1013005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 9) {
101449e659bcSJani Nikula 			intel_de_write(dev_priv, AUD_FREQ_CNTRL,
101549e659bcSJani Nikula 				       dev_priv->audio_freq_cntrl);
101663855149SWambui Karuga 			drm_dbg_kms(&dev_priv->drm,
101763855149SWambui Karuga 				    "restored AUD_FREQ_CNTRL to 0x%x\n",
101887c16945SKai Vehmanen 				    dev_priv->audio_freq_cntrl);
101987c16945SKai Vehmanen 		}
102087c16945SKai Vehmanen 
102187c16945SKai Vehmanen 		/* Force CDCLK to 2*BCLK as long as we need audio powered. */
10221ee48a61SKai Vehmanen 		if (IS_GEMINILAKE(dev_priv))
1023df0566a6SJani Nikula 			glk_force_audio_cdclk(dev_priv, true);
10241580d3cdSKai Vehmanen 
10252b5a4562SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 10)
102649e659bcSJani Nikula 			intel_de_write(dev_priv, AUD_PIN_BUF_CTL,
102749e659bcSJani Nikula 				       (intel_de_read(dev_priv, AUD_PIN_BUF_CTL) | AUD_PIN_BUF_ENABLE));
102887c16945SKai Vehmanen 	}
1029df0566a6SJani Nikula 
1030df0566a6SJani Nikula 	return ret;
1031df0566a6SJani Nikula }
1032df0566a6SJani Nikula 
1033df0566a6SJani Nikula static void i915_audio_component_put_power(struct device *kdev,
1034df0566a6SJani Nikula 					   unsigned long cookie)
1035df0566a6SJani Nikula {
1036df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1037df0566a6SJani Nikula 
1038df0566a6SJani Nikula 	/* Stop forcing CDCLK to 2*BCLK if no need for audio to be powered. */
1039df0566a6SJani Nikula 	if (--dev_priv->audio_power_refcount == 0)
10401ee48a61SKai Vehmanen 		if (IS_GEMINILAKE(dev_priv))
1041df0566a6SJani Nikula 			glk_force_audio_cdclk(dev_priv, false);
1042df0566a6SJani Nikula 
1043df0566a6SJani Nikula 	intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO, cookie);
1044df0566a6SJani Nikula }
1045df0566a6SJani Nikula 
1046df0566a6SJani Nikula static void i915_audio_component_codec_wake_override(struct device *kdev,
1047df0566a6SJani Nikula 						     bool enable)
1048df0566a6SJani Nikula {
1049df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1050df0566a6SJani Nikula 	unsigned long cookie;
1051df0566a6SJani Nikula 	u32 tmp;
1052df0566a6SJani Nikula 
1053005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) < 9)
1054df0566a6SJani Nikula 		return;
1055df0566a6SJani Nikula 
1056df0566a6SJani Nikula 	cookie = i915_audio_component_get_power(kdev);
1057df0566a6SJani Nikula 
1058df0566a6SJani Nikula 	/*
1059df0566a6SJani Nikula 	 * Enable/disable generating the codec wake signal, overriding the
1060df0566a6SJani Nikula 	 * internal logic to generate the codec wake to controller.
1061df0566a6SJani Nikula 	 */
106249e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_CHICKENBIT);
1063df0566a6SJani Nikula 	tmp &= ~SKL_AUD_CODEC_WAKE_SIGNAL;
106449e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_CHICKENBIT, tmp);
1065df0566a6SJani Nikula 	usleep_range(1000, 1500);
1066df0566a6SJani Nikula 
1067df0566a6SJani Nikula 	if (enable) {
106849e659bcSJani Nikula 		tmp = intel_de_read(dev_priv, HSW_AUD_CHICKENBIT);
1069df0566a6SJani Nikula 		tmp |= SKL_AUD_CODEC_WAKE_SIGNAL;
107049e659bcSJani Nikula 		intel_de_write(dev_priv, HSW_AUD_CHICKENBIT, tmp);
1071df0566a6SJani Nikula 		usleep_range(1000, 1500);
1072df0566a6SJani Nikula 	}
1073df0566a6SJani Nikula 
1074df0566a6SJani Nikula 	i915_audio_component_put_power(kdev, cookie);
1075df0566a6SJani Nikula }
1076df0566a6SJani Nikula 
1077df0566a6SJani Nikula /* Get CDCLK in kHz  */
1078df0566a6SJani Nikula static int i915_audio_component_get_cdclk_freq(struct device *kdev)
1079df0566a6SJani Nikula {
1080df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1081df0566a6SJani Nikula 
10829a3b466bSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !HAS_DDI(dev_priv)))
1083df0566a6SJani Nikula 		return -ENODEV;
1084df0566a6SJani Nikula 
1085df0566a6SJani Nikula 	return dev_priv->cdclk.hw.cdclk;
1086df0566a6SJani Nikula }
1087df0566a6SJani Nikula 
1088df0566a6SJani Nikula /*
1089df0566a6SJani Nikula  * get the intel_encoder according to the parameter port and pipe
1090df0566a6SJani Nikula  * intel_encoder is saved by the index of pipe
1091df0566a6SJani Nikula  * MST & (pipe >= 0): return the av_enc_map[pipe],
1092df0566a6SJani Nikula  *   when port is matched
1093df0566a6SJani Nikula  * MST & (pipe < 0): this is invalid
1094df0566a6SJani Nikula  * Non-MST & (pipe >= 0): only pipe = 0 (the first device entry)
1095df0566a6SJani Nikula  *   will get the right intel_encoder with port matched
1096df0566a6SJani Nikula  * Non-MST & (pipe < 0): get the right intel_encoder with port matched
1097df0566a6SJani Nikula  */
1098df0566a6SJani Nikula static struct intel_encoder *get_saved_enc(struct drm_i915_private *dev_priv,
1099df0566a6SJani Nikula 					       int port, int pipe)
1100df0566a6SJani Nikula {
1101df0566a6SJani Nikula 	struct intel_encoder *encoder;
1102df0566a6SJani Nikula 
1103df0566a6SJani Nikula 	/* MST */
1104df0566a6SJani Nikula 	if (pipe >= 0) {
11059a3b466bSPankaj Bharadiya 		if (drm_WARN_ON(&dev_priv->drm,
11069a3b466bSPankaj Bharadiya 				pipe >= ARRAY_SIZE(dev_priv->av_enc_map)))
1107df0566a6SJani Nikula 			return NULL;
1108df0566a6SJani Nikula 
1109df0566a6SJani Nikula 		encoder = dev_priv->av_enc_map[pipe];
1110df0566a6SJani Nikula 		/*
1111df0566a6SJani Nikula 		 * when bootup, audio driver may not know it is
1112df0566a6SJani Nikula 		 * MST or not. So it will poll all the port & pipe
1113df0566a6SJani Nikula 		 * combinations
1114df0566a6SJani Nikula 		 */
1115df0566a6SJani Nikula 		if (encoder != NULL && encoder->port == port &&
1116df0566a6SJani Nikula 		    encoder->type == INTEL_OUTPUT_DP_MST)
1117df0566a6SJani Nikula 			return encoder;
1118df0566a6SJani Nikula 	}
1119df0566a6SJani Nikula 
1120df0566a6SJani Nikula 	/* Non-MST */
1121df0566a6SJani Nikula 	if (pipe > 0)
1122df0566a6SJani Nikula 		return NULL;
1123df0566a6SJani Nikula 
1124df0566a6SJani Nikula 	for_each_pipe(dev_priv, pipe) {
1125df0566a6SJani Nikula 		encoder = dev_priv->av_enc_map[pipe];
1126df0566a6SJani Nikula 		if (encoder == NULL)
1127df0566a6SJani Nikula 			continue;
1128df0566a6SJani Nikula 
1129df0566a6SJani Nikula 		if (encoder->type == INTEL_OUTPUT_DP_MST)
1130df0566a6SJani Nikula 			continue;
1131df0566a6SJani Nikula 
1132df0566a6SJani Nikula 		if (port == encoder->port)
1133df0566a6SJani Nikula 			return encoder;
1134df0566a6SJani Nikula 	}
1135df0566a6SJani Nikula 
1136df0566a6SJani Nikula 	return NULL;
1137df0566a6SJani Nikula }
1138df0566a6SJani Nikula 
1139df0566a6SJani Nikula static int i915_audio_component_sync_audio_rate(struct device *kdev, int port,
1140df0566a6SJani Nikula 						int pipe, int rate)
1141df0566a6SJani Nikula {
1142df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1143df0566a6SJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio_component;
1144df0566a6SJani Nikula 	struct intel_encoder *encoder;
1145df0566a6SJani Nikula 	struct intel_crtc *crtc;
1146df0566a6SJani Nikula 	unsigned long cookie;
1147df0566a6SJani Nikula 	int err = 0;
1148df0566a6SJani Nikula 
1149df0566a6SJani Nikula 	if (!HAS_DDI(dev_priv))
1150df0566a6SJani Nikula 		return 0;
1151df0566a6SJani Nikula 
1152df0566a6SJani Nikula 	cookie = i915_audio_component_get_power(kdev);
1153df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
1154df0566a6SJani Nikula 
1155df0566a6SJani Nikula 	/* 1. get the pipe */
1156df0566a6SJani Nikula 	encoder = get_saved_enc(dev_priv, port, pipe);
1157df0566a6SJani Nikula 	if (!encoder || !encoder->base.crtc) {
115863855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "Not valid for port %c\n",
115963855149SWambui Karuga 			    port_name(port));
1160df0566a6SJani Nikula 		err = -ENODEV;
1161df0566a6SJani Nikula 		goto unlock;
1162df0566a6SJani Nikula 	}
1163df0566a6SJani Nikula 
1164df0566a6SJani Nikula 	crtc = to_intel_crtc(encoder->base.crtc);
1165df0566a6SJani Nikula 
1166df0566a6SJani Nikula 	/* port must be valid now, otherwise the pipe will be invalid */
1167df0566a6SJani Nikula 	acomp->aud_sample_rate[port] = rate;
1168df0566a6SJani Nikula 
1169df0566a6SJani Nikula 	hsw_audio_config_update(encoder, crtc->config);
1170df0566a6SJani Nikula 
1171df0566a6SJani Nikula  unlock:
1172df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
1173df0566a6SJani Nikula 	i915_audio_component_put_power(kdev, cookie);
1174df0566a6SJani Nikula 	return err;
1175df0566a6SJani Nikula }
1176df0566a6SJani Nikula 
1177df0566a6SJani Nikula static int i915_audio_component_get_eld(struct device *kdev, int port,
1178df0566a6SJani Nikula 					int pipe, bool *enabled,
1179df0566a6SJani Nikula 					unsigned char *buf, int max_bytes)
1180df0566a6SJani Nikula {
1181df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1182df0566a6SJani Nikula 	struct intel_encoder *intel_encoder;
1183df0566a6SJani Nikula 	const u8 *eld;
1184df0566a6SJani Nikula 	int ret = -EINVAL;
1185df0566a6SJani Nikula 
1186df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
1187df0566a6SJani Nikula 
1188df0566a6SJani Nikula 	intel_encoder = get_saved_enc(dev_priv, port, pipe);
1189df0566a6SJani Nikula 	if (!intel_encoder) {
119063855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "Not valid for port %c\n",
119163855149SWambui Karuga 			    port_name(port));
1192df0566a6SJani Nikula 		mutex_unlock(&dev_priv->av_mutex);
1193df0566a6SJani Nikula 		return ret;
1194df0566a6SJani Nikula 	}
1195df0566a6SJani Nikula 
1196df0566a6SJani Nikula 	ret = 0;
1197df0566a6SJani Nikula 	*enabled = intel_encoder->audio_connector != NULL;
1198df0566a6SJani Nikula 	if (*enabled) {
1199df0566a6SJani Nikula 		eld = intel_encoder->audio_connector->eld;
1200df0566a6SJani Nikula 		ret = drm_eld_size(eld);
1201df0566a6SJani Nikula 		memcpy(buf, eld, min(max_bytes, ret));
1202df0566a6SJani Nikula 	}
1203df0566a6SJani Nikula 
1204df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
1205df0566a6SJani Nikula 	return ret;
1206df0566a6SJani Nikula }
1207df0566a6SJani Nikula 
1208df0566a6SJani Nikula static const struct drm_audio_component_ops i915_audio_component_ops = {
1209df0566a6SJani Nikula 	.owner		= THIS_MODULE,
1210df0566a6SJani Nikula 	.get_power	= i915_audio_component_get_power,
1211df0566a6SJani Nikula 	.put_power	= i915_audio_component_put_power,
1212df0566a6SJani Nikula 	.codec_wake_override = i915_audio_component_codec_wake_override,
1213df0566a6SJani Nikula 	.get_cdclk_freq	= i915_audio_component_get_cdclk_freq,
1214df0566a6SJani Nikula 	.sync_audio_rate = i915_audio_component_sync_audio_rate,
1215df0566a6SJani Nikula 	.get_eld	= i915_audio_component_get_eld,
1216df0566a6SJani Nikula };
1217df0566a6SJani Nikula 
1218df0566a6SJani Nikula static int i915_audio_component_bind(struct device *i915_kdev,
1219df0566a6SJani Nikula 				     struct device *hda_kdev, void *data)
1220df0566a6SJani Nikula {
1221df0566a6SJani Nikula 	struct i915_audio_component *acomp = data;
1222df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
1223df0566a6SJani Nikula 	int i;
1224df0566a6SJani Nikula 
12259a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, acomp->base.ops || acomp->base.dev))
1226df0566a6SJani Nikula 		return -EEXIST;
1227df0566a6SJani Nikula 
12289a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
12299a3b466bSPankaj Bharadiya 			!device_link_add(hda_kdev, i915_kdev,
12309a3b466bSPankaj Bharadiya 					 DL_FLAG_STATELESS)))
1231df0566a6SJani Nikula 		return -ENOMEM;
1232df0566a6SJani Nikula 
1233df0566a6SJani Nikula 	drm_modeset_lock_all(&dev_priv->drm);
1234df0566a6SJani Nikula 	acomp->base.ops = &i915_audio_component_ops;
1235df0566a6SJani Nikula 	acomp->base.dev = i915_kdev;
1236df0566a6SJani Nikula 	BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
1237df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
1238df0566a6SJani Nikula 		acomp->aud_sample_rate[i] = 0;
1239df0566a6SJani Nikula 	dev_priv->audio_component = acomp;
1240df0566a6SJani Nikula 	drm_modeset_unlock_all(&dev_priv->drm);
1241df0566a6SJani Nikula 
1242df0566a6SJani Nikula 	return 0;
1243df0566a6SJani Nikula }
1244df0566a6SJani Nikula 
1245df0566a6SJani Nikula static void i915_audio_component_unbind(struct device *i915_kdev,
1246df0566a6SJani Nikula 					struct device *hda_kdev, void *data)
1247df0566a6SJani Nikula {
1248df0566a6SJani Nikula 	struct i915_audio_component *acomp = data;
1249df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
1250df0566a6SJani Nikula 
1251df0566a6SJani Nikula 	drm_modeset_lock_all(&dev_priv->drm);
1252df0566a6SJani Nikula 	acomp->base.ops = NULL;
1253df0566a6SJani Nikula 	acomp->base.dev = NULL;
1254df0566a6SJani Nikula 	dev_priv->audio_component = NULL;
1255df0566a6SJani Nikula 	drm_modeset_unlock_all(&dev_priv->drm);
1256df0566a6SJani Nikula 
1257df0566a6SJani Nikula 	device_link_remove(hda_kdev, i915_kdev);
1258b4ed131dSJani Nikula 
1259b4ed131dSJani Nikula 	if (dev_priv->audio_power_refcount)
1260b4ed131dSJani Nikula 		drm_err(&dev_priv->drm, "audio power refcount %d after unbind\n",
1261b4ed131dSJani Nikula 			dev_priv->audio_power_refcount);
1262df0566a6SJani Nikula }
1263df0566a6SJani Nikula 
1264df0566a6SJani Nikula static const struct component_ops i915_audio_component_bind_ops = {
1265df0566a6SJani Nikula 	.bind	= i915_audio_component_bind,
1266df0566a6SJani Nikula 	.unbind	= i915_audio_component_unbind,
1267df0566a6SJani Nikula };
1268df0566a6SJani Nikula 
1269989634fbSKai Vehmanen #define AUD_FREQ_TMODE_SHIFT	14
1270989634fbSKai Vehmanen #define AUD_FREQ_4T		0
1271989634fbSKai Vehmanen #define AUD_FREQ_8T		(2 << AUD_FREQ_TMODE_SHIFT)
1272989634fbSKai Vehmanen #define AUD_FREQ_PULLCLKS(x)	(((x) & 0x3) << 11)
1273989634fbSKai Vehmanen #define AUD_FREQ_BCLK_96M	BIT(4)
1274989634fbSKai Vehmanen 
1275989634fbSKai Vehmanen #define AUD_FREQ_GEN12          (AUD_FREQ_8T | AUD_FREQ_PULLCLKS(0) | AUD_FREQ_BCLK_96M)
1276989634fbSKai Vehmanen #define AUD_FREQ_TGL_BROKEN     (AUD_FREQ_8T | AUD_FREQ_PULLCLKS(2) | AUD_FREQ_BCLK_96M)
1277989634fbSKai Vehmanen 
1278df0566a6SJani Nikula /**
1279df0566a6SJani Nikula  * i915_audio_component_init - initialize and register the audio component
1280df0566a6SJani Nikula  * @dev_priv: i915 device instance
1281df0566a6SJani Nikula  *
1282df0566a6SJani Nikula  * This will register with the component framework a child component which
1283df0566a6SJani Nikula  * will bind dynamically to the snd_hda_intel driver's corresponding master
1284df0566a6SJani Nikula  * component when the latter is registered. During binding the child
1285df0566a6SJani Nikula  * initializes an instance of struct i915_audio_component which it receives
1286df0566a6SJani Nikula  * from the master. The master can then start to use the interface defined by
1287df0566a6SJani Nikula  * this struct. Each side can break the binding at any point by deregistering
1288df0566a6SJani Nikula  * its own component after which each side's component unbind callback is
1289df0566a6SJani Nikula  * called.
1290df0566a6SJani Nikula  *
1291df0566a6SJani Nikula  * We ignore any error during registration and continue with reduced
1292df0566a6SJani Nikula  * functionality (i.e. without HDMI audio).
1293df0566a6SJani Nikula  */
1294df0566a6SJani Nikula static void i915_audio_component_init(struct drm_i915_private *dev_priv)
1295df0566a6SJani Nikula {
1296989634fbSKai Vehmanen 	u32 aud_freq, aud_freq_init;
1297df0566a6SJani Nikula 	int ret;
1298df0566a6SJani Nikula 
1299df0566a6SJani Nikula 	ret = component_add_typed(dev_priv->drm.dev,
1300df0566a6SJani Nikula 				  &i915_audio_component_bind_ops,
1301df0566a6SJani Nikula 				  I915_COMPONENT_AUDIO);
1302df0566a6SJani Nikula 	if (ret < 0) {
130363855149SWambui Karuga 		drm_err(&dev_priv->drm,
130463855149SWambui Karuga 			"failed to add audio component (%d)\n", ret);
1305df0566a6SJani Nikula 		/* continue with reduced functionality */
1306df0566a6SJani Nikula 		return;
1307df0566a6SJani Nikula 	}
1308df0566a6SJani Nikula 
1309005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 9) {
1310989634fbSKai Vehmanen 		aud_freq_init = intel_de_read(dev_priv, AUD_FREQ_CNTRL);
1311989634fbSKai Vehmanen 
1312*27ec10b3SLucas De Marchi 		if (DISPLAY_VER(dev_priv) >= 12)
1313989634fbSKai Vehmanen 			aud_freq = AUD_FREQ_GEN12;
1314989634fbSKai Vehmanen 		else
1315989634fbSKai Vehmanen 			aud_freq = aud_freq_init;
1316989634fbSKai Vehmanen 
1317989634fbSKai Vehmanen 		/* use BIOS provided value for TGL unless it is a known bad value */
1318989634fbSKai Vehmanen 		if (IS_TIGERLAKE(dev_priv) && aud_freq_init != AUD_FREQ_TGL_BROKEN)
1319989634fbSKai Vehmanen 			aud_freq = aud_freq_init;
1320989634fbSKai Vehmanen 
1321989634fbSKai Vehmanen 		drm_dbg_kms(&dev_priv->drm, "use AUD_FREQ_CNTRL of 0x%x (init value 0x%x)\n",
1322989634fbSKai Vehmanen 			    aud_freq, aud_freq_init);
1323989634fbSKai Vehmanen 
1324989634fbSKai Vehmanen 		dev_priv->audio_freq_cntrl = aud_freq;
132587c16945SKai Vehmanen 	}
132687c16945SKai Vehmanen 
1327df0566a6SJani Nikula 	dev_priv->audio_component_registered = true;
1328df0566a6SJani Nikula }
1329df0566a6SJani Nikula 
1330df0566a6SJani Nikula /**
1331df0566a6SJani Nikula  * i915_audio_component_cleanup - deregister the audio component
1332df0566a6SJani Nikula  * @dev_priv: i915 device instance
1333df0566a6SJani Nikula  *
1334df0566a6SJani Nikula  * Deregisters the audio component, breaking any existing binding to the
1335df0566a6SJani Nikula  * corresponding snd_hda_intel driver's master component.
1336df0566a6SJani Nikula  */
1337df0566a6SJani Nikula static void i915_audio_component_cleanup(struct drm_i915_private *dev_priv)
1338df0566a6SJani Nikula {
1339df0566a6SJani Nikula 	if (!dev_priv->audio_component_registered)
1340df0566a6SJani Nikula 		return;
1341df0566a6SJani Nikula 
1342df0566a6SJani Nikula 	component_del(dev_priv->drm.dev, &i915_audio_component_bind_ops);
1343df0566a6SJani Nikula 	dev_priv->audio_component_registered = false;
1344df0566a6SJani Nikula }
1345df0566a6SJani Nikula 
1346df0566a6SJani Nikula /**
1347df0566a6SJani Nikula  * intel_audio_init() - Initialize the audio driver either using
1348df0566a6SJani Nikula  * component framework or using lpe audio bridge
1349df0566a6SJani Nikula  * @dev_priv: the i915 drm device private data
1350df0566a6SJani Nikula  *
1351df0566a6SJani Nikula  */
1352df0566a6SJani Nikula void intel_audio_init(struct drm_i915_private *dev_priv)
1353df0566a6SJani Nikula {
1354df0566a6SJani Nikula 	if (intel_lpe_audio_init(dev_priv) < 0)
1355df0566a6SJani Nikula 		i915_audio_component_init(dev_priv);
1356df0566a6SJani Nikula }
1357df0566a6SJani Nikula 
1358df0566a6SJani Nikula /**
1359df0566a6SJani Nikula  * intel_audio_deinit() - deinitialize the audio driver
1360df0566a6SJani Nikula  * @dev_priv: the i915 drm device private data
1361df0566a6SJani Nikula  *
1362df0566a6SJani Nikula  */
1363df0566a6SJani Nikula void intel_audio_deinit(struct drm_i915_private *dev_priv)
1364df0566a6SJani Nikula {
1365df0566a6SJani Nikula 	if ((dev_priv)->lpe_audio.platdev != NULL)
1366df0566a6SJani Nikula 		intel_lpe_audio_teardown(dev_priv);
1367df0566a6SJani Nikula 	else
1368df0566a6SJani Nikula 		i915_audio_component_cleanup(dev_priv);
1369df0566a6SJani Nikula }
1370