2 * Copyright © 2006-2007 Intel Corporation
3 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
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:
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
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.
25 * Eric Anholt <eric@anholt.net>
26 * Dave Airlie <airlied@linux.ie>
27 * Jesse Barnes <jesse.barnes@intel.com>
30 #include <linux/i2c.h>
35 #include "intel_drv.h"
40 * Sets the backlight level.
42 * \param level backlight level, from 0 to intel_lvds_get_max_backlight().
44 static void intel_lvds_set_backlight(struct drm_device *dev, int level)
46 struct drm_i915_private *dev_priv = dev->dev_private;
49 blc_pwm_ctl = I915_READ(BLC_PWM_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
50 I915_WRITE(BLC_PWM_CTL, (blc_pwm_ctl |
51 (level << BACKLIGHT_DUTY_CYCLE_SHIFT)));
55 * Returns the maximum level of the backlight duty cycle field.
57 static u32 intel_lvds_get_max_backlight(struct drm_device *dev)
59 struct drm_i915_private *dev_priv = dev->dev_private;
61 return ((I915_READ(BLC_PWM_CTL) & BACKLIGHT_MODULATION_FREQ_MASK) >>
62 BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
66 * Sets the power state for the panel.
68 static void intel_lvds_set_power(struct drm_device *dev, bool on)
70 struct drm_i915_private *dev_priv = dev->dev_private;
74 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
77 pp_status = I915_READ(PP_STATUS);
78 } while ((pp_status & PP_ON) == 0);
80 intel_lvds_set_backlight(dev, dev_priv->backlight_duty_cycle);
82 intel_lvds_set_backlight(dev, 0);
84 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) &
87 pp_status = I915_READ(PP_STATUS);
88 } while (pp_status & PP_ON);
92 static void intel_lvds_dpms(struct drm_output *output, int mode)
94 struct drm_device *dev = output->dev;
96 if (mode == DPMSModeOn)
97 intel_lvds_set_power(dev, true);
99 intel_lvds_set_power(dev, false);
101 /* XXX: We never power down the LVDS pairs. */
104 static void intel_lvds_save(struct drm_output *output)
106 struct drm_device *dev = output->dev;
107 struct drm_i915_private *dev_priv = dev->dev_private;
109 dev_priv->savePP_ON = I915_READ(LVDSPP_ON);
110 dev_priv->savePP_OFF = I915_READ(LVDSPP_OFF);
111 dev_priv->savePP_CONTROL = I915_READ(PP_CONTROL);
112 dev_priv->savePP_CYCLE = I915_READ(PP_CYCLE);
113 dev_priv->saveBLC_PWM_CTL = I915_READ(BLC_PWM_CTL);
114 dev_priv->backlight_duty_cycle = (dev_priv->saveBLC_PWM_CTL &
115 BACKLIGHT_DUTY_CYCLE_MASK);
118 * If the light is off at server startup, just make it full brightness
120 if (dev_priv->backlight_duty_cycle == 0)
121 dev_priv->backlight_duty_cycle =
122 intel_lvds_get_max_backlight(dev);
125 static void intel_lvds_restore(struct drm_output *output)
127 struct drm_device *dev = output->dev;
128 struct drm_i915_private *dev_priv = dev->dev_private;
130 I915_WRITE(BLC_PWM_CTL, dev_priv->saveBLC_PWM_CTL);
131 I915_WRITE(LVDSPP_ON, dev_priv->savePP_ON);
132 I915_WRITE(LVDSPP_OFF, dev_priv->savePP_OFF);
133 I915_WRITE(PP_CYCLE, dev_priv->savePP_CYCLE);
134 I915_WRITE(PP_CONTROL, dev_priv->savePP_CONTROL);
135 if (dev_priv->savePP_CONTROL & POWER_TARGET_ON)
136 intel_lvds_set_power(dev, true);
138 intel_lvds_set_power(dev, false);
141 static int intel_lvds_mode_valid(struct drm_output *output,
142 struct drm_display_mode *mode)
144 struct drm_device *dev = output->dev;
145 struct drm_i915_private *dev_priv = dev->dev_private;
146 struct drm_display_mode *fixed_mode = dev_priv->panel_fixed_mode;
149 if (mode->hdisplay > fixed_mode->hdisplay)
151 if (mode->vdisplay > fixed_mode->vdisplay)
158 static bool intel_lvds_mode_fixup(struct drm_output *output,
159 struct drm_display_mode *mode,
160 struct drm_display_mode *adjusted_mode)
162 struct drm_device *dev = output->dev;
163 struct drm_i915_private *dev_priv = dev->dev_private;
164 struct intel_crtc *intel_crtc = output->crtc->driver_private;
165 struct drm_output *tmp_output;
167 /* Should never happen!! */
168 if (!IS_I965G(dev) && intel_crtc->pipe == 0) {
169 printk(KERN_ERR "Can't support LVDS on pipe A\n");
173 /* Should never happen!! */
174 list_for_each_entry(tmp_output, &dev->mode_config.output_list, head) {
175 if (tmp_output != output && tmp_output->crtc == output->crtc) {
176 printk(KERN_ERR "Can't enable LVDS and another "
177 "output on the same pipe\n");
183 * If we have timings from the BIOS for the panel, put them in
184 * to the adjusted mode. The CRTC will be set up for this mode,
185 * with the panel scaling set up to source from the H/VDisplay
186 * of the original mode.
188 if (dev_priv->panel_fixed_mode != NULL) {
189 adjusted_mode->hdisplay = dev_priv->panel_fixed_mode->hdisplay;
190 adjusted_mode->hsync_start =
191 dev_priv->panel_fixed_mode->hsync_start;
192 adjusted_mode->hsync_end =
193 dev_priv->panel_fixed_mode->hsync_end;
194 adjusted_mode->htotal = dev_priv->panel_fixed_mode->htotal;
195 adjusted_mode->vdisplay = dev_priv->panel_fixed_mode->vdisplay;
196 adjusted_mode->vsync_start =
197 dev_priv->panel_fixed_mode->vsync_start;
198 adjusted_mode->vsync_end =
199 dev_priv->panel_fixed_mode->vsync_end;
200 adjusted_mode->vtotal = dev_priv->panel_fixed_mode->vtotal;
201 adjusted_mode->clock = dev_priv->panel_fixed_mode->clock;
202 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
206 * XXX: It would be nice to support lower refresh rates on the
207 * panels to reduce power consumption, and perhaps match the
208 * user's requested refresh rate.
214 static void intel_lvds_prepare(struct drm_output *output)
216 struct drm_device *dev = output->dev;
217 struct drm_i915_private *dev_priv = dev->dev_private;
219 dev_priv->saveBLC_PWM_CTL = I915_READ(BLC_PWM_CTL);
220 dev_priv->backlight_duty_cycle = (dev_priv->saveBLC_PWM_CTL &
221 BACKLIGHT_DUTY_CYCLE_MASK);
223 intel_lvds_set_power(dev, false);
226 static void intel_lvds_commit( struct drm_output *output)
228 struct drm_device *dev = output->dev;
229 struct drm_i915_private *dev_priv = dev->dev_private;
231 if (dev_priv->backlight_duty_cycle == 0)
232 dev_priv->backlight_duty_cycle =
233 intel_lvds_get_max_backlight(dev);
235 intel_lvds_set_power(dev, true);
238 static void intel_lvds_mode_set(struct drm_output *output,
239 struct drm_display_mode *mode,
240 struct drm_display_mode *adjusted_mode)
242 struct drm_device *dev = output->dev;
243 struct drm_i915_private *dev_priv = dev->dev_private;
244 struct intel_crtc *intel_crtc = output->crtc->driver_private;
248 * The LVDS pin pair will already have been turned on in the
249 * intel_crtc_mode_set since it has a large impact on the DPLL
254 * Enable automatic panel scaling so that non-native modes fill the
255 * screen. Should be enabled before the pipe is enabled, according to
256 * register description and PRM.
258 pfit_control = (PFIT_ENABLE | VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
259 VERT_INTERP_BILINEAR | HORIZ_INTERP_BILINEAR);
261 if (!IS_I965G(dev)) {
262 if (dev_priv->panel_wants_dither)
263 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
266 pfit_control |= intel_crtc->pipe << PFIT_PIPE_SHIFT;
268 I915_WRITE(PFIT_CONTROL, pfit_control);
272 * Detect the LVDS connection.
274 * This always returns OUTPUT_STATUS_CONNECTED. This output should only have
275 * been set up if the LVDS was actually connected anyway.
277 static enum drm_output_status intel_lvds_detect(struct drm_output *output)
279 return output_status_connected;
283 * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
285 static int intel_lvds_get_modes(struct drm_output *output)
287 struct drm_device *dev = output->dev;
288 struct drm_i915_private *dev_priv = dev->dev_private;
291 ret = intel_ddc_get_modes(output);
296 /* Didn't get an EDID */
297 if (!output->monitor_info) {
298 struct drm_display_info *dspinfo;
299 dspinfo = kzalloc(sizeof(*output->monitor_info), GFP_KERNEL);
303 /* Set wide sync ranges so we get all modes
304 * handed to valid_mode for checking
306 dspinfo->min_vfreq = 0;
307 dspinfo->max_vfreq = 200;
308 dspinfo->min_hfreq = 0;
309 dspinfo->max_hfreq = 200;
310 output->monitor_info = dspinfo;
314 if (dev_priv->panel_fixed_mode != NULL) {
315 struct drm_display_mode *mode =
316 drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
317 drm_mode_probed_add(output, mode);
325 * intel_lvds_destroy - unregister and free LVDS structures
326 * @output: output to free
328 * Unregister the DDC bus for this output then free the driver private
331 static void intel_lvds_destroy(struct drm_output *output)
333 struct intel_output *intel_output = output->driver_private;
335 intel_i2c_destroy(intel_output->ddc_bus);
336 kfree(output->driver_private);
339 static const struct drm_output_funcs intel_lvds_output_funcs = {
340 .dpms = intel_lvds_dpms,
341 .save = intel_lvds_save,
342 .restore = intel_lvds_restore,
343 .mode_valid = intel_lvds_mode_valid,
344 .mode_fixup = intel_lvds_mode_fixup,
345 .prepare = intel_lvds_prepare,
346 .mode_set = intel_lvds_mode_set,
347 .commit = intel_lvds_commit,
348 .detect = intel_lvds_detect,
349 .get_modes = intel_lvds_get_modes,
350 .cleanup = intel_lvds_destroy
354 * intel_lvds_init - setup LVDS outputs on this device
357 * Create the output, register the LVDS DDC bus, and try to figure out what
358 * modes we can display on the LVDS panel (if present).
360 void intel_lvds_init(struct drm_device *dev)
362 struct drm_i915_private *dev_priv = dev->dev_private;
363 struct drm_output *output;
364 struct intel_output *intel_output;
365 struct drm_display_mode *scan; /* *modes, *bios_mode; */
366 struct drm_crtc *crtc;
370 output = drm_output_create(dev, &intel_lvds_output_funcs, "LVDS");
374 intel_output = kmalloc(sizeof(struct intel_output), GFP_KERNEL);
376 drm_output_destroy(output);
380 intel_output->type = INTEL_OUTPUT_LVDS;
381 output->driver_private = intel_output;
382 output->subpixel_order = SubPixelHorizontalRGB;
383 output->interlace_allowed = FALSE;
384 output->doublescan_allowed = FALSE;
386 /* Set up the DDC bus. */
387 intel_output->ddc_bus = intel_i2c_create(dev, GPIOC, "LVDSDDC_C");
388 if (!intel_output->ddc_bus) {
389 dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
395 * Attempt to get the fixed panel mode from DDC. Assume that the
396 * preferred mode is the right one.
398 intel_ddc_get_modes(output);
400 list_for_each_entry(scan, &output->probed_modes, head) {
401 if (scan->type & DRM_MODE_TYPE_PREFERRED) {
402 dev_priv->panel_fixed_mode =
403 drm_mode_duplicate(dev, scan);
404 goto out; /* FIXME: check for quirks */
409 * If we didn't get EDID, try checking if the panel is already turned
410 * on. If so, assume that whatever is currently programmed is the
413 lvds = I915_READ(LVDS);
414 pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
415 crtc = intel_get_crtc_from_pipe(dev, pipe);
417 if (crtc && (lvds & LVDS_PORT_EN)) {
418 dev_priv->panel_fixed_mode = intel_crtc_mode_get(dev, crtc);
419 if (dev_priv->panel_fixed_mode) {
420 dev_priv->panel_fixed_mode->type |=
421 DRM_MODE_TYPE_PREFERRED;
422 goto out; /* FIXME: check for quirks */
426 /* If we still don't have a mode after all that, give up. */
427 if (!dev_priv->panel_fixed_mode)
430 /* FIXME: probe the BIOS for modes and check for LVDS quirks */
432 /* Get the LVDS fixed mode out of the BIOS. We should support LVDS
433 * with the BIOS being unavailable or broken, but lack the
434 * configuration options for now.
436 bios_mode = intel_bios_get_panel_mode(pScrn);
437 if (bios_mode != NULL) {
438 if (dev_priv->panel_fixed_mode != NULL) {
439 if (dev_priv->debug_modes &&
440 !xf86ModesEqual(dev_priv->panel_fixed_mode,
443 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
444 "BIOS panel mode data doesn't match probed data, "
445 "continuing with probed.\n");
446 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "BIOS mode:\n");
447 xf86PrintModeline(pScrn->scrnIndex, bios_mode);
448 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "probed mode:\n");
449 xf86PrintModeline(pScrn->scrnIndex, dev_priv->panel_fixed_mode);
450 xfree(bios_mode->name);
454 dev_priv->panel_fixed_mode = bios_mode;
457 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
458 "Couldn't detect panel mode. Disabling panel\n");
463 * Blacklist machines with BIOSes that list an LVDS panel without
464 * actually having one.
466 if (dev_priv->PciInfo->chipType == PCI_CHIP_I945_GM) {
468 if (dev_priv->PciInfo->subsysVendor == 0xa0a0)
471 if ((dev_priv->PciInfo->subsysVendor == 0x8086) &&
472 (dev_priv->PciInfo->subsysCard == 0x7270)) {
473 /* It's a Mac Mini or Macbook Pro.
475 * Apple hardware is out to get us. The macbook pro
476 * has a real LVDS panel, but the mac mini does not,
477 * and they have the same device IDs. We'll
478 * distinguish by panel size, on the assumption
479 * that Apple isn't about to make any machines with an
483 if (dev_priv->panel_fixed_mode != NULL &&
484 dev_priv->panel_fixed_mode->HDisplay == 800 &&
485 dev_priv->panel_fixed_mode->VDisplay == 600)
487 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
488 "Suspected Mac Mini, ignoring the LVDS\n");
500 DRM_DEBUG("No LVDS modes found, disabling.\n");
501 drm_output_destroy(output); /* calls intel_lvds_destroy above */