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 = dev->dev_private;
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;
79 * Set LVDS backlight level by I2C command
81 static int cdv_lvds_i2c_set_brightness(struct drm_device *dev,
84 struct drm_psb_private *dev_priv = dev->dev_private;
85 struct psb_intel_i2c_chan *lvds_i2c_bus = dev_priv->lvds_i2c_bus;
87 unsigned int blc_i2c_brightness;
89 struct i2c_msg msgs[] = {
91 .addr = lvds_i2c_bus->slave_addr,
98 blc_i2c_brightness = BRIGHTNESS_MASK & ((unsigned int)level *
100 BRIGHTNESS_MAX_LEVEL);
102 if (dev_priv->lvds_bl->pol == BLC_POLARITY_INVERSE)
103 blc_i2c_brightness = BRIGHTNESS_MASK - blc_i2c_brightness;
105 out_buf[0] = dev_priv->lvds_bl->brightnesscmd;
106 out_buf[1] = (u8)blc_i2c_brightness;
108 if (i2c_transfer(&lvds_i2c_bus->adapter, msgs, 1) == 1)
111 DRM_ERROR("I2C transfer error\n");
116 static int cdv_lvds_pwm_set_brightness(struct drm_device *dev, int level)
118 struct drm_psb_private *dev_priv = dev->dev_private;
121 u32 blc_pwm_duty_cycle;
123 max_pwm_blc = cdv_intel_lvds_get_max_backlight(dev);
125 /*BLC_PWM_CTL Should be initiated while backlight device init*/
126 BUG_ON((max_pwm_blc & PSB_BLC_MAX_PWM_REG_FREQ) == 0);
128 blc_pwm_duty_cycle = level * max_pwm_blc / BRIGHTNESS_MAX_LEVEL;
130 if (dev_priv->lvds_bl->pol == BLC_POLARITY_INVERSE)
131 blc_pwm_duty_cycle = max_pwm_blc - blc_pwm_duty_cycle;
133 blc_pwm_duty_cycle &= PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR;
134 REG_WRITE(BLC_PWM_CTL,
135 (max_pwm_blc << PSB_BACKLIGHT_PWM_CTL_SHIFT) |
136 (blc_pwm_duty_cycle));
142 * Set LVDS backlight level either by I2C or PWM
144 void cdv_intel_lvds_set_brightness(struct drm_device *dev, int level)
146 struct drm_psb_private *dev_priv = dev->dev_private;
148 if (!dev_priv->lvds_bl) {
149 DRM_ERROR("NO LVDS Backlight Info\n");
153 if (dev_priv->lvds_bl->type == BLC_I2C_TYPE)
154 cdv_lvds_i2c_set_brightness(dev, level);
156 cdv_lvds_pwm_set_brightness(dev, level);
161 * Sets the backlight level.
163 * level backlight level, from 0 to cdv_intel_lvds_get_max_backlight().
165 static void cdv_intel_lvds_set_backlight(struct drm_device *dev, int level)
167 struct drm_psb_private *dev_priv = dev->dev_private;
170 if (gma_power_begin(dev, false)) {
172 REG_READ(BLC_PWM_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
173 REG_WRITE(BLC_PWM_CTL,
175 (level << BACKLIGHT_DUTY_CYCLE_SHIFT)));
178 blc_pwm_ctl = dev_priv->regs.saveBLC_PWM_CTL &
179 ~BACKLIGHT_DUTY_CYCLE_MASK;
180 dev_priv->regs.saveBLC_PWM_CTL = (blc_pwm_ctl |
181 (level << BACKLIGHT_DUTY_CYCLE_SHIFT));
186 * Sets the power state for the panel.
188 static void cdv_intel_lvds_set_power(struct drm_device *dev,
189 struct drm_encoder *encoder, bool on)
191 struct drm_psb_private *dev_priv = dev->dev_private;
194 if (!gma_power_begin(dev, true))
198 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) |
201 pp_status = REG_READ(PP_STATUS);
202 } while ((pp_status & PP_ON) == 0);
204 cdv_intel_lvds_set_backlight(dev,
205 dev_priv->mode_dev.backlight_duty_cycle);
207 cdv_intel_lvds_set_backlight(dev, 0);
209 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) &
212 pp_status = REG_READ(PP_STATUS);
213 } while (pp_status & PP_ON);
218 static void cdv_intel_lvds_encoder_dpms(struct drm_encoder *encoder, int mode)
220 struct drm_device *dev = encoder->dev;
221 if (mode == DRM_MODE_DPMS_ON)
222 cdv_intel_lvds_set_power(dev, encoder, true);
224 cdv_intel_lvds_set_power(dev, encoder, false);
225 /* XXX: We never power down the LVDS pairs. */
228 static void cdv_intel_lvds_save(struct drm_connector *connector)
232 static void cdv_intel_lvds_restore(struct drm_connector *connector)
236 static enum drm_mode_status cdv_intel_lvds_mode_valid(struct drm_connector *connector,
237 struct drm_display_mode *mode)
239 struct drm_device *dev = connector->dev;
240 struct drm_psb_private *dev_priv = dev->dev_private;
241 struct drm_display_mode *fixed_mode =
242 dev_priv->mode_dev.panel_fixed_mode;
245 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
246 return MODE_NO_DBLESCAN;
249 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
250 return MODE_NO_INTERLACE;
253 if (mode->hdisplay > fixed_mode->hdisplay)
255 if (mode->vdisplay > fixed_mode->vdisplay)
261 static bool cdv_intel_lvds_mode_fixup(struct drm_encoder *encoder,
262 const struct drm_display_mode *mode,
263 struct drm_display_mode *adjusted_mode)
265 struct drm_device *dev = encoder->dev;
266 struct drm_psb_private *dev_priv = dev->dev_private;
267 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
268 struct drm_encoder *tmp_encoder;
269 struct drm_display_mode *panel_fixed_mode = mode_dev->panel_fixed_mode;
271 /* Should never happen!! */
272 list_for_each_entry(tmp_encoder, &dev->mode_config.encoder_list,
274 if (tmp_encoder != encoder
275 && tmp_encoder->crtc == encoder->crtc) {
276 pr_err("Can't enable LVDS and another encoder on the same pipe\n");
282 * If we have timings from the BIOS for the panel, put them in
283 * to the adjusted mode. The CRTC will be set up for this mode,
284 * with the panel scaling set up to source from the H/VDisplay
285 * of the original mode.
287 if (panel_fixed_mode != NULL) {
288 adjusted_mode->hdisplay = panel_fixed_mode->hdisplay;
289 adjusted_mode->hsync_start = panel_fixed_mode->hsync_start;
290 adjusted_mode->hsync_end = panel_fixed_mode->hsync_end;
291 adjusted_mode->htotal = panel_fixed_mode->htotal;
292 adjusted_mode->vdisplay = panel_fixed_mode->vdisplay;
293 adjusted_mode->vsync_start = panel_fixed_mode->vsync_start;
294 adjusted_mode->vsync_end = panel_fixed_mode->vsync_end;
295 adjusted_mode->vtotal = panel_fixed_mode->vtotal;
296 adjusted_mode->clock = panel_fixed_mode->clock;
297 drm_mode_set_crtcinfo(adjusted_mode,
298 CRTC_INTERLACE_HALVE_V);
302 * XXX: It would be nice to support lower refresh rates on the
303 * panels to reduce power consumption, and perhaps match the
304 * user's requested refresh rate.
310 static void cdv_intel_lvds_prepare(struct drm_encoder *encoder)
312 struct drm_device *dev = encoder->dev;
313 struct drm_psb_private *dev_priv = dev->dev_private;
314 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
316 if (!gma_power_begin(dev, true))
319 mode_dev->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
320 mode_dev->backlight_duty_cycle = (mode_dev->saveBLC_PWM_CTL &
321 BACKLIGHT_DUTY_CYCLE_MASK);
323 cdv_intel_lvds_set_power(dev, encoder, false);
328 static void cdv_intel_lvds_commit(struct drm_encoder *encoder)
330 struct drm_device *dev = encoder->dev;
331 struct drm_psb_private *dev_priv = dev->dev_private;
332 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
334 if (mode_dev->backlight_duty_cycle == 0)
335 mode_dev->backlight_duty_cycle =
336 cdv_intel_lvds_get_max_backlight(dev);
338 cdv_intel_lvds_set_power(dev, encoder, true);
341 static void cdv_intel_lvds_mode_set(struct drm_encoder *encoder,
342 struct drm_display_mode *mode,
343 struct drm_display_mode *adjusted_mode)
345 struct drm_device *dev = encoder->dev;
346 struct drm_psb_private *dev_priv = dev->dev_private;
347 struct gma_crtc *gma_crtc = to_gma_crtc(encoder->crtc);
351 * The LVDS pin pair will already have been turned on in the
352 * cdv_intel_crtc_mode_set since it has a large impact on the DPLL
357 * Enable automatic panel scaling so that non-native modes fill the
358 * screen. Should be enabled before the pipe is enabled, according to
359 * register description and PRM.
361 if (mode->hdisplay != adjusted_mode->hdisplay ||
362 mode->vdisplay != adjusted_mode->vdisplay)
363 pfit_control = (PFIT_ENABLE | VERT_AUTO_SCALE |
364 HORIZ_AUTO_SCALE | VERT_INTERP_BILINEAR |
365 HORIZ_INTERP_BILINEAR);
369 pfit_control |= gma_crtc->pipe << PFIT_PIPE_SHIFT;
371 if (dev_priv->lvds_dither)
372 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
374 REG_WRITE(PFIT_CONTROL, pfit_control);
378 * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
380 static int cdv_intel_lvds_get_modes(struct drm_connector *connector)
382 struct drm_device *dev = connector->dev;
383 struct drm_psb_private *dev_priv = dev->dev_private;
384 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
385 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
388 ret = psb_intel_ddc_get_modes(connector, &gma_encoder->i2c_bus->adapter);
393 if (mode_dev->panel_fixed_mode != NULL) {
394 struct drm_display_mode *mode =
395 drm_mode_duplicate(dev, mode_dev->panel_fixed_mode);
396 drm_mode_probed_add(connector, mode);
404 * cdv_intel_lvds_destroy - unregister and free LVDS structures
405 * @connector: connector to free
407 * Unregister the DDC bus for this connector then free the driver private
410 static void cdv_intel_lvds_destroy(struct drm_connector *connector)
412 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
414 psb_intel_i2c_destroy(gma_encoder->i2c_bus);
415 drm_connector_unregister(connector);
416 drm_connector_cleanup(connector);
420 static int cdv_intel_lvds_set_property(struct drm_connector *connector,
421 struct drm_property *property,
424 struct drm_encoder *encoder = connector->encoder;
426 if (!strcmp(property->name, "scaling mode") && encoder) {
427 struct gma_crtc *crtc = to_gma_crtc(encoder->crtc);
434 case DRM_MODE_SCALE_FULLSCREEN:
436 case DRM_MODE_SCALE_NO_SCALE:
438 case DRM_MODE_SCALE_ASPECT:
444 if (drm_object_property_get_value(&connector->base,
449 if (curValue == value)
452 if (drm_object_property_set_value(&connector->base,
457 if (crtc->saved_mode.hdisplay != 0 &&
458 crtc->saved_mode.vdisplay != 0) {
459 if (!drm_crtc_helper_set_mode(encoder->crtc,
463 encoder->crtc->primary->fb))
466 } else if (!strcmp(property->name, "backlight") && encoder) {
467 if (drm_object_property_set_value(&connector->base,
472 gma_backlight_set(encoder->dev, value);
473 } else if (!strcmp(property->name, "DPMS") && encoder) {
474 const struct drm_encoder_helper_funcs *helpers =
475 encoder->helper_private;
476 helpers->dpms(encoder, value);
481 static const struct drm_encoder_helper_funcs
482 cdv_intel_lvds_helper_funcs = {
483 .dpms = cdv_intel_lvds_encoder_dpms,
484 .mode_fixup = cdv_intel_lvds_mode_fixup,
485 .prepare = cdv_intel_lvds_prepare,
486 .mode_set = cdv_intel_lvds_mode_set,
487 .commit = cdv_intel_lvds_commit,
490 static const struct drm_connector_helper_funcs
491 cdv_intel_lvds_connector_helper_funcs = {
492 .get_modes = cdv_intel_lvds_get_modes,
493 .mode_valid = cdv_intel_lvds_mode_valid,
494 .best_encoder = gma_best_encoder,
497 static const struct drm_connector_funcs cdv_intel_lvds_connector_funcs = {
498 .dpms = drm_helper_connector_dpms,
499 .fill_modes = drm_helper_probe_single_connector_modes,
500 .set_property = cdv_intel_lvds_set_property,
501 .destroy = cdv_intel_lvds_destroy,
505 * Enumerate the child dev array parsed from VBT to check whether
506 * the LVDS is present.
507 * If it is present, return 1.
508 * If it is not present, return false.
509 * If no child dev is parsed from VBT, it assumes that the LVDS is present.
511 static bool lvds_is_present_in_vbt(struct drm_device *dev,
514 struct drm_psb_private *dev_priv = dev->dev_private;
517 if (!dev_priv->child_dev_num)
520 for (i = 0; i < dev_priv->child_dev_num; i++) {
521 struct child_device_config *child = dev_priv->child_dev + i;
523 /* If the device type is not LFP, continue.
524 * We have to check both the new identifiers as well as the
525 * old for compatibility with some BIOSes.
527 if (child->device_type != DEVICE_TYPE_INT_LFP &&
528 child->device_type != DEVICE_TYPE_LFP)
532 *i2c_pin = child->i2c_pin;
534 /* However, we cannot trust the BIOS writers to populate
535 * the VBT correctly. Since LVDS requires additional
536 * information from AIM blocks, a non-zero addin offset is
537 * a good indicator that the LVDS is actually present.
539 if (child->addin_offset)
542 /* But even then some BIOS writers perform some black magic
543 * and instantiate the device without reference to any
544 * additional data. Trust that if the VBT was written into
545 * the OpRegion then they have validated the LVDS's existence.
547 if (dev_priv->opregion.vbt)
555 * cdv_intel_lvds_init - setup LVDS connectors on this device
558 * Create the connector, register the LVDS DDC bus, and try to figure out what
559 * modes we can display on the LVDS panel (if present).
561 void cdv_intel_lvds_init(struct drm_device *dev,
562 struct psb_intel_mode_device *mode_dev)
564 struct gma_encoder *gma_encoder;
565 struct gma_connector *gma_connector;
566 struct cdv_intel_lvds_priv *lvds_priv;
567 struct drm_connector *connector;
568 struct drm_encoder *encoder;
569 struct drm_display_mode *scan;
570 struct drm_crtc *crtc;
571 struct drm_psb_private *dev_priv = dev->dev_private;
576 if (!dev_priv->lvds_enabled_in_vbt)
579 pin = GMBUS_PORT_PANEL;
580 if (!lvds_is_present_in_vbt(dev, &pin)) {
581 DRM_DEBUG_KMS("LVDS is not present in VBT\n");
585 gma_encoder = kzalloc(sizeof(struct gma_encoder),
590 gma_connector = kzalloc(sizeof(struct gma_connector),
593 goto failed_connector;
595 lvds_priv = kzalloc(sizeof(struct cdv_intel_lvds_priv), GFP_KERNEL);
597 goto failed_lvds_priv;
599 gma_encoder->dev_priv = lvds_priv;
601 connector = &gma_connector->base;
602 gma_connector->save = cdv_intel_lvds_save;
603 gma_connector->restore = cdv_intel_lvds_restore;
604 encoder = &gma_encoder->base;
607 drm_connector_init(dev, connector,
608 &cdv_intel_lvds_connector_funcs,
609 DRM_MODE_CONNECTOR_LVDS);
611 drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_LVDS);
613 gma_connector_attach_encoder(gma_connector, gma_encoder);
614 gma_encoder->type = INTEL_OUTPUT_LVDS;
616 drm_encoder_helper_add(encoder, &cdv_intel_lvds_helper_funcs);
617 drm_connector_helper_add(connector,
618 &cdv_intel_lvds_connector_helper_funcs);
619 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
620 connector->interlace_allowed = false;
621 connector->doublescan_allowed = false;
623 /*Attach connector properties*/
624 drm_object_attach_property(&connector->base,
625 dev->mode_config.scaling_mode_property,
626 DRM_MODE_SCALE_FULLSCREEN);
627 drm_object_attach_property(&connector->base,
628 dev_priv->backlight_property,
629 BRIGHTNESS_MAX_LEVEL);
633 * FIXME: distroy i2c_bus when exit
635 gma_encoder->i2c_bus = psb_intel_i2c_create(dev,
638 if (!gma_encoder->i2c_bus) {
640 &dev->pdev->dev, "I2C bus registration failed.\n");
643 gma_encoder->i2c_bus->slave_addr = 0x2C;
644 dev_priv->lvds_i2c_bus = gma_encoder->i2c_bus;
648 * 1) check for EDID on DDC
649 * 2) check for VBT data
650 * 3) check to see if LVDS is already on
651 * if none of the above, no panel
652 * 4) make sure lid is open
653 * if closed, act like it's not there for now
656 /* Set up the DDC bus. */
657 gma_encoder->ddc_bus = psb_intel_i2c_create(dev,
660 if (!gma_encoder->ddc_bus) {
661 dev_printk(KERN_ERR, &dev->pdev->dev,
662 "DDC bus registration " "failed.\n");
667 * Attempt to get the fixed panel mode from DDC. Assume that the
668 * preferred mode is the right one.
670 mutex_lock(&dev->mode_config.mutex);
671 psb_intel_ddc_get_modes(connector,
672 &gma_encoder->ddc_bus->adapter);
673 list_for_each_entry(scan, &connector->probed_modes, head) {
674 if (scan->type & DRM_MODE_TYPE_PREFERRED) {
675 mode_dev->panel_fixed_mode =
676 drm_mode_duplicate(dev, scan);
677 goto out; /* FIXME: check for quirks */
681 /* Failed to get EDID, what about VBT? do we need this?*/
682 if (dev_priv->lfp_lvds_vbt_mode) {
683 mode_dev->panel_fixed_mode =
684 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
685 if (mode_dev->panel_fixed_mode) {
686 mode_dev->panel_fixed_mode->type |=
687 DRM_MODE_TYPE_PREFERRED;
688 goto out; /* FIXME: check for quirks */
692 * If we didn't get EDID, try checking if the panel is already turned
693 * on. If so, assume that whatever is currently programmed is the
696 lvds = REG_READ(LVDS);
697 pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
698 crtc = psb_intel_get_crtc_from_pipe(dev, pipe);
700 if (crtc && (lvds & LVDS_PORT_EN)) {
701 mode_dev->panel_fixed_mode =
702 cdv_intel_crtc_mode_get(dev, crtc);
703 if (mode_dev->panel_fixed_mode) {
704 mode_dev->panel_fixed_mode->type |=
705 DRM_MODE_TYPE_PREFERRED;
706 goto out; /* FIXME: check for quirks */
710 /* If we still don't have a mode after all that, give up. */
711 if (!mode_dev->panel_fixed_mode) {
713 ("Found no modes on the lvds, ignoring the LVDS\n");
721 pwm = REG_READ(BLC_PWM_CTL2);
727 REG_WRITE(BLC_PWM_CTL2, pwm);
731 mutex_unlock(&dev->mode_config.mutex);
732 drm_connector_register(connector);
736 mutex_unlock(&dev->mode_config.mutex);
737 pr_err("Failed find\n");
738 psb_intel_i2c_destroy(gma_encoder->ddc_bus);
740 pr_err("Failed DDC\n");
741 psb_intel_i2c_destroy(gma_encoder->i2c_bus);
743 pr_err("Failed BLC\n");
744 drm_encoder_cleanup(encoder);
745 drm_connector_cleanup(connector);
748 kfree(gma_connector);