xref: /linux/drivers/gpu/drm/i915/display/intel_audio.c (revision 6385514932d5fc9379702d39b69e32c3f67cc4c2)
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"
331d455f8dSJani Nikula #include "intel_display_types.h"
34df0566a6SJani Nikula #include "intel_lpe_audio.h"
35df0566a6SJani Nikula 
36df0566a6SJani Nikula /**
37df0566a6SJani Nikula  * DOC: High Definition Audio over HDMI and Display Port
38df0566a6SJani Nikula  *
39df0566a6SJani Nikula  * The graphics and audio drivers together support High Definition Audio over
40df0566a6SJani Nikula  * HDMI and Display Port. The audio programming sequences are divided into audio
41df0566a6SJani Nikula  * codec and controller enable and disable sequences. The graphics driver
42df0566a6SJani Nikula  * handles the audio codec sequences, while the audio driver handles the audio
43df0566a6SJani Nikula  * controller sequences.
44df0566a6SJani Nikula  *
45df0566a6SJani Nikula  * The disable sequences must be performed before disabling the transcoder or
46df0566a6SJani Nikula  * port. The enable sequences may only be performed after enabling the
47df0566a6SJani Nikula  * transcoder and port, and after completed link training. Therefore the audio
48df0566a6SJani Nikula  * enable/disable sequences are part of the modeset sequence.
49df0566a6SJani Nikula  *
50df0566a6SJani Nikula  * The codec and controller sequences could be done either parallel or serial,
51df0566a6SJani Nikula  * but generally the ELDV/PD change in the codec sequence indicates to the audio
52df0566a6SJani Nikula  * driver that the controller sequence should start. Indeed, most of the
53df0566a6SJani Nikula  * co-operation between the graphics and audio drivers is handled via audio
54df0566a6SJani Nikula  * related registers. (The notable exception is the power management, not
55df0566a6SJani Nikula  * covered here.)
56df0566a6SJani Nikula  *
57df0566a6SJani Nikula  * The struct &i915_audio_component is used to interact between the graphics
58df0566a6SJani Nikula  * and audio drivers. The struct &i915_audio_component_ops @ops in it is
59df0566a6SJani Nikula  * defined in graphics driver and called in audio driver. The
60df0566a6SJani Nikula  * struct &i915_audio_component_audio_ops @audio_ops is called from i915 driver.
61df0566a6SJani Nikula  */
62df0566a6SJani Nikula 
63df0566a6SJani Nikula /* DP N/M table */
64df0566a6SJani Nikula #define LC_810M	810000
65df0566a6SJani Nikula #define LC_540M	540000
66df0566a6SJani Nikula #define LC_270M	270000
67df0566a6SJani Nikula #define LC_162M	162000
68df0566a6SJani Nikula 
69df0566a6SJani Nikula struct dp_aud_n_m {
70df0566a6SJani Nikula 	int sample_rate;
71df0566a6SJani Nikula 	int clock;
72df0566a6SJani Nikula 	u16 m;
73df0566a6SJani Nikula 	u16 n;
74df0566a6SJani Nikula };
75df0566a6SJani Nikula 
762c291417SAditya Swarup struct hdmi_aud_ncts {
772c291417SAditya Swarup 	int sample_rate;
782c291417SAditya Swarup 	int clock;
792c291417SAditya Swarup 	int n;
802c291417SAditya Swarup 	int cts;
812c291417SAditya Swarup };
822c291417SAditya Swarup 
83df0566a6SJani Nikula /* Values according to DP 1.4 Table 2-104 */
84df0566a6SJani Nikula static const struct dp_aud_n_m dp_aud_n_m[] = {
85df0566a6SJani Nikula 	{ 32000, LC_162M, 1024, 10125 },
86df0566a6SJani Nikula 	{ 44100, LC_162M, 784, 5625 },
87df0566a6SJani Nikula 	{ 48000, LC_162M, 512, 3375 },
88df0566a6SJani Nikula 	{ 64000, LC_162M, 2048, 10125 },
89df0566a6SJani Nikula 	{ 88200, LC_162M, 1568, 5625 },
90df0566a6SJani Nikula 	{ 96000, LC_162M, 1024, 3375 },
91df0566a6SJani Nikula 	{ 128000, LC_162M, 4096, 10125 },
92df0566a6SJani Nikula 	{ 176400, LC_162M, 3136, 5625 },
93df0566a6SJani Nikula 	{ 192000, LC_162M, 2048, 3375 },
94df0566a6SJani Nikula 	{ 32000, LC_270M, 1024, 16875 },
95df0566a6SJani Nikula 	{ 44100, LC_270M, 784, 9375 },
96df0566a6SJani Nikula 	{ 48000, LC_270M, 512, 5625 },
97df0566a6SJani Nikula 	{ 64000, LC_270M, 2048, 16875 },
98df0566a6SJani Nikula 	{ 88200, LC_270M, 1568, 9375 },
99df0566a6SJani Nikula 	{ 96000, LC_270M, 1024, 5625 },
100df0566a6SJani Nikula 	{ 128000, LC_270M, 4096, 16875 },
101df0566a6SJani Nikula 	{ 176400, LC_270M, 3136, 9375 },
102df0566a6SJani Nikula 	{ 192000, LC_270M, 2048, 5625 },
103df0566a6SJani Nikula 	{ 32000, LC_540M, 1024, 33750 },
104df0566a6SJani Nikula 	{ 44100, LC_540M, 784, 18750 },
105df0566a6SJani Nikula 	{ 48000, LC_540M, 512, 11250 },
106df0566a6SJani Nikula 	{ 64000, LC_540M, 2048, 33750 },
107df0566a6SJani Nikula 	{ 88200, LC_540M, 1568, 18750 },
108df0566a6SJani Nikula 	{ 96000, LC_540M, 1024, 11250 },
109df0566a6SJani Nikula 	{ 128000, LC_540M, 4096, 33750 },
110df0566a6SJani Nikula 	{ 176400, LC_540M, 3136, 18750 },
111df0566a6SJani Nikula 	{ 192000, LC_540M, 2048, 11250 },
112df0566a6SJani Nikula 	{ 32000, LC_810M, 1024, 50625 },
113df0566a6SJani Nikula 	{ 44100, LC_810M, 784, 28125 },
114df0566a6SJani Nikula 	{ 48000, LC_810M, 512, 16875 },
115df0566a6SJani Nikula 	{ 64000, LC_810M, 2048, 50625 },
116df0566a6SJani Nikula 	{ 88200, LC_810M, 1568, 28125 },
117df0566a6SJani Nikula 	{ 96000, LC_810M, 1024, 16875 },
118df0566a6SJani Nikula 	{ 128000, LC_810M, 4096, 50625 },
119df0566a6SJani Nikula 	{ 176400, LC_810M, 3136, 28125 },
120df0566a6SJani Nikula 	{ 192000, LC_810M, 2048, 16875 },
121df0566a6SJani Nikula };
122df0566a6SJani Nikula 
123df0566a6SJani Nikula static const struct dp_aud_n_m *
124df0566a6SJani Nikula audio_config_dp_get_n_m(const struct intel_crtc_state *crtc_state, int rate)
125df0566a6SJani Nikula {
126df0566a6SJani Nikula 	int i;
127df0566a6SJani Nikula 
128df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(dp_aud_n_m); i++) {
129df0566a6SJani Nikula 		if (rate == dp_aud_n_m[i].sample_rate &&
130df0566a6SJani Nikula 		    crtc_state->port_clock == dp_aud_n_m[i].clock)
131df0566a6SJani Nikula 			return &dp_aud_n_m[i];
132df0566a6SJani Nikula 	}
133df0566a6SJani Nikula 
134df0566a6SJani Nikula 	return NULL;
135df0566a6SJani Nikula }
136df0566a6SJani Nikula 
137df0566a6SJani Nikula static const struct {
138df0566a6SJani Nikula 	int clock;
139df0566a6SJani Nikula 	u32 config;
140df0566a6SJani Nikula } hdmi_audio_clock[] = {
141df0566a6SJani Nikula 	{ 25175, AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
142df0566a6SJani Nikula 	{ 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
143df0566a6SJani Nikula 	{ 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
144df0566a6SJani Nikula 	{ 27027, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
145df0566a6SJani Nikula 	{ 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
146df0566a6SJani Nikula 	{ 54054, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
147df0566a6SJani Nikula 	{ 74176, AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
148df0566a6SJani Nikula 	{ 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
149df0566a6SJani Nikula 	{ 148352, AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
150df0566a6SJani Nikula 	{ 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
151df0566a6SJani Nikula };
152df0566a6SJani Nikula 
153df0566a6SJani Nikula /* HDMI N/CTS table */
154df0566a6SJani Nikula #define TMDS_297M 297000
155df0566a6SJani Nikula #define TMDS_296M 296703
156df0566a6SJani Nikula #define TMDS_594M 594000
157df0566a6SJani Nikula #define TMDS_593M 593407
158df0566a6SJani Nikula 
1592c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_24bpp[] = {
160df0566a6SJani Nikula 	{ 32000, TMDS_296M, 5824, 421875 },
161df0566a6SJani Nikula 	{ 32000, TMDS_297M, 3072, 222750 },
162df0566a6SJani Nikula 	{ 32000, TMDS_593M, 5824, 843750 },
163df0566a6SJani Nikula 	{ 32000, TMDS_594M, 3072, 445500 },
164df0566a6SJani Nikula 	{ 44100, TMDS_296M, 4459, 234375 },
165df0566a6SJani Nikula 	{ 44100, TMDS_297M, 4704, 247500 },
166df0566a6SJani Nikula 	{ 44100, TMDS_593M, 8918, 937500 },
167df0566a6SJani Nikula 	{ 44100, TMDS_594M, 9408, 990000 },
168df0566a6SJani Nikula 	{ 88200, TMDS_296M, 8918, 234375 },
169df0566a6SJani Nikula 	{ 88200, TMDS_297M, 9408, 247500 },
170df0566a6SJani Nikula 	{ 88200, TMDS_593M, 17836, 937500 },
171df0566a6SJani Nikula 	{ 88200, TMDS_594M, 18816, 990000 },
172df0566a6SJani Nikula 	{ 176400, TMDS_296M, 17836, 234375 },
173df0566a6SJani Nikula 	{ 176400, TMDS_297M, 18816, 247500 },
174df0566a6SJani Nikula 	{ 176400, TMDS_593M, 35672, 937500 },
175df0566a6SJani Nikula 	{ 176400, TMDS_594M, 37632, 990000 },
176df0566a6SJani Nikula 	{ 48000, TMDS_296M, 5824, 281250 },
177df0566a6SJani Nikula 	{ 48000, TMDS_297M, 5120, 247500 },
178df0566a6SJani Nikula 	{ 48000, TMDS_593M, 5824, 562500 },
179df0566a6SJani Nikula 	{ 48000, TMDS_594M, 6144, 594000 },
180df0566a6SJani Nikula 	{ 96000, TMDS_296M, 11648, 281250 },
181df0566a6SJani Nikula 	{ 96000, TMDS_297M, 10240, 247500 },
182df0566a6SJani Nikula 	{ 96000, TMDS_593M, 11648, 562500 },
183df0566a6SJani Nikula 	{ 96000, TMDS_594M, 12288, 594000 },
184df0566a6SJani Nikula 	{ 192000, TMDS_296M, 23296, 281250 },
185df0566a6SJani Nikula 	{ 192000, TMDS_297M, 20480, 247500 },
186df0566a6SJani Nikula 	{ 192000, TMDS_593M, 23296, 562500 },
187df0566a6SJani Nikula 	{ 192000, TMDS_594M, 24576, 594000 },
188df0566a6SJani Nikula };
189df0566a6SJani Nikula 
1902c291417SAditya Swarup /* Appendix C - N & CTS values for deep color from HDMI 2.0 spec*/
1912c291417SAditya Swarup /* HDMI N/CTS table for 10 bit deep color(30 bpp)*/
1922c291417SAditya Swarup #define TMDS_371M 371250
1932c291417SAditya Swarup #define TMDS_370M 370878
1942c291417SAditya Swarup 
1952c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_30bpp[] = {
1962c291417SAditya Swarup 	{ 32000, TMDS_370M, 5824, 527344 },
1972c291417SAditya Swarup 	{ 32000, TMDS_371M, 6144, 556875 },
1982c291417SAditya Swarup 	{ 44100, TMDS_370M, 8918, 585938 },
1992c291417SAditya Swarup 	{ 44100, TMDS_371M, 4704, 309375 },
2002c291417SAditya Swarup 	{ 88200, TMDS_370M, 17836, 585938 },
2012c291417SAditya Swarup 	{ 88200, TMDS_371M, 9408, 309375 },
2022c291417SAditya Swarup 	{ 176400, TMDS_370M, 35672, 585938 },
2032c291417SAditya Swarup 	{ 176400, TMDS_371M, 18816, 309375 },
2042c291417SAditya Swarup 	{ 48000, TMDS_370M, 11648, 703125 },
2052c291417SAditya Swarup 	{ 48000, TMDS_371M, 5120, 309375 },
2062c291417SAditya Swarup 	{ 96000, TMDS_370M, 23296, 703125 },
2072c291417SAditya Swarup 	{ 96000, TMDS_371M, 10240, 309375 },
2082c291417SAditya Swarup 	{ 192000, TMDS_370M, 46592, 703125 },
2092c291417SAditya Swarup 	{ 192000, TMDS_371M, 20480, 309375 },
2102c291417SAditya Swarup };
2112c291417SAditya Swarup 
2122c291417SAditya Swarup /* HDMI N/CTS table for 12 bit deep color(36 bpp)*/
2132c291417SAditya Swarup #define TMDS_445_5M 445500
2142c291417SAditya Swarup #define TMDS_445M 445054
2152c291417SAditya Swarup 
2162c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_36bpp[] = {
2172c291417SAditya Swarup 	{ 32000, TMDS_445M, 5824, 632813 },
2182c291417SAditya Swarup 	{ 32000, TMDS_445_5M, 4096, 445500 },
2192c291417SAditya Swarup 	{ 44100, TMDS_445M, 8918, 703125 },
2202c291417SAditya Swarup 	{ 44100, TMDS_445_5M, 4704, 371250 },
2212c291417SAditya Swarup 	{ 88200, TMDS_445M, 17836, 703125 },
2222c291417SAditya Swarup 	{ 88200, TMDS_445_5M, 9408, 371250 },
2232c291417SAditya Swarup 	{ 176400, TMDS_445M, 35672, 703125 },
2242c291417SAditya Swarup 	{ 176400, TMDS_445_5M, 18816, 371250 },
2252c291417SAditya Swarup 	{ 48000, TMDS_445M, 5824, 421875 },
2262c291417SAditya Swarup 	{ 48000, TMDS_445_5M, 5120, 371250 },
2272c291417SAditya Swarup 	{ 96000, TMDS_445M, 11648, 421875 },
2282c291417SAditya Swarup 	{ 96000, TMDS_445_5M, 10240, 371250 },
2292c291417SAditya Swarup 	{ 192000, TMDS_445M, 23296, 421875 },
2302c291417SAditya Swarup 	{ 192000, TMDS_445_5M, 20480, 371250 },
2312c291417SAditya Swarup };
2322c291417SAditya Swarup 
233df0566a6SJani Nikula /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
234df0566a6SJani Nikula static u32 audio_config_hdmi_pixel_clock(const struct intel_crtc_state *crtc_state)
235df0566a6SJani Nikula {
236df0566a6SJani Nikula 	const struct drm_display_mode *adjusted_mode =
2371326a92cSMaarten Lankhorst 		&crtc_state->hw.adjusted_mode;
238df0566a6SJani Nikula 	int i;
239df0566a6SJani Nikula 
240df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
241df0566a6SJani Nikula 		if (adjusted_mode->crtc_clock == hdmi_audio_clock[i].clock)
242df0566a6SJani Nikula 			break;
243df0566a6SJani Nikula 	}
244df0566a6SJani Nikula 
245df0566a6SJani Nikula 	if (i == ARRAY_SIZE(hdmi_audio_clock)) {
246df0566a6SJani Nikula 		DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n",
247df0566a6SJani Nikula 			      adjusted_mode->crtc_clock);
248df0566a6SJani Nikula 		i = 1;
249df0566a6SJani Nikula 	}
250df0566a6SJani Nikula 
251df0566a6SJani Nikula 	DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
252df0566a6SJani Nikula 		      hdmi_audio_clock[i].clock,
253df0566a6SJani Nikula 		      hdmi_audio_clock[i].config);
254df0566a6SJani Nikula 
255df0566a6SJani Nikula 	return hdmi_audio_clock[i].config;
256df0566a6SJani Nikula }
257df0566a6SJani Nikula 
258df0566a6SJani Nikula static int audio_config_hdmi_get_n(const struct intel_crtc_state *crtc_state,
259df0566a6SJani Nikula 				   int rate)
260df0566a6SJani Nikula {
2612c291417SAditya Swarup 	const struct hdmi_aud_ncts *hdmi_ncts_table;
2622c291417SAditya Swarup 	int i, size;
263df0566a6SJani Nikula 
2642c291417SAditya Swarup 	if (crtc_state->pipe_bpp == 36) {
2652c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_36bpp;
2662c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_36bpp);
2672c291417SAditya Swarup 	} else if (crtc_state->pipe_bpp == 30) {
2682c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_30bpp;
2692c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_30bpp);
2702c291417SAditya Swarup 	} else {
2712c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_24bpp;
2722c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_24bpp);
2732c291417SAditya Swarup 	}
2742c291417SAditya Swarup 
2752c291417SAditya Swarup 	for (i = 0; i < size; i++) {
2762c291417SAditya Swarup 		if (rate == hdmi_ncts_table[i].sample_rate &&
2772c291417SAditya Swarup 		    crtc_state->port_clock == hdmi_ncts_table[i].clock) {
2782c291417SAditya Swarup 			return hdmi_ncts_table[i].n;
279df0566a6SJani Nikula 		}
280df0566a6SJani Nikula 	}
281df0566a6SJani Nikula 	return 0;
282df0566a6SJani Nikula }
283df0566a6SJani Nikula 
284df0566a6SJani Nikula static bool intel_eld_uptodate(struct drm_connector *connector,
285df0566a6SJani Nikula 			       i915_reg_t reg_eldv, u32 bits_eldv,
286df0566a6SJani Nikula 			       i915_reg_t reg_elda, u32 bits_elda,
287df0566a6SJani Nikula 			       i915_reg_t reg_edid)
288df0566a6SJani Nikula {
289df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
290df0566a6SJani Nikula 	const u8 *eld = connector->eld;
291df0566a6SJani Nikula 	u32 tmp;
292df0566a6SJani Nikula 	int i;
293df0566a6SJani Nikula 
294df0566a6SJani Nikula 	tmp = I915_READ(reg_eldv);
295df0566a6SJani Nikula 	tmp &= bits_eldv;
296df0566a6SJani Nikula 
297df0566a6SJani Nikula 	if (!tmp)
298df0566a6SJani Nikula 		return false;
299df0566a6SJani Nikula 
300df0566a6SJani Nikula 	tmp = I915_READ(reg_elda);
301df0566a6SJani Nikula 	tmp &= ~bits_elda;
302df0566a6SJani Nikula 	I915_WRITE(reg_elda, tmp);
303df0566a6SJani Nikula 
304df0566a6SJani Nikula 	for (i = 0; i < drm_eld_size(eld) / 4; i++)
305df0566a6SJani Nikula 		if (I915_READ(reg_edid) != *((const u32 *)eld + i))
306df0566a6SJani Nikula 			return false;
307df0566a6SJani Nikula 
308df0566a6SJani Nikula 	return true;
309df0566a6SJani Nikula }
310df0566a6SJani Nikula 
311df0566a6SJani Nikula static void g4x_audio_codec_disable(struct intel_encoder *encoder,
312df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
313df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
314df0566a6SJani Nikula {
315df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
316df0566a6SJani Nikula 	u32 eldv, tmp;
317df0566a6SJani Nikula 
318*63855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "Disable audio codec\n");
319df0566a6SJani Nikula 
320df0566a6SJani Nikula 	tmp = I915_READ(G4X_AUD_VID_DID);
321df0566a6SJani Nikula 	if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
322df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCL_DEVBLC;
323df0566a6SJani Nikula 	else
324df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCTG;
325df0566a6SJani Nikula 
326df0566a6SJani Nikula 	/* Invalidate ELD */
327df0566a6SJani Nikula 	tmp = I915_READ(G4X_AUD_CNTL_ST);
328df0566a6SJani Nikula 	tmp &= ~eldv;
329df0566a6SJani Nikula 	I915_WRITE(G4X_AUD_CNTL_ST, tmp);
330df0566a6SJani Nikula }
331df0566a6SJani Nikula 
332df0566a6SJani Nikula static void g4x_audio_codec_enable(struct intel_encoder *encoder,
333df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
334df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
335df0566a6SJani Nikula {
336df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
337df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
338df0566a6SJani Nikula 	const u8 *eld = connector->eld;
339df0566a6SJani Nikula 	u32 eldv;
340df0566a6SJani Nikula 	u32 tmp;
341df0566a6SJani Nikula 	int len, i;
342df0566a6SJani Nikula 
343*63855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "Enable audio codec, %u bytes ELD\n",
344*63855149SWambui Karuga 		    drm_eld_size(eld));
345df0566a6SJani Nikula 
346df0566a6SJani Nikula 	tmp = I915_READ(G4X_AUD_VID_DID);
347df0566a6SJani Nikula 	if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
348df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCL_DEVBLC;
349df0566a6SJani Nikula 	else
350df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCTG;
351df0566a6SJani Nikula 
352df0566a6SJani Nikula 	if (intel_eld_uptodate(connector,
353df0566a6SJani Nikula 			       G4X_AUD_CNTL_ST, eldv,
354df0566a6SJani Nikula 			       G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
355df0566a6SJani Nikula 			       G4X_HDMIW_HDMIEDID))
356df0566a6SJani Nikula 		return;
357df0566a6SJani Nikula 
358df0566a6SJani Nikula 	tmp = I915_READ(G4X_AUD_CNTL_ST);
359df0566a6SJani Nikula 	tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
360df0566a6SJani Nikula 	len = (tmp >> 9) & 0x1f;		/* ELD buffer size */
361df0566a6SJani Nikula 	I915_WRITE(G4X_AUD_CNTL_ST, tmp);
362df0566a6SJani Nikula 
363df0566a6SJani Nikula 	len = min(drm_eld_size(eld) / 4, len);
364*63855149SWambui Karuga 	drm_dbg(&dev_priv->drm, "ELD size %d\n", len);
365df0566a6SJani Nikula 	for (i = 0; i < len; i++)
366df0566a6SJani Nikula 		I915_WRITE(G4X_HDMIW_HDMIEDID, *((const u32 *)eld + i));
367df0566a6SJani Nikula 
368df0566a6SJani Nikula 	tmp = I915_READ(G4X_AUD_CNTL_ST);
369df0566a6SJani Nikula 	tmp |= eldv;
370df0566a6SJani Nikula 	I915_WRITE(G4X_AUD_CNTL_ST, tmp);
371df0566a6SJani Nikula }
372df0566a6SJani Nikula 
373df0566a6SJani Nikula static void
374df0566a6SJani Nikula hsw_dp_audio_config_update(struct intel_encoder *encoder,
375df0566a6SJani Nikula 			   const struct intel_crtc_state *crtc_state)
376df0566a6SJani Nikula {
377df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
378df0566a6SJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio_component;
379df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
380df0566a6SJani Nikula 	enum port port = encoder->port;
381df0566a6SJani Nikula 	const struct dp_aud_n_m *nm;
382df0566a6SJani Nikula 	int rate;
383df0566a6SJani Nikula 	u32 tmp;
384df0566a6SJani Nikula 
385df0566a6SJani Nikula 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
386df0566a6SJani Nikula 	nm = audio_config_dp_get_n_m(crtc_state, rate);
387df0566a6SJani Nikula 	if (nm)
388*63855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using Maud %u, Naud %u\n", nm->m,
389*63855149SWambui Karuga 			    nm->n);
390df0566a6SJani Nikula 	else
391*63855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using automatic Maud, Naud\n");
392df0566a6SJani Nikula 
393df0566a6SJani Nikula 	tmp = I915_READ(HSW_AUD_CFG(cpu_transcoder));
394df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
395df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
396df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
397df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_VALUE_INDEX;
398df0566a6SJani Nikula 
399df0566a6SJani Nikula 	if (nm) {
400df0566a6SJani Nikula 		tmp &= ~AUD_CONFIG_N_MASK;
401df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N(nm->n);
402df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
403df0566a6SJani Nikula 	}
404df0566a6SJani Nikula 
405df0566a6SJani Nikula 	I915_WRITE(HSW_AUD_CFG(cpu_transcoder), tmp);
406df0566a6SJani Nikula 
407df0566a6SJani Nikula 	tmp = I915_READ(HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
408df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_M_MASK;
409df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
410df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
411df0566a6SJani Nikula 
412df0566a6SJani Nikula 	if (nm) {
413df0566a6SJani Nikula 		tmp |= nm->m;
414df0566a6SJani Nikula 		tmp |= AUD_M_CTS_M_VALUE_INDEX;
415df0566a6SJani Nikula 		tmp |= AUD_M_CTS_M_PROG_ENABLE;
416df0566a6SJani Nikula 	}
417df0566a6SJani Nikula 
418df0566a6SJani Nikula 	I915_WRITE(HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
419df0566a6SJani Nikula }
420df0566a6SJani Nikula 
421df0566a6SJani Nikula static void
422df0566a6SJani Nikula hsw_hdmi_audio_config_update(struct intel_encoder *encoder,
423df0566a6SJani Nikula 			     const struct intel_crtc_state *crtc_state)
424df0566a6SJani Nikula {
425df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
426df0566a6SJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio_component;
427df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
428df0566a6SJani Nikula 	enum port port = encoder->port;
429df0566a6SJani Nikula 	int n, rate;
430df0566a6SJani Nikula 	u32 tmp;
431df0566a6SJani Nikula 
432df0566a6SJani Nikula 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
433df0566a6SJani Nikula 
434df0566a6SJani Nikula 	tmp = I915_READ(HSW_AUD_CFG(cpu_transcoder));
435df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
436df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
437df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
438df0566a6SJani Nikula 	tmp |= audio_config_hdmi_pixel_clock(crtc_state);
439df0566a6SJani Nikula 
440df0566a6SJani Nikula 	n = audio_config_hdmi_get_n(crtc_state, rate);
441df0566a6SJani Nikula 	if (n != 0) {
442*63855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using N %d\n", n);
443df0566a6SJani Nikula 
444df0566a6SJani Nikula 		tmp &= ~AUD_CONFIG_N_MASK;
445df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N(n);
446df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
447df0566a6SJani Nikula 	} else {
448*63855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using automatic N\n");
449df0566a6SJani Nikula 	}
450df0566a6SJani Nikula 
451df0566a6SJani Nikula 	I915_WRITE(HSW_AUD_CFG(cpu_transcoder), tmp);
452df0566a6SJani Nikula 
453df0566a6SJani Nikula 	/*
454df0566a6SJani Nikula 	 * Let's disable "Enable CTS or M Prog bit"
455df0566a6SJani Nikula 	 * and let HW calculate the value
456df0566a6SJani Nikula 	 */
457df0566a6SJani Nikula 	tmp = I915_READ(HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
458df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
459df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
460df0566a6SJani Nikula 	I915_WRITE(HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
461df0566a6SJani Nikula }
462df0566a6SJani Nikula 
463df0566a6SJani Nikula static void
464df0566a6SJani Nikula hsw_audio_config_update(struct intel_encoder *encoder,
465df0566a6SJani Nikula 			const struct intel_crtc_state *crtc_state)
466df0566a6SJani Nikula {
467df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state))
468df0566a6SJani Nikula 		hsw_dp_audio_config_update(encoder, crtc_state);
469df0566a6SJani Nikula 	else
470df0566a6SJani Nikula 		hsw_hdmi_audio_config_update(encoder, crtc_state);
471df0566a6SJani Nikula }
472df0566a6SJani Nikula 
473df0566a6SJani Nikula static void hsw_audio_codec_disable(struct intel_encoder *encoder,
474df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
475df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
476df0566a6SJani Nikula {
477df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
478df0566a6SJani Nikula 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
479df0566a6SJani Nikula 	u32 tmp;
480df0566a6SJani Nikula 
481*63855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "Disable audio codec on transcoder %s\n",
482df0566a6SJani Nikula 		    transcoder_name(cpu_transcoder));
483df0566a6SJani Nikula 
484df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
485df0566a6SJani Nikula 
486df0566a6SJani Nikula 	/* Disable timestamps */
487df0566a6SJani Nikula 	tmp = I915_READ(HSW_AUD_CFG(cpu_transcoder));
488df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
489df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_PROG_ENABLE;
490df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_UPPER_N_MASK;
491df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_LOWER_N_MASK;
492df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(old_crtc_state))
493df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
494df0566a6SJani Nikula 	I915_WRITE(HSW_AUD_CFG(cpu_transcoder), tmp);
495df0566a6SJani Nikula 
496df0566a6SJani Nikula 	/* Invalidate ELD */
497df0566a6SJani Nikula 	tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
498df0566a6SJani Nikula 	tmp &= ~AUDIO_ELD_VALID(cpu_transcoder);
499df0566a6SJani Nikula 	tmp &= ~AUDIO_OUTPUT_ENABLE(cpu_transcoder);
500df0566a6SJani Nikula 	I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
501df0566a6SJani Nikula 
502df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
503df0566a6SJani Nikula }
504df0566a6SJani Nikula 
505df0566a6SJani Nikula static void hsw_audio_codec_enable(struct intel_encoder *encoder,
506df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
507df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
508df0566a6SJani Nikula {
509df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
510df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
511df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
512df0566a6SJani Nikula 	const u8 *eld = connector->eld;
513df0566a6SJani Nikula 	u32 tmp;
514df0566a6SJani Nikula 	int len, i;
515df0566a6SJani Nikula 
516*63855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
517*63855149SWambui Karuga 		    "Enable audio codec on transcoder %s, %u bytes ELD\n",
518df0566a6SJani Nikula 		     transcoder_name(cpu_transcoder), drm_eld_size(eld));
519df0566a6SJani Nikula 
520df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
521df0566a6SJani Nikula 
522df0566a6SJani Nikula 	/* Enable audio presence detect, invalidate ELD */
523df0566a6SJani Nikula 	tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
524df0566a6SJani Nikula 	tmp |= AUDIO_OUTPUT_ENABLE(cpu_transcoder);
525df0566a6SJani Nikula 	tmp &= ~AUDIO_ELD_VALID(cpu_transcoder);
526df0566a6SJani Nikula 	I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
527df0566a6SJani Nikula 
528df0566a6SJani Nikula 	/*
529df0566a6SJani Nikula 	 * FIXME: We're supposed to wait for vblank here, but we have vblanks
530df0566a6SJani Nikula 	 * disabled during the mode set. The proper fix would be to push the
531df0566a6SJani Nikula 	 * rest of the setup into a vblank work item, queued here, but the
532df0566a6SJani Nikula 	 * infrastructure is not there yet.
533df0566a6SJani Nikula 	 */
534df0566a6SJani Nikula 
535df0566a6SJani Nikula 	/* Reset ELD write address */
536df0566a6SJani Nikula 	tmp = I915_READ(HSW_AUD_DIP_ELD_CTRL(cpu_transcoder));
537df0566a6SJani Nikula 	tmp &= ~IBX_ELD_ADDRESS_MASK;
538df0566a6SJani Nikula 	I915_WRITE(HSW_AUD_DIP_ELD_CTRL(cpu_transcoder), tmp);
539df0566a6SJani Nikula 
540df0566a6SJani Nikula 	/* Up to 84 bytes of hw ELD buffer */
541df0566a6SJani Nikula 	len = min(drm_eld_size(eld), 84);
542df0566a6SJani Nikula 	for (i = 0; i < len / 4; i++)
543df0566a6SJani Nikula 		I915_WRITE(HSW_AUD_EDID_DATA(cpu_transcoder), *((const u32 *)eld + i));
544df0566a6SJani Nikula 
545df0566a6SJani Nikula 	/* ELD valid */
546df0566a6SJani Nikula 	tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
547df0566a6SJani Nikula 	tmp |= AUDIO_ELD_VALID(cpu_transcoder);
548df0566a6SJani Nikula 	I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
549df0566a6SJani Nikula 
550df0566a6SJani Nikula 	/* Enable timestamps */
551df0566a6SJani Nikula 	hsw_audio_config_update(encoder, crtc_state);
552df0566a6SJani Nikula 
553df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
554df0566a6SJani Nikula }
555df0566a6SJani Nikula 
556df0566a6SJani Nikula static void ilk_audio_codec_disable(struct intel_encoder *encoder,
557df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
558df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
559df0566a6SJani Nikula {
560df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
5612225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
562df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
563df0566a6SJani Nikula 	enum port port = encoder->port;
564df0566a6SJani Nikula 	u32 tmp, eldv;
565df0566a6SJani Nikula 	i915_reg_t aud_config, aud_cntrl_st2;
566df0566a6SJani Nikula 
567*63855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
568*63855149SWambui Karuga 		    "Disable audio codec on [ENCODER:%d:%s], pipe %c\n",
56966a990ddSVille Syrjälä 		     encoder->base.base.id, encoder->base.name,
57066a990ddSVille Syrjälä 		     pipe_name(pipe));
571df0566a6SJani Nikula 
572df0566a6SJani Nikula 	if (WARN_ON(port == PORT_A))
573df0566a6SJani Nikula 		return;
574df0566a6SJani Nikula 
575df0566a6SJani Nikula 	if (HAS_PCH_IBX(dev_priv)) {
576df0566a6SJani Nikula 		aud_config = IBX_AUD_CFG(pipe);
577df0566a6SJani Nikula 		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
578df0566a6SJani Nikula 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
579df0566a6SJani Nikula 		aud_config = VLV_AUD_CFG(pipe);
580df0566a6SJani Nikula 		aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
581df0566a6SJani Nikula 	} else {
582df0566a6SJani Nikula 		aud_config = CPT_AUD_CFG(pipe);
583df0566a6SJani Nikula 		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
584df0566a6SJani Nikula 	}
585df0566a6SJani Nikula 
586df0566a6SJani Nikula 	/* Disable timestamps */
587df0566a6SJani Nikula 	tmp = I915_READ(aud_config);
588df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
589df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_PROG_ENABLE;
590df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_UPPER_N_MASK;
591df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_LOWER_N_MASK;
592df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(old_crtc_state))
593df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
594df0566a6SJani Nikula 	I915_WRITE(aud_config, tmp);
595df0566a6SJani Nikula 
596df0566a6SJani Nikula 	eldv = IBX_ELD_VALID(port);
597df0566a6SJani Nikula 
598df0566a6SJani Nikula 	/* Invalidate ELD */
599df0566a6SJani Nikula 	tmp = I915_READ(aud_cntrl_st2);
600df0566a6SJani Nikula 	tmp &= ~eldv;
601df0566a6SJani Nikula 	I915_WRITE(aud_cntrl_st2, tmp);
602df0566a6SJani Nikula }
603df0566a6SJani Nikula 
604df0566a6SJani Nikula static void ilk_audio_codec_enable(struct intel_encoder *encoder,
605df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
606df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
607df0566a6SJani Nikula {
608df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
6092225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
610df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
611df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
612df0566a6SJani Nikula 	enum port port = encoder->port;
613df0566a6SJani Nikula 	const u8 *eld = connector->eld;
614df0566a6SJani Nikula 	u32 tmp, eldv;
615df0566a6SJani Nikula 	int len, i;
616df0566a6SJani Nikula 	i915_reg_t hdmiw_hdmiedid, aud_config, aud_cntl_st, aud_cntrl_st2;
617df0566a6SJani Nikula 
618*63855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
619*63855149SWambui Karuga 		    "Enable audio codec on [ENCODER:%d:%s], pipe %c, %u bytes ELD\n",
62066a990ddSVille Syrjälä 		    encoder->base.base.id, encoder->base.name,
62166a990ddSVille Syrjälä 		    pipe_name(pipe), drm_eld_size(eld));
622df0566a6SJani Nikula 
623df0566a6SJani Nikula 	if (WARN_ON(port == PORT_A))
624df0566a6SJani Nikula 		return;
625df0566a6SJani Nikula 
626df0566a6SJani Nikula 	/*
627df0566a6SJani Nikula 	 * FIXME: We're supposed to wait for vblank here, but we have vblanks
628df0566a6SJani Nikula 	 * disabled during the mode set. The proper fix would be to push the
629df0566a6SJani Nikula 	 * rest of the setup into a vblank work item, queued here, but the
630df0566a6SJani Nikula 	 * infrastructure is not there yet.
631df0566a6SJani Nikula 	 */
632df0566a6SJani Nikula 
633df0566a6SJani Nikula 	if (HAS_PCH_IBX(dev_priv)) {
634df0566a6SJani Nikula 		hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
635df0566a6SJani Nikula 		aud_config = IBX_AUD_CFG(pipe);
636df0566a6SJani Nikula 		aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
637df0566a6SJani Nikula 		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
638df0566a6SJani Nikula 	} else if (IS_VALLEYVIEW(dev_priv) ||
639df0566a6SJani Nikula 		   IS_CHERRYVIEW(dev_priv)) {
640df0566a6SJani Nikula 		hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
641df0566a6SJani Nikula 		aud_config = VLV_AUD_CFG(pipe);
642df0566a6SJani Nikula 		aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
643df0566a6SJani Nikula 		aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
644df0566a6SJani Nikula 	} else {
645df0566a6SJani Nikula 		hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
646df0566a6SJani Nikula 		aud_config = CPT_AUD_CFG(pipe);
647df0566a6SJani Nikula 		aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
648df0566a6SJani Nikula 		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
649df0566a6SJani Nikula 	}
650df0566a6SJani Nikula 
651df0566a6SJani Nikula 	eldv = IBX_ELD_VALID(port);
652df0566a6SJani Nikula 
653df0566a6SJani Nikula 	/* Invalidate ELD */
654df0566a6SJani Nikula 	tmp = I915_READ(aud_cntrl_st2);
655df0566a6SJani Nikula 	tmp &= ~eldv;
656df0566a6SJani Nikula 	I915_WRITE(aud_cntrl_st2, tmp);
657df0566a6SJani Nikula 
658df0566a6SJani Nikula 	/* Reset ELD write address */
659df0566a6SJani Nikula 	tmp = I915_READ(aud_cntl_st);
660df0566a6SJani Nikula 	tmp &= ~IBX_ELD_ADDRESS_MASK;
661df0566a6SJani Nikula 	I915_WRITE(aud_cntl_st, tmp);
662df0566a6SJani Nikula 
663df0566a6SJani Nikula 	/* Up to 84 bytes of hw ELD buffer */
664df0566a6SJani Nikula 	len = min(drm_eld_size(eld), 84);
665df0566a6SJani Nikula 	for (i = 0; i < len / 4; i++)
666df0566a6SJani Nikula 		I915_WRITE(hdmiw_hdmiedid, *((const u32 *)eld + i));
667df0566a6SJani Nikula 
668df0566a6SJani Nikula 	/* ELD valid */
669df0566a6SJani Nikula 	tmp = I915_READ(aud_cntrl_st2);
670df0566a6SJani Nikula 	tmp |= eldv;
671df0566a6SJani Nikula 	I915_WRITE(aud_cntrl_st2, tmp);
672df0566a6SJani Nikula 
673df0566a6SJani Nikula 	/* Enable timestamps */
674df0566a6SJani Nikula 	tmp = I915_READ(aud_config);
675df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
676df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
677df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
678df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state))
679df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
680df0566a6SJani Nikula 	else
681df0566a6SJani Nikula 		tmp |= audio_config_hdmi_pixel_clock(crtc_state);
682df0566a6SJani Nikula 	I915_WRITE(aud_config, tmp);
683df0566a6SJani Nikula }
684df0566a6SJani Nikula 
685df0566a6SJani Nikula /**
686df0566a6SJani Nikula  * intel_audio_codec_enable - Enable the audio codec for HD audio
687df0566a6SJani Nikula  * @encoder: encoder on which to enable audio
688df0566a6SJani Nikula  * @crtc_state: pointer to the current crtc state.
689df0566a6SJani Nikula  * @conn_state: pointer to the current connector state.
690df0566a6SJani Nikula  *
691df0566a6SJani Nikula  * The enable sequences may only be performed after enabling the transcoder and
692df0566a6SJani Nikula  * port, and after completed link training.
693df0566a6SJani Nikula  */
694df0566a6SJani Nikula void intel_audio_codec_enable(struct intel_encoder *encoder,
695df0566a6SJani Nikula 			      const struct intel_crtc_state *crtc_state,
696df0566a6SJani Nikula 			      const struct drm_connector_state *conn_state)
697df0566a6SJani Nikula {
698df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
699df0566a6SJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio_component;
7002225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
701df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
702df0566a6SJani Nikula 	const struct drm_display_mode *adjusted_mode =
7031326a92cSMaarten Lankhorst 		&crtc_state->hw.adjusted_mode;
704df0566a6SJani Nikula 	enum port port = encoder->port;
705df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
706df0566a6SJani Nikula 
707df0566a6SJani Nikula 	/* FIXME precompute the ELD in .compute_config() */
708df0566a6SJani Nikula 	if (!connector->eld[0])
709*63855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
710*63855149SWambui Karuga 			    "Bogus ELD on [CONNECTOR:%d:%s]\n",
711df0566a6SJani Nikula 			    connector->base.id, connector->name);
712df0566a6SJani Nikula 
713*63855149SWambui Karuga 	drm_dbg(&dev_priv->drm, "ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
714df0566a6SJani Nikula 		connector->base.id,
715df0566a6SJani Nikula 		connector->name,
71679003e61SVille Syrjälä 		encoder->base.base.id,
71779003e61SVille Syrjälä 		encoder->base.name);
718df0566a6SJani Nikula 
719df0566a6SJani Nikula 	connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
720df0566a6SJani Nikula 
721df0566a6SJani Nikula 	if (dev_priv->display.audio_codec_enable)
722df0566a6SJani Nikula 		dev_priv->display.audio_codec_enable(encoder,
723df0566a6SJani Nikula 						     crtc_state,
724df0566a6SJani Nikula 						     conn_state);
725df0566a6SJani Nikula 
726df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
727df0566a6SJani Nikula 	encoder->audio_connector = connector;
728df0566a6SJani Nikula 
729df0566a6SJani Nikula 	/* referred in audio callbacks */
730df0566a6SJani Nikula 	dev_priv->av_enc_map[pipe] = encoder;
731df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
732df0566a6SJani Nikula 
733df0566a6SJani Nikula 	if (acomp && acomp->base.audio_ops &&
734df0566a6SJani Nikula 	    acomp->base.audio_ops->pin_eld_notify) {
735df0566a6SJani Nikula 		/* audio drivers expect pipe = -1 to indicate Non-MST cases */
736df0566a6SJani Nikula 		if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
737df0566a6SJani Nikula 			pipe = -1;
738df0566a6SJani Nikula 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
739df0566a6SJani Nikula 						 (int) port, (int) pipe);
740df0566a6SJani Nikula 	}
741df0566a6SJani Nikula 
742df0566a6SJani Nikula 	intel_lpe_audio_notify(dev_priv, pipe, port, connector->eld,
743df0566a6SJani Nikula 			       crtc_state->port_clock,
744df0566a6SJani Nikula 			       intel_crtc_has_dp_encoder(crtc_state));
745df0566a6SJani Nikula }
746df0566a6SJani Nikula 
747df0566a6SJani Nikula /**
748df0566a6SJani Nikula  * intel_audio_codec_disable - Disable the audio codec for HD audio
749df0566a6SJani Nikula  * @encoder: encoder on which to disable audio
750df0566a6SJani Nikula  * @old_crtc_state: pointer to the old crtc state.
751df0566a6SJani Nikula  * @old_conn_state: pointer to the old connector state.
752df0566a6SJani Nikula  *
753df0566a6SJani Nikula  * The disable sequences must be performed before disabling the transcoder or
754df0566a6SJani Nikula  * port.
755df0566a6SJani Nikula  */
756df0566a6SJani Nikula void intel_audio_codec_disable(struct intel_encoder *encoder,
757df0566a6SJani Nikula 			       const struct intel_crtc_state *old_crtc_state,
758df0566a6SJani Nikula 			       const struct drm_connector_state *old_conn_state)
759df0566a6SJani Nikula {
760df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
761df0566a6SJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio_component;
7622225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
763df0566a6SJani Nikula 	enum port port = encoder->port;
764df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
765df0566a6SJani Nikula 
766df0566a6SJani Nikula 	if (dev_priv->display.audio_codec_disable)
767df0566a6SJani Nikula 		dev_priv->display.audio_codec_disable(encoder,
768df0566a6SJani Nikula 						      old_crtc_state,
769df0566a6SJani Nikula 						      old_conn_state);
770df0566a6SJani Nikula 
771df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
772df0566a6SJani Nikula 	encoder->audio_connector = NULL;
773df0566a6SJani Nikula 	dev_priv->av_enc_map[pipe] = NULL;
774df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
775df0566a6SJani Nikula 
776df0566a6SJani Nikula 	if (acomp && acomp->base.audio_ops &&
777df0566a6SJani Nikula 	    acomp->base.audio_ops->pin_eld_notify) {
778df0566a6SJani Nikula 		/* audio drivers expect pipe = -1 to indicate Non-MST cases */
779df0566a6SJani Nikula 		if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
780df0566a6SJani Nikula 			pipe = -1;
781df0566a6SJani Nikula 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
782df0566a6SJani Nikula 						 (int) port, (int) pipe);
783df0566a6SJani Nikula 	}
784df0566a6SJani Nikula 
785df0566a6SJani Nikula 	intel_lpe_audio_notify(dev_priv, pipe, port, NULL, 0, false);
786df0566a6SJani Nikula }
787df0566a6SJani Nikula 
788df0566a6SJani Nikula /**
789df0566a6SJani Nikula  * intel_init_audio_hooks - Set up chip specific audio hooks
790df0566a6SJani Nikula  * @dev_priv: device private
791df0566a6SJani Nikula  */
792df0566a6SJani Nikula void intel_init_audio_hooks(struct drm_i915_private *dev_priv)
793df0566a6SJani Nikula {
794df0566a6SJani Nikula 	if (IS_G4X(dev_priv)) {
795df0566a6SJani Nikula 		dev_priv->display.audio_codec_enable = g4x_audio_codec_enable;
796df0566a6SJani Nikula 		dev_priv->display.audio_codec_disable = g4x_audio_codec_disable;
797df0566a6SJani Nikula 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
798df0566a6SJani Nikula 		dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
799df0566a6SJani Nikula 		dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
800df0566a6SJani Nikula 	} else if (IS_HASWELL(dev_priv) || INTEL_GEN(dev_priv) >= 8) {
801df0566a6SJani Nikula 		dev_priv->display.audio_codec_enable = hsw_audio_codec_enable;
802df0566a6SJani Nikula 		dev_priv->display.audio_codec_disable = hsw_audio_codec_disable;
803df0566a6SJani Nikula 	} else if (HAS_PCH_SPLIT(dev_priv)) {
804df0566a6SJani Nikula 		dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
805df0566a6SJani Nikula 		dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
806df0566a6SJani Nikula 	}
807df0566a6SJani Nikula }
808df0566a6SJani Nikula 
809df0566a6SJani Nikula static void glk_force_audio_cdclk(struct drm_i915_private *dev_priv,
810df0566a6SJani Nikula 				  bool enable)
811df0566a6SJani Nikula {
812df0566a6SJani Nikula 	struct drm_modeset_acquire_ctx ctx;
813df0566a6SJani Nikula 	struct drm_atomic_state *state;
814df0566a6SJani Nikula 	int ret;
815df0566a6SJani Nikula 
816df0566a6SJani Nikula 	drm_modeset_acquire_init(&ctx, 0);
817df0566a6SJani Nikula 	state = drm_atomic_state_alloc(&dev_priv->drm);
818df0566a6SJani Nikula 	if (WARN_ON(!state))
819df0566a6SJani Nikula 		return;
820df0566a6SJani Nikula 
821df0566a6SJani Nikula 	state->acquire_ctx = &ctx;
822df0566a6SJani Nikula 
823df0566a6SJani Nikula retry:
824df0566a6SJani Nikula 	to_intel_atomic_state(state)->cdclk.force_min_cdclk_changed = true;
825df0566a6SJani Nikula 	to_intel_atomic_state(state)->cdclk.force_min_cdclk =
826df0566a6SJani Nikula 		enable ? 2 * 96000 : 0;
827df0566a6SJani Nikula 
8281d5a95b5SVille Syrjälä 	/* Protects dev_priv->cdclk.force_min_cdclk */
8291d5a95b5SVille Syrjälä 	ret = intel_atomic_lock_global_state(to_intel_atomic_state(state));
830df0566a6SJani Nikula 	if (!ret)
831df0566a6SJani Nikula 		ret = drm_atomic_commit(state);
832df0566a6SJani Nikula 
833df0566a6SJani Nikula 	if (ret == -EDEADLK) {
834df0566a6SJani Nikula 		drm_atomic_state_clear(state);
835df0566a6SJani Nikula 		drm_modeset_backoff(&ctx);
836df0566a6SJani Nikula 		goto retry;
837df0566a6SJani Nikula 	}
838df0566a6SJani Nikula 
839df0566a6SJani Nikula 	WARN_ON(ret);
840df0566a6SJani Nikula 
841df0566a6SJani Nikula 	drm_atomic_state_put(state);
842df0566a6SJani Nikula 
843df0566a6SJani Nikula 	drm_modeset_drop_locks(&ctx);
844df0566a6SJani Nikula 	drm_modeset_acquire_fini(&ctx);
845df0566a6SJani Nikula }
846df0566a6SJani Nikula 
847df0566a6SJani Nikula static unsigned long i915_audio_component_get_power(struct device *kdev)
848df0566a6SJani Nikula {
849df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
850df0566a6SJani Nikula 	intel_wakeref_t ret;
851df0566a6SJani Nikula 
852df0566a6SJani Nikula 	/* Catch potential impedance mismatches before they occur! */
853df0566a6SJani Nikula 	BUILD_BUG_ON(sizeof(intel_wakeref_t) > sizeof(unsigned long));
854df0566a6SJani Nikula 
855df0566a6SJani Nikula 	ret = intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
856df0566a6SJani Nikula 
85787c16945SKai Vehmanen 	if (dev_priv->audio_power_refcount++ == 0) {
85887c16945SKai Vehmanen 		if (IS_TIGERLAKE(dev_priv) || IS_ICELAKE(dev_priv)) {
85987c16945SKai Vehmanen 			I915_WRITE(AUD_FREQ_CNTRL, dev_priv->audio_freq_cntrl);
860*63855149SWambui Karuga 			drm_dbg_kms(&dev_priv->drm,
861*63855149SWambui Karuga 				    "restored AUD_FREQ_CNTRL to 0x%x\n",
86287c16945SKai Vehmanen 				    dev_priv->audio_freq_cntrl);
86387c16945SKai Vehmanen 		}
86487c16945SKai Vehmanen 
86587c16945SKai Vehmanen 		/* Force CDCLK to 2*BCLK as long as we need audio powered. */
8661ee48a61SKai Vehmanen 		if (IS_GEMINILAKE(dev_priv))
867df0566a6SJani Nikula 			glk_force_audio_cdclk(dev_priv, true);
8681580d3cdSKai Vehmanen 
8691580d3cdSKai Vehmanen 		if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
8701580d3cdSKai Vehmanen 			I915_WRITE(AUD_PIN_BUF_CTL,
8711580d3cdSKai Vehmanen 				   (I915_READ(AUD_PIN_BUF_CTL) |
8721580d3cdSKai Vehmanen 				    AUD_PIN_BUF_ENABLE));
87387c16945SKai Vehmanen 	}
874df0566a6SJani Nikula 
875df0566a6SJani Nikula 	return ret;
876df0566a6SJani Nikula }
877df0566a6SJani Nikula 
878df0566a6SJani Nikula static void i915_audio_component_put_power(struct device *kdev,
879df0566a6SJani Nikula 					   unsigned long cookie)
880df0566a6SJani Nikula {
881df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
882df0566a6SJani Nikula 
883df0566a6SJani Nikula 	/* Stop forcing CDCLK to 2*BCLK if no need for audio to be powered. */
884df0566a6SJani Nikula 	if (--dev_priv->audio_power_refcount == 0)
8851ee48a61SKai Vehmanen 		if (IS_GEMINILAKE(dev_priv))
886df0566a6SJani Nikula 			glk_force_audio_cdclk(dev_priv, false);
887df0566a6SJani Nikula 
888df0566a6SJani Nikula 	intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO, cookie);
889df0566a6SJani Nikula }
890df0566a6SJani Nikula 
891df0566a6SJani Nikula static void i915_audio_component_codec_wake_override(struct device *kdev,
892df0566a6SJani Nikula 						     bool enable)
893df0566a6SJani Nikula {
894df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
895df0566a6SJani Nikula 	unsigned long cookie;
896df0566a6SJani Nikula 	u32 tmp;
897df0566a6SJani Nikula 
898df0566a6SJani Nikula 	if (!IS_GEN(dev_priv, 9))
899df0566a6SJani Nikula 		return;
900df0566a6SJani Nikula 
901df0566a6SJani Nikula 	cookie = i915_audio_component_get_power(kdev);
902df0566a6SJani Nikula 
903df0566a6SJani Nikula 	/*
904df0566a6SJani Nikula 	 * Enable/disable generating the codec wake signal, overriding the
905df0566a6SJani Nikula 	 * internal logic to generate the codec wake to controller.
906df0566a6SJani Nikula 	 */
907df0566a6SJani Nikula 	tmp = I915_READ(HSW_AUD_CHICKENBIT);
908df0566a6SJani Nikula 	tmp &= ~SKL_AUD_CODEC_WAKE_SIGNAL;
909df0566a6SJani Nikula 	I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
910df0566a6SJani Nikula 	usleep_range(1000, 1500);
911df0566a6SJani Nikula 
912df0566a6SJani Nikula 	if (enable) {
913df0566a6SJani Nikula 		tmp = I915_READ(HSW_AUD_CHICKENBIT);
914df0566a6SJani Nikula 		tmp |= SKL_AUD_CODEC_WAKE_SIGNAL;
915df0566a6SJani Nikula 		I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
916df0566a6SJani Nikula 		usleep_range(1000, 1500);
917df0566a6SJani Nikula 	}
918df0566a6SJani Nikula 
919df0566a6SJani Nikula 	i915_audio_component_put_power(kdev, cookie);
920df0566a6SJani Nikula }
921df0566a6SJani Nikula 
922df0566a6SJani Nikula /* Get CDCLK in kHz  */
923df0566a6SJani Nikula static int i915_audio_component_get_cdclk_freq(struct device *kdev)
924df0566a6SJani Nikula {
925df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
926df0566a6SJani Nikula 
927df0566a6SJani Nikula 	if (WARN_ON_ONCE(!HAS_DDI(dev_priv)))
928df0566a6SJani Nikula 		return -ENODEV;
929df0566a6SJani Nikula 
930df0566a6SJani Nikula 	return dev_priv->cdclk.hw.cdclk;
931df0566a6SJani Nikula }
932df0566a6SJani Nikula 
933df0566a6SJani Nikula /*
934df0566a6SJani Nikula  * get the intel_encoder according to the parameter port and pipe
935df0566a6SJani Nikula  * intel_encoder is saved by the index of pipe
936df0566a6SJani Nikula  * MST & (pipe >= 0): return the av_enc_map[pipe],
937df0566a6SJani Nikula  *   when port is matched
938df0566a6SJani Nikula  * MST & (pipe < 0): this is invalid
939df0566a6SJani Nikula  * Non-MST & (pipe >= 0): only pipe = 0 (the first device entry)
940df0566a6SJani Nikula  *   will get the right intel_encoder with port matched
941df0566a6SJani Nikula  * Non-MST & (pipe < 0): get the right intel_encoder with port matched
942df0566a6SJani Nikula  */
943df0566a6SJani Nikula static struct intel_encoder *get_saved_enc(struct drm_i915_private *dev_priv,
944df0566a6SJani Nikula 					       int port, int pipe)
945df0566a6SJani Nikula {
946df0566a6SJani Nikula 	struct intel_encoder *encoder;
947df0566a6SJani Nikula 
948df0566a6SJani Nikula 	/* MST */
949df0566a6SJani Nikula 	if (pipe >= 0) {
950df0566a6SJani Nikula 		if (WARN_ON(pipe >= ARRAY_SIZE(dev_priv->av_enc_map)))
951df0566a6SJani Nikula 			return NULL;
952df0566a6SJani Nikula 
953df0566a6SJani Nikula 		encoder = dev_priv->av_enc_map[pipe];
954df0566a6SJani Nikula 		/*
955df0566a6SJani Nikula 		 * when bootup, audio driver may not know it is
956df0566a6SJani Nikula 		 * MST or not. So it will poll all the port & pipe
957df0566a6SJani Nikula 		 * combinations
958df0566a6SJani Nikula 		 */
959df0566a6SJani Nikula 		if (encoder != NULL && encoder->port == port &&
960df0566a6SJani Nikula 		    encoder->type == INTEL_OUTPUT_DP_MST)
961df0566a6SJani Nikula 			return encoder;
962df0566a6SJani Nikula 	}
963df0566a6SJani Nikula 
964df0566a6SJani Nikula 	/* Non-MST */
965df0566a6SJani Nikula 	if (pipe > 0)
966df0566a6SJani Nikula 		return NULL;
967df0566a6SJani Nikula 
968df0566a6SJani Nikula 	for_each_pipe(dev_priv, pipe) {
969df0566a6SJani Nikula 		encoder = dev_priv->av_enc_map[pipe];
970df0566a6SJani Nikula 		if (encoder == NULL)
971df0566a6SJani Nikula 			continue;
972df0566a6SJani Nikula 
973df0566a6SJani Nikula 		if (encoder->type == INTEL_OUTPUT_DP_MST)
974df0566a6SJani Nikula 			continue;
975df0566a6SJani Nikula 
976df0566a6SJani Nikula 		if (port == encoder->port)
977df0566a6SJani Nikula 			return encoder;
978df0566a6SJani Nikula 	}
979df0566a6SJani Nikula 
980df0566a6SJani Nikula 	return NULL;
981df0566a6SJani Nikula }
982df0566a6SJani Nikula 
983df0566a6SJani Nikula static int i915_audio_component_sync_audio_rate(struct device *kdev, int port,
984df0566a6SJani Nikula 						int pipe, int rate)
985df0566a6SJani Nikula {
986df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
987df0566a6SJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio_component;
988df0566a6SJani Nikula 	struct intel_encoder *encoder;
989df0566a6SJani Nikula 	struct intel_crtc *crtc;
990df0566a6SJani Nikula 	unsigned long cookie;
991df0566a6SJani Nikula 	int err = 0;
992df0566a6SJani Nikula 
993df0566a6SJani Nikula 	if (!HAS_DDI(dev_priv))
994df0566a6SJani Nikula 		return 0;
995df0566a6SJani Nikula 
996df0566a6SJani Nikula 	cookie = i915_audio_component_get_power(kdev);
997df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
998df0566a6SJani Nikula 
999df0566a6SJani Nikula 	/* 1. get the pipe */
1000df0566a6SJani Nikula 	encoder = get_saved_enc(dev_priv, port, pipe);
1001df0566a6SJani Nikula 	if (!encoder || !encoder->base.crtc) {
1002*63855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "Not valid for port %c\n",
1003*63855149SWambui Karuga 			    port_name(port));
1004df0566a6SJani Nikula 		err = -ENODEV;
1005df0566a6SJani Nikula 		goto unlock;
1006df0566a6SJani Nikula 	}
1007df0566a6SJani Nikula 
1008df0566a6SJani Nikula 	crtc = to_intel_crtc(encoder->base.crtc);
1009df0566a6SJani Nikula 
1010df0566a6SJani Nikula 	/* port must be valid now, otherwise the pipe will be invalid */
1011df0566a6SJani Nikula 	acomp->aud_sample_rate[port] = rate;
1012df0566a6SJani Nikula 
1013df0566a6SJani Nikula 	hsw_audio_config_update(encoder, crtc->config);
1014df0566a6SJani Nikula 
1015df0566a6SJani Nikula  unlock:
1016df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
1017df0566a6SJani Nikula 	i915_audio_component_put_power(kdev, cookie);
1018df0566a6SJani Nikula 	return err;
1019df0566a6SJani Nikula }
1020df0566a6SJani Nikula 
1021df0566a6SJani Nikula static int i915_audio_component_get_eld(struct device *kdev, int port,
1022df0566a6SJani Nikula 					int pipe, bool *enabled,
1023df0566a6SJani Nikula 					unsigned char *buf, int max_bytes)
1024df0566a6SJani Nikula {
1025df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1026df0566a6SJani Nikula 	struct intel_encoder *intel_encoder;
1027df0566a6SJani Nikula 	const u8 *eld;
1028df0566a6SJani Nikula 	int ret = -EINVAL;
1029df0566a6SJani Nikula 
1030df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
1031df0566a6SJani Nikula 
1032df0566a6SJani Nikula 	intel_encoder = get_saved_enc(dev_priv, port, pipe);
1033df0566a6SJani Nikula 	if (!intel_encoder) {
1034*63855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "Not valid for port %c\n",
1035*63855149SWambui Karuga 			    port_name(port));
1036df0566a6SJani Nikula 		mutex_unlock(&dev_priv->av_mutex);
1037df0566a6SJani Nikula 		return ret;
1038df0566a6SJani Nikula 	}
1039df0566a6SJani Nikula 
1040df0566a6SJani Nikula 	ret = 0;
1041df0566a6SJani Nikula 	*enabled = intel_encoder->audio_connector != NULL;
1042df0566a6SJani Nikula 	if (*enabled) {
1043df0566a6SJani Nikula 		eld = intel_encoder->audio_connector->eld;
1044df0566a6SJani Nikula 		ret = drm_eld_size(eld);
1045df0566a6SJani Nikula 		memcpy(buf, eld, min(max_bytes, ret));
1046df0566a6SJani Nikula 	}
1047df0566a6SJani Nikula 
1048df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
1049df0566a6SJani Nikula 	return ret;
1050df0566a6SJani Nikula }
1051df0566a6SJani Nikula 
1052df0566a6SJani Nikula static const struct drm_audio_component_ops i915_audio_component_ops = {
1053df0566a6SJani Nikula 	.owner		= THIS_MODULE,
1054df0566a6SJani Nikula 	.get_power	= i915_audio_component_get_power,
1055df0566a6SJani Nikula 	.put_power	= i915_audio_component_put_power,
1056df0566a6SJani Nikula 	.codec_wake_override = i915_audio_component_codec_wake_override,
1057df0566a6SJani Nikula 	.get_cdclk_freq	= i915_audio_component_get_cdclk_freq,
1058df0566a6SJani Nikula 	.sync_audio_rate = i915_audio_component_sync_audio_rate,
1059df0566a6SJani Nikula 	.get_eld	= i915_audio_component_get_eld,
1060df0566a6SJani Nikula };
1061df0566a6SJani Nikula 
1062df0566a6SJani Nikula static int i915_audio_component_bind(struct device *i915_kdev,
1063df0566a6SJani Nikula 				     struct device *hda_kdev, void *data)
1064df0566a6SJani Nikula {
1065df0566a6SJani Nikula 	struct i915_audio_component *acomp = data;
1066df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
1067df0566a6SJani Nikula 	int i;
1068df0566a6SJani Nikula 
1069df0566a6SJani Nikula 	if (WARN_ON(acomp->base.ops || acomp->base.dev))
1070df0566a6SJani Nikula 		return -EEXIST;
1071df0566a6SJani Nikula 
1072df0566a6SJani Nikula 	if (WARN_ON(!device_link_add(hda_kdev, i915_kdev, DL_FLAG_STATELESS)))
1073df0566a6SJani Nikula 		return -ENOMEM;
1074df0566a6SJani Nikula 
1075df0566a6SJani Nikula 	drm_modeset_lock_all(&dev_priv->drm);
1076df0566a6SJani Nikula 	acomp->base.ops = &i915_audio_component_ops;
1077df0566a6SJani Nikula 	acomp->base.dev = i915_kdev;
1078df0566a6SJani Nikula 	BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
1079df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
1080df0566a6SJani Nikula 		acomp->aud_sample_rate[i] = 0;
1081df0566a6SJani Nikula 	dev_priv->audio_component = acomp;
1082df0566a6SJani Nikula 	drm_modeset_unlock_all(&dev_priv->drm);
1083df0566a6SJani Nikula 
1084df0566a6SJani Nikula 	return 0;
1085df0566a6SJani Nikula }
1086df0566a6SJani Nikula 
1087df0566a6SJani Nikula static void i915_audio_component_unbind(struct device *i915_kdev,
1088df0566a6SJani Nikula 					struct device *hda_kdev, void *data)
1089df0566a6SJani Nikula {
1090df0566a6SJani Nikula 	struct i915_audio_component *acomp = data;
1091df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
1092df0566a6SJani Nikula 
1093df0566a6SJani Nikula 	drm_modeset_lock_all(&dev_priv->drm);
1094df0566a6SJani Nikula 	acomp->base.ops = NULL;
1095df0566a6SJani Nikula 	acomp->base.dev = NULL;
1096df0566a6SJani Nikula 	dev_priv->audio_component = NULL;
1097df0566a6SJani Nikula 	drm_modeset_unlock_all(&dev_priv->drm);
1098df0566a6SJani Nikula 
1099df0566a6SJani Nikula 	device_link_remove(hda_kdev, i915_kdev);
1100df0566a6SJani Nikula }
1101df0566a6SJani Nikula 
1102df0566a6SJani Nikula static const struct component_ops i915_audio_component_bind_ops = {
1103df0566a6SJani Nikula 	.bind	= i915_audio_component_bind,
1104df0566a6SJani Nikula 	.unbind	= i915_audio_component_unbind,
1105df0566a6SJani Nikula };
1106df0566a6SJani Nikula 
1107df0566a6SJani Nikula /**
1108df0566a6SJani Nikula  * i915_audio_component_init - initialize and register the audio component
1109df0566a6SJani Nikula  * @dev_priv: i915 device instance
1110df0566a6SJani Nikula  *
1111df0566a6SJani Nikula  * This will register with the component framework a child component which
1112df0566a6SJani Nikula  * will bind dynamically to the snd_hda_intel driver's corresponding master
1113df0566a6SJani Nikula  * component when the latter is registered. During binding the child
1114df0566a6SJani Nikula  * initializes an instance of struct i915_audio_component which it receives
1115df0566a6SJani Nikula  * from the master. The master can then start to use the interface defined by
1116df0566a6SJani Nikula  * this struct. Each side can break the binding at any point by deregistering
1117df0566a6SJani Nikula  * its own component after which each side's component unbind callback is
1118df0566a6SJani Nikula  * called.
1119df0566a6SJani Nikula  *
1120df0566a6SJani Nikula  * We ignore any error during registration and continue with reduced
1121df0566a6SJani Nikula  * functionality (i.e. without HDMI audio).
1122df0566a6SJani Nikula  */
1123df0566a6SJani Nikula static void i915_audio_component_init(struct drm_i915_private *dev_priv)
1124df0566a6SJani Nikula {
1125df0566a6SJani Nikula 	int ret;
1126df0566a6SJani Nikula 
1127df0566a6SJani Nikula 	ret = component_add_typed(dev_priv->drm.dev,
1128df0566a6SJani Nikula 				  &i915_audio_component_bind_ops,
1129df0566a6SJani Nikula 				  I915_COMPONENT_AUDIO);
1130df0566a6SJani Nikula 	if (ret < 0) {
1131*63855149SWambui Karuga 		drm_err(&dev_priv->drm,
1132*63855149SWambui Karuga 			"failed to add audio component (%d)\n", ret);
1133df0566a6SJani Nikula 		/* continue with reduced functionality */
1134df0566a6SJani Nikula 		return;
1135df0566a6SJani Nikula 	}
1136df0566a6SJani Nikula 
113787c16945SKai Vehmanen 	if (IS_TIGERLAKE(dev_priv) || IS_ICELAKE(dev_priv)) {
113887c16945SKai Vehmanen 		dev_priv->audio_freq_cntrl = I915_READ(AUD_FREQ_CNTRL);
1139*63855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
1140*63855149SWambui Karuga 			    "init value of AUD_FREQ_CNTRL of 0x%x\n",
114187c16945SKai Vehmanen 			    dev_priv->audio_freq_cntrl);
114287c16945SKai Vehmanen 	}
114387c16945SKai Vehmanen 
1144df0566a6SJani Nikula 	dev_priv->audio_component_registered = true;
1145df0566a6SJani Nikula }
1146df0566a6SJani Nikula 
1147df0566a6SJani Nikula /**
1148df0566a6SJani Nikula  * i915_audio_component_cleanup - deregister the audio component
1149df0566a6SJani Nikula  * @dev_priv: i915 device instance
1150df0566a6SJani Nikula  *
1151df0566a6SJani Nikula  * Deregisters the audio component, breaking any existing binding to the
1152df0566a6SJani Nikula  * corresponding snd_hda_intel driver's master component.
1153df0566a6SJani Nikula  */
1154df0566a6SJani Nikula static void i915_audio_component_cleanup(struct drm_i915_private *dev_priv)
1155df0566a6SJani Nikula {
1156df0566a6SJani Nikula 	if (!dev_priv->audio_component_registered)
1157df0566a6SJani Nikula 		return;
1158df0566a6SJani Nikula 
1159df0566a6SJani Nikula 	component_del(dev_priv->drm.dev, &i915_audio_component_bind_ops);
1160df0566a6SJani Nikula 	dev_priv->audio_component_registered = false;
1161df0566a6SJani Nikula }
1162df0566a6SJani Nikula 
1163df0566a6SJani Nikula /**
1164df0566a6SJani Nikula  * intel_audio_init() - Initialize the audio driver either using
1165df0566a6SJani Nikula  * component framework or using lpe audio bridge
1166df0566a6SJani Nikula  * @dev_priv: the i915 drm device private data
1167df0566a6SJani Nikula  *
1168df0566a6SJani Nikula  */
1169df0566a6SJani Nikula void intel_audio_init(struct drm_i915_private *dev_priv)
1170df0566a6SJani Nikula {
1171df0566a6SJani Nikula 	if (intel_lpe_audio_init(dev_priv) < 0)
1172df0566a6SJani Nikula 		i915_audio_component_init(dev_priv);
1173df0566a6SJani Nikula }
1174df0566a6SJani Nikula 
1175df0566a6SJani Nikula /**
1176df0566a6SJani Nikula  * intel_audio_deinit() - deinitialize the audio driver
1177df0566a6SJani Nikula  * @dev_priv: the i915 drm device private data
1178df0566a6SJani Nikula  *
1179df0566a6SJani Nikula  */
1180df0566a6SJani Nikula void intel_audio_deinit(struct drm_i915_private *dev_priv)
1181df0566a6SJani Nikula {
1182df0566a6SJani Nikula 	if ((dev_priv)->lpe_audio.platdev != NULL)
1183df0566a6SJani Nikula 		intel_lpe_audio_teardown(dev_priv);
1184df0566a6SJani Nikula 	else
1185df0566a6SJani Nikula 		i915_audio_component_cleanup(dev_priv);
1186df0566a6SJani Nikula }
1187