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