blob: 65727f0a79a3440202e5f315762431d975b5a9cf [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
40struct intel_hdmi_priv {
41 u32 sdvox_reg;
Ma Ling9dff6af2009-04-02 13:13:26 +080042 bool has_hdmi_sink;
Eric Anholt7d573822009-01-02 13:33:00 -080043};
44
45static void intel_hdmi_mode_set(struct drm_encoder *encoder,
46 struct drm_display_mode *mode,
47 struct drm_display_mode *adjusted_mode)
48{
49 struct drm_device *dev = encoder->dev;
50 struct drm_i915_private *dev_priv = dev->dev_private;
51 struct drm_crtc *crtc = encoder->crtc;
52 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eric Anholt21d40d32010-03-25 11:11:14 -070053 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
54 struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
Eric Anholt7d573822009-01-02 13:33:00 -080055 u32 sdvox;
56
57 sdvox = SDVO_ENCODING_HDMI |
58 SDVO_BORDER_ENABLE |
59 SDVO_VSYNC_ACTIVE_HIGH |
Zhenyu Wang56d21b02009-06-17 09:43:25 +080060 SDVO_HSYNC_ACTIVE_HIGH;
Eric Anholt7d573822009-01-02 13:33:00 -080061
62 if (hdmi_priv->has_hdmi_sink)
63 sdvox |= SDVO_AUDIO_ENABLE;
64
Zhenyu Wang0f229062010-04-07 16:15:57 +080065 if (intel_crtc->pipe == 1) {
66 if (HAS_PCH_CPT(dev))
67 sdvox |= PORT_TRANS_B_SEL_CPT;
68 else
69 sdvox |= SDVO_PIPE_B_SELECT;
70 }
Eric Anholt7d573822009-01-02 13:33:00 -080071
72 I915_WRITE(hdmi_priv->sdvox_reg, sdvox);
73 POSTING_READ(hdmi_priv->sdvox_reg);
74}
75
76static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
77{
78 struct drm_device *dev = encoder->dev;
79 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -070080 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
81 struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
Eric Anholt7d573822009-01-02 13:33:00 -080082 u32 temp;
83
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000084 temp = I915_READ(hdmi_priv->sdvox_reg);
85
86 /* HW workaround, need to toggle enable bit off and on for 12bpc, but
87 * we do this anyway which shows more stable in testing.
88 */
Eric Anholtc619eed2010-01-28 16:45:52 -080089 if (HAS_PCH_SPLIT(dev)) {
Eric Anholt7d573822009-01-02 13:33:00 -080090 I915_WRITE(hdmi_priv->sdvox_reg, temp & ~SDVO_ENABLE);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000091 POSTING_READ(hdmi_priv->sdvox_reg);
Eric Anholt7d573822009-01-02 13:33:00 -080092 }
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000093
94 if (mode != DRM_MODE_DPMS_ON) {
95 temp &= ~SDVO_ENABLE;
96 } else {
97 temp |= SDVO_ENABLE;
98 }
99
100 I915_WRITE(hdmi_priv->sdvox_reg, temp);
Eric Anholt7d573822009-01-02 13:33:00 -0800101 POSTING_READ(hdmi_priv->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000102
103 /* HW workaround, need to write this twice for issue that may result
104 * in first write getting masked.
105 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800106 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000107 I915_WRITE(hdmi_priv->sdvox_reg, temp);
108 POSTING_READ(hdmi_priv->sdvox_reg);
109 }
Eric Anholt7d573822009-01-02 13:33:00 -0800110}
111
Eric Anholt7d573822009-01-02 13:33:00 -0800112static int intel_hdmi_mode_valid(struct drm_connector *connector,
113 struct drm_display_mode *mode)
114{
115 if (mode->clock > 165000)
116 return MODE_CLOCK_HIGH;
117 if (mode->clock < 20000)
118 return MODE_CLOCK_HIGH;
119
120 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
121 return MODE_NO_DBLESCAN;
122
123 return MODE_OK;
124}
125
126static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
127 struct drm_display_mode *mode,
128 struct drm_display_mode *adjusted_mode)
129{
130 return true;
131}
132
Keith Packardaa93d632009-05-05 09:52:46 -0700133static enum drm_connector_status
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800134intel_hdmi_detect(struct drm_connector *connector)
Ma Ling9dff6af2009-04-02 13:13:26 +0800135{
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800136 struct drm_encoder *encoder = intel_attached_encoder(connector);
137 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Eric Anholt21d40d32010-03-25 11:11:14 -0700138 struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
Ma Ling9dff6af2009-04-02 13:13:26 +0800139 struct edid *edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700140 enum drm_connector_status status = connector_status_disconnected;
Ma Ling9dff6af2009-04-02 13:13:26 +0800141
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800142 hdmi_priv->has_hdmi_sink = false;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800143 edid = drm_get_edid(connector,
Eric Anholt21d40d32010-03-25 11:11:14 -0700144 intel_encoder->ddc_bus);
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800145
Keith Packardaa93d632009-05-05 09:52:46 -0700146 if (edid) {
Eric Anholtbe9f1c42009-06-21 22:14:55 -0700147 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
Keith Packardaa93d632009-05-05 09:52:46 -0700148 status = connector_status_connected;
149 hdmi_priv->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
150 }
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800151 connector->display_info.raw_edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700152 kfree(edid);
Ma Ling9dff6af2009-04-02 13:13:26 +0800153 }
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800154
Keith Packardaa93d632009-05-05 09:52:46 -0700155 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +0800156}
157
Eric Anholt7d573822009-01-02 13:33:00 -0800158static int intel_hdmi_get_modes(struct drm_connector *connector)
159{
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800160 struct drm_encoder *encoder = intel_attached_encoder(connector);
161 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800162
163 /* We should parse the EDID data and find out if it's an HDMI sink so
164 * we can send audio to it.
165 */
166
Zhenyu Wang335af9a2010-03-30 14:39:31 +0800167 return intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
Eric Anholt7d573822009-01-02 13:33:00 -0800168}
169
170static void intel_hdmi_destroy(struct drm_connector *connector)
171{
Eric Anholt7d573822009-01-02 13:33:00 -0800172 drm_sysfs_connector_remove(connector);
173 drm_connector_cleanup(connector);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800174 kfree(connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800175}
176
177static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
178 .dpms = intel_hdmi_dpms,
179 .mode_fixup = intel_hdmi_mode_fixup,
180 .prepare = intel_encoder_prepare,
181 .mode_set = intel_hdmi_mode_set,
182 .commit = intel_encoder_commit,
183};
184
185static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700186 .dpms = drm_helper_connector_dpms,
Eric Anholt7d573822009-01-02 13:33:00 -0800187 .detect = intel_hdmi_detect,
188 .fill_modes = drm_helper_probe_single_connector_modes,
189 .destroy = intel_hdmi_destroy,
190};
191
192static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
193 .get_modes = intel_hdmi_get_modes,
194 .mode_valid = intel_hdmi_mode_valid,
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800195 .best_encoder = intel_attached_encoder,
Eric Anholt7d573822009-01-02 13:33:00 -0800196};
197
198static void intel_hdmi_enc_destroy(struct drm_encoder *encoder)
199{
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800200 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
201
202 if (intel_encoder->i2c_bus)
203 intel_i2c_destroy(intel_encoder->i2c_bus);
Eric Anholt7d573822009-01-02 13:33:00 -0800204 drm_encoder_cleanup(encoder);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800205 kfree(intel_encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800206}
207
208static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
209 .destroy = intel_hdmi_enc_destroy,
210};
211
Eric Anholt7d573822009-01-02 13:33:00 -0800212void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
213{
214 struct drm_i915_private *dev_priv = dev->dev_private;
215 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -0700216 struct intel_encoder *intel_encoder;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800217 struct intel_connector *intel_connector;
Eric Anholt7d573822009-01-02 13:33:00 -0800218 struct intel_hdmi_priv *hdmi_priv;
219
Eric Anholt21d40d32010-03-25 11:11:14 -0700220 intel_encoder = kcalloc(sizeof(struct intel_encoder) +
Eric Anholt7d573822009-01-02 13:33:00 -0800221 sizeof(struct intel_hdmi_priv), 1, GFP_KERNEL);
Eric Anholt21d40d32010-03-25 11:11:14 -0700222 if (!intel_encoder)
Eric Anholt7d573822009-01-02 13:33:00 -0800223 return;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800224
225 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
226 if (!intel_connector) {
227 kfree(intel_encoder);
228 return;
229 }
230
Eric Anholt21d40d32010-03-25 11:11:14 -0700231 hdmi_priv = (struct intel_hdmi_priv *)(intel_encoder + 1);
Eric Anholt7d573822009-01-02 13:33:00 -0800232
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800233 connector = &intel_connector->base;
Eric Anholt7d573822009-01-02 13:33:00 -0800234 drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
Adam Jackson8d911042009-09-23 15:08:29 -0400235 DRM_MODE_CONNECTOR_HDMIA);
Eric Anholt7d573822009-01-02 13:33:00 -0800236 drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
237
Eric Anholt21d40d32010-03-25 11:11:14 -0700238 intel_encoder->type = INTEL_OUTPUT_HDMI;
Eric Anholt7d573822009-01-02 13:33:00 -0800239
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000240 connector->polled = DRM_CONNECTOR_POLL_HPD;
Eric Anholt7d573822009-01-02 13:33:00 -0800241 connector->interlace_allowed = 0;
242 connector->doublescan_allowed = 0;
Eric Anholt21d40d32010-03-25 11:11:14 -0700243 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Eric Anholt7d573822009-01-02 13:33:00 -0800244
245 /* Set up the DDC bus. */
Ma Lingf8aed702009-08-24 13:50:24 +0800246 if (sdvox_reg == SDVOB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700247 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
248 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOE, "HDMIB");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800249 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800250 } else if (sdvox_reg == SDVOC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700251 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
252 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOD, "HDMIC");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800253 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800254 } else if (sdvox_reg == HDMIB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700255 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
256 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOE,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800257 "HDMIB");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800258 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800259 } else if (sdvox_reg == HDMIC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700260 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
261 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOD,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800262 "HDMIC");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800263 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800264 } else if (sdvox_reg == HDMID) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700265 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
266 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOF,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800267 "HDMID");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800268 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800269 }
Eric Anholt21d40d32010-03-25 11:11:14 -0700270 if (!intel_encoder->ddc_bus)
Eric Anholt7d573822009-01-02 13:33:00 -0800271 goto err_connector;
272
273 hdmi_priv->sdvox_reg = sdvox_reg;
Eric Anholt21d40d32010-03-25 11:11:14 -0700274 intel_encoder->dev_priv = hdmi_priv;
Eric Anholt7d573822009-01-02 13:33:00 -0800275
Eric Anholt21d40d32010-03-25 11:11:14 -0700276 drm_encoder_init(dev, &intel_encoder->enc, &intel_hdmi_enc_funcs,
Eric Anholt7d573822009-01-02 13:33:00 -0800277 DRM_MODE_ENCODER_TMDS);
Eric Anholt21d40d32010-03-25 11:11:14 -0700278 drm_encoder_helper_add(&intel_encoder->enc, &intel_hdmi_helper_funcs);
Eric Anholt7d573822009-01-02 13:33:00 -0800279
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800280 drm_mode_connector_attach_encoder(&intel_connector->base,
Eric Anholt21d40d32010-03-25 11:11:14 -0700281 &intel_encoder->enc);
Eric Anholt7d573822009-01-02 13:33:00 -0800282 drm_sysfs_connector_add(connector);
283
284 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
285 * 0xd. Failure to do so will result in spurious interrupts being
286 * generated on the port when a cable is not attached.
287 */
288 if (IS_G4X(dev) && !IS_GM45(dev)) {
289 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
290 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
291 }
292
293 return;
294
295err_connector:
296 drm_connector_cleanup(connector);
Eric Anholt21d40d32010-03-25 11:11:14 -0700297 kfree(intel_encoder);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800298 kfree(intel_connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800299
300 return;
301}