xref: /openbmc/linux/drivers/gpu/drm/i915/display/intel_audio.c (revision 6c71a0574249f5e5a45fe055ab5f837023d5eeca)
1df0566a6SJani Nikula /*
2df0566a6SJani Nikula  * Copyright © 2014 Intel Corporation
3df0566a6SJani Nikula  *
4df0566a6SJani Nikula  * Permission is hereby granted, free of charge, to any person obtaining a
5df0566a6SJani Nikula  * copy of this software and associated documentation files (the "Software"),
6df0566a6SJani Nikula  * to deal in the Software without restriction, including without limitation
7df0566a6SJani Nikula  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8df0566a6SJani Nikula  * and/or sell copies of the Software, and to permit persons to whom the
9df0566a6SJani Nikula  * Software is furnished to do so, subject to the following conditions:
10df0566a6SJani Nikula  *
11df0566a6SJani Nikula  * The above copyright notice and this permission notice (including the next
12df0566a6SJani Nikula  * paragraph) shall be included in all copies or substantial portions of the
13df0566a6SJani Nikula  * Software.
14df0566a6SJani Nikula  *
15df0566a6SJani Nikula  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16df0566a6SJani Nikula  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17df0566a6SJani Nikula  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18df0566a6SJani Nikula  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19df0566a6SJani Nikula  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20df0566a6SJani Nikula  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21df0566a6SJani Nikula  * DEALINGS IN THE SOFTWARE.
22df0566a6SJani Nikula  */
23df0566a6SJani Nikula 
24df0566a6SJani Nikula #include <linux/component.h>
25df0566a6SJani Nikula #include <linux/kernel.h>
26df0566a6SJani Nikula 
27df0566a6SJani Nikula #include <drm/drm_edid.h>
28df0566a6SJani Nikula #include <drm/i915_component.h>
29df0566a6SJani Nikula 
30df0566a6SJani Nikula #include "i915_drv.h"
311d5a95b5SVille Syrjälä #include "intel_atomic.h"
32df0566a6SJani Nikula #include "intel_audio.h"
33b43edc50SJani Nikula #include "intel_audio_regs.h"
3428a30b45SVille Syrjälä #include "intel_cdclk.h"
35fd2b94a5SJani Nikula #include "intel_crtc.h"
367785ae0bSVille Syrjälä #include "intel_de.h"
371d455f8dSJani Nikula #include "intel_display_types.h"
38df0566a6SJani Nikula #include "intel_lpe_audio.h"
39df0566a6SJani Nikula 
40df0566a6SJani Nikula /**
41df0566a6SJani Nikula  * DOC: High Definition Audio over HDMI and Display Port
42df0566a6SJani Nikula  *
43df0566a6SJani Nikula  * The graphics and audio drivers together support High Definition Audio over
44df0566a6SJani Nikula  * HDMI and Display Port. The audio programming sequences are divided into audio
45df0566a6SJani Nikula  * codec and controller enable and disable sequences. The graphics driver
46df0566a6SJani Nikula  * handles the audio codec sequences, while the audio driver handles the audio
47df0566a6SJani Nikula  * controller sequences.
48df0566a6SJani Nikula  *
49df0566a6SJani Nikula  * The disable sequences must be performed before disabling the transcoder or
50df0566a6SJani Nikula  * port. The enable sequences may only be performed after enabling the
51df0566a6SJani Nikula  * transcoder and port, and after completed link training. Therefore the audio
52df0566a6SJani Nikula  * enable/disable sequences are part of the modeset sequence.
53df0566a6SJani Nikula  *
54df0566a6SJani Nikula  * The codec and controller sequences could be done either parallel or serial,
55df0566a6SJani Nikula  * but generally the ELDV/PD change in the codec sequence indicates to the audio
56df0566a6SJani Nikula  * driver that the controller sequence should start. Indeed, most of the
57df0566a6SJani Nikula  * co-operation between the graphics and audio drivers is handled via audio
58df0566a6SJani Nikula  * related registers. (The notable exception is the power management, not
59df0566a6SJani Nikula  * covered here.)
60df0566a6SJani Nikula  *
61df0566a6SJani Nikula  * The struct &i915_audio_component is used to interact between the graphics
62df0566a6SJani Nikula  * and audio drivers. The struct &i915_audio_component_ops @ops in it is
63df0566a6SJani Nikula  * defined in graphics driver and called in audio driver. The
64df0566a6SJani Nikula  * struct &i915_audio_component_audio_ops @audio_ops is called from i915 driver.
65df0566a6SJani Nikula  */
66df0566a6SJani Nikula 
675d453746SJani Nikula struct intel_audio_funcs {
685d453746SJani Nikula 	void (*audio_codec_enable)(struct intel_encoder *encoder,
695d453746SJani Nikula 				   const struct intel_crtc_state *crtc_state,
705d453746SJani Nikula 				   const struct drm_connector_state *conn_state);
715d453746SJani Nikula 	void (*audio_codec_disable)(struct intel_encoder *encoder,
725d453746SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
735d453746SJani Nikula 				    const struct drm_connector_state *old_conn_state);
7461a60df6SVille Syrjälä 	void (*audio_codec_get_config)(struct intel_encoder *encoder,
7561a60df6SVille Syrjälä 				       struct intel_crtc_state *crtc_state);
765d453746SJani Nikula };
775d453746SJani Nikula 
782c291417SAditya Swarup struct hdmi_aud_ncts {
792c291417SAditya Swarup 	int sample_rate;
802c291417SAditya Swarup 	int clock;
812c291417SAditya Swarup 	int n;
822c291417SAditya Swarup 	int cts;
832c291417SAditya Swarup };
842c291417SAditya Swarup 
85df0566a6SJani Nikula static const struct {
86df0566a6SJani Nikula 	int clock;
87df0566a6SJani Nikula 	u32 config;
88df0566a6SJani Nikula } hdmi_audio_clock[] = {
89df0566a6SJani Nikula 	{ 25175, AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
90df0566a6SJani Nikula 	{ 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
91df0566a6SJani Nikula 	{ 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
92df0566a6SJani Nikula 	{ 27027, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
93df0566a6SJani Nikula 	{ 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
94df0566a6SJani Nikula 	{ 54054, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
95df0566a6SJani Nikula 	{ 74176, AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
96df0566a6SJani Nikula 	{ 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
97df0566a6SJani Nikula 	{ 148352, AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
98df0566a6SJani Nikula 	{ 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
991aae3065SKai Vehmanen 	{ 296703, AUD_CONFIG_PIXEL_CLOCK_HDMI_296703 },
1001aae3065SKai Vehmanen 	{ 297000, AUD_CONFIG_PIXEL_CLOCK_HDMI_297000 },
1011aae3065SKai Vehmanen 	{ 593407, AUD_CONFIG_PIXEL_CLOCK_HDMI_593407 },
1021aae3065SKai Vehmanen 	{ 594000, AUD_CONFIG_PIXEL_CLOCK_HDMI_594000 },
103df0566a6SJani Nikula };
104df0566a6SJani Nikula 
105df0566a6SJani Nikula /* HDMI N/CTS table */
106df0566a6SJani Nikula #define TMDS_297M 297000
107df0566a6SJani Nikula #define TMDS_296M 296703
108df0566a6SJani Nikula #define TMDS_594M 594000
109df0566a6SJani Nikula #define TMDS_593M 593407
110df0566a6SJani Nikula 
1112c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_24bpp[] = {
112df0566a6SJani Nikula 	{ 32000, TMDS_296M, 5824, 421875 },
113df0566a6SJani Nikula 	{ 32000, TMDS_297M, 3072, 222750 },
114df0566a6SJani Nikula 	{ 32000, TMDS_593M, 5824, 843750 },
115df0566a6SJani Nikula 	{ 32000, TMDS_594M, 3072, 445500 },
116df0566a6SJani Nikula 	{ 44100, TMDS_296M, 4459, 234375 },
117df0566a6SJani Nikula 	{ 44100, TMDS_297M, 4704, 247500 },
118df0566a6SJani Nikula 	{ 44100, TMDS_593M, 8918, 937500 },
119df0566a6SJani Nikula 	{ 44100, TMDS_594M, 9408, 990000 },
120df0566a6SJani Nikula 	{ 88200, TMDS_296M, 8918, 234375 },
121df0566a6SJani Nikula 	{ 88200, TMDS_297M, 9408, 247500 },
122df0566a6SJani Nikula 	{ 88200, TMDS_593M, 17836, 937500 },
123df0566a6SJani Nikula 	{ 88200, TMDS_594M, 18816, 990000 },
124df0566a6SJani Nikula 	{ 176400, TMDS_296M, 17836, 234375 },
125df0566a6SJani Nikula 	{ 176400, TMDS_297M, 18816, 247500 },
126df0566a6SJani Nikula 	{ 176400, TMDS_593M, 35672, 937500 },
127df0566a6SJani Nikula 	{ 176400, TMDS_594M, 37632, 990000 },
128df0566a6SJani Nikula 	{ 48000, TMDS_296M, 5824, 281250 },
129df0566a6SJani Nikula 	{ 48000, TMDS_297M, 5120, 247500 },
130df0566a6SJani Nikula 	{ 48000, TMDS_593M, 5824, 562500 },
131df0566a6SJani Nikula 	{ 48000, TMDS_594M, 6144, 594000 },
132df0566a6SJani Nikula 	{ 96000, TMDS_296M, 11648, 281250 },
133df0566a6SJani Nikula 	{ 96000, TMDS_297M, 10240, 247500 },
134df0566a6SJani Nikula 	{ 96000, TMDS_593M, 11648, 562500 },
135df0566a6SJani Nikula 	{ 96000, TMDS_594M, 12288, 594000 },
136df0566a6SJani Nikula 	{ 192000, TMDS_296M, 23296, 281250 },
137df0566a6SJani Nikula 	{ 192000, TMDS_297M, 20480, 247500 },
138df0566a6SJani Nikula 	{ 192000, TMDS_593M, 23296, 562500 },
139df0566a6SJani Nikula 	{ 192000, TMDS_594M, 24576, 594000 },
140df0566a6SJani Nikula };
141df0566a6SJani Nikula 
1422c291417SAditya Swarup /* Appendix C - N & CTS values for deep color from HDMI 2.0 spec*/
1432c291417SAditya Swarup /* HDMI N/CTS table for 10 bit deep color(30 bpp)*/
1442c291417SAditya Swarup #define TMDS_371M 371250
1452c291417SAditya Swarup #define TMDS_370M 370878
1462c291417SAditya Swarup 
1472c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_30bpp[] = {
1482c291417SAditya Swarup 	{ 32000, TMDS_370M, 5824, 527344 },
1492c291417SAditya Swarup 	{ 32000, TMDS_371M, 6144, 556875 },
1502c291417SAditya Swarup 	{ 44100, TMDS_370M, 8918, 585938 },
1512c291417SAditya Swarup 	{ 44100, TMDS_371M, 4704, 309375 },
1522c291417SAditya Swarup 	{ 88200, TMDS_370M, 17836, 585938 },
1532c291417SAditya Swarup 	{ 88200, TMDS_371M, 9408, 309375 },
1542c291417SAditya Swarup 	{ 176400, TMDS_370M, 35672, 585938 },
1552c291417SAditya Swarup 	{ 176400, TMDS_371M, 18816, 309375 },
1562c291417SAditya Swarup 	{ 48000, TMDS_370M, 11648, 703125 },
1572c291417SAditya Swarup 	{ 48000, TMDS_371M, 5120, 309375 },
1582c291417SAditya Swarup 	{ 96000, TMDS_370M, 23296, 703125 },
1592c291417SAditya Swarup 	{ 96000, TMDS_371M, 10240, 309375 },
1602c291417SAditya Swarup 	{ 192000, TMDS_370M, 46592, 703125 },
1612c291417SAditya Swarup 	{ 192000, TMDS_371M, 20480, 309375 },
1622c291417SAditya Swarup };
1632c291417SAditya Swarup 
1642c291417SAditya Swarup /* HDMI N/CTS table for 12 bit deep color(36 bpp)*/
1652c291417SAditya Swarup #define TMDS_445_5M 445500
1662c291417SAditya Swarup #define TMDS_445M 445054
1672c291417SAditya Swarup 
1682c291417SAditya Swarup static const struct hdmi_aud_ncts hdmi_aud_ncts_36bpp[] = {
1692c291417SAditya Swarup 	{ 32000, TMDS_445M, 5824, 632813 },
1702c291417SAditya Swarup 	{ 32000, TMDS_445_5M, 4096, 445500 },
1712c291417SAditya Swarup 	{ 44100, TMDS_445M, 8918, 703125 },
1722c291417SAditya Swarup 	{ 44100, TMDS_445_5M, 4704, 371250 },
1732c291417SAditya Swarup 	{ 88200, TMDS_445M, 17836, 703125 },
1742c291417SAditya Swarup 	{ 88200, TMDS_445_5M, 9408, 371250 },
1752c291417SAditya Swarup 	{ 176400, TMDS_445M, 35672, 703125 },
1762c291417SAditya Swarup 	{ 176400, TMDS_445_5M, 18816, 371250 },
1772c291417SAditya Swarup 	{ 48000, TMDS_445M, 5824, 421875 },
1782c291417SAditya Swarup 	{ 48000, TMDS_445_5M, 5120, 371250 },
1792c291417SAditya Swarup 	{ 96000, TMDS_445M, 11648, 421875 },
1802c291417SAditya Swarup 	{ 96000, TMDS_445_5M, 10240, 371250 },
1812c291417SAditya Swarup 	{ 192000, TMDS_445M, 23296, 421875 },
1822c291417SAditya Swarup 	{ 192000, TMDS_445_5M, 20480, 371250 },
1832c291417SAditya Swarup };
1842c291417SAditya Swarup 
185df0566a6SJani Nikula /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
audio_config_hdmi_pixel_clock(const struct intel_crtc_state * crtc_state)186df0566a6SJani Nikula static u32 audio_config_hdmi_pixel_clock(const struct intel_crtc_state *crtc_state)
187df0566a6SJani Nikula {
18846e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
189df0566a6SJani Nikula 	const struct drm_display_mode *adjusted_mode =
1901326a92cSMaarten Lankhorst 		&crtc_state->hw.adjusted_mode;
191df0566a6SJani Nikula 	int i;
192df0566a6SJani Nikula 
193df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
194df0566a6SJani Nikula 		if (adjusted_mode->crtc_clock == hdmi_audio_clock[i].clock)
195df0566a6SJani Nikula 			break;
196df0566a6SJani Nikula 	}
197df0566a6SJani Nikula 
19846e61ee4SVille Syrjälä 	if (DISPLAY_VER(i915) < 12 && adjusted_mode->crtc_clock > 148500)
1991aae3065SKai Vehmanen 		i = ARRAY_SIZE(hdmi_audio_clock);
2001aae3065SKai Vehmanen 
201df0566a6SJani Nikula 	if (i == ARRAY_SIZE(hdmi_audio_clock)) {
20246e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm,
2039282a66cSJani Nikula 			    "HDMI audio pixel clock setting for %d not found, falling back to defaults\n",
204df0566a6SJani Nikula 			    adjusted_mode->crtc_clock);
205df0566a6SJani Nikula 		i = 1;
206df0566a6SJani Nikula 	}
207df0566a6SJani Nikula 
20846e61ee4SVille Syrjälä 	drm_dbg_kms(&i915->drm,
2099282a66cSJani Nikula 		    "Configuring HDMI audio for pixel clock %d (0x%08x)\n",
210df0566a6SJani Nikula 		    hdmi_audio_clock[i].clock,
211df0566a6SJani Nikula 		    hdmi_audio_clock[i].config);
212df0566a6SJani Nikula 
213df0566a6SJani Nikula 	return hdmi_audio_clock[i].config;
214df0566a6SJani Nikula }
215df0566a6SJani Nikula 
audio_config_hdmi_get_n(const struct intel_crtc_state * crtc_state,int rate)216df0566a6SJani Nikula static int audio_config_hdmi_get_n(const struct intel_crtc_state *crtc_state,
217df0566a6SJani Nikula 				   int rate)
218df0566a6SJani Nikula {
2192c291417SAditya Swarup 	const struct hdmi_aud_ncts *hdmi_ncts_table;
2202c291417SAditya Swarup 	int i, size;
221df0566a6SJani Nikula 
2222c291417SAditya Swarup 	if (crtc_state->pipe_bpp == 36) {
2232c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_36bpp;
2242c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_36bpp);
2252c291417SAditya Swarup 	} else if (crtc_state->pipe_bpp == 30) {
2262c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_30bpp;
2272c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_30bpp);
2282c291417SAditya Swarup 	} else {
2292c291417SAditya Swarup 		hdmi_ncts_table = hdmi_aud_ncts_24bpp;
2302c291417SAditya Swarup 		size = ARRAY_SIZE(hdmi_aud_ncts_24bpp);
2312c291417SAditya Swarup 	}
2322c291417SAditya Swarup 
2332c291417SAditya Swarup 	for (i = 0; i < size; i++) {
2342c291417SAditya Swarup 		if (rate == hdmi_ncts_table[i].sample_rate &&
2352c291417SAditya Swarup 		    crtc_state->port_clock == hdmi_ncts_table[i].clock) {
2362c291417SAditya Swarup 			return hdmi_ncts_table[i].n;
237df0566a6SJani Nikula 		}
238df0566a6SJani Nikula 	}
239df0566a6SJani Nikula 	return 0;
240df0566a6SJani Nikula }
241df0566a6SJani Nikula 
2421c0ab71aSVille Syrjälä /* ELD buffer size in dwords */
g4x_eld_buffer_size(struct drm_i915_private * i915)2431c0ab71aSVille Syrjälä static int g4x_eld_buffer_size(struct drm_i915_private *i915)
2441c0ab71aSVille Syrjälä {
2451c0ab71aSVille Syrjälä 	u32 tmp;
2461c0ab71aSVille Syrjälä 
2471c0ab71aSVille Syrjälä 	tmp = intel_de_read(i915, G4X_AUD_CNTL_ST);
2481c0ab71aSVille Syrjälä 
2491c0ab71aSVille Syrjälä 	return REG_FIELD_GET(G4X_ELD_BUFFER_SIZE_MASK, tmp);
2501c0ab71aSVille Syrjälä }
2511c0ab71aSVille Syrjälä 
g4x_audio_codec_get_config(struct intel_encoder * encoder,struct intel_crtc_state * crtc_state)25261a60df6SVille Syrjälä static void g4x_audio_codec_get_config(struct intel_encoder *encoder,
25361a60df6SVille Syrjälä 				       struct intel_crtc_state *crtc_state)
25461a60df6SVille Syrjälä {
25561a60df6SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
25661a60df6SVille Syrjälä 	u32 *eld = (u32 *)crtc_state->eld;
25761a60df6SVille Syrjälä 	int eld_buffer_size, len, i;
25861a60df6SVille Syrjälä 	u32 tmp;
25961a60df6SVille Syrjälä 
26061a60df6SVille Syrjälä 	tmp = intel_de_read(i915, G4X_AUD_CNTL_ST);
26161a60df6SVille Syrjälä 	if ((tmp & G4X_ELD_VALID) == 0)
26261a60df6SVille Syrjälä 		return;
26361a60df6SVille Syrjälä 
26461a60df6SVille Syrjälä 	intel_de_rmw(i915, G4X_AUD_CNTL_ST, G4X_ELD_ADDRESS_MASK, 0);
26561a60df6SVille Syrjälä 
26661a60df6SVille Syrjälä 	eld_buffer_size = g4x_eld_buffer_size(i915);
26761a60df6SVille Syrjälä 	len = min_t(int, sizeof(crtc_state->eld) / 4, eld_buffer_size);
26861a60df6SVille Syrjälä 
26961a60df6SVille Syrjälä 	for (i = 0; i < len; i++)
27061a60df6SVille Syrjälä 		eld[i] = intel_de_read(i915, G4X_HDMIW_HDMIEDID);
27161a60df6SVille Syrjälä }
27261a60df6SVille Syrjälä 
g4x_audio_codec_disable(struct intel_encoder * encoder,const struct intel_crtc_state * old_crtc_state,const struct drm_connector_state * old_conn_state)273df0566a6SJani Nikula static void g4x_audio_codec_disable(struct intel_encoder *encoder,
274df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
275df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
276df0566a6SJani Nikula {
27746e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
278c3c5dc1dSVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
279df0566a6SJani Nikula 
280df0566a6SJani Nikula 	/* Invalidate ELD */
2817c8d74e8SVille Syrjälä 	intel_de_rmw(i915, G4X_AUD_CNTL_ST,
2827c8d74e8SVille Syrjälä 		     G4X_ELD_VALID, 0);
283c3c5dc1dSVille Syrjälä 
284c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
285c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
286df0566a6SJani Nikula }
287df0566a6SJani Nikula 
g4x_audio_codec_enable(struct intel_encoder * encoder,const struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)288df0566a6SJani Nikula static void g4x_audio_codec_enable(struct intel_encoder *encoder,
289df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
290df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
291df0566a6SJani Nikula {
29246e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
293c3c5dc1dSVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2945d986635SVille Syrjälä 	const u32 *eld = (const u32 *)crtc_state->eld;
2950234cda2SVille Syrjälä 	int eld_buffer_size, len, i;
296df0566a6SJani Nikula 
297c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
298c3c5dc1dSVille Syrjälä 
2997c8d74e8SVille Syrjälä 	intel_de_rmw(i915, G4X_AUD_CNTL_ST,
3007c8d74e8SVille Syrjälä 		     G4X_ELD_VALID | G4X_ELD_ADDRESS_MASK, 0);
301df0566a6SJani Nikula 
3020234cda2SVille Syrjälä 	eld_buffer_size = g4x_eld_buffer_size(i915);
3035d986635SVille Syrjälä 	len = min(drm_eld_size(crtc_state->eld) / 4, eld_buffer_size);
3041c0ab71aSVille Syrjälä 
305df0566a6SJani Nikula 	for (i = 0; i < len; i++)
30650a4a926SVille Syrjälä 		intel_de_write(i915, G4X_HDMIW_HDMIEDID, eld[i]);
3070234cda2SVille Syrjälä 	for (; i < eld_buffer_size; i++)
3080234cda2SVille Syrjälä 		intel_de_write(i915, G4X_HDMIW_HDMIEDID, 0);
3090234cda2SVille Syrjälä 
3100234cda2SVille Syrjälä 	drm_WARN_ON(&i915->drm,
3110234cda2SVille Syrjälä 		    (intel_de_read(i915, G4X_AUD_CNTL_ST) & G4X_ELD_ADDRESS_MASK) != 0);
312df0566a6SJani Nikula 
3137c8d74e8SVille Syrjälä 	intel_de_rmw(i915, G4X_AUD_CNTL_ST,
3147c8d74e8SVille Syrjälä 		     0, G4X_ELD_VALID);
315df0566a6SJani Nikula }
316df0566a6SJani Nikula 
317df0566a6SJani Nikula static void
hsw_dp_audio_config_update(struct intel_encoder * encoder,const struct intel_crtc_state * crtc_state)318df0566a6SJani Nikula hsw_dp_audio_config_update(struct intel_encoder *encoder,
319df0566a6SJani Nikula 			   const struct intel_crtc_state *crtc_state)
320df0566a6SJani Nikula {
32146e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
322df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
323df0566a6SJani Nikula 
324b0624c03SChaitanya Kumar Borah 	/* Enable time stamps. Let HW calculate Maud/Naud values */
325b0624c03SChaitanya Kumar Borah 	intel_de_rmw(i915, HSW_AUD_CFG(cpu_transcoder),
326b0624c03SChaitanya Kumar Borah 		     AUD_CONFIG_N_VALUE_INDEX |
327b0624c03SChaitanya Kumar Borah 		     AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK |
328b0624c03SChaitanya Kumar Borah 		     AUD_CONFIG_UPPER_N_MASK |
329b0624c03SChaitanya Kumar Borah 		     AUD_CONFIG_LOWER_N_MASK |
330b0624c03SChaitanya Kumar Borah 		     AUD_CONFIG_N_PROG_ENABLE,
331b0624c03SChaitanya Kumar Borah 		     AUD_CONFIG_N_VALUE_INDEX);
332df0566a6SJani Nikula 
333df0566a6SJani Nikula }
334df0566a6SJani Nikula 
335df0566a6SJani Nikula static void
hsw_hdmi_audio_config_update(struct intel_encoder * encoder,const struct intel_crtc_state * crtc_state)336df0566a6SJani Nikula hsw_hdmi_audio_config_update(struct intel_encoder *encoder,
337df0566a6SJani Nikula 			     const struct intel_crtc_state *crtc_state)
338df0566a6SJani Nikula {
33946e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
34046e61ee4SVille Syrjälä 	struct i915_audio_component *acomp = i915->display.audio.component;
341df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
342df0566a6SJani Nikula 	enum port port = encoder->port;
343df0566a6SJani Nikula 	int n, rate;
344df0566a6SJani Nikula 	u32 tmp;
345df0566a6SJani Nikula 
346df0566a6SJani Nikula 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
347df0566a6SJani Nikula 
34846e61ee4SVille Syrjälä 	tmp = intel_de_read(i915, HSW_AUD_CFG(cpu_transcoder));
349df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
350df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
351df0566a6SJani Nikula 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
352df0566a6SJani Nikula 	tmp |= audio_config_hdmi_pixel_clock(crtc_state);
353df0566a6SJani Nikula 
354df0566a6SJani Nikula 	n = audio_config_hdmi_get_n(crtc_state, rate);
355df0566a6SJani Nikula 	if (n != 0) {
35646e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "using N %d\n", n);
357df0566a6SJani Nikula 
358df0566a6SJani Nikula 		tmp &= ~AUD_CONFIG_N_MASK;
359df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N(n);
360df0566a6SJani Nikula 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
361df0566a6SJani Nikula 	} else {
36246e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "using automatic N\n");
363df0566a6SJani Nikula 	}
364df0566a6SJani Nikula 
36546e61ee4SVille Syrjälä 	intel_de_write(i915, HSW_AUD_CFG(cpu_transcoder), tmp);
366df0566a6SJani Nikula 
367df0566a6SJani Nikula 	/*
368df0566a6SJani Nikula 	 * Let's disable "Enable CTS or M Prog bit"
369df0566a6SJani Nikula 	 * and let HW calculate the value
370df0566a6SJani Nikula 	 */
37146e61ee4SVille Syrjälä 	tmp = intel_de_read(i915, HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
372df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
373df0566a6SJani Nikula 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
37446e61ee4SVille Syrjälä 	intel_de_write(i915, HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
375df0566a6SJani Nikula }
376df0566a6SJani Nikula 
377df0566a6SJani Nikula static void
hsw_audio_config_update(struct intel_encoder * encoder,const struct intel_crtc_state * crtc_state)378df0566a6SJani Nikula hsw_audio_config_update(struct intel_encoder *encoder,
379df0566a6SJani Nikula 			const struct intel_crtc_state *crtc_state)
380df0566a6SJani Nikula {
381df0566a6SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state))
382df0566a6SJani Nikula 		hsw_dp_audio_config_update(encoder, crtc_state);
383df0566a6SJani Nikula 	else
384df0566a6SJani Nikula 		hsw_hdmi_audio_config_update(encoder, crtc_state);
385df0566a6SJani Nikula }
386df0566a6SJani Nikula 
hsw_audio_codec_disable(struct intel_encoder * encoder,const struct intel_crtc_state * old_crtc_state,const struct drm_connector_state * old_conn_state)387df0566a6SJani Nikula static void hsw_audio_codec_disable(struct intel_encoder *encoder,
388df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
389df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
390df0566a6SJani Nikula {
39146e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
392c3c5dc1dSVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
393df0566a6SJani Nikula 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
394df0566a6SJani Nikula 
39546e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
396df0566a6SJani Nikula 
397df0566a6SJani Nikula 	/* Disable timestamps */
3987c8d74e8SVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_CFG(cpu_transcoder),
3997c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_VALUE_INDEX |
4007c8d74e8SVille Syrjälä 		     AUD_CONFIG_UPPER_N_MASK |
4017c8d74e8SVille Syrjälä 		     AUD_CONFIG_LOWER_N_MASK,
4027c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_PROG_ENABLE |
4037c8d74e8SVille Syrjälä 		     (intel_crtc_has_dp_encoder(old_crtc_state) ?
4047c8d74e8SVille Syrjälä 		      AUD_CONFIG_N_VALUE_INDEX : 0));
405df0566a6SJani Nikula 
406cbbda2ffSVille Syrjälä 	/* Invalidate ELD */
4077c8d74e8SVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_PIN_ELD_CP_VLD,
408cbbda2ffSVille Syrjälä 		     AUDIO_ELD_VALID(cpu_transcoder), 0);
409cbbda2ffSVille Syrjälä 
410c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
411c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
412c3c5dc1dSVille Syrjälä 
413cbbda2ffSVille Syrjälä 	/* Disable audio presence detect */
414cbbda2ffSVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_PIN_ELD_CP_VLD,
4157c8d74e8SVille Syrjälä 		     AUDIO_OUTPUT_ENABLE(cpu_transcoder), 0);
416df0566a6SJani Nikula 
41746e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
418df0566a6SJani Nikula }
419df0566a6SJani Nikula 
calc_hblank_early_prog(struct intel_encoder * encoder,const struct intel_crtc_state * crtc_state)4202dd43144SVille Syrjälä static unsigned int calc_hblank_early_prog(struct intel_encoder *encoder,
42148b8b04cSUma Shankar 					   const struct intel_crtc_state *crtc_state)
42248b8b04cSUma Shankar {
42348b8b04cSUma Shankar 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
42448b8b04cSUma Shankar 	unsigned int link_clks_available, link_clks_required;
42548b8b04cSUma Shankar 	unsigned int tu_data, tu_line, link_clks_active;
426d19b29beSVille Syrjälä 	unsigned int h_active, h_total, hblank_delta, pixel_clk;
427d19b29beSVille Syrjälä 	unsigned int fec_coeff, cdclk, vdsc_bpp;
42841ee86d6SVille Syrjälä 	unsigned int link_clk, lanes;
4292dd43144SVille Syrjälä 	unsigned int hblank_rise;
43048b8b04cSUma Shankar 
43148b8b04cSUma Shankar 	h_active = crtc_state->hw.adjusted_mode.crtc_hdisplay;
43248b8b04cSUma Shankar 	h_total = crtc_state->hw.adjusted_mode.crtc_htotal;
43348b8b04cSUma Shankar 	pixel_clk = crtc_state->hw.adjusted_mode.crtc_clock;
43448b8b04cSUma Shankar 	vdsc_bpp = crtc_state->dsc.compressed_bpp;
435d51309b4SJani Nikula 	cdclk = i915->display.cdclk.hw.cdclk;
43648b8b04cSUma Shankar 	/* fec= 0.972261, using rounding multiplier of 1000000 */
43748b8b04cSUma Shankar 	fec_coeff = 972261;
43841ee86d6SVille Syrjälä 	link_clk = crtc_state->port_clock;
43941ee86d6SVille Syrjälä 	lanes = crtc_state->lane_count;
44048b8b04cSUma Shankar 
44148b8b04cSUma Shankar 	drm_dbg_kms(&i915->drm, "h_active = %u link_clk = %u :"
44248b8b04cSUma Shankar 		    "lanes = %u vdsc_bpp = %u cdclk = %u\n",
44341ee86d6SVille Syrjälä 		    h_active, link_clk, lanes, vdsc_bpp, cdclk);
44448b8b04cSUma Shankar 
4452dd43144SVille Syrjälä 	if (WARN_ON(!link_clk || !pixel_clk || !lanes || !vdsc_bpp || !cdclk))
44611ebc232SJani Nikula 		return 0;
44711ebc232SJani Nikula 
4482dd43144SVille Syrjälä 	link_clks_available = (h_total - h_active) * link_clk / pixel_clk - 28;
4492dd43144SVille Syrjälä 	link_clks_required = DIV_ROUND_UP(192000 * h_total, 1000 * pixel_clk) * (48 / lanes + 2);
45048b8b04cSUma Shankar 
45148b8b04cSUma Shankar 	if (link_clks_available > link_clks_required)
45248b8b04cSUma Shankar 		hblank_delta = 32;
45348b8b04cSUma Shankar 	else
4542dd43144SVille Syrjälä 		hblank_delta = DIV64_U64_ROUND_UP(mul_u32_u32(5 * (link_clk + cdclk), pixel_clk),
4552dd43144SVille Syrjälä 						  mul_u32_u32(link_clk, cdclk));
45648b8b04cSUma Shankar 
4572dd43144SVille Syrjälä 	tu_data = div64_u64(mul_u32_u32(pixel_clk * vdsc_bpp * 8, 1000000),
4582dd43144SVille Syrjälä 			    mul_u32_u32(link_clk * lanes, fec_coeff));
4592dd43144SVille Syrjälä 	tu_line = div64_u64(h_active * mul_u32_u32(link_clk, fec_coeff),
4602dd43144SVille Syrjälä 			    mul_u32_u32(64 * pixel_clk, 1000000));
46148b8b04cSUma Shankar 	link_clks_active  = (tu_line - 1) * 64 + tu_data;
46248b8b04cSUma Shankar 
4632dd43144SVille Syrjälä 	hblank_rise = (link_clks_active + 6 * DIV_ROUND_UP(link_clks_active, 250) + 4) * pixel_clk / link_clk;
46448b8b04cSUma Shankar 
4652dd43144SVille Syrjälä 	return h_active - hblank_rise + hblank_delta;
46648b8b04cSUma Shankar }
46748b8b04cSUma Shankar 
calc_samples_room(const struct intel_crtc_state * crtc_state)4682dd43144SVille Syrjälä static unsigned int calc_samples_room(const struct intel_crtc_state *crtc_state)
46948b8b04cSUma Shankar {
47048b8b04cSUma Shankar 	unsigned int h_active, h_total, pixel_clk;
47141ee86d6SVille Syrjälä 	unsigned int link_clk, lanes;
47248b8b04cSUma Shankar 
47348b8b04cSUma Shankar 	h_active = crtc_state->hw.adjusted_mode.hdisplay;
47448b8b04cSUma Shankar 	h_total = crtc_state->hw.adjusted_mode.htotal;
47548b8b04cSUma Shankar 	pixel_clk = crtc_state->hw.adjusted_mode.clock;
47641ee86d6SVille Syrjälä 	link_clk = crtc_state->port_clock;
47741ee86d6SVille Syrjälä 	lanes = crtc_state->lane_count;
47848b8b04cSUma Shankar 
4792dd43144SVille Syrjälä 	return ((h_total - h_active) * link_clk - 12 * pixel_clk) /
4802dd43144SVille Syrjälä 		(pixel_clk * (48 / lanes + 2));
48148b8b04cSUma Shankar }
48248b8b04cSUma Shankar 
enable_audio_dsc_wa(struct intel_encoder * encoder,const struct intel_crtc_state * crtc_state)48348b8b04cSUma Shankar static void enable_audio_dsc_wa(struct intel_encoder *encoder,
48448b8b04cSUma Shankar 				const struct intel_crtc_state *crtc_state)
48548b8b04cSUma Shankar {
48648b8b04cSUma Shankar 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
4875eba7426SVille Syrjälä 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
48811ebc232SJani Nikula 	unsigned int hblank_early_prog, samples_room;
48948b8b04cSUma Shankar 	unsigned int val;
49048b8b04cSUma Shankar 
491005e9537SMatt Roper 	if (DISPLAY_VER(i915) < 11)
49248b8b04cSUma Shankar 		return;
49348b8b04cSUma Shankar 
49448b8b04cSUma Shankar 	val = intel_de_read(i915, AUD_CONFIG_BE);
49548b8b04cSUma Shankar 
49693e7e61eSLucas De Marchi 	if (DISPLAY_VER(i915) == 11)
4975eba7426SVille Syrjälä 		val |= HBLANK_EARLY_ENABLE_ICL(cpu_transcoder);
498005e9537SMatt Roper 	else if (DISPLAY_VER(i915) >= 12)
4995eba7426SVille Syrjälä 		val |= HBLANK_EARLY_ENABLE_TGL(cpu_transcoder);
50048b8b04cSUma Shankar 
50148b8b04cSUma Shankar 	if (crtc_state->dsc.compression_enable &&
50231824c03SJani Nikula 	    crtc_state->hw.adjusted_mode.hdisplay >= 3840 &&
50331824c03SJani Nikula 	    crtc_state->hw.adjusted_mode.vdisplay >= 2160) {
50448b8b04cSUma Shankar 		/* Get hblank early enable value required */
5055eba7426SVille Syrjälä 		val &= ~HBLANK_START_COUNT_MASK(cpu_transcoder);
5062dd43144SVille Syrjälä 		hblank_early_prog = calc_hblank_early_prog(encoder, crtc_state);
507f4c50deeSJani Nikula 		if (hblank_early_prog < 32)
5085eba7426SVille Syrjälä 			val |= HBLANK_START_COUNT(cpu_transcoder, HBLANK_START_COUNT_32);
509f4c50deeSJani Nikula 		else if (hblank_early_prog < 64)
5105eba7426SVille Syrjälä 			val |= HBLANK_START_COUNT(cpu_transcoder, HBLANK_START_COUNT_64);
511f4c50deeSJani Nikula 		else if (hblank_early_prog < 96)
5125eba7426SVille Syrjälä 			val |= HBLANK_START_COUNT(cpu_transcoder, HBLANK_START_COUNT_96);
513f4c50deeSJani Nikula 		else
5145eba7426SVille Syrjälä 			val |= HBLANK_START_COUNT(cpu_transcoder, HBLANK_START_COUNT_128);
51548b8b04cSUma Shankar 
51648b8b04cSUma Shankar 		/* Get samples room value required */
5175eba7426SVille Syrjälä 		val &= ~NUMBER_SAMPLES_PER_LINE_MASK(cpu_transcoder);
5182dd43144SVille Syrjälä 		samples_room = calc_samples_room(crtc_state);
519f4c50deeSJani Nikula 		if (samples_room < 3)
5205eba7426SVille Syrjälä 			val |= NUMBER_SAMPLES_PER_LINE(cpu_transcoder, samples_room);
521f4c50deeSJani Nikula 		else /* Program 0 i.e "All Samples available in buffer" */
5225eba7426SVille Syrjälä 			val |= NUMBER_SAMPLES_PER_LINE(cpu_transcoder, 0x0);
52348b8b04cSUma Shankar 	}
52448b8b04cSUma Shankar 
52548b8b04cSUma Shankar 	intel_de_write(i915, AUD_CONFIG_BE, val);
52648b8b04cSUma Shankar }
52748b8b04cSUma Shankar 
hsw_audio_codec_enable(struct intel_encoder * encoder,const struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)528df0566a6SJani Nikula static void hsw_audio_codec_enable(struct intel_encoder *encoder,
529df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
530df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
531df0566a6SJani Nikula {
53246e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
533c3c5dc1dSVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
534df0566a6SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
535df0566a6SJani Nikula 
53646e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
537df0566a6SJani Nikula 
53848b8b04cSUma Shankar 	/* Enable Audio WA for 4k DSC usecases */
53948b8b04cSUma Shankar 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP))
54048b8b04cSUma Shankar 		enable_audio_dsc_wa(encoder, crtc_state);
54148b8b04cSUma Shankar 
542cbbda2ffSVille Syrjälä 	/* Enable audio presence detect */
5437c8d74e8SVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_PIN_ELD_CP_VLD,
544cbbda2ffSVille Syrjälä 		     0, AUDIO_OUTPUT_ENABLE(cpu_transcoder));
545cbbda2ffSVille Syrjälä 
546c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
547c3c5dc1dSVille Syrjälä 
548cbbda2ffSVille Syrjälä 	/* Invalidate ELD */
549cbbda2ffSVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_PIN_ELD_CP_VLD,
550cbbda2ffSVille Syrjälä 		     AUDIO_ELD_VALID(cpu_transcoder), 0);
551df0566a6SJani Nikula 
55268470541SVille Syrjälä 	/*
55368470541SVille Syrjälä 	 * The audio componenent is used to convey the ELD
55468470541SVille Syrjälä 	 * instead using of the hardware ELD buffer.
55568470541SVille Syrjälä 	 */
556df0566a6SJani Nikula 
557df0566a6SJani Nikula 	/* Enable timestamps */
558df0566a6SJani Nikula 	hsw_audio_config_update(encoder, crtc_state);
559df0566a6SJani Nikula 
56046e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
561df0566a6SJani Nikula }
562df0566a6SJani Nikula 
5637e95cb09SVille Syrjälä struct ibx_audio_regs {
564669d7fd6SVille Syrjälä 	i915_reg_t hdmiw_hdmiedid, aud_config, aud_cntl_st, aud_cntrl_st2;
565669d7fd6SVille Syrjälä };
566669d7fd6SVille Syrjälä 
ibx_audio_regs_init(struct drm_i915_private * i915,enum pipe pipe,struct ibx_audio_regs * regs)5677e95cb09SVille Syrjälä static void ibx_audio_regs_init(struct drm_i915_private *i915,
568669d7fd6SVille Syrjälä 				enum pipe pipe,
5697e95cb09SVille Syrjälä 				struct ibx_audio_regs *regs)
570669d7fd6SVille Syrjälä {
57131395fbaSVille Syrjälä 	if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) {
572669d7fd6SVille Syrjälä 		regs->hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
573669d7fd6SVille Syrjälä 		regs->aud_config = VLV_AUD_CFG(pipe);
574669d7fd6SVille Syrjälä 		regs->aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
575669d7fd6SVille Syrjälä 		regs->aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
57631395fbaSVille Syrjälä 	} else if (HAS_PCH_CPT(i915)) {
577669d7fd6SVille Syrjälä 		regs->hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
578669d7fd6SVille Syrjälä 		regs->aud_config = CPT_AUD_CFG(pipe);
579669d7fd6SVille Syrjälä 		regs->aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
580669d7fd6SVille Syrjälä 		regs->aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
58131395fbaSVille Syrjälä 	} else if (HAS_PCH_IBX(i915)) {
58231395fbaSVille Syrjälä 		regs->hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
58331395fbaSVille Syrjälä 		regs->aud_config = IBX_AUD_CFG(pipe);
58431395fbaSVille Syrjälä 		regs->aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
58531395fbaSVille Syrjälä 		regs->aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
586669d7fd6SVille Syrjälä 	}
587669d7fd6SVille Syrjälä }
588669d7fd6SVille Syrjälä 
ibx_audio_codec_disable(struct intel_encoder * encoder,const struct intel_crtc_state * old_crtc_state,const struct drm_connector_state * old_conn_state)5897e95cb09SVille Syrjälä static void ibx_audio_codec_disable(struct intel_encoder *encoder,
590df0566a6SJani Nikula 				    const struct intel_crtc_state *old_crtc_state,
591df0566a6SJani Nikula 				    const struct drm_connector_state *old_conn_state)
592df0566a6SJani Nikula {
59346e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
5942225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
595df0566a6SJani Nikula 	enum port port = encoder->port;
5967c8d74e8SVille Syrjälä 	enum pipe pipe = crtc->pipe;
5977e95cb09SVille Syrjälä 	struct ibx_audio_regs regs;
598df0566a6SJani Nikula 
59946e61ee4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, port == PORT_A))
600df0566a6SJani Nikula 		return;
601df0566a6SJani Nikula 
6027e95cb09SVille Syrjälä 	ibx_audio_regs_init(i915, pipe, &regs);
603df0566a6SJani Nikula 
6049f4a5125SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
6059f4a5125SVille Syrjälä 
606df0566a6SJani Nikula 	/* Disable timestamps */
6077c8d74e8SVille Syrjälä 	intel_de_rmw(i915, regs.aud_config,
6087c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_VALUE_INDEX |
6097c8d74e8SVille Syrjälä 		     AUD_CONFIG_UPPER_N_MASK |
6107c8d74e8SVille Syrjälä 		     AUD_CONFIG_LOWER_N_MASK,
6117c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_PROG_ENABLE |
6127c8d74e8SVille Syrjälä 		     (intel_crtc_has_dp_encoder(old_crtc_state) ?
6137c8d74e8SVille Syrjälä 		      AUD_CONFIG_N_VALUE_INDEX : 0));
614df0566a6SJani Nikula 
615df0566a6SJani Nikula 	/* Invalidate ELD */
6167c8d74e8SVille Syrjälä 	intel_de_rmw(i915, regs.aud_cntrl_st2,
6177c8d74e8SVille Syrjälä 		     IBX_ELD_VALID(port), 0);
6189f4a5125SVille Syrjälä 
6199f4a5125SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
620c3c5dc1dSVille Syrjälä 
621c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
622c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
623df0566a6SJani Nikula }
624df0566a6SJani Nikula 
ibx_audio_codec_enable(struct intel_encoder * encoder,const struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)6257e95cb09SVille Syrjälä static void ibx_audio_codec_enable(struct intel_encoder *encoder,
626df0566a6SJani Nikula 				   const struct intel_crtc_state *crtc_state,
627df0566a6SJani Nikula 				   const struct drm_connector_state *conn_state)
628df0566a6SJani Nikula {
62946e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
6302225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
631df0566a6SJani Nikula 	enum port port = encoder->port;
6327c8d74e8SVille Syrjälä 	enum pipe pipe = crtc->pipe;
6337e95cb09SVille Syrjälä 	struct ibx_audio_regs regs;
634df0566a6SJani Nikula 
63546e61ee4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, port == PORT_A))
636df0566a6SJani Nikula 		return;
637df0566a6SJani Nikula 
638c3c5dc1dSVille Syrjälä 	intel_crtc_wait_for_next_vblank(crtc);
639df0566a6SJani Nikula 
6407e95cb09SVille Syrjälä 	ibx_audio_regs_init(i915, pipe, &regs);
641df0566a6SJani Nikula 
6429f4a5125SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
643df0566a6SJani Nikula 
644df0566a6SJani Nikula 	/* Invalidate ELD */
6457c8d74e8SVille Syrjälä 	intel_de_rmw(i915, regs.aud_cntrl_st2,
6467c8d74e8SVille Syrjälä 		     IBX_ELD_VALID(port), 0);
647df0566a6SJani Nikula 
648343cb0f9SVille Syrjälä 	/*
649343cb0f9SVille Syrjälä 	 * The audio componenent is used to convey the ELD
650343cb0f9SVille Syrjälä 	 * instead using of the hardware ELD buffer.
651343cb0f9SVille Syrjälä 	 */
652df0566a6SJani Nikula 
653df0566a6SJani Nikula 	/* Enable timestamps */
6547c8d74e8SVille Syrjälä 	intel_de_rmw(i915, regs.aud_config,
6557c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_VALUE_INDEX |
6567c8d74e8SVille Syrjälä 		     AUD_CONFIG_N_PROG_ENABLE |
6577c8d74e8SVille Syrjälä 		     AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK,
6587c8d74e8SVille Syrjälä 		     (intel_crtc_has_dp_encoder(crtc_state) ?
6597c8d74e8SVille Syrjälä 		      AUD_CONFIG_N_VALUE_INDEX :
6607c8d74e8SVille Syrjälä 		      audio_config_hdmi_pixel_clock(crtc_state)));
6619f4a5125SVille Syrjälä 
6629f4a5125SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
663df0566a6SJani Nikula }
664df0566a6SJani Nikula 
intel_audio_sdp_split_update(struct intel_encoder * encoder,const struct intel_crtc_state * crtc_state)6658853750dSVinod Govindapillai void intel_audio_sdp_split_update(struct intel_encoder *encoder,
6668853750dSVinod Govindapillai 				  const struct intel_crtc_state *crtc_state)
6678853750dSVinod Govindapillai {
6688853750dSVinod Govindapillai 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
6698853750dSVinod Govindapillai 	enum transcoder trans = crtc_state->cpu_transcoder;
6708853750dSVinod Govindapillai 
6718853750dSVinod Govindapillai 	if (HAS_DP20(i915))
6728853750dSVinod Govindapillai 		intel_de_rmw(i915, AUD_DP_2DOT0_CTRL(trans), AUD_ENABLE_SDP_SPLIT,
6738853750dSVinod Govindapillai 			     crtc_state->sdp_split_enable ? AUD_ENABLE_SDP_SPLIT : 0);
6748853750dSVinod Govindapillai }
6758853750dSVinod Govindapillai 
intel_audio_compute_config(struct intel_encoder * encoder,struct intel_crtc_state * crtc_state,struct drm_connector_state * conn_state)6765d986635SVille Syrjälä bool intel_audio_compute_config(struct intel_encoder *encoder,
6775d986635SVille Syrjälä 				struct intel_crtc_state *crtc_state,
6785d986635SVille Syrjälä 				struct drm_connector_state *conn_state)
6795d986635SVille Syrjälä {
6805d986635SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
6815d986635SVille Syrjälä 	struct drm_connector *connector = conn_state->connector;
6825d986635SVille Syrjälä 	const struct drm_display_mode *adjusted_mode =
6835d986635SVille Syrjälä 		&crtc_state->hw.adjusted_mode;
6845d986635SVille Syrjälä 
685d6393793SVille Syrjälä 	if (!connector->eld[0]) {
6865d986635SVille Syrjälä 		drm_dbg_kms(&i915->drm,
6875d986635SVille Syrjälä 			    "Bogus ELD on [CONNECTOR:%d:%s]\n",
6885d986635SVille Syrjälä 			    connector->base.id, connector->name);
689d6393793SVille Syrjälä 		return false;
690d6393793SVille Syrjälä 	}
6915d986635SVille Syrjälä 
6925d986635SVille Syrjälä 	BUILD_BUG_ON(sizeof(crtc_state->eld) != sizeof(connector->eld));
6935d986635SVille Syrjälä 	memcpy(crtc_state->eld, connector->eld, sizeof(crtc_state->eld));
6945d986635SVille Syrjälä 
6955d986635SVille Syrjälä 	crtc_state->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
6965d986635SVille Syrjälä 
6975d986635SVille Syrjälä 	return true;
6985d986635SVille Syrjälä }
6995d986635SVille Syrjälä 
700df0566a6SJani Nikula /**
701df0566a6SJani Nikula  * intel_audio_codec_enable - Enable the audio codec for HD audio
702df0566a6SJani Nikula  * @encoder: encoder on which to enable audio
703df0566a6SJani Nikula  * @crtc_state: pointer to the current crtc state.
704df0566a6SJani Nikula  * @conn_state: pointer to the current connector state.
705df0566a6SJani Nikula  *
706df0566a6SJani Nikula  * The enable sequences may only be performed after enabling the transcoder and
707df0566a6SJani Nikula  * port, and after completed link training.
708df0566a6SJani Nikula  */
intel_audio_codec_enable(struct intel_encoder * encoder,const struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)709df0566a6SJani Nikula void intel_audio_codec_enable(struct intel_encoder *encoder,
710df0566a6SJani Nikula 			      const struct intel_crtc_state *crtc_state,
711df0566a6SJani Nikula 			      const struct drm_connector_state *conn_state)
712df0566a6SJani Nikula {
71346e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
71446e61ee4SVille Syrjälä 	struct i915_audio_component *acomp = i915->display.audio.component;
7152225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
716c7104c38SVille Syrjälä 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
7175eba7426SVille Syrjälä 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
718c7104c38SVille Syrjälä 	struct intel_audio_state *audio_state;
719df0566a6SJani Nikula 	enum port port = encoder->port;
720df0566a6SJani Nikula 
721179db7c1SJani Nikula 	if (!crtc_state->has_audio)
722179db7c1SJani Nikula 		return;
723179db7c1SJani Nikula 
724c7104c38SVille Syrjälä 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s][ENCODER:%d:%s] Enable audio codec on [CRTC:%d:%s], %u bytes ELD\n",
725c7104c38SVille Syrjälä 		    connector->base.base.id, connector->base.name,
7261f31e35fSJani Nikula 		    encoder->base.base.id, encoder->base.name,
727c7104c38SVille Syrjälä 		    crtc->base.base.id, crtc->base.name,
7285d986635SVille Syrjälä 		    drm_eld_size(crtc_state->eld));
729df0566a6SJani Nikula 
73046e61ee4SVille Syrjälä 	if (i915->display.funcs.audio)
73146e61ee4SVille Syrjälä 		i915->display.funcs.audio->audio_codec_enable(encoder,
732df0566a6SJani Nikula 							      crtc_state,
733df0566a6SJani Nikula 							      conn_state);
734df0566a6SJani Nikula 
73546e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
736df0566a6SJani Nikula 
7375eba7426SVille Syrjälä 	audio_state = &i915->display.audio.state[cpu_transcoder];
738c7104c38SVille Syrjälä 
739c7104c38SVille Syrjälä 	audio_state->encoder = encoder;
7405d986635SVille Syrjälä 	BUILD_BUG_ON(sizeof(audio_state->eld) != sizeof(crtc_state->eld));
7415d986635SVille Syrjälä 	memcpy(audio_state->eld, crtc_state->eld, sizeof(audio_state->eld));
742c7104c38SVille Syrjälä 
74346e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
744df0566a6SJani Nikula 
745df0566a6SJani Nikula 	if (acomp && acomp->base.audio_ops &&
746df0566a6SJani Nikula 	    acomp->base.audio_ops->pin_eld_notify) {
7475eba7426SVille Syrjälä 		/* audio drivers expect cpu_transcoder = -1 to indicate Non-MST cases */
748df0566a6SJani Nikula 		if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
7495eba7426SVille Syrjälä 			cpu_transcoder = -1;
750df0566a6SJani Nikula 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
7515eba7426SVille Syrjälä 						      (int)port, (int)cpu_transcoder);
752df0566a6SJani Nikula 	}
753df0566a6SJani Nikula 
7545eba7426SVille Syrjälä 	intel_lpe_audio_notify(i915, cpu_transcoder, port, crtc_state->eld,
755df0566a6SJani Nikula 			       crtc_state->port_clock,
756df0566a6SJani Nikula 			       intel_crtc_has_dp_encoder(crtc_state));
757df0566a6SJani Nikula }
758df0566a6SJani Nikula 
759df0566a6SJani Nikula /**
760df0566a6SJani Nikula  * intel_audio_codec_disable - Disable the audio codec for HD audio
761df0566a6SJani Nikula  * @encoder: encoder on which to disable audio
762df0566a6SJani Nikula  * @old_crtc_state: pointer to the old crtc state.
763df0566a6SJani Nikula  * @old_conn_state: pointer to the old connector state.
764df0566a6SJani Nikula  *
765df0566a6SJani Nikula  * The disable sequences must be performed before disabling the transcoder or
766df0566a6SJani Nikula  * port.
767df0566a6SJani Nikula  */
intel_audio_codec_disable(struct intel_encoder * encoder,const struct intel_crtc_state * old_crtc_state,const struct drm_connector_state * old_conn_state)768df0566a6SJani Nikula void intel_audio_codec_disable(struct intel_encoder *encoder,
769df0566a6SJani Nikula 			       const struct intel_crtc_state *old_crtc_state,
770df0566a6SJani Nikula 			       const struct drm_connector_state *old_conn_state)
771df0566a6SJani Nikula {
77246e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
77346e61ee4SVille Syrjälä 	struct i915_audio_component *acomp = i915->display.audio.component;
7742225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
775c7104c38SVille Syrjälä 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
7765eba7426SVille Syrjälä 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
777c7104c38SVille Syrjälä 	struct intel_audio_state *audio_state;
778df0566a6SJani Nikula 	enum port port = encoder->port;
779df0566a6SJani Nikula 
780179db7c1SJani Nikula 	if (!old_crtc_state->has_audio)
781179db7c1SJani Nikula 		return;
782179db7c1SJani Nikula 
783c7104c38SVille Syrjälä 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s][ENCODER:%d:%s] Disable audio codec on [CRTC:%d:%s]\n",
784c7104c38SVille Syrjälä 		    connector->base.base.id, connector->base.name,
785c7104c38SVille Syrjälä 		    encoder->base.base.id, encoder->base.name,
786c7104c38SVille Syrjälä 		    crtc->base.base.id, crtc->base.name);
7871f31e35fSJani Nikula 
78846e61ee4SVille Syrjälä 	if (i915->display.funcs.audio)
78946e61ee4SVille Syrjälä 		i915->display.funcs.audio->audio_codec_disable(encoder,
790df0566a6SJani Nikula 							       old_crtc_state,
791df0566a6SJani Nikula 							       old_conn_state);
792df0566a6SJani Nikula 
79346e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
794c7104c38SVille Syrjälä 
7955eba7426SVille Syrjälä 	audio_state = &i915->display.audio.state[cpu_transcoder];
796c7104c38SVille Syrjälä 
797c7104c38SVille Syrjälä 	audio_state->encoder = NULL;
7985d986635SVille Syrjälä 	memset(audio_state->eld, 0, sizeof(audio_state->eld));
799c7104c38SVille Syrjälä 
80046e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
801df0566a6SJani Nikula 
802df0566a6SJani Nikula 	if (acomp && acomp->base.audio_ops &&
803df0566a6SJani Nikula 	    acomp->base.audio_ops->pin_eld_notify) {
8045eba7426SVille Syrjälä 		/* audio drivers expect cpu_transcoder = -1 to indicate Non-MST cases */
805df0566a6SJani Nikula 		if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
8065eba7426SVille Syrjälä 			cpu_transcoder = -1;
807df0566a6SJani Nikula 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
8085eba7426SVille Syrjälä 						      (int)port, (int)cpu_transcoder);
809df0566a6SJani Nikula 	}
810df0566a6SJani Nikula 
8115eba7426SVille Syrjälä 	intel_lpe_audio_notify(i915, cpu_transcoder, port, NULL, 0, false);
812df0566a6SJani Nikula }
813df0566a6SJani Nikula 
intel_acomp_get_config(struct intel_encoder * encoder,struct intel_crtc_state * crtc_state)81461a60df6SVille Syrjälä static void intel_acomp_get_config(struct intel_encoder *encoder,
81561a60df6SVille Syrjälä 				   struct intel_crtc_state *crtc_state)
81661a60df6SVille Syrjälä {
81761a60df6SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
8185eba7426SVille Syrjälä 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
81961a60df6SVille Syrjälä 	struct intel_audio_state *audio_state;
82061a60df6SVille Syrjälä 
82161a60df6SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
82261a60df6SVille Syrjälä 
8235eba7426SVille Syrjälä 	audio_state = &i915->display.audio.state[cpu_transcoder];
82461a60df6SVille Syrjälä 
82561a60df6SVille Syrjälä 	if (audio_state->encoder)
82661a60df6SVille Syrjälä 		memcpy(crtc_state->eld, audio_state->eld, sizeof(audio_state->eld));
82761a60df6SVille Syrjälä 
82861a60df6SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
82961a60df6SVille Syrjälä }
83061a60df6SVille Syrjälä 
intel_audio_codec_get_config(struct intel_encoder * encoder,struct intel_crtc_state * crtc_state)83161a60df6SVille Syrjälä void intel_audio_codec_get_config(struct intel_encoder *encoder,
83261a60df6SVille Syrjälä 				  struct intel_crtc_state *crtc_state)
83361a60df6SVille Syrjälä {
83461a60df6SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
83561a60df6SVille Syrjälä 
83661a60df6SVille Syrjälä 	if (!crtc_state->has_audio)
83761a60df6SVille Syrjälä 		return;
83861a60df6SVille Syrjälä 
83961a60df6SVille Syrjälä 	if (i915->display.funcs.audio)
84061a60df6SVille Syrjälä 		i915->display.funcs.audio->audio_codec_get_config(encoder, crtc_state);
84161a60df6SVille Syrjälä }
84261a60df6SVille Syrjälä 
8430a108bcaSDave Airlie static const struct intel_audio_funcs g4x_audio_funcs = {
8440a108bcaSDave Airlie 	.audio_codec_enable = g4x_audio_codec_enable,
8450a108bcaSDave Airlie 	.audio_codec_disable = g4x_audio_codec_disable,
84661a60df6SVille Syrjälä 	.audio_codec_get_config = g4x_audio_codec_get_config,
8470a108bcaSDave Airlie };
8480a108bcaSDave Airlie 
8497e95cb09SVille Syrjälä static const struct intel_audio_funcs ibx_audio_funcs = {
8507e95cb09SVille Syrjälä 	.audio_codec_enable = ibx_audio_codec_enable,
8517e95cb09SVille Syrjälä 	.audio_codec_disable = ibx_audio_codec_disable,
85261a60df6SVille Syrjälä 	.audio_codec_get_config = intel_acomp_get_config,
8530a108bcaSDave Airlie };
8540a108bcaSDave Airlie 
8550a108bcaSDave Airlie static const struct intel_audio_funcs hsw_audio_funcs = {
8560a108bcaSDave Airlie 	.audio_codec_enable = hsw_audio_codec_enable,
8570a108bcaSDave Airlie 	.audio_codec_disable = hsw_audio_codec_disable,
85861a60df6SVille Syrjälä 	.audio_codec_get_config = intel_acomp_get_config,
8590a108bcaSDave Airlie };
8600a108bcaSDave Airlie 
861df0566a6SJani Nikula /**
862f47a0e35SJani Nikula  * intel_audio_hooks_init - Set up chip specific audio hooks
86346e61ee4SVille Syrjälä  * @i915: device private
864df0566a6SJani Nikula  */
intel_audio_hooks_init(struct drm_i915_private * i915)86546e61ee4SVille Syrjälä void intel_audio_hooks_init(struct drm_i915_private *i915)
866df0566a6SJani Nikula {
86746e61ee4SVille Syrjälä 	if (IS_G4X(i915))
86846e61ee4SVille Syrjälä 		i915->display.funcs.audio = &g4x_audio_funcs;
86931395fbaSVille Syrjälä 	else if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915) ||
87031395fbaSVille Syrjälä 		 HAS_PCH_CPT(i915) || HAS_PCH_IBX(i915))
8717e95cb09SVille Syrjälä 		i915->display.funcs.audio = &ibx_audio_funcs;
87246e61ee4SVille Syrjälä 	else if (IS_HASWELL(i915) || DISPLAY_VER(i915) >= 8)
87346e61ee4SVille Syrjälä 		i915->display.funcs.audio = &hsw_audio_funcs;
874df0566a6SJani Nikula }
875df0566a6SJani Nikula 
876112a87c4SKai Vehmanen struct aud_ts_cdclk_m_n {
877112a87c4SKai Vehmanen 	u8 m;
878112a87c4SKai Vehmanen 	u16 n;
879112a87c4SKai Vehmanen };
880112a87c4SKai Vehmanen 
intel_audio_cdclk_change_pre(struct drm_i915_private * i915)881112a87c4SKai Vehmanen void intel_audio_cdclk_change_pre(struct drm_i915_private *i915)
882112a87c4SKai Vehmanen {
883112a87c4SKai Vehmanen 	if (DISPLAY_VER(i915) >= 13)
884112a87c4SKai Vehmanen 		intel_de_rmw(i915, AUD_TS_CDCLK_M, AUD_TS_CDCLK_M_EN, 0);
885112a87c4SKai Vehmanen }
886112a87c4SKai Vehmanen 
get_aud_ts_cdclk_m_n(int refclk,int cdclk,struct aud_ts_cdclk_m_n * aud_ts)887112a87c4SKai Vehmanen static void get_aud_ts_cdclk_m_n(int refclk, int cdclk, struct aud_ts_cdclk_m_n *aud_ts)
888112a87c4SKai Vehmanen {
889091496e6SClint Taylor 	aud_ts->m = 60;
890112a87c4SKai Vehmanen 	aud_ts->n = cdclk * aud_ts->m / 24000;
891112a87c4SKai Vehmanen }
892112a87c4SKai Vehmanen 
intel_audio_cdclk_change_post(struct drm_i915_private * i915)893112a87c4SKai Vehmanen void intel_audio_cdclk_change_post(struct drm_i915_private *i915)
894112a87c4SKai Vehmanen {
895112a87c4SKai Vehmanen 	struct aud_ts_cdclk_m_n aud_ts;
896112a87c4SKai Vehmanen 
897112a87c4SKai Vehmanen 	if (DISPLAY_VER(i915) >= 13) {
898d51309b4SJani Nikula 		get_aud_ts_cdclk_m_n(i915->display.cdclk.hw.ref, i915->display.cdclk.hw.cdclk, &aud_ts);
899112a87c4SKai Vehmanen 
900112a87c4SKai Vehmanen 		intel_de_write(i915, AUD_TS_CDCLK_N, aud_ts.n);
901112a87c4SKai Vehmanen 		intel_de_write(i915, AUD_TS_CDCLK_M, aud_ts.m | AUD_TS_CDCLK_M_EN);
902112a87c4SKai Vehmanen 		drm_dbg_kms(&i915->drm, "aud_ts_cdclk set to M=%u, N=%u\n", aud_ts.m, aud_ts.n);
903112a87c4SKai Vehmanen 	}
904112a87c4SKai Vehmanen }
905112a87c4SKai Vehmanen 
glk_force_audio_cdclk_commit(struct intel_atomic_state * state,struct intel_crtc * crtc,bool enable)90628a30b45SVille Syrjälä static int glk_force_audio_cdclk_commit(struct intel_atomic_state *state,
90708e3ed3aSChris Wilson 					struct intel_crtc *crtc,
90828a30b45SVille Syrjälä 					bool enable)
90928a30b45SVille Syrjälä {
91028a30b45SVille Syrjälä 	struct intel_cdclk_state *cdclk_state;
91128a30b45SVille Syrjälä 	int ret;
91228a30b45SVille Syrjälä 
91328a30b45SVille Syrjälä 	/* need to hold at least one crtc lock for the global state */
91428a30b45SVille Syrjälä 	ret = drm_modeset_lock(&crtc->base.mutex, state->base.acquire_ctx);
91528a30b45SVille Syrjälä 	if (ret)
91628a30b45SVille Syrjälä 		return ret;
91728a30b45SVille Syrjälä 
91828a30b45SVille Syrjälä 	cdclk_state = intel_atomic_get_cdclk_state(state);
91928a30b45SVille Syrjälä 	if (IS_ERR(cdclk_state))
92028a30b45SVille Syrjälä 		return PTR_ERR(cdclk_state);
92128a30b45SVille Syrjälä 
92228a30b45SVille Syrjälä 	cdclk_state->force_min_cdclk = enable ? 2 * 96000 : 0;
92328a30b45SVille Syrjälä 
92428a30b45SVille Syrjälä 	return drm_atomic_commit(&state->base);
92528a30b45SVille Syrjälä }
92628a30b45SVille Syrjälä 
glk_force_audio_cdclk(struct drm_i915_private * i915,bool enable)92746e61ee4SVille Syrjälä static void glk_force_audio_cdclk(struct drm_i915_private *i915,
928df0566a6SJani Nikula 				  bool enable)
929df0566a6SJani Nikula {
930df0566a6SJani Nikula 	struct drm_modeset_acquire_ctx ctx;
931df0566a6SJani Nikula 	struct drm_atomic_state *state;
93208e3ed3aSChris Wilson 	struct intel_crtc *crtc;
933df0566a6SJani Nikula 	int ret;
934df0566a6SJani Nikula 
93546e61ee4SVille Syrjälä 	crtc = intel_first_crtc(i915);
93608e3ed3aSChris Wilson 	if (!crtc)
93708e3ed3aSChris Wilson 		return;
93808e3ed3aSChris Wilson 
939df0566a6SJani Nikula 	drm_modeset_acquire_init(&ctx, 0);
94046e61ee4SVille Syrjälä 	state = drm_atomic_state_alloc(&i915->drm);
94146e61ee4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !state))
942df0566a6SJani Nikula 		return;
943df0566a6SJani Nikula 
944df0566a6SJani Nikula 	state->acquire_ctx = &ctx;
94576ec6927SVille Syrjälä 	to_intel_atomic_state(state)->internal = true;
946df0566a6SJani Nikula 
947df0566a6SJani Nikula retry:
94808e3ed3aSChris Wilson 	ret = glk_force_audio_cdclk_commit(to_intel_atomic_state(state), crtc,
94908e3ed3aSChris Wilson 					   enable);
950df0566a6SJani Nikula 	if (ret == -EDEADLK) {
951df0566a6SJani Nikula 		drm_atomic_state_clear(state);
952df0566a6SJani Nikula 		drm_modeset_backoff(&ctx);
953df0566a6SJani Nikula 		goto retry;
954df0566a6SJani Nikula 	}
955df0566a6SJani Nikula 
95646e61ee4SVille Syrjälä 	drm_WARN_ON(&i915->drm, ret);
957df0566a6SJani Nikula 
958df0566a6SJani Nikula 	drm_atomic_state_put(state);
959df0566a6SJani Nikula 
960df0566a6SJani Nikula 	drm_modeset_drop_locks(&ctx);
961df0566a6SJani Nikula 	drm_modeset_acquire_fini(&ctx);
962df0566a6SJani Nikula }
963df0566a6SJani Nikula 
i915_audio_component_get_power(struct device * kdev)964df0566a6SJani Nikula static unsigned long i915_audio_component_get_power(struct device *kdev)
965df0566a6SJani Nikula {
96646e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
967df0566a6SJani Nikula 	intel_wakeref_t ret;
968df0566a6SJani Nikula 
969df0566a6SJani Nikula 	/* Catch potential impedance mismatches before they occur! */
970df0566a6SJani Nikula 	BUILD_BUG_ON(sizeof(intel_wakeref_t) > sizeof(unsigned long));
971df0566a6SJani Nikula 
97246e61ee4SVille Syrjälä 	ret = intel_display_power_get(i915, POWER_DOMAIN_AUDIO_PLAYBACK);
973df0566a6SJani Nikula 
97446e61ee4SVille Syrjälä 	if (i915->display.audio.power_refcount++ == 0) {
97546e61ee4SVille Syrjälä 		if (DISPLAY_VER(i915) >= 9) {
97646e61ee4SVille Syrjälä 			intel_de_write(i915, AUD_FREQ_CNTRL,
97746e61ee4SVille Syrjälä 				       i915->display.audio.freq_cntrl);
97846e61ee4SVille Syrjälä 			drm_dbg_kms(&i915->drm,
97963855149SWambui Karuga 				    "restored AUD_FREQ_CNTRL to 0x%x\n",
98046e61ee4SVille Syrjälä 				    i915->display.audio.freq_cntrl);
98187c16945SKai Vehmanen 		}
98287c16945SKai Vehmanen 
98387c16945SKai Vehmanen 		/* Force CDCLK to 2*BCLK as long as we need audio powered. */
98446e61ee4SVille Syrjälä 		if (IS_GEMINILAKE(i915))
98546e61ee4SVille Syrjälä 			glk_force_audio_cdclk(i915, true);
9861580d3cdSKai Vehmanen 
98746e61ee4SVille Syrjälä 		if (DISPLAY_VER(i915) >= 10)
9887c8d74e8SVille Syrjälä 			intel_de_rmw(i915, AUD_PIN_BUF_CTL,
9897c8d74e8SVille Syrjälä 				     0, AUD_PIN_BUF_ENABLE);
99087c16945SKai Vehmanen 	}
991df0566a6SJani Nikula 
992df0566a6SJani Nikula 	return ret;
993df0566a6SJani Nikula }
994df0566a6SJani Nikula 
i915_audio_component_put_power(struct device * kdev,unsigned long cookie)995df0566a6SJani Nikula static void i915_audio_component_put_power(struct device *kdev,
996df0566a6SJani Nikula 					   unsigned long cookie)
997df0566a6SJani Nikula {
99846e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
999df0566a6SJani Nikula 
1000df0566a6SJani Nikula 	/* Stop forcing CDCLK to 2*BCLK if no need for audio to be powered. */
100146e61ee4SVille Syrjälä 	if (--i915->display.audio.power_refcount == 0)
100246e61ee4SVille Syrjälä 		if (IS_GEMINILAKE(i915))
100346e61ee4SVille Syrjälä 			glk_force_audio_cdclk(i915, false);
1004df0566a6SJani Nikula 
100546e61ee4SVille Syrjälä 	intel_display_power_put(i915, POWER_DOMAIN_AUDIO_PLAYBACK, cookie);
1006df0566a6SJani Nikula }
1007df0566a6SJani Nikula 
i915_audio_component_codec_wake_override(struct device * kdev,bool enable)1008df0566a6SJani Nikula static void i915_audio_component_codec_wake_override(struct device *kdev,
1009df0566a6SJani Nikula 						     bool enable)
1010df0566a6SJani Nikula {
101146e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1012df0566a6SJani Nikula 	unsigned long cookie;
1013df0566a6SJani Nikula 
101446e61ee4SVille Syrjälä 	if (DISPLAY_VER(i915) < 9)
1015df0566a6SJani Nikula 		return;
1016df0566a6SJani Nikula 
1017df0566a6SJani Nikula 	cookie = i915_audio_component_get_power(kdev);
1018df0566a6SJani Nikula 
1019df0566a6SJani Nikula 	/*
1020df0566a6SJani Nikula 	 * Enable/disable generating the codec wake signal, overriding the
1021df0566a6SJani Nikula 	 * internal logic to generate the codec wake to controller.
1022df0566a6SJani Nikula 	 */
10237c8d74e8SVille Syrjälä 	intel_de_rmw(i915, HSW_AUD_CHICKENBIT,
10247c8d74e8SVille Syrjälä 		     SKL_AUD_CODEC_WAKE_SIGNAL, 0);
1025df0566a6SJani Nikula 	usleep_range(1000, 1500);
1026df0566a6SJani Nikula 
1027df0566a6SJani Nikula 	if (enable) {
10287c8d74e8SVille Syrjälä 		intel_de_rmw(i915, HSW_AUD_CHICKENBIT,
10297c8d74e8SVille Syrjälä 			     0, SKL_AUD_CODEC_WAKE_SIGNAL);
1030df0566a6SJani Nikula 		usleep_range(1000, 1500);
1031df0566a6SJani Nikula 	}
1032df0566a6SJani Nikula 
1033df0566a6SJani Nikula 	i915_audio_component_put_power(kdev, cookie);
1034df0566a6SJani Nikula }
1035df0566a6SJani Nikula 
1036df0566a6SJani Nikula /* Get CDCLK in kHz  */
i915_audio_component_get_cdclk_freq(struct device * kdev)1037df0566a6SJani Nikula static int i915_audio_component_get_cdclk_freq(struct device *kdev)
1038df0566a6SJani Nikula {
103946e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1040df0566a6SJani Nikula 
104146e61ee4SVille Syrjälä 	if (drm_WARN_ON_ONCE(&i915->drm, !HAS_DDI(i915)))
1042df0566a6SJani Nikula 		return -ENODEV;
1043df0566a6SJani Nikula 
104446e61ee4SVille Syrjälä 	return i915->display.cdclk.hw.cdclk;
1045df0566a6SJani Nikula }
1046df0566a6SJani Nikula 
1047df0566a6SJani Nikula /*
10485eba7426SVille Syrjälä  * get the intel audio state according to the parameter port and cpu_transcoder
10495eba7426SVille Syrjälä  * MST & (cpu_transcoder >= 0): return the audio.state[cpu_transcoder].encoder],
1050df0566a6SJani Nikula  *   when port is matched
10515eba7426SVille Syrjälä  * MST & (cpu_transcoder < 0): this is invalid
10525eba7426SVille Syrjälä  * Non-MST & (cpu_transcoder >= 0): only cpu_transcoder = 0 (the first device entry)
1053df0566a6SJani Nikula  *   will get the right intel_encoder with port matched
10545eba7426SVille Syrjälä  * Non-MST & (cpu_transcoder < 0): get the right intel_encoder with port matched
1055df0566a6SJani Nikula  */
find_audio_state(struct drm_i915_private * i915,int port,int cpu_transcoder)1056c7104c38SVille Syrjälä static struct intel_audio_state *find_audio_state(struct drm_i915_private *i915,
10575eba7426SVille Syrjälä 						  int port, int cpu_transcoder)
1058df0566a6SJani Nikula {
1059df0566a6SJani Nikula 	/* MST */
10605eba7426SVille Syrjälä 	if (cpu_transcoder >= 0) {
1061c7104c38SVille Syrjälä 		struct intel_audio_state *audio_state;
1062734d06d2SVille Syrjälä 		struct intel_encoder *encoder;
1063734d06d2SVille Syrjälä 
106446e61ee4SVille Syrjälä 		if (drm_WARN_ON(&i915->drm,
10655eba7426SVille Syrjälä 				cpu_transcoder >= ARRAY_SIZE(i915->display.audio.state)))
1066df0566a6SJani Nikula 			return NULL;
1067df0566a6SJani Nikula 
10685eba7426SVille Syrjälä 		audio_state = &i915->display.audio.state[cpu_transcoder];
1069c7104c38SVille Syrjälä 		encoder = audio_state->encoder;
1070c7104c38SVille Syrjälä 
1071734d06d2SVille Syrjälä 		if (encoder && encoder->port == port &&
1072df0566a6SJani Nikula 		    encoder->type == INTEL_OUTPUT_DP_MST)
1073c7104c38SVille Syrjälä 			return audio_state;
1074df0566a6SJani Nikula 	}
1075df0566a6SJani Nikula 
1076df0566a6SJani Nikula 	/* Non-MST */
10775eba7426SVille Syrjälä 	if (cpu_transcoder > 0)
1078df0566a6SJani Nikula 		return NULL;
1079df0566a6SJani Nikula 
10805eba7426SVille Syrjälä 	for_each_cpu_transcoder(i915, cpu_transcoder) {
1081c7104c38SVille Syrjälä 		struct intel_audio_state *audio_state;
1082734d06d2SVille Syrjälä 		struct intel_encoder *encoder;
1083734d06d2SVille Syrjälä 
10845eba7426SVille Syrjälä 		audio_state = &i915->display.audio.state[cpu_transcoder];
1085c7104c38SVille Syrjälä 		encoder = audio_state->encoder;
1086df0566a6SJani Nikula 
1087734d06d2SVille Syrjälä 		if (encoder && encoder->port == port &&
1088734d06d2SVille Syrjälä 		    encoder->type != INTEL_OUTPUT_DP_MST)
1089c7104c38SVille Syrjälä 			return audio_state;
1090df0566a6SJani Nikula 	}
1091df0566a6SJani Nikula 
1092df0566a6SJani Nikula 	return NULL;
1093df0566a6SJani Nikula }
1094df0566a6SJani Nikula 
i915_audio_component_sync_audio_rate(struct device * kdev,int port,int cpu_transcoder,int rate)1095df0566a6SJani Nikula static int i915_audio_component_sync_audio_rate(struct device *kdev, int port,
10965eba7426SVille Syrjälä 						int cpu_transcoder, int rate)
1097df0566a6SJani Nikula {
109846e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
109946e61ee4SVille Syrjälä 	struct i915_audio_component *acomp = i915->display.audio.component;
1100c7104c38SVille Syrjälä 	const struct intel_audio_state *audio_state;
1101df0566a6SJani Nikula 	struct intel_encoder *encoder;
1102df0566a6SJani Nikula 	struct intel_crtc *crtc;
1103df0566a6SJani Nikula 	unsigned long cookie;
1104df0566a6SJani Nikula 	int err = 0;
1105df0566a6SJani Nikula 
110646e61ee4SVille Syrjälä 	if (!HAS_DDI(i915))
1107df0566a6SJani Nikula 		return 0;
1108df0566a6SJani Nikula 
1109df0566a6SJani Nikula 	cookie = i915_audio_component_get_power(kdev);
111046e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
1111df0566a6SJani Nikula 
11125eba7426SVille Syrjälä 	audio_state = find_audio_state(i915, port, cpu_transcoder);
1113c7104c38SVille Syrjälä 	if (!audio_state) {
1114c7104c38SVille Syrjälä 		drm_dbg_kms(&i915->drm, "Not valid for port %c\n", port_name(port));
1115df0566a6SJani Nikula 		err = -ENODEV;
1116df0566a6SJani Nikula 		goto unlock;
1117df0566a6SJani Nikula 	}
1118df0566a6SJani Nikula 
1119c7104c38SVille Syrjälä 	encoder = audio_state->encoder;
1120c7104c38SVille Syrjälä 
1121c7104c38SVille Syrjälä 	/* FIXME stop using the legacy crtc pointer */
1122df0566a6SJani Nikula 	crtc = to_intel_crtc(encoder->base.crtc);
1123df0566a6SJani Nikula 
11245eba7426SVille Syrjälä 	/* port must be valid now, otherwise the cpu_transcoder will be invalid */
1125df0566a6SJani Nikula 	acomp->aud_sample_rate[port] = rate;
1126df0566a6SJani Nikula 
1127c7104c38SVille Syrjälä 	/* FIXME get rid of the crtc->config stuff */
1128df0566a6SJani Nikula 	hsw_audio_config_update(encoder, crtc->config);
1129df0566a6SJani Nikula 
1130df0566a6SJani Nikula  unlock:
113146e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
1132df0566a6SJani Nikula 	i915_audio_component_put_power(kdev, cookie);
1133df0566a6SJani Nikula 	return err;
1134df0566a6SJani Nikula }
1135df0566a6SJani Nikula 
i915_audio_component_get_eld(struct device * kdev,int port,int cpu_transcoder,bool * enabled,unsigned char * buf,int max_bytes)1136df0566a6SJani Nikula static int i915_audio_component_get_eld(struct device *kdev, int port,
11375eba7426SVille Syrjälä 					int cpu_transcoder, bool *enabled,
1138df0566a6SJani Nikula 					unsigned char *buf, int max_bytes)
1139df0566a6SJani Nikula {
114046e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1141c7104c38SVille Syrjälä 	const struct intel_audio_state *audio_state;
1142c7104c38SVille Syrjälä 	int ret = 0;
1143df0566a6SJani Nikula 
114446e61ee4SVille Syrjälä 	mutex_lock(&i915->display.audio.mutex);
1145df0566a6SJani Nikula 
11465eba7426SVille Syrjälä 	audio_state = find_audio_state(i915, port, cpu_transcoder);
1147c7104c38SVille Syrjälä 	if (!audio_state) {
1148c7104c38SVille Syrjälä 		drm_dbg_kms(&i915->drm, "Not valid for port %c\n", port_name(port));
114946e61ee4SVille Syrjälä 		mutex_unlock(&i915->display.audio.mutex);
1150c7104c38SVille Syrjälä 		return -EINVAL;
1151df0566a6SJani Nikula 	}
1152df0566a6SJani Nikula 
11535d986635SVille Syrjälä 	*enabled = audio_state->encoder != NULL;
1154df0566a6SJani Nikula 	if (*enabled) {
11555d986635SVille Syrjälä 		const u8 *eld = audio_state->eld;
1156c7104c38SVille Syrjälä 
1157df0566a6SJani Nikula 		ret = drm_eld_size(eld);
1158df0566a6SJani Nikula 		memcpy(buf, eld, min(max_bytes, ret));
1159df0566a6SJani Nikula 	}
1160df0566a6SJani Nikula 
116146e61ee4SVille Syrjälä 	mutex_unlock(&i915->display.audio.mutex);
1162df0566a6SJani Nikula 	return ret;
1163df0566a6SJani Nikula }
1164df0566a6SJani Nikula 
1165df0566a6SJani Nikula static const struct drm_audio_component_ops i915_audio_component_ops = {
1166df0566a6SJani Nikula 	.owner		= THIS_MODULE,
1167df0566a6SJani Nikula 	.get_power	= i915_audio_component_get_power,
1168df0566a6SJani Nikula 	.put_power	= i915_audio_component_put_power,
1169df0566a6SJani Nikula 	.codec_wake_override = i915_audio_component_codec_wake_override,
1170df0566a6SJani Nikula 	.get_cdclk_freq	= i915_audio_component_get_cdclk_freq,
1171df0566a6SJani Nikula 	.sync_audio_rate = i915_audio_component_sync_audio_rate,
1172df0566a6SJani Nikula 	.get_eld	= i915_audio_component_get_eld,
1173df0566a6SJani Nikula };
1174df0566a6SJani Nikula 
i915_audio_component_bind(struct device * i915_kdev,struct device * hda_kdev,void * data)1175df0566a6SJani Nikula static int i915_audio_component_bind(struct device *i915_kdev,
1176df0566a6SJani Nikula 				     struct device *hda_kdev, void *data)
1177df0566a6SJani Nikula {
1178df0566a6SJani Nikula 	struct i915_audio_component *acomp = data;
117946e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(i915_kdev);
1180df0566a6SJani Nikula 	int i;
1181df0566a6SJani Nikula 
118246e61ee4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, acomp->base.ops || acomp->base.dev))
1183df0566a6SJani Nikula 		return -EEXIST;
1184df0566a6SJani Nikula 
118546e61ee4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm,
11869a3b466bSPankaj Bharadiya 			!device_link_add(hda_kdev, i915_kdev,
11879a3b466bSPankaj Bharadiya 					 DL_FLAG_STATELESS)))
1188df0566a6SJani Nikula 		return -ENOMEM;
1189df0566a6SJani Nikula 
119046e61ee4SVille Syrjälä 	drm_modeset_lock_all(&i915->drm);
1191df0566a6SJani Nikula 	acomp->base.ops = &i915_audio_component_ops;
1192df0566a6SJani Nikula 	acomp->base.dev = i915_kdev;
1193df0566a6SJani Nikula 	BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
1194df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
1195df0566a6SJani Nikula 		acomp->aud_sample_rate[i] = 0;
119646e61ee4SVille Syrjälä 	i915->display.audio.component = acomp;
119746e61ee4SVille Syrjälä 	drm_modeset_unlock_all(&i915->drm);
1198df0566a6SJani Nikula 
1199df0566a6SJani Nikula 	return 0;
1200df0566a6SJani Nikula }
1201df0566a6SJani Nikula 
i915_audio_component_unbind(struct device * i915_kdev,struct device * hda_kdev,void * data)1202df0566a6SJani Nikula static void i915_audio_component_unbind(struct device *i915_kdev,
1203df0566a6SJani Nikula 					struct device *hda_kdev, void *data)
1204df0566a6SJani Nikula {
1205df0566a6SJani Nikula 	struct i915_audio_component *acomp = data;
120646e61ee4SVille Syrjälä 	struct drm_i915_private *i915 = kdev_to_i915(i915_kdev);
1207df0566a6SJani Nikula 
120846e61ee4SVille Syrjälä 	drm_modeset_lock_all(&i915->drm);
1209df0566a6SJani Nikula 	acomp->base.ops = NULL;
1210df0566a6SJani Nikula 	acomp->base.dev = NULL;
121146e61ee4SVille Syrjälä 	i915->display.audio.component = NULL;
121246e61ee4SVille Syrjälä 	drm_modeset_unlock_all(&i915->drm);
1213df0566a6SJani Nikula 
1214df0566a6SJani Nikula 	device_link_remove(hda_kdev, i915_kdev);
1215b4ed131dSJani Nikula 
121646e61ee4SVille Syrjälä 	if (i915->display.audio.power_refcount)
121746e61ee4SVille Syrjälä 		drm_err(&i915->drm, "audio power refcount %d after unbind\n",
121846e61ee4SVille Syrjälä 			i915->display.audio.power_refcount);
1219df0566a6SJani Nikula }
1220df0566a6SJani Nikula 
1221df0566a6SJani Nikula static const struct component_ops i915_audio_component_bind_ops = {
1222df0566a6SJani Nikula 	.bind	= i915_audio_component_bind,
1223df0566a6SJani Nikula 	.unbind	= i915_audio_component_unbind,
1224df0566a6SJani Nikula };
1225df0566a6SJani Nikula 
1226989634fbSKai Vehmanen #define AUD_FREQ_TMODE_SHIFT	14
1227989634fbSKai Vehmanen #define AUD_FREQ_4T		0
1228989634fbSKai Vehmanen #define AUD_FREQ_8T		(2 << AUD_FREQ_TMODE_SHIFT)
1229989634fbSKai Vehmanen #define AUD_FREQ_PULLCLKS(x)	(((x) & 0x3) << 11)
1230989634fbSKai Vehmanen #define AUD_FREQ_BCLK_96M	BIT(4)
1231989634fbSKai Vehmanen 
1232989634fbSKai Vehmanen #define AUD_FREQ_GEN12          (AUD_FREQ_8T | AUD_FREQ_PULLCLKS(0) | AUD_FREQ_BCLK_96M)
1233989634fbSKai Vehmanen #define AUD_FREQ_TGL_BROKEN     (AUD_FREQ_8T | AUD_FREQ_PULLCLKS(2) | AUD_FREQ_BCLK_96M)
1234989634fbSKai Vehmanen 
1235df0566a6SJani Nikula /**
1236df0566a6SJani Nikula  * i915_audio_component_init - initialize and register the audio component
123746e61ee4SVille Syrjälä  * @i915: i915 device instance
1238df0566a6SJani Nikula  *
1239df0566a6SJani Nikula  * This will register with the component framework a child component which
1240df0566a6SJani Nikula  * will bind dynamically to the snd_hda_intel driver's corresponding master
1241df0566a6SJani Nikula  * component when the latter is registered. During binding the child
1242df0566a6SJani Nikula  * initializes an instance of struct i915_audio_component which it receives
1243df0566a6SJani Nikula  * from the master. The master can then start to use the interface defined by
1244df0566a6SJani Nikula  * this struct. Each side can break the binding at any point by deregistering
1245df0566a6SJani Nikula  * its own component after which each side's component unbind callback is
1246df0566a6SJani Nikula  * called.
1247df0566a6SJani Nikula  *
1248df0566a6SJani Nikula  * We ignore any error during registration and continue with reduced
1249df0566a6SJani Nikula  * functionality (i.e. without HDMI audio).
1250df0566a6SJani Nikula  */
i915_audio_component_init(struct drm_i915_private * i915)125146e61ee4SVille Syrjälä static void i915_audio_component_init(struct drm_i915_private *i915)
1252df0566a6SJani Nikula {
1253989634fbSKai Vehmanen 	u32 aud_freq, aud_freq_init;
1254df0566a6SJani Nikula 
125546e61ee4SVille Syrjälä 	if (DISPLAY_VER(i915) >= 9) {
125646e61ee4SVille Syrjälä 		aud_freq_init = intel_de_read(i915, AUD_FREQ_CNTRL);
1257989634fbSKai Vehmanen 
125846e61ee4SVille Syrjälä 		if (DISPLAY_VER(i915) >= 12)
1259989634fbSKai Vehmanen 			aud_freq = AUD_FREQ_GEN12;
1260989634fbSKai Vehmanen 		else
1261989634fbSKai Vehmanen 			aud_freq = aud_freq_init;
1262989634fbSKai Vehmanen 
1263c6b40ee3SKai-Heng Feng 		/* use BIOS provided value for TGL and RKL unless it is a known bad value */
126446e61ee4SVille Syrjälä 		if ((IS_TIGERLAKE(i915) || IS_ROCKETLAKE(i915)) &&
1265c6b40ee3SKai-Heng Feng 		    aud_freq_init != AUD_FREQ_TGL_BROKEN)
1266989634fbSKai Vehmanen 			aud_freq = aud_freq_init;
1267989634fbSKai Vehmanen 
126846e61ee4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "use AUD_FREQ_CNTRL of 0x%x (init value 0x%x)\n",
1269989634fbSKai Vehmanen 			    aud_freq, aud_freq_init);
1270989634fbSKai Vehmanen 
127146e61ee4SVille Syrjälä 		i915->display.audio.freq_cntrl = aud_freq;
127287c16945SKai Vehmanen 	}
127387c16945SKai Vehmanen 
1274112a87c4SKai Vehmanen 	/* init with current cdclk */
127546e61ee4SVille Syrjälä 	intel_audio_cdclk_change_post(i915);
1276*f287b1e3SImre Deak }
1277*f287b1e3SImre Deak 
i915_audio_component_register(struct drm_i915_private * i915)1278*f287b1e3SImre Deak static void i915_audio_component_register(struct drm_i915_private *i915)
1279*f287b1e3SImre Deak {
1280*f287b1e3SImre Deak 	int ret;
1281*f287b1e3SImre Deak 
1282*f287b1e3SImre Deak 	ret = component_add_typed(i915->drm.dev,
1283*f287b1e3SImre Deak 				  &i915_audio_component_bind_ops,
1284*f287b1e3SImre Deak 				  I915_COMPONENT_AUDIO);
1285*f287b1e3SImre Deak 	if (ret < 0) {
1286*f287b1e3SImre Deak 		drm_err(&i915->drm,
1287*f287b1e3SImre Deak 			"failed to add audio component (%d)\n", ret);
1288*f287b1e3SImre Deak 		/* continue with reduced functionality */
1289*f287b1e3SImre Deak 		return;
1290*f287b1e3SImre Deak 	}
1291112a87c4SKai Vehmanen 
129246e61ee4SVille Syrjälä 	i915->display.audio.component_registered = true;
1293df0566a6SJani Nikula }
1294df0566a6SJani Nikula 
1295df0566a6SJani Nikula /**
1296df0566a6SJani Nikula  * i915_audio_component_cleanup - deregister the audio component
129746e61ee4SVille Syrjälä  * @i915: i915 device instance
1298df0566a6SJani Nikula  *
1299df0566a6SJani Nikula  * Deregisters the audio component, breaking any existing binding to the
1300df0566a6SJani Nikula  * corresponding snd_hda_intel driver's master component.
1301df0566a6SJani Nikula  */
i915_audio_component_cleanup(struct drm_i915_private * i915)130246e61ee4SVille Syrjälä static void i915_audio_component_cleanup(struct drm_i915_private *i915)
1303df0566a6SJani Nikula {
130446e61ee4SVille Syrjälä 	if (!i915->display.audio.component_registered)
1305df0566a6SJani Nikula 		return;
1306df0566a6SJani Nikula 
130746e61ee4SVille Syrjälä 	component_del(i915->drm.dev, &i915_audio_component_bind_ops);
130846e61ee4SVille Syrjälä 	i915->display.audio.component_registered = false;
1309df0566a6SJani Nikula }
1310df0566a6SJani Nikula 
1311df0566a6SJani Nikula /**
1312df0566a6SJani Nikula  * intel_audio_init() - Initialize the audio driver either using
1313df0566a6SJani Nikula  * component framework or using lpe audio bridge
131446e61ee4SVille Syrjälä  * @i915: the i915 drm device private data
1315df0566a6SJani Nikula  *
1316df0566a6SJani Nikula  */
intel_audio_init(struct drm_i915_private * i915)131746e61ee4SVille Syrjälä void intel_audio_init(struct drm_i915_private *i915)
1318df0566a6SJani Nikula {
131946e61ee4SVille Syrjälä 	if (intel_lpe_audio_init(i915) < 0)
132046e61ee4SVille Syrjälä 		i915_audio_component_init(i915);
1321df0566a6SJani Nikula }
1322df0566a6SJani Nikula 
intel_audio_register(struct drm_i915_private * i915)1323*f287b1e3SImre Deak void intel_audio_register(struct drm_i915_private *i915)
1324*f287b1e3SImre Deak {
1325*f287b1e3SImre Deak 	if (!i915->display.audio.lpe.platdev)
1326*f287b1e3SImre Deak 		i915_audio_component_register(i915);
1327*f287b1e3SImre Deak }
1328*f287b1e3SImre Deak 
1329df0566a6SJani Nikula /**
1330df0566a6SJani Nikula  * intel_audio_deinit() - deinitialize the audio driver
133146e61ee4SVille Syrjälä  * @i915: the i915 drm device private data
1332df0566a6SJani Nikula  *
1333df0566a6SJani Nikula  */
intel_audio_deinit(struct drm_i915_private * i915)133446e61ee4SVille Syrjälä void intel_audio_deinit(struct drm_i915_private *i915)
1335df0566a6SJani Nikula {
133646e61ee4SVille Syrjälä 	if (i915->display.audio.lpe.platdev != NULL)
133746e61ee4SVille Syrjälä 		intel_lpe_audio_teardown(i915);
1338df0566a6SJani Nikula 	else
133946e61ee4SVille Syrjälä 		i915_audio_component_cleanup(i915);
1340df0566a6SJani Nikula }
1341