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_simple_kms_helper.h>
17 #include "cdv_device.h"
18 #include "intel_bios.h"
21 #include "psb_intel_drv.h"
22 #include "psb_intel_reg.h"
25 * LVDS I2C backlight control macros
27 #define BRIGHTNESS_MAX_LEVEL 100
28 #define BRIGHTNESS_MASK 0xFF
29 #define BLC_I2C_TYPE 0x01
30 #define BLC_PWM_TYPT 0x02
32 #define BLC_POLARITY_NORMAL 0
33 #define BLC_POLARITY_INVERSE 1
35 #define PSB_BLC_MAX_PWM_REG_FREQ (0xFFFE)
36 #define PSB_BLC_MIN_PWM_REG_FREQ (0x2)
37 #define PSB_BLC_PWM_PRECISION_FACTOR (10)
38 #define PSB_BACKLIGHT_PWM_CTL_SHIFT (16)
39 #define PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR (0xFFFE)
41 struct cdv_intel_lvds_priv {
43 * Saved LVDO output states
48 uint32_t savePP_CONTROL;
49 uint32_t savePP_CYCLE;
50 uint32_t savePFIT_CONTROL;
51 uint32_t savePFIT_PGM_RATIOS;
52 uint32_t saveBLC_PWM_CTL;
56 * Returns the maximum level of the backlight duty cycle field.
58 static u32 cdv_intel_lvds_get_max_backlight(struct drm_device *dev)
60 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
63 if (gma_power_begin(dev, false)) {
64 retval = ((REG_READ(BLC_PWM_CTL) &
65 BACKLIGHT_MODULATION_FREQ_MASK) >>
66 BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
70 retval = ((dev_priv->regs.saveBLC_PWM_CTL &
71 BACKLIGHT_MODULATION_FREQ_MASK) >>
72 BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
78 * Sets the backlight level.
80 * level backlight level, from 0 to cdv_intel_lvds_get_max_backlight().
82 static void cdv_intel_lvds_set_backlight(struct drm_device *dev, int level)
84 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
87 if (gma_power_begin(dev, false)) {
89 REG_READ(BLC_PWM_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
90 REG_WRITE(BLC_PWM_CTL,
92 (level << BACKLIGHT_DUTY_CYCLE_SHIFT)));
95 blc_pwm_ctl = dev_priv->regs.saveBLC_PWM_CTL &
96 ~BACKLIGHT_DUTY_CYCLE_MASK;
97 dev_priv->regs.saveBLC_PWM_CTL = (blc_pwm_ctl |
98 (level << BACKLIGHT_DUTY_CYCLE_SHIFT));
103 * Sets the power state for the panel.
105 static void cdv_intel_lvds_set_power(struct drm_device *dev,
106 struct drm_encoder *encoder, bool on)
108 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
111 if (!gma_power_begin(dev, true))
115 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) |
118 pp_status = REG_READ(PP_STATUS);
119 } while ((pp_status & PP_ON) == 0);
121 cdv_intel_lvds_set_backlight(dev,
122 dev_priv->mode_dev.backlight_duty_cycle);
124 cdv_intel_lvds_set_backlight(dev, 0);
126 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) &
129 pp_status = REG_READ(PP_STATUS);
130 } while (pp_status & PP_ON);
135 static void cdv_intel_lvds_encoder_dpms(struct drm_encoder *encoder, int mode)
137 struct drm_device *dev = encoder->dev;
138 if (mode == DRM_MODE_DPMS_ON)
139 cdv_intel_lvds_set_power(dev, encoder, true);
141 cdv_intel_lvds_set_power(dev, encoder, false);
142 /* XXX: We never power down the LVDS pairs. */
145 static void cdv_intel_lvds_save(struct drm_connector *connector)
149 static void cdv_intel_lvds_restore(struct drm_connector *connector)
153 static enum drm_mode_status cdv_intel_lvds_mode_valid(struct drm_connector *connector,
154 struct drm_display_mode *mode)
156 struct drm_device *dev = connector->dev;
157 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
158 struct drm_display_mode *fixed_mode =
159 dev_priv->mode_dev.panel_fixed_mode;
162 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
163 return MODE_NO_DBLESCAN;
166 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
167 return MODE_NO_INTERLACE;
170 if (mode->hdisplay > fixed_mode->hdisplay)
172 if (mode->vdisplay > fixed_mode->vdisplay)
178 static bool cdv_intel_lvds_mode_fixup(struct drm_encoder *encoder,
179 const struct drm_display_mode *mode,
180 struct drm_display_mode *adjusted_mode)
182 struct drm_device *dev = encoder->dev;
183 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
184 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
185 struct drm_encoder *tmp_encoder;
186 struct drm_display_mode *panel_fixed_mode = mode_dev->panel_fixed_mode;
188 /* Should never happen!! */
189 list_for_each_entry(tmp_encoder, &dev->mode_config.encoder_list,
191 if (tmp_encoder != encoder
192 && tmp_encoder->crtc == encoder->crtc) {
193 pr_err("Can't enable LVDS and another encoder on the same pipe\n");
199 * If we have timings from the BIOS for the panel, put them in
200 * to the adjusted mode. The CRTC will be set up for this mode,
201 * with the panel scaling set up to source from the H/VDisplay
202 * of the original mode.
204 if (panel_fixed_mode != NULL) {
205 adjusted_mode->hdisplay = panel_fixed_mode->hdisplay;
206 adjusted_mode->hsync_start = panel_fixed_mode->hsync_start;
207 adjusted_mode->hsync_end = panel_fixed_mode->hsync_end;
208 adjusted_mode->htotal = panel_fixed_mode->htotal;
209 adjusted_mode->vdisplay = panel_fixed_mode->vdisplay;
210 adjusted_mode->vsync_start = panel_fixed_mode->vsync_start;
211 adjusted_mode->vsync_end = panel_fixed_mode->vsync_end;
212 adjusted_mode->vtotal = panel_fixed_mode->vtotal;
213 adjusted_mode->clock = panel_fixed_mode->clock;
214 drm_mode_set_crtcinfo(adjusted_mode,
215 CRTC_INTERLACE_HALVE_V);
219 * XXX: It would be nice to support lower refresh rates on the
220 * panels to reduce power consumption, and perhaps match the
221 * user's requested refresh rate.
227 static void cdv_intel_lvds_prepare(struct drm_encoder *encoder)
229 struct drm_device *dev = encoder->dev;
230 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
231 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
233 if (!gma_power_begin(dev, true))
236 mode_dev->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
237 mode_dev->backlight_duty_cycle = (mode_dev->saveBLC_PWM_CTL &
238 BACKLIGHT_DUTY_CYCLE_MASK);
240 cdv_intel_lvds_set_power(dev, encoder, false);
245 static void cdv_intel_lvds_commit(struct drm_encoder *encoder)
247 struct drm_device *dev = encoder->dev;
248 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
249 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
251 if (mode_dev->backlight_duty_cycle == 0)
252 mode_dev->backlight_duty_cycle =
253 cdv_intel_lvds_get_max_backlight(dev);
255 cdv_intel_lvds_set_power(dev, encoder, true);
258 static void cdv_intel_lvds_mode_set(struct drm_encoder *encoder,
259 struct drm_display_mode *mode,
260 struct drm_display_mode *adjusted_mode)
262 struct drm_device *dev = encoder->dev;
263 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
264 struct gma_crtc *gma_crtc = to_gma_crtc(encoder->crtc);
268 * The LVDS pin pair will already have been turned on in the
269 * cdv_intel_crtc_mode_set since it has a large impact on the DPLL
274 * Enable automatic panel scaling so that non-native modes fill the
275 * screen. Should be enabled before the pipe is enabled, according to
276 * register description and PRM.
278 if (mode->hdisplay != adjusted_mode->hdisplay ||
279 mode->vdisplay != adjusted_mode->vdisplay)
280 pfit_control = (PFIT_ENABLE | VERT_AUTO_SCALE |
281 HORIZ_AUTO_SCALE | VERT_INTERP_BILINEAR |
282 HORIZ_INTERP_BILINEAR);
286 pfit_control |= gma_crtc->pipe << PFIT_PIPE_SHIFT;
288 if (dev_priv->lvds_dither)
289 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
291 REG_WRITE(PFIT_CONTROL, pfit_control);
295 * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
297 static int cdv_intel_lvds_get_modes(struct drm_connector *connector)
299 struct drm_device *dev = connector->dev;
300 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
301 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
304 ret = psb_intel_ddc_get_modes(connector, connector->ddc);
309 if (mode_dev->panel_fixed_mode != NULL) {
310 struct drm_display_mode *mode =
311 drm_mode_duplicate(dev, mode_dev->panel_fixed_mode);
312 drm_mode_probed_add(connector, mode);
319 static void cdv_intel_lvds_destroy(struct drm_connector *connector)
321 struct gma_connector *gma_connector = to_gma_connector(connector);
322 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
324 gma_i2c_destroy(to_gma_i2c_chan(connector->ddc));
325 gma_i2c_destroy(gma_encoder->i2c_bus);
326 drm_connector_cleanup(connector);
327 kfree(gma_connector);
330 static int cdv_intel_lvds_set_property(struct drm_connector *connector,
331 struct drm_property *property,
334 struct drm_encoder *encoder = connector->encoder;
336 if (!strcmp(property->name, "scaling mode") && encoder) {
337 struct gma_crtc *crtc = to_gma_crtc(encoder->crtc);
344 case DRM_MODE_SCALE_FULLSCREEN:
346 case DRM_MODE_SCALE_NO_SCALE:
348 case DRM_MODE_SCALE_ASPECT:
354 if (drm_object_property_get_value(&connector->base,
359 if (curValue == value)
362 if (drm_object_property_set_value(&connector->base,
367 if (crtc->saved_mode.hdisplay != 0 &&
368 crtc->saved_mode.vdisplay != 0) {
369 if (!drm_crtc_helper_set_mode(encoder->crtc,
373 encoder->crtc->primary->fb))
376 } else if (!strcmp(property->name, "backlight") && encoder) {
377 if (drm_object_property_set_value(&connector->base,
382 gma_backlight_set(encoder->dev, value);
383 } else if (!strcmp(property->name, "DPMS") && encoder) {
384 const struct drm_encoder_helper_funcs *helpers =
385 encoder->helper_private;
386 helpers->dpms(encoder, value);
391 static const struct drm_encoder_helper_funcs
392 cdv_intel_lvds_helper_funcs = {
393 .dpms = cdv_intel_lvds_encoder_dpms,
394 .mode_fixup = cdv_intel_lvds_mode_fixup,
395 .prepare = cdv_intel_lvds_prepare,
396 .mode_set = cdv_intel_lvds_mode_set,
397 .commit = cdv_intel_lvds_commit,
400 static const struct drm_connector_helper_funcs
401 cdv_intel_lvds_connector_helper_funcs = {
402 .get_modes = cdv_intel_lvds_get_modes,
403 .mode_valid = cdv_intel_lvds_mode_valid,
404 .best_encoder = gma_best_encoder,
407 static const struct drm_connector_funcs cdv_intel_lvds_connector_funcs = {
408 .dpms = drm_helper_connector_dpms,
409 .fill_modes = drm_helper_probe_single_connector_modes,
410 .set_property = cdv_intel_lvds_set_property,
411 .destroy = cdv_intel_lvds_destroy,
415 * Enumerate the child dev array parsed from VBT to check whether
416 * the LVDS is present.
417 * If it is present, return 1.
418 * If it is not present, return false.
419 * If no child dev is parsed from VBT, it assumes that the LVDS is present.
421 static bool lvds_is_present_in_vbt(struct drm_device *dev,
424 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
427 if (!dev_priv->child_dev_num)
430 for (i = 0; i < dev_priv->child_dev_num; i++) {
431 struct child_device_config *child = dev_priv->child_dev + i;
433 /* If the device type is not LFP, continue.
434 * We have to check both the new identifiers as well as the
435 * old for compatibility with some BIOSes.
437 if (child->device_type != DEVICE_TYPE_INT_LFP &&
438 child->device_type != DEVICE_TYPE_LFP)
442 *i2c_pin = child->i2c_pin;
444 /* However, we cannot trust the BIOS writers to populate
445 * the VBT correctly. Since LVDS requires additional
446 * information from AIM blocks, a non-zero addin offset is
447 * a good indicator that the LVDS is actually present.
449 if (child->addin_offset)
452 /* But even then some BIOS writers perform some black magic
453 * and instantiate the device without reference to any
454 * additional data. Trust that if the VBT was written into
455 * the OpRegion then they have validated the LVDS's existence.
457 if (dev_priv->opregion.vbt)
465 * cdv_intel_lvds_init - setup LVDS connectors on this device
467 * @mode_dev: PSB mode device
469 * Create the connector, register the LVDS DDC bus, and try to figure out what
470 * modes we can display on the LVDS panel (if present).
472 void cdv_intel_lvds_init(struct drm_device *dev,
473 struct psb_intel_mode_device *mode_dev)
475 struct gma_encoder *gma_encoder;
476 struct gma_connector *gma_connector;
477 struct cdv_intel_lvds_priv *lvds_priv;
478 struct drm_connector *connector;
479 struct drm_encoder *encoder;
480 struct drm_display_mode *scan;
481 struct drm_crtc *crtc;
482 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
483 struct gma_i2c_chan *ddc_bus;
489 if (!dev_priv->lvds_enabled_in_vbt)
492 pin = GMBUS_PORT_PANEL;
493 if (!lvds_is_present_in_vbt(dev, &pin)) {
494 DRM_DEBUG_KMS("LVDS is not present in VBT\n");
498 gma_encoder = kzalloc(sizeof(struct gma_encoder),
503 gma_connector = kzalloc(sizeof(struct gma_connector),
506 goto err_free_encoder;
508 lvds_priv = kzalloc(sizeof(struct cdv_intel_lvds_priv), GFP_KERNEL);
510 goto err_free_connector;
512 gma_encoder->dev_priv = lvds_priv;
514 connector = &gma_connector->base;
515 gma_connector->save = cdv_intel_lvds_save;
516 gma_connector->restore = cdv_intel_lvds_restore;
517 encoder = &gma_encoder->base;
519 /* Set up the DDC bus. */
520 ddc_bus = gma_i2c_create(dev, GPIOC, "LVDSDDC_C");
522 dev_printk(KERN_ERR, dev->dev,
523 "DDC bus registration " "failed.\n");
524 goto err_free_lvds_priv;
527 ret = drm_connector_init_with_ddc(dev, connector,
528 &cdv_intel_lvds_connector_funcs,
529 DRM_MODE_CONNECTOR_LVDS,
532 goto err_destroy_ddc;
534 ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_LVDS);
536 goto err_connector_cleanup;
538 gma_connector_attach_encoder(gma_connector, gma_encoder);
539 gma_encoder->type = INTEL_OUTPUT_LVDS;
541 drm_encoder_helper_add(encoder, &cdv_intel_lvds_helper_funcs);
542 drm_connector_helper_add(connector,
543 &cdv_intel_lvds_connector_helper_funcs);
544 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
545 connector->interlace_allowed = false;
546 connector->doublescan_allowed = false;
548 /*Attach connector properties*/
549 drm_object_attach_property(&connector->base,
550 dev->mode_config.scaling_mode_property,
551 DRM_MODE_SCALE_FULLSCREEN);
552 drm_object_attach_property(&connector->base,
553 dev_priv->backlight_property,
554 BRIGHTNESS_MAX_LEVEL);
558 * FIXME: distroy i2c_bus when exit
560 gma_encoder->i2c_bus = gma_i2c_create(dev, GPIOB, "LVDSBLC_B");
561 if (!gma_encoder->i2c_bus) {
563 dev->dev, "I2C bus registration failed.\n");
564 goto err_encoder_cleanup;
566 gma_encoder->i2c_bus->slave_addr = 0x2C;
567 dev_priv->lvds_i2c_bus = gma_encoder->i2c_bus;
571 * 1) check for EDID on DDC
572 * 2) check for VBT data
573 * 3) check to see if LVDS is already on
574 * if none of the above, no panel
575 * 4) make sure lid is open
576 * if closed, act like it's not there for now
580 * Attempt to get the fixed panel mode from DDC. Assume that the
581 * preferred mode is the right one.
583 mutex_lock(&dev->mode_config.mutex);
584 psb_intel_ddc_get_modes(connector, &ddc_bus->base);
586 list_for_each_entry(scan, &connector->probed_modes, head) {
587 if (scan->type & DRM_MODE_TYPE_PREFERRED) {
588 mode_dev->panel_fixed_mode =
589 drm_mode_duplicate(dev, scan);
590 goto out; /* FIXME: check for quirks */
594 /* Failed to get EDID, what about VBT? do we need this?*/
595 if (dev_priv->lfp_lvds_vbt_mode) {
596 mode_dev->panel_fixed_mode =
597 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
598 if (mode_dev->panel_fixed_mode) {
599 mode_dev->panel_fixed_mode->type |=
600 DRM_MODE_TYPE_PREFERRED;
601 goto out; /* FIXME: check for quirks */
605 * If we didn't get EDID, try checking if the panel is already turned
606 * on. If so, assume that whatever is currently programmed is the
609 lvds = REG_READ(LVDS);
610 pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
611 crtc = psb_intel_get_crtc_from_pipe(dev, pipe);
613 if (crtc && (lvds & LVDS_PORT_EN)) {
614 mode_dev->panel_fixed_mode =
615 cdv_intel_crtc_mode_get(dev, crtc);
616 if (mode_dev->panel_fixed_mode) {
617 mode_dev->panel_fixed_mode->type |=
618 DRM_MODE_TYPE_PREFERRED;
619 goto out; /* FIXME: check for quirks */
623 /* If we still don't have a mode after all that, give up. */
624 if (!mode_dev->panel_fixed_mode) {
626 ("Found no modes on the lvds, ignoring the LVDS\n");
634 pwm = REG_READ(BLC_PWM_CTL2);
640 REG_WRITE(BLC_PWM_CTL2, pwm);
644 mutex_unlock(&dev->mode_config.mutex);
648 mutex_unlock(&dev->mode_config.mutex);
649 gma_i2c_destroy(gma_encoder->i2c_bus);
651 drm_encoder_cleanup(encoder);
652 err_connector_cleanup:
653 drm_connector_cleanup(connector);
655 gma_i2c_destroy(ddc_bus);
659 kfree(gma_connector);