blob: 7de2d3b85b328cd9ff2d04d7419cea37edd550ce [file] [log] [blame]
Eric Anholt7d573822009-01-02 13:33:00 -08001/*
2 * Copyright 2006 Dave Airlie <airlied@linux.ie>
3 * Copyright © 2006-2009 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Eric Anholt <eric@anholt.net>
26 * Jesse Barnes <jesse.barnes@intel.com>
27 */
28
29#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Eric Anholt7d573822009-01-02 13:33:00 -080031#include <linux/delay.h>
32#include "drmP.h"
33#include "drm.h"
34#include "drm_crtc.h"
Keith Packardaa93d632009-05-05 09:52:46 -070035#include "drm_edid.h"
Eric Anholt7d573822009-01-02 13:33:00 -080036#include "intel_drv.h"
37#include "i915_drm.h"
38#include "i915_drv.h"
39
Chris Wilsonea5b2132010-08-04 13:50:23 +010040struct intel_hdmi {
41 struct intel_encoder base;
Eric Anholt7d573822009-01-02 13:33:00 -080042 u32 sdvox_reg;
Chris Wilsonf899fc62010-07-20 15:44:45 -070043 int ddc_bus;
Chris Wilsone953fd72011-02-21 22:23:52 +000044 uint32_t color_range;
Ma Ling9dff6af2009-04-02 13:13:26 +080045 bool has_hdmi_sink;
Zhenyu Wang2e3d6002010-09-10 10:39:40 +080046 bool has_audio;
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +080047 enum hdmi_force_audio force_audio;
Jesse Barnes45187ac2011-08-03 09:22:55 -070048 void (*write_infoframe)(struct drm_encoder *encoder,
49 struct dip_infoframe *frame);
Eric Anholt7d573822009-01-02 13:33:00 -080050};
51
Chris Wilsonea5b2132010-08-04 13:50:23 +010052static struct intel_hdmi *enc_to_intel_hdmi(struct drm_encoder *encoder)
53{
Chris Wilson4ef69c72010-09-09 15:14:28 +010054 return container_of(encoder, struct intel_hdmi, base.base);
Chris Wilsonea5b2132010-08-04 13:50:23 +010055}
56
Chris Wilsondf0e9242010-09-09 16:20:55 +010057static struct intel_hdmi *intel_attached_hdmi(struct drm_connector *connector)
58{
59 return container_of(intel_attached_encoder(connector),
60 struct intel_hdmi, base);
61}
62
Jesse Barnes45187ac2011-08-03 09:22:55 -070063void intel_dip_infoframe_csum(struct dip_infoframe *frame)
David Härdeman3c17fe42010-09-24 21:44:32 +020064{
Jesse Barnes45187ac2011-08-03 09:22:55 -070065 uint8_t *data = (uint8_t *)frame;
David Härdeman3c17fe42010-09-24 21:44:32 +020066 uint8_t sum = 0;
67 unsigned i;
68
Jesse Barnes45187ac2011-08-03 09:22:55 -070069 frame->checksum = 0;
70 frame->ecc = 0;
David Härdeman3c17fe42010-09-24 21:44:32 +020071
Jesse Barnes64a8fc02011-09-22 11:16:00 +053072 for (i = 0; i < frame->len + DIP_HEADER_SIZE; i++)
David Härdeman3c17fe42010-09-24 21:44:32 +020073 sum += data[i];
74
Jesse Barnes45187ac2011-08-03 09:22:55 -070075 frame->checksum = 0x100 - sum;
David Härdeman3c17fe42010-09-24 21:44:32 +020076}
77
Jesse Barnes45187ac2011-08-03 09:22:55 -070078static u32 intel_infoframe_index(struct dip_infoframe *frame)
David Härdeman3c17fe42010-09-24 21:44:32 +020079{
Jesse Barnes45187ac2011-08-03 09:22:55 -070080 u32 flags = 0;
81
82 switch (frame->type) {
83 case DIP_TYPE_AVI:
84 flags |= VIDEO_DIP_SELECT_AVI;
85 break;
86 case DIP_TYPE_SPD:
87 flags |= VIDEO_DIP_SELECT_SPD;
88 break;
89 default:
90 DRM_DEBUG_DRIVER("unknown info frame type %d\n", frame->type);
91 break;
92 }
93
94 return flags;
95}
96
97static u32 intel_infoframe_flags(struct dip_infoframe *frame)
98{
99 u32 flags = 0;
100
101 switch (frame->type) {
102 case DIP_TYPE_AVI:
103 flags |= VIDEO_DIP_ENABLE_AVI | VIDEO_DIP_FREQ_VSYNC;
104 break;
105 case DIP_TYPE_SPD:
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530106 flags |= VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_FREQ_VSYNC;
Jesse Barnes45187ac2011-08-03 09:22:55 -0700107 break;
108 default:
109 DRM_DEBUG_DRIVER("unknown info frame type %d\n", frame->type);
110 break;
111 }
112
113 return flags;
114}
115
116static void i9xx_write_infoframe(struct drm_encoder *encoder,
117 struct dip_infoframe *frame)
118{
119 uint32_t *data = (uint32_t *)frame;
David Härdeman3c17fe42010-09-24 21:44:32 +0200120 struct drm_device *dev = encoder->dev;
121 struct drm_i915_private *dev_priv = dev->dev_private;
122 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700123 u32 port, flags, val = I915_READ(VIDEO_DIP_CTL);
124 unsigned i, len = DIP_HEADER_SIZE + frame->len;
David Härdeman3c17fe42010-09-24 21:44:32 +0200125
David Härdeman3c17fe42010-09-24 21:44:32 +0200126
127 /* XXX first guess at handling video port, is this corrent? */
128 if (intel_hdmi->sdvox_reg == SDVOB)
129 port = VIDEO_DIP_PORT_B;
130 else if (intel_hdmi->sdvox_reg == SDVOC)
131 port = VIDEO_DIP_PORT_C;
132 else
133 return;
134
Jesse Barnes45187ac2011-08-03 09:22:55 -0700135 flags = intel_infoframe_index(frame);
David Härdeman3c17fe42010-09-24 21:44:32 +0200136
Jesse Barnes45187ac2011-08-03 09:22:55 -0700137 val &= ~VIDEO_DIP_SELECT_MASK;
138
139 I915_WRITE(VIDEO_DIP_CTL, val | port | flags);
140
141 for (i = 0; i < len; i += 4) {
David Härdeman3c17fe42010-09-24 21:44:32 +0200142 I915_WRITE(VIDEO_DIP_DATA, *data);
143 data++;
144 }
145
Jesse Barnes45187ac2011-08-03 09:22:55 -0700146 flags |= intel_infoframe_flags(frame);
147
148 I915_WRITE(VIDEO_DIP_CTL, VIDEO_DIP_ENABLE | val | port | flags);
David Härdeman3c17fe42010-09-24 21:44:32 +0200149}
150
Jesse Barnes45187ac2011-08-03 09:22:55 -0700151static void ironlake_write_infoframe(struct drm_encoder *encoder,
152 struct dip_infoframe *frame)
153{
154 uint32_t *data = (uint32_t *)frame;
155 struct drm_device *dev = encoder->dev;
156 struct drm_i915_private *dev_priv = dev->dev_private;
157 struct drm_crtc *crtc = encoder->crtc;
158 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
159 int reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
160 unsigned i, len = DIP_HEADER_SIZE + frame->len;
161 u32 flags, val = I915_READ(reg);
162
163 intel_wait_for_vblank(dev, intel_crtc->pipe);
164
165 flags = intel_infoframe_index(frame);
166
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530167 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
Jesse Barnes45187ac2011-08-03 09:22:55 -0700168
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530169 I915_WRITE(reg, VIDEO_DIP_ENABLE | val | flags);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700170
171 for (i = 0; i < len; i += 4) {
172 I915_WRITE(TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
173 data++;
174 }
175
176 flags |= intel_infoframe_flags(frame);
177
178 I915_WRITE(reg, VIDEO_DIP_ENABLE | val | flags);
179}
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700180
181static void vlv_write_infoframe(struct drm_encoder *encoder,
182 struct dip_infoframe *frame)
183{
184 uint32_t *data = (uint32_t *)frame;
185 struct drm_device *dev = encoder->dev;
186 struct drm_i915_private *dev_priv = dev->dev_private;
187 struct drm_crtc *crtc = encoder->crtc;
188 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
189 int reg = VLV_TVIDEO_DIP_CTL(intel_crtc->pipe);
190 unsigned i, len = DIP_HEADER_SIZE + frame->len;
191 u32 flags, val = I915_READ(reg);
192
193 intel_wait_for_vblank(dev, intel_crtc->pipe);
194
195 flags = intel_infoframe_index(frame);
196
197 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
198
199 I915_WRITE(reg, VIDEO_DIP_ENABLE | val | flags);
200
201 for (i = 0; i < len; i += 4) {
202 I915_WRITE(VLV_TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
203 data++;
204 }
205
206 flags |= intel_infoframe_flags(frame);
207
208 I915_WRITE(reg, VIDEO_DIP_ENABLE | val | flags);
209}
210
Jesse Barnes45187ac2011-08-03 09:22:55 -0700211static void intel_set_infoframe(struct drm_encoder *encoder,
212 struct dip_infoframe *frame)
213{
214 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
215
216 if (!intel_hdmi->has_hdmi_sink)
217 return;
218
219 intel_dip_infoframe_csum(frame);
220 intel_hdmi->write_infoframe(encoder, frame);
221}
222
223static void intel_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
Jesse Barnesb055c8f2011-07-08 11:31:57 -0700224{
225 struct dip_infoframe avi_if = {
226 .type = DIP_TYPE_AVI,
227 .ver = DIP_VERSION_AVI,
228 .len = DIP_LEN_AVI,
229 };
Jesse Barnesb055c8f2011-07-08 11:31:57 -0700230
Jesse Barnes45187ac2011-08-03 09:22:55 -0700231 intel_set_infoframe(encoder, &avi_if);
Jesse Barnesb055c8f2011-07-08 11:31:57 -0700232}
233
Jesse Barnesc0864cb2011-08-03 09:22:56 -0700234static void intel_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
235{
236 struct dip_infoframe spd_if;
237
238 memset(&spd_if, 0, sizeof(spd_if));
239 spd_if.type = DIP_TYPE_SPD;
240 spd_if.ver = DIP_VERSION_SPD;
241 spd_if.len = DIP_LEN_SPD;
242 strcpy(spd_if.body.spd.vn, "Intel");
243 strcpy(spd_if.body.spd.pd, "Integrated gfx");
244 spd_if.body.spd.sdi = DIP_SPD_PC;
245
246 intel_set_infoframe(encoder, &spd_if);
247}
248
Eric Anholt7d573822009-01-02 13:33:00 -0800249static void intel_hdmi_mode_set(struct drm_encoder *encoder,
250 struct drm_display_mode *mode,
251 struct drm_display_mode *adjusted_mode)
252{
253 struct drm_device *dev = encoder->dev;
254 struct drm_i915_private *dev_priv = dev->dev_private;
255 struct drm_crtc *crtc = encoder->crtc;
256 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100257 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800258 u32 sdvox;
259
Adam Jacksonb599c0b2010-07-16 14:46:31 -0400260 sdvox = SDVO_ENCODING_HDMI | SDVO_BORDER_ENABLE;
Jesse Barnes5d4fac92011-06-24 12:19:19 -0700261 if (!HAS_PCH_SPLIT(dev))
262 sdvox |= intel_hdmi->color_range;
Adam Jacksonb599c0b2010-07-16 14:46:31 -0400263 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
264 sdvox |= SDVO_VSYNC_ACTIVE_HIGH;
265 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
266 sdvox |= SDVO_HSYNC_ACTIVE_HIGH;
Eric Anholt7d573822009-01-02 13:33:00 -0800267
Jesse Barnes020f6702011-06-24 12:19:25 -0700268 if (intel_crtc->bpp > 24)
269 sdvox |= COLOR_FORMAT_12bpc;
270 else
271 sdvox |= COLOR_FORMAT_8bpc;
272
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800273 /* Required on CPT */
274 if (intel_hdmi->has_hdmi_sink && HAS_PCH_CPT(dev))
275 sdvox |= HDMI_MODE_SELECT;
276
David Härdeman3c17fe42010-09-24 21:44:32 +0200277 if (intel_hdmi->has_audio) {
Wu Fengguange0dac652011-09-05 14:25:34 +0800278 DRM_DEBUG_DRIVER("Enabling HDMI audio on pipe %c\n",
279 pipe_name(intel_crtc->pipe));
Eric Anholt7d573822009-01-02 13:33:00 -0800280 sdvox |= SDVO_AUDIO_ENABLE;
David Härdeman3c17fe42010-09-24 21:44:32 +0200281 sdvox |= SDVO_NULL_PACKETS_DURING_VSYNC;
Wu Fengguange0dac652011-09-05 14:25:34 +0800282 intel_write_eld(encoder, adjusted_mode);
David Härdeman3c17fe42010-09-24 21:44:32 +0200283 }
Eric Anholt7d573822009-01-02 13:33:00 -0800284
Jesse Barnes75770562011-10-12 09:01:58 -0700285 if (HAS_PCH_CPT(dev))
286 sdvox |= PORT_TRANS_SEL_CPT(intel_crtc->pipe);
287 else if (intel_crtc->pipe == 1)
288 sdvox |= SDVO_PIPE_B_SELECT;
Eric Anholt7d573822009-01-02 13:33:00 -0800289
Chris Wilsonea5b2132010-08-04 13:50:23 +0100290 I915_WRITE(intel_hdmi->sdvox_reg, sdvox);
291 POSTING_READ(intel_hdmi->sdvox_reg);
David Härdeman3c17fe42010-09-24 21:44:32 +0200292
Jesse Barnes45187ac2011-08-03 09:22:55 -0700293 intel_hdmi_set_avi_infoframe(encoder);
Jesse Barnesc0864cb2011-08-03 09:22:56 -0700294 intel_hdmi_set_spd_infoframe(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800295}
296
297static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
298{
299 struct drm_device *dev = encoder->dev;
300 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100301 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800302 u32 temp;
Wu Fengguang2deed762011-12-09 20:42:20 +0800303 u32 enable_bits = SDVO_ENABLE;
304
305 if (intel_hdmi->has_audio)
306 enable_bits |= SDVO_AUDIO_ENABLE;
Eric Anholt7d573822009-01-02 13:33:00 -0800307
Chris Wilsonea5b2132010-08-04 13:50:23 +0100308 temp = I915_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000309
310 /* HW workaround, need to toggle enable bit off and on for 12bpc, but
311 * we do this anyway which shows more stable in testing.
312 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800313 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100314 I915_WRITE(intel_hdmi->sdvox_reg, temp & ~SDVO_ENABLE);
315 POSTING_READ(intel_hdmi->sdvox_reg);
Eric Anholt7d573822009-01-02 13:33:00 -0800316 }
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000317
318 if (mode != DRM_MODE_DPMS_ON) {
Wu Fengguang2deed762011-12-09 20:42:20 +0800319 temp &= ~enable_bits;
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000320 } else {
Wu Fengguang2deed762011-12-09 20:42:20 +0800321 temp |= enable_bits;
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000322 }
323
Chris Wilsonea5b2132010-08-04 13:50:23 +0100324 I915_WRITE(intel_hdmi->sdvox_reg, temp);
325 POSTING_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000326
327 /* HW workaround, need to write this twice for issue that may result
328 * in first write getting masked.
329 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800330 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100331 I915_WRITE(intel_hdmi->sdvox_reg, temp);
332 POSTING_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000333 }
Eric Anholt7d573822009-01-02 13:33:00 -0800334}
335
Eric Anholt7d573822009-01-02 13:33:00 -0800336static int intel_hdmi_mode_valid(struct drm_connector *connector,
337 struct drm_display_mode *mode)
338{
339 if (mode->clock > 165000)
340 return MODE_CLOCK_HIGH;
341 if (mode->clock < 20000)
Nicolas Kaiser5cbba412011-05-30 12:48:26 +0200342 return MODE_CLOCK_LOW;
Eric Anholt7d573822009-01-02 13:33:00 -0800343
344 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
345 return MODE_NO_DBLESCAN;
346
347 return MODE_OK;
348}
349
350static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
351 struct drm_display_mode *mode,
352 struct drm_display_mode *adjusted_mode)
353{
354 return true;
355}
356
Keith Packardaa93d632009-05-05 09:52:46 -0700357static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100358intel_hdmi_detect(struct drm_connector *connector, bool force)
Ma Ling9dff6af2009-04-02 13:13:26 +0800359{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100360 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700361 struct drm_i915_private *dev_priv = connector->dev->dev_private;
362 struct edid *edid;
Keith Packardaa93d632009-05-05 09:52:46 -0700363 enum drm_connector_status status = connector_status_disconnected;
Ma Ling9dff6af2009-04-02 13:13:26 +0800364
Chris Wilsonea5b2132010-08-04 13:50:23 +0100365 intel_hdmi->has_hdmi_sink = false;
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800366 intel_hdmi->has_audio = false;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700367 edid = drm_get_edid(connector,
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800368 intel_gmbus_get_adapter(dev_priv,
369 intel_hdmi->ddc_bus));
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800370
Keith Packardaa93d632009-05-05 09:52:46 -0700371 if (edid) {
Eric Anholtbe9f1c42009-06-21 22:14:55 -0700372 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
Keith Packardaa93d632009-05-05 09:52:46 -0700373 status = connector_status_connected;
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800374 if (intel_hdmi->force_audio != HDMI_AUDIO_OFF_DVI)
375 intel_hdmi->has_hdmi_sink =
376 drm_detect_hdmi_monitor(edid);
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800377 intel_hdmi->has_audio = drm_detect_monitor_audio(edid);
Keith Packardaa93d632009-05-05 09:52:46 -0700378 }
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800379 connector->display_info.raw_edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700380 kfree(edid);
Ma Ling9dff6af2009-04-02 13:13:26 +0800381 }
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800382
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100383 if (status == connector_status_connected) {
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800384 if (intel_hdmi->force_audio != HDMI_AUDIO_AUTO)
385 intel_hdmi->has_audio =
386 (intel_hdmi->force_audio == HDMI_AUDIO_ON);
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100387 }
388
Keith Packardaa93d632009-05-05 09:52:46 -0700389 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +0800390}
391
Eric Anholt7d573822009-01-02 13:33:00 -0800392static int intel_hdmi_get_modes(struct drm_connector *connector)
393{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100394 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700395 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Eric Anholt7d573822009-01-02 13:33:00 -0800396
397 /* We should parse the EDID data and find out if it's an HDMI sink so
398 * we can send audio to it.
399 */
400
Chris Wilsonf899fc62010-07-20 15:44:45 -0700401 return intel_ddc_get_modes(connector,
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800402 intel_gmbus_get_adapter(dev_priv,
403 intel_hdmi->ddc_bus));
Eric Anholt7d573822009-01-02 13:33:00 -0800404}
405
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000406static bool
407intel_hdmi_detect_audio(struct drm_connector *connector)
408{
409 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
410 struct drm_i915_private *dev_priv = connector->dev->dev_private;
411 struct edid *edid;
412 bool has_audio = false;
413
414 edid = drm_get_edid(connector,
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800415 intel_gmbus_get_adapter(dev_priv,
416 intel_hdmi->ddc_bus));
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000417 if (edid) {
418 if (edid->input & DRM_EDID_INPUT_DIGITAL)
419 has_audio = drm_detect_monitor_audio(edid);
420
421 connector->display_info.raw_edid = NULL;
422 kfree(edid);
423 }
424
425 return has_audio;
426}
427
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100428static int
429intel_hdmi_set_property(struct drm_connector *connector,
430 struct drm_property *property,
431 uint64_t val)
432{
433 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +0000434 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100435 int ret;
436
437 ret = drm_connector_property_set_value(connector, property, val);
438 if (ret)
439 return ret;
440
Chris Wilson3f43c482011-05-12 22:17:24 +0100441 if (property == dev_priv->force_audio_property) {
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800442 enum hdmi_force_audio i = val;
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000443 bool has_audio;
444
445 if (i == intel_hdmi->force_audio)
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100446 return 0;
447
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000448 intel_hdmi->force_audio = i;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100449
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800450 if (i == HDMI_AUDIO_AUTO)
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000451 has_audio = intel_hdmi_detect_audio(connector);
452 else
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800453 has_audio = (i == HDMI_AUDIO_ON);
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000454
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800455 if (i == HDMI_AUDIO_OFF_DVI)
456 intel_hdmi->has_hdmi_sink = 0;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100457
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000458 intel_hdmi->has_audio = has_audio;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100459 goto done;
460 }
461
Chris Wilsone953fd72011-02-21 22:23:52 +0000462 if (property == dev_priv->broadcast_rgb_property) {
463 if (val == !!intel_hdmi->color_range)
464 return 0;
465
466 intel_hdmi->color_range = val ? SDVO_COLOR_RANGE_16_235 : 0;
467 goto done;
468 }
469
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100470 return -EINVAL;
471
472done:
473 if (intel_hdmi->base.base.crtc) {
474 struct drm_crtc *crtc = intel_hdmi->base.base.crtc;
475 drm_crtc_helper_set_mode(crtc, &crtc->mode,
476 crtc->x, crtc->y,
477 crtc->fb);
478 }
479
480 return 0;
481}
482
Eric Anholt7d573822009-01-02 13:33:00 -0800483static void intel_hdmi_destroy(struct drm_connector *connector)
484{
Eric Anholt7d573822009-01-02 13:33:00 -0800485 drm_sysfs_connector_remove(connector);
486 drm_connector_cleanup(connector);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800487 kfree(connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800488}
489
490static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
491 .dpms = intel_hdmi_dpms,
492 .mode_fixup = intel_hdmi_mode_fixup,
493 .prepare = intel_encoder_prepare,
494 .mode_set = intel_hdmi_mode_set,
495 .commit = intel_encoder_commit,
496};
497
498static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700499 .dpms = drm_helper_connector_dpms,
Eric Anholt7d573822009-01-02 13:33:00 -0800500 .detect = intel_hdmi_detect,
501 .fill_modes = drm_helper_probe_single_connector_modes,
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100502 .set_property = intel_hdmi_set_property,
Eric Anholt7d573822009-01-02 13:33:00 -0800503 .destroy = intel_hdmi_destroy,
504};
505
506static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
507 .get_modes = intel_hdmi_get_modes,
508 .mode_valid = intel_hdmi_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +0100509 .best_encoder = intel_best_encoder,
Eric Anholt7d573822009-01-02 13:33:00 -0800510};
511
Eric Anholt7d573822009-01-02 13:33:00 -0800512static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100513 .destroy = intel_encoder_destroy,
Eric Anholt7d573822009-01-02 13:33:00 -0800514};
515
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100516static void
517intel_hdmi_add_properties(struct intel_hdmi *intel_hdmi, struct drm_connector *connector)
518{
Chris Wilson3f43c482011-05-12 22:17:24 +0100519 intel_attach_force_audio_property(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +0000520 intel_attach_broadcast_rgb_property(connector);
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100521}
522
Eric Anholt7d573822009-01-02 13:33:00 -0800523void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
524{
525 struct drm_i915_private *dev_priv = dev->dev_private;
526 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -0700527 struct intel_encoder *intel_encoder;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800528 struct intel_connector *intel_connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100529 struct intel_hdmi *intel_hdmi;
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530530 int i;
Eric Anholt7d573822009-01-02 13:33:00 -0800531
Chris Wilsonea5b2132010-08-04 13:50:23 +0100532 intel_hdmi = kzalloc(sizeof(struct intel_hdmi), GFP_KERNEL);
533 if (!intel_hdmi)
Eric Anholt7d573822009-01-02 13:33:00 -0800534 return;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800535
536 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
537 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100538 kfree(intel_hdmi);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800539 return;
540 }
541
Chris Wilsonea5b2132010-08-04 13:50:23 +0100542 intel_encoder = &intel_hdmi->base;
Chris Wilson373a3cf2010-09-15 12:03:59 +0100543 drm_encoder_init(dev, &intel_encoder->base, &intel_hdmi_enc_funcs,
544 DRM_MODE_ENCODER_TMDS);
545
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800546 connector = &intel_connector->base;
Eric Anholt7d573822009-01-02 13:33:00 -0800547 drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
Adam Jackson8d911042009-09-23 15:08:29 -0400548 DRM_MODE_CONNECTOR_HDMIA);
Eric Anholt7d573822009-01-02 13:33:00 -0800549 drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
550
Eric Anholt21d40d32010-03-25 11:11:14 -0700551 intel_encoder->type = INTEL_OUTPUT_HDMI;
Eric Anholt7d573822009-01-02 13:33:00 -0800552
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000553 connector->polled = DRM_CONNECTOR_POLL_HPD;
Peter Rossc3febcc2012-01-28 14:49:26 +0100554 connector->interlace_allowed = 1;
Eric Anholt7d573822009-01-02 13:33:00 -0800555 connector->doublescan_allowed = 0;
Jesse Barnes27f82272011-09-02 12:54:37 -0700556 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
Eric Anholt7d573822009-01-02 13:33:00 -0800557
558 /* Set up the DDC bus. */
Ma Lingf8aed702009-08-24 13:50:24 +0800559 if (sdvox_reg == SDVOB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700560 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700561 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800562 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800563 } else if (sdvox_reg == SDVOC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700564 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700565 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800566 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800567 } else if (sdvox_reg == HDMIB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700568 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700569 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800570 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800571 } else if (sdvox_reg == HDMIC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700572 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700573 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800574 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800575 } else if (sdvox_reg == HDMID) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700576 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700577 intel_hdmi->ddc_bus = GMBUS_PORT_DPD;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800578 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800579 }
Eric Anholt7d573822009-01-02 13:33:00 -0800580
Chris Wilsonea5b2132010-08-04 13:50:23 +0100581 intel_hdmi->sdvox_reg = sdvox_reg;
Eric Anholt7d573822009-01-02 13:33:00 -0800582
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530583 if (!HAS_PCH_SPLIT(dev)) {
Jesse Barnes45187ac2011-08-03 09:22:55 -0700584 intel_hdmi->write_infoframe = i9xx_write_infoframe;
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530585 I915_WRITE(VIDEO_DIP_CTL, 0);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700586 } else if (IS_VALLEYVIEW(dev)) {
587 intel_hdmi->write_infoframe = vlv_write_infoframe;
588 for_each_pipe(i)
589 I915_WRITE(VLV_TVIDEO_DIP_CTL(i), 0);
590 } else {
Jesse Barnes45187ac2011-08-03 09:22:55 -0700591 intel_hdmi->write_infoframe = ironlake_write_infoframe;
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530592 for_each_pipe(i)
593 I915_WRITE(TVIDEO_DIP_CTL(i), 0);
594 }
Jesse Barnes45187ac2011-08-03 09:22:55 -0700595
Chris Wilson4ef69c72010-09-09 15:14:28 +0100596 drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs);
Eric Anholt7d573822009-01-02 13:33:00 -0800597
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100598 intel_hdmi_add_properties(intel_hdmi, connector);
599
Chris Wilsondf0e9242010-09-09 16:20:55 +0100600 intel_connector_attach_encoder(intel_connector, intel_encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800601 drm_sysfs_connector_add(connector);
602
603 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
604 * 0xd. Failure to do so will result in spurious interrupts being
605 * generated on the port when a cable is not attached.
606 */
607 if (IS_G4X(dev) && !IS_GM45(dev)) {
608 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
609 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
610 }
Eric Anholt7d573822009-01-02 13:33:00 -0800611}