xref: /linux/drivers/gpu/drm/i915/display/intel_audio.c (revision eae3da27ba84a95ade67b03318d7f1f003bd376a)
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 },
152df0566a6SJani Nikula };
153df0566a6SJani Nikula 
154df0566a6SJani Nikula /* HDMI N/CTS table */
155df0566a6SJani Nikula #define TMDS_297M 297000
156df0566a6SJani Nikula #define TMDS_296M 296703
157df0566a6SJani Nikula #define TMDS_594M 594000
158df0566a6SJani Nikula #define TMDS_593M 593407
159df0566a6SJani Nikula 
1602c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_24bpp[] = {
161df0566a6SJani Nikula 	{ 32000, TMDS_296M, 5824, 421875 },
162df0566a6SJani Nikula 	{ 32000, TMDS_297M, 3072, 222750 },
163df0566a6SJani Nikula 	{ 32000, TMDS_593M, 5824, 843750 },
164df0566a6SJani Nikula 	{ 32000, TMDS_594M, 3072, 445500 },
165df0566a6SJani Nikula 	{ 44100, TMDS_296M, 4459, 234375 },
166df0566a6SJani Nikula 	{ 44100, TMDS_297M, 4704, 247500 },
167df0566a6SJani Nikula 	{ 44100, TMDS_593M, 8918, 937500 },
168df0566a6SJani Nikula 	{ 44100, TMDS_594M, 9408, 990000 },
169df0566a6SJani Nikula 	{ 88200, TMDS_296M, 8918, 234375 },
170df0566a6SJani Nikula 	{ 88200, TMDS_297M, 9408, 247500 },
171df0566a6SJani Nikula 	{ 88200, TMDS_593M, 17836, 937500 },
172df0566a6SJani Nikula 	{ 88200, TMDS_594M, 18816, 990000 },
173df0566a6SJani Nikula 	{ 176400, TMDS_296M, 17836, 234375 },
174df0566a6SJani Nikula 	{ 176400, TMDS_297M, 18816, 247500 },
175df0566a6SJani Nikula 	{ 176400, TMDS_593M, 35672, 937500 },
176df0566a6SJani Nikula 	{ 176400, TMDS_594M, 37632, 990000 },
177df0566a6SJani Nikula 	{ 48000, TMDS_296M, 5824, 281250 },
178df0566a6SJani Nikula 	{ 48000, TMDS_297M, 5120, 247500 },
179df0566a6SJani Nikula 	{ 48000, TMDS_593M, 5824, 562500 },
180df0566a6SJani Nikula 	{ 48000, TMDS_594M, 6144, 594000 },
181df0566a6SJani Nikula 	{ 96000, TMDS_296M, 11648, 281250 },
182df0566a6SJani Nikula 	{ 96000, TMDS_297M, 10240, 247500 },
183df0566a6SJani Nikula 	{ 96000, TMDS_593M, 11648, 562500 },
184df0566a6SJani Nikula 	{ 96000, TMDS_594M, 12288, 594000 },
185df0566a6SJani Nikula 	{ 192000, TMDS_296M, 23296, 281250 },
186df0566a6SJani Nikula 	{ 192000, TMDS_297M, 20480, 247500 },
187df0566a6SJani Nikula 	{ 192000, TMDS_593M, 23296, 562500 },
188df0566a6SJani Nikula 	{ 192000, TMDS_594M, 24576, 594000 },
189df0566a6SJani Nikula };
190df0566a6SJani Nikula 
1912c291417SAditya Swarup /* Appendix C - N & CTS values for deep color from HDMI 2.0 spec*/
1922c291417SAditya Swarup /* HDMI N/CTS table for 10 bit deep color(30 bpp)*/
1932c291417SAditya Swarup #define TMDS_371M 371250
1942c291417SAditya Swarup #define TMDS_370M 370878
1952c291417SAditya Swarup 
1962c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_30bpp[] = {
1972c291417SAditya Swarup 	{ 32000, TMDS_370M, 5824, 527344 },
1982c291417SAditya Swarup 	{ 32000, TMDS_371M, 6144, 556875 },
1992c291417SAditya Swarup 	{ 44100, TMDS_370M, 8918, 585938 },
2002c291417SAditya Swarup 	{ 44100, TMDS_371M, 4704, 309375 },
2012c291417SAditya Swarup 	{ 88200, TMDS_370M, 17836, 585938 },
2022c291417SAditya Swarup 	{ 88200, TMDS_371M, 9408, 309375 },
2032c291417SAditya Swarup 	{ 176400, TMDS_370M, 35672, 585938 },
2042c291417SAditya Swarup 	{ 176400, TMDS_371M, 18816, 309375 },
2052c291417SAditya Swarup 	{ 48000, TMDS_370M, 11648, 703125 },
2062c291417SAditya Swarup 	{ 48000, TMDS_371M, 5120, 309375 },
2072c291417SAditya Swarup 	{ 96000, TMDS_370M, 23296, 703125 },
2082c291417SAditya Swarup 	{ 96000, TMDS_371M, 10240, 309375 },
2092c291417SAditya Swarup 	{ 192000, TMDS_370M, 46592, 703125 },
2102c291417SAditya Swarup 	{ 192000, TMDS_371M, 20480, 309375 },
2112c291417SAditya Swarup };
2122c291417SAditya Swarup 
2132c291417SAditya Swarup /* HDMI N/CTS table for 12 bit deep color(36 bpp)*/
2142c291417SAditya Swarup #define TMDS_445_5M 445500
2152c291417SAditya Swarup #define TMDS_445M 445054
2162c291417SAditya Swarup 
2172c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_36bpp[] = {
2182c291417SAditya Swarup 	{ 32000, TMDS_445M, 5824, 632813 },
2192c291417SAditya Swarup 	{ 32000, TMDS_445_5M, 4096, 445500 },
2202c291417SAditya Swarup 	{ 44100, TMDS_445M, 8918, 703125 },
2212c291417SAditya Swarup 	{ 44100, TMDS_445_5M, 4704, 371250 },
2222c291417SAditya Swarup 	{ 88200, TMDS_445M, 17836, 703125 },
2232c291417SAditya Swarup 	{ 88200, TMDS_445_5M, 9408, 371250 },
2242c291417SAditya Swarup 	{ 176400, TMDS_445M, 35672, 703125 },
2252c291417SAditya Swarup 	{ 176400, TMDS_445_5M, 18816, 371250 },
2262c291417SAditya Swarup 	{ 48000, TMDS_445M, 5824, 421875 },
2272c291417SAditya Swarup 	{ 48000, TMDS_445_5M, 5120, 371250 },
2282c291417SAditya Swarup 	{ 96000, TMDS_445M, 11648, 421875 },
2292c291417SAditya Swarup 	{ 96000, TMDS_445_5M, 10240, 371250 },
2302c291417SAditya Swarup 	{ 192000, TMDS_445M, 23296, 421875 },
2312c291417SAditya Swarup 	{ 192000, TMDS_445_5M, 20480, 371250 },
2322c291417SAditya Swarup };
2332c291417SAditya Swarup 
234df0566a6SJani Nikula /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
235df0566a6SJani Nikula static u32 audio_config_hdmi_pixel_clock(const struct intel_crtc_state *crtc_state)
236df0566a6SJani Nikula {
237df0566a6SJani Nikula 	const struct drm_display_mode *adjusted_mode =
2381326a92cSMaarten Lankhorst 		&crtc_state->hw.adjusted_mode;
239df0566a6SJani Nikula 	int i;
240df0566a6SJani Nikula 
241df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
242df0566a6SJani Nikula 		if (adjusted_mode->crtc_clock == hdmi_audio_clock[i].clock)
243df0566a6SJani Nikula 			break;
244df0566a6SJani Nikula 	}
245df0566a6SJani Nikula 
246df0566a6SJani Nikula 	if (i == ARRAY_SIZE(hdmi_audio_clock)) {
247df0566a6SJani Nikula 		DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n",
248df0566a6SJani Nikula 			      adjusted_mode->crtc_clock);
249df0566a6SJani Nikula 		i = 1;
250df0566a6SJani Nikula 	}
251df0566a6SJani Nikula 
252df0566a6SJani Nikula 	DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
253df0566a6SJani Nikula 		      hdmi_audio_clock[i].clock,
254df0566a6SJani Nikula 		      hdmi_audio_clock[i].config);
255df0566a6SJani Nikula 
256df0566a6SJani Nikula 	return hdmi_audio_clock[i].config;
257df0566a6SJani Nikula }
258df0566a6SJani Nikula 
259df0566a6SJani Nikula static int audio_config_hdmi_get_n(const struct intel_crtc_state *crtc_state,
260df0566a6SJani Nikula 				   int rate)
261df0566a6SJani Nikula {
2622c291417SAditya Swarup 	const struct hdmi_aud_ncts *hdmi_ncts_table;
2632c291417SAditya Swarup 	int i, size;
264df0566a6SJani Nikula 
2652c291417SAditya Swarup 	if (crtc_state->pipe_bpp == 36) {
2662c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_36bpp;
2672c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_36bpp);
2682c291417SAditya Swarup 	} else if (crtc_state->pipe_bpp == 30) {
2692c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_30bpp;
2702c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_30bpp);
2712c291417SAditya Swarup 	} else {
2722c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_24bpp;
2732c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_24bpp);
2742c291417SAditya Swarup 	}
2752c291417SAditya Swarup 
2762c291417SAditya Swarup 	for (i = 0; i < size; i++) {
2772c291417SAditya Swarup 		if (rate == hdmi_ncts_table[i].sample_rate &&
2782c291417SAditya Swarup 		    crtc_state->port_clock == hdmi_ncts_table[i].clock) {
2792c291417SAditya Swarup 			return hdmi_ncts_table[i].n;
280df0566a6SJani Nikula 		}
281df0566a6SJani Nikula 	}
282df0566a6SJani Nikula 	return 0;
283df0566a6SJani Nikula }
284df0566a6SJani Nikula 
285df0566a6SJani Nikula static bool intel_eld_uptodate(struct drm_connector *connector,
286df0566a6SJani Nikula 			       i915_reg_t reg_eldv, u32 bits_eldv,
287df0566a6SJani Nikula 			       i915_reg_t reg_elda, u32 bits_elda,
288df0566a6SJani Nikula 			       i915_reg_t reg_edid)
289df0566a6SJani Nikula {
290df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
291df0566a6SJani Nikula 	const u8 *eld = connector->eld;
292df0566a6SJani Nikula 	u32 tmp;
293df0566a6SJani Nikula 	int i;
294df0566a6SJani Nikula 
29549e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, reg_eldv);
296df0566a6SJani Nikula 	tmp &= bits_eldv;
297df0566a6SJani Nikula 
298df0566a6SJani Nikula 	if (!tmp)
299df0566a6SJani Nikula 		return false;
300df0566a6SJani Nikula 
30149e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, reg_elda);
302df0566a6SJani Nikula 	tmp &= ~bits_elda;
30349e659bcSJani Nikula 	intel_de_write(dev_priv, reg_elda, tmp);
304df0566a6SJani Nikula 
305df0566a6SJani Nikula 	for (i = 0; i < drm_eld_size(eld) / 4; i++)
30649e659bcSJani Nikula 		if (intel_de_read(dev_priv, reg_edid) != *((const u32 *)eld + i))
307df0566a6SJani Nikula 			return false;
308df0566a6SJani Nikula 
309df0566a6SJani Nikula 	return true;
310df0566a6SJani Nikula }
311df0566a6SJani Nikula 
312df0566a6SJani Nikula static void g4x_audio_codec_disable(struct intel_encoder *encoder,
313df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
314df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
315df0566a6SJani Nikula {
316df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
317df0566a6SJani Nikula 	u32 eldv, tmp;
318df0566a6SJani Nikula 
31963855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "Disable audio codec\n");
320df0566a6SJani Nikula 
32149e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_VID_DID);
322df0566a6SJani Nikula 	if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
323df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCL_DEVBLC;
324df0566a6SJani Nikula 	else
325df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCTG;
326df0566a6SJani Nikula 
327df0566a6SJani Nikula 	/* Invalidate ELD */
32849e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_CNTL_ST);
329df0566a6SJani Nikula 	tmp &= ~eldv;
33049e659bcSJani Nikula 	intel_de_write(dev_priv, G4X_AUD_CNTL_ST, tmp);
331df0566a6SJani Nikula }
332df0566a6SJani Nikula 
333df0566a6SJani Nikula static void g4x_audio_codec_enable(struct intel_encoder *encoder,
334df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
335df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
336df0566a6SJani Nikula {
337df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
338df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
339df0566a6SJani Nikula 	const u8 *eld = connector->eld;
340df0566a6SJani Nikula 	u32 eldv;
341df0566a6SJani Nikula 	u32 tmp;
342df0566a6SJani Nikula 	int len, i;
343df0566a6SJani Nikula 
34463855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "Enable audio codec, %u bytes ELD\n",
34563855149SWambui Karuga 		    drm_eld_size(eld));
346df0566a6SJani Nikula 
34749e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_VID_DID);
348df0566a6SJani Nikula 	if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
349df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCL_DEVBLC;
350df0566a6SJani Nikula 	else
351df0566a6SJani Nikula 		eldv = G4X_ELDV_DEVCTG;
352df0566a6SJani Nikula 
353df0566a6SJani Nikula 	if (intel_eld_uptodate(connector,
354df0566a6SJani Nikula 			       G4X_AUD_CNTL_ST, eldv,
355df0566a6SJani Nikula 			       G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
356df0566a6SJani Nikula 			       G4X_HDMIW_HDMIEDID))
357df0566a6SJani Nikula 		return;
358df0566a6SJani Nikula 
35949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_CNTL_ST);
360df0566a6SJani Nikula 	tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
361df0566a6SJani Nikula 	len = (tmp >> 9) & 0x1f;		/* ELD buffer size */
36249e659bcSJani Nikula 	intel_de_write(dev_priv, G4X_AUD_CNTL_ST, tmp);
363df0566a6SJani Nikula 
364df0566a6SJani Nikula 	len = min(drm_eld_size(eld) / 4, len);
36563855149SWambui Karuga 	drm_dbg(&dev_priv->drm, "ELD size %d\n", len);
366df0566a6SJani Nikula 	for (i = 0; i < len; i++)
36749e659bcSJani Nikula 		intel_de_write(dev_priv, G4X_HDMIW_HDMIEDID,
36849e659bcSJani Nikula 			       *((const u32 *)eld + i));
369df0566a6SJani Nikula 
37049e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, G4X_AUD_CNTL_ST);
371df0566a6SJani Nikula 	tmp |= eldv;
37249e659bcSJani Nikula 	intel_de_write(dev_priv, G4X_AUD_CNTL_ST, tmp);
373df0566a6SJani Nikula }
374df0566a6SJani Nikula 
375df0566a6SJani Nikula static void
376df0566a6SJani Nikula hsw_dp_audio_config_update(struct intel_encoder *encoder,
377df0566a6SJani Nikula 			   const struct intel_crtc_state *crtc_state)
378df0566a6SJani Nikula {
379df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
380df0566a6SJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio_component;
381df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
382df0566a6SJani Nikula 	enum port port = encoder->port;
383df0566a6SJani Nikula 	const struct dp_aud_n_m *nm;
384df0566a6SJani Nikula 	int rate;
385df0566a6SJani Nikula 	u32 tmp;
386df0566a6SJani Nikula 
387df0566a6SJani Nikula 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
388df0566a6SJani Nikula 	nm = audio_config_dp_get_n_m(crtc_state, rate);
389df0566a6SJani Nikula 	if (nm)
39063855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using Maud %u, Naud %u\n", nm->m,
39163855149SWambui Karuga 			    nm->n);
392df0566a6SJani Nikula 	else
39363855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using automatic Maud, Naud\n");
394df0566a6SJani Nikula 
39549e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_CFG(cpu_transcoder));
396df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
397df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
398df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
399df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_VALUE_INDEX;
400df0566a6SJani Nikula 
401df0566a6SJani Nikula 	if (nm) {
402df0566a6SJani Nikula 		tmp &= ~AUD_CONFIG_N_MASK;
403df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N(nm->n);
404df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
405df0566a6SJani Nikula 	}
406df0566a6SJani Nikula 
40749e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_CFG(cpu_transcoder), tmp);
408df0566a6SJani Nikula 
40949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
410df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_M_MASK;
411df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
412df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
413df0566a6SJani Nikula 
414df0566a6SJani Nikula 	if (nm) {
415df0566a6SJani Nikula 		tmp |= nm->m;
416df0566a6SJani Nikula 		tmp |= AUD_M_CTS_M_VALUE_INDEX;
417df0566a6SJani Nikula 		tmp |= AUD_M_CTS_M_PROG_ENABLE;
418df0566a6SJani Nikula 	}
419df0566a6SJani Nikula 
42049e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
421df0566a6SJani Nikula }
422df0566a6SJani Nikula 
423df0566a6SJani Nikula static void
424df0566a6SJani Nikula hsw_hdmi_audio_config_update(struct intel_encoder *encoder,
425df0566a6SJani Nikula 			     const struct intel_crtc_state *crtc_state)
426df0566a6SJani Nikula {
427df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
428df0566a6SJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio_component;
429df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
430df0566a6SJani Nikula 	enum port port = encoder->port;
431df0566a6SJani Nikula 	int n, rate;
432df0566a6SJani Nikula 	u32 tmp;
433df0566a6SJani Nikula 
434df0566a6SJani Nikula 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
435df0566a6SJani Nikula 
43649e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_CFG(cpu_transcoder));
437df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
438df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
439df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
440df0566a6SJani Nikula 	tmp |= audio_config_hdmi_pixel_clock(crtc_state);
441df0566a6SJani Nikula 
442df0566a6SJani Nikula 	n = audio_config_hdmi_get_n(crtc_state, rate);
443df0566a6SJani Nikula 	if (n != 0) {
44463855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using N %d\n", n);
445df0566a6SJani Nikula 
446df0566a6SJani Nikula 		tmp &= ~AUD_CONFIG_N_MASK;
447df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N(n);
448df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
449df0566a6SJani Nikula 	} else {
45063855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "using automatic N\n");
451df0566a6SJani Nikula 	}
452df0566a6SJani Nikula 
45349e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_CFG(cpu_transcoder), tmp);
454df0566a6SJani Nikula 
455df0566a6SJani Nikula 	/*
456df0566a6SJani Nikula 	 * Let's disable "Enable CTS or M Prog bit"
457df0566a6SJani Nikula 	 * and let HW calculate the value
458df0566a6SJani Nikula 	 */
45949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
460df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
461df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
46249e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
463df0566a6SJani Nikula }
464df0566a6SJani Nikula 
465df0566a6SJani Nikula static void
466df0566a6SJani Nikula hsw_audio_config_update(struct intel_encoder *encoder,
467df0566a6SJani Nikula 			const struct intel_crtc_state *crtc_state)
468df0566a6SJani Nikula {
469df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state))
470df0566a6SJani Nikula 		hsw_dp_audio_config_update(encoder, crtc_state);
471df0566a6SJani Nikula 	else
472df0566a6SJani Nikula 		hsw_hdmi_audio_config_update(encoder, crtc_state);
473df0566a6SJani Nikula }
474df0566a6SJani Nikula 
475df0566a6SJani Nikula static void hsw_audio_codec_disable(struct intel_encoder *encoder,
476df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
477df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
478df0566a6SJani Nikula {
479df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
480df0566a6SJani Nikula 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
481df0566a6SJani Nikula 	u32 tmp;
482df0566a6SJani Nikula 
48363855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "Disable audio codec on transcoder %s\n",
484df0566a6SJani Nikula 		    transcoder_name(cpu_transcoder));
485df0566a6SJani Nikula 
486df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
487df0566a6SJani Nikula 
488df0566a6SJani Nikula 	/* Disable timestamps */
48949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_CFG(cpu_transcoder));
490df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
491df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_PROG_ENABLE;
492df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_UPPER_N_MASK;
493df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_LOWER_N_MASK;
494df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(old_crtc_state))
495df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
49649e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_CFG(cpu_transcoder), tmp);
497df0566a6SJani Nikula 
498df0566a6SJani Nikula 	/* Invalidate ELD */
49949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD);
500df0566a6SJani Nikula 	tmp &= ~AUDIO_ELD_VALID(cpu_transcoder);
501df0566a6SJani Nikula 	tmp &= ~AUDIO_OUTPUT_ENABLE(cpu_transcoder);
50249e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_PIN_ELD_CP_VLD, tmp);
503df0566a6SJani Nikula 
504df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
505df0566a6SJani Nikula }
506df0566a6SJani Nikula 
507df0566a6SJani Nikula static void hsw_audio_codec_enable(struct intel_encoder *encoder,
508df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
509df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
510df0566a6SJani Nikula {
511df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
512df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
513df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
514df0566a6SJani Nikula 	const u8 *eld = connector->eld;
515df0566a6SJani Nikula 	u32 tmp;
516df0566a6SJani Nikula 	int len, i;
517df0566a6SJani Nikula 
51863855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
51963855149SWambui Karuga 		    "Enable audio codec on transcoder %s, %u bytes ELD\n",
520df0566a6SJani Nikula 		     transcoder_name(cpu_transcoder), drm_eld_size(eld));
521df0566a6SJani Nikula 
522df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
523df0566a6SJani Nikula 
524df0566a6SJani Nikula 	/* Enable audio presence detect, invalidate ELD */
52549e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD);
526df0566a6SJani Nikula 	tmp |= AUDIO_OUTPUT_ENABLE(cpu_transcoder);
527df0566a6SJani Nikula 	tmp &= ~AUDIO_ELD_VALID(cpu_transcoder);
52849e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_PIN_ELD_CP_VLD, tmp);
529df0566a6SJani Nikula 
530df0566a6SJani Nikula 	/*
531df0566a6SJani Nikula 	 * FIXME: We're supposed to wait for vblank here, but we have vblanks
532df0566a6SJani Nikula 	 * disabled during the mode set. The proper fix would be to push the
533df0566a6SJani Nikula 	 * rest of the setup into a vblank work item, queued here, but the
534df0566a6SJani Nikula 	 * infrastructure is not there yet.
535df0566a6SJani Nikula 	 */
536df0566a6SJani Nikula 
537df0566a6SJani Nikula 	/* Reset ELD write address */
53849e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_DIP_ELD_CTRL(cpu_transcoder));
539df0566a6SJani Nikula 	tmp &= ~IBX_ELD_ADDRESS_MASK;
54049e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_DIP_ELD_CTRL(cpu_transcoder), tmp);
541df0566a6SJani Nikula 
542df0566a6SJani Nikula 	/* Up to 84 bytes of hw ELD buffer */
543df0566a6SJani Nikula 	len = min(drm_eld_size(eld), 84);
544df0566a6SJani Nikula 	for (i = 0; i < len / 4; i++)
54549e659bcSJani Nikula 		intel_de_write(dev_priv, HSW_AUD_EDID_DATA(cpu_transcoder),
54649e659bcSJani Nikula 			       *((const u32 *)eld + i));
547df0566a6SJani Nikula 
548df0566a6SJani Nikula 	/* ELD valid */
54949e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD);
550df0566a6SJani Nikula 	tmp |= AUDIO_ELD_VALID(cpu_transcoder);
55149e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_PIN_ELD_CP_VLD, tmp);
552df0566a6SJani Nikula 
553df0566a6SJani Nikula 	/* Enable timestamps */
554df0566a6SJani Nikula 	hsw_audio_config_update(encoder, crtc_state);
555df0566a6SJani Nikula 
556df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
557df0566a6SJani Nikula }
558df0566a6SJani Nikula 
559df0566a6SJani Nikula static void ilk_audio_codec_disable(struct intel_encoder *encoder,
560df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
561df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
562df0566a6SJani Nikula {
563df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
5642225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
565df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
566df0566a6SJani Nikula 	enum port port = encoder->port;
567df0566a6SJani Nikula 	u32 tmp, eldv;
568df0566a6SJani Nikula 	i915_reg_t aud_config, aud_cntrl_st2;
569df0566a6SJani Nikula 
57063855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
57163855149SWambui Karuga 		    "Disable audio codec on [ENCODER:%d:%s], pipe %c\n",
57266a990ddSVille Syrjälä 		     encoder->base.base.id, encoder->base.name,
57366a990ddSVille Syrjälä 		     pipe_name(pipe));
574df0566a6SJani Nikula 
5759a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, port == PORT_A))
576df0566a6SJani Nikula 		return;
577df0566a6SJani Nikula 
578df0566a6SJani Nikula 	if (HAS_PCH_IBX(dev_priv)) {
579df0566a6SJani Nikula 		aud_config = IBX_AUD_CFG(pipe);
580df0566a6SJani Nikula 		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
581df0566a6SJani Nikula 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
582df0566a6SJani Nikula 		aud_config = VLV_AUD_CFG(pipe);
583df0566a6SJani Nikula 		aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
584df0566a6SJani Nikula 	} else {
585df0566a6SJani Nikula 		aud_config = CPT_AUD_CFG(pipe);
586df0566a6SJani Nikula 		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
587df0566a6SJani Nikula 	}
588df0566a6SJani Nikula 
589df0566a6SJani Nikula 	/* Disable timestamps */
59049e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_config);
591df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
592df0566a6SJani Nikula 	tmp |= AUD_CONFIG_N_PROG_ENABLE;
593df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_UPPER_N_MASK;
594df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_LOWER_N_MASK;
595df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(old_crtc_state))
596df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
59749e659bcSJani Nikula 	intel_de_write(dev_priv, aud_config, tmp);
598df0566a6SJani Nikula 
599df0566a6SJani Nikula 	eldv = IBX_ELD_VALID(port);
600df0566a6SJani Nikula 
601df0566a6SJani Nikula 	/* Invalidate ELD */
60249e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_cntrl_st2);
603df0566a6SJani Nikula 	tmp &= ~eldv;
60449e659bcSJani Nikula 	intel_de_write(dev_priv, aud_cntrl_st2, tmp);
605df0566a6SJani Nikula }
606df0566a6SJani Nikula 
607df0566a6SJani Nikula static void ilk_audio_codec_enable(struct intel_encoder *encoder,
608df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
609df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
610df0566a6SJani Nikula {
611df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
6122225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
613df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
614df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
615df0566a6SJani Nikula 	enum port port = encoder->port;
616df0566a6SJani Nikula 	const u8 *eld = connector->eld;
617df0566a6SJani Nikula 	u32 tmp, eldv;
618df0566a6SJani Nikula 	int len, i;
619df0566a6SJani Nikula 	i915_reg_t hdmiw_hdmiedid, aud_config, aud_cntl_st, aud_cntrl_st2;
620df0566a6SJani Nikula 
62163855149SWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
62263855149SWambui Karuga 		    "Enable audio codec on [ENCODER:%d:%s], pipe %c, %u bytes ELD\n",
62366a990ddSVille Syrjälä 		    encoder->base.base.id, encoder->base.name,
62466a990ddSVille Syrjälä 		    pipe_name(pipe), drm_eld_size(eld));
625df0566a6SJani Nikula 
6269a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, port == PORT_A))
627df0566a6SJani Nikula 		return;
628df0566a6SJani Nikula 
629df0566a6SJani Nikula 	/*
630df0566a6SJani Nikula 	 * FIXME: We're supposed to wait for vblank here, but we have vblanks
631df0566a6SJani Nikula 	 * disabled during the mode set. The proper fix would be to push the
632df0566a6SJani Nikula 	 * rest of the setup into a vblank work item, queued here, but the
633df0566a6SJani Nikula 	 * infrastructure is not there yet.
634df0566a6SJani Nikula 	 */
635df0566a6SJani Nikula 
636df0566a6SJani Nikula 	if (HAS_PCH_IBX(dev_priv)) {
637df0566a6SJani Nikula 		hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
638df0566a6SJani Nikula 		aud_config = IBX_AUD_CFG(pipe);
639df0566a6SJani Nikula 		aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
640df0566a6SJani Nikula 		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
641df0566a6SJani Nikula 	} else if (IS_VALLEYVIEW(dev_priv) ||
642df0566a6SJani Nikula 		   IS_CHERRYVIEW(dev_priv)) {
643df0566a6SJani Nikula 		hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
644df0566a6SJani Nikula 		aud_config = VLV_AUD_CFG(pipe);
645df0566a6SJani Nikula 		aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
646df0566a6SJani Nikula 		aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
647df0566a6SJani Nikula 	} else {
648df0566a6SJani Nikula 		hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
649df0566a6SJani Nikula 		aud_config = CPT_AUD_CFG(pipe);
650df0566a6SJani Nikula 		aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
651df0566a6SJani Nikula 		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
652df0566a6SJani Nikula 	}
653df0566a6SJani Nikula 
654df0566a6SJani Nikula 	eldv = IBX_ELD_VALID(port);
655df0566a6SJani Nikula 
656df0566a6SJani Nikula 	/* Invalidate ELD */
65749e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_cntrl_st2);
658df0566a6SJani Nikula 	tmp &= ~eldv;
65949e659bcSJani Nikula 	intel_de_write(dev_priv, aud_cntrl_st2, tmp);
660df0566a6SJani Nikula 
661df0566a6SJani Nikula 	/* Reset ELD write address */
66249e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_cntl_st);
663df0566a6SJani Nikula 	tmp &= ~IBX_ELD_ADDRESS_MASK;
66449e659bcSJani Nikula 	intel_de_write(dev_priv, aud_cntl_st, tmp);
665df0566a6SJani Nikula 
666df0566a6SJani Nikula 	/* Up to 84 bytes of hw ELD buffer */
667df0566a6SJani Nikula 	len = min(drm_eld_size(eld), 84);
668df0566a6SJani Nikula 	for (i = 0; i < len / 4; i++)
66949e659bcSJani Nikula 		intel_de_write(dev_priv, hdmiw_hdmiedid,
67049e659bcSJani Nikula 			       *((const u32 *)eld + i));
671df0566a6SJani Nikula 
672df0566a6SJani Nikula 	/* ELD valid */
67349e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_cntrl_st2);
674df0566a6SJani Nikula 	tmp |= eldv;
67549e659bcSJani Nikula 	intel_de_write(dev_priv, aud_cntrl_st2, tmp);
676df0566a6SJani Nikula 
677df0566a6SJani Nikula 	/* Enable timestamps */
67849e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, aud_config);
679df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
680df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
681df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
682df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state))
683df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
684df0566a6SJani Nikula 	else
685df0566a6SJani Nikula 		tmp |= audio_config_hdmi_pixel_clock(crtc_state);
68649e659bcSJani Nikula 	intel_de_write(dev_priv, aud_config, tmp);
687df0566a6SJani Nikula }
688df0566a6SJani Nikula 
689df0566a6SJani Nikula /**
690df0566a6SJani Nikula  * intel_audio_codec_enable - Enable the audio codec for HD audio
691df0566a6SJani Nikula  * @encoder: encoder on which to enable audio
692df0566a6SJani Nikula  * @crtc_state: pointer to the current crtc state.
693df0566a6SJani Nikula  * @conn_state: pointer to the current connector state.
694df0566a6SJani Nikula  *
695df0566a6SJani Nikula  * The enable sequences may only be performed after enabling the transcoder and
696df0566a6SJani Nikula  * port, and after completed link training.
697df0566a6SJani Nikula  */
698df0566a6SJani Nikula void intel_audio_codec_enable(struct intel_encoder *encoder,
699df0566a6SJani Nikula 			      const struct intel_crtc_state *crtc_state,
700df0566a6SJani Nikula 			      const struct drm_connector_state *conn_state)
701df0566a6SJani Nikula {
702df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
703df0566a6SJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio_component;
7042225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
705df0566a6SJani Nikula 	struct drm_connector *connector = conn_state->connector;
706df0566a6SJani Nikula 	const struct drm_display_mode *adjusted_mode =
7071326a92cSMaarten Lankhorst 		&crtc_state->hw.adjusted_mode;
708df0566a6SJani Nikula 	enum port port = encoder->port;
709df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
710df0566a6SJani Nikula 
711df0566a6SJani Nikula 	/* FIXME precompute the ELD in .compute_config() */
712df0566a6SJani Nikula 	if (!connector->eld[0])
71363855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
71463855149SWambui Karuga 			    "Bogus ELD on [CONNECTOR:%d:%s]\n",
715df0566a6SJani Nikula 			    connector->base.id, connector->name);
716df0566a6SJani Nikula 
71763855149SWambui Karuga 	drm_dbg(&dev_priv->drm, "ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
718df0566a6SJani Nikula 		connector->base.id,
719df0566a6SJani Nikula 		connector->name,
72079003e61SVille Syrjälä 		encoder->base.base.id,
72179003e61SVille Syrjälä 		encoder->base.name);
722df0566a6SJani Nikula 
723df0566a6SJani Nikula 	connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
724df0566a6SJani Nikula 
725df0566a6SJani Nikula 	if (dev_priv->display.audio_codec_enable)
726df0566a6SJani Nikula 		dev_priv->display.audio_codec_enable(encoder,
727df0566a6SJani Nikula 						     crtc_state,
728df0566a6SJani Nikula 						     conn_state);
729df0566a6SJani Nikula 
730df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
731df0566a6SJani Nikula 	encoder->audio_connector = connector;
732df0566a6SJani Nikula 
733df0566a6SJani Nikula 	/* referred in audio callbacks */
734df0566a6SJani Nikula 	dev_priv->av_enc_map[pipe] = encoder;
735df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
736df0566a6SJani Nikula 
737df0566a6SJani Nikula 	if (acomp && acomp->base.audio_ops &&
738df0566a6SJani Nikula 	    acomp->base.audio_ops->pin_eld_notify) {
739df0566a6SJani Nikula 		/* audio drivers expect pipe = -1 to indicate Non-MST cases */
740df0566a6SJani Nikula 		if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
741df0566a6SJani Nikula 			pipe = -1;
742df0566a6SJani Nikula 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
743df0566a6SJani Nikula 						 (int) port, (int) pipe);
744df0566a6SJani Nikula 	}
745df0566a6SJani Nikula 
746df0566a6SJani Nikula 	intel_lpe_audio_notify(dev_priv, pipe, port, connector->eld,
747df0566a6SJani Nikula 			       crtc_state->port_clock,
748df0566a6SJani Nikula 			       intel_crtc_has_dp_encoder(crtc_state));
749df0566a6SJani Nikula }
750df0566a6SJani Nikula 
751df0566a6SJani Nikula /**
752df0566a6SJani Nikula  * intel_audio_codec_disable - Disable the audio codec for HD audio
753df0566a6SJani Nikula  * @encoder: encoder on which to disable audio
754df0566a6SJani Nikula  * @old_crtc_state: pointer to the old crtc state.
755df0566a6SJani Nikula  * @old_conn_state: pointer to the old connector state.
756df0566a6SJani Nikula  *
757df0566a6SJani Nikula  * The disable sequences must be performed before disabling the transcoder or
758df0566a6SJani Nikula  * port.
759df0566a6SJani Nikula  */
760df0566a6SJani Nikula void intel_audio_codec_disable(struct intel_encoder *encoder,
761df0566a6SJani Nikula 			       const struct intel_crtc_state *old_crtc_state,
762df0566a6SJani Nikula 			       const struct drm_connector_state *old_conn_state)
763df0566a6SJani Nikula {
764df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
765df0566a6SJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio_component;
7662225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
767df0566a6SJani Nikula 	enum port port = encoder->port;
768df0566a6SJani Nikula 	enum pipe pipe = crtc->pipe;
769df0566a6SJani Nikula 
770df0566a6SJani Nikula 	if (dev_priv->display.audio_codec_disable)
771df0566a6SJani Nikula 		dev_priv->display.audio_codec_disable(encoder,
772df0566a6SJani Nikula 						      old_crtc_state,
773df0566a6SJani Nikula 						      old_conn_state);
774df0566a6SJani Nikula 
775df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
776df0566a6SJani Nikula 	encoder->audio_connector = NULL;
777df0566a6SJani Nikula 	dev_priv->av_enc_map[pipe] = NULL;
778df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
779df0566a6SJani Nikula 
780df0566a6SJani Nikula 	if (acomp && acomp->base.audio_ops &&
781df0566a6SJani Nikula 	    acomp->base.audio_ops->pin_eld_notify) {
782df0566a6SJani Nikula 		/* audio drivers expect pipe = -1 to indicate Non-MST cases */
783df0566a6SJani Nikula 		if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
784df0566a6SJani Nikula 			pipe = -1;
785df0566a6SJani Nikula 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
786df0566a6SJani Nikula 						 (int) port, (int) pipe);
787df0566a6SJani Nikula 	}
788df0566a6SJani Nikula 
789df0566a6SJani Nikula 	intel_lpe_audio_notify(dev_priv, pipe, port, NULL, 0, false);
790df0566a6SJani Nikula }
791df0566a6SJani Nikula 
792df0566a6SJani Nikula /**
793df0566a6SJani Nikula  * intel_init_audio_hooks - Set up chip specific audio hooks
794df0566a6SJani Nikula  * @dev_priv: device private
795df0566a6SJani Nikula  */
796df0566a6SJani Nikula void intel_init_audio_hooks(struct drm_i915_private *dev_priv)
797df0566a6SJani Nikula {
798df0566a6SJani Nikula 	if (IS_G4X(dev_priv)) {
799df0566a6SJani Nikula 		dev_priv->display.audio_codec_enable = g4x_audio_codec_enable;
800df0566a6SJani Nikula 		dev_priv->display.audio_codec_disable = g4x_audio_codec_disable;
801df0566a6SJani Nikula 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
802df0566a6SJani Nikula 		dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
803df0566a6SJani Nikula 		dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
804df0566a6SJani Nikula 	} else if (IS_HASWELL(dev_priv) || INTEL_GEN(dev_priv) >= 8) {
805df0566a6SJani Nikula 		dev_priv->display.audio_codec_enable = hsw_audio_codec_enable;
806df0566a6SJani Nikula 		dev_priv->display.audio_codec_disable = hsw_audio_codec_disable;
807df0566a6SJani Nikula 	} else if (HAS_PCH_SPLIT(dev_priv)) {
808df0566a6SJani Nikula 		dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
809df0566a6SJani Nikula 		dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
810df0566a6SJani Nikula 	}
811df0566a6SJani Nikula }
812df0566a6SJani Nikula 
81328a30b45SVille Syrjälä static int glk_force_audio_cdclk_commit(struct intel_atomic_state *state,
81408e3ed3aSChris Wilson 					struct intel_crtc *crtc,
81528a30b45SVille Syrjälä 					bool enable)
81628a30b45SVille Syrjälä {
81728a30b45SVille Syrjälä 	struct intel_cdclk_state *cdclk_state;
81828a30b45SVille Syrjälä 	int ret;
81928a30b45SVille Syrjälä 
82028a30b45SVille Syrjälä 	/* need to hold at least one crtc lock for the global state */
82128a30b45SVille Syrjälä 	ret = drm_modeset_lock(&crtc->base.mutex, state->base.acquire_ctx);
82228a30b45SVille Syrjälä 	if (ret)
82328a30b45SVille Syrjälä 		return ret;
82428a30b45SVille Syrjälä 
82528a30b45SVille Syrjälä 	cdclk_state = intel_atomic_get_cdclk_state(state);
82628a30b45SVille Syrjälä 	if (IS_ERR(cdclk_state))
82728a30b45SVille Syrjälä 		return PTR_ERR(cdclk_state);
82828a30b45SVille Syrjälä 
82928a30b45SVille Syrjälä 	cdclk_state->force_min_cdclk_changed = true;
83028a30b45SVille Syrjälä 	cdclk_state->force_min_cdclk = enable ? 2 * 96000 : 0;
83128a30b45SVille Syrjälä 
83228a30b45SVille Syrjälä 	ret = intel_atomic_lock_global_state(&cdclk_state->base);
83328a30b45SVille Syrjälä 	if (ret)
83428a30b45SVille Syrjälä 		return ret;
83528a30b45SVille Syrjälä 
83628a30b45SVille Syrjälä 	return drm_atomic_commit(&state->base);
83728a30b45SVille Syrjälä }
83828a30b45SVille Syrjälä 
839df0566a6SJani Nikula static void glk_force_audio_cdclk(struct drm_i915_private *dev_priv,
840df0566a6SJani Nikula 				  bool enable)
841df0566a6SJani Nikula {
842df0566a6SJani Nikula 	struct drm_modeset_acquire_ctx ctx;
843df0566a6SJani Nikula 	struct drm_atomic_state *state;
84408e3ed3aSChris Wilson 	struct intel_crtc *crtc;
845df0566a6SJani Nikula 	int ret;
846df0566a6SJani Nikula 
847*eae3da27SAnshuman Gupta 	crtc = intel_get_first_crtc(dev_priv);
84808e3ed3aSChris Wilson 	if (!crtc)
84908e3ed3aSChris Wilson 		return;
85008e3ed3aSChris Wilson 
851df0566a6SJani Nikula 	drm_modeset_acquire_init(&ctx, 0);
852df0566a6SJani Nikula 	state = drm_atomic_state_alloc(&dev_priv->drm);
8539a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !state))
854df0566a6SJani Nikula 		return;
855df0566a6SJani Nikula 
856df0566a6SJani Nikula 	state->acquire_ctx = &ctx;
857df0566a6SJani Nikula 
858df0566a6SJani Nikula retry:
85908e3ed3aSChris Wilson 	ret = glk_force_audio_cdclk_commit(to_intel_atomic_state(state), crtc,
86008e3ed3aSChris Wilson 					   enable);
861df0566a6SJani Nikula 	if (ret == -EDEADLK) {
862df0566a6SJani Nikula 		drm_atomic_state_clear(state);
863df0566a6SJani Nikula 		drm_modeset_backoff(&ctx);
864df0566a6SJani Nikula 		goto retry;
865df0566a6SJani Nikula 	}
866df0566a6SJani Nikula 
8679a3b466bSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, ret);
868df0566a6SJani Nikula 
869df0566a6SJani Nikula 	drm_atomic_state_put(state);
870df0566a6SJani Nikula 
871df0566a6SJani Nikula 	drm_modeset_drop_locks(&ctx);
872df0566a6SJani Nikula 	drm_modeset_acquire_fini(&ctx);
873df0566a6SJani Nikula }
874df0566a6SJani Nikula 
875df0566a6SJani Nikula static unsigned long i915_audio_component_get_power(struct device *kdev)
876df0566a6SJani Nikula {
877df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
878df0566a6SJani Nikula 	intel_wakeref_t ret;
879df0566a6SJani Nikula 
880df0566a6SJani Nikula 	/* Catch potential impedance mismatches before they occur! */
881df0566a6SJani Nikula 	BUILD_BUG_ON(sizeof(intel_wakeref_t) > sizeof(unsigned long));
882df0566a6SJani Nikula 
883df0566a6SJani Nikula 	ret = intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
884df0566a6SJani Nikula 
88587c16945SKai Vehmanen 	if (dev_priv->audio_power_refcount++ == 0) {
88687c16945SKai Vehmanen 		if (IS_TIGERLAKE(dev_priv) || IS_ICELAKE(dev_priv)) {
88749e659bcSJani Nikula 			intel_de_write(dev_priv, AUD_FREQ_CNTRL,
88849e659bcSJani Nikula 				       dev_priv->audio_freq_cntrl);
88963855149SWambui Karuga 			drm_dbg_kms(&dev_priv->drm,
89063855149SWambui Karuga 				    "restored AUD_FREQ_CNTRL to 0x%x\n",
89187c16945SKai Vehmanen 				    dev_priv->audio_freq_cntrl);
89287c16945SKai Vehmanen 		}
89387c16945SKai Vehmanen 
89487c16945SKai Vehmanen 		/* Force CDCLK to 2*BCLK as long as we need audio powered. */
8951ee48a61SKai Vehmanen 		if (IS_GEMINILAKE(dev_priv))
896df0566a6SJani Nikula 			glk_force_audio_cdclk(dev_priv, true);
8971580d3cdSKai Vehmanen 
8981580d3cdSKai Vehmanen 		if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
89949e659bcSJani Nikula 			intel_de_write(dev_priv, AUD_PIN_BUF_CTL,
90049e659bcSJani Nikula 				       (intel_de_read(dev_priv, AUD_PIN_BUF_CTL) | AUD_PIN_BUF_ENABLE));
90187c16945SKai Vehmanen 	}
902df0566a6SJani Nikula 
903df0566a6SJani Nikula 	return ret;
904df0566a6SJani Nikula }
905df0566a6SJani Nikula 
906df0566a6SJani Nikula static void i915_audio_component_put_power(struct device *kdev,
907df0566a6SJani Nikula 					   unsigned long cookie)
908df0566a6SJani Nikula {
909df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
910df0566a6SJani Nikula 
911df0566a6SJani Nikula 	/* Stop forcing CDCLK to 2*BCLK if no need for audio to be powered. */
912df0566a6SJani Nikula 	if (--dev_priv->audio_power_refcount == 0)
9131ee48a61SKai Vehmanen 		if (IS_GEMINILAKE(dev_priv))
914df0566a6SJani Nikula 			glk_force_audio_cdclk(dev_priv, false);
915df0566a6SJani Nikula 
916df0566a6SJani Nikula 	intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO, cookie);
917df0566a6SJani Nikula }
918df0566a6SJani Nikula 
919df0566a6SJani Nikula static void i915_audio_component_codec_wake_override(struct device *kdev,
920df0566a6SJani Nikula 						     bool enable)
921df0566a6SJani Nikula {
922df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
923df0566a6SJani Nikula 	unsigned long cookie;
924df0566a6SJani Nikula 	u32 tmp;
925df0566a6SJani Nikula 
926df0566a6SJani Nikula 	if (!IS_GEN(dev_priv, 9))
927df0566a6SJani Nikula 		return;
928df0566a6SJani Nikula 
929df0566a6SJani Nikula 	cookie = i915_audio_component_get_power(kdev);
930df0566a6SJani Nikula 
931df0566a6SJani Nikula 	/*
932df0566a6SJani Nikula 	 * Enable/disable generating the codec wake signal, overriding the
933df0566a6SJani Nikula 	 * internal logic to generate the codec wake to controller.
934df0566a6SJani Nikula 	 */
93549e659bcSJani Nikula 	tmp = intel_de_read(dev_priv, HSW_AUD_CHICKENBIT);
936df0566a6SJani Nikula 	tmp &= ~SKL_AUD_CODEC_WAKE_SIGNAL;
93749e659bcSJani Nikula 	intel_de_write(dev_priv, HSW_AUD_CHICKENBIT, tmp);
938df0566a6SJani Nikula 	usleep_range(1000, 1500);
939df0566a6SJani Nikula 
940df0566a6SJani Nikula 	if (enable) {
94149e659bcSJani Nikula 		tmp = intel_de_read(dev_priv, HSW_AUD_CHICKENBIT);
942df0566a6SJani Nikula 		tmp |= SKL_AUD_CODEC_WAKE_SIGNAL;
94349e659bcSJani Nikula 		intel_de_write(dev_priv, HSW_AUD_CHICKENBIT, tmp);
944df0566a6SJani Nikula 		usleep_range(1000, 1500);
945df0566a6SJani Nikula 	}
946df0566a6SJani Nikula 
947df0566a6SJani Nikula 	i915_audio_component_put_power(kdev, cookie);
948df0566a6SJani Nikula }
949df0566a6SJani Nikula 
950df0566a6SJani Nikula /* Get CDCLK in kHz  */
951df0566a6SJani Nikula static int i915_audio_component_get_cdclk_freq(struct device *kdev)
952df0566a6SJani Nikula {
953df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
954df0566a6SJani Nikula 
9559a3b466bSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !HAS_DDI(dev_priv)))
956df0566a6SJani Nikula 		return -ENODEV;
957df0566a6SJani Nikula 
958df0566a6SJani Nikula 	return dev_priv->cdclk.hw.cdclk;
959df0566a6SJani Nikula }
960df0566a6SJani Nikula 
961df0566a6SJani Nikula /*
962df0566a6SJani Nikula  * get the intel_encoder according to the parameter port and pipe
963df0566a6SJani Nikula  * intel_encoder is saved by the index of pipe
964df0566a6SJani Nikula  * MST & (pipe >= 0): return the av_enc_map[pipe],
965df0566a6SJani Nikula  *   when port is matched
966df0566a6SJani Nikula  * MST & (pipe < 0): this is invalid
967df0566a6SJani Nikula  * Non-MST & (pipe >= 0): only pipe = 0 (the first device entry)
968df0566a6SJani Nikula  *   will get the right intel_encoder with port matched
969df0566a6SJani Nikula  * Non-MST & (pipe < 0): get the right intel_encoder with port matched
970df0566a6SJani Nikula  */
971df0566a6SJani Nikula static struct intel_encoder *get_saved_enc(struct drm_i915_private *dev_priv,
972df0566a6SJani Nikula 					       int port, int pipe)
973df0566a6SJani Nikula {
974df0566a6SJani Nikula 	struct intel_encoder *encoder;
975df0566a6SJani Nikula 
976df0566a6SJani Nikula 	/* MST */
977df0566a6SJani Nikula 	if (pipe >= 0) {
9789a3b466bSPankaj Bharadiya 		if (drm_WARN_ON(&dev_priv->drm,
9799a3b466bSPankaj Bharadiya 				pipe >= ARRAY_SIZE(dev_priv->av_enc_map)))
980df0566a6SJani Nikula 			return NULL;
981df0566a6SJani Nikula 
982df0566a6SJani Nikula 		encoder = dev_priv->av_enc_map[pipe];
983df0566a6SJani Nikula 		/*
984df0566a6SJani Nikula 		 * when bootup, audio driver may not know it is
985df0566a6SJani Nikula 		 * MST or not. So it will poll all the port & pipe
986df0566a6SJani Nikula 		 * combinations
987df0566a6SJani Nikula 		 */
988df0566a6SJani Nikula 		if (encoder != NULL && encoder->port == port &&
989df0566a6SJani Nikula 		    encoder->type == INTEL_OUTPUT_DP_MST)
990df0566a6SJani Nikula 			return encoder;
991df0566a6SJani Nikula 	}
992df0566a6SJani Nikula 
993df0566a6SJani Nikula 	/* Non-MST */
994df0566a6SJani Nikula 	if (pipe > 0)
995df0566a6SJani Nikula 		return NULL;
996df0566a6SJani Nikula 
997df0566a6SJani Nikula 	for_each_pipe(dev_priv, pipe) {
998df0566a6SJani Nikula 		encoder = dev_priv->av_enc_map[pipe];
999df0566a6SJani Nikula 		if (encoder == NULL)
1000df0566a6SJani Nikula 			continue;
1001df0566a6SJani Nikula 
1002df0566a6SJani Nikula 		if (encoder->type == INTEL_OUTPUT_DP_MST)
1003df0566a6SJani Nikula 			continue;
1004df0566a6SJani Nikula 
1005df0566a6SJani Nikula 		if (port == encoder->port)
1006df0566a6SJani Nikula 			return encoder;
1007df0566a6SJani Nikula 	}
1008df0566a6SJani Nikula 
1009df0566a6SJani Nikula 	return NULL;
1010df0566a6SJani Nikula }
1011df0566a6SJani Nikula 
1012df0566a6SJani Nikula static int i915_audio_component_sync_audio_rate(struct device *kdev, int port,
1013df0566a6SJani Nikula 						int pipe, int rate)
1014df0566a6SJani Nikula {
1015df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1016df0566a6SJani Nikula 	struct i915_audio_component *acomp = dev_priv->audio_component;
1017df0566a6SJani Nikula 	struct intel_encoder *encoder;
1018df0566a6SJani Nikula 	struct intel_crtc *crtc;
1019df0566a6SJani Nikula 	unsigned long cookie;
1020df0566a6SJani Nikula 	int err = 0;
1021df0566a6SJani Nikula 
1022df0566a6SJani Nikula 	if (!HAS_DDI(dev_priv))
1023df0566a6SJani Nikula 		return 0;
1024df0566a6SJani Nikula 
1025df0566a6SJani Nikula 	cookie = i915_audio_component_get_power(kdev);
1026df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
1027df0566a6SJani Nikula 
1028df0566a6SJani Nikula 	/* 1. get the pipe */
1029df0566a6SJani Nikula 	encoder = get_saved_enc(dev_priv, port, pipe);
1030df0566a6SJani Nikula 	if (!encoder || !encoder->base.crtc) {
103163855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "Not valid for port %c\n",
103263855149SWambui Karuga 			    port_name(port));
1033df0566a6SJani Nikula 		err = -ENODEV;
1034df0566a6SJani Nikula 		goto unlock;
1035df0566a6SJani Nikula 	}
1036df0566a6SJani Nikula 
1037df0566a6SJani Nikula 	crtc = to_intel_crtc(encoder->base.crtc);
1038df0566a6SJani Nikula 
1039df0566a6SJani Nikula 	/* port must be valid now, otherwise the pipe will be invalid */
1040df0566a6SJani Nikula 	acomp->aud_sample_rate[port] = rate;
1041df0566a6SJani Nikula 
1042df0566a6SJani Nikula 	hsw_audio_config_update(encoder, crtc->config);
1043df0566a6SJani Nikula 
1044df0566a6SJani Nikula  unlock:
1045df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
1046df0566a6SJani Nikula 	i915_audio_component_put_power(kdev, cookie);
1047df0566a6SJani Nikula 	return err;
1048df0566a6SJani Nikula }
1049df0566a6SJani Nikula 
1050df0566a6SJani Nikula static int i915_audio_component_get_eld(struct device *kdev, int port,
1051df0566a6SJani Nikula 					int pipe, bool *enabled,
1052df0566a6SJani Nikula 					unsigned char *buf, int max_bytes)
1053df0566a6SJani Nikula {
1054df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1055df0566a6SJani Nikula 	struct intel_encoder *intel_encoder;
1056df0566a6SJani Nikula 	const u8 *eld;
1057df0566a6SJani Nikula 	int ret = -EINVAL;
1058df0566a6SJani Nikula 
1059df0566a6SJani Nikula 	mutex_lock(&dev_priv->av_mutex);
1060df0566a6SJani Nikula 
1061df0566a6SJani Nikula 	intel_encoder = get_saved_enc(dev_priv, port, pipe);
1062df0566a6SJani Nikula 	if (!intel_encoder) {
106363855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "Not valid for port %c\n",
106463855149SWambui Karuga 			    port_name(port));
1065df0566a6SJani Nikula 		mutex_unlock(&dev_priv->av_mutex);
1066df0566a6SJani Nikula 		return ret;
1067df0566a6SJani Nikula 	}
1068df0566a6SJani Nikula 
1069df0566a6SJani Nikula 	ret = 0;
1070df0566a6SJani Nikula 	*enabled = intel_encoder->audio_connector != NULL;
1071df0566a6SJani Nikula 	if (*enabled) {
1072df0566a6SJani Nikula 		eld = intel_encoder->audio_connector->eld;
1073df0566a6SJani Nikula 		ret = drm_eld_size(eld);
1074df0566a6SJani Nikula 		memcpy(buf, eld, min(max_bytes, ret));
1075df0566a6SJani Nikula 	}
1076df0566a6SJani Nikula 
1077df0566a6SJani Nikula 	mutex_unlock(&dev_priv->av_mutex);
1078df0566a6SJani Nikula 	return ret;
1079df0566a6SJani Nikula }
1080df0566a6SJani Nikula 
1081df0566a6SJani Nikula static const struct drm_audio_component_ops i915_audio_component_ops = {
1082df0566a6SJani Nikula 	.owner		= THIS_MODULE,
1083df0566a6SJani Nikula 	.get_power	= i915_audio_component_get_power,
1084df0566a6SJani Nikula 	.put_power	= i915_audio_component_put_power,
1085df0566a6SJani Nikula 	.codec_wake_override = i915_audio_component_codec_wake_override,
1086df0566a6SJani Nikula 	.get_cdclk_freq	= i915_audio_component_get_cdclk_freq,
1087df0566a6SJani Nikula 	.sync_audio_rate = i915_audio_component_sync_audio_rate,
1088df0566a6SJani Nikula 	.get_eld	= i915_audio_component_get_eld,
1089df0566a6SJani Nikula };
1090df0566a6SJani Nikula 
1091df0566a6SJani Nikula static int i915_audio_component_bind(struct device *i915_kdev,
1092df0566a6SJani Nikula 				     struct device *hda_kdev, void *data)
1093df0566a6SJani Nikula {
1094df0566a6SJani Nikula 	struct i915_audio_component *acomp = data;
1095df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
1096df0566a6SJani Nikula 	int i;
1097df0566a6SJani Nikula 
10989a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, acomp->base.ops || acomp->base.dev))
1099df0566a6SJani Nikula 		return -EEXIST;
1100df0566a6SJani Nikula 
11019a3b466bSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
11029a3b466bSPankaj Bharadiya 			!device_link_add(hda_kdev, i915_kdev,
11039a3b466bSPankaj Bharadiya 					 DL_FLAG_STATELESS)))
1104df0566a6SJani Nikula 		return -ENOMEM;
1105df0566a6SJani Nikula 
1106df0566a6SJani Nikula 	drm_modeset_lock_all(&dev_priv->drm);
1107df0566a6SJani Nikula 	acomp->base.ops = &i915_audio_component_ops;
1108df0566a6SJani Nikula 	acomp->base.dev = i915_kdev;
1109df0566a6SJani Nikula 	BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
1110df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
1111df0566a6SJani Nikula 		acomp->aud_sample_rate[i] = 0;
1112df0566a6SJani Nikula 	dev_priv->audio_component = acomp;
1113df0566a6SJani Nikula 	drm_modeset_unlock_all(&dev_priv->drm);
1114df0566a6SJani Nikula 
1115df0566a6SJani Nikula 	return 0;
1116df0566a6SJani Nikula }
1117df0566a6SJani Nikula 
1118df0566a6SJani Nikula static void i915_audio_component_unbind(struct device *i915_kdev,
1119df0566a6SJani Nikula 					struct device *hda_kdev, void *data)
1120df0566a6SJani Nikula {
1121df0566a6SJani Nikula 	struct i915_audio_component *acomp = data;
1122df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
1123df0566a6SJani Nikula 
1124df0566a6SJani Nikula 	drm_modeset_lock_all(&dev_priv->drm);
1125df0566a6SJani Nikula 	acomp->base.ops = NULL;
1126df0566a6SJani Nikula 	acomp->base.dev = NULL;
1127df0566a6SJani Nikula 	dev_priv->audio_component = NULL;
1128df0566a6SJani Nikula 	drm_modeset_unlock_all(&dev_priv->drm);
1129df0566a6SJani Nikula 
1130df0566a6SJani Nikula 	device_link_remove(hda_kdev, i915_kdev);
1131df0566a6SJani Nikula }
1132df0566a6SJani Nikula 
1133df0566a6SJani Nikula static const struct component_ops i915_audio_component_bind_ops = {
1134df0566a6SJani Nikula 	.bind	= i915_audio_component_bind,
1135df0566a6SJani Nikula 	.unbind	= i915_audio_component_unbind,
1136df0566a6SJani Nikula };
1137df0566a6SJani Nikula 
1138df0566a6SJani Nikula /**
1139df0566a6SJani Nikula  * i915_audio_component_init - initialize and register the audio component
1140df0566a6SJani Nikula  * @dev_priv: i915 device instance
1141df0566a6SJani Nikula  *
1142df0566a6SJani Nikula  * This will register with the component framework a child component which
1143df0566a6SJani Nikula  * will bind dynamically to the snd_hda_intel driver's corresponding master
1144df0566a6SJani Nikula  * component when the latter is registered. During binding the child
1145df0566a6SJani Nikula  * initializes an instance of struct i915_audio_component which it receives
1146df0566a6SJani Nikula  * from the master. The master can then start to use the interface defined by
1147df0566a6SJani Nikula  * this struct. Each side can break the binding at any point by deregistering
1148df0566a6SJani Nikula  * its own component after which each side's component unbind callback is
1149df0566a6SJani Nikula  * called.
1150df0566a6SJani Nikula  *
1151df0566a6SJani Nikula  * We ignore any error during registration and continue with reduced
1152df0566a6SJani Nikula  * functionality (i.e. without HDMI audio).
1153df0566a6SJani Nikula  */
1154df0566a6SJani Nikula static void i915_audio_component_init(struct drm_i915_private *dev_priv)
1155df0566a6SJani Nikula {
1156df0566a6SJani Nikula 	int ret;
1157df0566a6SJani Nikula 
1158df0566a6SJani Nikula 	ret = component_add_typed(dev_priv->drm.dev,
1159df0566a6SJani Nikula 				  &i915_audio_component_bind_ops,
1160df0566a6SJani Nikula 				  I915_COMPONENT_AUDIO);
1161df0566a6SJani Nikula 	if (ret < 0) {
116263855149SWambui Karuga 		drm_err(&dev_priv->drm,
116363855149SWambui Karuga 			"failed to add audio component (%d)\n", ret);
1164df0566a6SJani Nikula 		/* continue with reduced functionality */
1165df0566a6SJani Nikula 		return;
1166df0566a6SJani Nikula 	}
1167df0566a6SJani Nikula 
116887c16945SKai Vehmanen 	if (IS_TIGERLAKE(dev_priv) || IS_ICELAKE(dev_priv)) {
116949e659bcSJani Nikula 		dev_priv->audio_freq_cntrl = intel_de_read(dev_priv,
117049e659bcSJani Nikula 							   AUD_FREQ_CNTRL);
117163855149SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
117263855149SWambui Karuga 			    "init value of AUD_FREQ_CNTRL of 0x%x\n",
117387c16945SKai Vehmanen 			    dev_priv->audio_freq_cntrl);
117487c16945SKai Vehmanen 	}
117587c16945SKai Vehmanen 
1176df0566a6SJani Nikula 	dev_priv->audio_component_registered = true;
1177df0566a6SJani Nikula }
1178df0566a6SJani Nikula 
1179df0566a6SJani Nikula /**
1180df0566a6SJani Nikula  * i915_audio_component_cleanup - deregister the audio component
1181df0566a6SJani Nikula  * @dev_priv: i915 device instance
1182df0566a6SJani Nikula  *
1183df0566a6SJani Nikula  * Deregisters the audio component, breaking any existing binding to the
1184df0566a6SJani Nikula  * corresponding snd_hda_intel driver's master component.
1185df0566a6SJani Nikula  */
1186df0566a6SJani Nikula static void i915_audio_component_cleanup(struct drm_i915_private *dev_priv)
1187df0566a6SJani Nikula {
1188df0566a6SJani Nikula 	if (!dev_priv->audio_component_registered)
1189df0566a6SJani Nikula 		return;
1190df0566a6SJani Nikula 
1191df0566a6SJani Nikula 	component_del(dev_priv->drm.dev, &i915_audio_component_bind_ops);
1192df0566a6SJani Nikula 	dev_priv->audio_component_registered = false;
1193df0566a6SJani Nikula }
1194df0566a6SJani Nikula 
1195df0566a6SJani Nikula /**
1196df0566a6SJani Nikula  * intel_audio_init() - Initialize the audio driver either using
1197df0566a6SJani Nikula  * component framework or using lpe audio bridge
1198df0566a6SJani Nikula  * @dev_priv: the i915 drm device private data
1199df0566a6SJani Nikula  *
1200df0566a6SJani Nikula  */
1201df0566a6SJani Nikula void intel_audio_init(struct drm_i915_private *dev_priv)
1202df0566a6SJani Nikula {
1203df0566a6SJani Nikula 	if (intel_lpe_audio_init(dev_priv) < 0)
1204df0566a6SJani Nikula 		i915_audio_component_init(dev_priv);
1205df0566a6SJani Nikula }
1206df0566a6SJani Nikula 
1207df0566a6SJani Nikula /**
1208df0566a6SJani Nikula  * intel_audio_deinit() - deinitialize the audio driver
1209df0566a6SJani Nikula  * @dev_priv: the i915 drm device private data
1210df0566a6SJani Nikula  *
1211df0566a6SJani Nikula  */
1212df0566a6SJani Nikula void intel_audio_deinit(struct drm_i915_private *dev_priv)
1213df0566a6SJani Nikula {
1214df0566a6SJani Nikula 	if ((dev_priv)->lpe_audio.platdev != NULL)
1215df0566a6SJani Nikula 		intel_lpe_audio_teardown(dev_priv);
1216df0566a6SJani Nikula 	else
1217df0566a6SJani Nikula 		i915_audio_component_cleanup(dev_priv);
1218df0566a6SJani Nikula }
1219