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