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/dmi.h>
31 #include <linux/i2c.h>
36 #include "intel_drv.h"
41 * Sets the backlight level.
43 * \param level backlight level, from 0 to intel_lvds_get_max_backlight().
45 static void intel_lvds_set_backlight(struct drm_device *dev, int level)
47 struct drm_i915_private *dev_priv = dev->dev_private;
51 reg = BLC_PWM_CPU_CTL;
55 blc_pwm_ctl = I915_READ(reg) & ~BACKLIGHT_DUTY_CYCLE_MASK;
56 I915_WRITE(reg, (blc_pwm_ctl |
57 (level << BACKLIGHT_DUTY_CYCLE_SHIFT)));
61 * Returns the maximum level of the backlight duty cycle field.
63 static u32 intel_lvds_get_max_backlight(struct drm_device *dev)
65 struct drm_i915_private *dev_priv = dev->dev_private;
69 reg = BLC_PWM_PCH_CTL2;
73 return ((I915_READ(reg) & BACKLIGHT_MODULATION_FREQ_MASK) >>
74 BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
78 * Sets the power state for the panel.
80 static void intel_lvds_set_power(struct drm_device *dev, bool on)
82 struct drm_i915_private *dev_priv = dev->dev_private;
83 u32 pp_status, ctl_reg, status_reg;
86 ctl_reg = PCH_PP_CONTROL;
87 status_reg = PCH_PP_STATUS;
90 status_reg = PP_STATUS;
94 I915_WRITE(ctl_reg, I915_READ(ctl_reg) |
97 pp_status = I915_READ(status_reg);
98 } while ((pp_status & PP_ON) == 0);
100 intel_lvds_set_backlight(dev, dev_priv->backlight_duty_cycle);
102 intel_lvds_set_backlight(dev, 0);
104 I915_WRITE(ctl_reg, I915_READ(ctl_reg) &
107 pp_status = I915_READ(status_reg);
108 } while (pp_status & PP_ON);
112 static void intel_lvds_dpms(struct drm_encoder *encoder, int mode)
114 struct drm_device *dev = encoder->dev;
116 if (mode == DRM_MODE_DPMS_ON)
117 intel_lvds_set_power(dev, true);
119 intel_lvds_set_power(dev, false);
121 /* XXX: We never power down the LVDS pairs. */
124 static void intel_lvds_save(struct drm_connector *connector)
126 struct drm_device *dev = connector->dev;
127 struct drm_i915_private *dev_priv = dev->dev_private;
128 u32 pp_on_reg, pp_off_reg, pp_ctl_reg, pp_div_reg;
132 pp_on_reg = PCH_PP_ON_DELAYS;
133 pp_off_reg = PCH_PP_OFF_DELAYS;
134 pp_ctl_reg = PCH_PP_CONTROL;
135 pp_div_reg = PCH_PP_DIVISOR;
136 pwm_ctl_reg = BLC_PWM_CPU_CTL;
138 pp_on_reg = PP_ON_DELAYS;
139 pp_off_reg = PP_OFF_DELAYS;
140 pp_ctl_reg = PP_CONTROL;
141 pp_div_reg = PP_DIVISOR;
142 pwm_ctl_reg = BLC_PWM_CTL;
145 dev_priv->savePP_ON = I915_READ(pp_on_reg);
146 dev_priv->savePP_OFF = I915_READ(pp_off_reg);
147 dev_priv->savePP_CONTROL = I915_READ(pp_ctl_reg);
148 dev_priv->savePP_DIVISOR = I915_READ(pp_div_reg);
149 dev_priv->saveBLC_PWM_CTL = I915_READ(pwm_ctl_reg);
150 dev_priv->backlight_duty_cycle = (dev_priv->saveBLC_PWM_CTL &
151 BACKLIGHT_DUTY_CYCLE_MASK);
154 * If the light is off at server startup, just make it full brightness
156 if (dev_priv->backlight_duty_cycle == 0)
157 dev_priv->backlight_duty_cycle =
158 intel_lvds_get_max_backlight(dev);
161 static void intel_lvds_restore(struct drm_connector *connector)
163 struct drm_device *dev = connector->dev;
164 struct drm_i915_private *dev_priv = dev->dev_private;
165 u32 pp_on_reg, pp_off_reg, pp_ctl_reg, pp_div_reg;
169 pp_on_reg = PCH_PP_ON_DELAYS;
170 pp_off_reg = PCH_PP_OFF_DELAYS;
171 pp_ctl_reg = PCH_PP_CONTROL;
172 pp_div_reg = PCH_PP_DIVISOR;
173 pwm_ctl_reg = BLC_PWM_CPU_CTL;
175 pp_on_reg = PP_ON_DELAYS;
176 pp_off_reg = PP_OFF_DELAYS;
177 pp_ctl_reg = PP_CONTROL;
178 pp_div_reg = PP_DIVISOR;
179 pwm_ctl_reg = BLC_PWM_CTL;
182 I915_WRITE(pwm_ctl_reg, dev_priv->saveBLC_PWM_CTL);
183 I915_WRITE(pp_on_reg, dev_priv->savePP_ON);
184 I915_WRITE(pp_off_reg, dev_priv->savePP_OFF);
185 I915_WRITE(pp_div_reg, dev_priv->savePP_DIVISOR);
186 I915_WRITE(pp_ctl_reg, dev_priv->savePP_CONTROL);
187 if (dev_priv->savePP_CONTROL & POWER_TARGET_ON)
188 intel_lvds_set_power(dev, true);
190 intel_lvds_set_power(dev, false);
193 static int intel_lvds_mode_valid(struct drm_connector *connector,
194 struct drm_display_mode *mode)
196 struct drm_device *dev = connector->dev;
197 struct drm_i915_private *dev_priv = dev->dev_private;
198 struct drm_display_mode *fixed_mode = dev_priv->panel_fixed_mode;
201 if (mode->hdisplay > fixed_mode->hdisplay)
203 if (mode->vdisplay > fixed_mode->vdisplay)
210 static bool intel_lvds_mode_fixup(struct drm_encoder *encoder,
211 struct drm_display_mode *mode,
212 struct drm_display_mode *adjusted_mode)
214 struct drm_device *dev = encoder->dev;
215 struct drm_i915_private *dev_priv = dev->dev_private;
216 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
217 struct drm_encoder *tmp_encoder;
219 /* Should never happen!! */
220 if (!IS_I965G(dev) && intel_crtc->pipe == 0) {
221 printk(KERN_ERR "Can't support LVDS on pipe A\n");
225 /* Should never happen!! */
226 list_for_each_entry(tmp_encoder, &dev->mode_config.encoder_list, head) {
227 if (tmp_encoder != encoder && tmp_encoder->crtc == encoder->crtc) {
228 printk(KERN_ERR "Can't enable LVDS and another "
229 "encoder on the same pipe\n");
235 * If we have timings from the BIOS for the panel, put them in
236 * to the adjusted mode. The CRTC will be set up for this mode,
237 * with the panel scaling set up to source from the H/VDisplay
238 * of the original mode.
240 if (dev_priv->panel_fixed_mode != NULL) {
241 adjusted_mode->hdisplay = dev_priv->panel_fixed_mode->hdisplay;
242 adjusted_mode->hsync_start =
243 dev_priv->panel_fixed_mode->hsync_start;
244 adjusted_mode->hsync_end =
245 dev_priv->panel_fixed_mode->hsync_end;
246 adjusted_mode->htotal = dev_priv->panel_fixed_mode->htotal;
247 adjusted_mode->vdisplay = dev_priv->panel_fixed_mode->vdisplay;
248 adjusted_mode->vsync_start =
249 dev_priv->panel_fixed_mode->vsync_start;
250 adjusted_mode->vsync_end =
251 dev_priv->panel_fixed_mode->vsync_end;
252 adjusted_mode->vtotal = dev_priv->panel_fixed_mode->vtotal;
253 adjusted_mode->clock = dev_priv->panel_fixed_mode->clock;
254 drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
258 * XXX: It would be nice to support lower refresh rates on the
259 * panels to reduce power consumption, and perhaps match the
260 * user's requested refresh rate.
266 static void intel_lvds_prepare(struct drm_encoder *encoder)
268 struct drm_device *dev = encoder->dev;
269 struct drm_i915_private *dev_priv = dev->dev_private;
273 reg = BLC_PWM_CPU_CTL;
277 dev_priv->saveBLC_PWM_CTL = I915_READ(reg);
278 dev_priv->backlight_duty_cycle = (dev_priv->saveBLC_PWM_CTL &
279 BACKLIGHT_DUTY_CYCLE_MASK);
281 intel_lvds_set_power(dev, false);
284 static void intel_lvds_commit( struct drm_encoder *encoder)
286 struct drm_device *dev = encoder->dev;
287 struct drm_i915_private *dev_priv = dev->dev_private;
289 if (dev_priv->backlight_duty_cycle == 0)
290 dev_priv->backlight_duty_cycle =
291 intel_lvds_get_max_backlight(dev);
293 intel_lvds_set_power(dev, true);
296 static void intel_lvds_mode_set(struct drm_encoder *encoder,
297 struct drm_display_mode *mode,
298 struct drm_display_mode *adjusted_mode)
300 struct drm_device *dev = encoder->dev;
301 struct drm_i915_private *dev_priv = dev->dev_private;
302 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
306 * The LVDS pin pair will already have been turned on in the
307 * intel_crtc_mode_set since it has a large impact on the DPLL
311 /* No panel fitting yet, fixme */
316 * Enable automatic panel scaling so that non-native modes fill the
317 * screen. Should be enabled before the pipe is enabled, according to
318 * register description and PRM.
320 if (mode->hdisplay != adjusted_mode->hdisplay ||
321 mode->vdisplay != adjusted_mode->vdisplay)
322 pfit_control = (PFIT_ENABLE | VERT_AUTO_SCALE |
323 HORIZ_AUTO_SCALE | VERT_INTERP_BILINEAR |
324 HORIZ_INTERP_BILINEAR);
328 if (!IS_I965G(dev)) {
329 if (dev_priv->panel_wants_dither || dev_priv->lvds_dither)
330 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
333 pfit_control |= intel_crtc->pipe << PFIT_PIPE_SHIFT;
335 I915_WRITE(PFIT_CONTROL, pfit_control);
339 * Detect the LVDS connection.
341 * This always returns CONNECTOR_STATUS_CONNECTED. This connector should only have
342 * been set up if the LVDS was actually connected anyway.
344 static enum drm_connector_status intel_lvds_detect(struct drm_connector *connector)
346 return connector_status_connected;
350 * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
352 static int intel_lvds_get_modes(struct drm_connector *connector)
354 struct drm_device *dev = connector->dev;
355 struct intel_output *intel_output = to_intel_output(connector);
356 struct drm_i915_private *dev_priv = dev->dev_private;
359 ret = intel_ddc_get_modes(intel_output);
364 /* Didn't get an EDID, so
365 * Set wide sync ranges so we get all modes
366 * handed to valid_mode for checking
368 connector->display_info.min_vfreq = 0;
369 connector->display_info.max_vfreq = 200;
370 connector->display_info.min_hfreq = 0;
371 connector->display_info.max_hfreq = 200;
373 if (dev_priv->panel_fixed_mode != NULL) {
374 struct drm_display_mode *mode;
376 mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
377 drm_mode_probed_add(connector, mode);
386 * intel_lvds_destroy - unregister and free LVDS structures
387 * @connector: connector to free
389 * Unregister the DDC bus for this connector then free the driver private
392 static void intel_lvds_destroy(struct drm_connector *connector)
394 struct intel_output *intel_output = to_intel_output(connector);
396 if (intel_output->ddc_bus)
397 intel_i2c_destroy(intel_output->ddc_bus);
398 drm_sysfs_connector_remove(connector);
399 drm_connector_cleanup(connector);
403 static int intel_lvds_set_property(struct drm_connector *connector,
404 struct drm_property *property,
407 struct drm_device *dev = connector->dev;
409 if (property == dev->mode_config.dpms_property && connector->encoder)
410 intel_lvds_dpms(connector->encoder, (uint32_t)(value & 0xf));
415 static const struct drm_encoder_helper_funcs intel_lvds_helper_funcs = {
416 .dpms = intel_lvds_dpms,
417 .mode_fixup = intel_lvds_mode_fixup,
418 .prepare = intel_lvds_prepare,
419 .mode_set = intel_lvds_mode_set,
420 .commit = intel_lvds_commit,
423 static const struct drm_connector_helper_funcs intel_lvds_connector_helper_funcs = {
424 .get_modes = intel_lvds_get_modes,
425 .mode_valid = intel_lvds_mode_valid,
426 .best_encoder = intel_best_encoder,
429 static const struct drm_connector_funcs intel_lvds_connector_funcs = {
430 .save = intel_lvds_save,
431 .restore = intel_lvds_restore,
432 .detect = intel_lvds_detect,
433 .fill_modes = drm_helper_probe_single_connector_modes,
434 .set_property = intel_lvds_set_property,
435 .destroy = intel_lvds_destroy,
439 static void intel_lvds_enc_destroy(struct drm_encoder *encoder)
441 drm_encoder_cleanup(encoder);
444 static const struct drm_encoder_funcs intel_lvds_enc_funcs = {
445 .destroy = intel_lvds_enc_destroy,
448 static int __init intel_no_lvds_dmi_callback(const struct dmi_system_id *id)
450 DRM_DEBUG("Skipping LVDS initialization for %s\n", id->ident);
454 /* These systems claim to have LVDS, but really don't */
455 static const struct dmi_system_id __initdata intel_no_lvds[] = {
457 .callback = intel_no_lvds_dmi_callback,
458 .ident = "Apple Mac Mini (Core series)",
460 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
461 DMI_MATCH(DMI_PRODUCT_NAME, "Macmini1,1"),
465 .callback = intel_no_lvds_dmi_callback,
466 .ident = "Apple Mac Mini (Core 2 series)",
468 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
469 DMI_MATCH(DMI_PRODUCT_NAME, "Macmini2,1"),
473 .callback = intel_no_lvds_dmi_callback,
474 .ident = "MSI IM-945GSE-A",
476 DMI_MATCH(DMI_SYS_VENDOR, "MSI"),
477 DMI_MATCH(DMI_PRODUCT_NAME, "A9830IMS"),
481 .callback = intel_no_lvds_dmi_callback,
482 .ident = "Dell Studio Hybrid",
484 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
485 DMI_MATCH(DMI_PRODUCT_NAME, "Studio Hybrid 140g"),
489 /* FIXME: add a check for the Aopen Mini PC */
491 { } /* terminating entry */
495 * intel_lvds_init - setup LVDS connectors on this device
498 * Create the connector, register the LVDS DDC bus, and try to figure out what
499 * modes we can display on the LVDS panel (if present).
501 void intel_lvds_init(struct drm_device *dev)
503 struct drm_i915_private *dev_priv = dev->dev_private;
504 struct intel_output *intel_output;
505 struct drm_connector *connector;
506 struct drm_encoder *encoder;
507 struct drm_display_mode *scan; /* *modes, *bios_mode; */
508 struct drm_crtc *crtc;
510 int pipe, gpio = GPIOC;
512 /* Skip init on machines we know falsely report LVDS */
513 if (dmi_check_system(intel_no_lvds))
517 if ((I915_READ(PCH_LVDS) & LVDS_DETECTED) == 0)
522 intel_output = kzalloc(sizeof(struct intel_output), GFP_KERNEL);
527 connector = &intel_output->base;
528 encoder = &intel_output->enc;
529 drm_connector_init(dev, &intel_output->base, &intel_lvds_connector_funcs,
530 DRM_MODE_CONNECTOR_LVDS);
532 drm_encoder_init(dev, &intel_output->enc, &intel_lvds_enc_funcs,
533 DRM_MODE_ENCODER_LVDS);
535 drm_mode_connector_attach_encoder(&intel_output->base, &intel_output->enc);
536 intel_output->type = INTEL_OUTPUT_LVDS;
538 drm_encoder_helper_add(encoder, &intel_lvds_helper_funcs);
539 drm_connector_helper_add(connector, &intel_lvds_connector_helper_funcs);
540 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
541 connector->interlace_allowed = false;
542 connector->doublescan_allowed = false;
547 * 1) check for EDID on DDC
548 * 2) check for VBT data
549 * 3) check to see if LVDS is already on
550 * if none of the above, no panel
551 * 4) make sure lid is open
552 * if closed, act like it's not there for now
555 /* Set up the DDC bus. */
556 intel_output->ddc_bus = intel_i2c_create(dev, gpio, "LVDSDDC_C");
557 if (!intel_output->ddc_bus) {
558 dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
564 * Attempt to get the fixed panel mode from DDC. Assume that the
565 * preferred mode is the right one.
567 intel_ddc_get_modes(intel_output);
569 list_for_each_entry(scan, &connector->probed_modes, head) {
570 mutex_lock(&dev->mode_config.mutex);
571 if (scan->type & DRM_MODE_TYPE_PREFERRED) {
572 dev_priv->panel_fixed_mode =
573 drm_mode_duplicate(dev, scan);
574 mutex_unlock(&dev->mode_config.mutex);
577 mutex_unlock(&dev->mode_config.mutex);
580 /* Failed to get EDID, what about VBT? */
581 if (dev_priv->lfp_lvds_vbt_mode) {
582 mutex_lock(&dev->mode_config.mutex);
583 dev_priv->panel_fixed_mode =
584 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
585 mutex_unlock(&dev->mode_config.mutex);
586 if (dev_priv->panel_fixed_mode) {
587 dev_priv->panel_fixed_mode->type |=
588 DRM_MODE_TYPE_PREFERRED;
594 * If we didn't get EDID, try checking if the panel is already turned
595 * on. If so, assume that whatever is currently programmed is the
599 /* IGDNG: FIXME if still fail, not try pipe mode now */
603 lvds = I915_READ(LVDS);
604 pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
605 crtc = intel_get_crtc_from_pipe(dev, pipe);
607 if (crtc && (lvds & LVDS_PORT_EN)) {
608 dev_priv->panel_fixed_mode = intel_crtc_mode_get(dev, crtc);
609 if (dev_priv->panel_fixed_mode) {
610 dev_priv->panel_fixed_mode->type |=
611 DRM_MODE_TYPE_PREFERRED;
616 /* If we still don't have a mode after all that, give up. */
617 if (!dev_priv->panel_fixed_mode)
623 /* make sure PWM is enabled */
624 pwm = I915_READ(BLC_PWM_CPU_CTL2);
625 pwm |= (PWM_ENABLE | PWM_PIPE_B);
626 I915_WRITE(BLC_PWM_CPU_CTL2, pwm);
628 pwm = I915_READ(BLC_PWM_PCH_CTL1);
629 pwm |= PWM_PCH_ENABLE;
630 I915_WRITE(BLC_PWM_PCH_CTL1, pwm);
632 drm_sysfs_connector_add(connector);
636 DRM_DEBUG("No LVDS modes found, disabling.\n");
637 if (intel_output->ddc_bus)
638 intel_i2c_destroy(intel_output->ddc_bus);
639 drm_connector_cleanup(connector);