drm/i915/ddi: add break in DDI mode select switch
[linux-block.git] / drivers / gpu / drm / i915 / intel_audio.c
CommitLineData
7c10a2b5
JN
1/*
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>
25
26#include <drm/drmP.h>
27#include <drm/drm_edid.h>
28#include "intel_drv.h"
29#include "i915_drv.h"
30
28855d2a
JN
31/**
32 * DOC: High Definition Audio over HDMI and Display Port
33 *
34 * The graphics and audio drivers together support High Definition Audio over
35 * HDMI and Display Port. The audio programming sequences are divided into audio
36 * codec and controller enable and disable sequences. The graphics driver
37 * handles the audio codec sequences, while the audio driver handles the audio
38 * controller sequences.
39 *
40 * The disable sequences must be performed before disabling the transcoder or
41 * port. The enable sequences may only be performed after enabling the
42 * transcoder and port, and after completed link training.
43 *
44 * The codec and controller sequences could be done either parallel or serial,
45 * but generally the ELDV/PD change in the codec sequence indicates to the audio
46 * driver that the controller sequence should start. Indeed, most of the
47 * co-operation between the graphics and audio drivers is handled via audio
48 * related registers. (The notable exception is the power management, not
49 * covered here.)
50 */
51
87fcb2ad 52static const struct {
7c10a2b5
JN
53 int clock;
54 u32 config;
55} hdmi_audio_clock[] = {
56 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
57 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
58 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
59 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
60 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
61 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
62 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
63 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
64 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
65 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
66};
67
68/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
69static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
70{
71 int i;
72
73 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
74 if (mode->clock == hdmi_audio_clock[i].clock)
75 break;
76 }
77
78 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
79 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
80 i = 1;
81 }
82
83 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
84 hdmi_audio_clock[i].clock,
85 hdmi_audio_clock[i].config);
86
87 return hdmi_audio_clock[i].config;
88}
89
90static bool intel_eld_uptodate(struct drm_connector *connector,
91 int reg_eldv, uint32_t bits_eldv,
92 int reg_elda, uint32_t bits_elda,
93 int reg_edid)
94{
95 struct drm_i915_private *dev_priv = connector->dev->dev_private;
96 uint8_t *eld = connector->eld;
f9f682ae
JN
97 uint32_t tmp;
98 int i;
7c10a2b5 99
f9f682ae
JN
100 tmp = I915_READ(reg_eldv);
101 tmp &= bits_eldv;
7c10a2b5 102
f9f682ae 103 if (!tmp)
7c10a2b5
JN
104 return false;
105
f9f682ae
JN
106 tmp = I915_READ(reg_elda);
107 tmp &= ~bits_elda;
108 I915_WRITE(reg_elda, tmp);
7c10a2b5 109
938fd8aa 110 for (i = 0; i < drm_eld_size(eld) / 4; i++)
7c10a2b5
JN
111 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
112 return false;
113
114 return true;
115}
116
76d8d3e5
JN
117static void g4x_audio_codec_disable(struct intel_encoder *encoder)
118{
119 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
120 uint32_t eldv, tmp;
121
122 DRM_DEBUG_KMS("Disable audio codec\n");
123
124 tmp = I915_READ(G4X_AUD_VID_DID);
125 if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
126 eldv = G4X_ELDV_DEVCL_DEVBLC;
127 else
128 eldv = G4X_ELDV_DEVCTG;
129
130 /* Invalidate ELD */
131 tmp = I915_READ(G4X_AUD_CNTL_ST);
132 tmp &= ~eldv;
133 I915_WRITE(G4X_AUD_CNTL_ST, tmp);
134}
135
69bfe1a9
JN
136static void g4x_audio_codec_enable(struct drm_connector *connector,
137 struct intel_encoder *encoder,
138 struct drm_display_mode *mode)
7c10a2b5
JN
139{
140 struct drm_i915_private *dev_priv = connector->dev->dev_private;
141 uint8_t *eld = connector->eld;
142 uint32_t eldv;
f9f682ae
JN
143 uint32_t tmp;
144 int len, i;
7c10a2b5 145
d5ee08de
JN
146 DRM_DEBUG_KMS("Enable audio codec, %u bytes ELD\n", eld[2]);
147
f9f682ae
JN
148 tmp = I915_READ(G4X_AUD_VID_DID);
149 if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
7c10a2b5
JN
150 eldv = G4X_ELDV_DEVCL_DEVBLC;
151 else
152 eldv = G4X_ELDV_DEVCTG;
153
154 if (intel_eld_uptodate(connector,
155 G4X_AUD_CNTL_ST, eldv,
c46f111f 156 G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
7c10a2b5
JN
157 G4X_HDMIW_HDMIEDID))
158 return;
159
f9f682ae 160 tmp = I915_READ(G4X_AUD_CNTL_ST);
c46f111f 161 tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
f9f682ae
JN
162 len = (tmp >> 9) & 0x1f; /* ELD buffer size */
163 I915_WRITE(G4X_AUD_CNTL_ST, tmp);
7c10a2b5 164
938fd8aa 165 len = min(drm_eld_size(eld) / 4, len);
7c10a2b5
JN
166 DRM_DEBUG_DRIVER("ELD size %d\n", len);
167 for (i = 0; i < len; i++)
168 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
169
f9f682ae
JN
170 tmp = I915_READ(G4X_AUD_CNTL_ST);
171 tmp |= eldv;
172 I915_WRITE(G4X_AUD_CNTL_ST, tmp);
7c10a2b5
JN
173}
174
69bfe1a9
JN
175static void hsw_audio_codec_disable(struct intel_encoder *encoder)
176{
5fad84a7
JN
177 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
178 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
179 enum pipe pipe = intel_crtc->pipe;
69bfe1a9
JN
180 uint32_t tmp;
181
5fad84a7
JN
182 DRM_DEBUG_KMS("Disable audio codec on pipe %c\n", pipe_name(pipe));
183
184 /* Disable timestamps */
185 tmp = I915_READ(HSW_AUD_CFG(pipe));
186 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
187 tmp |= AUD_CONFIG_N_PROG_ENABLE;
188 tmp &= ~AUD_CONFIG_UPPER_N_MASK;
189 tmp &= ~AUD_CONFIG_LOWER_N_MASK;
190 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
191 tmp |= AUD_CONFIG_N_VALUE_INDEX;
192 I915_WRITE(HSW_AUD_CFG(pipe), tmp);
193
194 /* Invalidate ELD */
69bfe1a9 195 tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
82910ac6 196 tmp &= ~AUDIO_ELD_VALID(pipe);
69bfe1a9
JN
197 I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
198}
199
200static void hsw_audio_codec_enable(struct drm_connector *connector,
201 struct intel_encoder *encoder,
202 struct drm_display_mode *mode)
7c10a2b5
JN
203{
204 struct drm_i915_private *dev_priv = connector->dev->dev_private;
820d2d77 205 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
5fad84a7
JN
206 enum pipe pipe = intel_crtc->pipe;
207 const uint8_t *eld = connector->eld;
f9f682ae
JN
208 uint32_t tmp;
209 int len, i;
7c10a2b5 210
5fad84a7 211 DRM_DEBUG_KMS("Enable audio codec on pipe %c, %u bytes ELD\n",
938fd8aa 212 pipe_name(pipe), drm_eld_size(eld));
7c10a2b5 213
5fad84a7
JN
214 /* Enable audio presence detect, invalidate ELD */
215 tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
82910ac6
JN
216 tmp |= AUDIO_OUTPUT_ENABLE(pipe);
217 tmp &= ~AUDIO_ELD_VALID(pipe);
5fad84a7 218 I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
7c10a2b5 219
5fad84a7
JN
220 /*
221 * FIXME: We're supposed to wait for vblank here, but we have vblanks
222 * disabled during the mode set. The proper fix would be to push the
223 * rest of the setup into a vblank work item, queued here, but the
224 * infrastructure is not there yet.
225 */
7c10a2b5 226
5fad84a7
JN
227 /* Reset ELD write address */
228 tmp = I915_READ(HSW_AUD_DIP_ELD_CTRL(pipe));
c46f111f 229 tmp &= ~IBX_ELD_ADDRESS_MASK;
5fad84a7 230 I915_WRITE(HSW_AUD_DIP_ELD_CTRL(pipe), tmp);
7c10a2b5 231
5fad84a7 232 /* Up to 84 bytes of hw ELD buffer */
938fd8aa
JN
233 len = min(drm_eld_size(eld), 84);
234 for (i = 0; i < len / 4; i++)
5fad84a7 235 I915_WRITE(HSW_AUD_EDID_DATA(pipe), *((uint32_t *)eld + i));
7c10a2b5 236
5fad84a7 237 /* ELD valid */
69bfe1a9 238 tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
82910ac6 239 tmp |= AUDIO_ELD_VALID(pipe);
69bfe1a9 240 I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
5fad84a7
JN
241
242 /* Enable timestamps */
243 tmp = I915_READ(HSW_AUD_CFG(pipe));
244 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
245 tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
246 tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
247 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
248 tmp |= AUD_CONFIG_N_VALUE_INDEX;
249 else
250 tmp |= audio_config_hdmi_pixel_clock(mode);
251 I915_WRITE(HSW_AUD_CFG(pipe), tmp);
7c10a2b5
JN
252}
253
495a5bb8
JN
254static void ilk_audio_codec_disable(struct intel_encoder *encoder)
255{
256 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
257 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
258 struct intel_digital_port *intel_dig_port =
259 enc_to_dig_port(&encoder->base);
260 enum port port = intel_dig_port->port;
261 enum pipe pipe = intel_crtc->pipe;
262 uint32_t tmp, eldv;
263 int aud_config;
264 int aud_cntrl_st2;
265
266 DRM_DEBUG_KMS("Disable audio codec on port %c, pipe %c\n",
267 port_name(port), pipe_name(pipe));
268
269 if (HAS_PCH_IBX(dev_priv->dev)) {
270 aud_config = IBX_AUD_CFG(pipe);
271 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
272 } else if (IS_VALLEYVIEW(dev_priv)) {
273 aud_config = VLV_AUD_CFG(pipe);
274 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
275 } else {
276 aud_config = CPT_AUD_CFG(pipe);
277 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
278 }
279
280 /* Disable timestamps */
281 tmp = I915_READ(aud_config);
282 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
283 tmp |= AUD_CONFIG_N_PROG_ENABLE;
284 tmp &= ~AUD_CONFIG_UPPER_N_MASK;
285 tmp &= ~AUD_CONFIG_LOWER_N_MASK;
286 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
287 tmp |= AUD_CONFIG_N_VALUE_INDEX;
288 I915_WRITE(aud_config, tmp);
289
290 if (WARN_ON(!port)) {
82910ac6
JN
291 eldv = IBX_ELD_VALID(PORT_B) | IBX_ELD_VALID(PORT_C) |
292 IBX_ELD_VALID(PORT_D);
495a5bb8 293 } else {
82910ac6 294 eldv = IBX_ELD_VALID(port);
495a5bb8
JN
295 }
296
297 /* Invalidate ELD */
298 tmp = I915_READ(aud_cntrl_st2);
299 tmp &= ~eldv;
300 I915_WRITE(aud_cntrl_st2, tmp);
301}
302
69bfe1a9
JN
303static void ilk_audio_codec_enable(struct drm_connector *connector,
304 struct intel_encoder *encoder,
305 struct drm_display_mode *mode)
7c10a2b5
JN
306{
307 struct drm_i915_private *dev_priv = connector->dev->dev_private;
820d2d77 308 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
c6bde93b
JN
309 struct intel_digital_port *intel_dig_port =
310 enc_to_dig_port(&encoder->base);
311 enum port port = intel_dig_port->port;
312 enum pipe pipe = intel_crtc->pipe;
7c10a2b5
JN
313 uint8_t *eld = connector->eld;
314 uint32_t eldv;
f9f682ae
JN
315 uint32_t tmp;
316 int len, i;
7c10a2b5
JN
317 int hdmiw_hdmiedid;
318 int aud_config;
319 int aud_cntl_st;
320 int aud_cntrl_st2;
c6bde93b
JN
321
322 DRM_DEBUG_KMS("Enable audio codec on port %c, pipe %c, %u bytes ELD\n",
938fd8aa 323 port_name(port), pipe_name(pipe), drm_eld_size(eld));
c6bde93b
JN
324
325 /*
326 * FIXME: We're supposed to wait for vblank here, but we have vblanks
327 * disabled during the mode set. The proper fix would be to push the
328 * rest of the setup into a vblank work item, queued here, but the
329 * infrastructure is not there yet.
330 */
7c10a2b5
JN
331
332 if (HAS_PCH_IBX(connector->dev)) {
333 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
334 aud_config = IBX_AUD_CFG(pipe);
335 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
336 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
337 } else if (IS_VALLEYVIEW(connector->dev)) {
338 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
339 aud_config = VLV_AUD_CFG(pipe);
340 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
341 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
342 } else {
343 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
344 aud_config = CPT_AUD_CFG(pipe);
345 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
346 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
347 }
348
c6bde93b 349 if (WARN_ON(!port)) {
82910ac6
JN
350 eldv = IBX_ELD_VALID(PORT_B) | IBX_ELD_VALID(PORT_C) |
351 IBX_ELD_VALID(PORT_D);
7c10a2b5 352 } else {
82910ac6 353 eldv = IBX_ELD_VALID(port);
7c10a2b5
JN
354 }
355
c6bde93b 356 /* Invalidate ELD */
f9f682ae
JN
357 tmp = I915_READ(aud_cntrl_st2);
358 tmp &= ~eldv;
359 I915_WRITE(aud_cntrl_st2, tmp);
7c10a2b5 360
c6bde93b 361 /* Reset ELD write address */
f9f682ae 362 tmp = I915_READ(aud_cntl_st);
c46f111f 363 tmp &= ~IBX_ELD_ADDRESS_MASK;
f9f682ae 364 I915_WRITE(aud_cntl_st, tmp);
7c10a2b5 365
c6bde93b 366 /* Up to 84 bytes of hw ELD buffer */
938fd8aa
JN
367 len = min(drm_eld_size(eld), 84);
368 for (i = 0; i < len / 4; i++)
7c10a2b5
JN
369 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
370
c6bde93b 371 /* ELD valid */
f9f682ae
JN
372 tmp = I915_READ(aud_cntrl_st2);
373 tmp |= eldv;
374 I915_WRITE(aud_cntrl_st2, tmp);
c6bde93b
JN
375
376 /* Enable timestamps */
377 tmp = I915_READ(aud_config);
378 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
379 tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
380 tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
381 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
382 tmp |= AUD_CONFIG_N_VALUE_INDEX;
383 else
384 tmp |= audio_config_hdmi_pixel_clock(mode);
385 I915_WRITE(aud_config, tmp);
7c10a2b5
JN
386}
387
69bfe1a9
JN
388/**
389 * intel_audio_codec_enable - Enable the audio codec for HD audio
390 * @intel_encoder: encoder on which to enable audio
391 *
392 * The enable sequences may only be performed after enabling the transcoder and
393 * port, and after completed link training.
394 */
395void intel_audio_codec_enable(struct intel_encoder *intel_encoder)
7c10a2b5 396{
33d1e7c6
JN
397 struct drm_encoder *encoder = &intel_encoder->base;
398 struct intel_crtc *crtc = to_intel_crtc(encoder->crtc);
399 struct drm_display_mode *mode = &crtc->config.adjusted_mode;
7c10a2b5
JN
400 struct drm_connector *connector;
401 struct drm_device *dev = encoder->dev;
402 struct drm_i915_private *dev_priv = dev->dev_private;
403
404 connector = drm_select_eld(encoder, mode);
405 if (!connector)
406 return;
407
408 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
409 connector->base.id,
410 connector->name,
411 connector->encoder->base.id,
412 connector->encoder->name);
413
6189b036
JN
414 /* ELD Conn_Type */
415 connector->eld[5] &= ~(3 << 2);
416 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
417 connector->eld[5] |= (1 << 2);
418
7c10a2b5
JN
419 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
420
69bfe1a9
JN
421 if (dev_priv->display.audio_codec_enable)
422 dev_priv->display.audio_codec_enable(connector, intel_encoder, mode);
423}
424
425/**
426 * intel_audio_codec_disable - Disable the audio codec for HD audio
427 * @encoder: encoder on which to disable audio
428 *
429 * The disable sequences must be performed before disabling the transcoder or
430 * port.
431 */
432void intel_audio_codec_disable(struct intel_encoder *encoder)
433{
434 struct drm_device *dev = encoder->base.dev;
435 struct drm_i915_private *dev_priv = dev->dev_private;
436
437 if (dev_priv->display.audio_codec_disable)
438 dev_priv->display.audio_codec_disable(encoder);
7c10a2b5
JN
439}
440
441/**
442 * intel_init_audio - Set up chip specific audio functions
443 * @dev: drm device
444 */
445void intel_init_audio(struct drm_device *dev)
446{
447 struct drm_i915_private *dev_priv = dev->dev_private;
448
69bfe1a9
JN
449 if (IS_G4X(dev)) {
450 dev_priv->display.audio_codec_enable = g4x_audio_codec_enable;
76d8d3e5 451 dev_priv->display.audio_codec_disable = g4x_audio_codec_disable;
69bfe1a9
JN
452 } else if (IS_VALLEYVIEW(dev)) {
453 dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
495a5bb8 454 dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
69bfe1a9
JN
455 } else if (IS_HASWELL(dev) || INTEL_INFO(dev)->gen >= 8) {
456 dev_priv->display.audio_codec_enable = hsw_audio_codec_enable;
457 dev_priv->display.audio_codec_disable = hsw_audio_codec_disable;
458 } else if (HAS_PCH_SPLIT(dev)) {
459 dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
495a5bb8 460 dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
69bfe1a9 461 }
7c10a2b5 462}