1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2006-2011 Intel Corporation
6 * Eric Anholt <eric@anholt.net>
7 * Dave Airlie <airlied@linux.ie>
8 * Jesse Barnes <jesse.barnes@intel.com>
11 #include <linux/dmi.h>
12 #include <linux/i2c.h>
13 #include <linux/pm_runtime.h>
15 #include <drm/drm_crtc_helper.h>
16 #include <drm/drm_modeset_helper_vtables.h>
17 #include <drm/drm_simple_kms_helper.h>
19 #include "cdv_device.h"
20 #include "intel_bios.h"
23 #include "psb_intel_drv.h"
24 #include "psb_intel_reg.h"
27 * LVDS I2C backlight control macros
29 #define BRIGHTNESS_MAX_LEVEL 100
30 #define BRIGHTNESS_MASK 0xFF
31 #define BLC_I2C_TYPE 0x01
32 #define BLC_PWM_TYPT 0x02
34 #define BLC_POLARITY_NORMAL 0
35 #define BLC_POLARITY_INVERSE 1
37 #define PSB_BLC_MAX_PWM_REG_FREQ (0xFFFE)
38 #define PSB_BLC_MIN_PWM_REG_FREQ (0x2)
39 #define PSB_BLC_PWM_PRECISION_FACTOR (10)
40 #define PSB_BACKLIGHT_PWM_CTL_SHIFT (16)
41 #define PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR (0xFFFE)
43 struct cdv_intel_lvds_priv {
45 * Saved LVDO output states
50 uint32_t savePP_CONTROL;
51 uint32_t savePP_CYCLE;
52 uint32_t savePFIT_CONTROL;
53 uint32_t savePFIT_PGM_RATIOS;
54 uint32_t saveBLC_PWM_CTL;
58 * Returns the maximum level of the backlight duty cycle field.
60 static u32 cdv_intel_lvds_get_max_backlight(struct drm_device *dev)
62 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
65 if (gma_power_begin(dev, false)) {
66 retval = ((REG_READ(BLC_PWM_CTL) &
67 BACKLIGHT_MODULATION_FREQ_MASK) >>
68 BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
72 retval = ((dev_priv->regs.saveBLC_PWM_CTL &
73 BACKLIGHT_MODULATION_FREQ_MASK) >>
74 BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
80 * Sets the backlight level.
82 * level backlight level, from 0 to cdv_intel_lvds_get_max_backlight().
84 static void cdv_intel_lvds_set_backlight(struct drm_device *dev, int level)
86 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
89 if (gma_power_begin(dev, false)) {
91 REG_READ(BLC_PWM_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
92 REG_WRITE(BLC_PWM_CTL,
94 (level << BACKLIGHT_DUTY_CYCLE_SHIFT)));
97 blc_pwm_ctl = dev_priv->regs.saveBLC_PWM_CTL &
98 ~BACKLIGHT_DUTY_CYCLE_MASK;
99 dev_priv->regs.saveBLC_PWM_CTL = (blc_pwm_ctl |
100 (level << BACKLIGHT_DUTY_CYCLE_SHIFT));
105 * Sets the power state for the panel.
107 static void cdv_intel_lvds_set_power(struct drm_device *dev,
108 struct drm_encoder *encoder, bool on)
110 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
113 if (!gma_power_begin(dev, true))
117 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) |
120 pp_status = REG_READ(PP_STATUS);
121 } while ((pp_status & PP_ON) == 0);
123 cdv_intel_lvds_set_backlight(dev,
124 dev_priv->mode_dev.backlight_duty_cycle);
126 cdv_intel_lvds_set_backlight(dev, 0);
128 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) &
131 pp_status = REG_READ(PP_STATUS);
132 } while (pp_status & PP_ON);
137 static void cdv_intel_lvds_encoder_dpms(struct drm_encoder *encoder, int mode)
139 struct drm_device *dev = encoder->dev;
140 if (mode == DRM_MODE_DPMS_ON)
141 cdv_intel_lvds_set_power(dev, encoder, true);
143 cdv_intel_lvds_set_power(dev, encoder, false);
144 /* XXX: We never power down the LVDS pairs. */
147 static void cdv_intel_lvds_save(struct drm_connector *connector)
151 static void cdv_intel_lvds_restore(struct drm_connector *connector)
155 static enum drm_mode_status cdv_intel_lvds_mode_valid(struct drm_connector *connector,
156 struct drm_display_mode *mode)
158 struct drm_device *dev = connector->dev;
159 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
160 struct drm_display_mode *fixed_mode =
161 dev_priv->mode_dev.panel_fixed_mode;
164 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
165 return MODE_NO_DBLESCAN;
168 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
169 return MODE_NO_INTERLACE;
172 if (mode->hdisplay > fixed_mode->hdisplay)
174 if (mode->vdisplay > fixed_mode->vdisplay)
180 static bool cdv_intel_lvds_mode_fixup(struct drm_encoder *encoder,
181 const struct drm_display_mode *mode,
182 struct drm_display_mode *adjusted_mode)
184 struct drm_device *dev = encoder->dev;
185 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
186 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
187 struct drm_encoder *tmp_encoder;
188 struct drm_display_mode *panel_fixed_mode = mode_dev->panel_fixed_mode;
190 /* Should never happen!! */
191 list_for_each_entry(tmp_encoder, &dev->mode_config.encoder_list,
193 if (tmp_encoder != encoder
194 && tmp_encoder->crtc == encoder->crtc) {
195 pr_err("Can't enable LVDS and another encoder on the same pipe\n");
201 * If we have timings from the BIOS for the panel, put them in
202 * to the adjusted mode. The CRTC will be set up for this mode,
203 * with the panel scaling set up to source from the H/VDisplay
204 * of the original mode.
206 if (panel_fixed_mode != NULL) {
207 adjusted_mode->hdisplay = panel_fixed_mode->hdisplay;
208 adjusted_mode->hsync_start = panel_fixed_mode->hsync_start;
209 adjusted_mode->hsync_end = panel_fixed_mode->hsync_end;
210 adjusted_mode->htotal = panel_fixed_mode->htotal;
211 adjusted_mode->vdisplay = panel_fixed_mode->vdisplay;
212 adjusted_mode->vsync_start = panel_fixed_mode->vsync_start;
213 adjusted_mode->vsync_end = panel_fixed_mode->vsync_end;
214 adjusted_mode->vtotal = panel_fixed_mode->vtotal;
215 adjusted_mode->clock = panel_fixed_mode->clock;
216 drm_mode_set_crtcinfo(adjusted_mode,
217 CRTC_INTERLACE_HALVE_V);
221 * XXX: It would be nice to support lower refresh rates on the
222 * panels to reduce power consumption, and perhaps match the
223 * user's requested refresh rate.
229 static void cdv_intel_lvds_prepare(struct drm_encoder *encoder)
231 struct drm_device *dev = encoder->dev;
232 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
233 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
235 if (!gma_power_begin(dev, true))
238 mode_dev->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
239 mode_dev->backlight_duty_cycle = (mode_dev->saveBLC_PWM_CTL &
240 BACKLIGHT_DUTY_CYCLE_MASK);
242 cdv_intel_lvds_set_power(dev, encoder, false);
247 static void cdv_intel_lvds_commit(struct drm_encoder *encoder)
249 struct drm_device *dev = encoder->dev;
250 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
251 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
253 if (mode_dev->backlight_duty_cycle == 0)
254 mode_dev->backlight_duty_cycle =
255 cdv_intel_lvds_get_max_backlight(dev);
257 cdv_intel_lvds_set_power(dev, encoder, true);
260 static void cdv_intel_lvds_mode_set(struct drm_encoder *encoder,
261 struct drm_display_mode *mode,
262 struct drm_display_mode *adjusted_mode)
264 struct drm_device *dev = encoder->dev;
265 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
266 struct gma_crtc *gma_crtc = to_gma_crtc(encoder->crtc);
270 * The LVDS pin pair will already have been turned on in the
271 * cdv_intel_crtc_mode_set since it has a large impact on the DPLL
276 * Enable automatic panel scaling so that non-native modes fill the
277 * screen. Should be enabled before the pipe is enabled, according to
278 * register description and PRM.
280 if (mode->hdisplay != adjusted_mode->hdisplay ||
281 mode->vdisplay != adjusted_mode->vdisplay)
282 pfit_control = (PFIT_ENABLE | VERT_AUTO_SCALE |
283 HORIZ_AUTO_SCALE | VERT_INTERP_BILINEAR |
284 HORIZ_INTERP_BILINEAR);
288 pfit_control |= gma_crtc->pipe << PFIT_PIPE_SHIFT;
290 if (dev_priv->lvds_dither)
291 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
293 REG_WRITE(PFIT_CONTROL, pfit_control);
297 * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
299 static int cdv_intel_lvds_get_modes(struct drm_connector *connector)
301 struct drm_device *dev = connector->dev;
302 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
303 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
306 ret = psb_intel_ddc_get_modes(connector, connector->ddc);
311 if (mode_dev->panel_fixed_mode != NULL) {
312 struct drm_display_mode *mode =
313 drm_mode_duplicate(dev, mode_dev->panel_fixed_mode);
314 drm_mode_probed_add(connector, mode);
321 static void cdv_intel_lvds_destroy(struct drm_connector *connector)
323 struct gma_connector *gma_connector = to_gma_connector(connector);
324 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
326 gma_i2c_destroy(to_gma_i2c_chan(connector->ddc));
327 gma_i2c_destroy(gma_encoder->i2c_bus);
328 drm_connector_cleanup(connector);
329 kfree(gma_connector);
332 static int cdv_intel_lvds_set_property(struct drm_connector *connector,
333 struct drm_property *property,
336 struct drm_encoder *encoder = connector->encoder;
338 if (!strcmp(property->name, "scaling mode") && encoder) {
339 struct gma_crtc *crtc = to_gma_crtc(encoder->crtc);
346 case DRM_MODE_SCALE_FULLSCREEN:
348 case DRM_MODE_SCALE_NO_SCALE:
350 case DRM_MODE_SCALE_ASPECT:
356 if (drm_object_property_get_value(&connector->base,
361 if (curValue == value)
364 if (drm_object_property_set_value(&connector->base,
369 if (crtc->saved_mode.hdisplay != 0 &&
370 crtc->saved_mode.vdisplay != 0) {
371 if (!drm_crtc_helper_set_mode(encoder->crtc,
375 encoder->crtc->primary->fb))
378 } else if (!strcmp(property->name, "backlight") && encoder) {
379 if (drm_object_property_set_value(&connector->base,
384 gma_backlight_set(encoder->dev, value);
385 } else if (!strcmp(property->name, "DPMS") && encoder) {
386 const struct drm_encoder_helper_funcs *helpers =
387 encoder->helper_private;
388 helpers->dpms(encoder, value);
393 static const struct drm_encoder_helper_funcs
394 cdv_intel_lvds_helper_funcs = {
395 .dpms = cdv_intel_lvds_encoder_dpms,
396 .mode_fixup = cdv_intel_lvds_mode_fixup,
397 .prepare = cdv_intel_lvds_prepare,
398 .mode_set = cdv_intel_lvds_mode_set,
399 .commit = cdv_intel_lvds_commit,
402 static const struct drm_connector_helper_funcs
403 cdv_intel_lvds_connector_helper_funcs = {
404 .get_modes = cdv_intel_lvds_get_modes,
405 .mode_valid = cdv_intel_lvds_mode_valid,
406 .best_encoder = gma_best_encoder,
409 static const struct drm_connector_funcs cdv_intel_lvds_connector_funcs = {
410 .dpms = drm_helper_connector_dpms,
411 .fill_modes = drm_helper_probe_single_connector_modes,
412 .set_property = cdv_intel_lvds_set_property,
413 .destroy = cdv_intel_lvds_destroy,
417 * Enumerate the child dev array parsed from VBT to check whether
418 * the LVDS is present.
419 * If it is present, return 1.
420 * If it is not present, return false.
421 * If no child dev is parsed from VBT, it assumes that the LVDS is present.
423 static bool lvds_is_present_in_vbt(struct drm_device *dev,
426 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
429 if (!dev_priv->child_dev_num)
432 for (i = 0; i < dev_priv->child_dev_num; i++) {
433 struct child_device_config *child = dev_priv->child_dev + i;
435 /* If the device type is not LFP, continue.
436 * We have to check both the new identifiers as well as the
437 * old for compatibility with some BIOSes.
439 if (child->device_type != DEVICE_TYPE_INT_LFP &&
440 child->device_type != DEVICE_TYPE_LFP)
444 *i2c_pin = child->i2c_pin;
446 /* However, we cannot trust the BIOS writers to populate
447 * the VBT correctly. Since LVDS requires additional
448 * information from AIM blocks, a non-zero addin offset is
449 * a good indicator that the LVDS is actually present.
451 if (child->addin_offset)
454 /* But even then some BIOS writers perform some black magic
455 * and instantiate the device without reference to any
456 * additional data. Trust that if the VBT was written into
457 * the OpRegion then they have validated the LVDS's existence.
459 if (dev_priv->opregion.vbt)
467 * cdv_intel_lvds_init - setup LVDS connectors on this device
469 * @mode_dev: PSB mode device
471 * Create the connector, register the LVDS DDC bus, and try to figure out what
472 * modes we can display on the LVDS panel (if present).
474 void cdv_intel_lvds_init(struct drm_device *dev,
475 struct psb_intel_mode_device *mode_dev)
477 struct gma_encoder *gma_encoder;
478 struct gma_connector *gma_connector;
479 struct cdv_intel_lvds_priv *lvds_priv;
480 struct drm_connector *connector;
481 struct drm_encoder *encoder;
482 struct drm_display_mode *scan;
483 struct drm_crtc *crtc;
484 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
485 struct gma_i2c_chan *ddc_bus;
491 if (!dev_priv->lvds_enabled_in_vbt)
494 pin = GMBUS_PORT_PANEL;
495 if (!lvds_is_present_in_vbt(dev, &pin)) {
496 DRM_DEBUG_KMS("LVDS is not present in VBT\n");
500 gma_encoder = kzalloc(sizeof(struct gma_encoder),
505 gma_connector = kzalloc(sizeof(struct gma_connector),
508 goto err_free_encoder;
510 lvds_priv = kzalloc(sizeof(struct cdv_intel_lvds_priv), GFP_KERNEL);
512 goto err_free_connector;
514 gma_encoder->dev_priv = lvds_priv;
516 connector = &gma_connector->base;
517 gma_connector->save = cdv_intel_lvds_save;
518 gma_connector->restore = cdv_intel_lvds_restore;
519 encoder = &gma_encoder->base;
521 /* Set up the DDC bus. */
522 ddc_bus = gma_i2c_create(dev, GPIOC, "LVDSDDC_C");
524 dev_printk(KERN_ERR, dev->dev,
525 "DDC bus registration " "failed.\n");
526 goto err_free_lvds_priv;
529 ret = drm_connector_init_with_ddc(dev, connector,
530 &cdv_intel_lvds_connector_funcs,
531 DRM_MODE_CONNECTOR_LVDS,
534 goto err_destroy_ddc;
536 ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_LVDS);
538 goto err_connector_cleanup;
540 gma_connector_attach_encoder(gma_connector, gma_encoder);
541 gma_encoder->type = INTEL_OUTPUT_LVDS;
543 drm_encoder_helper_add(encoder, &cdv_intel_lvds_helper_funcs);
544 drm_connector_helper_add(connector,
545 &cdv_intel_lvds_connector_helper_funcs);
546 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
547 connector->interlace_allowed = false;
548 connector->doublescan_allowed = false;
550 /*Attach connector properties*/
551 drm_object_attach_property(&connector->base,
552 dev->mode_config.scaling_mode_property,
553 DRM_MODE_SCALE_FULLSCREEN);
554 drm_object_attach_property(&connector->base,
555 dev_priv->backlight_property,
556 BRIGHTNESS_MAX_LEVEL);
560 * FIXME: distroy i2c_bus when exit
562 gma_encoder->i2c_bus = gma_i2c_create(dev, GPIOB, "LVDSBLC_B");
563 if (!gma_encoder->i2c_bus) {
565 dev->dev, "I2C bus registration failed.\n");
566 goto err_encoder_cleanup;
568 gma_encoder->i2c_bus->slave_addr = 0x2C;
569 dev_priv->lvds_i2c_bus = gma_encoder->i2c_bus;
573 * 1) check for EDID on DDC
574 * 2) check for VBT data
575 * 3) check to see if LVDS is already on
576 * if none of the above, no panel
577 * 4) make sure lid is open
578 * if closed, act like it's not there for now
582 * Attempt to get the fixed panel mode from DDC. Assume that the
583 * preferred mode is the right one.
585 mutex_lock(&dev->mode_config.mutex);
586 psb_intel_ddc_get_modes(connector, &ddc_bus->base);
588 list_for_each_entry(scan, &connector->probed_modes, head) {
589 if (scan->type & DRM_MODE_TYPE_PREFERRED) {
590 mode_dev->panel_fixed_mode =
591 drm_mode_duplicate(dev, scan);
592 goto out; /* FIXME: check for quirks */
596 /* Failed to get EDID, what about VBT? do we need this?*/
597 if (dev_priv->lfp_lvds_vbt_mode) {
598 mode_dev->panel_fixed_mode =
599 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
600 if (mode_dev->panel_fixed_mode) {
601 mode_dev->panel_fixed_mode->type |=
602 DRM_MODE_TYPE_PREFERRED;
603 goto out; /* FIXME: check for quirks */
607 * If we didn't get EDID, try checking if the panel is already turned
608 * on. If so, assume that whatever is currently programmed is the
611 lvds = REG_READ(LVDS);
612 pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
613 crtc = psb_intel_get_crtc_from_pipe(dev, pipe);
615 if (crtc && (lvds & LVDS_PORT_EN)) {
616 mode_dev->panel_fixed_mode =
617 cdv_intel_crtc_mode_get(dev, crtc);
618 if (mode_dev->panel_fixed_mode) {
619 mode_dev->panel_fixed_mode->type |=
620 DRM_MODE_TYPE_PREFERRED;
621 goto out; /* FIXME: check for quirks */
625 /* If we still don't have a mode after all that, give up. */
626 if (!mode_dev->panel_fixed_mode) {
628 ("Found no modes on the lvds, ignoring the LVDS\n");
636 pwm = REG_READ(BLC_PWM_CTL2);
642 REG_WRITE(BLC_PWM_CTL2, pwm);
646 mutex_unlock(&dev->mode_config.mutex);
650 mutex_unlock(&dev->mode_config.mutex);
651 gma_i2c_destroy(gma_encoder->i2c_bus);
653 drm_encoder_cleanup(encoder);
654 err_connector_cleanup:
655 drm_connector_cleanup(connector);
657 gma_i2c_destroy(ddc_bus);
661 kfree(gma_connector);