Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006-2008 Intel Corporation |
| 3 | * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> |
| 4 | * |
| 5 | * DRM core CRTC related functions |
| 6 | * |
| 7 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 8 | * documentation for any purpose is hereby granted without fee, provided that |
| 9 | * the above copyright notice appear in all copies and that both that copyright |
| 10 | * notice and this permission notice appear in supporting documentation, and |
| 11 | * that the name of the copyright holders not be used in advertising or |
| 12 | * publicity pertaining to distribution of the software without specific, |
| 13 | * written prior permission. The copyright holders make no representations |
| 14 | * about the suitability of this software for any purpose. It is provided "as |
| 15 | * is" without express or implied warranty. |
| 16 | * |
| 17 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 18 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 19 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 20 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 21 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 22 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 23 | * OF THIS SOFTWARE. |
| 24 | * |
| 25 | * Authors: |
| 26 | * Keith Packard |
| 27 | * Eric Anholt <eric@anholt.net> |
| 28 | * Dave Airlie <airlied@linux.ie> |
| 29 | * Jesse Barnes <jesse.barnes@intel.com> |
| 30 | */ |
| 31 | |
| 32 | #include "drmP.h" |
| 33 | #include "drm_crtc.h" |
| 34 | #include "drm_crtc_helper.h" |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 35 | #include "drm_fb_helper.h" |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 36 | |
yakui_zhao | 6714977 | 2009-04-02 11:52:12 +0800 | [diff] [blame] | 37 | static void drm_mode_validate_flag(struct drm_connector *connector, |
| 38 | int flags) |
| 39 | { |
| 40 | struct drm_display_mode *mode, *t; |
| 41 | |
| 42 | if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE)) |
| 43 | return; |
| 44 | |
| 45 | list_for_each_entry_safe(mode, t, &connector->modes, head) { |
| 46 | if ((mode->flags & DRM_MODE_FLAG_INTERLACE) && |
| 47 | !(flags & DRM_MODE_FLAG_INTERLACE)) |
| 48 | mode->status = MODE_NO_INTERLACE; |
| 49 | if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) && |
| 50 | !(flags & DRM_MODE_FLAG_DBLSCAN)) |
| 51 | mode->status = MODE_NO_DBLESCAN; |
| 52 | } |
| 53 | |
| 54 | return; |
| 55 | } |
| 56 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 57 | /** |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 58 | * drm_helper_probe_single_connector_modes - get complete set of display modes |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 59 | * @dev: DRM device |
| 60 | * @maxX: max width for modes |
| 61 | * @maxY: max height for modes |
| 62 | * |
| 63 | * LOCKING: |
| 64 | * Caller must hold mode config lock. |
| 65 | * |
| 66 | * Based on @dev's mode_config layout, scan all the connectors and try to detect |
| 67 | * modes on them. Modes will first be added to the connector's probed_modes |
| 68 | * list, then culled (based on validity and the @maxX, @maxY parameters) and |
| 69 | * put into the normal modes list. |
| 70 | * |
| 71 | * Intended to be used either at bootup time or when major configuration |
| 72 | * changes have occurred. |
| 73 | * |
| 74 | * FIXME: take into account monitor limits |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 75 | * |
| 76 | * RETURNS: |
| 77 | * Number of modes found on @connector. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 78 | */ |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 79 | int drm_helper_probe_single_connector_modes(struct drm_connector *connector, |
| 80 | uint32_t maxX, uint32_t maxY) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 81 | { |
| 82 | struct drm_device *dev = connector->dev; |
| 83 | struct drm_display_mode *mode, *t; |
| 84 | struct drm_connector_helper_funcs *connector_funcs = |
| 85 | connector->helper_private; |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 86 | int count = 0; |
yakui_zhao | 6714977 | 2009-04-02 11:52:12 +0800 | [diff] [blame] | 87 | int mode_flags = 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 88 | |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 89 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id, |
| 90 | drm_get_connector_name(connector)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 91 | /* set all modes to the unverified state */ |
| 92 | list_for_each_entry_safe(mode, t, &connector->modes, head) |
| 93 | mode->status = MODE_UNVERIFIED; |
| 94 | |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 95 | if (connector->force) { |
| 96 | if (connector->force == DRM_FORCE_ON) |
| 97 | connector->status = connector_status_connected; |
| 98 | else |
| 99 | connector->status = connector_status_disconnected; |
| 100 | if (connector->funcs->force) |
| 101 | connector->funcs->force(connector); |
| 102 | } else |
| 103 | connector->status = connector->funcs->detect(connector); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 104 | |
| 105 | if (connector->status == connector_status_disconnected) { |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 106 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n", |
| 107 | connector->base.id, drm_get_connector_name(connector)); |
Zhao Yakui | 7253983 | 2010-03-04 08:25:55 +0000 | [diff] [blame] | 108 | drm_mode_connector_update_edid_property(connector, NULL); |
Adam Jackson | 620f378 | 2009-09-08 11:51:46 +1000 | [diff] [blame] | 109 | goto prune; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 112 | count = (*connector_funcs->get_modes)(connector); |
ykzhao | 50fe4cf | 2009-09-03 14:30:04 +0800 | [diff] [blame] | 113 | if (!count) { |
Adam Jackson | 9632b41 | 2009-11-23 14:23:07 -0500 | [diff] [blame] | 114 | count = drm_add_modes_noedid(connector, 1024, 768); |
ykzhao | 50fe4cf | 2009-09-03 14:30:04 +0800 | [diff] [blame] | 115 | if (!count) |
| 116 | return 0; |
| 117 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 118 | |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 119 | drm_mode_connector_list_update(connector); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 120 | |
| 121 | if (maxX && maxY) |
| 122 | drm_mode_validate_size(dev, &connector->modes, maxX, |
| 123 | maxY, 0); |
yakui_zhao | 6714977 | 2009-04-02 11:52:12 +0800 | [diff] [blame] | 124 | |
| 125 | if (connector->interlace_allowed) |
| 126 | mode_flags |= DRM_MODE_FLAG_INTERLACE; |
| 127 | if (connector->doublescan_allowed) |
| 128 | mode_flags |= DRM_MODE_FLAG_DBLSCAN; |
| 129 | drm_mode_validate_flag(connector, mode_flags); |
| 130 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 131 | list_for_each_entry_safe(mode, t, &connector->modes, head) { |
| 132 | if (mode->status == MODE_OK) |
| 133 | mode->status = connector_funcs->mode_valid(connector, |
| 134 | mode); |
| 135 | } |
| 136 | |
Adam Jackson | 620f378 | 2009-09-08 11:51:46 +1000 | [diff] [blame] | 137 | prune: |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 138 | drm_mode_prune_invalid(dev, &connector->modes, true); |
| 139 | |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 140 | if (list_empty(&connector->modes)) |
| 141 | return 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 142 | |
| 143 | drm_mode_sort(&connector->modes); |
| 144 | |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 145 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id, |
| 146 | drm_get_connector_name(connector)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 147 | list_for_each_entry_safe(mode, t, &connector->modes, head) { |
| 148 | mode->vrefresh = drm_mode_vrefresh(mode); |
| 149 | |
| 150 | drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V); |
| 151 | drm_mode_debug_printmodeline(mode); |
| 152 | } |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 153 | |
| 154 | return count; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 155 | } |
| 156 | EXPORT_SYMBOL(drm_helper_probe_single_connector_modes); |
| 157 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 158 | /** |
Keith Packard | c9fb15f | 2009-05-30 20:42:28 -0700 | [diff] [blame] | 159 | * drm_helper_encoder_in_use - check if a given encoder is in use |
| 160 | * @encoder: encoder to check |
| 161 | * |
| 162 | * LOCKING: |
| 163 | * Caller must hold mode config lock. |
| 164 | * |
| 165 | * Walk @encoders's DRM device's mode_config and see if it's in use. |
| 166 | * |
| 167 | * RETURNS: |
| 168 | * True if @encoder is part of the mode_config, false otherwise. |
| 169 | */ |
| 170 | bool drm_helper_encoder_in_use(struct drm_encoder *encoder) |
| 171 | { |
| 172 | struct drm_connector *connector; |
| 173 | struct drm_device *dev = encoder->dev; |
| 174 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) |
| 175 | if (connector->encoder == encoder) |
| 176 | return true; |
| 177 | return false; |
| 178 | } |
| 179 | EXPORT_SYMBOL(drm_helper_encoder_in_use); |
| 180 | |
| 181 | /** |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 182 | * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config |
| 183 | * @crtc: CRTC to check |
| 184 | * |
| 185 | * LOCKING: |
| 186 | * Caller must hold mode config lock. |
| 187 | * |
| 188 | * Walk @crtc's DRM device's mode_config and see if it's in use. |
| 189 | * |
| 190 | * RETURNS: |
| 191 | * True if @crtc is part of the mode_config, false otherwise. |
| 192 | */ |
| 193 | bool drm_helper_crtc_in_use(struct drm_crtc *crtc) |
| 194 | { |
| 195 | struct drm_encoder *encoder; |
| 196 | struct drm_device *dev = crtc->dev; |
| 197 | /* FIXME: Locking around list access? */ |
| 198 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) |
Keith Packard | c9fb15f | 2009-05-30 20:42:28 -0700 | [diff] [blame] | 199 | if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder)) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 200 | return true; |
| 201 | return false; |
| 202 | } |
| 203 | EXPORT_SYMBOL(drm_helper_crtc_in_use); |
| 204 | |
Ben Skeggs | 86a1b9d | 2010-07-01 16:49:57 +1000 | [diff] [blame] | 205 | static void |
| 206 | drm_encoder_disable(struct drm_encoder *encoder) |
| 207 | { |
| 208 | struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private; |
| 209 | |
| 210 | if (encoder_funcs->disable) |
| 211 | (*encoder_funcs->disable)(encoder); |
| 212 | else |
| 213 | (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF); |
| 214 | } |
| 215 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 216 | /** |
David John | 89347bb | 2009-12-31 12:00:46 +0530 | [diff] [blame] | 217 | * drm_helper_disable_unused_functions - disable unused objects |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 218 | * @dev: DRM device |
| 219 | * |
| 220 | * LOCKING: |
| 221 | * Caller must hold mode config lock. |
| 222 | * |
| 223 | * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled |
| 224 | * by calling its dpms function, which should power it off. |
| 225 | */ |
| 226 | void drm_helper_disable_unused_functions(struct drm_device *dev) |
| 227 | { |
| 228 | struct drm_encoder *encoder; |
Dave Airlie | a3a0544 | 2009-08-31 15:16:30 +1000 | [diff] [blame] | 229 | struct drm_connector *connector; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 230 | struct drm_crtc *crtc; |
| 231 | |
Dave Airlie | a3a0544 | 2009-08-31 15:16:30 +1000 | [diff] [blame] | 232 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 233 | if (!connector->encoder) |
| 234 | continue; |
| 235 | if (connector->status == connector_status_disconnected) |
| 236 | connector->encoder = NULL; |
| 237 | } |
| 238 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 239 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
Dave Airlie | a3a0544 | 2009-08-31 15:16:30 +1000 | [diff] [blame] | 240 | if (!drm_helper_encoder_in_use(encoder)) { |
Ben Skeggs | 86a1b9d | 2010-07-01 16:49:57 +1000 | [diff] [blame] | 241 | drm_encoder_disable(encoder); |
Dave Airlie | 9c552dd | 2009-09-02 14:00:11 +1000 | [diff] [blame] | 242 | /* disconnector encoder from any connector */ |
| 243 | encoder->crtc = NULL; |
Dave Airlie | a3a0544 | 2009-08-31 15:16:30 +1000 | [diff] [blame] | 244 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
| 248 | struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; |
| 249 | crtc->enabled = drm_helper_crtc_in_use(crtc); |
| 250 | if (!crtc->enabled) { |
Alex Deucher | 5c8d717 | 2010-06-11 17:04:35 -0400 | [diff] [blame] | 251 | if (crtc_funcs->disable) |
| 252 | (*crtc_funcs->disable)(crtc); |
| 253 | else |
| 254 | (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 255 | crtc->fb = NULL; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | EXPORT_SYMBOL(drm_helper_disable_unused_functions); |
| 260 | |
Jesse Barnes | 7bec756 | 2009-02-23 16:09:34 -0800 | [diff] [blame] | 261 | /** |
| 262 | * drm_encoder_crtc_ok - can a given crtc drive a given encoder? |
| 263 | * @encoder: encoder to test |
| 264 | * @crtc: crtc to test |
| 265 | * |
| 266 | * Return false if @encoder can't be driven by @crtc, true otherwise. |
| 267 | */ |
| 268 | static bool drm_encoder_crtc_ok(struct drm_encoder *encoder, |
| 269 | struct drm_crtc *crtc) |
| 270 | { |
| 271 | struct drm_device *dev; |
| 272 | struct drm_crtc *tmp; |
| 273 | int crtc_mask = 1; |
| 274 | |
| 275 | WARN(!crtc, "checking null crtc?"); |
| 276 | |
| 277 | dev = crtc->dev; |
| 278 | |
| 279 | list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) { |
| 280 | if (tmp == crtc) |
| 281 | break; |
| 282 | crtc_mask <<= 1; |
| 283 | } |
| 284 | |
| 285 | if (encoder->possible_crtcs & crtc_mask) |
| 286 | return true; |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Check the CRTC we're going to map each output to vs. its current |
| 292 | * CRTC. If they don't match, we have to disable the output and the CRTC |
| 293 | * since the driver will have to re-route things. |
| 294 | */ |
| 295 | static void |
| 296 | drm_crtc_prepare_encoders(struct drm_device *dev) |
| 297 | { |
| 298 | struct drm_encoder_helper_funcs *encoder_funcs; |
| 299 | struct drm_encoder *encoder; |
| 300 | |
| 301 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 302 | encoder_funcs = encoder->helper_private; |
| 303 | /* Disable unused encoders */ |
| 304 | if (encoder->crtc == NULL) |
Ben Skeggs | 86a1b9d | 2010-07-01 16:49:57 +1000 | [diff] [blame] | 305 | drm_encoder_disable(encoder); |
Jesse Barnes | 7bec756 | 2009-02-23 16:09:34 -0800 | [diff] [blame] | 306 | /* Disable encoders whose CRTC is about to change */ |
| 307 | if (encoder_funcs->get_crtc && |
| 308 | encoder->crtc != (*encoder_funcs->get_crtc)(encoder)) |
Ben Skeggs | 86a1b9d | 2010-07-01 16:49:57 +1000 | [diff] [blame] | 309 | drm_encoder_disable(encoder); |
Jesse Barnes | 7bec756 | 2009-02-23 16:09:34 -0800 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 313 | /** |
| 314 | * drm_crtc_set_mode - set a mode |
| 315 | * @crtc: CRTC to program |
| 316 | * @mode: mode to use |
| 317 | * @x: width of mode |
| 318 | * @y: height of mode |
| 319 | * |
| 320 | * LOCKING: |
| 321 | * Caller must hold mode config lock. |
| 322 | * |
| 323 | * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance |
| 324 | * to fixup or reject the mode prior to trying to set it. |
| 325 | * |
| 326 | * RETURNS: |
| 327 | * True if the mode was set successfully, or false otherwise. |
| 328 | */ |
| 329 | bool drm_crtc_helper_set_mode(struct drm_crtc *crtc, |
| 330 | struct drm_display_mode *mode, |
Kristian Høgsberg | 3c4fdcf | 2008-12-17 22:14:46 -0500 | [diff] [blame] | 331 | int x, int y, |
| 332 | struct drm_framebuffer *old_fb) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 333 | { |
| 334 | struct drm_device *dev = crtc->dev; |
| 335 | struct drm_display_mode *adjusted_mode, saved_mode; |
| 336 | struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; |
| 337 | struct drm_encoder_helper_funcs *encoder_funcs; |
| 338 | int saved_x, saved_y; |
| 339 | struct drm_encoder *encoder; |
| 340 | bool ret = true; |
| 341 | |
| 342 | adjusted_mode = drm_mode_duplicate(dev, mode); |
| 343 | |
| 344 | crtc->enabled = drm_helper_crtc_in_use(crtc); |
| 345 | |
| 346 | if (!crtc->enabled) |
| 347 | return true; |
| 348 | |
| 349 | saved_mode = crtc->mode; |
| 350 | saved_x = crtc->x; |
| 351 | saved_y = crtc->y; |
| 352 | |
| 353 | /* Update crtc values up front so the driver can rely on them for mode |
| 354 | * setting. |
| 355 | */ |
| 356 | crtc->mode = *mode; |
| 357 | crtc->x = x; |
| 358 | crtc->y = y; |
| 359 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 360 | /* Pass our mode to the connectors and the CRTC to give them a chance to |
| 361 | * adjust it according to limitations or connector properties, and also |
| 362 | * a chance to reject the mode entirely. |
| 363 | */ |
| 364 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 365 | |
| 366 | if (encoder->crtc != crtc) |
| 367 | continue; |
| 368 | encoder_funcs = encoder->helper_private; |
| 369 | if (!(ret = encoder_funcs->mode_fixup(encoder, mode, |
| 370 | adjusted_mode))) { |
| 371 | goto done; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) { |
| 376 | goto done; |
| 377 | } |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 378 | DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 379 | |
| 380 | /* Prepare the encoders and CRTCs before setting the mode. */ |
| 381 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 382 | |
| 383 | if (encoder->crtc != crtc) |
| 384 | continue; |
| 385 | encoder_funcs = encoder->helper_private; |
| 386 | /* Disable the encoders as the first thing we do. */ |
| 387 | encoder_funcs->prepare(encoder); |
| 388 | } |
| 389 | |
Jesse Barnes | 7bec756 | 2009-02-23 16:09:34 -0800 | [diff] [blame] | 390 | drm_crtc_prepare_encoders(dev); |
| 391 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 392 | crtc_funcs->prepare(crtc); |
| 393 | |
| 394 | /* Set up the DPLL and any encoders state that needs to adjust or depend |
| 395 | * on the DPLL. |
| 396 | */ |
Chris Wilson | 5c3b82e | 2009-02-11 13:25:09 +0000 | [diff] [blame] | 397 | ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb); |
| 398 | if (!ret) |
| 399 | goto done; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 400 | |
| 401 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 402 | |
| 403 | if (encoder->crtc != crtc) |
| 404 | continue; |
| 405 | |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 406 | DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n", |
| 407 | encoder->base.id, drm_get_encoder_name(encoder), |
| 408 | mode->base.id, mode->name); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 409 | encoder_funcs = encoder->helper_private; |
| 410 | encoder_funcs->mode_set(encoder, mode, adjusted_mode); |
| 411 | } |
| 412 | |
| 413 | /* Now enable the clocks, plane, pipe, and connectors that we set up. */ |
| 414 | crtc_funcs->commit(crtc); |
| 415 | |
| 416 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 417 | |
| 418 | if (encoder->crtc != crtc) |
| 419 | continue; |
| 420 | |
| 421 | encoder_funcs = encoder->helper_private; |
| 422 | encoder_funcs->commit(encoder); |
| 423 | |
| 424 | } |
| 425 | |
| 426 | /* XXX free adjustedmode */ |
| 427 | drm_mode_destroy(dev, adjusted_mode); |
| 428 | /* FIXME: add subpixel order */ |
| 429 | done: |
| 430 | if (!ret) { |
| 431 | crtc->mode = saved_mode; |
| 432 | crtc->x = saved_x; |
| 433 | crtc->y = saved_y; |
| 434 | } |
| 435 | |
| 436 | return ret; |
| 437 | } |
| 438 | EXPORT_SYMBOL(drm_crtc_helper_set_mode); |
| 439 | |
| 440 | |
| 441 | /** |
| 442 | * drm_crtc_helper_set_config - set a new config from userspace |
| 443 | * @crtc: CRTC to setup |
| 444 | * @crtc_info: user provided configuration |
| 445 | * @new_mode: new mode to set |
| 446 | * @connector_set: set of connectors for the new config |
| 447 | * @fb: new framebuffer |
| 448 | * |
| 449 | * LOCKING: |
| 450 | * Caller must hold mode config lock. |
| 451 | * |
| 452 | * Setup a new configuration, provided by the user in @crtc_info, and enable |
| 453 | * it. |
| 454 | * |
| 455 | * RETURNS: |
| 456 | * Zero. (FIXME) |
| 457 | */ |
| 458 | int drm_crtc_helper_set_config(struct drm_mode_set *set) |
| 459 | { |
| 460 | struct drm_device *dev; |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 461 | struct drm_crtc *save_crtcs, *new_crtc, *crtc; |
| 462 | struct drm_encoder *save_encoders, *new_encoder, *encoder; |
Jesse Barnes | 7bec756 | 2009-02-23 16:09:34 -0800 | [diff] [blame] | 463 | struct drm_framebuffer *old_fb = NULL; |
Jakob Bornecrantz | 4cb72b1 | 2009-08-03 13:43:59 +0100 | [diff] [blame] | 464 | bool mode_changed = false; /* if true do a full mode set */ |
| 465 | bool fb_changed = false; /* if true and !mode_changed just do a flip */ |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 466 | struct drm_connector *save_connectors, *connector; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 467 | int count = 0, ro, fail = 0; |
| 468 | struct drm_crtc_helper_funcs *crtc_funcs; |
| 469 | int ret = 0; |
| 470 | |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 471 | DRM_DEBUG_KMS("\n"); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 472 | |
| 473 | if (!set) |
| 474 | return -EINVAL; |
| 475 | |
| 476 | if (!set->crtc) |
| 477 | return -EINVAL; |
| 478 | |
| 479 | if (!set->crtc->helper_private) |
| 480 | return -EINVAL; |
| 481 | |
| 482 | crtc_funcs = set->crtc->helper_private; |
| 483 | |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 484 | if (set->fb) { |
| 485 | DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n", |
| 486 | set->crtc->base.id, set->fb->base.id, |
| 487 | (int)set->num_connectors, set->x, set->y); |
| 488 | } else { |
| 489 | DRM_DEBUG_KMS("[CRTC:%d] [NOFB] #connectors=%d (x y) (%i %i)\n", |
| 490 | set->crtc->base.id, (int)set->num_connectors, |
| 491 | set->x, set->y); |
| 492 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 493 | |
| 494 | dev = set->crtc->dev; |
| 495 | |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 496 | /* Allocate space for the backup of all (non-pointer) crtc, encoder and |
| 497 | * connector data. */ |
| 498 | save_crtcs = kzalloc(dev->mode_config.num_crtc * |
| 499 | sizeof(struct drm_crtc), GFP_KERNEL); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 500 | if (!save_crtcs) |
| 501 | return -ENOMEM; |
| 502 | |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 503 | save_encoders = kzalloc(dev->mode_config.num_encoder * |
| 504 | sizeof(struct drm_encoder), GFP_KERNEL); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 505 | if (!save_encoders) { |
| 506 | kfree(save_crtcs); |
| 507 | return -ENOMEM; |
| 508 | } |
| 509 | |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 510 | save_connectors = kzalloc(dev->mode_config.num_connector * |
| 511 | sizeof(struct drm_connector), GFP_KERNEL); |
| 512 | if (!save_connectors) { |
| 513 | kfree(save_crtcs); |
| 514 | kfree(save_encoders); |
| 515 | return -ENOMEM; |
| 516 | } |
| 517 | |
| 518 | /* Copy data. Note that driver private data is not affected. |
| 519 | * Should anything bad happen only the expected state is |
| 520 | * restored, not the drivers personal bookkeeping. |
| 521 | */ |
| 522 | count = 0; |
| 523 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
| 524 | save_crtcs[count++] = *crtc; |
| 525 | } |
| 526 | |
| 527 | count = 0; |
| 528 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 529 | save_encoders[count++] = *encoder; |
| 530 | } |
| 531 | |
| 532 | count = 0; |
| 533 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 534 | save_connectors[count++] = *connector; |
| 535 | } |
| 536 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 537 | /* We should be able to check here if the fb has the same properties |
| 538 | * and then just flip_or_move it */ |
| 539 | if (set->crtc->fb != set->fb) { |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 540 | /* If we have no fb then treat it as a full mode set */ |
Jesse Barnes | 7bec756 | 2009-02-23 16:09:34 -0800 | [diff] [blame] | 541 | if (set->crtc->fb == NULL) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 542 | DRM_DEBUG_KMS("crtc has no fb, full mode set\n"); |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 543 | mode_changed = true; |
Jakob Bornecrantz | 4cb72b1 | 2009-08-03 13:43:59 +0100 | [diff] [blame] | 544 | } else if (set->fb == NULL) { |
| 545 | mode_changed = true; |
Dave Airlie | 8dff474 | 2010-02-11 14:28:58 +1000 | [diff] [blame] | 546 | } else |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 547 | fb_changed = true; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | if (set->x != set->crtc->x || set->y != set->crtc->y) |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 551 | fb_changed = true; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 552 | |
| 553 | if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 554 | DRM_DEBUG_KMS("modes are different, full mode set\n"); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 555 | drm_mode_debug_printmodeline(&set->crtc->mode); |
| 556 | drm_mode_debug_printmodeline(set->mode); |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 557 | mode_changed = true; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /* a) traverse passed in connector list and get encoders for them */ |
| 561 | count = 0; |
| 562 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 563 | struct drm_connector_helper_funcs *connector_funcs = |
| 564 | connector->helper_private; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 565 | new_encoder = connector->encoder; |
| 566 | for (ro = 0; ro < set->num_connectors; ro++) { |
| 567 | if (set->connectors[ro] == connector) { |
| 568 | new_encoder = connector_funcs->best_encoder(connector); |
| 569 | /* if we can't get an encoder for a connector |
| 570 | we are setting now - then fail */ |
| 571 | if (new_encoder == NULL) |
| 572 | /* don't break so fail path works correct */ |
| 573 | fail = 1; |
| 574 | break; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | if (new_encoder != connector->encoder) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 579 | DRM_DEBUG_KMS("encoder changed, full mode switch\n"); |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 580 | mode_changed = true; |
Maarten Maathuis | ff846ab | 2009-08-19 00:56:45 +0200 | [diff] [blame] | 581 | /* If the encoder is reused for another connector, then |
| 582 | * the appropriate crtc will be set later. |
| 583 | */ |
Maarten Maathuis | ff6fdbe | 2009-09-01 03:39:04 +0200 | [diff] [blame] | 584 | if (connector->encoder) |
| 585 | connector->encoder->crtc = NULL; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 586 | connector->encoder = new_encoder; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | if (fail) { |
| 591 | ret = -EINVAL; |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 592 | goto fail; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | count = 0; |
| 596 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 597 | if (!connector->encoder) |
| 598 | continue; |
| 599 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 600 | if (connector->encoder->crtc == set->crtc) |
| 601 | new_crtc = NULL; |
| 602 | else |
| 603 | new_crtc = connector->encoder->crtc; |
| 604 | |
| 605 | for (ro = 0; ro < set->num_connectors; ro++) { |
| 606 | if (set->connectors[ro] == connector) |
| 607 | new_crtc = set->crtc; |
| 608 | } |
Jesse Barnes | 7bec756 | 2009-02-23 16:09:34 -0800 | [diff] [blame] | 609 | |
| 610 | /* Make sure the new CRTC will work with the encoder */ |
| 611 | if (new_crtc && |
| 612 | !drm_encoder_crtc_ok(connector->encoder, new_crtc)) { |
| 613 | ret = -EINVAL; |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 614 | goto fail; |
Jesse Barnes | 7bec756 | 2009-02-23 16:09:34 -0800 | [diff] [blame] | 615 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 616 | if (new_crtc != connector->encoder->crtc) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 617 | DRM_DEBUG_KMS("crtc changed, full mode switch\n"); |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 618 | mode_changed = true; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 619 | connector->encoder->crtc = new_crtc; |
| 620 | } |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 621 | if (new_crtc) { |
| 622 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n", |
| 623 | connector->base.id, drm_get_connector_name(connector), |
| 624 | new_crtc->base.id); |
| 625 | } else { |
| 626 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n", |
| 627 | connector->base.id, drm_get_connector_name(connector)); |
| 628 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /* mode_set_base is not a required function */ |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 632 | if (fb_changed && !crtc_funcs->mode_set_base) |
| 633 | mode_changed = true; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 634 | |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 635 | if (mode_changed) { |
Kristian Høgsberg | 3c4fdcf | 2008-12-17 22:14:46 -0500 | [diff] [blame] | 636 | old_fb = set->crtc->fb; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 637 | set->crtc->fb = set->fb; |
| 638 | set->crtc->enabled = (set->mode != NULL); |
| 639 | if (set->mode != NULL) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 640 | DRM_DEBUG_KMS("attempting to set mode from" |
| 641 | " userspace\n"); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 642 | drm_mode_debug_printmodeline(set->mode); |
| 643 | if (!drm_crtc_helper_set_mode(set->crtc, set->mode, |
Kristian Høgsberg | 3c4fdcf | 2008-12-17 22:14:46 -0500 | [diff] [blame] | 644 | set->x, set->y, |
| 645 | old_fb)) { |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 646 | DRM_ERROR("failed to set mode on [CRTC:%d]\n", |
| 647 | set->crtc->base.id); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 648 | ret = -EINVAL; |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 649 | goto fail; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 650 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 651 | } |
| 652 | drm_helper_disable_unused_functions(dev); |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 653 | } else if (fb_changed) { |
Ben Skeggs | 9b1596a | 2009-09-18 10:43:52 +1000 | [diff] [blame] | 654 | set->crtc->x = set->x; |
| 655 | set->crtc->y = set->y; |
| 656 | |
Kristian Høgsberg | 3c4fdcf | 2008-12-17 22:14:46 -0500 | [diff] [blame] | 657 | old_fb = set->crtc->fb; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 658 | if (set->crtc->fb != set->fb) |
| 659 | set->crtc->fb = set->fb; |
Chris Wilson | 5c3b82e | 2009-02-11 13:25:09 +0000 | [diff] [blame] | 660 | ret = crtc_funcs->mode_set_base(set->crtc, |
| 661 | set->x, set->y, old_fb); |
| 662 | if (ret != 0) |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 663 | goto fail; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 664 | } |
| 665 | |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 666 | kfree(save_connectors); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 667 | kfree(save_encoders); |
| 668 | kfree(save_crtcs); |
| 669 | return 0; |
| 670 | |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 671 | fail: |
| 672 | /* Restore all previous data. */ |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 673 | count = 0; |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 674 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
| 675 | *crtc = save_crtcs[count++]; |
| 676 | } |
Chris Wilson | e62fb64 | 2009-02-11 16:39:21 +0000 | [diff] [blame] | 677 | |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 678 | count = 0; |
| 679 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 680 | *encoder = save_encoders[count++]; |
Chris Wilson | e62fb64 | 2009-02-11 16:39:21 +0000 | [diff] [blame] | 681 | } |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 682 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 683 | count = 0; |
| 684 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 685 | *connector = save_connectors[count++]; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 686 | } |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 687 | |
| 688 | kfree(save_connectors); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 689 | kfree(save_encoders); |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame] | 690 | kfree(save_crtcs); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 691 | return ret; |
| 692 | } |
| 693 | EXPORT_SYMBOL(drm_crtc_helper_set_config); |
| 694 | |
Keith Packard | c9fb15f | 2009-05-30 20:42:28 -0700 | [diff] [blame] | 695 | static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder) |
| 696 | { |
| 697 | int dpms = DRM_MODE_DPMS_OFF; |
| 698 | struct drm_connector *connector; |
| 699 | struct drm_device *dev = encoder->dev; |
| 700 | |
| 701 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) |
| 702 | if (connector->encoder == encoder) |
| 703 | if (connector->dpms < dpms) |
| 704 | dpms = connector->dpms; |
| 705 | return dpms; |
| 706 | } |
| 707 | |
| 708 | static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc) |
| 709 | { |
| 710 | int dpms = DRM_MODE_DPMS_OFF; |
| 711 | struct drm_connector *connector; |
| 712 | struct drm_device *dev = crtc->dev; |
| 713 | |
| 714 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) |
| 715 | if (connector->encoder && connector->encoder->crtc == crtc) |
| 716 | if (connector->dpms < dpms) |
| 717 | dpms = connector->dpms; |
| 718 | return dpms; |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * drm_helper_connector_dpms |
| 723 | * @connector affected connector |
| 724 | * @mode DPMS mode |
| 725 | * |
| 726 | * Calls the low-level connector DPMS function, then |
| 727 | * calls appropriate encoder and crtc DPMS functions as well |
| 728 | */ |
| 729 | void drm_helper_connector_dpms(struct drm_connector *connector, int mode) |
| 730 | { |
| 731 | struct drm_encoder *encoder = connector->encoder; |
| 732 | struct drm_crtc *crtc = encoder ? encoder->crtc : NULL; |
| 733 | int old_dpms; |
| 734 | |
| 735 | if (mode == connector->dpms) |
| 736 | return; |
| 737 | |
| 738 | old_dpms = connector->dpms; |
| 739 | connector->dpms = mode; |
| 740 | |
| 741 | /* from off to on, do crtc then encoder */ |
| 742 | if (mode < old_dpms) { |
| 743 | if (crtc) { |
| 744 | struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; |
| 745 | if (crtc_funcs->dpms) |
| 746 | (*crtc_funcs->dpms) (crtc, |
| 747 | drm_helper_choose_crtc_dpms(crtc)); |
| 748 | } |
| 749 | if (encoder) { |
| 750 | struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private; |
| 751 | if (encoder_funcs->dpms) |
| 752 | (*encoder_funcs->dpms) (encoder, |
| 753 | drm_helper_choose_encoder_dpms(encoder)); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | /* from on to off, do encoder then crtc */ |
| 758 | if (mode > old_dpms) { |
| 759 | if (encoder) { |
| 760 | struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private; |
| 761 | if (encoder_funcs->dpms) |
| 762 | (*encoder_funcs->dpms) (encoder, |
| 763 | drm_helper_choose_encoder_dpms(encoder)); |
| 764 | } |
| 765 | if (crtc) { |
| 766 | struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; |
| 767 | if (crtc_funcs->dpms) |
| 768 | (*crtc_funcs->dpms) (crtc, |
| 769 | drm_helper_choose_crtc_dpms(crtc)); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | return; |
| 774 | } |
| 775 | EXPORT_SYMBOL(drm_helper_connector_dpms); |
| 776 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 777 | int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb, |
| 778 | struct drm_mode_fb_cmd *mode_cmd) |
| 779 | { |
| 780 | fb->width = mode_cmd->width; |
| 781 | fb->height = mode_cmd->height; |
| 782 | fb->pitch = mode_cmd->pitch; |
| 783 | fb->bits_per_pixel = mode_cmd->bpp; |
| 784 | fb->depth = mode_cmd->depth; |
| 785 | |
| 786 | return 0; |
| 787 | } |
| 788 | EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct); |
| 789 | |
| 790 | int drm_helper_resume_force_mode(struct drm_device *dev) |
| 791 | { |
| 792 | struct drm_crtc *crtc; |
David John | 89347bb | 2009-12-31 12:00:46 +0530 | [diff] [blame] | 793 | struct drm_encoder *encoder; |
| 794 | struct drm_encoder_helper_funcs *encoder_funcs; |
| 795 | struct drm_crtc_helper_funcs *crtc_funcs; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 796 | int ret; |
| 797 | |
| 798 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
| 799 | |
| 800 | if (!crtc->enabled) |
| 801 | continue; |
| 802 | |
Kristian Høgsberg | 3c4fdcf | 2008-12-17 22:14:46 -0500 | [diff] [blame] | 803 | ret = drm_crtc_helper_set_mode(crtc, &crtc->mode, |
| 804 | crtc->x, crtc->y, crtc->fb); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 805 | |
| 806 | if (ret == false) |
| 807 | DRM_ERROR("failed to set mode on crtc %p\n", crtc); |
David John | 89347bb | 2009-12-31 12:00:46 +0530 | [diff] [blame] | 808 | |
| 809 | /* Turn off outputs that were already powered off */ |
| 810 | if (drm_helper_choose_crtc_dpms(crtc)) { |
| 811 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 812 | |
| 813 | if(encoder->crtc != crtc) |
| 814 | continue; |
| 815 | |
| 816 | encoder_funcs = encoder->helper_private; |
| 817 | if (encoder_funcs->dpms) |
| 818 | (*encoder_funcs->dpms) (encoder, |
| 819 | drm_helper_choose_encoder_dpms(encoder)); |
David John | 89347bb | 2009-12-31 12:00:46 +0530 | [diff] [blame] | 820 | } |
Chris Wilson | 817e631 | 2010-08-06 15:03:31 +0100 | [diff] [blame^] | 821 | |
| 822 | crtc_funcs = crtc->helper_private; |
| 823 | if (crtc_funcs->dpms) |
| 824 | (*crtc_funcs->dpms) (crtc, |
| 825 | drm_helper_choose_crtc_dpms(crtc)); |
David John | 89347bb | 2009-12-31 12:00:46 +0530 | [diff] [blame] | 826 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 827 | } |
Zhao Yakui | af4fcb5 | 2009-07-08 14:13:13 +0800 | [diff] [blame] | 828 | /* disable the unused connectors while restoring the modesetting */ |
| 829 | drm_helper_disable_unused_functions(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 830 | return 0; |
| 831 | } |
| 832 | EXPORT_SYMBOL(drm_helper_resume_force_mode); |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 833 | |
| 834 | static struct slow_work_ops output_poll_ops; |
| 835 | |
| 836 | #define DRM_OUTPUT_POLL_PERIOD (10*HZ) |
| 837 | static void output_poll_execute(struct slow_work *work) |
| 838 | { |
| 839 | struct delayed_slow_work *delayed_work = container_of(work, struct delayed_slow_work, work); |
| 840 | struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_slow_work); |
| 841 | struct drm_connector *connector; |
| 842 | enum drm_connector_status old_status, status; |
| 843 | bool repoll = false, changed = false; |
| 844 | int ret; |
| 845 | |
| 846 | mutex_lock(&dev->mode_config.mutex); |
| 847 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 848 | |
| 849 | /* if this is HPD or polled don't check it - |
| 850 | TV out for instance */ |
| 851 | if (!connector->polled) |
| 852 | continue; |
| 853 | |
| 854 | else if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT)) |
| 855 | repoll = true; |
| 856 | |
| 857 | old_status = connector->status; |
| 858 | /* if we are connected and don't want to poll for disconnect |
| 859 | skip it */ |
| 860 | if (old_status == connector_status_connected && |
| 861 | !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT) && |
| 862 | !(connector->polled & DRM_CONNECTOR_POLL_HPD)) |
| 863 | continue; |
| 864 | |
| 865 | status = connector->funcs->detect(connector); |
| 866 | if (old_status != status) |
| 867 | changed = true; |
| 868 | } |
| 869 | |
| 870 | mutex_unlock(&dev->mode_config.mutex); |
| 871 | |
| 872 | if (changed) { |
| 873 | /* send a uevent + call fbdev */ |
| 874 | drm_sysfs_hotplug_event(dev); |
| 875 | if (dev->mode_config.funcs->output_poll_changed) |
| 876 | dev->mode_config.funcs->output_poll_changed(dev); |
| 877 | } |
| 878 | |
| 879 | if (repoll) { |
| 880 | ret = delayed_slow_work_enqueue(delayed_work, DRM_OUTPUT_POLL_PERIOD); |
| 881 | if (ret) |
| 882 | DRM_ERROR("delayed enqueue failed %d\n", ret); |
| 883 | } |
| 884 | } |
| 885 | |
Dave Airlie | fbf8176 | 2010-06-01 09:09:06 +1000 | [diff] [blame] | 886 | void drm_kms_helper_poll_disable(struct drm_device *dev) |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 887 | { |
Dave Airlie | fbf8176 | 2010-06-01 09:09:06 +1000 | [diff] [blame] | 888 | if (!dev->mode_config.poll_enabled) |
| 889 | return; |
| 890 | delayed_slow_work_cancel(&dev->mode_config.output_poll_slow_work); |
| 891 | } |
| 892 | EXPORT_SYMBOL(drm_kms_helper_poll_disable); |
| 893 | |
| 894 | void drm_kms_helper_poll_enable(struct drm_device *dev) |
| 895 | { |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 896 | bool poll = false; |
Dave Airlie | fbf8176 | 2010-06-01 09:09:06 +1000 | [diff] [blame] | 897 | struct drm_connector *connector; |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 898 | int ret; |
| 899 | |
| 900 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 901 | if (connector->polled) |
| 902 | poll = true; |
| 903 | } |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 904 | |
| 905 | if (poll) { |
| 906 | ret = delayed_slow_work_enqueue(&dev->mode_config.output_poll_slow_work, DRM_OUTPUT_POLL_PERIOD); |
| 907 | if (ret) |
| 908 | DRM_ERROR("delayed enqueue failed %d\n", ret); |
| 909 | } |
| 910 | } |
Dave Airlie | fbf8176 | 2010-06-01 09:09:06 +1000 | [diff] [blame] | 911 | EXPORT_SYMBOL(drm_kms_helper_poll_enable); |
| 912 | |
| 913 | void drm_kms_helper_poll_init(struct drm_device *dev) |
| 914 | { |
| 915 | slow_work_register_user(THIS_MODULE); |
| 916 | delayed_slow_work_init(&dev->mode_config.output_poll_slow_work, |
| 917 | &output_poll_ops); |
| 918 | dev->mode_config.poll_enabled = true; |
| 919 | |
| 920 | drm_kms_helper_poll_enable(dev); |
| 921 | } |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 922 | EXPORT_SYMBOL(drm_kms_helper_poll_init); |
| 923 | |
| 924 | void drm_kms_helper_poll_fini(struct drm_device *dev) |
| 925 | { |
Dave Airlie | fbf8176 | 2010-06-01 09:09:06 +1000 | [diff] [blame] | 926 | drm_kms_helper_poll_disable(dev); |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 927 | slow_work_unregister_user(THIS_MODULE); |
| 928 | } |
| 929 | EXPORT_SYMBOL(drm_kms_helper_poll_fini); |
| 930 | |
| 931 | void drm_helper_hpd_irq_event(struct drm_device *dev) |
| 932 | { |
| 933 | if (!dev->mode_config.poll_enabled) |
| 934 | return; |
| 935 | delayed_slow_work_cancel(&dev->mode_config.output_poll_slow_work); |
| 936 | /* schedule a slow work asap */ |
| 937 | delayed_slow_work_enqueue(&dev->mode_config.output_poll_slow_work, 0); |
| 938 | } |
| 939 | EXPORT_SYMBOL(drm_helper_hpd_irq_event); |
| 940 | |
| 941 | static struct slow_work_ops output_poll_ops = { |
| 942 | .execute = output_poll_execute, |
| 943 | }; |