blob: bba0aba15a9632798b8d0f055778b6692637d621 [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;
Ma Ling9dff6af2009-04-02 13:13:26 +080043 bool has_hdmi_sink;
Eric Anholt7d573822009-01-02 13:33:00 -080044};
45
Chris Wilsonea5b2132010-08-04 13:50:23 +010046static struct intel_hdmi *enc_to_intel_hdmi(struct drm_encoder *encoder)
47{
Chris Wilson4ef69c72010-09-09 15:14:28 +010048 return container_of(encoder, struct intel_hdmi, base.base);
Chris Wilsonea5b2132010-08-04 13:50:23 +010049}
50
Chris Wilsondf0e9242010-09-09 16:20:55 +010051static struct intel_hdmi *intel_attached_hdmi(struct drm_connector *connector)
52{
53 return container_of(intel_attached_encoder(connector),
54 struct intel_hdmi, base);
55}
56
Eric Anholt7d573822009-01-02 13:33:00 -080057static void intel_hdmi_mode_set(struct drm_encoder *encoder,
58 struct drm_display_mode *mode,
59 struct drm_display_mode *adjusted_mode)
60{
61 struct drm_device *dev = encoder->dev;
62 struct drm_i915_private *dev_priv = dev->dev_private;
63 struct drm_crtc *crtc = encoder->crtc;
64 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonea5b2132010-08-04 13:50:23 +010065 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -080066 u32 sdvox;
67
Adam Jacksonb599c0b2010-07-16 14:46:31 -040068 sdvox = SDVO_ENCODING_HDMI | SDVO_BORDER_ENABLE;
69 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
70 sdvox |= SDVO_VSYNC_ACTIVE_HIGH;
71 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
72 sdvox |= SDVO_HSYNC_ACTIVE_HIGH;
Eric Anholt7d573822009-01-02 13:33:00 -080073
Chris Wilsonea5b2132010-08-04 13:50:23 +010074 if (intel_hdmi->has_hdmi_sink) {
Eric Anholt7d573822009-01-02 13:33:00 -080075 sdvox |= SDVO_AUDIO_ENABLE;
Zhenyu Wang467b2002010-05-12 11:02:14 +080076 if (HAS_PCH_CPT(dev))
77 sdvox |= HDMI_MODE_SELECT;
78 }
Eric Anholt7d573822009-01-02 13:33:00 -080079
Zhenyu Wang0f229062010-04-07 16:15:57 +080080 if (intel_crtc->pipe == 1) {
81 if (HAS_PCH_CPT(dev))
82 sdvox |= PORT_TRANS_B_SEL_CPT;
83 else
84 sdvox |= SDVO_PIPE_B_SELECT;
85 }
Eric Anholt7d573822009-01-02 13:33:00 -080086
Chris Wilsonea5b2132010-08-04 13:50:23 +010087 I915_WRITE(intel_hdmi->sdvox_reg, sdvox);
88 POSTING_READ(intel_hdmi->sdvox_reg);
Eric Anholt7d573822009-01-02 13:33:00 -080089}
90
91static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
92{
93 struct drm_device *dev = encoder->dev;
94 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +010095 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -080096 u32 temp;
97
Chris Wilsonea5b2132010-08-04 13:50:23 +010098 temp = I915_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000099
100 /* HW workaround, need to toggle enable bit off and on for 12bpc, but
101 * we do this anyway which shows more stable in testing.
102 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800103 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100104 I915_WRITE(intel_hdmi->sdvox_reg, temp & ~SDVO_ENABLE);
105 POSTING_READ(intel_hdmi->sdvox_reg);
Eric Anholt7d573822009-01-02 13:33:00 -0800106 }
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000107
108 if (mode != DRM_MODE_DPMS_ON) {
109 temp &= ~SDVO_ENABLE;
110 } else {
111 temp |= SDVO_ENABLE;
112 }
113
Chris Wilsonea5b2132010-08-04 13:50:23 +0100114 I915_WRITE(intel_hdmi->sdvox_reg, temp);
115 POSTING_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000116
117 /* HW workaround, need to write this twice for issue that may result
118 * in first write getting masked.
119 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800120 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100121 I915_WRITE(intel_hdmi->sdvox_reg, temp);
122 POSTING_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000123 }
Eric Anholt7d573822009-01-02 13:33:00 -0800124}
125
Eric Anholt7d573822009-01-02 13:33:00 -0800126static int intel_hdmi_mode_valid(struct drm_connector *connector,
127 struct drm_display_mode *mode)
128{
129 if (mode->clock > 165000)
130 return MODE_CLOCK_HIGH;
131 if (mode->clock < 20000)
132 return MODE_CLOCK_HIGH;
133
134 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
135 return MODE_NO_DBLESCAN;
136
137 return MODE_OK;
138}
139
140static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
141 struct drm_display_mode *mode,
142 struct drm_display_mode *adjusted_mode)
143{
144 return true;
145}
146
Keith Packardaa93d632009-05-05 09:52:46 -0700147static enum drm_connector_status
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800148intel_hdmi_detect(struct drm_connector *connector)
Ma Ling9dff6af2009-04-02 13:13:26 +0800149{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100150 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Ma Ling9dff6af2009-04-02 13:13:26 +0800151 struct edid *edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700152 enum drm_connector_status status = connector_status_disconnected;
Ma Ling9dff6af2009-04-02 13:13:26 +0800153
Chris Wilsonea5b2132010-08-04 13:50:23 +0100154 intel_hdmi->has_hdmi_sink = false;
155 edid = drm_get_edid(connector, intel_hdmi->base.ddc_bus);
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800156
Keith Packardaa93d632009-05-05 09:52:46 -0700157 if (edid) {
Eric Anholtbe9f1c42009-06-21 22:14:55 -0700158 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
Keith Packardaa93d632009-05-05 09:52:46 -0700159 status = connector_status_connected;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100160 intel_hdmi->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
Keith Packardaa93d632009-05-05 09:52:46 -0700161 }
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800162 connector->display_info.raw_edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700163 kfree(edid);
Ma Ling9dff6af2009-04-02 13:13:26 +0800164 }
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800165
Keith Packardaa93d632009-05-05 09:52:46 -0700166 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +0800167}
168
Eric Anholt7d573822009-01-02 13:33:00 -0800169static int intel_hdmi_get_modes(struct drm_connector *connector)
170{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100171 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800172
173 /* We should parse the EDID data and find out if it's an HDMI sink so
174 * we can send audio to it.
175 */
176
Chris Wilsonea5b2132010-08-04 13:50:23 +0100177 return intel_ddc_get_modes(connector, intel_hdmi->base.ddc_bus);
Eric Anholt7d573822009-01-02 13:33:00 -0800178}
179
180static void intel_hdmi_destroy(struct drm_connector *connector)
181{
Eric Anholt7d573822009-01-02 13:33:00 -0800182 drm_sysfs_connector_remove(connector);
183 drm_connector_cleanup(connector);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800184 kfree(connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800185}
186
187static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
188 .dpms = intel_hdmi_dpms,
189 .mode_fixup = intel_hdmi_mode_fixup,
190 .prepare = intel_encoder_prepare,
191 .mode_set = intel_hdmi_mode_set,
192 .commit = intel_encoder_commit,
193};
194
195static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700196 .dpms = drm_helper_connector_dpms,
Eric Anholt7d573822009-01-02 13:33:00 -0800197 .detect = intel_hdmi_detect,
198 .fill_modes = drm_helper_probe_single_connector_modes,
199 .destroy = intel_hdmi_destroy,
200};
201
202static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
203 .get_modes = intel_hdmi_get_modes,
204 .mode_valid = intel_hdmi_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +0100205 .best_encoder = intel_best_encoder,
Eric Anholt7d573822009-01-02 13:33:00 -0800206};
207
Eric Anholt7d573822009-01-02 13:33:00 -0800208static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100209 .destroy = intel_encoder_destroy,
Eric Anholt7d573822009-01-02 13:33:00 -0800210};
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;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100218 struct intel_hdmi *intel_hdmi;
Eric Anholt7d573822009-01-02 13:33:00 -0800219
Chris Wilsonea5b2132010-08-04 13:50:23 +0100220 intel_hdmi = kzalloc(sizeof(struct intel_hdmi), GFP_KERNEL);
221 if (!intel_hdmi)
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) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100226 kfree(intel_hdmi);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800227 return;
228 }
229
Chris Wilsonea5b2132010-08-04 13:50:23 +0100230 intel_encoder = &intel_hdmi->base;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800231 connector = &intel_connector->base;
Eric Anholt7d573822009-01-02 13:33:00 -0800232 drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
Adam Jackson8d911042009-09-23 15:08:29 -0400233 DRM_MODE_CONNECTOR_HDMIA);
Eric Anholt7d573822009-01-02 13:33:00 -0800234 drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
235
Eric Anholt21d40d32010-03-25 11:11:14 -0700236 intel_encoder->type = INTEL_OUTPUT_HDMI;
Eric Anholt7d573822009-01-02 13:33:00 -0800237
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000238 connector->polled = DRM_CONNECTOR_POLL_HPD;
Eric Anholt7d573822009-01-02 13:33:00 -0800239 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
Chris Wilsonea5b2132010-08-04 13:50:23 +0100271 intel_hdmi->sdvox_reg = sdvox_reg;
Eric Anholt7d573822009-01-02 13:33:00 -0800272
Chris Wilson4ef69c72010-09-09 15:14:28 +0100273 drm_encoder_init(dev, &intel_encoder->base, &intel_hdmi_enc_funcs,
Eric Anholt7d573822009-01-02 13:33:00 -0800274 DRM_MODE_ENCODER_TMDS);
Chris Wilson4ef69c72010-09-09 15:14:28 +0100275 drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs);
Eric Anholt7d573822009-01-02 13:33:00 -0800276
Chris Wilsondf0e9242010-09-09 16:20:55 +0100277 intel_connector_attach_encoder(intel_connector, intel_encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800278 drm_sysfs_connector_add(connector);
279
280 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
281 * 0xd. Failure to do so will result in spurious interrupts being
282 * generated on the port when a cable is not attached.
283 */
284 if (IS_G4X(dev) && !IS_GM45(dev)) {
285 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
286 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
287 }
288
289 return;
290
291err_connector:
292 drm_connector_cleanup(connector);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100293 kfree(intel_hdmi);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800294 kfree(intel_connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800295
296 return;
297}