1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2006-2009 Intel Corporation
6 * Eric Anholt <eric@anholt.net>
7 * Dave Airlie <airlied@linux.ie>
8 * Jesse Barnes <jesse.barnes@intel.com>
11 #include <linux/i2c.h>
12 #include <linux/pm_runtime.h>
14 #include <asm/intel-mid.h>
16 #include <drm/drm_edid.h>
17 #include <drm/drm_simple_kms_helper.h>
19 #include "intel_bios.h"
22 #include "psb_intel_drv.h"
23 #include "psb_intel_reg.h"
25 /* The max/min PWM frequency in BPCR[31:17] - */
26 /* The smallest number is 1 (not 0) that can fit in the
27 * 15-bit field of the and then*/
28 /* shifts to the left by one bit to get the actual 16-bit
29 * value that the 15-bits correspond to.*/
30 #define MRST_BLC_MAX_PWM_REG_FREQ 0xFFFF
31 #define BRIGHTNESS_MAX_LEVEL 100
34 * Sets the power state for the panel.
36 static void oaktrail_lvds_set_power(struct drm_device *dev,
37 struct gma_encoder *gma_encoder,
41 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
43 if (!gma_power_begin(dev, true))
47 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) |
50 pp_status = REG_READ(PP_STATUS);
51 } while ((pp_status & (PP_ON | PP_READY)) == PP_READY);
52 dev_priv->is_lvds_on = true;
53 if (dev_priv->ops->lvds_bl_power)
54 dev_priv->ops->lvds_bl_power(dev, true);
56 if (dev_priv->ops->lvds_bl_power)
57 dev_priv->ops->lvds_bl_power(dev, false);
58 REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) &
61 pp_status = REG_READ(PP_STATUS);
62 } while (pp_status & PP_ON);
63 dev_priv->is_lvds_on = false;
64 pm_request_idle(dev->dev);
69 static void oaktrail_lvds_dpms(struct drm_encoder *encoder, int mode)
71 struct drm_device *dev = encoder->dev;
72 struct gma_encoder *gma_encoder = to_gma_encoder(encoder);
74 if (mode == DRM_MODE_DPMS_ON)
75 oaktrail_lvds_set_power(dev, gma_encoder, true);
77 oaktrail_lvds_set_power(dev, gma_encoder, false);
79 /* XXX: We never power down the LVDS pairs. */
82 static void oaktrail_lvds_mode_set(struct drm_encoder *encoder,
83 struct drm_display_mode *mode,
84 struct drm_display_mode *adjusted_mode)
86 struct drm_device *dev = encoder->dev;
87 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
88 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
89 struct drm_connector_list_iter conn_iter;
90 struct drm_connector *connector = NULL;
91 struct drm_crtc *crtc = encoder->crtc;
93 uint64_t v = DRM_MODE_SCALE_FULLSCREEN;
95 if (!gma_power_begin(dev, true))
99 * The LVDS pin pair will already have been turned on in the
100 * psb_intel_crtc_mode_set since it has a large impact on the DPLL
103 lvds_port = (REG_READ(LVDS) &
104 (~LVDS_PIPEB_SELECT)) |
108 /* If the firmware says dither on Moorestown, or the BIOS does
109 on Oaktrail then enable dithering */
110 if (mode_dev->panel_wants_dither || dev_priv->lvds_dither)
111 lvds_port |= MRST_PANEL_8TO6_DITHER_ENABLE;
113 REG_WRITE(LVDS, lvds_port);
115 /* Find the connector we're trying to set up */
116 drm_connector_list_iter_begin(dev, &conn_iter);
117 drm_for_each_connector_iter(connector, &conn_iter) {
118 if (connector->encoder && connector->encoder->crtc == crtc)
123 drm_connector_list_iter_end(&conn_iter);
124 DRM_ERROR("Couldn't find connector when setting mode");
129 drm_object_property_get_value( &connector->base,
130 dev->mode_config.scaling_mode_property, &v);
131 drm_connector_list_iter_end(&conn_iter);
133 if (v == DRM_MODE_SCALE_NO_SCALE)
134 REG_WRITE(PFIT_CONTROL, 0);
135 else if (v == DRM_MODE_SCALE_ASPECT) {
136 if ((mode->vdisplay != adjusted_mode->crtc_vdisplay) ||
137 (mode->hdisplay != adjusted_mode->crtc_hdisplay)) {
138 if ((adjusted_mode->crtc_hdisplay * mode->vdisplay) ==
139 (mode->hdisplay * adjusted_mode->crtc_vdisplay))
140 REG_WRITE(PFIT_CONTROL, PFIT_ENABLE);
141 else if ((adjusted_mode->crtc_hdisplay *
142 mode->vdisplay) > (mode->hdisplay *
143 adjusted_mode->crtc_vdisplay))
144 REG_WRITE(PFIT_CONTROL, PFIT_ENABLE |
145 PFIT_SCALING_MODE_PILLARBOX);
147 REG_WRITE(PFIT_CONTROL, PFIT_ENABLE |
148 PFIT_SCALING_MODE_LETTERBOX);
150 REG_WRITE(PFIT_CONTROL, PFIT_ENABLE);
151 } else /*(v == DRM_MODE_SCALE_FULLSCREEN)*/
152 REG_WRITE(PFIT_CONTROL, PFIT_ENABLE);
157 static void oaktrail_lvds_prepare(struct drm_encoder *encoder)
159 struct drm_device *dev = encoder->dev;
160 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
161 struct gma_encoder *gma_encoder = to_gma_encoder(encoder);
162 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
164 if (!gma_power_begin(dev, true))
167 mode_dev->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
168 mode_dev->backlight_duty_cycle = (mode_dev->saveBLC_PWM_CTL &
169 BACKLIGHT_DUTY_CYCLE_MASK);
170 oaktrail_lvds_set_power(dev, gma_encoder, false);
174 static u32 oaktrail_lvds_get_max_backlight(struct drm_device *dev)
176 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
179 if (gma_power_begin(dev, false)) {
180 ret = ((REG_READ(BLC_PWM_CTL) &
181 BACKLIGHT_MODULATION_FREQ_MASK) >>
182 BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
186 ret = ((dev_priv->regs.saveBLC_PWM_CTL &
187 BACKLIGHT_MODULATION_FREQ_MASK) >>
188 BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
193 static void oaktrail_lvds_commit(struct drm_encoder *encoder)
195 struct drm_device *dev = encoder->dev;
196 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
197 struct gma_encoder *gma_encoder = to_gma_encoder(encoder);
198 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
200 if (mode_dev->backlight_duty_cycle == 0)
201 mode_dev->backlight_duty_cycle =
202 oaktrail_lvds_get_max_backlight(dev);
203 oaktrail_lvds_set_power(dev, gma_encoder, true);
206 static const struct drm_encoder_helper_funcs oaktrail_lvds_helper_funcs = {
207 .dpms = oaktrail_lvds_dpms,
208 .mode_fixup = psb_intel_lvds_mode_fixup,
209 .prepare = oaktrail_lvds_prepare,
210 .mode_set = oaktrail_lvds_mode_set,
211 .commit = oaktrail_lvds_commit,
214 /* Returns the panel fixed mode from configuration. */
216 static void oaktrail_lvds_get_configuration_mode(struct drm_device *dev,
217 struct psb_intel_mode_device *mode_dev)
219 struct drm_display_mode *mode = NULL;
220 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
221 struct oaktrail_timing_info *ti = &dev_priv->gct_data.DTD;
223 mode_dev->panel_fixed_mode = NULL;
225 /* Use the firmware provided data on Moorestown */
226 if (dev_priv->has_gct) {
227 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
231 mode->hdisplay = (ti->hactive_hi << 8) | ti->hactive_lo;
232 mode->vdisplay = (ti->vactive_hi << 8) | ti->vactive_lo;
233 mode->hsync_start = mode->hdisplay + \
234 ((ti->hsync_offset_hi << 8) | \
235 ti->hsync_offset_lo);
236 mode->hsync_end = mode->hsync_start + \
237 ((ti->hsync_pulse_width_hi << 8) | \
238 ti->hsync_pulse_width_lo);
239 mode->htotal = mode->hdisplay + ((ti->hblank_hi << 8) | \
241 mode->vsync_start = \
242 mode->vdisplay + ((ti->vsync_offset_hi << 4) | \
243 ti->vsync_offset_lo);
245 mode->vsync_start + ((ti->vsync_pulse_width_hi << 4) | \
246 ti->vsync_pulse_width_lo);
247 mode->vtotal = mode->vdisplay + \
248 ((ti->vblank_hi << 8) | ti->vblank_lo);
249 mode->clock = ti->pixel_clock * 10;
251 pr_info("hdisplay is %d\n", mode->hdisplay);
252 pr_info("vdisplay is %d\n", mode->vdisplay);
253 pr_info("HSS is %d\n", mode->hsync_start);
254 pr_info("HSE is %d\n", mode->hsync_end);
255 pr_info("htotal is %d\n", mode->htotal);
256 pr_info("VSS is %d\n", mode->vsync_start);
257 pr_info("VSE is %d\n", mode->vsync_end);
258 pr_info("vtotal is %d\n", mode->vtotal);
259 pr_info("clock is %d\n", mode->clock);
261 mode_dev->panel_fixed_mode = mode;
264 /* Use the BIOS VBT mode if available */
265 if (mode_dev->panel_fixed_mode == NULL && mode_dev->vbt_mode)
266 mode_dev->panel_fixed_mode = drm_mode_duplicate(dev,
269 /* Then try the LVDS VBT mode */
270 if (mode_dev->panel_fixed_mode == NULL)
271 if (dev_priv->lfp_lvds_vbt_mode)
272 mode_dev->panel_fixed_mode =
273 drm_mode_duplicate(dev,
274 dev_priv->lfp_lvds_vbt_mode);
276 /* If we still got no mode then bail */
277 if (mode_dev->panel_fixed_mode == NULL)
280 drm_mode_set_name(mode_dev->panel_fixed_mode);
281 drm_mode_set_crtcinfo(mode_dev->panel_fixed_mode, 0);
285 * oaktrail_lvds_init - setup LVDS connectors on this device
287 * @mode_dev: PSB mode device
289 * Create the connector, register the LVDS DDC bus, and try to figure out what
290 * modes we can display on the LVDS panel (if present).
292 void oaktrail_lvds_init(struct drm_device *dev,
293 struct psb_intel_mode_device *mode_dev)
295 struct gma_encoder *gma_encoder;
296 struct gma_connector *gma_connector;
297 struct gma_i2c_chan *ddc_bus;
298 struct drm_connector *connector;
299 struct drm_encoder *encoder;
300 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
302 struct i2c_adapter *i2c_adap;
303 struct drm_display_mode *scan; /* *modes, *bios_mode; */
306 gma_encoder = kzalloc(sizeof(struct gma_encoder), GFP_KERNEL);
310 gma_connector = kzalloc(sizeof(struct gma_connector), GFP_KERNEL);
312 goto err_free_encoder;
314 connector = &gma_connector->base;
315 encoder = &gma_encoder->base;
316 dev_priv->is_lvds_on = true;
317 ret = drm_connector_init(dev, connector,
318 &psb_intel_lvds_connector_funcs,
319 DRM_MODE_CONNECTOR_LVDS);
321 goto err_free_connector;
323 ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_LVDS);
325 goto err_connector_cleanup;
327 gma_connector_attach_encoder(gma_connector, gma_encoder);
328 gma_encoder->type = INTEL_OUTPUT_LVDS;
330 drm_encoder_helper_add(encoder, &oaktrail_lvds_helper_funcs);
331 drm_connector_helper_add(connector,
332 &psb_intel_lvds_connector_helper_funcs);
333 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
334 connector->interlace_allowed = false;
335 connector->doublescan_allowed = false;
337 drm_object_attach_property(&connector->base,
338 dev->mode_config.scaling_mode_property,
339 DRM_MODE_SCALE_FULLSCREEN);
340 drm_object_attach_property(&connector->base,
341 dev_priv->backlight_property,
342 BRIGHTNESS_MAX_LEVEL);
344 mode_dev->panel_wants_dither = false;
345 if (dev_priv->has_gct)
346 mode_dev->panel_wants_dither = (dev_priv->gct_data.
347 Panel_Port_Control & MRST_PANEL_8TO6_DITHER_ENABLE);
348 if (dev_priv->lvds_dither)
349 mode_dev->panel_wants_dither = 1;
353 * 1) check for EDID on DDC
354 * 2) check for VBT data
355 * 3) check to see if LVDS is already on
356 * if none of the above, no panel
357 * 4) make sure lid is open
358 * if closed, act like it's not there for now
362 mutex_lock(&dev->mode_config.mutex);
364 i2c_adap = i2c_get_adapter(dev_priv->ops->i2c_bus);
366 edid = drm_get_edid(connector, i2c_adap);
368 if (edid == NULL && dev_priv->lpc_gpio_base) {
369 ddc_bus = oaktrail_lvds_i2c_init(dev);
370 if (!IS_ERR(ddc_bus)) {
371 i2c_adap = &ddc_bus->base;
372 edid = drm_get_edid(connector, i2c_adap);
377 * Due to the logic in probing for i2c buses above we do not know the
378 * i2c_adap until now. Hence we cannot use drm_connector_init_with_ddc()
379 * but must instead set connector->ddc manually here.
381 connector->ddc = i2c_adap;
384 * Attempt to get the fixed panel mode from DDC. Assume that the
385 * preferred mode is the right one.
388 drm_connector_update_edid_property(connector, edid);
389 drm_add_edid_modes(connector, edid);
392 list_for_each_entry(scan, &connector->probed_modes, head) {
393 if (scan->type & DRM_MODE_TYPE_PREFERRED) {
394 mode_dev->panel_fixed_mode =
395 drm_mode_duplicate(dev, scan);
396 goto out; /* FIXME: check for quirks */
400 dev_err(dev->dev, "No ddc adapter available!\n");
402 * If we didn't get EDID, try geting panel timing
403 * from configuration data
405 oaktrail_lvds_get_configuration_mode(dev, mode_dev);
407 if (mode_dev->panel_fixed_mode) {
408 mode_dev->panel_fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
409 goto out; /* FIXME: check for quirks */
412 /* If we still don't have a mode after all that, give up. */
413 if (!mode_dev->panel_fixed_mode) {
414 dev_err(dev->dev, "Found no modes on the lvds, ignoring the LVDS\n");
419 mutex_unlock(&dev->mode_config.mutex);
424 mutex_unlock(&dev->mode_config.mutex);
425 gma_i2c_destroy(to_gma_i2c_chan(connector->ddc));
426 drm_encoder_cleanup(encoder);
427 err_connector_cleanup:
428 drm_connector_cleanup(connector);
430 kfree(gma_connector);