blob: 537f6d8927f1ce224f2dfecc3716896d5f73c099 [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>
25
26#include <drm/drmP.h>
27#include <drm/drm_edid.h>
28#include "intel_drv.h"
29#include "i915_drv.h"
30
Jani Nikula87fcb2a2014-10-27 16:26:44 +020031static const struct {
Jani Nikula7c10a2b2014-10-27 16:26:43 +020032 int clock;
33 u32 config;
34} hdmi_audio_clock[] = {
35 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
36 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
37 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
38 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
39 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
40 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
41 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
42 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
43 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
44 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
45};
46
47/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
48static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
49{
50 int i;
51
52 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
53 if (mode->clock == hdmi_audio_clock[i].clock)
54 break;
55 }
56
57 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
58 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
59 i = 1;
60 }
61
62 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
63 hdmi_audio_clock[i].clock,
64 hdmi_audio_clock[i].config);
65
66 return hdmi_audio_clock[i].config;
67}
68
69static bool intel_eld_uptodate(struct drm_connector *connector,
70 int reg_eldv, uint32_t bits_eldv,
71 int reg_elda, uint32_t bits_elda,
72 int reg_edid)
73{
74 struct drm_i915_private *dev_priv = connector->dev->dev_private;
75 uint8_t *eld = connector->eld;
Jani Nikulaf9f682a2014-10-27 16:26:45 +020076 uint32_t tmp;
77 int i;
Jani Nikula7c10a2b2014-10-27 16:26:43 +020078
Jani Nikulaf9f682a2014-10-27 16:26:45 +020079 tmp = I915_READ(reg_eldv);
80 tmp &= bits_eldv;
Jani Nikula7c10a2b2014-10-27 16:26:43 +020081
82 if (!eld[0])
Jani Nikulaf9f682a2014-10-27 16:26:45 +020083 return !tmp;
Jani Nikula7c10a2b2014-10-27 16:26:43 +020084
Jani Nikulaf9f682a2014-10-27 16:26:45 +020085 if (!tmp)
Jani Nikula7c10a2b2014-10-27 16:26:43 +020086 return false;
87
Jani Nikulaf9f682a2014-10-27 16:26:45 +020088 tmp = I915_READ(reg_elda);
89 tmp &= ~bits_elda;
90 I915_WRITE(reg_elda, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020091
92 for (i = 0; i < eld[2]; i++)
93 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
94 return false;
95
96 return true;
97}
98
99static void g4x_write_eld(struct drm_connector *connector,
Jani Nikula820d2d72014-10-27 16:26:47 +0200100 struct intel_encoder *encoder,
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200101 struct drm_display_mode *mode)
102{
103 struct drm_i915_private *dev_priv = connector->dev->dev_private;
104 uint8_t *eld = connector->eld;
105 uint32_t eldv;
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200106 uint32_t tmp;
107 int len, i;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200108
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200109 tmp = I915_READ(G4X_AUD_VID_DID);
110 if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200111 eldv = G4X_ELDV_DEVCL_DEVBLC;
112 else
113 eldv = G4X_ELDV_DEVCTG;
114
115 if (intel_eld_uptodate(connector,
116 G4X_AUD_CNTL_ST, eldv,
117 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
118 G4X_HDMIW_HDMIEDID))
119 return;
120
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200121 tmp = I915_READ(G4X_AUD_CNTL_ST);
122 tmp &= ~(eldv | G4X_ELD_ADDR);
123 len = (tmp >> 9) & 0x1f; /* ELD buffer size */
124 I915_WRITE(G4X_AUD_CNTL_ST, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200125
126 if (!eld[0])
127 return;
128
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200129 len = min_t(int, eld[2], len);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200130 DRM_DEBUG_DRIVER("ELD size %d\n", len);
131 for (i = 0; i < len; i++)
132 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
133
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200134 tmp = I915_READ(G4X_AUD_CNTL_ST);
135 tmp |= eldv;
136 I915_WRITE(G4X_AUD_CNTL_ST, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200137}
138
139static void haswell_write_eld(struct drm_connector *connector,
Jani Nikula820d2d72014-10-27 16:26:47 +0200140 struct intel_encoder *encoder,
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200141 struct drm_display_mode *mode)
142{
143 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Jani Nikula820d2d72014-10-27 16:26:47 +0200144 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200145 uint8_t *eld = connector->eld;
146 uint32_t eldv;
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200147 uint32_t tmp;
148 int len, i;
Jani Nikula820d2d72014-10-27 16:26:47 +0200149 enum pipe pipe = intel_crtc->pipe;
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200150 enum port port;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200151 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
152 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
153 int aud_config = HSW_AUD_CFG(pipe);
154 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
155
156 /* Audio output enable */
157 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
158 tmp = I915_READ(aud_cntrl_st2);
159 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
160 I915_WRITE(aud_cntrl_st2, tmp);
161 POSTING_READ(aud_cntrl_st2);
162
Jani Nikula820d2d72014-10-27 16:26:47 +0200163 assert_pipe_disabled(dev_priv, pipe);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200164
165 /* Set ELD valid state */
166 tmp = I915_READ(aud_cntrl_st2);
167 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
168 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
169 I915_WRITE(aud_cntrl_st2, tmp);
170 tmp = I915_READ(aud_cntrl_st2);
171 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
172
173 /* Enable HDMI mode */
174 tmp = I915_READ(aud_config);
175 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
176 /* clear N_programing_enable and N_value_index */
177 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
178 I915_WRITE(aud_config, tmp);
179
180 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
181
182 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
183
Jani Nikula6189b032014-10-28 13:53:01 +0200184 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200185 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula6189b032014-10-28 13:53:01 +0200186 else
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200187 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200188
189 if (intel_eld_uptodate(connector,
190 aud_cntrl_st2, eldv,
191 aud_cntl_st, IBX_ELD_ADDRESS,
192 hdmiw_hdmiedid))
193 return;
194
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200195 tmp = I915_READ(aud_cntrl_st2);
196 tmp &= ~eldv;
197 I915_WRITE(aud_cntrl_st2, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200198
199 if (!eld[0])
200 return;
201
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200202 tmp = I915_READ(aud_cntl_st);
203 tmp &= ~IBX_ELD_ADDRESS;
204 I915_WRITE(aud_cntl_st, tmp);
205 port = (tmp >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
206 DRM_DEBUG_DRIVER("port num:%d\n", port);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200207
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200208 len = min_t(int, eld[2], 21); /* 84 bytes of hw ELD buffer */
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200209 DRM_DEBUG_DRIVER("ELD size %d\n", len);
210 for (i = 0; i < len; i++)
211 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
212
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200213 tmp = I915_READ(aud_cntrl_st2);
214 tmp |= eldv;
215 I915_WRITE(aud_cntrl_st2, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200216}
217
218static void ironlake_write_eld(struct drm_connector *connector,
Jani Nikula820d2d72014-10-27 16:26:47 +0200219 struct intel_encoder *encoder,
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200220 struct drm_display_mode *mode)
221{
222 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Jani Nikula820d2d72014-10-27 16:26:47 +0200223 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200224 uint8_t *eld = connector->eld;
225 uint32_t eldv;
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200226 uint32_t tmp;
227 int len, i;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200228 int hdmiw_hdmiedid;
229 int aud_config;
230 int aud_cntl_st;
231 int aud_cntrl_st2;
Jani Nikula820d2d72014-10-27 16:26:47 +0200232 enum pipe pipe = intel_crtc->pipe;
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200233 enum port port;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200234
235 if (HAS_PCH_IBX(connector->dev)) {
236 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
237 aud_config = IBX_AUD_CFG(pipe);
238 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
239 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
240 } else if (IS_VALLEYVIEW(connector->dev)) {
241 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
242 aud_config = VLV_AUD_CFG(pipe);
243 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
244 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
245 } else {
246 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
247 aud_config = CPT_AUD_CFG(pipe);
248 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
249 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
250 }
251
252 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
253
254 if (IS_VALLEYVIEW(connector->dev)) {
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200255 struct intel_digital_port *intel_dig_port;
256
Jani Nikula820d2d72014-10-27 16:26:47 +0200257 intel_dig_port = enc_to_dig_port(&encoder->base);
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200258 port = intel_dig_port->port;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200259 } else {
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200260 tmp = I915_READ(aud_cntl_st);
261 port = (tmp >> 29) & DIP_PORT_SEL_MASK;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200262 /* DIP_Port_Select, 0x1 = PortB */
263 }
264
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200265 if (!port) {
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200266 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
267 /* operate blindly on all ports */
268 eldv = IBX_ELD_VALIDB;
269 eldv |= IBX_ELD_VALIDB << 4;
270 eldv |= IBX_ELD_VALIDB << 8;
271 } else {
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200272 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(port));
273 eldv = IBX_ELD_VALIDB << ((port - 1) * 4);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200274 }
275
Jani Nikula6189b032014-10-28 13:53:01 +0200276 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200277 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula6189b032014-10-28 13:53:01 +0200278 else
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200279 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200280
281 if (intel_eld_uptodate(connector,
282 aud_cntrl_st2, eldv,
283 aud_cntl_st, IBX_ELD_ADDRESS,
284 hdmiw_hdmiedid))
285 return;
286
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200287 tmp = I915_READ(aud_cntrl_st2);
288 tmp &= ~eldv;
289 I915_WRITE(aud_cntrl_st2, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200290
291 if (!eld[0])
292 return;
293
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200294 tmp = I915_READ(aud_cntl_st);
295 tmp &= ~IBX_ELD_ADDRESS;
296 I915_WRITE(aud_cntl_st, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200297
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200298 len = min_t(int, eld[2], 21); /* 84 bytes of hw ELD buffer */
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200299 DRM_DEBUG_DRIVER("ELD size %d\n", len);
300 for (i = 0; i < len; i++)
301 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
302
Jani Nikulaf9f682a2014-10-27 16:26:45 +0200303 tmp = I915_READ(aud_cntrl_st2);
304 tmp |= eldv;
305 I915_WRITE(aud_cntrl_st2, tmp);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200306}
307
Jani Nikula33d1e7c62014-10-27 16:26:46 +0200308void intel_write_eld(struct intel_encoder *intel_encoder)
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200309{
Jani Nikula33d1e7c62014-10-27 16:26:46 +0200310 struct drm_encoder *encoder = &intel_encoder->base;
311 struct intel_crtc *crtc = to_intel_crtc(encoder->crtc);
312 struct drm_display_mode *mode = &crtc->config.adjusted_mode;
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200313 struct drm_connector *connector;
314 struct drm_device *dev = encoder->dev;
315 struct drm_i915_private *dev_priv = dev->dev_private;
316
317 connector = drm_select_eld(encoder, mode);
318 if (!connector)
319 return;
320
321 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
322 connector->base.id,
323 connector->name,
324 connector->encoder->base.id,
325 connector->encoder->name);
326
Jani Nikula6189b032014-10-28 13:53:01 +0200327 /* ELD Conn_Type */
328 connector->eld[5] &= ~(3 << 2);
329 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
330 connector->eld[5] |= (1 << 2);
331
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200332 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
333
334 if (dev_priv->display.write_eld)
Jani Nikula820d2d72014-10-27 16:26:47 +0200335 dev_priv->display.write_eld(connector, intel_encoder, mode);
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200336}
337
338/**
339 * intel_init_audio - Set up chip specific audio functions
340 * @dev: drm device
341 */
342void intel_init_audio(struct drm_device *dev)
343{
344 struct drm_i915_private *dev_priv = dev->dev_private;
345
346 if (IS_G4X(dev))
347 dev_priv->display.write_eld = g4x_write_eld;
348 else if (IS_VALLEYVIEW(dev))
349 dev_priv->display.write_eld = ironlake_write_eld;
350 else if (IS_HASWELL(dev) || INTEL_INFO(dev)->gen >= 8)
351 dev_priv->display.write_eld = haswell_write_eld;
352 else if (HAS_PCH_SPLIT(dev))
353 dev_priv->display.write_eld = ironlake_write_eld;
354}