blob: 231c9433341fe3732dbb7008cf3900c00941803d [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
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 Gortmaker2d1a8a42011-08-30 18:16:33 -040032#include <linux/export.h>
Paul Gortmaker0603ba12011-08-31 11:29:09 -040033#include <linux/moduleparam.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040034
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
36#include <drm/drm_crtc.h>
37#include <drm/drm_fourcc.h>
38#include <drm/drm_crtc_helper.h>
39#include <drm/drm_fb_helper.h>
40#include <drm/drm_edid.h>
Dave Airlief453ba02008-11-07 14:05:41 -080041
Daniel Vetter92b6f892013-10-08 17:44:47 +020042MODULE_AUTHOR("David Airlie, Jesse Barnes");
43MODULE_DESCRIPTION("DRM KMS helper");
44MODULE_LICENSE("GPL and additional rights");
45
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +010046/**
47 * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
48 * connector list
49 * @dev: drm device to operate on
50 *
51 * Some userspace presumes that the first connected connector is the main
52 * display, where it's supposed to display e.g. the login screen. For
53 * laptops, this should be the main panel. Use this function to sort all
54 * (eDP/LVDS) panels to the front of the connector list, instead of
55 * painstakingly trying to initialize them in the right order.
56 */
Daniel Vettercfc1a062012-10-27 15:52:04 +020057void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
58{
59 struct drm_connector *connector, *tmp;
60 struct list_head panel_list;
61
62 INIT_LIST_HEAD(&panel_list);
63
64 list_for_each_entry_safe(connector, tmp,
65 &dev->mode_config.connector_list, head) {
66 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
67 connector->connector_type == DRM_MODE_CONNECTOR_eDP)
68 list_move_tail(&connector->head, &panel_list);
69 }
70
71 list_splice(&panel_list, &dev->mode_config.connector_list);
72}
73EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
74
Dave Airlief453ba02008-11-07 14:05:41 -080075/**
Keith Packardc9fb15f2009-05-30 20:42:28 -070076 * drm_helper_encoder_in_use - check if a given encoder is in use
77 * @encoder: encoder to check
78 *
Daniel Vetter3fcc42e2014-01-23 22:58:26 +010079 * Checks whether @encoder is with the current mode setting output configuration
80 * in use by any connector. This doesn't mean that it is actually enabled since
81 * the DPMS state is tracked separately.
Keith Packardc9fb15f2009-05-30 20:42:28 -070082 *
Daniel Vetter3fcc42e2014-01-23 22:58:26 +010083 * Returns:
84 * True if @encoder is used, false otherwise.
Keith Packardc9fb15f2009-05-30 20:42:28 -070085 */
86bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
87{
88 struct drm_connector *connector;
89 struct drm_device *dev = encoder->dev;
Daniel Vetter62ff94a2014-01-23 22:18:47 +010090
91 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
Keith Packardc9fb15f2009-05-30 20:42:28 -070092 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
93 if (connector->encoder == encoder)
94 return true;
95 return false;
96}
97EXPORT_SYMBOL(drm_helper_encoder_in_use);
98
99/**
Dave Airlief453ba02008-11-07 14:05:41 -0800100 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
101 * @crtc: CRTC to check
102 *
Daniel Vetter3fcc42e2014-01-23 22:58:26 +0100103 * Checks whether @crtc is with the current mode setting output configuration
104 * in use by any connector. This doesn't mean that it is actually enabled since
105 * the DPMS state is tracked separately.
Dave Airlief453ba02008-11-07 14:05:41 -0800106 *
Daniel Vetter3fcc42e2014-01-23 22:58:26 +0100107 * Returns:
108 * True if @crtc is used, false otherwise.
Dave Airlief453ba02008-11-07 14:05:41 -0800109 */
110bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
111{
112 struct drm_encoder *encoder;
113 struct drm_device *dev = crtc->dev;
Daniel Vetter62ff94a2014-01-23 22:18:47 +0100114
115 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
Dave Airlief453ba02008-11-07 14:05:41 -0800116 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
Keith Packardc9fb15f2009-05-30 20:42:28 -0700117 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
Dave Airlief453ba02008-11-07 14:05:41 -0800118 return true;
119 return false;
120}
121EXPORT_SYMBOL(drm_helper_crtc_in_use);
122
Ben Skeggs86a1b9d12010-07-01 16:49:57 +1000123static void
124drm_encoder_disable(struct drm_encoder *encoder)
125{
126 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
127
Sean Paul3b336ec2013-08-14 16:47:37 -0400128 if (encoder->bridge)
129 encoder->bridge->funcs->disable(encoder->bridge);
130
Ben Skeggs86a1b9d12010-07-01 16:49:57 +1000131 if (encoder_funcs->disable)
132 (*encoder_funcs->disable)(encoder);
133 else
134 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
Sean Paul3b336ec2013-08-14 16:47:37 -0400135
136 if (encoder->bridge)
137 encoder->bridge->funcs->post_disable(encoder->bridge);
Ben Skeggs86a1b9d12010-07-01 16:49:57 +1000138}
139
Daniel Vetterb182cc52014-03-20 14:26:34 +0100140static void __drm_helper_disable_unused_functions(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -0800141{
142 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -0800143 struct drm_crtc *crtc;
144
Daniel Vetter62ff94a2014-01-23 22:18:47 +0100145 drm_warn_on_modeset_not_all_locked(dev);
146
Dave Airlief453ba02008-11-07 14:05:41 -0800147 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Dave Airlie929710212010-12-21 12:47:56 +1000148 if (!drm_helper_encoder_in_use(encoder)) {
Ben Skeggs86a1b9d12010-07-01 16:49:57 +1000149 drm_encoder_disable(encoder);
Rob Clarkb5e6c1d2014-05-24 14:30:10 -0400150 /* disconnect encoder from any connector */
Dave Airlie9c552dd2009-09-02 14:00:11 +1000151 encoder->crtc = NULL;
Dave Airliea3a05442009-08-31 15:16:30 +1000152 }
Dave Airlief453ba02008-11-07 14:05:41 -0800153 }
154
155 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
156 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
157 crtc->enabled = drm_helper_crtc_in_use(crtc);
158 if (!crtc->enabled) {
Alex Deucher5c8d7172010-06-11 17:04:35 -0400159 if (crtc_funcs->disable)
160 (*crtc_funcs->disable)(crtc);
161 else
162 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
Matt Roperf4510a22014-04-01 15:22:40 -0700163 crtc->primary->fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800164 }
165 }
166}
Daniel Vetterb182cc52014-03-20 14:26:34 +0100167
168/**
169 * drm_helper_disable_unused_functions - disable unused objects
170 * @dev: DRM device
171 *
172 * This function walks through the entire mode setting configuration of @dev. It
173 * will remove any crtc links of unused encoders and encoder links of
174 * disconnected connectors. Then it will disable all unused encoders and crtcs
175 * either by calling their disable callback if available or by calling their
176 * dpms callback with DRM_MODE_DPMS_OFF.
177 */
178void drm_helper_disable_unused_functions(struct drm_device *dev)
179{
180 drm_modeset_lock_all(dev);
181 __drm_helper_disable_unused_functions(dev);
182 drm_modeset_unlock_all(dev);
183}
Dave Airlief453ba02008-11-07 14:05:41 -0800184EXPORT_SYMBOL(drm_helper_disable_unused_functions);
185
Jesse Barnes7bec7562009-02-23 16:09:34 -0800186/*
187 * Check the CRTC we're going to map each output to vs. its current
188 * CRTC. If they don't match, we have to disable the output and the CRTC
189 * since the driver will have to re-route things.
190 */
191static void
192drm_crtc_prepare_encoders(struct drm_device *dev)
193{
194 struct drm_encoder_helper_funcs *encoder_funcs;
195 struct drm_encoder *encoder;
196
197 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
198 encoder_funcs = encoder->helper_private;
199 /* Disable unused encoders */
200 if (encoder->crtc == NULL)
Ben Skeggs86a1b9d12010-07-01 16:49:57 +1000201 drm_encoder_disable(encoder);
Jesse Barnes7bec7562009-02-23 16:09:34 -0800202 /* Disable encoders whose CRTC is about to change */
203 if (encoder_funcs->get_crtc &&
204 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
Ben Skeggs86a1b9d12010-07-01 16:49:57 +1000205 drm_encoder_disable(encoder);
Jesse Barnes7bec7562009-02-23 16:09:34 -0800206 }
207}
208
Dave Airlief453ba02008-11-07 14:05:41 -0800209/**
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +0100210 * drm_crtc_helper_set_mode - internal helper to set a mode
Dave Airlief453ba02008-11-07 14:05:41 -0800211 * @crtc: CRTC to program
212 * @mode: mode to use
Alex Deucher4c9287c2012-11-09 17:26:32 +0000213 * @x: horizontal offset into the surface
214 * @y: vertical offset into the surface
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +0100215 * @old_fb: old framebuffer, for cleanup
Dave Airlief453ba02008-11-07 14:05:41 -0800216 *
Dave Airlief453ba02008-11-07 14:05:41 -0800217 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +0100218 * to fixup or reject the mode prior to trying to set it. This is an internal
219 * helper that drivers could e.g. use to update properties that require the
220 * entire output pipe to be disabled and re-enabled in a new configuration. For
221 * example for changing whether audio is enabled on a hdmi link or for changing
222 * panel fitter or dither attributes. It is also called by the
223 * drm_crtc_helper_set_config() helper function to drive the mode setting
224 * sequence.
Dave Airlief453ba02008-11-07 14:05:41 -0800225 *
Daniel Vetter3fcc42e2014-01-23 22:58:26 +0100226 * Returns:
227 * True if the mode was set successfully, false otherwise.
Dave Airlief453ba02008-11-07 14:05:41 -0800228 */
229bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
230 struct drm_display_mode *mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500231 int x, int y,
232 struct drm_framebuffer *old_fb)
Dave Airlief453ba02008-11-07 14:05:41 -0800233{
234 struct drm_device *dev = crtc->dev;
Ilija Hadzic7e99acd2013-10-29 11:09:44 -0400235 struct drm_display_mode *adjusted_mode, saved_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800236 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
237 struct drm_encoder_helper_funcs *encoder_funcs;
238 int saved_x, saved_y;
Ilija Hadzic7e99acd2013-10-29 11:09:44 -0400239 bool saved_enabled;
Dave Airlief453ba02008-11-07 14:05:41 -0800240 struct drm_encoder *encoder;
241 bool ret = true;
242
Daniel Vetter62ff94a2014-01-23 22:18:47 +0100243 drm_warn_on_modeset_not_all_locked(dev);
244
Ilija Hadzic7e99acd2013-10-29 11:09:44 -0400245 saved_enabled = crtc->enabled;
Dave Airlief453ba02008-11-07 14:05:41 -0800246 crtc->enabled = drm_helper_crtc_in_use(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800247 if (!crtc->enabled)
248 return true;
249
Chris Wilson021a8452011-01-28 11:31:56 +0000250 adjusted_mode = drm_mode_duplicate(dev, mode);
Ilija Hadzic7e99acd2013-10-29 11:09:44 -0400251 if (!adjusted_mode) {
252 crtc->enabled = saved_enabled;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200253 return false;
Ilija Hadzic7e99acd2013-10-29 11:09:44 -0400254 }
Chris Wilson021a8452011-01-28 11:31:56 +0000255
Dave Airlief453ba02008-11-07 14:05:41 -0800256 saved_mode = crtc->mode;
257 saved_x = crtc->x;
258 saved_y = crtc->y;
259
260 /* Update crtc values up front so the driver can rely on them for mode
261 * setting.
262 */
263 crtc->mode = *mode;
264 crtc->x = x;
265 crtc->y = y;
266
Dave Airlief453ba02008-11-07 14:05:41 -0800267 /* Pass our mode to the connectors and the CRTC to give them a chance to
268 * adjust it according to limitations or connector properties, and also
269 * a chance to reject the mode entirely.
270 */
271 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
272
273 if (encoder->crtc != crtc)
274 continue;
Sean Paul3b336ec2013-08-14 16:47:37 -0400275
276 if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
277 ret = encoder->bridge->funcs->mode_fixup(
278 encoder->bridge, mode, adjusted_mode);
279 if (!ret) {
280 DRM_DEBUG_KMS("Bridge fixup failed\n");
281 goto done;
282 }
283 }
284
Dave Airlief453ba02008-11-07 14:05:41 -0800285 encoder_funcs = encoder->helper_private;
286 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
287 adjusted_mode))) {
Adam Jackson836e53d2011-10-10 16:21:27 -0400288 DRM_DEBUG_KMS("Encoder fixup failed\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800289 goto done;
290 }
291 }
292
293 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
Adam Jackson836e53d2011-10-10 16:21:27 -0400294 DRM_DEBUG_KMS("CRTC fixup failed\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800295 goto done;
296 }
Jerome Glisse94401062010-07-15 15:43:25 -0400297 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -0800298
299 /* Prepare the encoders and CRTCs before setting the mode. */
300 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
301
302 if (encoder->crtc != crtc)
303 continue;
Sean Paul3b336ec2013-08-14 16:47:37 -0400304
305 if (encoder->bridge)
306 encoder->bridge->funcs->disable(encoder->bridge);
307
Dave Airlief453ba02008-11-07 14:05:41 -0800308 encoder_funcs = encoder->helper_private;
309 /* Disable the encoders as the first thing we do. */
310 encoder_funcs->prepare(encoder);
Sean Paul3b336ec2013-08-14 16:47:37 -0400311
312 if (encoder->bridge)
313 encoder->bridge->funcs->post_disable(encoder->bridge);
Dave Airlief453ba02008-11-07 14:05:41 -0800314 }
315
Jesse Barnes7bec7562009-02-23 16:09:34 -0800316 drm_crtc_prepare_encoders(dev);
317
Dave Airlief453ba02008-11-07 14:05:41 -0800318 crtc_funcs->prepare(crtc);
319
320 /* Set up the DPLL and any encoders state that needs to adjust or depend
321 * on the DPLL.
322 */
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000323 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
324 if (!ret)
325 goto done;
Dave Airlief453ba02008-11-07 14:05:41 -0800326
327 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
328
329 if (encoder->crtc != crtc)
330 continue;
331
Jerome Glisse94401062010-07-15 15:43:25 -0400332 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
Jani Nikula83a8cfd2014-06-03 14:56:22 +0300333 encoder->base.id, encoder->name,
Jerome Glisse94401062010-07-15 15:43:25 -0400334 mode->base.id, mode->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800335 encoder_funcs = encoder->helper_private;
336 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
Sean Paul3b336ec2013-08-14 16:47:37 -0400337
338 if (encoder->bridge && encoder->bridge->funcs->mode_set)
339 encoder->bridge->funcs->mode_set(encoder->bridge, mode,
340 adjusted_mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800341 }
342
343 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
344 crtc_funcs->commit(crtc);
345
346 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
347
348 if (encoder->crtc != crtc)
349 continue;
350
Sean Paul3b336ec2013-08-14 16:47:37 -0400351 if (encoder->bridge)
352 encoder->bridge->funcs->pre_enable(encoder->bridge);
353
Dave Airlief453ba02008-11-07 14:05:41 -0800354 encoder_funcs = encoder->helper_private;
355 encoder_funcs->commit(encoder);
356
Sean Paul3b336ec2013-08-14 16:47:37 -0400357 if (encoder->bridge)
358 encoder->bridge->funcs->enable(encoder->bridge);
Dave Airlief453ba02008-11-07 14:05:41 -0800359 }
360
Mario Kleiner27641c32010-10-23 04:20:23 +0200361 /* Store real post-adjustment hardware mode. */
362 crtc->hwmode = *adjusted_mode;
363
364 /* Calculate and store various constants which
365 * are later needed by vblank and swap-completion
366 * timestamping. They are derived from true hwmode.
367 */
Ville Syrjälä545cdd52013-10-26 17:16:30 +0300368 drm_calc_timestamping_constants(crtc, &crtc->hwmode);
Mario Kleiner27641c32010-10-23 04:20:23 +0200369
Dave Airlief453ba02008-11-07 14:05:41 -0800370 /* FIXME: add subpixel order */
371done:
Chris Wilson021a8452011-01-28 11:31:56 +0000372 drm_mode_destroy(dev, adjusted_mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800373 if (!ret) {
Ilija Hadzic7e99acd2013-10-29 11:09:44 -0400374 crtc->enabled = saved_enabled;
Dave Airlief453ba02008-11-07 14:05:41 -0800375 crtc->mode = saved_mode;
376 crtc->x = saved_x;
377 crtc->y = saved_y;
378 }
379
380 return ret;
381}
382EXPORT_SYMBOL(drm_crtc_helper_set_mode);
383
Thierry Redinga74591d2014-04-29 11:44:39 +0200384static void
Chris Wilson6eebd6b2011-11-28 21:10:05 +0000385drm_crtc_helper_disable(struct drm_crtc *crtc)
386{
387 struct drm_device *dev = crtc->dev;
388 struct drm_connector *connector;
389 struct drm_encoder *encoder;
390
391 /* Decouple all encoders and their attached connectors from this crtc */
392 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
393 if (encoder->crtc != crtc)
394 continue;
395
396 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
397 if (connector->encoder != encoder)
398 continue;
399
400 connector->encoder = NULL;
Thierry Redinga6ad6232013-10-02 15:50:06 +0200401
402 /*
403 * drm_helper_disable_unused_functions() ought to be
404 * doing this, but since we've decoupled the encoder
405 * from the connector above, the required connection
406 * between them is henceforth no longer available.
407 */
408 connector->dpms = DRM_MODE_DPMS_OFF;
Chris Wilson6eebd6b2011-11-28 21:10:05 +0000409 }
410 }
411
Daniel Vetterb182cc52014-03-20 14:26:34 +0100412 __drm_helper_disable_unused_functions(dev);
Chris Wilson6eebd6b2011-11-28 21:10:05 +0000413}
414
Dave Airlief453ba02008-11-07 14:05:41 -0800415/**
416 * drm_crtc_helper_set_config - set a new config from userspace
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +0100417 * @set: mode set configuration
Dave Airlief453ba02008-11-07 14:05:41 -0800418 *
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +0100419 * Setup a new configuration, provided by the upper layers (either an ioctl call
Masanari Iidae2278672014-02-18 22:54:36 +0900420 * from userspace or internally e.g. from the fbdev support code) in @set, and
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +0100421 * enable it. This is the main helper functions for drivers that implement
422 * kernel mode setting with the crtc helper functions and the assorted
423 * ->prepare(), ->modeset() and ->commit() helper callbacks.
Dave Airlief453ba02008-11-07 14:05:41 -0800424 *
Daniel Vetter3fcc42e2014-01-23 22:58:26 +0100425 * Returns:
426 * Returns 0 on success, negative errno numbers on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800427 */
428int drm_crtc_helper_set_config(struct drm_mode_set *set)
429{
430 struct drm_device *dev;
Ilija Hadzic02ee4e92013-10-29 11:09:46 -0400431 struct drm_crtc *new_crtc;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200432 struct drm_encoder *save_encoders, *new_encoder, *encoder;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100433 bool mode_changed = false; /* if true do a full mode set */
434 bool fb_changed = false; /* if true and !mode_changed just do a flip */
Maarten Maathuise67aae72009-08-27 10:18:29 +0200435 struct drm_connector *save_connectors, *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800436 int count = 0, ro, fail = 0;
437 struct drm_crtc_helper_funcs *crtc_funcs;
Jesse Barnesc5006cfe2011-11-07 10:39:57 -0800438 struct drm_mode_set save_set;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200439 int ret;
Keith Packardbf9dc102010-11-26 10:45:58 -0800440 int i;
Dave Airlief453ba02008-11-07 14:05:41 -0800441
Zhao Yakui58367ed2009-07-20 13:48:07 +0800442 DRM_DEBUG_KMS("\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800443
Daniel Vettere58de882013-06-15 00:13:11 +0200444 BUG_ON(!set);
445 BUG_ON(!set->crtc);
446 BUG_ON(!set->crtc->helper_private);
Dave Airlief453ba02008-11-07 14:05:41 -0800447
Daniel Vettere58de882013-06-15 00:13:11 +0200448 /* Enforce sane interface api - has been abused by the fb helper. */
449 BUG_ON(!set->mode && set->fb);
450 BUG_ON(set->fb && set->num_connectors == 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800451
452 crtc_funcs = set->crtc->helper_private;
453
Chris Wilsonede3ff52011-01-31 11:16:33 +0000454 if (!set->mode)
455 set->fb = NULL;
456
Jerome Glisse94401062010-07-15 15:43:25 -0400457 if (set->fb) {
458 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
459 set->crtc->base.id, set->fb->base.id,
460 (int)set->num_connectors, set->x, set->y);
461 } else {
Chris Wilsonede3ff52011-01-31 11:16:33 +0000462 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Thierry Redinga74591d2014-04-29 11:44:39 +0200463 drm_crtc_helper_disable(set->crtc);
464 return 0;
Jerome Glisse94401062010-07-15 15:43:25 -0400465 }
Dave Airlief453ba02008-11-07 14:05:41 -0800466
467 dev = set->crtc->dev;
468
Daniel Vetter62ff94a2014-01-23 22:18:47 +0100469 drm_warn_on_modeset_not_all_locked(dev);
470
Ilija Hadzic02ee4e92013-10-29 11:09:46 -0400471 /*
472 * Allocate space for the backup of all (non-pointer) encoder and
473 * connector data.
474 */
Maarten Maathuise67aae72009-08-27 10:18:29 +0200475 save_encoders = kzalloc(dev->mode_config.num_encoder *
476 sizeof(struct drm_encoder), GFP_KERNEL);
Ilija Hadzic02ee4e92013-10-29 11:09:46 -0400477 if (!save_encoders)
Dave Airlief453ba02008-11-07 14:05:41 -0800478 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -0800479
Maarten Maathuise67aae72009-08-27 10:18:29 +0200480 save_connectors = kzalloc(dev->mode_config.num_connector *
481 sizeof(struct drm_connector), GFP_KERNEL);
482 if (!save_connectors) {
Maarten Maathuise67aae72009-08-27 10:18:29 +0200483 kfree(save_encoders);
484 return -ENOMEM;
485 }
486
Ilija Hadzic02ee4e92013-10-29 11:09:46 -0400487 /*
488 * Copy data. Note that driver private data is not affected.
Maarten Maathuise67aae72009-08-27 10:18:29 +0200489 * Should anything bad happen only the expected state is
490 * restored, not the drivers personal bookkeeping.
491 */
492 count = 0;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200493 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
494 save_encoders[count++] = *encoder;
495 }
496
497 count = 0;
498 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
499 save_connectors[count++] = *connector;
500 }
501
Jesse Barnesc5006cfe2011-11-07 10:39:57 -0800502 save_set.crtc = set->crtc;
503 save_set.mode = &set->crtc->mode;
504 save_set.x = set->crtc->x;
505 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -0700506 save_set.fb = set->crtc->primary->fb;
Jesse Barnesc5006cfe2011-11-07 10:39:57 -0800507
Dave Airlief453ba02008-11-07 14:05:41 -0800508 /* We should be able to check here if the fb has the same properties
509 * and then just flip_or_move it */
Matt Roperf4510a22014-04-01 15:22:40 -0700510 if (set->crtc->primary->fb != set->fb) {
Jesse Barnes712531b2009-01-09 13:56:14 -0800511 /* If we have no fb then treat it as a full mode set */
Matt Roperf4510a22014-04-01 15:22:40 -0700512 if (set->crtc->primary->fb == NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800513 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800514 mode_changed = true;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100515 } else if (set->fb == NULL) {
516 mode_changed = true;
Laurent Pinchartce83adf2013-04-22 01:38:47 +0200517 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -0700518 set->crtc->primary->fb->pixel_format) {
Laurent Pinchartce83adf2013-04-22 01:38:47 +0200519 mode_changed = true;
Dave Airlie8dff4742010-02-11 14:28:58 +1000520 } else
Jesse Barnes712531b2009-01-09 13:56:14 -0800521 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800522 }
523
524 if (set->x != set->crtc->x || set->y != set->crtc->y)
Jesse Barnes712531b2009-01-09 13:56:14 -0800525 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800526
527 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800528 DRM_DEBUG_KMS("modes are different, full mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800529 drm_mode_debug_printmodeline(&set->crtc->mode);
530 drm_mode_debug_printmodeline(set->mode);
Jesse Barnes712531b2009-01-09 13:56:14 -0800531 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800532 }
533
534 /* a) traverse passed in connector list and get encoders for them */
535 count = 0;
536 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
537 struct drm_connector_helper_funcs *connector_funcs =
538 connector->helper_private;
Dave Airlief453ba02008-11-07 14:05:41 -0800539 new_encoder = connector->encoder;
540 for (ro = 0; ro < set->num_connectors; ro++) {
541 if (set->connectors[ro] == connector) {
542 new_encoder = connector_funcs->best_encoder(connector);
543 /* if we can't get an encoder for a connector
544 we are setting now - then fail */
545 if (new_encoder == NULL)
546 /* don't break so fail path works correct */
547 fail = 1;
Daniel Vetter25f397a2013-07-19 18:57:11 +0200548
549 if (connector->dpms != DRM_MODE_DPMS_ON) {
550 DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
551 mode_changed = true;
552 }
Daniel Vetter177cf922014-04-01 22:14:59 +0200553
554 break;
Dave Airlief453ba02008-11-07 14:05:41 -0800555 }
556 }
557
558 if (new_encoder != connector->encoder) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800559 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800560 mode_changed = true;
Maarten Maathuisff846ab2009-08-19 00:56:45 +0200561 /* If the encoder is reused for another connector, then
562 * the appropriate crtc will be set later.
563 */
Maarten Maathuisff6fdbe2009-09-01 03:39:04 +0200564 if (connector->encoder)
565 connector->encoder->crtc = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800566 connector->encoder = new_encoder;
567 }
568 }
569
570 if (fail) {
571 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200572 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800573 }
574
575 count = 0;
576 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
577 if (!connector->encoder)
578 continue;
579
Dave Airlief453ba02008-11-07 14:05:41 -0800580 if (connector->encoder->crtc == set->crtc)
581 new_crtc = NULL;
582 else
583 new_crtc = connector->encoder->crtc;
584
585 for (ro = 0; ro < set->num_connectors; ro++) {
586 if (set->connectors[ro] == connector)
587 new_crtc = set->crtc;
588 }
Jesse Barnes7bec7562009-02-23 16:09:34 -0800589
590 /* Make sure the new CRTC will work with the encoder */
591 if (new_crtc &&
592 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
593 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200594 goto fail;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800595 }
Dave Airlief453ba02008-11-07 14:05:41 -0800596 if (new_crtc != connector->encoder->crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800597 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800598 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800599 connector->encoder->crtc = new_crtc;
600 }
Jerome Glisse94401062010-07-15 15:43:25 -0400601 if (new_crtc) {
602 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
Jani Nikula25933822014-06-03 14:56:20 +0300603 connector->base.id, connector->name,
Jerome Glisse94401062010-07-15 15:43:25 -0400604 new_crtc->base.id);
605 } else {
606 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
Jani Nikula25933822014-06-03 14:56:20 +0300607 connector->base.id, connector->name);
Jerome Glisse94401062010-07-15 15:43:25 -0400608 }
Dave Airlief453ba02008-11-07 14:05:41 -0800609 }
610
611 /* mode_set_base is not a required function */
Jesse Barnes712531b2009-01-09 13:56:14 -0800612 if (fb_changed && !crtc_funcs->mode_set_base)
613 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800614
Jesse Barnes712531b2009-01-09 13:56:14 -0800615 if (mode_changed) {
Ilija Hadzic48b1f5d2013-10-29 11:09:45 -0400616 if (drm_helper_crtc_in_use(set->crtc)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800617 DRM_DEBUG_KMS("attempting to set mode from"
618 " userspace\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800619 drm_mode_debug_printmodeline(set->mode);
Matt Roperf4510a22014-04-01 15:22:40 -0700620 set->crtc->primary->fb = set->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800621 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500622 set->x, set->y,
Ilija Hadzicbec2eac2013-10-29 11:09:42 -0400623 save_set.fb)) {
Jerome Glisse94401062010-07-15 15:43:25 -0400624 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
625 set->crtc->base.id);
Matt Roperf4510a22014-04-01 15:22:40 -0700626 set->crtc->primary->fb = save_set.fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800627 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200628 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800629 }
Daniel Vetter25f397a2013-07-19 18:57:11 +0200630 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
631 for (i = 0; i < set->num_connectors; i++) {
632 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
Jani Nikula25933822014-06-03 14:56:20 +0300633 set->connectors[i]->name);
Daniel Vetter25f397a2013-07-19 18:57:11 +0200634 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
635 }
Dave Airlief453ba02008-11-07 14:05:41 -0800636 }
Daniel Vetterb182cc52014-03-20 14:26:34 +0100637 __drm_helper_disable_unused_functions(dev);
Jesse Barnes712531b2009-01-09 13:56:14 -0800638 } else if (fb_changed) {
Ben Skeggs9b1596a2009-09-18 10:43:52 +1000639 set->crtc->x = set->x;
640 set->crtc->y = set->y;
Matt Roperf4510a22014-04-01 15:22:40 -0700641 set->crtc->primary->fb = set->fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000642 ret = crtc_funcs->mode_set_base(set->crtc,
Ilija Hadzicbec2eac2013-10-29 11:09:42 -0400643 set->x, set->y, save_set.fb);
Chris Wilson0ba41e42011-01-08 15:10:41 +0000644 if (ret != 0) {
Ilija Hadzicfbce4062013-10-29 11:09:43 -0400645 set->crtc->x = save_set.x;
646 set->crtc->y = save_set.y;
Matt Roperf4510a22014-04-01 15:22:40 -0700647 set->crtc->primary->fb = save_set.fb;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200648 goto fail;
Chris Wilson0ba41e42011-01-08 15:10:41 +0000649 }
Dave Airlief453ba02008-11-07 14:05:41 -0800650 }
651
Maarten Maathuise67aae72009-08-27 10:18:29 +0200652 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800653 kfree(save_encoders);
Dave Airlief453ba02008-11-07 14:05:41 -0800654 return 0;
655
Maarten Maathuise67aae72009-08-27 10:18:29 +0200656fail:
657 /* Restore all previous data. */
Dave Airlief453ba02008-11-07 14:05:41 -0800658 count = 0;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200659 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
660 *encoder = save_encoders[count++];
Chris Wilsone62fb642009-02-11 16:39:21 +0000661 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200662
Dave Airlief453ba02008-11-07 14:05:41 -0800663 count = 0;
664 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Maarten Maathuise67aae72009-08-27 10:18:29 +0200665 *connector = save_connectors[count++];
Dave Airlief453ba02008-11-07 14:05:41 -0800666 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200667
Jesse Barnesc5006cfe2011-11-07 10:39:57 -0800668 /* Try to restore the config */
669 if (mode_changed &&
670 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
671 save_set.y, save_set.fb))
672 DRM_ERROR("failed to restore config after modeset failure\n");
673
Maarten Maathuise67aae72009-08-27 10:18:29 +0200674 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800675 kfree(save_encoders);
676 return ret;
677}
678EXPORT_SYMBOL(drm_crtc_helper_set_config);
679
Keith Packardc9fb15f2009-05-30 20:42:28 -0700680static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
681{
682 int dpms = DRM_MODE_DPMS_OFF;
683 struct drm_connector *connector;
684 struct drm_device *dev = encoder->dev;
685
686 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
687 if (connector->encoder == encoder)
688 if (connector->dpms < dpms)
689 dpms = connector->dpms;
690 return dpms;
691}
692
Sean Paul3b336ec2013-08-14 16:47:37 -0400693/* Helper which handles bridge ordering around encoder dpms */
694static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
695{
696 struct drm_bridge *bridge = encoder->bridge;
697 struct drm_encoder_helper_funcs *encoder_funcs;
698
699 if (bridge) {
700 if (mode == DRM_MODE_DPMS_ON)
701 bridge->funcs->pre_enable(bridge);
702 else
703 bridge->funcs->disable(bridge);
704 }
705
706 encoder_funcs = encoder->helper_private;
707 if (encoder_funcs->dpms)
708 encoder_funcs->dpms(encoder, mode);
709
710 if (bridge) {
711 if (mode == DRM_MODE_DPMS_ON)
712 bridge->funcs->enable(bridge);
713 else
714 bridge->funcs->post_disable(bridge);
715 }
716}
717
Keith Packardc9fb15f2009-05-30 20:42:28 -0700718static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
719{
720 int dpms = DRM_MODE_DPMS_OFF;
721 struct drm_connector *connector;
722 struct drm_device *dev = crtc->dev;
723
724 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
725 if (connector->encoder && connector->encoder->crtc == crtc)
726 if (connector->dpms < dpms)
727 dpms = connector->dpms;
728 return dpms;
729}
730
731/**
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +0100732 * drm_helper_connector_dpms() - connector dpms helper implementation
733 * @connector: affected connector
734 * @mode: DPMS mode
Keith Packardc9fb15f2009-05-30 20:42:28 -0700735 *
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +0100736 * This is the main helper function provided by the crtc helper framework for
737 * implementing the DPMS connector attribute. It computes the new desired DPMS
738 * state for all encoders and crtcs in the output mesh and calls the ->dpms()
739 * callback provided by the driver appropriately.
Keith Packardc9fb15f2009-05-30 20:42:28 -0700740 */
741void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
742{
743 struct drm_encoder *encoder = connector->encoder;
744 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
Sean Paul3b336ec2013-08-14 16:47:37 -0400745 int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
Keith Packardc9fb15f2009-05-30 20:42:28 -0700746
747 if (mode == connector->dpms)
748 return;
749
750 old_dpms = connector->dpms;
751 connector->dpms = mode;
752
Sean Paul3b336ec2013-08-14 16:47:37 -0400753 if (encoder)
754 encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
755
Keith Packardc9fb15f2009-05-30 20:42:28 -0700756 /* from off to on, do crtc then encoder */
757 if (mode < old_dpms) {
758 if (crtc) {
759 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
760 if (crtc_funcs->dpms)
761 (*crtc_funcs->dpms) (crtc,
762 drm_helper_choose_crtc_dpms(crtc));
763 }
Sean Paul3b336ec2013-08-14 16:47:37 -0400764 if (encoder)
765 drm_helper_encoder_dpms(encoder, encoder_dpms);
Keith Packardc9fb15f2009-05-30 20:42:28 -0700766 }
767
768 /* from on to off, do encoder then crtc */
769 if (mode > old_dpms) {
Sean Paul3b336ec2013-08-14 16:47:37 -0400770 if (encoder)
771 drm_helper_encoder_dpms(encoder, encoder_dpms);
Keith Packardc9fb15f2009-05-30 20:42:28 -0700772 if (crtc) {
773 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
774 if (crtc_funcs->dpms)
775 (*crtc_funcs->dpms) (crtc,
776 drm_helper_choose_crtc_dpms(crtc));
777 }
778 }
779
780 return;
781}
782EXPORT_SYMBOL(drm_helper_connector_dpms);
783
Daniel Vetter3fcc42e2014-01-23 22:58:26 +0100784/**
785 * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
786 * @fb: drm_framebuffer object to fill out
787 * @mode_cmd: metadata from the userspace fb creation request
788 *
789 * This helper can be used in a drivers fb_create callback to pre-fill the fb's
790 * metadata fields.
791 */
Daniel Vetter9fd93782014-01-23 22:16:24 +0100792void drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
793 struct drm_mode_fb_cmd2 *mode_cmd)
Dave Airlief453ba02008-11-07 14:05:41 -0800794{
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200795 int i;
796
Dave Airlief453ba02008-11-07 14:05:41 -0800797 fb->width = mode_cmd->width;
798 fb->height = mode_cmd->height;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200799 for (i = 0; i < 4; i++) {
800 fb->pitches[i] = mode_cmd->pitches[i];
801 fb->offsets[i] = mode_cmd->offsets[i];
802 }
Dave Airlie248dbc22011-11-29 20:02:54 +0000803 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800804 &fb->bits_per_pixel);
805 fb->pixel_format = mode_cmd->pixel_format;
Dave Airlief453ba02008-11-07 14:05:41 -0800806}
807EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
808
Daniel Vetteraa4cd912014-01-22 16:42:02 +0100809/**
810 * drm_helper_resume_force_mode - force-restore mode setting configuration
811 * @dev: drm_device which should be restored
812 *
813 * Drivers which use the mode setting helpers can use this function to
814 * force-restore the mode setting configuration e.g. on resume or when something
815 * else might have trampled over the hw state (like some overzealous old BIOSen
816 * tended to do).
Daniel Vetter00d762c2014-01-23 22:28:30 +0100817 *
818 * This helper doesn't provide a error return value since restoring the old
819 * config should never fail due to resource allocation issues since the driver
820 * has successfully set the restored configuration already. Hence this should
821 * boil down to the equivalent of a few dpms on calls, which also don't provide
822 * an error code.
823 *
824 * Drivers where simply restoring an old configuration again might fail (e.g.
825 * due to slight differences in allocating shared resources when the
826 * configuration is restored in a different order than when userspace set it up)
827 * need to use their own restore logic.
Daniel Vetteraa4cd912014-01-22 16:42:02 +0100828 */
Daniel Vetter00d762c2014-01-23 22:28:30 +0100829void drm_helper_resume_force_mode(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -0800830{
831 struct drm_crtc *crtc;
David John89347bb2009-12-31 12:00:46 +0530832 struct drm_encoder *encoder;
David John89347bb2009-12-31 12:00:46 +0530833 struct drm_crtc_helper_funcs *crtc_funcs;
Daniel Vetter00d762c2014-01-23 22:28:30 +0100834 int encoder_dpms;
835 bool ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800836
Dave Airlie3ea87852014-03-21 10:45:40 +1000837 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800838 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
839
840 if (!crtc->enabled)
841 continue;
842
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500843 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
Matt Roperf4510a22014-04-01 15:22:40 -0700844 crtc->x, crtc->y, crtc->primary->fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800845
Daniel Vetter00d762c2014-01-23 22:28:30 +0100846 /* Restoring the old config should never fail! */
Dave Airlief453ba02008-11-07 14:05:41 -0800847 if (ret == false)
848 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
David John89347bb2009-12-31 12:00:46 +0530849
850 /* Turn off outputs that were already powered off */
851 if (drm_helper_choose_crtc_dpms(crtc)) {
852 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
853
854 if(encoder->crtc != crtc)
855 continue;
856
Sean Paul3b336ec2013-08-14 16:47:37 -0400857 encoder_dpms = drm_helper_choose_encoder_dpms(
858 encoder);
859
860 drm_helper_encoder_dpms(encoder, encoder_dpms);
David John89347bb2009-12-31 12:00:46 +0530861 }
Chris Wilson817e6312010-08-06 15:03:31 +0100862
863 crtc_funcs = crtc->helper_private;
864 if (crtc_funcs->dpms)
865 (*crtc_funcs->dpms) (crtc,
866 drm_helper_choose_crtc_dpms(crtc));
David John89347bb2009-12-31 12:00:46 +0530867 }
Dave Airlief453ba02008-11-07 14:05:41 -0800868 }
Daniel Vetter00d762c2014-01-23 22:28:30 +0100869
Zhao Yakuiaf4fcb52009-07-08 14:13:13 +0800870 /* disable the unused connectors while restoring the modesetting */
Daniel Vetterb182cc52014-03-20 14:26:34 +0100871 __drm_helper_disable_unused_functions(dev);
Dave Airlie3ea87852014-03-21 10:45:40 +1000872 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800873}
874EXPORT_SYMBOL(drm_helper_resume_force_mode);