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 "intel_bios.h"
19 #include "psb_intel_drv.h"
20 #include "psb_intel_reg.h"
22 /* The max/min PWM frequency in BPCR[31:17] - */
23 /* The smallest number is 1 (not 0) that can fit in the
24 * 15-bit field of the and then*/
25 /* shifts to the left by one bit to get the actual 16-bit
26 * value that the 15-bits correspond to.*/
27 #define MRST_BLC_MAX_PWM_REG_FREQ 0xFFFF
28 #define BRIGHTNESS_MAX_LEVEL 100
31 * Sets the power state for the panel.
33 static void oaktrail_lvds_set_power(struct drm_device
*dev
,
34 struct gma_encoder
*gma_encoder
,
38 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
40 if (!gma_power_begin(dev
, true))
44 REG_WRITE(PP_CONTROL
, REG_READ(PP_CONTROL
) |
47 pp_status
= REG_READ(PP_STATUS
);
48 } while ((pp_status
& (PP_ON
| PP_READY
)) == PP_READY
);
49 dev_priv
->is_lvds_on
= true;
50 if (dev_priv
->ops
->lvds_bl_power
)
51 dev_priv
->ops
->lvds_bl_power(dev
, true);
53 if (dev_priv
->ops
->lvds_bl_power
)
54 dev_priv
->ops
->lvds_bl_power(dev
, false);
55 REG_WRITE(PP_CONTROL
, REG_READ(PP_CONTROL
) &
58 pp_status
= REG_READ(PP_STATUS
);
59 } while (pp_status
& PP_ON
);
60 dev_priv
->is_lvds_on
= false;
61 pm_request_idle(&dev
->pdev
->dev
);
66 static void oaktrail_lvds_dpms(struct drm_encoder
*encoder
, int mode
)
68 struct drm_device
*dev
= encoder
->dev
;
69 struct gma_encoder
*gma_encoder
= to_gma_encoder(encoder
);
71 if (mode
== DRM_MODE_DPMS_ON
)
72 oaktrail_lvds_set_power(dev
, gma_encoder
, true);
74 oaktrail_lvds_set_power(dev
, gma_encoder
, false);
76 /* XXX: We never power down the LVDS pairs. */
79 static void oaktrail_lvds_mode_set(struct drm_encoder
*encoder
,
80 struct drm_display_mode
*mode
,
81 struct drm_display_mode
*adjusted_mode
)
83 struct drm_device
*dev
= encoder
->dev
;
84 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
85 struct psb_intel_mode_device
*mode_dev
= &dev_priv
->mode_dev
;
86 struct drm_mode_config
*mode_config
= &dev
->mode_config
;
87 struct drm_connector
*connector
= NULL
;
88 struct drm_crtc
*crtc
= encoder
->crtc
;
90 uint64_t v
= DRM_MODE_SCALE_FULLSCREEN
;
92 if (!gma_power_begin(dev
, true))
96 * The LVDS pin pair will already have been turned on in the
97 * psb_intel_crtc_mode_set since it has a large impact on the DPLL
100 lvds_port
= (REG_READ(LVDS
) &
101 (~LVDS_PIPEB_SELECT
)) |
105 /* If the firmware says dither on Moorestown, or the BIOS does
106 on Oaktrail then enable dithering */
107 if (mode_dev
->panel_wants_dither
|| dev_priv
->lvds_dither
)
108 lvds_port
|= MRST_PANEL_8TO6_DITHER_ENABLE
;
110 REG_WRITE(LVDS
, lvds_port
);
112 /* Find the connector we're trying to set up */
113 list_for_each_entry(connector
, &mode_config
->connector_list
, head
) {
114 if (!connector
->encoder
|| connector
->encoder
->crtc
!= crtc
)
119 DRM_ERROR("Couldn't find connector when setting mode");
124 drm_object_property_get_value(
126 dev
->mode_config
.scaling_mode_property
,
129 if (v
== DRM_MODE_SCALE_NO_SCALE
)
130 REG_WRITE(PFIT_CONTROL
, 0);
131 else if (v
== DRM_MODE_SCALE_ASPECT
) {
132 if ((mode
->vdisplay
!= adjusted_mode
->crtc_vdisplay
) ||
133 (mode
->hdisplay
!= adjusted_mode
->crtc_hdisplay
)) {
134 if ((adjusted_mode
->crtc_hdisplay
* mode
->vdisplay
) ==
135 (mode
->hdisplay
* adjusted_mode
->crtc_vdisplay
))
136 REG_WRITE(PFIT_CONTROL
, PFIT_ENABLE
);
137 else if ((adjusted_mode
->crtc_hdisplay
*
138 mode
->vdisplay
) > (mode
->hdisplay
*
139 adjusted_mode
->crtc_vdisplay
))
140 REG_WRITE(PFIT_CONTROL
, PFIT_ENABLE
|
141 PFIT_SCALING_MODE_PILLARBOX
);
143 REG_WRITE(PFIT_CONTROL
, PFIT_ENABLE
|
144 PFIT_SCALING_MODE_LETTERBOX
);
146 REG_WRITE(PFIT_CONTROL
, PFIT_ENABLE
);
147 } else /*(v == DRM_MODE_SCALE_FULLSCREEN)*/
148 REG_WRITE(PFIT_CONTROL
, PFIT_ENABLE
);
153 static void oaktrail_lvds_prepare(struct drm_encoder
*encoder
)
155 struct drm_device
*dev
= encoder
->dev
;
156 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
157 struct gma_encoder
*gma_encoder
= to_gma_encoder(encoder
);
158 struct psb_intel_mode_device
*mode_dev
= &dev_priv
->mode_dev
;
160 if (!gma_power_begin(dev
, true))
163 mode_dev
->saveBLC_PWM_CTL
= REG_READ(BLC_PWM_CTL
);
164 mode_dev
->backlight_duty_cycle
= (mode_dev
->saveBLC_PWM_CTL
&
165 BACKLIGHT_DUTY_CYCLE_MASK
);
166 oaktrail_lvds_set_power(dev
, gma_encoder
, false);
170 static u32
oaktrail_lvds_get_max_backlight(struct drm_device
*dev
)
172 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
175 if (gma_power_begin(dev
, false)) {
176 ret
= ((REG_READ(BLC_PWM_CTL
) &
177 BACKLIGHT_MODULATION_FREQ_MASK
) >>
178 BACKLIGHT_MODULATION_FREQ_SHIFT
) * 2;
182 ret
= ((dev_priv
->regs
.saveBLC_PWM_CTL
&
183 BACKLIGHT_MODULATION_FREQ_MASK
) >>
184 BACKLIGHT_MODULATION_FREQ_SHIFT
) * 2;
189 static void oaktrail_lvds_commit(struct drm_encoder
*encoder
)
191 struct drm_device
*dev
= encoder
->dev
;
192 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
193 struct gma_encoder
*gma_encoder
= to_gma_encoder(encoder
);
194 struct psb_intel_mode_device
*mode_dev
= &dev_priv
->mode_dev
;
196 if (mode_dev
->backlight_duty_cycle
== 0)
197 mode_dev
->backlight_duty_cycle
=
198 oaktrail_lvds_get_max_backlight(dev
);
199 oaktrail_lvds_set_power(dev
, gma_encoder
, true);
202 static const struct drm_encoder_helper_funcs oaktrail_lvds_helper_funcs
= {
203 .dpms
= oaktrail_lvds_dpms
,
204 .mode_fixup
= psb_intel_lvds_mode_fixup
,
205 .prepare
= oaktrail_lvds_prepare
,
206 .mode_set
= oaktrail_lvds_mode_set
,
207 .commit
= oaktrail_lvds_commit
,
210 /* Returns the panel fixed mode from configuration. */
212 static void oaktrail_lvds_get_configuration_mode(struct drm_device
*dev
,
213 struct psb_intel_mode_device
*mode_dev
)
215 struct drm_display_mode
*mode
= NULL
;
216 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
217 struct oaktrail_timing_info
*ti
= &dev_priv
->gct_data
.DTD
;
219 mode_dev
->panel_fixed_mode
= NULL
;
221 /* Use the firmware provided data on Moorestown */
222 if (dev_priv
->has_gct
) {
223 mode
= kzalloc(sizeof(*mode
), GFP_KERNEL
);
227 mode
->hdisplay
= (ti
->hactive_hi
<< 8) | ti
->hactive_lo
;
228 mode
->vdisplay
= (ti
->vactive_hi
<< 8) | ti
->vactive_lo
;
229 mode
->hsync_start
= mode
->hdisplay
+ \
230 ((ti
->hsync_offset_hi
<< 8) | \
231 ti
->hsync_offset_lo
);
232 mode
->hsync_end
= mode
->hsync_start
+ \
233 ((ti
->hsync_pulse_width_hi
<< 8) | \
234 ti
->hsync_pulse_width_lo
);
235 mode
->htotal
= mode
->hdisplay
+ ((ti
->hblank_hi
<< 8) | \
237 mode
->vsync_start
= \
238 mode
->vdisplay
+ ((ti
->vsync_offset_hi
<< 4) | \
239 ti
->vsync_offset_lo
);
241 mode
->vsync_start
+ ((ti
->vsync_pulse_width_hi
<< 4) | \
242 ti
->vsync_pulse_width_lo
);
243 mode
->vtotal
= mode
->vdisplay
+ \
244 ((ti
->vblank_hi
<< 8) | ti
->vblank_lo
);
245 mode
->clock
= ti
->pixel_clock
* 10;
247 pr_info("hdisplay is %d\n", mode
->hdisplay
);
248 pr_info("vdisplay is %d\n", mode
->vdisplay
);
249 pr_info("HSS is %d\n", mode
->hsync_start
);
250 pr_info("HSE is %d\n", mode
->hsync_end
);
251 pr_info("htotal is %d\n", mode
->htotal
);
252 pr_info("VSS is %d\n", mode
->vsync_start
);
253 pr_info("VSE is %d\n", mode
->vsync_end
);
254 pr_info("vtotal is %d\n", mode
->vtotal
);
255 pr_info("clock is %d\n", mode
->clock
);
257 mode_dev
->panel_fixed_mode
= mode
;
260 /* Use the BIOS VBT mode if available */
261 if (mode_dev
->panel_fixed_mode
== NULL
&& mode_dev
->vbt_mode
)
262 mode_dev
->panel_fixed_mode
= drm_mode_duplicate(dev
,
265 /* Then try the LVDS VBT mode */
266 if (mode_dev
->panel_fixed_mode
== NULL
)
267 if (dev_priv
->lfp_lvds_vbt_mode
)
268 mode_dev
->panel_fixed_mode
=
269 drm_mode_duplicate(dev
,
270 dev_priv
->lfp_lvds_vbt_mode
);
272 /* If we still got no mode then bail */
273 if (mode_dev
->panel_fixed_mode
== NULL
)
276 drm_mode_set_name(mode_dev
->panel_fixed_mode
);
277 drm_mode_set_crtcinfo(mode_dev
->panel_fixed_mode
, 0);
281 * oaktrail_lvds_init - setup LVDS connectors on this device
284 * Create the connector, register the LVDS DDC bus, and try to figure out what
285 * modes we can display on the LVDS panel (if present).
287 void oaktrail_lvds_init(struct drm_device
*dev
,
288 struct psb_intel_mode_device
*mode_dev
)
290 struct gma_encoder
*gma_encoder
;
291 struct gma_connector
*gma_connector
;
292 struct drm_connector
*connector
;
293 struct drm_encoder
*encoder
;
294 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
296 struct i2c_adapter
*i2c_adap
;
297 struct drm_display_mode
*scan
; /* *modes, *bios_mode; */
299 gma_encoder
= kzalloc(sizeof(struct gma_encoder
), GFP_KERNEL
);
303 gma_connector
= kzalloc(sizeof(struct gma_connector
), GFP_KERNEL
);
305 goto failed_connector
;
307 connector
= &gma_connector
->base
;
308 encoder
= &gma_encoder
->base
;
309 dev_priv
->is_lvds_on
= true;
310 drm_connector_init(dev
, connector
,
311 &psb_intel_lvds_connector_funcs
,
312 DRM_MODE_CONNECTOR_LVDS
);
314 drm_encoder_init(dev
, encoder
, &psb_intel_lvds_enc_funcs
,
315 DRM_MODE_ENCODER_LVDS
, NULL
);
317 gma_connector_attach_encoder(gma_connector
, gma_encoder
);
318 gma_encoder
->type
= INTEL_OUTPUT_LVDS
;
320 drm_encoder_helper_add(encoder
, &oaktrail_lvds_helper_funcs
);
321 drm_connector_helper_add(connector
,
322 &psb_intel_lvds_connector_helper_funcs
);
323 connector
->display_info
.subpixel_order
= SubPixelHorizontalRGB
;
324 connector
->interlace_allowed
= false;
325 connector
->doublescan_allowed
= false;
327 drm_object_attach_property(&connector
->base
,
328 dev
->mode_config
.scaling_mode_property
,
329 DRM_MODE_SCALE_FULLSCREEN
);
330 drm_object_attach_property(&connector
->base
,
331 dev_priv
->backlight_property
,
332 BRIGHTNESS_MAX_LEVEL
);
334 mode_dev
->panel_wants_dither
= false;
335 if (dev_priv
->has_gct
)
336 mode_dev
->panel_wants_dither
= (dev_priv
->gct_data
.
337 Panel_Port_Control
& MRST_PANEL_8TO6_DITHER_ENABLE
);
338 if (dev_priv
->lvds_dither
)
339 mode_dev
->panel_wants_dither
= 1;
343 * 1) check for EDID on DDC
344 * 2) check for VBT data
345 * 3) check to see if LVDS is already on
346 * if none of the above, no panel
347 * 4) make sure lid is open
348 * if closed, act like it's not there for now
352 mutex_lock(&dev
->mode_config
.mutex
);
353 i2c_adap
= i2c_get_adapter(dev_priv
->ops
->i2c_bus
);
355 edid
= drm_get_edid(connector
, i2c_adap
);
356 if (edid
== NULL
&& dev_priv
->lpc_gpio_base
) {
357 oaktrail_lvds_i2c_init(encoder
);
358 if (gma_encoder
->ddc_bus
!= NULL
) {
359 i2c_adap
= &gma_encoder
->ddc_bus
->adapter
;
360 edid
= drm_get_edid(connector
, i2c_adap
);
364 * Attempt to get the fixed panel mode from DDC. Assume that the
365 * preferred mode is the right one.
368 drm_connector_update_edid_property(connector
, edid
);
369 drm_add_edid_modes(connector
, edid
);
372 list_for_each_entry(scan
, &connector
->probed_modes
, head
) {
373 if (scan
->type
& DRM_MODE_TYPE_PREFERRED
) {
374 mode_dev
->panel_fixed_mode
=
375 drm_mode_duplicate(dev
, scan
);
376 goto out
; /* FIXME: check for quirks */
380 dev_err(dev
->dev
, "No ddc adapter available!\n");
382 * If we didn't get EDID, try geting panel timing
383 * from configuration data
385 oaktrail_lvds_get_configuration_mode(dev
, mode_dev
);
387 if (mode_dev
->panel_fixed_mode
) {
388 mode_dev
->panel_fixed_mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
389 goto out
; /* FIXME: check for quirks */
392 /* If we still don't have a mode after all that, give up. */
393 if (!mode_dev
->panel_fixed_mode
) {
394 dev_err(dev
->dev
, "Found no modes on the lvds, ignoring the LVDS\n");
399 mutex_unlock(&dev
->mode_config
.mutex
);
401 drm_connector_register(connector
);
405 mutex_unlock(&dev
->mode_config
.mutex
);
407 dev_dbg(dev
->dev
, "No LVDS modes found, disabling.\n");
408 if (gma_encoder
->ddc_bus
) {
409 psb_intel_i2c_destroy(gma_encoder
->ddc_bus
);
410 gma_encoder
->ddc_bus
= NULL
;
415 drm_encoder_cleanup(encoder
);
416 drm_connector_cleanup(connector
);
417 kfree(gma_connector
);