blob: bfc1bb443b057426e8c27b605ddb4ccbbb994e38 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
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 * Dave Airlie <airlied@linux.ie>
27 * Jesse Barnes <jesse.barnes@intel.com>
28 */
29
Jesse Barnesc1c7af62009-09-10 15:28:03 -070030#include <acpi/button.h>
Paul Collins565dcd42009-02-04 23:05:41 +130031#include <linux/dmi.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080032#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080034#include "drmP.h"
35#include "drm.h"
36#include "drm_crtc.h"
37#include "drm_edid.h"
38#include "intel_drv.h"
39#include "i915_drm.h"
40#include "i915_drv.h"
Zhao Yakuie99da352009-06-26 09:46:18 +080041#include <linux/acpi.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080042
Zhao Yakui3fbe18d2009-06-22 15:31:25 +080043/* Private structure for the integrated LVDS support */
Chris Wilsonea5b2132010-08-04 13:50:23 +010044struct intel_lvds {
45 struct intel_encoder base;
Zhao Yakui3fbe18d2009-06-22 15:31:25 +080046 int fitting_mode;
47 u32 pfit_control;
48 u32 pfit_pgm_ratios;
Chris Wilsone9e331a2010-09-13 01:16:10 +010049 bool pfit_dirty;
Zhao Yakui3fbe18d2009-06-22 15:31:25 +080050};
51
Chris Wilsonea5b2132010-08-04 13:50:23 +010052static struct intel_lvds *enc_to_intel_lvds(struct drm_encoder *encoder)
53{
Chris Wilson4ef69c72010-09-09 15:14:28 +010054 return container_of(encoder, struct intel_lvds, base.base);
Chris Wilsonea5b2132010-08-04 13:50:23 +010055}
56
Jesse Barnes79e53942008-11-07 14:24:08 -080057/**
Jesse Barnes79e53942008-11-07 14:24:08 -080058 * Sets the power state for the panel.
59 */
Chris Wilsone9e331a2010-09-13 01:16:10 +010060static void intel_lvds_set_power(struct intel_lvds *intel_lvds, bool on)
Jesse Barnes79e53942008-11-07 14:24:08 -080061{
Chris Wilsone9e331a2010-09-13 01:16:10 +010062 struct drm_device *dev = intel_lvds->base.base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080063 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone9e331a2010-09-13 01:16:10 +010064 u32 ctl_reg, lvds_reg;
Zhenyu Wang541998a2009-06-05 15:38:44 +080065
Eric Anholtc619eed2010-01-28 16:45:52 -080066 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang541998a2009-06-05 15:38:44 +080067 ctl_reg = PCH_PP_CONTROL;
Jesse Barnes469d1292010-02-11 12:41:05 -080068 lvds_reg = PCH_LVDS;
Zhenyu Wang541998a2009-06-05 15:38:44 +080069 } else {
70 ctl_reg = PP_CONTROL;
Jesse Barnes469d1292010-02-11 12:41:05 -080071 lvds_reg = LVDS;
Zhenyu Wang541998a2009-06-05 15:38:44 +080072 }
Jesse Barnes79e53942008-11-07 14:24:08 -080073
74 if (on) {
Jesse Barnes469d1292010-02-11 12:41:05 -080075 I915_WRITE(lvds_reg, I915_READ(lvds_reg) | LVDS_PORT_EN);
Chris Wilson77d07fd2010-09-12 12:42:35 +010076 I915_WRITE(ctl_reg, I915_READ(ctl_reg) | POWER_TARGET_ON);
Chris Wilsona9573552010-08-22 13:18:16 +010077 intel_panel_set_backlight(dev, dev_priv->backlight_level);
Jesse Barnes79e53942008-11-07 14:24:08 -080078 } else {
Chris Wilsone9e331a2010-09-13 01:16:10 +010079 dev_priv->backlight_level = intel_panel_get_backlight(dev);
80
Chris Wilsona9573552010-08-22 13:18:16 +010081 intel_panel_set_backlight(dev, 0);
Chris Wilson77d07fd2010-09-12 12:42:35 +010082 I915_WRITE(ctl_reg, I915_READ(ctl_reg) & ~POWER_TARGET_ON);
Chris Wilsone9e331a2010-09-13 01:16:10 +010083
84 if (intel_lvds->pfit_control) {
85 if (wait_for((I915_READ(PP_STATUS) & PP_ON) == 0, 1000))
86 DRM_ERROR("timed out waiting for panel to power off\n");
87 I915_WRITE(PFIT_CONTROL, 0);
88 intel_lvds->pfit_control = 0;
89 }
90
Jesse Barnes469d1292010-02-11 12:41:05 -080091 I915_WRITE(lvds_reg, I915_READ(lvds_reg) & ~LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080092 }
Chris Wilsonc9f9ccc2010-09-12 13:07:25 +010093 POSTING_READ(lvds_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -080094}
95
96static void intel_lvds_dpms(struct drm_encoder *encoder, int mode)
97{
Chris Wilsone9e331a2010-09-13 01:16:10 +010098 struct intel_lvds *intel_lvds = enc_to_intel_lvds(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080099
100 if (mode == DRM_MODE_DPMS_ON)
Chris Wilsone9e331a2010-09-13 01:16:10 +0100101 intel_lvds_set_power(intel_lvds, true);
Jesse Barnes79e53942008-11-07 14:24:08 -0800102 else
Chris Wilsone9e331a2010-09-13 01:16:10 +0100103 intel_lvds_set_power(intel_lvds, false);
Jesse Barnes79e53942008-11-07 14:24:08 -0800104
105 /* XXX: We never power down the LVDS pairs. */
106}
107
Jesse Barnes79e53942008-11-07 14:24:08 -0800108static int intel_lvds_mode_valid(struct drm_connector *connector,
109 struct drm_display_mode *mode)
110{
111 struct drm_device *dev = connector->dev;
112 struct drm_i915_private *dev_priv = dev->dev_private;
113 struct drm_display_mode *fixed_mode = dev_priv->panel_fixed_mode;
114
115 if (fixed_mode) {
116 if (mode->hdisplay > fixed_mode->hdisplay)
117 return MODE_PANEL;
118 if (mode->vdisplay > fixed_mode->vdisplay)
119 return MODE_PANEL;
120 }
121
122 return MODE_OK;
123}
124
Chris Wilson49be6632010-07-18 12:05:54 +0100125static void
126centre_horizontally(struct drm_display_mode *mode,
127 int width)
128{
129 u32 border, sync_pos, blank_width, sync_width;
130
131 /* keep the hsync and hblank widths constant */
132 sync_width = mode->crtc_hsync_end - mode->crtc_hsync_start;
133 blank_width = mode->crtc_hblank_end - mode->crtc_hblank_start;
134 sync_pos = (blank_width - sync_width + 1) / 2;
135
136 border = (mode->hdisplay - width + 1) / 2;
137 border += border & 1; /* make the border even */
138
139 mode->crtc_hdisplay = width;
140 mode->crtc_hblank_start = width + border;
141 mode->crtc_hblank_end = mode->crtc_hblank_start + blank_width;
142
143 mode->crtc_hsync_start = mode->crtc_hblank_start + sync_pos;
144 mode->crtc_hsync_end = mode->crtc_hsync_start + sync_width;
145}
146
147static void
148centre_vertically(struct drm_display_mode *mode,
149 int height)
150{
151 u32 border, sync_pos, blank_width, sync_width;
152
153 /* keep the vsync and vblank widths constant */
154 sync_width = mode->crtc_vsync_end - mode->crtc_vsync_start;
155 blank_width = mode->crtc_vblank_end - mode->crtc_vblank_start;
156 sync_pos = (blank_width - sync_width + 1) / 2;
157
158 border = (mode->vdisplay - height + 1) / 2;
159
160 mode->crtc_vdisplay = height;
161 mode->crtc_vblank_start = height + border;
162 mode->crtc_vblank_end = mode->crtc_vblank_start + blank_width;
163
164 mode->crtc_vsync_start = mode->crtc_vblank_start + sync_pos;
165 mode->crtc_vsync_end = mode->crtc_vsync_start + sync_width;
166}
167
168static inline u32 panel_fitter_scaling(u32 source, u32 target)
169{
170 /*
171 * Floating point operation is not supported. So the FACTOR
172 * is defined, which can avoid the floating point computation
173 * when calculating the panel ratio.
174 */
175#define ACCURACY 12
176#define FACTOR (1 << ACCURACY)
177 u32 ratio = source * FACTOR / target;
178 return (FACTOR * ratio + FACTOR/2) / FACTOR;
179}
180
Jesse Barnes79e53942008-11-07 14:24:08 -0800181static bool intel_lvds_mode_fixup(struct drm_encoder *encoder,
182 struct drm_display_mode *mode,
183 struct drm_display_mode *adjusted_mode)
184{
185 struct drm_device *dev = encoder->dev;
186 struct drm_i915_private *dev_priv = dev->dev_private;
187 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100188 struct intel_lvds *intel_lvds = enc_to_intel_lvds(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -0800189 struct drm_encoder *tmp_encoder;
Chris Wilson49be6632010-07-18 12:05:54 +0100190 u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800191
192 /* Should never happen!! */
193 if (!IS_I965G(dev) && intel_crtc->pipe == 0) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700194 DRM_ERROR("Can't support LVDS on pipe A\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800195 return false;
196 }
197
198 /* Should never happen!! */
199 list_for_each_entry(tmp_encoder, &dev->mode_config.encoder_list, head) {
200 if (tmp_encoder != encoder && tmp_encoder->crtc == encoder->crtc) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700201 DRM_ERROR("Can't enable LVDS and another "
Jesse Barnes79e53942008-11-07 14:24:08 -0800202 "encoder on the same pipe\n");
203 return false;
204 }
205 }
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800206 /* If we don't have a panel mode, there is nothing we can do */
207 if (dev_priv->panel_fixed_mode == NULL)
208 return true;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100209
Jesse Barnes79e53942008-11-07 14:24:08 -0800210 /*
Chris Wilson71677042010-07-17 13:38:43 +0100211 * We have timings from the BIOS for the panel, put them in
Jesse Barnes79e53942008-11-07 14:24:08 -0800212 * to the adjusted mode. The CRTC will be set up for this mode,
213 * with the panel scaling set up to source from the H/VDisplay
214 * of the original mode.
215 */
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100216 intel_fixed_panel_mode(dev_priv->panel_fixed_mode, adjusted_mode);
217
218 if (HAS_PCH_SPLIT(dev)) {
219 intel_pch_panel_fitting(dev, intel_lvds->fitting_mode,
220 mode, adjusted_mode);
221 return true;
222 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800223
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800224 /* Make sure pre-965s set dither correctly */
225 if (!IS_I965G(dev)) {
226 if (dev_priv->panel_wants_dither || dev_priv->lvds_dither)
227 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
228 }
229
230 /* Native modes don't need fitting */
231 if (adjusted_mode->hdisplay == mode->hdisplay &&
Chris Wilson49be6632010-07-18 12:05:54 +0100232 adjusted_mode->vdisplay == mode->vdisplay)
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800233 goto out;
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800234
235 /* 965+ wants fuzzy fitting */
236 if (IS_I965G(dev))
Chris Wilson49be6632010-07-18 12:05:54 +0100237 pfit_control |= ((intel_crtc->pipe << PFIT_PIPE_SHIFT) |
238 PFIT_FILTER_FUZZY);
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800239
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800240 /*
241 * Enable automatic panel scaling for non-native modes so that they fill
242 * the screen. Should be enabled before the pipe is enabled, according
243 * to register description and PRM.
244 * Change the value here to see the borders for debugging
245 */
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100246 I915_WRITE(BCLRPAT_A, 0);
247 I915_WRITE(BCLRPAT_B, 0);
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800248
Chris Wilsonea5b2132010-08-04 13:50:23 +0100249 switch (intel_lvds->fitting_mode) {
Jesse Barnes53bd8382009-07-01 10:04:40 -0700250 case DRM_MODE_SCALE_CENTER:
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800251 /*
252 * For centered modes, we have to calculate border widths &
253 * heights and modify the values programmed into the CRTC.
254 */
Chris Wilson49be6632010-07-18 12:05:54 +0100255 centre_horizontally(adjusted_mode, mode->hdisplay);
256 centre_vertically(adjusted_mode, mode->vdisplay);
257 border = LVDS_BORDER_ENABLE;
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800258 break;
Chris Wilson49be6632010-07-18 12:05:54 +0100259
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800260 case DRM_MODE_SCALE_ASPECT:
Chris Wilson49be6632010-07-18 12:05:54 +0100261 /* Scale but preserve the aspect ratio */
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800262 if (IS_I965G(dev)) {
Chris Wilson49be6632010-07-18 12:05:54 +0100263 u32 scaled_width = adjusted_mode->hdisplay * mode->vdisplay;
264 u32 scaled_height = mode->hdisplay * adjusted_mode->vdisplay;
265
266 pfit_control |= PFIT_ENABLE;
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800267 /* 965+ is easy, it does everything in hw */
Chris Wilson49be6632010-07-18 12:05:54 +0100268 if (scaled_width > scaled_height)
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800269 pfit_control |= PFIT_SCALING_PILLAR;
Chris Wilson49be6632010-07-18 12:05:54 +0100270 else if (scaled_width < scaled_height)
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800271 pfit_control |= PFIT_SCALING_LETTER;
272 else
273 pfit_control |= PFIT_SCALING_AUTO;
274 } else {
Chris Wilson49be6632010-07-18 12:05:54 +0100275 u32 scaled_width = adjusted_mode->hdisplay * mode->vdisplay;
276 u32 scaled_height = mode->hdisplay * adjusted_mode->vdisplay;
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800277 /*
278 * For earlier chips we have to calculate the scaling
279 * ratio by hand and program it into the
280 * PFIT_PGM_RATIO register
281 */
Chris Wilson49be6632010-07-18 12:05:54 +0100282 if (scaled_width > scaled_height) { /* pillar */
283 centre_horizontally(adjusted_mode, scaled_height / mode->vdisplay);
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800284
Chris Wilson49be6632010-07-18 12:05:54 +0100285 border = LVDS_BORDER_ENABLE;
286 if (mode->vdisplay != adjusted_mode->vdisplay) {
287 u32 bits = panel_fitter_scaling(mode->vdisplay, adjusted_mode->vdisplay);
288 pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
289 bits << PFIT_VERT_SCALE_SHIFT);
290 pfit_control |= (PFIT_ENABLE |
291 VERT_INTERP_BILINEAR |
292 HORIZ_INTERP_BILINEAR);
293 }
294 } else if (scaled_width < scaled_height) { /* letter */
295 centre_vertically(adjusted_mode, scaled_width / mode->hdisplay);
296
297 border = LVDS_BORDER_ENABLE;
298 if (mode->hdisplay != adjusted_mode->hdisplay) {
299 u32 bits = panel_fitter_scaling(mode->hdisplay, adjusted_mode->hdisplay);
300 pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
301 bits << PFIT_VERT_SCALE_SHIFT);
302 pfit_control |= (PFIT_ENABLE |
303 VERT_INTERP_BILINEAR |
304 HORIZ_INTERP_BILINEAR);
305 }
306 } else
307 /* Aspects match, Let hw scale both directions */
308 pfit_control |= (PFIT_ENABLE |
309 VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800310 VERT_INTERP_BILINEAR |
311 HORIZ_INTERP_BILINEAR);
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800312 }
313 break;
314
315 case DRM_MODE_SCALE_FULLSCREEN:
316 /*
317 * Full scaling, even if it changes the aspect ratio.
318 * Fortunately this is all done for us in hw.
319 */
320 pfit_control |= PFIT_ENABLE;
321 if (IS_I965G(dev))
322 pfit_control |= PFIT_SCALING_AUTO;
323 else
324 pfit_control |= (VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
325 VERT_INTERP_BILINEAR |
326 HORIZ_INTERP_BILINEAR);
327 break;
Chris Wilson49be6632010-07-18 12:05:54 +0100328
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800329 default:
330 break;
331 }
332
333out:
Chris Wilsone9e331a2010-09-13 01:16:10 +0100334 if (pfit_control != intel_lvds->pfit_control ||
335 pfit_pgm_ratios != intel_lvds->pfit_pgm_ratios) {
336 intel_lvds->pfit_control = pfit_control;
337 intel_lvds->pfit_pgm_ratios = pfit_pgm_ratios;
338 intel_lvds->pfit_dirty = true;
339 }
Chris Wilson49be6632010-07-18 12:05:54 +0100340 dev_priv->lvds_border_bits = border;
341
Zhao Yakuia3e17eb2009-10-10 10:42:37 +0800342 /*
Jesse Barnes79e53942008-11-07 14:24:08 -0800343 * XXX: It would be nice to support lower refresh rates on the
344 * panels to reduce power consumption, and perhaps match the
345 * user's requested refresh rate.
346 */
347
348 return true;
349}
350
351static void intel_lvds_prepare(struct drm_encoder *encoder)
352{
353 struct drm_device *dev = encoder->dev;
354 struct drm_i915_private *dev_priv = dev->dev_private;
Matthew Garrett309b1e32010-05-18 13:53:16 -0400355 struct intel_lvds *intel_lvds = enc_to_intel_lvds(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -0800356
Chris Wilsona9573552010-08-22 13:18:16 +0100357 dev_priv->backlight_level = intel_panel_get_backlight(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -0800358
Chris Wilsone9e331a2010-09-13 01:16:10 +0100359 /* We try to do the minimum that is necessary in order to unlock
360 * the registers for mode setting.
361 *
362 * On Ironlake, this is quite simple as we just set the unlock key
363 * and ignore all subtleties. (This may cause some issues...)
364 *
365 * Prior to Ironlake, we must disable the pipe if we want to adjust
366 * the panel fitter. However at all other times we can just reset
367 * the registers regardless.
368 */
369
370 if (HAS_PCH_SPLIT(dev)) {
371 I915_WRITE(PCH_PP_CONTROL,
372 I915_READ(PCH_PP_CONTROL) | PANEL_UNLOCK_REGS);
373 } else if (intel_lvds->pfit_dirty) {
374 I915_WRITE(PP_CONTROL,
375 I915_READ(PP_CONTROL) & ~POWER_TARGET_ON);
376 I915_WRITE(LVDS, I915_READ(LVDS) & ~LVDS_PORT_EN);
377 } else {
378 I915_WRITE(PP_CONTROL,
379 I915_READ(PP_CONTROL) | PANEL_UNLOCK_REGS);
380 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800381}
382
Chris Wilsone9e331a2010-09-13 01:16:10 +0100383static void intel_lvds_commit(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -0800384{
385 struct drm_device *dev = encoder->dev;
386 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone9e331a2010-09-13 01:16:10 +0100387 struct intel_lvds *intel_lvds = enc_to_intel_lvds(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -0800388
Chris Wilsona9573552010-08-22 13:18:16 +0100389 if (dev_priv->backlight_level == 0)
390 dev_priv->backlight_level = intel_panel_get_max_backlight(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -0800391
Chris Wilsone9e331a2010-09-13 01:16:10 +0100392 /* Undo any unlocking done in prepare to prevent accidental
393 * adjustment of the registers.
394 */
395 if (HAS_PCH_SPLIT(dev)) {
396 u32 val = I915_READ(PCH_PP_CONTROL);
397 if ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS)
398 I915_WRITE(PCH_PP_CONTROL, val & 0x3);
399 } else {
400 u32 val = I915_READ(PP_CONTROL);
401 if ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS)
402 I915_WRITE(PP_CONTROL, val & 0x3);
403 }
404
405 /* Always do a full power on as we do not know what state
406 * we were left in.
407 */
408 intel_lvds_set_power(intel_lvds, true);
Jesse Barnes79e53942008-11-07 14:24:08 -0800409}
410
411static void intel_lvds_mode_set(struct drm_encoder *encoder,
412 struct drm_display_mode *mode,
413 struct drm_display_mode *adjusted_mode)
414{
415 struct drm_device *dev = encoder->dev;
416 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100417 struct intel_lvds *intel_lvds = enc_to_intel_lvds(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -0800418
419 /*
420 * The LVDS pin pair will already have been turned on in the
421 * intel_crtc_mode_set since it has a large impact on the DPLL
422 * settings.
423 */
424
Eric Anholtc619eed2010-01-28 16:45:52 -0800425 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang541998a2009-06-05 15:38:44 +0800426 return;
427
Chris Wilsone9e331a2010-09-13 01:16:10 +0100428 if (!intel_lvds->pfit_dirty)
429 return;
430
Jesse Barnes79e53942008-11-07 14:24:08 -0800431 /*
432 * Enable automatic panel scaling so that non-native modes fill the
433 * screen. Should be enabled before the pipe is enabled, according to
434 * register description and PRM.
435 */
Chris Wilsone9e331a2010-09-13 01:16:10 +0100436 if (wait_for((I915_READ(PP_STATUS) & PP_ON) == 0, 1000))
437 DRM_ERROR("timed out waiting for panel to power off\n");
438
Chris Wilsonea5b2132010-08-04 13:50:23 +0100439 I915_WRITE(PFIT_PGM_RATIOS, intel_lvds->pfit_pgm_ratios);
440 I915_WRITE(PFIT_CONTROL, intel_lvds->pfit_control);
Chris Wilsone9e331a2010-09-13 01:16:10 +0100441 intel_lvds->pfit_dirty = false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800442}
443
444/**
445 * Detect the LVDS connection.
446 *
Jesse Barnesb42d4c52009-09-10 15:28:04 -0700447 * Since LVDS doesn't have hotlug, we use the lid as a proxy. Open means
448 * connected and closed means disconnected. We also send hotplug events as
449 * needed, using lid status notification from the input layer.
Jesse Barnes79e53942008-11-07 14:24:08 -0800450 */
451static enum drm_connector_status intel_lvds_detect(struct drm_connector *connector)
452{
Jesse Barnes7b9c5ab2010-02-12 09:30:00 -0800453 struct drm_device *dev = connector->dev;
Jesse Barnesb42d4c52009-09-10 15:28:04 -0700454 enum drm_connector_status status = connector_status_connected;
455
Jesse Barnes7b9c5ab2010-02-12 09:30:00 -0800456 /* ACPI lid methods were generally unreliable in this generation, so
457 * don't even bother.
458 */
Eric Anholt6e6c8222010-03-17 13:48:06 -0700459 if (IS_GEN2(dev) || IS_GEN3(dev))
Jesse Barnes7b9c5ab2010-02-12 09:30:00 -0800460 return connector_status_connected;
461
Jesse Barnesb42d4c52009-09-10 15:28:04 -0700462 return status;
Jesse Barnes79e53942008-11-07 14:24:08 -0800463}
464
465/**
466 * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
467 */
468static int intel_lvds_get_modes(struct drm_connector *connector)
469{
470 struct drm_device *dev = connector->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800471 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -0800472
Zhao Yakuibfac4d62010-04-07 17:11:22 +0800473 if (dev_priv->lvds_edid_good) {
Chris Wilsondf0e9242010-09-09 16:20:55 +0100474 struct intel_encoder *encoder = intel_attached_encoder(connector);
475 int ret = intel_ddc_get_modes(connector, encoder->ddc_bus);
Zhao Yakuibfac4d62010-04-07 17:11:22 +0800476 if (ret)
477 return ret;
478 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800479
480 /* Didn't get an EDID, so
481 * Set wide sync ranges so we get all modes
482 * handed to valid_mode for checking
483 */
484 connector->display_info.min_vfreq = 0;
485 connector->display_info.max_vfreq = 200;
486 connector->display_info.min_hfreq = 0;
487 connector->display_info.max_hfreq = 200;
488
489 if (dev_priv->panel_fixed_mode != NULL) {
490 struct drm_display_mode *mode;
491
Jesse Barnes79e53942008-11-07 14:24:08 -0800492 mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
493 drm_mode_probed_add(connector, mode);
Jesse Barnes79e53942008-11-07 14:24:08 -0800494
495 return 1;
496 }
497
498 return 0;
499}
500
Thomas Bächler0544edf2010-07-02 10:44:23 +0200501static int intel_no_modeset_on_lid_dmi_callback(const struct dmi_system_id *id)
502{
503 DRM_DEBUG_KMS("Skipping forced modeset for %s\n", id->ident);
504 return 1;
505}
506
507/* The GPU hangs up on these systems if modeset is performed on LID open */
508static const struct dmi_system_id intel_no_modeset_on_lid[] = {
509 {
510 .callback = intel_no_modeset_on_lid_dmi_callback,
511 .ident = "Toshiba Tecra A11",
512 .matches = {
513 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
514 DMI_MATCH(DMI_PRODUCT_NAME, "TECRA A11"),
515 },
516 },
517
518 { } /* terminating entry */
519};
520
Linus Torvaldsc9354c82009-11-02 09:29:55 -0800521/*
522 * Lid events. Note the use of 'modeset_on_lid':
523 * - we set it on lid close, and reset it on open
524 * - we use it as a "only once" bit (ie we ignore
525 * duplicate events where it was already properly
526 * set/reset)
527 * - the suspend/resume paths will also set it to
528 * zero, since they restore the mode ("lid open").
529 */
Jesse Barnesc1c7af62009-09-10 15:28:03 -0700530static int intel_lid_notify(struct notifier_block *nb, unsigned long val,
531 void *unused)
532{
533 struct drm_i915_private *dev_priv =
534 container_of(nb, struct drm_i915_private, lid_notifier);
535 struct drm_device *dev = dev_priv->dev;
Zhao Yakuia2565372009-12-11 09:26:11 +0800536 struct drm_connector *connector = dev_priv->int_lvds_connector;
Jesse Barnesc1c7af62009-09-10 15:28:03 -0700537
Zhao Yakuia2565372009-12-11 09:26:11 +0800538 /*
539 * check and update the status of LVDS connector after receiving
540 * the LID nofication event.
541 */
542 if (connector)
543 connector->status = connector->funcs->detect(connector);
Thomas Bächler0544edf2010-07-02 10:44:23 +0200544 /* Don't force modeset on machines where it causes a GPU lockup */
545 if (dmi_check_system(intel_no_modeset_on_lid))
546 return NOTIFY_OK;
Linus Torvaldsc9354c82009-11-02 09:29:55 -0800547 if (!acpi_lid_open()) {
548 dev_priv->modeset_on_lid = 1;
549 return NOTIFY_OK;
Jesse Barnes06891e22009-09-14 10:58:48 -0700550 }
Jesse Barnesc1c7af62009-09-10 15:28:03 -0700551
Linus Torvaldsc9354c82009-11-02 09:29:55 -0800552 if (!dev_priv->modeset_on_lid)
553 return NOTIFY_OK;
554
555 dev_priv->modeset_on_lid = 0;
556
557 mutex_lock(&dev->mode_config.mutex);
558 drm_helper_resume_force_mode(dev);
559 mutex_unlock(&dev->mode_config.mutex);
Jesse Barnes06324192009-09-10 15:28:05 -0700560
Jesse Barnesc1c7af62009-09-10 15:28:03 -0700561 return NOTIFY_OK;
562}
563
Jesse Barnes79e53942008-11-07 14:24:08 -0800564/**
565 * intel_lvds_destroy - unregister and free LVDS structures
566 * @connector: connector to free
567 *
568 * Unregister the DDC bus for this connector then free the driver private
569 * structure.
570 */
571static void intel_lvds_destroy(struct drm_connector *connector)
572{
Jesse Barnesc1c7af62009-09-10 15:28:03 -0700573 struct drm_device *dev = connector->dev;
Jesse Barnesc1c7af62009-09-10 15:28:03 -0700574 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -0800575
Jesse Barnesc1c7af62009-09-10 15:28:03 -0700576 if (dev_priv->lid_notifier.notifier_call)
577 acpi_lid_notifier_unregister(&dev_priv->lid_notifier);
Jesse Barnes79e53942008-11-07 14:24:08 -0800578 drm_sysfs_connector_remove(connector);
579 drm_connector_cleanup(connector);
580 kfree(connector);
581}
582
Jesse Barnes335041e2009-01-22 22:22:06 +1000583static int intel_lvds_set_property(struct drm_connector *connector,
584 struct drm_property *property,
585 uint64_t value)
586{
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800587 struct drm_device *dev = connector->dev;
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800588
589 if (property == dev->mode_config.scaling_mode_property &&
590 connector->encoder) {
591 struct drm_crtc *crtc = connector->encoder->crtc;
Zhenyu Wangbb8a3562010-03-29 16:40:50 +0800592 struct drm_encoder *encoder = connector->encoder;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100593 struct intel_lvds *intel_lvds = enc_to_intel_lvds(encoder);
Zhenyu Wangbb8a3562010-03-29 16:40:50 +0800594
Jesse Barnes53bd8382009-07-01 10:04:40 -0700595 if (value == DRM_MODE_SCALE_NONE) {
596 DRM_DEBUG_KMS("no scaling not supported\n");
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800597 return 0;
598 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100599 if (intel_lvds->fitting_mode == value) {
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800600 /* the LVDS scaling property is not changed */
601 return 0;
602 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100603 intel_lvds->fitting_mode = value;
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800604 if (crtc && crtc->enabled) {
605 /*
606 * If the CRTC is enabled, the display will be changed
607 * according to the new panel fitting mode.
608 */
609 drm_crtc_helper_set_mode(crtc, &crtc->mode,
610 crtc->x, crtc->y, crtc->fb);
611 }
612 }
613
Jesse Barnes335041e2009-01-22 22:22:06 +1000614 return 0;
615}
616
Jesse Barnes79e53942008-11-07 14:24:08 -0800617static const struct drm_encoder_helper_funcs intel_lvds_helper_funcs = {
618 .dpms = intel_lvds_dpms,
619 .mode_fixup = intel_lvds_mode_fixup,
620 .prepare = intel_lvds_prepare,
621 .mode_set = intel_lvds_mode_set,
622 .commit = intel_lvds_commit,
623};
624
625static const struct drm_connector_helper_funcs intel_lvds_connector_helper_funcs = {
626 .get_modes = intel_lvds_get_modes,
627 .mode_valid = intel_lvds_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +0100628 .best_encoder = intel_best_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800629};
630
631static const struct drm_connector_funcs intel_lvds_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700632 .dpms = drm_helper_connector_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -0800633 .detect = intel_lvds_detect,
634 .fill_modes = drm_helper_probe_single_connector_modes,
Jesse Barnes335041e2009-01-22 22:22:06 +1000635 .set_property = intel_lvds_set_property,
Jesse Barnes79e53942008-11-07 14:24:08 -0800636 .destroy = intel_lvds_destroy,
637};
638
Jesse Barnes79e53942008-11-07 14:24:08 -0800639static const struct drm_encoder_funcs intel_lvds_enc_funcs = {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100640 .destroy = intel_encoder_destroy,
Jesse Barnes79e53942008-11-07 14:24:08 -0800641};
642
Jarod Wilson425d2442009-05-05 10:00:25 -0400643static int __init intel_no_lvds_dmi_callback(const struct dmi_system_id *id)
644{
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800645 DRM_DEBUG_KMS("Skipping LVDS initialization for %s\n", id->ident);
Jarod Wilson425d2442009-05-05 10:00:25 -0400646 return 1;
647}
Jesse Barnes79e53942008-11-07 14:24:08 -0800648
Jarod Wilson425d2442009-05-05 10:00:25 -0400649/* These systems claim to have LVDS, but really don't */
Jaswinder Singh Rajput93c05f22009-06-04 09:41:19 +1000650static const struct dmi_system_id intel_no_lvds[] = {
Jarod Wilson425d2442009-05-05 10:00:25 -0400651 {
652 .callback = intel_no_lvds_dmi_callback,
653 .ident = "Apple Mac Mini (Core series)",
654 .matches = {
Keith Packard98acd462009-06-14 12:31:58 -0700655 DMI_MATCH(DMI_SYS_VENDOR, "Apple"),
Jarod Wilson425d2442009-05-05 10:00:25 -0400656 DMI_MATCH(DMI_PRODUCT_NAME, "Macmini1,1"),
657 },
658 },
659 {
660 .callback = intel_no_lvds_dmi_callback,
661 .ident = "Apple Mac Mini (Core 2 series)",
662 .matches = {
Keith Packard98acd462009-06-14 12:31:58 -0700663 DMI_MATCH(DMI_SYS_VENDOR, "Apple"),
Jarod Wilson425d2442009-05-05 10:00:25 -0400664 DMI_MATCH(DMI_PRODUCT_NAME, "Macmini2,1"),
665 },
666 },
667 {
668 .callback = intel_no_lvds_dmi_callback,
669 .ident = "MSI IM-945GSE-A",
670 .matches = {
671 DMI_MATCH(DMI_SYS_VENDOR, "MSI"),
672 DMI_MATCH(DMI_PRODUCT_NAME, "A9830IMS"),
673 },
674 },
675 {
676 .callback = intel_no_lvds_dmi_callback,
677 .ident = "Dell Studio Hybrid",
678 .matches = {
679 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
680 DMI_MATCH(DMI_PRODUCT_NAME, "Studio Hybrid 140g"),
681 },
682 },
Jarod Wilson70aa96c2009-05-27 17:20:39 -0400683 {
684 .callback = intel_no_lvds_dmi_callback,
685 .ident = "AOpen Mini PC",
686 .matches = {
687 DMI_MATCH(DMI_SYS_VENDOR, "AOpen"),
688 DMI_MATCH(DMI_PRODUCT_NAME, "i965GMx-IF"),
689 },
690 },
Michael Cousinfa0864b2009-06-05 21:16:22 +0200691 {
692 .callback = intel_no_lvds_dmi_callback,
Tormod Voldened8c7542009-07-13 22:26:48 +0200693 .ident = "AOpen Mini PC MP915",
694 .matches = {
695 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
696 DMI_MATCH(DMI_BOARD_NAME, "i915GMx-F"),
697 },
698 },
699 {
700 .callback = intel_no_lvds_dmi_callback,
Michael Cousinfa0864b2009-06-05 21:16:22 +0200701 .ident = "Aopen i945GTt-VFA",
702 .matches = {
703 DMI_MATCH(DMI_PRODUCT_VERSION, "AO00001JW"),
704 },
705 },
Stefan Bader9875557ee82010-03-29 17:53:12 +0200706 {
707 .callback = intel_no_lvds_dmi_callback,
708 .ident = "Clientron U800",
709 .matches = {
710 DMI_MATCH(DMI_SYS_VENDOR, "Clientron"),
711 DMI_MATCH(DMI_PRODUCT_NAME, "U800"),
712 },
713 },
Jarod Wilson425d2442009-05-05 10:00:25 -0400714
715 { } /* terminating entry */
716};
Jesse Barnes79e53942008-11-07 14:24:08 -0800717
718/**
Zhao Yakui18f9ed12009-11-20 03:24:16 +0000719 * intel_find_lvds_downclock - find the reduced downclock for LVDS in EDID
720 * @dev: drm device
721 * @connector: LVDS connector
722 *
723 * Find the reduced downclock for LVDS in EDID.
724 */
725static void intel_find_lvds_downclock(struct drm_device *dev,
726 struct drm_connector *connector)
727{
728 struct drm_i915_private *dev_priv = dev->dev_private;
729 struct drm_display_mode *scan, *panel_fixed_mode;
730 int temp_downclock;
731
732 panel_fixed_mode = dev_priv->panel_fixed_mode;
733 temp_downclock = panel_fixed_mode->clock;
734
735 mutex_lock(&dev->mode_config.mutex);
736 list_for_each_entry(scan, &connector->probed_modes, head) {
737 /*
738 * If one mode has the same resolution with the fixed_panel
739 * mode while they have the different refresh rate, it means
740 * that the reduced downclock is found for the LVDS. In such
741 * case we can set the different FPx0/1 to dynamically select
742 * between low and high frequency.
743 */
744 if (scan->hdisplay == panel_fixed_mode->hdisplay &&
745 scan->hsync_start == panel_fixed_mode->hsync_start &&
746 scan->hsync_end == panel_fixed_mode->hsync_end &&
747 scan->htotal == panel_fixed_mode->htotal &&
748 scan->vdisplay == panel_fixed_mode->vdisplay &&
749 scan->vsync_start == panel_fixed_mode->vsync_start &&
750 scan->vsync_end == panel_fixed_mode->vsync_end &&
751 scan->vtotal == panel_fixed_mode->vtotal) {
752 if (scan->clock < temp_downclock) {
753 /*
754 * The downclock is already found. But we
755 * expect to find the lower downclock.
756 */
757 temp_downclock = scan->clock;
758 }
759 }
760 }
761 mutex_unlock(&dev->mode_config.mutex);
Jesse Barnes33814342010-01-14 20:48:02 +0000762 if (temp_downclock < panel_fixed_mode->clock &&
763 i915_lvds_downclock) {
Zhao Yakui18f9ed12009-11-20 03:24:16 +0000764 /* We found the downclock for LVDS. */
765 dev_priv->lvds_downclock_avail = 1;
766 dev_priv->lvds_downclock = temp_downclock;
767 DRM_DEBUG_KMS("LVDS downclock is found in EDID. "
768 "Normal clock %dKhz, downclock %dKhz\n",
769 panel_fixed_mode->clock, temp_downclock);
770 }
771 return;
772}
773
Zhao Yakui7cf4f692009-11-24 09:48:47 +0800774/*
775 * Enumerate the child dev array parsed from VBT to check whether
776 * the LVDS is present.
777 * If it is present, return 1.
778 * If it is not present, return false.
779 * If no child dev is parsed from VBT, it assumes that the LVDS is present.
Zhao Yakui7cf4f692009-11-24 09:48:47 +0800780 */
Chris Wilson425904d2010-08-22 18:21:42 +0100781static bool lvds_is_present_in_vbt(struct drm_device *dev)
Zhao Yakui7cf4f692009-11-24 09:48:47 +0800782{
783 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson425904d2010-08-22 18:21:42 +0100784 int i;
Zhao Yakui7cf4f692009-11-24 09:48:47 +0800785
786 if (!dev_priv->child_dev_num)
Chris Wilson425904d2010-08-22 18:21:42 +0100787 return true;
Zhao Yakui7cf4f692009-11-24 09:48:47 +0800788
Zhao Yakui7cf4f692009-11-24 09:48:47 +0800789 for (i = 0; i < dev_priv->child_dev_num; i++) {
Chris Wilson425904d2010-08-22 18:21:42 +0100790 struct child_device_config *child = dev_priv->child_dev + i;
791
792 /* If the device type is not LFP, continue.
793 * We have to check both the new identifiers as well as the
794 * old for compatibility with some BIOSes.
Zhao Yakui7cf4f692009-11-24 09:48:47 +0800795 */
Chris Wilson425904d2010-08-22 18:21:42 +0100796 if (child->device_type != DEVICE_TYPE_INT_LFP &&
797 child->device_type != DEVICE_TYPE_LFP)
Zhao Yakui7cf4f692009-11-24 09:48:47 +0800798 continue;
799
Chris Wilson425904d2010-08-22 18:21:42 +0100800 /* However, we cannot trust the BIOS writers to populate
801 * the VBT correctly. Since LVDS requires additional
802 * information from AIM blocks, a non-zero addin offset is
803 * a good indicator that the LVDS is actually present.
Zhao Yakui7cf4f692009-11-24 09:48:47 +0800804 */
Chris Wilson425904d2010-08-22 18:21:42 +0100805 if (child->addin_offset)
806 return true;
807
808 /* But even then some BIOS writers perform some black magic
809 * and instantiate the device without reference to any
810 * additional data. Trust that if the VBT was written into
811 * the OpRegion then they have validated the LVDS's existence.
812 */
813 if (dev_priv->opregion.vbt)
814 return true;
Zhao Yakui7cf4f692009-11-24 09:48:47 +0800815 }
Chris Wilson425904d2010-08-22 18:21:42 +0100816
817 return false;
Zhao Yakui7cf4f692009-11-24 09:48:47 +0800818}
819
Zhao Yakui18f9ed12009-11-20 03:24:16 +0000820/**
Jesse Barnes79e53942008-11-07 14:24:08 -0800821 * intel_lvds_init - setup LVDS connectors on this device
822 * @dev: drm device
823 *
824 * Create the connector, register the LVDS DDC bus, and try to figure out what
825 * modes we can display on the LVDS panel (if present).
826 */
827void intel_lvds_init(struct drm_device *dev)
828{
829 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100830 struct intel_lvds *intel_lvds;
Eric Anholt21d40d32010-03-25 11:11:14 -0700831 struct intel_encoder *intel_encoder;
Zhenyu Wangbb8a3562010-03-29 16:40:50 +0800832 struct intel_connector *intel_connector;
Jesse Barnes79e53942008-11-07 14:24:08 -0800833 struct drm_connector *connector;
834 struct drm_encoder *encoder;
835 struct drm_display_mode *scan; /* *modes, *bios_mode; */
836 struct drm_crtc *crtc;
837 u32 lvds;
Zhenyu Wang541998a2009-06-05 15:38:44 +0800838 int pipe, gpio = GPIOC;
Jesse Barnes79e53942008-11-07 14:24:08 -0800839
Jarod Wilson425d2442009-05-05 10:00:25 -0400840 /* Skip init on machines we know falsely report LVDS */
841 if (dmi_check_system(intel_no_lvds))
Paul Collins565dcd42009-02-04 23:05:41 +1300842 return;
Paul Collins565dcd42009-02-04 23:05:41 +1300843
Matthew Garrett11ba1592009-12-15 13:55:24 -0500844 if (!lvds_is_present_in_vbt(dev)) {
845 DRM_DEBUG_KMS("LVDS is not present in VBT\n");
Zhao Yakuie99da352009-06-26 09:46:18 +0800846 return;
Zhao Yakui7cf4f692009-11-24 09:48:47 +0800847 }
Zhao Yakuie99da352009-06-26 09:46:18 +0800848
Eric Anholtc619eed2010-01-28 16:45:52 -0800849 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang541998a2009-06-05 15:38:44 +0800850 if ((I915_READ(PCH_LVDS) & LVDS_DETECTED) == 0)
851 return;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800852 if (dev_priv->edp_support) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800853 DRM_DEBUG_KMS("disable LVDS for eDP support\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800854 return;
855 }
Zhenyu Wang541998a2009-06-05 15:38:44 +0800856 gpio = PCH_GPIOC;
857 }
858
Chris Wilsonea5b2132010-08-04 13:50:23 +0100859 intel_lvds = kzalloc(sizeof(struct intel_lvds), GFP_KERNEL);
860 if (!intel_lvds) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800861 return;
862 }
863
Zhenyu Wangbb8a3562010-03-29 16:40:50 +0800864 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
865 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100866 kfree(intel_lvds);
Zhenyu Wangbb8a3562010-03-29 16:40:50 +0800867 return;
868 }
869
Chris Wilsone9e331a2010-09-13 01:16:10 +0100870 if (!HAS_PCH_SPLIT(dev)) {
871 intel_lvds->pfit_control = I915_READ(PFIT_CONTROL);
872 }
873
Chris Wilsonea5b2132010-08-04 13:50:23 +0100874 intel_encoder = &intel_lvds->base;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100875 encoder = &intel_encoder->base;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100876 connector = &intel_connector->base;
Zhenyu Wangbb8a3562010-03-29 16:40:50 +0800877 drm_connector_init(dev, &intel_connector->base, &intel_lvds_connector_funcs,
Jesse Barnes79e53942008-11-07 14:24:08 -0800878 DRM_MODE_CONNECTOR_LVDS);
879
Chris Wilson4ef69c72010-09-09 15:14:28 +0100880 drm_encoder_init(dev, &intel_encoder->base, &intel_lvds_enc_funcs,
Jesse Barnes79e53942008-11-07 14:24:08 -0800881 DRM_MODE_ENCODER_LVDS);
882
Chris Wilsondf0e9242010-09-09 16:20:55 +0100883 intel_connector_attach_encoder(intel_connector, intel_encoder);
Eric Anholt21d40d32010-03-25 11:11:14 -0700884 intel_encoder->type = INTEL_OUTPUT_LVDS;
Jesse Barnes79e53942008-11-07 14:24:08 -0800885
Eric Anholt21d40d32010-03-25 11:11:14 -0700886 intel_encoder->clone_mask = (1 << INTEL_LVDS_CLONE_BIT);
887 intel_encoder->crtc_mask = (1 << 1);
Jesse Barnes79e53942008-11-07 14:24:08 -0800888 drm_encoder_helper_add(encoder, &intel_lvds_helper_funcs);
889 drm_connector_helper_add(connector, &intel_lvds_connector_helper_funcs);
890 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
891 connector->interlace_allowed = false;
892 connector->doublescan_allowed = false;
893
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800894 /* create the scaling mode property */
895 drm_mode_create_scaling_mode_property(dev);
896 /*
897 * the initial panel fitting mode will be FULL_SCREEN.
898 */
Jesse Barnes79e53942008-11-07 14:24:08 -0800899
Zhenyu Wangbb8a3562010-03-29 16:40:50 +0800900 drm_connector_attach_property(&intel_connector->base,
Zhao Yakui3fbe18d2009-06-22 15:31:25 +0800901 dev->mode_config.scaling_mode_property,
Jesse Barnesdd1ea372010-06-24 11:05:10 -0700902 DRM_MODE_SCALE_ASPECT);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100903 intel_lvds->fitting_mode = DRM_MODE_SCALE_ASPECT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800904 /*
905 * LVDS discovery:
906 * 1) check for EDID on DDC
907 * 2) check for VBT data
908 * 3) check to see if LVDS is already on
909 * if none of the above, no panel
910 * 4) make sure lid is open
911 * if closed, act like it's not there for now
912 */
913
914 /* Set up the DDC bus. */
Eric Anholt21d40d32010-03-25 11:11:14 -0700915 intel_encoder->ddc_bus = intel_i2c_create(dev, gpio, "LVDSDDC_C");
916 if (!intel_encoder->ddc_bus) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800917 dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
918 "failed.\n");
919 goto failed;
920 }
921
922 /*
923 * Attempt to get the fixed panel mode from DDC. Assume that the
924 * preferred mode is the right one.
925 */
Zhao Yakuibfac4d62010-04-07 17:11:22 +0800926 dev_priv->lvds_edid_good = true;
927
Zhenyu Wang335af9a2010-03-30 14:39:31 +0800928 if (!intel_ddc_get_modes(connector, intel_encoder->ddc_bus))
Zhao Yakuibfac4d62010-04-07 17:11:22 +0800929 dev_priv->lvds_edid_good = false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800930
931 list_for_each_entry(scan, &connector->probed_modes, head) {
932 mutex_lock(&dev->mode_config.mutex);
933 if (scan->type & DRM_MODE_TYPE_PREFERRED) {
934 dev_priv->panel_fixed_mode =
935 drm_mode_duplicate(dev, scan);
936 mutex_unlock(&dev->mode_config.mutex);
Zhao Yakui18f9ed12009-11-20 03:24:16 +0000937 intel_find_lvds_downclock(dev, connector);
Paul Collins565dcd42009-02-04 23:05:41 +1300938 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -0800939 }
940 mutex_unlock(&dev->mode_config.mutex);
941 }
942
943 /* Failed to get EDID, what about VBT? */
Ma Ling88631702009-05-13 11:19:55 +0800944 if (dev_priv->lfp_lvds_vbt_mode) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800945 mutex_lock(&dev->mode_config.mutex);
946 dev_priv->panel_fixed_mode =
Ma Ling88631702009-05-13 11:19:55 +0800947 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -0800948 mutex_unlock(&dev->mode_config.mutex);
Jesse Barnese285f3cd2009-01-14 10:53:36 -0800949 if (dev_priv->panel_fixed_mode) {
950 dev_priv->panel_fixed_mode->type |=
951 DRM_MODE_TYPE_PREFERRED;
Jesse Barnese285f3cd2009-01-14 10:53:36 -0800952 goto out;
953 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800954 }
955
956 /*
957 * If we didn't get EDID, try checking if the panel is already turned
958 * on. If so, assume that whatever is currently programmed is the
959 * correct mode.
960 */
Zhenyu Wang541998a2009-06-05 15:38:44 +0800961
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500962 /* Ironlake: FIXME if still fail, not try pipe mode now */
Eric Anholtc619eed2010-01-28 16:45:52 -0800963 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang541998a2009-06-05 15:38:44 +0800964 goto failed;
965
Jesse Barnes79e53942008-11-07 14:24:08 -0800966 lvds = I915_READ(LVDS);
967 pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
Chris Wilsonf875c152010-09-09 15:44:14 +0100968 crtc = intel_get_crtc_for_pipe(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -0800969
970 if (crtc && (lvds & LVDS_PORT_EN)) {
971 dev_priv->panel_fixed_mode = intel_crtc_mode_get(dev, crtc);
972 if (dev_priv->panel_fixed_mode) {
973 dev_priv->panel_fixed_mode->type |=
974 DRM_MODE_TYPE_PREFERRED;
Paul Collins565dcd42009-02-04 23:05:41 +1300975 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -0800976 }
977 }
978
979 /* If we still don't have a mode after all that, give up. */
980 if (!dev_priv->panel_fixed_mode)
981 goto failed;
982
Jesse Barnes79e53942008-11-07 14:24:08 -0800983out:
Eric Anholtc619eed2010-01-28 16:45:52 -0800984 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang541998a2009-06-05 15:38:44 +0800985 u32 pwm;
986 /* make sure PWM is enabled */
987 pwm = I915_READ(BLC_PWM_CPU_CTL2);
988 pwm |= (PWM_ENABLE | PWM_PIPE_B);
989 I915_WRITE(BLC_PWM_CPU_CTL2, pwm);
990
991 pwm = I915_READ(BLC_PWM_PCH_CTL1);
992 pwm |= PWM_PCH_ENABLE;
993 I915_WRITE(BLC_PWM_PCH_CTL1, pwm);
994 }
Jesse Barnesc1c7af62009-09-10 15:28:03 -0700995 dev_priv->lid_notifier.notifier_call = intel_lid_notify;
996 if (acpi_lid_notifier_register(&dev_priv->lid_notifier)) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800997 DRM_DEBUG_KMS("lid notifier registration failed\n");
Jesse Barnesc1c7af62009-09-10 15:28:03 -0700998 dev_priv->lid_notifier.notifier_call = NULL;
999 }
Zhao Yakuia2565372009-12-11 09:26:11 +08001000 /* keep the LVDS connector */
1001 dev_priv->int_lvds_connector = connector;
Jesse Barnes79e53942008-11-07 14:24:08 -08001002 drm_sysfs_connector_add(connector);
1003 return;
1004
1005failed:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001006 DRM_DEBUG_KMS("No LVDS modes found, disabling.\n");
Eric Anholt21d40d32010-03-25 11:11:14 -07001007 if (intel_encoder->ddc_bus)
1008 intel_i2c_destroy(intel_encoder->ddc_bus);
Jesse Barnes79e53942008-11-07 14:24:08 -08001009 drm_connector_cleanup(connector);
Shaohua Li1991bdf2009-11-17 17:19:23 +08001010 drm_encoder_cleanup(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01001011 kfree(intel_lvds);
Zhenyu Wangbb8a3562010-03-29 16:40:50 +08001012 kfree(intel_connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001013}