Merge branch 'fix/soundcore' into for-linus
[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/delay.h>
31 #include "drmP.h"
32 #include "drm.h"
33 #include "drm_crtc.h"
34 #include "intel_drv.h"
35 #include "i915_drm.h"
36 #include "i915_drv.h"
37
38 struct intel_hdmi_priv {
39         u32 sdvox_reg;
40         u32 save_SDVOX;
41         bool has_hdmi_sink;
42 };
43
44 static void intel_hdmi_mode_set(struct drm_encoder *encoder,
45                                 struct drm_display_mode *mode,
46                                 struct drm_display_mode *adjusted_mode)
47 {
48         struct drm_device *dev = encoder->dev;
49         struct drm_i915_private *dev_priv = dev->dev_private;
50         struct drm_crtc *crtc = encoder->crtc;
51         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
52         struct intel_output *intel_output = enc_to_intel_output(encoder);
53         struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
54         u32 sdvox;
55
56         sdvox = SDVO_ENCODING_HDMI |
57                 SDVO_BORDER_ENABLE |
58                 SDVO_VSYNC_ACTIVE_HIGH |
59                 SDVO_HSYNC_ACTIVE_HIGH |
60                 SDVO_NULL_PACKETS_DURING_VSYNC;
61
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
72 static 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;
76         struct intel_output *intel_output = enc_to_intel_output(encoder);
77         struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
78         u32 temp;
79
80         if (mode != DRM_MODE_DPMS_ON) {
81                 temp = I915_READ(hdmi_priv->sdvox_reg);
82                 I915_WRITE(hdmi_priv->sdvox_reg, temp & ~SDVO_ENABLE);
83         } else {
84                 temp = I915_READ(hdmi_priv->sdvox_reg);
85                 I915_WRITE(hdmi_priv->sdvox_reg, temp | SDVO_ENABLE);
86         }
87         POSTING_READ(hdmi_priv->sdvox_reg);
88 }
89
90 static void intel_hdmi_save(struct drm_connector *connector)
91 {
92         struct drm_device *dev = connector->dev;
93         struct drm_i915_private *dev_priv = dev->dev_private;
94         struct intel_output *intel_output = to_intel_output(connector);
95         struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
96
97         hdmi_priv->save_SDVOX = I915_READ(hdmi_priv->sdvox_reg);
98 }
99
100 static void intel_hdmi_restore(struct drm_connector *connector)
101 {
102         struct drm_device *dev = connector->dev;
103         struct drm_i915_private *dev_priv = dev->dev_private;
104         struct intel_output *intel_output = to_intel_output(connector);
105         struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
106
107         I915_WRITE(hdmi_priv->sdvox_reg, hdmi_priv->save_SDVOX);
108         POSTING_READ(hdmi_priv->sdvox_reg);
109 }
110
111 static int intel_hdmi_mode_valid(struct drm_connector *connector,
112                                  struct drm_display_mode *mode)
113 {
114         if (mode->clock > 165000)
115                 return MODE_CLOCK_HIGH;
116         if (mode->clock < 20000)
117                 return MODE_CLOCK_HIGH;
118
119         if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
120                 return MODE_NO_DBLESCAN;
121
122         return MODE_OK;
123 }
124
125 static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
126                                   struct drm_display_mode *mode,
127                                   struct drm_display_mode *adjusted_mode)
128 {
129         return true;
130 }
131
132 static void
133 intel_hdmi_sink_detect(struct drm_connector *connector)
134 {
135         struct intel_output *intel_output = to_intel_output(connector);
136         struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
137         struct edid *edid = NULL;
138
139         edid = drm_get_edid(&intel_output->base,
140                             &intel_output->ddc_bus->adapter);
141         if (edid != NULL) {
142                 hdmi_priv->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
143                 kfree(edid);
144                 intel_output->base.display_info.raw_edid = NULL;
145         }
146 }
147
148 static enum drm_connector_status
149 igdng_hdmi_detect(struct drm_connector *connector)
150 {
151         struct intel_output *intel_output = to_intel_output(connector);
152         struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
153
154         /* FIXME hotplug detect */
155
156         hdmi_priv->has_hdmi_sink = false;
157         intel_hdmi_sink_detect(connector);
158         if (hdmi_priv->has_hdmi_sink)
159                 return connector_status_connected;
160         else
161                 return connector_status_disconnected;
162 }
163
164 static enum drm_connector_status
165 intel_hdmi_detect(struct drm_connector *connector)
166 {
167         struct drm_device *dev = connector->dev;
168         struct drm_i915_private *dev_priv = dev->dev_private;
169         struct intel_output *intel_output = to_intel_output(connector);
170         struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
171         u32 temp, bit;
172
173         if (IS_IGDNG(dev))
174                 return igdng_hdmi_detect(connector);
175
176         temp = I915_READ(PORT_HOTPLUG_EN);
177
178         switch (hdmi_priv->sdvox_reg) {
179         case SDVOB:
180                 temp |= HDMIB_HOTPLUG_INT_EN;
181                 break;
182         case SDVOC:
183                 temp |= HDMIC_HOTPLUG_INT_EN;
184                 break;
185         default:
186                 return connector_status_unknown;
187         }
188
189         I915_WRITE(PORT_HOTPLUG_EN, temp);
190
191         POSTING_READ(PORT_HOTPLUG_EN);
192
193         switch (hdmi_priv->sdvox_reg) {
194         case SDVOB:
195                 bit = HDMIB_HOTPLUG_INT_STATUS;
196                 break;
197         case SDVOC:
198                 bit = HDMIC_HOTPLUG_INT_STATUS;
199                 break;
200         default:
201                 return connector_status_unknown;
202         }
203
204         if ((I915_READ(PORT_HOTPLUG_STAT) & bit) != 0) {
205                 intel_hdmi_sink_detect(connector);
206                 return connector_status_connected;
207         } else
208                 return connector_status_disconnected;
209 }
210
211 static int intel_hdmi_get_modes(struct drm_connector *connector)
212 {
213         struct intel_output *intel_output = to_intel_output(connector);
214
215         /* We should parse the EDID data and find out if it's an HDMI sink so
216          * we can send audio to it.
217          */
218
219         return intel_ddc_get_modes(intel_output);
220 }
221
222 static void intel_hdmi_destroy(struct drm_connector *connector)
223 {
224         struct intel_output *intel_output = to_intel_output(connector);
225
226         if (intel_output->i2c_bus)
227                 intel_i2c_destroy(intel_output->i2c_bus);
228         drm_sysfs_connector_remove(connector);
229         drm_connector_cleanup(connector);
230         kfree(intel_output);
231 }
232
233 static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
234         .dpms = intel_hdmi_dpms,
235         .mode_fixup = intel_hdmi_mode_fixup,
236         .prepare = intel_encoder_prepare,
237         .mode_set = intel_hdmi_mode_set,
238         .commit = intel_encoder_commit,
239 };
240
241 static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
242         .dpms = drm_helper_connector_dpms,
243         .save = intel_hdmi_save,
244         .restore = intel_hdmi_restore,
245         .detect = intel_hdmi_detect,
246         .fill_modes = drm_helper_probe_single_connector_modes,
247         .destroy = intel_hdmi_destroy,
248 };
249
250 static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
251         .get_modes = intel_hdmi_get_modes,
252         .mode_valid = intel_hdmi_mode_valid,
253         .best_encoder = intel_best_encoder,
254 };
255
256 static void intel_hdmi_enc_destroy(struct drm_encoder *encoder)
257 {
258         drm_encoder_cleanup(encoder);
259 }
260
261 static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
262         .destroy = intel_hdmi_enc_destroy,
263 };
264
265
266 void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
267 {
268         struct drm_i915_private *dev_priv = dev->dev_private;
269         struct drm_connector *connector;
270         struct intel_output *intel_output;
271         struct intel_hdmi_priv *hdmi_priv;
272
273         intel_output = kcalloc(sizeof(struct intel_output) +
274                                sizeof(struct intel_hdmi_priv), 1, GFP_KERNEL);
275         if (!intel_output)
276                 return;
277         hdmi_priv = (struct intel_hdmi_priv *)(intel_output + 1);
278
279         connector = &intel_output->base;
280         drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
281                            DRM_MODE_CONNECTOR_DVID);
282         drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
283
284         intel_output->type = INTEL_OUTPUT_HDMI;
285
286         connector->interlace_allowed = 0;
287         connector->doublescan_allowed = 0;
288
289         /* Set up the DDC bus. */
290         if (sdvox_reg == SDVOB)
291                 intel_output->ddc_bus = intel_i2c_create(dev, GPIOE, "HDMIB");
292         else if (sdvox_reg == SDVOC)
293                 intel_output->ddc_bus = intel_i2c_create(dev, GPIOD, "HDMIC");
294         else if (sdvox_reg == HDMIB)
295                 intel_output->ddc_bus = intel_i2c_create(dev, PCH_GPIOE,
296                                                                 "HDMIB");
297         else if (sdvox_reg == HDMIC)
298                 intel_output->ddc_bus = intel_i2c_create(dev, PCH_GPIOD,
299                                                                 "HDMIC");
300         else if (sdvox_reg == HDMID)
301                 intel_output->ddc_bus = intel_i2c_create(dev, PCH_GPIOF,
302                                                                 "HDMID");
303
304         if (!intel_output->ddc_bus)
305                 goto err_connector;
306
307         hdmi_priv->sdvox_reg = sdvox_reg;
308         intel_output->dev_priv = hdmi_priv;
309
310         drm_encoder_init(dev, &intel_output->enc, &intel_hdmi_enc_funcs,
311                          DRM_MODE_ENCODER_TMDS);
312         drm_encoder_helper_add(&intel_output->enc, &intel_hdmi_helper_funcs);
313
314         drm_mode_connector_attach_encoder(&intel_output->base,
315                                           &intel_output->enc);
316         drm_sysfs_connector_add(connector);
317
318         /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
319          * 0xd.  Failure to do so will result in spurious interrupts being
320          * generated on the port when a cable is not attached.
321          */
322         if (IS_G4X(dev) && !IS_GM45(dev)) {
323                 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
324                 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
325         }
326
327         return;
328
329 err_connector:
330         drm_connector_cleanup(connector);
331         kfree(intel_output);
332
333         return;
334 }