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