Merge branch 'drm-radeon-evergreen-accel' into drm-core-next
[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 |
58                 SDVO_BORDER_ENABLE |
59                 SDVO_VSYNC_ACTIVE_HIGH |
60                 SDVO_HSYNC_ACTIVE_HIGH;
61
62         if (hdmi_priv->has_hdmi_sink)
63                 sdvox |= SDVO_AUDIO_ENABLE;
64
65         if (intel_crtc->pipe == 1) {
66                 if (HAS_PCH_CPT(dev))
67                         sdvox |= PORT_TRANS_B_SEL_CPT;
68                 else
69                         sdvox |= SDVO_PIPE_B_SELECT;
70         }
71
72         I915_WRITE(hdmi_priv->sdvox_reg, sdvox);
73         POSTING_READ(hdmi_priv->sdvox_reg);
74 }
75
76 static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
77 {
78         struct drm_device *dev = encoder->dev;
79         struct drm_i915_private *dev_priv = dev->dev_private;
80         struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
81         struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
82         u32 temp;
83
84         temp = I915_READ(hdmi_priv->sdvox_reg);
85
86         /* HW workaround, need to toggle enable bit off and on for 12bpc, but
87          * we do this anyway which shows more stable in testing.
88          */
89         if (HAS_PCH_SPLIT(dev)) {
90                 I915_WRITE(hdmi_priv->sdvox_reg, temp & ~SDVO_ENABLE);
91                 POSTING_READ(hdmi_priv->sdvox_reg);
92         }
93
94         if (mode != DRM_MODE_DPMS_ON) {
95                 temp &= ~SDVO_ENABLE;
96         } else {
97                 temp |= SDVO_ENABLE;
98         }
99
100         I915_WRITE(hdmi_priv->sdvox_reg, temp);
101         POSTING_READ(hdmi_priv->sdvox_reg);
102
103         /* HW workaround, need to write this twice for issue that may result
104          * in first write getting masked.
105          */
106         if (HAS_PCH_SPLIT(dev)) {
107                 I915_WRITE(hdmi_priv->sdvox_reg, temp);
108                 POSTING_READ(hdmi_priv->sdvox_reg);
109         }
110 }
111
112 static int intel_hdmi_mode_valid(struct drm_connector *connector,
113                                  struct drm_display_mode *mode)
114 {
115         if (mode->clock > 165000)
116                 return MODE_CLOCK_HIGH;
117         if (mode->clock < 20000)
118                 return MODE_CLOCK_HIGH;
119
120         if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
121                 return MODE_NO_DBLESCAN;
122
123         return MODE_OK;
124 }
125
126 static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
127                                   struct drm_display_mode *mode,
128                                   struct drm_display_mode *adjusted_mode)
129 {
130         return true;
131 }
132
133 static enum drm_connector_status
134 intel_hdmi_detect(struct drm_connector *connector)
135 {
136         struct drm_encoder *encoder = intel_attached_encoder(connector);
137         struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
138         struct intel_hdmi_priv *hdmi_priv = intel_encoder->dev_priv;
139         struct edid *edid = NULL;
140         enum drm_connector_status status = connector_status_disconnected;
141
142         hdmi_priv->has_hdmi_sink = false;
143         edid = drm_get_edid(connector,
144                             intel_encoder->ddc_bus);
145
146         if (edid) {
147                 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
148                         status = connector_status_connected;
149                         hdmi_priv->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
150                 }
151                 connector->display_info.raw_edid = NULL;
152                 kfree(edid);
153         }
154
155         return status;
156 }
157
158 static int intel_hdmi_get_modes(struct drm_connector *connector)
159 {
160         struct drm_encoder *encoder = intel_attached_encoder(connector);
161         struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
162
163         /* We should parse the EDID data and find out if it's an HDMI sink so
164          * we can send audio to it.
165          */
166
167         return intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
168 }
169
170 static void intel_hdmi_destroy(struct drm_connector *connector)
171 {
172         drm_sysfs_connector_remove(connector);
173         drm_connector_cleanup(connector);
174         kfree(connector);
175 }
176
177 static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
178         .dpms = intel_hdmi_dpms,
179         .mode_fixup = intel_hdmi_mode_fixup,
180         .prepare = intel_encoder_prepare,
181         .mode_set = intel_hdmi_mode_set,
182         .commit = intel_encoder_commit,
183 };
184
185 static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
186         .dpms = drm_helper_connector_dpms,
187         .detect = intel_hdmi_detect,
188         .fill_modes = drm_helper_probe_single_connector_modes,
189         .destroy = intel_hdmi_destroy,
190 };
191
192 static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
193         .get_modes = intel_hdmi_get_modes,
194         .mode_valid = intel_hdmi_mode_valid,
195         .best_encoder = intel_attached_encoder,
196 };
197
198 static void intel_hdmi_enc_destroy(struct drm_encoder *encoder)
199 {
200         struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
201
202         if (intel_encoder->i2c_bus)
203                 intel_i2c_destroy(intel_encoder->i2c_bus);
204         drm_encoder_cleanup(encoder);
205         kfree(intel_encoder);
206 }
207
208 static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
209         .destroy = intel_hdmi_enc_destroy,
210 };
211
212 void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
213 {
214         struct drm_i915_private *dev_priv = dev->dev_private;
215         struct drm_connector *connector;
216         struct intel_encoder *intel_encoder;
217         struct intel_connector *intel_connector;
218         struct intel_hdmi_priv *hdmi_priv;
219
220         intel_encoder = kcalloc(sizeof(struct intel_encoder) +
221                                sizeof(struct intel_hdmi_priv), 1, GFP_KERNEL);
222         if (!intel_encoder)
223                 return;
224
225         intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
226         if (!intel_connector) {
227                 kfree(intel_encoder);
228                 return;
229         }
230
231         hdmi_priv = (struct intel_hdmi_priv *)(intel_encoder + 1);
232
233         connector = &intel_connector->base;
234         drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
235                            DRM_MODE_CONNECTOR_HDMIA);
236         drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
237
238         intel_encoder->type = INTEL_OUTPUT_HDMI;
239
240         connector->interlace_allowed = 0;
241         connector->doublescan_allowed = 0;
242         intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
243
244         /* Set up the DDC bus. */
245         if (sdvox_reg == SDVOB) {
246                 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
247                 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOE, "HDMIB");
248                 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
249         } else if (sdvox_reg == SDVOC) {
250                 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
251                 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOD, "HDMIC");
252                 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
253         } else if (sdvox_reg == HDMIB) {
254                 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
255                 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOE,
256                                                                 "HDMIB");
257                 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
258         } else if (sdvox_reg == HDMIC) {
259                 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
260                 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOD,
261                                                                 "HDMIC");
262                 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
263         } else if (sdvox_reg == HDMID) {
264                 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
265                 intel_encoder->ddc_bus = intel_i2c_create(dev, PCH_GPIOF,
266                                                                 "HDMID");
267                 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
268         }
269         if (!intel_encoder->ddc_bus)
270                 goto err_connector;
271
272         hdmi_priv->sdvox_reg = sdvox_reg;
273         intel_encoder->dev_priv = hdmi_priv;
274
275         drm_encoder_init(dev, &intel_encoder->enc, &intel_hdmi_enc_funcs,
276                          DRM_MODE_ENCODER_TMDS);
277         drm_encoder_helper_add(&intel_encoder->enc, &intel_hdmi_helper_funcs);
278
279         drm_mode_connector_attach_encoder(&intel_connector->base,
280                                           &intel_encoder->enc);
281         drm_sysfs_connector_add(connector);
282
283         /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
284          * 0xd.  Failure to do so will result in spurious interrupts being
285          * generated on the port when a cable is not attached.
286          */
287         if (IS_G4X(dev) && !IS_GM45(dev)) {
288                 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
289                 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
290         }
291
292         return;
293
294 err_connector:
295         drm_connector_cleanup(connector);
296         kfree(intel_encoder);
297         kfree(intel_connector);
298
299         return;
300 }