blob: 8f6cc15ca39c417d6c8c6597077ecbe98b9c5ca1 [file] [log] [blame]
Jani Nikula7c10a2b2014-10-27 16:26:43 +02001/*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#include <linux/kernel.h>
Imre Deak58fddc22015-01-08 17:54:14 +020025#include <linux/component.h>
26#include <drm/i915_component.h>
27#include "intel_drv.h"
Jani Nikula7c10a2b2014-10-27 16:26:43 +020028
29#include <drm/drmP.h>
30#include <drm/drm_edid.h>
Jani Nikula7c10a2b2014-10-27 16:26:43 +020031#include "i915_drv.h"
32
Jani Nikula28855d22014-10-27 16:27:00 +020033/**
34 * DOC: High Definition Audio over HDMI and Display Port
35 *
36 * The graphics and audio drivers together support High Definition Audio over
37 * HDMI and Display Port. The audio programming sequences are divided into audio
38 * codec and controller enable and disable sequences. The graphics driver
39 * handles the audio codec sequences, while the audio driver handles the audio
40 * controller sequences.
41 *
42 * The disable sequences must be performed before disabling the transcoder or
43 * port. The enable sequences may only be performed after enabling the
Jani Nikula3e6da4a2015-07-02 16:05:27 +030044 * transcoder and port, and after completed link training. Therefore the audio
45 * enable/disable sequences are part of the modeset sequence.
Jani Nikula28855d22014-10-27 16:27:00 +020046 *
47 * The codec and controller sequences could be done either parallel or serial,
48 * but generally the ELDV/PD change in the codec sequence indicates to the audio
49 * driver that the controller sequence should start. Indeed, most of the
50 * co-operation between the graphics and audio drivers is handled via audio
51 * related registers. (The notable exception is the power management, not
52 * covered here.)
Libin Yangcb422612015-10-01 17:01:09 +080053 *
54 * The struct i915_audio_component is used to interact between the graphics
55 * and audio drivers. The struct i915_audio_component_ops *ops in it is
56 * defined in graphics driver and called in audio driver. The
57 * struct i915_audio_component_audio_ops *audio_ops is called from i915 driver.
Jani Nikula28855d22014-10-27 16:27:00 +020058 */
59
Jani Nikula87fcb2a2014-10-27 16:26:44 +020060static const struct {
Jani Nikula7c10a2b2014-10-27 16:26:43 +020061 int clock;
62 u32 config;
63} hdmi_audio_clock[] = {
Ville Syrjälä606bb5e2015-10-08 11:43:34 +030064 { 25175, AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
Jani Nikula7c10a2b2014-10-27 16:26:43 +020065 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
66 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
Ville Syrjälä606bb5e2015-10-08 11:43:34 +030067 { 27027, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
Jani Nikula7c10a2b2014-10-27 16:26:43 +020068 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
Ville Syrjälä606bb5e2015-10-08 11:43:34 +030069 { 54054, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
70 { 74176, AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
Jani Nikula7c10a2b2014-10-27 16:26:43 +020071 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
Ville Syrjälä606bb5e2015-10-08 11:43:34 +030072 { 148352, AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
Jani Nikula7c10a2b2014-10-27 16:26:43 +020073 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
74};
75
Libin Yang4a21ef72015-09-02 14:11:39 +080076/* HDMI N/CTS table */
77#define TMDS_297M 297000
Ville Syrjälä606bb5e2015-10-08 11:43:34 +030078#define TMDS_296M 296703
Libin Yang4a21ef72015-09-02 14:11:39 +080079static const struct {
80 int sample_rate;
81 int clock;
82 int n;
83 int cts;
84} aud_ncts[] = {
85 { 44100, TMDS_296M, 4459, 234375 },
86 { 44100, TMDS_297M, 4704, 247500 },
87 { 48000, TMDS_296M, 5824, 281250 },
88 { 48000, TMDS_297M, 5120, 247500 },
89 { 32000, TMDS_296M, 5824, 421875 },
90 { 32000, TMDS_297M, 3072, 222750 },
91 { 88200, TMDS_296M, 8918, 234375 },
92 { 88200, TMDS_297M, 9408, 247500 },
93 { 96000, TMDS_296M, 11648, 281250 },
94 { 96000, TMDS_297M, 10240, 247500 },
95 { 176400, TMDS_296M, 17836, 234375 },
96 { 176400, TMDS_297M, 18816, 247500 },
97 { 192000, TMDS_296M, 23296, 281250 },
98 { 192000, TMDS_297M, 20480, 247500 },
99};
100
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200101/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300102static u32 audio_config_hdmi_pixel_clock(const struct drm_display_mode *adjusted_mode)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200103{
104 int i;
105
106 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300107 if (adjusted_mode->crtc_clock == hdmi_audio_clock[i].clock)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200108 break;
109 }
110
111 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300112 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n",
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300113 adjusted_mode->crtc_clock);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200114 i = 1;
115 }
116
117 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
118 hdmi_audio_clock[i].clock,
119 hdmi_audio_clock[i].config);
120
121 return hdmi_audio_clock[i].config;
122}
123
Libin Yang4a21ef72015-09-02 14:11:39 +0800124static int audio_config_get_n(const struct drm_display_mode *mode, int rate)
125{
126 int i;
127
128 for (i = 0; i < ARRAY_SIZE(aud_ncts); i++) {
129 if ((rate == aud_ncts[i].sample_rate) &&
130 (mode->clock == aud_ncts[i].clock)) {
131 return aud_ncts[i].n;
132 }
133 }
134 return 0;
135}
136
Libin Yang7e8275c2015-09-25 09:36:12 +0800137static uint32_t audio_config_setup_n_reg(int n, uint32_t val)
138{
139 int n_low, n_up;
140 uint32_t tmp = val;
141
142 n_low = n & 0xfff;
143 n_up = (n >> 12) & 0xff;
144 tmp &= ~(AUD_CONFIG_UPPER_N_MASK | AUD_CONFIG_LOWER_N_MASK);
145 tmp |= ((n_up << AUD_CONFIG_UPPER_N_SHIFT) |
146 (n_low << AUD_CONFIG_LOWER_N_SHIFT) |
147 AUD_CONFIG_N_PROG_ENABLE);
148 return tmp;
149}
150
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200151static bool intel_eld_uptodate(struct drm_connector *connector,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200152 i915_reg_t reg_eldv, uint32_t bits_eldv,
153 i915_reg_t reg_elda, uint32_t bits_elda,
154 i915_reg_t reg_edid)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200155{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100156 struct drm_i915_private *dev_priv = to_i915(connector->dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200157 uint8_t *eld = connector->eld;
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200158 uint32_t tmp;
159 int i;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200160
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200161 tmp = I915_READ(reg_eldv);
162 tmp &= bits_eldv;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200163
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200164 if (!tmp)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200165 return false;
166
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200167 tmp = I915_READ(reg_elda);
168 tmp &= ~bits_elda;
169 I915_WRITE(reg_elda, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200170
Jani Nikula938fd8a2014-10-28 16:20:48 +0200171 for (i = 0; i < drm_eld_size(eld) / 4; i++)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200172 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
173 return false;
174
175 return true;
176}
177
Jani Nikula76d8d3e2014-10-27 16:26:57 +0200178static void g4x_audio_codec_disable(struct intel_encoder *encoder)
179{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100180 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Jani Nikula76d8d3e2014-10-27 16:26:57 +0200181 uint32_t eldv, tmp;
182
183 DRM_DEBUG_KMS("Disable audio codec\n");
184
185 tmp = I915_READ(G4X_AUD_VID_DID);
186 if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
187 eldv = G4X_ELDV_DEVCL_DEVBLC;
188 else
189 eldv = G4X_ELDV_DEVCTG;
190
191 /* Invalidate ELD */
192 tmp = I915_READ(G4X_AUD_CNTL_ST);
193 tmp &= ~eldv;
194 I915_WRITE(G4X_AUD_CNTL_ST, tmp);
195}
196
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200197static void g4x_audio_codec_enable(struct drm_connector *connector,
198 struct intel_encoder *encoder,
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300199 const struct drm_display_mode *adjusted_mode)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200200{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100201 struct drm_i915_private *dev_priv = to_i915(connector->dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200202 uint8_t *eld = connector->eld;
203 uint32_t eldv;
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200204 uint32_t tmp;
205 int len, i;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200206
Jani Nikulad5ee08d2014-10-27 16:26:58 +0200207 DRM_DEBUG_KMS("Enable audio codec, %u bytes ELD\n", eld[2]);
208
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200209 tmp = I915_READ(G4X_AUD_VID_DID);
210 if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200211 eldv = G4X_ELDV_DEVCL_DEVBLC;
212 else
213 eldv = G4X_ELDV_DEVCTG;
214
215 if (intel_eld_uptodate(connector,
216 G4X_AUD_CNTL_ST, eldv,
Jani Nikulac46f1112014-10-27 16:26:52 +0200217 G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200218 G4X_HDMIW_HDMIEDID))
219 return;
220
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200221 tmp = I915_READ(G4X_AUD_CNTL_ST);
Jani Nikulac46f1112014-10-27 16:26:52 +0200222 tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200223 len = (tmp >> 9) & 0x1f; /* ELD buffer size */
224 I915_WRITE(G4X_AUD_CNTL_ST, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200225
Jani Nikula938fd8a2014-10-28 16:20:48 +0200226 len = min(drm_eld_size(eld) / 4, len);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200227 DRM_DEBUG_DRIVER("ELD size %d\n", len);
228 for (i = 0; i < len; i++)
229 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
230
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200231 tmp = I915_READ(G4X_AUD_CNTL_ST);
232 tmp |= eldv;
233 I915_WRITE(G4X_AUD_CNTL_ST, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200234}
235
Jani Nikula12e87f22016-10-10 18:04:03 +0300236static void
237hsw_dp_audio_config_update(struct intel_crtc *intel_crtc, enum port port,
238 const struct drm_display_mode *adjusted_mode)
239{
240 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
241 enum pipe pipe = intel_crtc->pipe;
242 u32 tmp;
243
244 tmp = I915_READ(HSW_AUD_CFG(pipe));
245 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
246 tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
247 tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
248 tmp |= AUD_CONFIG_N_VALUE_INDEX;
249
250 I915_WRITE(HSW_AUD_CFG(pipe), tmp);
251}
252
253static void
254hsw_hdmi_audio_config_update(struct intel_crtc *intel_crtc, enum port port,
255 const struct drm_display_mode *adjusted_mode)
Jani Nikula6c262912016-10-10 18:04:00 +0300256{
257 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
258 struct i915_audio_component *acomp = dev_priv->audio_component;
Jani Nikula3af306d2016-10-10 18:04:01 +0300259 int rate = acomp ? acomp->aud_sample_rate[port] : 0;
Jani Nikula6c262912016-10-10 18:04:00 +0300260 enum pipe pipe = intel_crtc->pipe;
Jani Nikula3af306d2016-10-10 18:04:01 +0300261 int n;
Jani Nikula6c262912016-10-10 18:04:00 +0300262 u32 tmp;
263
264 tmp = I915_READ(HSW_AUD_CFG(pipe));
265 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
266 tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
Jani Nikula6c262912016-10-10 18:04:00 +0300267 tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
Jani Nikula12e87f22016-10-10 18:04:03 +0300268 tmp |= audio_config_hdmi_pixel_clock(adjusted_mode);
269
270 if (adjusted_mode->clock == TMDS_296M ||
271 adjusted_mode->clock == TMDS_297M) {
Jani Nikula6c262912016-10-10 18:04:00 +0300272 n = audio_config_get_n(adjusted_mode, rate);
273 if (n != 0)
274 tmp = audio_config_setup_n_reg(n, tmp);
275 else
276 DRM_DEBUG_KMS("no suitable N value is found\n");
277 }
278
279 I915_WRITE(HSW_AUD_CFG(pipe), tmp);
280}
281
Jani Nikula12e87f22016-10-10 18:04:03 +0300282static void
283hsw_audio_config_update(struct intel_crtc *intel_crtc, enum port port,
284 const struct drm_display_mode *adjusted_mode)
285{
286 if (intel_crtc_has_dp_encoder(intel_crtc->config))
287 hsw_dp_audio_config_update(intel_crtc, port, adjusted_mode);
288 else
289 hsw_hdmi_audio_config_update(intel_crtc, port, adjusted_mode);
290}
291
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200292static void hsw_audio_codec_disable(struct intel_encoder *encoder)
293{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100294 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Jani Nikula5fad84a2014-11-04 10:30:23 +0200295 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
296 enum pipe pipe = intel_crtc->pipe;
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200297 uint32_t tmp;
298
Jani Nikula5fad84a2014-11-04 10:30:23 +0200299 DRM_DEBUG_KMS("Disable audio codec on pipe %c\n", pipe_name(pipe));
300
Libin Yang4a21ef72015-09-02 14:11:39 +0800301 mutex_lock(&dev_priv->av_mutex);
302
Jani Nikula5fad84a2014-11-04 10:30:23 +0200303 /* Disable timestamps */
304 tmp = I915_READ(HSW_AUD_CFG(pipe));
305 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
306 tmp |= AUD_CONFIG_N_PROG_ENABLE;
307 tmp &= ~AUD_CONFIG_UPPER_N_MASK;
308 tmp &= ~AUD_CONFIG_LOWER_N_MASK;
Ville Syrjälä2210ce72016-06-22 21:57:05 +0300309 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Jani Nikula5fad84a2014-11-04 10:30:23 +0200310 tmp |= AUD_CONFIG_N_VALUE_INDEX;
311 I915_WRITE(HSW_AUD_CFG(pipe), tmp);
312
313 /* Invalidate ELD */
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200314 tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
Jani Nikula82910ac2014-10-27 16:26:59 +0200315 tmp &= ~AUDIO_ELD_VALID(pipe);
Jani Nikulaeb45fa02014-11-18 12:11:29 +0200316 tmp &= ~AUDIO_OUTPUT_ENABLE(pipe);
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200317 I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
Libin Yang4a21ef72015-09-02 14:11:39 +0800318
319 mutex_unlock(&dev_priv->av_mutex);
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200320}
321
322static void hsw_audio_codec_enable(struct drm_connector *connector,
Pandiyan, Dhinakarand8dee422016-09-19 18:24:39 -0700323 struct intel_encoder *intel_encoder,
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300324 const struct drm_display_mode *adjusted_mode)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200325{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100326 struct drm_i915_private *dev_priv = to_i915(connector->dev);
Pandiyan, Dhinakarand8dee422016-09-19 18:24:39 -0700327 struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
Jani Nikula5fad84a2014-11-04 10:30:23 +0200328 enum pipe pipe = intel_crtc->pipe;
Pandiyan, Dhinakarand8dee422016-09-19 18:24:39 -0700329 enum port port = intel_encoder->port;
Jani Nikula5fad84a2014-11-04 10:30:23 +0200330 const uint8_t *eld = connector->eld;
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200331 uint32_t tmp;
332 int len, i;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200333
Jani Nikula5fad84a2014-11-04 10:30:23 +0200334 DRM_DEBUG_KMS("Enable audio codec on pipe %c, %u bytes ELD\n",
Jani Nikula938fd8a2014-10-28 16:20:48 +0200335 pipe_name(pipe), drm_eld_size(eld));
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200336
Libin Yang4a21ef72015-09-02 14:11:39 +0800337 mutex_lock(&dev_priv->av_mutex);
338
Jani Nikula5fad84a2014-11-04 10:30:23 +0200339 /* Enable audio presence detect, invalidate ELD */
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200340 tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
Jani Nikula82910ac2014-10-27 16:26:59 +0200341 tmp |= AUDIO_OUTPUT_ENABLE(pipe);
342 tmp &= ~AUDIO_ELD_VALID(pipe);
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200343 I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
Jani Nikula5fad84a2014-11-04 10:30:23 +0200344
345 /*
346 * FIXME: We're supposed to wait for vblank here, but we have vblanks
347 * disabled during the mode set. The proper fix would be to push the
348 * rest of the setup into a vblank work item, queued here, but the
349 * infrastructure is not there yet.
350 */
351
352 /* Reset ELD write address */
353 tmp = I915_READ(HSW_AUD_DIP_ELD_CTRL(pipe));
354 tmp &= ~IBX_ELD_ADDRESS_MASK;
355 I915_WRITE(HSW_AUD_DIP_ELD_CTRL(pipe), tmp);
356
357 /* Up to 84 bytes of hw ELD buffer */
Jani Nikula938fd8a2014-10-28 16:20:48 +0200358 len = min(drm_eld_size(eld), 84);
359 for (i = 0; i < len / 4; i++)
Jani Nikula5fad84a2014-11-04 10:30:23 +0200360 I915_WRITE(HSW_AUD_EDID_DATA(pipe), *((uint32_t *)eld + i));
361
362 /* ELD valid */
363 tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
Jani Nikula82910ac2014-10-27 16:26:59 +0200364 tmp |= AUDIO_ELD_VALID(pipe);
Jani Nikula5fad84a2014-11-04 10:30:23 +0200365 I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
366
367 /* Enable timestamps */
Jani Nikula6c262912016-10-10 18:04:00 +0300368 hsw_audio_config_update(intel_crtc, port, adjusted_mode);
Libin Yang4a21ef72015-09-02 14:11:39 +0800369
370 mutex_unlock(&dev_priv->av_mutex);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200371}
372
Pandiyan, Dhinakarand8dee422016-09-19 18:24:39 -0700373static void ilk_audio_codec_disable(struct intel_encoder *intel_encoder)
Jani Nikula495a5bb2014-10-27 16:26:55 +0200374{
Pandiyan, Dhinakarand8dee422016-09-19 18:24:39 -0700375 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
376 struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
Jani Nikula495a5bb2014-10-27 16:26:55 +0200377 enum pipe pipe = intel_crtc->pipe;
Pandiyan, Dhinakarand8dee422016-09-19 18:24:39 -0700378 enum port port = intel_encoder->port;
Jani Nikula495a5bb2014-10-27 16:26:55 +0200379 uint32_t tmp, eldv;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200380 i915_reg_t aud_config, aud_cntrl_st2;
Jani Nikula495a5bb2014-10-27 16:26:55 +0200381
382 DRM_DEBUG_KMS("Disable audio codec on port %c, pipe %c\n",
383 port_name(port), pipe_name(pipe));
384
Jani Nikulad3902c32015-05-04 17:20:49 +0300385 if (WARN_ON(port == PORT_A))
386 return;
387
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +0300388 if (HAS_PCH_IBX(dev_priv)) {
Jani Nikula495a5bb2014-10-27 16:26:55 +0200389 aud_config = IBX_AUD_CFG(pipe);
390 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wayne Boyer666a4532015-12-09 12:29:35 -0800391 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikula495a5bb2014-10-27 16:26:55 +0200392 aud_config = VLV_AUD_CFG(pipe);
393 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
394 } else {
395 aud_config = CPT_AUD_CFG(pipe);
396 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
397 }
398
399 /* Disable timestamps */
400 tmp = I915_READ(aud_config);
401 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
402 tmp |= AUD_CONFIG_N_PROG_ENABLE;
403 tmp &= ~AUD_CONFIG_UPPER_N_MASK;
404 tmp &= ~AUD_CONFIG_LOWER_N_MASK;
Ville Syrjälä2210ce72016-06-22 21:57:05 +0300405 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Jani Nikula495a5bb2014-10-27 16:26:55 +0200406 tmp |= AUD_CONFIG_N_VALUE_INDEX;
407 I915_WRITE(aud_config, tmp);
408
Jani Nikulad3902c32015-05-04 17:20:49 +0300409 eldv = IBX_ELD_VALID(port);
Jani Nikula495a5bb2014-10-27 16:26:55 +0200410
411 /* Invalidate ELD */
412 tmp = I915_READ(aud_cntrl_st2);
413 tmp &= ~eldv;
414 I915_WRITE(aud_cntrl_st2, tmp);
415}
416
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200417static void ilk_audio_codec_enable(struct drm_connector *connector,
Pandiyan, Dhinakarand8dee422016-09-19 18:24:39 -0700418 struct intel_encoder *intel_encoder,
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300419 const struct drm_display_mode *adjusted_mode)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200420{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100421 struct drm_i915_private *dev_priv = to_i915(connector->dev);
Pandiyan, Dhinakarand8dee422016-09-19 18:24:39 -0700422 struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
Jani Nikulac6bde932014-11-04 10:31:28 +0200423 enum pipe pipe = intel_crtc->pipe;
Pandiyan, Dhinakarand8dee422016-09-19 18:24:39 -0700424 enum port port = intel_encoder->port;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200425 uint8_t *eld = connector->eld;
Pandiyan, Dhinakaran38cb2ec2016-08-10 23:41:13 -0700426 uint32_t tmp, eldv;
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200427 int len, i;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200428 i915_reg_t hdmiw_hdmiedid, aud_config, aud_cntl_st, aud_cntrl_st2;
Jani Nikulac6bde932014-11-04 10:31:28 +0200429
430 DRM_DEBUG_KMS("Enable audio codec on port %c, pipe %c, %u bytes ELD\n",
Jani Nikula938fd8a2014-10-28 16:20:48 +0200431 port_name(port), pipe_name(pipe), drm_eld_size(eld));
Jani Nikulac6bde932014-11-04 10:31:28 +0200432
Jani Nikulad3902c32015-05-04 17:20:49 +0300433 if (WARN_ON(port == PORT_A))
434 return;
435
Jani Nikulac6bde932014-11-04 10:31:28 +0200436 /*
437 * FIXME: We're supposed to wait for vblank here, but we have vblanks
438 * disabled during the mode set. The proper fix would be to push the
439 * rest of the setup into a vblank work item, queued here, but the
440 * infrastructure is not there yet.
441 */
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200442
443 if (HAS_PCH_IBX(connector->dev)) {
444 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
445 aud_config = IBX_AUD_CFG(pipe);
446 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
447 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wayne Boyer666a4532015-12-09 12:29:35 -0800448 } else if (IS_VALLEYVIEW(connector->dev) ||
449 IS_CHERRYVIEW(connector->dev)) {
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200450 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
451 aud_config = VLV_AUD_CFG(pipe);
452 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
453 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
454 } else {
455 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
456 aud_config = CPT_AUD_CFG(pipe);
457 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
458 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
459 }
460
Jani Nikulad3902c32015-05-04 17:20:49 +0300461 eldv = IBX_ELD_VALID(port);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200462
Jani Nikulac6bde932014-11-04 10:31:28 +0200463 /* Invalidate ELD */
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200464 tmp = I915_READ(aud_cntrl_st2);
465 tmp &= ~eldv;
466 I915_WRITE(aud_cntrl_st2, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200467
Jani Nikulac6bde932014-11-04 10:31:28 +0200468 /* Reset ELD write address */
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200469 tmp = I915_READ(aud_cntl_st);
Jani Nikulac46f1112014-10-27 16:26:52 +0200470 tmp &= ~IBX_ELD_ADDRESS_MASK;
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200471 I915_WRITE(aud_cntl_st, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200472
Jani Nikulac6bde932014-11-04 10:31:28 +0200473 /* Up to 84 bytes of hw ELD buffer */
Jani Nikula938fd8a2014-10-28 16:20:48 +0200474 len = min(drm_eld_size(eld), 84);
475 for (i = 0; i < len / 4; i++)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200476 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
477
Jani Nikulac6bde932014-11-04 10:31:28 +0200478 /* ELD valid */
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200479 tmp = I915_READ(aud_cntrl_st2);
480 tmp |= eldv;
481 I915_WRITE(aud_cntrl_st2, tmp);
Jani Nikulac6bde932014-11-04 10:31:28 +0200482
483 /* Enable timestamps */
484 tmp = I915_READ(aud_config);
485 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
486 tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
487 tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
Ville Syrjälä2210ce72016-06-22 21:57:05 +0300488 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Jani Nikulac6bde932014-11-04 10:31:28 +0200489 tmp |= AUD_CONFIG_N_VALUE_INDEX;
490 else
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300491 tmp |= audio_config_hdmi_pixel_clock(adjusted_mode);
Jani Nikulac6bde932014-11-04 10:31:28 +0200492 I915_WRITE(aud_config, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200493}
494
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200495/**
496 * intel_audio_codec_enable - Enable the audio codec for HD audio
497 * @intel_encoder: encoder on which to enable audio
498 *
499 * The enable sequences may only be performed after enabling the transcoder and
500 * port, and after completed link training.
501 */
502void intel_audio_codec_enable(struct intel_encoder *intel_encoder)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200503{
Jani Nikula33d1e7c62014-10-27 16:26:46 +0200504 struct drm_encoder *encoder = &intel_encoder->base;
505 struct intel_crtc *crtc = to_intel_crtc(encoder->crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300506 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200507 struct drm_connector *connector;
Pandiyan, Dhinakarand8dee422016-09-19 18:24:39 -0700508 struct drm_i915_private *dev_priv = to_i915(encoder->dev);
David Henningsson51e1d832015-08-19 10:48:56 +0200509 struct i915_audio_component *acomp = dev_priv->audio_component;
Pandiyan, Dhinakarand8dee422016-09-19 18:24:39 -0700510 enum port port = intel_encoder->port;
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -0700511 enum pipe pipe = crtc->pipe;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200512
Ville Syrjälä9e5a3b52015-09-07 18:22:57 +0300513 connector = drm_select_eld(encoder);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200514 if (!connector)
515 return;
516
517 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
518 connector->base.id,
519 connector->name,
520 connector->encoder->base.id,
521 connector->encoder->name);
522
Jani Nikula6189b032014-10-28 13:53:01 +0200523 /* ELD Conn_Type */
524 connector->eld[5] &= ~(3 << 2);
Ville Syrjälä2210ce72016-06-22 21:57:05 +0300525 if (intel_crtc_has_dp_encoder(crtc->config))
Jani Nikula6189b032014-10-28 13:53:01 +0200526 connector->eld[5] |= (1 << 2);
527
Ville Syrjälä124abe02015-09-08 13:40:45 +0300528 connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200529
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200530 if (dev_priv->display.audio_codec_enable)
Ville Syrjälä124abe02015-09-08 13:40:45 +0300531 dev_priv->display.audio_codec_enable(connector, intel_encoder,
532 adjusted_mode);
David Henningsson51e1d832015-08-19 10:48:56 +0200533
Takashi Iwaicae666c2015-11-12 15:23:41 +0100534 mutex_lock(&dev_priv->av_mutex);
Pandiyan, Dhinakaranf1a3ace2016-09-19 18:24:40 -0700535 intel_encoder->audio_connector = connector;
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -0700536
Takashi Iwai9dfbffc2016-02-24 15:35:22 +0100537 /* referred in audio callbacks */
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -0700538 dev_priv->av_enc_map[pipe] = intel_encoder;
Takashi Iwaicae666c2015-11-12 15:23:41 +0100539 mutex_unlock(&dev_priv->av_mutex);
540
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -0700541 /* audio drivers expect pipe = -1 to indicate Non-MST cases */
542 if (intel_encoder->type != INTEL_OUTPUT_DP_MST)
543 pipe = -1;
544
David Henningsson51e1d832015-08-19 10:48:56 +0200545 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -0700546 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
547 (int) port, (int) pipe);
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200548}
549
550/**
551 * intel_audio_codec_disable - Disable the audio codec for HD audio
Geliang Tang95d0be62015-09-15 06:04:36 -0700552 * @intel_encoder: encoder on which to disable audio
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200553 *
554 * The disable sequences must be performed before disabling the transcoder or
555 * port.
556 */
David Henningsson51e1d832015-08-19 10:48:56 +0200557void intel_audio_codec_disable(struct intel_encoder *intel_encoder)
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200558{
David Henningsson51e1d832015-08-19 10:48:56 +0200559 struct drm_encoder *encoder = &intel_encoder->base;
Pandiyan, Dhinakarand8dee422016-09-19 18:24:39 -0700560 struct drm_i915_private *dev_priv = to_i915(encoder->dev);
David Henningsson51e1d832015-08-19 10:48:56 +0200561 struct i915_audio_component *acomp = dev_priv->audio_component;
Pandiyan, Dhinakarand8dee422016-09-19 18:24:39 -0700562 enum port port = intel_encoder->port;
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -0700563 struct intel_crtc *crtc = to_intel_crtc(encoder->crtc);
564 enum pipe pipe = crtc->pipe;
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200565
566 if (dev_priv->display.audio_codec_disable)
David Henningsson51e1d832015-08-19 10:48:56 +0200567 dev_priv->display.audio_codec_disable(intel_encoder);
568
Takashi Iwaicae666c2015-11-12 15:23:41 +0100569 mutex_lock(&dev_priv->av_mutex);
Pandiyan, Dhinakaranf1a3ace2016-09-19 18:24:40 -0700570 intel_encoder->audio_connector = NULL;
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -0700571 dev_priv->av_enc_map[pipe] = NULL;
Takashi Iwaicae666c2015-11-12 15:23:41 +0100572 mutex_unlock(&dev_priv->av_mutex);
573
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -0700574 /* audio drivers expect pipe = -1 to indicate Non-MST cases */
575 if (intel_encoder->type != INTEL_OUTPUT_DP_MST)
576 pipe = -1;
577
David Henningsson51e1d832015-08-19 10:48:56 +0200578 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -0700579 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
580 (int) port, (int) pipe);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200581}
582
583/**
Imre Deak88212942016-03-16 13:38:53 +0200584 * intel_init_audio_hooks - Set up chip specific audio hooks
585 * @dev_priv: device private
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200586 */
Imre Deak88212942016-03-16 13:38:53 +0200587void intel_init_audio_hooks(struct drm_i915_private *dev_priv)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200588{
Imre Deak88212942016-03-16 13:38:53 +0200589 if (IS_G4X(dev_priv)) {
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200590 dev_priv->display.audio_codec_enable = g4x_audio_codec_enable;
Jani Nikula76d8d3e2014-10-27 16:26:57 +0200591 dev_priv->display.audio_codec_disable = g4x_audio_codec_disable;
Imre Deak88212942016-03-16 13:38:53 +0200592 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200593 dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
Jani Nikula495a5bb2014-10-27 16:26:55 +0200594 dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
Imre Deak88212942016-03-16 13:38:53 +0200595 } else if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8) {
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200596 dev_priv->display.audio_codec_enable = hsw_audio_codec_enable;
597 dev_priv->display.audio_codec_disable = hsw_audio_codec_disable;
Imre Deak88212942016-03-16 13:38:53 +0200598 } else if (HAS_PCH_SPLIT(dev_priv)) {
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200599 dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
Jani Nikula495a5bb2014-10-27 16:26:55 +0200600 dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200601 }
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200602}
Imre Deak58fddc22015-01-08 17:54:14 +0200603
David Weinehallc49d13e2016-08-22 13:32:42 +0300604static void i915_audio_component_get_power(struct device *kdev)
Imre Deak58fddc22015-01-08 17:54:14 +0200605{
David Weinehallc49d13e2016-08-22 13:32:42 +0300606 intel_display_power_get(kdev_to_i915(kdev), POWER_DOMAIN_AUDIO);
Imre Deak58fddc22015-01-08 17:54:14 +0200607}
608
David Weinehallc49d13e2016-08-22 13:32:42 +0300609static void i915_audio_component_put_power(struct device *kdev)
Imre Deak58fddc22015-01-08 17:54:14 +0200610{
David Weinehallc49d13e2016-08-22 13:32:42 +0300611 intel_display_power_put(kdev_to_i915(kdev), POWER_DOMAIN_AUDIO);
Imre Deak58fddc22015-01-08 17:54:14 +0200612}
613
David Weinehallc49d13e2016-08-22 13:32:42 +0300614static void i915_audio_component_codec_wake_override(struct device *kdev,
Lu, Han632f3ab2015-05-05 09:05:47 +0800615 bool enable)
616{
David Weinehallc49d13e2016-08-22 13:32:42 +0300617 struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
Lu, Han632f3ab2015-05-05 09:05:47 +0800618 u32 tmp;
619
Rodrigo Vivief11bdb2015-10-28 04:16:45 -0700620 if (!IS_SKYLAKE(dev_priv) && !IS_KABYLAKE(dev_priv))
Lu, Han632f3ab2015-05-05 09:05:47 +0800621 return;
622
David Weinehallc49d13e2016-08-22 13:32:42 +0300623 i915_audio_component_get_power(kdev);
Chris Wilsond838a112016-08-03 17:09:00 +0100624
Lu, Han632f3ab2015-05-05 09:05:47 +0800625 /*
626 * Enable/disable generating the codec wake signal, overriding the
627 * internal logic to generate the codec wake to controller.
628 */
629 tmp = I915_READ(HSW_AUD_CHICKENBIT);
630 tmp &= ~SKL_AUD_CODEC_WAKE_SIGNAL;
631 I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
632 usleep_range(1000, 1500);
633
634 if (enable) {
635 tmp = I915_READ(HSW_AUD_CHICKENBIT);
636 tmp |= SKL_AUD_CODEC_WAKE_SIGNAL;
637 I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
638 usleep_range(1000, 1500);
639 }
Chris Wilsond838a112016-08-03 17:09:00 +0100640
David Weinehallc49d13e2016-08-22 13:32:42 +0300641 i915_audio_component_put_power(kdev);
Lu, Han632f3ab2015-05-05 09:05:47 +0800642}
643
Imre Deak58fddc22015-01-08 17:54:14 +0200644/* Get CDCLK in kHz */
David Weinehallc49d13e2016-08-22 13:32:42 +0300645static int i915_audio_component_get_cdclk_freq(struct device *kdev)
Imre Deak58fddc22015-01-08 17:54:14 +0200646{
David Weinehallc49d13e2016-08-22 13:32:42 +0300647 struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
Imre Deak58fddc22015-01-08 17:54:14 +0200648
649 if (WARN_ON_ONCE(!HAS_DDI(dev_priv)))
650 return -ENODEV;
651
Ville Syrjälä1033f922016-04-26 19:46:33 +0300652 return dev_priv->cdclk_freq;
Imre Deak58fddc22015-01-08 17:54:14 +0200653}
654
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -0700655static struct intel_encoder *get_saved_enc(struct drm_i915_private *dev_priv,
656 int port, int pipe)
657{
658
659 if (WARN_ON(pipe >= I915_MAX_PIPES))
660 return NULL;
661
662 /* MST */
663 if (pipe >= 0)
664 return dev_priv->av_enc_map[pipe];
665
666 /* Non-MST */
667 for_each_pipe(dev_priv, pipe) {
668 struct intel_encoder *encoder;
669
670 encoder = dev_priv->av_enc_map[pipe];
671 if (encoder == NULL)
672 continue;
673
674 if (port == encoder->port)
675 return encoder;
676 }
677
678 return NULL;
679}
680
681static int i915_audio_component_sync_audio_rate(struct device *kdev, int port,
682 int pipe, int rate)
Libin Yang4a21ef72015-09-02 14:11:39 +0800683{
David Weinehallc49d13e2016-08-22 13:32:42 +0300684 struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
Libin Yang4a21ef72015-09-02 14:11:39 +0800685 struct intel_encoder *intel_encoder;
Libin Yang4a21ef72015-09-02 14:11:39 +0800686 struct intel_crtc *crtc;
Jani Nikula8f1ec182016-10-10 18:04:02 +0300687 struct drm_display_mode *adjusted_mode;
Libin Yang7e8275c2015-09-25 09:36:12 +0800688 struct i915_audio_component *acomp = dev_priv->audio_component;
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100689 int err = 0;
Libin Yang4a21ef72015-09-02 14:11:39 +0800690
Libin Yang4bd2d6f2016-10-10 18:04:04 +0300691 if (!HAS_DDI(dev_priv))
Libin Yang4a21ef72015-09-02 14:11:39 +0800692 return 0;
693
David Weinehallc49d13e2016-08-22 13:32:42 +0300694 i915_audio_component_get_power(kdev);
Libin Yang4a21ef72015-09-02 14:11:39 +0800695 mutex_lock(&dev_priv->av_mutex);
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -0700696
Libin Yang4a21ef72015-09-02 14:11:39 +0800697 /* 1. get the pipe */
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -0700698 intel_encoder = get_saved_enc(dev_priv, port, pipe);
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100699 if (!intel_encoder || !intel_encoder->base.crtc ||
700 intel_encoder->type != INTEL_OUTPUT_HDMI) {
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -0700701 DRM_DEBUG_KMS("Not valid for port %c\n", port_name(port));
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100702 err = -ENODEV;
703 goto unlock;
Libin Yang4a21ef72015-09-02 14:11:39 +0800704 }
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100705
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -0700706 /* pipe passed from the audio driver will be -1 for Non-MST case */
707 crtc = to_intel_crtc(intel_encoder->base.crtc);
708 pipe = crtc->pipe;
709
Jani Nikula8f1ec182016-10-10 18:04:02 +0300710 adjusted_mode = &crtc->config->base.adjusted_mode;
Libin Yang4a21ef72015-09-02 14:11:39 +0800711
Libin Yang7e8275c2015-09-25 09:36:12 +0800712 /* port must be valid now, otherwise the pipe will be invalid */
713 acomp->aud_sample_rate[port] = rate;
714
Jani Nikula8f1ec182016-10-10 18:04:02 +0300715 hsw_audio_config_update(crtc, port, adjusted_mode);
Libin Yang4a21ef72015-09-02 14:11:39 +0800716
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100717 unlock:
Libin Yang4a21ef72015-09-02 14:11:39 +0800718 mutex_unlock(&dev_priv->av_mutex);
David Weinehallc49d13e2016-08-22 13:32:42 +0300719 i915_audio_component_put_power(kdev);
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100720 return err;
Libin Yang4a21ef72015-09-02 14:11:39 +0800721}
722
David Weinehallc49d13e2016-08-22 13:32:42 +0300723static int i915_audio_component_get_eld(struct device *kdev, int port,
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -0700724 int pipe, bool *enabled,
Takashi Iwaicae666c2015-11-12 15:23:41 +0100725 unsigned char *buf, int max_bytes)
726{
David Weinehallc49d13e2016-08-22 13:32:42 +0300727 struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
Takashi Iwaicae666c2015-11-12 15:23:41 +0100728 struct intel_encoder *intel_encoder;
Takashi Iwaicae666c2015-11-12 15:23:41 +0100729 const u8 *eld;
730 int ret = -EINVAL;
731
732 mutex_lock(&dev_priv->av_mutex);
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -0700733
734 intel_encoder = get_saved_enc(dev_priv, port, pipe);
735 if (!intel_encoder) {
736 DRM_DEBUG_KMS("Not valid for port %c\n", port_name(port));
737 mutex_unlock(&dev_priv->av_mutex);
738 return ret;
739 }
740
741 ret = 0;
742 *enabled = intel_encoder->audio_connector != NULL;
743 if (*enabled) {
744 eld = intel_encoder->audio_connector->eld;
745 ret = drm_eld_size(eld);
746 memcpy(buf, eld, min(max_bytes, ret));
Takashi Iwaicae666c2015-11-12 15:23:41 +0100747 }
748
749 mutex_unlock(&dev_priv->av_mutex);
750 return ret;
Imre Deak58fddc22015-01-08 17:54:14 +0200751}
752
753static const struct i915_audio_component_ops i915_audio_component_ops = {
754 .owner = THIS_MODULE,
755 .get_power = i915_audio_component_get_power,
756 .put_power = i915_audio_component_put_power,
Lu, Han632f3ab2015-05-05 09:05:47 +0800757 .codec_wake_override = i915_audio_component_codec_wake_override,
Imre Deak58fddc22015-01-08 17:54:14 +0200758 .get_cdclk_freq = i915_audio_component_get_cdclk_freq,
Libin Yang4a21ef72015-09-02 14:11:39 +0800759 .sync_audio_rate = i915_audio_component_sync_audio_rate,
Takashi Iwaicae666c2015-11-12 15:23:41 +0100760 .get_eld = i915_audio_component_get_eld,
Imre Deak58fddc22015-01-08 17:54:14 +0200761};
762
David Weinehallc49d13e2016-08-22 13:32:42 +0300763static int i915_audio_component_bind(struct device *i915_kdev,
764 struct device *hda_kdev, void *data)
Imre Deak58fddc22015-01-08 17:54:14 +0200765{
766 struct i915_audio_component *acomp = data;
David Weinehallc49d13e2016-08-22 13:32:42 +0300767 struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
Libin Yang7e8275c2015-09-25 09:36:12 +0800768 int i;
Imre Deak58fddc22015-01-08 17:54:14 +0200769
770 if (WARN_ON(acomp->ops || acomp->dev))
771 return -EEXIST;
772
Chris Wilson91c8a322016-07-05 10:40:23 +0100773 drm_modeset_lock_all(&dev_priv->drm);
Imre Deak58fddc22015-01-08 17:54:14 +0200774 acomp->ops = &i915_audio_component_ops;
David Weinehallc49d13e2016-08-22 13:32:42 +0300775 acomp->dev = i915_kdev;
Libin Yang7e8275c2015-09-25 09:36:12 +0800776 BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
777 for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
778 acomp->aud_sample_rate[i] = 0;
David Henningsson51e1d832015-08-19 10:48:56 +0200779 dev_priv->audio_component = acomp;
Chris Wilson91c8a322016-07-05 10:40:23 +0100780 drm_modeset_unlock_all(&dev_priv->drm);
Imre Deak58fddc22015-01-08 17:54:14 +0200781
782 return 0;
783}
784
David Weinehallc49d13e2016-08-22 13:32:42 +0300785static void i915_audio_component_unbind(struct device *i915_kdev,
786 struct device *hda_kdev, void *data)
Imre Deak58fddc22015-01-08 17:54:14 +0200787{
788 struct i915_audio_component *acomp = data;
David Weinehallc49d13e2016-08-22 13:32:42 +0300789 struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
Imre Deak58fddc22015-01-08 17:54:14 +0200790
Chris Wilson91c8a322016-07-05 10:40:23 +0100791 drm_modeset_lock_all(&dev_priv->drm);
Imre Deak58fddc22015-01-08 17:54:14 +0200792 acomp->ops = NULL;
793 acomp->dev = NULL;
David Henningsson51e1d832015-08-19 10:48:56 +0200794 dev_priv->audio_component = NULL;
Chris Wilson91c8a322016-07-05 10:40:23 +0100795 drm_modeset_unlock_all(&dev_priv->drm);
Imre Deak58fddc22015-01-08 17:54:14 +0200796}
797
798static const struct component_ops i915_audio_component_bind_ops = {
799 .bind = i915_audio_component_bind,
800 .unbind = i915_audio_component_unbind,
801};
802
803/**
804 * i915_audio_component_init - initialize and register the audio component
805 * @dev_priv: i915 device instance
806 *
807 * This will register with the component framework a child component which
808 * will bind dynamically to the snd_hda_intel driver's corresponding master
809 * component when the latter is registered. During binding the child
810 * initializes an instance of struct i915_audio_component which it receives
811 * from the master. The master can then start to use the interface defined by
812 * this struct. Each side can break the binding at any point by deregistering
813 * its own component after which each side's component unbind callback is
814 * called.
815 *
816 * We ignore any error during registration and continue with reduced
817 * functionality (i.e. without HDMI audio).
818 */
819void i915_audio_component_init(struct drm_i915_private *dev_priv)
820{
821 int ret;
822
Chris Wilson91c8a322016-07-05 10:40:23 +0100823 ret = component_add(dev_priv->drm.dev, &i915_audio_component_bind_ops);
Imre Deak58fddc22015-01-08 17:54:14 +0200824 if (ret < 0) {
825 DRM_ERROR("failed to add audio component (%d)\n", ret);
826 /* continue with reduced functionality */
827 return;
828 }
829
830 dev_priv->audio_component_registered = true;
831}
832
833/**
834 * i915_audio_component_cleanup - deregister the audio component
835 * @dev_priv: i915 device instance
836 *
837 * Deregisters the audio component, breaking any existing binding to the
838 * corresponding snd_hda_intel driver's master component.
839 */
840void i915_audio_component_cleanup(struct drm_i915_private *dev_priv)
841{
842 if (!dev_priv->audio_component_registered)
843 return;
844
Chris Wilson91c8a322016-07-05 10:40:23 +0100845 component_del(dev_priv->drm.dev, &i915_audio_component_bind_ops);
Imre Deak58fddc22015-01-08 17:54:14 +0200846 dev_priv->audio_component_registered = false;
847}