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