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