blob: 161f6c2bca360255e0dbf72fce2b99eb30d8dd20 [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
Libin Yang4a21ef72015-09-02 14:11:39 +0800151/* check whether N/CTS/M need be set manually */
152static bool audio_rate_need_prog(struct intel_crtc *crtc,
Takashi Iwai87f77ef2015-09-30 09:39:01 +0200153 const struct drm_display_mode *mode)
Libin Yang4a21ef72015-09-02 14:11:39 +0800154{
155 if (((mode->clock == TMDS_297M) ||
156 (mode->clock == TMDS_296M)) &&
157 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
158 return true;
159 else
160 return false;
161}
162
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200163static bool intel_eld_uptodate(struct drm_connector *connector,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200164 i915_reg_t reg_eldv, uint32_t bits_eldv,
165 i915_reg_t reg_elda, uint32_t bits_elda,
166 i915_reg_t reg_edid)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200167{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100168 struct drm_i915_private *dev_priv = to_i915(connector->dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200169 uint8_t *eld = connector->eld;
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200170 uint32_t tmp;
171 int i;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200172
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200173 tmp = I915_READ(reg_eldv);
174 tmp &= bits_eldv;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200175
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200176 if (!tmp)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200177 return false;
178
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200179 tmp = I915_READ(reg_elda);
180 tmp &= ~bits_elda;
181 I915_WRITE(reg_elda, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200182
Jani Nikula938fd8a2014-10-28 16:20:48 +0200183 for (i = 0; i < drm_eld_size(eld) / 4; i++)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200184 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
185 return false;
186
187 return true;
188}
189
Jani Nikula76d8d3e2014-10-27 16:26:57 +0200190static void g4x_audio_codec_disable(struct intel_encoder *encoder)
191{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100192 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Jani Nikula76d8d3e2014-10-27 16:26:57 +0200193 uint32_t eldv, tmp;
194
195 DRM_DEBUG_KMS("Disable audio codec\n");
196
197 tmp = I915_READ(G4X_AUD_VID_DID);
198 if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
199 eldv = G4X_ELDV_DEVCL_DEVBLC;
200 else
201 eldv = G4X_ELDV_DEVCTG;
202
203 /* Invalidate ELD */
204 tmp = I915_READ(G4X_AUD_CNTL_ST);
205 tmp &= ~eldv;
206 I915_WRITE(G4X_AUD_CNTL_ST, tmp);
207}
208
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200209static void g4x_audio_codec_enable(struct drm_connector *connector,
210 struct intel_encoder *encoder,
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300211 const struct drm_display_mode *adjusted_mode)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200212{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100213 struct drm_i915_private *dev_priv = to_i915(connector->dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200214 uint8_t *eld = connector->eld;
215 uint32_t eldv;
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200216 uint32_t tmp;
217 int len, i;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200218
Jani Nikulad5ee08d2014-10-27 16:26:58 +0200219 DRM_DEBUG_KMS("Enable audio codec, %u bytes ELD\n", eld[2]);
220
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200221 tmp = I915_READ(G4X_AUD_VID_DID);
222 if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200223 eldv = G4X_ELDV_DEVCL_DEVBLC;
224 else
225 eldv = G4X_ELDV_DEVCTG;
226
227 if (intel_eld_uptodate(connector,
228 G4X_AUD_CNTL_ST, eldv,
Jani Nikulac46f1112014-10-27 16:26:52 +0200229 G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200230 G4X_HDMIW_HDMIEDID))
231 return;
232
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200233 tmp = I915_READ(G4X_AUD_CNTL_ST);
Jani Nikulac46f1112014-10-27 16:26:52 +0200234 tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200235 len = (tmp >> 9) & 0x1f; /* ELD buffer size */
236 I915_WRITE(G4X_AUD_CNTL_ST, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200237
Jani Nikula938fd8a2014-10-28 16:20:48 +0200238 len = min(drm_eld_size(eld) / 4, len);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200239 DRM_DEBUG_DRIVER("ELD size %d\n", len);
240 for (i = 0; i < len; i++)
241 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
242
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200243 tmp = I915_READ(G4X_AUD_CNTL_ST);
244 tmp |= eldv;
245 I915_WRITE(G4X_AUD_CNTL_ST, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200246}
247
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200248static void hsw_audio_codec_disable(struct intel_encoder *encoder)
249{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100250 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Jani Nikula5fad84a2014-11-04 10:30:23 +0200251 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
252 enum pipe pipe = intel_crtc->pipe;
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200253 uint32_t tmp;
254
Jani Nikula5fad84a2014-11-04 10:30:23 +0200255 DRM_DEBUG_KMS("Disable audio codec on pipe %c\n", pipe_name(pipe));
256
Libin Yang4a21ef72015-09-02 14:11:39 +0800257 mutex_lock(&dev_priv->av_mutex);
258
Jani Nikula5fad84a2014-11-04 10:30:23 +0200259 /* Disable timestamps */
260 tmp = I915_READ(HSW_AUD_CFG(pipe));
261 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
262 tmp |= AUD_CONFIG_N_PROG_ENABLE;
263 tmp &= ~AUD_CONFIG_UPPER_N_MASK;
264 tmp &= ~AUD_CONFIG_LOWER_N_MASK;
Lyude5a8f97e2016-05-03 11:01:32 -0400265 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
Jani Nikula5fad84a2014-11-04 10:30:23 +0200266 tmp |= AUD_CONFIG_N_VALUE_INDEX;
267 I915_WRITE(HSW_AUD_CFG(pipe), tmp);
268
269 /* Invalidate ELD */
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200270 tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
Jani Nikula82910ac2014-10-27 16:26:59 +0200271 tmp &= ~AUDIO_ELD_VALID(pipe);
Jani Nikulaeb45fa02014-11-18 12:11:29 +0200272 tmp &= ~AUDIO_OUTPUT_ENABLE(pipe);
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200273 I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
Libin Yang4a21ef72015-09-02 14:11:39 +0800274
275 mutex_unlock(&dev_priv->av_mutex);
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200276}
277
278static void hsw_audio_codec_enable(struct drm_connector *connector,
279 struct intel_encoder *encoder,
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300280 const struct drm_display_mode *adjusted_mode)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200281{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100282 struct drm_i915_private *dev_priv = to_i915(connector->dev);
Jani Nikula820d2d72014-10-27 16:26:47 +0200283 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
Jani Nikula5fad84a2014-11-04 10:30:23 +0200284 enum pipe pipe = intel_crtc->pipe;
Libin Yang7e8275c2015-09-25 09:36:12 +0800285 struct i915_audio_component *acomp = dev_priv->audio_component;
Jani Nikula5fad84a2014-11-04 10:30:23 +0200286 const uint8_t *eld = connector->eld;
Libin Yang7e8275c2015-09-25 09:36:12 +0800287 struct intel_digital_port *intel_dig_port =
288 enc_to_dig_port(&encoder->base);
289 enum port port = intel_dig_port->port;
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200290 uint32_t tmp;
291 int len, i;
Libin Yang7e8275c2015-09-25 09:36:12 +0800292 int n, rate;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200293
Jani Nikula5fad84a2014-11-04 10:30:23 +0200294 DRM_DEBUG_KMS("Enable audio codec on pipe %c, %u bytes ELD\n",
Jani Nikula938fd8a2014-10-28 16:20:48 +0200295 pipe_name(pipe), drm_eld_size(eld));
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200296
Libin Yang4a21ef72015-09-02 14:11:39 +0800297 mutex_lock(&dev_priv->av_mutex);
298
Jani Nikula5fad84a2014-11-04 10:30:23 +0200299 /* Enable audio presence detect, invalidate ELD */
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200300 tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
Jani Nikula82910ac2014-10-27 16:26:59 +0200301 tmp |= AUDIO_OUTPUT_ENABLE(pipe);
302 tmp &= ~AUDIO_ELD_VALID(pipe);
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200303 I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
Jani Nikula5fad84a2014-11-04 10:30:23 +0200304
305 /*
306 * FIXME: We're supposed to wait for vblank here, but we have vblanks
307 * disabled during the mode set. The proper fix would be to push the
308 * rest of the setup into a vblank work item, queued here, but the
309 * infrastructure is not there yet.
310 */
311
312 /* Reset ELD write address */
313 tmp = I915_READ(HSW_AUD_DIP_ELD_CTRL(pipe));
314 tmp &= ~IBX_ELD_ADDRESS_MASK;
315 I915_WRITE(HSW_AUD_DIP_ELD_CTRL(pipe), tmp);
316
317 /* Up to 84 bytes of hw ELD buffer */
Jani Nikula938fd8a2014-10-28 16:20:48 +0200318 len = min(drm_eld_size(eld), 84);
319 for (i = 0; i < len / 4; i++)
Jani Nikula5fad84a2014-11-04 10:30:23 +0200320 I915_WRITE(HSW_AUD_EDID_DATA(pipe), *((uint32_t *)eld + i));
321
322 /* ELD valid */
323 tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
Jani Nikula82910ac2014-10-27 16:26:59 +0200324 tmp |= AUDIO_ELD_VALID(pipe);
Jani Nikula5fad84a2014-11-04 10:30:23 +0200325 I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
326
327 /* Enable timestamps */
328 tmp = I915_READ(HSW_AUD_CFG(pipe));
329 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
Jani Nikula5fad84a2014-11-04 10:30:23 +0200330 tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
331 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
332 tmp |= AUD_CONFIG_N_VALUE_INDEX;
333 else
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300334 tmp |= audio_config_hdmi_pixel_clock(adjusted_mode);
Libin Yang7e8275c2015-09-25 09:36:12 +0800335
336 tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
Daniel Vetter28446592015-10-07 15:34:15 +0200337 if (audio_rate_need_prog(intel_crtc, adjusted_mode)) {
Libin Yang7e8275c2015-09-25 09:36:12 +0800338 if (!acomp)
339 rate = 0;
340 else if (port >= PORT_A && port <= PORT_E)
341 rate = acomp->aud_sample_rate[port];
342 else {
343 DRM_ERROR("invalid port: %d\n", port);
344 rate = 0;
345 }
Daniel Vetter28446592015-10-07 15:34:15 +0200346 n = audio_config_get_n(adjusted_mode, rate);
Libin Yang7e8275c2015-09-25 09:36:12 +0800347 if (n != 0)
348 tmp = audio_config_setup_n_reg(n, tmp);
349 else
350 DRM_DEBUG_KMS("no suitable N value is found\n");
351 }
352
Jani Nikula5fad84a2014-11-04 10:30:23 +0200353 I915_WRITE(HSW_AUD_CFG(pipe), tmp);
Libin Yang4a21ef72015-09-02 14:11:39 +0800354
355 mutex_unlock(&dev_priv->av_mutex);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200356}
357
Jani Nikula495a5bb2014-10-27 16:26:55 +0200358static void ilk_audio_codec_disable(struct intel_encoder *encoder)
359{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100360 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Jani Nikula495a5bb2014-10-27 16:26:55 +0200361 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
362 struct intel_digital_port *intel_dig_port =
363 enc_to_dig_port(&encoder->base);
364 enum port port = intel_dig_port->port;
365 enum pipe pipe = intel_crtc->pipe;
366 uint32_t tmp, eldv;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200367 i915_reg_t aud_config, aud_cntrl_st2;
Jani Nikula495a5bb2014-10-27 16:26:55 +0200368
369 DRM_DEBUG_KMS("Disable audio codec on port %c, pipe %c\n",
370 port_name(port), pipe_name(pipe));
371
Jani Nikulad3902c32015-05-04 17:20:49 +0300372 if (WARN_ON(port == PORT_A))
373 return;
374
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +0300375 if (HAS_PCH_IBX(dev_priv)) {
Jani Nikula495a5bb2014-10-27 16:26:55 +0200376 aud_config = IBX_AUD_CFG(pipe);
377 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wayne Boyer666a4532015-12-09 12:29:35 -0800378 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikula495a5bb2014-10-27 16:26:55 +0200379 aud_config = VLV_AUD_CFG(pipe);
380 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
381 } else {
382 aud_config = CPT_AUD_CFG(pipe);
383 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
384 }
385
386 /* Disable timestamps */
387 tmp = I915_READ(aud_config);
388 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
389 tmp |= AUD_CONFIG_N_PROG_ENABLE;
390 tmp &= ~AUD_CONFIG_UPPER_N_MASK;
391 tmp &= ~AUD_CONFIG_LOWER_N_MASK;
392 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
393 tmp |= AUD_CONFIG_N_VALUE_INDEX;
394 I915_WRITE(aud_config, tmp);
395
Jani Nikulad3902c32015-05-04 17:20:49 +0300396 eldv = IBX_ELD_VALID(port);
Jani Nikula495a5bb2014-10-27 16:26:55 +0200397
398 /* Invalidate ELD */
399 tmp = I915_READ(aud_cntrl_st2);
400 tmp &= ~eldv;
401 I915_WRITE(aud_cntrl_st2, tmp);
402}
403
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200404static void ilk_audio_codec_enable(struct drm_connector *connector,
405 struct intel_encoder *encoder,
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300406 const struct drm_display_mode *adjusted_mode)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200407{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100408 struct drm_i915_private *dev_priv = to_i915(connector->dev);
Jani Nikula820d2d72014-10-27 16:26:47 +0200409 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
Jani Nikulac6bde932014-11-04 10:31:28 +0200410 struct intel_digital_port *intel_dig_port =
411 enc_to_dig_port(&encoder->base);
412 enum port port = intel_dig_port->port;
413 enum pipe pipe = intel_crtc->pipe;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200414 uint8_t *eld = connector->eld;
415 uint32_t eldv;
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200416 uint32_t tmp;
417 int len, i;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200418 i915_reg_t hdmiw_hdmiedid, aud_config, aud_cntl_st, aud_cntrl_st2;
Jani Nikulac6bde932014-11-04 10:31:28 +0200419
420 DRM_DEBUG_KMS("Enable audio codec on port %c, pipe %c, %u bytes ELD\n",
Jani Nikula938fd8a2014-10-28 16:20:48 +0200421 port_name(port), pipe_name(pipe), drm_eld_size(eld));
Jani Nikulac6bde932014-11-04 10:31:28 +0200422
Jani Nikulad3902c32015-05-04 17:20:49 +0300423 if (WARN_ON(port == PORT_A))
424 return;
425
Jani Nikulac6bde932014-11-04 10:31:28 +0200426 /*
427 * FIXME: We're supposed to wait for vblank here, but we have vblanks
428 * disabled during the mode set. The proper fix would be to push the
429 * rest of the setup into a vblank work item, queued here, but the
430 * infrastructure is not there yet.
431 */
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200432
433 if (HAS_PCH_IBX(connector->dev)) {
434 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
435 aud_config = IBX_AUD_CFG(pipe);
436 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
437 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wayne Boyer666a4532015-12-09 12:29:35 -0800438 } else if (IS_VALLEYVIEW(connector->dev) ||
439 IS_CHERRYVIEW(connector->dev)) {
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200440 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
441 aud_config = VLV_AUD_CFG(pipe);
442 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
443 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
444 } else {
445 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
446 aud_config = CPT_AUD_CFG(pipe);
447 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
448 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
449 }
450
Jani Nikulad3902c32015-05-04 17:20:49 +0300451 eldv = IBX_ELD_VALID(port);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200452
Jani Nikulac6bde932014-11-04 10:31:28 +0200453 /* Invalidate ELD */
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200454 tmp = I915_READ(aud_cntrl_st2);
455 tmp &= ~eldv;
456 I915_WRITE(aud_cntrl_st2, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200457
Jani Nikulac6bde932014-11-04 10:31:28 +0200458 /* Reset ELD write address */
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200459 tmp = I915_READ(aud_cntl_st);
Jani Nikulac46f1112014-10-27 16:26:52 +0200460 tmp &= ~IBX_ELD_ADDRESS_MASK;
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200461 I915_WRITE(aud_cntl_st, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200462
Jani Nikulac6bde932014-11-04 10:31:28 +0200463 /* Up to 84 bytes of hw ELD buffer */
Jani Nikula938fd8a2014-10-28 16:20:48 +0200464 len = min(drm_eld_size(eld), 84);
465 for (i = 0; i < len / 4; i++)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200466 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
467
Jani Nikulac6bde932014-11-04 10:31:28 +0200468 /* ELD valid */
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200469 tmp = I915_READ(aud_cntrl_st2);
470 tmp |= eldv;
471 I915_WRITE(aud_cntrl_st2, tmp);
Jani Nikulac6bde932014-11-04 10:31:28 +0200472
473 /* Enable timestamps */
474 tmp = I915_READ(aud_config);
475 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
476 tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
477 tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
Lyude5a8f97e2016-05-03 11:01:32 -0400478 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
Jani Nikulac6bde932014-11-04 10:31:28 +0200479 tmp |= AUD_CONFIG_N_VALUE_INDEX;
480 else
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300481 tmp |= audio_config_hdmi_pixel_clock(adjusted_mode);
Jani Nikulac6bde932014-11-04 10:31:28 +0200482 I915_WRITE(aud_config, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200483}
484
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200485/**
486 * intel_audio_codec_enable - Enable the audio codec for HD audio
487 * @intel_encoder: encoder on which to enable audio
488 *
489 * The enable sequences may only be performed after enabling the transcoder and
490 * port, and after completed link training.
491 */
492void intel_audio_codec_enable(struct intel_encoder *intel_encoder)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200493{
Jani Nikula33d1e7c62014-10-27 16:26:46 +0200494 struct drm_encoder *encoder = &intel_encoder->base;
495 struct intel_crtc *crtc = to_intel_crtc(encoder->crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300496 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200497 struct drm_connector *connector;
498 struct drm_device *dev = encoder->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100499 struct drm_i915_private *dev_priv = to_i915(dev);
David Henningsson51e1d832015-08-19 10:48:56 +0200500 struct i915_audio_component *acomp = dev_priv->audio_component;
501 struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
502 enum port port = intel_dig_port->port;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200503
Ville Syrjälä9e5a3b52015-09-07 18:22:57 +0300504 connector = drm_select_eld(encoder);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200505 if (!connector)
506 return;
507
508 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
509 connector->base.id,
510 connector->name,
511 connector->encoder->base.id,
512 connector->encoder->name);
513
Jani Nikula6189b032014-10-28 13:53:01 +0200514 /* ELD Conn_Type */
515 connector->eld[5] &= ~(3 << 2);
Lyude5a8f97e2016-05-03 11:01:32 -0400516 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
Jani Nikula6189b032014-10-28 13:53:01 +0200517 connector->eld[5] |= (1 << 2);
518
Ville Syrjälä124abe02015-09-08 13:40:45 +0300519 connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200520
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200521 if (dev_priv->display.audio_codec_enable)
Ville Syrjälä124abe02015-09-08 13:40:45 +0300522 dev_priv->display.audio_codec_enable(connector, intel_encoder,
523 adjusted_mode);
David Henningsson51e1d832015-08-19 10:48:56 +0200524
Takashi Iwaicae666c2015-11-12 15:23:41 +0100525 mutex_lock(&dev_priv->av_mutex);
526 intel_dig_port->audio_connector = connector;
Takashi Iwai9dfbffc2016-02-24 15:35:22 +0100527 /* referred in audio callbacks */
528 dev_priv->dig_port_map[port] = intel_encoder;
Takashi Iwaicae666c2015-11-12 15:23:41 +0100529 mutex_unlock(&dev_priv->av_mutex);
530
David Henningsson51e1d832015-08-19 10:48:56 +0200531 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
David Henningssonf0675d42015-09-03 11:51:34 +0200532 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr, (int) port);
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200533}
534
535/**
536 * intel_audio_codec_disable - Disable the audio codec for HD audio
Geliang Tang95d0be62015-09-15 06:04:36 -0700537 * @intel_encoder: encoder on which to disable audio
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200538 *
539 * The disable sequences must be performed before disabling the transcoder or
540 * port.
541 */
David Henningsson51e1d832015-08-19 10:48:56 +0200542void intel_audio_codec_disable(struct intel_encoder *intel_encoder)
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200543{
David Henningsson51e1d832015-08-19 10:48:56 +0200544 struct drm_encoder *encoder = &intel_encoder->base;
545 struct drm_device *dev = encoder->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100546 struct drm_i915_private *dev_priv = to_i915(dev);
David Henningsson51e1d832015-08-19 10:48:56 +0200547 struct i915_audio_component *acomp = dev_priv->audio_component;
548 struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
549 enum port port = intel_dig_port->port;
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200550
551 if (dev_priv->display.audio_codec_disable)
David Henningsson51e1d832015-08-19 10:48:56 +0200552 dev_priv->display.audio_codec_disable(intel_encoder);
553
Takashi Iwaicae666c2015-11-12 15:23:41 +0100554 mutex_lock(&dev_priv->av_mutex);
555 intel_dig_port->audio_connector = NULL;
Takashi Iwai9dfbffc2016-02-24 15:35:22 +0100556 dev_priv->dig_port_map[port] = NULL;
Takashi Iwaicae666c2015-11-12 15:23:41 +0100557 mutex_unlock(&dev_priv->av_mutex);
558
David Henningsson51e1d832015-08-19 10:48:56 +0200559 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
David Henningssonf0675d42015-09-03 11:51:34 +0200560 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr, (int) port);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200561}
562
563/**
Imre Deak88212942016-03-16 13:38:53 +0200564 * intel_init_audio_hooks - Set up chip specific audio hooks
565 * @dev_priv: device private
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200566 */
Imre Deak88212942016-03-16 13:38:53 +0200567void intel_init_audio_hooks(struct drm_i915_private *dev_priv)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200568{
Imre Deak88212942016-03-16 13:38:53 +0200569 if (IS_G4X(dev_priv)) {
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200570 dev_priv->display.audio_codec_enable = g4x_audio_codec_enable;
Jani Nikula76d8d3e2014-10-27 16:26:57 +0200571 dev_priv->display.audio_codec_disable = g4x_audio_codec_disable;
Imre Deak88212942016-03-16 13:38:53 +0200572 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200573 dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
Jani Nikula495a5bb2014-10-27 16:26:55 +0200574 dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
Imre Deak88212942016-03-16 13:38:53 +0200575 } else if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8) {
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200576 dev_priv->display.audio_codec_enable = hsw_audio_codec_enable;
577 dev_priv->display.audio_codec_disable = hsw_audio_codec_disable;
Imre Deak88212942016-03-16 13:38:53 +0200578 } else if (HAS_PCH_SPLIT(dev_priv)) {
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200579 dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
Jani Nikula495a5bb2014-10-27 16:26:55 +0200580 dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
Jani Nikula69bfe1a2014-10-27 16:26:50 +0200581 }
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200582}
Imre Deak58fddc22015-01-08 17:54:14 +0200583
584static void i915_audio_component_get_power(struct device *dev)
585{
586 intel_display_power_get(dev_to_i915(dev), POWER_DOMAIN_AUDIO);
587}
588
589static void i915_audio_component_put_power(struct device *dev)
590{
591 intel_display_power_put(dev_to_i915(dev), POWER_DOMAIN_AUDIO);
592}
593
Lu, Han632f3ab2015-05-05 09:05:47 +0800594static void i915_audio_component_codec_wake_override(struct device *dev,
595 bool enable)
596{
597 struct drm_i915_private *dev_priv = dev_to_i915(dev);
598 u32 tmp;
599
Rodrigo Vivief11bdb2015-10-28 04:16:45 -0700600 if (!IS_SKYLAKE(dev_priv) && !IS_KABYLAKE(dev_priv))
Lu, Han632f3ab2015-05-05 09:05:47 +0800601 return;
602
603 /*
604 * Enable/disable generating the codec wake signal, overriding the
605 * internal logic to generate the codec wake to controller.
606 */
607 tmp = I915_READ(HSW_AUD_CHICKENBIT);
608 tmp &= ~SKL_AUD_CODEC_WAKE_SIGNAL;
609 I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
610 usleep_range(1000, 1500);
611
612 if (enable) {
613 tmp = I915_READ(HSW_AUD_CHICKENBIT);
614 tmp |= SKL_AUD_CODEC_WAKE_SIGNAL;
615 I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
616 usleep_range(1000, 1500);
617 }
618}
619
Imre Deak58fddc22015-01-08 17:54:14 +0200620/* Get CDCLK in kHz */
621static int i915_audio_component_get_cdclk_freq(struct device *dev)
622{
623 struct drm_i915_private *dev_priv = dev_to_i915(dev);
Imre Deak58fddc22015-01-08 17:54:14 +0200624
625 if (WARN_ON_ONCE(!HAS_DDI(dev_priv)))
626 return -ENODEV;
627
Ville Syrjälä1033f922016-04-26 19:46:33 +0300628 return dev_priv->cdclk_freq;
Imre Deak58fddc22015-01-08 17:54:14 +0200629}
630
Libin Yang4a21ef72015-09-02 14:11:39 +0800631static int i915_audio_component_sync_audio_rate(struct device *dev,
632 int port, int rate)
633{
634 struct drm_i915_private *dev_priv = dev_to_i915(dev);
Libin Yang4a21ef72015-09-02 14:11:39 +0800635 struct intel_encoder *intel_encoder;
Libin Yang4a21ef72015-09-02 14:11:39 +0800636 struct intel_crtc *crtc;
637 struct drm_display_mode *mode;
Libin Yang7e8275c2015-09-25 09:36:12 +0800638 struct i915_audio_component *acomp = dev_priv->audio_component;
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100639 enum pipe pipe = INVALID_PIPE;
Libin Yang4a21ef72015-09-02 14:11:39 +0800640 u32 tmp;
Libin Yang7e8275c2015-09-25 09:36:12 +0800641 int n;
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100642 int err = 0;
Libin Yang4a21ef72015-09-02 14:11:39 +0800643
Rodrigo Vivief11bdb2015-10-28 04:16:45 -0700644 /* HSW, BDW, SKL, KBL need this fix */
Libin Yang4a21ef72015-09-02 14:11:39 +0800645 if (!IS_SKYLAKE(dev_priv) &&
Rodrigo Vivief11bdb2015-10-28 04:16:45 -0700646 !IS_KABYLAKE(dev_priv) &&
647 !IS_BROADWELL(dev_priv) &&
648 !IS_HASWELL(dev_priv))
Libin Yang4a21ef72015-09-02 14:11:39 +0800649 return 0;
650
651 mutex_lock(&dev_priv->av_mutex);
652 /* 1. get the pipe */
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100653 intel_encoder = dev_priv->dig_port_map[port];
654 /* intel_encoder might be NULL for DP MST */
655 if (!intel_encoder || !intel_encoder->base.crtc ||
656 intel_encoder->type != INTEL_OUTPUT_HDMI) {
657 DRM_DEBUG_KMS("no valid port %c\n", port_name(port));
658 err = -ENODEV;
659 goto unlock;
Libin Yang4a21ef72015-09-02 14:11:39 +0800660 }
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100661 crtc = to_intel_crtc(intel_encoder->base.crtc);
662 pipe = crtc->pipe;
Libin Yang4a21ef72015-09-02 14:11:39 +0800663 if (pipe == INVALID_PIPE) {
664 DRM_DEBUG_KMS("no pipe for the port %c\n", port_name(port));
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100665 err = -ENODEV;
666 goto unlock;
Libin Yang4a21ef72015-09-02 14:11:39 +0800667 }
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100668
Libin Yang4a21ef72015-09-02 14:11:39 +0800669 DRM_DEBUG_KMS("pipe %c connects port %c\n",
670 pipe_name(pipe), port_name(port));
671 mode = &crtc->config->base.adjusted_mode;
672
Libin Yang7e8275c2015-09-25 09:36:12 +0800673 /* port must be valid now, otherwise the pipe will be invalid */
674 acomp->aud_sample_rate[port] = rate;
675
Libin Yang4a21ef72015-09-02 14:11:39 +0800676 /* 2. check whether to set the N/CTS/M manually or not */
677 if (!audio_rate_need_prog(crtc, mode)) {
678 tmp = I915_READ(HSW_AUD_CFG(pipe));
679 tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
680 I915_WRITE(HSW_AUD_CFG(pipe), tmp);
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100681 goto unlock;
Libin Yang4a21ef72015-09-02 14:11:39 +0800682 }
683
684 n = audio_config_get_n(mode, rate);
685 if (n == 0) {
686 DRM_DEBUG_KMS("Using automatic mode for N value on port %c\n",
687 port_name(port));
688 tmp = I915_READ(HSW_AUD_CFG(pipe));
689 tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
690 I915_WRITE(HSW_AUD_CFG(pipe), tmp);
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100691 goto unlock;
Libin Yang4a21ef72015-09-02 14:11:39 +0800692 }
Libin Yang4a21ef72015-09-02 14:11:39 +0800693
Libin Yang7e8275c2015-09-25 09:36:12 +0800694 /* 3. set the N/CTS/M */
Libin Yang4a21ef72015-09-02 14:11:39 +0800695 tmp = I915_READ(HSW_AUD_CFG(pipe));
Libin Yang7e8275c2015-09-25 09:36:12 +0800696 tmp = audio_config_setup_n_reg(n, tmp);
Libin Yang4a21ef72015-09-02 14:11:39 +0800697 I915_WRITE(HSW_AUD_CFG(pipe), tmp);
698
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100699 unlock:
Libin Yang4a21ef72015-09-02 14:11:39 +0800700 mutex_unlock(&dev_priv->av_mutex);
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100701 return err;
Libin Yang4a21ef72015-09-02 14:11:39 +0800702}
703
Takashi Iwaicae666c2015-11-12 15:23:41 +0100704static int i915_audio_component_get_eld(struct device *dev, int port,
705 bool *enabled,
706 unsigned char *buf, int max_bytes)
707{
708 struct drm_i915_private *dev_priv = dev_to_i915(dev);
Takashi Iwaicae666c2015-11-12 15:23:41 +0100709 struct intel_encoder *intel_encoder;
710 struct intel_digital_port *intel_dig_port;
711 const u8 *eld;
712 int ret = -EINVAL;
713
714 mutex_lock(&dev_priv->av_mutex);
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100715 intel_encoder = dev_priv->dig_port_map[port];
716 /* intel_encoder might be NULL for DP MST */
717 if (intel_encoder) {
718 ret = 0;
Takashi Iwaicae666c2015-11-12 15:23:41 +0100719 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Takashi Iwai0bdf5a02015-11-30 18:19:39 +0100720 *enabled = intel_dig_port->audio_connector != NULL;
721 if (*enabled) {
Takashi Iwaicae666c2015-11-12 15:23:41 +0100722 eld = intel_dig_port->audio_connector->eld;
723 ret = drm_eld_size(eld);
724 memcpy(buf, eld, min(max_bytes, ret));
Takashi Iwaicae666c2015-11-12 15:23:41 +0100725 }
726 }
727
728 mutex_unlock(&dev_priv->av_mutex);
729 return ret;
Imre Deak58fddc22015-01-08 17:54:14 +0200730}
731
732static const struct i915_audio_component_ops i915_audio_component_ops = {
733 .owner = THIS_MODULE,
734 .get_power = i915_audio_component_get_power,
735 .put_power = i915_audio_component_put_power,
Lu, Han632f3ab2015-05-05 09:05:47 +0800736 .codec_wake_override = i915_audio_component_codec_wake_override,
Imre Deak58fddc22015-01-08 17:54:14 +0200737 .get_cdclk_freq = i915_audio_component_get_cdclk_freq,
Libin Yang4a21ef72015-09-02 14:11:39 +0800738 .sync_audio_rate = i915_audio_component_sync_audio_rate,
Takashi Iwaicae666c2015-11-12 15:23:41 +0100739 .get_eld = i915_audio_component_get_eld,
Imre Deak58fddc22015-01-08 17:54:14 +0200740};
741
742static int i915_audio_component_bind(struct device *i915_dev,
743 struct device *hda_dev, void *data)
744{
745 struct i915_audio_component *acomp = data;
David Henningsson51e1d832015-08-19 10:48:56 +0200746 struct drm_i915_private *dev_priv = dev_to_i915(i915_dev);
Libin Yang7e8275c2015-09-25 09:36:12 +0800747 int i;
Imre Deak58fddc22015-01-08 17:54:14 +0200748
749 if (WARN_ON(acomp->ops || acomp->dev))
750 return -EEXIST;
751
Chris Wilson91c8a322016-07-05 10:40:23 +0100752 drm_modeset_lock_all(&dev_priv->drm);
Imre Deak58fddc22015-01-08 17:54:14 +0200753 acomp->ops = &i915_audio_component_ops;
754 acomp->dev = i915_dev;
Libin Yang7e8275c2015-09-25 09:36:12 +0800755 BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
756 for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
757 acomp->aud_sample_rate[i] = 0;
David Henningsson51e1d832015-08-19 10:48:56 +0200758 dev_priv->audio_component = acomp;
Chris Wilson91c8a322016-07-05 10:40:23 +0100759 drm_modeset_unlock_all(&dev_priv->drm);
Imre Deak58fddc22015-01-08 17:54:14 +0200760
761 return 0;
762}
763
764static void i915_audio_component_unbind(struct device *i915_dev,
765 struct device *hda_dev, void *data)
766{
767 struct i915_audio_component *acomp = data;
David Henningsson51e1d832015-08-19 10:48:56 +0200768 struct drm_i915_private *dev_priv = dev_to_i915(i915_dev);
Imre Deak58fddc22015-01-08 17:54:14 +0200769
Chris Wilson91c8a322016-07-05 10:40:23 +0100770 drm_modeset_lock_all(&dev_priv->drm);
Imre Deak58fddc22015-01-08 17:54:14 +0200771 acomp->ops = NULL;
772 acomp->dev = NULL;
David Henningsson51e1d832015-08-19 10:48:56 +0200773 dev_priv->audio_component = NULL;
Chris Wilson91c8a322016-07-05 10:40:23 +0100774 drm_modeset_unlock_all(&dev_priv->drm);
Imre Deak58fddc22015-01-08 17:54:14 +0200775}
776
777static const struct component_ops i915_audio_component_bind_ops = {
778 .bind = i915_audio_component_bind,
779 .unbind = i915_audio_component_unbind,
780};
781
782/**
783 * i915_audio_component_init - initialize and register the audio component
784 * @dev_priv: i915 device instance
785 *
786 * This will register with the component framework a child component which
787 * will bind dynamically to the snd_hda_intel driver's corresponding master
788 * component when the latter is registered. During binding the child
789 * initializes an instance of struct i915_audio_component which it receives
790 * from the master. The master can then start to use the interface defined by
791 * this struct. Each side can break the binding at any point by deregistering
792 * its own component after which each side's component unbind callback is
793 * called.
794 *
795 * We ignore any error during registration and continue with reduced
796 * functionality (i.e. without HDMI audio).
797 */
798void i915_audio_component_init(struct drm_i915_private *dev_priv)
799{
800 int ret;
801
Chris Wilson91c8a322016-07-05 10:40:23 +0100802 ret = component_add(dev_priv->drm.dev, &i915_audio_component_bind_ops);
Imre Deak58fddc22015-01-08 17:54:14 +0200803 if (ret < 0) {
804 DRM_ERROR("failed to add audio component (%d)\n", ret);
805 /* continue with reduced functionality */
806 return;
807 }
808
809 dev_priv->audio_component_registered = true;
810}
811
812/**
813 * i915_audio_component_cleanup - deregister the audio component
814 * @dev_priv: i915 device instance
815 *
816 * Deregisters the audio component, breaking any existing binding to the
817 * corresponding snd_hda_intel driver's master component.
818 */
819void i915_audio_component_cleanup(struct drm_i915_private *dev_priv)
820{
821 if (!dev_priv->audio_component_registered)
822 return;
823
Chris Wilson91c8a322016-07-05 10:40:23 +0100824 component_del(dev_priv->drm.dev, &i915_audio_component_bind_ops);
Imre Deak58fddc22015-01-08 17:54:14 +0200825 dev_priv->audio_component_registered = false;
826}