blob: 74823e77b2fe524f2618a9ef5c55ed6149d037f9 [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;
41 u32 save_SDVOX;
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
65 if (intel_crtc->pipe == 1)
66 sdvox |= SDVO_PIPE_B_SELECT;
67
68 I915_WRITE(hdmi_priv->sdvox_reg, sdvox);
69 POSTING_READ(hdmi_priv->sdvox_reg);
70}
71
72static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
73{
74 struct drm_device *dev = encoder->dev;
75 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -070076 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
77 struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
Eric Anholt7d573822009-01-02 13:33:00 -080078 u32 temp;
79
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000080 temp = I915_READ(hdmi_priv->sdvox_reg);
81
82 /* HW workaround, need to toggle enable bit off and on for 12bpc, but
83 * we do this anyway which shows more stable in testing.
84 */
Eric Anholtc619eed2010-01-28 16:45:52 -080085 if (HAS_PCH_SPLIT(dev)) {
Eric Anholt7d573822009-01-02 13:33:00 -080086 I915_WRITE(hdmi_priv->sdvox_reg, temp & ~SDVO_ENABLE);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000087 POSTING_READ(hdmi_priv->sdvox_reg);
Eric Anholt7d573822009-01-02 13:33:00 -080088 }
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000089
90 if (mode != DRM_MODE_DPMS_ON) {
91 temp &= ~SDVO_ENABLE;
92 } else {
93 temp |= SDVO_ENABLE;
94 }
95
96 I915_WRITE(hdmi_priv->sdvox_reg, temp);
Eric Anholt7d573822009-01-02 13:33:00 -080097 POSTING_READ(hdmi_priv->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000098
99 /* HW workaround, need to write this twice for issue that may result
100 * in first write getting masked.
101 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800102 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000103 I915_WRITE(hdmi_priv->sdvox_reg, temp);
104 POSTING_READ(hdmi_priv->sdvox_reg);
105 }
Eric Anholt7d573822009-01-02 13:33:00 -0800106}
107
108static void intel_hdmi_save(struct drm_connector *connector)
109{
110 struct drm_device *dev = connector->dev;
111 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -0700112 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
113 struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
Eric Anholt7d573822009-01-02 13:33:00 -0800114
115 hdmi_priv->save_SDVOX = I915_READ(hdmi_priv->sdvox_reg);
116}
117
118static void intel_hdmi_restore(struct drm_connector *connector)
119{
120 struct drm_device *dev = connector->dev;
121 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -0700122 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
123 struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
Eric Anholt7d573822009-01-02 13:33:00 -0800124
125 I915_WRITE(hdmi_priv->sdvox_reg, hdmi_priv->save_SDVOX);
126 POSTING_READ(hdmi_priv->sdvox_reg);
127}
128
129static int intel_hdmi_mode_valid(struct drm_connector *connector,
130 struct drm_display_mode *mode)
131{
132 if (mode->clock > 165000)
133 return MODE_CLOCK_HIGH;
134 if (mode->clock < 20000)
135 return MODE_CLOCK_HIGH;
136
137 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
138 return MODE_NO_DBLESCAN;
139
140 return MODE_OK;
141}
142
143static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
144 struct drm_display_mode *mode,
145 struct drm_display_mode *adjusted_mode)
146{
147 return true;
148}
149
Keith Packardaa93d632009-05-05 09:52:46 -0700150static enum drm_connector_status
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800151intel_hdmi_detect(struct drm_connector *connector)
Ma Ling9dff6af2009-04-02 13:13:26 +0800152{
Eric Anholt21d40d32010-03-25 11:11:14 -0700153 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
154 struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
Ma Ling9dff6af2009-04-02 13:13:26 +0800155 struct edid *edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700156 enum drm_connector_status status = connector_status_disconnected;
Ma Ling9dff6af2009-04-02 13:13:26 +0800157
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800158 hdmi_priv->has_hdmi_sink = false;
Eric Anholt21d40d32010-03-25 11:11:14 -0700159 edid = drm_get_edid(&intel_encoder->base,
160 intel_encoder->ddc_bus);
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800161
Keith Packardaa93d632009-05-05 09:52:46 -0700162 if (edid) {
Eric Anholtbe9f1c42009-06-21 22:14:55 -0700163 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
Keith Packardaa93d632009-05-05 09:52:46 -0700164 status = connector_status_connected;
165 hdmi_priv->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
166 }
Eric Anholt21d40d32010-03-25 11:11:14 -0700167 intel_encoder->base.display_info.raw_edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700168 kfree(edid);
Ma Ling9dff6af2009-04-02 13:13:26 +0800169 }
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800170
Keith Packardaa93d632009-05-05 09:52:46 -0700171 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +0800172}
173
Eric Anholt7d573822009-01-02 13:33:00 -0800174static int intel_hdmi_get_modes(struct drm_connector *connector)
175{
Eric Anholt21d40d32010-03-25 11:11:14 -0700176 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800177
178 /* We should parse the EDID data and find out if it's an HDMI sink so
179 * we can send audio to it.
180 */
181
Zhenyu Wang335af9a2010-03-30 14:39:31 +0800182 return intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
Eric Anholt7d573822009-01-02 13:33:00 -0800183}
184
185static void intel_hdmi_destroy(struct drm_connector *connector)
186{
Eric Anholt21d40d32010-03-25 11:11:14 -0700187 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800188
Eric Anholt21d40d32010-03-25 11:11:14 -0700189 if (intel_encoder->i2c_bus)
190 intel_i2c_destroy(intel_encoder->i2c_bus);
Eric Anholt7d573822009-01-02 13:33:00 -0800191 drm_sysfs_connector_remove(connector);
192 drm_connector_cleanup(connector);
Eric Anholt21d40d32010-03-25 11:11:14 -0700193 kfree(intel_encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800194}
195
196static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
197 .dpms = intel_hdmi_dpms,
198 .mode_fixup = intel_hdmi_mode_fixup,
199 .prepare = intel_encoder_prepare,
200 .mode_set = intel_hdmi_mode_set,
201 .commit = intel_encoder_commit,
202};
203
204static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700205 .dpms = drm_helper_connector_dpms,
Eric Anholt7d573822009-01-02 13:33:00 -0800206 .save = intel_hdmi_save,
207 .restore = intel_hdmi_restore,
208 .detect = intel_hdmi_detect,
209 .fill_modes = drm_helper_probe_single_connector_modes,
210 .destroy = intel_hdmi_destroy,
211};
212
213static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
214 .get_modes = intel_hdmi_get_modes,
215 .mode_valid = intel_hdmi_mode_valid,
216 .best_encoder = intel_best_encoder,
217};
218
219static void intel_hdmi_enc_destroy(struct drm_encoder *encoder)
220{
221 drm_encoder_cleanup(encoder);
222}
223
224static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
225 .destroy = intel_hdmi_enc_destroy,
226};
227
Eric Anholt7d573822009-01-02 13:33:00 -0800228void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
229{
230 struct drm_i915_private *dev_priv = dev->dev_private;
231 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -0700232 struct intel_encoder *intel_encoder;
Eric Anholt7d573822009-01-02 13:33:00 -0800233 struct intel_hdmi_priv *hdmi_priv;
234
Eric Anholt21d40d32010-03-25 11:11:14 -0700235 intel_encoder = kcalloc(sizeof(struct intel_encoder) +
Eric Anholt7d573822009-01-02 13:33:00 -0800236 sizeof(struct intel_hdmi_priv), 1, GFP_KERNEL);
Eric Anholt21d40d32010-03-25 11:11:14 -0700237 if (!intel_encoder)
Eric Anholt7d573822009-01-02 13:33:00 -0800238 return;
Eric Anholt21d40d32010-03-25 11:11:14 -0700239 hdmi_priv = (struct intel_hdmi_priv *)(intel_encoder + 1);
Eric Anholt7d573822009-01-02 13:33:00 -0800240
Eric Anholt21d40d32010-03-25 11:11:14 -0700241 connector = &intel_encoder->base;
Eric Anholt7d573822009-01-02 13:33:00 -0800242 drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
Adam Jackson8d911042009-09-23 15:08:29 -0400243 DRM_MODE_CONNECTOR_HDMIA);
Eric Anholt7d573822009-01-02 13:33:00 -0800244 drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
245
Eric Anholt21d40d32010-03-25 11:11:14 -0700246 intel_encoder->type = INTEL_OUTPUT_HDMI;
Eric Anholt7d573822009-01-02 13:33:00 -0800247
248 connector->interlace_allowed = 0;
249 connector->doublescan_allowed = 0;
Eric Anholt21d40d32010-03-25 11:11:14 -0700250 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Eric Anholt7d573822009-01-02 13:33:00 -0800251
252 /* Set up the DDC bus. */
Ma Lingf8aed702009-08-24 13:50:24 +0800253 if (sdvox_reg == SDVOB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700254 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
255 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOE, "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 == SDVOC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700258 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
259 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOD, "HDMIC");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800260 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800261 } else if (sdvox_reg == HDMIB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700262 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
263 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOE,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800264 "HDMIB");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800265 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800266 } else if (sdvox_reg == HDMIC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700267 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
268 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOD,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800269 "HDMIC");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800270 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800271 } else if (sdvox_reg == HDMID) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700272 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
273 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOF,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800274 "HDMID");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800275 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800276 }
Eric Anholt21d40d32010-03-25 11:11:14 -0700277 if (!intel_encoder->ddc_bus)
Eric Anholt7d573822009-01-02 13:33:00 -0800278 goto err_connector;
279
280 hdmi_priv->sdvox_reg = sdvox_reg;
Eric Anholt21d40d32010-03-25 11:11:14 -0700281 intel_encoder->dev_priv = hdmi_priv;
Eric Anholt7d573822009-01-02 13:33:00 -0800282
Eric Anholt21d40d32010-03-25 11:11:14 -0700283 drm_encoder_init(dev, &intel_encoder->enc, &intel_hdmi_enc_funcs,
Eric Anholt7d573822009-01-02 13:33:00 -0800284 DRM_MODE_ENCODER_TMDS);
Eric Anholt21d40d32010-03-25 11:11:14 -0700285 drm_encoder_helper_add(&intel_encoder->enc, &intel_hdmi_helper_funcs);
Eric Anholt7d573822009-01-02 13:33:00 -0800286
Eric Anholt21d40d32010-03-25 11:11:14 -0700287 drm_mode_connector_attach_encoder(&intel_encoder->base,
288 &intel_encoder->enc);
Eric Anholt7d573822009-01-02 13:33:00 -0800289 drm_sysfs_connector_add(connector);
290
291 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
292 * 0xd. Failure to do so will result in spurious interrupts being
293 * generated on the port when a cable is not attached.
294 */
295 if (IS_G4X(dev) && !IS_GM45(dev)) {
296 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
297 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
298 }
299
300 return;
301
302err_connector:
303 drm_connector_cleanup(connector);
Eric Anholt21d40d32010-03-25 11:11:14 -0700304 kfree(intel_encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800305
306 return;
307}