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