Alan Cox | 54edcea | 2011-03-30 09:59:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2006-2009 Intel Corporation |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | * |
| 17 | * Authors: |
| 18 | * Eric Anholt <eric@anholt.net> |
| 19 | * Dave Airlie <airlied@linux.ie> |
| 20 | * Jesse Barnes <jesse.barnes@intel.com> |
| 21 | */ |
| 22 | |
| 23 | #include <linux/i2c.h> |
| 24 | #include <drm/drmP.h> |
| 25 | |
| 26 | #include "psb_intel_bios.h" |
| 27 | #include "psb_drv.h" |
| 28 | #include "psb_intel_drv.h" |
| 29 | #include "psb_intel_reg.h" |
| 30 | #include "psb_powermgmt.h" |
| 31 | #include <linux/pm_runtime.h> |
| 32 | |
| 33 | /* The max/min PWM frequency in BPCR[31:17] - */ |
| 34 | /* The smallest number is 1 (not 0) that can fit in the |
| 35 | * 15-bit field of the and then*/ |
| 36 | /* shifts to the left by one bit to get the actual 16-bit |
| 37 | * value that the 15-bits correspond to.*/ |
| 38 | #define MRST_BLC_MAX_PWM_REG_FREQ 0xFFFF |
| 39 | #define BRIGHTNESS_MAX_LEVEL 100 |
| 40 | |
| 41 | /** |
| 42 | * Sets the power state for the panel. |
| 43 | */ |
| 44 | static void mrst_lvds_set_power(struct drm_device *dev, |
| 45 | struct psb_intel_output *output, bool on) |
| 46 | { |
| 47 | u32 pp_status; |
| 48 | DRM_DRIVER_PRIVATE_T *dev_priv = dev->dev_private; |
| 49 | PSB_DEBUG_ENTRY("\n"); |
| 50 | |
Alan Cox | c3460fd | 2011-04-01 18:42:08 +0100 | [diff] [blame] | 51 | if (!gma_power_begin(dev, true)) |
Alan Cox | 54edcea | 2011-03-30 09:59:17 +0100 | [diff] [blame] | 52 | return; |
| 53 | |
| 54 | if (on) { |
| 55 | REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) | |
| 56 | POWER_TARGET_ON); |
| 57 | do { |
| 58 | pp_status = REG_READ(PP_STATUS); |
| 59 | } while ((pp_status & (PP_ON | PP_READY)) == PP_READY); |
| 60 | dev_priv->is_lvds_on = true; |
| 61 | } else { |
| 62 | REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) & |
| 63 | ~POWER_TARGET_ON); |
| 64 | do { |
| 65 | pp_status = REG_READ(PP_STATUS); |
| 66 | } while (pp_status & PP_ON); |
| 67 | dev_priv->is_lvds_on = false; |
| 68 | pm_request_idle(&dev->pdev->dev); |
| 69 | } |
| 70 | |
Alan Cox | c3460fd | 2011-04-01 18:42:08 +0100 | [diff] [blame] | 71 | gma_power_end(dev); |
Alan Cox | 54edcea | 2011-03-30 09:59:17 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static void mrst_lvds_dpms(struct drm_encoder *encoder, int mode) |
| 75 | { |
| 76 | struct drm_device *dev = encoder->dev; |
| 77 | struct psb_intel_output *output = enc_to_psb_intel_output(encoder); |
| 78 | |
| 79 | PSB_DEBUG_ENTRY("\n"); |
| 80 | |
| 81 | if (mode == DRM_MODE_DPMS_ON) |
| 82 | mrst_lvds_set_power(dev, output, true); |
| 83 | else |
| 84 | mrst_lvds_set_power(dev, output, false); |
| 85 | |
| 86 | /* XXX: We never power down the LVDS pairs. */ |
| 87 | } |
| 88 | |
| 89 | static void mrst_lvds_mode_set(struct drm_encoder *encoder, |
| 90 | struct drm_display_mode *mode, |
| 91 | struct drm_display_mode *adjusted_mode) |
| 92 | { |
| 93 | struct psb_intel_mode_device *mode_dev = |
| 94 | enc_to_psb_intel_output(encoder)->mode_dev; |
| 95 | struct drm_device *dev = encoder->dev; |
| 96 | u32 lvds_port; |
| 97 | uint64_t v = DRM_MODE_SCALE_FULLSCREEN; |
| 98 | |
| 99 | PSB_DEBUG_ENTRY("\n"); |
| 100 | |
Alan Cox | c3460fd | 2011-04-01 18:42:08 +0100 | [diff] [blame] | 101 | if (!gma_power_begin(dev, true)) |
Alan Cox | 54edcea | 2011-03-30 09:59:17 +0100 | [diff] [blame] | 102 | return; |
| 103 | |
| 104 | /* |
| 105 | * The LVDS pin pair will already have been turned on in the |
| 106 | * psb_intel_crtc_mode_set since it has a large impact on the DPLL |
| 107 | * settings. |
| 108 | */ |
| 109 | lvds_port = (REG_READ(LVDS) & |
| 110 | (~LVDS_PIPEB_SELECT)) | |
| 111 | LVDS_PORT_EN | |
| 112 | LVDS_BORDER_EN; |
| 113 | |
| 114 | if (mode_dev->panel_wants_dither) |
| 115 | lvds_port |= MRST_PANEL_8TO6_DITHER_ENABLE; |
| 116 | |
| 117 | REG_WRITE(LVDS, lvds_port); |
| 118 | |
| 119 | drm_connector_property_get_value( |
| 120 | &enc_to_psb_intel_output(encoder)->base, |
| 121 | dev->mode_config.scaling_mode_property, |
| 122 | &v); |
| 123 | |
| 124 | if (v == DRM_MODE_SCALE_NO_SCALE) |
| 125 | REG_WRITE(PFIT_CONTROL, 0); |
| 126 | else if (v == DRM_MODE_SCALE_ASPECT) { |
| 127 | if ((mode->vdisplay != adjusted_mode->crtc_vdisplay) || |
| 128 | (mode->hdisplay != adjusted_mode->crtc_hdisplay)) { |
| 129 | if ((adjusted_mode->crtc_hdisplay * mode->vdisplay) == |
| 130 | (mode->hdisplay * adjusted_mode->crtc_vdisplay)) |
| 131 | REG_WRITE(PFIT_CONTROL, PFIT_ENABLE); |
| 132 | else if ((adjusted_mode->crtc_hdisplay * |
| 133 | mode->vdisplay) > (mode->hdisplay * |
| 134 | adjusted_mode->crtc_vdisplay)) |
| 135 | REG_WRITE(PFIT_CONTROL, PFIT_ENABLE | |
| 136 | PFIT_SCALING_MODE_PILLARBOX); |
| 137 | else |
| 138 | REG_WRITE(PFIT_CONTROL, PFIT_ENABLE | |
| 139 | PFIT_SCALING_MODE_LETTERBOX); |
| 140 | } else |
| 141 | REG_WRITE(PFIT_CONTROL, PFIT_ENABLE); |
| 142 | } else /*(v == DRM_MODE_SCALE_FULLSCREEN)*/ |
| 143 | REG_WRITE(PFIT_CONTROL, PFIT_ENABLE); |
| 144 | |
Alan Cox | c3460fd | 2011-04-01 18:42:08 +0100 | [diff] [blame] | 145 | gma_power_end(dev); |
Alan Cox | 54edcea | 2011-03-30 09:59:17 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
| 149 | static const struct drm_encoder_helper_funcs mrst_lvds_helper_funcs = { |
| 150 | .dpms = mrst_lvds_dpms, |
| 151 | .mode_fixup = psb_intel_lvds_mode_fixup, |
| 152 | .prepare = psb_intel_lvds_prepare, |
| 153 | .mode_set = mrst_lvds_mode_set, |
| 154 | .commit = psb_intel_lvds_commit, |
| 155 | }; |
| 156 | |
| 157 | static struct drm_display_mode lvds_configuration_modes[] = { |
| 158 | /* hard coded fixed mode for TPO LTPS LPJ040K001A */ |
| 159 | { DRM_MODE("800x480", DRM_MODE_TYPE_DRIVER, 33264, 800, 836, |
| 160 | 846, 1056, 0, 480, 489, 491, 525, 0, 0) }, |
| 161 | /* hard coded fixed mode for LVDS 800x480 */ |
| 162 | { DRM_MODE("800x480", DRM_MODE_TYPE_DRIVER, 30994, 800, 801, |
| 163 | 802, 1024, 0, 480, 481, 482, 525, 0, 0) }, |
| 164 | /* hard coded fixed mode for Samsung 480wsvga LVDS 1024x600@75 */ |
| 165 | { DRM_MODE("1024x600", DRM_MODE_TYPE_DRIVER, 53990, 1024, 1072, |
| 166 | 1104, 1184, 0, 600, 603, 604, 608, 0, 0) }, |
| 167 | /* hard coded fixed mode for Samsung 480wsvga LVDS 1024x600@75 */ |
| 168 | { DRM_MODE("1024x600", DRM_MODE_TYPE_DRIVER, 53990, 1024, 1104, |
| 169 | 1136, 1184, 0, 600, 603, 604, 608, 0, 0) }, |
| 170 | /* hard coded fixed mode for Sharp wsvga LVDS 1024x600 */ |
| 171 | { DRM_MODE("1024x600", DRM_MODE_TYPE_DRIVER, 48885, 1024, 1124, |
| 172 | 1204, 1312, 0, 600, 607, 610, 621, 0, 0) }, |
| 173 | /* hard coded fixed mode for LVDS 1024x768 */ |
| 174 | { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048, |
| 175 | 1184, 1344, 0, 768, 771, 777, 806, 0, 0) }, |
| 176 | /* hard coded fixed mode for LVDS 1366x768 */ |
| 177 | { DRM_MODE("1366x768", DRM_MODE_TYPE_DRIVER, 77500, 1366, 1430, |
| 178 | 1558, 1664, 0, 768, 769, 770, 776, 0, 0) }, |
| 179 | }; |
| 180 | |
| 181 | /* Returns the panel fixed mode from configuration. */ |
| 182 | |
| 183 | static struct drm_display_mode * |
| 184 | mrst_lvds_get_configuration_mode(struct drm_device *dev) |
| 185 | { |
| 186 | struct drm_display_mode *mode = NULL; |
| 187 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 188 | struct mrst_timing_info *ti = &dev_priv->gct_data.DTD; |
| 189 | |
| 190 | if (dev_priv->vbt_data.size != 0x00) { /*if non-zero, then use vbt*/ |
| 191 | mode = kzalloc(sizeof(*mode), GFP_KERNEL); |
| 192 | if (!mode) |
| 193 | return NULL; |
| 194 | |
| 195 | mode->hdisplay = (ti->hactive_hi << 8) | ti->hactive_lo; |
| 196 | mode->vdisplay = (ti->vactive_hi << 8) | ti->vactive_lo; |
| 197 | mode->hsync_start = mode->hdisplay + \ |
| 198 | ((ti->hsync_offset_hi << 8) | \ |
| 199 | ti->hsync_offset_lo); |
| 200 | mode->hsync_end = mode->hsync_start + \ |
| 201 | ((ti->hsync_pulse_width_hi << 8) | \ |
| 202 | ti->hsync_pulse_width_lo); |
| 203 | mode->htotal = mode->hdisplay + ((ti->hblank_hi << 8) | \ |
| 204 | ti->hblank_lo); |
| 205 | mode->vsync_start = \ |
| 206 | mode->vdisplay + ((ti->vsync_offset_hi << 4) | \ |
| 207 | ti->vsync_offset_lo); |
| 208 | mode->vsync_end = \ |
| 209 | mode->vsync_start + ((ti->vsync_pulse_width_hi << 4) | \ |
| 210 | ti->vsync_pulse_width_lo); |
| 211 | mode->vtotal = mode->vdisplay + \ |
| 212 | ((ti->vblank_hi << 8) | ti->vblank_lo); |
| 213 | mode->clock = ti->pixel_clock * 10; |
| 214 | #if 0 |
| 215 | printk(KERN_INFO "hdisplay is %d\n", mode->hdisplay); |
| 216 | printk(KERN_INFO "vdisplay is %d\n", mode->vdisplay); |
| 217 | printk(KERN_INFO "HSS is %d\n", mode->hsync_start); |
| 218 | printk(KERN_INFO "HSE is %d\n", mode->hsync_end); |
| 219 | printk(KERN_INFO "htotal is %d\n", mode->htotal); |
| 220 | printk(KERN_INFO "VSS is %d\n", mode->vsync_start); |
| 221 | printk(KERN_INFO "VSE is %d\n", mode->vsync_end); |
| 222 | printk(KERN_INFO "vtotal is %d\n", mode->vtotal); |
| 223 | printk(KERN_INFO "clock is %d\n", mode->clock); |
| 224 | #endif |
| 225 | } else |
| 226 | mode = drm_mode_duplicate(dev, &lvds_configuration_modes[2]); |
| 227 | |
| 228 | drm_mode_set_name(mode); |
| 229 | drm_mode_set_crtcinfo(mode, 0); |
| 230 | |
| 231 | return mode; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * mrst_lvds_init - setup LVDS connectors on this device |
| 236 | * @dev: drm device |
| 237 | * |
| 238 | * Create the connector, register the LVDS DDC bus, and try to figure out what |
| 239 | * modes we can display on the LVDS panel (if present). |
| 240 | */ |
| 241 | void mrst_lvds_init(struct drm_device *dev, |
| 242 | struct psb_intel_mode_device *mode_dev) |
| 243 | { |
| 244 | struct psb_intel_output *psb_intel_output; |
| 245 | struct drm_connector *connector; |
| 246 | struct drm_encoder *encoder; |
| 247 | struct drm_psb_private *dev_priv = |
| 248 | (struct drm_psb_private *) dev->dev_private; |
| 249 | struct edid *edid; |
| 250 | int ret = 0; |
| 251 | struct i2c_adapter *i2c_adap; |
| 252 | struct drm_display_mode *scan; /* *modes, *bios_mode; */ |
| 253 | |
| 254 | PSB_DEBUG_ENTRY("\n"); |
| 255 | |
| 256 | psb_intel_output = kzalloc(sizeof(struct psb_intel_output), GFP_KERNEL); |
| 257 | if (!psb_intel_output) |
| 258 | return; |
| 259 | |
| 260 | psb_intel_output->mode_dev = mode_dev; |
| 261 | connector = &psb_intel_output->base; |
| 262 | encoder = &psb_intel_output->enc; |
| 263 | dev_priv->is_lvds_on = true; |
| 264 | drm_connector_init(dev, &psb_intel_output->base, |
| 265 | &psb_intel_lvds_connector_funcs, |
| 266 | DRM_MODE_CONNECTOR_LVDS); |
| 267 | |
| 268 | drm_encoder_init(dev, &psb_intel_output->enc, &psb_intel_lvds_enc_funcs, |
| 269 | DRM_MODE_ENCODER_LVDS); |
| 270 | |
| 271 | drm_mode_connector_attach_encoder(&psb_intel_output->base, |
| 272 | &psb_intel_output->enc); |
| 273 | psb_intel_output->type = INTEL_OUTPUT_LVDS; |
| 274 | |
| 275 | drm_encoder_helper_add(encoder, &mrst_lvds_helper_funcs); |
| 276 | drm_connector_helper_add(connector, |
| 277 | &psb_intel_lvds_connector_helper_funcs); |
| 278 | connector->display_info.subpixel_order = SubPixelHorizontalRGB; |
| 279 | connector->interlace_allowed = false; |
| 280 | connector->doublescan_allowed = false; |
| 281 | |
| 282 | drm_connector_attach_property(connector, |
| 283 | dev->mode_config.scaling_mode_property, |
| 284 | DRM_MODE_SCALE_FULLSCREEN); |
| 285 | drm_connector_attach_property(connector, |
| 286 | dev_priv->backlight_property, |
| 287 | BRIGHTNESS_MAX_LEVEL); |
| 288 | |
| 289 | mode_dev->panel_wants_dither = false; |
| 290 | if (dev_priv->vbt_data.size != 0x00) |
| 291 | mode_dev->panel_wants_dither = (dev_priv->gct_data. |
| 292 | Panel_Port_Control & MRST_PANEL_8TO6_DITHER_ENABLE); |
| 293 | |
| 294 | /* |
| 295 | * LVDS discovery: |
| 296 | * 1) check for EDID on DDC |
| 297 | * 2) check for VBT data |
| 298 | * 3) check to see if LVDS is already on |
| 299 | * if none of the above, no panel |
| 300 | * 4) make sure lid is open |
| 301 | * if closed, act like it's not there for now |
| 302 | */ |
| 303 | i2c_adap = i2c_get_adapter(2); |
| 304 | if (i2c_adap == NULL) |
| 305 | printk(KERN_ALERT "No ddc adapter available!\n"); |
| 306 | /* |
| 307 | * Attempt to get the fixed panel mode from DDC. Assume that the |
| 308 | * preferred mode is the right one. |
| 309 | */ |
| 310 | if (i2c_adap) { |
| 311 | edid = drm_get_edid(connector, i2c_adap); |
| 312 | if (edid) { |
| 313 | drm_mode_connector_update_edid_property(connector, |
| 314 | edid); |
| 315 | ret = drm_add_edid_modes(connector, edid); |
| 316 | kfree(edid); |
| 317 | } |
| 318 | |
| 319 | list_for_each_entry(scan, &connector->probed_modes, head) { |
| 320 | if (scan->type & DRM_MODE_TYPE_PREFERRED) { |
| 321 | mode_dev->panel_fixed_mode = |
| 322 | drm_mode_duplicate(dev, scan); |
| 323 | goto out; /* FIXME: check for quirks */ |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * If we didn't get EDID, try geting panel timing |
| 330 | * from configuration data |
| 331 | */ |
| 332 | mode_dev->panel_fixed_mode = mrst_lvds_get_configuration_mode(dev); |
| 333 | |
| 334 | if (mode_dev->panel_fixed_mode) { |
| 335 | mode_dev->panel_fixed_mode->type |= |
| 336 | DRM_MODE_TYPE_PREFERRED; |
| 337 | goto out; /* FIXME: check for quirks */ |
| 338 | } |
| 339 | |
| 340 | /* If we still don't have a mode after all that, give up. */ |
| 341 | if (!mode_dev->panel_fixed_mode) { |
| 342 | DRM_DEBUG |
| 343 | ("Found no modes on the lvds, ignoring the LVDS\n"); |
| 344 | goto failed_find; |
| 345 | } |
| 346 | |
| 347 | out: |
| 348 | drm_sysfs_connector_add(connector); |
| 349 | return; |
| 350 | |
| 351 | failed_find: |
| 352 | DRM_DEBUG("No LVDS modes found, disabling.\n"); |
| 353 | if (psb_intel_output->ddc_bus) |
| 354 | psb_intel_i2c_destroy(psb_intel_output->ddc_bus); |
| 355 | |
| 356 | /* failed_ddc: */ |
| 357 | |
| 358 | drm_encoder_cleanup(encoder); |
| 359 | drm_connector_cleanup(connector); |
| 360 | kfree(connector); |
| 361 | } |
| 362 | |