Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / gma500 / cdv_intel_lvds.c
blobeaaf4efec21765a494c3bc2ef6750bc4cd080b74
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright © 2006-2011 Intel Corporation
5 * Authors:
6 * Eric Anholt <eric@anholt.net>
7 * Dave Airlie <airlied@linux.ie>
8 * Jesse Barnes <jesse.barnes@intel.com>
9 */
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"
19 #include "power.h"
20 #include "psb_drv.h"
21 #include "psb_intel_drv.h"
22 #include "psb_intel_reg.h"
24 /**
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 {
42 /**
43 * Saved LVDO output states
45 uint32_t savePP_ON;
46 uint32_t savePP_OFF;
47 uint32_t saveLVDS;
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;
61 u32 retval;
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;
68 gma_power_end(dev);
69 } else
70 retval = ((dev_priv->regs.saveBLC_PWM_CTL &
71 BACKLIGHT_MODULATION_FREQ_MASK) >>
72 BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
74 return retval;
77 /**
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 = dev->dev_private;
85 u32 blc_pwm_ctl;
87 if (gma_power_begin(dev, false)) {
88 blc_pwm_ctl =
89 REG_READ(BLC_PWM_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
90 REG_WRITE(BLC_PWM_CTL,
91 (blc_pwm_ctl |
92 (level << BACKLIGHT_DUTY_CYCLE_SHIFT)));
93 gma_power_end(dev);
94 } else {
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 = dev->dev_private;
109 u32 pp_status;
111 if (!gma_power_begin(dev, true))
112 return;
114 if (on) {
115 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) |
116 POWER_TARGET_ON);
117 do {
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);
123 } else {
124 cdv_intel_lvds_set_backlight(dev, 0);
126 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) &
127 ~POWER_TARGET_ON);
128 do {
129 pp_status = REG_READ(PP_STATUS);
130 } while (pp_status & PP_ON);
132 gma_power_end(dev);
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);
140 else
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 = dev->dev_private;
158 struct drm_display_mode *fixed_mode =
159 dev_priv->mode_dev.panel_fixed_mode;
161 /* just in case */
162 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
163 return MODE_NO_DBLESCAN;
165 /* just in case */
166 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
167 return MODE_NO_INTERLACE;
169 if (fixed_mode) {
170 if (mode->hdisplay > fixed_mode->hdisplay)
171 return MODE_PANEL;
172 if (mode->vdisplay > fixed_mode->vdisplay)
173 return MODE_PANEL;
175 return MODE_OK;
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 = dev->dev_private;
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,
190 head) {
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");
194 return false;
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.
224 return true;
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 = dev->dev_private;
231 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
233 if (!gma_power_begin(dev, true))
234 return;
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);
242 gma_power_end(dev);
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 = dev->dev_private;
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 = dev->dev_private;
264 struct gma_crtc *gma_crtc = to_gma_crtc(encoder->crtc);
265 u32 pfit_control;
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
270 * settings.
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);
283 else
284 pfit_control = 0;
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 = dev->dev_private;
301 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
302 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
303 int ret;
305 ret = psb_intel_ddc_get_modes(connector, &gma_encoder->i2c_bus->adapter);
307 if (ret)
308 return ret;
310 if (mode_dev->panel_fixed_mode != NULL) {
311 struct drm_display_mode *mode =
312 drm_mode_duplicate(dev, mode_dev->panel_fixed_mode);
313 drm_mode_probed_add(connector, mode);
314 return 1;
317 return 0;
321 * cdv_intel_lvds_destroy - unregister and free LVDS structures
322 * @connector: connector to free
324 * Unregister the DDC bus for this connector then free the driver private
325 * structure.
327 static void cdv_intel_lvds_destroy(struct drm_connector *connector)
329 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
331 psb_intel_i2c_destroy(gma_encoder->i2c_bus);
332 drm_connector_unregister(connector);
333 drm_connector_cleanup(connector);
334 kfree(connector);
337 static int cdv_intel_lvds_set_property(struct drm_connector *connector,
338 struct drm_property *property,
339 uint64_t value)
341 struct drm_encoder *encoder = connector->encoder;
343 if (!strcmp(property->name, "scaling mode") && encoder) {
344 struct gma_crtc *crtc = to_gma_crtc(encoder->crtc);
345 uint64_t curValue;
347 if (!crtc)
348 return -1;
350 switch (value) {
351 case DRM_MODE_SCALE_FULLSCREEN:
352 break;
353 case DRM_MODE_SCALE_NO_SCALE:
354 break;
355 case DRM_MODE_SCALE_ASPECT:
356 break;
357 default:
358 return -1;
361 if (drm_object_property_get_value(&connector->base,
362 property,
363 &curValue))
364 return -1;
366 if (curValue == value)
367 return 0;
369 if (drm_object_property_set_value(&connector->base,
370 property,
371 value))
372 return -1;
374 if (crtc->saved_mode.hdisplay != 0 &&
375 crtc->saved_mode.vdisplay != 0) {
376 if (!drm_crtc_helper_set_mode(encoder->crtc,
377 &crtc->saved_mode,
378 encoder->crtc->x,
379 encoder->crtc->y,
380 encoder->crtc->primary->fb))
381 return -1;
383 } else if (!strcmp(property->name, "backlight") && encoder) {
384 if (drm_object_property_set_value(&connector->base,
385 property,
386 value))
387 return -1;
388 else
389 gma_backlight_set(encoder->dev, value);
390 } else if (!strcmp(property->name, "DPMS") && encoder) {
391 const struct drm_encoder_helper_funcs *helpers =
392 encoder->helper_private;
393 helpers->dpms(encoder, value);
395 return 0;
398 static const struct drm_encoder_helper_funcs
399 cdv_intel_lvds_helper_funcs = {
400 .dpms = cdv_intel_lvds_encoder_dpms,
401 .mode_fixup = cdv_intel_lvds_mode_fixup,
402 .prepare = cdv_intel_lvds_prepare,
403 .mode_set = cdv_intel_lvds_mode_set,
404 .commit = cdv_intel_lvds_commit,
407 static const struct drm_connector_helper_funcs
408 cdv_intel_lvds_connector_helper_funcs = {
409 .get_modes = cdv_intel_lvds_get_modes,
410 .mode_valid = cdv_intel_lvds_mode_valid,
411 .best_encoder = gma_best_encoder,
414 static const struct drm_connector_funcs cdv_intel_lvds_connector_funcs = {
415 .dpms = drm_helper_connector_dpms,
416 .fill_modes = drm_helper_probe_single_connector_modes,
417 .set_property = cdv_intel_lvds_set_property,
418 .destroy = cdv_intel_lvds_destroy,
422 * Enumerate the child dev array parsed from VBT to check whether
423 * the LVDS is present.
424 * If it is present, return 1.
425 * If it is not present, return false.
426 * If no child dev is parsed from VBT, it assumes that the LVDS is present.
428 static bool lvds_is_present_in_vbt(struct drm_device *dev,
429 u8 *i2c_pin)
431 struct drm_psb_private *dev_priv = dev->dev_private;
432 int i;
434 if (!dev_priv->child_dev_num)
435 return true;
437 for (i = 0; i < dev_priv->child_dev_num; i++) {
438 struct child_device_config *child = dev_priv->child_dev + i;
440 /* If the device type is not LFP, continue.
441 * We have to check both the new identifiers as well as the
442 * old for compatibility with some BIOSes.
444 if (child->device_type != DEVICE_TYPE_INT_LFP &&
445 child->device_type != DEVICE_TYPE_LFP)
446 continue;
448 if (child->i2c_pin)
449 *i2c_pin = child->i2c_pin;
451 /* However, we cannot trust the BIOS writers to populate
452 * the VBT correctly. Since LVDS requires additional
453 * information from AIM blocks, a non-zero addin offset is
454 * a good indicator that the LVDS is actually present.
456 if (child->addin_offset)
457 return true;
459 /* But even then some BIOS writers perform some black magic
460 * and instantiate the device without reference to any
461 * additional data. Trust that if the VBT was written into
462 * the OpRegion then they have validated the LVDS's existence.
464 if (dev_priv->opregion.vbt)
465 return true;
468 return false;
472 * cdv_intel_lvds_init - setup LVDS connectors on this device
473 * @dev: drm device
475 * Create the connector, register the LVDS DDC bus, and try to figure out what
476 * modes we can display on the LVDS panel (if present).
478 void cdv_intel_lvds_init(struct drm_device *dev,
479 struct psb_intel_mode_device *mode_dev)
481 struct gma_encoder *gma_encoder;
482 struct gma_connector *gma_connector;
483 struct cdv_intel_lvds_priv *lvds_priv;
484 struct drm_connector *connector;
485 struct drm_encoder *encoder;
486 struct drm_display_mode *scan;
487 struct drm_crtc *crtc;
488 struct drm_psb_private *dev_priv = dev->dev_private;
489 u32 lvds;
490 int pipe;
491 u8 pin;
493 if (!dev_priv->lvds_enabled_in_vbt)
494 return;
496 pin = GMBUS_PORT_PANEL;
497 if (!lvds_is_present_in_vbt(dev, &pin)) {
498 DRM_DEBUG_KMS("LVDS is not present in VBT\n");
499 return;
502 gma_encoder = kzalloc(sizeof(struct gma_encoder),
503 GFP_KERNEL);
504 if (!gma_encoder)
505 return;
507 gma_connector = kzalloc(sizeof(struct gma_connector),
508 GFP_KERNEL);
509 if (!gma_connector)
510 goto failed_connector;
512 lvds_priv = kzalloc(sizeof(struct cdv_intel_lvds_priv), GFP_KERNEL);
513 if (!lvds_priv)
514 goto failed_lvds_priv;
516 gma_encoder->dev_priv = lvds_priv;
518 connector = &gma_connector->base;
519 gma_connector->save = cdv_intel_lvds_save;
520 gma_connector->restore = cdv_intel_lvds_restore;
521 encoder = &gma_encoder->base;
524 drm_connector_init(dev, connector,
525 &cdv_intel_lvds_connector_funcs,
526 DRM_MODE_CONNECTOR_LVDS);
528 drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_LVDS);
530 gma_connector_attach_encoder(gma_connector, gma_encoder);
531 gma_encoder->type = INTEL_OUTPUT_LVDS;
533 drm_encoder_helper_add(encoder, &cdv_intel_lvds_helper_funcs);
534 drm_connector_helper_add(connector,
535 &cdv_intel_lvds_connector_helper_funcs);
536 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
537 connector->interlace_allowed = false;
538 connector->doublescan_allowed = false;
540 /*Attach connector properties*/
541 drm_object_attach_property(&connector->base,
542 dev->mode_config.scaling_mode_property,
543 DRM_MODE_SCALE_FULLSCREEN);
544 drm_object_attach_property(&connector->base,
545 dev_priv->backlight_property,
546 BRIGHTNESS_MAX_LEVEL);
549 * Set up I2C bus
550 * FIXME: distroy i2c_bus when exit
552 gma_encoder->i2c_bus = psb_intel_i2c_create(dev,
553 GPIOB,
554 "LVDSBLC_B");
555 if (!gma_encoder->i2c_bus) {
556 dev_printk(KERN_ERR,
557 &dev->pdev->dev, "I2C bus registration failed.\n");
558 goto failed_blc_i2c;
560 gma_encoder->i2c_bus->slave_addr = 0x2C;
561 dev_priv->lvds_i2c_bus = gma_encoder->i2c_bus;
564 * LVDS discovery:
565 * 1) check for EDID on DDC
566 * 2) check for VBT data
567 * 3) check to see if LVDS is already on
568 * if none of the above, no panel
569 * 4) make sure lid is open
570 * if closed, act like it's not there for now
573 /* Set up the DDC bus. */
574 gma_encoder->ddc_bus = psb_intel_i2c_create(dev,
575 GPIOC,
576 "LVDSDDC_C");
577 if (!gma_encoder->ddc_bus) {
578 dev_printk(KERN_ERR, &dev->pdev->dev,
579 "DDC bus registration " "failed.\n");
580 goto failed_ddc;
584 * Attempt to get the fixed panel mode from DDC. Assume that the
585 * preferred mode is the right one.
587 mutex_lock(&dev->mode_config.mutex);
588 psb_intel_ddc_get_modes(connector,
589 &gma_encoder->ddc_bus->adapter);
590 list_for_each_entry(scan, &connector->probed_modes, head) {
591 if (scan->type & DRM_MODE_TYPE_PREFERRED) {
592 mode_dev->panel_fixed_mode =
593 drm_mode_duplicate(dev, scan);
594 goto out; /* FIXME: check for quirks */
598 /* Failed to get EDID, what about VBT? do we need this?*/
599 if (dev_priv->lfp_lvds_vbt_mode) {
600 mode_dev->panel_fixed_mode =
601 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
602 if (mode_dev->panel_fixed_mode) {
603 mode_dev->panel_fixed_mode->type |=
604 DRM_MODE_TYPE_PREFERRED;
605 goto out; /* FIXME: check for quirks */
609 * If we didn't get EDID, try checking if the panel is already turned
610 * on. If so, assume that whatever is currently programmed is the
611 * correct mode.
613 lvds = REG_READ(LVDS);
614 pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
615 crtc = psb_intel_get_crtc_from_pipe(dev, pipe);
617 if (crtc && (lvds & LVDS_PORT_EN)) {
618 mode_dev->panel_fixed_mode =
619 cdv_intel_crtc_mode_get(dev, crtc);
620 if (mode_dev->panel_fixed_mode) {
621 mode_dev->panel_fixed_mode->type |=
622 DRM_MODE_TYPE_PREFERRED;
623 goto out; /* FIXME: check for quirks */
627 /* If we still don't have a mode after all that, give up. */
628 if (!mode_dev->panel_fixed_mode) {
629 DRM_DEBUG
630 ("Found no modes on the lvds, ignoring the LVDS\n");
631 goto failed_find;
634 /* setup PWM */
636 u32 pwm;
638 pwm = REG_READ(BLC_PWM_CTL2);
639 if (pipe == 1)
640 pwm |= PWM_PIPE_B;
641 else
642 pwm &= ~PWM_PIPE_B;
643 pwm |= PWM_ENABLE;
644 REG_WRITE(BLC_PWM_CTL2, pwm);
647 out:
648 mutex_unlock(&dev->mode_config.mutex);
649 drm_connector_register(connector);
650 return;
652 failed_find:
653 mutex_unlock(&dev->mode_config.mutex);
654 pr_err("Failed find\n");
655 psb_intel_i2c_destroy(gma_encoder->ddc_bus);
656 failed_ddc:
657 pr_err("Failed DDC\n");
658 psb_intel_i2c_destroy(gma_encoder->i2c_bus);
659 failed_blc_i2c:
660 pr_err("Failed BLC\n");
661 drm_encoder_cleanup(encoder);
662 drm_connector_cleanup(connector);
663 kfree(lvds_priv);
664 failed_lvds_priv:
665 kfree(gma_connector);
666 failed_connector:
667 kfree(gma_encoder);