Merge branch 'msm-next' of git://people.freedesktop.org/~robclark/linux into drm...
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / drm_crtc_helper.c
1 /*
2  * Copyright (c) 2006-2008 Intel Corporation
3  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4  *
5  * DRM core CRTC related functions
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that copyright
10  * notice and this permission notice appear in supporting documentation, and
11  * that the name of the copyright holders not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  The copyright holders make no representations
14  * about the suitability of this software for any purpose.  It is provided "as
15  * is" without express or implied warranty.
16  *
17  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23  * OF THIS SOFTWARE.
24  *
25  * Authors:
26  *      Keith Packard
27  *      Eric Anholt <eric@anholt.net>
28  *      Dave Airlie <airlied@linux.ie>
29  *      Jesse Barnes <jesse.barnes@intel.com>
30  */
31
32 #include <linux/export.h>
33 #include <linux/moduleparam.h>
34
35 #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>
41
42 MODULE_AUTHOR("David Airlie, Jesse Barnes");
43 MODULE_DESCRIPTION("DRM KMS helper");
44 MODULE_LICENSE("GPL and additional rights");
45
46 /**
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  */
57 void 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 }
73 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
74
75 /**
76  * drm_helper_encoder_in_use - check if a given encoder is in use
77  * @encoder: encoder to check
78  *
79  * 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.
82  *
83  * Returns:
84  * True if @encoder is used, false otherwise.
85  */
86 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
87 {
88         struct drm_connector *connector;
89         struct drm_device *dev = encoder->dev;
90
91         WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
92         WARN_ON(!mutex_is_locked(&dev->mode_config.connection_mutex));
93
94         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
95                 if (connector->encoder == encoder)
96                         return true;
97         return false;
98 }
99 EXPORT_SYMBOL(drm_helper_encoder_in_use);
100
101 /**
102  * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
103  * @crtc: CRTC to check
104  *
105  * Checks whether @crtc is with the current mode setting output configuration
106  * in use by any connector. This doesn't mean that it is actually enabled since
107  * the DPMS state is tracked separately.
108  *
109  * Returns:
110  * True if @crtc is used, false otherwise.
111  */
112 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
113 {
114         struct drm_encoder *encoder;
115         struct drm_device *dev = crtc->dev;
116
117         WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
118         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
119                 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
120                         return true;
121         return false;
122 }
123 EXPORT_SYMBOL(drm_helper_crtc_in_use);
124
125 static void
126 drm_encoder_disable(struct drm_encoder *encoder)
127 {
128         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
129
130         if (encoder->bridge)
131                 encoder->bridge->funcs->disable(encoder->bridge);
132
133         if (encoder_funcs->disable)
134                 (*encoder_funcs->disable)(encoder);
135         else
136                 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
137
138         if (encoder->bridge)
139                 encoder->bridge->funcs->post_disable(encoder->bridge);
140 }
141
142 static void __drm_helper_disable_unused_functions(struct drm_device *dev)
143 {
144         struct drm_encoder *encoder;
145         struct drm_crtc *crtc;
146
147         drm_warn_on_modeset_not_all_locked(dev);
148
149         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
150                 if (!drm_helper_encoder_in_use(encoder)) {
151                         drm_encoder_disable(encoder);
152                         /* disconnect encoder from any connector */
153                         encoder->crtc = NULL;
154                 }
155         }
156
157         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
158                 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
159                 crtc->enabled = drm_helper_crtc_in_use(crtc);
160                 if (!crtc->enabled) {
161                         if (crtc_funcs->disable)
162                                 (*crtc_funcs->disable)(crtc);
163                         else
164                                 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
165                         crtc->primary->fb = NULL;
166                 }
167         }
168 }
169
170 /**
171  * drm_helper_disable_unused_functions - disable unused objects
172  * @dev: DRM device
173  *
174  * This function walks through the entire mode setting configuration of @dev. It
175  * will remove any crtc links of unused encoders and encoder links of
176  * disconnected connectors. Then it will disable all unused encoders and crtcs
177  * either by calling their disable callback if available or by calling their
178  * dpms callback with DRM_MODE_DPMS_OFF.
179  */
180 void drm_helper_disable_unused_functions(struct drm_device *dev)
181 {
182         drm_modeset_lock_all(dev);
183         __drm_helper_disable_unused_functions(dev);
184         drm_modeset_unlock_all(dev);
185 }
186 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
187
188 /*
189  * Check the CRTC we're going to map each output to vs. its current
190  * CRTC.  If they don't match, we have to disable the output and the CRTC
191  * since the driver will have to re-route things.
192  */
193 static void
194 drm_crtc_prepare_encoders(struct drm_device *dev)
195 {
196         struct drm_encoder_helper_funcs *encoder_funcs;
197         struct drm_encoder *encoder;
198
199         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
200                 encoder_funcs = encoder->helper_private;
201                 /* Disable unused encoders */
202                 if (encoder->crtc == NULL)
203                         drm_encoder_disable(encoder);
204                 /* Disable encoders whose CRTC is about to change */
205                 if (encoder_funcs->get_crtc &&
206                     encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
207                         drm_encoder_disable(encoder);
208         }
209 }
210
211 /**
212  * drm_crtc_helper_set_mode - internal helper to set a mode
213  * @crtc: CRTC to program
214  * @mode: mode to use
215  * @x: horizontal offset into the surface
216  * @y: vertical offset into the surface
217  * @old_fb: old framebuffer, for cleanup
218  *
219  * Try to set @mode on @crtc.  Give @crtc and its associated connectors a chance
220  * to fixup or reject the mode prior to trying to set it. This is an internal
221  * helper that drivers could e.g. use to update properties that require the
222  * entire output pipe to be disabled and re-enabled in a new configuration. For
223  * example for changing whether audio is enabled on a hdmi link or for changing
224  * panel fitter or dither attributes. It is also called by the
225  * drm_crtc_helper_set_config() helper function to drive the mode setting
226  * sequence.
227  *
228  * Returns:
229  * True if the mode was set successfully, false otherwise.
230  */
231 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
232                               struct drm_display_mode *mode,
233                               int x, int y,
234                               struct drm_framebuffer *old_fb)
235 {
236         struct drm_device *dev = crtc->dev;
237         struct drm_display_mode *adjusted_mode, saved_mode;
238         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
239         struct drm_encoder_helper_funcs *encoder_funcs;
240         int saved_x, saved_y;
241         bool saved_enabled;
242         struct drm_encoder *encoder;
243         bool ret = true;
244
245         drm_warn_on_modeset_not_all_locked(dev);
246
247         saved_enabled = crtc->enabled;
248         crtc->enabled = drm_helper_crtc_in_use(crtc);
249         if (!crtc->enabled)
250                 return true;
251
252         adjusted_mode = drm_mode_duplicate(dev, mode);
253         if (!adjusted_mode) {
254                 crtc->enabled = saved_enabled;
255                 return false;
256         }
257
258         saved_mode = crtc->mode;
259         saved_x = crtc->x;
260         saved_y = crtc->y;
261
262         /* Update crtc values up front so the driver can rely on them for mode
263          * setting.
264          */
265         crtc->mode = *mode;
266         crtc->x = x;
267         crtc->y = y;
268
269         /* Pass our mode to the connectors and the CRTC to give them a chance to
270          * adjust it according to limitations or connector properties, and also
271          * a chance to reject the mode entirely.
272          */
273         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
274
275                 if (encoder->crtc != crtc)
276                         continue;
277
278                 if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
279                         ret = encoder->bridge->funcs->mode_fixup(
280                                         encoder->bridge, mode, adjusted_mode);
281                         if (!ret) {
282                                 DRM_DEBUG_KMS("Bridge fixup failed\n");
283                                 goto done;
284                         }
285                 }
286
287                 encoder_funcs = encoder->helper_private;
288                 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
289                                                       adjusted_mode))) {
290                         DRM_DEBUG_KMS("Encoder fixup failed\n");
291                         goto done;
292                 }
293         }
294
295         if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
296                 DRM_DEBUG_KMS("CRTC fixup failed\n");
297                 goto done;
298         }
299         DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
300
301         /* Prepare the encoders and CRTCs before setting the mode. */
302         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
303
304                 if (encoder->crtc != crtc)
305                         continue;
306
307                 if (encoder->bridge)
308                         encoder->bridge->funcs->disable(encoder->bridge);
309
310                 encoder_funcs = encoder->helper_private;
311                 /* Disable the encoders as the first thing we do. */
312                 encoder_funcs->prepare(encoder);
313
314                 if (encoder->bridge)
315                         encoder->bridge->funcs->post_disable(encoder->bridge);
316         }
317
318         drm_crtc_prepare_encoders(dev);
319
320         crtc_funcs->prepare(crtc);
321
322         /* Set up the DPLL and any encoders state that needs to adjust or depend
323          * on the DPLL.
324          */
325         ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
326         if (!ret)
327             goto done;
328
329         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
330
331                 if (encoder->crtc != crtc)
332                         continue;
333
334                 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
335                         encoder->base.id, encoder->name,
336                         mode->base.id, mode->name);
337                 encoder_funcs = encoder->helper_private;
338                 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
339
340                 if (encoder->bridge && encoder->bridge->funcs->mode_set)
341                         encoder->bridge->funcs->mode_set(encoder->bridge, mode,
342                                         adjusted_mode);
343         }
344
345         /* Now enable the clocks, plane, pipe, and connectors that we set up. */
346         crtc_funcs->commit(crtc);
347
348         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
349
350                 if (encoder->crtc != crtc)
351                         continue;
352
353                 if (encoder->bridge)
354                         encoder->bridge->funcs->pre_enable(encoder->bridge);
355
356                 encoder_funcs = encoder->helper_private;
357                 encoder_funcs->commit(encoder);
358
359                 if (encoder->bridge)
360                         encoder->bridge->funcs->enable(encoder->bridge);
361         }
362
363         /* Store real post-adjustment hardware mode. */
364         crtc->hwmode = *adjusted_mode;
365
366         /* Calculate and store various constants which
367          * are later needed by vblank and swap-completion
368          * timestamping. They are derived from true hwmode.
369          */
370         drm_calc_timestamping_constants(crtc, &crtc->hwmode);
371
372         /* FIXME: add subpixel order */
373 done:
374         drm_mode_destroy(dev, adjusted_mode);
375         if (!ret) {
376                 crtc->enabled = saved_enabled;
377                 crtc->mode = saved_mode;
378                 crtc->x = saved_x;
379                 crtc->y = saved_y;
380         }
381
382         return ret;
383 }
384 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
385
386 static void
387 drm_crtc_helper_disable(struct drm_crtc *crtc)
388 {
389         struct drm_device *dev = crtc->dev;
390         struct drm_connector *connector;
391         struct drm_encoder *encoder;
392
393         /* Decouple all encoders and their attached connectors from this crtc */
394         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
395                 if (encoder->crtc != crtc)
396                         continue;
397
398                 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
399                         if (connector->encoder != encoder)
400                                 continue;
401
402                         connector->encoder = NULL;
403
404                         /*
405                          * drm_helper_disable_unused_functions() ought to be
406                          * doing this, but since we've decoupled the encoder
407                          * from the connector above, the required connection
408                          * between them is henceforth no longer available.
409                          */
410                         connector->dpms = DRM_MODE_DPMS_OFF;
411                 }
412         }
413
414         __drm_helper_disable_unused_functions(dev);
415 }
416
417 /**
418  * drm_crtc_helper_set_config - set a new config from userspace
419  * @set: mode set configuration
420  *
421  * Setup a new configuration, provided by the upper layers (either an ioctl call
422  * from userspace or internally e.g. from the fbdev support code) in @set, and
423  * enable it. This is the main helper functions for drivers that implement
424  * kernel mode setting with the crtc helper functions and the assorted
425  * ->prepare(), ->modeset() and ->commit() helper callbacks.
426  *
427  * Returns:
428  * Returns 0 on success, negative errno numbers on failure.
429  */
430 int drm_crtc_helper_set_config(struct drm_mode_set *set)
431 {
432         struct drm_device *dev;
433         struct drm_crtc *new_crtc;
434         struct drm_encoder *save_encoders, *new_encoder, *encoder;
435         bool mode_changed = false; /* if true do a full mode set */
436         bool fb_changed = false; /* if true and !mode_changed just do a flip */
437         struct drm_connector *save_connectors, *connector;
438         int count = 0, ro, fail = 0;
439         struct drm_crtc_helper_funcs *crtc_funcs;
440         struct drm_mode_set save_set;
441         int ret;
442         int i;
443
444         DRM_DEBUG_KMS("\n");
445
446         BUG_ON(!set);
447         BUG_ON(!set->crtc);
448         BUG_ON(!set->crtc->helper_private);
449
450         /* Enforce sane interface api - has been abused by the fb helper. */
451         BUG_ON(!set->mode && set->fb);
452         BUG_ON(set->fb && set->num_connectors == 0);
453
454         crtc_funcs = set->crtc->helper_private;
455
456         if (!set->mode)
457                 set->fb = NULL;
458
459         if (set->fb) {
460                 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
461                                 set->crtc->base.id, set->fb->base.id,
462                                 (int)set->num_connectors, set->x, set->y);
463         } else {
464                 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
465                 drm_crtc_helper_disable(set->crtc);
466                 return 0;
467         }
468
469         dev = set->crtc->dev;
470
471         drm_warn_on_modeset_not_all_locked(dev);
472
473         /*
474          * Allocate space for the backup of all (non-pointer) encoder and
475          * connector data.
476          */
477         save_encoders = kzalloc(dev->mode_config.num_encoder *
478                                 sizeof(struct drm_encoder), GFP_KERNEL);
479         if (!save_encoders)
480                 return -ENOMEM;
481
482         save_connectors = kzalloc(dev->mode_config.num_connector *
483                                 sizeof(struct drm_connector), GFP_KERNEL);
484         if (!save_connectors) {
485                 kfree(save_encoders);
486                 return -ENOMEM;
487         }
488
489         /*
490          * Copy data. Note that driver private data is not affected.
491          * Should anything bad happen only the expected state is
492          * restored, not the drivers personal bookkeeping.
493          */
494         count = 0;
495         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
496                 save_encoders[count++] = *encoder;
497         }
498
499         count = 0;
500         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
501                 save_connectors[count++] = *connector;
502         }
503
504         save_set.crtc = set->crtc;
505         save_set.mode = &set->crtc->mode;
506         save_set.x = set->crtc->x;
507         save_set.y = set->crtc->y;
508         save_set.fb = set->crtc->primary->fb;
509
510         /* We should be able to check here if the fb has the same properties
511          * and then just flip_or_move it */
512         if (set->crtc->primary->fb != set->fb) {
513                 /* If we have no fb then treat it as a full mode set */
514                 if (set->crtc->primary->fb == NULL) {
515                         DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
516                         mode_changed = true;
517                 } else if (set->fb == NULL) {
518                         mode_changed = true;
519                 } else if (set->fb->pixel_format !=
520                            set->crtc->primary->fb->pixel_format) {
521                         mode_changed = true;
522                 } else
523                         fb_changed = true;
524         }
525
526         if (set->x != set->crtc->x || set->y != set->crtc->y)
527                 fb_changed = true;
528
529         if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
530                 DRM_DEBUG_KMS("modes are different, full mode set\n");
531                 drm_mode_debug_printmodeline(&set->crtc->mode);
532                 drm_mode_debug_printmodeline(set->mode);
533                 mode_changed = true;
534         }
535
536         /* a) traverse passed in connector list and get encoders for them */
537         count = 0;
538         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
539                 struct drm_connector_helper_funcs *connector_funcs =
540                         connector->helper_private;
541                 new_encoder = connector->encoder;
542                 for (ro = 0; ro < set->num_connectors; ro++) {
543                         if (set->connectors[ro] == connector) {
544                                 new_encoder = connector_funcs->best_encoder(connector);
545                                 /* if we can't get an encoder for a connector
546                                    we are setting now - then fail */
547                                 if (new_encoder == NULL)
548                                         /* don't break so fail path works correct */
549                                         fail = 1;
550
551                                 if (connector->dpms != DRM_MODE_DPMS_ON) {
552                                         DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
553                                         mode_changed = true;
554                                 }
555
556                                 break;
557                         }
558                 }
559
560                 if (new_encoder != connector->encoder) {
561                         DRM_DEBUG_KMS("encoder changed, full mode switch\n");
562                         mode_changed = true;
563                         /* If the encoder is reused for another connector, then
564                          * the appropriate crtc will be set later.
565                          */
566                         if (connector->encoder)
567                                 connector->encoder->crtc = NULL;
568                         connector->encoder = new_encoder;
569                 }
570         }
571
572         if (fail) {
573                 ret = -EINVAL;
574                 goto fail;
575         }
576
577         count = 0;
578         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
579                 if (!connector->encoder)
580                         continue;
581
582                 if (connector->encoder->crtc == set->crtc)
583                         new_crtc = NULL;
584                 else
585                         new_crtc = connector->encoder->crtc;
586
587                 for (ro = 0; ro < set->num_connectors; ro++) {
588                         if (set->connectors[ro] == connector)
589                                 new_crtc = set->crtc;
590                 }
591
592                 /* Make sure the new CRTC will work with the encoder */
593                 if (new_crtc &&
594                     !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
595                         ret = -EINVAL;
596                         goto fail;
597                 }
598                 if (new_crtc != connector->encoder->crtc) {
599                         DRM_DEBUG_KMS("crtc changed, full mode switch\n");
600                         mode_changed = true;
601                         connector->encoder->crtc = new_crtc;
602                 }
603                 if (new_crtc) {
604                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
605                                 connector->base.id, connector->name,
606                                 new_crtc->base.id);
607                 } else {
608                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
609                                 connector->base.id, connector->name);
610                 }
611         }
612
613         /* mode_set_base is not a required function */
614         if (fb_changed && !crtc_funcs->mode_set_base)
615                 mode_changed = true;
616
617         if (mode_changed) {
618                 if (drm_helper_crtc_in_use(set->crtc)) {
619                         DRM_DEBUG_KMS("attempting to set mode from"
620                                         " userspace\n");
621                         drm_mode_debug_printmodeline(set->mode);
622                         set->crtc->primary->fb = set->fb;
623                         if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
624                                                       set->x, set->y,
625                                                       save_set.fb)) {
626                                 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
627                                           set->crtc->base.id);
628                                 set->crtc->primary->fb = save_set.fb;
629                                 ret = -EINVAL;
630                                 goto fail;
631                         }
632                         DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
633                         for (i = 0; i < set->num_connectors; i++) {
634                                 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
635                                               set->connectors[i]->name);
636                                 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
637                         }
638                 }
639                 __drm_helper_disable_unused_functions(dev);
640         } else if (fb_changed) {
641                 set->crtc->x = set->x;
642                 set->crtc->y = set->y;
643                 set->crtc->primary->fb = set->fb;
644                 ret = crtc_funcs->mode_set_base(set->crtc,
645                                                 set->x, set->y, save_set.fb);
646                 if (ret != 0) {
647                         set->crtc->x = save_set.x;
648                         set->crtc->y = save_set.y;
649                         set->crtc->primary->fb = save_set.fb;
650                         goto fail;
651                 }
652         }
653
654         kfree(save_connectors);
655         kfree(save_encoders);
656         return 0;
657
658 fail:
659         /* Restore all previous data. */
660         count = 0;
661         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
662                 *encoder = save_encoders[count++];
663         }
664
665         count = 0;
666         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
667                 *connector = save_connectors[count++];
668         }
669
670         /* Try to restore the config */
671         if (mode_changed &&
672             !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
673                                       save_set.y, save_set.fb))
674                 DRM_ERROR("failed to restore config after modeset failure\n");
675
676         kfree(save_connectors);
677         kfree(save_encoders);
678         return ret;
679 }
680 EXPORT_SYMBOL(drm_crtc_helper_set_config);
681
682 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
683 {
684         int dpms = DRM_MODE_DPMS_OFF;
685         struct drm_connector *connector;
686         struct drm_device *dev = encoder->dev;
687
688         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
689                 if (connector->encoder == encoder)
690                         if (connector->dpms < dpms)
691                                 dpms = connector->dpms;
692         return dpms;
693 }
694
695 /* Helper which handles bridge ordering around encoder dpms */
696 static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
697 {
698         struct drm_bridge *bridge = encoder->bridge;
699         struct drm_encoder_helper_funcs *encoder_funcs;
700
701         if (bridge) {
702                 if (mode == DRM_MODE_DPMS_ON)
703                         bridge->funcs->pre_enable(bridge);
704                 else
705                         bridge->funcs->disable(bridge);
706         }
707
708         encoder_funcs = encoder->helper_private;
709         if (encoder_funcs->dpms)
710                 encoder_funcs->dpms(encoder, mode);
711
712         if (bridge) {
713                 if (mode == DRM_MODE_DPMS_ON)
714                         bridge->funcs->enable(bridge);
715                 else
716                         bridge->funcs->post_disable(bridge);
717         }
718 }
719
720 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
721 {
722         int dpms = DRM_MODE_DPMS_OFF;
723         struct drm_connector *connector;
724         struct drm_device *dev = crtc->dev;
725
726         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
727                 if (connector->encoder && connector->encoder->crtc == crtc)
728                         if (connector->dpms < dpms)
729                                 dpms = connector->dpms;
730         return dpms;
731 }
732
733 /**
734  * drm_helper_connector_dpms() - connector dpms helper implementation
735  * @connector: affected connector
736  * @mode: DPMS mode
737  *
738  * This is the main helper function provided by the crtc helper framework for
739  * implementing the DPMS connector attribute. It computes the new desired DPMS
740  * state for all encoders and crtcs in the output mesh and calls the ->dpms()
741  * callback provided by the driver appropriately.
742  */
743 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
744 {
745         struct drm_encoder *encoder = connector->encoder;
746         struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
747         int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
748
749         if (mode == connector->dpms)
750                 return;
751
752         old_dpms = connector->dpms;
753         connector->dpms = mode;
754
755         if (encoder)
756                 encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
757
758         /* from off to on, do crtc then encoder */
759         if (mode < old_dpms) {
760                 if (crtc) {
761                         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
762                         if (crtc_funcs->dpms)
763                                 (*crtc_funcs->dpms) (crtc,
764                                                      drm_helper_choose_crtc_dpms(crtc));
765                 }
766                 if (encoder)
767                         drm_helper_encoder_dpms(encoder, encoder_dpms);
768         }
769
770         /* from on to off, do encoder then crtc */
771         if (mode > old_dpms) {
772                 if (encoder)
773                         drm_helper_encoder_dpms(encoder, encoder_dpms);
774                 if (crtc) {
775                         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
776                         if (crtc_funcs->dpms)
777                                 (*crtc_funcs->dpms) (crtc,
778                                                      drm_helper_choose_crtc_dpms(crtc));
779                 }
780         }
781
782         return;
783 }
784 EXPORT_SYMBOL(drm_helper_connector_dpms);
785
786 /**
787  * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
788  * @fb: drm_framebuffer object to fill out
789  * @mode_cmd: metadata from the userspace fb creation request
790  *
791  * This helper can be used in a drivers fb_create callback to pre-fill the fb's
792  * metadata fields.
793  */
794 void drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
795                                     struct drm_mode_fb_cmd2 *mode_cmd)
796 {
797         int i;
798
799         fb->width = mode_cmd->width;
800         fb->height = mode_cmd->height;
801         for (i = 0; i < 4; i++) {
802                 fb->pitches[i] = mode_cmd->pitches[i];
803                 fb->offsets[i] = mode_cmd->offsets[i];
804         }
805         drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
806                                     &fb->bits_per_pixel);
807         fb->pixel_format = mode_cmd->pixel_format;
808 }
809 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
810
811 /**
812  * drm_helper_resume_force_mode - force-restore mode setting configuration
813  * @dev: drm_device which should be restored
814  *
815  * Drivers which use the mode setting helpers can use this function to
816  * force-restore the mode setting configuration e.g. on resume or when something
817  * else might have trampled over the hw state (like some overzealous old BIOSen
818  * tended to do).
819  *
820  * This helper doesn't provide a error return value since restoring the old
821  * config should never fail due to resource allocation issues since the driver
822  * has successfully set the restored configuration already. Hence this should
823  * boil down to the equivalent of a few dpms on calls, which also don't provide
824  * an error code.
825  *
826  * Drivers where simply restoring an old configuration again might fail (e.g.
827  * due to slight differences in allocating shared resources when the
828  * configuration is restored in a different order than when userspace set it up)
829  * need to use their own restore logic.
830  */
831 void drm_helper_resume_force_mode(struct drm_device *dev)
832 {
833         struct drm_crtc *crtc;
834         struct drm_encoder *encoder;
835         struct drm_crtc_helper_funcs *crtc_funcs;
836         int encoder_dpms;
837         bool ret;
838
839         drm_modeset_lock_all(dev);
840         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
841
842                 if (!crtc->enabled)
843                         continue;
844
845                 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
846                                                crtc->x, crtc->y, crtc->primary->fb);
847
848                 /* Restoring the old config should never fail! */
849                 if (ret == false)
850                         DRM_ERROR("failed to set mode on crtc %p\n", crtc);
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_dpms = drm_helper_choose_encoder_dpms(
860                                                         encoder);
861
862                                 drm_helper_encoder_dpms(encoder, encoder_dpms);
863                         }
864
865                         crtc_funcs = crtc->helper_private;
866                         if (crtc_funcs->dpms)
867                                 (*crtc_funcs->dpms) (crtc,
868                                                      drm_helper_choose_crtc_dpms(crtc));
869                 }
870         }
871
872         /* disable the unused connectors while restoring the modesetting */
873         __drm_helper_disable_unused_functions(dev);
874         drm_modeset_unlock_all(dev);
875 }
876 EXPORT_SYMBOL(drm_helper_resume_force_mode);