Merge commit 'v2.6.36-rc1' into HEAD
[pandora-kernel.git] / drivers / gpu / drm / i915 / intel_hdmi.c
1 /*
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/slab.h>
31 #include <linux/delay.h>
32 #include "drmP.h"
33 #include "drm.h"
34 #include "drm_crtc.h"
35 #include "drm_edid.h"
36 #include "intel_drv.h"
37 #include "i915_drm.h"
38 #include "i915_drv.h"
39
40 struct intel_hdmi_priv {
41         u32 sdvox_reg;
42         bool has_hdmi_sink;
43 };
44
45 static 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);
53         struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
54         struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
55         u32 sdvox;
56
57         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;
62
63         if (hdmi_priv->has_hdmi_sink) {
64                 sdvox |= SDVO_AUDIO_ENABLE;
65                 if (HAS_PCH_CPT(dev))
66                         sdvox |= HDMI_MODE_SELECT;
67         }
68
69         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         }
75
76         I915_WRITE(hdmi_priv->sdvox_reg, sdvox);
77         POSTING_READ(hdmi_priv->sdvox_reg);
78 }
79
80 static 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;
84         struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
85         struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
86         u32 temp;
87
88         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          */
93         if (HAS_PCH_SPLIT(dev)) {
94                 I915_WRITE(hdmi_priv->sdvox_reg, temp & ~SDVO_ENABLE);
95                 POSTING_READ(hdmi_priv->sdvox_reg);
96         }
97
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);
105         POSTING_READ(hdmi_priv->sdvox_reg);
106
107         /* HW workaround, need to write this twice for issue that may result
108          * in first write getting masked.
109          */
110         if (HAS_PCH_SPLIT(dev)) {
111                 I915_WRITE(hdmi_priv->sdvox_reg, temp);
112                 POSTING_READ(hdmi_priv->sdvox_reg);
113         }
114 }
115
116 static 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
130 static 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
137 static enum drm_connector_status
138 intel_hdmi_detect(struct drm_connector *connector)
139 {
140         struct drm_encoder *encoder = intel_attached_encoder(connector);
141         struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
142         struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
143         struct edid *edid = NULL;
144         enum drm_connector_status status = connector_status_disconnected;
145
146         hdmi_priv->has_hdmi_sink = false;
147         edid = drm_get_edid(connector,
148                             intel_encoder->ddc_bus);
149
150         if (edid) {
151                 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
152                         status = connector_status_connected;
153                         hdmi_priv->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
154                 }
155                 connector->display_info.raw_edid = NULL;
156                 kfree(edid);
157         }
158
159         return status;
160 }
161
162 static int intel_hdmi_get_modes(struct drm_connector *connector)
163 {
164         struct drm_encoder *encoder = intel_attached_encoder(connector);
165         struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
166
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
171         return intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
172 }
173
174 static void intel_hdmi_destroy(struct drm_connector *connector)
175 {
176         drm_sysfs_connector_remove(connector);
177         drm_connector_cleanup(connector);
178         kfree(connector);
179 }
180
181 static 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
189 static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
190         .dpms = drm_helper_connector_dpms,
191         .detect = intel_hdmi_detect,
192         .fill_modes = drm_helper_probe_single_connector_modes,
193         .destroy = intel_hdmi_destroy,
194 };
195
196 static 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,
199         .best_encoder = intel_attached_encoder,
200 };
201
202 static void intel_hdmi_enc_destroy(struct drm_encoder *encoder)
203 {
204         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);
208         drm_encoder_cleanup(encoder);
209         kfree(intel_encoder);
210 }
211
212 static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
213         .destroy = intel_hdmi_enc_destroy,
214 };
215
216 void 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;
220         struct intel_encoder *intel_encoder;
221         struct intel_connector *intel_connector;
222         struct intel_hdmi_priv *hdmi_priv;
223
224         intel_encoder = kcalloc(sizeof(struct intel_encoder) +
225                                sizeof(struct intel_hdmi_priv), 1, GFP_KERNEL);
226         if (!intel_encoder)
227                 return;
228
229         intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
230         if (!intel_connector) {
231                 kfree(intel_encoder);
232                 return;
233         }
234
235         hdmi_priv = (struct intel_hdmi_priv *)(intel_encoder + 1);
236
237         connector = &intel_connector->base;
238         drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
239                            DRM_MODE_CONNECTOR_HDMIA);
240         drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
241
242         intel_encoder->type = INTEL_OUTPUT_HDMI;
243
244         connector->polled = DRM_CONNECTOR_POLL_HPD;
245         connector->interlace_allowed = 0;
246         connector->doublescan_allowed = 0;
247         intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
248
249         /* Set up the DDC bus. */
250         if (sdvox_reg == SDVOB) {
251                 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
252                 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOE, "HDMIB");
253                 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
254         } else if (sdvox_reg == SDVOC) {
255                 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
256                 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOD, "HDMIC");
257                 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
258         } else if (sdvox_reg == HDMIB) {
259                 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
260                 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOE,
261                                                                 "HDMIB");
262                 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
263         } else if (sdvox_reg == HDMIC) {
264                 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
265                 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOD,
266                                                                 "HDMIC");
267                 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
268         } else if (sdvox_reg == HDMID) {
269                 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
270                 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOF,
271                                                                 "HDMID");
272                 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
273         }
274         if (!intel_encoder->ddc_bus)
275                 goto err_connector;
276
277         hdmi_priv->sdvox_reg = sdvox_reg;
278         intel_encoder->dev_priv = hdmi_priv;
279
280         drm_encoder_init(dev, &intel_encoder->enc, &intel_hdmi_enc_funcs,
281                          DRM_MODE_ENCODER_TMDS);
282         drm_encoder_helper_add(&intel_encoder->enc, &intel_hdmi_helper_funcs);
283
284         drm_mode_connector_attach_encoder(&intel_connector->base,
285                                           &intel_encoder->enc);
286         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
299 err_connector:
300         drm_connector_cleanup(connector);
301         kfree(intel_encoder);
302         kfree(intel_connector);
303
304         return;
305 }