blob: 83bd764b000e51cff80ac0a79a14b8cc5f05fcb4 [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
Zhenyu Wang467b2002010-05-12 11:02:14 +080062 if (hdmi_priv->has_hdmi_sink) {
Eric Anholt7d573822009-01-02 13:33:00 -080063 sdvox |= SDVO_AUDIO_ENABLE;
Zhenyu Wang467b2002010-05-12 11:02:14 +080064 if (HAS_PCH_CPT(dev))
65 sdvox |= HDMI_MODE_SELECT;
66 }
Eric Anholt7d573822009-01-02 13:33:00 -080067
Zhenyu Wang0f229062010-04-07 16:15:57 +080068 if (intel_crtc->pipe == 1) {
69 if (HAS_PCH_CPT(dev))
70 sdvox |= PORT_TRANS_B_SEL_CPT;
71 else
72 sdvox |= SDVO_PIPE_B_SELECT;
73 }
Eric Anholt7d573822009-01-02 13:33:00 -080074
75 I915_WRITE(hdmi_priv->sdvox_reg, sdvox);
76 POSTING_READ(hdmi_priv->sdvox_reg);
77}
78
79static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
80{
81 struct drm_device *dev = encoder->dev;
82 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -070083 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
84 struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
Eric Anholt7d573822009-01-02 13:33:00 -080085 u32 temp;
86
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000087 temp = I915_READ(hdmi_priv->sdvox_reg);
88
89 /* HW workaround, need to toggle enable bit off and on for 12bpc, but
90 * we do this anyway which shows more stable in testing.
91 */
Eric Anholtc619eed2010-01-28 16:45:52 -080092 if (HAS_PCH_SPLIT(dev)) {
Eric Anholt7d573822009-01-02 13:33:00 -080093 I915_WRITE(hdmi_priv->sdvox_reg, temp & ~SDVO_ENABLE);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000094 POSTING_READ(hdmi_priv->sdvox_reg);
Eric Anholt7d573822009-01-02 13:33:00 -080095 }
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +000096
97 if (mode != DRM_MODE_DPMS_ON) {
98 temp &= ~SDVO_ENABLE;
99 } else {
100 temp |= SDVO_ENABLE;
101 }
102
103 I915_WRITE(hdmi_priv->sdvox_reg, temp);
Eric Anholt7d573822009-01-02 13:33:00 -0800104 POSTING_READ(hdmi_priv->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000105
106 /* HW workaround, need to write this twice for issue that may result
107 * in first write getting masked.
108 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800109 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000110 I915_WRITE(hdmi_priv->sdvox_reg, temp);
111 POSTING_READ(hdmi_priv->sdvox_reg);
112 }
Eric Anholt7d573822009-01-02 13:33:00 -0800113}
114
Eric Anholt7d573822009-01-02 13:33:00 -0800115static int intel_hdmi_mode_valid(struct drm_connector *connector,
116 struct drm_display_mode *mode)
117{
118 if (mode->clock > 165000)
119 return MODE_CLOCK_HIGH;
120 if (mode->clock < 20000)
121 return MODE_CLOCK_HIGH;
122
123 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
124 return MODE_NO_DBLESCAN;
125
126 return MODE_OK;
127}
128
129static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
130 struct drm_display_mode *mode,
131 struct drm_display_mode *adjusted_mode)
132{
133 return true;
134}
135
Keith Packardaa93d632009-05-05 09:52:46 -0700136static enum drm_connector_status
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800137intel_hdmi_detect(struct drm_connector *connector)
Ma Ling9dff6af2009-04-02 13:13:26 +0800138{
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800139 struct drm_encoder *encoder = intel_attached_encoder(connector);
140 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Eric Anholt21d40d32010-03-25 11:11:14 -0700141 struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
Ma Ling9dff6af2009-04-02 13:13:26 +0800142 struct edid *edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700143 enum drm_connector_status status = connector_status_disconnected;
Ma Ling9dff6af2009-04-02 13:13:26 +0800144
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800145 hdmi_priv->has_hdmi_sink = false;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800146 edid = drm_get_edid(connector,
Eric Anholt21d40d32010-03-25 11:11:14 -0700147 intel_encoder->ddc_bus);
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800148
Keith Packardaa93d632009-05-05 09:52:46 -0700149 if (edid) {
Eric Anholtbe9f1c42009-06-21 22:14:55 -0700150 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
Keith Packardaa93d632009-05-05 09:52:46 -0700151 status = connector_status_connected;
152 hdmi_priv->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
153 }
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800154 connector->display_info.raw_edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700155 kfree(edid);
Ma Ling9dff6af2009-04-02 13:13:26 +0800156 }
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800157
Keith Packardaa93d632009-05-05 09:52:46 -0700158 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +0800159}
160
Eric Anholt7d573822009-01-02 13:33:00 -0800161static int intel_hdmi_get_modes(struct drm_connector *connector)
162{
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800163 struct drm_encoder *encoder = intel_attached_encoder(connector);
164 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800165
166 /* We should parse the EDID data and find out if it's an HDMI sink so
167 * we can send audio to it.
168 */
169
Zhenyu Wang335af9a2010-03-30 14:39:31 +0800170 return intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
Eric Anholt7d573822009-01-02 13:33:00 -0800171}
172
173static void intel_hdmi_destroy(struct drm_connector *connector)
174{
Eric Anholt7d573822009-01-02 13:33:00 -0800175 drm_sysfs_connector_remove(connector);
176 drm_connector_cleanup(connector);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800177 kfree(connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800178}
179
180static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
181 .dpms = intel_hdmi_dpms,
182 .mode_fixup = intel_hdmi_mode_fixup,
183 .prepare = intel_encoder_prepare,
184 .mode_set = intel_hdmi_mode_set,
185 .commit = intel_encoder_commit,
186};
187
188static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700189 .dpms = drm_helper_connector_dpms,
Eric Anholt7d573822009-01-02 13:33:00 -0800190 .detect = intel_hdmi_detect,
191 .fill_modes = drm_helper_probe_single_connector_modes,
192 .destroy = intel_hdmi_destroy,
193};
194
195static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
196 .get_modes = intel_hdmi_get_modes,
197 .mode_valid = intel_hdmi_mode_valid,
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800198 .best_encoder = intel_attached_encoder,
Eric Anholt7d573822009-01-02 13:33:00 -0800199};
200
201static void intel_hdmi_enc_destroy(struct drm_encoder *encoder)
202{
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800203 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
204
205 if (intel_encoder->i2c_bus)
206 intel_i2c_destroy(intel_encoder->i2c_bus);
Eric Anholt7d573822009-01-02 13:33:00 -0800207 drm_encoder_cleanup(encoder);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800208 kfree(intel_encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800209}
210
211static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
212 .destroy = intel_hdmi_enc_destroy,
213};
214
Eric Anholt7d573822009-01-02 13:33:00 -0800215void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
216{
217 struct drm_i915_private *dev_priv = dev->dev_private;
218 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -0700219 struct intel_encoder *intel_encoder;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800220 struct intel_connector *intel_connector;
Eric Anholt7d573822009-01-02 13:33:00 -0800221 struct intel_hdmi_priv *hdmi_priv;
222
Eric Anholt21d40d32010-03-25 11:11:14 -0700223 intel_encoder = kcalloc(sizeof(struct intel_encoder) +
Eric Anholt7d573822009-01-02 13:33:00 -0800224 sizeof(struct intel_hdmi_priv), 1, GFP_KERNEL);
Eric Anholt21d40d32010-03-25 11:11:14 -0700225 if (!intel_encoder)
Eric Anholt7d573822009-01-02 13:33:00 -0800226 return;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800227
228 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
229 if (!intel_connector) {
230 kfree(intel_encoder);
231 return;
232 }
233
Eric Anholt21d40d32010-03-25 11:11:14 -0700234 hdmi_priv = (struct intel_hdmi_priv *)(intel_encoder + 1);
Eric Anholt7d573822009-01-02 13:33:00 -0800235
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800236 connector = &intel_connector->base;
Eric Anholt7d573822009-01-02 13:33:00 -0800237 drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
Adam Jackson8d911042009-09-23 15:08:29 -0400238 DRM_MODE_CONNECTOR_HDMIA);
Eric Anholt7d573822009-01-02 13:33:00 -0800239 drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
240
Eric Anholt21d40d32010-03-25 11:11:14 -0700241 intel_encoder->type = INTEL_OUTPUT_HDMI;
Eric Anholt7d573822009-01-02 13:33:00 -0800242
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000243 connector->polled = DRM_CONNECTOR_POLL_HPD;
Eric Anholt7d573822009-01-02 13:33:00 -0800244 connector->interlace_allowed = 0;
245 connector->doublescan_allowed = 0;
Eric Anholt21d40d32010-03-25 11:11:14 -0700246 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Eric Anholt7d573822009-01-02 13:33:00 -0800247
248 /* Set up the DDC bus. */
Ma Lingf8aed702009-08-24 13:50:24 +0800249 if (sdvox_reg == SDVOB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700250 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
251 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOE, "HDMIB");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800252 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800253 } else if (sdvox_reg == SDVOC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700254 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
255 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOD, "HDMIC");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800256 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800257 } else if (sdvox_reg == HDMIB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700258 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
259 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOE,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800260 "HDMIB");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800261 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800262 } else if (sdvox_reg == HDMIC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700263 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
264 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOD,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800265 "HDMIC");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800266 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800267 } else if (sdvox_reg == HDMID) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700268 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
269 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOF,
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800270 "HDMID");
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800271 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800272 }
Eric Anholt21d40d32010-03-25 11:11:14 -0700273 if (!intel_encoder->ddc_bus)
Eric Anholt7d573822009-01-02 13:33:00 -0800274 goto err_connector;
275
276 hdmi_priv->sdvox_reg = sdvox_reg;
Eric Anholt21d40d32010-03-25 11:11:14 -0700277 intel_encoder->dev_priv = hdmi_priv;
Eric Anholt7d573822009-01-02 13:33:00 -0800278
Eric Anholt21d40d32010-03-25 11:11:14 -0700279 drm_encoder_init(dev, &intel_encoder->enc, &intel_hdmi_enc_funcs,
Eric Anholt7d573822009-01-02 13:33:00 -0800280 DRM_MODE_ENCODER_TMDS);
Eric Anholt21d40d32010-03-25 11:11:14 -0700281 drm_encoder_helper_add(&intel_encoder->enc, &intel_hdmi_helper_funcs);
Eric Anholt7d573822009-01-02 13:33:00 -0800282
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800283 drm_mode_connector_attach_encoder(&intel_connector->base,
Eric Anholt21d40d32010-03-25 11:11:14 -0700284 &intel_encoder->enc);
Eric Anholt7d573822009-01-02 13:33:00 -0800285 drm_sysfs_connector_add(connector);
286
287 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
288 * 0xd. Failure to do so will result in spurious interrupts being
289 * generated on the port when a cable is not attached.
290 */
291 if (IS_G4X(dev) && !IS_GM45(dev)) {
292 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
293 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
294 }
295
296 return;
297
298err_connector:
299 drm_connector_cleanup(connector);
Eric Anholt21d40d32010-03-25 11:11:14 -0700300 kfree(intel_encoder);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800301 kfree(intel_connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800302
303 return;
304}