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"
40 #define I915_LVDS "i915_lvds"
43 * Sets the backlight level.
45 * \param level backlight level, from 0 to intel_lvds_get_max_backlight().
47 static void intel_lvds_set_backlight(struct drm_device
*dev
, int level
)
49 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
53 reg
= BLC_PWM_CPU_CTL
;
57 blc_pwm_ctl
= I915_READ(reg
) & ~BACKLIGHT_DUTY_CYCLE_MASK
;
58 I915_WRITE(reg
, (blc_pwm_ctl
|
59 (level
<< BACKLIGHT_DUTY_CYCLE_SHIFT
)));
63 * Returns the maximum level of the backlight duty cycle field.
65 static u32
intel_lvds_get_max_backlight(struct drm_device
*dev
)
67 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
71 reg
= BLC_PWM_PCH_CTL2
;
75 return ((I915_READ(reg
) & BACKLIGHT_MODULATION_FREQ_MASK
) >>
76 BACKLIGHT_MODULATION_FREQ_SHIFT
) * 2;
80 * Sets the power state for the panel.
82 static void intel_lvds_set_power(struct drm_device
*dev
, bool on
)
84 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
85 u32 pp_status
, ctl_reg
, status_reg
;
88 ctl_reg
= PCH_PP_CONTROL
;
89 status_reg
= PCH_PP_STATUS
;
92 status_reg
= PP_STATUS
;
96 I915_WRITE(ctl_reg
, I915_READ(ctl_reg
) |
99 pp_status
= I915_READ(status_reg
);
100 } while ((pp_status
& PP_ON
) == 0);
102 intel_lvds_set_backlight(dev
, dev_priv
->backlight_duty_cycle
);
104 intel_lvds_set_backlight(dev
, 0);
106 I915_WRITE(ctl_reg
, I915_READ(ctl_reg
) &
109 pp_status
= I915_READ(status_reg
);
110 } while (pp_status
& PP_ON
);
114 static void intel_lvds_dpms(struct drm_encoder
*encoder
, int mode
)
116 struct drm_device
*dev
= encoder
->dev
;
118 if (mode
== DRM_MODE_DPMS_ON
)
119 intel_lvds_set_power(dev
, true);
121 intel_lvds_set_power(dev
, false);
123 /* XXX: We never power down the LVDS pairs. */
126 static void intel_lvds_save(struct drm_connector
*connector
)
128 struct drm_device
*dev
= connector
->dev
;
129 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
130 u32 pp_on_reg
, pp_off_reg
, pp_ctl_reg
, pp_div_reg
;
134 pp_on_reg
= PCH_PP_ON_DELAYS
;
135 pp_off_reg
= PCH_PP_OFF_DELAYS
;
136 pp_ctl_reg
= PCH_PP_CONTROL
;
137 pp_div_reg
= PCH_PP_DIVISOR
;
138 pwm_ctl_reg
= BLC_PWM_CPU_CTL
;
140 pp_on_reg
= PP_ON_DELAYS
;
141 pp_off_reg
= PP_OFF_DELAYS
;
142 pp_ctl_reg
= PP_CONTROL
;
143 pp_div_reg
= PP_DIVISOR
;
144 pwm_ctl_reg
= BLC_PWM_CTL
;
147 dev_priv
->savePP_ON
= I915_READ(pp_on_reg
);
148 dev_priv
->savePP_OFF
= I915_READ(pp_off_reg
);
149 dev_priv
->savePP_CONTROL
= I915_READ(pp_ctl_reg
);
150 dev_priv
->savePP_DIVISOR
= I915_READ(pp_div_reg
);
151 dev_priv
->saveBLC_PWM_CTL
= I915_READ(pwm_ctl_reg
);
152 dev_priv
->backlight_duty_cycle
= (dev_priv
->saveBLC_PWM_CTL
&
153 BACKLIGHT_DUTY_CYCLE_MASK
);
156 * If the light is off at server startup, just make it full brightness
158 if (dev_priv
->backlight_duty_cycle
== 0)
159 dev_priv
->backlight_duty_cycle
=
160 intel_lvds_get_max_backlight(dev
);
163 static void intel_lvds_restore(struct drm_connector
*connector
)
165 struct drm_device
*dev
= connector
->dev
;
166 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
167 u32 pp_on_reg
, pp_off_reg
, pp_ctl_reg
, pp_div_reg
;
171 pp_on_reg
= PCH_PP_ON_DELAYS
;
172 pp_off_reg
= PCH_PP_OFF_DELAYS
;
173 pp_ctl_reg
= PCH_PP_CONTROL
;
174 pp_div_reg
= PCH_PP_DIVISOR
;
175 pwm_ctl_reg
= BLC_PWM_CPU_CTL
;
177 pp_on_reg
= PP_ON_DELAYS
;
178 pp_off_reg
= PP_OFF_DELAYS
;
179 pp_ctl_reg
= PP_CONTROL
;
180 pp_div_reg
= PP_DIVISOR
;
181 pwm_ctl_reg
= BLC_PWM_CTL
;
184 I915_WRITE(pwm_ctl_reg
, dev_priv
->saveBLC_PWM_CTL
);
185 I915_WRITE(pp_on_reg
, dev_priv
->savePP_ON
);
186 I915_WRITE(pp_off_reg
, dev_priv
->savePP_OFF
);
187 I915_WRITE(pp_div_reg
, dev_priv
->savePP_DIVISOR
);
188 I915_WRITE(pp_ctl_reg
, dev_priv
->savePP_CONTROL
);
189 if (dev_priv
->savePP_CONTROL
& POWER_TARGET_ON
)
190 intel_lvds_set_power(dev
, true);
192 intel_lvds_set_power(dev
, false);
195 static int intel_lvds_mode_valid(struct drm_connector
*connector
,
196 struct drm_display_mode
*mode
)
198 struct drm_device
*dev
= connector
->dev
;
199 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
200 struct drm_display_mode
*fixed_mode
= dev_priv
->panel_fixed_mode
;
203 if (mode
->hdisplay
> fixed_mode
->hdisplay
)
205 if (mode
->vdisplay
> fixed_mode
->vdisplay
)
212 static bool intel_lvds_mode_fixup(struct drm_encoder
*encoder
,
213 struct drm_display_mode
*mode
,
214 struct drm_display_mode
*adjusted_mode
)
216 struct drm_device
*dev
= encoder
->dev
;
217 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
218 struct intel_crtc
*intel_crtc
= to_intel_crtc(encoder
->crtc
);
219 struct drm_encoder
*tmp_encoder
;
221 /* Should never happen!! */
222 if (!IS_I965G(dev
) && intel_crtc
->pipe
== 0) {
223 printk(KERN_ERR
"Can't support LVDS on pipe A\n");
227 /* Should never happen!! */
228 list_for_each_entry(tmp_encoder
, &dev
->mode_config
.encoder_list
, head
) {
229 if (tmp_encoder
!= encoder
&& tmp_encoder
->crtc
== encoder
->crtc
) {
230 printk(KERN_ERR
"Can't enable LVDS and another "
231 "encoder on the same pipe\n");
237 * If we have timings from the BIOS for the panel, put them in
238 * to the adjusted mode. The CRTC will be set up for this mode,
239 * with the panel scaling set up to source from the H/VDisplay
240 * of the original mode.
242 if (dev_priv
->panel_fixed_mode
!= NULL
) {
243 adjusted_mode
->hdisplay
= dev_priv
->panel_fixed_mode
->hdisplay
;
244 adjusted_mode
->hsync_start
=
245 dev_priv
->panel_fixed_mode
->hsync_start
;
246 adjusted_mode
->hsync_end
=
247 dev_priv
->panel_fixed_mode
->hsync_end
;
248 adjusted_mode
->htotal
= dev_priv
->panel_fixed_mode
->htotal
;
249 adjusted_mode
->vdisplay
= dev_priv
->panel_fixed_mode
->vdisplay
;
250 adjusted_mode
->vsync_start
=
251 dev_priv
->panel_fixed_mode
->vsync_start
;
252 adjusted_mode
->vsync_end
=
253 dev_priv
->panel_fixed_mode
->vsync_end
;
254 adjusted_mode
->vtotal
= dev_priv
->panel_fixed_mode
->vtotal
;
255 adjusted_mode
->clock
= dev_priv
->panel_fixed_mode
->clock
;
256 drm_mode_set_crtcinfo(adjusted_mode
, CRTC_INTERLACE_HALVE_V
);
260 * XXX: It would be nice to support lower refresh rates on the
261 * panels to reduce power consumption, and perhaps match the
262 * user's requested refresh rate.
268 static void intel_lvds_prepare(struct drm_encoder
*encoder
)
270 struct drm_device
*dev
= encoder
->dev
;
271 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
275 reg
= BLC_PWM_CPU_CTL
;
279 dev_priv
->saveBLC_PWM_CTL
= I915_READ(reg
);
280 dev_priv
->backlight_duty_cycle
= (dev_priv
->saveBLC_PWM_CTL
&
281 BACKLIGHT_DUTY_CYCLE_MASK
);
283 intel_lvds_set_power(dev
, false);
286 static void intel_lvds_commit( struct drm_encoder
*encoder
)
288 struct drm_device
*dev
= encoder
->dev
;
289 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
291 if (dev_priv
->backlight_duty_cycle
== 0)
292 dev_priv
->backlight_duty_cycle
=
293 intel_lvds_get_max_backlight(dev
);
295 intel_lvds_set_power(dev
, true);
298 static void intel_lvds_mode_set(struct drm_encoder
*encoder
,
299 struct drm_display_mode
*mode
,
300 struct drm_display_mode
*adjusted_mode
)
302 struct drm_device
*dev
= encoder
->dev
;
303 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
304 struct intel_crtc
*intel_crtc
= to_intel_crtc(encoder
->crtc
);
308 * The LVDS pin pair will already have been turned on in the
309 * intel_crtc_mode_set since it has a large impact on the DPLL
313 /* No panel fitting yet, fixme */
318 * Enable automatic panel scaling so that non-native modes fill the
319 * screen. Should be enabled before the pipe is enabled, according to
320 * register description and PRM.
322 if (mode
->hdisplay
!= adjusted_mode
->hdisplay
||
323 mode
->vdisplay
!= adjusted_mode
->vdisplay
)
324 pfit_control
= (PFIT_ENABLE
| VERT_AUTO_SCALE
|
325 HORIZ_AUTO_SCALE
| VERT_INTERP_BILINEAR
|
326 HORIZ_INTERP_BILINEAR
);
330 if (!IS_I965G(dev
)) {
331 if (dev_priv
->panel_wants_dither
|| dev_priv
->lvds_dither
)
332 pfit_control
|= PANEL_8TO6_DITHER_ENABLE
;
335 pfit_control
|= intel_crtc
->pipe
<< PFIT_PIPE_SHIFT
;
337 I915_WRITE(PFIT_CONTROL
, pfit_control
);
341 * Detect the LVDS connection.
343 * This always returns CONNECTOR_STATUS_CONNECTED. This connector should only have
344 * been set up if the LVDS was actually connected anyway.
346 static enum drm_connector_status
intel_lvds_detect(struct drm_connector
*connector
)
348 return connector_status_connected
;
352 * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
354 static int intel_lvds_get_modes(struct drm_connector
*connector
)
356 struct drm_device
*dev
= connector
->dev
;
357 struct intel_output
*intel_output
= to_intel_output(connector
);
358 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
361 ret
= intel_ddc_get_modes(intel_output
);
366 /* Didn't get an EDID, so
367 * Set wide sync ranges so we get all modes
368 * handed to valid_mode for checking
370 connector
->display_info
.min_vfreq
= 0;
371 connector
->display_info
.max_vfreq
= 200;
372 connector
->display_info
.min_hfreq
= 0;
373 connector
->display_info
.max_hfreq
= 200;
375 if (dev_priv
->panel_fixed_mode
!= NULL
) {
376 struct drm_display_mode
*mode
;
378 mode
= drm_mode_duplicate(dev
, dev_priv
->panel_fixed_mode
);
379 drm_mode_probed_add(connector
, mode
);
388 * intel_lvds_destroy - unregister and free LVDS structures
389 * @connector: connector to free
391 * Unregister the DDC bus for this connector then free the driver private
394 static void intel_lvds_destroy(struct drm_connector
*connector
)
396 struct intel_output
*intel_output
= to_intel_output(connector
);
398 if (intel_output
->ddc_bus
)
399 intel_i2c_destroy(intel_output
->ddc_bus
);
400 drm_sysfs_connector_remove(connector
);
401 drm_connector_cleanup(connector
);
405 static int intel_lvds_set_property(struct drm_connector
*connector
,
406 struct drm_property
*property
,
412 static const struct drm_encoder_helper_funcs intel_lvds_helper_funcs
= {
413 .dpms
= intel_lvds_dpms
,
414 .mode_fixup
= intel_lvds_mode_fixup
,
415 .prepare
= intel_lvds_prepare
,
416 .mode_set
= intel_lvds_mode_set
,
417 .commit
= intel_lvds_commit
,
420 static const struct drm_connector_helper_funcs intel_lvds_connector_helper_funcs
= {
421 .get_modes
= intel_lvds_get_modes
,
422 .mode_valid
= intel_lvds_mode_valid
,
423 .best_encoder
= intel_best_encoder
,
426 static const struct drm_connector_funcs intel_lvds_connector_funcs
= {
427 .dpms
= drm_helper_connector_dpms
,
428 .save
= intel_lvds_save
,
429 .restore
= intel_lvds_restore
,
430 .detect
= intel_lvds_detect
,
431 .fill_modes
= drm_helper_probe_single_connector_modes
,
432 .set_property
= intel_lvds_set_property
,
433 .destroy
= intel_lvds_destroy
,
437 static void intel_lvds_enc_destroy(struct drm_encoder
*encoder
)
439 drm_encoder_cleanup(encoder
);
442 static const struct drm_encoder_funcs intel_lvds_enc_funcs
= {
443 .destroy
= intel_lvds_enc_destroy
,
446 static int __init
intel_no_lvds_dmi_callback(const struct dmi_system_id
*id
)
448 DRM_DEBUG_KMS(I915_LVDS
,
449 "Skipping LVDS initialization for %s\n", id
->ident
);
453 /* These systems claim to have LVDS, but really don't */
454 static const struct dmi_system_id intel_no_lvds
[] = {
456 .callback
= intel_no_lvds_dmi_callback
,
457 .ident
= "Apple Mac Mini (Core series)",
459 DMI_MATCH(DMI_SYS_VENDOR
, "Apple Inc."),
460 DMI_MATCH(DMI_PRODUCT_NAME
, "Macmini1,1"),
464 .callback
= intel_no_lvds_dmi_callback
,
465 .ident
= "Apple Mac Mini (Core 2 series)",
467 DMI_MATCH(DMI_SYS_VENDOR
, "Apple Inc."),
468 DMI_MATCH(DMI_PRODUCT_NAME
, "Macmini2,1"),
472 .callback
= intel_no_lvds_dmi_callback
,
473 .ident
= "MSI IM-945GSE-A",
475 DMI_MATCH(DMI_SYS_VENDOR
, "MSI"),
476 DMI_MATCH(DMI_PRODUCT_NAME
, "A9830IMS"),
480 .callback
= intel_no_lvds_dmi_callback
,
481 .ident
= "Dell Studio Hybrid",
483 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
484 DMI_MATCH(DMI_PRODUCT_NAME
, "Studio Hybrid 140g"),
488 .callback
= intel_no_lvds_dmi_callback
,
489 .ident
= "AOpen Mini PC",
491 DMI_MATCH(DMI_SYS_VENDOR
, "AOpen"),
492 DMI_MATCH(DMI_PRODUCT_NAME
, "i965GMx-IF"),
496 .callback
= intel_no_lvds_dmi_callback
,
497 .ident
= "Aopen i945GTt-VFA",
499 DMI_MATCH(DMI_PRODUCT_VERSION
, "AO00001JW"),
503 { } /* terminating entry */
507 * intel_lvds_init - setup LVDS connectors on this device
510 * Create the connector, register the LVDS DDC bus, and try to figure out what
511 * modes we can display on the LVDS panel (if present).
513 void intel_lvds_init(struct drm_device
*dev
)
515 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
516 struct intel_output
*intel_output
;
517 struct drm_connector
*connector
;
518 struct drm_encoder
*encoder
;
519 struct drm_display_mode
*scan
; /* *modes, *bios_mode; */
520 struct drm_crtc
*crtc
;
522 int pipe
, gpio
= GPIOC
;
524 /* Skip init on machines we know falsely report LVDS */
525 if (dmi_check_system(intel_no_lvds
))
529 if ((I915_READ(PCH_LVDS
) & LVDS_DETECTED
) == 0)
534 intel_output
= kzalloc(sizeof(struct intel_output
), GFP_KERNEL
);
539 connector
= &intel_output
->base
;
540 encoder
= &intel_output
->enc
;
541 drm_connector_init(dev
, &intel_output
->base
, &intel_lvds_connector_funcs
,
542 DRM_MODE_CONNECTOR_LVDS
);
544 drm_encoder_init(dev
, &intel_output
->enc
, &intel_lvds_enc_funcs
,
545 DRM_MODE_ENCODER_LVDS
);
547 drm_mode_connector_attach_encoder(&intel_output
->base
, &intel_output
->enc
);
548 intel_output
->type
= INTEL_OUTPUT_LVDS
;
550 drm_encoder_helper_add(encoder
, &intel_lvds_helper_funcs
);
551 drm_connector_helper_add(connector
, &intel_lvds_connector_helper_funcs
);
552 connector
->display_info
.subpixel_order
= SubPixelHorizontalRGB
;
553 connector
->interlace_allowed
= false;
554 connector
->doublescan_allowed
= false;
559 * 1) check for EDID on DDC
560 * 2) check for VBT data
561 * 3) check to see if LVDS is already on
562 * if none of the above, no panel
563 * 4) make sure lid is open
564 * if closed, act like it's not there for now
567 /* Set up the DDC bus. */
568 intel_output
->ddc_bus
= intel_i2c_create(dev
, gpio
, "LVDSDDC_C");
569 if (!intel_output
->ddc_bus
) {
570 dev_printk(KERN_ERR
, &dev
->pdev
->dev
, "DDC bus registration "
576 * Attempt to get the fixed panel mode from DDC. Assume that the
577 * preferred mode is the right one.
579 intel_ddc_get_modes(intel_output
);
581 list_for_each_entry(scan
, &connector
->probed_modes
, head
) {
582 mutex_lock(&dev
->mode_config
.mutex
);
583 if (scan
->type
& DRM_MODE_TYPE_PREFERRED
) {
584 dev_priv
->panel_fixed_mode
=
585 drm_mode_duplicate(dev
, scan
);
586 mutex_unlock(&dev
->mode_config
.mutex
);
589 mutex_unlock(&dev
->mode_config
.mutex
);
592 /* Failed to get EDID, what about VBT? */
593 if (dev_priv
->lfp_lvds_vbt_mode
) {
594 mutex_lock(&dev
->mode_config
.mutex
);
595 dev_priv
->panel_fixed_mode
=
596 drm_mode_duplicate(dev
, dev_priv
->lfp_lvds_vbt_mode
);
597 mutex_unlock(&dev
->mode_config
.mutex
);
598 if (dev_priv
->panel_fixed_mode
) {
599 dev_priv
->panel_fixed_mode
->type
|=
600 DRM_MODE_TYPE_PREFERRED
;
606 * If we didn't get EDID, try checking if the panel is already turned
607 * on. If so, assume that whatever is currently programmed is the
611 /* IGDNG: FIXME if still fail, not try pipe mode now */
615 lvds
= I915_READ(LVDS
);
616 pipe
= (lvds
& LVDS_PIPEB_SELECT
) ? 1 : 0;
617 crtc
= intel_get_crtc_from_pipe(dev
, pipe
);
619 if (crtc
&& (lvds
& LVDS_PORT_EN
)) {
620 dev_priv
->panel_fixed_mode
= intel_crtc_mode_get(dev
, crtc
);
621 if (dev_priv
->panel_fixed_mode
) {
622 dev_priv
->panel_fixed_mode
->type
|=
623 DRM_MODE_TYPE_PREFERRED
;
628 /* If we still don't have a mode after all that, give up. */
629 if (!dev_priv
->panel_fixed_mode
)
635 /* make sure PWM is enabled */
636 pwm
= I915_READ(BLC_PWM_CPU_CTL2
);
637 pwm
|= (PWM_ENABLE
| PWM_PIPE_B
);
638 I915_WRITE(BLC_PWM_CPU_CTL2
, pwm
);
640 pwm
= I915_READ(BLC_PWM_PCH_CTL1
);
641 pwm
|= PWM_PCH_ENABLE
;
642 I915_WRITE(BLC_PWM_PCH_CTL1
, pwm
);
644 drm_sysfs_connector_add(connector
);
648 DRM_DEBUG_KMS(I915_LVDS
, "No LVDS modes found, disabling.\n");
649 if (intel_output
->ddc_bus
)
650 intel_i2c_destroy(intel_output
->ddc_bus
);
651 drm_connector_cleanup(connector
);