2 * Copyright 2006 Dave Airlie <airlied@linux.ie>
3 * Copyright © 2006-2009 Intel Corporation
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:
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
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.
25 * Eric Anholt <eric@anholt.net>
26 * Jesse Barnes <jesse.barnes@intel.com>
29 #include <linux/i2c.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
36 #include "intel_drv.h"
41 struct intel_encoder base;
48 struct drm_property *force_audio_property;
51 static struct intel_hdmi *enc_to_intel_hdmi(struct drm_encoder *encoder)
53 return container_of(encoder, struct intel_hdmi, base.base);
56 static struct intel_hdmi *intel_attached_hdmi(struct drm_connector *connector)
58 return container_of(intel_attached_encoder(connector),
59 struct intel_hdmi, base);
62 void intel_dip_infoframe_csum(struct dip_infoframe *avi_if)
64 uint8_t *data = (uint8_t *)avi_if;
71 for (i = 0; i < sizeof(*avi_if); i++)
74 avi_if->checksum = 0x100 - sum;
77 static void intel_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
79 struct dip_infoframe avi_if = {
81 .ver = DIP_VERSION_AVI,
84 uint32_t *data = (uint32_t *)&avi_if;
85 struct drm_device *dev = encoder->dev;
86 struct drm_i915_private *dev_priv = dev->dev_private;
87 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
91 if (!intel_hdmi->has_hdmi_sink)
94 /* XXX first guess at handling video port, is this corrent? */
95 if (intel_hdmi->sdvox_reg == SDVOB)
96 port = VIDEO_DIP_PORT_B;
97 else if (intel_hdmi->sdvox_reg == SDVOC)
98 port = VIDEO_DIP_PORT_C;
102 I915_WRITE(VIDEO_DIP_CTL, VIDEO_DIP_ENABLE | port |
103 VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC);
105 intel_dip_infoframe_csum(&avi_if);
106 for (i = 0; i < sizeof(avi_if); i += 4) {
107 I915_WRITE(VIDEO_DIP_DATA, *data);
111 I915_WRITE(VIDEO_DIP_CTL, VIDEO_DIP_ENABLE | port |
112 VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC |
113 VIDEO_DIP_ENABLE_AVI);
116 static void intel_hdmi_mode_set(struct drm_encoder *encoder,
117 struct drm_display_mode *mode,
118 struct drm_display_mode *adjusted_mode)
120 struct drm_device *dev = encoder->dev;
121 struct drm_i915_private *dev_priv = dev->dev_private;
122 struct drm_crtc *crtc = encoder->crtc;
123 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
124 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
127 sdvox = SDVO_ENCODING_HDMI | SDVO_BORDER_ENABLE;
128 sdvox |= intel_hdmi->color_range;
129 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
130 sdvox |= SDVO_VSYNC_ACTIVE_HIGH;
131 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
132 sdvox |= SDVO_HSYNC_ACTIVE_HIGH;
134 /* Required on CPT */
135 if (intel_hdmi->has_hdmi_sink && HAS_PCH_CPT(dev))
136 sdvox |= HDMI_MODE_SELECT;
138 if (intel_hdmi->has_audio) {
139 sdvox |= SDVO_AUDIO_ENABLE;
140 sdvox |= SDVO_NULL_PACKETS_DURING_VSYNC;
143 if (intel_crtc->pipe == 1) {
144 if (HAS_PCH_CPT(dev))
145 sdvox |= PORT_TRANS_B_SEL_CPT;
147 sdvox |= SDVO_PIPE_B_SELECT;
150 I915_WRITE(intel_hdmi->sdvox_reg, sdvox);
151 POSTING_READ(intel_hdmi->sdvox_reg);
153 intel_hdmi_set_avi_infoframe(encoder);
156 static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
158 struct drm_device *dev = encoder->dev;
159 struct drm_i915_private *dev_priv = dev->dev_private;
160 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
163 temp = I915_READ(intel_hdmi->sdvox_reg);
165 /* HW workaround, need to toggle enable bit off and on for 12bpc, but
166 * we do this anyway which shows more stable in testing.
168 if (HAS_PCH_SPLIT(dev)) {
169 I915_WRITE(intel_hdmi->sdvox_reg, temp & ~SDVO_ENABLE);
170 POSTING_READ(intel_hdmi->sdvox_reg);
173 if (mode != DRM_MODE_DPMS_ON) {
174 temp &= ~SDVO_ENABLE;
179 I915_WRITE(intel_hdmi->sdvox_reg, temp);
180 POSTING_READ(intel_hdmi->sdvox_reg);
182 /* HW workaround, need to write this twice for issue that may result
183 * in first write getting masked.
185 if (HAS_PCH_SPLIT(dev)) {
186 I915_WRITE(intel_hdmi->sdvox_reg, temp);
187 POSTING_READ(intel_hdmi->sdvox_reg);
191 static int intel_hdmi_mode_valid(struct drm_connector *connector,
192 struct drm_display_mode *mode)
194 if (mode->clock > 165000)
195 return MODE_CLOCK_HIGH;
196 if (mode->clock < 20000)
197 return MODE_CLOCK_HIGH;
199 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
200 return MODE_NO_DBLESCAN;
205 static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
206 struct drm_display_mode *mode,
207 struct drm_display_mode *adjusted_mode)
212 static enum drm_connector_status
213 intel_hdmi_detect(struct drm_connector *connector, bool force)
215 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
216 struct drm_i915_private *dev_priv = connector->dev->dev_private;
218 enum drm_connector_status status = connector_status_disconnected;
220 intel_hdmi->has_hdmi_sink = false;
221 intel_hdmi->has_audio = false;
222 edid = drm_get_edid(connector,
223 &dev_priv->gmbus[intel_hdmi->ddc_bus].adapter);
226 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
227 status = connector_status_connected;
228 intel_hdmi->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
229 intel_hdmi->has_audio = drm_detect_monitor_audio(edid);
231 connector->display_info.raw_edid = NULL;
235 if (status == connector_status_connected) {
236 if (intel_hdmi->force_audio)
237 intel_hdmi->has_audio = intel_hdmi->force_audio > 0;
243 static int intel_hdmi_get_modes(struct drm_connector *connector)
245 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
246 struct drm_i915_private *dev_priv = connector->dev->dev_private;
248 /* We should parse the EDID data and find out if it's an HDMI sink so
249 * we can send audio to it.
252 return intel_ddc_get_modes(connector,
253 &dev_priv->gmbus[intel_hdmi->ddc_bus].adapter);
257 intel_hdmi_detect_audio(struct drm_connector *connector)
259 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
260 struct drm_i915_private *dev_priv = connector->dev->dev_private;
262 bool has_audio = false;
264 edid = drm_get_edid(connector,
265 &dev_priv->gmbus[intel_hdmi->ddc_bus].adapter);
267 if (edid->input & DRM_EDID_INPUT_DIGITAL)
268 has_audio = drm_detect_monitor_audio(edid);
270 connector->display_info.raw_edid = NULL;
278 intel_hdmi_set_property(struct drm_connector *connector,
279 struct drm_property *property,
282 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
283 struct drm_i915_private *dev_priv = connector->dev->dev_private;
286 ret = drm_connector_property_set_value(connector, property, val);
290 if (property == intel_hdmi->force_audio_property) {
294 if (i == intel_hdmi->force_audio)
297 intel_hdmi->force_audio = i;
300 has_audio = intel_hdmi_detect_audio(connector);
304 if (has_audio == intel_hdmi->has_audio)
307 intel_hdmi->has_audio = has_audio;
311 if (property == dev_priv->broadcast_rgb_property) {
312 if (val == !!intel_hdmi->color_range)
315 intel_hdmi->color_range = val ? SDVO_COLOR_RANGE_16_235 : 0;
322 if (intel_hdmi->base.base.crtc) {
323 struct drm_crtc *crtc = intel_hdmi->base.base.crtc;
324 drm_crtc_helper_set_mode(crtc, &crtc->mode,
332 static void intel_hdmi_destroy(struct drm_connector *connector)
334 drm_sysfs_connector_remove(connector);
335 drm_connector_cleanup(connector);
339 static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
340 .dpms = intel_hdmi_dpms,
341 .mode_fixup = intel_hdmi_mode_fixup,
342 .prepare = intel_encoder_prepare,
343 .mode_set = intel_hdmi_mode_set,
344 .commit = intel_encoder_commit,
347 static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
348 .dpms = drm_helper_connector_dpms,
349 .detect = intel_hdmi_detect,
350 .fill_modes = drm_helper_probe_single_connector_modes,
351 .set_property = intel_hdmi_set_property,
352 .destroy = intel_hdmi_destroy,
355 static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
356 .get_modes = intel_hdmi_get_modes,
357 .mode_valid = intel_hdmi_mode_valid,
358 .best_encoder = intel_best_encoder,
361 static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
362 .destroy = intel_encoder_destroy,
366 intel_hdmi_add_properties(struct intel_hdmi *intel_hdmi, struct drm_connector *connector)
368 struct drm_device *dev = connector->dev;
370 intel_hdmi->force_audio_property =
371 drm_property_create(dev, DRM_MODE_PROP_RANGE, "force_audio", 2);
372 if (intel_hdmi->force_audio_property) {
373 intel_hdmi->force_audio_property->values[0] = -1;
374 intel_hdmi->force_audio_property->values[1] = 1;
375 drm_connector_attach_property(connector, intel_hdmi->force_audio_property, 0);
378 intel_attach_broadcast_rgb_property(connector);
381 void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
383 struct drm_i915_private *dev_priv = dev->dev_private;
384 struct drm_connector *connector;
385 struct intel_encoder *intel_encoder;
386 struct intel_connector *intel_connector;
387 struct intel_hdmi *intel_hdmi;
389 intel_hdmi = kzalloc(sizeof(struct intel_hdmi), GFP_KERNEL);
393 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
394 if (!intel_connector) {
399 intel_encoder = &intel_hdmi->base;
400 drm_encoder_init(dev, &intel_encoder->base, &intel_hdmi_enc_funcs,
401 DRM_MODE_ENCODER_TMDS);
403 connector = &intel_connector->base;
404 drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
405 DRM_MODE_CONNECTOR_HDMIA);
406 drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
408 intel_encoder->type = INTEL_OUTPUT_HDMI;
410 connector->polled = DRM_CONNECTOR_POLL_HPD;
411 connector->interlace_allowed = 0;
412 connector->doublescan_allowed = 0;
413 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
415 /* Set up the DDC bus. */
416 if (sdvox_reg == SDVOB) {
417 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
418 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
419 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
420 } else if (sdvox_reg == SDVOC) {
421 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
422 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
423 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
424 } else if (sdvox_reg == HDMIB) {
425 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
426 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
427 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
428 } else if (sdvox_reg == HDMIC) {
429 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
430 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
431 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
432 } else if (sdvox_reg == HDMID) {
433 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
434 intel_hdmi->ddc_bus = GMBUS_PORT_DPD;
435 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
438 intel_hdmi->sdvox_reg = sdvox_reg;
440 drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs);
442 intel_hdmi_add_properties(intel_hdmi, connector);
444 intel_connector_attach_encoder(intel_connector, intel_encoder);
445 drm_sysfs_connector_add(connector);
447 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
448 * 0xd. Failure to do so will result in spurious interrupts being
449 * generated on the port when a cable is not attached.
451 if (IS_G4X(dev) && !IS_GM45(dev)) {
452 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
453 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);