blob: 197887ed1823fcc4e1023d3fbcc2900488b2796c [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
Adam Jacksonb599c0b2010-07-16 14:46:31 -040057 sdvox = SDVO_ENCODING_HDMI | SDVO_BORDER_ENABLE;
58 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
59 sdvox |= SDVO_VSYNC_ACTIVE_HIGH;
60 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
61 sdvox |= SDVO_HSYNC_ACTIVE_HIGH;
Eric Anholt7d573822009-01-02 13:33:00 -080062
Zhenyu Wang467b2002010-05-12 11:02:14 +080063 if (hdmi_priv->has_hdmi_sink) {
Eric Anholt7d573822009-01-02 13:33:00 -080064 sdvox |= SDVO_AUDIO_ENABLE;
Zhenyu Wang467b2002010-05-12 11:02:14 +080065 if (HAS_PCH_CPT(dev))
66 sdvox |= HDMI_MODE_SELECT;
67 }
Eric Anholt7d573822009-01-02 13:33:00 -080068
Zhenyu Wang0f229062010-04-07 16:15:57 +080069 if (intel_crtc->pipe == 1) {
70 if (HAS_PCH_CPT(dev))
71 sdvox |= PORT_TRANS_B_SEL_CPT;
72 else
73 sdvox |= SDVO_PIPE_B_SELECT;
74 }
Eric Anholt7d573822009-01-02 13:33:00 -080075
76 I915_WRITE(hdmi_priv->sdvox_reg, sdvox);
77 POSTING_READ(hdmi_priv->sdvox_reg);
78}
79
80static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
81{
82 struct drm_device *dev = encoder->dev;
83 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -070084 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
85 struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
Eric Anholt7d573822009-01-02 13:33:00 -080086 u32 temp;
87
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000088 temp = I915_READ(hdmi_priv->sdvox_reg);
89
90 /* HW workaround, need to toggle enable bit off and on for 12bpc, but
91 * we do this anyway which shows more stable in testing.
92 */
Eric Anholtc619eed2010-01-28 16:45:52 -080093 if (HAS_PCH_SPLIT(dev)) {
Eric Anholt7d573822009-01-02 13:33:00 -080094 I915_WRITE(hdmi_priv->sdvox_reg, temp & ~SDVO_ENABLE);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000095 POSTING_READ(hdmi_priv->sdvox_reg);
Eric Anholt7d573822009-01-02 13:33:00 -080096 }
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000097
98 if (mode != DRM_MODE_DPMS_ON) {
99 temp &= ~SDVO_ENABLE;
100 } else {
101 temp |= SDVO_ENABLE;
102 }
103
104 I915_WRITE(hdmi_priv->sdvox_reg, temp);
Eric Anholt7d573822009-01-02 13:33:00 -0800105 POSTING_READ(hdmi_priv->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000106
107 /* HW workaround, need to write this twice for issue that may result
108 * in first write getting masked.
109 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800110 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000111 I915_WRITE(hdmi_priv->sdvox_reg, temp);
112 POSTING_READ(hdmi_priv->sdvox_reg);
113 }
Eric Anholt7d573822009-01-02 13:33:00 -0800114}
115
Eric Anholt7d573822009-01-02 13:33:00 -0800116static int intel_hdmi_mode_valid(struct drm_connector *connector,
117 struct drm_display_mode *mode)
118{
119 if (mode->clock > 165000)
120 return MODE_CLOCK_HIGH;
121 if (mode->clock < 20000)
122 return MODE_CLOCK_HIGH;
123
124 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
125 return MODE_NO_DBLESCAN;
126
127 return MODE_OK;
128}
129
130static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
131 struct drm_display_mode *mode,
132 struct drm_display_mode *adjusted_mode)
133{
134 return true;
135}
136
Keith Packardaa93d632009-05-05 09:52:46 -0700137static enum drm_connector_status
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800138intel_hdmi_detect(struct drm_connector *connector)
Ma Ling9dff6af2009-04-02 13:13:26 +0800139{
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800140 struct drm_encoder *encoder = intel_attached_encoder(connector);
141 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Eric Anholt21d40d32010-03-25 11:11:14 -0700142 struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
Ma Ling9dff6af2009-04-02 13:13:26 +0800143 struct edid *edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700144 enum drm_connector_status status = connector_status_disconnected;
Ma Ling9dff6af2009-04-02 13:13:26 +0800145
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800146 hdmi_priv->has_hdmi_sink = false;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800147 edid = drm_get_edid(connector,
Eric Anholt21d40d32010-03-25 11:11:14 -0700148 intel_encoder->ddc_bus);
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800149
Keith Packardaa93d632009-05-05 09:52:46 -0700150 if (edid) {
Eric Anholtbe9f1c42009-06-21 22:14:55 -0700151 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
Keith Packardaa93d632009-05-05 09:52:46 -0700152 status = connector_status_connected;
153 hdmi_priv->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
154 }
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800155 connector->display_info.raw_edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700156 kfree(edid);
Ma Ling9dff6af2009-04-02 13:13:26 +0800157 }
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800158
Keith Packardaa93d632009-05-05 09:52:46 -0700159 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +0800160}
161
Eric Anholt7d573822009-01-02 13:33:00 -0800162static int intel_hdmi_get_modes(struct drm_connector *connector)
163{
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800164 struct drm_encoder *encoder = intel_attached_encoder(connector);
165 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800166
167 /* We should parse the EDID data and find out if it's an HDMI sink so
168 * we can send audio to it.
169 */
170
Zhenyu Wang335af9a2010-03-30 14:39:31 +0800171 return intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
Eric Anholt7d573822009-01-02 13:33:00 -0800172}
173
174static void intel_hdmi_destroy(struct drm_connector *connector)
175{
Eric Anholt7d573822009-01-02 13:33:00 -0800176 drm_sysfs_connector_remove(connector);
177 drm_connector_cleanup(connector);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800178 kfree(connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800179}
180
181static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
182 .dpms = intel_hdmi_dpms,
183 .mode_fixup = intel_hdmi_mode_fixup,
184 .prepare = intel_encoder_prepare,
185 .mode_set = intel_hdmi_mode_set,
186 .commit = intel_encoder_commit,
187};
188
189static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700190 .dpms = drm_helper_connector_dpms,
Eric Anholt7d573822009-01-02 13:33:00 -0800191 .detect = intel_hdmi_detect,
192 .fill_modes = drm_helper_probe_single_connector_modes,
193 .destroy = intel_hdmi_destroy,
194};
195
196static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
197 .get_modes = intel_hdmi_get_modes,
198 .mode_valid = intel_hdmi_mode_valid,
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800199 .best_encoder = intel_attached_encoder,
Eric Anholt7d573822009-01-02 13:33:00 -0800200};
201
202static void intel_hdmi_enc_destroy(struct drm_encoder *encoder)
203{
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800204 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
205
206 if (intel_encoder->i2c_bus)
207 intel_i2c_destroy(intel_encoder->i2c_bus);
Eric Anholt7d573822009-01-02 13:33:00 -0800208 drm_encoder_cleanup(encoder);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800209 kfree(intel_encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800210}
211
212static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
213 .destroy = intel_hdmi_enc_destroy,
214};
215
Eric Anholt7d573822009-01-02 13:33:00 -0800216void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
217{
218 struct drm_i915_private *dev_priv = dev->dev_private;
219 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -0700220 struct intel_encoder *intel_encoder;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800221 struct intel_connector *intel_connector;
Eric Anholt7d573822009-01-02 13:33:00 -0800222 struct intel_hdmi_priv *hdmi_priv;
223
Eric Anholt21d40d32010-03-25 11:11:14 -0700224 intel_encoder = kcalloc(sizeof(struct intel_encoder) +
Eric Anholt7d573822009-01-02 13:33:00 -0800225 sizeof(struct intel_hdmi_priv), 1, GFP_KERNEL);
Eric Anholt21d40d32010-03-25 11:11:14 -0700226 if (!intel_encoder)
Eric Anholt7d573822009-01-02 13:33:00 -0800227 return;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800228
229 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
230 if (!intel_connector) {
231 kfree(intel_encoder);
232 return;
233 }
234
Eric Anholt21d40d32010-03-25 11:11:14 -0700235 hdmi_priv = (struct intel_hdmi_priv *)(intel_encoder + 1);
Eric Anholt7d573822009-01-02 13:33:00 -0800236
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800237 connector = &intel_connector->base;
Eric Anholt7d573822009-01-02 13:33:00 -0800238 drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
Adam Jackson8d911042009-09-23 15:08:29 -0400239 DRM_MODE_CONNECTOR_HDMIA);
Eric Anholt7d573822009-01-02 13:33:00 -0800240 drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
241
Eric Anholt21d40d32010-03-25 11:11:14 -0700242 intel_encoder->type = INTEL_OUTPUT_HDMI;
Eric Anholt7d573822009-01-02 13:33:00 -0800243
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000244 connector->polled = DRM_CONNECTOR_POLL_HPD;
Eric Anholt7d573822009-01-02 13:33:00 -0800245 connector->interlace_allowed = 0;
246 connector->doublescan_allowed = 0;
Eric Anholt21d40d32010-03-25 11:11:14 -0700247 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Eric Anholt7d573822009-01-02 13:33:00 -0800248
249 /* Set up the DDC bus. */
Ma Lingf8aed702009-08-24 13:50:24 +0800250 if (sdvox_reg == SDVOB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700251 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
252 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOE, "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 == SDVOC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700255 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
256 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOD, "HDMIC");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800257 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800258 } else if (sdvox_reg == HDMIB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700259 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
260 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOE,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800261 "HDMIB");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800262 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800263 } else if (sdvox_reg == HDMIC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700264 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
265 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOD,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800266 "HDMIC");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800267 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800268 } else if (sdvox_reg == HDMID) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700269 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
270 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOF,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800271 "HDMID");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800272 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800273 }
Eric Anholt21d40d32010-03-25 11:11:14 -0700274 if (!intel_encoder->ddc_bus)
Eric Anholt7d573822009-01-02 13:33:00 -0800275 goto err_connector;
276
277 hdmi_priv->sdvox_reg = sdvox_reg;
Eric Anholt21d40d32010-03-25 11:11:14 -0700278 intel_encoder->dev_priv = hdmi_priv;
Eric Anholt7d573822009-01-02 13:33:00 -0800279
Eric Anholt21d40d32010-03-25 11:11:14 -0700280 drm_encoder_init(dev, &intel_encoder->enc, &intel_hdmi_enc_funcs,
Eric Anholt7d573822009-01-02 13:33:00 -0800281 DRM_MODE_ENCODER_TMDS);
Eric Anholt21d40d32010-03-25 11:11:14 -0700282 drm_encoder_helper_add(&intel_encoder->enc, &intel_hdmi_helper_funcs);
Eric Anholt7d573822009-01-02 13:33:00 -0800283
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800284 drm_mode_connector_attach_encoder(&intel_connector->base,
Eric Anholt21d40d32010-03-25 11:11:14 -0700285 &intel_encoder->enc);
Eric Anholt7d573822009-01-02 13:33:00 -0800286 drm_sysfs_connector_add(connector);
287
288 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
289 * 0xd. Failure to do so will result in spurious interrupts being
290 * generated on the port when a cable is not attached.
291 */
292 if (IS_G4X(dev) && !IS_GM45(dev)) {
293 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
294 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
295 }
296
297 return;
298
299err_connector:
300 drm_connector_cleanup(connector);
Eric Anholt21d40d32010-03-25 11:11:14 -0700301 kfree(intel_encoder);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800302 kfree(intel_connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800303
304 return;
305}