blob: 9e30daae37dca8c45ba0635f3e22a9d1e98da967 [file] [log] [blame]
Eric Anholt7d573822009-01-02 13:33:00 -08001/*
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"
Keith Packardaa93d632009-05-05 09:52:46 -070034#include "drm_edid.h"
Eric Anholt7d573822009-01-02 13:33:00 -080035#include "intel_drv.h"
36#include "i915_drm.h"
37#include "i915_drv.h"
38
39struct intel_hdmi_priv {
40 u32 sdvox_reg;
41 u32 save_SDVOX;
Ma Ling9dff6af2009-04-02 13:13:26 +080042 bool has_hdmi_sink;
Eric Anholt7d573822009-01-02 13:33:00 -080043};
44
45static 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 |
Zhenyu Wang56d21b02009-06-17 09:43:25 +080060 SDVO_HSYNC_ACTIVE_HIGH;
Eric Anholt7d573822009-01-02 13:33:00 -080061
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
72static 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
90static 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
100static 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
111static 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
125static 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
Keith Packardaa93d632009-05-05 09:52:46 -0700132static enum drm_connector_status
133intel_hdmi_edid_detect(struct drm_connector *connector)
Ma Ling9dff6af2009-04-02 13:13:26 +0800134{
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;
Keith Packardaa93d632009-05-05 09:52:46 -0700138 enum drm_connector_status status = connector_status_disconnected;
Ma Ling9dff6af2009-04-02 13:13:26 +0800139
140 edid = drm_get_edid(&intel_output->base,
Keith Packardf9c10a92009-05-30 12:16:25 -0700141 intel_output->ddc_bus);
Keith Packardaa93d632009-05-05 09:52:46 -0700142 hdmi_priv->has_hdmi_sink = false;
143 if (edid) {
Eric Anholtbe9f1c42009-06-21 22:14:55 -0700144 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
Keith Packardaa93d632009-05-05 09:52:46 -0700145 status = connector_status_connected;
146 hdmi_priv->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
147 }
Ma Ling9dff6af2009-04-02 13:13:26 +0800148 intel_output->base.display_info.raw_edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700149 kfree(edid);
Ma Ling9dff6af2009-04-02 13:13:26 +0800150 }
Keith Packardaa93d632009-05-05 09:52:46 -0700151 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +0800152}
153
Eric Anholt7d573822009-01-02 13:33:00 -0800154static enum drm_connector_status
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800155igdng_hdmi_detect(struct drm_connector *connector)
156{
157 struct intel_output *intel_output = to_intel_output(connector);
158 struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
159
160 /* FIXME hotplug detect */
161
162 hdmi_priv->has_hdmi_sink = false;
Keith Packardaa93d632009-05-05 09:52:46 -0700163 return intel_hdmi_edid_detect(connector);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800164}
165
166static enum drm_connector_status
Eric Anholt7d573822009-01-02 13:33:00 -0800167intel_hdmi_detect(struct drm_connector *connector)
168{
169 struct drm_device *dev = connector->dev;
170 struct drm_i915_private *dev_priv = dev->dev_private;
171 struct intel_output *intel_output = to_intel_output(connector);
172 struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
173 u32 temp, bit;
174
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800175 if (IS_IGDNG(dev))
176 return igdng_hdmi_detect(connector);
177
Eric Anholt7d573822009-01-02 13:33:00 -0800178 temp = I915_READ(PORT_HOTPLUG_EN);
179
Ma Ling9d2949a2009-05-11 11:33:22 +0800180 switch (hdmi_priv->sdvox_reg) {
181 case SDVOB:
182 temp |= HDMIB_HOTPLUG_INT_EN;
183 break;
184 case SDVOC:
185 temp |= HDMIC_HOTPLUG_INT_EN;
186 break;
187 default:
188 return connector_status_unknown;
189 }
190
191 I915_WRITE(PORT_HOTPLUG_EN, temp);
Eric Anholt7d573822009-01-02 13:33:00 -0800192
193 POSTING_READ(PORT_HOTPLUG_EN);
194
195 switch (hdmi_priv->sdvox_reg) {
196 case SDVOB:
197 bit = HDMIB_HOTPLUG_INT_STATUS;
198 break;
199 case SDVOC:
200 bit = HDMIC_HOTPLUG_INT_STATUS;
201 break;
202 default:
203 return connector_status_unknown;
204 }
205
Keith Packardaa93d632009-05-05 09:52:46 -0700206 if ((I915_READ(PORT_HOTPLUG_STAT) & bit) != 0)
207 return intel_hdmi_edid_detect(connector);
208 else
Eric Anholt7d573822009-01-02 13:33:00 -0800209 return connector_status_disconnected;
210}
211
212static int intel_hdmi_get_modes(struct drm_connector *connector)
213{
214 struct intel_output *intel_output = to_intel_output(connector);
215
216 /* We should parse the EDID data and find out if it's an HDMI sink so
217 * we can send audio to it.
218 */
219
220 return intel_ddc_get_modes(intel_output);
221}
222
223static void intel_hdmi_destroy(struct drm_connector *connector)
224{
225 struct intel_output *intel_output = to_intel_output(connector);
226
227 if (intel_output->i2c_bus)
228 intel_i2c_destroy(intel_output->i2c_bus);
229 drm_sysfs_connector_remove(connector);
230 drm_connector_cleanup(connector);
231 kfree(intel_output);
232}
233
234static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
235 .dpms = intel_hdmi_dpms,
236 .mode_fixup = intel_hdmi_mode_fixup,
237 .prepare = intel_encoder_prepare,
238 .mode_set = intel_hdmi_mode_set,
239 .commit = intel_encoder_commit,
240};
241
242static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700243 .dpms = drm_helper_connector_dpms,
Eric Anholt7d573822009-01-02 13:33:00 -0800244 .save = intel_hdmi_save,
245 .restore = intel_hdmi_restore,
246 .detect = intel_hdmi_detect,
247 .fill_modes = drm_helper_probe_single_connector_modes,
248 .destroy = intel_hdmi_destroy,
249};
250
251static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
252 .get_modes = intel_hdmi_get_modes,
253 .mode_valid = intel_hdmi_mode_valid,
254 .best_encoder = intel_best_encoder,
255};
256
257static void intel_hdmi_enc_destroy(struct drm_encoder *encoder)
258{
259 drm_encoder_cleanup(encoder);
260}
261
262static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
263 .destroy = intel_hdmi_enc_destroy,
264};
265
266
267void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
268{
269 struct drm_i915_private *dev_priv = dev->dev_private;
270 struct drm_connector *connector;
271 struct intel_output *intel_output;
272 struct intel_hdmi_priv *hdmi_priv;
273
274 intel_output = kcalloc(sizeof(struct intel_output) +
275 sizeof(struct intel_hdmi_priv), 1, GFP_KERNEL);
276 if (!intel_output)
277 return;
278 hdmi_priv = (struct intel_hdmi_priv *)(intel_output + 1);
279
280 connector = &intel_output->base;
281 drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
282 DRM_MODE_CONNECTOR_DVID);
283 drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
284
285 intel_output->type = INTEL_OUTPUT_HDMI;
286
287 connector->interlace_allowed = 0;
288 connector->doublescan_allowed = 0;
289
290 /* Set up the DDC bus. */
291 if (sdvox_reg == SDVOB)
292 intel_output->ddc_bus = intel_i2c_create(dev, GPIOE, "HDMIB");
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800293 else if (sdvox_reg == SDVOC)
Eric Anholt7d573822009-01-02 13:33:00 -0800294 intel_output->ddc_bus = intel_i2c_create(dev, GPIOD, "HDMIC");
Zhenyu Wang30ad48b2009-06-05 15:38:43 +0800295 else if (sdvox_reg == HDMIB)
296 intel_output->ddc_bus = intel_i2c_create(dev, PCH_GPIOE,
297 "HDMIB");
298 else if (sdvox_reg == HDMIC)
299 intel_output->ddc_bus = intel_i2c_create(dev, PCH_GPIOD,
300 "HDMIC");
301 else if (sdvox_reg == HDMID)
302 intel_output->ddc_bus = intel_i2c_create(dev, PCH_GPIOF,
303 "HDMID");
Eric Anholt7d573822009-01-02 13:33:00 -0800304
305 if (!intel_output->ddc_bus)
306 goto err_connector;
307
308 hdmi_priv->sdvox_reg = sdvox_reg;
309 intel_output->dev_priv = hdmi_priv;
310
311 drm_encoder_init(dev, &intel_output->enc, &intel_hdmi_enc_funcs,
312 DRM_MODE_ENCODER_TMDS);
313 drm_encoder_helper_add(&intel_output->enc, &intel_hdmi_helper_funcs);
314
315 drm_mode_connector_attach_encoder(&intel_output->base,
316 &intel_output->enc);
317 drm_sysfs_connector_add(connector);
318
319 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
320 * 0xd. Failure to do so will result in spurious interrupts being
321 * generated on the port when a cable is not attached.
322 */
323 if (IS_G4X(dev) && !IS_GM45(dev)) {
324 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
325 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
326 }
327
328 return;
329
330err_connector:
331 drm_connector_cleanup(connector);
332 kfree(intel_output);
333
334 return;
335}