blob: 6c3b2ecd59d5d85fd6d14d186d4bc499198c6135 [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;
Chris Wilsonf899fc62010-07-20 15:44:45 -070043 int ddc_bus;
Ma Ling9dff6af2009-04-02 13:13:26 +080044 bool has_hdmi_sink;
Zhenyu Wang2e3d6002010-09-10 10:39:40 +080045 bool has_audio;
Chris Wilson55b7d6e82010-09-19 09:29:33 +010046 int force_audio;
47 struct drm_property *force_audio_property;
Eric Anholt7d573822009-01-02 13:33:00 -080048};
49
Chris Wilsonea5b2132010-08-04 13:50:23 +010050static struct intel_hdmi *enc_to_intel_hdmi(struct drm_encoder *encoder)
51{
Chris Wilson4ef69c72010-09-09 15:14:28 +010052 return container_of(encoder, struct intel_hdmi, base.base);
Chris Wilsonea5b2132010-08-04 13:50:23 +010053}
54
Chris Wilsondf0e9242010-09-09 16:20:55 +010055static struct intel_hdmi *intel_attached_hdmi(struct drm_connector *connector)
56{
57 return container_of(intel_attached_encoder(connector),
58 struct intel_hdmi, base);
59}
60
Eric Anholt7d573822009-01-02 13:33:00 -080061static void intel_hdmi_mode_set(struct drm_encoder *encoder,
62 struct drm_display_mode *mode,
63 struct drm_display_mode *adjusted_mode)
64{
65 struct drm_device *dev = encoder->dev;
66 struct drm_i915_private *dev_priv = dev->dev_private;
67 struct drm_crtc *crtc = encoder->crtc;
68 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonea5b2132010-08-04 13:50:23 +010069 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -080070 u32 sdvox;
71
Adam Jacksonb599c0b2010-07-16 14:46:31 -040072 sdvox = SDVO_ENCODING_HDMI | SDVO_BORDER_ENABLE;
73 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
74 sdvox |= SDVO_VSYNC_ACTIVE_HIGH;
75 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
76 sdvox |= SDVO_HSYNC_ACTIVE_HIGH;
Eric Anholt7d573822009-01-02 13:33:00 -080077
Zhenyu Wang2e3d6002010-09-10 10:39:40 +080078 /* Required on CPT */
79 if (intel_hdmi->has_hdmi_sink && HAS_PCH_CPT(dev))
80 sdvox |= HDMI_MODE_SELECT;
81
82 if (intel_hdmi->has_audio)
Eric Anholt7d573822009-01-02 13:33:00 -080083 sdvox |= SDVO_AUDIO_ENABLE;
84
Zhenyu Wang0f229062010-04-07 16:15:57 +080085 if (intel_crtc->pipe == 1) {
86 if (HAS_PCH_CPT(dev))
87 sdvox |= PORT_TRANS_B_SEL_CPT;
88 else
89 sdvox |= SDVO_PIPE_B_SELECT;
90 }
Eric Anholt7d573822009-01-02 13:33:00 -080091
Chris Wilsonea5b2132010-08-04 13:50:23 +010092 I915_WRITE(intel_hdmi->sdvox_reg, sdvox);
93 POSTING_READ(intel_hdmi->sdvox_reg);
Eric Anholt7d573822009-01-02 13:33:00 -080094}
95
96static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
97{
98 struct drm_device *dev = encoder->dev;
99 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100100 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800101 u32 temp;
102
Chris Wilsonea5b2132010-08-04 13:50:23 +0100103 temp = I915_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000104
105 /* HW workaround, need to toggle enable bit off and on for 12bpc, but
106 * we do this anyway which shows more stable in testing.
107 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800108 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100109 I915_WRITE(intel_hdmi->sdvox_reg, temp & ~SDVO_ENABLE);
110 POSTING_READ(intel_hdmi->sdvox_reg);
Eric Anholt7d573822009-01-02 13:33:00 -0800111 }
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000112
113 if (mode != DRM_MODE_DPMS_ON) {
114 temp &= ~SDVO_ENABLE;
115 } else {
116 temp |= SDVO_ENABLE;
117 }
118
Chris Wilsonea5b2132010-08-04 13:50:23 +0100119 I915_WRITE(intel_hdmi->sdvox_reg, temp);
120 POSTING_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000121
122 /* HW workaround, need to write this twice for issue that may result
123 * in first write getting masked.
124 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800125 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100126 I915_WRITE(intel_hdmi->sdvox_reg, temp);
127 POSTING_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000128 }
Eric Anholt7d573822009-01-02 13:33:00 -0800129}
130
Eric Anholt7d573822009-01-02 13:33:00 -0800131static int intel_hdmi_mode_valid(struct drm_connector *connector,
132 struct drm_display_mode *mode)
133{
134 if (mode->clock > 165000)
135 return MODE_CLOCK_HIGH;
136 if (mode->clock < 20000)
137 return MODE_CLOCK_HIGH;
138
139 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
140 return MODE_NO_DBLESCAN;
141
142 return MODE_OK;
143}
144
145static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
146 struct drm_display_mode *mode,
147 struct drm_display_mode *adjusted_mode)
148{
149 return true;
150}
151
Keith Packardaa93d632009-05-05 09:52:46 -0700152static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100153intel_hdmi_detect(struct drm_connector *connector, bool force)
Ma Ling9dff6af2009-04-02 13:13:26 +0800154{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100155 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700156 struct drm_i915_private *dev_priv = connector->dev->dev_private;
157 struct edid *edid;
Keith Packardaa93d632009-05-05 09:52:46 -0700158 enum drm_connector_status status = connector_status_disconnected;
Ma Ling9dff6af2009-04-02 13:13:26 +0800159
Chris Wilsonea5b2132010-08-04 13:50:23 +0100160 intel_hdmi->has_hdmi_sink = false;
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800161 intel_hdmi->has_audio = false;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700162 edid = drm_get_edid(connector,
163 &dev_priv->gmbus[intel_hdmi->ddc_bus].adapter);
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800164
Keith Packardaa93d632009-05-05 09:52:46 -0700165 if (edid) {
Eric Anholtbe9f1c42009-06-21 22:14:55 -0700166 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
Keith Packardaa93d632009-05-05 09:52:46 -0700167 status = connector_status_connected;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100168 intel_hdmi->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800169 intel_hdmi->has_audio = drm_detect_monitor_audio(edid);
Keith Packardaa93d632009-05-05 09:52:46 -0700170 }
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800171 connector->display_info.raw_edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700172 kfree(edid);
Ma Ling9dff6af2009-04-02 13:13:26 +0800173 }
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800174
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100175 if (status == connector_status_connected) {
176 if (intel_hdmi->force_audio)
177 intel_hdmi->has_audio = intel_hdmi->force_audio > 0;
178 }
179
Keith Packardaa93d632009-05-05 09:52:46 -0700180 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +0800181}
182
Eric Anholt7d573822009-01-02 13:33:00 -0800183static int intel_hdmi_get_modes(struct drm_connector *connector)
184{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100185 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700186 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Eric Anholt7d573822009-01-02 13:33:00 -0800187
188 /* We should parse the EDID data and find out if it's an HDMI sink so
189 * we can send audio to it.
190 */
191
Chris Wilsonf899fc62010-07-20 15:44:45 -0700192 return intel_ddc_get_modes(connector,
193 &dev_priv->gmbus[intel_hdmi->ddc_bus].adapter);
Eric Anholt7d573822009-01-02 13:33:00 -0800194}
195
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100196static int
197intel_hdmi_set_property(struct drm_connector *connector,
198 struct drm_property *property,
199 uint64_t val)
200{
201 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
202 int ret;
203
204 ret = drm_connector_property_set_value(connector, property, val);
205 if (ret)
206 return ret;
207
208 if (property == intel_hdmi->force_audio_property) {
209 if (val == intel_hdmi->force_audio)
210 return 0;
211
212 intel_hdmi->force_audio = val;
213
214 if (val > 0 && intel_hdmi->has_audio)
215 return 0;
216 if (val < 0 && !intel_hdmi->has_audio)
217 return 0;
218
219 intel_hdmi->has_audio = val > 0;
220 goto done;
221 }
222
223 return -EINVAL;
224
225done:
226 if (intel_hdmi->base.base.crtc) {
227 struct drm_crtc *crtc = intel_hdmi->base.base.crtc;
228 drm_crtc_helper_set_mode(crtc, &crtc->mode,
229 crtc->x, crtc->y,
230 crtc->fb);
231 }
232
233 return 0;
234}
235
Eric Anholt7d573822009-01-02 13:33:00 -0800236static void intel_hdmi_destroy(struct drm_connector *connector)
237{
Eric Anholt7d573822009-01-02 13:33:00 -0800238 drm_sysfs_connector_remove(connector);
239 drm_connector_cleanup(connector);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800240 kfree(connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800241}
242
243static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
244 .dpms = intel_hdmi_dpms,
245 .mode_fixup = intel_hdmi_mode_fixup,
246 .prepare = intel_encoder_prepare,
247 .mode_set = intel_hdmi_mode_set,
248 .commit = intel_encoder_commit,
249};
250
251static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700252 .dpms = drm_helper_connector_dpms,
Eric Anholt7d573822009-01-02 13:33:00 -0800253 .detect = intel_hdmi_detect,
254 .fill_modes = drm_helper_probe_single_connector_modes,
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100255 .set_property = intel_hdmi_set_property,
Eric Anholt7d573822009-01-02 13:33:00 -0800256 .destroy = intel_hdmi_destroy,
257};
258
259static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
260 .get_modes = intel_hdmi_get_modes,
261 .mode_valid = intel_hdmi_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +0100262 .best_encoder = intel_best_encoder,
Eric Anholt7d573822009-01-02 13:33:00 -0800263};
264
Eric Anholt7d573822009-01-02 13:33:00 -0800265static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100266 .destroy = intel_encoder_destroy,
Eric Anholt7d573822009-01-02 13:33:00 -0800267};
268
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100269static void
270intel_hdmi_add_properties(struct intel_hdmi *intel_hdmi, struct drm_connector *connector)
271{
272 struct drm_device *dev = connector->dev;
273
274 intel_hdmi->force_audio_property =
275 drm_property_create(dev, DRM_MODE_PROP_RANGE, "force_audio", 2);
276 if (intel_hdmi->force_audio_property) {
277 intel_hdmi->force_audio_property->values[0] = -1;
278 intel_hdmi->force_audio_property->values[1] = 1;
279 drm_connector_attach_property(connector, intel_hdmi->force_audio_property, 0);
280 }
281}
282
Eric Anholt7d573822009-01-02 13:33:00 -0800283void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
284{
285 struct drm_i915_private *dev_priv = dev->dev_private;
286 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -0700287 struct intel_encoder *intel_encoder;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800288 struct intel_connector *intel_connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100289 struct intel_hdmi *intel_hdmi;
Eric Anholt7d573822009-01-02 13:33:00 -0800290
Chris Wilsonea5b2132010-08-04 13:50:23 +0100291 intel_hdmi = kzalloc(sizeof(struct intel_hdmi), GFP_KERNEL);
292 if (!intel_hdmi)
Eric Anholt7d573822009-01-02 13:33:00 -0800293 return;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800294
295 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
296 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100297 kfree(intel_hdmi);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800298 return;
299 }
300
Chris Wilsonea5b2132010-08-04 13:50:23 +0100301 intel_encoder = &intel_hdmi->base;
Chris Wilson373a3cf2010-09-15 12:03:59 +0100302 drm_encoder_init(dev, &intel_encoder->base, &intel_hdmi_enc_funcs,
303 DRM_MODE_ENCODER_TMDS);
304
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800305 connector = &intel_connector->base;
Eric Anholt7d573822009-01-02 13:33:00 -0800306 drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
Adam Jackson8d911042009-09-23 15:08:29 -0400307 DRM_MODE_CONNECTOR_HDMIA);
Eric Anholt7d573822009-01-02 13:33:00 -0800308 drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
309
Eric Anholt21d40d32010-03-25 11:11:14 -0700310 intel_encoder->type = INTEL_OUTPUT_HDMI;
Eric Anholt7d573822009-01-02 13:33:00 -0800311
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000312 connector->polled = DRM_CONNECTOR_POLL_HPD;
Eric Anholt7d573822009-01-02 13:33:00 -0800313 connector->interlace_allowed = 0;
314 connector->doublescan_allowed = 0;
Eric Anholt21d40d32010-03-25 11:11:14 -0700315 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Eric Anholt7d573822009-01-02 13:33:00 -0800316
317 /* Set up the DDC bus. */
Ma Lingf8aed702009-08-24 13:50:24 +0800318 if (sdvox_reg == SDVOB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700319 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700320 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800321 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800322 } else if (sdvox_reg == SDVOC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700323 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700324 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800325 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800326 } else if (sdvox_reg == HDMIB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700327 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700328 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800329 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800330 } else if (sdvox_reg == HDMIC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700331 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700332 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800333 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800334 } else if (sdvox_reg == HDMID) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700335 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700336 intel_hdmi->ddc_bus = GMBUS_PORT_DPD;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800337 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800338 }
Eric Anholt7d573822009-01-02 13:33:00 -0800339
Chris Wilsonea5b2132010-08-04 13:50:23 +0100340 intel_hdmi->sdvox_reg = sdvox_reg;
Eric Anholt7d573822009-01-02 13:33:00 -0800341
Chris Wilson4ef69c72010-09-09 15:14:28 +0100342 drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs);
Eric Anholt7d573822009-01-02 13:33:00 -0800343
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100344 intel_hdmi_add_properties(intel_hdmi, connector);
345
Chris Wilsondf0e9242010-09-09 16:20:55 +0100346 intel_connector_attach_encoder(intel_connector, intel_encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800347 drm_sysfs_connector_add(connector);
348
349 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
350 * 0xd. Failure to do so will result in spurious interrupts being
351 * generated on the port when a cable is not attached.
352 */
353 if (IS_G4X(dev) && !IS_GM45(dev)) {
354 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
355 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
356 }
Eric Anholt7d573822009-01-02 13:33:00 -0800357}