blob: fd9544852d33ca1e1695237434004c8d5995dcb6 [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>
30#include <linux/delay.h>
31#include "drmP.h"
32#include "drm.h"
33#include "drm_crtc.h"
Keith Packardaa93d632009-05-05 09:52:46 -070034#include "drm_edid.h"
Eric Anholt7d573822009-01-02 13:33:00 -080035#include "intel_drv.h"
36#include "i915_drm.h"
37#include "i915_drv.h"
38
39struct intel_hdmi_priv {
40 u32 sdvox_reg;
Ma Ling9dff6af2009-04-02 13:13:26 +080041 bool has_hdmi_sink;
Eric Anholt7d573822009-01-02 13:33:00 -080042};
43
44static void intel_hdmi_mode_set(struct drm_encoder *encoder,
45 struct drm_display_mode *mode,
46 struct drm_display_mode *adjusted_mode)
47{
48 struct drm_device *dev = encoder->dev;
49 struct drm_i915_private *dev_priv = dev->dev_private;
50 struct drm_crtc *crtc = encoder->crtc;
51 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eric Anholt21d40d32010-03-25 11:11:14 -070052 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
53 struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
Eric Anholt7d573822009-01-02 13:33:00 -080054 u32 sdvox;
55
56 sdvox = SDVO_ENCODING_HDMI |
57 SDVO_BORDER_ENABLE |
58 SDVO_VSYNC_ACTIVE_HIGH |
Zhenyu Wang56d21b02009-06-17 09:43:25 +080059 SDVO_HSYNC_ACTIVE_HIGH;
Eric Anholt7d573822009-01-02 13:33:00 -080060
61 if (hdmi_priv->has_hdmi_sink)
62 sdvox |= SDVO_AUDIO_ENABLE;
63
Zhenyu Wang0f229062010-04-07 16:15:57 +080064 if (intel_crtc->pipe == 1) {
65 if (HAS_PCH_CPT(dev))
66 sdvox |= PORT_TRANS_B_SEL_CPT;
67 else
68 sdvox |= SDVO_PIPE_B_SELECT;
69 }
Eric Anholt7d573822009-01-02 13:33:00 -080070
71 I915_WRITE(hdmi_priv->sdvox_reg, sdvox);
72 POSTING_READ(hdmi_priv->sdvox_reg);
73}
74
75static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
76{
77 struct drm_device *dev = encoder->dev;
78 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -070079 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
80 struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
Eric Anholt7d573822009-01-02 13:33:00 -080081 u32 temp;
82
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000083 temp = I915_READ(hdmi_priv->sdvox_reg);
84
85 /* HW workaround, need to toggle enable bit off and on for 12bpc, but
86 * we do this anyway which shows more stable in testing.
87 */
Eric Anholtc619eed2010-01-28 16:45:52 -080088 if (HAS_PCH_SPLIT(dev)) {
Eric Anholt7d573822009-01-02 13:33:00 -080089 I915_WRITE(hdmi_priv->sdvox_reg, temp & ~SDVO_ENABLE);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000090 POSTING_READ(hdmi_priv->sdvox_reg);
Eric Anholt7d573822009-01-02 13:33:00 -080091 }
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000092
93 if (mode != DRM_MODE_DPMS_ON) {
94 temp &= ~SDVO_ENABLE;
95 } else {
96 temp |= SDVO_ENABLE;
97 }
98
99 I915_WRITE(hdmi_priv->sdvox_reg, temp);
Eric Anholt7d573822009-01-02 13:33:00 -0800100 POSTING_READ(hdmi_priv->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000101
102 /* HW workaround, need to write this twice for issue that may result
103 * in first write getting masked.
104 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800105 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000106 I915_WRITE(hdmi_priv->sdvox_reg, temp);
107 POSTING_READ(hdmi_priv->sdvox_reg);
108 }
Eric Anholt7d573822009-01-02 13:33:00 -0800109}
110
Eric Anholt7d573822009-01-02 13:33:00 -0800111static int intel_hdmi_mode_valid(struct drm_connector *connector,
112 struct drm_display_mode *mode)
113{
114 if (mode->clock > 165000)
115 return MODE_CLOCK_HIGH;
116 if (mode->clock < 20000)
117 return MODE_CLOCK_HIGH;
118
119 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
120 return MODE_NO_DBLESCAN;
121
122 return MODE_OK;
123}
124
125static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
126 struct drm_display_mode *mode,
127 struct drm_display_mode *adjusted_mode)
128{
129 return true;
130}
131
Keith Packardaa93d632009-05-05 09:52:46 -0700132static enum drm_connector_status
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800133intel_hdmi_detect(struct drm_connector *connector)
Ma Ling9dff6af2009-04-02 13:13:26 +0800134{
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800135 struct drm_encoder *encoder = intel_attached_encoder(connector);
136 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Eric Anholt21d40d32010-03-25 11:11:14 -0700137 struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
Ma Ling9dff6af2009-04-02 13:13:26 +0800138 struct edid *edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700139 enum drm_connector_status status = connector_status_disconnected;
Ma Ling9dff6af2009-04-02 13:13:26 +0800140
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800141 hdmi_priv->has_hdmi_sink = false;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800142 edid = drm_get_edid(connector,
Eric Anholt21d40d32010-03-25 11:11:14 -0700143 intel_encoder->ddc_bus);
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800144
Keith Packardaa93d632009-05-05 09:52:46 -0700145 if (edid) {
Eric Anholtbe9f1c42009-06-21 22:14:55 -0700146 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
Keith Packardaa93d632009-05-05 09:52:46 -0700147 status = connector_status_connected;
148 hdmi_priv->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
149 }
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800150 connector->display_info.raw_edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700151 kfree(edid);
Ma Ling9dff6af2009-04-02 13:13:26 +0800152 }
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800153
Keith Packardaa93d632009-05-05 09:52:46 -0700154 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +0800155}
156
Eric Anholt7d573822009-01-02 13:33:00 -0800157static int intel_hdmi_get_modes(struct drm_connector *connector)
158{
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800159 struct drm_encoder *encoder = intel_attached_encoder(connector);
160 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800161
162 /* We should parse the EDID data and find out if it's an HDMI sink so
163 * we can send audio to it.
164 */
165
Zhenyu Wang335af9a2010-03-30 14:39:31 +0800166 return intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
Eric Anholt7d573822009-01-02 13:33:00 -0800167}
168
169static void intel_hdmi_destroy(struct drm_connector *connector)
170{
Eric Anholt7d573822009-01-02 13:33:00 -0800171 drm_sysfs_connector_remove(connector);
172 drm_connector_cleanup(connector);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800173 kfree(connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800174}
175
176static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
177 .dpms = intel_hdmi_dpms,
178 .mode_fixup = intel_hdmi_mode_fixup,
179 .prepare = intel_encoder_prepare,
180 .mode_set = intel_hdmi_mode_set,
181 .commit = intel_encoder_commit,
182};
183
184static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700185 .dpms = drm_helper_connector_dpms,
Eric Anholt7d573822009-01-02 13:33:00 -0800186 .detect = intel_hdmi_detect,
187 .fill_modes = drm_helper_probe_single_connector_modes,
188 .destroy = intel_hdmi_destroy,
189};
190
191static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
192 .get_modes = intel_hdmi_get_modes,
193 .mode_valid = intel_hdmi_mode_valid,
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800194 .best_encoder = intel_attached_encoder,
Eric Anholt7d573822009-01-02 13:33:00 -0800195};
196
197static void intel_hdmi_enc_destroy(struct drm_encoder *encoder)
198{
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800199 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
200
201 if (intel_encoder->i2c_bus)
202 intel_i2c_destroy(intel_encoder->i2c_bus);
Eric Anholt7d573822009-01-02 13:33:00 -0800203 drm_encoder_cleanup(encoder);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800204 kfree(intel_encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800205}
206
207static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
208 .destroy = intel_hdmi_enc_destroy,
209};
210
Eric Anholt7d573822009-01-02 13:33:00 -0800211void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
212{
213 struct drm_i915_private *dev_priv = dev->dev_private;
214 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -0700215 struct intel_encoder *intel_encoder;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800216 struct intel_connector *intel_connector;
Eric Anholt7d573822009-01-02 13:33:00 -0800217 struct intel_hdmi_priv *hdmi_priv;
218
Eric Anholt21d40d32010-03-25 11:11:14 -0700219 intel_encoder = kcalloc(sizeof(struct intel_encoder) +
Eric Anholt7d573822009-01-02 13:33:00 -0800220 sizeof(struct intel_hdmi_priv), 1, GFP_KERNEL);
Eric Anholt21d40d32010-03-25 11:11:14 -0700221 if (!intel_encoder)
Eric Anholt7d573822009-01-02 13:33:00 -0800222 return;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800223
224 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
225 if (!intel_connector) {
226 kfree(intel_encoder);
227 return;
228 }
229
Eric Anholt21d40d32010-03-25 11:11:14 -0700230 hdmi_priv = (struct intel_hdmi_priv *)(intel_encoder + 1);
Eric Anholt7d573822009-01-02 13:33:00 -0800231
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800232 connector = &intel_connector->base;
Eric Anholt7d573822009-01-02 13:33:00 -0800233 drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
Adam Jackson8d911042009-09-23 15:08:29 -0400234 DRM_MODE_CONNECTOR_HDMIA);
Eric Anholt7d573822009-01-02 13:33:00 -0800235 drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
236
Eric Anholt21d40d32010-03-25 11:11:14 -0700237 intel_encoder->type = INTEL_OUTPUT_HDMI;
Eric Anholt7d573822009-01-02 13:33:00 -0800238
239 connector->interlace_allowed = 0;
240 connector->doublescan_allowed = 0;
Eric Anholt21d40d32010-03-25 11:11:14 -0700241 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Eric Anholt7d573822009-01-02 13:33:00 -0800242
243 /* Set up the DDC bus. */
Ma Lingf8aed702009-08-24 13:50:24 +0800244 if (sdvox_reg == SDVOB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700245 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
246 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOE, "HDMIB");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800247 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800248 } else if (sdvox_reg == SDVOC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700249 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
250 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOD, "HDMIC");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800251 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800252 } else if (sdvox_reg == HDMIB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700253 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
254 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOE,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800255 "HDMIB");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800256 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800257 } else if (sdvox_reg == HDMIC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700258 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
259 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOD,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800260 "HDMIC");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800261 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800262 } else if (sdvox_reg == HDMID) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700263 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
264 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOF,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800265 "HDMID");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800266 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800267 }
Eric Anholt21d40d32010-03-25 11:11:14 -0700268 if (!intel_encoder->ddc_bus)
Eric Anholt7d573822009-01-02 13:33:00 -0800269 goto err_connector;
270
271 hdmi_priv->sdvox_reg = sdvox_reg;
Eric Anholt21d40d32010-03-25 11:11:14 -0700272 intel_encoder->dev_priv = hdmi_priv;
Eric Anholt7d573822009-01-02 13:33:00 -0800273
Eric Anholt21d40d32010-03-25 11:11:14 -0700274 drm_encoder_init(dev, &intel_encoder->enc, &intel_hdmi_enc_funcs,
Eric Anholt7d573822009-01-02 13:33:00 -0800275 DRM_MODE_ENCODER_TMDS);
Eric Anholt21d40d32010-03-25 11:11:14 -0700276 drm_encoder_helper_add(&intel_encoder->enc, &intel_hdmi_helper_funcs);
Eric Anholt7d573822009-01-02 13:33:00 -0800277
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800278 drm_mode_connector_attach_encoder(&intel_connector->base,
Eric Anholt21d40d32010-03-25 11:11:14 -0700279 &intel_encoder->enc);
Eric Anholt7d573822009-01-02 13:33:00 -0800280 drm_sysfs_connector_add(connector);
281
282 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
283 * 0xd. Failure to do so will result in spurious interrupts being
284 * generated on the port when a cable is not attached.
285 */
286 if (IS_G4X(dev) && !IS_GM45(dev)) {
287 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
288 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
289 }
290
291 return;
292
293err_connector:
294 drm_connector_cleanup(connector);
Eric Anholt21d40d32010-03-25 11:11:14 -0700295 kfree(intel_encoder);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800296 kfree(intel_connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800297
298 return;
299}