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;
47 struct drm_property *force_audio_property;
50 static struct intel_hdmi *enc_to_intel_hdmi(struct drm_encoder *encoder)
52 return container_of(encoder, struct intel_hdmi, base.base);
55 static struct intel_hdmi *intel_attached_hdmi(struct drm_connector *connector)
57 return container_of(intel_attached_encoder(connector),
58 struct intel_hdmi, base);
61 static void intel_hdmi_mode_set(struct drm_encoder *encoder,
62 struct drm_display_mode *mode,
63 struct drm_display_mode *adjusted_mode)
65 struct drm_device *dev = encoder->dev;
66 struct drm_i915_private *dev_priv = dev->dev_private;
67 struct drm_crtc *crtc = encoder->crtc;
68 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
69 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
72 sdvox = SDVO_ENCODING_HDMI | SDVO_BORDER_ENABLE;
73 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
74 sdvox |= SDVO_VSYNC_ACTIVE_HIGH;
75 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
76 sdvox |= SDVO_HSYNC_ACTIVE_HIGH;
79 if (intel_hdmi->has_hdmi_sink && HAS_PCH_CPT(dev))
80 sdvox |= HDMI_MODE_SELECT;
82 if (intel_hdmi->has_audio)
83 sdvox |= SDVO_AUDIO_ENABLE;
85 if (intel_crtc->pipe == 1) {
87 sdvox |= PORT_TRANS_B_SEL_CPT;
89 sdvox |= SDVO_PIPE_B_SELECT;
92 I915_WRITE(intel_hdmi->sdvox_reg, sdvox);
93 POSTING_READ(intel_hdmi->sdvox_reg);
96 static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
98 struct drm_device *dev = encoder->dev;
99 struct drm_i915_private *dev_priv = dev->dev_private;
100 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
103 temp = I915_READ(intel_hdmi->sdvox_reg);
105 /* HW workaround, need to toggle enable bit off and on for 12bpc, but
106 * we do this anyway which shows more stable in testing.
108 if (HAS_PCH_SPLIT(dev)) {
109 I915_WRITE(intel_hdmi->sdvox_reg, temp & ~SDVO_ENABLE);
110 POSTING_READ(intel_hdmi->sdvox_reg);
113 if (mode != DRM_MODE_DPMS_ON) {
114 temp &= ~SDVO_ENABLE;
119 I915_WRITE(intel_hdmi->sdvox_reg, temp);
120 POSTING_READ(intel_hdmi->sdvox_reg);
122 /* HW workaround, need to write this twice for issue that may result
123 * in first write getting masked.
125 if (HAS_PCH_SPLIT(dev)) {
126 I915_WRITE(intel_hdmi->sdvox_reg, temp);
127 POSTING_READ(intel_hdmi->sdvox_reg);
131 static int intel_hdmi_mode_valid(struct drm_connector *connector,
132 struct drm_display_mode *mode)
134 if (mode->clock > 165000)
135 return MODE_CLOCK_HIGH;
136 if (mode->clock < 20000)
137 return MODE_CLOCK_HIGH;
139 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
140 return MODE_NO_DBLESCAN;
145 static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
146 struct drm_display_mode *mode,
147 struct drm_display_mode *adjusted_mode)
152 static enum drm_connector_status
153 intel_hdmi_detect(struct drm_connector *connector, bool force)
155 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
156 struct drm_i915_private *dev_priv = connector->dev->dev_private;
158 enum drm_connector_status status = connector_status_disconnected;
160 intel_hdmi->has_hdmi_sink = false;
161 intel_hdmi->has_audio = false;
162 edid = drm_get_edid(connector,
163 &dev_priv->gmbus[intel_hdmi->ddc_bus].adapter);
166 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
167 status = connector_status_connected;
168 intel_hdmi->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
169 intel_hdmi->has_audio = drm_detect_monitor_audio(edid);
171 connector->display_info.raw_edid = NULL;
175 if (status == connector_status_connected) {
176 if (intel_hdmi->force_audio)
177 intel_hdmi->has_audio = intel_hdmi->force_audio > 0;
183 static int intel_hdmi_get_modes(struct drm_connector *connector)
185 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
186 struct drm_i915_private *dev_priv = connector->dev->dev_private;
188 /* We should parse the EDID data and find out if it's an HDMI sink so
189 * we can send audio to it.
192 return intel_ddc_get_modes(connector,
193 &dev_priv->gmbus[intel_hdmi->ddc_bus].adapter);
197 intel_hdmi_set_property(struct drm_connector *connector,
198 struct drm_property *property,
201 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
204 ret = drm_connector_property_set_value(connector, property, val);
208 if (property == intel_hdmi->force_audio_property) {
209 if (val == intel_hdmi->force_audio)
212 intel_hdmi->force_audio = val;
214 if (val > 0 && intel_hdmi->has_audio)
216 if (val < 0 && !intel_hdmi->has_audio)
219 intel_hdmi->has_audio = val > 0;
226 if (intel_hdmi->base.base.crtc) {
227 struct drm_crtc *crtc = intel_hdmi->base.base.crtc;
228 drm_crtc_helper_set_mode(crtc, &crtc->mode,
236 static void intel_hdmi_destroy(struct drm_connector *connector)
238 drm_sysfs_connector_remove(connector);
239 drm_connector_cleanup(connector);
243 static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
244 .dpms = intel_hdmi_dpms,
245 .mode_fixup = intel_hdmi_mode_fixup,
246 .prepare = intel_encoder_prepare,
247 .mode_set = intel_hdmi_mode_set,
248 .commit = intel_encoder_commit,
251 static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
252 .dpms = drm_helper_connector_dpms,
253 .detect = intel_hdmi_detect,
254 .fill_modes = drm_helper_probe_single_connector_modes,
255 .set_property = intel_hdmi_set_property,
256 .destroy = intel_hdmi_destroy,
259 static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
260 .get_modes = intel_hdmi_get_modes,
261 .mode_valid = intel_hdmi_mode_valid,
262 .best_encoder = intel_best_encoder,
265 static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
266 .destroy = intel_encoder_destroy,
270 intel_hdmi_add_properties(struct intel_hdmi *intel_hdmi, struct drm_connector *connector)
272 struct drm_device *dev = connector->dev;
274 intel_hdmi->force_audio_property =
275 drm_property_create(dev, DRM_MODE_PROP_RANGE, "force_audio", 2);
276 if (intel_hdmi->force_audio_property) {
277 intel_hdmi->force_audio_property->values[0] = -1;
278 intel_hdmi->force_audio_property->values[1] = 1;
279 drm_connector_attach_property(connector, intel_hdmi->force_audio_property, 0);
283 void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
285 struct drm_i915_private *dev_priv = dev->dev_private;
286 struct drm_connector *connector;
287 struct intel_encoder *intel_encoder;
288 struct intel_connector *intel_connector;
289 struct intel_hdmi *intel_hdmi;
291 intel_hdmi = kzalloc(sizeof(struct intel_hdmi), GFP_KERNEL);
295 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
296 if (!intel_connector) {
301 intel_encoder = &intel_hdmi->base;
302 drm_encoder_init(dev, &intel_encoder->base, &intel_hdmi_enc_funcs,
303 DRM_MODE_ENCODER_TMDS);
305 connector = &intel_connector->base;
306 drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
307 DRM_MODE_CONNECTOR_HDMIA);
308 drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
310 intel_encoder->type = INTEL_OUTPUT_HDMI;
312 connector->polled = DRM_CONNECTOR_POLL_HPD;
313 connector->interlace_allowed = 0;
314 connector->doublescan_allowed = 0;
315 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
317 /* Set up the DDC bus. */
318 if (sdvox_reg == SDVOB) {
319 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
320 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
321 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
322 } else if (sdvox_reg == SDVOC) {
323 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
324 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
325 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
326 } else if (sdvox_reg == HDMIB) {
327 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
328 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
329 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
330 } else if (sdvox_reg == HDMIC) {
331 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
332 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
333 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
334 } else if (sdvox_reg == HDMID) {
335 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
336 intel_hdmi->ddc_bus = GMBUS_PORT_DPD;
337 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
340 intel_hdmi->sdvox_reg = sdvox_reg;
342 drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs);
344 intel_hdmi_add_properties(intel_hdmi, connector);
346 intel_connector_attach_encoder(intel_connector, intel_encoder);
347 drm_sysfs_connector_add(connector);
349 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
350 * 0xd. Failure to do so will result in spurious interrupts being
351 * generated on the port when a cable is not attached.
353 if (IS_G4X(dev) && !IS_GM45(dev)) {
354 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
355 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);