blob: 2ea123d8d22b2b9a2a6e59e1cd692c3b3d3bbf9e [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{
48 return container_of(enc_to_intel_encoder(encoder), struct intel_hdmi, base);
49}
50
Eric Anholt7d573822009-01-02 13:33:00 -080051static void intel_hdmi_mode_set(struct drm_encoder *encoder,
52 struct drm_display_mode *mode,
53 struct drm_display_mode *adjusted_mode)
54{
55 struct drm_device *dev = encoder->dev;
56 struct drm_i915_private *dev_priv = dev->dev_private;
57 struct drm_crtc *crtc = encoder->crtc;
58 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonea5b2132010-08-04 13:50:23 +010059 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -080060 u32 sdvox;
61
Adam Jacksonb599c0b2010-07-16 14:46:31 -040062 sdvox = SDVO_ENCODING_HDMI | SDVO_BORDER_ENABLE;
63 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
64 sdvox |= SDVO_VSYNC_ACTIVE_HIGH;
65 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
66 sdvox |= SDVO_HSYNC_ACTIVE_HIGH;
Eric Anholt7d573822009-01-02 13:33:00 -080067
Chris Wilsonea5b2132010-08-04 13:50:23 +010068 if (intel_hdmi->has_hdmi_sink) {
Eric Anholt7d573822009-01-02 13:33:00 -080069 sdvox |= SDVO_AUDIO_ENABLE;
Zhenyu Wang467b2002010-05-12 11:02:14 +080070 if (HAS_PCH_CPT(dev))
71 sdvox |= HDMI_MODE_SELECT;
72 }
Eric Anholt7d573822009-01-02 13:33:00 -080073
Zhenyu Wang0f229062010-04-07 16:15:57 +080074 if (intel_crtc->pipe == 1) {
75 if (HAS_PCH_CPT(dev))
76 sdvox |= PORT_TRANS_B_SEL_CPT;
77 else
78 sdvox |= SDVO_PIPE_B_SELECT;
79 }
Eric Anholt7d573822009-01-02 13:33:00 -080080
Chris Wilsonea5b2132010-08-04 13:50:23 +010081 I915_WRITE(intel_hdmi->sdvox_reg, sdvox);
82 POSTING_READ(intel_hdmi->sdvox_reg);
Eric Anholt7d573822009-01-02 13:33:00 -080083}
84
85static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
86{
87 struct drm_device *dev = encoder->dev;
88 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +010089 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -080090 u32 temp;
91
Chris Wilsonea5b2132010-08-04 13:50:23 +010092 temp = I915_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000093
94 /* HW workaround, need to toggle enable bit off and on for 12bpc, but
95 * we do this anyway which shows more stable in testing.
96 */
Eric Anholtc619eed2010-01-28 16:45:52 -080097 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +010098 I915_WRITE(intel_hdmi->sdvox_reg, temp & ~SDVO_ENABLE);
99 POSTING_READ(intel_hdmi->sdvox_reg);
Eric Anholt7d573822009-01-02 13:33:00 -0800100 }
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000101
102 if (mode != DRM_MODE_DPMS_ON) {
103 temp &= ~SDVO_ENABLE;
104 } else {
105 temp |= SDVO_ENABLE;
106 }
107
Chris Wilsonea5b2132010-08-04 13:50:23 +0100108 I915_WRITE(intel_hdmi->sdvox_reg, temp);
109 POSTING_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000110
111 /* HW workaround, need to write this twice for issue that may result
112 * in first write getting masked.
113 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800114 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100115 I915_WRITE(intel_hdmi->sdvox_reg, temp);
116 POSTING_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000117 }
Eric Anholt7d573822009-01-02 13:33:00 -0800118}
119
Eric Anholt7d573822009-01-02 13:33:00 -0800120static int intel_hdmi_mode_valid(struct drm_connector *connector,
121 struct drm_display_mode *mode)
122{
123 if (mode->clock > 165000)
124 return MODE_CLOCK_HIGH;
125 if (mode->clock < 20000)
126 return MODE_CLOCK_HIGH;
127
128 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
129 return MODE_NO_DBLESCAN;
130
131 return MODE_OK;
132}
133
134static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
135 struct drm_display_mode *mode,
136 struct drm_display_mode *adjusted_mode)
137{
138 return true;
139}
140
Keith Packardaa93d632009-05-05 09:52:46 -0700141static enum drm_connector_status
Chris Wilson7b334fc2010-09-09 23:51:02 +0100142intel_hdmi_detect(struct drm_connector *connector,
143 bool nondestructive)
Ma Ling9dff6af2009-04-02 13:13:26 +0800144{
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800145 struct drm_encoder *encoder = intel_attached_encoder(connector);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100146 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Ma Ling9dff6af2009-04-02 13:13:26 +0800147 struct edid *edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700148 enum drm_connector_status status = connector_status_disconnected;
Ma Ling9dff6af2009-04-02 13:13:26 +0800149
Chris Wilsonea5b2132010-08-04 13:50:23 +0100150 intel_hdmi->has_hdmi_sink = false;
151 edid = drm_get_edid(connector, intel_hdmi->base.ddc_bus);
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800152
Keith Packardaa93d632009-05-05 09:52:46 -0700153 if (edid) {
Eric Anholtbe9f1c42009-06-21 22:14:55 -0700154 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
Keith Packardaa93d632009-05-05 09:52:46 -0700155 status = connector_status_connected;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100156 intel_hdmi->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
Keith Packardaa93d632009-05-05 09:52:46 -0700157 }
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800158 connector->display_info.raw_edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700159 kfree(edid);
Ma Ling9dff6af2009-04-02 13:13:26 +0800160 }
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800161
Keith Packardaa93d632009-05-05 09:52:46 -0700162 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +0800163}
164
Eric Anholt7d573822009-01-02 13:33:00 -0800165static int intel_hdmi_get_modes(struct drm_connector *connector)
166{
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800167 struct drm_encoder *encoder = intel_attached_encoder(connector);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100168 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800169
170 /* We should parse the EDID data and find out if it's an HDMI sink so
171 * we can send audio to it.
172 */
173
Chris Wilsonea5b2132010-08-04 13:50:23 +0100174 return intel_ddc_get_modes(connector, intel_hdmi->base.ddc_bus);
Eric Anholt7d573822009-01-02 13:33:00 -0800175}
176
177static void intel_hdmi_destroy(struct drm_connector *connector)
178{
Eric Anholt7d573822009-01-02 13:33:00 -0800179 drm_sysfs_connector_remove(connector);
180 drm_connector_cleanup(connector);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800181 kfree(connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800182}
183
184static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
185 .dpms = intel_hdmi_dpms,
186 .mode_fixup = intel_hdmi_mode_fixup,
187 .prepare = intel_encoder_prepare,
188 .mode_set = intel_hdmi_mode_set,
189 .commit = intel_encoder_commit,
190};
191
192static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700193 .dpms = drm_helper_connector_dpms,
Eric Anholt7d573822009-01-02 13:33:00 -0800194 .detect = intel_hdmi_detect,
195 .fill_modes = drm_helper_probe_single_connector_modes,
196 .destroy = intel_hdmi_destroy,
197};
198
199static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
200 .get_modes = intel_hdmi_get_modes,
201 .mode_valid = intel_hdmi_mode_valid,
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800202 .best_encoder = intel_attached_encoder,
Eric Anholt7d573822009-01-02 13:33:00 -0800203};
204
Eric Anholt7d573822009-01-02 13:33:00 -0800205static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100206 .destroy = intel_encoder_destroy,
Eric Anholt7d573822009-01-02 13:33:00 -0800207};
208
Eric Anholt7d573822009-01-02 13:33:00 -0800209void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
210{
211 struct drm_i915_private *dev_priv = dev->dev_private;
212 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -0700213 struct intel_encoder *intel_encoder;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800214 struct intel_connector *intel_connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100215 struct intel_hdmi *intel_hdmi;
Eric Anholt7d573822009-01-02 13:33:00 -0800216
Chris Wilsonea5b2132010-08-04 13:50:23 +0100217 intel_hdmi = kzalloc(sizeof(struct intel_hdmi), GFP_KERNEL);
218 if (!intel_hdmi)
Eric Anholt7d573822009-01-02 13:33:00 -0800219 return;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800220
221 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
222 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100223 kfree(intel_hdmi);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800224 return;
225 }
226
Chris Wilsonea5b2132010-08-04 13:50:23 +0100227 intel_encoder = &intel_hdmi->base;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800228 connector = &intel_connector->base;
Eric Anholt7d573822009-01-02 13:33:00 -0800229 drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
Adam Jackson8d911042009-09-23 15:08:29 -0400230 DRM_MODE_CONNECTOR_HDMIA);
Eric Anholt7d573822009-01-02 13:33:00 -0800231 drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
232
Eric Anholt21d40d32010-03-25 11:11:14 -0700233 intel_encoder->type = INTEL_OUTPUT_HDMI;
Eric Anholt7d573822009-01-02 13:33:00 -0800234
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000235 connector->polled = DRM_CONNECTOR_POLL_HPD;
Eric Anholt7d573822009-01-02 13:33:00 -0800236 connector->interlace_allowed = 0;
237 connector->doublescan_allowed = 0;
Eric Anholt21d40d32010-03-25 11:11:14 -0700238 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Eric Anholt7d573822009-01-02 13:33:00 -0800239
240 /* Set up the DDC bus. */
Ma Lingf8aed702009-08-24 13:50:24 +0800241 if (sdvox_reg == SDVOB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700242 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
243 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOE, "HDMIB");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800244 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800245 } else if (sdvox_reg == SDVOC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700246 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
247 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOD, "HDMIC");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800248 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800249 } else if (sdvox_reg == HDMIB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700250 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
251 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOE,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800252 "HDMIB");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800253 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800254 } else if (sdvox_reg == HDMIC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700255 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
256 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOD,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800257 "HDMIC");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800258 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800259 } else if (sdvox_reg == HDMID) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700260 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
261 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOF,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800262 "HDMID");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800263 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800264 }
Eric Anholt21d40d32010-03-25 11:11:14 -0700265 if (!intel_encoder->ddc_bus)
Eric Anholt7d573822009-01-02 13:33:00 -0800266 goto err_connector;
267
Chris Wilsonea5b2132010-08-04 13:50:23 +0100268 intel_hdmi->sdvox_reg = sdvox_reg;
Eric Anholt7d573822009-01-02 13:33:00 -0800269
Eric Anholt21d40d32010-03-25 11:11:14 -0700270 drm_encoder_init(dev, &intel_encoder->enc, &intel_hdmi_enc_funcs,
Eric Anholt7d573822009-01-02 13:33:00 -0800271 DRM_MODE_ENCODER_TMDS);
Eric Anholt21d40d32010-03-25 11:11:14 -0700272 drm_encoder_helper_add(&intel_encoder->enc, &intel_hdmi_helper_funcs);
Eric Anholt7d573822009-01-02 13:33:00 -0800273
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800274 drm_mode_connector_attach_encoder(&intel_connector->base,
Eric Anholt21d40d32010-03-25 11:11:14 -0700275 &intel_encoder->enc);
Eric Anholt7d573822009-01-02 13:33:00 -0800276 drm_sysfs_connector_add(connector);
277
278 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
279 * 0xd. Failure to do so will result in spurious interrupts being
280 * generated on the port when a cable is not attached.
281 */
282 if (IS_G4X(dev) && !IS_GM45(dev)) {
283 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
284 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
285 }
286
287 return;
288
289err_connector:
290 drm_connector_cleanup(connector);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100291 kfree(intel_hdmi);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800292 kfree(intel_connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800293
294 return;
295}